]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
Implement P0315R4, Lambdas in unevaluated contexts.
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 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 and 'this' forbidden in "
540 "current context",
541 parser->local_variables_forbidden_p);
542 cp_debug_print_flag (file, "In unbraced linkage specification",
543 parser->in_unbraced_linkage_specification_p);
544 cp_debug_print_flag (file, "Parsing a declarator",
545 parser->in_declarator_p);
546 cp_debug_print_flag (file, "In template argument list",
547 parser->in_template_argument_list_p);
548 cp_debug_print_flag (file, "Parsing an iteration statement",
549 parser->in_statement & IN_ITERATION_STMT);
550 cp_debug_print_flag (file, "Parsing a switch statement",
551 parser->in_statement & IN_SWITCH_STMT);
552 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
553 parser->in_statement & IN_OMP_BLOCK);
554 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
555 parser->in_statement & IN_OMP_FOR);
556 cp_debug_print_flag (file, "Parsing an if statement",
557 parser->in_statement & IN_IF_STMT);
558 cp_debug_print_flag (file, "Parsing a type-id in an expression "
559 "context", parser->in_type_id_in_expr_p);
560 cp_debug_print_flag (file, "String expressions should be translated "
561 "to execution character set",
562 parser->translate_strings_p);
563 cp_debug_print_flag (file, "Parsing function body outside of a "
564 "local class", parser->in_function_body);
565 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
566 parser->colon_corrects_to_scope_p);
567 cp_debug_print_flag (file, "Colon doesn't start a class definition",
568 parser->colon_doesnt_start_class_def_p);
569 if (parser->type_definition_forbidden_message)
570 fprintf (file, "Error message for forbidden type definitions: %s\n",
571 parser->type_definition_forbidden_message);
572 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
573 fprintf (file, "Number of class definitions in progress: %u\n",
574 parser->num_classes_being_defined);
575 fprintf (file, "Number of template parameter lists for the current "
576 "declaration: %u\n", parser->num_template_parameter_lists);
577 cp_debug_parser_tokens (file, parser, window_size);
578 token = parser->lexer->next_token;
579 fprintf (file, "Next token to parse:\n");
580 fprintf (file, "\tToken: ");
581 cp_lexer_print_token (file, token);
582 eloc = expand_location (token->location);
583 fprintf (file, "\n\tFile: %s\n", eloc.file);
584 fprintf (file, "\tLine: %d\n", eloc.line);
585 fprintf (file, "\tColumn: %d\n", eloc.column);
586 }
587
588 DEBUG_FUNCTION void
589 debug (cp_parser &ref)
590 {
591 cp_debug_parser (stderr, &ref);
592 }
593
594 DEBUG_FUNCTION void
595 debug (cp_parser *ptr)
596 {
597 if (ptr)
598 debug (*ptr);
599 else
600 fprintf (stderr, "<nil>\n");
601 }
602
603 /* Allocate memory for a new lexer object and return it. */
604
605 static cp_lexer *
606 cp_lexer_alloc (void)
607 {
608 cp_lexer *lexer;
609
610 c_common_no_more_pch ();
611
612 /* Allocate the memory. */
613 lexer = ggc_cleared_alloc<cp_lexer> ();
614
615 /* Initially we are not debugging. */
616 lexer->debugging_p = false;
617
618 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
619
620 /* Create the buffer. */
621 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
622
623 return lexer;
624 }
625
626
627 /* Create a new main C++ lexer, the lexer that gets tokens from the
628 preprocessor. */
629
630 static cp_lexer *
631 cp_lexer_new_main (void)
632 {
633 cp_lexer *lexer;
634 cp_token token;
635
636 /* It's possible that parsing the first pragma will load a PCH file,
637 which is a GC collection point. So we have to do that before
638 allocating any memory. */
639 cp_parser_initial_pragma (&token);
640
641 lexer = cp_lexer_alloc ();
642
643 /* Put the first token in the buffer. */
644 lexer->buffer->quick_push (token);
645
646 /* Get the remaining tokens from the preprocessor. */
647 while (token.type != CPP_EOF)
648 {
649 cp_lexer_get_preprocessor_token (lexer, &token);
650 vec_safe_push (lexer->buffer, token);
651 }
652
653 lexer->last_token = lexer->buffer->address ()
654 + lexer->buffer->length ()
655 - 1;
656 lexer->next_token = lexer->buffer->length ()
657 ? lexer->buffer->address ()
658 : &eof_token;
659
660 /* Subsequent preprocessor diagnostics should use compiler
661 diagnostic functions to get the compiler source location. */
662 done_lexing = true;
663
664 gcc_assert (!lexer->next_token->purged_p);
665 return lexer;
666 }
667
668 /* Create a new lexer whose token stream is primed with the tokens in
669 CACHE. When these tokens are exhausted, no new tokens will be read. */
670
671 static cp_lexer *
672 cp_lexer_new_from_tokens (cp_token_cache *cache)
673 {
674 cp_token *first = cache->first;
675 cp_token *last = cache->last;
676 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
677
678 /* We do not own the buffer. */
679 lexer->buffer = NULL;
680 lexer->next_token = first == last ? &eof_token : first;
681 lexer->last_token = last;
682
683 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
684
685 /* Initially we are not debugging. */
686 lexer->debugging_p = false;
687
688 gcc_assert (!lexer->next_token->purged_p);
689 return lexer;
690 }
691
692 /* Frees all resources associated with LEXER. */
693
694 static void
695 cp_lexer_destroy (cp_lexer *lexer)
696 {
697 vec_free (lexer->buffer);
698 lexer->saved_tokens.release ();
699 ggc_free (lexer);
700 }
701
702 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
703 be used. The point of this flag is to help the compiler to fold away calls
704 to cp_lexer_debugging_p within this source file at compile time, when the
705 lexer is not being debugged. */
706
707 #define LEXER_DEBUGGING_ENABLED_P false
708
709 /* Returns nonzero if debugging information should be output. */
710
711 static inline bool
712 cp_lexer_debugging_p (cp_lexer *lexer)
713 {
714 if (!LEXER_DEBUGGING_ENABLED_P)
715 return false;
716
717 return lexer->debugging_p;
718 }
719
720
721 static inline cp_token_position
722 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
723 {
724 gcc_assert (!previous_p || lexer->next_token != &eof_token);
725
726 return lexer->next_token - previous_p;
727 }
728
729 static inline cp_token *
730 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
731 {
732 return pos;
733 }
734
735 static inline void
736 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
737 {
738 lexer->next_token = cp_lexer_token_at (lexer, pos);
739 }
740
741 static inline cp_token_position
742 cp_lexer_previous_token_position (cp_lexer *lexer)
743 {
744 if (lexer->next_token == &eof_token)
745 return lexer->last_token - 1;
746 else
747 return cp_lexer_token_position (lexer, true);
748 }
749
750 static inline cp_token *
751 cp_lexer_previous_token (cp_lexer *lexer)
752 {
753 cp_token_position tp = cp_lexer_previous_token_position (lexer);
754
755 /* Skip past purged tokens. */
756 while (tp->purged_p)
757 {
758 gcc_assert (tp != vec_safe_address (lexer->buffer));
759 tp--;
760 }
761
762 return cp_lexer_token_at (lexer, tp);
763 }
764
765 /* nonzero if we are presently saving tokens. */
766
767 static inline int
768 cp_lexer_saving_tokens (const cp_lexer* lexer)
769 {
770 return lexer->saved_tokens.length () != 0;
771 }
772
773 /* Store the next token from the preprocessor in *TOKEN. Return true
774 if we reach EOF. If LEXER is NULL, assume we are handling an
775 initial #pragma pch_preprocess, and thus want the lexer to return
776 processed strings. */
777
778 static void
779 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
780 {
781 static int is_extern_c = 0;
782
783 /* Get a new token from the preprocessor. */
784 token->type
785 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
786 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
787 token->keyword = RID_MAX;
788 token->purged_p = false;
789 token->error_reported = false;
790
791 /* On some systems, some header files are surrounded by an
792 implicit extern "C" block. Set a flag in the token if it
793 comes from such a header. */
794 is_extern_c += pending_lang_change;
795 pending_lang_change = 0;
796 token->implicit_extern_c = is_extern_c > 0;
797
798 /* Check to see if this token is a keyword. */
799 if (token->type == CPP_NAME)
800 {
801 if (IDENTIFIER_KEYWORD_P (token->u.value))
802 {
803 /* Mark this token as a keyword. */
804 token->type = CPP_KEYWORD;
805 /* Record which keyword. */
806 token->keyword = C_RID_CODE (token->u.value);
807 }
808 else
809 {
810 if (warn_cxx11_compat
811 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
812 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
813 {
814 /* Warn about the C++0x keyword (but still treat it as
815 an identifier). */
816 warning (OPT_Wc__11_compat,
817 "identifier %qE is a keyword in C++11",
818 token->u.value);
819
820 /* Clear out the C_RID_CODE so we don't warn about this
821 particular identifier-turned-keyword again. */
822 C_SET_RID_CODE (token->u.value, RID_MAX);
823 }
824
825 token->keyword = RID_MAX;
826 }
827 }
828 else if (token->type == CPP_AT_NAME)
829 {
830 /* This only happens in Objective-C++; it must be a keyword. */
831 token->type = CPP_KEYWORD;
832 switch (C_RID_CODE (token->u.value))
833 {
834 /* Replace 'class' with '@class', 'private' with '@private',
835 etc. This prevents confusion with the C++ keyword
836 'class', and makes the tokens consistent with other
837 Objective-C 'AT' keywords. For example '@class' is
838 reported as RID_AT_CLASS which is consistent with
839 '@synchronized', which is reported as
840 RID_AT_SYNCHRONIZED.
841 */
842 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
843 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
844 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
845 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
846 case RID_THROW: token->keyword = RID_AT_THROW; break;
847 case RID_TRY: token->keyword = RID_AT_TRY; break;
848 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
849 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
850 default: token->keyword = C_RID_CODE (token->u.value);
851 }
852 }
853 }
854
855 /* Update the globals input_location and the input file stack from TOKEN. */
856 static inline void
857 cp_lexer_set_source_position_from_token (cp_token *token)
858 {
859 if (token->type != CPP_EOF)
860 {
861 input_location = token->location;
862 }
863 }
864
865 /* Update the globals input_location and the input file stack from LEXER. */
866 static inline void
867 cp_lexer_set_source_position (cp_lexer *lexer)
868 {
869 cp_token *token = cp_lexer_peek_token (lexer);
870 cp_lexer_set_source_position_from_token (token);
871 }
872
873 /* Return a pointer to the next token in the token stream, but do not
874 consume it. */
875
876 static inline cp_token *
877 cp_lexer_peek_token (cp_lexer *lexer)
878 {
879 if (cp_lexer_debugging_p (lexer))
880 {
881 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
882 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
883 putc ('\n', cp_lexer_debug_stream);
884 }
885 return lexer->next_token;
886 }
887
888 /* Return true if the next token has the indicated TYPE. */
889
890 static inline bool
891 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
892 {
893 return cp_lexer_peek_token (lexer)->type == type;
894 }
895
896 /* Return true if the next token does not have the indicated TYPE. */
897
898 static inline bool
899 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
900 {
901 return !cp_lexer_next_token_is (lexer, type);
902 }
903
904 /* Return true if the next token is the indicated KEYWORD. */
905
906 static inline bool
907 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
908 {
909 return cp_lexer_peek_token (lexer)->keyword == keyword;
910 }
911
912 static inline bool
913 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
914 {
915 return cp_lexer_peek_nth_token (lexer, n)->type == type;
916 }
917
918 static inline bool
919 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
920 {
921 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
922 }
923
924 /* Return true if KEYWORD can start a decl-specifier. */
925
926 bool
927 cp_keyword_starts_decl_specifier_p (enum rid keyword)
928 {
929 switch (keyword)
930 {
931 /* auto specifier: storage-class-specifier in C++,
932 simple-type-specifier in C++0x. */
933 case RID_AUTO:
934 /* Storage classes. */
935 case RID_REGISTER:
936 case RID_STATIC:
937 case RID_EXTERN:
938 case RID_MUTABLE:
939 case RID_THREAD:
940 /* Elaborated type specifiers. */
941 case RID_ENUM:
942 case RID_CLASS:
943 case RID_STRUCT:
944 case RID_UNION:
945 case RID_TYPENAME:
946 /* Simple type specifiers. */
947 case RID_CHAR:
948 case RID_CHAR16:
949 case RID_CHAR32:
950 case RID_WCHAR:
951 case RID_BOOL:
952 case RID_SHORT:
953 case RID_INT:
954 case RID_LONG:
955 case RID_SIGNED:
956 case RID_UNSIGNED:
957 case RID_FLOAT:
958 case RID_DOUBLE:
959 case RID_VOID:
960 /* GNU extensions. */
961 case RID_ATTRIBUTE:
962 case RID_TYPEOF:
963 /* C++0x extensions. */
964 case RID_DECLTYPE:
965 case RID_UNDERLYING_TYPE:
966 case RID_CONSTEXPR:
967 return true;
968
969 default:
970 if (keyword >= RID_FIRST_INT_N
971 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
972 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
973 return true;
974 return false;
975 }
976 }
977
978 /* Return true if the next token is a keyword for a decl-specifier. */
979
980 static bool
981 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
982 {
983 cp_token *token;
984
985 token = cp_lexer_peek_token (lexer);
986 return cp_keyword_starts_decl_specifier_p (token->keyword);
987 }
988
989 /* Returns TRUE iff the token T begins a decltype type. */
990
991 static bool
992 token_is_decltype (cp_token *t)
993 {
994 return (t->keyword == RID_DECLTYPE
995 || t->type == CPP_DECLTYPE);
996 }
997
998 /* Returns TRUE iff the next token begins a decltype type. */
999
1000 static bool
1001 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1002 {
1003 cp_token *t = cp_lexer_peek_token (lexer);
1004 return token_is_decltype (t);
1005 }
1006
1007 /* Called when processing a token with tree_check_value; perform or defer the
1008 associated checks and return the value. */
1009
1010 static tree
1011 saved_checks_value (struct tree_check *check_value)
1012 {
1013 /* Perform any access checks that were deferred. */
1014 vec<deferred_access_check, va_gc> *checks;
1015 deferred_access_check *chk;
1016 checks = check_value->checks;
1017 if (checks)
1018 {
1019 int i;
1020 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1021 perform_or_defer_access_check (chk->binfo,
1022 chk->decl,
1023 chk->diag_decl, tf_warning_or_error);
1024 }
1025 /* Return the stored value. */
1026 return check_value->value;
1027 }
1028
1029 /* Return a pointer to the Nth token in the token stream. If N is 1,
1030 then this is precisely equivalent to cp_lexer_peek_token (except
1031 that it is not inline). One would like to disallow that case, but
1032 there is one case (cp_parser_nth_token_starts_template_id) where
1033 the caller passes a variable for N and it might be 1. */
1034
1035 static cp_token *
1036 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1037 {
1038 cp_token *token;
1039
1040 /* N is 1-based, not zero-based. */
1041 gcc_assert (n > 0);
1042
1043 if (cp_lexer_debugging_p (lexer))
1044 fprintf (cp_lexer_debug_stream,
1045 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1046
1047 --n;
1048 token = lexer->next_token;
1049 gcc_assert (!n || token != &eof_token);
1050 while (n != 0)
1051 {
1052 ++token;
1053 if (token == lexer->last_token)
1054 {
1055 token = &eof_token;
1056 break;
1057 }
1058
1059 if (!token->purged_p)
1060 --n;
1061 }
1062
1063 if (cp_lexer_debugging_p (lexer))
1064 {
1065 cp_lexer_print_token (cp_lexer_debug_stream, token);
1066 putc ('\n', cp_lexer_debug_stream);
1067 }
1068
1069 return token;
1070 }
1071
1072 /* Return the next token, and advance the lexer's next_token pointer
1073 to point to the next non-purged token. */
1074
1075 static cp_token *
1076 cp_lexer_consume_token (cp_lexer* lexer)
1077 {
1078 cp_token *token = lexer->next_token;
1079
1080 gcc_assert (token != &eof_token);
1081 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1082
1083 do
1084 {
1085 lexer->next_token++;
1086 if (lexer->next_token == lexer->last_token)
1087 {
1088 lexer->next_token = &eof_token;
1089 break;
1090 }
1091
1092 }
1093 while (lexer->next_token->purged_p);
1094
1095 cp_lexer_set_source_position_from_token (token);
1096
1097 /* Provide debugging output. */
1098 if (cp_lexer_debugging_p (lexer))
1099 {
1100 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1101 cp_lexer_print_token (cp_lexer_debug_stream, token);
1102 putc ('\n', cp_lexer_debug_stream);
1103 }
1104
1105 return token;
1106 }
1107
1108 /* Permanently remove the next token from the token stream, and
1109 advance the next_token pointer to refer to the next non-purged
1110 token. */
1111
1112 static void
1113 cp_lexer_purge_token (cp_lexer *lexer)
1114 {
1115 cp_token *tok = lexer->next_token;
1116
1117 gcc_assert (tok != &eof_token);
1118 tok->purged_p = true;
1119 tok->location = UNKNOWN_LOCATION;
1120 tok->u.value = NULL_TREE;
1121 tok->keyword = RID_MAX;
1122
1123 do
1124 {
1125 tok++;
1126 if (tok == lexer->last_token)
1127 {
1128 tok = &eof_token;
1129 break;
1130 }
1131 }
1132 while (tok->purged_p);
1133 lexer->next_token = tok;
1134 }
1135
1136 /* Permanently remove all tokens after TOK, up to, but not
1137 including, the token that will be returned next by
1138 cp_lexer_peek_token. */
1139
1140 static void
1141 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1142 {
1143 cp_token *peek = lexer->next_token;
1144
1145 if (peek == &eof_token)
1146 peek = lexer->last_token;
1147
1148 gcc_assert (tok < peek);
1149
1150 for ( tok += 1; tok != peek; tok += 1)
1151 {
1152 tok->purged_p = true;
1153 tok->location = UNKNOWN_LOCATION;
1154 tok->u.value = NULL_TREE;
1155 tok->keyword = RID_MAX;
1156 }
1157 }
1158
1159 /* Begin saving tokens. All tokens consumed after this point will be
1160 preserved. */
1161
1162 static void
1163 cp_lexer_save_tokens (cp_lexer* lexer)
1164 {
1165 /* Provide debugging output. */
1166 if (cp_lexer_debugging_p (lexer))
1167 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1168
1169 lexer->saved_tokens.safe_push (lexer->next_token);
1170 }
1171
1172 /* Commit to the portion of the token stream most recently saved. */
1173
1174 static void
1175 cp_lexer_commit_tokens (cp_lexer* lexer)
1176 {
1177 /* Provide debugging output. */
1178 if (cp_lexer_debugging_p (lexer))
1179 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1180
1181 lexer->saved_tokens.pop ();
1182 }
1183
1184 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1185 to the token stream. Stop saving tokens. */
1186
1187 static void
1188 cp_lexer_rollback_tokens (cp_lexer* lexer)
1189 {
1190 /* Provide debugging output. */
1191 if (cp_lexer_debugging_p (lexer))
1192 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1193
1194 lexer->next_token = lexer->saved_tokens.pop ();
1195 }
1196
1197 /* RAII wrapper around the above functions, with sanity checking. Creating
1198 a variable saves tokens, which are committed when the variable is
1199 destroyed unless they are explicitly rolled back by calling the rollback
1200 member function. */
1201
1202 struct saved_token_sentinel
1203 {
1204 cp_lexer *lexer;
1205 unsigned len;
1206 bool commit;
1207 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1208 {
1209 len = lexer->saved_tokens.length ();
1210 cp_lexer_save_tokens (lexer);
1211 }
1212 void rollback ()
1213 {
1214 cp_lexer_rollback_tokens (lexer);
1215 commit = false;
1216 }
1217 ~saved_token_sentinel()
1218 {
1219 if (commit)
1220 cp_lexer_commit_tokens (lexer);
1221 gcc_assert (lexer->saved_tokens.length () == len);
1222 }
1223 };
1224
1225 /* Print a representation of the TOKEN on the STREAM. */
1226
1227 static void
1228 cp_lexer_print_token (FILE * stream, cp_token *token)
1229 {
1230 /* We don't use cpp_type2name here because the parser defines
1231 a few tokens of its own. */
1232 static const char *const token_names[] = {
1233 /* cpplib-defined token types */
1234 #define OP(e, s) #e,
1235 #define TK(e, s) #e,
1236 TTYPE_TABLE
1237 #undef OP
1238 #undef TK
1239 /* C++ parser token types - see "Manifest constants", above. */
1240 "KEYWORD",
1241 "TEMPLATE_ID",
1242 "NESTED_NAME_SPECIFIER",
1243 };
1244
1245 /* For some tokens, print the associated data. */
1246 switch (token->type)
1247 {
1248 case CPP_KEYWORD:
1249 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1250 For example, `struct' is mapped to an INTEGER_CST. */
1251 if (!identifier_p (token->u.value))
1252 break;
1253 /* fall through */
1254 case CPP_NAME:
1255 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1256 break;
1257
1258 case CPP_STRING:
1259 case CPP_STRING16:
1260 case CPP_STRING32:
1261 case CPP_WSTRING:
1262 case CPP_UTF8STRING:
1263 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1264 break;
1265
1266 case CPP_NUMBER:
1267 print_generic_expr (stream, token->u.value);
1268 break;
1269
1270 default:
1271 /* If we have a name for the token, print it out. Otherwise, we
1272 simply give the numeric code. */
1273 if (token->type < ARRAY_SIZE(token_names))
1274 fputs (token_names[token->type], stream);
1275 else
1276 fprintf (stream, "[%d]", token->type);
1277 break;
1278 }
1279 }
1280
1281 DEBUG_FUNCTION void
1282 debug (cp_token &ref)
1283 {
1284 cp_lexer_print_token (stderr, &ref);
1285 fprintf (stderr, "\n");
1286 }
1287
1288 DEBUG_FUNCTION void
1289 debug (cp_token *ptr)
1290 {
1291 if (ptr)
1292 debug (*ptr);
1293 else
1294 fprintf (stderr, "<nil>\n");
1295 }
1296
1297
1298 /* Start emitting debugging information. */
1299
1300 static void
1301 cp_lexer_start_debugging (cp_lexer* lexer)
1302 {
1303 if (!LEXER_DEBUGGING_ENABLED_P)
1304 fatal_error (input_location,
1305 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1306
1307 lexer->debugging_p = true;
1308 cp_lexer_debug_stream = stderr;
1309 }
1310
1311 /* Stop emitting debugging information. */
1312
1313 static void
1314 cp_lexer_stop_debugging (cp_lexer* lexer)
1315 {
1316 if (!LEXER_DEBUGGING_ENABLED_P)
1317 fatal_error (input_location,
1318 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1319
1320 lexer->debugging_p = false;
1321 cp_lexer_debug_stream = NULL;
1322 }
1323
1324 /* Create a new cp_token_cache, representing a range of tokens. */
1325
1326 static cp_token_cache *
1327 cp_token_cache_new (cp_token *first, cp_token *last)
1328 {
1329 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1330 cache->first = first;
1331 cache->last = last;
1332 return cache;
1333 }
1334
1335 /* Diagnose if #pragma omp declare simd isn't followed immediately
1336 by function declaration or definition. */
1337
1338 static inline void
1339 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1340 {
1341 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1342 {
1343 error ("%<#pragma omp declare simd%> not immediately followed by "
1344 "function declaration or definition");
1345 parser->omp_declare_simd = NULL;
1346 }
1347 }
1348
1349 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1350 and put that into "omp declare simd" attribute. */
1351
1352 static inline void
1353 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1354 {
1355 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1356 {
1357 if (fndecl == error_mark_node)
1358 {
1359 parser->omp_declare_simd = NULL;
1360 return;
1361 }
1362 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1363 {
1364 cp_ensure_no_omp_declare_simd (parser);
1365 return;
1366 }
1367 }
1368 }
1369
1370 /* Diagnose if #pragma acc routine isn't followed immediately by function
1371 declaration or definition. */
1372
1373 static inline void
1374 cp_ensure_no_oacc_routine (cp_parser *parser)
1375 {
1376 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1377 {
1378 error_at (parser->oacc_routine->loc,
1379 "%<#pragma acc routine%> not immediately followed by "
1380 "function declaration or definition");
1381 parser->oacc_routine = NULL;
1382 }
1383 }
1384 \f
1385 /* Decl-specifiers. */
1386
1387 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1388
1389 static void
1390 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1391 {
1392 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1393 }
1394
1395 /* Declarators. */
1396
1397 /* Nothing other than the parser should be creating declarators;
1398 declarators are a semi-syntactic representation of C++ entities.
1399 Other parts of the front end that need to create entities (like
1400 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1401
1402 static cp_declarator *make_call_declarator
1403 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1404 static cp_declarator *make_array_declarator
1405 (cp_declarator *, tree);
1406 static cp_declarator *make_pointer_declarator
1407 (cp_cv_quals, cp_declarator *, tree);
1408 static cp_declarator *make_reference_declarator
1409 (cp_cv_quals, cp_declarator *, bool, tree);
1410 static cp_declarator *make_ptrmem_declarator
1411 (cp_cv_quals, tree, cp_declarator *, tree);
1412
1413 /* An erroneous declarator. */
1414 static cp_declarator *cp_error_declarator;
1415
1416 /* The obstack on which declarators and related data structures are
1417 allocated. */
1418 static struct obstack declarator_obstack;
1419
1420 /* Alloc BYTES from the declarator memory pool. */
1421
1422 static inline void *
1423 alloc_declarator (size_t bytes)
1424 {
1425 return obstack_alloc (&declarator_obstack, bytes);
1426 }
1427
1428 /* Allocate a declarator of the indicated KIND. Clear fields that are
1429 common to all declarators. */
1430
1431 static cp_declarator *
1432 make_declarator (cp_declarator_kind kind)
1433 {
1434 cp_declarator *declarator;
1435
1436 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1437 declarator->kind = kind;
1438 declarator->parenthesized = UNKNOWN_LOCATION;
1439 declarator->attributes = NULL_TREE;
1440 declarator->std_attributes = NULL_TREE;
1441 declarator->declarator = NULL;
1442 declarator->parameter_pack_p = false;
1443 declarator->id_loc = UNKNOWN_LOCATION;
1444
1445 return declarator;
1446 }
1447
1448 /* Make a declarator for a generalized identifier. If
1449 QUALIFYING_SCOPE is non-NULL, the identifier is
1450 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1451 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1452 is, if any. */
1453
1454 static cp_declarator *
1455 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1456 special_function_kind sfk)
1457 {
1458 cp_declarator *declarator;
1459
1460 /* It is valid to write:
1461
1462 class C { void f(); };
1463 typedef C D;
1464 void D::f();
1465
1466 The standard is not clear about whether `typedef const C D' is
1467 legal; as of 2002-09-15 the committee is considering that
1468 question. EDG 3.0 allows that syntax. Therefore, we do as
1469 well. */
1470 if (qualifying_scope && TYPE_P (qualifying_scope))
1471 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1472
1473 gcc_assert (identifier_p (unqualified_name)
1474 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1475 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1476
1477 declarator = make_declarator (cdk_id);
1478 declarator->u.id.qualifying_scope = qualifying_scope;
1479 declarator->u.id.unqualified_name = unqualified_name;
1480 declarator->u.id.sfk = sfk;
1481
1482 return declarator;
1483 }
1484
1485 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1486 of modifiers such as const or volatile to apply to the pointer
1487 type, represented as identifiers. ATTRIBUTES represent the attributes that
1488 appertain to the pointer or reference. */
1489
1490 cp_declarator *
1491 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1492 tree attributes)
1493 {
1494 cp_declarator *declarator;
1495
1496 declarator = make_declarator (cdk_pointer);
1497 declarator->declarator = target;
1498 declarator->u.pointer.qualifiers = cv_qualifiers;
1499 declarator->u.pointer.class_type = NULL_TREE;
1500 if (target)
1501 {
1502 declarator->id_loc = target->id_loc;
1503 declarator->parameter_pack_p = target->parameter_pack_p;
1504 target->parameter_pack_p = false;
1505 }
1506 else
1507 declarator->parameter_pack_p = false;
1508
1509 declarator->std_attributes = attributes;
1510
1511 return declarator;
1512 }
1513
1514 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1515 represent the attributes that appertain to the pointer or
1516 reference. */
1517
1518 cp_declarator *
1519 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1520 bool rvalue_ref, tree attributes)
1521 {
1522 cp_declarator *declarator;
1523
1524 declarator = make_declarator (cdk_reference);
1525 declarator->declarator = target;
1526 declarator->u.reference.qualifiers = cv_qualifiers;
1527 declarator->u.reference.rvalue_ref = rvalue_ref;
1528 if (target)
1529 {
1530 declarator->id_loc = target->id_loc;
1531 declarator->parameter_pack_p = target->parameter_pack_p;
1532 target->parameter_pack_p = false;
1533 }
1534 else
1535 declarator->parameter_pack_p = false;
1536
1537 declarator->std_attributes = attributes;
1538
1539 return declarator;
1540 }
1541
1542 /* Like make_pointer_declarator -- but for a pointer to a non-static
1543 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1544 appertain to the pointer or reference. */
1545
1546 cp_declarator *
1547 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1548 cp_declarator *pointee,
1549 tree attributes)
1550 {
1551 cp_declarator *declarator;
1552
1553 declarator = make_declarator (cdk_ptrmem);
1554 declarator->declarator = pointee;
1555 declarator->u.pointer.qualifiers = cv_qualifiers;
1556 declarator->u.pointer.class_type = class_type;
1557
1558 if (pointee)
1559 {
1560 declarator->parameter_pack_p = pointee->parameter_pack_p;
1561 pointee->parameter_pack_p = false;
1562 }
1563 else
1564 declarator->parameter_pack_p = false;
1565
1566 declarator->std_attributes = attributes;
1567
1568 return declarator;
1569 }
1570
1571 /* Make a declarator for the function given by TARGET, with the
1572 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1573 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1574 indicates what exceptions can be thrown. */
1575
1576 cp_declarator *
1577 make_call_declarator (cp_declarator *target,
1578 tree parms,
1579 cp_cv_quals cv_qualifiers,
1580 cp_virt_specifiers virt_specifiers,
1581 cp_ref_qualifier ref_qualifier,
1582 tree tx_qualifier,
1583 tree exception_specification,
1584 tree late_return_type,
1585 tree requires_clause)
1586 {
1587 cp_declarator *declarator;
1588
1589 declarator = make_declarator (cdk_function);
1590 declarator->declarator = target;
1591 declarator->u.function.parameters = parms;
1592 declarator->u.function.qualifiers = cv_qualifiers;
1593 declarator->u.function.virt_specifiers = virt_specifiers;
1594 declarator->u.function.ref_qualifier = ref_qualifier;
1595 declarator->u.function.tx_qualifier = tx_qualifier;
1596 declarator->u.function.exception_specification = exception_specification;
1597 declarator->u.function.late_return_type = late_return_type;
1598 declarator->u.function.requires_clause = requires_clause;
1599 if (target)
1600 {
1601 declarator->id_loc = target->id_loc;
1602 declarator->parameter_pack_p = target->parameter_pack_p;
1603 target->parameter_pack_p = false;
1604 }
1605 else
1606 declarator->parameter_pack_p = false;
1607
1608 return declarator;
1609 }
1610
1611 /* Make a declarator for an array of BOUNDS elements, each of which is
1612 defined by ELEMENT. */
1613
1614 cp_declarator *
1615 make_array_declarator (cp_declarator *element, tree bounds)
1616 {
1617 cp_declarator *declarator;
1618
1619 declarator = make_declarator (cdk_array);
1620 declarator->declarator = element;
1621 declarator->u.array.bounds = bounds;
1622 if (element)
1623 {
1624 declarator->id_loc = element->id_loc;
1625 declarator->parameter_pack_p = element->parameter_pack_p;
1626 element->parameter_pack_p = false;
1627 }
1628 else
1629 declarator->parameter_pack_p = false;
1630
1631 return declarator;
1632 }
1633
1634 /* Determine whether the declarator we've seen so far can be a
1635 parameter pack, when followed by an ellipsis. */
1636 static bool
1637 declarator_can_be_parameter_pack (cp_declarator *declarator)
1638 {
1639 if (declarator && declarator->parameter_pack_p)
1640 /* We already saw an ellipsis. */
1641 return false;
1642
1643 /* Search for a declarator name, or any other declarator that goes
1644 after the point where the ellipsis could appear in a parameter
1645 pack. If we find any of these, then this declarator can not be
1646 made into a parameter pack. */
1647 bool found = false;
1648 while (declarator && !found)
1649 {
1650 switch ((int)declarator->kind)
1651 {
1652 case cdk_id:
1653 case cdk_array:
1654 case cdk_decomp:
1655 found = true;
1656 break;
1657
1658 case cdk_error:
1659 return true;
1660
1661 default:
1662 declarator = declarator->declarator;
1663 break;
1664 }
1665 }
1666
1667 return !found;
1668 }
1669
1670 cp_parameter_declarator *no_parameters;
1671
1672 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1673 DECLARATOR and DEFAULT_ARGUMENT. */
1674
1675 cp_parameter_declarator *
1676 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1677 cp_declarator *declarator,
1678 tree default_argument,
1679 location_t loc,
1680 bool template_parameter_pack_p = false)
1681 {
1682 cp_parameter_declarator *parameter;
1683
1684 parameter = ((cp_parameter_declarator *)
1685 alloc_declarator (sizeof (cp_parameter_declarator)));
1686 parameter->next = NULL;
1687 if (decl_specifiers)
1688 parameter->decl_specifiers = *decl_specifiers;
1689 else
1690 clear_decl_specs (&parameter->decl_specifiers);
1691 parameter->declarator = declarator;
1692 parameter->default_argument = default_argument;
1693 parameter->template_parameter_pack_p = template_parameter_pack_p;
1694 parameter->loc = loc;
1695
1696 return parameter;
1697 }
1698
1699 /* Returns true iff DECLARATOR is a declaration for a function. */
1700
1701 static bool
1702 function_declarator_p (const cp_declarator *declarator)
1703 {
1704 while (declarator)
1705 {
1706 if (declarator->kind == cdk_function
1707 && declarator->declarator->kind == cdk_id)
1708 return true;
1709 if (declarator->kind == cdk_id
1710 || declarator->kind == cdk_decomp
1711 || declarator->kind == cdk_error)
1712 return false;
1713 declarator = declarator->declarator;
1714 }
1715 return false;
1716 }
1717
1718 /* The parser. */
1719
1720 /* Overview
1721 --------
1722
1723 A cp_parser parses the token stream as specified by the C++
1724 grammar. Its job is purely parsing, not semantic analysis. For
1725 example, the parser breaks the token stream into declarators,
1726 expressions, statements, and other similar syntactic constructs.
1727 It does not check that the types of the expressions on either side
1728 of an assignment-statement are compatible, or that a function is
1729 not declared with a parameter of type `void'.
1730
1731 The parser invokes routines elsewhere in the compiler to perform
1732 semantic analysis and to build up the abstract syntax tree for the
1733 code processed.
1734
1735 The parser (and the template instantiation code, which is, in a
1736 way, a close relative of parsing) are the only parts of the
1737 compiler that should be calling push_scope and pop_scope, or
1738 related functions. The parser (and template instantiation code)
1739 keeps track of what scope is presently active; everything else
1740 should simply honor that. (The code that generates static
1741 initializers may also need to set the scope, in order to check
1742 access control correctly when emitting the initializers.)
1743
1744 Methodology
1745 -----------
1746
1747 The parser is of the standard recursive-descent variety. Upcoming
1748 tokens in the token stream are examined in order to determine which
1749 production to use when parsing a non-terminal. Some C++ constructs
1750 require arbitrary look ahead to disambiguate. For example, it is
1751 impossible, in the general case, to tell whether a statement is an
1752 expression or declaration without scanning the entire statement.
1753 Therefore, the parser is capable of "parsing tentatively." When the
1754 parser is not sure what construct comes next, it enters this mode.
1755 Then, while we attempt to parse the construct, the parser queues up
1756 error messages, rather than issuing them immediately, and saves the
1757 tokens it consumes. If the construct is parsed successfully, the
1758 parser "commits", i.e., it issues any queued error messages and
1759 the tokens that were being preserved are permanently discarded.
1760 If, however, the construct is not parsed successfully, the parser
1761 rolls back its state completely so that it can resume parsing using
1762 a different alternative.
1763
1764 Future Improvements
1765 -------------------
1766
1767 The performance of the parser could probably be improved substantially.
1768 We could often eliminate the need to parse tentatively by looking ahead
1769 a little bit. In some places, this approach might not entirely eliminate
1770 the need to parse tentatively, but it might still speed up the average
1771 case. */
1772
1773 /* Flags that are passed to some parsing functions. These values can
1774 be bitwise-ored together. */
1775
1776 enum
1777 {
1778 /* No flags. */
1779 CP_PARSER_FLAGS_NONE = 0x0,
1780 /* The construct is optional. If it is not present, then no error
1781 should be issued. */
1782 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1783 /* When parsing a type-specifier, treat user-defined type-names
1784 as non-type identifiers. */
1785 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1786 /* When parsing a type-specifier, do not try to parse a class-specifier
1787 or enum-specifier. */
1788 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1789 /* When parsing a decl-specifier-seq, only allow type-specifier or
1790 constexpr. */
1791 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1792 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1793 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1794 };
1795
1796 /* This type is used for parameters and variables which hold
1797 combinations of the above flags. */
1798 typedef int cp_parser_flags;
1799
1800 /* The different kinds of declarators we want to parse. */
1801
1802 enum cp_parser_declarator_kind
1803 {
1804 /* We want an abstract declarator. */
1805 CP_PARSER_DECLARATOR_ABSTRACT,
1806 /* We want a named declarator. */
1807 CP_PARSER_DECLARATOR_NAMED,
1808 /* We don't mind, but the name must be an unqualified-id. */
1809 CP_PARSER_DECLARATOR_EITHER
1810 };
1811
1812 /* The precedence values used to parse binary expressions. The minimum value
1813 of PREC must be 1, because zero is reserved to quickly discriminate
1814 binary operators from other tokens. */
1815
1816 enum cp_parser_prec
1817 {
1818 PREC_NOT_OPERATOR,
1819 PREC_LOGICAL_OR_EXPRESSION,
1820 PREC_LOGICAL_AND_EXPRESSION,
1821 PREC_INCLUSIVE_OR_EXPRESSION,
1822 PREC_EXCLUSIVE_OR_EXPRESSION,
1823 PREC_AND_EXPRESSION,
1824 PREC_EQUALITY_EXPRESSION,
1825 PREC_RELATIONAL_EXPRESSION,
1826 PREC_SHIFT_EXPRESSION,
1827 PREC_ADDITIVE_EXPRESSION,
1828 PREC_MULTIPLICATIVE_EXPRESSION,
1829 PREC_PM_EXPRESSION,
1830 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1831 };
1832
1833 /* A mapping from a token type to a corresponding tree node type, with a
1834 precedence value. */
1835
1836 struct cp_parser_binary_operations_map_node
1837 {
1838 /* The token type. */
1839 enum cpp_ttype token_type;
1840 /* The corresponding tree code. */
1841 enum tree_code tree_type;
1842 /* The precedence of this operator. */
1843 enum cp_parser_prec prec;
1844 };
1845
1846 struct cp_parser_expression_stack_entry
1847 {
1848 /* Left hand side of the binary operation we are currently
1849 parsing. */
1850 cp_expr lhs;
1851 /* Original tree code for left hand side, if it was a binary
1852 expression itself (used for -Wparentheses). */
1853 enum tree_code lhs_type;
1854 /* Tree code for the binary operation we are parsing. */
1855 enum tree_code tree_type;
1856 /* Precedence of the binary operation we are parsing. */
1857 enum cp_parser_prec prec;
1858 /* Location of the binary operation we are parsing. */
1859 location_t loc;
1860 };
1861
1862 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1863 entries because precedence levels on the stack are monotonically
1864 increasing. */
1865 typedef struct cp_parser_expression_stack_entry
1866 cp_parser_expression_stack[NUM_PREC_VALUES];
1867
1868 /* Prototypes. */
1869
1870 /* Constructors and destructors. */
1871
1872 static cp_parser_context *cp_parser_context_new
1873 (cp_parser_context *);
1874
1875 /* Class variables. */
1876
1877 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1878
1879 /* The operator-precedence table used by cp_parser_binary_expression.
1880 Transformed into an associative array (binops_by_token) by
1881 cp_parser_new. */
1882
1883 static const cp_parser_binary_operations_map_node binops[] = {
1884 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1885 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1886
1887 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1888 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1889 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890
1891 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1892 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1893
1894 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1895 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1896
1897 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1898 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1899 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1900 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1901
1902 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1903 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1904
1905 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1906
1907 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1908
1909 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1910
1911 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1912
1913 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1914 };
1915
1916 /* The same as binops, but initialized by cp_parser_new so that
1917 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1918 for speed. */
1919 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1920
1921 /* Constructors and destructors. */
1922
1923 /* Construct a new context. The context below this one on the stack
1924 is given by NEXT. */
1925
1926 static cp_parser_context *
1927 cp_parser_context_new (cp_parser_context* next)
1928 {
1929 cp_parser_context *context;
1930
1931 /* Allocate the storage. */
1932 if (cp_parser_context_free_list != NULL)
1933 {
1934 /* Pull the first entry from the free list. */
1935 context = cp_parser_context_free_list;
1936 cp_parser_context_free_list = context->next;
1937 memset (context, 0, sizeof (*context));
1938 }
1939 else
1940 context = ggc_cleared_alloc<cp_parser_context> ();
1941
1942 /* No errors have occurred yet in this context. */
1943 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1944 /* If this is not the bottommost context, copy information that we
1945 need from the previous context. */
1946 if (next)
1947 {
1948 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1949 expression, then we are parsing one in this context, too. */
1950 context->object_type = next->object_type;
1951 /* Thread the stack. */
1952 context->next = next;
1953 }
1954
1955 return context;
1956 }
1957
1958 /* Managing the unparsed function queues. */
1959
1960 #define unparsed_funs_with_default_args \
1961 parser->unparsed_queues->last ().funs_with_default_args
1962 #define unparsed_funs_with_definitions \
1963 parser->unparsed_queues->last ().funs_with_definitions
1964 #define unparsed_nsdmis \
1965 parser->unparsed_queues->last ().nsdmis
1966 #define unparsed_classes \
1967 parser->unparsed_queues->last ().classes
1968
1969 static void
1970 push_unparsed_function_queues (cp_parser *parser)
1971 {
1972 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1973 vec_safe_push (parser->unparsed_queues, e);
1974 }
1975
1976 static void
1977 pop_unparsed_function_queues (cp_parser *parser)
1978 {
1979 release_tree_vector (unparsed_funs_with_definitions);
1980 parser->unparsed_queues->pop ();
1981 }
1982
1983 /* Prototypes. */
1984
1985 /* Constructors and destructors. */
1986
1987 static cp_parser *cp_parser_new
1988 (void);
1989
1990 /* Routines to parse various constructs.
1991
1992 Those that return `tree' will return the error_mark_node (rather
1993 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1994 Sometimes, they will return an ordinary node if error-recovery was
1995 attempted, even though a parse error occurred. So, to check
1996 whether or not a parse error occurred, you should always use
1997 cp_parser_error_occurred. If the construct is optional (indicated
1998 either by an `_opt' in the name of the function that does the
1999 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2000 the construct is not present. */
2001
2002 /* Lexical conventions [gram.lex] */
2003
2004 static cp_expr cp_parser_identifier
2005 (cp_parser *);
2006 static cp_expr cp_parser_string_literal
2007 (cp_parser *, bool, bool, bool);
2008 static cp_expr cp_parser_userdef_char_literal
2009 (cp_parser *);
2010 static tree cp_parser_userdef_string_literal
2011 (tree);
2012 static cp_expr cp_parser_userdef_numeric_literal
2013 (cp_parser *);
2014
2015 /* Basic concepts [gram.basic] */
2016
2017 static void cp_parser_translation_unit (cp_parser *);
2018
2019 /* Expressions [gram.expr] */
2020
2021 static cp_expr cp_parser_primary_expression
2022 (cp_parser *, bool, bool, bool, cp_id_kind *);
2023 static cp_expr cp_parser_id_expression
2024 (cp_parser *, bool, bool, bool *, bool, bool);
2025 static cp_expr cp_parser_unqualified_id
2026 (cp_parser *, bool, bool, bool, bool);
2027 static tree cp_parser_nested_name_specifier_opt
2028 (cp_parser *, bool, bool, bool, bool, bool = false);
2029 static tree cp_parser_nested_name_specifier
2030 (cp_parser *, bool, bool, bool, bool);
2031 static tree cp_parser_qualifying_entity
2032 (cp_parser *, bool, bool, bool, bool, bool);
2033 static cp_expr cp_parser_postfix_expression
2034 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2035 static tree cp_parser_postfix_open_square_expression
2036 (cp_parser *, tree, bool, bool);
2037 static tree cp_parser_postfix_dot_deref_expression
2038 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2039 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2040 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2041 bool = false);
2042 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2043 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2044 static void cp_parser_pseudo_destructor_name
2045 (cp_parser *, tree, tree *, tree *);
2046 static cp_expr cp_parser_unary_expression
2047 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2048 static enum tree_code cp_parser_unary_operator
2049 (cp_token *);
2050 static tree cp_parser_new_expression
2051 (cp_parser *);
2052 static vec<tree, va_gc> *cp_parser_new_placement
2053 (cp_parser *);
2054 static tree cp_parser_new_type_id
2055 (cp_parser *, tree *);
2056 static cp_declarator *cp_parser_new_declarator_opt
2057 (cp_parser *);
2058 static cp_declarator *cp_parser_direct_new_declarator
2059 (cp_parser *);
2060 static vec<tree, va_gc> *cp_parser_new_initializer
2061 (cp_parser *);
2062 static tree cp_parser_delete_expression
2063 (cp_parser *);
2064 static cp_expr cp_parser_cast_expression
2065 (cp_parser *, bool, bool, bool, cp_id_kind *);
2066 static cp_expr cp_parser_binary_expression
2067 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2068 static tree cp_parser_question_colon_clause
2069 (cp_parser *, cp_expr);
2070 static cp_expr cp_parser_assignment_expression
2071 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2072 static enum tree_code cp_parser_assignment_operator_opt
2073 (cp_parser *);
2074 static cp_expr cp_parser_expression
2075 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2076 static cp_expr cp_parser_constant_expression
2077 (cp_parser *, bool = false, bool * = NULL, bool = false);
2078 static cp_expr cp_parser_builtin_offsetof
2079 (cp_parser *);
2080 static cp_expr cp_parser_lambda_expression
2081 (cp_parser *);
2082 static void cp_parser_lambda_introducer
2083 (cp_parser *, tree);
2084 static bool cp_parser_lambda_declarator_opt
2085 (cp_parser *, tree);
2086 static void cp_parser_lambda_body
2087 (cp_parser *, tree);
2088
2089 /* Statements [gram.stmt.stmt] */
2090
2091 static void cp_parser_statement
2092 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2093 static void cp_parser_label_for_labeled_statement
2094 (cp_parser *, tree);
2095 static tree cp_parser_expression_statement
2096 (cp_parser *, tree);
2097 static tree cp_parser_compound_statement
2098 (cp_parser *, tree, int, bool);
2099 static void cp_parser_statement_seq_opt
2100 (cp_parser *, tree);
2101 static tree cp_parser_selection_statement
2102 (cp_parser *, bool *, vec<tree> *);
2103 static tree cp_parser_condition
2104 (cp_parser *);
2105 static tree cp_parser_iteration_statement
2106 (cp_parser *, bool *, bool, unsigned short);
2107 static bool cp_parser_init_statement
2108 (cp_parser *, tree *decl);
2109 static tree cp_parser_for
2110 (cp_parser *, bool, unsigned short);
2111 static tree cp_parser_c_for
2112 (cp_parser *, tree, tree, bool, unsigned short);
2113 static tree cp_parser_range_for
2114 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2115 static void do_range_for_auto_deduction
2116 (tree, tree);
2117 static tree cp_parser_perform_range_for_lookup
2118 (tree, tree *, tree *);
2119 static tree cp_parser_range_for_member_function
2120 (tree, tree);
2121 static tree cp_parser_jump_statement
2122 (cp_parser *);
2123 static void cp_parser_declaration_statement
2124 (cp_parser *);
2125
2126 static tree cp_parser_implicitly_scoped_statement
2127 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2128 static void cp_parser_already_scoped_statement
2129 (cp_parser *, bool *, const token_indent_info &);
2130
2131 /* Declarations [gram.dcl.dcl] */
2132
2133 static void cp_parser_declaration_seq_opt
2134 (cp_parser *);
2135 static void cp_parser_declaration
2136 (cp_parser *);
2137 static void cp_parser_toplevel_declaration
2138 (cp_parser *);
2139 static void cp_parser_block_declaration
2140 (cp_parser *, bool);
2141 static void cp_parser_simple_declaration
2142 (cp_parser *, bool, tree *);
2143 static void cp_parser_decl_specifier_seq
2144 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2145 static tree cp_parser_storage_class_specifier_opt
2146 (cp_parser *);
2147 static tree cp_parser_function_specifier_opt
2148 (cp_parser *, cp_decl_specifier_seq *);
2149 static tree cp_parser_type_specifier
2150 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2151 int *, bool *);
2152 static tree cp_parser_simple_type_specifier
2153 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2154 static tree cp_parser_type_name
2155 (cp_parser *, bool);
2156 static tree cp_parser_type_name
2157 (cp_parser *);
2158 static tree cp_parser_nonclass_name
2159 (cp_parser* parser);
2160 static tree cp_parser_elaborated_type_specifier
2161 (cp_parser *, bool, bool);
2162 static tree cp_parser_enum_specifier
2163 (cp_parser *);
2164 static void cp_parser_enumerator_list
2165 (cp_parser *, tree);
2166 static void cp_parser_enumerator_definition
2167 (cp_parser *, tree);
2168 static tree cp_parser_namespace_name
2169 (cp_parser *);
2170 static void cp_parser_namespace_definition
2171 (cp_parser *);
2172 static void cp_parser_namespace_body
2173 (cp_parser *);
2174 static tree cp_parser_qualified_namespace_specifier
2175 (cp_parser *);
2176 static void cp_parser_namespace_alias_definition
2177 (cp_parser *);
2178 static bool cp_parser_using_declaration
2179 (cp_parser *, bool);
2180 static void cp_parser_using_directive
2181 (cp_parser *);
2182 static tree cp_parser_alias_declaration
2183 (cp_parser *);
2184 static void cp_parser_asm_definition
2185 (cp_parser *);
2186 static void cp_parser_linkage_specification
2187 (cp_parser *);
2188 static void cp_parser_static_assert
2189 (cp_parser *, bool);
2190 static tree cp_parser_decltype
2191 (cp_parser *);
2192 static tree cp_parser_decomposition_declaration
2193 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2194
2195 /* Declarators [gram.dcl.decl] */
2196
2197 static tree cp_parser_init_declarator
2198 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2199 bool, bool, int, bool *, tree *, location_t *, tree *);
2200 static cp_declarator *cp_parser_declarator
2201 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2202 static cp_declarator *cp_parser_direct_declarator
2203 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2204 static enum tree_code cp_parser_ptr_operator
2205 (cp_parser *, tree *, cp_cv_quals *, tree *);
2206 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2207 (cp_parser *);
2208 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2209 (cp_parser *);
2210 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2211 (cp_parser *);
2212 static tree cp_parser_tx_qualifier_opt
2213 (cp_parser *);
2214 static tree cp_parser_late_return_type_opt
2215 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2216 static tree cp_parser_declarator_id
2217 (cp_parser *, bool);
2218 static tree cp_parser_type_id
2219 (cp_parser *, location_t * = NULL);
2220 static tree cp_parser_template_type_arg
2221 (cp_parser *);
2222 static tree cp_parser_trailing_type_id (cp_parser *);
2223 static tree cp_parser_type_id_1
2224 (cp_parser *, bool, bool, location_t *);
2225 static void cp_parser_type_specifier_seq
2226 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2227 static tree cp_parser_parameter_declaration_clause
2228 (cp_parser *);
2229 static tree cp_parser_parameter_declaration_list
2230 (cp_parser *);
2231 static cp_parameter_declarator *cp_parser_parameter_declaration
2232 (cp_parser *, bool, bool *);
2233 static tree cp_parser_default_argument
2234 (cp_parser *, bool);
2235 static void cp_parser_function_body
2236 (cp_parser *, bool);
2237 static tree cp_parser_initializer
2238 (cp_parser *, bool *, bool *, bool = false);
2239 static cp_expr cp_parser_initializer_clause
2240 (cp_parser *, bool *);
2241 static cp_expr cp_parser_braced_list
2242 (cp_parser*, bool*);
2243 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2244 (cp_parser *, bool *);
2245
2246 static void cp_parser_ctor_initializer_opt_and_function_body
2247 (cp_parser *, bool);
2248
2249 static tree cp_parser_late_parsing_omp_declare_simd
2250 (cp_parser *, tree);
2251
2252 static tree cp_parser_late_parsing_oacc_routine
2253 (cp_parser *, tree);
2254
2255 static tree synthesize_implicit_template_parm
2256 (cp_parser *, tree);
2257 static tree finish_fully_implicit_template
2258 (cp_parser *, tree);
2259 static void abort_fully_implicit_template
2260 (cp_parser *);
2261
2262 /* Classes [gram.class] */
2263
2264 static tree cp_parser_class_name
2265 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2266 static tree cp_parser_class_specifier
2267 (cp_parser *);
2268 static tree cp_parser_class_head
2269 (cp_parser *, bool *);
2270 static enum tag_types cp_parser_class_key
2271 (cp_parser *);
2272 static void cp_parser_type_parameter_key
2273 (cp_parser* parser);
2274 static void cp_parser_member_specification_opt
2275 (cp_parser *);
2276 static void cp_parser_member_declaration
2277 (cp_parser *);
2278 static tree cp_parser_pure_specifier
2279 (cp_parser *);
2280 static tree cp_parser_constant_initializer
2281 (cp_parser *);
2282
2283 /* Derived classes [gram.class.derived] */
2284
2285 static tree cp_parser_base_clause
2286 (cp_parser *);
2287 static tree cp_parser_base_specifier
2288 (cp_parser *);
2289
2290 /* Special member functions [gram.special] */
2291
2292 static tree cp_parser_conversion_function_id
2293 (cp_parser *);
2294 static tree cp_parser_conversion_type_id
2295 (cp_parser *);
2296 static cp_declarator *cp_parser_conversion_declarator_opt
2297 (cp_parser *);
2298 static void cp_parser_ctor_initializer_opt
2299 (cp_parser *);
2300 static void cp_parser_mem_initializer_list
2301 (cp_parser *);
2302 static tree cp_parser_mem_initializer
2303 (cp_parser *);
2304 static tree cp_parser_mem_initializer_id
2305 (cp_parser *);
2306
2307 /* Overloading [gram.over] */
2308
2309 static cp_expr cp_parser_operator_function_id
2310 (cp_parser *);
2311 static cp_expr cp_parser_operator
2312 (cp_parser *);
2313
2314 /* Templates [gram.temp] */
2315
2316 static void cp_parser_template_declaration
2317 (cp_parser *, bool);
2318 static tree cp_parser_template_parameter_list
2319 (cp_parser *);
2320 static tree cp_parser_template_parameter
2321 (cp_parser *, bool *, bool *);
2322 static tree cp_parser_type_parameter
2323 (cp_parser *, bool *);
2324 static tree cp_parser_template_id
2325 (cp_parser *, bool, bool, enum tag_types, bool);
2326 static tree cp_parser_template_name
2327 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2328 static tree cp_parser_template_argument_list
2329 (cp_parser *);
2330 static tree cp_parser_template_argument
2331 (cp_parser *);
2332 static void cp_parser_explicit_instantiation
2333 (cp_parser *);
2334 static void cp_parser_explicit_specialization
2335 (cp_parser *);
2336
2337 /* Exception handling [gram.exception] */
2338
2339 static tree cp_parser_try_block
2340 (cp_parser *);
2341 static void cp_parser_function_try_block
2342 (cp_parser *);
2343 static void cp_parser_handler_seq
2344 (cp_parser *);
2345 static void cp_parser_handler
2346 (cp_parser *);
2347 static tree cp_parser_exception_declaration
2348 (cp_parser *);
2349 static tree cp_parser_throw_expression
2350 (cp_parser *);
2351 static tree cp_parser_exception_specification_opt
2352 (cp_parser *);
2353 static tree cp_parser_type_id_list
2354 (cp_parser *);
2355
2356 /* GNU Extensions */
2357
2358 static tree cp_parser_asm_specification_opt
2359 (cp_parser *);
2360 static tree cp_parser_asm_operand_list
2361 (cp_parser *);
2362 static tree cp_parser_asm_clobber_list
2363 (cp_parser *);
2364 static tree cp_parser_asm_label_list
2365 (cp_parser *);
2366 static bool cp_next_tokens_can_be_attribute_p
2367 (cp_parser *);
2368 static bool cp_next_tokens_can_be_gnu_attribute_p
2369 (cp_parser *);
2370 static bool cp_next_tokens_can_be_std_attribute_p
2371 (cp_parser *);
2372 static bool cp_nth_tokens_can_be_std_attribute_p
2373 (cp_parser *, size_t);
2374 static bool cp_nth_tokens_can_be_gnu_attribute_p
2375 (cp_parser *, size_t);
2376 static bool cp_nth_tokens_can_be_attribute_p
2377 (cp_parser *, size_t);
2378 static tree cp_parser_attributes_opt
2379 (cp_parser *);
2380 static tree cp_parser_gnu_attributes_opt
2381 (cp_parser *);
2382 static tree cp_parser_gnu_attribute_list
2383 (cp_parser *);
2384 static tree cp_parser_std_attribute
2385 (cp_parser *, tree);
2386 static tree cp_parser_std_attribute_spec
2387 (cp_parser *);
2388 static tree cp_parser_std_attribute_spec_seq
2389 (cp_parser *);
2390 static size_t cp_parser_skip_attributes_opt
2391 (cp_parser *, size_t);
2392 static bool cp_parser_extension_opt
2393 (cp_parser *, int *);
2394 static void cp_parser_label_declaration
2395 (cp_parser *);
2396
2397 /* Concept Extensions */
2398
2399 static tree cp_parser_requires_clause
2400 (cp_parser *);
2401 static tree cp_parser_requires_clause_opt
2402 (cp_parser *);
2403 static tree cp_parser_requires_expression
2404 (cp_parser *);
2405 static tree cp_parser_requirement_parameter_list
2406 (cp_parser *);
2407 static tree cp_parser_requirement_body
2408 (cp_parser *);
2409 static tree cp_parser_requirement_list
2410 (cp_parser *);
2411 static tree cp_parser_requirement
2412 (cp_parser *);
2413 static tree cp_parser_simple_requirement
2414 (cp_parser *);
2415 static tree cp_parser_compound_requirement
2416 (cp_parser *);
2417 static tree cp_parser_type_requirement
2418 (cp_parser *);
2419 static tree cp_parser_nested_requirement
2420 (cp_parser *);
2421
2422 /* Transactional Memory Extensions */
2423
2424 static tree cp_parser_transaction
2425 (cp_parser *, cp_token *);
2426 static tree cp_parser_transaction_expression
2427 (cp_parser *, enum rid);
2428 static void cp_parser_function_transaction
2429 (cp_parser *, enum rid);
2430 static tree cp_parser_transaction_cancel
2431 (cp_parser *);
2432
2433 enum pragma_context {
2434 pragma_external,
2435 pragma_member,
2436 pragma_objc_icode,
2437 pragma_stmt,
2438 pragma_compound
2439 };
2440 static bool cp_parser_pragma
2441 (cp_parser *, enum pragma_context, bool *);
2442
2443 /* Objective-C++ Productions */
2444
2445 static tree cp_parser_objc_message_receiver
2446 (cp_parser *);
2447 static tree cp_parser_objc_message_args
2448 (cp_parser *);
2449 static tree cp_parser_objc_message_expression
2450 (cp_parser *);
2451 static cp_expr cp_parser_objc_encode_expression
2452 (cp_parser *);
2453 static tree cp_parser_objc_defs_expression
2454 (cp_parser *);
2455 static tree cp_parser_objc_protocol_expression
2456 (cp_parser *);
2457 static tree cp_parser_objc_selector_expression
2458 (cp_parser *);
2459 static cp_expr cp_parser_objc_expression
2460 (cp_parser *);
2461 static bool cp_parser_objc_selector_p
2462 (enum cpp_ttype);
2463 static tree cp_parser_objc_selector
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_refs_opt
2466 (cp_parser *);
2467 static void cp_parser_objc_declaration
2468 (cp_parser *, tree);
2469 static tree cp_parser_objc_statement
2470 (cp_parser *);
2471 static bool cp_parser_objc_valid_prefix_attributes
2472 (cp_parser *, tree *);
2473 static void cp_parser_objc_at_property_declaration
2474 (cp_parser *) ;
2475 static void cp_parser_objc_at_synthesize_declaration
2476 (cp_parser *) ;
2477 static void cp_parser_objc_at_dynamic_declaration
2478 (cp_parser *) ;
2479 static tree cp_parser_objc_struct_declaration
2480 (cp_parser *) ;
2481
2482 /* Utility Routines */
2483
2484 static cp_expr cp_parser_lookup_name
2485 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2486 static tree cp_parser_lookup_name_simple
2487 (cp_parser *, tree, location_t);
2488 static tree cp_parser_maybe_treat_template_as_class
2489 (tree, bool);
2490 static bool cp_parser_check_declarator_template_parameters
2491 (cp_parser *, cp_declarator *, location_t);
2492 static bool cp_parser_check_template_parameters
2493 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2494 static cp_expr cp_parser_simple_cast_expression
2495 (cp_parser *);
2496 static tree cp_parser_global_scope_opt
2497 (cp_parser *, bool);
2498 static bool cp_parser_constructor_declarator_p
2499 (cp_parser *, bool);
2500 static tree cp_parser_function_definition_from_specifiers_and_declarator
2501 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2502 static tree cp_parser_function_definition_after_declarator
2503 (cp_parser *, bool);
2504 static bool cp_parser_template_declaration_after_export
2505 (cp_parser *, bool);
2506 static void cp_parser_perform_template_parameter_access_checks
2507 (vec<deferred_access_check, va_gc> *);
2508 static tree cp_parser_single_declaration
2509 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2510 static cp_expr cp_parser_functional_cast
2511 (cp_parser *, tree);
2512 static tree cp_parser_save_member_function_body
2513 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2514 static tree cp_parser_save_nsdmi
2515 (cp_parser *);
2516 static tree cp_parser_enclosed_template_argument_list
2517 (cp_parser *);
2518 static void cp_parser_save_default_args
2519 (cp_parser *, tree);
2520 static void cp_parser_late_parsing_for_member
2521 (cp_parser *, tree);
2522 static tree cp_parser_late_parse_one_default_arg
2523 (cp_parser *, tree, tree, tree);
2524 static void cp_parser_late_parsing_nsdmi
2525 (cp_parser *, tree);
2526 static void cp_parser_late_parsing_default_args
2527 (cp_parser *, tree);
2528 static tree cp_parser_sizeof_operand
2529 (cp_parser *, enum rid);
2530 static cp_expr cp_parser_trait_expr
2531 (cp_parser *, enum rid);
2532 static bool cp_parser_declares_only_class_p
2533 (cp_parser *);
2534 static void cp_parser_set_storage_class
2535 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2536 static void cp_parser_set_decl_spec_type
2537 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2538 static void set_and_check_decl_spec_loc
2539 (cp_decl_specifier_seq *decl_specs,
2540 cp_decl_spec ds, cp_token *);
2541 static bool cp_parser_friend_p
2542 (const cp_decl_specifier_seq *);
2543 static void cp_parser_required_error
2544 (cp_parser *, required_token, bool, location_t);
2545 static cp_token *cp_parser_require
2546 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2547 static cp_token *cp_parser_require_keyword
2548 (cp_parser *, enum rid, required_token);
2549 static bool cp_parser_token_starts_function_definition_p
2550 (cp_token *);
2551 static bool cp_parser_next_token_starts_class_definition_p
2552 (cp_parser *);
2553 static bool cp_parser_next_token_ends_template_argument_p
2554 (cp_parser *);
2555 static bool cp_parser_nth_token_starts_template_argument_list_p
2556 (cp_parser *, size_t);
2557 static enum tag_types cp_parser_token_is_class_key
2558 (cp_token *);
2559 static enum tag_types cp_parser_token_is_type_parameter_key
2560 (cp_token *);
2561 static void cp_parser_check_class_key
2562 (enum tag_types, tree type);
2563 static void cp_parser_check_access_in_redeclaration
2564 (tree type, location_t location);
2565 static bool cp_parser_optional_template_keyword
2566 (cp_parser *);
2567 static void cp_parser_pre_parsed_nested_name_specifier
2568 (cp_parser *);
2569 static bool cp_parser_cache_group
2570 (cp_parser *, enum cpp_ttype, unsigned);
2571 static tree cp_parser_cache_defarg
2572 (cp_parser *parser, bool nsdmi);
2573 static void cp_parser_parse_tentatively
2574 (cp_parser *);
2575 static void cp_parser_commit_to_tentative_parse
2576 (cp_parser *);
2577 static void cp_parser_commit_to_topmost_tentative_parse
2578 (cp_parser *);
2579 static void cp_parser_abort_tentative_parse
2580 (cp_parser *);
2581 static bool cp_parser_parse_definitely
2582 (cp_parser *);
2583 static inline bool cp_parser_parsing_tentatively
2584 (cp_parser *);
2585 static bool cp_parser_uncommitted_to_tentative_parse_p
2586 (cp_parser *);
2587 static void cp_parser_error
2588 (cp_parser *, const char *);
2589 static void cp_parser_name_lookup_error
2590 (cp_parser *, tree, tree, name_lookup_error, location_t);
2591 static bool cp_parser_simulate_error
2592 (cp_parser *);
2593 static bool cp_parser_check_type_definition
2594 (cp_parser *);
2595 static void cp_parser_check_for_definition_in_return_type
2596 (cp_declarator *, tree, location_t type_location);
2597 static void cp_parser_check_for_invalid_template_id
2598 (cp_parser *, tree, enum tag_types, location_t location);
2599 static bool cp_parser_non_integral_constant_expression
2600 (cp_parser *, non_integral_constant);
2601 static void cp_parser_diagnose_invalid_type_name
2602 (cp_parser *, tree, location_t);
2603 static bool cp_parser_parse_and_diagnose_invalid_type_name
2604 (cp_parser *);
2605 static int cp_parser_skip_to_closing_parenthesis
2606 (cp_parser *, bool, bool, bool);
2607 static void cp_parser_skip_to_end_of_statement
2608 (cp_parser *);
2609 static void cp_parser_consume_semicolon_at_end_of_statement
2610 (cp_parser *);
2611 static void cp_parser_skip_to_end_of_block_or_statement
2612 (cp_parser *);
2613 static bool cp_parser_skip_to_closing_brace
2614 (cp_parser *);
2615 static void cp_parser_skip_to_end_of_template_parameter_list
2616 (cp_parser *);
2617 static void cp_parser_skip_to_pragma_eol
2618 (cp_parser*, cp_token *);
2619 static bool cp_parser_error_occurred
2620 (cp_parser *);
2621 static bool cp_parser_allow_gnu_extensions_p
2622 (cp_parser *);
2623 static bool cp_parser_is_pure_string_literal
2624 (cp_token *);
2625 static bool cp_parser_is_string_literal
2626 (cp_token *);
2627 static bool cp_parser_is_keyword
2628 (cp_token *, enum rid);
2629 static tree cp_parser_make_typename_type
2630 (cp_parser *, tree, location_t location);
2631 static cp_declarator * cp_parser_make_indirect_declarator
2632 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2633 static bool cp_parser_compound_literal_p
2634 (cp_parser *);
2635 static bool cp_parser_array_designator_p
2636 (cp_parser *);
2637 static bool cp_parser_init_statement_p
2638 (cp_parser *);
2639 static bool cp_parser_skip_to_closing_square_bracket
2640 (cp_parser *);
2641
2642 /* Concept-related syntactic transformations */
2643
2644 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2645 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2646
2647 // -------------------------------------------------------------------------- //
2648 // Unevaluated Operand Guard
2649 //
2650 // Implementation of an RAII helper for unevaluated operand parsing.
2651 cp_unevaluated::cp_unevaluated ()
2652 {
2653 ++cp_unevaluated_operand;
2654 ++c_inhibit_evaluation_warnings;
2655 }
2656
2657 cp_unevaluated::~cp_unevaluated ()
2658 {
2659 --c_inhibit_evaluation_warnings;
2660 --cp_unevaluated_operand;
2661 }
2662
2663 // -------------------------------------------------------------------------- //
2664 // Tentative Parsing
2665
2666 /* Returns nonzero if we are parsing tentatively. */
2667
2668 static inline bool
2669 cp_parser_parsing_tentatively (cp_parser* parser)
2670 {
2671 return parser->context->next != NULL;
2672 }
2673
2674 /* Returns nonzero if TOKEN is a string literal. */
2675
2676 static bool
2677 cp_parser_is_pure_string_literal (cp_token* token)
2678 {
2679 return (token->type == CPP_STRING ||
2680 token->type == CPP_STRING16 ||
2681 token->type == CPP_STRING32 ||
2682 token->type == CPP_WSTRING ||
2683 token->type == CPP_UTF8STRING);
2684 }
2685
2686 /* Returns nonzero if TOKEN is a string literal
2687 of a user-defined string literal. */
2688
2689 static bool
2690 cp_parser_is_string_literal (cp_token* token)
2691 {
2692 return (cp_parser_is_pure_string_literal (token) ||
2693 token->type == CPP_STRING_USERDEF ||
2694 token->type == CPP_STRING16_USERDEF ||
2695 token->type == CPP_STRING32_USERDEF ||
2696 token->type == CPP_WSTRING_USERDEF ||
2697 token->type == CPP_UTF8STRING_USERDEF);
2698 }
2699
2700 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2701
2702 static bool
2703 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2704 {
2705 return token->keyword == keyword;
2706 }
2707
2708 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2709 PRAGMA_NONE. */
2710
2711 static enum pragma_kind
2712 cp_parser_pragma_kind (cp_token *token)
2713 {
2714 if (token->type != CPP_PRAGMA)
2715 return PRAGMA_NONE;
2716 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2717 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2718 }
2719
2720 /* Helper function for cp_parser_error.
2721 Having peeked a token of kind TOK1_KIND that might signify
2722 a conflict marker, peek successor tokens to determine
2723 if we actually do have a conflict marker.
2724 Specifically, we consider a run of 7 '<', '=' or '>' characters
2725 at the start of a line as a conflict marker.
2726 These come through the lexer as three pairs and a single,
2727 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2728 If it returns true, *OUT_LOC is written to with the location/range
2729 of the marker. */
2730
2731 static bool
2732 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2733 location_t *out_loc)
2734 {
2735 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2736 if (token2->type != tok1_kind)
2737 return false;
2738 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2739 if (token3->type != tok1_kind)
2740 return false;
2741 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2742 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2743 return false;
2744
2745 /* It must be at the start of the line. */
2746 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2747 if (LOCATION_COLUMN (start_loc) != 1)
2748 return false;
2749
2750 /* We have a conflict marker. Construct a location of the form:
2751 <<<<<<<
2752 ^~~~~~~
2753 with start == caret, finishing at the end of the marker. */
2754 location_t finish_loc = get_finish (token4->location);
2755 *out_loc = make_location (start_loc, start_loc, finish_loc);
2756
2757 return true;
2758 }
2759
2760 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2761 RT_CLOSE_PAREN. */
2762
2763 static const char *
2764 get_matching_symbol (required_token token_desc)
2765 {
2766 switch (token_desc)
2767 {
2768 default:
2769 gcc_unreachable ();
2770 return "";
2771 case RT_CLOSE_BRACE:
2772 return "{";
2773 case RT_CLOSE_PAREN:
2774 return "(";
2775 }
2776 }
2777
2778 /* Attempt to convert TOKEN_DESC from a required_token to an
2779 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2780
2781 static enum cpp_ttype
2782 get_required_cpp_ttype (required_token token_desc)
2783 {
2784 switch (token_desc)
2785 {
2786 case RT_SEMICOLON:
2787 return CPP_SEMICOLON;
2788 case RT_OPEN_PAREN:
2789 return CPP_OPEN_PAREN;
2790 case RT_CLOSE_BRACE:
2791 return CPP_CLOSE_BRACE;
2792 case RT_OPEN_BRACE:
2793 return CPP_OPEN_BRACE;
2794 case RT_CLOSE_SQUARE:
2795 return CPP_CLOSE_SQUARE;
2796 case RT_OPEN_SQUARE:
2797 return CPP_OPEN_SQUARE;
2798 case RT_COMMA:
2799 return CPP_COMMA;
2800 case RT_COLON:
2801 return CPP_COLON;
2802 case RT_CLOSE_PAREN:
2803 return CPP_CLOSE_PAREN;
2804
2805 default:
2806 /* Use CPP_EOF as a "no completions possible" code. */
2807 return CPP_EOF;
2808 }
2809 }
2810
2811
2812 /* Subroutine of cp_parser_error and cp_parser_required_error.
2813
2814 Issue a diagnostic of the form
2815 FILE:LINE: MESSAGE before TOKEN
2816 where TOKEN is the next token in the input stream. MESSAGE
2817 (specified by the caller) is usually of the form "expected
2818 OTHER-TOKEN".
2819
2820 This bypasses the check for tentative passing, and potentially
2821 adds material needed by cp_parser_required_error.
2822
2823 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2824 suggesting insertion of the missing token.
2825
2826 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2827 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2828 location. */
2829
2830 static void
2831 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2832 required_token missing_token_desc,
2833 location_t matching_location)
2834 {
2835 cp_token *token = cp_lexer_peek_token (parser->lexer);
2836 /* This diagnostic makes more sense if it is tagged to the line
2837 of the token we just peeked at. */
2838 cp_lexer_set_source_position_from_token (token);
2839
2840 if (token->type == CPP_PRAGMA)
2841 {
2842 error_at (token->location,
2843 "%<#pragma%> is not allowed here");
2844 cp_parser_skip_to_pragma_eol (parser, token);
2845 return;
2846 }
2847
2848 /* If this is actually a conflict marker, report it as such. */
2849 if (token->type == CPP_LSHIFT
2850 || token->type == CPP_RSHIFT
2851 || token->type == CPP_EQ_EQ)
2852 {
2853 location_t loc;
2854 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2855 {
2856 error_at (loc, "version control conflict marker in file");
2857 expanded_location token_exploc = expand_location (token->location);
2858 /* Consume tokens until the end of the source line. */
2859 while (1)
2860 {
2861 cp_lexer_consume_token (parser->lexer);
2862 cp_token *next = cp_lexer_peek_token (parser->lexer);
2863 if (next == NULL)
2864 break;
2865 expanded_location next_exploc = expand_location (next->location);
2866 if (next_exploc.file != token_exploc.file)
2867 break;
2868 if (next_exploc.line != token_exploc.line)
2869 break;
2870 }
2871 return;
2872 }
2873 }
2874
2875 gcc_rich_location richloc (input_location);
2876
2877 bool added_matching_location = false;
2878
2879 if (missing_token_desc != RT_NONE)
2880 {
2881 /* Potentially supply a fix-it hint, suggesting to add the
2882 missing token immediately after the *previous* token.
2883 This may move the primary location within richloc. */
2884 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2885 location_t prev_token_loc
2886 = cp_lexer_previous_token (parser->lexer)->location;
2887 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2888
2889 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2890 Attempt to consolidate diagnostics by printing it as a
2891 secondary range within the main diagnostic. */
2892 if (matching_location != UNKNOWN_LOCATION)
2893 added_matching_location
2894 = richloc.add_location_if_nearby (matching_location);
2895 }
2896
2897 /* Actually emit the error. */
2898 c_parse_error (gmsgid,
2899 /* Because c_parser_error does not understand
2900 CPP_KEYWORD, keywords are treated like
2901 identifiers. */
2902 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2903 token->u.value, token->flags, &richloc);
2904
2905 if (missing_token_desc != RT_NONE)
2906 {
2907 /* If we weren't able to consolidate matching_location, then
2908 print it as a secondary diagnostic. */
2909 if (matching_location != UNKNOWN_LOCATION
2910 && !added_matching_location)
2911 inform (matching_location, "to match this %qs",
2912 get_matching_symbol (missing_token_desc));
2913 }
2914 }
2915
2916 /* If not parsing tentatively, issue a diagnostic of the form
2917 FILE:LINE: MESSAGE before TOKEN
2918 where TOKEN is the next token in the input stream. MESSAGE
2919 (specified by the caller) is usually of the form "expected
2920 OTHER-TOKEN". */
2921
2922 static void
2923 cp_parser_error (cp_parser* parser, const char* gmsgid)
2924 {
2925 if (!cp_parser_simulate_error (parser))
2926 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2927 }
2928
2929 /* Issue an error about name-lookup failing. NAME is the
2930 IDENTIFIER_NODE DECL is the result of
2931 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2932 the thing that we hoped to find. */
2933
2934 static void
2935 cp_parser_name_lookup_error (cp_parser* parser,
2936 tree name,
2937 tree decl,
2938 name_lookup_error desired,
2939 location_t location)
2940 {
2941 /* If name lookup completely failed, tell the user that NAME was not
2942 declared. */
2943 if (decl == error_mark_node)
2944 {
2945 if (parser->scope && parser->scope != global_namespace)
2946 error_at (location, "%<%E::%E%> has not been declared",
2947 parser->scope, name);
2948 else if (parser->scope == global_namespace)
2949 error_at (location, "%<::%E%> has not been declared", name);
2950 else if (parser->object_scope
2951 && !CLASS_TYPE_P (parser->object_scope))
2952 error_at (location, "request for member %qE in non-class type %qT",
2953 name, parser->object_scope);
2954 else if (parser->object_scope)
2955 error_at (location, "%<%T::%E%> has not been declared",
2956 parser->object_scope, name);
2957 else
2958 error_at (location, "%qE has not been declared", name);
2959 }
2960 else if (parser->scope && parser->scope != global_namespace)
2961 {
2962 switch (desired)
2963 {
2964 case NLE_TYPE:
2965 error_at (location, "%<%E::%E%> is not a type",
2966 parser->scope, name);
2967 break;
2968 case NLE_CXX98:
2969 error_at (location, "%<%E::%E%> is not a class or namespace",
2970 parser->scope, name);
2971 break;
2972 case NLE_NOT_CXX98:
2973 error_at (location,
2974 "%<%E::%E%> is not a class, namespace, or enumeration",
2975 parser->scope, name);
2976 break;
2977 default:
2978 gcc_unreachable ();
2979
2980 }
2981 }
2982 else if (parser->scope == global_namespace)
2983 {
2984 switch (desired)
2985 {
2986 case NLE_TYPE:
2987 error_at (location, "%<::%E%> is not a type", name);
2988 break;
2989 case NLE_CXX98:
2990 error_at (location, "%<::%E%> is not a class or namespace", name);
2991 break;
2992 case NLE_NOT_CXX98:
2993 error_at (location,
2994 "%<::%E%> is not a class, namespace, or enumeration",
2995 name);
2996 break;
2997 default:
2998 gcc_unreachable ();
2999 }
3000 }
3001 else
3002 {
3003 switch (desired)
3004 {
3005 case NLE_TYPE:
3006 error_at (location, "%qE is not a type", name);
3007 break;
3008 case NLE_CXX98:
3009 error_at (location, "%qE is not a class or namespace", name);
3010 break;
3011 case NLE_NOT_CXX98:
3012 error_at (location,
3013 "%qE is not a class, namespace, or enumeration", name);
3014 break;
3015 default:
3016 gcc_unreachable ();
3017 }
3018 }
3019 }
3020
3021 /* If we are parsing tentatively, remember that an error has occurred
3022 during this tentative parse. Returns true if the error was
3023 simulated; false if a message should be issued by the caller. */
3024
3025 static bool
3026 cp_parser_simulate_error (cp_parser* parser)
3027 {
3028 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3029 {
3030 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3031 return true;
3032 }
3033 return false;
3034 }
3035
3036 /* This function is called when a type is defined. If type
3037 definitions are forbidden at this point, an error message is
3038 issued. */
3039
3040 static bool
3041 cp_parser_check_type_definition (cp_parser* parser)
3042 {
3043 /* If types are forbidden here, issue a message. */
3044 if (parser->type_definition_forbidden_message)
3045 {
3046 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3047 in the message need to be interpreted. */
3048 error (parser->type_definition_forbidden_message);
3049 return false;
3050 }
3051 return true;
3052 }
3053
3054 /* This function is called when the DECLARATOR is processed. The TYPE
3055 was a type defined in the decl-specifiers. If it is invalid to
3056 define a type in the decl-specifiers for DECLARATOR, an error is
3057 issued. TYPE_LOCATION is the location of TYPE and is used
3058 for error reporting. */
3059
3060 static void
3061 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3062 tree type, location_t type_location)
3063 {
3064 /* [dcl.fct] forbids type definitions in return types.
3065 Unfortunately, it's not easy to know whether or not we are
3066 processing a return type until after the fact. */
3067 while (declarator
3068 && (declarator->kind == cdk_pointer
3069 || declarator->kind == cdk_reference
3070 || declarator->kind == cdk_ptrmem))
3071 declarator = declarator->declarator;
3072 if (declarator
3073 && declarator->kind == cdk_function)
3074 {
3075 error_at (type_location,
3076 "new types may not be defined in a return type");
3077 inform (type_location,
3078 "(perhaps a semicolon is missing after the definition of %qT)",
3079 type);
3080 }
3081 }
3082
3083 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3084 "<" in any valid C++ program. If the next token is indeed "<",
3085 issue a message warning the user about what appears to be an
3086 invalid attempt to form a template-id. LOCATION is the location
3087 of the type-specifier (TYPE) */
3088
3089 static void
3090 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3091 tree type,
3092 enum tag_types tag_type,
3093 location_t location)
3094 {
3095 cp_token_position start = 0;
3096
3097 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3098 {
3099 if (TREE_CODE (type) == TYPE_DECL)
3100 type = TREE_TYPE (type);
3101 if (TYPE_P (type) && !template_placeholder_p (type))
3102 error_at (location, "%qT is not a template", type);
3103 else if (identifier_p (type))
3104 {
3105 if (tag_type != none_type)
3106 error_at (location, "%qE is not a class template", type);
3107 else
3108 error_at (location, "%qE is not a template", type);
3109 }
3110 else
3111 error_at (location, "invalid template-id");
3112 /* Remember the location of the invalid "<". */
3113 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3114 start = cp_lexer_token_position (parser->lexer, true);
3115 /* Consume the "<". */
3116 cp_lexer_consume_token (parser->lexer);
3117 /* Parse the template arguments. */
3118 cp_parser_enclosed_template_argument_list (parser);
3119 /* Permanently remove the invalid template arguments so that
3120 this error message is not issued again. */
3121 if (start)
3122 cp_lexer_purge_tokens_after (parser->lexer, start);
3123 }
3124 }
3125
3126 /* If parsing an integral constant-expression, issue an error message
3127 about the fact that THING appeared and return true. Otherwise,
3128 return false. In either case, set
3129 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3130
3131 static bool
3132 cp_parser_non_integral_constant_expression (cp_parser *parser,
3133 non_integral_constant thing)
3134 {
3135 parser->non_integral_constant_expression_p = true;
3136 if (parser->integral_constant_expression_p)
3137 {
3138 if (!parser->allow_non_integral_constant_expression_p)
3139 {
3140 const char *msg = NULL;
3141 switch (thing)
3142 {
3143 case NIC_FLOAT:
3144 pedwarn (input_location, OPT_Wpedantic,
3145 "ISO C++ forbids using a floating-point literal "
3146 "in a constant-expression");
3147 return true;
3148 case NIC_CAST:
3149 error ("a cast to a type other than an integral or "
3150 "enumeration type cannot appear in a "
3151 "constant-expression");
3152 return true;
3153 case NIC_TYPEID:
3154 error ("%<typeid%> operator "
3155 "cannot appear in a constant-expression");
3156 return true;
3157 case NIC_NCC:
3158 error ("non-constant compound literals "
3159 "cannot appear in a constant-expression");
3160 return true;
3161 case NIC_FUNC_CALL:
3162 error ("a function call "
3163 "cannot appear in a constant-expression");
3164 return true;
3165 case NIC_INC:
3166 error ("an increment "
3167 "cannot appear in a constant-expression");
3168 return true;
3169 case NIC_DEC:
3170 error ("an decrement "
3171 "cannot appear in a constant-expression");
3172 return true;
3173 case NIC_ARRAY_REF:
3174 error ("an array reference "
3175 "cannot appear in a constant-expression");
3176 return true;
3177 case NIC_ADDR_LABEL:
3178 error ("the address of a label "
3179 "cannot appear in a constant-expression");
3180 return true;
3181 case NIC_OVERLOADED:
3182 error ("calls to overloaded operators "
3183 "cannot appear in a constant-expression");
3184 return true;
3185 case NIC_ASSIGNMENT:
3186 error ("an assignment cannot appear in a constant-expression");
3187 return true;
3188 case NIC_COMMA:
3189 error ("a comma operator "
3190 "cannot appear in a constant-expression");
3191 return true;
3192 case NIC_CONSTRUCTOR:
3193 error ("a call to a constructor "
3194 "cannot appear in a constant-expression");
3195 return true;
3196 case NIC_TRANSACTION:
3197 error ("a transaction expression "
3198 "cannot appear in a constant-expression");
3199 return true;
3200 case NIC_THIS:
3201 msg = "this";
3202 break;
3203 case NIC_FUNC_NAME:
3204 msg = "__FUNCTION__";
3205 break;
3206 case NIC_PRETTY_FUNC:
3207 msg = "__PRETTY_FUNCTION__";
3208 break;
3209 case NIC_C99_FUNC:
3210 msg = "__func__";
3211 break;
3212 case NIC_VA_ARG:
3213 msg = "va_arg";
3214 break;
3215 case NIC_ARROW:
3216 msg = "->";
3217 break;
3218 case NIC_POINT:
3219 msg = ".";
3220 break;
3221 case NIC_STAR:
3222 msg = "*";
3223 break;
3224 case NIC_ADDR:
3225 msg = "&";
3226 break;
3227 case NIC_PREINCREMENT:
3228 msg = "++";
3229 break;
3230 case NIC_PREDECREMENT:
3231 msg = "--";
3232 break;
3233 case NIC_NEW:
3234 msg = "new";
3235 break;
3236 case NIC_DEL:
3237 msg = "delete";
3238 break;
3239 default:
3240 gcc_unreachable ();
3241 }
3242 if (msg)
3243 error ("%qs cannot appear in a constant-expression", msg);
3244 return true;
3245 }
3246 }
3247 return false;
3248 }
3249
3250 /* Emit a diagnostic for an invalid type name. This function commits
3251 to the current active tentative parse, if any. (Otherwise, the
3252 problematic construct might be encountered again later, resulting
3253 in duplicate error messages.) LOCATION is the location of ID. */
3254
3255 static void
3256 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3257 location_t location)
3258 {
3259 tree decl, ambiguous_decls;
3260 cp_parser_commit_to_tentative_parse (parser);
3261 /* Try to lookup the identifier. */
3262 decl = cp_parser_lookup_name (parser, id, none_type,
3263 /*is_template=*/false,
3264 /*is_namespace=*/false,
3265 /*check_dependency=*/true,
3266 &ambiguous_decls, location);
3267 if (ambiguous_decls)
3268 /* If the lookup was ambiguous, an error will already have
3269 been issued. */
3270 return;
3271 /* If the lookup found a template-name, it means that the user forgot
3272 to specify an argument list. Emit a useful error message. */
3273 if (DECL_TYPE_TEMPLATE_P (decl))
3274 {
3275 auto_diagnostic_group d;
3276 error_at (location,
3277 "invalid use of template-name %qE without an argument list",
3278 decl);
3279 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3280 inform (location, "class template argument deduction is only available "
3281 "with -std=c++17 or -std=gnu++17");
3282 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3283 }
3284 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3285 error_at (location, "invalid use of destructor %qD as a type", id);
3286 else if (TREE_CODE (decl) == TYPE_DECL)
3287 /* Something like 'unsigned A a;' */
3288 error_at (location, "invalid combination of multiple type-specifiers");
3289 else if (!parser->scope)
3290 {
3291 /* Issue an error message. */
3292 auto_diagnostic_group d;
3293 name_hint hint;
3294 if (TREE_CODE (id) == IDENTIFIER_NODE)
3295 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3296 if (const char *suggestion = hint.suggestion ())
3297 {
3298 gcc_rich_location richloc (location);
3299 richloc.add_fixit_replace (suggestion);
3300 error_at (&richloc,
3301 "%qE does not name a type; did you mean %qs?",
3302 id, suggestion);
3303 }
3304 else
3305 error_at (location, "%qE does not name a type", id);
3306 /* If we're in a template class, it's possible that the user was
3307 referring to a type from a base class. For example:
3308
3309 template <typename T> struct A { typedef T X; };
3310 template <typename T> struct B : public A<T> { X x; };
3311
3312 The user should have said "typename A<T>::X". */
3313 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3314 inform (location, "C++11 %<constexpr%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3317 inform (location, "C++11 %<noexcept%> only available with "
3318 "-std=c++11 or -std=gnu++11");
3319 else if (cxx_dialect < cxx11
3320 && TREE_CODE (id) == IDENTIFIER_NODE
3321 && id_equal (id, "thread_local"))
3322 inform (location, "C++11 %<thread_local%> only available with "
3323 "-std=c++11 or -std=gnu++11");
3324 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3325 inform (location, "%<concept%> only available with -fconcepts");
3326 else if (processing_template_decl && current_class_type
3327 && TYPE_BINFO (current_class_type))
3328 {
3329 tree b;
3330
3331 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3332 b;
3333 b = TREE_CHAIN (b))
3334 {
3335 tree base_type = BINFO_TYPE (b);
3336 if (CLASS_TYPE_P (base_type)
3337 && dependent_type_p (base_type))
3338 {
3339 tree field;
3340 /* Go from a particular instantiation of the
3341 template (which will have an empty TYPE_FIELDs),
3342 to the main version. */
3343 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3344 for (field = TYPE_FIELDS (base_type);
3345 field;
3346 field = DECL_CHAIN (field))
3347 if (TREE_CODE (field) == TYPE_DECL
3348 && DECL_NAME (field) == id)
3349 {
3350 inform (location,
3351 "(perhaps %<typename %T::%E%> was intended)",
3352 BINFO_TYPE (b), id);
3353 break;
3354 }
3355 if (field)
3356 break;
3357 }
3358 }
3359 }
3360 }
3361 /* Here we diagnose qualified-ids where the scope is actually correct,
3362 but the identifier does not resolve to a valid type name. */
3363 else if (parser->scope != error_mark_node)
3364 {
3365 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3366 {
3367 auto_diagnostic_group d;
3368 name_hint hint;
3369 if (decl == error_mark_node)
3370 hint = suggest_alternative_in_explicit_scope (location, id,
3371 parser->scope);
3372 const char *suggestion = hint.suggestion ();
3373 gcc_rich_location richloc (location_of (id));
3374 if (suggestion)
3375 richloc.add_fixit_replace (suggestion);
3376 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3377 {
3378 if (suggestion)
3379 error_at (&richloc,
3380 "%qE in namespace %qE does not name a template"
3381 " type; did you mean %qs?",
3382 id, parser->scope, suggestion);
3383 else
3384 error_at (&richloc,
3385 "%qE in namespace %qE does not name a template type",
3386 id, parser->scope);
3387 }
3388 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3389 {
3390 if (suggestion)
3391 error_at (&richloc,
3392 "%qE in namespace %qE does not name a template"
3393 " type; did you mean %qs?",
3394 TREE_OPERAND (id, 0), parser->scope, suggestion);
3395 else
3396 error_at (&richloc,
3397 "%qE in namespace %qE does not name a template"
3398 " type",
3399 TREE_OPERAND (id, 0), parser->scope);
3400 }
3401 else
3402 {
3403 if (suggestion)
3404 error_at (&richloc,
3405 "%qE in namespace %qE does not name a type"
3406 "; did you mean %qs?",
3407 id, parser->scope, suggestion);
3408 else
3409 error_at (&richloc,
3410 "%qE in namespace %qE does not name a type",
3411 id, parser->scope);
3412 }
3413 if (DECL_P (decl))
3414 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3415 }
3416 else if (CLASS_TYPE_P (parser->scope)
3417 && constructor_name_p (id, parser->scope))
3418 {
3419 /* A<T>::A<T>() */
3420 auto_diagnostic_group d;
3421 error_at (location, "%<%T::%E%> names the constructor, not"
3422 " the type", parser->scope, id);
3423 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3424 error_at (location, "and %qT has no template constructors",
3425 parser->scope);
3426 }
3427 else if (TYPE_P (parser->scope)
3428 && dependent_scope_p (parser->scope))
3429 {
3430 gcc_rich_location richloc (location);
3431 richloc.add_fixit_insert_before ("typename ");
3432 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3433 error_at (&richloc,
3434 "need %<typename%> before %<%T::%D::%E%> because "
3435 "%<%T::%D%> is a dependent scope",
3436 TYPE_CONTEXT (parser->scope),
3437 TYPENAME_TYPE_FULLNAME (parser->scope),
3438 id,
3439 TYPE_CONTEXT (parser->scope),
3440 TYPENAME_TYPE_FULLNAME (parser->scope));
3441 else
3442 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3443 "%qT is a dependent scope",
3444 parser->scope, id, parser->scope);
3445 }
3446 else if (TYPE_P (parser->scope))
3447 {
3448 auto_diagnostic_group d;
3449 if (!COMPLETE_TYPE_P (parser->scope))
3450 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3451 parser->scope);
3452 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3453 error_at (location_of (id),
3454 "%qE in %q#T does not name a template type",
3455 id, parser->scope);
3456 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3457 error_at (location_of (id),
3458 "%qE in %q#T does not name a template type",
3459 TREE_OPERAND (id, 0), parser->scope);
3460 else
3461 error_at (location_of (id),
3462 "%qE in %q#T does not name a type",
3463 id, parser->scope);
3464 if (DECL_P (decl))
3465 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3466 }
3467 else
3468 gcc_unreachable ();
3469 }
3470 }
3471
3472 /* Check for a common situation where a type-name should be present,
3473 but is not, and issue a sensible error message. Returns true if an
3474 invalid type-name was detected.
3475
3476 The situation handled by this function are variable declarations of the
3477 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3478 Usually, `ID' should name a type, but if we got here it means that it
3479 does not. We try to emit the best possible error message depending on
3480 how exactly the id-expression looks like. */
3481
3482 static bool
3483 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3484 {
3485 tree id;
3486 cp_token *token = cp_lexer_peek_token (parser->lexer);
3487
3488 /* Avoid duplicate error about ambiguous lookup. */
3489 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3490 {
3491 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3492 if (next->type == CPP_NAME && next->error_reported)
3493 goto out;
3494 }
3495
3496 cp_parser_parse_tentatively (parser);
3497 id = cp_parser_id_expression (parser,
3498 /*template_keyword_p=*/false,
3499 /*check_dependency_p=*/true,
3500 /*template_p=*/NULL,
3501 /*declarator_p=*/false,
3502 /*optional_p=*/false);
3503 /* If the next token is a (, this is a function with no explicit return
3504 type, i.e. constructor, destructor or conversion op. */
3505 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3506 || TREE_CODE (id) == TYPE_DECL)
3507 {
3508 cp_parser_abort_tentative_parse (parser);
3509 return false;
3510 }
3511 if (!cp_parser_parse_definitely (parser))
3512 return false;
3513
3514 /* Emit a diagnostic for the invalid type. */
3515 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3516 out:
3517 /* If we aren't in the middle of a declarator (i.e. in a
3518 parameter-declaration-clause), skip to the end of the declaration;
3519 there's no point in trying to process it. */
3520 if (!parser->in_declarator_p)
3521 cp_parser_skip_to_end_of_block_or_statement (parser);
3522 return true;
3523 }
3524
3525 /* Consume tokens up to, and including, the next non-nested closing `)'.
3526 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3527 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3528 found an unnested token of that type. */
3529
3530 static int
3531 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3532 bool recovering,
3533 cpp_ttype or_ttype,
3534 bool consume_paren)
3535 {
3536 unsigned paren_depth = 0;
3537 unsigned brace_depth = 0;
3538 unsigned square_depth = 0;
3539 unsigned condop_depth = 0;
3540
3541 if (recovering && or_ttype == CPP_EOF
3542 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3543 return 0;
3544
3545 while (true)
3546 {
3547 cp_token * token = cp_lexer_peek_token (parser->lexer);
3548
3549 /* Have we found what we're looking for before the closing paren? */
3550 if (token->type == or_ttype && or_ttype != CPP_EOF
3551 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3552 return -1;
3553
3554 switch (token->type)
3555 {
3556 case CPP_EOF:
3557 case CPP_PRAGMA_EOL:
3558 /* If we've run out of tokens, then there is no closing `)'. */
3559 return 0;
3560
3561 /* This is good for lambda expression capture-lists. */
3562 case CPP_OPEN_SQUARE:
3563 ++square_depth;
3564 break;
3565 case CPP_CLOSE_SQUARE:
3566 if (!square_depth--)
3567 return 0;
3568 break;
3569
3570 case CPP_SEMICOLON:
3571 /* This matches the processing in skip_to_end_of_statement. */
3572 if (!brace_depth)
3573 return 0;
3574 break;
3575
3576 case CPP_OPEN_BRACE:
3577 ++brace_depth;
3578 break;
3579 case CPP_CLOSE_BRACE:
3580 if (!brace_depth--)
3581 return 0;
3582 break;
3583
3584 case CPP_OPEN_PAREN:
3585 if (!brace_depth)
3586 ++paren_depth;
3587 break;
3588
3589 case CPP_CLOSE_PAREN:
3590 if (!brace_depth && !paren_depth--)
3591 {
3592 if (consume_paren)
3593 cp_lexer_consume_token (parser->lexer);
3594 return 1;
3595 }
3596 break;
3597
3598 case CPP_QUERY:
3599 if (!brace_depth && !paren_depth && !square_depth)
3600 ++condop_depth;
3601 break;
3602
3603 case CPP_COLON:
3604 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3605 condop_depth--;
3606 break;
3607
3608 default:
3609 break;
3610 }
3611
3612 /* Consume the token. */
3613 cp_lexer_consume_token (parser->lexer);
3614 }
3615 }
3616
3617 /* Consume tokens up to, and including, the next non-nested closing `)'.
3618 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3619 are doing error recovery. Returns -1 if OR_COMMA is true and we
3620 found an unnested token of that type. */
3621
3622 static int
3623 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3624 bool recovering,
3625 bool or_comma,
3626 bool consume_paren)
3627 {
3628 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3629 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3630 ttype, consume_paren);
3631 }
3632
3633 /* Consume tokens until we reach the end of the current statement.
3634 Normally, that will be just before consuming a `;'. However, if a
3635 non-nested `}' comes first, then we stop before consuming that. */
3636
3637 static void
3638 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3639 {
3640 unsigned nesting_depth = 0;
3641
3642 /* Unwind generic function template scope if necessary. */
3643 if (parser->fully_implicit_function_template_p)
3644 abort_fully_implicit_template (parser);
3645
3646 while (true)
3647 {
3648 cp_token *token = cp_lexer_peek_token (parser->lexer);
3649
3650 switch (token->type)
3651 {
3652 case CPP_EOF:
3653 case CPP_PRAGMA_EOL:
3654 /* If we've run out of tokens, stop. */
3655 return;
3656
3657 case CPP_SEMICOLON:
3658 /* If the next token is a `;', we have reached the end of the
3659 statement. */
3660 if (!nesting_depth)
3661 return;
3662 break;
3663
3664 case CPP_CLOSE_BRACE:
3665 /* If this is a non-nested '}', stop before consuming it.
3666 That way, when confronted with something like:
3667
3668 { 3 + }
3669
3670 we stop before consuming the closing '}', even though we
3671 have not yet reached a `;'. */
3672 if (nesting_depth == 0)
3673 return;
3674
3675 /* If it is the closing '}' for a block that we have
3676 scanned, stop -- but only after consuming the token.
3677 That way given:
3678
3679 void f g () { ... }
3680 typedef int I;
3681
3682 we will stop after the body of the erroneously declared
3683 function, but before consuming the following `typedef'
3684 declaration. */
3685 if (--nesting_depth == 0)
3686 {
3687 cp_lexer_consume_token (parser->lexer);
3688 return;
3689 }
3690 break;
3691
3692 case CPP_OPEN_BRACE:
3693 ++nesting_depth;
3694 break;
3695
3696 default:
3697 break;
3698 }
3699
3700 /* Consume the token. */
3701 cp_lexer_consume_token (parser->lexer);
3702 }
3703 }
3704
3705 /* This function is called at the end of a statement or declaration.
3706 If the next token is a semicolon, it is consumed; otherwise, error
3707 recovery is attempted. */
3708
3709 static void
3710 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3711 {
3712 /* Look for the trailing `;'. */
3713 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3714 {
3715 /* If there is additional (erroneous) input, skip to the end of
3716 the statement. */
3717 cp_parser_skip_to_end_of_statement (parser);
3718 /* If the next token is now a `;', consume it. */
3719 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3720 cp_lexer_consume_token (parser->lexer);
3721 }
3722 }
3723
3724 /* Skip tokens until we have consumed an entire block, or until we
3725 have consumed a non-nested `;'. */
3726
3727 static void
3728 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3729 {
3730 int nesting_depth = 0;
3731
3732 /* Unwind generic function template scope if necessary. */
3733 if (parser->fully_implicit_function_template_p)
3734 abort_fully_implicit_template (parser);
3735
3736 while (nesting_depth >= 0)
3737 {
3738 cp_token *token = cp_lexer_peek_token (parser->lexer);
3739
3740 switch (token->type)
3741 {
3742 case CPP_EOF:
3743 case CPP_PRAGMA_EOL:
3744 /* If we've run out of tokens, stop. */
3745 return;
3746
3747 case CPP_SEMICOLON:
3748 /* Stop if this is an unnested ';'. */
3749 if (!nesting_depth)
3750 nesting_depth = -1;
3751 break;
3752
3753 case CPP_CLOSE_BRACE:
3754 /* Stop if this is an unnested '}', or closes the outermost
3755 nesting level. */
3756 nesting_depth--;
3757 if (nesting_depth < 0)
3758 return;
3759 if (!nesting_depth)
3760 nesting_depth = -1;
3761 break;
3762
3763 case CPP_OPEN_BRACE:
3764 /* Nest. */
3765 nesting_depth++;
3766 break;
3767
3768 default:
3769 break;
3770 }
3771
3772 /* Consume the token. */
3773 cp_lexer_consume_token (parser->lexer);
3774 }
3775 }
3776
3777 /* Skip tokens until a non-nested closing curly brace is the next
3778 token, or there are no more tokens. Return true in the first case,
3779 false otherwise. */
3780
3781 static bool
3782 cp_parser_skip_to_closing_brace (cp_parser *parser)
3783 {
3784 unsigned nesting_depth = 0;
3785
3786 while (true)
3787 {
3788 cp_token *token = cp_lexer_peek_token (parser->lexer);
3789
3790 switch (token->type)
3791 {
3792 case CPP_EOF:
3793 case CPP_PRAGMA_EOL:
3794 /* If we've run out of tokens, stop. */
3795 return false;
3796
3797 case CPP_CLOSE_BRACE:
3798 /* If the next token is a non-nested `}', then we have reached
3799 the end of the current block. */
3800 if (nesting_depth-- == 0)
3801 return true;
3802 break;
3803
3804 case CPP_OPEN_BRACE:
3805 /* If it the next token is a `{', then we are entering a new
3806 block. Consume the entire block. */
3807 ++nesting_depth;
3808 break;
3809
3810 default:
3811 break;
3812 }
3813
3814 /* Consume the token. */
3815 cp_lexer_consume_token (parser->lexer);
3816 }
3817 }
3818
3819 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3820 parameter is the PRAGMA token, allowing us to purge the entire pragma
3821 sequence. */
3822
3823 static void
3824 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3825 {
3826 cp_token *token;
3827
3828 parser->lexer->in_pragma = false;
3829
3830 do
3831 token = cp_lexer_consume_token (parser->lexer);
3832 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3833
3834 /* Ensure that the pragma is not parsed again. */
3835 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3836 }
3837
3838 /* Require pragma end of line, resyncing with it as necessary. The
3839 arguments are as for cp_parser_skip_to_pragma_eol. */
3840
3841 static void
3842 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3843 {
3844 parser->lexer->in_pragma = false;
3845 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3846 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3847 }
3848
3849 /* This is a simple wrapper around make_typename_type. When the id is
3850 an unresolved identifier node, we can provide a superior diagnostic
3851 using cp_parser_diagnose_invalid_type_name. */
3852
3853 static tree
3854 cp_parser_make_typename_type (cp_parser *parser, tree id,
3855 location_t id_location)
3856 {
3857 tree result;
3858 if (identifier_p (id))
3859 {
3860 result = make_typename_type (parser->scope, id, typename_type,
3861 /*complain=*/tf_none);
3862 if (result == error_mark_node)
3863 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3864 return result;
3865 }
3866 return make_typename_type (parser->scope, id, typename_type, tf_error);
3867 }
3868
3869 /* This is a wrapper around the
3870 make_{pointer,ptrmem,reference}_declarator functions that decides
3871 which one to call based on the CODE and CLASS_TYPE arguments. The
3872 CODE argument should be one of the values returned by
3873 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3874 appertain to the pointer or reference. */
3875
3876 static cp_declarator *
3877 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3878 cp_cv_quals cv_qualifiers,
3879 cp_declarator *target,
3880 tree attributes)
3881 {
3882 if (code == ERROR_MARK || target == cp_error_declarator)
3883 return cp_error_declarator;
3884
3885 if (code == INDIRECT_REF)
3886 if (class_type == NULL_TREE)
3887 return make_pointer_declarator (cv_qualifiers, target, attributes);
3888 else
3889 return make_ptrmem_declarator (cv_qualifiers, class_type,
3890 target, attributes);
3891 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3892 return make_reference_declarator (cv_qualifiers, target,
3893 false, attributes);
3894 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3895 return make_reference_declarator (cv_qualifiers, target,
3896 true, attributes);
3897 gcc_unreachable ();
3898 }
3899
3900 /* Create a new C++ parser. */
3901
3902 static cp_parser *
3903 cp_parser_new (void)
3904 {
3905 cp_parser *parser;
3906 cp_lexer *lexer;
3907 unsigned i;
3908
3909 /* cp_lexer_new_main is called before doing GC allocation because
3910 cp_lexer_new_main might load a PCH file. */
3911 lexer = cp_lexer_new_main ();
3912
3913 /* Initialize the binops_by_token so that we can get the tree
3914 directly from the token. */
3915 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3916 binops_by_token[binops[i].token_type] = binops[i];
3917
3918 parser = ggc_cleared_alloc<cp_parser> ();
3919 parser->lexer = lexer;
3920 parser->context = cp_parser_context_new (NULL);
3921
3922 /* For now, we always accept GNU extensions. */
3923 parser->allow_gnu_extensions_p = 1;
3924
3925 /* The `>' token is a greater-than operator, not the end of a
3926 template-id. */
3927 parser->greater_than_is_operator_p = true;
3928
3929 parser->default_arg_ok_p = true;
3930
3931 /* We are not parsing a constant-expression. */
3932 parser->integral_constant_expression_p = false;
3933 parser->allow_non_integral_constant_expression_p = false;
3934 parser->non_integral_constant_expression_p = false;
3935
3936 /* Local variable names are not forbidden. */
3937 parser->local_variables_forbidden_p = false;
3938
3939 /* We are not processing an `extern "C"' declaration. */
3940 parser->in_unbraced_linkage_specification_p = false;
3941
3942 /* We are not processing a declarator. */
3943 parser->in_declarator_p = false;
3944
3945 /* We are not processing a template-argument-list. */
3946 parser->in_template_argument_list_p = false;
3947
3948 /* We are not in an iteration statement. */
3949 parser->in_statement = 0;
3950
3951 /* We are not in a switch statement. */
3952 parser->in_switch_statement_p = false;
3953
3954 /* We are not parsing a type-id inside an expression. */
3955 parser->in_type_id_in_expr_p = false;
3956
3957 /* String literals should be translated to the execution character set. */
3958 parser->translate_strings_p = true;
3959
3960 /* We are not parsing a function body. */
3961 parser->in_function_body = false;
3962
3963 /* We can correct until told otherwise. */
3964 parser->colon_corrects_to_scope_p = true;
3965
3966 /* The unparsed function queue is empty. */
3967 push_unparsed_function_queues (parser);
3968
3969 /* There are no classes being defined. */
3970 parser->num_classes_being_defined = 0;
3971
3972 /* No template parameters apply. */
3973 parser->num_template_parameter_lists = 0;
3974
3975 /* Special parsing data structures. */
3976 parser->omp_declare_simd = NULL;
3977 parser->oacc_routine = NULL;
3978
3979 /* Not declaring an implicit function template. */
3980 parser->auto_is_implicit_function_template_parm_p = false;
3981 parser->fully_implicit_function_template_p = false;
3982 parser->implicit_template_parms = 0;
3983 parser->implicit_template_scope = 0;
3984
3985 /* Allow constrained-type-specifiers. */
3986 parser->prevent_constrained_type_specifiers = 0;
3987
3988 /* We haven't yet seen an 'extern "C"'. */
3989 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3990
3991 return parser;
3992 }
3993
3994 /* Create a cp_lexer structure which will emit the tokens in CACHE
3995 and push it onto the parser's lexer stack. This is used for delayed
3996 parsing of in-class method bodies and default arguments, and should
3997 not be confused with tentative parsing. */
3998 static void
3999 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4000 {
4001 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4002 lexer->next = parser->lexer;
4003 parser->lexer = lexer;
4004
4005 /* Move the current source position to that of the first token in the
4006 new lexer. */
4007 cp_lexer_set_source_position_from_token (lexer->next_token);
4008 }
4009
4010 /* Pop the top lexer off the parser stack. This is never used for the
4011 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4012 static void
4013 cp_parser_pop_lexer (cp_parser *parser)
4014 {
4015 cp_lexer *lexer = parser->lexer;
4016 parser->lexer = lexer->next;
4017 cp_lexer_destroy (lexer);
4018
4019 /* Put the current source position back where it was before this
4020 lexer was pushed. */
4021 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4022 }
4023
4024 /* Lexical conventions [gram.lex] */
4025
4026 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4027 identifier. */
4028
4029 static cp_expr
4030 cp_parser_identifier (cp_parser* parser)
4031 {
4032 cp_token *token;
4033
4034 /* Look for the identifier. */
4035 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4036 /* Return the value. */
4037 if (token)
4038 return cp_expr (token->u.value, token->location);
4039 else
4040 return error_mark_node;
4041 }
4042
4043 /* Parse a sequence of adjacent string constants. Returns a
4044 TREE_STRING representing the combined, nul-terminated string
4045 constant. If TRANSLATE is true, translate the string to the
4046 execution character set. If WIDE_OK is true, a wide string is
4047 invalid here.
4048
4049 C++98 [lex.string] says that if a narrow string literal token is
4050 adjacent to a wide string literal token, the behavior is undefined.
4051 However, C99 6.4.5p4 says that this results in a wide string literal.
4052 We follow C99 here, for consistency with the C front end.
4053
4054 This code is largely lifted from lex_string() in c-lex.c.
4055
4056 FUTURE: ObjC++ will need to handle @-strings here. */
4057 static cp_expr
4058 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4059 bool lookup_udlit = true)
4060 {
4061 tree value;
4062 size_t count;
4063 struct obstack str_ob;
4064 struct obstack loc_ob;
4065 cpp_string str, istr, *strs;
4066 cp_token *tok;
4067 enum cpp_ttype type, curr_type;
4068 int have_suffix_p = 0;
4069 tree string_tree;
4070 tree suffix_id = NULL_TREE;
4071 bool curr_tok_is_userdef_p = false;
4072
4073 tok = cp_lexer_peek_token (parser->lexer);
4074 if (!cp_parser_is_string_literal (tok))
4075 {
4076 cp_parser_error (parser, "expected string-literal");
4077 return error_mark_node;
4078 }
4079
4080 location_t loc = tok->location;
4081
4082 if (cpp_userdef_string_p (tok->type))
4083 {
4084 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4085 curr_type = cpp_userdef_string_remove_type (tok->type);
4086 curr_tok_is_userdef_p = true;
4087 }
4088 else
4089 {
4090 string_tree = tok->u.value;
4091 curr_type = tok->type;
4092 }
4093 type = curr_type;
4094
4095 /* Try to avoid the overhead of creating and destroying an obstack
4096 for the common case of just one string. */
4097 if (!cp_parser_is_string_literal
4098 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4099 {
4100 cp_lexer_consume_token (parser->lexer);
4101
4102 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4103 str.len = TREE_STRING_LENGTH (string_tree);
4104 count = 1;
4105
4106 if (curr_tok_is_userdef_p)
4107 {
4108 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4109 have_suffix_p = 1;
4110 curr_type = cpp_userdef_string_remove_type (tok->type);
4111 }
4112 else
4113 curr_type = tok->type;
4114
4115 strs = &str;
4116 }
4117 else
4118 {
4119 location_t last_tok_loc = tok->location;
4120 gcc_obstack_init (&str_ob);
4121 gcc_obstack_init (&loc_ob);
4122 count = 0;
4123
4124 do
4125 {
4126 cp_lexer_consume_token (parser->lexer);
4127 count++;
4128 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4129 str.len = TREE_STRING_LENGTH (string_tree);
4130
4131 if (curr_tok_is_userdef_p)
4132 {
4133 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4134 if (have_suffix_p == 0)
4135 {
4136 suffix_id = curr_suffix_id;
4137 have_suffix_p = 1;
4138 }
4139 else if (have_suffix_p == 1
4140 && curr_suffix_id != suffix_id)
4141 {
4142 error ("inconsistent user-defined literal suffixes"
4143 " %qD and %qD in string literal",
4144 suffix_id, curr_suffix_id);
4145 have_suffix_p = -1;
4146 }
4147 curr_type = cpp_userdef_string_remove_type (tok->type);
4148 }
4149 else
4150 curr_type = tok->type;
4151
4152 if (type != curr_type)
4153 {
4154 if (type == CPP_STRING)
4155 type = curr_type;
4156 else if (curr_type != CPP_STRING)
4157 {
4158 rich_location rich_loc (line_table, tok->location);
4159 rich_loc.add_range (last_tok_loc);
4160 error_at (&rich_loc,
4161 "unsupported non-standard concatenation "
4162 "of string literals");
4163 }
4164 }
4165
4166 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4167 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4168
4169 last_tok_loc = tok->location;
4170
4171 tok = cp_lexer_peek_token (parser->lexer);
4172 if (cpp_userdef_string_p (tok->type))
4173 {
4174 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4175 curr_type = cpp_userdef_string_remove_type (tok->type);
4176 curr_tok_is_userdef_p = true;
4177 }
4178 else
4179 {
4180 string_tree = tok->u.value;
4181 curr_type = tok->type;
4182 curr_tok_is_userdef_p = false;
4183 }
4184 }
4185 while (cp_parser_is_string_literal (tok));
4186
4187 /* A string literal built by concatenation has its caret=start at
4188 the start of the initial string, and its finish at the finish of
4189 the final string literal. */
4190 loc = make_location (loc, loc, get_finish (last_tok_loc));
4191
4192 strs = (cpp_string *) obstack_finish (&str_ob);
4193 }
4194
4195 if (type != CPP_STRING && !wide_ok)
4196 {
4197 cp_parser_error (parser, "a wide string is invalid in this context");
4198 type = CPP_STRING;
4199 }
4200
4201 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4202 (parse_in, strs, count, &istr, type))
4203 {
4204 value = build_string (istr.len, (const char *)istr.text);
4205 free (CONST_CAST (unsigned char *, istr.text));
4206 if (count > 1)
4207 {
4208 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4209 gcc_assert (g_string_concat_db);
4210 g_string_concat_db->record_string_concatenation (count, locs);
4211 }
4212
4213 switch (type)
4214 {
4215 default:
4216 case CPP_STRING:
4217 case CPP_UTF8STRING:
4218 TREE_TYPE (value) = char_array_type_node;
4219 break;
4220 case CPP_STRING16:
4221 TREE_TYPE (value) = char16_array_type_node;
4222 break;
4223 case CPP_STRING32:
4224 TREE_TYPE (value) = char32_array_type_node;
4225 break;
4226 case CPP_WSTRING:
4227 TREE_TYPE (value) = wchar_array_type_node;
4228 break;
4229 }
4230
4231 value = fix_string_type (value);
4232
4233 if (have_suffix_p)
4234 {
4235 tree literal = build_userdef_literal (suffix_id, value,
4236 OT_NONE, NULL_TREE);
4237 if (lookup_udlit)
4238 value = cp_parser_userdef_string_literal (literal);
4239 else
4240 value = literal;
4241 }
4242 }
4243 else
4244 /* cpp_interpret_string has issued an error. */
4245 value = error_mark_node;
4246
4247 if (count > 1)
4248 {
4249 obstack_free (&str_ob, 0);
4250 obstack_free (&loc_ob, 0);
4251 }
4252
4253 return cp_expr (value, loc);
4254 }
4255
4256 /* Look up a literal operator with the name and the exact arguments. */
4257
4258 static tree
4259 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4260 {
4261 tree decl;
4262 decl = lookup_name (name);
4263 if (!decl || !is_overloaded_fn (decl))
4264 return error_mark_node;
4265
4266 for (lkp_iterator iter (decl); iter; ++iter)
4267 {
4268 unsigned int ix;
4269 bool found = true;
4270 tree fn = *iter;
4271 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4272 if (parmtypes != NULL_TREE)
4273 {
4274 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4275 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4276 {
4277 tree tparm = TREE_VALUE (parmtypes);
4278 tree targ = TREE_TYPE ((*args)[ix]);
4279 bool ptr = TYPE_PTR_P (tparm);
4280 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4281 if ((ptr || arr || !same_type_p (tparm, targ))
4282 && (!ptr || !arr
4283 || !same_type_p (TREE_TYPE (tparm),
4284 TREE_TYPE (targ))))
4285 found = false;
4286 }
4287 if (found
4288 && ix == vec_safe_length (args)
4289 /* May be this should be sufficient_parms_p instead,
4290 depending on how exactly should user-defined literals
4291 work in presence of default arguments on the literal
4292 operator parameters. */
4293 && parmtypes == void_list_node)
4294 return decl;
4295 }
4296 }
4297
4298 return error_mark_node;
4299 }
4300
4301 /* Parse a user-defined char constant. Returns a call to a user-defined
4302 literal operator taking the character as an argument. */
4303
4304 static cp_expr
4305 cp_parser_userdef_char_literal (cp_parser *parser)
4306 {
4307 cp_token *token = cp_lexer_consume_token (parser->lexer);
4308 tree literal = token->u.value;
4309 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4310 tree value = USERDEF_LITERAL_VALUE (literal);
4311 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4312 tree decl, result;
4313
4314 /* Build up a call to the user-defined operator */
4315 /* Lookup the name we got back from the id-expression. */
4316 vec<tree, va_gc> *args = make_tree_vector ();
4317 vec_safe_push (args, value);
4318 decl = lookup_literal_operator (name, args);
4319 if (!decl || decl == error_mark_node)
4320 {
4321 error ("unable to find character literal operator %qD with %qT argument",
4322 name, TREE_TYPE (value));
4323 release_tree_vector (args);
4324 return error_mark_node;
4325 }
4326 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4327 release_tree_vector (args);
4328 return result;
4329 }
4330
4331 /* A subroutine of cp_parser_userdef_numeric_literal to
4332 create a char... template parameter pack from a string node. */
4333
4334 static tree
4335 make_char_string_pack (tree value)
4336 {
4337 tree charvec;
4338 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4339 const char *str = TREE_STRING_POINTER (value);
4340 int i, len = TREE_STRING_LENGTH (value) - 1;
4341 tree argvec = make_tree_vec (1);
4342
4343 /* Fill in CHARVEC with all of the parameters. */
4344 charvec = make_tree_vec (len);
4345 for (i = 0; i < len; ++i)
4346 {
4347 unsigned char s[3] = { '\'', str[i], '\'' };
4348 cpp_string in = { 3, s };
4349 cpp_string out = { 0, 0 };
4350 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4351 return NULL_TREE;
4352 gcc_assert (out.len == 2);
4353 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4354 out.text[0]);
4355 }
4356
4357 /* Build the argument packs. */
4358 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4359
4360 TREE_VEC_ELT (argvec, 0) = argpack;
4361
4362 return argvec;
4363 }
4364
4365 /* A subroutine of cp_parser_userdef_numeric_literal to
4366 create a char... template parameter pack from a string node. */
4367
4368 static tree
4369 make_string_pack (tree value)
4370 {
4371 tree charvec;
4372 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4373 const unsigned char *str
4374 = (const unsigned char *) TREE_STRING_POINTER (value);
4375 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4376 int len = TREE_STRING_LENGTH (value) / sz - 1;
4377 tree argvec = make_tree_vec (2);
4378
4379 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4380 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4381
4382 /* First template parm is character type. */
4383 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4384
4385 /* Fill in CHARVEC with all of the parameters. */
4386 charvec = make_tree_vec (len);
4387 for (int i = 0; i < len; ++i)
4388 TREE_VEC_ELT (charvec, i)
4389 = double_int_to_tree (str_char_type_node,
4390 double_int::from_buffer (str + i * sz, sz));
4391
4392 /* Build the argument packs. */
4393 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4394
4395 TREE_VEC_ELT (argvec, 1) = argpack;
4396
4397 return argvec;
4398 }
4399
4400 /* Parse a user-defined numeric constant. returns a call to a user-defined
4401 literal operator. */
4402
4403 static cp_expr
4404 cp_parser_userdef_numeric_literal (cp_parser *parser)
4405 {
4406 cp_token *token = cp_lexer_consume_token (parser->lexer);
4407 tree literal = token->u.value;
4408 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4409 tree value = USERDEF_LITERAL_VALUE (literal);
4410 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4411 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4412 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4413 tree decl, result;
4414 vec<tree, va_gc> *args;
4415
4416 /* Look for a literal operator taking the exact type of numeric argument
4417 as the literal value. */
4418 args = make_tree_vector ();
4419 vec_safe_push (args, value);
4420 decl = lookup_literal_operator (name, args);
4421 if (decl && decl != error_mark_node)
4422 {
4423 result = finish_call_expr (decl, &args, false, true,
4424 tf_warning_or_error);
4425
4426 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4427 {
4428 warning_at (token->location, OPT_Woverflow,
4429 "integer literal exceeds range of %qT type",
4430 long_long_unsigned_type_node);
4431 }
4432 else
4433 {
4434 if (overflow > 0)
4435 warning_at (token->location, OPT_Woverflow,
4436 "floating literal exceeds range of %qT type",
4437 long_double_type_node);
4438 else if (overflow < 0)
4439 warning_at (token->location, OPT_Woverflow,
4440 "floating literal truncated to zero");
4441 }
4442
4443 release_tree_vector (args);
4444 return result;
4445 }
4446 release_tree_vector (args);
4447
4448 /* If the numeric argument didn't work, look for a raw literal
4449 operator taking a const char* argument consisting of the number
4450 in string format. */
4451 args = make_tree_vector ();
4452 vec_safe_push (args, num_string);
4453 decl = lookup_literal_operator (name, args);
4454 if (decl && decl != error_mark_node)
4455 {
4456 result = finish_call_expr (decl, &args, false, true,
4457 tf_warning_or_error);
4458 release_tree_vector (args);
4459 return result;
4460 }
4461 release_tree_vector (args);
4462
4463 /* If the raw literal didn't work, look for a non-type template
4464 function with parameter pack char.... Call the function with
4465 template parameter characters representing the number. */
4466 args = make_tree_vector ();
4467 decl = lookup_literal_operator (name, args);
4468 if (decl && decl != error_mark_node)
4469 {
4470 tree tmpl_args = make_char_string_pack (num_string);
4471 if (tmpl_args == NULL_TREE)
4472 {
4473 error ("failed to translate literal to execution character set %qT",
4474 num_string);
4475 return error_mark_node;
4476 }
4477 decl = lookup_template_function (decl, tmpl_args);
4478 result = finish_call_expr (decl, &args, false, true,
4479 tf_warning_or_error);
4480 release_tree_vector (args);
4481 return result;
4482 }
4483
4484 release_tree_vector (args);
4485
4486 /* In C++14 the standard library defines complex number suffixes that
4487 conflict with GNU extensions. Prefer them if <complex> is #included. */
4488 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4489 bool i14 = (cxx_dialect > cxx11
4490 && (id_equal (suffix_id, "i")
4491 || id_equal (suffix_id, "if")
4492 || id_equal (suffix_id, "il")));
4493 diagnostic_t kind = DK_ERROR;
4494 int opt = 0;
4495
4496 if (i14 && ext)
4497 {
4498 tree cxlit = lookup_qualified_name (std_node,
4499 get_identifier ("complex_literals"),
4500 0, false, false);
4501 if (cxlit == error_mark_node)
4502 {
4503 /* No <complex>, so pedwarn and use GNU semantics. */
4504 kind = DK_PEDWARN;
4505 opt = OPT_Wpedantic;
4506 }
4507 }
4508
4509 bool complained
4510 = emit_diagnostic (kind, input_location, opt,
4511 "unable to find numeric literal operator %qD", name);
4512
4513 if (!complained)
4514 /* Don't inform either. */;
4515 else if (i14)
4516 {
4517 inform (token->location, "add %<using namespace std::complex_literals%> "
4518 "(from <complex>) to enable the C++14 user-defined literal "
4519 "suffixes");
4520 if (ext)
4521 inform (token->location, "or use %<j%> instead of %<i%> for the "
4522 "GNU built-in suffix");
4523 }
4524 else if (!ext)
4525 inform (token->location, "use -fext-numeric-literals "
4526 "to enable more built-in suffixes");
4527
4528 if (kind == DK_ERROR)
4529 value = error_mark_node;
4530 else
4531 {
4532 /* Use the built-in semantics. */
4533 tree type;
4534 if (id_equal (suffix_id, "i"))
4535 {
4536 if (TREE_CODE (value) == INTEGER_CST)
4537 type = integer_type_node;
4538 else
4539 type = double_type_node;
4540 }
4541 else if (id_equal (suffix_id, "if"))
4542 type = float_type_node;
4543 else /* if (id_equal (suffix_id, "il")) */
4544 type = long_double_type_node;
4545
4546 value = build_complex (build_complex_type (type),
4547 fold_convert (type, integer_zero_node),
4548 fold_convert (type, value));
4549 }
4550
4551 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4552 /* Avoid repeated diagnostics. */
4553 token->u.value = value;
4554 return value;
4555 }
4556
4557 /* Parse a user-defined string constant. Returns a call to a user-defined
4558 literal operator taking a character pointer and the length of the string
4559 as arguments. */
4560
4561 static tree
4562 cp_parser_userdef_string_literal (tree literal)
4563 {
4564 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4565 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4566 tree value = USERDEF_LITERAL_VALUE (literal);
4567 int len = TREE_STRING_LENGTH (value)
4568 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4569 tree decl;
4570
4571 /* Build up a call to the user-defined operator. */
4572 /* Lookup the name we got back from the id-expression. */
4573 releasing_vec rargs;
4574 vec<tree, va_gc> *&args = rargs.get_ref();
4575 vec_safe_push (args, value);
4576 vec_safe_push (args, build_int_cst (size_type_node, len));
4577 decl = lookup_literal_operator (name, args);
4578
4579 if (decl && decl != error_mark_node)
4580 return finish_call_expr (decl, &args, false, true,
4581 tf_warning_or_error);
4582
4583 /* Look for a suitable template function, either (C++20) with a single
4584 parameter of class type, or (N3599) with typename parameter CharT and
4585 parameter pack CharT... */
4586 args->truncate (0);
4587 decl = lookup_literal_operator (name, args);
4588 if (decl && decl != error_mark_node)
4589 {
4590 /* Use resolve_nondeduced_context to try to choose one form of template
4591 or the other. */
4592 tree tmpl_args = make_tree_vec (1);
4593 TREE_VEC_ELT (tmpl_args, 0) = value;
4594 decl = lookup_template_function (decl, tmpl_args);
4595 tree res = resolve_nondeduced_context (decl, tf_none);
4596 if (DECL_P (res))
4597 decl = res;
4598 else
4599 {
4600 TREE_OPERAND (decl, 1) = make_string_pack (value);
4601 res = resolve_nondeduced_context (decl, tf_none);
4602 if (DECL_P (res))
4603 decl = res;
4604 }
4605 if (!DECL_P (decl) && cxx_dialect > cxx17)
4606 TREE_OPERAND (decl, 1) = tmpl_args;
4607 return finish_call_expr (decl, &args, false, true,
4608 tf_warning_or_error);
4609 }
4610
4611 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4612 name, TREE_TYPE (value), size_type_node);
4613 return error_mark_node;
4614 }
4615
4616
4617 /* Basic concepts [gram.basic] */
4618
4619 /* Parse a translation-unit.
4620
4621 translation-unit:
4622 declaration-seq [opt] */
4623
4624 static void
4625 cp_parser_translation_unit (cp_parser* parser)
4626 {
4627 gcc_checking_assert (!cp_error_declarator);
4628
4629 /* Create the declarator obstack. */
4630 gcc_obstack_init (&declarator_obstack);
4631 /* Create the error declarator. */
4632 cp_error_declarator = make_declarator (cdk_error);
4633 /* Create the empty parameter list. */
4634 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4635 UNKNOWN_LOCATION);
4636 /* Remember where the base of the declarator obstack lies. */
4637 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4638
4639 bool implicit_extern_c = false;
4640
4641 for (;;)
4642 {
4643 cp_token *token = cp_lexer_peek_token (parser->lexer);
4644
4645 /* If we're entering or exiting a region that's implicitly
4646 extern "C", modify the lang context appropriately. */
4647 if (implicit_extern_c
4648 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4649 {
4650 implicit_extern_c = !implicit_extern_c;
4651 if (implicit_extern_c)
4652 push_lang_context (lang_name_c);
4653 else
4654 pop_lang_context ();
4655 }
4656
4657 if (token->type == CPP_EOF)
4658 break;
4659
4660 if (token->type == CPP_CLOSE_BRACE)
4661 {
4662 cp_parser_error (parser, "expected declaration");
4663 cp_lexer_consume_token (parser->lexer);
4664 /* If the next token is now a `;', consume it. */
4665 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4666 cp_lexer_consume_token (parser->lexer);
4667 }
4668 else
4669 cp_parser_toplevel_declaration (parser);
4670 }
4671
4672 /* Get rid of the token array; we don't need it any more. */
4673 cp_lexer_destroy (parser->lexer);
4674 parser->lexer = NULL;
4675
4676 /* The EOF should have reset this. */
4677 gcc_checking_assert (!implicit_extern_c);
4678
4679 /* Make sure the declarator obstack was fully cleaned up. */
4680 gcc_assert (obstack_next_free (&declarator_obstack)
4681 == declarator_obstack_base);
4682 }
4683
4684 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4685 decltype context. */
4686
4687 static inline tsubst_flags_t
4688 complain_flags (bool decltype_p)
4689 {
4690 tsubst_flags_t complain = tf_warning_or_error;
4691 if (decltype_p)
4692 complain |= tf_decltype;
4693 return complain;
4694 }
4695
4696 /* We're about to parse a collection of statements. If we're currently
4697 parsing tentatively, set up a firewall so that any nested
4698 cp_parser_commit_to_tentative_parse won't affect the current context. */
4699
4700 static cp_token_position
4701 cp_parser_start_tentative_firewall (cp_parser *parser)
4702 {
4703 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4704 return 0;
4705
4706 cp_parser_parse_tentatively (parser);
4707 cp_parser_commit_to_topmost_tentative_parse (parser);
4708 return cp_lexer_token_position (parser->lexer, false);
4709 }
4710
4711 /* We've finished parsing the collection of statements. Wrap up the
4712 firewall and replace the relevant tokens with the parsed form. */
4713
4714 static void
4715 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4716 tree expr)
4717 {
4718 if (!start)
4719 return;
4720
4721 /* Finish the firewall level. */
4722 cp_parser_parse_definitely (parser);
4723 /* And remember the result of the parse for when we try again. */
4724 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4725 token->type = CPP_PREPARSED_EXPR;
4726 token->u.value = expr;
4727 token->keyword = RID_MAX;
4728 cp_lexer_purge_tokens_after (parser->lexer, start);
4729 }
4730
4731 /* Like the above functions, but let the user modify the tokens. Used by
4732 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4733 later parses, so it makes sense to localize the effects of
4734 cp_parser_commit_to_tentative_parse. */
4735
4736 struct tentative_firewall
4737 {
4738 cp_parser *parser;
4739 bool set;
4740
4741 tentative_firewall (cp_parser *p): parser(p)
4742 {
4743 /* If we're currently parsing tentatively, start a committed level as a
4744 firewall and then an inner tentative parse. */
4745 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4746 {
4747 cp_parser_parse_tentatively (parser);
4748 cp_parser_commit_to_topmost_tentative_parse (parser);
4749 cp_parser_parse_tentatively (parser);
4750 }
4751 }
4752
4753 ~tentative_firewall()
4754 {
4755 if (set)
4756 {
4757 /* Finish the inner tentative parse and the firewall, propagating any
4758 uncommitted error state to the outer tentative parse. */
4759 bool err = cp_parser_error_occurred (parser);
4760 cp_parser_parse_definitely (parser);
4761 cp_parser_parse_definitely (parser);
4762 if (err)
4763 cp_parser_simulate_error (parser);
4764 }
4765 }
4766 };
4767
4768 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4769 This class is for tracking such a matching pair of symbols.
4770 In particular, it tracks the location of the first token,
4771 so that if the second token is missing, we can highlight the
4772 location of the first token when notifying the user about the
4773 problem. */
4774
4775 template <typename traits_t>
4776 class token_pair
4777 {
4778 public:
4779 /* token_pair's ctor. */
4780 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4781
4782 /* If the next token is the opening symbol for this pair, consume it and
4783 return true.
4784 Otherwise, issue an error and return false.
4785 In either case, record the location of the opening token. */
4786
4787 bool require_open (cp_parser *parser)
4788 {
4789 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4790 return cp_parser_require (parser, traits_t::open_token_type,
4791 traits_t::required_token_open);
4792 }
4793
4794 /* Consume the next token from PARSER, recording its location as
4795 that of the opening token within the pair. */
4796
4797 cp_token * consume_open (cp_parser *parser)
4798 {
4799 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4800 gcc_assert (tok->type == traits_t::open_token_type);
4801 m_open_loc = tok->location;
4802 return tok;
4803 }
4804
4805 /* If the next token is the closing symbol for this pair, consume it
4806 and return it.
4807 Otherwise, issue an error, highlighting the location of the
4808 corresponding opening token, and return NULL. */
4809
4810 cp_token *require_close (cp_parser *parser) const
4811 {
4812 return cp_parser_require (parser, traits_t::close_token_type,
4813 traits_t::required_token_close,
4814 m_open_loc);
4815 }
4816
4817 private:
4818 location_t m_open_loc;
4819 };
4820
4821 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4822
4823 struct matching_paren_traits
4824 {
4825 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4826 static const enum required_token required_token_open = RT_OPEN_PAREN;
4827 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4828 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4829 };
4830
4831 /* "matching_parens" is a token_pair<T> class for tracking matching
4832 pairs of parentheses. */
4833
4834 typedef token_pair<matching_paren_traits> matching_parens;
4835
4836 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4837
4838 struct matching_brace_traits
4839 {
4840 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4841 static const enum required_token required_token_open = RT_OPEN_BRACE;
4842 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4843 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4844 };
4845
4846 /* "matching_braces" is a token_pair<T> class for tracking matching
4847 pairs of braces. */
4848
4849 typedef token_pair<matching_brace_traits> matching_braces;
4850
4851
4852 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4853 enclosing parentheses. */
4854
4855 static cp_expr
4856 cp_parser_statement_expr (cp_parser *parser)
4857 {
4858 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4859
4860 /* Consume the '('. */
4861 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4862 matching_parens parens;
4863 parens.consume_open (parser);
4864 /* Start the statement-expression. */
4865 tree expr = begin_stmt_expr ();
4866 /* Parse the compound-statement. */
4867 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4868 /* Finish up. */
4869 expr = finish_stmt_expr (expr, false);
4870 /* Consume the ')'. */
4871 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4872 if (!parens.require_close (parser))
4873 cp_parser_skip_to_end_of_statement (parser);
4874
4875 cp_parser_end_tentative_firewall (parser, start, expr);
4876 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4877 return cp_expr (expr, combined_loc);
4878 }
4879
4880 /* Expressions [gram.expr] */
4881
4882 /* Parse a fold-operator.
4883
4884 fold-operator:
4885 - * / % ^ & | = < > << >>
4886 = -= *= /= %= ^= &= |= <<= >>=
4887 == != <= >= && || , .* ->*
4888
4889 This returns the tree code corresponding to the matched operator
4890 as an int. When the current token matches a compound assignment
4891 opertor, the resulting tree code is the negative value of the
4892 non-assignment operator. */
4893
4894 static int
4895 cp_parser_fold_operator (cp_token *token)
4896 {
4897 switch (token->type)
4898 {
4899 case CPP_PLUS: return PLUS_EXPR;
4900 case CPP_MINUS: return MINUS_EXPR;
4901 case CPP_MULT: return MULT_EXPR;
4902 case CPP_DIV: return TRUNC_DIV_EXPR;
4903 case CPP_MOD: return TRUNC_MOD_EXPR;
4904 case CPP_XOR: return BIT_XOR_EXPR;
4905 case CPP_AND: return BIT_AND_EXPR;
4906 case CPP_OR: return BIT_IOR_EXPR;
4907 case CPP_LSHIFT: return LSHIFT_EXPR;
4908 case CPP_RSHIFT: return RSHIFT_EXPR;
4909
4910 case CPP_EQ: return -NOP_EXPR;
4911 case CPP_PLUS_EQ: return -PLUS_EXPR;
4912 case CPP_MINUS_EQ: return -MINUS_EXPR;
4913 case CPP_MULT_EQ: return -MULT_EXPR;
4914 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4915 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4916 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4917 case CPP_AND_EQ: return -BIT_AND_EXPR;
4918 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4919 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4920 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4921
4922 case CPP_EQ_EQ: return EQ_EXPR;
4923 case CPP_NOT_EQ: return NE_EXPR;
4924 case CPP_LESS: return LT_EXPR;
4925 case CPP_GREATER: return GT_EXPR;
4926 case CPP_LESS_EQ: return LE_EXPR;
4927 case CPP_GREATER_EQ: return GE_EXPR;
4928
4929 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4930 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4931
4932 case CPP_COMMA: return COMPOUND_EXPR;
4933
4934 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4935 case CPP_DEREF_STAR: return MEMBER_REF;
4936
4937 default: return ERROR_MARK;
4938 }
4939 }
4940
4941 /* Returns true if CODE indicates a binary expression, which is not allowed in
4942 the LHS of a fold-expression. More codes will need to be added to use this
4943 function in other contexts. */
4944
4945 static bool
4946 is_binary_op (tree_code code)
4947 {
4948 switch (code)
4949 {
4950 case PLUS_EXPR:
4951 case POINTER_PLUS_EXPR:
4952 case MINUS_EXPR:
4953 case MULT_EXPR:
4954 case TRUNC_DIV_EXPR:
4955 case TRUNC_MOD_EXPR:
4956 case BIT_XOR_EXPR:
4957 case BIT_AND_EXPR:
4958 case BIT_IOR_EXPR:
4959 case LSHIFT_EXPR:
4960 case RSHIFT_EXPR:
4961
4962 case MODOP_EXPR:
4963
4964 case EQ_EXPR:
4965 case NE_EXPR:
4966 case LE_EXPR:
4967 case GE_EXPR:
4968 case LT_EXPR:
4969 case GT_EXPR:
4970
4971 case TRUTH_ANDIF_EXPR:
4972 case TRUTH_ORIF_EXPR:
4973
4974 case COMPOUND_EXPR:
4975
4976 case DOTSTAR_EXPR:
4977 case MEMBER_REF:
4978 return true;
4979
4980 default:
4981 return false;
4982 }
4983 }
4984
4985 /* If the next token is a suitable fold operator, consume it and return as
4986 the function above. */
4987
4988 static int
4989 cp_parser_fold_operator (cp_parser *parser)
4990 {
4991 cp_token* token = cp_lexer_peek_token (parser->lexer);
4992 int code = cp_parser_fold_operator (token);
4993 if (code != ERROR_MARK)
4994 cp_lexer_consume_token (parser->lexer);
4995 return code;
4996 }
4997
4998 /* Parse a fold-expression.
4999
5000 fold-expression:
5001 ( ... folding-operator cast-expression)
5002 ( cast-expression folding-operator ... )
5003 ( cast-expression folding operator ... folding-operator cast-expression)
5004
5005 Note that the '(' and ')' are matched in primary expression. */
5006
5007 static cp_expr
5008 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5009 {
5010 cp_id_kind pidk;
5011
5012 // Left fold.
5013 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5014 {
5015 cp_lexer_consume_token (parser->lexer);
5016 int op = cp_parser_fold_operator (parser);
5017 if (op == ERROR_MARK)
5018 {
5019 cp_parser_error (parser, "expected binary operator");
5020 return error_mark_node;
5021 }
5022
5023 tree expr = cp_parser_cast_expression (parser, false, false,
5024 false, &pidk);
5025 if (expr == error_mark_node)
5026 return error_mark_node;
5027 return finish_left_unary_fold_expr (expr, op);
5028 }
5029
5030 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5031 int op = cp_parser_fold_operator (parser);
5032 if (op == ERROR_MARK)
5033 {
5034 cp_parser_error (parser, "expected binary operator");
5035 return error_mark_node;
5036 }
5037
5038 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5039 {
5040 cp_parser_error (parser, "expected ...");
5041 return error_mark_node;
5042 }
5043 cp_lexer_consume_token (parser->lexer);
5044
5045 /* The operands of a fold-expression are cast-expressions, so binary or
5046 conditional expressions are not allowed. We check this here to avoid
5047 tentative parsing. */
5048 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5049 /* OK, the expression was parenthesized. */;
5050 else if (is_binary_op (TREE_CODE (expr1)))
5051 error_at (location_of (expr1),
5052 "binary expression in operand of fold-expression");
5053 else if (TREE_CODE (expr1) == COND_EXPR
5054 || (REFERENCE_REF_P (expr1)
5055 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5056 error_at (location_of (expr1),
5057 "conditional expression in operand of fold-expression");
5058
5059 // Right fold.
5060 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5061 return finish_right_unary_fold_expr (expr1, op);
5062
5063 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5064 {
5065 cp_parser_error (parser, "mismatched operator in fold-expression");
5066 return error_mark_node;
5067 }
5068 cp_lexer_consume_token (parser->lexer);
5069
5070 // Binary left or right fold.
5071 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5072 if (expr2 == error_mark_node)
5073 return error_mark_node;
5074 return finish_binary_fold_expr (expr1, expr2, op);
5075 }
5076
5077 /* Parse a primary-expression.
5078
5079 primary-expression:
5080 literal
5081 this
5082 ( expression )
5083 id-expression
5084 lambda-expression (C++11)
5085
5086 GNU Extensions:
5087
5088 primary-expression:
5089 ( compound-statement )
5090 __builtin_va_arg ( assignment-expression , type-id )
5091 __builtin_offsetof ( type-id , offsetof-expression )
5092
5093 C++ Extensions:
5094 __has_nothrow_assign ( type-id )
5095 __has_nothrow_constructor ( type-id )
5096 __has_nothrow_copy ( type-id )
5097 __has_trivial_assign ( type-id )
5098 __has_trivial_constructor ( type-id )
5099 __has_trivial_copy ( type-id )
5100 __has_trivial_destructor ( type-id )
5101 __has_virtual_destructor ( type-id )
5102 __is_abstract ( type-id )
5103 __is_base_of ( type-id , type-id )
5104 __is_class ( type-id )
5105 __is_empty ( type-id )
5106 __is_enum ( type-id )
5107 __is_final ( type-id )
5108 __is_literal_type ( type-id )
5109 __is_pod ( type-id )
5110 __is_polymorphic ( type-id )
5111 __is_std_layout ( type-id )
5112 __is_trivial ( type-id )
5113 __is_union ( type-id )
5114
5115 Objective-C++ Extension:
5116
5117 primary-expression:
5118 objc-expression
5119
5120 literal:
5121 __null
5122
5123 ADDRESS_P is true iff this expression was immediately preceded by
5124 "&" and therefore might denote a pointer-to-member. CAST_P is true
5125 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5126 true iff this expression is a template argument.
5127
5128 Returns a representation of the expression. Upon return, *IDK
5129 indicates what kind of id-expression (if any) was present. */
5130
5131 static cp_expr
5132 cp_parser_primary_expression (cp_parser *parser,
5133 bool address_p,
5134 bool cast_p,
5135 bool template_arg_p,
5136 bool decltype_p,
5137 cp_id_kind *idk)
5138 {
5139 cp_token *token = NULL;
5140
5141 /* Assume the primary expression is not an id-expression. */
5142 *idk = CP_ID_KIND_NONE;
5143
5144 /* Peek at the next token. */
5145 token = cp_lexer_peek_token (parser->lexer);
5146 switch ((int) token->type)
5147 {
5148 /* literal:
5149 integer-literal
5150 character-literal
5151 floating-literal
5152 string-literal
5153 boolean-literal
5154 pointer-literal
5155 user-defined-literal */
5156 case CPP_CHAR:
5157 case CPP_CHAR16:
5158 case CPP_CHAR32:
5159 case CPP_WCHAR:
5160 case CPP_UTF8CHAR:
5161 case CPP_NUMBER:
5162 case CPP_PREPARSED_EXPR:
5163 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5164 return cp_parser_userdef_numeric_literal (parser);
5165 token = cp_lexer_consume_token (parser->lexer);
5166 if (TREE_CODE (token->u.value) == FIXED_CST)
5167 {
5168 error_at (token->location,
5169 "fixed-point types not supported in C++");
5170 return error_mark_node;
5171 }
5172 /* Floating-point literals are only allowed in an integral
5173 constant expression if they are cast to an integral or
5174 enumeration type. */
5175 if (TREE_CODE (token->u.value) == REAL_CST
5176 && parser->integral_constant_expression_p
5177 && pedantic)
5178 {
5179 /* CAST_P will be set even in invalid code like "int(2.7 +
5180 ...)". Therefore, we have to check that the next token
5181 is sure to end the cast. */
5182 if (cast_p)
5183 {
5184 cp_token *next_token;
5185
5186 next_token = cp_lexer_peek_token (parser->lexer);
5187 if (/* The comma at the end of an
5188 enumerator-definition. */
5189 next_token->type != CPP_COMMA
5190 /* The curly brace at the end of an enum-specifier. */
5191 && next_token->type != CPP_CLOSE_BRACE
5192 /* The end of a statement. */
5193 && next_token->type != CPP_SEMICOLON
5194 /* The end of the cast-expression. */
5195 && next_token->type != CPP_CLOSE_PAREN
5196 /* The end of an array bound. */
5197 && next_token->type != CPP_CLOSE_SQUARE
5198 /* The closing ">" in a template-argument-list. */
5199 && (next_token->type != CPP_GREATER
5200 || parser->greater_than_is_operator_p)
5201 /* C++0x only: A ">>" treated like two ">" tokens,
5202 in a template-argument-list. */
5203 && (next_token->type != CPP_RSHIFT
5204 || (cxx_dialect == cxx98)
5205 || parser->greater_than_is_operator_p))
5206 cast_p = false;
5207 }
5208
5209 /* If we are within a cast, then the constraint that the
5210 cast is to an integral or enumeration type will be
5211 checked at that point. If we are not within a cast, then
5212 this code is invalid. */
5213 if (!cast_p)
5214 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5215 }
5216 return cp_expr (token->u.value, token->location);
5217
5218 case CPP_CHAR_USERDEF:
5219 case CPP_CHAR16_USERDEF:
5220 case CPP_CHAR32_USERDEF:
5221 case CPP_WCHAR_USERDEF:
5222 case CPP_UTF8CHAR_USERDEF:
5223 return cp_parser_userdef_char_literal (parser);
5224
5225 case CPP_STRING:
5226 case CPP_STRING16:
5227 case CPP_STRING32:
5228 case CPP_WSTRING:
5229 case CPP_UTF8STRING:
5230 case CPP_STRING_USERDEF:
5231 case CPP_STRING16_USERDEF:
5232 case CPP_STRING32_USERDEF:
5233 case CPP_WSTRING_USERDEF:
5234 case CPP_UTF8STRING_USERDEF:
5235 /* ??? Should wide strings be allowed when parser->translate_strings_p
5236 is false (i.e. in attributes)? If not, we can kill the third
5237 argument to cp_parser_string_literal. */
5238 return cp_parser_string_literal (parser,
5239 parser->translate_strings_p,
5240 true);
5241
5242 case CPP_OPEN_PAREN:
5243 /* If we see `( { ' then we are looking at the beginning of
5244 a GNU statement-expression. */
5245 if (cp_parser_allow_gnu_extensions_p (parser)
5246 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5247 {
5248 /* Statement-expressions are not allowed by the standard. */
5249 pedwarn (token->location, OPT_Wpedantic,
5250 "ISO C++ forbids braced-groups within expressions");
5251
5252 /* And they're not allowed outside of a function-body; you
5253 cannot, for example, write:
5254
5255 int i = ({ int j = 3; j + 1; });
5256
5257 at class or namespace scope. */
5258 if (!parser->in_function_body
5259 || parser->in_template_argument_list_p)
5260 {
5261 error_at (token->location,
5262 "statement-expressions are not allowed outside "
5263 "functions nor in template-argument lists");
5264 cp_parser_skip_to_end_of_block_or_statement (parser);
5265 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5266 cp_lexer_consume_token (parser->lexer);
5267 return error_mark_node;
5268 }
5269 else
5270 return cp_parser_statement_expr (parser);
5271 }
5272 /* Otherwise it's a normal parenthesized expression. */
5273 {
5274 cp_expr expr;
5275 bool saved_greater_than_is_operator_p;
5276
5277 location_t open_paren_loc = token->location;
5278
5279 /* Consume the `('. */
5280 matching_parens parens;
5281 parens.consume_open (parser);
5282 /* Within a parenthesized expression, a `>' token is always
5283 the greater-than operator. */
5284 saved_greater_than_is_operator_p
5285 = parser->greater_than_is_operator_p;
5286 parser->greater_than_is_operator_p = true;
5287
5288 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5289 /* Left fold expression. */
5290 expr = NULL_TREE;
5291 else
5292 /* Parse the parenthesized expression. */
5293 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5294
5295 token = cp_lexer_peek_token (parser->lexer);
5296 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5297 {
5298 expr = cp_parser_fold_expression (parser, expr);
5299 if (expr != error_mark_node
5300 && cxx_dialect < cxx17
5301 && !in_system_header_at (input_location))
5302 pedwarn (input_location, 0, "fold-expressions only available "
5303 "with -std=c++17 or -std=gnu++17");
5304 }
5305 else
5306 /* Let the front end know that this expression was
5307 enclosed in parentheses. This matters in case, for
5308 example, the expression is of the form `A::B', since
5309 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5310 not. */
5311 expr = finish_parenthesized_expr (expr);
5312
5313 /* DR 705: Wrapping an unqualified name in parentheses
5314 suppresses arg-dependent lookup. We want to pass back
5315 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5316 (c++/37862), but none of the others. */
5317 if (*idk != CP_ID_KIND_QUALIFIED)
5318 *idk = CP_ID_KIND_NONE;
5319
5320 /* The `>' token might be the end of a template-id or
5321 template-parameter-list now. */
5322 parser->greater_than_is_operator_p
5323 = saved_greater_than_is_operator_p;
5324
5325 /* Consume the `)'. */
5326 token = cp_lexer_peek_token (parser->lexer);
5327 location_t close_paren_loc = token->location;
5328 expr.set_range (open_paren_loc, close_paren_loc);
5329 if (!parens.require_close (parser)
5330 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5331 cp_parser_skip_to_end_of_statement (parser);
5332
5333 return expr;
5334 }
5335
5336 case CPP_OPEN_SQUARE:
5337 {
5338 if (c_dialect_objc ())
5339 {
5340 /* We might have an Objective-C++ message. */
5341 cp_parser_parse_tentatively (parser);
5342 tree msg = cp_parser_objc_message_expression (parser);
5343 /* If that works out, we're done ... */
5344 if (cp_parser_parse_definitely (parser))
5345 return msg;
5346 /* ... else, fall though to see if it's a lambda. */
5347 }
5348 cp_expr lam = cp_parser_lambda_expression (parser);
5349 /* Don't warn about a failed tentative parse. */
5350 if (cp_parser_error_occurred (parser))
5351 return error_mark_node;
5352 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5353 return lam;
5354 }
5355
5356 case CPP_OBJC_STRING:
5357 if (c_dialect_objc ())
5358 /* We have an Objective-C++ string literal. */
5359 return cp_parser_objc_expression (parser);
5360 cp_parser_error (parser, "expected primary-expression");
5361 return error_mark_node;
5362
5363 case CPP_KEYWORD:
5364 switch (token->keyword)
5365 {
5366 /* These two are the boolean literals. */
5367 case RID_TRUE:
5368 cp_lexer_consume_token (parser->lexer);
5369 return cp_expr (boolean_true_node, token->location);
5370 case RID_FALSE:
5371 cp_lexer_consume_token (parser->lexer);
5372 return cp_expr (boolean_false_node, token->location);
5373
5374 /* The `__null' literal. */
5375 case RID_NULL:
5376 cp_lexer_consume_token (parser->lexer);
5377 return cp_expr (null_node, token->location);
5378
5379 /* The `nullptr' literal. */
5380 case RID_NULLPTR:
5381 cp_lexer_consume_token (parser->lexer);
5382 return cp_expr (nullptr_node, token->location);
5383
5384 /* Recognize the `this' keyword. */
5385 case RID_THIS:
5386 cp_lexer_consume_token (parser->lexer);
5387 if (parser->local_variables_forbidden_p)
5388 {
5389 error_at (token->location,
5390 "%<this%> may not be used in this context");
5391 return error_mark_node;
5392 }
5393 /* Pointers cannot appear in constant-expressions. */
5394 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5395 return error_mark_node;
5396 return cp_expr (finish_this_expr (), token->location);
5397
5398 /* The `operator' keyword can be the beginning of an
5399 id-expression. */
5400 case RID_OPERATOR:
5401 goto id_expression;
5402
5403 case RID_FUNCTION_NAME:
5404 case RID_PRETTY_FUNCTION_NAME:
5405 case RID_C99_FUNCTION_NAME:
5406 {
5407 non_integral_constant name;
5408
5409 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5410 __func__ are the names of variables -- but they are
5411 treated specially. Therefore, they are handled here,
5412 rather than relying on the generic id-expression logic
5413 below. Grammatically, these names are id-expressions.
5414
5415 Consume the token. */
5416 token = cp_lexer_consume_token (parser->lexer);
5417
5418 switch (token->keyword)
5419 {
5420 case RID_FUNCTION_NAME:
5421 name = NIC_FUNC_NAME;
5422 break;
5423 case RID_PRETTY_FUNCTION_NAME:
5424 name = NIC_PRETTY_FUNC;
5425 break;
5426 case RID_C99_FUNCTION_NAME:
5427 name = NIC_C99_FUNC;
5428 break;
5429 default:
5430 gcc_unreachable ();
5431 }
5432
5433 if (cp_parser_non_integral_constant_expression (parser, name))
5434 return error_mark_node;
5435
5436 /* Look up the name. */
5437 return finish_fname (token->u.value);
5438 }
5439
5440 case RID_VA_ARG:
5441 {
5442 tree expression;
5443 tree type;
5444 source_location type_location;
5445 location_t start_loc
5446 = cp_lexer_peek_token (parser->lexer)->location;
5447 /* The `__builtin_va_arg' construct is used to handle
5448 `va_arg'. Consume the `__builtin_va_arg' token. */
5449 cp_lexer_consume_token (parser->lexer);
5450 /* Look for the opening `('. */
5451 matching_parens parens;
5452 parens.require_open (parser);
5453 /* Now, parse the assignment-expression. */
5454 expression = cp_parser_assignment_expression (parser);
5455 /* Look for the `,'. */
5456 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5457 type_location = cp_lexer_peek_token (parser->lexer)->location;
5458 /* Parse the type-id. */
5459 {
5460 type_id_in_expr_sentinel s (parser);
5461 type = cp_parser_type_id (parser);
5462 }
5463 /* Look for the closing `)'. */
5464 location_t finish_loc
5465 = cp_lexer_peek_token (parser->lexer)->location;
5466 parens.require_close (parser);
5467 /* Using `va_arg' in a constant-expression is not
5468 allowed. */
5469 if (cp_parser_non_integral_constant_expression (parser,
5470 NIC_VA_ARG))
5471 return error_mark_node;
5472 /* Construct a location of the form:
5473 __builtin_va_arg (v, int)
5474 ~~~~~~~~~~~~~~~~~~~~~^~~~
5475 with the caret at the type, ranging from the start of the
5476 "__builtin_va_arg" token to the close paren. */
5477 location_t combined_loc
5478 = make_location (type_location, start_loc, finish_loc);
5479 return build_x_va_arg (combined_loc, expression, type);
5480 }
5481
5482 case RID_OFFSETOF:
5483 return cp_parser_builtin_offsetof (parser);
5484
5485 case RID_HAS_NOTHROW_ASSIGN:
5486 case RID_HAS_NOTHROW_CONSTRUCTOR:
5487 case RID_HAS_NOTHROW_COPY:
5488 case RID_HAS_TRIVIAL_ASSIGN:
5489 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5490 case RID_HAS_TRIVIAL_COPY:
5491 case RID_HAS_TRIVIAL_DESTRUCTOR:
5492 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5493 case RID_HAS_VIRTUAL_DESTRUCTOR:
5494 case RID_IS_ABSTRACT:
5495 case RID_IS_AGGREGATE:
5496 case RID_IS_BASE_OF:
5497 case RID_IS_CLASS:
5498 case RID_IS_EMPTY:
5499 case RID_IS_ENUM:
5500 case RID_IS_FINAL:
5501 case RID_IS_LITERAL_TYPE:
5502 case RID_IS_POD:
5503 case RID_IS_POLYMORPHIC:
5504 case RID_IS_SAME_AS:
5505 case RID_IS_STD_LAYOUT:
5506 case RID_IS_TRIVIAL:
5507 case RID_IS_TRIVIALLY_ASSIGNABLE:
5508 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5509 case RID_IS_TRIVIALLY_COPYABLE:
5510 case RID_IS_UNION:
5511 case RID_IS_ASSIGNABLE:
5512 case RID_IS_CONSTRUCTIBLE:
5513 return cp_parser_trait_expr (parser, token->keyword);
5514
5515 // C++ concepts
5516 case RID_REQUIRES:
5517 return cp_parser_requires_expression (parser);
5518
5519 /* Objective-C++ expressions. */
5520 case RID_AT_ENCODE:
5521 case RID_AT_PROTOCOL:
5522 case RID_AT_SELECTOR:
5523 return cp_parser_objc_expression (parser);
5524
5525 case RID_TEMPLATE:
5526 if (parser->in_function_body
5527 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5528 == CPP_LESS))
5529 {
5530 error_at (token->location,
5531 "a template declaration cannot appear at block scope");
5532 cp_parser_skip_to_end_of_block_or_statement (parser);
5533 return error_mark_node;
5534 }
5535 /* FALLTHRU */
5536 default:
5537 cp_parser_error (parser, "expected primary-expression");
5538 return error_mark_node;
5539 }
5540
5541 /* An id-expression can start with either an identifier, a
5542 `::' as the beginning of a qualified-id, or the "operator"
5543 keyword. */
5544 case CPP_NAME:
5545 case CPP_SCOPE:
5546 case CPP_TEMPLATE_ID:
5547 case CPP_NESTED_NAME_SPECIFIER:
5548 {
5549 id_expression:
5550 cp_expr id_expression;
5551 cp_expr decl;
5552 const char *error_msg;
5553 bool template_p;
5554 bool done;
5555 cp_token *id_expr_token;
5556
5557 /* Parse the id-expression. */
5558 id_expression
5559 = cp_parser_id_expression (parser,
5560 /*template_keyword_p=*/false,
5561 /*check_dependency_p=*/true,
5562 &template_p,
5563 /*declarator_p=*/false,
5564 /*optional_p=*/false);
5565 if (id_expression == error_mark_node)
5566 return error_mark_node;
5567 id_expr_token = token;
5568 token = cp_lexer_peek_token (parser->lexer);
5569 done = (token->type != CPP_OPEN_SQUARE
5570 && token->type != CPP_OPEN_PAREN
5571 && token->type != CPP_DOT
5572 && token->type != CPP_DEREF
5573 && token->type != CPP_PLUS_PLUS
5574 && token->type != CPP_MINUS_MINUS);
5575 /* If we have a template-id, then no further lookup is
5576 required. If the template-id was for a template-class, we
5577 will sometimes have a TYPE_DECL at this point. */
5578 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5579 || TREE_CODE (id_expression) == TYPE_DECL)
5580 decl = id_expression;
5581 /* Look up the name. */
5582 else
5583 {
5584 tree ambiguous_decls;
5585
5586 /* If we already know that this lookup is ambiguous, then
5587 we've already issued an error message; there's no reason
5588 to check again. */
5589 if (id_expr_token->type == CPP_NAME
5590 && id_expr_token->error_reported)
5591 {
5592 cp_parser_simulate_error (parser);
5593 return error_mark_node;
5594 }
5595
5596 decl = cp_parser_lookup_name (parser, id_expression,
5597 none_type,
5598 template_p,
5599 /*is_namespace=*/false,
5600 /*check_dependency=*/true,
5601 &ambiguous_decls,
5602 id_expr_token->location);
5603 /* If the lookup was ambiguous, an error will already have
5604 been issued. */
5605 if (ambiguous_decls)
5606 return error_mark_node;
5607
5608 /* In Objective-C++, we may have an Objective-C 2.0
5609 dot-syntax for classes here. */
5610 if (c_dialect_objc ()
5611 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5612 && TREE_CODE (decl) == TYPE_DECL
5613 && objc_is_class_name (decl))
5614 {
5615 tree component;
5616 cp_lexer_consume_token (parser->lexer);
5617 component = cp_parser_identifier (parser);
5618 if (component == error_mark_node)
5619 return error_mark_node;
5620
5621 tree result = objc_build_class_component_ref (id_expression,
5622 component);
5623 /* Build a location of the form:
5624 expr.component
5625 ~~~~~^~~~~~~~~
5626 with caret at the start of the component name (at
5627 input_location), ranging from the start of the id_expression
5628 to the end of the component name. */
5629 location_t combined_loc
5630 = make_location (input_location, id_expression.get_start (),
5631 get_finish (input_location));
5632 protected_set_expr_location (result, combined_loc);
5633 return result;
5634 }
5635
5636 /* In Objective-C++, an instance variable (ivar) may be preferred
5637 to whatever cp_parser_lookup_name() found.
5638 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5639 rest of c-family, we have to do a little extra work to preserve
5640 any location information in cp_expr "decl". Given that
5641 objc_lookup_ivar is implemented in "c-family" and "objc", we
5642 have a trip through the pure "tree" type, rather than cp_expr.
5643 Naively copying it back to "decl" would implicitly give the
5644 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5645 store an EXPR_LOCATION. Hence we only update "decl" (and
5646 hence its location_t) if we get back a different tree node. */
5647 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5648 id_expression);
5649 if (decl_tree != decl.get_value ())
5650 decl = cp_expr (decl_tree);
5651
5652 /* If name lookup gives us a SCOPE_REF, then the
5653 qualifying scope was dependent. */
5654 if (TREE_CODE (decl) == SCOPE_REF)
5655 {
5656 /* At this point, we do not know if DECL is a valid
5657 integral constant expression. We assume that it is
5658 in fact such an expression, so that code like:
5659
5660 template <int N> struct A {
5661 int a[B<N>::i];
5662 };
5663
5664 is accepted. At template-instantiation time, we
5665 will check that B<N>::i is actually a constant. */
5666 return decl;
5667 }
5668 /* Check to see if DECL is a local variable in a context
5669 where that is forbidden. */
5670 if (parser->local_variables_forbidden_p
5671 && local_variable_p (decl))
5672 {
5673 error_at (id_expr_token->location,
5674 "local variable %qD may not appear in this context",
5675 decl.get_value ());
5676 return error_mark_node;
5677 }
5678 }
5679
5680 if (processing_template_decl)
5681 if (tree fns = maybe_get_fns (decl))
5682 /* It's too difficult to mark ths in all the places where
5683 we know for sure we need to keep the lookup, so do it
5684 now. The cost is extra GC to recycle the lookups
5685 resolved at parse time. */
5686 lookup_keep (fns);
5687
5688 decl = (finish_id_expression
5689 (id_expression, decl, parser->scope,
5690 idk,
5691 parser->integral_constant_expression_p,
5692 parser->allow_non_integral_constant_expression_p,
5693 &parser->non_integral_constant_expression_p,
5694 template_p, done, address_p,
5695 template_arg_p,
5696 &error_msg,
5697 id_expression.get_location ()));
5698 if (error_msg)
5699 cp_parser_error (parser, error_msg);
5700 decl.set_location (id_expr_token->location);
5701 return decl;
5702 }
5703
5704 /* Anything else is an error. */
5705 default:
5706 cp_parser_error (parser, "expected primary-expression");
5707 return error_mark_node;
5708 }
5709 }
5710
5711 static inline cp_expr
5712 cp_parser_primary_expression (cp_parser *parser,
5713 bool address_p,
5714 bool cast_p,
5715 bool template_arg_p,
5716 cp_id_kind *idk)
5717 {
5718 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5719 /*decltype*/false, idk);
5720 }
5721
5722 /* Parse an id-expression.
5723
5724 id-expression:
5725 unqualified-id
5726 qualified-id
5727
5728 qualified-id:
5729 :: [opt] nested-name-specifier template [opt] unqualified-id
5730 :: identifier
5731 :: operator-function-id
5732 :: template-id
5733
5734 Return a representation of the unqualified portion of the
5735 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5736 a `::' or nested-name-specifier.
5737
5738 Often, if the id-expression was a qualified-id, the caller will
5739 want to make a SCOPE_REF to represent the qualified-id. This
5740 function does not do this in order to avoid wastefully creating
5741 SCOPE_REFs when they are not required.
5742
5743 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5744 `template' keyword.
5745
5746 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5747 uninstantiated templates.
5748
5749 If *TEMPLATE_P is non-NULL, it is set to true iff the
5750 `template' keyword is used to explicitly indicate that the entity
5751 named is a template.
5752
5753 If DECLARATOR_P is true, the id-expression is appearing as part of
5754 a declarator, rather than as part of an expression. */
5755
5756 static cp_expr
5757 cp_parser_id_expression (cp_parser *parser,
5758 bool template_keyword_p,
5759 bool check_dependency_p,
5760 bool *template_p,
5761 bool declarator_p,
5762 bool optional_p)
5763 {
5764 bool global_scope_p;
5765 bool nested_name_specifier_p;
5766
5767 /* Assume the `template' keyword was not used. */
5768 if (template_p)
5769 *template_p = template_keyword_p;
5770
5771 /* Look for the optional `::' operator. */
5772 global_scope_p
5773 = (!template_keyword_p
5774 && (cp_parser_global_scope_opt (parser,
5775 /*current_scope_valid_p=*/false)
5776 != NULL_TREE));
5777
5778 /* Look for the optional nested-name-specifier. */
5779 nested_name_specifier_p
5780 = (cp_parser_nested_name_specifier_opt (parser,
5781 /*typename_keyword_p=*/false,
5782 check_dependency_p,
5783 /*type_p=*/false,
5784 declarator_p,
5785 template_keyword_p)
5786 != NULL_TREE);
5787
5788 /* If there is a nested-name-specifier, then we are looking at
5789 the first qualified-id production. */
5790 if (nested_name_specifier_p)
5791 {
5792 tree saved_scope;
5793 tree saved_object_scope;
5794 tree saved_qualifying_scope;
5795 cp_expr unqualified_id;
5796 bool is_template;
5797
5798 /* See if the next token is the `template' keyword. */
5799 if (!template_p)
5800 template_p = &is_template;
5801 *template_p = cp_parser_optional_template_keyword (parser);
5802 /* Name lookup we do during the processing of the
5803 unqualified-id might obliterate SCOPE. */
5804 saved_scope = parser->scope;
5805 saved_object_scope = parser->object_scope;
5806 saved_qualifying_scope = parser->qualifying_scope;
5807 /* Process the final unqualified-id. */
5808 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5809 check_dependency_p,
5810 declarator_p,
5811 /*optional_p=*/false);
5812 /* Restore the SAVED_SCOPE for our caller. */
5813 parser->scope = saved_scope;
5814 parser->object_scope = saved_object_scope;
5815 parser->qualifying_scope = saved_qualifying_scope;
5816
5817 return unqualified_id;
5818 }
5819 /* Otherwise, if we are in global scope, then we are looking at one
5820 of the other qualified-id productions. */
5821 else if (global_scope_p)
5822 {
5823 cp_token *token;
5824 tree id;
5825
5826 /* Peek at the next token. */
5827 token = cp_lexer_peek_token (parser->lexer);
5828
5829 /* If it's an identifier, and the next token is not a "<", then
5830 we can avoid the template-id case. This is an optimization
5831 for this common case. */
5832 if (token->type == CPP_NAME
5833 && !cp_parser_nth_token_starts_template_argument_list_p
5834 (parser, 2))
5835 return cp_parser_identifier (parser);
5836
5837 cp_parser_parse_tentatively (parser);
5838 /* Try a template-id. */
5839 id = cp_parser_template_id (parser,
5840 /*template_keyword_p=*/false,
5841 /*check_dependency_p=*/true,
5842 none_type,
5843 declarator_p);
5844 /* If that worked, we're done. */
5845 if (cp_parser_parse_definitely (parser))
5846 return id;
5847
5848 /* Peek at the next token. (Changes in the token buffer may
5849 have invalidated the pointer obtained above.) */
5850 token = cp_lexer_peek_token (parser->lexer);
5851
5852 switch (token->type)
5853 {
5854 case CPP_NAME:
5855 return cp_parser_identifier (parser);
5856
5857 case CPP_KEYWORD:
5858 if (token->keyword == RID_OPERATOR)
5859 return cp_parser_operator_function_id (parser);
5860 /* Fall through. */
5861
5862 default:
5863 cp_parser_error (parser, "expected id-expression");
5864 return error_mark_node;
5865 }
5866 }
5867 else
5868 return cp_parser_unqualified_id (parser, template_keyword_p,
5869 /*check_dependency_p=*/true,
5870 declarator_p,
5871 optional_p);
5872 }
5873
5874 /* Parse an unqualified-id.
5875
5876 unqualified-id:
5877 identifier
5878 operator-function-id
5879 conversion-function-id
5880 ~ class-name
5881 template-id
5882
5883 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5884 keyword, in a construct like `A::template ...'.
5885
5886 Returns a representation of unqualified-id. For the `identifier'
5887 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5888 production a BIT_NOT_EXPR is returned; the operand of the
5889 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5890 other productions, see the documentation accompanying the
5891 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5892 names are looked up in uninstantiated templates. If DECLARATOR_P
5893 is true, the unqualified-id is appearing as part of a declarator,
5894 rather than as part of an expression. */
5895
5896 static cp_expr
5897 cp_parser_unqualified_id (cp_parser* parser,
5898 bool template_keyword_p,
5899 bool check_dependency_p,
5900 bool declarator_p,
5901 bool optional_p)
5902 {
5903 cp_token *token;
5904
5905 /* Peek at the next token. */
5906 token = cp_lexer_peek_token (parser->lexer);
5907
5908 switch ((int) token->type)
5909 {
5910 case CPP_NAME:
5911 {
5912 tree id;
5913
5914 /* We don't know yet whether or not this will be a
5915 template-id. */
5916 cp_parser_parse_tentatively (parser);
5917 /* Try a template-id. */
5918 id = cp_parser_template_id (parser, template_keyword_p,
5919 check_dependency_p,
5920 none_type,
5921 declarator_p);
5922 /* If it worked, we're done. */
5923 if (cp_parser_parse_definitely (parser))
5924 return id;
5925 /* Otherwise, it's an ordinary identifier. */
5926 return cp_parser_identifier (parser);
5927 }
5928
5929 case CPP_TEMPLATE_ID:
5930 return cp_parser_template_id (parser, template_keyword_p,
5931 check_dependency_p,
5932 none_type,
5933 declarator_p);
5934
5935 case CPP_COMPL:
5936 {
5937 tree type_decl;
5938 tree qualifying_scope;
5939 tree object_scope;
5940 tree scope;
5941 bool done;
5942
5943 /* Consume the `~' token. */
5944 cp_lexer_consume_token (parser->lexer);
5945 /* Parse the class-name. The standard, as written, seems to
5946 say that:
5947
5948 template <typename T> struct S { ~S (); };
5949 template <typename T> S<T>::~S() {}
5950
5951 is invalid, since `~' must be followed by a class-name, but
5952 `S<T>' is dependent, and so not known to be a class.
5953 That's not right; we need to look in uninstantiated
5954 templates. A further complication arises from:
5955
5956 template <typename T> void f(T t) {
5957 t.T::~T();
5958 }
5959
5960 Here, it is not possible to look up `T' in the scope of `T'
5961 itself. We must look in both the current scope, and the
5962 scope of the containing complete expression.
5963
5964 Yet another issue is:
5965
5966 struct S {
5967 int S;
5968 ~S();
5969 };
5970
5971 S::~S() {}
5972
5973 The standard does not seem to say that the `S' in `~S'
5974 should refer to the type `S' and not the data member
5975 `S::S'. */
5976
5977 /* DR 244 says that we look up the name after the "~" in the
5978 same scope as we looked up the qualifying name. That idea
5979 isn't fully worked out; it's more complicated than that. */
5980 scope = parser->scope;
5981 object_scope = parser->object_scope;
5982 qualifying_scope = parser->qualifying_scope;
5983
5984 /* Check for invalid scopes. */
5985 if (scope == error_mark_node)
5986 {
5987 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5988 cp_lexer_consume_token (parser->lexer);
5989 return error_mark_node;
5990 }
5991 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5992 {
5993 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5994 error_at (token->location,
5995 "scope %qT before %<~%> is not a class-name",
5996 scope);
5997 cp_parser_simulate_error (parser);
5998 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5999 cp_lexer_consume_token (parser->lexer);
6000 return error_mark_node;
6001 }
6002 gcc_assert (!scope || TYPE_P (scope));
6003
6004 /* If the name is of the form "X::~X" it's OK even if X is a
6005 typedef. */
6006 token = cp_lexer_peek_token (parser->lexer);
6007 if (scope
6008 && token->type == CPP_NAME
6009 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6010 != CPP_LESS)
6011 && (token->u.value == TYPE_IDENTIFIER (scope)
6012 || (CLASS_TYPE_P (scope)
6013 && constructor_name_p (token->u.value, scope))))
6014 {
6015 cp_lexer_consume_token (parser->lexer);
6016 return build_nt (BIT_NOT_EXPR, scope);
6017 }
6018
6019 /* ~auto means the destructor of whatever the object is. */
6020 if (cp_parser_is_keyword (token, RID_AUTO))
6021 {
6022 if (cxx_dialect < cxx14)
6023 pedwarn (input_location, 0,
6024 "%<~auto%> only available with "
6025 "-std=c++14 or -std=gnu++14");
6026 cp_lexer_consume_token (parser->lexer);
6027 return build_nt (BIT_NOT_EXPR, make_auto ());
6028 }
6029
6030 /* If there was an explicit qualification (S::~T), first look
6031 in the scope given by the qualification (i.e., S).
6032
6033 Note: in the calls to cp_parser_class_name below we pass
6034 typename_type so that lookup finds the injected-class-name
6035 rather than the constructor. */
6036 done = false;
6037 type_decl = NULL_TREE;
6038 if (scope)
6039 {
6040 cp_parser_parse_tentatively (parser);
6041 type_decl = cp_parser_class_name (parser,
6042 /*typename_keyword_p=*/false,
6043 /*template_keyword_p=*/false,
6044 typename_type,
6045 /*check_dependency=*/false,
6046 /*class_head_p=*/false,
6047 declarator_p);
6048 if (cp_parser_parse_definitely (parser))
6049 done = true;
6050 }
6051 /* In "N::S::~S", look in "N" as well. */
6052 if (!done && scope && qualifying_scope)
6053 {
6054 cp_parser_parse_tentatively (parser);
6055 parser->scope = qualifying_scope;
6056 parser->object_scope = NULL_TREE;
6057 parser->qualifying_scope = NULL_TREE;
6058 type_decl
6059 = cp_parser_class_name (parser,
6060 /*typename_keyword_p=*/false,
6061 /*template_keyword_p=*/false,
6062 typename_type,
6063 /*check_dependency=*/false,
6064 /*class_head_p=*/false,
6065 declarator_p);
6066 if (cp_parser_parse_definitely (parser))
6067 done = true;
6068 }
6069 /* In "p->S::~T", look in the scope given by "*p" as well. */
6070 else if (!done && object_scope)
6071 {
6072 cp_parser_parse_tentatively (parser);
6073 parser->scope = object_scope;
6074 parser->object_scope = NULL_TREE;
6075 parser->qualifying_scope = NULL_TREE;
6076 type_decl
6077 = cp_parser_class_name (parser,
6078 /*typename_keyword_p=*/false,
6079 /*template_keyword_p=*/false,
6080 typename_type,
6081 /*check_dependency=*/false,
6082 /*class_head_p=*/false,
6083 declarator_p);
6084 if (cp_parser_parse_definitely (parser))
6085 done = true;
6086 }
6087 /* Look in the surrounding context. */
6088 if (!done)
6089 {
6090 parser->scope = NULL_TREE;
6091 parser->object_scope = NULL_TREE;
6092 parser->qualifying_scope = NULL_TREE;
6093 if (processing_template_decl)
6094 cp_parser_parse_tentatively (parser);
6095 type_decl
6096 = cp_parser_class_name (parser,
6097 /*typename_keyword_p=*/false,
6098 /*template_keyword_p=*/false,
6099 typename_type,
6100 /*check_dependency=*/false,
6101 /*class_head_p=*/false,
6102 declarator_p);
6103 if (processing_template_decl
6104 && ! cp_parser_parse_definitely (parser))
6105 {
6106 /* We couldn't find a type with this name. If we're parsing
6107 tentatively, fail and try something else. */
6108 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6109 {
6110 cp_parser_simulate_error (parser);
6111 return error_mark_node;
6112 }
6113 /* Otherwise, accept it and check for a match at instantiation
6114 time. */
6115 type_decl = cp_parser_identifier (parser);
6116 if (type_decl != error_mark_node)
6117 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6118 return type_decl;
6119 }
6120 }
6121 /* If an error occurred, assume that the name of the
6122 destructor is the same as the name of the qualifying
6123 class. That allows us to keep parsing after running
6124 into ill-formed destructor names. */
6125 if (type_decl == error_mark_node && scope)
6126 return build_nt (BIT_NOT_EXPR, scope);
6127 else if (type_decl == error_mark_node)
6128 return error_mark_node;
6129
6130 /* Check that destructor name and scope match. */
6131 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6132 {
6133 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6134 error_at (token->location,
6135 "declaration of %<~%T%> as member of %qT",
6136 type_decl, scope);
6137 cp_parser_simulate_error (parser);
6138 return error_mark_node;
6139 }
6140
6141 /* [class.dtor]
6142
6143 A typedef-name that names a class shall not be used as the
6144 identifier in the declarator for a destructor declaration. */
6145 if (declarator_p
6146 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6147 && !DECL_SELF_REFERENCE_P (type_decl)
6148 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6149 error_at (token->location,
6150 "typedef-name %qD used as destructor declarator",
6151 type_decl);
6152
6153 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6154 }
6155
6156 case CPP_KEYWORD:
6157 if (token->keyword == RID_OPERATOR)
6158 {
6159 cp_expr id;
6160
6161 /* This could be a template-id, so we try that first. */
6162 cp_parser_parse_tentatively (parser);
6163 /* Try a template-id. */
6164 id = cp_parser_template_id (parser, template_keyword_p,
6165 /*check_dependency_p=*/true,
6166 none_type,
6167 declarator_p);
6168 /* If that worked, we're done. */
6169 if (cp_parser_parse_definitely (parser))
6170 return id;
6171 /* We still don't know whether we're looking at an
6172 operator-function-id or a conversion-function-id. */
6173 cp_parser_parse_tentatively (parser);
6174 /* Try an operator-function-id. */
6175 id = cp_parser_operator_function_id (parser);
6176 /* If that didn't work, try a conversion-function-id. */
6177 if (!cp_parser_parse_definitely (parser))
6178 id = cp_parser_conversion_function_id (parser);
6179
6180 return id;
6181 }
6182 /* Fall through. */
6183
6184 default:
6185 if (optional_p)
6186 return NULL_TREE;
6187 cp_parser_error (parser, "expected unqualified-id");
6188 return error_mark_node;
6189 }
6190 }
6191
6192 /* Parse an (optional) nested-name-specifier.
6193
6194 nested-name-specifier: [C++98]
6195 class-or-namespace-name :: nested-name-specifier [opt]
6196 class-or-namespace-name :: template nested-name-specifier [opt]
6197
6198 nested-name-specifier: [C++0x]
6199 type-name ::
6200 namespace-name ::
6201 nested-name-specifier identifier ::
6202 nested-name-specifier template [opt] simple-template-id ::
6203
6204 PARSER->SCOPE should be set appropriately before this function is
6205 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6206 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6207 in name lookups.
6208
6209 Sets PARSER->SCOPE to the class (TYPE) or namespace
6210 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6211 it unchanged if there is no nested-name-specifier. Returns the new
6212 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6213
6214 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6215 part of a declaration and/or decl-specifier. */
6216
6217 static tree
6218 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6219 bool typename_keyword_p,
6220 bool check_dependency_p,
6221 bool type_p,
6222 bool is_declaration,
6223 bool template_keyword_p /* = false */)
6224 {
6225 bool success = false;
6226 cp_token_position start = 0;
6227 cp_token *token;
6228
6229 /* Remember where the nested-name-specifier starts. */
6230 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6231 {
6232 start = cp_lexer_token_position (parser->lexer, false);
6233 push_deferring_access_checks (dk_deferred);
6234 }
6235
6236 while (true)
6237 {
6238 tree new_scope;
6239 tree old_scope;
6240 tree saved_qualifying_scope;
6241
6242 /* Spot cases that cannot be the beginning of a
6243 nested-name-specifier. */
6244 token = cp_lexer_peek_token (parser->lexer);
6245
6246 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6247 the already parsed nested-name-specifier. */
6248 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6249 {
6250 /* Grab the nested-name-specifier and continue the loop. */
6251 cp_parser_pre_parsed_nested_name_specifier (parser);
6252 /* If we originally encountered this nested-name-specifier
6253 with IS_DECLARATION set to false, we will not have
6254 resolved TYPENAME_TYPEs, so we must do so here. */
6255 if (is_declaration
6256 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6257 {
6258 new_scope = resolve_typename_type (parser->scope,
6259 /*only_current_p=*/false);
6260 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6261 parser->scope = new_scope;
6262 }
6263 success = true;
6264 continue;
6265 }
6266
6267 /* Spot cases that cannot be the beginning of a
6268 nested-name-specifier. On the second and subsequent times
6269 through the loop, we look for the `template' keyword. */
6270 if (success && token->keyword == RID_TEMPLATE)
6271 ;
6272 /* A template-id can start a nested-name-specifier. */
6273 else if (token->type == CPP_TEMPLATE_ID)
6274 ;
6275 /* DR 743: decltype can be used in a nested-name-specifier. */
6276 else if (token_is_decltype (token))
6277 ;
6278 else
6279 {
6280 /* If the next token is not an identifier, then it is
6281 definitely not a type-name or namespace-name. */
6282 if (token->type != CPP_NAME)
6283 break;
6284 /* If the following token is neither a `<' (to begin a
6285 template-id), nor a `::', then we are not looking at a
6286 nested-name-specifier. */
6287 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6288
6289 if (token->type == CPP_COLON
6290 && parser->colon_corrects_to_scope_p
6291 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6292 {
6293 gcc_rich_location richloc (token->location);
6294 richloc.add_fixit_replace ("::");
6295 error_at (&richloc,
6296 "found %<:%> in nested-name-specifier, "
6297 "expected %<::%>");
6298 token->type = CPP_SCOPE;
6299 }
6300
6301 if (token->type != CPP_SCOPE
6302 && !cp_parser_nth_token_starts_template_argument_list_p
6303 (parser, 2))
6304 break;
6305 }
6306
6307 /* The nested-name-specifier is optional, so we parse
6308 tentatively. */
6309 cp_parser_parse_tentatively (parser);
6310
6311 /* Look for the optional `template' keyword, if this isn't the
6312 first time through the loop. */
6313 if (success)
6314 template_keyword_p = cp_parser_optional_template_keyword (parser);
6315
6316 /* Save the old scope since the name lookup we are about to do
6317 might destroy it. */
6318 old_scope = parser->scope;
6319 saved_qualifying_scope = parser->qualifying_scope;
6320 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6321 look up names in "X<T>::I" in order to determine that "Y" is
6322 a template. So, if we have a typename at this point, we make
6323 an effort to look through it. */
6324 if (is_declaration
6325 && !typename_keyword_p
6326 && parser->scope
6327 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6328 parser->scope = resolve_typename_type (parser->scope,
6329 /*only_current_p=*/false);
6330 /* Parse the qualifying entity. */
6331 new_scope
6332 = cp_parser_qualifying_entity (parser,
6333 typename_keyword_p,
6334 template_keyword_p,
6335 check_dependency_p,
6336 type_p,
6337 is_declaration);
6338 /* Look for the `::' token. */
6339 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6340
6341 /* If we found what we wanted, we keep going; otherwise, we're
6342 done. */
6343 if (!cp_parser_parse_definitely (parser))
6344 {
6345 bool error_p = false;
6346
6347 /* Restore the OLD_SCOPE since it was valid before the
6348 failed attempt at finding the last
6349 class-or-namespace-name. */
6350 parser->scope = old_scope;
6351 parser->qualifying_scope = saved_qualifying_scope;
6352
6353 /* If the next token is a decltype, and the one after that is a
6354 `::', then the decltype has failed to resolve to a class or
6355 enumeration type. Give this error even when parsing
6356 tentatively since it can't possibly be valid--and we're going
6357 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6358 won't get another chance.*/
6359 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6360 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6361 == CPP_SCOPE))
6362 {
6363 token = cp_lexer_consume_token (parser->lexer);
6364 error_at (token->location, "decltype evaluates to %qT, "
6365 "which is not a class or enumeration type",
6366 token->u.tree_check_value->value);
6367 parser->scope = error_mark_node;
6368 error_p = true;
6369 /* As below. */
6370 success = true;
6371 cp_lexer_consume_token (parser->lexer);
6372 }
6373
6374 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6375 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6376 {
6377 /* If we have a non-type template-id followed by ::, it can't
6378 possibly be valid. */
6379 token = cp_lexer_peek_token (parser->lexer);
6380 tree tid = token->u.tree_check_value->value;
6381 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6382 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6383 {
6384 tree tmpl = NULL_TREE;
6385 if (is_overloaded_fn (tid))
6386 {
6387 tree fns = get_fns (tid);
6388 if (OVL_SINGLE_P (fns))
6389 tmpl = OVL_FIRST (fns);
6390 error_at (token->location, "function template-id %qD "
6391 "in nested-name-specifier", tid);
6392 }
6393 else
6394 {
6395 /* Variable template. */
6396 tmpl = TREE_OPERAND (tid, 0);
6397 gcc_assert (variable_template_p (tmpl));
6398 error_at (token->location, "variable template-id %qD "
6399 "in nested-name-specifier", tid);
6400 }
6401 if (tmpl)
6402 inform (DECL_SOURCE_LOCATION (tmpl),
6403 "%qD declared here", tmpl);
6404
6405 parser->scope = error_mark_node;
6406 error_p = true;
6407 /* As below. */
6408 success = true;
6409 cp_lexer_consume_token (parser->lexer);
6410 cp_lexer_consume_token (parser->lexer);
6411 }
6412 }
6413
6414 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6415 break;
6416 /* If the next token is an identifier, and the one after
6417 that is a `::', then any valid interpretation would have
6418 found a class-or-namespace-name. */
6419 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6420 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6421 == CPP_SCOPE)
6422 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6423 != CPP_COMPL))
6424 {
6425 token = cp_lexer_consume_token (parser->lexer);
6426 if (!error_p)
6427 {
6428 if (!token->error_reported)
6429 {
6430 tree decl;
6431 tree ambiguous_decls;
6432
6433 decl = cp_parser_lookup_name (parser, token->u.value,
6434 none_type,
6435 /*is_template=*/false,
6436 /*is_namespace=*/false,
6437 /*check_dependency=*/true,
6438 &ambiguous_decls,
6439 token->location);
6440 if (TREE_CODE (decl) == TEMPLATE_DECL)
6441 error_at (token->location,
6442 "%qD used without template arguments",
6443 decl);
6444 else if (ambiguous_decls)
6445 {
6446 // cp_parser_lookup_name has the same diagnostic,
6447 // thus make sure to emit it at most once.
6448 if (cp_parser_uncommitted_to_tentative_parse_p
6449 (parser))
6450 {
6451 error_at (token->location,
6452 "reference to %qD is ambiguous",
6453 token->u.value);
6454 print_candidates (ambiguous_decls);
6455 }
6456 decl = error_mark_node;
6457 }
6458 else
6459 {
6460 if (cxx_dialect != cxx98)
6461 cp_parser_name_lookup_error
6462 (parser, token->u.value, decl, NLE_NOT_CXX98,
6463 token->location);
6464 else
6465 cp_parser_name_lookup_error
6466 (parser, token->u.value, decl, NLE_CXX98,
6467 token->location);
6468 }
6469 }
6470 parser->scope = error_mark_node;
6471 error_p = true;
6472 /* Treat this as a successful nested-name-specifier
6473 due to:
6474
6475 [basic.lookup.qual]
6476
6477 If the name found is not a class-name (clause
6478 _class_) or namespace-name (_namespace.def_), the
6479 program is ill-formed. */
6480 success = true;
6481 }
6482 cp_lexer_consume_token (parser->lexer);
6483 }
6484 break;
6485 }
6486 /* We've found one valid nested-name-specifier. */
6487 success = true;
6488 /* Name lookup always gives us a DECL. */
6489 if (TREE_CODE (new_scope) == TYPE_DECL)
6490 new_scope = TREE_TYPE (new_scope);
6491 /* Uses of "template" must be followed by actual templates. */
6492 if (template_keyword_p
6493 && !(CLASS_TYPE_P (new_scope)
6494 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6495 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6496 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6497 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6498 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6499 == TEMPLATE_ID_EXPR)))
6500 permerror (input_location, TYPE_P (new_scope)
6501 ? G_("%qT is not a template")
6502 : G_("%qD is not a template"),
6503 new_scope);
6504 /* If it is a class scope, try to complete it; we are about to
6505 be looking up names inside the class. */
6506 if (TYPE_P (new_scope)
6507 /* Since checking types for dependency can be expensive,
6508 avoid doing it if the type is already complete. */
6509 && !COMPLETE_TYPE_P (new_scope)
6510 /* Do not try to complete dependent types. */
6511 && !dependent_type_p (new_scope))
6512 {
6513 new_scope = complete_type (new_scope);
6514 /* If it is a typedef to current class, use the current
6515 class instead, as the typedef won't have any names inside
6516 it yet. */
6517 if (!COMPLETE_TYPE_P (new_scope)
6518 && currently_open_class (new_scope))
6519 new_scope = TYPE_MAIN_VARIANT (new_scope);
6520 }
6521 /* Make sure we look in the right scope the next time through
6522 the loop. */
6523 parser->scope = new_scope;
6524 }
6525
6526 /* If parsing tentatively, replace the sequence of tokens that makes
6527 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6528 token. That way, should we re-parse the token stream, we will
6529 not have to repeat the effort required to do the parse, nor will
6530 we issue duplicate error messages. */
6531 if (success && start)
6532 {
6533 cp_token *token;
6534
6535 token = cp_lexer_token_at (parser->lexer, start);
6536 /* Reset the contents of the START token. */
6537 token->type = CPP_NESTED_NAME_SPECIFIER;
6538 /* Retrieve any deferred checks. Do not pop this access checks yet
6539 so the memory will not be reclaimed during token replacing below. */
6540 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6541 token->u.tree_check_value->value = parser->scope;
6542 token->u.tree_check_value->checks = get_deferred_access_checks ();
6543 token->u.tree_check_value->qualifying_scope =
6544 parser->qualifying_scope;
6545 token->keyword = RID_MAX;
6546
6547 /* Purge all subsequent tokens. */
6548 cp_lexer_purge_tokens_after (parser->lexer, start);
6549 }
6550
6551 if (start)
6552 pop_to_parent_deferring_access_checks ();
6553
6554 return success ? parser->scope : NULL_TREE;
6555 }
6556
6557 /* Parse a nested-name-specifier. See
6558 cp_parser_nested_name_specifier_opt for details. This function
6559 behaves identically, except that it will an issue an error if no
6560 nested-name-specifier is present. */
6561
6562 static tree
6563 cp_parser_nested_name_specifier (cp_parser *parser,
6564 bool typename_keyword_p,
6565 bool check_dependency_p,
6566 bool type_p,
6567 bool is_declaration)
6568 {
6569 tree scope;
6570
6571 /* Look for the nested-name-specifier. */
6572 scope = cp_parser_nested_name_specifier_opt (parser,
6573 typename_keyword_p,
6574 check_dependency_p,
6575 type_p,
6576 is_declaration);
6577 /* If it was not present, issue an error message. */
6578 if (!scope)
6579 {
6580 cp_parser_error (parser, "expected nested-name-specifier");
6581 parser->scope = NULL_TREE;
6582 }
6583
6584 return scope;
6585 }
6586
6587 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6588 this is either a class-name or a namespace-name (which corresponds
6589 to the class-or-namespace-name production in the grammar). For
6590 C++0x, it can also be a type-name that refers to an enumeration
6591 type or a simple-template-id.
6592
6593 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6594 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6595 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6596 TYPE_P is TRUE iff the next name should be taken as a class-name,
6597 even the same name is declared to be another entity in the same
6598 scope.
6599
6600 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6601 specified by the class-or-namespace-name. If neither is found the
6602 ERROR_MARK_NODE is returned. */
6603
6604 static tree
6605 cp_parser_qualifying_entity (cp_parser *parser,
6606 bool typename_keyword_p,
6607 bool template_keyword_p,
6608 bool check_dependency_p,
6609 bool type_p,
6610 bool is_declaration)
6611 {
6612 tree saved_scope;
6613 tree saved_qualifying_scope;
6614 tree saved_object_scope;
6615 tree scope;
6616 bool only_class_p;
6617 bool successful_parse_p;
6618
6619 /* DR 743: decltype can appear in a nested-name-specifier. */
6620 if (cp_lexer_next_token_is_decltype (parser->lexer))
6621 {
6622 scope = cp_parser_decltype (parser);
6623 if (TREE_CODE (scope) != ENUMERAL_TYPE
6624 && !MAYBE_CLASS_TYPE_P (scope))
6625 {
6626 cp_parser_simulate_error (parser);
6627 return error_mark_node;
6628 }
6629 if (TYPE_NAME (scope))
6630 scope = TYPE_NAME (scope);
6631 return scope;
6632 }
6633
6634 /* Before we try to parse the class-name, we must save away the
6635 current PARSER->SCOPE since cp_parser_class_name will destroy
6636 it. */
6637 saved_scope = parser->scope;
6638 saved_qualifying_scope = parser->qualifying_scope;
6639 saved_object_scope = parser->object_scope;
6640 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6641 there is no need to look for a namespace-name. */
6642 only_class_p = template_keyword_p
6643 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6644 if (!only_class_p)
6645 cp_parser_parse_tentatively (parser);
6646 scope = cp_parser_class_name (parser,
6647 typename_keyword_p,
6648 template_keyword_p,
6649 type_p ? class_type : none_type,
6650 check_dependency_p,
6651 /*class_head_p=*/false,
6652 is_declaration,
6653 /*enum_ok=*/cxx_dialect > cxx98);
6654 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6655 /* If that didn't work, try for a namespace-name. */
6656 if (!only_class_p && !successful_parse_p)
6657 {
6658 /* Restore the saved scope. */
6659 parser->scope = saved_scope;
6660 parser->qualifying_scope = saved_qualifying_scope;
6661 parser->object_scope = saved_object_scope;
6662 /* If we are not looking at an identifier followed by the scope
6663 resolution operator, then this is not part of a
6664 nested-name-specifier. (Note that this function is only used
6665 to parse the components of a nested-name-specifier.) */
6666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6667 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6668 return error_mark_node;
6669 scope = cp_parser_namespace_name (parser);
6670 }
6671
6672 return scope;
6673 }
6674
6675 /* Return true if we are looking at a compound-literal, false otherwise. */
6676
6677 static bool
6678 cp_parser_compound_literal_p (cp_parser *parser)
6679 {
6680 cp_lexer_save_tokens (parser->lexer);
6681
6682 /* Skip tokens until the next token is a closing parenthesis.
6683 If we find the closing `)', and the next token is a `{', then
6684 we are looking at a compound-literal. */
6685 bool compound_literal_p
6686 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6687 /*consume_paren=*/true)
6688 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6689
6690 /* Roll back the tokens we skipped. */
6691 cp_lexer_rollback_tokens (parser->lexer);
6692
6693 return compound_literal_p;
6694 }
6695
6696 /* Return true if EXPR is the integer constant zero or a complex constant
6697 of zero, without any folding, but ignoring location wrappers. */
6698
6699 bool
6700 literal_integer_zerop (const_tree expr)
6701 {
6702 return (location_wrapper_p (expr)
6703 && integer_zerop (TREE_OPERAND (expr, 0)));
6704 }
6705
6706 /* Parse a postfix-expression.
6707
6708 postfix-expression:
6709 primary-expression
6710 postfix-expression [ expression ]
6711 postfix-expression ( expression-list [opt] )
6712 simple-type-specifier ( expression-list [opt] )
6713 typename :: [opt] nested-name-specifier identifier
6714 ( expression-list [opt] )
6715 typename :: [opt] nested-name-specifier template [opt] template-id
6716 ( expression-list [opt] )
6717 postfix-expression . template [opt] id-expression
6718 postfix-expression -> template [opt] id-expression
6719 postfix-expression . pseudo-destructor-name
6720 postfix-expression -> pseudo-destructor-name
6721 postfix-expression ++
6722 postfix-expression --
6723 dynamic_cast < type-id > ( expression )
6724 static_cast < type-id > ( expression )
6725 reinterpret_cast < type-id > ( expression )
6726 const_cast < type-id > ( expression )
6727 typeid ( expression )
6728 typeid ( type-id )
6729
6730 GNU Extension:
6731
6732 postfix-expression:
6733 ( type-id ) { initializer-list , [opt] }
6734
6735 This extension is a GNU version of the C99 compound-literal
6736 construct. (The C99 grammar uses `type-name' instead of `type-id',
6737 but they are essentially the same concept.)
6738
6739 If ADDRESS_P is true, the postfix expression is the operand of the
6740 `&' operator. CAST_P is true if this expression is the target of a
6741 cast.
6742
6743 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6744 class member access expressions [expr.ref].
6745
6746 Returns a representation of the expression. */
6747
6748 static cp_expr
6749 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6750 bool member_access_only_p, bool decltype_p,
6751 cp_id_kind * pidk_return)
6752 {
6753 cp_token *token;
6754 location_t loc;
6755 enum rid keyword;
6756 cp_id_kind idk = CP_ID_KIND_NONE;
6757 cp_expr postfix_expression = NULL_TREE;
6758 bool is_member_access = false;
6759
6760 /* Peek at the next token. */
6761 token = cp_lexer_peek_token (parser->lexer);
6762 loc = token->location;
6763 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6764
6765 /* Some of the productions are determined by keywords. */
6766 keyword = token->keyword;
6767 switch (keyword)
6768 {
6769 case RID_DYNCAST:
6770 case RID_STATCAST:
6771 case RID_REINTCAST:
6772 case RID_CONSTCAST:
6773 {
6774 tree type;
6775 cp_expr expression;
6776 const char *saved_message;
6777 bool saved_in_type_id_in_expr_p;
6778
6779 /* All of these can be handled in the same way from the point
6780 of view of parsing. Begin by consuming the token
6781 identifying the cast. */
6782 cp_lexer_consume_token (parser->lexer);
6783
6784 /* New types cannot be defined in the cast. */
6785 saved_message = parser->type_definition_forbidden_message;
6786 parser->type_definition_forbidden_message
6787 = G_("types may not be defined in casts");
6788
6789 /* Look for the opening `<'. */
6790 cp_parser_require (parser, CPP_LESS, RT_LESS);
6791 /* Parse the type to which we are casting. */
6792 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6793 parser->in_type_id_in_expr_p = true;
6794 type = cp_parser_type_id (parser);
6795 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6796 /* Look for the closing `>'. */
6797 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6798 /* Restore the old message. */
6799 parser->type_definition_forbidden_message = saved_message;
6800
6801 bool saved_greater_than_is_operator_p
6802 = parser->greater_than_is_operator_p;
6803 parser->greater_than_is_operator_p = true;
6804
6805 /* And the expression which is being cast. */
6806 matching_parens parens;
6807 parens.require_open (parser);
6808 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6809 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6810 RT_CLOSE_PAREN);
6811 location_t end_loc = close_paren ?
6812 close_paren->location : UNKNOWN_LOCATION;
6813
6814 parser->greater_than_is_operator_p
6815 = saved_greater_than_is_operator_p;
6816
6817 /* Only type conversions to integral or enumeration types
6818 can be used in constant-expressions. */
6819 if (!cast_valid_in_integral_constant_expression_p (type)
6820 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6821 {
6822 postfix_expression = error_mark_node;
6823 break;
6824 }
6825
6826 switch (keyword)
6827 {
6828 case RID_DYNCAST:
6829 postfix_expression
6830 = build_dynamic_cast (type, expression, tf_warning_or_error);
6831 break;
6832 case RID_STATCAST:
6833 postfix_expression
6834 = build_static_cast (type, expression, tf_warning_or_error);
6835 break;
6836 case RID_REINTCAST:
6837 postfix_expression
6838 = build_reinterpret_cast (type, expression,
6839 tf_warning_or_error);
6840 break;
6841 case RID_CONSTCAST:
6842 postfix_expression
6843 = build_const_cast (type, expression, tf_warning_or_error);
6844 break;
6845 default:
6846 gcc_unreachable ();
6847 }
6848
6849 /* Construct a location e.g. :
6850 reinterpret_cast <int *> (expr)
6851 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6852 ranging from the start of the "*_cast" token to the final closing
6853 paren, with the caret at the start. */
6854 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6855 postfix_expression.set_location (cp_cast_loc);
6856 }
6857 break;
6858
6859 case RID_TYPEID:
6860 {
6861 tree type;
6862 const char *saved_message;
6863 bool saved_in_type_id_in_expr_p;
6864
6865 /* Consume the `typeid' token. */
6866 cp_lexer_consume_token (parser->lexer);
6867 /* Look for the `(' token. */
6868 matching_parens parens;
6869 parens.require_open (parser);
6870 /* Types cannot be defined in a `typeid' expression. */
6871 saved_message = parser->type_definition_forbidden_message;
6872 parser->type_definition_forbidden_message
6873 = G_("types may not be defined in a %<typeid%> expression");
6874 /* We can't be sure yet whether we're looking at a type-id or an
6875 expression. */
6876 cp_parser_parse_tentatively (parser);
6877 /* Try a type-id first. */
6878 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6879 parser->in_type_id_in_expr_p = true;
6880 type = cp_parser_type_id (parser);
6881 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6882 /* Look for the `)' token. Otherwise, we can't be sure that
6883 we're not looking at an expression: consider `typeid (int
6884 (3))', for example. */
6885 cp_token *close_paren = parens.require_close (parser);
6886 /* If all went well, simply lookup the type-id. */
6887 if (cp_parser_parse_definitely (parser))
6888 postfix_expression = get_typeid (type, tf_warning_or_error);
6889 /* Otherwise, fall back to the expression variant. */
6890 else
6891 {
6892 tree expression;
6893
6894 /* Look for an expression. */
6895 expression = cp_parser_expression (parser, & idk);
6896 /* Compute its typeid. */
6897 postfix_expression = build_typeid (expression, tf_warning_or_error);
6898 /* Look for the `)' token. */
6899 close_paren = parens.require_close (parser);
6900 }
6901 /* Restore the saved message. */
6902 parser->type_definition_forbidden_message = saved_message;
6903 /* `typeid' may not appear in an integral constant expression. */
6904 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6905 postfix_expression = error_mark_node;
6906
6907 /* Construct a location e.g. :
6908 typeid (expr)
6909 ^~~~~~~~~~~~~
6910 ranging from the start of the "typeid" token to the final closing
6911 paren, with the caret at the start. */
6912 if (close_paren)
6913 {
6914 location_t typeid_loc
6915 = make_location (start_loc, start_loc, close_paren->location);
6916 postfix_expression.set_location (typeid_loc);
6917 postfix_expression.maybe_add_location_wrapper ();
6918 }
6919 }
6920 break;
6921
6922 case RID_TYPENAME:
6923 {
6924 tree type;
6925 /* The syntax permitted here is the same permitted for an
6926 elaborated-type-specifier. */
6927 ++parser->prevent_constrained_type_specifiers;
6928 type = cp_parser_elaborated_type_specifier (parser,
6929 /*is_friend=*/false,
6930 /*is_declaration=*/false);
6931 --parser->prevent_constrained_type_specifiers;
6932 postfix_expression = cp_parser_functional_cast (parser, type);
6933 }
6934 break;
6935
6936 case RID_ADDRESSOF:
6937 case RID_BUILTIN_SHUFFLE:
6938 case RID_BUILTIN_LAUNDER:
6939 {
6940 vec<tree, va_gc> *vec;
6941 unsigned int i;
6942 tree p;
6943
6944 cp_lexer_consume_token (parser->lexer);
6945 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6946 /*cast_p=*/false, /*allow_expansion_p=*/true,
6947 /*non_constant_p=*/NULL);
6948 if (vec == NULL)
6949 {
6950 postfix_expression = error_mark_node;
6951 break;
6952 }
6953
6954 FOR_EACH_VEC_ELT (*vec, i, p)
6955 mark_exp_read (p);
6956
6957 switch (keyword)
6958 {
6959 case RID_ADDRESSOF:
6960 if (vec->length () == 1)
6961 postfix_expression
6962 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6963 else
6964 {
6965 error_at (loc, "wrong number of arguments to "
6966 "%<__builtin_addressof%>");
6967 postfix_expression = error_mark_node;
6968 }
6969 break;
6970
6971 case RID_BUILTIN_LAUNDER:
6972 if (vec->length () == 1)
6973 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6974 tf_warning_or_error);
6975 else
6976 {
6977 error_at (loc, "wrong number of arguments to "
6978 "%<__builtin_launder%>");
6979 postfix_expression = error_mark_node;
6980 }
6981 break;
6982
6983 case RID_BUILTIN_SHUFFLE:
6984 if (vec->length () == 2)
6985 postfix_expression
6986 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6987 (*vec)[1], tf_warning_or_error);
6988 else if (vec->length () == 3)
6989 postfix_expression
6990 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6991 (*vec)[2], tf_warning_or_error);
6992 else
6993 {
6994 error_at (loc, "wrong number of arguments to "
6995 "%<__builtin_shuffle%>");
6996 postfix_expression = error_mark_node;
6997 }
6998 break;
6999
7000 default:
7001 gcc_unreachable ();
7002 }
7003 break;
7004 }
7005
7006 default:
7007 {
7008 tree type;
7009
7010 /* If the next thing is a simple-type-specifier, we may be
7011 looking at a functional cast. We could also be looking at
7012 an id-expression. So, we try the functional cast, and if
7013 that doesn't work we fall back to the primary-expression. */
7014 cp_parser_parse_tentatively (parser);
7015 /* Look for the simple-type-specifier. */
7016 ++parser->prevent_constrained_type_specifiers;
7017 type = cp_parser_simple_type_specifier (parser,
7018 /*decl_specs=*/NULL,
7019 CP_PARSER_FLAGS_NONE);
7020 --parser->prevent_constrained_type_specifiers;
7021 /* Parse the cast itself. */
7022 if (!cp_parser_error_occurred (parser))
7023 postfix_expression
7024 = cp_parser_functional_cast (parser, type);
7025 /* If that worked, we're done. */
7026 if (cp_parser_parse_definitely (parser))
7027 break;
7028
7029 /* If the functional-cast didn't work out, try a
7030 compound-literal. */
7031 if (cp_parser_allow_gnu_extensions_p (parser)
7032 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7033 {
7034 cp_expr initializer = NULL_TREE;
7035
7036 cp_parser_parse_tentatively (parser);
7037
7038 matching_parens parens;
7039 parens.consume_open (parser);
7040
7041 /* Avoid calling cp_parser_type_id pointlessly, see comment
7042 in cp_parser_cast_expression about c++/29234. */
7043 if (!cp_parser_compound_literal_p (parser))
7044 cp_parser_simulate_error (parser);
7045 else
7046 {
7047 /* Parse the type. */
7048 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7049 parser->in_type_id_in_expr_p = true;
7050 type = cp_parser_type_id (parser);
7051 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7052 parens.require_close (parser);
7053 }
7054
7055 /* If things aren't going well, there's no need to
7056 keep going. */
7057 if (!cp_parser_error_occurred (parser))
7058 {
7059 bool non_constant_p;
7060 /* Parse the brace-enclosed initializer list. */
7061 initializer = cp_parser_braced_list (parser,
7062 &non_constant_p);
7063 }
7064 /* If that worked, we're definitely looking at a
7065 compound-literal expression. */
7066 if (cp_parser_parse_definitely (parser))
7067 {
7068 /* Warn the user that a compound literal is not
7069 allowed in standard C++. */
7070 pedwarn (input_location, OPT_Wpedantic,
7071 "ISO C++ forbids compound-literals");
7072 /* For simplicity, we disallow compound literals in
7073 constant-expressions. We could
7074 allow compound literals of integer type, whose
7075 initializer was a constant, in constant
7076 expressions. Permitting that usage, as a further
7077 extension, would not change the meaning of any
7078 currently accepted programs. (Of course, as
7079 compound literals are not part of ISO C++, the
7080 standard has nothing to say.) */
7081 if (cp_parser_non_integral_constant_expression (parser,
7082 NIC_NCC))
7083 {
7084 postfix_expression = error_mark_node;
7085 break;
7086 }
7087 /* Form the representation of the compound-literal. */
7088 postfix_expression
7089 = finish_compound_literal (type, initializer,
7090 tf_warning_or_error, fcl_c99);
7091 postfix_expression.set_location (initializer.get_location ());
7092 break;
7093 }
7094 }
7095
7096 /* It must be a primary-expression. */
7097 postfix_expression
7098 = cp_parser_primary_expression (parser, address_p, cast_p,
7099 /*template_arg_p=*/false,
7100 decltype_p,
7101 &idk);
7102 }
7103 break;
7104 }
7105
7106 /* Note that we don't need to worry about calling build_cplus_new on a
7107 class-valued CALL_EXPR in decltype when it isn't the end of the
7108 postfix-expression; unary_complex_lvalue will take care of that for
7109 all these cases. */
7110
7111 /* Keep looping until the postfix-expression is complete. */
7112 while (true)
7113 {
7114 if (idk == CP_ID_KIND_UNQUALIFIED
7115 && identifier_p (postfix_expression)
7116 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7117 /* It is not a Koenig lookup function call. */
7118 postfix_expression
7119 = unqualified_name_lookup_error (postfix_expression);
7120
7121 /* Peek at the next token. */
7122 token = cp_lexer_peek_token (parser->lexer);
7123
7124 switch (token->type)
7125 {
7126 case CPP_OPEN_SQUARE:
7127 if (cp_next_tokens_can_be_std_attribute_p (parser))
7128 {
7129 cp_parser_error (parser,
7130 "two consecutive %<[%> shall "
7131 "only introduce an attribute");
7132 return error_mark_node;
7133 }
7134 postfix_expression
7135 = cp_parser_postfix_open_square_expression (parser,
7136 postfix_expression,
7137 false,
7138 decltype_p);
7139 postfix_expression.set_range (start_loc,
7140 postfix_expression.get_location ());
7141
7142 idk = CP_ID_KIND_NONE;
7143 is_member_access = false;
7144 break;
7145
7146 case CPP_OPEN_PAREN:
7147 /* postfix-expression ( expression-list [opt] ) */
7148 {
7149 bool koenig_p;
7150 bool is_builtin_constant_p;
7151 bool saved_integral_constant_expression_p = false;
7152 bool saved_non_integral_constant_expression_p = false;
7153 tsubst_flags_t complain = complain_flags (decltype_p);
7154 vec<tree, va_gc> *args;
7155 location_t close_paren_loc = UNKNOWN_LOCATION;
7156
7157 is_member_access = false;
7158
7159 is_builtin_constant_p
7160 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7161 if (is_builtin_constant_p)
7162 {
7163 /* The whole point of __builtin_constant_p is to allow
7164 non-constant expressions to appear as arguments. */
7165 saved_integral_constant_expression_p
7166 = parser->integral_constant_expression_p;
7167 saved_non_integral_constant_expression_p
7168 = parser->non_integral_constant_expression_p;
7169 parser->integral_constant_expression_p = false;
7170 }
7171 args = (cp_parser_parenthesized_expression_list
7172 (parser, non_attr,
7173 /*cast_p=*/false, /*allow_expansion_p=*/true,
7174 /*non_constant_p=*/NULL,
7175 /*close_paren_loc=*/&close_paren_loc,
7176 /*wrap_locations_p=*/true));
7177 if (is_builtin_constant_p)
7178 {
7179 parser->integral_constant_expression_p
7180 = saved_integral_constant_expression_p;
7181 parser->non_integral_constant_expression_p
7182 = saved_non_integral_constant_expression_p;
7183 }
7184
7185 if (args == NULL)
7186 {
7187 postfix_expression = error_mark_node;
7188 break;
7189 }
7190
7191 /* Function calls are not permitted in
7192 constant-expressions. */
7193 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7194 && cp_parser_non_integral_constant_expression (parser,
7195 NIC_FUNC_CALL))
7196 {
7197 postfix_expression = error_mark_node;
7198 release_tree_vector (args);
7199 break;
7200 }
7201
7202 koenig_p = false;
7203 if (idk == CP_ID_KIND_UNQUALIFIED
7204 || idk == CP_ID_KIND_TEMPLATE_ID)
7205 {
7206 if (identifier_p (postfix_expression)
7207 /* In C++2A, we may need to perform ADL for a template
7208 name. */
7209 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7210 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7211 {
7212 if (!args->is_empty ())
7213 {
7214 koenig_p = true;
7215 if (!any_type_dependent_arguments_p (args))
7216 postfix_expression
7217 = perform_koenig_lookup (postfix_expression, args,
7218 complain);
7219 }
7220 else
7221 postfix_expression
7222 = unqualified_fn_lookup_error (postfix_expression);
7223 }
7224 /* We do not perform argument-dependent lookup if
7225 normal lookup finds a non-function, in accordance
7226 with the expected resolution of DR 218. */
7227 else if (!args->is_empty ()
7228 && is_overloaded_fn (postfix_expression))
7229 {
7230 tree fn = get_first_fn (postfix_expression);
7231 fn = STRIP_TEMPLATE (fn);
7232
7233 /* Do not do argument dependent lookup if regular
7234 lookup finds a member function or a block-scope
7235 function declaration. [basic.lookup.argdep]/3 */
7236 if (!DECL_FUNCTION_MEMBER_P (fn)
7237 && !DECL_LOCAL_FUNCTION_P (fn))
7238 {
7239 koenig_p = true;
7240 if (!any_type_dependent_arguments_p (args))
7241 postfix_expression
7242 = perform_koenig_lookup (postfix_expression, args,
7243 complain);
7244 }
7245 }
7246 }
7247
7248 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7249 {
7250 tree instance = TREE_OPERAND (postfix_expression, 0);
7251 tree fn = TREE_OPERAND (postfix_expression, 1);
7252
7253 if (processing_template_decl
7254 && (type_dependent_object_expression_p (instance)
7255 || (!BASELINK_P (fn)
7256 && TREE_CODE (fn) != FIELD_DECL)
7257 || type_dependent_expression_p (fn)
7258 || any_type_dependent_arguments_p (args)))
7259 {
7260 maybe_generic_this_capture (instance, fn);
7261 postfix_expression
7262 = build_min_nt_call_vec (postfix_expression, args);
7263 release_tree_vector (args);
7264 break;
7265 }
7266
7267 if (BASELINK_P (fn))
7268 {
7269 postfix_expression
7270 = (build_new_method_call
7271 (instance, fn, &args, NULL_TREE,
7272 (idk == CP_ID_KIND_QUALIFIED
7273 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7274 : LOOKUP_NORMAL),
7275 /*fn_p=*/NULL,
7276 complain));
7277 }
7278 else
7279 postfix_expression
7280 = finish_call_expr (postfix_expression, &args,
7281 /*disallow_virtual=*/false,
7282 /*koenig_p=*/false,
7283 complain);
7284 }
7285 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7286 || TREE_CODE (postfix_expression) == MEMBER_REF
7287 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7288 postfix_expression = (build_offset_ref_call_from_tree
7289 (postfix_expression, &args,
7290 complain));
7291 else if (idk == CP_ID_KIND_QUALIFIED)
7292 /* A call to a static class member, or a namespace-scope
7293 function. */
7294 postfix_expression
7295 = finish_call_expr (postfix_expression, &args,
7296 /*disallow_virtual=*/true,
7297 koenig_p,
7298 complain);
7299 else
7300 /* All other function calls. */
7301 postfix_expression
7302 = finish_call_expr (postfix_expression, &args,
7303 /*disallow_virtual=*/false,
7304 koenig_p,
7305 complain);
7306
7307 if (close_paren_loc != UNKNOWN_LOCATION)
7308 {
7309 location_t combined_loc = make_location (token->location,
7310 start_loc,
7311 close_paren_loc);
7312 postfix_expression.set_location (combined_loc);
7313 }
7314
7315 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7316 idk = CP_ID_KIND_NONE;
7317
7318 release_tree_vector (args);
7319 }
7320 break;
7321
7322 case CPP_DOT:
7323 case CPP_DEREF:
7324 /* postfix-expression . template [opt] id-expression
7325 postfix-expression . pseudo-destructor-name
7326 postfix-expression -> template [opt] id-expression
7327 postfix-expression -> pseudo-destructor-name */
7328
7329 /* Consume the `.' or `->' operator. */
7330 cp_lexer_consume_token (parser->lexer);
7331
7332 postfix_expression
7333 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7334 postfix_expression,
7335 false, &idk, loc);
7336
7337 is_member_access = true;
7338 break;
7339
7340 case CPP_PLUS_PLUS:
7341 /* postfix-expression ++ */
7342 /* Consume the `++' token. */
7343 cp_lexer_consume_token (parser->lexer);
7344 /* Generate a representation for the complete expression. */
7345 postfix_expression
7346 = finish_increment_expr (postfix_expression,
7347 POSTINCREMENT_EXPR);
7348 /* Increments may not appear in constant-expressions. */
7349 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7350 postfix_expression = error_mark_node;
7351 idk = CP_ID_KIND_NONE;
7352 is_member_access = false;
7353 break;
7354
7355 case CPP_MINUS_MINUS:
7356 /* postfix-expression -- */
7357 /* Consume the `--' token. */
7358 cp_lexer_consume_token (parser->lexer);
7359 /* Generate a representation for the complete expression. */
7360 postfix_expression
7361 = finish_increment_expr (postfix_expression,
7362 POSTDECREMENT_EXPR);
7363 /* Decrements may not appear in constant-expressions. */
7364 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7365 postfix_expression = error_mark_node;
7366 idk = CP_ID_KIND_NONE;
7367 is_member_access = false;
7368 break;
7369
7370 default:
7371 if (pidk_return != NULL)
7372 * pidk_return = idk;
7373 if (member_access_only_p)
7374 return is_member_access
7375 ? postfix_expression
7376 : cp_expr (error_mark_node);
7377 else
7378 return postfix_expression;
7379 }
7380 }
7381
7382 /* We should never get here. */
7383 gcc_unreachable ();
7384 return error_mark_node;
7385 }
7386
7387 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7388 by cp_parser_builtin_offsetof. We're looking for
7389
7390 postfix-expression [ expression ]
7391 postfix-expression [ braced-init-list ] (C++11)
7392
7393 FOR_OFFSETOF is set if we're being called in that context, which
7394 changes how we deal with integer constant expressions. */
7395
7396 static tree
7397 cp_parser_postfix_open_square_expression (cp_parser *parser,
7398 tree postfix_expression,
7399 bool for_offsetof,
7400 bool decltype_p)
7401 {
7402 tree index = NULL_TREE;
7403 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7404 bool saved_greater_than_is_operator_p;
7405
7406 /* Consume the `[' token. */
7407 cp_lexer_consume_token (parser->lexer);
7408
7409 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7410 parser->greater_than_is_operator_p = true;
7411
7412 /* Parse the index expression. */
7413 /* ??? For offsetof, there is a question of what to allow here. If
7414 offsetof is not being used in an integral constant expression context,
7415 then we *could* get the right answer by computing the value at runtime.
7416 If we are in an integral constant expression context, then we might
7417 could accept any constant expression; hard to say without analysis.
7418 Rather than open the barn door too wide right away, allow only integer
7419 constant expressions here. */
7420 if (for_offsetof)
7421 index = cp_parser_constant_expression (parser);
7422 else
7423 {
7424 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7425 {
7426 bool expr_nonconst_p;
7427 cp_lexer_set_source_position (parser->lexer);
7428 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7429 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7430 }
7431 else
7432 index = cp_parser_expression (parser);
7433 }
7434
7435 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7436
7437 /* Look for the closing `]'. */
7438 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7439
7440 /* Build the ARRAY_REF. */
7441 postfix_expression = grok_array_decl (loc, postfix_expression,
7442 index, decltype_p);
7443
7444 /* When not doing offsetof, array references are not permitted in
7445 constant-expressions. */
7446 if (!for_offsetof
7447 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7448 postfix_expression = error_mark_node;
7449
7450 return postfix_expression;
7451 }
7452
7453 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7454 dereference of incomplete type, returns true if error_mark_node should
7455 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7456 and *DEPENDENT_P. */
7457
7458 bool
7459 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7460 bool *dependent_p)
7461 {
7462 /* In a template, be permissive by treating an object expression
7463 of incomplete type as dependent (after a pedwarn). */
7464 diagnostic_t kind = (processing_template_decl
7465 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7466
7467 switch (TREE_CODE (*postfix_expression))
7468 {
7469 case CAST_EXPR:
7470 case REINTERPRET_CAST_EXPR:
7471 case CONST_CAST_EXPR:
7472 case STATIC_CAST_EXPR:
7473 case DYNAMIC_CAST_EXPR:
7474 case IMPLICIT_CONV_EXPR:
7475 case VIEW_CONVERT_EXPR:
7476 case NON_LVALUE_EXPR:
7477 kind = DK_ERROR;
7478 break;
7479 case OVERLOAD:
7480 /* Don't emit any diagnostic for OVERLOADs. */
7481 kind = DK_IGNORED;
7482 break;
7483 default:
7484 /* Avoid clobbering e.g. DECLs. */
7485 if (!EXPR_P (*postfix_expression))
7486 kind = DK_ERROR;
7487 break;
7488 }
7489
7490 if (kind == DK_IGNORED)
7491 return false;
7492
7493 location_t exploc = location_of (*postfix_expression);
7494 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7495 if (!MAYBE_CLASS_TYPE_P (*scope))
7496 return true;
7497 if (kind == DK_ERROR)
7498 *scope = *postfix_expression = error_mark_node;
7499 else if (processing_template_decl)
7500 {
7501 *dependent_p = true;
7502 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7503 }
7504 return false;
7505 }
7506
7507 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7508 by cp_parser_builtin_offsetof. We're looking for
7509
7510 postfix-expression . template [opt] id-expression
7511 postfix-expression . pseudo-destructor-name
7512 postfix-expression -> template [opt] id-expression
7513 postfix-expression -> pseudo-destructor-name
7514
7515 FOR_OFFSETOF is set if we're being called in that context. That sorta
7516 limits what of the above we'll actually accept, but nevermind.
7517 TOKEN_TYPE is the "." or "->" token, which will already have been
7518 removed from the stream. */
7519
7520 static tree
7521 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7522 enum cpp_ttype token_type,
7523 cp_expr postfix_expression,
7524 bool for_offsetof, cp_id_kind *idk,
7525 location_t location)
7526 {
7527 tree name;
7528 bool dependent_p;
7529 bool pseudo_destructor_p;
7530 tree scope = NULL_TREE;
7531 location_t start_loc = postfix_expression.get_start ();
7532
7533 /* If this is a `->' operator, dereference the pointer. */
7534 if (token_type == CPP_DEREF)
7535 postfix_expression = build_x_arrow (location, postfix_expression,
7536 tf_warning_or_error);
7537 /* Check to see whether or not the expression is type-dependent and
7538 not the current instantiation. */
7539 dependent_p = type_dependent_object_expression_p (postfix_expression);
7540 /* The identifier following the `->' or `.' is not qualified. */
7541 parser->scope = NULL_TREE;
7542 parser->qualifying_scope = NULL_TREE;
7543 parser->object_scope = NULL_TREE;
7544 *idk = CP_ID_KIND_NONE;
7545
7546 /* Enter the scope corresponding to the type of the object
7547 given by the POSTFIX_EXPRESSION. */
7548 if (!dependent_p)
7549 {
7550 scope = TREE_TYPE (postfix_expression);
7551 /* According to the standard, no expression should ever have
7552 reference type. Unfortunately, we do not currently match
7553 the standard in this respect in that our internal representation
7554 of an expression may have reference type even when the standard
7555 says it does not. Therefore, we have to manually obtain the
7556 underlying type here. */
7557 scope = non_reference (scope);
7558 /* The type of the POSTFIX_EXPRESSION must be complete. */
7559 /* Unlike the object expression in other contexts, *this is not
7560 required to be of complete type for purposes of class member
7561 access (5.2.5) outside the member function body. */
7562 if (postfix_expression != current_class_ref
7563 && scope != error_mark_node
7564 && !currently_open_class (scope))
7565 {
7566 scope = complete_type (scope);
7567 if (!COMPLETE_TYPE_P (scope)
7568 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7569 &dependent_p))
7570 return error_mark_node;
7571 }
7572
7573 if (!dependent_p)
7574 {
7575 /* Let the name lookup machinery know that we are processing a
7576 class member access expression. */
7577 parser->context->object_type = scope;
7578 /* If something went wrong, we want to be able to discern that case,
7579 as opposed to the case where there was no SCOPE due to the type
7580 of expression being dependent. */
7581 if (!scope)
7582 scope = error_mark_node;
7583 /* If the SCOPE was erroneous, make the various semantic analysis
7584 functions exit quickly -- and without issuing additional error
7585 messages. */
7586 if (scope == error_mark_node)
7587 postfix_expression = error_mark_node;
7588 }
7589 }
7590
7591 if (dependent_p)
7592 /* Tell cp_parser_lookup_name that there was an object, even though it's
7593 type-dependent. */
7594 parser->context->object_type = unknown_type_node;
7595
7596 /* Assume this expression is not a pseudo-destructor access. */
7597 pseudo_destructor_p = false;
7598
7599 /* If the SCOPE is a scalar type, then, if this is a valid program,
7600 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7601 is type dependent, it can be pseudo-destructor-name or something else.
7602 Try to parse it as pseudo-destructor-name first. */
7603 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7604 {
7605 tree s;
7606 tree type;
7607
7608 cp_parser_parse_tentatively (parser);
7609 /* Parse the pseudo-destructor-name. */
7610 s = NULL_TREE;
7611 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7612 &s, &type);
7613 if (dependent_p
7614 && (cp_parser_error_occurred (parser)
7615 || !SCALAR_TYPE_P (type)))
7616 cp_parser_abort_tentative_parse (parser);
7617 else if (cp_parser_parse_definitely (parser))
7618 {
7619 pseudo_destructor_p = true;
7620 postfix_expression
7621 = finish_pseudo_destructor_expr (postfix_expression,
7622 s, type, location);
7623 }
7624 }
7625
7626 if (!pseudo_destructor_p)
7627 {
7628 /* If the SCOPE is not a scalar type, we are looking at an
7629 ordinary class member access expression, rather than a
7630 pseudo-destructor-name. */
7631 bool template_p;
7632 cp_token *token = cp_lexer_peek_token (parser->lexer);
7633 /* Parse the id-expression. */
7634 name = (cp_parser_id_expression
7635 (parser,
7636 cp_parser_optional_template_keyword (parser),
7637 /*check_dependency_p=*/true,
7638 &template_p,
7639 /*declarator_p=*/false,
7640 /*optional_p=*/false));
7641 /* In general, build a SCOPE_REF if the member name is qualified.
7642 However, if the name was not dependent and has already been
7643 resolved; there is no need to build the SCOPE_REF. For example;
7644
7645 struct X { void f(); };
7646 template <typename T> void f(T* t) { t->X::f(); }
7647
7648 Even though "t" is dependent, "X::f" is not and has been resolved
7649 to a BASELINK; there is no need to include scope information. */
7650
7651 /* But we do need to remember that there was an explicit scope for
7652 virtual function calls. */
7653 if (parser->scope)
7654 *idk = CP_ID_KIND_QUALIFIED;
7655
7656 /* If the name is a template-id that names a type, we will get a
7657 TYPE_DECL here. That is invalid code. */
7658 if (TREE_CODE (name) == TYPE_DECL)
7659 {
7660 error_at (token->location, "invalid use of %qD", name);
7661 postfix_expression = error_mark_node;
7662 }
7663 else
7664 {
7665 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7666 {
7667 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7668 {
7669 error_at (token->location, "%<%D::%D%> is not a class member",
7670 parser->scope, name);
7671 postfix_expression = error_mark_node;
7672 }
7673 else
7674 name = build_qualified_name (/*type=*/NULL_TREE,
7675 parser->scope,
7676 name,
7677 template_p);
7678 parser->scope = NULL_TREE;
7679 parser->qualifying_scope = NULL_TREE;
7680 parser->object_scope = NULL_TREE;
7681 }
7682 if (parser->scope && name && BASELINK_P (name))
7683 adjust_result_of_qualified_name_lookup
7684 (name, parser->scope, scope);
7685 postfix_expression
7686 = finish_class_member_access_expr (postfix_expression, name,
7687 template_p,
7688 tf_warning_or_error);
7689 /* Build a location e.g.:
7690 ptr->access_expr
7691 ~~~^~~~~~~~~~~~~
7692 where the caret is at the deref token, ranging from
7693 the start of postfix_expression to the end of the access expr. */
7694 location_t end_loc
7695 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7696 location_t combined_loc
7697 = make_location (input_location, start_loc, end_loc);
7698 protected_set_expr_location (postfix_expression, combined_loc);
7699 }
7700 }
7701
7702 /* We no longer need to look up names in the scope of the object on
7703 the left-hand side of the `.' or `->' operator. */
7704 parser->context->object_type = NULL_TREE;
7705
7706 /* Outside of offsetof, these operators may not appear in
7707 constant-expressions. */
7708 if (!for_offsetof
7709 && (cp_parser_non_integral_constant_expression
7710 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7711 postfix_expression = error_mark_node;
7712
7713 return postfix_expression;
7714 }
7715
7716 /* Parse a parenthesized expression-list.
7717
7718 expression-list:
7719 assignment-expression
7720 expression-list, assignment-expression
7721
7722 attribute-list:
7723 expression-list
7724 identifier
7725 identifier, expression-list
7726
7727 CAST_P is true if this expression is the target of a cast.
7728
7729 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7730 argument pack.
7731
7732 WRAP_LOCATIONS_P is true if expressions within this list for which
7733 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7734 their source locations.
7735
7736 Returns a vector of trees. Each element is a representation of an
7737 assignment-expression. NULL is returned if the ( and or ) are
7738 missing. An empty, but allocated, vector is returned on no
7739 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7740 if we are parsing an attribute list for an attribute that wants a
7741 plain identifier argument, normal_attr for an attribute that wants
7742 an expression, or non_attr if we aren't parsing an attribute list. If
7743 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7744 not all of the expressions in the list were constant.
7745 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7746 will be written to with the location of the closing parenthesis. If
7747 an error occurs, it may or may not be written to. */
7748
7749 static vec<tree, va_gc> *
7750 cp_parser_parenthesized_expression_list (cp_parser* parser,
7751 int is_attribute_list,
7752 bool cast_p,
7753 bool allow_expansion_p,
7754 bool *non_constant_p,
7755 location_t *close_paren_loc,
7756 bool wrap_locations_p)
7757 {
7758 vec<tree, va_gc> *expression_list;
7759 bool fold_expr_p = is_attribute_list != non_attr;
7760 tree identifier = NULL_TREE;
7761 bool saved_greater_than_is_operator_p;
7762
7763 /* Assume all the expressions will be constant. */
7764 if (non_constant_p)
7765 *non_constant_p = false;
7766
7767 matching_parens parens;
7768 if (!parens.require_open (parser))
7769 return NULL;
7770
7771 expression_list = make_tree_vector ();
7772
7773 /* Within a parenthesized expression, a `>' token is always
7774 the greater-than operator. */
7775 saved_greater_than_is_operator_p
7776 = parser->greater_than_is_operator_p;
7777 parser->greater_than_is_operator_p = true;
7778
7779 cp_expr expr (NULL_TREE);
7780
7781 /* Consume expressions until there are no more. */
7782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7783 while (true)
7784 {
7785 /* At the beginning of attribute lists, check to see if the
7786 next token is an identifier. */
7787 if (is_attribute_list == id_attr
7788 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7789 {
7790 cp_token *token;
7791
7792 /* Consume the identifier. */
7793 token = cp_lexer_consume_token (parser->lexer);
7794 /* Save the identifier. */
7795 identifier = token->u.value;
7796 }
7797 else
7798 {
7799 bool expr_non_constant_p;
7800
7801 /* Parse the next assignment-expression. */
7802 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7803 {
7804 /* A braced-init-list. */
7805 cp_lexer_set_source_position (parser->lexer);
7806 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7807 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7808 if (non_constant_p && expr_non_constant_p)
7809 *non_constant_p = true;
7810 }
7811 else if (non_constant_p)
7812 {
7813 expr = (cp_parser_constant_expression
7814 (parser, /*allow_non_constant_p=*/true,
7815 &expr_non_constant_p));
7816 if (expr_non_constant_p)
7817 *non_constant_p = true;
7818 }
7819 else
7820 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7821 cast_p);
7822
7823 if (fold_expr_p)
7824 expr = instantiate_non_dependent_expr (expr);
7825
7826 /* If we have an ellipsis, then this is an expression
7827 expansion. */
7828 if (allow_expansion_p
7829 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7830 {
7831 /* Consume the `...'. */
7832 cp_lexer_consume_token (parser->lexer);
7833
7834 /* Build the argument pack. */
7835 expr = make_pack_expansion (expr);
7836 }
7837
7838 if (wrap_locations_p)
7839 expr.maybe_add_location_wrapper ();
7840
7841 /* Add it to the list. We add error_mark_node
7842 expressions to the list, so that we can still tell if
7843 the correct form for a parenthesized expression-list
7844 is found. That gives better errors. */
7845 vec_safe_push (expression_list, expr.get_value ());
7846
7847 if (expr == error_mark_node)
7848 goto skip_comma;
7849 }
7850
7851 /* After the first item, attribute lists look the same as
7852 expression lists. */
7853 is_attribute_list = non_attr;
7854
7855 get_comma:;
7856 /* If the next token isn't a `,', then we are done. */
7857 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7858 break;
7859
7860 /* Otherwise, consume the `,' and keep going. */
7861 cp_lexer_consume_token (parser->lexer);
7862 }
7863
7864 if (close_paren_loc)
7865 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7866
7867 if (!parens.require_close (parser))
7868 {
7869 int ending;
7870
7871 skip_comma:;
7872 /* We try and resync to an unnested comma, as that will give the
7873 user better diagnostics. */
7874 ending = cp_parser_skip_to_closing_parenthesis (parser,
7875 /*recovering=*/true,
7876 /*or_comma=*/true,
7877 /*consume_paren=*/true);
7878 if (ending < 0)
7879 goto get_comma;
7880 if (!ending)
7881 {
7882 parser->greater_than_is_operator_p
7883 = saved_greater_than_is_operator_p;
7884 return NULL;
7885 }
7886 }
7887
7888 parser->greater_than_is_operator_p
7889 = saved_greater_than_is_operator_p;
7890
7891 if (identifier)
7892 vec_safe_insert (expression_list, 0, identifier);
7893
7894 return expression_list;
7895 }
7896
7897 /* Parse a pseudo-destructor-name.
7898
7899 pseudo-destructor-name:
7900 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7901 :: [opt] nested-name-specifier template template-id :: ~ type-name
7902 :: [opt] nested-name-specifier [opt] ~ type-name
7903
7904 If either of the first two productions is used, sets *SCOPE to the
7905 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7906 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7907 or ERROR_MARK_NODE if the parse fails. */
7908
7909 static void
7910 cp_parser_pseudo_destructor_name (cp_parser* parser,
7911 tree object,
7912 tree* scope,
7913 tree* type)
7914 {
7915 bool nested_name_specifier_p;
7916
7917 /* Handle ~auto. */
7918 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7919 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7920 && !type_dependent_expression_p (object))
7921 {
7922 if (cxx_dialect < cxx14)
7923 pedwarn (input_location, 0,
7924 "%<~auto%> only available with "
7925 "-std=c++14 or -std=gnu++14");
7926 cp_lexer_consume_token (parser->lexer);
7927 cp_lexer_consume_token (parser->lexer);
7928 *scope = NULL_TREE;
7929 *type = TREE_TYPE (object);
7930 return;
7931 }
7932
7933 /* Assume that things will not work out. */
7934 *type = error_mark_node;
7935
7936 /* Look for the optional `::' operator. */
7937 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7938 /* Look for the optional nested-name-specifier. */
7939 nested_name_specifier_p
7940 = (cp_parser_nested_name_specifier_opt (parser,
7941 /*typename_keyword_p=*/false,
7942 /*check_dependency_p=*/true,
7943 /*type_p=*/false,
7944 /*is_declaration=*/false)
7945 != NULL_TREE);
7946 /* Now, if we saw a nested-name-specifier, we might be doing the
7947 second production. */
7948 if (nested_name_specifier_p
7949 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7950 {
7951 /* Consume the `template' keyword. */
7952 cp_lexer_consume_token (parser->lexer);
7953 /* Parse the template-id. */
7954 cp_parser_template_id (parser,
7955 /*template_keyword_p=*/true,
7956 /*check_dependency_p=*/false,
7957 class_type,
7958 /*is_declaration=*/true);
7959 /* Look for the `::' token. */
7960 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7961 }
7962 /* If the next token is not a `~', then there might be some
7963 additional qualification. */
7964 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7965 {
7966 /* At this point, we're looking for "type-name :: ~". The type-name
7967 must not be a class-name, since this is a pseudo-destructor. So,
7968 it must be either an enum-name, or a typedef-name -- both of which
7969 are just identifiers. So, we peek ahead to check that the "::"
7970 and "~" tokens are present; if they are not, then we can avoid
7971 calling type_name. */
7972 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7973 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7974 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7975 {
7976 cp_parser_error (parser, "non-scalar type");
7977 return;
7978 }
7979
7980 /* Look for the type-name. */
7981 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7982 if (*scope == error_mark_node)
7983 return;
7984
7985 /* Look for the `::' token. */
7986 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7987 }
7988 else
7989 *scope = NULL_TREE;
7990
7991 /* Look for the `~'. */
7992 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7993
7994 /* Once we see the ~, this has to be a pseudo-destructor. */
7995 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7996 cp_parser_commit_to_topmost_tentative_parse (parser);
7997
7998 /* Look for the type-name again. We are not responsible for
7999 checking that it matches the first type-name. */
8000 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8001 }
8002
8003 /* Parse a unary-expression.
8004
8005 unary-expression:
8006 postfix-expression
8007 ++ cast-expression
8008 -- cast-expression
8009 unary-operator cast-expression
8010 sizeof unary-expression
8011 sizeof ( type-id )
8012 alignof ( type-id ) [C++0x]
8013 new-expression
8014 delete-expression
8015
8016 GNU Extensions:
8017
8018 unary-expression:
8019 __extension__ cast-expression
8020 __alignof__ unary-expression
8021 __alignof__ ( type-id )
8022 alignof unary-expression [C++0x]
8023 __real__ cast-expression
8024 __imag__ cast-expression
8025 && identifier
8026 sizeof ( type-id ) { initializer-list , [opt] }
8027 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8028 __alignof__ ( type-id ) { initializer-list , [opt] }
8029
8030 ADDRESS_P is true iff the unary-expression is appearing as the
8031 operand of the `&' operator. CAST_P is true if this expression is
8032 the target of a cast.
8033
8034 Returns a representation of the expression. */
8035
8036 static cp_expr
8037 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8038 bool address_p, bool cast_p, bool decltype_p)
8039 {
8040 cp_token *token;
8041 enum tree_code unary_operator;
8042
8043 /* Peek at the next token. */
8044 token = cp_lexer_peek_token (parser->lexer);
8045 /* Some keywords give away the kind of expression. */
8046 if (token->type == CPP_KEYWORD)
8047 {
8048 enum rid keyword = token->keyword;
8049
8050 switch (keyword)
8051 {
8052 case RID_ALIGNOF:
8053 case RID_SIZEOF:
8054 {
8055 tree operand, ret;
8056 enum tree_code op;
8057 location_t start_loc = token->location;
8058
8059 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8060 bool std_alignof = id_equal (token->u.value, "alignof");
8061
8062 /* Consume the token. */
8063 cp_lexer_consume_token (parser->lexer);
8064 /* Parse the operand. */
8065 operand = cp_parser_sizeof_operand (parser, keyword);
8066
8067 if (TYPE_P (operand))
8068 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8069 true);
8070 else
8071 {
8072 /* ISO C++ defines alignof only with types, not with
8073 expressions. So pedwarn if alignof is used with a non-
8074 type expression. However, __alignof__ is ok. */
8075 if (std_alignof)
8076 pedwarn (token->location, OPT_Wpedantic,
8077 "ISO C++ does not allow %<alignof%> "
8078 "with a non-type");
8079
8080 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8081 }
8082 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8083 SIZEOF_EXPR with the original operand. */
8084 if (op == SIZEOF_EXPR && ret != error_mark_node)
8085 {
8086 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8087 {
8088 if (!processing_template_decl && TYPE_P (operand))
8089 {
8090 ret = build_min (SIZEOF_EXPR, size_type_node,
8091 build1 (NOP_EXPR, operand,
8092 error_mark_node));
8093 SIZEOF_EXPR_TYPE_P (ret) = 1;
8094 }
8095 else
8096 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8097 TREE_SIDE_EFFECTS (ret) = 0;
8098 TREE_READONLY (ret) = 1;
8099 }
8100 }
8101
8102 /* Construct a location e.g. :
8103 alignof (expr)
8104 ^~~~~~~~~~~~~~
8105 with start == caret at the start of the "alignof"/"sizeof"
8106 token, with the endpoint at the final closing paren. */
8107 location_t finish_loc
8108 = cp_lexer_previous_token (parser->lexer)->location;
8109 location_t compound_loc
8110 = make_location (start_loc, start_loc, finish_loc);
8111
8112 cp_expr ret_expr (ret);
8113 ret_expr.set_location (compound_loc);
8114 ret_expr = ret_expr.maybe_add_location_wrapper ();
8115 return ret_expr;
8116 }
8117
8118 case RID_NEW:
8119 return cp_parser_new_expression (parser);
8120
8121 case RID_DELETE:
8122 return cp_parser_delete_expression (parser);
8123
8124 case RID_EXTENSION:
8125 {
8126 /* The saved value of the PEDANTIC flag. */
8127 int saved_pedantic;
8128 tree expr;
8129
8130 /* Save away the PEDANTIC flag. */
8131 cp_parser_extension_opt (parser, &saved_pedantic);
8132 /* Parse the cast-expression. */
8133 expr = cp_parser_simple_cast_expression (parser);
8134 /* Restore the PEDANTIC flag. */
8135 pedantic = saved_pedantic;
8136
8137 return expr;
8138 }
8139
8140 case RID_REALPART:
8141 case RID_IMAGPART:
8142 {
8143 tree expression;
8144
8145 /* Consume the `__real__' or `__imag__' token. */
8146 cp_lexer_consume_token (parser->lexer);
8147 /* Parse the cast-expression. */
8148 expression = cp_parser_simple_cast_expression (parser);
8149 /* Create the complete representation. */
8150 return build_x_unary_op (token->location,
8151 (keyword == RID_REALPART
8152 ? REALPART_EXPR : IMAGPART_EXPR),
8153 expression,
8154 tf_warning_or_error);
8155 }
8156 break;
8157
8158 case RID_TRANSACTION_ATOMIC:
8159 case RID_TRANSACTION_RELAXED:
8160 return cp_parser_transaction_expression (parser, keyword);
8161
8162 case RID_NOEXCEPT:
8163 {
8164 tree expr;
8165 const char *saved_message;
8166 bool saved_integral_constant_expression_p;
8167 bool saved_non_integral_constant_expression_p;
8168 bool saved_greater_than_is_operator_p;
8169
8170 location_t start_loc = token->location;
8171
8172 cp_lexer_consume_token (parser->lexer);
8173 matching_parens parens;
8174 parens.require_open (parser);
8175
8176 saved_message = parser->type_definition_forbidden_message;
8177 parser->type_definition_forbidden_message
8178 = G_("types may not be defined in %<noexcept%> expressions");
8179
8180 saved_integral_constant_expression_p
8181 = parser->integral_constant_expression_p;
8182 saved_non_integral_constant_expression_p
8183 = parser->non_integral_constant_expression_p;
8184 parser->integral_constant_expression_p = false;
8185
8186 saved_greater_than_is_operator_p
8187 = parser->greater_than_is_operator_p;
8188 parser->greater_than_is_operator_p = true;
8189
8190 ++cp_unevaluated_operand;
8191 ++c_inhibit_evaluation_warnings;
8192 ++cp_noexcept_operand;
8193 expr = cp_parser_expression (parser);
8194 --cp_noexcept_operand;
8195 --c_inhibit_evaluation_warnings;
8196 --cp_unevaluated_operand;
8197
8198 parser->greater_than_is_operator_p
8199 = saved_greater_than_is_operator_p;
8200
8201 parser->integral_constant_expression_p
8202 = saved_integral_constant_expression_p;
8203 parser->non_integral_constant_expression_p
8204 = saved_non_integral_constant_expression_p;
8205
8206 parser->type_definition_forbidden_message = saved_message;
8207
8208 location_t finish_loc
8209 = cp_lexer_peek_token (parser->lexer)->location;
8210 parens.require_close (parser);
8211
8212 /* Construct a location of the form:
8213 noexcept (expr)
8214 ^~~~~~~~~~~~~~~
8215 with start == caret, finishing at the close-paren. */
8216 location_t noexcept_loc
8217 = make_location (start_loc, start_loc, finish_loc);
8218
8219 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8220 noexcept_loc);
8221 }
8222
8223 default:
8224 break;
8225 }
8226 }
8227
8228 /* Look for the `:: new' and `:: delete', which also signal the
8229 beginning of a new-expression, or delete-expression,
8230 respectively. If the next token is `::', then it might be one of
8231 these. */
8232 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8233 {
8234 enum rid keyword;
8235
8236 /* See if the token after the `::' is one of the keywords in
8237 which we're interested. */
8238 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8239 /* If it's `new', we have a new-expression. */
8240 if (keyword == RID_NEW)
8241 return cp_parser_new_expression (parser);
8242 /* Similarly, for `delete'. */
8243 else if (keyword == RID_DELETE)
8244 return cp_parser_delete_expression (parser);
8245 }
8246
8247 /* Look for a unary operator. */
8248 unary_operator = cp_parser_unary_operator (token);
8249 /* The `++' and `--' operators can be handled similarly, even though
8250 they are not technically unary-operators in the grammar. */
8251 if (unary_operator == ERROR_MARK)
8252 {
8253 if (token->type == CPP_PLUS_PLUS)
8254 unary_operator = PREINCREMENT_EXPR;
8255 else if (token->type == CPP_MINUS_MINUS)
8256 unary_operator = PREDECREMENT_EXPR;
8257 /* Handle the GNU address-of-label extension. */
8258 else if (cp_parser_allow_gnu_extensions_p (parser)
8259 && token->type == CPP_AND_AND)
8260 {
8261 tree identifier;
8262 tree expression;
8263 location_t start_loc = token->location;
8264
8265 /* Consume the '&&' token. */
8266 cp_lexer_consume_token (parser->lexer);
8267 /* Look for the identifier. */
8268 location_t finish_loc
8269 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8270 identifier = cp_parser_identifier (parser);
8271 /* Construct a location of the form:
8272 &&label
8273 ^~~~~~~
8274 with caret==start at the "&&", finish at the end of the label. */
8275 location_t combined_loc
8276 = make_location (start_loc, start_loc, finish_loc);
8277 /* Create an expression representing the address. */
8278 expression = finish_label_address_expr (identifier, combined_loc);
8279 if (cp_parser_non_integral_constant_expression (parser,
8280 NIC_ADDR_LABEL))
8281 expression = error_mark_node;
8282 return expression;
8283 }
8284 }
8285 if (unary_operator != ERROR_MARK)
8286 {
8287 cp_expr cast_expression;
8288 cp_expr expression = error_mark_node;
8289 non_integral_constant non_constant_p = NIC_NONE;
8290 location_t loc = token->location;
8291 tsubst_flags_t complain = complain_flags (decltype_p);
8292
8293 /* Consume the operator token. */
8294 token = cp_lexer_consume_token (parser->lexer);
8295 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8296
8297 /* Parse the cast-expression. */
8298 cast_expression
8299 = cp_parser_cast_expression (parser,
8300 unary_operator == ADDR_EXPR,
8301 /*cast_p=*/false,
8302 /*decltype*/false,
8303 pidk);
8304
8305 /* Make a location:
8306 OP_TOKEN CAST_EXPRESSION
8307 ^~~~~~~~~~~~~~~~~~~~~~~~~
8308 with start==caret at the operator token, and
8309 extending to the end of the cast_expression. */
8310 loc = make_location (loc, loc, cast_expression.get_finish ());
8311
8312 /* Now, build an appropriate representation. */
8313 switch (unary_operator)
8314 {
8315 case INDIRECT_REF:
8316 non_constant_p = NIC_STAR;
8317 expression = build_x_indirect_ref (loc, cast_expression,
8318 RO_UNARY_STAR,
8319 complain);
8320 /* TODO: build_x_indirect_ref does not always honor the
8321 location, so ensure it is set. */
8322 expression.set_location (loc);
8323 break;
8324
8325 case ADDR_EXPR:
8326 non_constant_p = NIC_ADDR;
8327 /* Fall through. */
8328 case BIT_NOT_EXPR:
8329 expression = build_x_unary_op (loc, unary_operator,
8330 cast_expression,
8331 complain);
8332 /* TODO: build_x_unary_op does not always honor the location,
8333 so ensure it is set. */
8334 expression.set_location (loc);
8335 break;
8336
8337 case PREINCREMENT_EXPR:
8338 case PREDECREMENT_EXPR:
8339 non_constant_p = unary_operator == PREINCREMENT_EXPR
8340 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8341 /* Fall through. */
8342 case NEGATE_EXPR:
8343 /* Immediately fold negation of a constant, unless the constant is 0
8344 (since -0 == 0) or it would overflow. */
8345 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8346 && CONSTANT_CLASS_P (cast_expression)
8347 && !integer_zerop (cast_expression)
8348 && !TREE_OVERFLOW (cast_expression))
8349 {
8350 tree folded = fold_build1 (unary_operator,
8351 TREE_TYPE (cast_expression),
8352 cast_expression);
8353 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8354 {
8355 expression = cp_expr (folded, loc);
8356 break;
8357 }
8358 }
8359 /* Fall through. */
8360 case UNARY_PLUS_EXPR:
8361 case TRUTH_NOT_EXPR:
8362 expression = finish_unary_op_expr (loc, unary_operator,
8363 cast_expression, complain);
8364 break;
8365
8366 default:
8367 gcc_unreachable ();
8368 }
8369
8370 if (non_constant_p != NIC_NONE
8371 && cp_parser_non_integral_constant_expression (parser,
8372 non_constant_p))
8373 expression = error_mark_node;
8374
8375 return expression;
8376 }
8377
8378 return cp_parser_postfix_expression (parser, address_p, cast_p,
8379 /*member_access_only_p=*/false,
8380 decltype_p,
8381 pidk);
8382 }
8383
8384 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8385 unary-operator, the corresponding tree code is returned. */
8386
8387 static enum tree_code
8388 cp_parser_unary_operator (cp_token* token)
8389 {
8390 switch (token->type)
8391 {
8392 case CPP_MULT:
8393 return INDIRECT_REF;
8394
8395 case CPP_AND:
8396 return ADDR_EXPR;
8397
8398 case CPP_PLUS:
8399 return UNARY_PLUS_EXPR;
8400
8401 case CPP_MINUS:
8402 return NEGATE_EXPR;
8403
8404 case CPP_NOT:
8405 return TRUTH_NOT_EXPR;
8406
8407 case CPP_COMPL:
8408 return BIT_NOT_EXPR;
8409
8410 default:
8411 return ERROR_MARK;
8412 }
8413 }
8414
8415 /* Parse a new-expression.
8416
8417 new-expression:
8418 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8419 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8420
8421 Returns a representation of the expression. */
8422
8423 static tree
8424 cp_parser_new_expression (cp_parser* parser)
8425 {
8426 bool global_scope_p;
8427 vec<tree, va_gc> *placement;
8428 tree type;
8429 vec<tree, va_gc> *initializer;
8430 tree nelts = NULL_TREE;
8431 tree ret;
8432
8433 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8434
8435 /* Look for the optional `::' operator. */
8436 global_scope_p
8437 = (cp_parser_global_scope_opt (parser,
8438 /*current_scope_valid_p=*/false)
8439 != NULL_TREE);
8440 /* Look for the `new' operator. */
8441 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8442 /* There's no easy way to tell a new-placement from the
8443 `( type-id )' construct. */
8444 cp_parser_parse_tentatively (parser);
8445 /* Look for a new-placement. */
8446 placement = cp_parser_new_placement (parser);
8447 /* If that didn't work out, there's no new-placement. */
8448 if (!cp_parser_parse_definitely (parser))
8449 {
8450 if (placement != NULL)
8451 release_tree_vector (placement);
8452 placement = NULL;
8453 }
8454
8455 /* If the next token is a `(', then we have a parenthesized
8456 type-id. */
8457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8458 {
8459 cp_token *token;
8460 const char *saved_message = parser->type_definition_forbidden_message;
8461
8462 /* Consume the `('. */
8463 matching_parens parens;
8464 parens.consume_open (parser);
8465
8466 /* Parse the type-id. */
8467 parser->type_definition_forbidden_message
8468 = G_("types may not be defined in a new-expression");
8469 {
8470 type_id_in_expr_sentinel s (parser);
8471 type = cp_parser_type_id (parser);
8472 }
8473 parser->type_definition_forbidden_message = saved_message;
8474
8475 /* Look for the closing `)'. */
8476 parens.require_close (parser);
8477 token = cp_lexer_peek_token (parser->lexer);
8478 /* There should not be a direct-new-declarator in this production,
8479 but GCC used to allowed this, so we check and emit a sensible error
8480 message for this case. */
8481 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8482 {
8483 error_at (token->location,
8484 "array bound forbidden after parenthesized type-id");
8485 inform (token->location,
8486 "try removing the parentheses around the type-id");
8487 cp_parser_direct_new_declarator (parser);
8488 }
8489 }
8490 /* Otherwise, there must be a new-type-id. */
8491 else
8492 type = cp_parser_new_type_id (parser, &nelts);
8493
8494 /* If the next token is a `(' or '{', then we have a new-initializer. */
8495 cp_token *token = cp_lexer_peek_token (parser->lexer);
8496 if (token->type == CPP_OPEN_PAREN
8497 || token->type == CPP_OPEN_BRACE)
8498 initializer = cp_parser_new_initializer (parser);
8499 else
8500 initializer = NULL;
8501
8502 /* A new-expression may not appear in an integral constant
8503 expression. */
8504 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8505 ret = error_mark_node;
8506 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8507 of a new-type-id or type-id of a new-expression, the new-expression shall
8508 contain a new-initializer of the form ( assignment-expression )".
8509 Additionally, consistently with the spirit of DR 1467, we want to accept
8510 'new auto { 2 }' too. */
8511 else if ((ret = type_uses_auto (type))
8512 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8513 && (vec_safe_length (initializer) != 1
8514 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8515 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8516 {
8517 error_at (token->location,
8518 "initialization of new-expression for type %<auto%> "
8519 "requires exactly one element");
8520 ret = error_mark_node;
8521 }
8522 else
8523 {
8524 /* Construct a location e.g.:
8525 ptr = new int[100]
8526 ^~~~~~~~~~~~
8527 with caret == start at the start of the "new" token, and the end
8528 at the end of the final token we consumed. */
8529 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8530 location_t end_loc = get_finish (end_tok->location);
8531 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8532
8533 /* Create a representation of the new-expression. */
8534 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8535 tf_warning_or_error);
8536 protected_set_expr_location (ret, combined_loc);
8537 }
8538
8539 if (placement != NULL)
8540 release_tree_vector (placement);
8541 if (initializer != NULL)
8542 release_tree_vector (initializer);
8543
8544 return ret;
8545 }
8546
8547 /* Parse a new-placement.
8548
8549 new-placement:
8550 ( expression-list )
8551
8552 Returns the same representation as for an expression-list. */
8553
8554 static vec<tree, va_gc> *
8555 cp_parser_new_placement (cp_parser* parser)
8556 {
8557 vec<tree, va_gc> *expression_list;
8558
8559 /* Parse the expression-list. */
8560 expression_list = (cp_parser_parenthesized_expression_list
8561 (parser, non_attr, /*cast_p=*/false,
8562 /*allow_expansion_p=*/true,
8563 /*non_constant_p=*/NULL));
8564
8565 if (expression_list && expression_list->is_empty ())
8566 error ("expected expression-list or type-id");
8567
8568 return expression_list;
8569 }
8570
8571 /* Parse a new-type-id.
8572
8573 new-type-id:
8574 type-specifier-seq new-declarator [opt]
8575
8576 Returns the TYPE allocated. If the new-type-id indicates an array
8577 type, *NELTS is set to the number of elements in the last array
8578 bound; the TYPE will not include the last array bound. */
8579
8580 static tree
8581 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8582 {
8583 cp_decl_specifier_seq type_specifier_seq;
8584 cp_declarator *new_declarator;
8585 cp_declarator *declarator;
8586 cp_declarator *outer_declarator;
8587 const char *saved_message;
8588
8589 /* The type-specifier sequence must not contain type definitions.
8590 (It cannot contain declarations of new types either, but if they
8591 are not definitions we will catch that because they are not
8592 complete.) */
8593 saved_message = parser->type_definition_forbidden_message;
8594 parser->type_definition_forbidden_message
8595 = G_("types may not be defined in a new-type-id");
8596 /* Parse the type-specifier-seq. */
8597 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8598 /*is_trailing_return=*/false,
8599 &type_specifier_seq);
8600 /* Restore the old message. */
8601 parser->type_definition_forbidden_message = saved_message;
8602
8603 if (type_specifier_seq.type == error_mark_node)
8604 return error_mark_node;
8605
8606 /* Parse the new-declarator. */
8607 new_declarator = cp_parser_new_declarator_opt (parser);
8608
8609 /* Determine the number of elements in the last array dimension, if
8610 any. */
8611 *nelts = NULL_TREE;
8612 /* Skip down to the last array dimension. */
8613 declarator = new_declarator;
8614 outer_declarator = NULL;
8615 while (declarator && (declarator->kind == cdk_pointer
8616 || declarator->kind == cdk_ptrmem))
8617 {
8618 outer_declarator = declarator;
8619 declarator = declarator->declarator;
8620 }
8621 while (declarator
8622 && declarator->kind == cdk_array
8623 && declarator->declarator
8624 && declarator->declarator->kind == cdk_array)
8625 {
8626 outer_declarator = declarator;
8627 declarator = declarator->declarator;
8628 }
8629
8630 if (declarator && declarator->kind == cdk_array)
8631 {
8632 *nelts = declarator->u.array.bounds;
8633 if (*nelts == error_mark_node)
8634 *nelts = integer_one_node;
8635
8636 if (outer_declarator)
8637 outer_declarator->declarator = declarator->declarator;
8638 else
8639 new_declarator = NULL;
8640 }
8641
8642 return groktypename (&type_specifier_seq, new_declarator, false);
8643 }
8644
8645 /* Parse an (optional) new-declarator.
8646
8647 new-declarator:
8648 ptr-operator new-declarator [opt]
8649 direct-new-declarator
8650
8651 Returns the declarator. */
8652
8653 static cp_declarator *
8654 cp_parser_new_declarator_opt (cp_parser* parser)
8655 {
8656 enum tree_code code;
8657 tree type, std_attributes = NULL_TREE;
8658 cp_cv_quals cv_quals;
8659
8660 /* We don't know if there's a ptr-operator next, or not. */
8661 cp_parser_parse_tentatively (parser);
8662 /* Look for a ptr-operator. */
8663 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8664 /* If that worked, look for more new-declarators. */
8665 if (cp_parser_parse_definitely (parser))
8666 {
8667 cp_declarator *declarator;
8668
8669 /* Parse another optional declarator. */
8670 declarator = cp_parser_new_declarator_opt (parser);
8671
8672 declarator = cp_parser_make_indirect_declarator
8673 (code, type, cv_quals, declarator, std_attributes);
8674
8675 return declarator;
8676 }
8677
8678 /* If the next token is a `[', there is a direct-new-declarator. */
8679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8680 return cp_parser_direct_new_declarator (parser);
8681
8682 return NULL;
8683 }
8684
8685 /* Parse a direct-new-declarator.
8686
8687 direct-new-declarator:
8688 [ expression ]
8689 direct-new-declarator [constant-expression]
8690
8691 */
8692
8693 static cp_declarator *
8694 cp_parser_direct_new_declarator (cp_parser* parser)
8695 {
8696 cp_declarator *declarator = NULL;
8697
8698 while (true)
8699 {
8700 tree expression;
8701 cp_token *token;
8702
8703 /* Look for the opening `['. */
8704 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8705
8706 token = cp_lexer_peek_token (parser->lexer);
8707 expression = cp_parser_expression (parser);
8708 /* The standard requires that the expression have integral
8709 type. DR 74 adds enumeration types. We believe that the
8710 real intent is that these expressions be handled like the
8711 expression in a `switch' condition, which also allows
8712 classes with a single conversion to integral or
8713 enumeration type. */
8714 if (!processing_template_decl)
8715 {
8716 expression
8717 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8718 expression,
8719 /*complain=*/true);
8720 if (!expression)
8721 {
8722 error_at (token->location,
8723 "expression in new-declarator must have integral "
8724 "or enumeration type");
8725 expression = error_mark_node;
8726 }
8727 }
8728
8729 /* Look for the closing `]'. */
8730 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8731
8732 /* Add this bound to the declarator. */
8733 declarator = make_array_declarator (declarator, expression);
8734
8735 /* If the next token is not a `[', then there are no more
8736 bounds. */
8737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8738 break;
8739 }
8740
8741 return declarator;
8742 }
8743
8744 /* Parse a new-initializer.
8745
8746 new-initializer:
8747 ( expression-list [opt] )
8748 braced-init-list
8749
8750 Returns a representation of the expression-list. */
8751
8752 static vec<tree, va_gc> *
8753 cp_parser_new_initializer (cp_parser* parser)
8754 {
8755 vec<tree, va_gc> *expression_list;
8756
8757 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8758 {
8759 tree t;
8760 bool expr_non_constant_p;
8761 cp_lexer_set_source_position (parser->lexer);
8762 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8763 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8764 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8765 expression_list = make_tree_vector_single (t);
8766 }
8767 else
8768 expression_list = (cp_parser_parenthesized_expression_list
8769 (parser, non_attr, /*cast_p=*/false,
8770 /*allow_expansion_p=*/true,
8771 /*non_constant_p=*/NULL));
8772
8773 return expression_list;
8774 }
8775
8776 /* Parse a delete-expression.
8777
8778 delete-expression:
8779 :: [opt] delete cast-expression
8780 :: [opt] delete [ ] cast-expression
8781
8782 Returns a representation of the expression. */
8783
8784 static tree
8785 cp_parser_delete_expression (cp_parser* parser)
8786 {
8787 bool global_scope_p;
8788 bool array_p;
8789 tree expression;
8790
8791 /* Look for the optional `::' operator. */
8792 global_scope_p
8793 = (cp_parser_global_scope_opt (parser,
8794 /*current_scope_valid_p=*/false)
8795 != NULL_TREE);
8796 /* Look for the `delete' keyword. */
8797 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8798 /* See if the array syntax is in use. */
8799 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8800 {
8801 /* Consume the `[' token. */
8802 cp_lexer_consume_token (parser->lexer);
8803 /* Look for the `]' token. */
8804 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8805 /* Remember that this is the `[]' construct. */
8806 array_p = true;
8807 }
8808 else
8809 array_p = false;
8810
8811 /* Parse the cast-expression. */
8812 expression = cp_parser_simple_cast_expression (parser);
8813
8814 /* A delete-expression may not appear in an integral constant
8815 expression. */
8816 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8817 return error_mark_node;
8818
8819 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8820 tf_warning_or_error);
8821 }
8822
8823 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8824 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8825 0 otherwise. */
8826
8827 static int
8828 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8829 {
8830 cp_token *token = cp_lexer_peek_token (parser->lexer);
8831 switch (token->type)
8832 {
8833 case CPP_COMMA:
8834 case CPP_SEMICOLON:
8835 case CPP_QUERY:
8836 case CPP_COLON:
8837 case CPP_CLOSE_SQUARE:
8838 case CPP_CLOSE_PAREN:
8839 case CPP_CLOSE_BRACE:
8840 case CPP_OPEN_BRACE:
8841 case CPP_DOT:
8842 case CPP_DOT_STAR:
8843 case CPP_DEREF:
8844 case CPP_DEREF_STAR:
8845 case CPP_DIV:
8846 case CPP_MOD:
8847 case CPP_LSHIFT:
8848 case CPP_RSHIFT:
8849 case CPP_LESS:
8850 case CPP_GREATER:
8851 case CPP_LESS_EQ:
8852 case CPP_GREATER_EQ:
8853 case CPP_EQ_EQ:
8854 case CPP_NOT_EQ:
8855 case CPP_EQ:
8856 case CPP_MULT_EQ:
8857 case CPP_DIV_EQ:
8858 case CPP_MOD_EQ:
8859 case CPP_PLUS_EQ:
8860 case CPP_MINUS_EQ:
8861 case CPP_RSHIFT_EQ:
8862 case CPP_LSHIFT_EQ:
8863 case CPP_AND_EQ:
8864 case CPP_XOR_EQ:
8865 case CPP_OR_EQ:
8866 case CPP_XOR:
8867 case CPP_OR:
8868 case CPP_OR_OR:
8869 case CPP_EOF:
8870 case CPP_ELLIPSIS:
8871 return 0;
8872
8873 case CPP_OPEN_PAREN:
8874 /* In ((type ()) () the last () isn't a valid cast-expression,
8875 so the whole must be parsed as postfix-expression. */
8876 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8877 != CPP_CLOSE_PAREN;
8878
8879 case CPP_OPEN_SQUARE:
8880 /* '[' may start a primary-expression in obj-c++ and in C++11,
8881 as a lambda-expression, eg, '(void)[]{}'. */
8882 if (cxx_dialect >= cxx11)
8883 return -1;
8884 return c_dialect_objc ();
8885
8886 case CPP_PLUS_PLUS:
8887 case CPP_MINUS_MINUS:
8888 /* '++' and '--' may or may not start a cast-expression:
8889
8890 struct T { void operator++(int); };
8891 void f() { (T())++; }
8892
8893 vs
8894
8895 int a;
8896 (int)++a; */
8897 return -1;
8898
8899 default:
8900 return 1;
8901 }
8902 }
8903
8904 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8905 in the order: const_cast, static_cast, reinterpret_cast.
8906
8907 Don't suggest dynamic_cast.
8908
8909 Return the first legal cast kind found, or NULL otherwise. */
8910
8911 static const char *
8912 get_cast_suggestion (tree dst_type, tree orig_expr)
8913 {
8914 tree trial;
8915
8916 /* Reuse the parser logic by attempting to build the various kinds of
8917 cast, with "complain" disabled.
8918 Identify the first such cast that is valid. */
8919
8920 /* Don't attempt to run such logic within template processing. */
8921 if (processing_template_decl)
8922 return NULL;
8923
8924 /* First try const_cast. */
8925 trial = build_const_cast (dst_type, orig_expr, tf_none);
8926 if (trial != error_mark_node)
8927 return "const_cast";
8928
8929 /* If that fails, try static_cast. */
8930 trial = build_static_cast (dst_type, orig_expr, tf_none);
8931 if (trial != error_mark_node)
8932 return "static_cast";
8933
8934 /* Finally, try reinterpret_cast. */
8935 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8936 if (trial != error_mark_node)
8937 return "reinterpret_cast";
8938
8939 /* No such cast possible. */
8940 return NULL;
8941 }
8942
8943 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8944 suggesting how to convert a C-style cast of the form:
8945
8946 (DST_TYPE)ORIG_EXPR
8947
8948 to a C++-style cast.
8949
8950 The primary range of RICHLOC is asssumed to be that of the original
8951 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8952 of the parens in the C-style cast. */
8953
8954 static void
8955 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8956 location_t close_paren_loc, tree orig_expr,
8957 tree dst_type)
8958 {
8959 /* This function is non-trivial, so bail out now if the warning isn't
8960 going to be emitted. */
8961 if (!warn_old_style_cast)
8962 return;
8963
8964 /* Try to find a legal C++ cast, trying them in order:
8965 const_cast, static_cast, reinterpret_cast. */
8966 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8967 if (!cast_suggestion)
8968 return;
8969
8970 /* Replace the open paren with "CAST_SUGGESTION<". */
8971 pretty_printer pp;
8972 pp_printf (&pp, "%s<", cast_suggestion);
8973 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8974
8975 /* Replace the close paren with "> (". */
8976 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8977
8978 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8979 rich_loc->add_fixit_insert_after (")");
8980 }
8981
8982
8983 /* Parse a cast-expression.
8984
8985 cast-expression:
8986 unary-expression
8987 ( type-id ) cast-expression
8988
8989 ADDRESS_P is true iff the unary-expression is appearing as the
8990 operand of the `&' operator. CAST_P is true if this expression is
8991 the target of a cast.
8992
8993 Returns a representation of the expression. */
8994
8995 static cp_expr
8996 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8997 bool decltype_p, cp_id_kind * pidk)
8998 {
8999 /* If it's a `(', then we might be looking at a cast. */
9000 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9001 {
9002 tree type = NULL_TREE;
9003 cp_expr expr (NULL_TREE);
9004 int cast_expression = 0;
9005 const char *saved_message;
9006
9007 /* There's no way to know yet whether or not this is a cast.
9008 For example, `(int (3))' is a unary-expression, while `(int)
9009 3' is a cast. So, we resort to parsing tentatively. */
9010 cp_parser_parse_tentatively (parser);
9011 /* Types may not be defined in a cast. */
9012 saved_message = parser->type_definition_forbidden_message;
9013 parser->type_definition_forbidden_message
9014 = G_("types may not be defined in casts");
9015 /* Consume the `('. */
9016 matching_parens parens;
9017 cp_token *open_paren = parens.consume_open (parser);
9018 location_t open_paren_loc = open_paren->location;
9019 location_t close_paren_loc = UNKNOWN_LOCATION;
9020
9021 /* A very tricky bit is that `(struct S) { 3 }' is a
9022 compound-literal (which we permit in C++ as an extension).
9023 But, that construct is not a cast-expression -- it is a
9024 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9025 is legal; if the compound-literal were a cast-expression,
9026 you'd need an extra set of parentheses.) But, if we parse
9027 the type-id, and it happens to be a class-specifier, then we
9028 will commit to the parse at that point, because we cannot
9029 undo the action that is done when creating a new class. So,
9030 then we cannot back up and do a postfix-expression.
9031
9032 Another tricky case is the following (c++/29234):
9033
9034 struct S { void operator () (); };
9035
9036 void foo ()
9037 {
9038 ( S()() );
9039 }
9040
9041 As a type-id we parse the parenthesized S()() as a function
9042 returning a function, groktypename complains and we cannot
9043 back up in this case either.
9044
9045 Therefore, we scan ahead to the closing `)', and check to see
9046 if the tokens after the `)' can start a cast-expression. Otherwise
9047 we are dealing with an unary-expression, a postfix-expression
9048 or something else.
9049
9050 Yet another tricky case, in C++11, is the following (c++/54891):
9051
9052 (void)[]{};
9053
9054 The issue is that usually, besides the case of lambda-expressions,
9055 the parenthesized type-id cannot be followed by '[', and, eg, we
9056 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9057 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9058 we don't commit, we try a cast-expression, then an unary-expression.
9059
9060 Save tokens so that we can put them back. */
9061 cp_lexer_save_tokens (parser->lexer);
9062
9063 /* We may be looking at a cast-expression. */
9064 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9065 /*consume_paren=*/true))
9066 cast_expression
9067 = cp_parser_tokens_start_cast_expression (parser);
9068
9069 /* Roll back the tokens we skipped. */
9070 cp_lexer_rollback_tokens (parser->lexer);
9071 /* If we aren't looking at a cast-expression, simulate an error so
9072 that the call to cp_parser_error_occurred below returns true. */
9073 if (!cast_expression)
9074 cp_parser_simulate_error (parser);
9075 else
9076 {
9077 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9078 parser->in_type_id_in_expr_p = true;
9079 /* Look for the type-id. */
9080 type = cp_parser_type_id (parser);
9081 /* Look for the closing `)'. */
9082 cp_token *close_paren = parens.require_close (parser);
9083 if (close_paren)
9084 close_paren_loc = close_paren->location;
9085 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9086 }
9087
9088 /* Restore the saved message. */
9089 parser->type_definition_forbidden_message = saved_message;
9090
9091 /* At this point this can only be either a cast or a
9092 parenthesized ctor such as `(T ())' that looks like a cast to
9093 function returning T. */
9094 if (!cp_parser_error_occurred (parser))
9095 {
9096 /* Only commit if the cast-expression doesn't start with
9097 '++', '--', or '[' in C++11. */
9098 if (cast_expression > 0)
9099 cp_parser_commit_to_topmost_tentative_parse (parser);
9100
9101 expr = cp_parser_cast_expression (parser,
9102 /*address_p=*/false,
9103 /*cast_p=*/true,
9104 /*decltype_p=*/false,
9105 pidk);
9106
9107 if (cp_parser_parse_definitely (parser))
9108 {
9109 /* Warn about old-style casts, if so requested. */
9110 if (warn_old_style_cast
9111 && !in_system_header_at (input_location)
9112 && !VOID_TYPE_P (type)
9113 && current_lang_name != lang_name_c)
9114 {
9115 gcc_rich_location rich_loc (input_location);
9116 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9117 expr, type);
9118 warning_at (&rich_loc, OPT_Wold_style_cast,
9119 "use of old-style cast to %q#T", type);
9120 }
9121
9122 /* Only type conversions to integral or enumeration types
9123 can be used in constant-expressions. */
9124 if (!cast_valid_in_integral_constant_expression_p (type)
9125 && cp_parser_non_integral_constant_expression (parser,
9126 NIC_CAST))
9127 return error_mark_node;
9128
9129 /* Perform the cast. */
9130 /* Make a location:
9131 (TYPE) EXPR
9132 ^~~~~~~~~~~
9133 with start==caret at the open paren, extending to the
9134 end of "expr". */
9135 location_t cast_loc = make_location (open_paren_loc,
9136 open_paren_loc,
9137 expr.get_finish ());
9138 expr = build_c_cast (cast_loc, type, expr);
9139 return expr;
9140 }
9141 }
9142 else
9143 cp_parser_abort_tentative_parse (parser);
9144 }
9145
9146 /* If we get here, then it's not a cast, so it must be a
9147 unary-expression. */
9148 return cp_parser_unary_expression (parser, pidk, address_p,
9149 cast_p, decltype_p);
9150 }
9151
9152 /* Parse a binary expression of the general form:
9153
9154 pm-expression:
9155 cast-expression
9156 pm-expression .* cast-expression
9157 pm-expression ->* cast-expression
9158
9159 multiplicative-expression:
9160 pm-expression
9161 multiplicative-expression * pm-expression
9162 multiplicative-expression / pm-expression
9163 multiplicative-expression % pm-expression
9164
9165 additive-expression:
9166 multiplicative-expression
9167 additive-expression + multiplicative-expression
9168 additive-expression - multiplicative-expression
9169
9170 shift-expression:
9171 additive-expression
9172 shift-expression << additive-expression
9173 shift-expression >> additive-expression
9174
9175 relational-expression:
9176 shift-expression
9177 relational-expression < shift-expression
9178 relational-expression > shift-expression
9179 relational-expression <= shift-expression
9180 relational-expression >= shift-expression
9181
9182 GNU Extension:
9183
9184 relational-expression:
9185 relational-expression <? shift-expression
9186 relational-expression >? shift-expression
9187
9188 equality-expression:
9189 relational-expression
9190 equality-expression == relational-expression
9191 equality-expression != relational-expression
9192
9193 and-expression:
9194 equality-expression
9195 and-expression & equality-expression
9196
9197 exclusive-or-expression:
9198 and-expression
9199 exclusive-or-expression ^ and-expression
9200
9201 inclusive-or-expression:
9202 exclusive-or-expression
9203 inclusive-or-expression | exclusive-or-expression
9204
9205 logical-and-expression:
9206 inclusive-or-expression
9207 logical-and-expression && inclusive-or-expression
9208
9209 logical-or-expression:
9210 logical-and-expression
9211 logical-or-expression || logical-and-expression
9212
9213 All these are implemented with a single function like:
9214
9215 binary-expression:
9216 simple-cast-expression
9217 binary-expression <token> binary-expression
9218
9219 CAST_P is true if this expression is the target of a cast.
9220
9221 The binops_by_token map is used to get the tree codes for each <token> type.
9222 binary-expressions are associated according to a precedence table. */
9223
9224 #define TOKEN_PRECEDENCE(token) \
9225 (((token->type == CPP_GREATER \
9226 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9227 && !parser->greater_than_is_operator_p) \
9228 ? PREC_NOT_OPERATOR \
9229 : binops_by_token[token->type].prec)
9230
9231 static cp_expr
9232 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9233 bool no_toplevel_fold_p,
9234 bool decltype_p,
9235 enum cp_parser_prec prec,
9236 cp_id_kind * pidk)
9237 {
9238 cp_parser_expression_stack stack;
9239 cp_parser_expression_stack_entry *sp = &stack[0];
9240 cp_parser_expression_stack_entry current;
9241 cp_expr rhs;
9242 cp_token *token;
9243 enum tree_code rhs_type;
9244 enum cp_parser_prec new_prec, lookahead_prec;
9245 tree overload;
9246
9247 /* Parse the first expression. */
9248 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9249 ? TRUTH_NOT_EXPR : ERROR_MARK);
9250 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9251 cast_p, decltype_p, pidk);
9252 current.prec = prec;
9253
9254 if (cp_parser_error_occurred (parser))
9255 return error_mark_node;
9256
9257 for (;;)
9258 {
9259 /* Get an operator token. */
9260 token = cp_lexer_peek_token (parser->lexer);
9261
9262 if (warn_cxx11_compat
9263 && token->type == CPP_RSHIFT
9264 && !parser->greater_than_is_operator_p)
9265 {
9266 if (warning_at (token->location, OPT_Wc__11_compat,
9267 "%<>>%> operator is treated"
9268 " as two right angle brackets in C++11"))
9269 inform (token->location,
9270 "suggest parentheses around %<>>%> expression");
9271 }
9272
9273 new_prec = TOKEN_PRECEDENCE (token);
9274 if (new_prec != PREC_NOT_OPERATOR
9275 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9276 /* This is a fold-expression; handle it later. */
9277 new_prec = PREC_NOT_OPERATOR;
9278
9279 /* Popping an entry off the stack means we completed a subexpression:
9280 - either we found a token which is not an operator (`>' where it is not
9281 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9282 will happen repeatedly;
9283 - or, we found an operator which has lower priority. This is the case
9284 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9285 parsing `3 * 4'. */
9286 if (new_prec <= current.prec)
9287 {
9288 if (sp == stack)
9289 break;
9290 else
9291 goto pop;
9292 }
9293
9294 get_rhs:
9295 current.tree_type = binops_by_token[token->type].tree_type;
9296 current.loc = token->location;
9297
9298 /* We used the operator token. */
9299 cp_lexer_consume_token (parser->lexer);
9300
9301 /* For "false && x" or "true || x", x will never be executed;
9302 disable warnings while evaluating it. */
9303 if (current.tree_type == TRUTH_ANDIF_EXPR)
9304 c_inhibit_evaluation_warnings +=
9305 cp_fully_fold (current.lhs) == truthvalue_false_node;
9306 else if (current.tree_type == TRUTH_ORIF_EXPR)
9307 c_inhibit_evaluation_warnings +=
9308 cp_fully_fold (current.lhs) == truthvalue_true_node;
9309
9310 /* Extract another operand. It may be the RHS of this expression
9311 or the LHS of a new, higher priority expression. */
9312 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9313 ? TRUTH_NOT_EXPR : ERROR_MARK);
9314 rhs = cp_parser_simple_cast_expression (parser);
9315
9316 /* Get another operator token. Look up its precedence to avoid
9317 building a useless (immediately popped) stack entry for common
9318 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9319 token = cp_lexer_peek_token (parser->lexer);
9320 lookahead_prec = TOKEN_PRECEDENCE (token);
9321 if (lookahead_prec != PREC_NOT_OPERATOR
9322 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9323 lookahead_prec = PREC_NOT_OPERATOR;
9324 if (lookahead_prec > new_prec)
9325 {
9326 /* ... and prepare to parse the RHS of the new, higher priority
9327 expression. Since precedence levels on the stack are
9328 monotonically increasing, we do not have to care about
9329 stack overflows. */
9330 *sp = current;
9331 ++sp;
9332 current.lhs = rhs;
9333 current.lhs_type = rhs_type;
9334 current.prec = new_prec;
9335 new_prec = lookahead_prec;
9336 goto get_rhs;
9337
9338 pop:
9339 lookahead_prec = new_prec;
9340 /* If the stack is not empty, we have parsed into LHS the right side
9341 (`4' in the example above) of an expression we had suspended.
9342 We can use the information on the stack to recover the LHS (`3')
9343 from the stack together with the tree code (`MULT_EXPR'), and
9344 the precedence of the higher level subexpression
9345 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9346 which will be used to actually build the additive expression. */
9347 rhs = current.lhs;
9348 rhs_type = current.lhs_type;
9349 --sp;
9350 current = *sp;
9351 }
9352
9353 /* Undo the disabling of warnings done above. */
9354 if (current.tree_type == TRUTH_ANDIF_EXPR)
9355 c_inhibit_evaluation_warnings -=
9356 cp_fully_fold (current.lhs) == truthvalue_false_node;
9357 else if (current.tree_type == TRUTH_ORIF_EXPR)
9358 c_inhibit_evaluation_warnings -=
9359 cp_fully_fold (current.lhs) == truthvalue_true_node;
9360
9361 if (warn_logical_not_paren
9362 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9363 && current.lhs_type == TRUTH_NOT_EXPR
9364 /* Avoid warning for !!x == y. */
9365 && (TREE_CODE (current.lhs) != NE_EXPR
9366 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9367 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9368 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9369 /* Avoid warning for !b == y where b is boolean. */
9370 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9371 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9372 != BOOLEAN_TYPE))))
9373 /* Avoid warning for !!b == y where b is boolean. */
9374 && (!DECL_P (current.lhs)
9375 || TREE_TYPE (current.lhs) == NULL_TREE
9376 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9377 warn_logical_not_parentheses (current.loc, current.tree_type,
9378 current.lhs, maybe_constant_value (rhs));
9379
9380 overload = NULL;
9381
9382 location_t combined_loc = make_location (current.loc,
9383 current.lhs.get_start (),
9384 rhs.get_finish ());
9385
9386 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9387 ERROR_MARK for everything that is not a binary expression.
9388 This makes warn_about_parentheses miss some warnings that
9389 involve unary operators. For unary expressions we should
9390 pass the correct tree_code unless the unary expression was
9391 surrounded by parentheses.
9392 */
9393 if (no_toplevel_fold_p
9394 && lookahead_prec <= current.prec
9395 && sp == stack)
9396 {
9397 if (current.lhs == error_mark_node || rhs == error_mark_node)
9398 current.lhs = error_mark_node;
9399 else
9400 {
9401 current.lhs
9402 = build_min (current.tree_type,
9403 TREE_CODE_CLASS (current.tree_type)
9404 == tcc_comparison
9405 ? boolean_type_node : TREE_TYPE (current.lhs),
9406 current.lhs.get_value (), rhs.get_value ());
9407 SET_EXPR_LOCATION (current.lhs, combined_loc);
9408 }
9409 }
9410 else
9411 {
9412 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9413 current.lhs, current.lhs_type,
9414 rhs, rhs_type, &overload,
9415 complain_flags (decltype_p));
9416 /* TODO: build_x_binary_op doesn't always honor the location. */
9417 current.lhs.set_location (combined_loc);
9418 }
9419 current.lhs_type = current.tree_type;
9420
9421 /* If the binary operator required the use of an overloaded operator,
9422 then this expression cannot be an integral constant-expression.
9423 An overloaded operator can be used even if both operands are
9424 otherwise permissible in an integral constant-expression if at
9425 least one of the operands is of enumeration type. */
9426
9427 if (overload
9428 && cp_parser_non_integral_constant_expression (parser,
9429 NIC_OVERLOADED))
9430 return error_mark_node;
9431 }
9432
9433 return current.lhs;
9434 }
9435
9436 static cp_expr
9437 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9438 bool no_toplevel_fold_p,
9439 enum cp_parser_prec prec,
9440 cp_id_kind * pidk)
9441 {
9442 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9443 /*decltype*/false, prec, pidk);
9444 }
9445
9446 /* Parse the `? expression : assignment-expression' part of a
9447 conditional-expression. The LOGICAL_OR_EXPR is the
9448 logical-or-expression that started the conditional-expression.
9449 Returns a representation of the entire conditional-expression.
9450
9451 This routine is used by cp_parser_assignment_expression.
9452
9453 ? expression : assignment-expression
9454
9455 GNU Extensions:
9456
9457 ? : assignment-expression */
9458
9459 static tree
9460 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9461 {
9462 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9463 cp_expr assignment_expr;
9464 struct cp_token *token;
9465 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9466
9467 /* Consume the `?' token. */
9468 cp_lexer_consume_token (parser->lexer);
9469 token = cp_lexer_peek_token (parser->lexer);
9470 if (cp_parser_allow_gnu_extensions_p (parser)
9471 && token->type == CPP_COLON)
9472 {
9473 pedwarn (token->location, OPT_Wpedantic,
9474 "ISO C++ does not allow ?: with omitted middle operand");
9475 /* Implicit true clause. */
9476 expr = NULL_TREE;
9477 c_inhibit_evaluation_warnings +=
9478 folded_logical_or_expr == truthvalue_true_node;
9479 warn_for_omitted_condop (token->location, logical_or_expr);
9480 }
9481 else
9482 {
9483 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9484 parser->colon_corrects_to_scope_p = false;
9485 /* Parse the expression. */
9486 c_inhibit_evaluation_warnings +=
9487 folded_logical_or_expr == truthvalue_false_node;
9488 expr = cp_parser_expression (parser);
9489 c_inhibit_evaluation_warnings +=
9490 ((folded_logical_or_expr == truthvalue_true_node)
9491 - (folded_logical_or_expr == truthvalue_false_node));
9492 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9493 }
9494
9495 /* The next token should be a `:'. */
9496 cp_parser_require (parser, CPP_COLON, RT_COLON);
9497 /* Parse the assignment-expression. */
9498 assignment_expr = cp_parser_assignment_expression (parser);
9499 c_inhibit_evaluation_warnings -=
9500 folded_logical_or_expr == truthvalue_true_node;
9501
9502 /* Make a location:
9503 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9504 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9505 with the caret at the "?", ranging from the start of
9506 the logical_or_expr to the end of the assignment_expr. */
9507 loc = make_location (loc,
9508 logical_or_expr.get_start (),
9509 assignment_expr.get_finish ());
9510
9511 /* Build the conditional-expression. */
9512 return build_x_conditional_expr (loc, logical_or_expr,
9513 expr,
9514 assignment_expr,
9515 tf_warning_or_error);
9516 }
9517
9518 /* Parse an assignment-expression.
9519
9520 assignment-expression:
9521 conditional-expression
9522 logical-or-expression assignment-operator assignment_expression
9523 throw-expression
9524
9525 CAST_P is true if this expression is the target of a cast.
9526 DECLTYPE_P is true if this expression is the operand of decltype.
9527
9528 Returns a representation for the expression. */
9529
9530 static cp_expr
9531 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9532 bool cast_p, bool decltype_p)
9533 {
9534 cp_expr expr;
9535
9536 /* If the next token is the `throw' keyword, then we're looking at
9537 a throw-expression. */
9538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9539 expr = cp_parser_throw_expression (parser);
9540 /* Otherwise, it must be that we are looking at a
9541 logical-or-expression. */
9542 else
9543 {
9544 /* Parse the binary expressions (logical-or-expression). */
9545 expr = cp_parser_binary_expression (parser, cast_p, false,
9546 decltype_p,
9547 PREC_NOT_OPERATOR, pidk);
9548 /* If the next token is a `?' then we're actually looking at a
9549 conditional-expression. */
9550 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9551 return cp_parser_question_colon_clause (parser, expr);
9552 else
9553 {
9554 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9555
9556 /* If it's an assignment-operator, we're using the second
9557 production. */
9558 enum tree_code assignment_operator
9559 = cp_parser_assignment_operator_opt (parser);
9560 if (assignment_operator != ERROR_MARK)
9561 {
9562 bool non_constant_p;
9563
9564 /* Parse the right-hand side of the assignment. */
9565 cp_expr rhs = cp_parser_initializer_clause (parser,
9566 &non_constant_p);
9567
9568 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9569 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9570
9571 /* An assignment may not appear in a
9572 constant-expression. */
9573 if (cp_parser_non_integral_constant_expression (parser,
9574 NIC_ASSIGNMENT))
9575 return error_mark_node;
9576 /* Build the assignment expression. Its default
9577 location:
9578 LHS = RHS
9579 ~~~~^~~~~
9580 is the location of the '=' token as the
9581 caret, ranging from the start of the lhs to the
9582 end of the rhs. */
9583 loc = make_location (loc,
9584 expr.get_start (),
9585 rhs.get_finish ());
9586 expr = build_x_modify_expr (loc, expr,
9587 assignment_operator,
9588 rhs,
9589 complain_flags (decltype_p));
9590 /* TODO: build_x_modify_expr doesn't honor the location,
9591 so we must set it here. */
9592 expr.set_location (loc);
9593 }
9594 }
9595 }
9596
9597 return expr;
9598 }
9599
9600 /* Parse an (optional) assignment-operator.
9601
9602 assignment-operator: one of
9603 = *= /= %= += -= >>= <<= &= ^= |=
9604
9605 GNU Extension:
9606
9607 assignment-operator: one of
9608 <?= >?=
9609
9610 If the next token is an assignment operator, the corresponding tree
9611 code is returned, and the token is consumed. For example, for
9612 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9613 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9614 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9615 operator, ERROR_MARK is returned. */
9616
9617 static enum tree_code
9618 cp_parser_assignment_operator_opt (cp_parser* parser)
9619 {
9620 enum tree_code op;
9621 cp_token *token;
9622
9623 /* Peek at the next token. */
9624 token = cp_lexer_peek_token (parser->lexer);
9625
9626 switch (token->type)
9627 {
9628 case CPP_EQ:
9629 op = NOP_EXPR;
9630 break;
9631
9632 case CPP_MULT_EQ:
9633 op = MULT_EXPR;
9634 break;
9635
9636 case CPP_DIV_EQ:
9637 op = TRUNC_DIV_EXPR;
9638 break;
9639
9640 case CPP_MOD_EQ:
9641 op = TRUNC_MOD_EXPR;
9642 break;
9643
9644 case CPP_PLUS_EQ:
9645 op = PLUS_EXPR;
9646 break;
9647
9648 case CPP_MINUS_EQ:
9649 op = MINUS_EXPR;
9650 break;
9651
9652 case CPP_RSHIFT_EQ:
9653 op = RSHIFT_EXPR;
9654 break;
9655
9656 case CPP_LSHIFT_EQ:
9657 op = LSHIFT_EXPR;
9658 break;
9659
9660 case CPP_AND_EQ:
9661 op = BIT_AND_EXPR;
9662 break;
9663
9664 case CPP_XOR_EQ:
9665 op = BIT_XOR_EXPR;
9666 break;
9667
9668 case CPP_OR_EQ:
9669 op = BIT_IOR_EXPR;
9670 break;
9671
9672 default:
9673 /* Nothing else is an assignment operator. */
9674 op = ERROR_MARK;
9675 }
9676
9677 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9678 if (op != ERROR_MARK
9679 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9680 op = ERROR_MARK;
9681
9682 /* If it was an assignment operator, consume it. */
9683 if (op != ERROR_MARK)
9684 cp_lexer_consume_token (parser->lexer);
9685
9686 return op;
9687 }
9688
9689 /* Parse an expression.
9690
9691 expression:
9692 assignment-expression
9693 expression , assignment-expression
9694
9695 CAST_P is true if this expression is the target of a cast.
9696 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9697 except possibly parenthesized or on the RHS of a comma (N3276).
9698
9699 Returns a representation of the expression. */
9700
9701 static cp_expr
9702 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9703 bool cast_p, bool decltype_p)
9704 {
9705 cp_expr expression = NULL_TREE;
9706 location_t loc = UNKNOWN_LOCATION;
9707
9708 while (true)
9709 {
9710 cp_expr assignment_expression;
9711
9712 /* Parse the next assignment-expression. */
9713 assignment_expression
9714 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9715
9716 /* We don't create a temporary for a call that is the immediate operand
9717 of decltype or on the RHS of a comma. But when we see a comma, we
9718 need to create a temporary for a call on the LHS. */
9719 if (decltype_p && !processing_template_decl
9720 && TREE_CODE (assignment_expression) == CALL_EXPR
9721 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9722 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9723 assignment_expression
9724 = build_cplus_new (TREE_TYPE (assignment_expression),
9725 assignment_expression, tf_warning_or_error);
9726
9727 /* If this is the first assignment-expression, we can just
9728 save it away. */
9729 if (!expression)
9730 expression = assignment_expression;
9731 else
9732 {
9733 /* Create a location with caret at the comma, ranging
9734 from the start of the LHS to the end of the RHS. */
9735 loc = make_location (loc,
9736 expression.get_start (),
9737 assignment_expression.get_finish ());
9738 expression = build_x_compound_expr (loc, expression,
9739 assignment_expression,
9740 complain_flags (decltype_p));
9741 expression.set_location (loc);
9742 }
9743 /* If the next token is not a comma, or we're in a fold-expression, then
9744 we are done with the expression. */
9745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9746 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9747 break;
9748 /* Consume the `,'. */
9749 loc = cp_lexer_peek_token (parser->lexer)->location;
9750 cp_lexer_consume_token (parser->lexer);
9751 /* A comma operator cannot appear in a constant-expression. */
9752 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9753 expression = error_mark_node;
9754 }
9755
9756 return expression;
9757 }
9758
9759 /* Parse a constant-expression.
9760
9761 constant-expression:
9762 conditional-expression
9763
9764 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9765 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9766 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9767 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9768 only parse a conditional-expression, otherwise parse an
9769 assignment-expression. See below for rationale. */
9770
9771 static cp_expr
9772 cp_parser_constant_expression (cp_parser* parser,
9773 bool allow_non_constant_p,
9774 bool *non_constant_p,
9775 bool strict_p)
9776 {
9777 bool saved_integral_constant_expression_p;
9778 bool saved_allow_non_integral_constant_expression_p;
9779 bool saved_non_integral_constant_expression_p;
9780 cp_expr expression;
9781
9782 /* It might seem that we could simply parse the
9783 conditional-expression, and then check to see if it were
9784 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9785 one that the compiler can figure out is constant, possibly after
9786 doing some simplifications or optimizations. The standard has a
9787 precise definition of constant-expression, and we must honor
9788 that, even though it is somewhat more restrictive.
9789
9790 For example:
9791
9792 int i[(2, 3)];
9793
9794 is not a legal declaration, because `(2, 3)' is not a
9795 constant-expression. The `,' operator is forbidden in a
9796 constant-expression. However, GCC's constant-folding machinery
9797 will fold this operation to an INTEGER_CST for `3'. */
9798
9799 /* Save the old settings. */
9800 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9801 saved_allow_non_integral_constant_expression_p
9802 = parser->allow_non_integral_constant_expression_p;
9803 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9804 /* We are now parsing a constant-expression. */
9805 parser->integral_constant_expression_p = true;
9806 parser->allow_non_integral_constant_expression_p
9807 = (allow_non_constant_p || cxx_dialect >= cxx11);
9808 parser->non_integral_constant_expression_p = false;
9809 /* Although the grammar says "conditional-expression", when not STRICT_P,
9810 we parse an "assignment-expression", which also permits
9811 "throw-expression" and the use of assignment operators. In the case
9812 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9813 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9814 actually essential that we look for an assignment-expression.
9815 For example, cp_parser_initializer_clauses uses this function to
9816 determine whether a particular assignment-expression is in fact
9817 constant. */
9818 if (strict_p)
9819 {
9820 /* Parse the binary expressions (logical-or-expression). */
9821 expression = cp_parser_binary_expression (parser, false, false, false,
9822 PREC_NOT_OPERATOR, NULL);
9823 /* If the next token is a `?' then we're actually looking at
9824 a conditional-expression; otherwise we're done. */
9825 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9826 expression = cp_parser_question_colon_clause (parser, expression);
9827 }
9828 else
9829 expression = cp_parser_assignment_expression (parser);
9830 /* Restore the old settings. */
9831 parser->integral_constant_expression_p
9832 = saved_integral_constant_expression_p;
9833 parser->allow_non_integral_constant_expression_p
9834 = saved_allow_non_integral_constant_expression_p;
9835 if (cxx_dialect >= cxx11)
9836 {
9837 /* Require an rvalue constant expression here; that's what our
9838 callers expect. Reference constant expressions are handled
9839 separately in e.g. cp_parser_template_argument. */
9840 tree decay = expression;
9841 if (TREE_TYPE (expression)
9842 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9843 decay = build_address (expression);
9844 bool is_const = potential_rvalue_constant_expression (decay);
9845 parser->non_integral_constant_expression_p = !is_const;
9846 if (!is_const && !allow_non_constant_p)
9847 require_potential_rvalue_constant_expression (decay);
9848 }
9849 if (allow_non_constant_p)
9850 *non_constant_p = parser->non_integral_constant_expression_p;
9851 parser->non_integral_constant_expression_p
9852 = saved_non_integral_constant_expression_p;
9853
9854 return expression;
9855 }
9856
9857 /* Parse __builtin_offsetof.
9858
9859 offsetof-expression:
9860 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9861
9862 offsetof-member-designator:
9863 id-expression
9864 | offsetof-member-designator "." id-expression
9865 | offsetof-member-designator "[" expression "]"
9866 | offsetof-member-designator "->" id-expression */
9867
9868 static cp_expr
9869 cp_parser_builtin_offsetof (cp_parser *parser)
9870 {
9871 int save_ice_p, save_non_ice_p;
9872 tree type;
9873 cp_expr expr;
9874 cp_id_kind dummy;
9875 cp_token *token;
9876 location_t finish_loc;
9877
9878 /* We're about to accept non-integral-constant things, but will
9879 definitely yield an integral constant expression. Save and
9880 restore these values around our local parsing. */
9881 save_ice_p = parser->integral_constant_expression_p;
9882 save_non_ice_p = parser->non_integral_constant_expression_p;
9883
9884 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9885
9886 /* Consume the "__builtin_offsetof" token. */
9887 cp_lexer_consume_token (parser->lexer);
9888 /* Consume the opening `('. */
9889 matching_parens parens;
9890 parens.require_open (parser);
9891 /* Parse the type-id. */
9892 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9893 {
9894 const char *saved_message = parser->type_definition_forbidden_message;
9895 parser->type_definition_forbidden_message
9896 = G_("types may not be defined within __builtin_offsetof");
9897 type = cp_parser_type_id (parser);
9898 parser->type_definition_forbidden_message = saved_message;
9899 }
9900 /* Look for the `,'. */
9901 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9902 token = cp_lexer_peek_token (parser->lexer);
9903
9904 /* Build the (type *)null that begins the traditional offsetof macro. */
9905 tree object_ptr
9906 = build_static_cast (build_pointer_type (type), null_pointer_node,
9907 tf_warning_or_error);
9908
9909 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9910 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9911 true, &dummy, token->location);
9912 while (true)
9913 {
9914 token = cp_lexer_peek_token (parser->lexer);
9915 switch (token->type)
9916 {
9917 case CPP_OPEN_SQUARE:
9918 /* offsetof-member-designator "[" expression "]" */
9919 expr = cp_parser_postfix_open_square_expression (parser, expr,
9920 true, false);
9921 break;
9922
9923 case CPP_DEREF:
9924 /* offsetof-member-designator "->" identifier */
9925 expr = grok_array_decl (token->location, expr,
9926 integer_zero_node, false);
9927 /* FALLTHRU */
9928
9929 case CPP_DOT:
9930 /* offsetof-member-designator "." identifier */
9931 cp_lexer_consume_token (parser->lexer);
9932 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9933 expr, true, &dummy,
9934 token->location);
9935 break;
9936
9937 case CPP_CLOSE_PAREN:
9938 /* Consume the ")" token. */
9939 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9940 cp_lexer_consume_token (parser->lexer);
9941 goto success;
9942
9943 default:
9944 /* Error. We know the following require will fail, but
9945 that gives the proper error message. */
9946 parens.require_close (parser);
9947 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9948 expr = error_mark_node;
9949 goto failure;
9950 }
9951 }
9952
9953 success:
9954 /* Make a location of the form:
9955 __builtin_offsetof (struct s, f)
9956 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9957 with caret at the type-id, ranging from the start of the
9958 "_builtin_offsetof" token to the close paren. */
9959 loc = make_location (loc, start_loc, finish_loc);
9960 /* The result will be an INTEGER_CST, so we need to explicitly
9961 preserve the location. */
9962 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9963
9964 failure:
9965 parser->integral_constant_expression_p = save_ice_p;
9966 parser->non_integral_constant_expression_p = save_non_ice_p;
9967
9968 expr = expr.maybe_add_location_wrapper ();
9969 return expr;
9970 }
9971
9972 /* Parse a trait expression.
9973
9974 Returns a representation of the expression, the underlying type
9975 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9976
9977 static cp_expr
9978 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9979 {
9980 cp_trait_kind kind;
9981 tree type1, type2 = NULL_TREE;
9982 bool binary = false;
9983 bool variadic = false;
9984
9985 switch (keyword)
9986 {
9987 case RID_HAS_NOTHROW_ASSIGN:
9988 kind = CPTK_HAS_NOTHROW_ASSIGN;
9989 break;
9990 case RID_HAS_NOTHROW_CONSTRUCTOR:
9991 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9992 break;
9993 case RID_HAS_NOTHROW_COPY:
9994 kind = CPTK_HAS_NOTHROW_COPY;
9995 break;
9996 case RID_HAS_TRIVIAL_ASSIGN:
9997 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9998 break;
9999 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10000 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10001 break;
10002 case RID_HAS_TRIVIAL_COPY:
10003 kind = CPTK_HAS_TRIVIAL_COPY;
10004 break;
10005 case RID_HAS_TRIVIAL_DESTRUCTOR:
10006 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10007 break;
10008 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10009 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10010 break;
10011 case RID_HAS_VIRTUAL_DESTRUCTOR:
10012 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10013 break;
10014 case RID_IS_ABSTRACT:
10015 kind = CPTK_IS_ABSTRACT;
10016 break;
10017 case RID_IS_AGGREGATE:
10018 kind = CPTK_IS_AGGREGATE;
10019 break;
10020 case RID_IS_BASE_OF:
10021 kind = CPTK_IS_BASE_OF;
10022 binary = true;
10023 break;
10024 case RID_IS_CLASS:
10025 kind = CPTK_IS_CLASS;
10026 break;
10027 case RID_IS_EMPTY:
10028 kind = CPTK_IS_EMPTY;
10029 break;
10030 case RID_IS_ENUM:
10031 kind = CPTK_IS_ENUM;
10032 break;
10033 case RID_IS_FINAL:
10034 kind = CPTK_IS_FINAL;
10035 break;
10036 case RID_IS_LITERAL_TYPE:
10037 kind = CPTK_IS_LITERAL_TYPE;
10038 break;
10039 case RID_IS_POD:
10040 kind = CPTK_IS_POD;
10041 break;
10042 case RID_IS_POLYMORPHIC:
10043 kind = CPTK_IS_POLYMORPHIC;
10044 break;
10045 case RID_IS_SAME_AS:
10046 kind = CPTK_IS_SAME_AS;
10047 binary = true;
10048 break;
10049 case RID_IS_STD_LAYOUT:
10050 kind = CPTK_IS_STD_LAYOUT;
10051 break;
10052 case RID_IS_TRIVIAL:
10053 kind = CPTK_IS_TRIVIAL;
10054 break;
10055 case RID_IS_TRIVIALLY_ASSIGNABLE:
10056 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10057 binary = true;
10058 break;
10059 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10060 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10061 variadic = true;
10062 break;
10063 case RID_IS_TRIVIALLY_COPYABLE:
10064 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10065 break;
10066 case RID_IS_UNION:
10067 kind = CPTK_IS_UNION;
10068 break;
10069 case RID_UNDERLYING_TYPE:
10070 kind = CPTK_UNDERLYING_TYPE;
10071 break;
10072 case RID_BASES:
10073 kind = CPTK_BASES;
10074 break;
10075 case RID_DIRECT_BASES:
10076 kind = CPTK_DIRECT_BASES;
10077 break;
10078 case RID_IS_ASSIGNABLE:
10079 kind = CPTK_IS_ASSIGNABLE;
10080 binary = true;
10081 break;
10082 case RID_IS_CONSTRUCTIBLE:
10083 kind = CPTK_IS_CONSTRUCTIBLE;
10084 variadic = true;
10085 break;
10086 default:
10087 gcc_unreachable ();
10088 }
10089
10090 /* Get location of initial token. */
10091 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10092
10093 /* Consume the token. */
10094 cp_lexer_consume_token (parser->lexer);
10095
10096 matching_parens parens;
10097 parens.require_open (parser);
10098
10099 {
10100 type_id_in_expr_sentinel s (parser);
10101 type1 = cp_parser_type_id (parser);
10102 }
10103
10104 if (type1 == error_mark_node)
10105 return error_mark_node;
10106
10107 if (binary)
10108 {
10109 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10110
10111 {
10112 type_id_in_expr_sentinel s (parser);
10113 type2 = cp_parser_type_id (parser);
10114 }
10115
10116 if (type2 == error_mark_node)
10117 return error_mark_node;
10118 }
10119 else if (variadic)
10120 {
10121 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10122 {
10123 cp_lexer_consume_token (parser->lexer);
10124 tree elt = cp_parser_type_id (parser);
10125 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10126 {
10127 cp_lexer_consume_token (parser->lexer);
10128 elt = make_pack_expansion (elt);
10129 }
10130 if (elt == error_mark_node)
10131 return error_mark_node;
10132 type2 = tree_cons (NULL_TREE, elt, type2);
10133 }
10134 }
10135
10136 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10137 parens.require_close (parser);
10138
10139 /* Construct a location of the form:
10140 __is_trivially_copyable(_Tp)
10141 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10142 with start == caret, finishing at the close-paren. */
10143 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10144
10145 /* Complete the trait expression, which may mean either processing
10146 the trait expr now or saving it for template instantiation. */
10147 switch (kind)
10148 {
10149 case CPTK_UNDERLYING_TYPE:
10150 return cp_expr (finish_underlying_type (type1), trait_loc);
10151 case CPTK_BASES:
10152 return cp_expr (finish_bases (type1, false), trait_loc);
10153 case CPTK_DIRECT_BASES:
10154 return cp_expr (finish_bases (type1, true), trait_loc);
10155 default:
10156 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10157 }
10158 }
10159
10160 /* Parse a lambda expression.
10161
10162 lambda-expression:
10163 lambda-introducer lambda-declarator [opt] compound-statement
10164
10165 Returns a representation of the expression. */
10166
10167 static cp_expr
10168 cp_parser_lambda_expression (cp_parser* parser)
10169 {
10170 tree lambda_expr = build_lambda_expr ();
10171 tree type;
10172 bool ok = true;
10173 cp_token *token = cp_lexer_peek_token (parser->lexer);
10174 cp_token_position start = 0;
10175
10176 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10177
10178 if (cxx_dialect >= cxx2a)
10179 /* C++20 allows lambdas in unevaluated context. */;
10180 else if (cp_unevaluated_operand)
10181 {
10182 if (!token->error_reported)
10183 {
10184 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10185 "lambda-expression in unevaluated context"
10186 " only available with -std=c++2a or -std=gnu++2a");
10187 token->error_reported = true;
10188 }
10189 ok = false;
10190 }
10191 else if (parser->in_template_argument_list_p)
10192 {
10193 if (!token->error_reported)
10194 {
10195 error_at (token->location, "lambda-expression in template-argument"
10196 " only available with -std=c++2a or -std=gnu++2a");
10197 token->error_reported = true;
10198 }
10199 ok = false;
10200 }
10201
10202 /* We may be in the middle of deferred access check. Disable
10203 it now. */
10204 push_deferring_access_checks (dk_no_deferred);
10205
10206 cp_parser_lambda_introducer (parser, lambda_expr);
10207 if (cp_parser_error_occurred (parser))
10208 return error_mark_node;
10209
10210 type = begin_lambda_type (lambda_expr);
10211 if (type == error_mark_node)
10212 return error_mark_node;
10213
10214 record_lambda_scope (lambda_expr);
10215
10216 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10217 determine_visibility (TYPE_NAME (type));
10218
10219 /* Now that we've started the type, add the capture fields for any
10220 explicit captures. */
10221 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10222
10223 {
10224 /* Inside the class, surrounding template-parameter-lists do not apply. */
10225 unsigned int saved_num_template_parameter_lists
10226 = parser->num_template_parameter_lists;
10227 unsigned char in_statement = parser->in_statement;
10228 bool in_switch_statement_p = parser->in_switch_statement_p;
10229 bool fully_implicit_function_template_p
10230 = parser->fully_implicit_function_template_p;
10231 tree implicit_template_parms = parser->implicit_template_parms;
10232 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10233 bool auto_is_implicit_function_template_parm_p
10234 = parser->auto_is_implicit_function_template_parm_p;
10235
10236 parser->num_template_parameter_lists = 0;
10237 parser->in_statement = 0;
10238 parser->in_switch_statement_p = false;
10239 parser->fully_implicit_function_template_p = false;
10240 parser->implicit_template_parms = 0;
10241 parser->implicit_template_scope = 0;
10242 parser->auto_is_implicit_function_template_parm_p = false;
10243
10244 /* By virtue of defining a local class, a lambda expression has access to
10245 the private variables of enclosing classes. */
10246
10247 if (cp_parser_start_tentative_firewall (parser))
10248 start = token;
10249
10250 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10251
10252 if (ok && cp_parser_error_occurred (parser))
10253 ok = false;
10254
10255 if (ok)
10256 {
10257 cp_parser_lambda_body (parser, lambda_expr);
10258 }
10259 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10260 {
10261 if (cp_parser_skip_to_closing_brace (parser))
10262 cp_lexer_consume_token (parser->lexer);
10263 }
10264
10265 /* The capture list was built up in reverse order; fix that now. */
10266 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10267 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10268
10269 if (ok)
10270 maybe_add_lambda_conv_op (type);
10271
10272 type = finish_struct (type, /*attributes=*/NULL_TREE);
10273
10274 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10275 parser->in_statement = in_statement;
10276 parser->in_switch_statement_p = in_switch_statement_p;
10277 parser->fully_implicit_function_template_p
10278 = fully_implicit_function_template_p;
10279 parser->implicit_template_parms = implicit_template_parms;
10280 parser->implicit_template_scope = implicit_template_scope;
10281 parser->auto_is_implicit_function_template_parm_p
10282 = auto_is_implicit_function_template_parm_p;
10283 }
10284
10285 /* This field is only used during parsing of the lambda. */
10286 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10287
10288 /* This lambda shouldn't have any proxies left at this point. */
10289 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10290 /* And now that we're done, push proxies for an enclosing lambda. */
10291 insert_pending_capture_proxies ();
10292
10293 /* Update the lambda expression to a range. */
10294 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10295 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10296 token->location,
10297 end_tok->location);
10298
10299 if (ok)
10300 lambda_expr = build_lambda_object (lambda_expr);
10301 else
10302 lambda_expr = error_mark_node;
10303
10304 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10305
10306 pop_deferring_access_checks ();
10307
10308 return lambda_expr;
10309 }
10310
10311 /* Parse the beginning of a lambda expression.
10312
10313 lambda-introducer:
10314 [ lambda-capture [opt] ]
10315
10316 LAMBDA_EXPR is the current representation of the lambda expression. */
10317
10318 static void
10319 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10320 {
10321 /* Need commas after the first capture. */
10322 bool first = true;
10323
10324 /* Eat the leading `['. */
10325 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10326
10327 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10328 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10329 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10330 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10331 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10332 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10333
10334 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10335 {
10336 cp_lexer_consume_token (parser->lexer);
10337 first = false;
10338
10339 if (!(at_function_scope_p () || parsing_nsdmi ()))
10340 error ("non-local lambda expression cannot have a capture-default");
10341 }
10342
10343 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10344 {
10345 cp_token* capture_token;
10346 tree capture_id;
10347 tree capture_init_expr;
10348 cp_id_kind idk = CP_ID_KIND_NONE;
10349 bool explicit_init_p = false;
10350
10351 enum capture_kind_type
10352 {
10353 BY_COPY,
10354 BY_REFERENCE
10355 };
10356 enum capture_kind_type capture_kind = BY_COPY;
10357
10358 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10359 {
10360 error ("expected end of capture-list");
10361 return;
10362 }
10363
10364 if (first)
10365 first = false;
10366 else
10367 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10368
10369 /* Possibly capture `this'. */
10370 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10371 {
10372 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10373 if (cxx_dialect < cxx2a
10374 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10375 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10376 "with by-copy capture default");
10377 cp_lexer_consume_token (parser->lexer);
10378 add_capture (lambda_expr,
10379 /*id=*/this_identifier,
10380 /*initializer=*/finish_this_expr (),
10381 /*by_reference_p=*/true,
10382 explicit_init_p);
10383 continue;
10384 }
10385
10386 /* Possibly capture `*this'. */
10387 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10388 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10389 {
10390 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10391 if (cxx_dialect < cxx17)
10392 pedwarn (loc, 0, "%<*this%> capture only available with "
10393 "-std=c++17 or -std=gnu++17");
10394 cp_lexer_consume_token (parser->lexer);
10395 cp_lexer_consume_token (parser->lexer);
10396 add_capture (lambda_expr,
10397 /*id=*/this_identifier,
10398 /*initializer=*/finish_this_expr (),
10399 /*by_reference_p=*/false,
10400 explicit_init_p);
10401 continue;
10402 }
10403
10404 bool init_pack_expansion = false;
10405 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10406 {
10407 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10408 if (cxx_dialect < cxx2a)
10409 pedwarn (loc, 0, "pack init-capture only available with "
10410 "-std=c++2a or -std=gnu++2a");
10411 cp_lexer_consume_token (parser->lexer);
10412 init_pack_expansion = true;
10413 }
10414
10415 /* Remember whether we want to capture as a reference or not. */
10416 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10417 {
10418 capture_kind = BY_REFERENCE;
10419 cp_lexer_consume_token (parser->lexer);
10420 }
10421
10422 /* Get the identifier. */
10423 capture_token = cp_lexer_peek_token (parser->lexer);
10424 capture_id = cp_parser_identifier (parser);
10425
10426 if (capture_id == error_mark_node)
10427 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10428 delimiters, but I modified this to stop on unnested ']' as well. It
10429 was already changed to stop on unnested '}', so the
10430 "closing_parenthesis" name is no more misleading with my change. */
10431 {
10432 cp_parser_skip_to_closing_parenthesis (parser,
10433 /*recovering=*/true,
10434 /*or_comma=*/true,
10435 /*consume_paren=*/true);
10436 break;
10437 }
10438
10439 /* Find the initializer for this capture. */
10440 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10441 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10442 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10443 {
10444 bool direct, non_constant;
10445 /* An explicit initializer exists. */
10446 if (cxx_dialect < cxx14)
10447 pedwarn (input_location, 0,
10448 "lambda capture initializers "
10449 "only available with -std=c++14 or -std=gnu++14");
10450 capture_init_expr = cp_parser_initializer (parser, &direct,
10451 &non_constant, true);
10452 explicit_init_p = true;
10453 if (capture_init_expr == NULL_TREE)
10454 {
10455 error ("empty initializer for lambda init-capture");
10456 capture_init_expr = error_mark_node;
10457 }
10458 if (init_pack_expansion)
10459 capture_init_expr = make_pack_expansion (capture_init_expr);
10460 }
10461 else
10462 {
10463 const char* error_msg;
10464
10465 /* Turn the identifier into an id-expression. */
10466 capture_init_expr
10467 = cp_parser_lookup_name_simple (parser, capture_id,
10468 capture_token->location);
10469
10470 if (capture_init_expr == error_mark_node)
10471 {
10472 unqualified_name_lookup_error (capture_id);
10473 continue;
10474 }
10475 else if (!VAR_P (capture_init_expr)
10476 && TREE_CODE (capture_init_expr) != PARM_DECL)
10477 {
10478 error_at (capture_token->location,
10479 "capture of non-variable %qE",
10480 capture_init_expr);
10481 if (DECL_P (capture_init_expr))
10482 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10483 "%q#D declared here", capture_init_expr);
10484 continue;
10485 }
10486 if (VAR_P (capture_init_expr)
10487 && decl_storage_duration (capture_init_expr) != dk_auto)
10488 {
10489 if (pedwarn (capture_token->location, 0, "capture of variable "
10490 "%qD with non-automatic storage duration",
10491 capture_init_expr))
10492 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10493 "%q#D declared here", capture_init_expr);
10494 continue;
10495 }
10496
10497 capture_init_expr
10498 = finish_id_expression
10499 (capture_id,
10500 capture_init_expr,
10501 parser->scope,
10502 &idk,
10503 /*integral_constant_expression_p=*/false,
10504 /*allow_non_integral_constant_expression_p=*/false,
10505 /*non_integral_constant_expression_p=*/NULL,
10506 /*template_p=*/false,
10507 /*done=*/true,
10508 /*address_p=*/false,
10509 /*template_arg_p=*/false,
10510 &error_msg,
10511 capture_token->location);
10512
10513 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10514 {
10515 cp_lexer_consume_token (parser->lexer);
10516 capture_init_expr = make_pack_expansion (capture_init_expr);
10517 }
10518 }
10519
10520 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10521 && !explicit_init_p)
10522 {
10523 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10524 && capture_kind == BY_COPY)
10525 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10526 "of %qD redundant with by-copy capture default",
10527 capture_id);
10528 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10529 && capture_kind == BY_REFERENCE)
10530 pedwarn (capture_token->location, 0, "explicit by-reference "
10531 "capture of %qD redundant with by-reference capture "
10532 "default", capture_id);
10533 }
10534
10535 add_capture (lambda_expr,
10536 capture_id,
10537 capture_init_expr,
10538 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10539 explicit_init_p);
10540
10541 /* If there is any qualification still in effect, clear it
10542 now; we will be starting fresh with the next capture. */
10543 parser->scope = NULL_TREE;
10544 parser->qualifying_scope = NULL_TREE;
10545 parser->object_scope = NULL_TREE;
10546 }
10547
10548 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10549 }
10550
10551 /* Parse the (optional) middle of a lambda expression.
10552
10553 lambda-declarator:
10554 < template-parameter-list [opt] >
10555 ( parameter-declaration-clause [opt] )
10556 attribute-specifier [opt]
10557 decl-specifier-seq [opt]
10558 exception-specification [opt]
10559 lambda-return-type-clause [opt]
10560
10561 LAMBDA_EXPR is the current representation of the lambda expression. */
10562
10563 static bool
10564 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10565 {
10566 /* 5.1.1.4 of the standard says:
10567 If a lambda-expression does not include a lambda-declarator, it is as if
10568 the lambda-declarator were ().
10569 This means an empty parameter list, no attributes, and no exception
10570 specification. */
10571 tree param_list = void_list_node;
10572 tree attributes = NULL_TREE;
10573 tree exception_spec = NULL_TREE;
10574 tree template_param_list = NULL_TREE;
10575 tree tx_qual = NULL_TREE;
10576 tree return_type = NULL_TREE;
10577 cp_decl_specifier_seq lambda_specs;
10578 clear_decl_specs (&lambda_specs);
10579
10580 /* The template-parameter-list is optional, but must begin with
10581 an opening angle if present. */
10582 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10583 {
10584 if (cxx_dialect < cxx14)
10585 pedwarn (parser->lexer->next_token->location, 0,
10586 "lambda templates are only available with "
10587 "-std=c++14 or -std=gnu++14");
10588 else if (cxx_dialect < cxx2a)
10589 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10590 "lambda templates are only available with "
10591 "-std=c++2a or -std=gnu++2a");
10592
10593 cp_lexer_consume_token (parser->lexer);
10594
10595 template_param_list = cp_parser_template_parameter_list (parser);
10596
10597 cp_parser_skip_to_end_of_template_parameter_list (parser);
10598
10599 /* We just processed one more parameter list. */
10600 ++parser->num_template_parameter_lists;
10601 }
10602
10603 /* The parameter-declaration-clause is optional (unless
10604 template-parameter-list was given), but must begin with an
10605 opening parenthesis if present. */
10606 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10607 {
10608 matching_parens parens;
10609 parens.consume_open (parser);
10610
10611 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10612
10613 /* Parse parameters. */
10614 param_list = cp_parser_parameter_declaration_clause (parser);
10615
10616 /* Default arguments shall not be specified in the
10617 parameter-declaration-clause of a lambda-declarator. */
10618 if (cxx_dialect < cxx14)
10619 for (tree t = param_list; t; t = TREE_CHAIN (t))
10620 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10621 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10622 "default argument specified for lambda parameter");
10623
10624 parens.require_close (parser);
10625
10626 /* In the decl-specifier-seq of the lambda-declarator, each
10627 decl-specifier shall either be mutable or constexpr. */
10628 int declares_class_or_enum;
10629 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10630 cp_parser_decl_specifier_seq (parser,
10631 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10632 &lambda_specs, &declares_class_or_enum);
10633 if (lambda_specs.storage_class == sc_mutable)
10634 {
10635 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10636 if (lambda_specs.conflicting_specifiers_p)
10637 error_at (lambda_specs.locations[ds_storage_class],
10638 "duplicate %<mutable%>");
10639 }
10640
10641 tx_qual = cp_parser_tx_qualifier_opt (parser);
10642
10643 /* Parse optional exception specification. */
10644 exception_spec = cp_parser_exception_specification_opt (parser);
10645
10646 attributes = cp_parser_std_attribute_spec_seq (parser);
10647
10648 /* Parse optional trailing return type. */
10649 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10650 {
10651 cp_lexer_consume_token (parser->lexer);
10652 return_type = cp_parser_trailing_type_id (parser);
10653 }
10654
10655 /* The function parameters must be in scope all the way until after the
10656 trailing-return-type in case of decltype. */
10657 pop_bindings_and_leave_scope ();
10658 }
10659 else if (template_param_list != NULL_TREE) // generate diagnostic
10660 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10661
10662 /* Create the function call operator.
10663
10664 Messing with declarators like this is no uglier than building up the
10665 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10666 other code. */
10667 {
10668 cp_decl_specifier_seq return_type_specs;
10669 cp_declarator* declarator;
10670 tree fco;
10671 int quals;
10672 void *p;
10673
10674 clear_decl_specs (&return_type_specs);
10675 return_type_specs.type = make_auto ();
10676
10677 if (lambda_specs.locations[ds_constexpr])
10678 {
10679 if (cxx_dialect >= cxx17)
10680 return_type_specs.locations[ds_constexpr]
10681 = lambda_specs.locations[ds_constexpr];
10682 else
10683 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10684 "lambda only available with -std=c++17 or -std=gnu++17");
10685 }
10686
10687 p = obstack_alloc (&declarator_obstack, 0);
10688
10689 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none);
10690
10691 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10692 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10693 declarator = make_call_declarator (declarator, param_list, quals,
10694 VIRT_SPEC_UNSPECIFIED,
10695 REF_QUAL_NONE,
10696 tx_qual,
10697 exception_spec,
10698 return_type,
10699 /*requires_clause*/NULL_TREE);
10700 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10701 declarator->std_attributes = attributes;
10702
10703 fco = grokmethod (&return_type_specs,
10704 declarator,
10705 NULL_TREE);
10706 if (fco != error_mark_node)
10707 {
10708 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10709 DECL_ARTIFICIAL (fco) = 1;
10710 /* Give the object parameter a different name. */
10711 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10712 DECL_LAMBDA_FUNCTION (fco) = 1;
10713 }
10714 if (template_param_list)
10715 {
10716 fco = finish_member_template_decl (fco);
10717 finish_template_decl (template_param_list);
10718 --parser->num_template_parameter_lists;
10719 }
10720 else if (parser->fully_implicit_function_template_p)
10721 fco = finish_fully_implicit_template (parser, fco);
10722
10723 finish_member_declaration (fco);
10724
10725 obstack_free (&declarator_obstack, p);
10726
10727 return (fco != error_mark_node);
10728 }
10729 }
10730
10731 /* Parse the body of a lambda expression, which is simply
10732
10733 compound-statement
10734
10735 but which requires special handling.
10736 LAMBDA_EXPR is the current representation of the lambda expression. */
10737
10738 static void
10739 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10740 {
10741 bool nested = (current_function_decl != NULL_TREE);
10742 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10743 bool in_function_body = parser->in_function_body;
10744
10745 /* The body of a lambda-expression is not a subexpression of the enclosing
10746 expression. */
10747 cp_evaluated ev;
10748
10749 if (nested)
10750 push_function_context ();
10751 else
10752 /* Still increment function_depth so that we don't GC in the
10753 middle of an expression. */
10754 ++function_depth;
10755
10756 vec<tree> omp_privatization_save;
10757 save_omp_privatization_clauses (omp_privatization_save);
10758 /* Clear this in case we're in the middle of a default argument. */
10759 parser->local_variables_forbidden_p = false;
10760 parser->in_function_body = true;
10761
10762 {
10763 local_specialization_stack s (lss_copy);
10764 tree fco = lambda_function (lambda_expr);
10765 tree body = start_lambda_function (fco, lambda_expr);
10766 matching_braces braces;
10767
10768 if (braces.require_open (parser))
10769 {
10770 tree compound_stmt = begin_compound_stmt (0);
10771
10772 /* Originally C++11 required us to peek for 'return expr'; and
10773 process it specially here to deduce the return type. N3638
10774 removed the need for that. */
10775
10776 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10777 cp_parser_label_declaration (parser);
10778 cp_parser_statement_seq_opt (parser, NULL_TREE);
10779 braces.require_close (parser);
10780
10781 finish_compound_stmt (compound_stmt);
10782 }
10783
10784 finish_lambda_function (body);
10785 }
10786
10787 restore_omp_privatization_clauses (omp_privatization_save);
10788 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10789 parser->in_function_body = in_function_body;
10790 if (nested)
10791 pop_function_context();
10792 else
10793 --function_depth;
10794 }
10795
10796 /* Statements [gram.stmt.stmt] */
10797
10798 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10799
10800 static void
10801 add_debug_begin_stmt (location_t loc)
10802 {
10803 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10804 return;
10805 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10806 /* A concept is never expanded normally. */
10807 return;
10808
10809 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10810 SET_EXPR_LOCATION (stmt, loc);
10811 add_stmt (stmt);
10812 }
10813
10814 /* Parse a statement.
10815
10816 statement:
10817 labeled-statement
10818 expression-statement
10819 compound-statement
10820 selection-statement
10821 iteration-statement
10822 jump-statement
10823 declaration-statement
10824 try-block
10825
10826 C++11:
10827
10828 statement:
10829 labeled-statement
10830 attribute-specifier-seq (opt) expression-statement
10831 attribute-specifier-seq (opt) compound-statement
10832 attribute-specifier-seq (opt) selection-statement
10833 attribute-specifier-seq (opt) iteration-statement
10834 attribute-specifier-seq (opt) jump-statement
10835 declaration-statement
10836 attribute-specifier-seq (opt) try-block
10837
10838 init-statement:
10839 expression-statement
10840 simple-declaration
10841
10842 TM Extension:
10843
10844 statement:
10845 atomic-statement
10846
10847 IN_COMPOUND is true when the statement is nested inside a
10848 cp_parser_compound_statement; this matters for certain pragmas.
10849
10850 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10851 is a (possibly labeled) if statement which is not enclosed in braces
10852 and has an else clause. This is used to implement -Wparentheses.
10853
10854 CHAIN is a vector of if-else-if conditions. */
10855
10856 static void
10857 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10858 bool in_compound, bool *if_p, vec<tree> *chain,
10859 location_t *loc_after_labels)
10860 {
10861 tree statement, std_attrs = NULL_TREE;
10862 cp_token *token;
10863 location_t statement_location, attrs_location;
10864
10865 restart:
10866 if (if_p != NULL)
10867 *if_p = false;
10868 /* There is no statement yet. */
10869 statement = NULL_TREE;
10870
10871 saved_token_sentinel saved_tokens (parser->lexer);
10872 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10873 if (c_dialect_objc ())
10874 /* In obj-c++, seeing '[[' might be the either the beginning of
10875 c++11 attributes, or a nested objc-message-expression. So
10876 let's parse the c++11 attributes tentatively. */
10877 cp_parser_parse_tentatively (parser);
10878 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10879 if (c_dialect_objc ())
10880 {
10881 if (!cp_parser_parse_definitely (parser))
10882 std_attrs = NULL_TREE;
10883 }
10884
10885 /* Peek at the next token. */
10886 token = cp_lexer_peek_token (parser->lexer);
10887 /* Remember the location of the first token in the statement. */
10888 statement_location = token->location;
10889 add_debug_begin_stmt (statement_location);
10890 /* If this is a keyword, then that will often determine what kind of
10891 statement we have. */
10892 if (token->type == CPP_KEYWORD)
10893 {
10894 enum rid keyword = token->keyword;
10895
10896 switch (keyword)
10897 {
10898 case RID_CASE:
10899 case RID_DEFAULT:
10900 /* Looks like a labeled-statement with a case label.
10901 Parse the label, and then use tail recursion to parse
10902 the statement. */
10903 cp_parser_label_for_labeled_statement (parser, std_attrs);
10904 in_compound = false;
10905 goto restart;
10906
10907 case RID_IF:
10908 case RID_SWITCH:
10909 statement = cp_parser_selection_statement (parser, if_p, chain);
10910 break;
10911
10912 case RID_WHILE:
10913 case RID_DO:
10914 case RID_FOR:
10915 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10916 break;
10917
10918 case RID_BREAK:
10919 case RID_CONTINUE:
10920 case RID_RETURN:
10921 case RID_GOTO:
10922 statement = cp_parser_jump_statement (parser);
10923 break;
10924
10925 /* Objective-C++ exception-handling constructs. */
10926 case RID_AT_TRY:
10927 case RID_AT_CATCH:
10928 case RID_AT_FINALLY:
10929 case RID_AT_SYNCHRONIZED:
10930 case RID_AT_THROW:
10931 statement = cp_parser_objc_statement (parser);
10932 break;
10933
10934 case RID_TRY:
10935 statement = cp_parser_try_block (parser);
10936 break;
10937
10938 case RID_NAMESPACE:
10939 /* This must be a namespace alias definition. */
10940 cp_parser_declaration_statement (parser);
10941 return;
10942
10943 case RID_TRANSACTION_ATOMIC:
10944 case RID_TRANSACTION_RELAXED:
10945 case RID_SYNCHRONIZED:
10946 case RID_ATOMIC_NOEXCEPT:
10947 case RID_ATOMIC_CANCEL:
10948 statement = cp_parser_transaction (parser, token);
10949 break;
10950 case RID_TRANSACTION_CANCEL:
10951 statement = cp_parser_transaction_cancel (parser);
10952 break;
10953
10954 default:
10955 /* It might be a keyword like `int' that can start a
10956 declaration-statement. */
10957 break;
10958 }
10959 }
10960 else if (token->type == CPP_NAME)
10961 {
10962 /* If the next token is a `:', then we are looking at a
10963 labeled-statement. */
10964 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10965 if (token->type == CPP_COLON)
10966 {
10967 /* Looks like a labeled-statement with an ordinary label.
10968 Parse the label, and then use tail recursion to parse
10969 the statement. */
10970
10971 cp_parser_label_for_labeled_statement (parser, std_attrs);
10972 in_compound = false;
10973 goto restart;
10974 }
10975 }
10976 /* Anything that starts with a `{' must be a compound-statement. */
10977 else if (token->type == CPP_OPEN_BRACE)
10978 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10979 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10980 a statement all its own. */
10981 else if (token->type == CPP_PRAGMA)
10982 {
10983 /* Only certain OpenMP pragmas are attached to statements, and thus
10984 are considered statements themselves. All others are not. In
10985 the context of a compound, accept the pragma as a "statement" and
10986 return so that we can check for a close brace. Otherwise we
10987 require a real statement and must go back and read one. */
10988 if (in_compound)
10989 cp_parser_pragma (parser, pragma_compound, if_p);
10990 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10991 goto restart;
10992 return;
10993 }
10994 else if (token->type == CPP_EOF)
10995 {
10996 cp_parser_error (parser, "expected statement");
10997 return;
10998 }
10999
11000 /* Everything else must be a declaration-statement or an
11001 expression-statement. Try for the declaration-statement
11002 first, unless we are looking at a `;', in which case we know that
11003 we have an expression-statement. */
11004 if (!statement)
11005 {
11006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11007 {
11008 if (std_attrs != NULL_TREE)
11009 {
11010 /* Attributes should be parsed as part of the the
11011 declaration, so let's un-parse them. */
11012 saved_tokens.rollback();
11013 std_attrs = NULL_TREE;
11014 }
11015
11016 cp_parser_parse_tentatively (parser);
11017 /* Try to parse the declaration-statement. */
11018 cp_parser_declaration_statement (parser);
11019 /* If that worked, we're done. */
11020 if (cp_parser_parse_definitely (parser))
11021 return;
11022 }
11023 /* All preceding labels have been parsed at this point. */
11024 if (loc_after_labels != NULL)
11025 *loc_after_labels = statement_location;
11026
11027 /* Look for an expression-statement instead. */
11028 statement = cp_parser_expression_statement (parser, in_statement_expr);
11029
11030 /* Handle [[fallthrough]];. */
11031 if (attribute_fallthrough_p (std_attrs))
11032 {
11033 /* The next token after the fallthrough attribute is ';'. */
11034 if (statement == NULL_TREE)
11035 {
11036 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11037 statement = build_call_expr_internal_loc (statement_location,
11038 IFN_FALLTHROUGH,
11039 void_type_node, 0);
11040 finish_expr_stmt (statement);
11041 }
11042 else
11043 warning_at (statement_location, OPT_Wattributes,
11044 "%<fallthrough%> attribute not followed by %<;%>");
11045 std_attrs = NULL_TREE;
11046 }
11047 }
11048
11049 /* Set the line number for the statement. */
11050 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11051 SET_EXPR_LOCATION (statement, statement_location);
11052
11053 /* Allow "[[fallthrough]];", but warn otherwise. */
11054 if (std_attrs != NULL_TREE)
11055 warning_at (attrs_location,
11056 OPT_Wattributes,
11057 "attributes at the beginning of statement are ignored");
11058 }
11059
11060 /* Append ATTR to attribute list ATTRS. */
11061
11062 static tree
11063 attr_chainon (tree attrs, tree attr)
11064 {
11065 if (attrs == error_mark_node)
11066 return error_mark_node;
11067 if (attr == error_mark_node)
11068 return error_mark_node;
11069 return chainon (attrs, attr);
11070 }
11071
11072 /* Parse the label for a labeled-statement, i.e.
11073
11074 identifier :
11075 case constant-expression :
11076 default :
11077
11078 GNU Extension:
11079 case constant-expression ... constant-expression : statement
11080
11081 When a label is parsed without errors, the label is added to the
11082 parse tree by the finish_* functions, so this function doesn't
11083 have to return the label. */
11084
11085 static void
11086 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11087 {
11088 cp_token *token;
11089 tree label = NULL_TREE;
11090 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11091
11092 /* The next token should be an identifier. */
11093 token = cp_lexer_peek_token (parser->lexer);
11094 if (token->type != CPP_NAME
11095 && token->type != CPP_KEYWORD)
11096 {
11097 cp_parser_error (parser, "expected labeled-statement");
11098 return;
11099 }
11100
11101 /* Remember whether this case or a user-defined label is allowed to fall
11102 through to. */
11103 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11104
11105 parser->colon_corrects_to_scope_p = false;
11106 switch (token->keyword)
11107 {
11108 case RID_CASE:
11109 {
11110 tree expr, expr_hi;
11111 cp_token *ellipsis;
11112
11113 /* Consume the `case' token. */
11114 cp_lexer_consume_token (parser->lexer);
11115 /* Parse the constant-expression. */
11116 expr = cp_parser_constant_expression (parser);
11117 if (check_for_bare_parameter_packs (expr))
11118 expr = error_mark_node;
11119
11120 ellipsis = cp_lexer_peek_token (parser->lexer);
11121 if (ellipsis->type == CPP_ELLIPSIS)
11122 {
11123 /* Consume the `...' token. */
11124 cp_lexer_consume_token (parser->lexer);
11125 expr_hi = cp_parser_constant_expression (parser);
11126 if (check_for_bare_parameter_packs (expr_hi))
11127 expr_hi = error_mark_node;
11128
11129 /* We don't need to emit warnings here, as the common code
11130 will do this for us. */
11131 }
11132 else
11133 expr_hi = NULL_TREE;
11134
11135 if (parser->in_switch_statement_p)
11136 {
11137 tree l = finish_case_label (token->location, expr, expr_hi);
11138 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11139 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11140 }
11141 else
11142 error_at (token->location,
11143 "case label %qE not within a switch statement",
11144 expr);
11145 }
11146 break;
11147
11148 case RID_DEFAULT:
11149 /* Consume the `default' token. */
11150 cp_lexer_consume_token (parser->lexer);
11151
11152 if (parser->in_switch_statement_p)
11153 {
11154 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11155 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11156 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11157 }
11158 else
11159 error_at (token->location, "case label not within a switch statement");
11160 break;
11161
11162 default:
11163 /* Anything else must be an ordinary label. */
11164 label = finish_label_stmt (cp_parser_identifier (parser));
11165 if (label && TREE_CODE (label) == LABEL_DECL)
11166 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11167 break;
11168 }
11169
11170 /* Require the `:' token. */
11171 cp_parser_require (parser, CPP_COLON, RT_COLON);
11172
11173 /* An ordinary label may optionally be followed by attributes.
11174 However, this is only permitted if the attributes are then
11175 followed by a semicolon. This is because, for backward
11176 compatibility, when parsing
11177 lab: __attribute__ ((unused)) int i;
11178 we want the attribute to attach to "i", not "lab". */
11179 if (label != NULL_TREE
11180 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11181 {
11182 tree attrs;
11183 cp_parser_parse_tentatively (parser);
11184 attrs = cp_parser_gnu_attributes_opt (parser);
11185 if (attrs == NULL_TREE
11186 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11187 cp_parser_abort_tentative_parse (parser);
11188 else if (!cp_parser_parse_definitely (parser))
11189 ;
11190 else
11191 attributes = attr_chainon (attributes, attrs);
11192 }
11193
11194 if (attributes != NULL_TREE)
11195 cplus_decl_attributes (&label, attributes, 0);
11196
11197 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11198 }
11199
11200 /* Parse an expression-statement.
11201
11202 expression-statement:
11203 expression [opt] ;
11204
11205 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11206 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11207 indicates whether this expression-statement is part of an
11208 expression statement. */
11209
11210 static tree
11211 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11212 {
11213 tree statement = NULL_TREE;
11214 cp_token *token = cp_lexer_peek_token (parser->lexer);
11215 location_t loc = token->location;
11216
11217 /* There might be attribute fallthrough. */
11218 tree attr = cp_parser_gnu_attributes_opt (parser);
11219
11220 /* If the next token is a ';', then there is no expression
11221 statement. */
11222 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11223 {
11224 statement = cp_parser_expression (parser);
11225 if (statement == error_mark_node
11226 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11227 {
11228 cp_parser_skip_to_end_of_block_or_statement (parser);
11229 return error_mark_node;
11230 }
11231 }
11232
11233 /* Handle [[fallthrough]];. */
11234 if (attribute_fallthrough_p (attr))
11235 {
11236 /* The next token after the fallthrough attribute is ';'. */
11237 if (statement == NULL_TREE)
11238 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11239 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11240 void_type_node, 0);
11241 else
11242 warning_at (loc, OPT_Wattributes,
11243 "%<fallthrough%> attribute not followed by %<;%>");
11244 attr = NULL_TREE;
11245 }
11246
11247 /* Allow "[[fallthrough]];", but warn otherwise. */
11248 if (attr != NULL_TREE)
11249 warning_at (loc, OPT_Wattributes,
11250 "attributes at the beginning of statement are ignored");
11251
11252 /* Give a helpful message for "A<T>::type t;" and the like. */
11253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11254 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11255 {
11256 if (TREE_CODE (statement) == SCOPE_REF)
11257 error_at (token->location, "need %<typename%> before %qE because "
11258 "%qT is a dependent scope",
11259 statement, TREE_OPERAND (statement, 0));
11260 else if (is_overloaded_fn (statement)
11261 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11262 {
11263 /* A::A a; */
11264 tree fn = get_first_fn (statement);
11265 error_at (token->location,
11266 "%<%T::%D%> names the constructor, not the type",
11267 DECL_CONTEXT (fn), DECL_NAME (fn));
11268 }
11269 }
11270
11271 /* Consume the final `;'. */
11272 cp_parser_consume_semicolon_at_end_of_statement (parser);
11273
11274 if (in_statement_expr
11275 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11276 /* This is the final expression statement of a statement
11277 expression. */
11278 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11279 else if (statement)
11280 statement = finish_expr_stmt (statement);
11281
11282 return statement;
11283 }
11284
11285 /* Parse a compound-statement.
11286
11287 compound-statement:
11288 { statement-seq [opt] }
11289
11290 GNU extension:
11291
11292 compound-statement:
11293 { label-declaration-seq [opt] statement-seq [opt] }
11294
11295 label-declaration-seq:
11296 label-declaration
11297 label-declaration-seq label-declaration
11298
11299 Returns a tree representing the statement. */
11300
11301 static tree
11302 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11303 int bcs_flags, bool function_body)
11304 {
11305 tree compound_stmt;
11306 matching_braces braces;
11307
11308 /* Consume the `{'. */
11309 if (!braces.require_open (parser))
11310 return error_mark_node;
11311 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11312 && !function_body && cxx_dialect < cxx14)
11313 pedwarn (input_location, OPT_Wpedantic,
11314 "compound-statement in %<constexpr%> function");
11315 /* Begin the compound-statement. */
11316 compound_stmt = begin_compound_stmt (bcs_flags);
11317 /* If the next keyword is `__label__' we have a label declaration. */
11318 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11319 cp_parser_label_declaration (parser);
11320 /* Parse an (optional) statement-seq. */
11321 cp_parser_statement_seq_opt (parser, in_statement_expr);
11322 /* Finish the compound-statement. */
11323 finish_compound_stmt (compound_stmt);
11324 /* Consume the `}'. */
11325 braces.require_close (parser);
11326
11327 return compound_stmt;
11328 }
11329
11330 /* Parse an (optional) statement-seq.
11331
11332 statement-seq:
11333 statement
11334 statement-seq [opt] statement */
11335
11336 static void
11337 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11338 {
11339 /* Scan statements until there aren't any more. */
11340 while (true)
11341 {
11342 cp_token *token = cp_lexer_peek_token (parser->lexer);
11343
11344 /* If we are looking at a `}', then we have run out of
11345 statements; the same is true if we have reached the end
11346 of file, or have stumbled upon a stray '@end'. */
11347 if (token->type == CPP_CLOSE_BRACE
11348 || token->type == CPP_EOF
11349 || token->type == CPP_PRAGMA_EOL
11350 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11351 break;
11352
11353 /* If we are in a compound statement and find 'else' then
11354 something went wrong. */
11355 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11356 {
11357 if (parser->in_statement & IN_IF_STMT)
11358 break;
11359 else
11360 {
11361 token = cp_lexer_consume_token (parser->lexer);
11362 error_at (token->location, "%<else%> without a previous %<if%>");
11363 }
11364 }
11365
11366 /* Parse the statement. */
11367 cp_parser_statement (parser, in_statement_expr, true, NULL);
11368 }
11369 }
11370
11371 /* Return true if this is the C++20 version of range-based-for with
11372 init-statement. */
11373
11374 static bool
11375 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11376 {
11377 bool r = false;
11378
11379 /* Save tokens so that we can put them back. */
11380 cp_lexer_save_tokens (parser->lexer);
11381
11382 /* There has to be an unnested ; followed by an unnested :. */
11383 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11384 /*recovering=*/false,
11385 CPP_SEMICOLON,
11386 /*consume_paren=*/false) != -1)
11387 goto out;
11388
11389 /* We found the semicolon, eat it now. */
11390 cp_lexer_consume_token (parser->lexer);
11391
11392 /* Now look for ':' that is not nested in () or {}. */
11393 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11394 /*recovering=*/false,
11395 CPP_COLON,
11396 /*consume_paren=*/false) == -1);
11397
11398 out:
11399 /* Roll back the tokens we skipped. */
11400 cp_lexer_rollback_tokens (parser->lexer);
11401
11402 return r;
11403 }
11404
11405 /* Return true if we're looking at (init; cond), false otherwise. */
11406
11407 static bool
11408 cp_parser_init_statement_p (cp_parser *parser)
11409 {
11410 /* Save tokens so that we can put them back. */
11411 cp_lexer_save_tokens (parser->lexer);
11412
11413 /* Look for ';' that is not nested in () or {}. */
11414 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11415 /*recovering=*/false,
11416 CPP_SEMICOLON,
11417 /*consume_paren=*/false);
11418
11419 /* Roll back the tokens we skipped. */
11420 cp_lexer_rollback_tokens (parser->lexer);
11421
11422 return ret == -1;
11423 }
11424
11425 /* Parse a selection-statement.
11426
11427 selection-statement:
11428 if ( init-statement [opt] condition ) statement
11429 if ( init-statement [opt] condition ) statement else statement
11430 switch ( init-statement [opt] condition ) statement
11431
11432 Returns the new IF_STMT or SWITCH_STMT.
11433
11434 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11435 is a (possibly labeled) if statement which is not enclosed in
11436 braces and has an else clause. This is used to implement
11437 -Wparentheses.
11438
11439 CHAIN is a vector of if-else-if conditions. This is used to implement
11440 -Wduplicated-cond. */
11441
11442 static tree
11443 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11444 vec<tree> *chain)
11445 {
11446 cp_token *token;
11447 enum rid keyword;
11448 token_indent_info guard_tinfo;
11449
11450 if (if_p != NULL)
11451 *if_p = false;
11452
11453 /* Peek at the next token. */
11454 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11455 guard_tinfo = get_token_indent_info (token);
11456
11457 /* See what kind of keyword it is. */
11458 keyword = token->keyword;
11459 switch (keyword)
11460 {
11461 case RID_IF:
11462 case RID_SWITCH:
11463 {
11464 tree statement;
11465 tree condition;
11466
11467 bool cx = false;
11468 if (keyword == RID_IF
11469 && cp_lexer_next_token_is_keyword (parser->lexer,
11470 RID_CONSTEXPR))
11471 {
11472 cx = true;
11473 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11474 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11475 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11476 "with -std=c++17 or -std=gnu++17");
11477 }
11478
11479 /* Look for the `('. */
11480 matching_parens parens;
11481 if (!parens.require_open (parser))
11482 {
11483 cp_parser_skip_to_end_of_statement (parser);
11484 return error_mark_node;
11485 }
11486
11487 /* Begin the selection-statement. */
11488 if (keyword == RID_IF)
11489 {
11490 statement = begin_if_stmt ();
11491 IF_STMT_CONSTEXPR_P (statement) = cx;
11492 }
11493 else
11494 statement = begin_switch_stmt ();
11495
11496 /* Parse the optional init-statement. */
11497 if (cp_parser_init_statement_p (parser))
11498 {
11499 tree decl;
11500 if (cxx_dialect < cxx17)
11501 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11502 "init-statement in selection statements only available "
11503 "with -std=c++17 or -std=gnu++17");
11504 cp_parser_init_statement (parser, &decl);
11505 }
11506
11507 /* Parse the condition. */
11508 condition = cp_parser_condition (parser);
11509 /* Look for the `)'. */
11510 if (!parens.require_close (parser))
11511 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11512 /*consume_paren=*/true);
11513
11514 if (keyword == RID_IF)
11515 {
11516 bool nested_if;
11517 unsigned char in_statement;
11518
11519 /* Add the condition. */
11520 condition = finish_if_stmt_cond (condition, statement);
11521
11522 if (warn_duplicated_cond)
11523 warn_duplicated_cond_add_or_warn (token->location, condition,
11524 &chain);
11525
11526 /* Parse the then-clause. */
11527 in_statement = parser->in_statement;
11528 parser->in_statement |= IN_IF_STMT;
11529
11530 /* Outside a template, the non-selected branch of a constexpr
11531 if is a 'discarded statement', i.e. unevaluated. */
11532 bool was_discarded = in_discarded_stmt;
11533 bool discard_then = (cx && !processing_template_decl
11534 && integer_zerop (condition));
11535 if (discard_then)
11536 {
11537 in_discarded_stmt = true;
11538 ++c_inhibit_evaluation_warnings;
11539 }
11540
11541 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11542 guard_tinfo);
11543
11544 parser->in_statement = in_statement;
11545
11546 finish_then_clause (statement);
11547
11548 if (discard_then)
11549 {
11550 THEN_CLAUSE (statement) = NULL_TREE;
11551 in_discarded_stmt = was_discarded;
11552 --c_inhibit_evaluation_warnings;
11553 }
11554
11555 /* If the next token is `else', parse the else-clause. */
11556 if (cp_lexer_next_token_is_keyword (parser->lexer,
11557 RID_ELSE))
11558 {
11559 bool discard_else = (cx && !processing_template_decl
11560 && integer_nonzerop (condition));
11561 if (discard_else)
11562 {
11563 in_discarded_stmt = true;
11564 ++c_inhibit_evaluation_warnings;
11565 }
11566
11567 guard_tinfo
11568 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11569 /* Consume the `else' keyword. */
11570 cp_lexer_consume_token (parser->lexer);
11571 if (warn_duplicated_cond)
11572 {
11573 if (cp_lexer_next_token_is_keyword (parser->lexer,
11574 RID_IF)
11575 && chain == NULL)
11576 {
11577 /* We've got "if (COND) else if (COND2)". Start
11578 the condition chain and add COND as the first
11579 element. */
11580 chain = new vec<tree> ();
11581 if (!CONSTANT_CLASS_P (condition)
11582 && !TREE_SIDE_EFFECTS (condition))
11583 {
11584 /* Wrap it in a NOP_EXPR so that we can set the
11585 location of the condition. */
11586 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11587 condition);
11588 SET_EXPR_LOCATION (e, token->location);
11589 chain->safe_push (e);
11590 }
11591 }
11592 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11593 RID_IF))
11594 {
11595 /* This is if-else without subsequent if. Zap the
11596 condition chain; we would have already warned at
11597 this point. */
11598 delete chain;
11599 chain = NULL;
11600 }
11601 }
11602 begin_else_clause (statement);
11603 /* Parse the else-clause. */
11604 cp_parser_implicitly_scoped_statement (parser, NULL,
11605 guard_tinfo, chain);
11606
11607 finish_else_clause (statement);
11608
11609 /* If we are currently parsing a then-clause, then
11610 IF_P will not be NULL. We set it to true to
11611 indicate that this if statement has an else clause.
11612 This may trigger the Wparentheses warning below
11613 when we get back up to the parent if statement. */
11614 if (if_p != NULL)
11615 *if_p = true;
11616
11617 if (discard_else)
11618 {
11619 ELSE_CLAUSE (statement) = NULL_TREE;
11620 in_discarded_stmt = was_discarded;
11621 --c_inhibit_evaluation_warnings;
11622 }
11623 }
11624 else
11625 {
11626 /* This if statement does not have an else clause. If
11627 NESTED_IF is true, then the then-clause has an if
11628 statement which does have an else clause. We warn
11629 about the potential ambiguity. */
11630 if (nested_if)
11631 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11632 "suggest explicit braces to avoid ambiguous"
11633 " %<else%>");
11634 if (warn_duplicated_cond)
11635 {
11636 /* We don't need the condition chain anymore. */
11637 delete chain;
11638 chain = NULL;
11639 }
11640 }
11641
11642 /* Now we're all done with the if-statement. */
11643 finish_if_stmt (statement);
11644 }
11645 else
11646 {
11647 bool in_switch_statement_p;
11648 unsigned char in_statement;
11649
11650 /* Add the condition. */
11651 finish_switch_cond (condition, statement);
11652
11653 /* Parse the body of the switch-statement. */
11654 in_switch_statement_p = parser->in_switch_statement_p;
11655 in_statement = parser->in_statement;
11656 parser->in_switch_statement_p = true;
11657 parser->in_statement |= IN_SWITCH_STMT;
11658 cp_parser_implicitly_scoped_statement (parser, if_p,
11659 guard_tinfo);
11660 parser->in_switch_statement_p = in_switch_statement_p;
11661 parser->in_statement = in_statement;
11662
11663 /* Now we're all done with the switch-statement. */
11664 finish_switch_stmt (statement);
11665 }
11666
11667 return statement;
11668 }
11669 break;
11670
11671 default:
11672 cp_parser_error (parser, "expected selection-statement");
11673 return error_mark_node;
11674 }
11675 }
11676
11677 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11678 If we have seen at least one decl-specifier, and the next token
11679 is not a parenthesis, then we must be looking at a declaration.
11680 (After "int (" we might be looking at a functional cast.) */
11681
11682 static void
11683 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11684 bool any_specifiers_p)
11685 {
11686 if (any_specifiers_p
11687 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11688 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11689 && !cp_parser_error_occurred (parser))
11690 cp_parser_commit_to_tentative_parse (parser);
11691 }
11692
11693 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11694 The declarator shall not specify a function or an array. Returns
11695 TRUE if the declarator is valid, FALSE otherwise. */
11696
11697 static bool
11698 cp_parser_check_condition_declarator (cp_parser* parser,
11699 cp_declarator *declarator,
11700 location_t loc)
11701 {
11702 if (declarator == cp_error_declarator
11703 || function_declarator_p (declarator)
11704 || declarator->kind == cdk_array)
11705 {
11706 if (declarator == cp_error_declarator)
11707 /* Already complained. */;
11708 else if (declarator->kind == cdk_array)
11709 error_at (loc, "condition declares an array");
11710 else
11711 error_at (loc, "condition declares a function");
11712 if (parser->fully_implicit_function_template_p)
11713 abort_fully_implicit_template (parser);
11714 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11715 /*or_comma=*/false,
11716 /*consume_paren=*/false);
11717 return false;
11718 }
11719 else
11720 return true;
11721 }
11722
11723 /* Parse a condition.
11724
11725 condition:
11726 expression
11727 type-specifier-seq declarator = initializer-clause
11728 type-specifier-seq declarator braced-init-list
11729
11730 GNU Extension:
11731
11732 condition:
11733 type-specifier-seq declarator asm-specification [opt]
11734 attributes [opt] = assignment-expression
11735
11736 Returns the expression that should be tested. */
11737
11738 static tree
11739 cp_parser_condition (cp_parser* parser)
11740 {
11741 cp_decl_specifier_seq type_specifiers;
11742 const char *saved_message;
11743 int declares_class_or_enum;
11744
11745 /* Try the declaration first. */
11746 cp_parser_parse_tentatively (parser);
11747 /* New types are not allowed in the type-specifier-seq for a
11748 condition. */
11749 saved_message = parser->type_definition_forbidden_message;
11750 parser->type_definition_forbidden_message
11751 = G_("types may not be defined in conditions");
11752 /* Parse the type-specifier-seq. */
11753 cp_parser_decl_specifier_seq (parser,
11754 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11755 &type_specifiers,
11756 &declares_class_or_enum);
11757 /* Restore the saved message. */
11758 parser->type_definition_forbidden_message = saved_message;
11759
11760 cp_parser_maybe_commit_to_declaration (parser,
11761 type_specifiers.any_specifiers_p);
11762
11763 /* If all is well, we might be looking at a declaration. */
11764 if (!cp_parser_error_occurred (parser))
11765 {
11766 tree decl;
11767 tree asm_specification;
11768 tree attributes;
11769 cp_declarator *declarator;
11770 tree initializer = NULL_TREE;
11771 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11772
11773 /* Parse the declarator. */
11774 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11775 /*ctor_dtor_or_conv_p=*/NULL,
11776 /*parenthesized_p=*/NULL,
11777 /*member_p=*/false,
11778 /*friend_p=*/false);
11779 /* Parse the attributes. */
11780 attributes = cp_parser_attributes_opt (parser);
11781 /* Parse the asm-specification. */
11782 asm_specification = cp_parser_asm_specification_opt (parser);
11783 /* If the next token is not an `=' or '{', then we might still be
11784 looking at an expression. For example:
11785
11786 if (A(a).x)
11787
11788 looks like a decl-specifier-seq and a declarator -- but then
11789 there is no `=', so this is an expression. */
11790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11791 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11792 cp_parser_simulate_error (parser);
11793
11794 /* If we did see an `=' or '{', then we are looking at a declaration
11795 for sure. */
11796 if (cp_parser_parse_definitely (parser))
11797 {
11798 tree pushed_scope;
11799 bool non_constant_p = false;
11800 int flags = LOOKUP_ONLYCONVERTING;
11801
11802 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11803 return error_mark_node;
11804
11805 /* Create the declaration. */
11806 decl = start_decl (declarator, &type_specifiers,
11807 /*initialized_p=*/true,
11808 attributes, /*prefix_attributes=*/NULL_TREE,
11809 &pushed_scope);
11810
11811 /* Parse the initializer. */
11812 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11813 {
11814 initializer = cp_parser_braced_list (parser, &non_constant_p);
11815 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11816 flags = 0;
11817 }
11818 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11819 {
11820 /* Consume the `='. */
11821 cp_lexer_consume_token (parser->lexer);
11822 initializer = cp_parser_initializer_clause (parser,
11823 &non_constant_p);
11824 }
11825 else
11826 {
11827 cp_parser_error (parser, "expected initializer");
11828 initializer = error_mark_node;
11829 }
11830 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11831 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11832
11833 /* Process the initializer. */
11834 cp_finish_decl (decl,
11835 initializer, !non_constant_p,
11836 asm_specification,
11837 flags);
11838
11839 if (pushed_scope)
11840 pop_scope (pushed_scope);
11841
11842 return convert_from_reference (decl);
11843 }
11844 }
11845 /* If we didn't even get past the declarator successfully, we are
11846 definitely not looking at a declaration. */
11847 else
11848 cp_parser_abort_tentative_parse (parser);
11849
11850 /* Otherwise, we are looking at an expression. */
11851 return cp_parser_expression (parser);
11852 }
11853
11854 /* Parses a for-statement or range-for-statement until the closing ')',
11855 not included. */
11856
11857 static tree
11858 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11859 {
11860 tree init, scope, decl;
11861 bool is_range_for;
11862
11863 /* Begin the for-statement. */
11864 scope = begin_for_scope (&init);
11865
11866 /* Parse the initialization. */
11867 is_range_for = cp_parser_init_statement (parser, &decl);
11868
11869 if (is_range_for)
11870 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
11871 false);
11872 else
11873 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11874 }
11875
11876 static tree
11877 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11878 unsigned short unroll)
11879 {
11880 /* Normal for loop */
11881 tree condition = NULL_TREE;
11882 tree expression = NULL_TREE;
11883 tree stmt;
11884
11885 stmt = begin_for_stmt (scope, init);
11886 /* The init-statement has already been parsed in
11887 cp_parser_init_statement, so no work is needed here. */
11888 finish_init_stmt (stmt);
11889
11890 /* If there's a condition, process it. */
11891 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11892 condition = cp_parser_condition (parser);
11893 else if (ivdep)
11894 {
11895 cp_parser_error (parser, "missing loop condition in loop with "
11896 "%<GCC ivdep%> pragma");
11897 condition = error_mark_node;
11898 }
11899 else if (unroll)
11900 {
11901 cp_parser_error (parser, "missing loop condition in loop with "
11902 "%<GCC unroll%> pragma");
11903 condition = error_mark_node;
11904 }
11905 finish_for_cond (condition, stmt, ivdep, unroll);
11906 /* Look for the `;'. */
11907 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11908
11909 /* If there's an expression, process it. */
11910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11911 expression = cp_parser_expression (parser);
11912 finish_for_expr (expression, stmt);
11913
11914 return stmt;
11915 }
11916
11917 /* Tries to parse a range-based for-statement:
11918
11919 range-based-for:
11920 decl-specifier-seq declarator : expression
11921
11922 The decl-specifier-seq declarator and the `:' are already parsed by
11923 cp_parser_init_statement. If processing_template_decl it returns a
11924 newly created RANGE_FOR_STMT; if not, it is converted to a
11925 regular FOR_STMT. */
11926
11927 static tree
11928 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11929 bool ivdep, unsigned short unroll, bool is_omp)
11930 {
11931 tree stmt, range_expr;
11932 auto_vec <cxx_binding *, 16> bindings;
11933 auto_vec <tree, 16> names;
11934 tree decomp_first_name = NULL_TREE;
11935 unsigned int decomp_cnt = 0;
11936
11937 /* Get the range declaration momentarily out of the way so that
11938 the range expression doesn't clash with it. */
11939 if (range_decl != error_mark_node)
11940 {
11941 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11942 {
11943 tree v = DECL_VALUE_EXPR (range_decl);
11944 /* For decomposition declaration get all of the corresponding
11945 declarations out of the way. */
11946 if (TREE_CODE (v) == ARRAY_REF
11947 && VAR_P (TREE_OPERAND (v, 0))
11948 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11949 {
11950 tree d = range_decl;
11951 range_decl = TREE_OPERAND (v, 0);
11952 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11953 decomp_first_name = d;
11954 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11955 {
11956 tree name = DECL_NAME (d);
11957 names.safe_push (name);
11958 bindings.safe_push (IDENTIFIER_BINDING (name));
11959 IDENTIFIER_BINDING (name)
11960 = IDENTIFIER_BINDING (name)->previous;
11961 }
11962 }
11963 }
11964 if (names.is_empty ())
11965 {
11966 tree name = DECL_NAME (range_decl);
11967 names.safe_push (name);
11968 bindings.safe_push (IDENTIFIER_BINDING (name));
11969 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11970 }
11971 }
11972
11973 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11974 {
11975 bool expr_non_constant_p;
11976 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11977 }
11978 else
11979 range_expr = cp_parser_expression (parser);
11980
11981 /* Put the range declaration(s) back into scope. */
11982 for (unsigned int i = 0; i < names.length (); i++)
11983 {
11984 cxx_binding *binding = bindings[i];
11985 binding->previous = IDENTIFIER_BINDING (names[i]);
11986 IDENTIFIER_BINDING (names[i]) = binding;
11987 }
11988
11989 /* finish_omp_for has its own code for the following, so just
11990 return the range_expr instead. */
11991 if (is_omp)
11992 return range_expr;
11993
11994 /* If in template, STMT is converted to a normal for-statement
11995 at instantiation. If not, it is done just ahead. */
11996 if (processing_template_decl)
11997 {
11998 if (check_for_bare_parameter_packs (range_expr))
11999 range_expr = error_mark_node;
12000 stmt = begin_range_for_stmt (scope, init);
12001 if (ivdep)
12002 RANGE_FOR_IVDEP (stmt) = 1;
12003 if (unroll)
12004 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12005 finish_range_for_decl (stmt, range_decl, range_expr);
12006 if (!type_dependent_expression_p (range_expr)
12007 /* do_auto_deduction doesn't mess with template init-lists. */
12008 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12009 do_range_for_auto_deduction (range_decl, range_expr);
12010 }
12011 else
12012 {
12013 stmt = begin_for_stmt (scope, init);
12014 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12015 decomp_first_name, decomp_cnt, ivdep,
12016 unroll);
12017 }
12018 return stmt;
12019 }
12020
12021 /* Subroutine of cp_convert_range_for: given the initializer expression,
12022 builds up the range temporary. */
12023
12024 static tree
12025 build_range_temp (tree range_expr)
12026 {
12027 tree range_type, range_temp;
12028
12029 /* Find out the type deduced by the declaration
12030 `auto &&__range = range_expr'. */
12031 range_type = cp_build_reference_type (make_auto (), true);
12032 range_type = do_auto_deduction (range_type, range_expr,
12033 type_uses_auto (range_type));
12034
12035 /* Create the __range variable. */
12036 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12037 range_type);
12038 TREE_USED (range_temp) = 1;
12039 DECL_ARTIFICIAL (range_temp) = 1;
12040
12041 return range_temp;
12042 }
12043
12044 /* Used by cp_parser_range_for in template context: we aren't going to
12045 do a full conversion yet, but we still need to resolve auto in the
12046 type of the for-range-declaration if present. This is basically
12047 a shortcut version of cp_convert_range_for. */
12048
12049 static void
12050 do_range_for_auto_deduction (tree decl, tree range_expr)
12051 {
12052 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12053 if (auto_node)
12054 {
12055 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12056 range_temp = convert_from_reference (build_range_temp (range_expr));
12057 iter_type = (cp_parser_perform_range_for_lookup
12058 (range_temp, &begin_dummy, &end_dummy));
12059 if (iter_type)
12060 {
12061 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12062 iter_type);
12063 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12064 RO_UNARY_STAR,
12065 tf_warning_or_error);
12066 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12067 iter_decl, auto_node);
12068 }
12069 }
12070 }
12071
12072 /* Converts a range-based for-statement into a normal
12073 for-statement, as per the definition.
12074
12075 for (RANGE_DECL : RANGE_EXPR)
12076 BLOCK
12077
12078 should be equivalent to:
12079
12080 {
12081 auto &&__range = RANGE_EXPR;
12082 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12083 __begin != __end;
12084 ++__begin)
12085 {
12086 RANGE_DECL = *__begin;
12087 BLOCK
12088 }
12089 }
12090
12091 If RANGE_EXPR is an array:
12092 BEGIN_EXPR = __range
12093 END_EXPR = __range + ARRAY_SIZE(__range)
12094 Else if RANGE_EXPR has a member 'begin' or 'end':
12095 BEGIN_EXPR = __range.begin()
12096 END_EXPR = __range.end()
12097 Else:
12098 BEGIN_EXPR = begin(__range)
12099 END_EXPR = end(__range);
12100
12101 If __range has a member 'begin' but not 'end', or vice versa, we must
12102 still use the second alternative (it will surely fail, however).
12103 When calling begin()/end() in the third alternative we must use
12104 argument dependent lookup, but always considering 'std' as an associated
12105 namespace. */
12106
12107 tree
12108 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12109 tree decomp_first_name, unsigned int decomp_cnt,
12110 bool ivdep, unsigned short unroll)
12111 {
12112 tree begin, end;
12113 tree iter_type, begin_expr, end_expr;
12114 tree condition, expression;
12115
12116 range_expr = mark_lvalue_use (range_expr);
12117
12118 if (range_decl == error_mark_node || range_expr == error_mark_node)
12119 /* If an error happened previously do nothing or else a lot of
12120 unhelpful errors would be issued. */
12121 begin_expr = end_expr = iter_type = error_mark_node;
12122 else
12123 {
12124 tree range_temp;
12125
12126 if (VAR_P (range_expr)
12127 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12128 /* Can't bind a reference to an array of runtime bound. */
12129 range_temp = range_expr;
12130 else
12131 {
12132 range_temp = build_range_temp (range_expr);
12133 pushdecl (range_temp);
12134 cp_finish_decl (range_temp, range_expr,
12135 /*is_constant_init*/false, NULL_TREE,
12136 LOOKUP_ONLYCONVERTING);
12137 range_temp = convert_from_reference (range_temp);
12138 }
12139 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12140 &begin_expr, &end_expr);
12141 }
12142
12143 /* The new for initialization statement. */
12144 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12145 iter_type);
12146 TREE_USED (begin) = 1;
12147 DECL_ARTIFICIAL (begin) = 1;
12148 pushdecl (begin);
12149 cp_finish_decl (begin, begin_expr,
12150 /*is_constant_init*/false, NULL_TREE,
12151 LOOKUP_ONLYCONVERTING);
12152
12153 if (cxx_dialect >= cxx17)
12154 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12155 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12156 TREE_USED (end) = 1;
12157 DECL_ARTIFICIAL (end) = 1;
12158 pushdecl (end);
12159 cp_finish_decl (end, end_expr,
12160 /*is_constant_init*/false, NULL_TREE,
12161 LOOKUP_ONLYCONVERTING);
12162
12163 finish_init_stmt (statement);
12164
12165 /* The new for condition. */
12166 condition = build_x_binary_op (input_location, NE_EXPR,
12167 begin, ERROR_MARK,
12168 end, ERROR_MARK,
12169 NULL, tf_warning_or_error);
12170 finish_for_cond (condition, statement, ivdep, unroll);
12171
12172 /* The new increment expression. */
12173 expression = finish_unary_op_expr (input_location,
12174 PREINCREMENT_EXPR, begin,
12175 tf_warning_or_error);
12176 finish_for_expr (expression, statement);
12177
12178 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12179 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12180
12181 /* The declaration is initialized with *__begin inside the loop body. */
12182 cp_finish_decl (range_decl,
12183 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12184 tf_warning_or_error),
12185 /*is_constant_init*/false, NULL_TREE,
12186 LOOKUP_ONLYCONVERTING);
12187 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12188 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12189
12190 return statement;
12191 }
12192
12193 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12194 We need to solve both at the same time because the method used
12195 depends on the existence of members begin or end.
12196 Returns the type deduced for the iterator expression. */
12197
12198 static tree
12199 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12200 {
12201 if (error_operand_p (range))
12202 {
12203 *begin = *end = error_mark_node;
12204 return error_mark_node;
12205 }
12206
12207 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12208 {
12209 error ("range-based %<for%> expression of type %qT "
12210 "has incomplete type", TREE_TYPE (range));
12211 *begin = *end = error_mark_node;
12212 return error_mark_node;
12213 }
12214 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12215 {
12216 /* If RANGE is an array, we will use pointer arithmetic. */
12217 *begin = decay_conversion (range, tf_warning_or_error);
12218 *end = build_binary_op (input_location, PLUS_EXPR,
12219 range,
12220 array_type_nelts_top (TREE_TYPE (range)),
12221 false);
12222 return TREE_TYPE (*begin);
12223 }
12224 else
12225 {
12226 /* If it is not an array, we must do a bit of magic. */
12227 tree id_begin, id_end;
12228 tree member_begin, member_end;
12229
12230 *begin = *end = error_mark_node;
12231
12232 id_begin = get_identifier ("begin");
12233 id_end = get_identifier ("end");
12234 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12235 /*protect=*/2, /*want_type=*/false,
12236 tf_warning_or_error);
12237 member_end = lookup_member (TREE_TYPE (range), id_end,
12238 /*protect=*/2, /*want_type=*/false,
12239 tf_warning_or_error);
12240
12241 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12242 {
12243 /* Use the member functions. */
12244 *begin = cp_parser_range_for_member_function (range, id_begin);
12245 *end = cp_parser_range_for_member_function (range, id_end);
12246 }
12247 else
12248 {
12249 /* Use global functions with ADL. */
12250 vec<tree, va_gc> *vec;
12251 vec = make_tree_vector ();
12252
12253 vec_safe_push (vec, range);
12254
12255 member_begin = perform_koenig_lookup (id_begin, vec,
12256 tf_warning_or_error);
12257 *begin = finish_call_expr (member_begin, &vec, false, true,
12258 tf_warning_or_error);
12259 member_end = perform_koenig_lookup (id_end, vec,
12260 tf_warning_or_error);
12261 *end = finish_call_expr (member_end, &vec, false, true,
12262 tf_warning_or_error);
12263
12264 release_tree_vector (vec);
12265 }
12266
12267 /* Last common checks. */
12268 if (*begin == error_mark_node || *end == error_mark_node)
12269 {
12270 /* If one of the expressions is an error do no more checks. */
12271 *begin = *end = error_mark_node;
12272 return error_mark_node;
12273 }
12274 else if (type_dependent_expression_p (*begin)
12275 || type_dependent_expression_p (*end))
12276 /* Can happen, when, eg, in a template context, Koenig lookup
12277 can't resolve begin/end (c++/58503). */
12278 return NULL_TREE;
12279 else
12280 {
12281 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12282 /* The unqualified type of the __begin and __end temporaries should
12283 be the same, as required by the multiple auto declaration. */
12284 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12285 {
12286 if (cxx_dialect >= cxx17
12287 && (build_x_binary_op (input_location, NE_EXPR,
12288 *begin, ERROR_MARK,
12289 *end, ERROR_MARK,
12290 NULL, tf_none)
12291 != error_mark_node))
12292 /* P0184R0 allows __begin and __end to have different types,
12293 but make sure they are comparable so we can give a better
12294 diagnostic. */;
12295 else
12296 error ("inconsistent begin/end types in range-based %<for%> "
12297 "statement: %qT and %qT",
12298 TREE_TYPE (*begin), TREE_TYPE (*end));
12299 }
12300 return iter_type;
12301 }
12302 }
12303 }
12304
12305 /* Helper function for cp_parser_perform_range_for_lookup.
12306 Builds a tree for RANGE.IDENTIFIER(). */
12307
12308 static tree
12309 cp_parser_range_for_member_function (tree range, tree identifier)
12310 {
12311 tree member, res;
12312 vec<tree, va_gc> *vec;
12313
12314 member = finish_class_member_access_expr (range, identifier,
12315 false, tf_warning_or_error);
12316 if (member == error_mark_node)
12317 return error_mark_node;
12318
12319 vec = make_tree_vector ();
12320 res = finish_call_expr (member, &vec,
12321 /*disallow_virtual=*/false,
12322 /*koenig_p=*/false,
12323 tf_warning_or_error);
12324 release_tree_vector (vec);
12325 return res;
12326 }
12327
12328 /* Parse an iteration-statement.
12329
12330 iteration-statement:
12331 while ( condition ) statement
12332 do statement while ( expression ) ;
12333 for ( init-statement condition [opt] ; expression [opt] )
12334 statement
12335
12336 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12337
12338 static tree
12339 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12340 unsigned short unroll)
12341 {
12342 cp_token *token;
12343 enum rid keyword;
12344 tree statement;
12345 unsigned char in_statement;
12346 token_indent_info guard_tinfo;
12347
12348 /* Peek at the next token. */
12349 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12350 if (!token)
12351 return error_mark_node;
12352
12353 guard_tinfo = get_token_indent_info (token);
12354
12355 /* Remember whether or not we are already within an iteration
12356 statement. */
12357 in_statement = parser->in_statement;
12358
12359 /* See what kind of keyword it is. */
12360 keyword = token->keyword;
12361 switch (keyword)
12362 {
12363 case RID_WHILE:
12364 {
12365 tree condition;
12366
12367 /* Begin the while-statement. */
12368 statement = begin_while_stmt ();
12369 /* Look for the `('. */
12370 matching_parens parens;
12371 parens.require_open (parser);
12372 /* Parse the condition. */
12373 condition = cp_parser_condition (parser);
12374 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12375 /* Look for the `)'. */
12376 parens.require_close (parser);
12377 /* Parse the dependent statement. */
12378 parser->in_statement = IN_ITERATION_STMT;
12379 bool prev = note_iteration_stmt_body_start ();
12380 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12381 note_iteration_stmt_body_end (prev);
12382 parser->in_statement = in_statement;
12383 /* We're done with the while-statement. */
12384 finish_while_stmt (statement);
12385 }
12386 break;
12387
12388 case RID_DO:
12389 {
12390 tree expression;
12391
12392 /* Begin the do-statement. */
12393 statement = begin_do_stmt ();
12394 /* Parse the body of the do-statement. */
12395 parser->in_statement = IN_ITERATION_STMT;
12396 bool prev = note_iteration_stmt_body_start ();
12397 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12398 note_iteration_stmt_body_end (prev);
12399 parser->in_statement = in_statement;
12400 finish_do_body (statement);
12401 /* Look for the `while' keyword. */
12402 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12403 /* Look for the `('. */
12404 matching_parens parens;
12405 parens.require_open (parser);
12406 /* Parse the expression. */
12407 expression = cp_parser_expression (parser);
12408 /* We're done with the do-statement. */
12409 finish_do_stmt (expression, statement, ivdep, unroll);
12410 /* Look for the `)'. */
12411 parens.require_close (parser);
12412 /* Look for the `;'. */
12413 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12414 }
12415 break;
12416
12417 case RID_FOR:
12418 {
12419 /* Look for the `('. */
12420 matching_parens parens;
12421 parens.require_open (parser);
12422
12423 statement = cp_parser_for (parser, ivdep, unroll);
12424
12425 /* Look for the `)'. */
12426 parens.require_close (parser);
12427
12428 /* Parse the body of the for-statement. */
12429 parser->in_statement = IN_ITERATION_STMT;
12430 bool prev = note_iteration_stmt_body_start ();
12431 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12432 note_iteration_stmt_body_end (prev);
12433 parser->in_statement = in_statement;
12434
12435 /* We're done with the for-statement. */
12436 finish_for_stmt (statement);
12437 }
12438 break;
12439
12440 default:
12441 cp_parser_error (parser, "expected iteration-statement");
12442 statement = error_mark_node;
12443 break;
12444 }
12445
12446 return statement;
12447 }
12448
12449 /* Parse a init-statement or the declarator of a range-based-for.
12450 Returns true if a range-based-for declaration is seen.
12451
12452 init-statement:
12453 expression-statement
12454 simple-declaration */
12455
12456 static bool
12457 cp_parser_init_statement (cp_parser *parser, tree *decl)
12458 {
12459 /* If the next token is a `;', then we have an empty
12460 expression-statement. Grammatically, this is also a
12461 simple-declaration, but an invalid one, because it does not
12462 declare anything. Therefore, if we did not handle this case
12463 specially, we would issue an error message about an invalid
12464 declaration. */
12465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12466 {
12467 bool is_range_for = false;
12468 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12469
12470 /* Try to parse the init-statement. */
12471 if (cp_parser_range_based_for_with_init_p (parser))
12472 {
12473 tree dummy;
12474 cp_parser_parse_tentatively (parser);
12475 /* Parse the declaration. */
12476 cp_parser_simple_declaration (parser,
12477 /*function_definition_allowed_p=*/false,
12478 &dummy);
12479 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12480 if (!cp_parser_parse_definitely (parser))
12481 /* That didn't work, try to parse it as an expression-statement. */
12482 cp_parser_expression_statement (parser, NULL_TREE);
12483
12484 if (cxx_dialect < cxx2a)
12485 {
12486 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12487 "range-based %<for%> loops with initializer only "
12488 "available with -std=c++2a or -std=gnu++2a");
12489 *decl = error_mark_node;
12490 }
12491 }
12492
12493 /* A colon is used in range-based for. */
12494 parser->colon_corrects_to_scope_p = false;
12495
12496 /* We're going to speculatively look for a declaration, falling back
12497 to an expression, if necessary. */
12498 cp_parser_parse_tentatively (parser);
12499 /* Parse the declaration. */
12500 cp_parser_simple_declaration (parser,
12501 /*function_definition_allowed_p=*/false,
12502 decl);
12503 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12504 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12505 {
12506 /* It is a range-for, consume the ':'. */
12507 cp_lexer_consume_token (parser->lexer);
12508 is_range_for = true;
12509 if (cxx_dialect < cxx11)
12510 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12511 "range-based %<for%> loops only available with "
12512 "-std=c++11 or -std=gnu++11");
12513 }
12514 else
12515 /* The ';' is not consumed yet because we told
12516 cp_parser_simple_declaration not to. */
12517 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12518
12519 if (cp_parser_parse_definitely (parser))
12520 return is_range_for;
12521 /* If the tentative parse failed, then we shall need to look for an
12522 expression-statement. */
12523 }
12524 /* If we are here, it is an expression-statement. */
12525 cp_parser_expression_statement (parser, NULL_TREE);
12526 return false;
12527 }
12528
12529 /* Parse a jump-statement.
12530
12531 jump-statement:
12532 break ;
12533 continue ;
12534 return expression [opt] ;
12535 return braced-init-list ;
12536 goto identifier ;
12537
12538 GNU extension:
12539
12540 jump-statement:
12541 goto * expression ;
12542
12543 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12544
12545 static tree
12546 cp_parser_jump_statement (cp_parser* parser)
12547 {
12548 tree statement = error_mark_node;
12549 cp_token *token;
12550 enum rid keyword;
12551 unsigned char in_statement;
12552
12553 /* Peek at the next token. */
12554 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12555 if (!token)
12556 return error_mark_node;
12557
12558 /* See what kind of keyword it is. */
12559 keyword = token->keyword;
12560 switch (keyword)
12561 {
12562 case RID_BREAK:
12563 in_statement = parser->in_statement & ~IN_IF_STMT;
12564 switch (in_statement)
12565 {
12566 case 0:
12567 error_at (token->location, "break statement not within loop or switch");
12568 break;
12569 default:
12570 gcc_assert ((in_statement & IN_SWITCH_STMT)
12571 || in_statement == IN_ITERATION_STMT);
12572 statement = finish_break_stmt ();
12573 if (in_statement == IN_ITERATION_STMT)
12574 break_maybe_infinite_loop ();
12575 break;
12576 case IN_OMP_BLOCK:
12577 error_at (token->location, "invalid exit from OpenMP structured block");
12578 break;
12579 case IN_OMP_FOR:
12580 error_at (token->location, "break statement used with OpenMP for loop");
12581 break;
12582 }
12583 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12584 break;
12585
12586 case RID_CONTINUE:
12587 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12588 {
12589 case 0:
12590 error_at (token->location, "continue statement not within a loop");
12591 break;
12592 /* Fall through. */
12593 case IN_ITERATION_STMT:
12594 case IN_OMP_FOR:
12595 statement = finish_continue_stmt ();
12596 break;
12597 case IN_OMP_BLOCK:
12598 error_at (token->location, "invalid exit from OpenMP structured block");
12599 break;
12600 default:
12601 gcc_unreachable ();
12602 }
12603 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12604 break;
12605
12606 case RID_RETURN:
12607 {
12608 tree expr;
12609 bool expr_non_constant_p;
12610
12611 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12612 {
12613 cp_lexer_set_source_position (parser->lexer);
12614 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12615 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12616 }
12617 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12618 expr = cp_parser_expression (parser);
12619 else
12620 /* If the next token is a `;', then there is no
12621 expression. */
12622 expr = NULL_TREE;
12623 /* Build the return-statement. */
12624 if (current_function_auto_return_pattern && in_discarded_stmt)
12625 /* Don't deduce from a discarded return statement. */;
12626 else
12627 statement = finish_return_stmt (expr);
12628 /* Look for the final `;'. */
12629 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12630 }
12631 break;
12632
12633 case RID_GOTO:
12634 if (parser->in_function_body
12635 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12636 {
12637 error ("%<goto%> in %<constexpr%> function");
12638 cp_function_chain->invalid_constexpr = true;
12639 }
12640
12641 /* Create the goto-statement. */
12642 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12643 {
12644 /* Issue a warning about this use of a GNU extension. */
12645 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12646 /* Consume the '*' token. */
12647 cp_lexer_consume_token (parser->lexer);
12648 /* Parse the dependent expression. */
12649 finish_goto_stmt (cp_parser_expression (parser));
12650 }
12651 else
12652 finish_goto_stmt (cp_parser_identifier (parser));
12653 /* Look for the final `;'. */
12654 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12655 break;
12656
12657 default:
12658 cp_parser_error (parser, "expected jump-statement");
12659 break;
12660 }
12661
12662 return statement;
12663 }
12664
12665 /* Parse a declaration-statement.
12666
12667 declaration-statement:
12668 block-declaration */
12669
12670 static void
12671 cp_parser_declaration_statement (cp_parser* parser)
12672 {
12673 void *p;
12674
12675 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12676 p = obstack_alloc (&declarator_obstack, 0);
12677
12678 /* Parse the block-declaration. */
12679 cp_parser_block_declaration (parser, /*statement_p=*/true);
12680
12681 /* Free any declarators allocated. */
12682 obstack_free (&declarator_obstack, p);
12683 }
12684
12685 /* Some dependent statements (like `if (cond) statement'), are
12686 implicitly in their own scope. In other words, if the statement is
12687 a single statement (as opposed to a compound-statement), it is
12688 none-the-less treated as if it were enclosed in braces. Any
12689 declarations appearing in the dependent statement are out of scope
12690 after control passes that point. This function parses a statement,
12691 but ensures that is in its own scope, even if it is not a
12692 compound-statement.
12693
12694 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12695 is a (possibly labeled) if statement which is not enclosed in
12696 braces and has an else clause. This is used to implement
12697 -Wparentheses.
12698
12699 CHAIN is a vector of if-else-if conditions. This is used to implement
12700 -Wduplicated-cond.
12701
12702 Returns the new statement. */
12703
12704 static tree
12705 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12706 const token_indent_info &guard_tinfo,
12707 vec<tree> *chain)
12708 {
12709 tree statement;
12710 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12711 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12712 token_indent_info body_tinfo
12713 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12714
12715 if (if_p != NULL)
12716 *if_p = false;
12717
12718 /* Mark if () ; with a special NOP_EXPR. */
12719 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12720 {
12721 cp_lexer_consume_token (parser->lexer);
12722 statement = add_stmt (build_empty_stmt (body_loc));
12723
12724 if (guard_tinfo.keyword == RID_IF
12725 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12726 warning_at (body_loc, OPT_Wempty_body,
12727 "suggest braces around empty body in an %<if%> statement");
12728 else if (guard_tinfo.keyword == RID_ELSE)
12729 warning_at (body_loc, OPT_Wempty_body,
12730 "suggest braces around empty body in an %<else%> statement");
12731 }
12732 /* if a compound is opened, we simply parse the statement directly. */
12733 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12734 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12735 /* If the token is not a `{', then we must take special action. */
12736 else
12737 {
12738 /* Create a compound-statement. */
12739 statement = begin_compound_stmt (0);
12740 /* Parse the dependent-statement. */
12741 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12742 &body_loc_after_labels);
12743 /* Finish the dummy compound-statement. */
12744 finish_compound_stmt (statement);
12745 }
12746
12747 token_indent_info next_tinfo
12748 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12749 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12750
12751 if (body_loc_after_labels != UNKNOWN_LOCATION
12752 && next_tinfo.type != CPP_SEMICOLON)
12753 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12754 guard_tinfo.location, guard_tinfo.keyword);
12755
12756 /* Return the statement. */
12757 return statement;
12758 }
12759
12760 /* For some dependent statements (like `while (cond) statement'), we
12761 have already created a scope. Therefore, even if the dependent
12762 statement is a compound-statement, we do not want to create another
12763 scope. */
12764
12765 static void
12766 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12767 const token_indent_info &guard_tinfo)
12768 {
12769 /* If the token is a `{', then we must take special action. */
12770 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12771 {
12772 token_indent_info body_tinfo
12773 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12774 location_t loc_after_labels = UNKNOWN_LOCATION;
12775
12776 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12777 &loc_after_labels);
12778 token_indent_info next_tinfo
12779 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12780 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12781
12782 if (loc_after_labels != UNKNOWN_LOCATION
12783 && next_tinfo.type != CPP_SEMICOLON)
12784 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12785 guard_tinfo.location,
12786 guard_tinfo.keyword);
12787 }
12788 else
12789 {
12790 /* Avoid calling cp_parser_compound_statement, so that we
12791 don't create a new scope. Do everything else by hand. */
12792 matching_braces braces;
12793 braces.require_open (parser);
12794 /* If the next keyword is `__label__' we have a label declaration. */
12795 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12796 cp_parser_label_declaration (parser);
12797 /* Parse an (optional) statement-seq. */
12798 cp_parser_statement_seq_opt (parser, NULL_TREE);
12799 braces.require_close (parser);
12800 }
12801 }
12802
12803 /* Declarations [gram.dcl.dcl] */
12804
12805 /* Parse an optional declaration-sequence.
12806
12807 declaration-seq:
12808 declaration
12809 declaration-seq declaration */
12810
12811 static void
12812 cp_parser_declaration_seq_opt (cp_parser* parser)
12813 {
12814 while (true)
12815 {
12816 cp_token *token = cp_lexer_peek_token (parser->lexer);
12817
12818 if (token->type == CPP_CLOSE_BRACE
12819 || token->type == CPP_EOF)
12820 break;
12821 else
12822 cp_parser_toplevel_declaration (parser);
12823 }
12824 }
12825
12826 /* Parse a declaration.
12827
12828 declaration:
12829 block-declaration
12830 function-definition
12831 template-declaration
12832 explicit-instantiation
12833 explicit-specialization
12834 linkage-specification
12835 namespace-definition
12836
12837 C++17:
12838 deduction-guide
12839
12840 GNU extension:
12841
12842 declaration:
12843 __extension__ declaration */
12844
12845 static void
12846 cp_parser_declaration (cp_parser* parser)
12847 {
12848 cp_token token1;
12849 cp_token token2;
12850 int saved_pedantic;
12851 void *p;
12852 tree attributes = NULL_TREE;
12853
12854 /* Check for the `__extension__' keyword. */
12855 if (cp_parser_extension_opt (parser, &saved_pedantic))
12856 {
12857 /* Parse the qualified declaration. */
12858 cp_parser_declaration (parser);
12859 /* Restore the PEDANTIC flag. */
12860 pedantic = saved_pedantic;
12861
12862 return;
12863 }
12864
12865 /* Try to figure out what kind of declaration is present. */
12866 token1 = *cp_lexer_peek_token (parser->lexer);
12867
12868 if (token1.type != CPP_EOF)
12869 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12870 else
12871 {
12872 token2.type = CPP_EOF;
12873 token2.keyword = RID_MAX;
12874 }
12875
12876 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12877 p = obstack_alloc (&declarator_obstack, 0);
12878
12879 /* If the next token is `extern' and the following token is a string
12880 literal, then we have a linkage specification. */
12881 if (token1.keyword == RID_EXTERN
12882 && cp_parser_is_pure_string_literal (&token2))
12883 cp_parser_linkage_specification (parser);
12884 /* If the next token is `template', then we have either a template
12885 declaration, an explicit instantiation, or an explicit
12886 specialization. */
12887 else if (token1.keyword == RID_TEMPLATE)
12888 {
12889 /* `template <>' indicates a template specialization. */
12890 if (token2.type == CPP_LESS
12891 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12892 cp_parser_explicit_specialization (parser);
12893 /* `template <' indicates a template declaration. */
12894 else if (token2.type == CPP_LESS)
12895 cp_parser_template_declaration (parser, /*member_p=*/false);
12896 /* Anything else must be an explicit instantiation. */
12897 else
12898 cp_parser_explicit_instantiation (parser);
12899 }
12900 /* If the next token is `export', then we have a template
12901 declaration. */
12902 else if (token1.keyword == RID_EXPORT)
12903 cp_parser_template_declaration (parser, /*member_p=*/false);
12904 /* If the next token is `extern', 'static' or 'inline' and the one
12905 after that is `template', we have a GNU extended explicit
12906 instantiation directive. */
12907 else if (cp_parser_allow_gnu_extensions_p (parser)
12908 && (token1.keyword == RID_EXTERN
12909 || token1.keyword == RID_STATIC
12910 || token1.keyword == RID_INLINE)
12911 && token2.keyword == RID_TEMPLATE)
12912 cp_parser_explicit_instantiation (parser);
12913 /* If the next token is `namespace', check for a named or unnamed
12914 namespace definition. */
12915 else if (token1.keyword == RID_NAMESPACE
12916 && (/* A named namespace definition. */
12917 (token2.type == CPP_NAME
12918 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12919 != CPP_EQ))
12920 || (token2.type == CPP_OPEN_SQUARE
12921 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12922 == CPP_OPEN_SQUARE)
12923 /* An unnamed namespace definition. */
12924 || token2.type == CPP_OPEN_BRACE
12925 || token2.keyword == RID_ATTRIBUTE))
12926 cp_parser_namespace_definition (parser);
12927 /* An inline (associated) namespace definition. */
12928 else if (token1.keyword == RID_INLINE
12929 && token2.keyword == RID_NAMESPACE)
12930 cp_parser_namespace_definition (parser);
12931 /* Objective-C++ declaration/definition. */
12932 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12933 cp_parser_objc_declaration (parser, NULL_TREE);
12934 else if (c_dialect_objc ()
12935 && token1.keyword == RID_ATTRIBUTE
12936 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12937 cp_parser_objc_declaration (parser, attributes);
12938 /* At this point we may have a template declared by a concept
12939 introduction. */
12940 else if (flag_concepts
12941 && cp_parser_template_declaration_after_export (parser,
12942 /*member_p=*/false))
12943 /* We did. */;
12944 else
12945 /* Try to parse a block-declaration, or a function-definition. */
12946 cp_parser_block_declaration (parser, /*statement_p=*/false);
12947
12948 /* Free any declarators allocated. */
12949 obstack_free (&declarator_obstack, p);
12950 }
12951
12952 /* Parse a namespace-scope declaration. */
12953
12954 static void
12955 cp_parser_toplevel_declaration (cp_parser* parser)
12956 {
12957 cp_token *token = cp_lexer_peek_token (parser->lexer);
12958
12959 if (token->type == CPP_PRAGMA)
12960 /* A top-level declaration can consist solely of a #pragma. A
12961 nested declaration cannot, so this is done here and not in
12962 cp_parser_declaration. (A #pragma at block scope is
12963 handled in cp_parser_statement.) */
12964 cp_parser_pragma (parser, pragma_external, NULL);
12965 else if (token->type == CPP_SEMICOLON)
12966 {
12967 /* A declaration consisting of a single semicolon is
12968 invalid. Allow it unless we're being pedantic. */
12969 cp_lexer_consume_token (parser->lexer);
12970 if (!in_system_header_at (input_location))
12971 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12972 }
12973 else
12974 /* Parse the declaration itself. */
12975 cp_parser_declaration (parser);
12976 }
12977
12978 /* Parse a block-declaration.
12979
12980 block-declaration:
12981 simple-declaration
12982 asm-definition
12983 namespace-alias-definition
12984 using-declaration
12985 using-directive
12986
12987 GNU Extension:
12988
12989 block-declaration:
12990 __extension__ block-declaration
12991
12992 C++0x Extension:
12993
12994 block-declaration:
12995 static_assert-declaration
12996
12997 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12998 part of a declaration-statement. */
12999
13000 static void
13001 cp_parser_block_declaration (cp_parser *parser,
13002 bool statement_p)
13003 {
13004 cp_token *token1;
13005 int saved_pedantic;
13006
13007 /* Check for the `__extension__' keyword. */
13008 if (cp_parser_extension_opt (parser, &saved_pedantic))
13009 {
13010 /* Parse the qualified declaration. */
13011 cp_parser_block_declaration (parser, statement_p);
13012 /* Restore the PEDANTIC flag. */
13013 pedantic = saved_pedantic;
13014
13015 return;
13016 }
13017
13018 /* Peek at the next token to figure out which kind of declaration is
13019 present. */
13020 token1 = cp_lexer_peek_token (parser->lexer);
13021
13022 /* If the next keyword is `asm', we have an asm-definition. */
13023 if (token1->keyword == RID_ASM)
13024 {
13025 if (statement_p)
13026 cp_parser_commit_to_tentative_parse (parser);
13027 cp_parser_asm_definition (parser);
13028 }
13029 /* If the next keyword is `namespace', we have a
13030 namespace-alias-definition. */
13031 else if (token1->keyword == RID_NAMESPACE)
13032 cp_parser_namespace_alias_definition (parser);
13033 /* If the next keyword is `using', we have a
13034 using-declaration, a using-directive, or an alias-declaration. */
13035 else if (token1->keyword == RID_USING)
13036 {
13037 cp_token *token2;
13038
13039 if (statement_p)
13040 cp_parser_commit_to_tentative_parse (parser);
13041 /* If the token after `using' is `namespace', then we have a
13042 using-directive. */
13043 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13044 if (token2->keyword == RID_NAMESPACE)
13045 cp_parser_using_directive (parser);
13046 /* If the second token after 'using' is '=', then we have an
13047 alias-declaration. */
13048 else if (cxx_dialect >= cxx11
13049 && token2->type == CPP_NAME
13050 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13051 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13052 cp_parser_alias_declaration (parser);
13053 /* Otherwise, it's a using-declaration. */
13054 else
13055 cp_parser_using_declaration (parser,
13056 /*access_declaration_p=*/false);
13057 }
13058 /* If the next keyword is `__label__' we have a misplaced label
13059 declaration. */
13060 else if (token1->keyword == RID_LABEL)
13061 {
13062 cp_lexer_consume_token (parser->lexer);
13063 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13064 cp_parser_skip_to_end_of_statement (parser);
13065 /* If the next token is now a `;', consume it. */
13066 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13067 cp_lexer_consume_token (parser->lexer);
13068 }
13069 /* If the next token is `static_assert' we have a static assertion. */
13070 else if (token1->keyword == RID_STATIC_ASSERT)
13071 cp_parser_static_assert (parser, /*member_p=*/false);
13072 /* Anything else must be a simple-declaration. */
13073 else
13074 cp_parser_simple_declaration (parser, !statement_p,
13075 /*maybe_range_for_decl*/NULL);
13076 }
13077
13078 /* Parse a simple-declaration.
13079
13080 simple-declaration:
13081 decl-specifier-seq [opt] init-declarator-list [opt] ;
13082 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13083 brace-or-equal-initializer ;
13084
13085 init-declarator-list:
13086 init-declarator
13087 init-declarator-list , init-declarator
13088
13089 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13090 function-definition as a simple-declaration.
13091
13092 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13093 parsed declaration if it is an uninitialized single declarator not followed
13094 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13095 if present, will not be consumed. */
13096
13097 static void
13098 cp_parser_simple_declaration (cp_parser* parser,
13099 bool function_definition_allowed_p,
13100 tree *maybe_range_for_decl)
13101 {
13102 cp_decl_specifier_seq decl_specifiers;
13103 int declares_class_or_enum;
13104 bool saw_declarator;
13105 location_t comma_loc = UNKNOWN_LOCATION;
13106 location_t init_loc = UNKNOWN_LOCATION;
13107
13108 if (maybe_range_for_decl)
13109 *maybe_range_for_decl = NULL_TREE;
13110
13111 /* Defer access checks until we know what is being declared; the
13112 checks for names appearing in the decl-specifier-seq should be
13113 done as if we were in the scope of the thing being declared. */
13114 push_deferring_access_checks (dk_deferred);
13115
13116 /* Parse the decl-specifier-seq. We have to keep track of whether
13117 or not the decl-specifier-seq declares a named class or
13118 enumeration type, since that is the only case in which the
13119 init-declarator-list is allowed to be empty.
13120
13121 [dcl.dcl]
13122
13123 In a simple-declaration, the optional init-declarator-list can be
13124 omitted only when declaring a class or enumeration, that is when
13125 the decl-specifier-seq contains either a class-specifier, an
13126 elaborated-type-specifier, or an enum-specifier. */
13127 cp_parser_decl_specifier_seq (parser,
13128 CP_PARSER_FLAGS_OPTIONAL,
13129 &decl_specifiers,
13130 &declares_class_or_enum);
13131 /* We no longer need to defer access checks. */
13132 stop_deferring_access_checks ();
13133
13134 /* In a block scope, a valid declaration must always have a
13135 decl-specifier-seq. By not trying to parse declarators, we can
13136 resolve the declaration/expression ambiguity more quickly. */
13137 if (!function_definition_allowed_p
13138 && !decl_specifiers.any_specifiers_p)
13139 {
13140 cp_parser_error (parser, "expected declaration");
13141 goto done;
13142 }
13143
13144 /* If the next two tokens are both identifiers, the code is
13145 erroneous. The usual cause of this situation is code like:
13146
13147 T t;
13148
13149 where "T" should name a type -- but does not. */
13150 if (!decl_specifiers.any_type_specifiers_p
13151 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13152 {
13153 /* If parsing tentatively, we should commit; we really are
13154 looking at a declaration. */
13155 cp_parser_commit_to_tentative_parse (parser);
13156 /* Give up. */
13157 goto done;
13158 }
13159
13160 cp_parser_maybe_commit_to_declaration (parser,
13161 decl_specifiers.any_specifiers_p);
13162
13163 /* Look for C++17 decomposition declaration. */
13164 for (size_t n = 1; ; n++)
13165 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13166 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13167 continue;
13168 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13169 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13170 && decl_specifiers.any_specifiers_p)
13171 {
13172 tree decl
13173 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13174 maybe_range_for_decl,
13175 &init_loc);
13176
13177 /* The next token should be either a `,' or a `;'. */
13178 cp_token *token = cp_lexer_peek_token (parser->lexer);
13179 /* If it's a `;', we are done. */
13180 if (token->type == CPP_SEMICOLON)
13181 goto finish;
13182 else if (maybe_range_for_decl)
13183 {
13184 if (*maybe_range_for_decl == NULL_TREE)
13185 *maybe_range_for_decl = error_mark_node;
13186 goto finish;
13187 }
13188 /* Anything else is an error. */
13189 else
13190 {
13191 /* If we have already issued an error message we don't need
13192 to issue another one. */
13193 if ((decl != error_mark_node
13194 && DECL_INITIAL (decl) != error_mark_node)
13195 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13196 cp_parser_error (parser, "expected %<,%> or %<;%>");
13197 /* Skip tokens until we reach the end of the statement. */
13198 cp_parser_skip_to_end_of_statement (parser);
13199 /* If the next token is now a `;', consume it. */
13200 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13201 cp_lexer_consume_token (parser->lexer);
13202 goto done;
13203 }
13204 }
13205 else
13206 break;
13207
13208 tree last_type;
13209 bool auto_specifier_p;
13210 /* NULL_TREE if both variable and function declaration are allowed,
13211 error_mark_node if function declaration are not allowed and
13212 a FUNCTION_DECL that should be diagnosed if it is followed by
13213 variable declarations. */
13214 tree auto_function_declaration;
13215
13216 last_type = NULL_TREE;
13217 auto_specifier_p
13218 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13219 auto_function_declaration = NULL_TREE;
13220
13221 /* Keep going until we hit the `;' at the end of the simple
13222 declaration. */
13223 saw_declarator = false;
13224 while (cp_lexer_next_token_is_not (parser->lexer,
13225 CPP_SEMICOLON))
13226 {
13227 cp_token *token;
13228 bool function_definition_p;
13229 tree decl;
13230 tree auto_result = NULL_TREE;
13231
13232 if (saw_declarator)
13233 {
13234 /* If we are processing next declarator, comma is expected */
13235 token = cp_lexer_peek_token (parser->lexer);
13236 gcc_assert (token->type == CPP_COMMA);
13237 cp_lexer_consume_token (parser->lexer);
13238 if (maybe_range_for_decl)
13239 {
13240 *maybe_range_for_decl = error_mark_node;
13241 if (comma_loc == UNKNOWN_LOCATION)
13242 comma_loc = token->location;
13243 }
13244 }
13245 else
13246 saw_declarator = true;
13247
13248 /* Parse the init-declarator. */
13249 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13250 /*checks=*/NULL,
13251 function_definition_allowed_p,
13252 /*member_p=*/false,
13253 declares_class_or_enum,
13254 &function_definition_p,
13255 maybe_range_for_decl,
13256 &init_loc,
13257 &auto_result);
13258 /* If an error occurred while parsing tentatively, exit quickly.
13259 (That usually happens when in the body of a function; each
13260 statement is treated as a declaration-statement until proven
13261 otherwise.) */
13262 if (cp_parser_error_occurred (parser))
13263 goto done;
13264
13265 if (auto_specifier_p && cxx_dialect >= cxx14)
13266 {
13267 /* If the init-declarator-list contains more than one
13268 init-declarator, they shall all form declarations of
13269 variables. */
13270 if (auto_function_declaration == NULL_TREE)
13271 auto_function_declaration
13272 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13273 else if (TREE_CODE (decl) == FUNCTION_DECL
13274 || auto_function_declaration != error_mark_node)
13275 {
13276 error_at (decl_specifiers.locations[ds_type_spec],
13277 "non-variable %qD in declaration with more than one "
13278 "declarator with placeholder type",
13279 TREE_CODE (decl) == FUNCTION_DECL
13280 ? decl : auto_function_declaration);
13281 auto_function_declaration = error_mark_node;
13282 }
13283 }
13284
13285 if (auto_result
13286 && (!processing_template_decl || !type_uses_auto (auto_result)))
13287 {
13288 if (last_type
13289 && last_type != error_mark_node
13290 && !same_type_p (auto_result, last_type))
13291 {
13292 /* If the list of declarators contains more than one declarator,
13293 the type of each declared variable is determined as described
13294 above. If the type deduced for the template parameter U is not
13295 the same in each deduction, the program is ill-formed. */
13296 error_at (decl_specifiers.locations[ds_type_spec],
13297 "inconsistent deduction for %qT: %qT and then %qT",
13298 decl_specifiers.type, last_type, auto_result);
13299 last_type = error_mark_node;
13300 }
13301 else
13302 last_type = auto_result;
13303 }
13304
13305 /* Handle function definitions specially. */
13306 if (function_definition_p)
13307 {
13308 /* If the next token is a `,', then we are probably
13309 processing something like:
13310
13311 void f() {}, *p;
13312
13313 which is erroneous. */
13314 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13315 {
13316 cp_token *token = cp_lexer_peek_token (parser->lexer);
13317 error_at (token->location,
13318 "mixing"
13319 " declarations and function-definitions is forbidden");
13320 }
13321 /* Otherwise, we're done with the list of declarators. */
13322 else
13323 {
13324 pop_deferring_access_checks ();
13325 return;
13326 }
13327 }
13328 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13329 *maybe_range_for_decl = decl;
13330 /* The next token should be either a `,' or a `;'. */
13331 token = cp_lexer_peek_token (parser->lexer);
13332 /* If it's a `,', there are more declarators to come. */
13333 if (token->type == CPP_COMMA)
13334 /* will be consumed next time around */;
13335 /* If it's a `;', we are done. */
13336 else if (token->type == CPP_SEMICOLON)
13337 break;
13338 else if (maybe_range_for_decl)
13339 {
13340 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13341 permerror (decl_specifiers.locations[ds_type_spec],
13342 "types may not be defined in a for-range-declaration");
13343 break;
13344 }
13345 /* Anything else is an error. */
13346 else
13347 {
13348 /* If we have already issued an error message we don't need
13349 to issue another one. */
13350 if ((decl != error_mark_node
13351 && DECL_INITIAL (decl) != error_mark_node)
13352 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13353 cp_parser_error (parser, "expected %<,%> or %<;%>");
13354 /* Skip tokens until we reach the end of the statement. */
13355 cp_parser_skip_to_end_of_statement (parser);
13356 /* If the next token is now a `;', consume it. */
13357 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13358 cp_lexer_consume_token (parser->lexer);
13359 goto done;
13360 }
13361 /* After the first time around, a function-definition is not
13362 allowed -- even if it was OK at first. For example:
13363
13364 int i, f() {}
13365
13366 is not valid. */
13367 function_definition_allowed_p = false;
13368 }
13369
13370 /* Issue an error message if no declarators are present, and the
13371 decl-specifier-seq does not itself declare a class or
13372 enumeration: [dcl.dcl]/3. */
13373 if (!saw_declarator)
13374 {
13375 if (cp_parser_declares_only_class_p (parser))
13376 {
13377 if (!declares_class_or_enum
13378 && decl_specifiers.type
13379 && OVERLOAD_TYPE_P (decl_specifiers.type))
13380 /* Ensure an error is issued anyway when finish_decltype_type,
13381 called via cp_parser_decl_specifier_seq, returns a class or
13382 an enumeration (c++/51786). */
13383 decl_specifiers.type = NULL_TREE;
13384 shadow_tag (&decl_specifiers);
13385 }
13386 /* Perform any deferred access checks. */
13387 perform_deferred_access_checks (tf_warning_or_error);
13388 }
13389
13390 /* Consume the `;'. */
13391 finish:
13392 if (!maybe_range_for_decl)
13393 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13394 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13395 {
13396 if (init_loc != UNKNOWN_LOCATION)
13397 error_at (init_loc, "initializer in range-based %<for%> loop");
13398 if (comma_loc != UNKNOWN_LOCATION)
13399 error_at (comma_loc,
13400 "multiple declarations in range-based %<for%> loop");
13401 }
13402
13403 done:
13404 pop_deferring_access_checks ();
13405 }
13406
13407 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13408 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13409 initializer ; */
13410
13411 static tree
13412 cp_parser_decomposition_declaration (cp_parser *parser,
13413 cp_decl_specifier_seq *decl_specifiers,
13414 tree *maybe_range_for_decl,
13415 location_t *init_loc)
13416 {
13417 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13418 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13419 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13420
13421 /* Parse the identifier-list. */
13422 auto_vec<cp_expr, 10> v;
13423 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13424 while (true)
13425 {
13426 cp_expr e = cp_parser_identifier (parser);
13427 if (e.get_value () == error_mark_node)
13428 break;
13429 v.safe_push (e);
13430 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13431 break;
13432 cp_lexer_consume_token (parser->lexer);
13433 }
13434
13435 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13436 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13437 {
13438 end_loc = UNKNOWN_LOCATION;
13439 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13440 false);
13441 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13442 cp_lexer_consume_token (parser->lexer);
13443 else
13444 {
13445 cp_parser_skip_to_end_of_statement (parser);
13446 return error_mark_node;
13447 }
13448 }
13449
13450 if (cxx_dialect < cxx17)
13451 pedwarn (loc, 0, "structured bindings only available with "
13452 "-std=c++17 or -std=gnu++17");
13453
13454 tree pushed_scope;
13455 cp_declarator *declarator = make_declarator (cdk_decomp);
13456 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13457 declarator->id_loc = loc;
13458 if (ref_qual != REF_QUAL_NONE)
13459 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13460 ref_qual == REF_QUAL_RVALUE,
13461 NULL_TREE);
13462 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13463 NULL_TREE, decl_specifiers->attributes,
13464 &pushed_scope);
13465 tree orig_decl = decl;
13466
13467 unsigned int i;
13468 cp_expr e;
13469 cp_decl_specifier_seq decl_specs;
13470 clear_decl_specs (&decl_specs);
13471 decl_specs.type = make_auto ();
13472 tree prev = decl;
13473 FOR_EACH_VEC_ELT (v, i, e)
13474 {
13475 if (i == 0)
13476 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13477 else
13478 declarator->u.id.unqualified_name = e.get_value ();
13479 declarator->id_loc = e.get_location ();
13480 tree elt_pushed_scope;
13481 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13482 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13483 if (decl2 == error_mark_node)
13484 decl = error_mark_node;
13485 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13486 {
13487 /* Ensure we've diagnosed redeclaration if we aren't creating
13488 a new VAR_DECL. */
13489 gcc_assert (errorcount);
13490 decl = error_mark_node;
13491 }
13492 else
13493 prev = decl2;
13494 if (elt_pushed_scope)
13495 pop_scope (elt_pushed_scope);
13496 }
13497
13498 if (v.is_empty ())
13499 {
13500 error_at (loc, "empty structured binding declaration");
13501 decl = error_mark_node;
13502 }
13503
13504 if (maybe_range_for_decl == NULL
13505 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13506 {
13507 bool non_constant_p = false, is_direct_init = false;
13508 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13509 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13510 &non_constant_p);
13511 if (initializer == NULL_TREE
13512 || (TREE_CODE (initializer) == TREE_LIST
13513 && TREE_CHAIN (initializer))
13514 || (is_direct_init
13515 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13516 && CONSTRUCTOR_NELTS (initializer) != 1))
13517 {
13518 error_at (loc, "invalid initializer for structured binding "
13519 "declaration");
13520 initializer = error_mark_node;
13521 }
13522
13523 if (decl != error_mark_node)
13524 {
13525 cp_maybe_mangle_decomp (decl, prev, v.length ());
13526 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13527 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13528 cp_finish_decomp (decl, prev, v.length ());
13529 }
13530 }
13531 else if (decl != error_mark_node)
13532 {
13533 *maybe_range_for_decl = prev;
13534 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13535 the underlying DECL. */
13536 cp_finish_decomp (decl, prev, v.length ());
13537 }
13538
13539 if (pushed_scope)
13540 pop_scope (pushed_scope);
13541
13542 if (decl == error_mark_node && DECL_P (orig_decl))
13543 {
13544 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13545 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13546 }
13547
13548 return decl;
13549 }
13550
13551 /* Parse a decl-specifier-seq.
13552
13553 decl-specifier-seq:
13554 decl-specifier-seq [opt] decl-specifier
13555 decl-specifier attribute-specifier-seq [opt] (C++11)
13556
13557 decl-specifier:
13558 storage-class-specifier
13559 type-specifier
13560 function-specifier
13561 friend
13562 typedef
13563
13564 GNU Extension:
13565
13566 decl-specifier:
13567 attributes
13568
13569 Concepts Extension:
13570
13571 decl-specifier:
13572 concept
13573
13574 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13575
13576 The parser flags FLAGS is used to control type-specifier parsing.
13577
13578 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13579 flags:
13580
13581 1: one of the decl-specifiers is an elaborated-type-specifier
13582 (i.e., a type declaration)
13583 2: one of the decl-specifiers is an enum-specifier or a
13584 class-specifier (i.e., a type definition)
13585
13586 */
13587
13588 static void
13589 cp_parser_decl_specifier_seq (cp_parser* parser,
13590 cp_parser_flags flags,
13591 cp_decl_specifier_seq *decl_specs,
13592 int* declares_class_or_enum)
13593 {
13594 bool constructor_possible_p = !parser->in_declarator_p;
13595 bool found_decl_spec = false;
13596 cp_token *start_token = NULL;
13597 cp_decl_spec ds;
13598
13599 /* Clear DECL_SPECS. */
13600 clear_decl_specs (decl_specs);
13601
13602 /* Assume no class or enumeration type is declared. */
13603 *declares_class_or_enum = 0;
13604
13605 /* Keep reading specifiers until there are no more to read. */
13606 while (true)
13607 {
13608 bool constructor_p;
13609 cp_token *token;
13610 ds = ds_last;
13611
13612 /* Peek at the next token. */
13613 token = cp_lexer_peek_token (parser->lexer);
13614
13615 /* Save the first token of the decl spec list for error
13616 reporting. */
13617 if (!start_token)
13618 start_token = token;
13619 /* Handle attributes. */
13620 if (cp_next_tokens_can_be_attribute_p (parser))
13621 {
13622 /* Parse the attributes. */
13623 tree attrs = cp_parser_attributes_opt (parser);
13624
13625 /* In a sequence of declaration specifiers, c++11 attributes
13626 appertain to the type that precede them. In that case
13627 [dcl.spec]/1 says:
13628
13629 The attribute-specifier-seq affects the type only for
13630 the declaration it appears in, not other declarations
13631 involving the same type.
13632
13633 But for now let's force the user to position the
13634 attribute either at the beginning of the declaration or
13635 after the declarator-id, which would clearly mean that it
13636 applies to the declarator. */
13637 if (cxx11_attribute_p (attrs))
13638 {
13639 if (!found_decl_spec)
13640 /* The c++11 attribute is at the beginning of the
13641 declaration. It appertains to the entity being
13642 declared. */;
13643 else
13644 {
13645 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13646 {
13647 /* This is an attribute following a
13648 class-specifier. */
13649 if (decl_specs->type_definition_p)
13650 warn_misplaced_attr_for_class_type (token->location,
13651 decl_specs->type);
13652 attrs = NULL_TREE;
13653 }
13654 else
13655 {
13656 decl_specs->std_attributes
13657 = attr_chainon (decl_specs->std_attributes, attrs);
13658 if (decl_specs->locations[ds_std_attribute] == 0)
13659 decl_specs->locations[ds_std_attribute] = token->location;
13660 }
13661 continue;
13662 }
13663 }
13664
13665 decl_specs->attributes
13666 = attr_chainon (decl_specs->attributes, attrs);
13667 if (decl_specs->locations[ds_attribute] == 0)
13668 decl_specs->locations[ds_attribute] = token->location;
13669 continue;
13670 }
13671 /* Assume we will find a decl-specifier keyword. */
13672 found_decl_spec = true;
13673 /* If the next token is an appropriate keyword, we can simply
13674 add it to the list. */
13675 switch (token->keyword)
13676 {
13677 /* decl-specifier:
13678 friend
13679 constexpr */
13680 case RID_FRIEND:
13681 if (!at_class_scope_p ())
13682 {
13683 gcc_rich_location richloc (token->location);
13684 richloc.add_fixit_remove ();
13685 error_at (&richloc, "%<friend%> used outside of class");
13686 cp_lexer_purge_token (parser->lexer);
13687 }
13688 else
13689 {
13690 ds = ds_friend;
13691 /* Consume the token. */
13692 cp_lexer_consume_token (parser->lexer);
13693 }
13694 break;
13695
13696 case RID_CONSTEXPR:
13697 ds = ds_constexpr;
13698 cp_lexer_consume_token (parser->lexer);
13699 break;
13700
13701 case RID_CONCEPT:
13702 ds = ds_concept;
13703 cp_lexer_consume_token (parser->lexer);
13704 break;
13705
13706 /* function-specifier:
13707 inline
13708 virtual
13709 explicit */
13710 case RID_INLINE:
13711 case RID_VIRTUAL:
13712 case RID_EXPLICIT:
13713 cp_parser_function_specifier_opt (parser, decl_specs);
13714 break;
13715
13716 /* decl-specifier:
13717 typedef */
13718 case RID_TYPEDEF:
13719 ds = ds_typedef;
13720 /* Consume the token. */
13721 cp_lexer_consume_token (parser->lexer);
13722 /* A constructor declarator cannot appear in a typedef. */
13723 constructor_possible_p = false;
13724 /* The "typedef" keyword can only occur in a declaration; we
13725 may as well commit at this point. */
13726 cp_parser_commit_to_tentative_parse (parser);
13727
13728 if (decl_specs->storage_class != sc_none)
13729 decl_specs->conflicting_specifiers_p = true;
13730 break;
13731
13732 /* storage-class-specifier:
13733 auto
13734 register
13735 static
13736 extern
13737 mutable
13738
13739 GNU Extension:
13740 thread */
13741 case RID_AUTO:
13742 if (cxx_dialect == cxx98)
13743 {
13744 /* Consume the token. */
13745 cp_lexer_consume_token (parser->lexer);
13746
13747 /* Complain about `auto' as a storage specifier, if
13748 we're complaining about C++0x compatibility. */
13749 gcc_rich_location richloc (token->location);
13750 richloc.add_fixit_remove ();
13751 warning_at (&richloc, OPT_Wc__11_compat,
13752 "%<auto%> changes meaning in C++11; "
13753 "please remove it");
13754
13755 /* Set the storage class anyway. */
13756 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13757 token);
13758 }
13759 else
13760 /* C++0x auto type-specifier. */
13761 found_decl_spec = false;
13762 break;
13763
13764 case RID_REGISTER:
13765 case RID_STATIC:
13766 case RID_EXTERN:
13767 case RID_MUTABLE:
13768 /* Consume the token. */
13769 cp_lexer_consume_token (parser->lexer);
13770 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13771 token);
13772 break;
13773 case RID_THREAD:
13774 /* Consume the token. */
13775 ds = ds_thread;
13776 cp_lexer_consume_token (parser->lexer);
13777 break;
13778
13779 default:
13780 /* We did not yet find a decl-specifier yet. */
13781 found_decl_spec = false;
13782 break;
13783 }
13784
13785 if (found_decl_spec
13786 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13787 && token->keyword != RID_CONSTEXPR)
13788 error ("decl-specifier invalid in condition");
13789
13790 if (found_decl_spec
13791 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13792 && token->keyword != RID_MUTABLE
13793 && token->keyword != RID_CONSTEXPR)
13794 error_at (token->location, "%qD invalid in lambda",
13795 ridpointers[token->keyword]);
13796
13797 if (ds != ds_last)
13798 set_and_check_decl_spec_loc (decl_specs, ds, token);
13799
13800 /* Constructors are a special case. The `S' in `S()' is not a
13801 decl-specifier; it is the beginning of the declarator. */
13802 constructor_p
13803 = (!found_decl_spec
13804 && constructor_possible_p
13805 && (cp_parser_constructor_declarator_p
13806 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13807
13808 /* If we don't have a DECL_SPEC yet, then we must be looking at
13809 a type-specifier. */
13810 if (!found_decl_spec && !constructor_p)
13811 {
13812 int decl_spec_declares_class_or_enum;
13813 bool is_cv_qualifier;
13814 tree type_spec;
13815
13816 type_spec
13817 = cp_parser_type_specifier (parser, flags,
13818 decl_specs,
13819 /*is_declaration=*/true,
13820 &decl_spec_declares_class_or_enum,
13821 &is_cv_qualifier);
13822 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13823
13824 /* If this type-specifier referenced a user-defined type
13825 (a typedef, class-name, etc.), then we can't allow any
13826 more such type-specifiers henceforth.
13827
13828 [dcl.spec]
13829
13830 The longest sequence of decl-specifiers that could
13831 possibly be a type name is taken as the
13832 decl-specifier-seq of a declaration. The sequence shall
13833 be self-consistent as described below.
13834
13835 [dcl.type]
13836
13837 As a general rule, at most one type-specifier is allowed
13838 in the complete decl-specifier-seq of a declaration. The
13839 only exceptions are the following:
13840
13841 -- const or volatile can be combined with any other
13842 type-specifier.
13843
13844 -- signed or unsigned can be combined with char, long,
13845 short, or int.
13846
13847 -- ..
13848
13849 Example:
13850
13851 typedef char* Pc;
13852 void g (const int Pc);
13853
13854 Here, Pc is *not* part of the decl-specifier seq; it's
13855 the declarator. Therefore, once we see a type-specifier
13856 (other than a cv-qualifier), we forbid any additional
13857 user-defined types. We *do* still allow things like `int
13858 int' to be considered a decl-specifier-seq, and issue the
13859 error message later. */
13860 if (type_spec && !is_cv_qualifier)
13861 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13862 /* A constructor declarator cannot follow a type-specifier. */
13863 if (type_spec)
13864 {
13865 constructor_possible_p = false;
13866 found_decl_spec = true;
13867 if (!is_cv_qualifier)
13868 decl_specs->any_type_specifiers_p = true;
13869
13870 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13871 error_at (token->location, "type-specifier invalid in lambda");
13872 }
13873 }
13874
13875 /* If we still do not have a DECL_SPEC, then there are no more
13876 decl-specifiers. */
13877 if (!found_decl_spec)
13878 break;
13879
13880 decl_specs->any_specifiers_p = true;
13881 /* After we see one decl-specifier, further decl-specifiers are
13882 always optional. */
13883 flags |= CP_PARSER_FLAGS_OPTIONAL;
13884 }
13885
13886 /* Don't allow a friend specifier with a class definition. */
13887 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13888 && (*declares_class_or_enum & 2))
13889 error_at (decl_specs->locations[ds_friend],
13890 "class definition may not be declared a friend");
13891 }
13892
13893 /* Parse an (optional) storage-class-specifier.
13894
13895 storage-class-specifier:
13896 auto
13897 register
13898 static
13899 extern
13900 mutable
13901
13902 GNU Extension:
13903
13904 storage-class-specifier:
13905 thread
13906
13907 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13908
13909 static tree
13910 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13911 {
13912 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13913 {
13914 case RID_AUTO:
13915 if (cxx_dialect != cxx98)
13916 return NULL_TREE;
13917 /* Fall through for C++98. */
13918 gcc_fallthrough ();
13919
13920 case RID_REGISTER:
13921 case RID_STATIC:
13922 case RID_EXTERN:
13923 case RID_MUTABLE:
13924 case RID_THREAD:
13925 /* Consume the token. */
13926 return cp_lexer_consume_token (parser->lexer)->u.value;
13927
13928 default:
13929 return NULL_TREE;
13930 }
13931 }
13932
13933 /* Parse an (optional) function-specifier.
13934
13935 function-specifier:
13936 inline
13937 virtual
13938 explicit
13939
13940 C++2A Extension:
13941 explicit(constant-expression)
13942
13943 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13944 Updates DECL_SPECS, if it is non-NULL. */
13945
13946 static tree
13947 cp_parser_function_specifier_opt (cp_parser* parser,
13948 cp_decl_specifier_seq *decl_specs)
13949 {
13950 cp_token *token = cp_lexer_peek_token (parser->lexer);
13951 switch (token->keyword)
13952 {
13953 case RID_INLINE:
13954 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13955 break;
13956
13957 case RID_VIRTUAL:
13958 /* 14.5.2.3 [temp.mem]
13959
13960 A member function template shall not be virtual. */
13961 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13962 && current_class_type)
13963 error_at (token->location, "templates may not be %<virtual%>");
13964 else
13965 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13966 break;
13967
13968 case RID_EXPLICIT:
13969 {
13970 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
13971 /* If we see '(', it's C++20 explicit(bool). */
13972 tree expr;
13973 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13974 {
13975 matching_parens parens;
13976 parens.consume_open (parser);
13977
13978 /* New types are not allowed in an explicit-specifier. */
13979 const char *saved_message
13980 = parser->type_definition_forbidden_message;
13981 parser->type_definition_forbidden_message
13982 = G_("types may not be defined in explicit-specifier");
13983
13984 if (cxx_dialect < cxx2a)
13985 pedwarn (token->location, 0,
13986 "%<explicit(bool)%> only available with -std=c++2a "
13987 "or -std=gnu++2a");
13988
13989 /* Parse the constant-expression. */
13990 expr = cp_parser_constant_expression (parser);
13991
13992 /* Restore the saved message. */
13993 parser->type_definition_forbidden_message = saved_message;
13994 parens.require_close (parser);
13995 }
13996 else
13997 /* The explicit-specifier explicit without a constant-expression is
13998 equivalent to the explicit-specifier explicit(true). */
13999 expr = boolean_true_node;
14000
14001 /* [dcl.fct.spec]
14002 "the constant-expression, if supplied, shall be a contextually
14003 converted constant expression of type bool." */
14004 expr = build_explicit_specifier (expr, tf_warning_or_error);
14005 /* We could evaluate it -- mark the decl as appropriate. */
14006 if (expr == boolean_true_node)
14007 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14008 else if (expr == boolean_false_node)
14009 /* Don't mark the decl as explicit. */;
14010 else if (decl_specs)
14011 /* The expression was value-dependent. Remember it so that we can
14012 substitute it later. */
14013 decl_specs->explicit_specifier = expr;
14014 return id;
14015 }
14016
14017 default:
14018 return NULL_TREE;
14019 }
14020
14021 /* Consume the token. */
14022 return cp_lexer_consume_token (parser->lexer)->u.value;
14023 }
14024
14025 /* Parse a linkage-specification.
14026
14027 linkage-specification:
14028 extern string-literal { declaration-seq [opt] }
14029 extern string-literal declaration */
14030
14031 static void
14032 cp_parser_linkage_specification (cp_parser* parser)
14033 {
14034 tree linkage;
14035
14036 /* Look for the `extern' keyword. */
14037 cp_token *extern_token
14038 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14039
14040 /* Look for the string-literal. */
14041 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14042 linkage = cp_parser_string_literal (parser, false, false);
14043
14044 /* Transform the literal into an identifier. If the literal is a
14045 wide-character string, or contains embedded NULs, then we can't
14046 handle it as the user wants. */
14047 if (strlen (TREE_STRING_POINTER (linkage))
14048 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14049 {
14050 cp_parser_error (parser, "invalid linkage-specification");
14051 /* Assume C++ linkage. */
14052 linkage = lang_name_cplusplus;
14053 }
14054 else
14055 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14056
14057 /* We're now using the new linkage. */
14058 push_lang_context (linkage);
14059
14060 /* Preserve the location of the the innermost linkage specification,
14061 tracking the locations of nested specifications via a local. */
14062 location_t saved_location
14063 = parser->innermost_linkage_specification_location;
14064 /* Construct a location ranging from the start of the "extern" to
14065 the end of the string-literal, with the caret at the start, e.g.:
14066 extern "C" {
14067 ^~~~~~~~~~
14068 */
14069 parser->innermost_linkage_specification_location
14070 = make_location (extern_token->location,
14071 extern_token->location,
14072 get_finish (string_token->location));
14073
14074 /* If the next token is a `{', then we're using the first
14075 production. */
14076 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14077 {
14078 cp_ensure_no_omp_declare_simd (parser);
14079 cp_ensure_no_oacc_routine (parser);
14080
14081 /* Consume the `{' token. */
14082 matching_braces braces;
14083 braces.consume_open (parser)->location;
14084 /* Parse the declarations. */
14085 cp_parser_declaration_seq_opt (parser);
14086 /* Look for the closing `}'. */
14087 braces.require_close (parser);
14088 }
14089 /* Otherwise, there's just one declaration. */
14090 else
14091 {
14092 bool saved_in_unbraced_linkage_specification_p;
14093
14094 saved_in_unbraced_linkage_specification_p
14095 = parser->in_unbraced_linkage_specification_p;
14096 parser->in_unbraced_linkage_specification_p = true;
14097 cp_parser_declaration (parser);
14098 parser->in_unbraced_linkage_specification_p
14099 = saved_in_unbraced_linkage_specification_p;
14100 }
14101
14102 /* We're done with the linkage-specification. */
14103 pop_lang_context ();
14104
14105 /* Restore location of parent linkage specification, if any. */
14106 parser->innermost_linkage_specification_location = saved_location;
14107 }
14108
14109 /* Parse a static_assert-declaration.
14110
14111 static_assert-declaration:
14112 static_assert ( constant-expression , string-literal ) ;
14113 static_assert ( constant-expression ) ; (C++17)
14114
14115 If MEMBER_P, this static_assert is a class member. */
14116
14117 static void
14118 cp_parser_static_assert(cp_parser *parser, bool member_p)
14119 {
14120 cp_expr condition;
14121 location_t token_loc;
14122 tree message;
14123 bool dummy;
14124
14125 /* Peek at the `static_assert' token so we can keep track of exactly
14126 where the static assertion started. */
14127 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14128
14129 /* Look for the `static_assert' keyword. */
14130 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14131 RT_STATIC_ASSERT))
14132 return;
14133
14134 /* We know we are in a static assertion; commit to any tentative
14135 parse. */
14136 if (cp_parser_parsing_tentatively (parser))
14137 cp_parser_commit_to_tentative_parse (parser);
14138
14139 /* Parse the `(' starting the static assertion condition. */
14140 matching_parens parens;
14141 parens.require_open (parser);
14142
14143 /* Parse the constant-expression. Allow a non-constant expression
14144 here in order to give better diagnostics in finish_static_assert. */
14145 condition =
14146 cp_parser_constant_expression (parser,
14147 /*allow_non_constant_p=*/true,
14148 /*non_constant_p=*/&dummy);
14149
14150 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14151 {
14152 if (cxx_dialect < cxx17)
14153 pedwarn (input_location, OPT_Wpedantic,
14154 "static_assert without a message "
14155 "only available with -std=c++17 or -std=gnu++17");
14156 /* Eat the ')' */
14157 cp_lexer_consume_token (parser->lexer);
14158 message = build_string (1, "");
14159 TREE_TYPE (message) = char_array_type_node;
14160 fix_string_type (message);
14161 }
14162 else
14163 {
14164 /* Parse the separating `,'. */
14165 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14166
14167 /* Parse the string-literal message. */
14168 message = cp_parser_string_literal (parser,
14169 /*translate=*/false,
14170 /*wide_ok=*/true);
14171
14172 /* A `)' completes the static assertion. */
14173 if (!parens.require_close (parser))
14174 cp_parser_skip_to_closing_parenthesis (parser,
14175 /*recovering=*/true,
14176 /*or_comma=*/false,
14177 /*consume_paren=*/true);
14178 }
14179
14180 /* A semicolon terminates the declaration. */
14181 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14182
14183 /* Get the location for the static assertion. Use that of the
14184 condition if available, otherwise, use that of the "static_assert"
14185 token. */
14186 location_t assert_loc = condition.get_location ();
14187 if (assert_loc == UNKNOWN_LOCATION)
14188 assert_loc = token_loc;
14189
14190 /* Complete the static assertion, which may mean either processing
14191 the static assert now or saving it for template instantiation. */
14192 finish_static_assert (condition, message, assert_loc, member_p);
14193 }
14194
14195 /* Parse the expression in decltype ( expression ). */
14196
14197 static tree
14198 cp_parser_decltype_expr (cp_parser *parser,
14199 bool &id_expression_or_member_access_p)
14200 {
14201 cp_token *id_expr_start_token;
14202 tree expr;
14203
14204 /* Since we're going to preserve any side-effects from this parse, set up a
14205 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14206 in the expression. */
14207 tentative_firewall firewall (parser);
14208
14209 /* First, try parsing an id-expression. */
14210 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14211 cp_parser_parse_tentatively (parser);
14212 expr = cp_parser_id_expression (parser,
14213 /*template_keyword_p=*/false,
14214 /*check_dependency_p=*/true,
14215 /*template_p=*/NULL,
14216 /*declarator_p=*/false,
14217 /*optional_p=*/false);
14218
14219 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14220 {
14221 bool non_integral_constant_expression_p = false;
14222 tree id_expression = expr;
14223 cp_id_kind idk;
14224 const char *error_msg;
14225
14226 if (identifier_p (expr))
14227 /* Lookup the name we got back from the id-expression. */
14228 expr = cp_parser_lookup_name_simple (parser, expr,
14229 id_expr_start_token->location);
14230
14231 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14232 /* A template without args is not a complete id-expression. */
14233 expr = error_mark_node;
14234
14235 if (expr
14236 && expr != error_mark_node
14237 && TREE_CODE (expr) != TYPE_DECL
14238 && (TREE_CODE (expr) != BIT_NOT_EXPR
14239 || !TYPE_P (TREE_OPERAND (expr, 0)))
14240 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14241 {
14242 /* Complete lookup of the id-expression. */
14243 expr = (finish_id_expression
14244 (id_expression, expr, parser->scope, &idk,
14245 /*integral_constant_expression_p=*/false,
14246 /*allow_non_integral_constant_expression_p=*/true,
14247 &non_integral_constant_expression_p,
14248 /*template_p=*/false,
14249 /*done=*/true,
14250 /*address_p=*/false,
14251 /*template_arg_p=*/false,
14252 &error_msg,
14253 id_expr_start_token->location));
14254
14255 if (expr == error_mark_node)
14256 /* We found an id-expression, but it was something that we
14257 should not have found. This is an error, not something
14258 we can recover from, so note that we found an
14259 id-expression and we'll recover as gracefully as
14260 possible. */
14261 id_expression_or_member_access_p = true;
14262 }
14263
14264 if (expr
14265 && expr != error_mark_node
14266 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14267 /* We have an id-expression. */
14268 id_expression_or_member_access_p = true;
14269 }
14270
14271 if (!id_expression_or_member_access_p)
14272 {
14273 /* Abort the id-expression parse. */
14274 cp_parser_abort_tentative_parse (parser);
14275
14276 /* Parsing tentatively, again. */
14277 cp_parser_parse_tentatively (parser);
14278
14279 /* Parse a class member access. */
14280 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14281 /*cast_p=*/false, /*decltype*/true,
14282 /*member_access_only_p=*/true, NULL);
14283
14284 if (expr
14285 && expr != error_mark_node
14286 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14287 /* We have an id-expression. */
14288 id_expression_or_member_access_p = true;
14289 }
14290
14291 if (id_expression_or_member_access_p)
14292 /* We have parsed the complete id-expression or member access. */
14293 cp_parser_parse_definitely (parser);
14294 else
14295 {
14296 /* Abort our attempt to parse an id-expression or member access
14297 expression. */
14298 cp_parser_abort_tentative_parse (parser);
14299
14300 /* Commit to the tentative_firewall so we get syntax errors. */
14301 cp_parser_commit_to_tentative_parse (parser);
14302
14303 /* Parse a full expression. */
14304 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14305 /*decltype_p=*/true);
14306 }
14307
14308 return expr;
14309 }
14310
14311 /* Parse a `decltype' type. Returns the type.
14312
14313 simple-type-specifier:
14314 decltype ( expression )
14315 C++14 proposal:
14316 decltype ( auto ) */
14317
14318 static tree
14319 cp_parser_decltype (cp_parser *parser)
14320 {
14321 bool id_expression_or_member_access_p = false;
14322 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14323
14324 if (start_token->type == CPP_DECLTYPE)
14325 {
14326 /* Already parsed. */
14327 cp_lexer_consume_token (parser->lexer);
14328 return saved_checks_value (start_token->u.tree_check_value);
14329 }
14330
14331 /* Look for the `decltype' token. */
14332 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14333 return error_mark_node;
14334
14335 /* Parse the opening `('. */
14336 matching_parens parens;
14337 if (!parens.require_open (parser))
14338 return error_mark_node;
14339
14340 push_deferring_access_checks (dk_deferred);
14341
14342 tree expr = NULL_TREE;
14343
14344 if (cxx_dialect >= cxx14
14345 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14346 /* decltype (auto) */
14347 cp_lexer_consume_token (parser->lexer);
14348 else
14349 {
14350 /* decltype (expression) */
14351
14352 /* Types cannot be defined in a `decltype' expression. Save away the
14353 old message and set the new one. */
14354 const char *saved_message = parser->type_definition_forbidden_message;
14355 parser->type_definition_forbidden_message
14356 = G_("types may not be defined in %<decltype%> expressions");
14357
14358 /* The restrictions on constant-expressions do not apply inside
14359 decltype expressions. */
14360 bool saved_integral_constant_expression_p
14361 = parser->integral_constant_expression_p;
14362 bool saved_non_integral_constant_expression_p
14363 = parser->non_integral_constant_expression_p;
14364 parser->integral_constant_expression_p = false;
14365
14366 /* Within a parenthesized expression, a `>' token is always
14367 the greater-than operator. */
14368 bool saved_greater_than_is_operator_p
14369 = parser->greater_than_is_operator_p;
14370 parser->greater_than_is_operator_p = true;
14371
14372 /* Do not actually evaluate the expression. */
14373 ++cp_unevaluated_operand;
14374
14375 /* Do not warn about problems with the expression. */
14376 ++c_inhibit_evaluation_warnings;
14377
14378 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14379
14380 /* Go back to evaluating expressions. */
14381 --cp_unevaluated_operand;
14382 --c_inhibit_evaluation_warnings;
14383
14384 /* The `>' token might be the end of a template-id or
14385 template-parameter-list now. */
14386 parser->greater_than_is_operator_p
14387 = saved_greater_than_is_operator_p;
14388
14389 /* Restore the old message and the integral constant expression
14390 flags. */
14391 parser->type_definition_forbidden_message = saved_message;
14392 parser->integral_constant_expression_p
14393 = saved_integral_constant_expression_p;
14394 parser->non_integral_constant_expression_p
14395 = saved_non_integral_constant_expression_p;
14396 }
14397
14398 /* Parse to the closing `)'. */
14399 if (!parens.require_close (parser))
14400 {
14401 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14402 /*consume_paren=*/true);
14403 pop_deferring_access_checks ();
14404 return error_mark_node;
14405 }
14406
14407 if (!expr)
14408 {
14409 /* Build auto. */
14410 expr = make_decltype_auto ();
14411 AUTO_IS_DECLTYPE (expr) = true;
14412 }
14413 else
14414 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14415 tf_warning_or_error);
14416
14417 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14418 it again. */
14419 start_token->type = CPP_DECLTYPE;
14420 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14421 start_token->u.tree_check_value->value = expr;
14422 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14423 start_token->keyword = RID_MAX;
14424 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14425
14426 pop_to_parent_deferring_access_checks ();
14427
14428 return expr;
14429 }
14430
14431 /* Special member functions [gram.special] */
14432
14433 /* Parse a conversion-function-id.
14434
14435 conversion-function-id:
14436 operator conversion-type-id
14437
14438 Returns an IDENTIFIER_NODE representing the operator. */
14439
14440 static tree
14441 cp_parser_conversion_function_id (cp_parser* parser)
14442 {
14443 tree type;
14444 tree saved_scope;
14445 tree saved_qualifying_scope;
14446 tree saved_object_scope;
14447 tree pushed_scope = NULL_TREE;
14448
14449 /* Look for the `operator' token. */
14450 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14451 return error_mark_node;
14452 /* When we parse the conversion-type-id, the current scope will be
14453 reset. However, we need that information in able to look up the
14454 conversion function later, so we save it here. */
14455 saved_scope = parser->scope;
14456 saved_qualifying_scope = parser->qualifying_scope;
14457 saved_object_scope = parser->object_scope;
14458 /* We must enter the scope of the class so that the names of
14459 entities declared within the class are available in the
14460 conversion-type-id. For example, consider:
14461
14462 struct S {
14463 typedef int I;
14464 operator I();
14465 };
14466
14467 S::operator I() { ... }
14468
14469 In order to see that `I' is a type-name in the definition, we
14470 must be in the scope of `S'. */
14471 if (saved_scope)
14472 pushed_scope = push_scope (saved_scope);
14473 /* Parse the conversion-type-id. */
14474 type = cp_parser_conversion_type_id (parser);
14475 /* Leave the scope of the class, if any. */
14476 if (pushed_scope)
14477 pop_scope (pushed_scope);
14478 /* Restore the saved scope. */
14479 parser->scope = saved_scope;
14480 parser->qualifying_scope = saved_qualifying_scope;
14481 parser->object_scope = saved_object_scope;
14482 /* If the TYPE is invalid, indicate failure. */
14483 if (type == error_mark_node)
14484 return error_mark_node;
14485 return make_conv_op_name (type);
14486 }
14487
14488 /* Parse a conversion-type-id:
14489
14490 conversion-type-id:
14491 type-specifier-seq conversion-declarator [opt]
14492
14493 Returns the TYPE specified. */
14494
14495 static tree
14496 cp_parser_conversion_type_id (cp_parser* parser)
14497 {
14498 tree attributes;
14499 cp_decl_specifier_seq type_specifiers;
14500 cp_declarator *declarator;
14501 tree type_specified;
14502 const char *saved_message;
14503
14504 /* Parse the attributes. */
14505 attributes = cp_parser_attributes_opt (parser);
14506
14507 saved_message = parser->type_definition_forbidden_message;
14508 parser->type_definition_forbidden_message
14509 = G_("types may not be defined in a conversion-type-id");
14510
14511 /* Parse the type-specifiers. */
14512 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14513 /*is_trailing_return=*/false,
14514 &type_specifiers);
14515
14516 parser->type_definition_forbidden_message = saved_message;
14517
14518 /* If that didn't work, stop. */
14519 if (type_specifiers.type == error_mark_node)
14520 return error_mark_node;
14521 /* Parse the conversion-declarator. */
14522 declarator = cp_parser_conversion_declarator_opt (parser);
14523
14524 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14525 /*initialized=*/0, &attributes);
14526 if (attributes)
14527 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14528
14529 /* Don't give this error when parsing tentatively. This happens to
14530 work because we always parse this definitively once. */
14531 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14532 && type_uses_auto (type_specified))
14533 {
14534 if (cxx_dialect < cxx14)
14535 {
14536 error ("invalid use of %<auto%> in conversion operator");
14537 return error_mark_node;
14538 }
14539 else if (template_parm_scope_p ())
14540 warning (0, "use of %<auto%> in member template "
14541 "conversion operator can never be deduced");
14542 }
14543
14544 return type_specified;
14545 }
14546
14547 /* Parse an (optional) conversion-declarator.
14548
14549 conversion-declarator:
14550 ptr-operator conversion-declarator [opt]
14551
14552 */
14553
14554 static cp_declarator *
14555 cp_parser_conversion_declarator_opt (cp_parser* parser)
14556 {
14557 enum tree_code code;
14558 tree class_type, std_attributes = NULL_TREE;
14559 cp_cv_quals cv_quals;
14560
14561 /* We don't know if there's a ptr-operator next, or not. */
14562 cp_parser_parse_tentatively (parser);
14563 /* Try the ptr-operator. */
14564 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14565 &std_attributes);
14566 /* If it worked, look for more conversion-declarators. */
14567 if (cp_parser_parse_definitely (parser))
14568 {
14569 cp_declarator *declarator;
14570
14571 /* Parse another optional declarator. */
14572 declarator = cp_parser_conversion_declarator_opt (parser);
14573
14574 declarator = cp_parser_make_indirect_declarator
14575 (code, class_type, cv_quals, declarator, std_attributes);
14576
14577 return declarator;
14578 }
14579
14580 return NULL;
14581 }
14582
14583 /* Parse an (optional) ctor-initializer.
14584
14585 ctor-initializer:
14586 : mem-initializer-list */
14587
14588 static void
14589 cp_parser_ctor_initializer_opt (cp_parser* parser)
14590 {
14591 /* If the next token is not a `:', then there is no
14592 ctor-initializer. */
14593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14594 {
14595 /* Do default initialization of any bases and members. */
14596 if (DECL_CONSTRUCTOR_P (current_function_decl))
14597 finish_mem_initializers (NULL_TREE);
14598 return;
14599 }
14600
14601 /* Consume the `:' token. */
14602 cp_lexer_consume_token (parser->lexer);
14603 /* And the mem-initializer-list. */
14604 cp_parser_mem_initializer_list (parser);
14605 }
14606
14607 /* Parse a mem-initializer-list.
14608
14609 mem-initializer-list:
14610 mem-initializer ... [opt]
14611 mem-initializer ... [opt] , mem-initializer-list */
14612
14613 static void
14614 cp_parser_mem_initializer_list (cp_parser* parser)
14615 {
14616 tree mem_initializer_list = NULL_TREE;
14617 tree target_ctor = error_mark_node;
14618 cp_token *token = cp_lexer_peek_token (parser->lexer);
14619
14620 /* Let the semantic analysis code know that we are starting the
14621 mem-initializer-list. */
14622 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14623 error_at (token->location,
14624 "only constructors take member initializers");
14625
14626 /* Loop through the list. */
14627 while (true)
14628 {
14629 tree mem_initializer;
14630
14631 token = cp_lexer_peek_token (parser->lexer);
14632 /* Parse the mem-initializer. */
14633 mem_initializer = cp_parser_mem_initializer (parser);
14634 /* If the next token is a `...', we're expanding member initializers. */
14635 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14636 if (ellipsis
14637 || (mem_initializer != error_mark_node
14638 && check_for_bare_parameter_packs (TREE_PURPOSE
14639 (mem_initializer))))
14640 {
14641 /* Consume the `...'. */
14642 if (ellipsis)
14643 cp_lexer_consume_token (parser->lexer);
14644
14645 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14646 can be expanded but members cannot. */
14647 if (mem_initializer != error_mark_node
14648 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14649 {
14650 error_at (token->location,
14651 "cannot expand initializer for member %qD",
14652 TREE_PURPOSE (mem_initializer));
14653 mem_initializer = error_mark_node;
14654 }
14655
14656 /* Construct the pack expansion type. */
14657 if (mem_initializer != error_mark_node)
14658 mem_initializer = make_pack_expansion (mem_initializer);
14659 }
14660 if (target_ctor != error_mark_node
14661 && mem_initializer != error_mark_node)
14662 {
14663 error ("mem-initializer for %qD follows constructor delegation",
14664 TREE_PURPOSE (mem_initializer));
14665 mem_initializer = error_mark_node;
14666 }
14667 /* Look for a target constructor. */
14668 if (mem_initializer != error_mark_node
14669 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14670 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14671 {
14672 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14673 if (mem_initializer_list)
14674 {
14675 error ("constructor delegation follows mem-initializer for %qD",
14676 TREE_PURPOSE (mem_initializer_list));
14677 mem_initializer = error_mark_node;
14678 }
14679 target_ctor = mem_initializer;
14680 }
14681 /* Add it to the list, unless it was erroneous. */
14682 if (mem_initializer != error_mark_node)
14683 {
14684 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14685 mem_initializer_list = mem_initializer;
14686 }
14687 /* If the next token is not a `,', we're done. */
14688 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14689 break;
14690 /* Consume the `,' token. */
14691 cp_lexer_consume_token (parser->lexer);
14692 }
14693
14694 /* Perform semantic analysis. */
14695 if (DECL_CONSTRUCTOR_P (current_function_decl))
14696 finish_mem_initializers (mem_initializer_list);
14697 }
14698
14699 /* Parse a mem-initializer.
14700
14701 mem-initializer:
14702 mem-initializer-id ( expression-list [opt] )
14703 mem-initializer-id braced-init-list
14704
14705 GNU extension:
14706
14707 mem-initializer:
14708 ( expression-list [opt] )
14709
14710 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14711 class) or FIELD_DECL (for a non-static data member) to initialize;
14712 the TREE_VALUE is the expression-list. An empty initialization
14713 list is represented by void_list_node. */
14714
14715 static tree
14716 cp_parser_mem_initializer (cp_parser* parser)
14717 {
14718 tree mem_initializer_id;
14719 tree expression_list;
14720 tree member;
14721 cp_token *token = cp_lexer_peek_token (parser->lexer);
14722
14723 /* Find out what is being initialized. */
14724 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14725 {
14726 permerror (token->location,
14727 "anachronistic old-style base class initializer");
14728 mem_initializer_id = NULL_TREE;
14729 }
14730 else
14731 {
14732 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14733 if (mem_initializer_id == error_mark_node)
14734 return mem_initializer_id;
14735 }
14736 member = expand_member_init (mem_initializer_id);
14737 if (member && !DECL_P (member))
14738 in_base_initializer = 1;
14739
14740 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14741 {
14742 bool expr_non_constant_p;
14743 cp_lexer_set_source_position (parser->lexer);
14744 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14745 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14746 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14747 expression_list = build_tree_list (NULL_TREE, expression_list);
14748 }
14749 else
14750 {
14751 vec<tree, va_gc> *vec;
14752 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14753 /*cast_p=*/false,
14754 /*allow_expansion_p=*/true,
14755 /*non_constant_p=*/NULL);
14756 if (vec == NULL)
14757 return error_mark_node;
14758 expression_list = build_tree_list_vec (vec);
14759 release_tree_vector (vec);
14760 }
14761
14762 if (expression_list == error_mark_node)
14763 return error_mark_node;
14764 if (!expression_list)
14765 expression_list = void_type_node;
14766
14767 in_base_initializer = 0;
14768
14769 return member ? build_tree_list (member, expression_list) : error_mark_node;
14770 }
14771
14772 /* Parse a mem-initializer-id.
14773
14774 mem-initializer-id:
14775 :: [opt] nested-name-specifier [opt] class-name
14776 decltype-specifier (C++11)
14777 identifier
14778
14779 Returns a TYPE indicating the class to be initialized for the first
14780 production (and the second in C++11). Returns an IDENTIFIER_NODE
14781 indicating the data member to be initialized for the last production. */
14782
14783 static tree
14784 cp_parser_mem_initializer_id (cp_parser* parser)
14785 {
14786 bool global_scope_p;
14787 bool nested_name_specifier_p;
14788 bool template_p = false;
14789 tree id;
14790
14791 cp_token *token = cp_lexer_peek_token (parser->lexer);
14792
14793 /* `typename' is not allowed in this context ([temp.res]). */
14794 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14795 {
14796 error_at (token->location,
14797 "keyword %<typename%> not allowed in this context (a qualified "
14798 "member initializer is implicitly a type)");
14799 cp_lexer_consume_token (parser->lexer);
14800 }
14801 /* Look for the optional `::' operator. */
14802 global_scope_p
14803 = (cp_parser_global_scope_opt (parser,
14804 /*current_scope_valid_p=*/false)
14805 != NULL_TREE);
14806 /* Look for the optional nested-name-specifier. The simplest way to
14807 implement:
14808
14809 [temp.res]
14810
14811 The keyword `typename' is not permitted in a base-specifier or
14812 mem-initializer; in these contexts a qualified name that
14813 depends on a template-parameter is implicitly assumed to be a
14814 type name.
14815
14816 is to assume that we have seen the `typename' keyword at this
14817 point. */
14818 nested_name_specifier_p
14819 = (cp_parser_nested_name_specifier_opt (parser,
14820 /*typename_keyword_p=*/true,
14821 /*check_dependency_p=*/true,
14822 /*type_p=*/true,
14823 /*is_declaration=*/true)
14824 != NULL_TREE);
14825 if (nested_name_specifier_p)
14826 template_p = cp_parser_optional_template_keyword (parser);
14827 /* If there is a `::' operator or a nested-name-specifier, then we
14828 are definitely looking for a class-name. */
14829 if (global_scope_p || nested_name_specifier_p)
14830 return cp_parser_class_name (parser,
14831 /*typename_keyword_p=*/true,
14832 /*template_keyword_p=*/template_p,
14833 typename_type,
14834 /*check_dependency_p=*/true,
14835 /*class_head_p=*/false,
14836 /*is_declaration=*/true);
14837 /* Otherwise, we could also be looking for an ordinary identifier. */
14838 cp_parser_parse_tentatively (parser);
14839 if (cp_lexer_next_token_is_decltype (parser->lexer))
14840 /* Try a decltype-specifier. */
14841 id = cp_parser_decltype (parser);
14842 else
14843 /* Otherwise, try a class-name. */
14844 id = cp_parser_class_name (parser,
14845 /*typename_keyword_p=*/true,
14846 /*template_keyword_p=*/false,
14847 none_type,
14848 /*check_dependency_p=*/true,
14849 /*class_head_p=*/false,
14850 /*is_declaration=*/true);
14851 /* If we found one, we're done. */
14852 if (cp_parser_parse_definitely (parser))
14853 return id;
14854 /* Otherwise, look for an ordinary identifier. */
14855 return cp_parser_identifier (parser);
14856 }
14857
14858 /* Overloading [gram.over] */
14859
14860 /* Parse an operator-function-id.
14861
14862 operator-function-id:
14863 operator operator
14864
14865 Returns an IDENTIFIER_NODE for the operator which is a
14866 human-readable spelling of the identifier, e.g., `operator +'. */
14867
14868 static cp_expr
14869 cp_parser_operator_function_id (cp_parser* parser)
14870 {
14871 /* Look for the `operator' keyword. */
14872 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14873 return error_mark_node;
14874 /* And then the name of the operator itself. */
14875 return cp_parser_operator (parser);
14876 }
14877
14878 /* Return an identifier node for a user-defined literal operator.
14879 The suffix identifier is chained to the operator name identifier. */
14880
14881 tree
14882 cp_literal_operator_id (const char* name)
14883 {
14884 tree identifier;
14885 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14886 + strlen (name) + 10);
14887 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14888 identifier = get_identifier (buffer);
14889
14890 return identifier;
14891 }
14892
14893 /* Parse an operator.
14894
14895 operator:
14896 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14897 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14898 || ++ -- , ->* -> () []
14899
14900 GNU Extensions:
14901
14902 operator:
14903 <? >? <?= >?=
14904
14905 Returns an IDENTIFIER_NODE for the operator which is a
14906 human-readable spelling of the identifier, e.g., `operator +'. */
14907
14908 static cp_expr
14909 cp_parser_operator (cp_parser* parser)
14910 {
14911 tree id = NULL_TREE;
14912 cp_token *token;
14913 bool utf8 = false;
14914
14915 /* Peek at the next token. */
14916 token = cp_lexer_peek_token (parser->lexer);
14917
14918 location_t start_loc = token->location;
14919
14920 /* Figure out which operator we have. */
14921 enum tree_code op = ERROR_MARK;
14922 bool assop = false;
14923 bool consumed = false;
14924 switch (token->type)
14925 {
14926 case CPP_KEYWORD:
14927 {
14928 /* The keyword should be either `new' or `delete'. */
14929 if (token->keyword == RID_NEW)
14930 op = NEW_EXPR;
14931 else if (token->keyword == RID_DELETE)
14932 op = DELETE_EXPR;
14933 else
14934 break;
14935
14936 /* Consume the `new' or `delete' token. */
14937 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14938
14939 /* Peek at the next token. */
14940 token = cp_lexer_peek_token (parser->lexer);
14941 /* If it's a `[' token then this is the array variant of the
14942 operator. */
14943 if (token->type == CPP_OPEN_SQUARE)
14944 {
14945 /* Consume the `[' token. */
14946 cp_lexer_consume_token (parser->lexer);
14947 /* Look for the `]' token. */
14948 if (cp_token *close_token
14949 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14950 end_loc = close_token->location;
14951 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14952 }
14953 start_loc = make_location (start_loc, start_loc, end_loc);
14954 consumed = true;
14955 break;
14956 }
14957
14958 case CPP_PLUS:
14959 op = PLUS_EXPR;
14960 break;
14961
14962 case CPP_MINUS:
14963 op = MINUS_EXPR;
14964 break;
14965
14966 case CPP_MULT:
14967 op = MULT_EXPR;
14968 break;
14969
14970 case CPP_DIV:
14971 op = TRUNC_DIV_EXPR;
14972 break;
14973
14974 case CPP_MOD:
14975 op = TRUNC_MOD_EXPR;
14976 break;
14977
14978 case CPP_XOR:
14979 op = BIT_XOR_EXPR;
14980 break;
14981
14982 case CPP_AND:
14983 op = BIT_AND_EXPR;
14984 break;
14985
14986 case CPP_OR:
14987 op = BIT_IOR_EXPR;
14988 break;
14989
14990 case CPP_COMPL:
14991 op = BIT_NOT_EXPR;
14992 break;
14993
14994 case CPP_NOT:
14995 op = TRUTH_NOT_EXPR;
14996 break;
14997
14998 case CPP_EQ:
14999 assop = true;
15000 op = NOP_EXPR;
15001 break;
15002
15003 case CPP_LESS:
15004 op = LT_EXPR;
15005 break;
15006
15007 case CPP_GREATER:
15008 op = GT_EXPR;
15009 break;
15010
15011 case CPP_PLUS_EQ:
15012 assop = true;
15013 op = PLUS_EXPR;
15014 break;
15015
15016 case CPP_MINUS_EQ:
15017 assop = true;
15018 op = MINUS_EXPR;
15019 break;
15020
15021 case CPP_MULT_EQ:
15022 assop = true;
15023 op = MULT_EXPR;
15024 break;
15025
15026 case CPP_DIV_EQ:
15027 assop = true;
15028 op = TRUNC_DIV_EXPR;
15029 break;
15030
15031 case CPP_MOD_EQ:
15032 assop = true;
15033 op = TRUNC_MOD_EXPR;
15034 break;
15035
15036 case CPP_XOR_EQ:
15037 assop = true;
15038 op = BIT_XOR_EXPR;
15039 break;
15040
15041 case CPP_AND_EQ:
15042 assop = true;
15043 op = BIT_AND_EXPR;
15044 break;
15045
15046 case CPP_OR_EQ:
15047 assop = true;
15048 op = BIT_IOR_EXPR;
15049 break;
15050
15051 case CPP_LSHIFT:
15052 op = LSHIFT_EXPR;
15053 break;
15054
15055 case CPP_RSHIFT:
15056 op = RSHIFT_EXPR;
15057 break;
15058
15059 case CPP_LSHIFT_EQ:
15060 assop = true;
15061 op = LSHIFT_EXPR;
15062 break;
15063
15064 case CPP_RSHIFT_EQ:
15065 assop = true;
15066 op = RSHIFT_EXPR;
15067 break;
15068
15069 case CPP_EQ_EQ:
15070 op = EQ_EXPR;
15071 break;
15072
15073 case CPP_NOT_EQ:
15074 op = NE_EXPR;
15075 break;
15076
15077 case CPP_LESS_EQ:
15078 op = LE_EXPR;
15079 break;
15080
15081 case CPP_GREATER_EQ:
15082 op = GE_EXPR;
15083 break;
15084
15085 case CPP_AND_AND:
15086 op = TRUTH_ANDIF_EXPR;
15087 break;
15088
15089 case CPP_OR_OR:
15090 op = TRUTH_ORIF_EXPR;
15091 break;
15092
15093 case CPP_PLUS_PLUS:
15094 op = POSTINCREMENT_EXPR;
15095 break;
15096
15097 case CPP_MINUS_MINUS:
15098 op = PREDECREMENT_EXPR;
15099 break;
15100
15101 case CPP_COMMA:
15102 op = COMPOUND_EXPR;
15103 break;
15104
15105 case CPP_DEREF_STAR:
15106 op = MEMBER_REF;
15107 break;
15108
15109 case CPP_DEREF:
15110 op = COMPONENT_REF;
15111 break;
15112
15113 case CPP_OPEN_PAREN:
15114 {
15115 /* Consume the `('. */
15116 matching_parens parens;
15117 parens.consume_open (parser);
15118 /* Look for the matching `)'. */
15119 parens.require_close (parser);
15120 op = CALL_EXPR;
15121 consumed = true;
15122 break;
15123 }
15124
15125 case CPP_OPEN_SQUARE:
15126 /* Consume the `['. */
15127 cp_lexer_consume_token (parser->lexer);
15128 /* Look for the matching `]'. */
15129 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15130 op = ARRAY_REF;
15131 consumed = true;
15132 break;
15133
15134 case CPP_UTF8STRING:
15135 case CPP_UTF8STRING_USERDEF:
15136 utf8 = true;
15137 /* FALLTHRU */
15138 case CPP_STRING:
15139 case CPP_WSTRING:
15140 case CPP_STRING16:
15141 case CPP_STRING32:
15142 case CPP_STRING_USERDEF:
15143 case CPP_WSTRING_USERDEF:
15144 case CPP_STRING16_USERDEF:
15145 case CPP_STRING32_USERDEF:
15146 {
15147 tree str, string_tree;
15148 int sz, len;
15149
15150 if (cxx_dialect == cxx98)
15151 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15152
15153 /* Consume the string. */
15154 str = cp_parser_string_literal (parser, /*translate=*/true,
15155 /*wide_ok=*/true, /*lookup_udlit=*/false);
15156 if (str == error_mark_node)
15157 return error_mark_node;
15158 else if (TREE_CODE (str) == USERDEF_LITERAL)
15159 {
15160 string_tree = USERDEF_LITERAL_VALUE (str);
15161 id = USERDEF_LITERAL_SUFFIX_ID (str);
15162 }
15163 else
15164 {
15165 string_tree = str;
15166 /* Look for the suffix identifier. */
15167 token = cp_lexer_peek_token (parser->lexer);
15168 if (token->type == CPP_NAME)
15169 id = cp_parser_identifier (parser);
15170 else if (token->type == CPP_KEYWORD)
15171 {
15172 error ("unexpected keyword;"
15173 " remove space between quotes and suffix identifier");
15174 return error_mark_node;
15175 }
15176 else
15177 {
15178 error ("expected suffix identifier");
15179 return error_mark_node;
15180 }
15181 }
15182 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15183 (TREE_TYPE (TREE_TYPE (string_tree))));
15184 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15185 if (len != 0)
15186 {
15187 error ("expected empty string after %<operator%> keyword");
15188 return error_mark_node;
15189 }
15190 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15191 != char_type_node)
15192 {
15193 error ("invalid encoding prefix in literal operator");
15194 return error_mark_node;
15195 }
15196 if (id != error_mark_node)
15197 {
15198 const char *name = IDENTIFIER_POINTER (id);
15199 id = cp_literal_operator_id (name);
15200 }
15201 return id;
15202 }
15203
15204 default:
15205 /* Anything else is an error. */
15206 break;
15207 }
15208
15209 /* If we have selected an identifier, we need to consume the
15210 operator token. */
15211 if (op != ERROR_MARK)
15212 {
15213 id = ovl_op_identifier (assop, op);
15214 if (!consumed)
15215 cp_lexer_consume_token (parser->lexer);
15216 }
15217 /* Otherwise, no valid operator name was present. */
15218 else
15219 {
15220 cp_parser_error (parser, "expected operator");
15221 id = error_mark_node;
15222 }
15223
15224 return cp_expr (id, start_loc);
15225 }
15226
15227 /* Parse a template-declaration.
15228
15229 template-declaration:
15230 export [opt] template < template-parameter-list > declaration
15231
15232 If MEMBER_P is TRUE, this template-declaration occurs within a
15233 class-specifier.
15234
15235 The grammar rule given by the standard isn't correct. What
15236 is really meant is:
15237
15238 template-declaration:
15239 export [opt] template-parameter-list-seq
15240 decl-specifier-seq [opt] init-declarator [opt] ;
15241 export [opt] template-parameter-list-seq
15242 function-definition
15243
15244 template-parameter-list-seq:
15245 template-parameter-list-seq [opt]
15246 template < template-parameter-list >
15247
15248 Concept Extensions:
15249
15250 template-parameter-list-seq:
15251 template < template-parameter-list > requires-clause [opt]
15252
15253 requires-clause:
15254 requires logical-or-expression */
15255
15256 static void
15257 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15258 {
15259 /* Check for `export'. */
15260 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15261 {
15262 /* Consume the `export' token. */
15263 cp_lexer_consume_token (parser->lexer);
15264 /* Warn that we do not support `export'. */
15265 warning (0, "keyword %<export%> not implemented, and will be ignored");
15266 }
15267
15268 cp_parser_template_declaration_after_export (parser, member_p);
15269 }
15270
15271 /* Parse a template-parameter-list.
15272
15273 template-parameter-list:
15274 template-parameter
15275 template-parameter-list , template-parameter
15276
15277 Returns a TREE_LIST. Each node represents a template parameter.
15278 The nodes are connected via their TREE_CHAINs. */
15279
15280 static tree
15281 cp_parser_template_parameter_list (cp_parser* parser)
15282 {
15283 tree parameter_list = NULL_TREE;
15284
15285 begin_template_parm_list ();
15286
15287 /* The loop below parses the template parms. We first need to know
15288 the total number of template parms to be able to compute proper
15289 canonical types of each dependent type. So after the loop, when
15290 we know the total number of template parms,
15291 end_template_parm_list computes the proper canonical types and
15292 fixes up the dependent types accordingly. */
15293 while (true)
15294 {
15295 tree parameter;
15296 bool is_non_type;
15297 bool is_parameter_pack;
15298 location_t parm_loc;
15299
15300 /* Parse the template-parameter. */
15301 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15302 parameter = cp_parser_template_parameter (parser,
15303 &is_non_type,
15304 &is_parameter_pack);
15305 /* Add it to the list. */
15306 if (parameter != error_mark_node)
15307 parameter_list = process_template_parm (parameter_list,
15308 parm_loc,
15309 parameter,
15310 is_non_type,
15311 is_parameter_pack);
15312 else
15313 {
15314 tree err_parm = build_tree_list (parameter, parameter);
15315 parameter_list = chainon (parameter_list, err_parm);
15316 }
15317
15318 /* If the next token is not a `,', we're done. */
15319 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15320 break;
15321 /* Otherwise, consume the `,' token. */
15322 cp_lexer_consume_token (parser->lexer);
15323 }
15324
15325 return end_template_parm_list (parameter_list);
15326 }
15327
15328 /* Parse a introduction-list.
15329
15330 introduction-list:
15331 introduced-parameter
15332 introduction-list , introduced-parameter
15333
15334 introduced-parameter:
15335 ...[opt] identifier
15336
15337 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15338 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15339 WILDCARD_DECL will also have DECL_NAME set and token location in
15340 DECL_SOURCE_LOCATION. */
15341
15342 static tree
15343 cp_parser_introduction_list (cp_parser *parser)
15344 {
15345 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15346
15347 while (true)
15348 {
15349 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15350 if (is_pack)
15351 cp_lexer_consume_token (parser->lexer);
15352
15353 tree identifier = cp_parser_identifier (parser);
15354 if (identifier == error_mark_node)
15355 break;
15356
15357 /* Build placeholder. */
15358 tree parm = build_nt (WILDCARD_DECL);
15359 DECL_SOURCE_LOCATION (parm)
15360 = cp_lexer_peek_token (parser->lexer)->location;
15361 DECL_NAME (parm) = identifier;
15362 WILDCARD_PACK_P (parm) = is_pack;
15363 vec_safe_push (introduction_vec, parm);
15364
15365 /* If the next token is not a `,', we're done. */
15366 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15367 break;
15368 /* Otherwise, consume the `,' token. */
15369 cp_lexer_consume_token (parser->lexer);
15370 }
15371
15372 /* Convert the vec into a TREE_VEC. */
15373 tree introduction_list = make_tree_vec (introduction_vec->length ());
15374 unsigned int n;
15375 tree parm;
15376 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15377 TREE_VEC_ELT (introduction_list, n) = parm;
15378
15379 release_tree_vector (introduction_vec);
15380 return introduction_list;
15381 }
15382
15383 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15384 is an abstract declarator. */
15385
15386 static inline cp_declarator*
15387 get_id_declarator (cp_declarator *declarator)
15388 {
15389 cp_declarator *d = declarator;
15390 while (d && d->kind != cdk_id)
15391 d = d->declarator;
15392 return d;
15393 }
15394
15395 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15396 is an abstract declarator. */
15397
15398 static inline tree
15399 get_unqualified_id (cp_declarator *declarator)
15400 {
15401 declarator = get_id_declarator (declarator);
15402 if (declarator)
15403 return declarator->u.id.unqualified_name;
15404 else
15405 return NULL_TREE;
15406 }
15407
15408 /* Returns true if DECL represents a constrained-parameter. */
15409
15410 static inline bool
15411 is_constrained_parameter (tree decl)
15412 {
15413 return (decl
15414 && TREE_CODE (decl) == TYPE_DECL
15415 && CONSTRAINED_PARM_CONCEPT (decl)
15416 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15417 }
15418
15419 /* Returns true if PARM declares a constrained-parameter. */
15420
15421 static inline bool
15422 is_constrained_parameter (cp_parameter_declarator *parm)
15423 {
15424 return is_constrained_parameter (parm->decl_specifiers.type);
15425 }
15426
15427 /* Check that the type parameter is only a declarator-id, and that its
15428 type is not cv-qualified. */
15429
15430 bool
15431 cp_parser_check_constrained_type_parm (cp_parser *parser,
15432 cp_parameter_declarator *parm)
15433 {
15434 if (!parm->declarator)
15435 return true;
15436
15437 if (parm->declarator->kind != cdk_id)
15438 {
15439 cp_parser_error (parser, "invalid constrained type parameter");
15440 return false;
15441 }
15442
15443 /* Don't allow cv-qualified type parameters. */
15444 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15445 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15446 {
15447 cp_parser_error (parser, "cv-qualified type parameter");
15448 return false;
15449 }
15450
15451 return true;
15452 }
15453
15454 /* Finish parsing/processing a template type parameter and checking
15455 various restrictions. */
15456
15457 static inline tree
15458 cp_parser_constrained_type_template_parm (cp_parser *parser,
15459 tree id,
15460 cp_parameter_declarator* parmdecl)
15461 {
15462 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15463 return finish_template_type_parm (class_type_node, id);
15464 else
15465 return error_mark_node;
15466 }
15467
15468 static tree
15469 finish_constrained_template_template_parm (tree proto, tree id)
15470 {
15471 /* FIXME: This should probably be copied, and we may need to adjust
15472 the template parameter depths. */
15473 tree saved_parms = current_template_parms;
15474 begin_template_parm_list ();
15475 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15476 end_template_parm_list ();
15477
15478 tree parm = finish_template_template_parm (class_type_node, id);
15479 current_template_parms = saved_parms;
15480
15481 return parm;
15482 }
15483
15484 /* Finish parsing/processing a template template parameter by borrowing
15485 the template parameter list from the prototype parameter. */
15486
15487 static tree
15488 cp_parser_constrained_template_template_parm (cp_parser *parser,
15489 tree proto,
15490 tree id,
15491 cp_parameter_declarator *parmdecl)
15492 {
15493 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15494 return error_mark_node;
15495 return finish_constrained_template_template_parm (proto, id);
15496 }
15497
15498 /* Create a new non-type template parameter from the given PARM
15499 declarator. */
15500
15501 static tree
15502 constrained_non_type_template_parm (bool *is_non_type,
15503 cp_parameter_declarator *parm)
15504 {
15505 *is_non_type = true;
15506 cp_declarator *decl = parm->declarator;
15507 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15508 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15509 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15510 }
15511
15512 /* Build a constrained template parameter based on the PARMDECL
15513 declarator. The type of PARMDECL is the constrained type, which
15514 refers to the prototype template parameter that ultimately
15515 specifies the type of the declared parameter. */
15516
15517 static tree
15518 finish_constrained_parameter (cp_parser *parser,
15519 cp_parameter_declarator *parmdecl,
15520 bool *is_non_type,
15521 bool *is_parameter_pack)
15522 {
15523 tree decl = parmdecl->decl_specifiers.type;
15524 tree id = get_unqualified_id (parmdecl->declarator);
15525 tree def = parmdecl->default_argument;
15526 tree proto = DECL_INITIAL (decl);
15527
15528 /* A template parameter constrained by a variadic concept shall also
15529 be declared as a template parameter pack. */
15530 bool is_variadic = template_parameter_pack_p (proto);
15531 if (is_variadic && !*is_parameter_pack)
15532 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15533
15534 /* Build the parameter. Return an error if the declarator was invalid. */
15535 tree parm;
15536 if (TREE_CODE (proto) == TYPE_DECL)
15537 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15538 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15539 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15540 parmdecl);
15541 else
15542 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15543 if (parm == error_mark_node)
15544 return error_mark_node;
15545
15546 /* Finish the parameter decl and create a node attaching the
15547 default argument and constraint. */
15548 parm = build_tree_list (def, parm);
15549 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15550
15551 return parm;
15552 }
15553
15554 /* Returns true if the parsed type actually represents the declaration
15555 of a type template-parameter. */
15556
15557 static inline bool
15558 declares_constrained_type_template_parameter (tree type)
15559 {
15560 return (is_constrained_parameter (type)
15561 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15562 }
15563
15564
15565 /* Returns true if the parsed type actually represents the declaration of
15566 a template template-parameter. */
15567
15568 static bool
15569 declares_constrained_template_template_parameter (tree type)
15570 {
15571 return (is_constrained_parameter (type)
15572 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15573 }
15574
15575 /* Parse a default argument for a type template-parameter.
15576 Note that diagnostics are handled in cp_parser_template_parameter. */
15577
15578 static tree
15579 cp_parser_default_type_template_argument (cp_parser *parser)
15580 {
15581 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15582
15583 /* Consume the `=' token. */
15584 cp_lexer_consume_token (parser->lexer);
15585
15586 cp_token *token = cp_lexer_peek_token (parser->lexer);
15587
15588 /* Parse the default-argument. */
15589 push_deferring_access_checks (dk_no_deferred);
15590 tree default_argument = cp_parser_type_id (parser);
15591 pop_deferring_access_checks ();
15592
15593 if (flag_concepts && type_uses_auto (default_argument))
15594 {
15595 error_at (token->location,
15596 "invalid use of %<auto%> in default template argument");
15597 return error_mark_node;
15598 }
15599
15600 return default_argument;
15601 }
15602
15603 /* Parse a default argument for a template template-parameter. */
15604
15605 static tree
15606 cp_parser_default_template_template_argument (cp_parser *parser)
15607 {
15608 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15609
15610 bool is_template;
15611
15612 /* Consume the `='. */
15613 cp_lexer_consume_token (parser->lexer);
15614 /* Parse the id-expression. */
15615 push_deferring_access_checks (dk_no_deferred);
15616 /* save token before parsing the id-expression, for error
15617 reporting */
15618 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15619 tree default_argument
15620 = cp_parser_id_expression (parser,
15621 /*template_keyword_p=*/false,
15622 /*check_dependency_p=*/true,
15623 /*template_p=*/&is_template,
15624 /*declarator_p=*/false,
15625 /*optional_p=*/false);
15626 if (TREE_CODE (default_argument) == TYPE_DECL)
15627 /* If the id-expression was a template-id that refers to
15628 a template-class, we already have the declaration here,
15629 so no further lookup is needed. */
15630 ;
15631 else
15632 /* Look up the name. */
15633 default_argument
15634 = cp_parser_lookup_name (parser, default_argument,
15635 none_type,
15636 /*is_template=*/is_template,
15637 /*is_namespace=*/false,
15638 /*check_dependency=*/true,
15639 /*ambiguous_decls=*/NULL,
15640 token->location);
15641 /* See if the default argument is valid. */
15642 default_argument = check_template_template_default_arg (default_argument);
15643 pop_deferring_access_checks ();
15644 return default_argument;
15645 }
15646
15647 /* Parse a template-parameter.
15648
15649 template-parameter:
15650 type-parameter
15651 parameter-declaration
15652
15653 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15654 the parameter. The TREE_PURPOSE is the default value, if any.
15655 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15656 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15657 set to true iff this parameter is a parameter pack. */
15658
15659 static tree
15660 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15661 bool *is_parameter_pack)
15662 {
15663 cp_token *token;
15664 cp_parameter_declarator *parameter_declarator;
15665 tree parm;
15666
15667 /* Assume it is a type parameter or a template parameter. */
15668 *is_non_type = false;
15669 /* Assume it not a parameter pack. */
15670 *is_parameter_pack = false;
15671 /* Peek at the next token. */
15672 token = cp_lexer_peek_token (parser->lexer);
15673 /* If it is `template', we have a type-parameter. */
15674 if (token->keyword == RID_TEMPLATE)
15675 return cp_parser_type_parameter (parser, is_parameter_pack);
15676 /* If it is `class' or `typename' we do not know yet whether it is a
15677 type parameter or a non-type parameter. Consider:
15678
15679 template <typename T, typename T::X X> ...
15680
15681 or:
15682
15683 template <class C, class D*> ...
15684
15685 Here, the first parameter is a type parameter, and the second is
15686 a non-type parameter. We can tell by looking at the token after
15687 the identifier -- if it is a `,', `=', or `>' then we have a type
15688 parameter. */
15689 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15690 {
15691 /* Peek at the token after `class' or `typename'. */
15692 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15693 /* If it's an ellipsis, we have a template type parameter
15694 pack. */
15695 if (token->type == CPP_ELLIPSIS)
15696 return cp_parser_type_parameter (parser, is_parameter_pack);
15697 /* If it's an identifier, skip it. */
15698 if (token->type == CPP_NAME)
15699 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15700 /* Now, see if the token looks like the end of a template
15701 parameter. */
15702 if (token->type == CPP_COMMA
15703 || token->type == CPP_EQ
15704 || token->type == CPP_GREATER)
15705 return cp_parser_type_parameter (parser, is_parameter_pack);
15706 }
15707
15708 /* Otherwise, it is a non-type parameter or a constrained parameter.
15709
15710 [temp.param]
15711
15712 When parsing a default template-argument for a non-type
15713 template-parameter, the first non-nested `>' is taken as the end
15714 of the template parameter-list rather than a greater-than
15715 operator. */
15716 parameter_declarator
15717 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15718 /*parenthesized_p=*/NULL);
15719
15720 if (!parameter_declarator)
15721 return error_mark_node;
15722
15723 /* If the parameter declaration is marked as a parameter pack, set
15724 *IS_PARAMETER_PACK to notify the caller. */
15725 if (parameter_declarator->template_parameter_pack_p)
15726 *is_parameter_pack = true;
15727
15728 if (parameter_declarator->default_argument)
15729 {
15730 /* Can happen in some cases of erroneous input (c++/34892). */
15731 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15732 /* Consume the `...' for better error recovery. */
15733 cp_lexer_consume_token (parser->lexer);
15734 }
15735
15736 // The parameter may have been constrained.
15737 if (is_constrained_parameter (parameter_declarator))
15738 return finish_constrained_parameter (parser,
15739 parameter_declarator,
15740 is_non_type,
15741 is_parameter_pack);
15742
15743 // Now we're sure that the parameter is a non-type parameter.
15744 *is_non_type = true;
15745
15746 parm = grokdeclarator (parameter_declarator->declarator,
15747 &parameter_declarator->decl_specifiers,
15748 TPARM, /*initialized=*/0,
15749 /*attrlist=*/NULL);
15750 if (parm == error_mark_node)
15751 return error_mark_node;
15752
15753 return build_tree_list (parameter_declarator->default_argument, parm);
15754 }
15755
15756 /* Parse a type-parameter.
15757
15758 type-parameter:
15759 class identifier [opt]
15760 class identifier [opt] = type-id
15761 typename identifier [opt]
15762 typename identifier [opt] = type-id
15763 template < template-parameter-list > class identifier [opt]
15764 template < template-parameter-list > class identifier [opt]
15765 = id-expression
15766
15767 GNU Extension (variadic templates):
15768
15769 type-parameter:
15770 class ... identifier [opt]
15771 typename ... identifier [opt]
15772
15773 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15774 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15775 the declaration of the parameter.
15776
15777 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15778
15779 static tree
15780 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15781 {
15782 cp_token *token;
15783 tree parameter;
15784
15785 /* Look for a keyword to tell us what kind of parameter this is. */
15786 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15787 if (!token)
15788 return error_mark_node;
15789
15790 switch (token->keyword)
15791 {
15792 case RID_CLASS:
15793 case RID_TYPENAME:
15794 {
15795 tree identifier;
15796 tree default_argument;
15797
15798 /* If the next token is an ellipsis, we have a template
15799 argument pack. */
15800 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15801 {
15802 /* Consume the `...' token. */
15803 cp_lexer_consume_token (parser->lexer);
15804 maybe_warn_variadic_templates ();
15805
15806 *is_parameter_pack = true;
15807 }
15808
15809 /* If the next token is an identifier, then it names the
15810 parameter. */
15811 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15812 identifier = cp_parser_identifier (parser);
15813 else
15814 identifier = NULL_TREE;
15815
15816 /* Create the parameter. */
15817 parameter = finish_template_type_parm (class_type_node, identifier);
15818
15819 /* If the next token is an `=', we have a default argument. */
15820 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15821 {
15822 default_argument
15823 = cp_parser_default_type_template_argument (parser);
15824
15825 /* Template parameter packs cannot have default
15826 arguments. */
15827 if (*is_parameter_pack)
15828 {
15829 if (identifier)
15830 error_at (token->location,
15831 "template parameter pack %qD cannot have a "
15832 "default argument", identifier);
15833 else
15834 error_at (token->location,
15835 "template parameter packs cannot have "
15836 "default arguments");
15837 default_argument = NULL_TREE;
15838 }
15839 else if (check_for_bare_parameter_packs (default_argument))
15840 default_argument = error_mark_node;
15841 }
15842 else
15843 default_argument = NULL_TREE;
15844
15845 /* Create the combined representation of the parameter and the
15846 default argument. */
15847 parameter = build_tree_list (default_argument, parameter);
15848 }
15849 break;
15850
15851 case RID_TEMPLATE:
15852 {
15853 tree identifier;
15854 tree default_argument;
15855
15856 /* Look for the `<'. */
15857 cp_parser_require (parser, CPP_LESS, RT_LESS);
15858 /* Parse the template-parameter-list. */
15859 cp_parser_template_parameter_list (parser);
15860 /* Look for the `>'. */
15861 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15862
15863 // If template requirements are present, parse them.
15864 if (flag_concepts)
15865 {
15866 tree reqs = get_shorthand_constraints (current_template_parms);
15867 if (tree r = cp_parser_requires_clause_opt (parser))
15868 reqs = conjoin_constraints (reqs, normalize_expression (r));
15869 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15870 }
15871
15872 /* Look for the `class' or 'typename' keywords. */
15873 cp_parser_type_parameter_key (parser);
15874 /* If the next token is an ellipsis, we have a template
15875 argument pack. */
15876 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15877 {
15878 /* Consume the `...' token. */
15879 cp_lexer_consume_token (parser->lexer);
15880 maybe_warn_variadic_templates ();
15881
15882 *is_parameter_pack = true;
15883 }
15884 /* If the next token is an `=', then there is a
15885 default-argument. If the next token is a `>', we are at
15886 the end of the parameter-list. If the next token is a `,',
15887 then we are at the end of this parameter. */
15888 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15889 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15890 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15891 {
15892 identifier = cp_parser_identifier (parser);
15893 /* Treat invalid names as if the parameter were nameless. */
15894 if (identifier == error_mark_node)
15895 identifier = NULL_TREE;
15896 }
15897 else
15898 identifier = NULL_TREE;
15899
15900 /* Create the template parameter. */
15901 parameter = finish_template_template_parm (class_type_node,
15902 identifier);
15903
15904 /* If the next token is an `=', then there is a
15905 default-argument. */
15906 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15907 {
15908 default_argument
15909 = cp_parser_default_template_template_argument (parser);
15910
15911 /* Template parameter packs cannot have default
15912 arguments. */
15913 if (*is_parameter_pack)
15914 {
15915 if (identifier)
15916 error_at (token->location,
15917 "template parameter pack %qD cannot "
15918 "have a default argument",
15919 identifier);
15920 else
15921 error_at (token->location, "template parameter packs cannot "
15922 "have default arguments");
15923 default_argument = NULL_TREE;
15924 }
15925 }
15926 else
15927 default_argument = NULL_TREE;
15928
15929 /* Create the combined representation of the parameter and the
15930 default argument. */
15931 parameter = build_tree_list (default_argument, parameter);
15932 }
15933 break;
15934
15935 default:
15936 gcc_unreachable ();
15937 break;
15938 }
15939
15940 return parameter;
15941 }
15942
15943 /* Parse a template-id.
15944
15945 template-id:
15946 template-name < template-argument-list [opt] >
15947
15948 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15949 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15950 returned. Otherwise, if the template-name names a function, or set
15951 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15952 names a class, returns a TYPE_DECL for the specialization.
15953
15954 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15955 uninstantiated templates. */
15956
15957 static tree
15958 cp_parser_template_id (cp_parser *parser,
15959 bool template_keyword_p,
15960 bool check_dependency_p,
15961 enum tag_types tag_type,
15962 bool is_declaration)
15963 {
15964 tree templ;
15965 tree arguments;
15966 tree template_id;
15967 cp_token_position start_of_id = 0;
15968 cp_token *next_token = NULL, *next_token_2 = NULL;
15969 bool is_identifier;
15970
15971 /* If the next token corresponds to a template-id, there is no need
15972 to reparse it. */
15973 cp_token *token = cp_lexer_peek_token (parser->lexer);
15974 if (token->type == CPP_TEMPLATE_ID)
15975 {
15976 cp_lexer_consume_token (parser->lexer);
15977 return saved_checks_value (token->u.tree_check_value);
15978 }
15979
15980 /* Avoid performing name lookup if there is no possibility of
15981 finding a template-id. */
15982 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15983 || (token->type == CPP_NAME
15984 && !cp_parser_nth_token_starts_template_argument_list_p
15985 (parser, 2)))
15986 {
15987 cp_parser_error (parser, "expected template-id");
15988 return error_mark_node;
15989 }
15990
15991 /* Remember where the template-id starts. */
15992 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15993 start_of_id = cp_lexer_token_position (parser->lexer, false);
15994
15995 push_deferring_access_checks (dk_deferred);
15996
15997 /* Parse the template-name. */
15998 is_identifier = false;
15999 templ = cp_parser_template_name (parser, template_keyword_p,
16000 check_dependency_p,
16001 is_declaration,
16002 tag_type,
16003 &is_identifier);
16004 if (templ == error_mark_node || is_identifier)
16005 {
16006 pop_deferring_access_checks ();
16007 return templ;
16008 }
16009
16010 /* Since we're going to preserve any side-effects from this parse, set up a
16011 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16012 in the template arguments. */
16013 tentative_firewall firewall (parser);
16014
16015 /* If we find the sequence `[:' after a template-name, it's probably
16016 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16017 parse correctly the argument list. */
16018 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16019 == CPP_OPEN_SQUARE)
16020 && next_token->flags & DIGRAPH
16021 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16022 == CPP_COLON)
16023 && !(next_token_2->flags & PREV_WHITE))
16024 {
16025 cp_parser_parse_tentatively (parser);
16026 /* Change `:' into `::'. */
16027 next_token_2->type = CPP_SCOPE;
16028 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16029 CPP_LESS. */
16030 cp_lexer_consume_token (parser->lexer);
16031
16032 /* Parse the arguments. */
16033 arguments = cp_parser_enclosed_template_argument_list (parser);
16034 if (!cp_parser_parse_definitely (parser))
16035 {
16036 /* If we couldn't parse an argument list, then we revert our changes
16037 and return simply an error. Maybe this is not a template-id
16038 after all. */
16039 next_token_2->type = CPP_COLON;
16040 cp_parser_error (parser, "expected %<<%>");
16041 pop_deferring_access_checks ();
16042 return error_mark_node;
16043 }
16044 /* Otherwise, emit an error about the invalid digraph, but continue
16045 parsing because we got our argument list. */
16046 if (permerror (next_token->location,
16047 "%<<::%> cannot begin a template-argument list"))
16048 {
16049 static bool hint = false;
16050 inform (next_token->location,
16051 "%<<:%> is an alternate spelling for %<[%>."
16052 " Insert whitespace between %<<%> and %<::%>");
16053 if (!hint && !flag_permissive)
16054 {
16055 inform (next_token->location, "(if you use %<-fpermissive%> "
16056 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16057 "accept your code)");
16058 hint = true;
16059 }
16060 }
16061 }
16062 else
16063 {
16064 /* Look for the `<' that starts the template-argument-list. */
16065 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16066 {
16067 pop_deferring_access_checks ();
16068 return error_mark_node;
16069 }
16070 /* Parse the arguments. */
16071 arguments = cp_parser_enclosed_template_argument_list (parser);
16072
16073 if ((cxx_dialect > cxx17)
16074 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16075 && !template_keyword_p
16076 && (cp_parser_error_occurred (parser)
16077 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16078 {
16079 /* This didn't go well. */
16080 if (TREE_CODE (templ) == FUNCTION_DECL)
16081 {
16082 /* C++2A says that "function-name < a;" is now ill-formed. */
16083 if (cp_parser_error_occurred (parser))
16084 {
16085 error_at (token->location, "invalid template-argument-list");
16086 inform (token->location, "function name as the left hand "
16087 "operand of %<<%> is ill-formed in C++2a; wrap the "
16088 "function name in %<()%>");
16089 }
16090 else
16091 /* We expect "f<targs>" to be followed by "(args)". */
16092 error_at (cp_lexer_peek_token (parser->lexer)->location,
16093 "expected %<(%> after template-argument-list");
16094 if (start_of_id)
16095 /* Purge all subsequent tokens. */
16096 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16097 }
16098 else
16099 cp_parser_simulate_error (parser);
16100 pop_deferring_access_checks ();
16101 return error_mark_node;
16102 }
16103 }
16104
16105 /* Set the location to be of the form:
16106 template-name < template-argument-list [opt] >
16107 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16108 with caret == start at the start of the template-name,
16109 ranging until the closing '>'. */
16110 location_t finish_loc
16111 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16112 location_t combined_loc
16113 = make_location (token->location, token->location, finish_loc);
16114
16115 /* Check for concepts autos where they don't belong. We could
16116 identify types in some cases of idnetifier TEMPL, looking ahead
16117 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16118 types. We reject them in functions, but if what we have is an
16119 identifier, even with none_type we can't conclude it's NOT a
16120 type, we have to wait for template substitution. */
16121 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16122 template_id = error_mark_node;
16123 /* Build a representation of the specialization. */
16124 else if (identifier_p (templ))
16125 template_id = build_min_nt_loc (combined_loc,
16126 TEMPLATE_ID_EXPR,
16127 templ, arguments);
16128 else if (DECL_TYPE_TEMPLATE_P (templ)
16129 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16130 {
16131 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16132 template (rather than some instantiation thereof) only if
16133 is not nested within some other construct. For example, in
16134 "template <typename T> void f(T) { A<T>::", A<T> is just an
16135 instantiation of A. */
16136 bool entering_scope
16137 = (template_parm_scope_p ()
16138 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16139 template_id
16140 = finish_template_type (templ, arguments, entering_scope);
16141 }
16142 /* A template-like identifier may be a partial concept id. */
16143 else if (flag_concepts
16144 && (template_id = (cp_parser_maybe_partial_concept_id
16145 (parser, templ, arguments))))
16146 return template_id;
16147 else if (variable_template_p (templ))
16148 {
16149 template_id = lookup_template_variable (templ, arguments);
16150 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16151 SET_EXPR_LOCATION (template_id, combined_loc);
16152 }
16153 else
16154 {
16155 /* If it's not a class-template or a template-template, it should be
16156 a function-template. */
16157 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16158 || TREE_CODE (templ) == OVERLOAD
16159 || TREE_CODE (templ) == FUNCTION_DECL
16160 || BASELINK_P (templ)));
16161
16162 template_id = lookup_template_function (templ, arguments);
16163 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16164 SET_EXPR_LOCATION (template_id, combined_loc);
16165 }
16166
16167 /* If parsing tentatively, replace the sequence of tokens that makes
16168 up the template-id with a CPP_TEMPLATE_ID token. That way,
16169 should we re-parse the token stream, we will not have to repeat
16170 the effort required to do the parse, nor will we issue duplicate
16171 error messages about problems during instantiation of the
16172 template. */
16173 if (start_of_id
16174 /* Don't do this if we had a parse error in a declarator; re-parsing
16175 might succeed if a name changes meaning (60361). */
16176 && !(cp_parser_error_occurred (parser)
16177 && cp_parser_parsing_tentatively (parser)
16178 && parser->in_declarator_p))
16179 {
16180 /* Reset the contents of the START_OF_ID token. */
16181 token->type = CPP_TEMPLATE_ID;
16182 token->location = combined_loc;
16183
16184 /* Retrieve any deferred checks. Do not pop this access checks yet
16185 so the memory will not be reclaimed during token replacing below. */
16186 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16187 token->u.tree_check_value->value = template_id;
16188 token->u.tree_check_value->checks = get_deferred_access_checks ();
16189 token->keyword = RID_MAX;
16190
16191 /* Purge all subsequent tokens. */
16192 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16193
16194 /* ??? Can we actually assume that, if template_id ==
16195 error_mark_node, we will have issued a diagnostic to the
16196 user, as opposed to simply marking the tentative parse as
16197 failed? */
16198 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16199 error_at (token->location, "parse error in template argument list");
16200 }
16201
16202 pop_to_parent_deferring_access_checks ();
16203 return template_id;
16204 }
16205
16206 /* Parse a template-name.
16207
16208 template-name:
16209 identifier
16210
16211 The standard should actually say:
16212
16213 template-name:
16214 identifier
16215 operator-function-id
16216
16217 A defect report has been filed about this issue.
16218
16219 A conversion-function-id cannot be a template name because they cannot
16220 be part of a template-id. In fact, looking at this code:
16221
16222 a.operator K<int>()
16223
16224 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16225 It is impossible to call a templated conversion-function-id with an
16226 explicit argument list, since the only allowed template parameter is
16227 the type to which it is converting.
16228
16229 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16230 `template' keyword, in a construction like:
16231
16232 T::template f<3>()
16233
16234 In that case `f' is taken to be a template-name, even though there
16235 is no way of knowing for sure.
16236
16237 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16238 name refers to a set of overloaded functions, at least one of which
16239 is a template, or an IDENTIFIER_NODE with the name of the template,
16240 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16241 names are looked up inside uninstantiated templates. */
16242
16243 static tree
16244 cp_parser_template_name (cp_parser* parser,
16245 bool template_keyword_p,
16246 bool check_dependency_p,
16247 bool is_declaration,
16248 enum tag_types tag_type,
16249 bool *is_identifier)
16250 {
16251 tree identifier;
16252 tree decl;
16253 cp_token *token = cp_lexer_peek_token (parser->lexer);
16254
16255 /* If the next token is `operator', then we have either an
16256 operator-function-id or a conversion-function-id. */
16257 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16258 {
16259 /* We don't know whether we're looking at an
16260 operator-function-id or a conversion-function-id. */
16261 cp_parser_parse_tentatively (parser);
16262 /* Try an operator-function-id. */
16263 identifier = cp_parser_operator_function_id (parser);
16264 /* If that didn't work, try a conversion-function-id. */
16265 if (!cp_parser_parse_definitely (parser))
16266 {
16267 cp_parser_error (parser, "expected template-name");
16268 return error_mark_node;
16269 }
16270 }
16271 /* Look for the identifier. */
16272 else
16273 identifier = cp_parser_identifier (parser);
16274
16275 /* If we didn't find an identifier, we don't have a template-id. */
16276 if (identifier == error_mark_node)
16277 return error_mark_node;
16278
16279 /* If the name immediately followed the `template' keyword, then it
16280 is a template-name. However, if the next token is not `<', then
16281 we do not treat it as a template-name, since it is not being used
16282 as part of a template-id. This enables us to handle constructs
16283 like:
16284
16285 template <typename T> struct S { S(); };
16286 template <typename T> S<T>::S();
16287
16288 correctly. We would treat `S' as a template -- if it were `S<T>'
16289 -- but we do not if there is no `<'. */
16290
16291 if (processing_template_decl
16292 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16293 {
16294 /* In a declaration, in a dependent context, we pretend that the
16295 "template" keyword was present in order to improve error
16296 recovery. For example, given:
16297
16298 template <typename T> void f(T::X<int>);
16299
16300 we want to treat "X<int>" as a template-id. */
16301 if (is_declaration
16302 && !template_keyword_p
16303 && parser->scope && TYPE_P (parser->scope)
16304 && check_dependency_p
16305 && dependent_scope_p (parser->scope)
16306 /* Do not do this for dtors (or ctors), since they never
16307 need the template keyword before their name. */
16308 && !constructor_name_p (identifier, parser->scope))
16309 {
16310 cp_token_position start = 0;
16311
16312 /* Explain what went wrong. */
16313 error_at (token->location, "non-template %qD used as template",
16314 identifier);
16315 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16316 parser->scope, identifier);
16317 /* If parsing tentatively, find the location of the "<" token. */
16318 if (cp_parser_simulate_error (parser))
16319 start = cp_lexer_token_position (parser->lexer, true);
16320 /* Parse the template arguments so that we can issue error
16321 messages about them. */
16322 cp_lexer_consume_token (parser->lexer);
16323 cp_parser_enclosed_template_argument_list (parser);
16324 /* Skip tokens until we find a good place from which to
16325 continue parsing. */
16326 cp_parser_skip_to_closing_parenthesis (parser,
16327 /*recovering=*/true,
16328 /*or_comma=*/true,
16329 /*consume_paren=*/false);
16330 /* If parsing tentatively, permanently remove the
16331 template argument list. That will prevent duplicate
16332 error messages from being issued about the missing
16333 "template" keyword. */
16334 if (start)
16335 cp_lexer_purge_tokens_after (parser->lexer, start);
16336 if (is_identifier)
16337 *is_identifier = true;
16338 parser->context->object_type = NULL_TREE;
16339 return identifier;
16340 }
16341
16342 /* If the "template" keyword is present, then there is generally
16343 no point in doing name-lookup, so we just return IDENTIFIER.
16344 But, if the qualifying scope is non-dependent then we can
16345 (and must) do name-lookup normally. */
16346 if (template_keyword_p)
16347 {
16348 tree scope = (parser->scope ? parser->scope
16349 : parser->context->object_type);
16350 if (scope && TYPE_P (scope)
16351 && (!CLASS_TYPE_P (scope)
16352 || (check_dependency_p && dependent_type_p (scope))))
16353 {
16354 /* We're optimizing away the call to cp_parser_lookup_name, but
16355 we still need to do this. */
16356 parser->context->object_type = NULL_TREE;
16357 return identifier;
16358 }
16359 }
16360 }
16361
16362 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16363 const bool scoped_p = ((parser->scope ? parser->scope
16364 : parser->context->object_type) != NULL_TREE);
16365
16366 /* Look up the name. */
16367 decl = cp_parser_lookup_name (parser, identifier,
16368 tag_type,
16369 /*is_template=*/true,
16370 /*is_namespace=*/false,
16371 check_dependency_p,
16372 /*ambiguous_decls=*/NULL,
16373 token->location);
16374
16375 decl = strip_using_decl (decl);
16376
16377 /* If DECL is a template, then the name was a template-name. */
16378 if (TREE_CODE (decl) == TEMPLATE_DECL)
16379 {
16380 if (TREE_DEPRECATED (decl)
16381 && deprecated_state != DEPRECATED_SUPPRESS)
16382 warn_deprecated_use (decl, NULL_TREE);
16383 }
16384 else
16385 {
16386 /* The standard does not explicitly indicate whether a name that
16387 names a set of overloaded declarations, some of which are
16388 templates, is a template-name. However, such a name should
16389 be a template-name; otherwise, there is no way to form a
16390 template-id for the overloaded templates. */
16391 bool found = false;
16392
16393 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16394 !found && iter; ++iter)
16395 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16396 found = true;
16397
16398 if (!found
16399 && (cxx_dialect > cxx17)
16400 && !scoped_p
16401 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16402 {
16403 /* [temp.names] says "A name is also considered to refer to a template
16404 if it is an unqualified-id followed by a < and name lookup finds
16405 either one or more functions or finds nothing." */
16406
16407 /* The "more functions" case. Just use the OVERLOAD as normally.
16408 We don't use is_overloaded_fn here to avoid considering
16409 BASELINKs. */
16410 if (TREE_CODE (decl) == OVERLOAD
16411 /* Name lookup found one function. */
16412 || TREE_CODE (decl) == FUNCTION_DECL)
16413 found = true;
16414 /* Name lookup found nothing. */
16415 else if (decl == error_mark_node)
16416 return identifier;
16417 }
16418
16419 if (!found)
16420 {
16421 /* The name does not name a template. */
16422 cp_parser_error (parser, "expected template-name");
16423 return error_mark_node;
16424 }
16425 }
16426
16427 return decl;
16428 }
16429
16430 /* Parse a template-argument-list.
16431
16432 template-argument-list:
16433 template-argument ... [opt]
16434 template-argument-list , template-argument ... [opt]
16435
16436 Returns a TREE_VEC containing the arguments. */
16437
16438 static tree
16439 cp_parser_template_argument_list (cp_parser* parser)
16440 {
16441 tree fixed_args[10];
16442 unsigned n_args = 0;
16443 unsigned alloced = 10;
16444 tree *arg_ary = fixed_args;
16445 tree vec;
16446 bool saved_in_template_argument_list_p;
16447 bool saved_ice_p;
16448 bool saved_non_ice_p;
16449
16450 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16451 parser->in_template_argument_list_p = true;
16452 /* Even if the template-id appears in an integral
16453 constant-expression, the contents of the argument list do
16454 not. */
16455 saved_ice_p = parser->integral_constant_expression_p;
16456 parser->integral_constant_expression_p = false;
16457 saved_non_ice_p = parser->non_integral_constant_expression_p;
16458 parser->non_integral_constant_expression_p = false;
16459
16460 /* Parse the arguments. */
16461 do
16462 {
16463 tree argument;
16464
16465 if (n_args)
16466 /* Consume the comma. */
16467 cp_lexer_consume_token (parser->lexer);
16468
16469 /* Parse the template-argument. */
16470 argument = cp_parser_template_argument (parser);
16471
16472 /* If the next token is an ellipsis, we're expanding a template
16473 argument pack. */
16474 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16475 {
16476 if (argument == error_mark_node)
16477 {
16478 cp_token *token = cp_lexer_peek_token (parser->lexer);
16479 error_at (token->location,
16480 "expected parameter pack before %<...%>");
16481 }
16482 /* Consume the `...' token. */
16483 cp_lexer_consume_token (parser->lexer);
16484
16485 /* Make the argument into a TYPE_PACK_EXPANSION or
16486 EXPR_PACK_EXPANSION. */
16487 argument = make_pack_expansion (argument);
16488 }
16489
16490 if (n_args == alloced)
16491 {
16492 alloced *= 2;
16493
16494 if (arg_ary == fixed_args)
16495 {
16496 arg_ary = XNEWVEC (tree, alloced);
16497 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16498 }
16499 else
16500 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16501 }
16502 arg_ary[n_args++] = argument;
16503 }
16504 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16505
16506 vec = make_tree_vec (n_args);
16507
16508 while (n_args--)
16509 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16510
16511 if (arg_ary != fixed_args)
16512 free (arg_ary);
16513 parser->non_integral_constant_expression_p = saved_non_ice_p;
16514 parser->integral_constant_expression_p = saved_ice_p;
16515 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16516 if (CHECKING_P)
16517 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16518 return vec;
16519 }
16520
16521 /* Parse a template-argument.
16522
16523 template-argument:
16524 assignment-expression
16525 type-id
16526 id-expression
16527
16528 The representation is that of an assignment-expression, type-id, or
16529 id-expression -- except that the qualified id-expression is
16530 evaluated, so that the value returned is either a DECL or an
16531 OVERLOAD.
16532
16533 Although the standard says "assignment-expression", it forbids
16534 throw-expressions or assignments in the template argument.
16535 Therefore, we use "conditional-expression" instead. */
16536
16537 static tree
16538 cp_parser_template_argument (cp_parser* parser)
16539 {
16540 tree argument;
16541 bool template_p;
16542 bool address_p;
16543 bool maybe_type_id = false;
16544 cp_token *token = NULL, *argument_start_token = NULL;
16545 location_t loc = 0;
16546 cp_id_kind idk;
16547
16548 /* There's really no way to know what we're looking at, so we just
16549 try each alternative in order.
16550
16551 [temp.arg]
16552
16553 In a template-argument, an ambiguity between a type-id and an
16554 expression is resolved to a type-id, regardless of the form of
16555 the corresponding template-parameter.
16556
16557 Therefore, we try a type-id first. */
16558 cp_parser_parse_tentatively (parser);
16559 argument = cp_parser_template_type_arg (parser);
16560 /* If there was no error parsing the type-id but the next token is a
16561 '>>', our behavior depends on which dialect of C++ we're
16562 parsing. In C++98, we probably found a typo for '> >'. But there
16563 are type-id which are also valid expressions. For instance:
16564
16565 struct X { int operator >> (int); };
16566 template <int V> struct Foo {};
16567 Foo<X () >> 5> r;
16568
16569 Here 'X()' is a valid type-id of a function type, but the user just
16570 wanted to write the expression "X() >> 5". Thus, we remember that we
16571 found a valid type-id, but we still try to parse the argument as an
16572 expression to see what happens.
16573
16574 In C++0x, the '>>' will be considered two separate '>'
16575 tokens. */
16576 if (!cp_parser_error_occurred (parser)
16577 && cxx_dialect == cxx98
16578 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16579 {
16580 maybe_type_id = true;
16581 cp_parser_abort_tentative_parse (parser);
16582 }
16583 else
16584 {
16585 /* If the next token isn't a `,' or a `>', then this argument wasn't
16586 really finished. This means that the argument is not a valid
16587 type-id. */
16588 if (!cp_parser_next_token_ends_template_argument_p (parser))
16589 cp_parser_error (parser, "expected template-argument");
16590 /* If that worked, we're done. */
16591 if (cp_parser_parse_definitely (parser))
16592 return argument;
16593 }
16594 /* We're still not sure what the argument will be. */
16595 cp_parser_parse_tentatively (parser);
16596 /* Try a template. */
16597 argument_start_token = cp_lexer_peek_token (parser->lexer);
16598 argument = cp_parser_id_expression (parser,
16599 /*template_keyword_p=*/false,
16600 /*check_dependency_p=*/true,
16601 &template_p,
16602 /*declarator_p=*/false,
16603 /*optional_p=*/false);
16604 /* If the next token isn't a `,' or a `>', then this argument wasn't
16605 really finished. */
16606 if (!cp_parser_next_token_ends_template_argument_p (parser))
16607 cp_parser_error (parser, "expected template-argument");
16608 if (!cp_parser_error_occurred (parser))
16609 {
16610 /* Figure out what is being referred to. If the id-expression
16611 was for a class template specialization, then we will have a
16612 TYPE_DECL at this point. There is no need to do name lookup
16613 at this point in that case. */
16614 if (TREE_CODE (argument) != TYPE_DECL)
16615 argument = cp_parser_lookup_name (parser, argument,
16616 none_type,
16617 /*is_template=*/template_p,
16618 /*is_namespace=*/false,
16619 /*check_dependency=*/true,
16620 /*ambiguous_decls=*/NULL,
16621 argument_start_token->location);
16622 /* Handle a constrained-type-specifier for a non-type template
16623 parameter. */
16624 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16625 argument = decl;
16626 else if (TREE_CODE (argument) != TEMPLATE_DECL
16627 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16628 cp_parser_error (parser, "expected template-name");
16629 }
16630 if (cp_parser_parse_definitely (parser))
16631 {
16632 if (TREE_DEPRECATED (argument))
16633 warn_deprecated_use (argument, NULL_TREE);
16634 return argument;
16635 }
16636 /* It must be a non-type argument. In C++17 any constant-expression is
16637 allowed. */
16638 if (cxx_dialect > cxx14)
16639 goto general_expr;
16640
16641 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16642
16643 -- an integral constant-expression of integral or enumeration
16644 type; or
16645
16646 -- the name of a non-type template-parameter; or
16647
16648 -- the name of an object or function with external linkage...
16649
16650 -- the address of an object or function with external linkage...
16651
16652 -- a pointer to member... */
16653 /* Look for a non-type template parameter. */
16654 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16655 {
16656 cp_parser_parse_tentatively (parser);
16657 argument = cp_parser_primary_expression (parser,
16658 /*address_p=*/false,
16659 /*cast_p=*/false,
16660 /*template_arg_p=*/true,
16661 &idk);
16662 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16663 || !cp_parser_next_token_ends_template_argument_p (parser))
16664 cp_parser_simulate_error (parser);
16665 if (cp_parser_parse_definitely (parser))
16666 return argument;
16667 }
16668
16669 /* If the next token is "&", the argument must be the address of an
16670 object or function with external linkage. */
16671 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16672 if (address_p)
16673 {
16674 loc = cp_lexer_peek_token (parser->lexer)->location;
16675 cp_lexer_consume_token (parser->lexer);
16676 }
16677 /* See if we might have an id-expression. */
16678 token = cp_lexer_peek_token (parser->lexer);
16679 if (token->type == CPP_NAME
16680 || token->keyword == RID_OPERATOR
16681 || token->type == CPP_SCOPE
16682 || token->type == CPP_TEMPLATE_ID
16683 || token->type == CPP_NESTED_NAME_SPECIFIER)
16684 {
16685 cp_parser_parse_tentatively (parser);
16686 argument = cp_parser_primary_expression (parser,
16687 address_p,
16688 /*cast_p=*/false,
16689 /*template_arg_p=*/true,
16690 &idk);
16691 if (cp_parser_error_occurred (parser)
16692 || !cp_parser_next_token_ends_template_argument_p (parser))
16693 cp_parser_abort_tentative_parse (parser);
16694 else
16695 {
16696 tree probe;
16697
16698 if (INDIRECT_REF_P (argument))
16699 {
16700 /* Strip the dereference temporarily. */
16701 gcc_assert (REFERENCE_REF_P (argument));
16702 argument = TREE_OPERAND (argument, 0);
16703 }
16704
16705 /* If we're in a template, we represent a qualified-id referring
16706 to a static data member as a SCOPE_REF even if the scope isn't
16707 dependent so that we can check access control later. */
16708 probe = argument;
16709 if (TREE_CODE (probe) == SCOPE_REF)
16710 probe = TREE_OPERAND (probe, 1);
16711 if (VAR_P (probe))
16712 {
16713 /* A variable without external linkage might still be a
16714 valid constant-expression, so no error is issued here
16715 if the external-linkage check fails. */
16716 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16717 cp_parser_simulate_error (parser);
16718 }
16719 else if (is_overloaded_fn (argument))
16720 /* All overloaded functions are allowed; if the external
16721 linkage test does not pass, an error will be issued
16722 later. */
16723 ;
16724 else if (address_p
16725 && (TREE_CODE (argument) == OFFSET_REF
16726 || TREE_CODE (argument) == SCOPE_REF))
16727 /* A pointer-to-member. */
16728 ;
16729 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16730 ;
16731 else
16732 cp_parser_simulate_error (parser);
16733
16734 if (cp_parser_parse_definitely (parser))
16735 {
16736 if (address_p)
16737 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16738 tf_warning_or_error);
16739 else
16740 argument = convert_from_reference (argument);
16741 return argument;
16742 }
16743 }
16744 }
16745 /* If the argument started with "&", there are no other valid
16746 alternatives at this point. */
16747 if (address_p)
16748 {
16749 cp_parser_error (parser, "invalid non-type template argument");
16750 return error_mark_node;
16751 }
16752
16753 general_expr:
16754 /* If the argument wasn't successfully parsed as a type-id followed
16755 by '>>', the argument can only be a constant expression now.
16756 Otherwise, we try parsing the constant-expression tentatively,
16757 because the argument could really be a type-id. */
16758 if (maybe_type_id)
16759 cp_parser_parse_tentatively (parser);
16760
16761 if (cxx_dialect <= cxx14)
16762 argument = cp_parser_constant_expression (parser);
16763 else
16764 {
16765 /* With C++17 generalized non-type template arguments we need to handle
16766 lvalue constant expressions, too. */
16767 argument = cp_parser_assignment_expression (parser);
16768 require_potential_constant_expression (argument);
16769 }
16770
16771 if (!maybe_type_id)
16772 return argument;
16773 if (!cp_parser_next_token_ends_template_argument_p (parser))
16774 cp_parser_error (parser, "expected template-argument");
16775 if (cp_parser_parse_definitely (parser))
16776 return argument;
16777 /* We did our best to parse the argument as a non type-id, but that
16778 was the only alternative that matched (albeit with a '>' after
16779 it). We can assume it's just a typo from the user, and a
16780 diagnostic will then be issued. */
16781 return cp_parser_template_type_arg (parser);
16782 }
16783
16784 /* Parse an explicit-instantiation.
16785
16786 explicit-instantiation:
16787 template declaration
16788
16789 Although the standard says `declaration', what it really means is:
16790
16791 explicit-instantiation:
16792 template decl-specifier-seq [opt] declarator [opt] ;
16793
16794 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16795 supposed to be allowed. A defect report has been filed about this
16796 issue.
16797
16798 GNU Extension:
16799
16800 explicit-instantiation:
16801 storage-class-specifier template
16802 decl-specifier-seq [opt] declarator [opt] ;
16803 function-specifier template
16804 decl-specifier-seq [opt] declarator [opt] ; */
16805
16806 static void
16807 cp_parser_explicit_instantiation (cp_parser* parser)
16808 {
16809 int declares_class_or_enum;
16810 cp_decl_specifier_seq decl_specifiers;
16811 tree extension_specifier = NULL_TREE;
16812
16813 timevar_push (TV_TEMPLATE_INST);
16814
16815 /* Look for an (optional) storage-class-specifier or
16816 function-specifier. */
16817 if (cp_parser_allow_gnu_extensions_p (parser))
16818 {
16819 extension_specifier
16820 = cp_parser_storage_class_specifier_opt (parser);
16821 if (!extension_specifier)
16822 extension_specifier
16823 = cp_parser_function_specifier_opt (parser,
16824 /*decl_specs=*/NULL);
16825 }
16826
16827 /* Look for the `template' keyword. */
16828 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16829 /* Let the front end know that we are processing an explicit
16830 instantiation. */
16831 begin_explicit_instantiation ();
16832 /* [temp.explicit] says that we are supposed to ignore access
16833 control while processing explicit instantiation directives. */
16834 push_deferring_access_checks (dk_no_check);
16835 /* Parse a decl-specifier-seq. */
16836 cp_parser_decl_specifier_seq (parser,
16837 CP_PARSER_FLAGS_OPTIONAL,
16838 &decl_specifiers,
16839 &declares_class_or_enum);
16840 /* If there was exactly one decl-specifier, and it declared a class,
16841 and there's no declarator, then we have an explicit type
16842 instantiation. */
16843 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16844 {
16845 tree type;
16846
16847 type = check_tag_decl (&decl_specifiers,
16848 /*explicit_type_instantiation_p=*/true);
16849 /* Turn access control back on for names used during
16850 template instantiation. */
16851 pop_deferring_access_checks ();
16852 if (type)
16853 do_type_instantiation (type, extension_specifier,
16854 /*complain=*/tf_error);
16855 }
16856 else
16857 {
16858 cp_declarator *declarator;
16859 tree decl;
16860
16861 /* Parse the declarator. */
16862 declarator
16863 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16864 /*ctor_dtor_or_conv_p=*/NULL,
16865 /*parenthesized_p=*/NULL,
16866 /*member_p=*/false,
16867 /*friend_p=*/false);
16868 if (declares_class_or_enum & 2)
16869 cp_parser_check_for_definition_in_return_type (declarator,
16870 decl_specifiers.type,
16871 decl_specifiers.locations[ds_type_spec]);
16872 if (declarator != cp_error_declarator)
16873 {
16874 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16875 permerror (decl_specifiers.locations[ds_inline],
16876 "explicit instantiation shall not use"
16877 " %<inline%> specifier");
16878 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16879 permerror (decl_specifiers.locations[ds_constexpr],
16880 "explicit instantiation shall not use"
16881 " %<constexpr%> specifier");
16882
16883 decl = grokdeclarator (declarator, &decl_specifiers,
16884 NORMAL, 0, &decl_specifiers.attributes);
16885 /* Turn access control back on for names used during
16886 template instantiation. */
16887 pop_deferring_access_checks ();
16888 /* Do the explicit instantiation. */
16889 do_decl_instantiation (decl, extension_specifier);
16890 }
16891 else
16892 {
16893 pop_deferring_access_checks ();
16894 /* Skip the body of the explicit instantiation. */
16895 cp_parser_skip_to_end_of_statement (parser);
16896 }
16897 }
16898 /* We're done with the instantiation. */
16899 end_explicit_instantiation ();
16900
16901 cp_parser_consume_semicolon_at_end_of_statement (parser);
16902
16903 timevar_pop (TV_TEMPLATE_INST);
16904 }
16905
16906 /* Parse an explicit-specialization.
16907
16908 explicit-specialization:
16909 template < > declaration
16910
16911 Although the standard says `declaration', what it really means is:
16912
16913 explicit-specialization:
16914 template <> decl-specifier [opt] init-declarator [opt] ;
16915 template <> function-definition
16916 template <> explicit-specialization
16917 template <> template-declaration */
16918
16919 static void
16920 cp_parser_explicit_specialization (cp_parser* parser)
16921 {
16922 bool need_lang_pop;
16923 cp_token *token = cp_lexer_peek_token (parser->lexer);
16924
16925 /* Look for the `template' keyword. */
16926 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16927 /* Look for the `<'. */
16928 cp_parser_require (parser, CPP_LESS, RT_LESS);
16929 /* Look for the `>'. */
16930 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16931 /* We have processed another parameter list. */
16932 ++parser->num_template_parameter_lists;
16933 /* [temp]
16934
16935 A template ... explicit specialization ... shall not have C
16936 linkage. */
16937 if (current_lang_name == lang_name_c)
16938 {
16939 error_at (token->location, "template specialization with C linkage");
16940 maybe_show_extern_c_location ();
16941 /* Give it C++ linkage to avoid confusing other parts of the
16942 front end. */
16943 push_lang_context (lang_name_cplusplus);
16944 need_lang_pop = true;
16945 }
16946 else
16947 need_lang_pop = false;
16948 /* Let the front end know that we are beginning a specialization. */
16949 if (!begin_specialization ())
16950 {
16951 end_specialization ();
16952 return;
16953 }
16954
16955 /* If the next keyword is `template', we need to figure out whether
16956 or not we're looking a template-declaration. */
16957 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16958 {
16959 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16960 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16961 cp_parser_template_declaration_after_export (parser,
16962 /*member_p=*/false);
16963 else
16964 cp_parser_explicit_specialization (parser);
16965 }
16966 else
16967 /* Parse the dependent declaration. */
16968 cp_parser_single_declaration (parser,
16969 /*checks=*/NULL,
16970 /*member_p=*/false,
16971 /*explicit_specialization_p=*/true,
16972 /*friend_p=*/NULL);
16973 /* We're done with the specialization. */
16974 end_specialization ();
16975 /* For the erroneous case of a template with C linkage, we pushed an
16976 implicit C++ linkage scope; exit that scope now. */
16977 if (need_lang_pop)
16978 pop_lang_context ();
16979 /* We're done with this parameter list. */
16980 --parser->num_template_parameter_lists;
16981 }
16982
16983 /* Parse a type-specifier.
16984
16985 type-specifier:
16986 simple-type-specifier
16987 class-specifier
16988 enum-specifier
16989 elaborated-type-specifier
16990 cv-qualifier
16991
16992 GNU Extension:
16993
16994 type-specifier:
16995 __complex__
16996
16997 Returns a representation of the type-specifier. For a
16998 class-specifier, enum-specifier, or elaborated-type-specifier, a
16999 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17000
17001 The parser flags FLAGS is used to control type-specifier parsing.
17002
17003 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17004 in a decl-specifier-seq.
17005
17006 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17007 class-specifier, enum-specifier, or elaborated-type-specifier, then
17008 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17009 if a type is declared; 2 if it is defined. Otherwise, it is set to
17010 zero.
17011
17012 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17013 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17014 is set to FALSE. */
17015
17016 static tree
17017 cp_parser_type_specifier (cp_parser* parser,
17018 cp_parser_flags flags,
17019 cp_decl_specifier_seq *decl_specs,
17020 bool is_declaration,
17021 int* declares_class_or_enum,
17022 bool* is_cv_qualifier)
17023 {
17024 tree type_spec = NULL_TREE;
17025 cp_token *token;
17026 enum rid keyword;
17027 cp_decl_spec ds = ds_last;
17028
17029 /* Assume this type-specifier does not declare a new type. */
17030 if (declares_class_or_enum)
17031 *declares_class_or_enum = 0;
17032 /* And that it does not specify a cv-qualifier. */
17033 if (is_cv_qualifier)
17034 *is_cv_qualifier = false;
17035 /* Peek at the next token. */
17036 token = cp_lexer_peek_token (parser->lexer);
17037
17038 /* If we're looking at a keyword, we can use that to guide the
17039 production we choose. */
17040 keyword = token->keyword;
17041 switch (keyword)
17042 {
17043 case RID_ENUM:
17044 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17045 goto elaborated_type_specifier;
17046
17047 /* Look for the enum-specifier. */
17048 type_spec = cp_parser_enum_specifier (parser);
17049 /* If that worked, we're done. */
17050 if (type_spec)
17051 {
17052 if (declares_class_or_enum)
17053 *declares_class_or_enum = 2;
17054 if (decl_specs)
17055 cp_parser_set_decl_spec_type (decl_specs,
17056 type_spec,
17057 token,
17058 /*type_definition_p=*/true);
17059 return type_spec;
17060 }
17061 else
17062 goto elaborated_type_specifier;
17063
17064 /* Any of these indicate either a class-specifier, or an
17065 elaborated-type-specifier. */
17066 case RID_CLASS:
17067 case RID_STRUCT:
17068 case RID_UNION:
17069 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17070 goto elaborated_type_specifier;
17071
17072 /* Parse tentatively so that we can back up if we don't find a
17073 class-specifier. */
17074 cp_parser_parse_tentatively (parser);
17075 /* Look for the class-specifier. */
17076 type_spec = cp_parser_class_specifier (parser);
17077 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17078 /* If that worked, we're done. */
17079 if (cp_parser_parse_definitely (parser))
17080 {
17081 if (declares_class_or_enum)
17082 *declares_class_or_enum = 2;
17083 if (decl_specs)
17084 cp_parser_set_decl_spec_type (decl_specs,
17085 type_spec,
17086 token,
17087 /*type_definition_p=*/true);
17088 return type_spec;
17089 }
17090
17091 /* Fall through. */
17092 elaborated_type_specifier:
17093 /* We're declaring (not defining) a class or enum. */
17094 if (declares_class_or_enum)
17095 *declares_class_or_enum = 1;
17096
17097 /* Fall through. */
17098 case RID_TYPENAME:
17099 /* Look for an elaborated-type-specifier. */
17100 type_spec
17101 = (cp_parser_elaborated_type_specifier
17102 (parser,
17103 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17104 is_declaration));
17105 if (decl_specs)
17106 cp_parser_set_decl_spec_type (decl_specs,
17107 type_spec,
17108 token,
17109 /*type_definition_p=*/false);
17110 return type_spec;
17111
17112 case RID_CONST:
17113 ds = ds_const;
17114 if (is_cv_qualifier)
17115 *is_cv_qualifier = true;
17116 break;
17117
17118 case RID_VOLATILE:
17119 ds = ds_volatile;
17120 if (is_cv_qualifier)
17121 *is_cv_qualifier = true;
17122 break;
17123
17124 case RID_RESTRICT:
17125 ds = ds_restrict;
17126 if (is_cv_qualifier)
17127 *is_cv_qualifier = true;
17128 break;
17129
17130 case RID_COMPLEX:
17131 /* The `__complex__' keyword is a GNU extension. */
17132 ds = ds_complex;
17133 break;
17134
17135 default:
17136 break;
17137 }
17138
17139 /* Handle simple keywords. */
17140 if (ds != ds_last)
17141 {
17142 if (decl_specs)
17143 {
17144 set_and_check_decl_spec_loc (decl_specs, ds, token);
17145 decl_specs->any_specifiers_p = true;
17146 }
17147 return cp_lexer_consume_token (parser->lexer)->u.value;
17148 }
17149
17150 /* If we do not already have a type-specifier, assume we are looking
17151 at a simple-type-specifier. */
17152 type_spec = cp_parser_simple_type_specifier (parser,
17153 decl_specs,
17154 flags);
17155
17156 /* If we didn't find a type-specifier, and a type-specifier was not
17157 optional in this context, issue an error message. */
17158 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17159 {
17160 cp_parser_error (parser, "expected type specifier");
17161 return error_mark_node;
17162 }
17163
17164 return type_spec;
17165 }
17166
17167 /* Parse a simple-type-specifier.
17168
17169 simple-type-specifier:
17170 :: [opt] nested-name-specifier [opt] type-name
17171 :: [opt] nested-name-specifier template template-id
17172 char
17173 wchar_t
17174 bool
17175 short
17176 int
17177 long
17178 signed
17179 unsigned
17180 float
17181 double
17182 void
17183
17184 C++11 Extension:
17185
17186 simple-type-specifier:
17187 auto
17188 decltype ( expression )
17189 char16_t
17190 char32_t
17191 __underlying_type ( type-id )
17192
17193 C++17 extension:
17194
17195 nested-name-specifier(opt) template-name
17196
17197 GNU Extension:
17198
17199 simple-type-specifier:
17200 __int128
17201 __typeof__ unary-expression
17202 __typeof__ ( type-id )
17203 __typeof__ ( type-id ) { initializer-list , [opt] }
17204
17205 Concepts Extension:
17206
17207 simple-type-specifier:
17208 constrained-type-specifier
17209
17210 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17211 appropriately updated. */
17212
17213 static tree
17214 cp_parser_simple_type_specifier (cp_parser* parser,
17215 cp_decl_specifier_seq *decl_specs,
17216 cp_parser_flags flags)
17217 {
17218 tree type = NULL_TREE;
17219 cp_token *token;
17220 int idx;
17221
17222 /* Peek at the next token. */
17223 token = cp_lexer_peek_token (parser->lexer);
17224
17225 /* If we're looking at a keyword, things are easy. */
17226 switch (token->keyword)
17227 {
17228 case RID_CHAR:
17229 if (decl_specs)
17230 decl_specs->explicit_char_p = true;
17231 type = char_type_node;
17232 break;
17233 case RID_CHAR16:
17234 type = char16_type_node;
17235 break;
17236 case RID_CHAR32:
17237 type = char32_type_node;
17238 break;
17239 case RID_WCHAR:
17240 type = wchar_type_node;
17241 break;
17242 case RID_BOOL:
17243 type = boolean_type_node;
17244 break;
17245 case RID_SHORT:
17246 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17247 type = short_integer_type_node;
17248 break;
17249 case RID_INT:
17250 if (decl_specs)
17251 decl_specs->explicit_int_p = true;
17252 type = integer_type_node;
17253 break;
17254 case RID_INT_N_0:
17255 case RID_INT_N_1:
17256 case RID_INT_N_2:
17257 case RID_INT_N_3:
17258 idx = token->keyword - RID_INT_N_0;
17259 if (! int_n_enabled_p [idx])
17260 break;
17261 if (decl_specs)
17262 {
17263 decl_specs->explicit_intN_p = true;
17264 decl_specs->int_n_idx = idx;
17265 }
17266 type = int_n_trees [idx].signed_type;
17267 break;
17268 case RID_LONG:
17269 if (decl_specs)
17270 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17271 type = long_integer_type_node;
17272 break;
17273 case RID_SIGNED:
17274 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17275 type = integer_type_node;
17276 break;
17277 case RID_UNSIGNED:
17278 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17279 type = unsigned_type_node;
17280 break;
17281 case RID_FLOAT:
17282 type = float_type_node;
17283 break;
17284 case RID_DOUBLE:
17285 type = double_type_node;
17286 break;
17287 case RID_VOID:
17288 type = void_type_node;
17289 break;
17290
17291 case RID_AUTO:
17292 maybe_warn_cpp0x (CPP0X_AUTO);
17293 if (parser->auto_is_implicit_function_template_parm_p)
17294 {
17295 /* The 'auto' might be the placeholder return type for a function decl
17296 with trailing return type. */
17297 bool have_trailing_return_fn_decl = false;
17298
17299 cp_parser_parse_tentatively (parser);
17300 cp_lexer_consume_token (parser->lexer);
17301 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17302 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17303 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17304 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17305 {
17306 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17307 {
17308 cp_lexer_consume_token (parser->lexer);
17309 cp_parser_skip_to_closing_parenthesis (parser,
17310 /*recovering*/false,
17311 /*or_comma*/false,
17312 /*consume_paren*/true);
17313 continue;
17314 }
17315
17316 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17317 {
17318 have_trailing_return_fn_decl = true;
17319 break;
17320 }
17321
17322 cp_lexer_consume_token (parser->lexer);
17323 }
17324 cp_parser_abort_tentative_parse (parser);
17325
17326 if (have_trailing_return_fn_decl)
17327 {
17328 type = make_auto ();
17329 break;
17330 }
17331
17332 if (cxx_dialect >= cxx14)
17333 {
17334 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17335 type = TREE_TYPE (type);
17336 }
17337 else
17338 type = error_mark_node;
17339
17340 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17341 {
17342 if (cxx_dialect < cxx14)
17343 error_at (token->location,
17344 "use of %<auto%> in lambda parameter declaration "
17345 "only available with "
17346 "-std=c++14 or -std=gnu++14");
17347 }
17348 else if (cxx_dialect < cxx14)
17349 error_at (token->location,
17350 "use of %<auto%> in parameter declaration "
17351 "only available with "
17352 "-std=c++14 or -std=gnu++14");
17353 else if (!flag_concepts)
17354 pedwarn (token->location, 0,
17355 "use of %<auto%> in parameter declaration "
17356 "only available with -fconcepts");
17357 }
17358 else
17359 type = make_auto ();
17360 break;
17361
17362 case RID_DECLTYPE:
17363 /* Since DR 743, decltype can either be a simple-type-specifier by
17364 itself or begin a nested-name-specifier. Parsing it will replace
17365 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17366 handling below decide what to do. */
17367 cp_parser_decltype (parser);
17368 cp_lexer_set_token_position (parser->lexer, token);
17369 break;
17370
17371 case RID_TYPEOF:
17372 /* Consume the `typeof' token. */
17373 cp_lexer_consume_token (parser->lexer);
17374 /* Parse the operand to `typeof'. */
17375 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17376 /* If it is not already a TYPE, take its type. */
17377 if (!TYPE_P (type))
17378 type = finish_typeof (type);
17379
17380 if (decl_specs)
17381 cp_parser_set_decl_spec_type (decl_specs, type,
17382 token,
17383 /*type_definition_p=*/false);
17384
17385 return type;
17386
17387 case RID_UNDERLYING_TYPE:
17388 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17389 if (decl_specs)
17390 cp_parser_set_decl_spec_type (decl_specs, type,
17391 token,
17392 /*type_definition_p=*/false);
17393
17394 return type;
17395
17396 case RID_BASES:
17397 case RID_DIRECT_BASES:
17398 type = cp_parser_trait_expr (parser, token->keyword);
17399 if (decl_specs)
17400 cp_parser_set_decl_spec_type (decl_specs, type,
17401 token,
17402 /*type_definition_p=*/false);
17403 return type;
17404 default:
17405 break;
17406 }
17407
17408 /* If token is an already-parsed decltype not followed by ::,
17409 it's a simple-type-specifier. */
17410 if (token->type == CPP_DECLTYPE
17411 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17412 {
17413 type = saved_checks_value (token->u.tree_check_value);
17414 if (decl_specs)
17415 {
17416 cp_parser_set_decl_spec_type (decl_specs, type,
17417 token,
17418 /*type_definition_p=*/false);
17419 /* Remember that we are handling a decltype in order to
17420 implement the resolution of DR 1510 when the argument
17421 isn't instantiation dependent. */
17422 decl_specs->decltype_p = true;
17423 }
17424 cp_lexer_consume_token (parser->lexer);
17425 return type;
17426 }
17427
17428 /* If the type-specifier was for a built-in type, we're done. */
17429 if (type)
17430 {
17431 /* Record the type. */
17432 if (decl_specs
17433 && (token->keyword != RID_SIGNED
17434 && token->keyword != RID_UNSIGNED
17435 && token->keyword != RID_SHORT
17436 && token->keyword != RID_LONG))
17437 cp_parser_set_decl_spec_type (decl_specs,
17438 type,
17439 token,
17440 /*type_definition_p=*/false);
17441 if (decl_specs)
17442 decl_specs->any_specifiers_p = true;
17443
17444 /* Consume the token. */
17445 cp_lexer_consume_token (parser->lexer);
17446
17447 if (type == error_mark_node)
17448 return error_mark_node;
17449
17450 /* There is no valid C++ program where a non-template type is
17451 followed by a "<". That usually indicates that the user thought
17452 that the type was a template. */
17453 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17454 token->location);
17455
17456 return TYPE_NAME (type);
17457 }
17458
17459 /* The type-specifier must be a user-defined type. */
17460 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17461 {
17462 bool qualified_p;
17463 bool global_p;
17464
17465 /* Don't gobble tokens or issue error messages if this is an
17466 optional type-specifier. */
17467 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17468 cp_parser_parse_tentatively (parser);
17469
17470 token = cp_lexer_peek_token (parser->lexer);
17471
17472 /* Look for the optional `::' operator. */
17473 global_p
17474 = (cp_parser_global_scope_opt (parser,
17475 /*current_scope_valid_p=*/false)
17476 != NULL_TREE);
17477 /* Look for the nested-name specifier. */
17478 qualified_p
17479 = (cp_parser_nested_name_specifier_opt (parser,
17480 /*typename_keyword_p=*/false,
17481 /*check_dependency_p=*/true,
17482 /*type_p=*/false,
17483 /*is_declaration=*/false)
17484 != NULL_TREE);
17485 /* If we have seen a nested-name-specifier, and the next token
17486 is `template', then we are using the template-id production. */
17487 if (parser->scope
17488 && cp_parser_optional_template_keyword (parser))
17489 {
17490 /* Look for the template-id. */
17491 type = cp_parser_template_id (parser,
17492 /*template_keyword_p=*/true,
17493 /*check_dependency_p=*/true,
17494 none_type,
17495 /*is_declaration=*/false);
17496 /* If the template-id did not name a type, we are out of
17497 luck. */
17498 if (TREE_CODE (type) != TYPE_DECL)
17499 {
17500 cp_parser_error (parser, "expected template-id for type");
17501 type = NULL_TREE;
17502 }
17503 }
17504 /* Otherwise, look for a type-name. */
17505 else
17506 type = cp_parser_type_name (parser);
17507 /* Keep track of all name-lookups performed in class scopes. */
17508 if (type
17509 && !global_p
17510 && !qualified_p
17511 && TREE_CODE (type) == TYPE_DECL
17512 && identifier_p (DECL_NAME (type)))
17513 maybe_note_name_used_in_class (DECL_NAME (type), type);
17514 /* If it didn't work out, we don't have a TYPE. */
17515 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17516 && !cp_parser_parse_definitely (parser))
17517 type = NULL_TREE;
17518 if (!type && cxx_dialect >= cxx17)
17519 {
17520 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17521 cp_parser_parse_tentatively (parser);
17522
17523 cp_parser_global_scope_opt (parser,
17524 /*current_scope_valid_p=*/false);
17525 cp_parser_nested_name_specifier_opt (parser,
17526 /*typename_keyword_p=*/false,
17527 /*check_dependency_p=*/true,
17528 /*type_p=*/false,
17529 /*is_declaration=*/false);
17530 tree name = cp_parser_identifier (parser);
17531 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17532 && parser->scope != error_mark_node)
17533 {
17534 tree tmpl = cp_parser_lookup_name (parser, name,
17535 none_type,
17536 /*is_template=*/false,
17537 /*is_namespace=*/false,
17538 /*check_dependency=*/true,
17539 /*ambiguous_decls=*/NULL,
17540 token->location);
17541 if (tmpl && tmpl != error_mark_node
17542 && (DECL_CLASS_TEMPLATE_P (tmpl)
17543 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17544 type = make_template_placeholder (tmpl);
17545 else
17546 {
17547 type = error_mark_node;
17548 if (!cp_parser_simulate_error (parser))
17549 cp_parser_name_lookup_error (parser, name, tmpl,
17550 NLE_TYPE, token->location);
17551 }
17552 }
17553 else
17554 type = error_mark_node;
17555
17556 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17557 && !cp_parser_parse_definitely (parser))
17558 type = NULL_TREE;
17559 }
17560 if (type && decl_specs)
17561 cp_parser_set_decl_spec_type (decl_specs, type,
17562 token,
17563 /*type_definition_p=*/false);
17564 }
17565
17566 /* If we didn't get a type-name, issue an error message. */
17567 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17568 {
17569 cp_parser_error (parser, "expected type-name");
17570 return error_mark_node;
17571 }
17572
17573 if (type && type != error_mark_node)
17574 {
17575 /* See if TYPE is an Objective-C type, and if so, parse and
17576 accept any protocol references following it. Do this before
17577 the cp_parser_check_for_invalid_template_id() call, because
17578 Objective-C types can be followed by '<...>' which would
17579 enclose protocol names rather than template arguments, and so
17580 everything is fine. */
17581 if (c_dialect_objc () && !parser->scope
17582 && (objc_is_id (type) || objc_is_class_name (type)))
17583 {
17584 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17585 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17586
17587 /* Clobber the "unqualified" type previously entered into
17588 DECL_SPECS with the new, improved protocol-qualified version. */
17589 if (decl_specs)
17590 decl_specs->type = qual_type;
17591
17592 return qual_type;
17593 }
17594
17595 /* There is no valid C++ program where a non-template type is
17596 followed by a "<". That usually indicates that the user
17597 thought that the type was a template. */
17598 cp_parser_check_for_invalid_template_id (parser, type,
17599 none_type,
17600 token->location);
17601 }
17602
17603 return type;
17604 }
17605
17606 /* Parse a type-name.
17607
17608 type-name:
17609 class-name
17610 enum-name
17611 typedef-name
17612 simple-template-id [in c++0x]
17613
17614 enum-name:
17615 identifier
17616
17617 typedef-name:
17618 identifier
17619
17620 Concepts:
17621
17622 type-name:
17623 concept-name
17624 partial-concept-id
17625
17626 concept-name:
17627 identifier
17628
17629 Returns a TYPE_DECL for the type. */
17630
17631 static tree
17632 cp_parser_type_name (cp_parser* parser)
17633 {
17634 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17635 }
17636
17637 /* See above. */
17638 static tree
17639 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17640 {
17641 tree type_decl;
17642
17643 /* We can't know yet whether it is a class-name or not. */
17644 cp_parser_parse_tentatively (parser);
17645 /* Try a class-name. */
17646 type_decl = cp_parser_class_name (parser,
17647 typename_keyword_p,
17648 /*template_keyword_p=*/false,
17649 none_type,
17650 /*check_dependency_p=*/true,
17651 /*class_head_p=*/false,
17652 /*is_declaration=*/false);
17653 /* If it's not a class-name, keep looking. */
17654 if (!cp_parser_parse_definitely (parser))
17655 {
17656 if (cxx_dialect < cxx11)
17657 /* It must be a typedef-name or an enum-name. */
17658 return cp_parser_nonclass_name (parser);
17659
17660 cp_parser_parse_tentatively (parser);
17661 /* It is either a simple-template-id representing an
17662 instantiation of an alias template... */
17663 type_decl = cp_parser_template_id (parser,
17664 /*template_keyword_p=*/false,
17665 /*check_dependency_p=*/true,
17666 none_type,
17667 /*is_declaration=*/false);
17668 /* Note that this must be an instantiation of an alias template
17669 because [temp.names]/6 says:
17670
17671 A template-id that names an alias template specialization
17672 is a type-name.
17673
17674 Whereas [temp.names]/7 says:
17675
17676 A simple-template-id that names a class template
17677 specialization is a class-name.
17678
17679 With concepts, this could also be a partial-concept-id that
17680 declares a non-type template parameter. */
17681 if (type_decl != NULL_TREE
17682 && TREE_CODE (type_decl) == TYPE_DECL
17683 && TYPE_DECL_ALIAS_P (type_decl))
17684 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17685 else if (is_constrained_parameter (type_decl))
17686 /* Don't do anything. */ ;
17687 else
17688 cp_parser_simulate_error (parser);
17689
17690 if (!cp_parser_parse_definitely (parser))
17691 /* ... Or a typedef-name or an enum-name. */
17692 return cp_parser_nonclass_name (parser);
17693 }
17694
17695 return type_decl;
17696 }
17697
17698 /* Check if DECL and ARGS can form a constrained-type-specifier.
17699 If ARGS is non-null, we try to form a concept check of the
17700 form DECL<?, ARGS> where ? is a wildcard that matches any
17701 kind of template argument. If ARGS is NULL, then we try to
17702 form a concept check of the form DECL<?>. */
17703
17704 static tree
17705 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17706 tree decl, tree args)
17707 {
17708 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17709
17710 /* If we a constrained-type-specifier cannot be deduced. */
17711 if (parser->prevent_constrained_type_specifiers)
17712 return NULL_TREE;
17713
17714 /* A constrained type specifier can only be found in an
17715 overload set or as a reference to a template declaration.
17716
17717 FIXME: This might be masking a bug. It's possible that
17718 that the deduction below is causing template specializations
17719 to be formed with the wildcard as an argument. */
17720 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17721 return NULL_TREE;
17722
17723 /* Try to build a call expression that evaluates the
17724 concept. This can fail if the overload set refers
17725 only to non-templates. */
17726 tree placeholder = build_nt (WILDCARD_DECL);
17727 tree check = build_concept_check (decl, placeholder, args);
17728 if (check == error_mark_node)
17729 return NULL_TREE;
17730
17731 /* Deduce the checked constraint and the prototype parameter.
17732
17733 FIXME: In certain cases, failure to deduce should be a
17734 diagnosable error. */
17735 tree conc;
17736 tree proto;
17737 if (!deduce_constrained_parameter (check, conc, proto))
17738 return NULL_TREE;
17739
17740 /* In template parameter scope, this results in a constrained
17741 parameter. Return a descriptor of that parm. */
17742 if (processing_template_parmlist)
17743 return build_constrained_parameter (conc, proto, args);
17744
17745 /* In a parameter-declaration-clause, constrained-type
17746 specifiers result in invented template parameters. */
17747 if (parser->auto_is_implicit_function_template_parm_p)
17748 {
17749 tree x = build_constrained_parameter (conc, proto, args);
17750 return synthesize_implicit_template_parm (parser, x);
17751 }
17752 else
17753 {
17754 /* Otherwise, we're in a context where the constrained
17755 type name is deduced and the constraint applies
17756 after deduction. */
17757 return make_constrained_auto (conc, args);
17758 }
17759
17760 return NULL_TREE;
17761 }
17762
17763 /* If DECL refers to a concept, return a TYPE_DECL representing
17764 the result of using the constrained type specifier in the
17765 current context. DECL refers to a concept if
17766
17767 - it is an overload set containing a function concept taking a single
17768 type argument, or
17769
17770 - it is a variable concept taking a single type argument. */
17771
17772 static tree
17773 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17774 {
17775 if (flag_concepts
17776 && (TREE_CODE (decl) == OVERLOAD
17777 || BASELINK_P (decl)
17778 || variable_concept_p (decl)))
17779 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17780 else
17781 return NULL_TREE;
17782 }
17783
17784 /* Check if DECL and ARGS form a partial-concept-id. If so,
17785 assign ID to the resulting constrained placeholder.
17786
17787 Returns true if the partial-concept-id designates a placeholder
17788 and false otherwise. Note that *id is set to NULL_TREE in
17789 this case. */
17790
17791 static tree
17792 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17793 {
17794 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17795 }
17796
17797 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17798 or a concept-name.
17799
17800 enum-name:
17801 identifier
17802
17803 typedef-name:
17804 identifier
17805
17806 concept-name:
17807 identifier
17808
17809 Returns a TYPE_DECL for the type. */
17810
17811 static tree
17812 cp_parser_nonclass_name (cp_parser* parser)
17813 {
17814 tree type_decl;
17815 tree identifier;
17816
17817 cp_token *token = cp_lexer_peek_token (parser->lexer);
17818 identifier = cp_parser_identifier (parser);
17819 if (identifier == error_mark_node)
17820 return error_mark_node;
17821
17822 /* Look up the type-name. */
17823 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17824
17825 type_decl = strip_using_decl (type_decl);
17826
17827 /* If we found an overload set, then it may refer to a concept-name. */
17828 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17829 type_decl = decl;
17830
17831 if (TREE_CODE (type_decl) != TYPE_DECL
17832 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17833 {
17834 /* See if this is an Objective-C type. */
17835 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17836 tree type = objc_get_protocol_qualified_type (identifier, protos);
17837 if (type)
17838 type_decl = TYPE_NAME (type);
17839 }
17840
17841 /* Issue an error if we did not find a type-name. */
17842 if (TREE_CODE (type_decl) != TYPE_DECL
17843 /* In Objective-C, we have the complication that class names are
17844 normally type names and start declarations (eg, the
17845 "NSObject" in "NSObject *object;"), but can be used in an
17846 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17847 is an expression. So, a classname followed by a dot is not a
17848 valid type-name. */
17849 || (objc_is_class_name (TREE_TYPE (type_decl))
17850 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17851 {
17852 if (!cp_parser_simulate_error (parser))
17853 cp_parser_name_lookup_error (parser, identifier, type_decl,
17854 NLE_TYPE, token->location);
17855 return error_mark_node;
17856 }
17857 /* Remember that the name was used in the definition of the
17858 current class so that we can check later to see if the
17859 meaning would have been different after the class was
17860 entirely defined. */
17861 else if (type_decl != error_mark_node
17862 && !parser->scope)
17863 maybe_note_name_used_in_class (identifier, type_decl);
17864
17865 return type_decl;
17866 }
17867
17868 /* Parse an elaborated-type-specifier. Note that the grammar given
17869 here incorporates the resolution to DR68.
17870
17871 elaborated-type-specifier:
17872 class-key :: [opt] nested-name-specifier [opt] identifier
17873 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17874 enum-key :: [opt] nested-name-specifier [opt] identifier
17875 typename :: [opt] nested-name-specifier identifier
17876 typename :: [opt] nested-name-specifier template [opt]
17877 template-id
17878
17879 GNU extension:
17880
17881 elaborated-type-specifier:
17882 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17883 class-key attributes :: [opt] nested-name-specifier [opt]
17884 template [opt] template-id
17885 enum attributes :: [opt] nested-name-specifier [opt] identifier
17886
17887 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17888 declared `friend'. If IS_DECLARATION is TRUE, then this
17889 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17890 something is being declared.
17891
17892 Returns the TYPE specified. */
17893
17894 static tree
17895 cp_parser_elaborated_type_specifier (cp_parser* parser,
17896 bool is_friend,
17897 bool is_declaration)
17898 {
17899 enum tag_types tag_type;
17900 tree identifier;
17901 tree type = NULL_TREE;
17902 tree attributes = NULL_TREE;
17903 tree globalscope;
17904 cp_token *token = NULL;
17905
17906 /* See if we're looking at the `enum' keyword. */
17907 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17908 {
17909 /* Consume the `enum' token. */
17910 cp_lexer_consume_token (parser->lexer);
17911 /* Remember that it's an enumeration type. */
17912 tag_type = enum_type;
17913 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17914 enums) is used here. */
17915 cp_token *token = cp_lexer_peek_token (parser->lexer);
17916 if (cp_parser_is_keyword (token, RID_CLASS)
17917 || cp_parser_is_keyword (token, RID_STRUCT))
17918 {
17919 gcc_rich_location richloc (token->location);
17920 richloc.add_range (input_location);
17921 richloc.add_fixit_remove ();
17922 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17923 "a scoped enum must not use the %qD keyword",
17924 token->u.value);
17925 /* Consume the `struct' or `class' and parse it anyway. */
17926 cp_lexer_consume_token (parser->lexer);
17927 }
17928 /* Parse the attributes. */
17929 attributes = cp_parser_attributes_opt (parser);
17930 }
17931 /* Or, it might be `typename'. */
17932 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17933 RID_TYPENAME))
17934 {
17935 /* Consume the `typename' token. */
17936 cp_lexer_consume_token (parser->lexer);
17937 /* Remember that it's a `typename' type. */
17938 tag_type = typename_type;
17939 }
17940 /* Otherwise it must be a class-key. */
17941 else
17942 {
17943 tag_type = cp_parser_class_key (parser);
17944 if (tag_type == none_type)
17945 return error_mark_node;
17946 /* Parse the attributes. */
17947 attributes = cp_parser_attributes_opt (parser);
17948 }
17949
17950 /* Look for the `::' operator. */
17951 globalscope = cp_parser_global_scope_opt (parser,
17952 /*current_scope_valid_p=*/false);
17953 /* Look for the nested-name-specifier. */
17954 tree nested_name_specifier;
17955 if (tag_type == typename_type && !globalscope)
17956 {
17957 nested_name_specifier
17958 = cp_parser_nested_name_specifier (parser,
17959 /*typename_keyword_p=*/true,
17960 /*check_dependency_p=*/true,
17961 /*type_p=*/true,
17962 is_declaration);
17963 if (!nested_name_specifier)
17964 return error_mark_node;
17965 }
17966 else
17967 /* Even though `typename' is not present, the proposed resolution
17968 to Core Issue 180 says that in `class A<T>::B', `B' should be
17969 considered a type-name, even if `A<T>' is dependent. */
17970 nested_name_specifier
17971 = cp_parser_nested_name_specifier_opt (parser,
17972 /*typename_keyword_p=*/true,
17973 /*check_dependency_p=*/true,
17974 /*type_p=*/true,
17975 is_declaration);
17976 /* For everything but enumeration types, consider a template-id.
17977 For an enumeration type, consider only a plain identifier. */
17978 if (tag_type != enum_type)
17979 {
17980 bool template_p = false;
17981 tree decl;
17982
17983 /* Allow the `template' keyword. */
17984 template_p = cp_parser_optional_template_keyword (parser);
17985 /* If we didn't see `template', we don't know if there's a
17986 template-id or not. */
17987 if (!template_p)
17988 cp_parser_parse_tentatively (parser);
17989 /* Parse the template-id. */
17990 token = cp_lexer_peek_token (parser->lexer);
17991 decl = cp_parser_template_id (parser, template_p,
17992 /*check_dependency_p=*/true,
17993 tag_type,
17994 is_declaration);
17995 /* If we didn't find a template-id, look for an ordinary
17996 identifier. */
17997 if (!template_p && !cp_parser_parse_definitely (parser))
17998 ;
17999 /* We can get here when cp_parser_template_id, called by
18000 cp_parser_class_name with tag_type == none_type, succeeds
18001 and caches a BASELINK. Then, when called again here,
18002 instead of failing and returning an error_mark_node
18003 returns it (see template/typename17.C in C++11).
18004 ??? Could we diagnose this earlier? */
18005 else if (tag_type == typename_type && BASELINK_P (decl))
18006 {
18007 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18008 type = error_mark_node;
18009 }
18010 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18011 in effect, then we must assume that, upon instantiation, the
18012 template will correspond to a class. */
18013 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18014 && tag_type == typename_type)
18015 type = make_typename_type (parser->scope, decl,
18016 typename_type,
18017 /*complain=*/tf_error);
18018 /* If the `typename' keyword is in effect and DECL is not a type
18019 decl, then type is non existent. */
18020 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18021 ;
18022 else if (TREE_CODE (decl) == TYPE_DECL)
18023 {
18024 type = check_elaborated_type_specifier (tag_type, decl,
18025 /*allow_template_p=*/true);
18026
18027 /* If the next token is a semicolon, this must be a specialization,
18028 instantiation, or friend declaration. Check the scope while we
18029 still know whether or not we had a nested-name-specifier. */
18030 if (type != error_mark_node
18031 && !nested_name_specifier && !is_friend
18032 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18033 check_unqualified_spec_or_inst (type, token->location);
18034 }
18035 else if (decl == error_mark_node)
18036 type = error_mark_node;
18037 }
18038
18039 if (!type)
18040 {
18041 token = cp_lexer_peek_token (parser->lexer);
18042 identifier = cp_parser_identifier (parser);
18043
18044 if (identifier == error_mark_node)
18045 {
18046 parser->scope = NULL_TREE;
18047 return error_mark_node;
18048 }
18049
18050 /* For a `typename', we needn't call xref_tag. */
18051 if (tag_type == typename_type
18052 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18053 return cp_parser_make_typename_type (parser, identifier,
18054 token->location);
18055
18056 /* Template parameter lists apply only if we are not within a
18057 function parameter list. */
18058 bool template_parm_lists_apply
18059 = parser->num_template_parameter_lists;
18060 if (template_parm_lists_apply)
18061 for (cp_binding_level *s = current_binding_level;
18062 s && s->kind != sk_template_parms;
18063 s = s->level_chain)
18064 if (s->kind == sk_function_parms)
18065 template_parm_lists_apply = false;
18066
18067 /* Look up a qualified name in the usual way. */
18068 if (parser->scope)
18069 {
18070 tree decl;
18071 tree ambiguous_decls;
18072
18073 decl = cp_parser_lookup_name (parser, identifier,
18074 tag_type,
18075 /*is_template=*/false,
18076 /*is_namespace=*/false,
18077 /*check_dependency=*/true,
18078 &ambiguous_decls,
18079 token->location);
18080
18081 /* If the lookup was ambiguous, an error will already have been
18082 issued. */
18083 if (ambiguous_decls)
18084 return error_mark_node;
18085
18086 /* If we are parsing friend declaration, DECL may be a
18087 TEMPLATE_DECL tree node here. However, we need to check
18088 whether this TEMPLATE_DECL results in valid code. Consider
18089 the following example:
18090
18091 namespace N {
18092 template <class T> class C {};
18093 }
18094 class X {
18095 template <class T> friend class N::C; // #1, valid code
18096 };
18097 template <class T> class Y {
18098 friend class N::C; // #2, invalid code
18099 };
18100
18101 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18102 name lookup of `N::C'. We see that friend declaration must
18103 be template for the code to be valid. Note that
18104 processing_template_decl does not work here since it is
18105 always 1 for the above two cases. */
18106
18107 decl = (cp_parser_maybe_treat_template_as_class
18108 (decl, /*tag_name_p=*/is_friend
18109 && template_parm_lists_apply));
18110
18111 if (TREE_CODE (decl) != TYPE_DECL)
18112 {
18113 cp_parser_diagnose_invalid_type_name (parser,
18114 identifier,
18115 token->location);
18116 return error_mark_node;
18117 }
18118
18119 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18120 {
18121 bool allow_template = (template_parm_lists_apply
18122 || DECL_SELF_REFERENCE_P (decl));
18123 type = check_elaborated_type_specifier (tag_type, decl,
18124 allow_template);
18125
18126 if (type == error_mark_node)
18127 return error_mark_node;
18128 }
18129
18130 /* Forward declarations of nested types, such as
18131
18132 class C1::C2;
18133 class C1::C2::C3;
18134
18135 are invalid unless all components preceding the final '::'
18136 are complete. If all enclosing types are complete, these
18137 declarations become merely pointless.
18138
18139 Invalid forward declarations of nested types are errors
18140 caught elsewhere in parsing. Those that are pointless arrive
18141 here. */
18142
18143 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18144 && !is_friend && !processing_explicit_instantiation)
18145 warning (0, "declaration %qD does not declare anything", decl);
18146
18147 type = TREE_TYPE (decl);
18148 }
18149 else
18150 {
18151 /* An elaborated-type-specifier sometimes introduces a new type and
18152 sometimes names an existing type. Normally, the rule is that it
18153 introduces a new type only if there is not an existing type of
18154 the same name already in scope. For example, given:
18155
18156 struct S {};
18157 void f() { struct S s; }
18158
18159 the `struct S' in the body of `f' is the same `struct S' as in
18160 the global scope; the existing definition is used. However, if
18161 there were no global declaration, this would introduce a new
18162 local class named `S'.
18163
18164 An exception to this rule applies to the following code:
18165
18166 namespace N { struct S; }
18167
18168 Here, the elaborated-type-specifier names a new type
18169 unconditionally; even if there is already an `S' in the
18170 containing scope this declaration names a new type.
18171 This exception only applies if the elaborated-type-specifier
18172 forms the complete declaration:
18173
18174 [class.name]
18175
18176 A declaration consisting solely of `class-key identifier ;' is
18177 either a redeclaration of the name in the current scope or a
18178 forward declaration of the identifier as a class name. It
18179 introduces the name into the current scope.
18180
18181 We are in this situation precisely when the next token is a `;'.
18182
18183 An exception to the exception is that a `friend' declaration does
18184 *not* name a new type; i.e., given:
18185
18186 struct S { friend struct T; };
18187
18188 `T' is not a new type in the scope of `S'.
18189
18190 Also, `new struct S' or `sizeof (struct S)' never results in the
18191 definition of a new type; a new type can only be declared in a
18192 declaration context. */
18193
18194 tag_scope ts;
18195 bool template_p;
18196
18197 if (is_friend)
18198 /* Friends have special name lookup rules. */
18199 ts = ts_within_enclosing_non_class;
18200 else if (is_declaration
18201 && cp_lexer_next_token_is (parser->lexer,
18202 CPP_SEMICOLON))
18203 /* This is a `class-key identifier ;' */
18204 ts = ts_current;
18205 else
18206 ts = ts_global;
18207
18208 template_p =
18209 (template_parm_lists_apply
18210 && (cp_parser_next_token_starts_class_definition_p (parser)
18211 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18212 /* An unqualified name was used to reference this type, so
18213 there were no qualifying templates. */
18214 if (template_parm_lists_apply
18215 && !cp_parser_check_template_parameters (parser,
18216 /*num_templates=*/0,
18217 /*template_id*/false,
18218 token->location,
18219 /*declarator=*/NULL))
18220 return error_mark_node;
18221 type = xref_tag (tag_type, identifier, ts, template_p);
18222 }
18223 }
18224
18225 if (type == error_mark_node)
18226 return error_mark_node;
18227
18228 /* Allow attributes on forward declarations of classes. */
18229 if (attributes)
18230 {
18231 if (TREE_CODE (type) == TYPENAME_TYPE)
18232 warning (OPT_Wattributes,
18233 "attributes ignored on uninstantiated type");
18234 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18235 && ! processing_explicit_instantiation)
18236 warning (OPT_Wattributes,
18237 "attributes ignored on template instantiation");
18238 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18239 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18240 else
18241 warning (OPT_Wattributes,
18242 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18243 }
18244
18245 if (tag_type != enum_type)
18246 {
18247 /* Indicate whether this class was declared as a `class' or as a
18248 `struct'. */
18249 if (CLASS_TYPE_P (type))
18250 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18251 cp_parser_check_class_key (tag_type, type);
18252 }
18253
18254 /* A "<" cannot follow an elaborated type specifier. If that
18255 happens, the user was probably trying to form a template-id. */
18256 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18257 token->location);
18258
18259 return type;
18260 }
18261
18262 /* Parse an enum-specifier.
18263
18264 enum-specifier:
18265 enum-head { enumerator-list [opt] }
18266 enum-head { enumerator-list , } [C++0x]
18267
18268 enum-head:
18269 enum-key identifier [opt] enum-base [opt]
18270 enum-key nested-name-specifier identifier enum-base [opt]
18271
18272 enum-key:
18273 enum
18274 enum class [C++0x]
18275 enum struct [C++0x]
18276
18277 enum-base: [C++0x]
18278 : type-specifier-seq
18279
18280 opaque-enum-specifier:
18281 enum-key identifier enum-base [opt] ;
18282
18283 GNU Extensions:
18284 enum-key attributes[opt] identifier [opt] enum-base [opt]
18285 { enumerator-list [opt] }attributes[opt]
18286 enum-key attributes[opt] identifier [opt] enum-base [opt]
18287 { enumerator-list, }attributes[opt] [C++0x]
18288
18289 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18290 if the token stream isn't an enum-specifier after all. */
18291
18292 static tree
18293 cp_parser_enum_specifier (cp_parser* parser)
18294 {
18295 tree identifier;
18296 tree type = NULL_TREE;
18297 tree prev_scope;
18298 tree nested_name_specifier = NULL_TREE;
18299 tree attributes;
18300 bool scoped_enum_p = false;
18301 bool has_underlying_type = false;
18302 bool nested_being_defined = false;
18303 bool new_value_list = false;
18304 bool is_new_type = false;
18305 bool is_unnamed = false;
18306 tree underlying_type = NULL_TREE;
18307 cp_token *type_start_token = NULL;
18308 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18309
18310 parser->colon_corrects_to_scope_p = false;
18311
18312 /* Parse tentatively so that we can back up if we don't find a
18313 enum-specifier. */
18314 cp_parser_parse_tentatively (parser);
18315
18316 /* Caller guarantees that the current token is 'enum', an identifier
18317 possibly follows, and the token after that is an opening brace.
18318 If we don't have an identifier, fabricate an anonymous name for
18319 the enumeration being defined. */
18320 cp_lexer_consume_token (parser->lexer);
18321
18322 /* Parse the "class" or "struct", which indicates a scoped
18323 enumeration type in C++0x. */
18324 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18325 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18326 {
18327 if (cxx_dialect < cxx11)
18328 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18329
18330 /* Consume the `struct' or `class' token. */
18331 cp_lexer_consume_token (parser->lexer);
18332
18333 scoped_enum_p = true;
18334 }
18335
18336 attributes = cp_parser_attributes_opt (parser);
18337
18338 /* Clear the qualification. */
18339 parser->scope = NULL_TREE;
18340 parser->qualifying_scope = NULL_TREE;
18341 parser->object_scope = NULL_TREE;
18342
18343 /* Figure out in what scope the declaration is being placed. */
18344 prev_scope = current_scope ();
18345
18346 type_start_token = cp_lexer_peek_token (parser->lexer);
18347
18348 push_deferring_access_checks (dk_no_check);
18349 nested_name_specifier
18350 = cp_parser_nested_name_specifier_opt (parser,
18351 /*typename_keyword_p=*/true,
18352 /*check_dependency_p=*/false,
18353 /*type_p=*/false,
18354 /*is_declaration=*/false);
18355
18356 if (nested_name_specifier)
18357 {
18358 tree name;
18359
18360 identifier = cp_parser_identifier (parser);
18361 name = cp_parser_lookup_name (parser, identifier,
18362 enum_type,
18363 /*is_template=*/false,
18364 /*is_namespace=*/false,
18365 /*check_dependency=*/true,
18366 /*ambiguous_decls=*/NULL,
18367 input_location);
18368 if (name && name != error_mark_node)
18369 {
18370 type = TREE_TYPE (name);
18371 if (TREE_CODE (type) == TYPENAME_TYPE)
18372 {
18373 /* Are template enums allowed in ISO? */
18374 if (template_parm_scope_p ())
18375 pedwarn (type_start_token->location, OPT_Wpedantic,
18376 "%qD is an enumeration template", name);
18377 /* ignore a typename reference, for it will be solved by name
18378 in start_enum. */
18379 type = NULL_TREE;
18380 }
18381 }
18382 else if (nested_name_specifier == error_mark_node)
18383 /* We already issued an error. */;
18384 else
18385 {
18386 error_at (type_start_token->location,
18387 "%qD does not name an enumeration in %qT",
18388 identifier, nested_name_specifier);
18389 nested_name_specifier = error_mark_node;
18390 }
18391 }
18392 else
18393 {
18394 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18395 identifier = cp_parser_identifier (parser);
18396 else
18397 {
18398 identifier = make_anon_name ();
18399 is_unnamed = true;
18400 if (scoped_enum_p)
18401 error_at (type_start_token->location,
18402 "unnamed scoped enum is not allowed");
18403 }
18404 }
18405 pop_deferring_access_checks ();
18406
18407 /* Check for the `:' that denotes a specified underlying type in C++0x.
18408 Note that a ':' could also indicate a bitfield width, however. */
18409 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18410 {
18411 cp_decl_specifier_seq type_specifiers;
18412
18413 /* Consume the `:'. */
18414 cp_lexer_consume_token (parser->lexer);
18415
18416 /* Parse the type-specifier-seq. */
18417 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18418 /*is_trailing_return=*/false,
18419 &type_specifiers);
18420
18421 /* At this point this is surely not elaborated type specifier. */
18422 if (!cp_parser_parse_definitely (parser))
18423 return NULL_TREE;
18424
18425 if (cxx_dialect < cxx11)
18426 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18427
18428 has_underlying_type = true;
18429
18430 /* If that didn't work, stop. */
18431 if (type_specifiers.type != error_mark_node)
18432 {
18433 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18434 /*initialized=*/0, NULL);
18435 if (underlying_type == error_mark_node
18436 || check_for_bare_parameter_packs (underlying_type))
18437 underlying_type = NULL_TREE;
18438 }
18439 }
18440
18441 /* Look for the `{' but don't consume it yet. */
18442 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18443 {
18444 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18445 {
18446 cp_parser_error (parser, "expected %<{%>");
18447 if (has_underlying_type)
18448 {
18449 type = NULL_TREE;
18450 goto out;
18451 }
18452 }
18453 /* An opaque-enum-specifier must have a ';' here. */
18454 if ((scoped_enum_p || underlying_type)
18455 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18456 {
18457 cp_parser_error (parser, "expected %<;%> or %<{%>");
18458 if (has_underlying_type)
18459 {
18460 type = NULL_TREE;
18461 goto out;
18462 }
18463 }
18464 }
18465
18466 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18467 return NULL_TREE;
18468
18469 if (nested_name_specifier)
18470 {
18471 if (CLASS_TYPE_P (nested_name_specifier))
18472 {
18473 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18474 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18475 push_scope (nested_name_specifier);
18476 }
18477 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18478 {
18479 push_nested_namespace (nested_name_specifier);
18480 }
18481 }
18482
18483 /* Issue an error message if type-definitions are forbidden here. */
18484 if (!cp_parser_check_type_definition (parser))
18485 type = error_mark_node;
18486 else
18487 /* Create the new type. We do this before consuming the opening
18488 brace so the enum will be recorded as being on the line of its
18489 tag (or the 'enum' keyword, if there is no tag). */
18490 type = start_enum (identifier, type, underlying_type,
18491 attributes, scoped_enum_p, &is_new_type);
18492
18493 /* If the next token is not '{' it is an opaque-enum-specifier or an
18494 elaborated-type-specifier. */
18495 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18496 {
18497 timevar_push (TV_PARSE_ENUM);
18498 if (nested_name_specifier
18499 && nested_name_specifier != error_mark_node)
18500 {
18501 /* The following catches invalid code such as:
18502 enum class S<int>::E { A, B, C }; */
18503 if (!processing_specialization
18504 && CLASS_TYPE_P (nested_name_specifier)
18505 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18506 error_at (type_start_token->location, "cannot add an enumerator "
18507 "list to a template instantiation");
18508
18509 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18510 {
18511 error_at (type_start_token->location,
18512 "%<%T::%E%> has not been declared",
18513 TYPE_CONTEXT (nested_name_specifier),
18514 nested_name_specifier);
18515 type = error_mark_node;
18516 }
18517 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18518 && !CLASS_TYPE_P (nested_name_specifier))
18519 {
18520 error_at (type_start_token->location, "nested name specifier "
18521 "%qT for enum declaration does not name a class "
18522 "or namespace", nested_name_specifier);
18523 type = error_mark_node;
18524 }
18525 /* If that scope does not contain the scope in which the
18526 class was originally declared, the program is invalid. */
18527 else if (prev_scope && !is_ancestor (prev_scope,
18528 nested_name_specifier))
18529 {
18530 if (at_namespace_scope_p ())
18531 error_at (type_start_token->location,
18532 "declaration of %qD in namespace %qD which does not "
18533 "enclose %qD",
18534 type, prev_scope, nested_name_specifier);
18535 else
18536 error_at (type_start_token->location,
18537 "declaration of %qD in %qD which does not "
18538 "enclose %qD",
18539 type, prev_scope, nested_name_specifier);
18540 type = error_mark_node;
18541 }
18542 /* If that scope is the scope where the declaration is being placed
18543 the program is invalid. */
18544 else if (CLASS_TYPE_P (nested_name_specifier)
18545 && CLASS_TYPE_P (prev_scope)
18546 && same_type_p (nested_name_specifier, prev_scope))
18547 {
18548 permerror (type_start_token->location,
18549 "extra qualification not allowed");
18550 nested_name_specifier = NULL_TREE;
18551 }
18552 }
18553
18554 if (scoped_enum_p)
18555 begin_scope (sk_scoped_enum, type);
18556
18557 /* Consume the opening brace. */
18558 matching_braces braces;
18559 braces.consume_open (parser);
18560
18561 if (type == error_mark_node)
18562 ; /* Nothing to add */
18563 else if (OPAQUE_ENUM_P (type)
18564 || (cxx_dialect > cxx98 && processing_specialization))
18565 {
18566 new_value_list = true;
18567 SET_OPAQUE_ENUM_P (type, false);
18568 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18569 }
18570 else
18571 {
18572 error_at (type_start_token->location,
18573 "multiple definition of %q#T", type);
18574 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18575 "previous definition here");
18576 type = error_mark_node;
18577 }
18578
18579 if (type == error_mark_node)
18580 cp_parser_skip_to_end_of_block_or_statement (parser);
18581 /* If the next token is not '}', then there are some enumerators. */
18582 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18583 {
18584 if (is_unnamed && !scoped_enum_p)
18585 pedwarn (type_start_token->location, OPT_Wpedantic,
18586 "ISO C++ forbids empty unnamed enum");
18587 }
18588 else
18589 cp_parser_enumerator_list (parser, type);
18590
18591 /* Consume the final '}'. */
18592 braces.require_close (parser);
18593
18594 if (scoped_enum_p)
18595 finish_scope ();
18596 timevar_pop (TV_PARSE_ENUM);
18597 }
18598 else
18599 {
18600 /* If a ';' follows, then it is an opaque-enum-specifier
18601 and additional restrictions apply. */
18602 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18603 {
18604 if (is_unnamed)
18605 error_at (type_start_token->location,
18606 "opaque-enum-specifier without name");
18607 else if (nested_name_specifier)
18608 error_at (type_start_token->location,
18609 "opaque-enum-specifier must use a simple identifier");
18610 }
18611 }
18612
18613 /* Look for trailing attributes to apply to this enumeration, and
18614 apply them if appropriate. */
18615 if (cp_parser_allow_gnu_extensions_p (parser))
18616 {
18617 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18618 cplus_decl_attributes (&type,
18619 trailing_attr,
18620 (int) ATTR_FLAG_TYPE_IN_PLACE);
18621 }
18622
18623 /* Finish up the enumeration. */
18624 if (type != error_mark_node)
18625 {
18626 if (new_value_list)
18627 finish_enum_value_list (type);
18628 if (is_new_type)
18629 finish_enum (type);
18630 }
18631
18632 if (nested_name_specifier)
18633 {
18634 if (CLASS_TYPE_P (nested_name_specifier))
18635 {
18636 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18637 pop_scope (nested_name_specifier);
18638 }
18639 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18640 {
18641 pop_nested_namespace (nested_name_specifier);
18642 }
18643 }
18644 out:
18645 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18646 return type;
18647 }
18648
18649 /* Parse an enumerator-list. The enumerators all have the indicated
18650 TYPE.
18651
18652 enumerator-list:
18653 enumerator-definition
18654 enumerator-list , enumerator-definition */
18655
18656 static void
18657 cp_parser_enumerator_list (cp_parser* parser, tree type)
18658 {
18659 while (true)
18660 {
18661 /* Parse an enumerator-definition. */
18662 cp_parser_enumerator_definition (parser, type);
18663
18664 /* If the next token is not a ',', we've reached the end of
18665 the list. */
18666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18667 break;
18668 /* Otherwise, consume the `,' and keep going. */
18669 cp_lexer_consume_token (parser->lexer);
18670 /* If the next token is a `}', there is a trailing comma. */
18671 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18672 {
18673 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18674 pedwarn (input_location, OPT_Wpedantic,
18675 "comma at end of enumerator list");
18676 break;
18677 }
18678 }
18679 }
18680
18681 /* Parse an enumerator-definition. The enumerator has the indicated
18682 TYPE.
18683
18684 enumerator-definition:
18685 enumerator
18686 enumerator = constant-expression
18687
18688 enumerator:
18689 identifier
18690
18691 GNU Extensions:
18692
18693 enumerator-definition:
18694 enumerator attributes [opt]
18695 enumerator attributes [opt] = constant-expression */
18696
18697 static void
18698 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18699 {
18700 tree identifier;
18701 tree value;
18702 location_t loc;
18703
18704 /* Save the input location because we are interested in the location
18705 of the identifier and not the location of the explicit value. */
18706 loc = cp_lexer_peek_token (parser->lexer)->location;
18707
18708 /* Look for the identifier. */
18709 identifier = cp_parser_identifier (parser);
18710 if (identifier == error_mark_node)
18711 return;
18712
18713 /* Parse any specified attributes. */
18714 tree attrs = cp_parser_attributes_opt (parser);
18715
18716 /* If the next token is an '=', then there is an explicit value. */
18717 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18718 {
18719 /* Consume the `=' token. */
18720 cp_lexer_consume_token (parser->lexer);
18721 /* Parse the value. */
18722 value = cp_parser_constant_expression (parser);
18723 }
18724 else
18725 value = NULL_TREE;
18726
18727 /* If we are processing a template, make sure the initializer of the
18728 enumerator doesn't contain any bare template parameter pack. */
18729 if (check_for_bare_parameter_packs (value))
18730 value = error_mark_node;
18731
18732 /* Create the enumerator. */
18733 build_enumerator (identifier, value, type, attrs, loc);
18734 }
18735
18736 /* Parse a namespace-name.
18737
18738 namespace-name:
18739 original-namespace-name
18740 namespace-alias
18741
18742 Returns the NAMESPACE_DECL for the namespace. */
18743
18744 static tree
18745 cp_parser_namespace_name (cp_parser* parser)
18746 {
18747 tree identifier;
18748 tree namespace_decl;
18749
18750 cp_token *token = cp_lexer_peek_token (parser->lexer);
18751
18752 /* Get the name of the namespace. */
18753 identifier = cp_parser_identifier (parser);
18754 if (identifier == error_mark_node)
18755 return error_mark_node;
18756
18757 /* Look up the identifier in the currently active scope. Look only
18758 for namespaces, due to:
18759
18760 [basic.lookup.udir]
18761
18762 When looking up a namespace-name in a using-directive or alias
18763 definition, only namespace names are considered.
18764
18765 And:
18766
18767 [basic.lookup.qual]
18768
18769 During the lookup of a name preceding the :: scope resolution
18770 operator, object, function, and enumerator names are ignored.
18771
18772 (Note that cp_parser_qualifying_entity only calls this
18773 function if the token after the name is the scope resolution
18774 operator.) */
18775 namespace_decl = cp_parser_lookup_name (parser, identifier,
18776 none_type,
18777 /*is_template=*/false,
18778 /*is_namespace=*/true,
18779 /*check_dependency=*/true,
18780 /*ambiguous_decls=*/NULL,
18781 token->location);
18782 /* If it's not a namespace, issue an error. */
18783 if (namespace_decl == error_mark_node
18784 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18785 {
18786 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18787 {
18788 auto_diagnostic_group d;
18789 name_hint hint;
18790 if (namespace_decl == error_mark_node
18791 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18792 hint = suggest_alternative_in_explicit_scope (token->location,
18793 identifier,
18794 parser->scope);
18795 if (const char *suggestion = hint.suggestion ())
18796 {
18797 gcc_rich_location richloc (token->location);
18798 richloc.add_fixit_replace (suggestion);
18799 error_at (&richloc,
18800 "%qD is not a namespace-name; did you mean %qs?",
18801 identifier, suggestion);
18802 }
18803 else
18804 error_at (token->location, "%qD is not a namespace-name",
18805 identifier);
18806 }
18807 else
18808 cp_parser_error (parser, "expected namespace-name");
18809 namespace_decl = error_mark_node;
18810 }
18811
18812 return namespace_decl;
18813 }
18814
18815 /* Parse a namespace-definition.
18816
18817 namespace-definition:
18818 named-namespace-definition
18819 unnamed-namespace-definition
18820
18821 named-namespace-definition:
18822 original-namespace-definition
18823 extension-namespace-definition
18824
18825 original-namespace-definition:
18826 namespace identifier { namespace-body }
18827
18828 extension-namespace-definition:
18829 namespace original-namespace-name { namespace-body }
18830
18831 unnamed-namespace-definition:
18832 namespace { namespace-body } */
18833
18834 static void
18835 cp_parser_namespace_definition (cp_parser* parser)
18836 {
18837 tree identifier;
18838 int nested_definition_count = 0;
18839
18840 cp_ensure_no_omp_declare_simd (parser);
18841 cp_ensure_no_oacc_routine (parser);
18842
18843 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18844
18845 if (is_inline)
18846 {
18847 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18848 cp_lexer_consume_token (parser->lexer);
18849 }
18850
18851 /* Look for the `namespace' keyword. */
18852 cp_token* token
18853 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18854
18855 /* Parse any specified attributes before the identifier. */
18856 tree attribs = cp_parser_attributes_opt (parser);
18857
18858 for (;;)
18859 {
18860 identifier = NULL_TREE;
18861
18862 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18863 {
18864 identifier = cp_parser_identifier (parser);
18865
18866 if (cp_next_tokens_can_be_std_attribute_p (parser))
18867 pedwarn (input_location, OPT_Wpedantic,
18868 "standard attributes on namespaces must precede "
18869 "the namespace name");
18870
18871 /* Parse any attributes specified after the identifier. */
18872 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18873 }
18874
18875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18876 break;
18877
18878 if (!nested_definition_count && cxx_dialect < cxx17)
18879 pedwarn (input_location, OPT_Wpedantic,
18880 "nested namespace definitions only available with "
18881 "-std=c++17 or -std=gnu++17");
18882
18883 /* Nested namespace names can create new namespaces (unlike
18884 other qualified-ids). */
18885 if (int count = identifier ? push_namespace (identifier) : 0)
18886 nested_definition_count += count;
18887 else
18888 cp_parser_error (parser, "nested namespace name required");
18889 cp_lexer_consume_token (parser->lexer);
18890 }
18891
18892 if (nested_definition_count && !identifier)
18893 cp_parser_error (parser, "namespace name required");
18894
18895 if (nested_definition_count && attribs)
18896 error_at (token->location,
18897 "a nested namespace definition cannot have attributes");
18898 if (nested_definition_count && is_inline)
18899 error_at (token->location,
18900 "a nested namespace definition cannot be inline");
18901
18902 /* Start the namespace. */
18903 nested_definition_count += push_namespace (identifier, is_inline);
18904
18905 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18906
18907 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18908
18909 /* Look for the `{' to validate starting the namespace. */
18910 matching_braces braces;
18911 if (braces.require_open (parser))
18912 {
18913 /* Parse the body of the namespace. */
18914 cp_parser_namespace_body (parser);
18915
18916 /* Look for the final `}'. */
18917 braces.require_close (parser);
18918 }
18919
18920 if (has_visibility)
18921 pop_visibility (1);
18922
18923 /* Pop the nested namespace definitions. */
18924 while (nested_definition_count--)
18925 pop_namespace ();
18926 }
18927
18928 /* Parse a namespace-body.
18929
18930 namespace-body:
18931 declaration-seq [opt] */
18932
18933 static void
18934 cp_parser_namespace_body (cp_parser* parser)
18935 {
18936 cp_parser_declaration_seq_opt (parser);
18937 }
18938
18939 /* Parse a namespace-alias-definition.
18940
18941 namespace-alias-definition:
18942 namespace identifier = qualified-namespace-specifier ; */
18943
18944 static void
18945 cp_parser_namespace_alias_definition (cp_parser* parser)
18946 {
18947 tree identifier;
18948 tree namespace_specifier;
18949
18950 cp_token *token = cp_lexer_peek_token (parser->lexer);
18951
18952 /* Look for the `namespace' keyword. */
18953 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18954 /* Look for the identifier. */
18955 identifier = cp_parser_identifier (parser);
18956 if (identifier == error_mark_node)
18957 return;
18958 /* Look for the `=' token. */
18959 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18960 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18961 {
18962 error_at (token->location, "%<namespace%> definition is not allowed here");
18963 /* Skip the definition. */
18964 cp_lexer_consume_token (parser->lexer);
18965 if (cp_parser_skip_to_closing_brace (parser))
18966 cp_lexer_consume_token (parser->lexer);
18967 return;
18968 }
18969 cp_parser_require (parser, CPP_EQ, RT_EQ);
18970 /* Look for the qualified-namespace-specifier. */
18971 namespace_specifier
18972 = cp_parser_qualified_namespace_specifier (parser);
18973 /* Look for the `;' token. */
18974 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18975
18976 /* Register the alias in the symbol table. */
18977 do_namespace_alias (identifier, namespace_specifier);
18978 }
18979
18980 /* Parse a qualified-namespace-specifier.
18981
18982 qualified-namespace-specifier:
18983 :: [opt] nested-name-specifier [opt] namespace-name
18984
18985 Returns a NAMESPACE_DECL corresponding to the specified
18986 namespace. */
18987
18988 static tree
18989 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18990 {
18991 /* Look for the optional `::'. */
18992 cp_parser_global_scope_opt (parser,
18993 /*current_scope_valid_p=*/false);
18994
18995 /* Look for the optional nested-name-specifier. */
18996 cp_parser_nested_name_specifier_opt (parser,
18997 /*typename_keyword_p=*/false,
18998 /*check_dependency_p=*/true,
18999 /*type_p=*/false,
19000 /*is_declaration=*/true);
19001
19002 return cp_parser_namespace_name (parser);
19003 }
19004
19005 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19006 access declaration.
19007
19008 using-declaration:
19009 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19010 using :: unqualified-id ;
19011
19012 access-declaration:
19013 qualified-id ;
19014
19015 */
19016
19017 static bool
19018 cp_parser_using_declaration (cp_parser* parser,
19019 bool access_declaration_p)
19020 {
19021 cp_token *token;
19022 bool typename_p = false;
19023 bool global_scope_p;
19024 tree decl;
19025 tree identifier;
19026 tree qscope;
19027 int oldcount = errorcount;
19028 cp_token *diag_token = NULL;
19029
19030 if (access_declaration_p)
19031 {
19032 diag_token = cp_lexer_peek_token (parser->lexer);
19033 cp_parser_parse_tentatively (parser);
19034 }
19035 else
19036 {
19037 /* Look for the `using' keyword. */
19038 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19039
19040 again:
19041 /* Peek at the next token. */
19042 token = cp_lexer_peek_token (parser->lexer);
19043 /* See if it's `typename'. */
19044 if (token->keyword == RID_TYPENAME)
19045 {
19046 /* Remember that we've seen it. */
19047 typename_p = true;
19048 /* Consume the `typename' token. */
19049 cp_lexer_consume_token (parser->lexer);
19050 }
19051 }
19052
19053 /* Look for the optional global scope qualification. */
19054 global_scope_p
19055 = (cp_parser_global_scope_opt (parser,
19056 /*current_scope_valid_p=*/false)
19057 != NULL_TREE);
19058
19059 /* If we saw `typename', or didn't see `::', then there must be a
19060 nested-name-specifier present. */
19061 if (typename_p || !global_scope_p)
19062 {
19063 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19064 /*check_dependency_p=*/true,
19065 /*type_p=*/false,
19066 /*is_declaration=*/true);
19067 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19068 {
19069 cp_parser_skip_to_end_of_block_or_statement (parser);
19070 return false;
19071 }
19072 }
19073 /* Otherwise, we could be in either of the two productions. In that
19074 case, treat the nested-name-specifier as optional. */
19075 else
19076 qscope = cp_parser_nested_name_specifier_opt (parser,
19077 /*typename_keyword_p=*/false,
19078 /*check_dependency_p=*/true,
19079 /*type_p=*/false,
19080 /*is_declaration=*/true);
19081 if (!qscope)
19082 qscope = global_namespace;
19083 else if (UNSCOPED_ENUM_P (qscope))
19084 qscope = CP_TYPE_CONTEXT (qscope);
19085
19086 if (access_declaration_p && cp_parser_error_occurred (parser))
19087 /* Something has already gone wrong; there's no need to parse
19088 further. Since an error has occurred, the return value of
19089 cp_parser_parse_definitely will be false, as required. */
19090 return cp_parser_parse_definitely (parser);
19091
19092 token = cp_lexer_peek_token (parser->lexer);
19093 /* Parse the unqualified-id. */
19094 identifier = cp_parser_unqualified_id (parser,
19095 /*template_keyword_p=*/false,
19096 /*check_dependency_p=*/true,
19097 /*declarator_p=*/true,
19098 /*optional_p=*/false);
19099
19100 if (access_declaration_p)
19101 {
19102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19103 cp_parser_simulate_error (parser);
19104 if (!cp_parser_parse_definitely (parser))
19105 return false;
19106 }
19107 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19108 {
19109 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19110 if (cxx_dialect < cxx17
19111 && !in_system_header_at (ell->location))
19112 pedwarn (ell->location, 0,
19113 "pack expansion in using-declaration only available "
19114 "with -std=c++17 or -std=gnu++17");
19115 qscope = make_pack_expansion (qscope);
19116 }
19117
19118 /* The function we call to handle a using-declaration is different
19119 depending on what scope we are in. */
19120 if (qscope == error_mark_node || identifier == error_mark_node)
19121 ;
19122 else if (!identifier_p (identifier)
19123 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19124 /* [namespace.udecl]
19125
19126 A using declaration shall not name a template-id. */
19127 error_at (token->location,
19128 "a template-id may not appear in a using-declaration");
19129 else
19130 {
19131 if (at_class_scope_p ())
19132 {
19133 /* Create the USING_DECL. */
19134 decl = do_class_using_decl (qscope, identifier);
19135
19136 if (decl && typename_p)
19137 USING_DECL_TYPENAME_P (decl) = 1;
19138
19139 if (check_for_bare_parameter_packs (decl))
19140 {
19141 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19142 return false;
19143 }
19144 else
19145 /* Add it to the list of members in this class. */
19146 finish_member_declaration (decl);
19147 }
19148 else
19149 {
19150 decl = cp_parser_lookup_name_simple (parser,
19151 identifier,
19152 token->location);
19153 if (decl == error_mark_node)
19154 cp_parser_name_lookup_error (parser, identifier,
19155 decl, NLE_NULL,
19156 token->location);
19157 else if (check_for_bare_parameter_packs (decl))
19158 {
19159 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19160 return false;
19161 }
19162 else if (!at_namespace_scope_p ())
19163 finish_local_using_decl (decl, qscope, identifier);
19164 else
19165 finish_namespace_using_decl (decl, qscope, identifier);
19166 }
19167 }
19168
19169 if (!access_declaration_p
19170 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19171 {
19172 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19173 if (cxx_dialect < cxx17)
19174 pedwarn (comma->location, 0,
19175 "comma-separated list in using-declaration only available "
19176 "with -std=c++17 or -std=gnu++17");
19177 goto again;
19178 }
19179
19180 /* Look for the final `;'. */
19181 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19182
19183 if (access_declaration_p && errorcount == oldcount)
19184 warning_at (diag_token->location, OPT_Wdeprecated,
19185 "access declarations are deprecated "
19186 "in favour of using-declarations; "
19187 "suggestion: add the %<using%> keyword");
19188
19189 return true;
19190 }
19191
19192 /* Parse an alias-declaration.
19193
19194 alias-declaration:
19195 using identifier attribute-specifier-seq [opt] = type-id */
19196
19197 static tree
19198 cp_parser_alias_declaration (cp_parser* parser)
19199 {
19200 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19201 location_t id_location, type_location;
19202 cp_declarator *declarator;
19203 cp_decl_specifier_seq decl_specs;
19204 bool member_p;
19205 const char *saved_message = NULL;
19206
19207 /* Look for the `using' keyword. */
19208 cp_token *using_token
19209 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19210 if (using_token == NULL)
19211 return error_mark_node;
19212
19213 id_location = cp_lexer_peek_token (parser->lexer)->location;
19214 id = cp_parser_identifier (parser);
19215 if (id == error_mark_node)
19216 return error_mark_node;
19217
19218 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19219 attributes = cp_parser_attributes_opt (parser);
19220 if (attributes == error_mark_node)
19221 return error_mark_node;
19222
19223 cp_parser_require (parser, CPP_EQ, RT_EQ);
19224
19225 if (cp_parser_error_occurred (parser))
19226 return error_mark_node;
19227
19228 cp_parser_commit_to_tentative_parse (parser);
19229
19230 /* Now we are going to parse the type-id of the declaration. */
19231
19232 /*
19233 [dcl.type]/3 says:
19234
19235 "A type-specifier-seq shall not define a class or enumeration
19236 unless it appears in the type-id of an alias-declaration (7.1.3) that
19237 is not the declaration of a template-declaration."
19238
19239 In other words, if we currently are in an alias template, the
19240 type-id should not define a type.
19241
19242 So let's set parser->type_definition_forbidden_message in that
19243 case; cp_parser_check_type_definition (called by
19244 cp_parser_class_specifier) will then emit an error if a type is
19245 defined in the type-id. */
19246 if (parser->num_template_parameter_lists)
19247 {
19248 saved_message = parser->type_definition_forbidden_message;
19249 parser->type_definition_forbidden_message =
19250 G_("types may not be defined in alias template declarations");
19251 }
19252
19253 type = cp_parser_type_id (parser, &type_location);
19254
19255 /* Restore the error message if need be. */
19256 if (parser->num_template_parameter_lists)
19257 parser->type_definition_forbidden_message = saved_message;
19258
19259 if (type == error_mark_node
19260 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19261 {
19262 cp_parser_skip_to_end_of_block_or_statement (parser);
19263 return error_mark_node;
19264 }
19265
19266 /* A typedef-name can also be introduced by an alias-declaration. The
19267 identifier following the using keyword becomes a typedef-name. It has
19268 the same semantics as if it were introduced by the typedef
19269 specifier. In particular, it does not define a new type and it shall
19270 not appear in the type-id. */
19271
19272 clear_decl_specs (&decl_specs);
19273 decl_specs.type = type;
19274 if (attributes != NULL_TREE)
19275 {
19276 decl_specs.attributes = attributes;
19277 set_and_check_decl_spec_loc (&decl_specs,
19278 ds_attribute,
19279 attrs_token);
19280 }
19281 set_and_check_decl_spec_loc (&decl_specs,
19282 ds_typedef,
19283 using_token);
19284 set_and_check_decl_spec_loc (&decl_specs,
19285 ds_alias,
19286 using_token);
19287 decl_specs.locations[ds_type_spec] = type_location;
19288
19289 if (parser->num_template_parameter_lists
19290 && !cp_parser_check_template_parameters (parser,
19291 /*num_templates=*/0,
19292 /*template_id*/false,
19293 id_location,
19294 /*declarator=*/NULL))
19295 return error_mark_node;
19296
19297 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
19298 declarator->id_loc = id_location;
19299
19300 member_p = at_class_scope_p ();
19301 if (member_p)
19302 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19303 NULL_TREE, attributes);
19304 else
19305 decl = start_decl (declarator, &decl_specs, 0,
19306 attributes, NULL_TREE, &pushed_scope);
19307 if (decl == error_mark_node)
19308 return decl;
19309
19310 // Attach constraints to the alias declaration.
19311 if (flag_concepts && current_template_parms)
19312 {
19313 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19314 tree constr = build_constraints (reqs, NULL_TREE);
19315 set_constraints (decl, constr);
19316 }
19317
19318 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19319
19320 if (pushed_scope)
19321 pop_scope (pushed_scope);
19322
19323 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19324 added into the symbol table; otherwise, return the TYPE_DECL. */
19325 if (DECL_LANG_SPECIFIC (decl)
19326 && DECL_TEMPLATE_INFO (decl)
19327 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19328 {
19329 decl = DECL_TI_TEMPLATE (decl);
19330 if (member_p)
19331 check_member_template (decl);
19332 }
19333
19334 return decl;
19335 }
19336
19337 /* Parse a using-directive.
19338
19339 using-directive:
19340 using namespace :: [opt] nested-name-specifier [opt]
19341 namespace-name ; */
19342
19343 static void
19344 cp_parser_using_directive (cp_parser* parser)
19345 {
19346 tree namespace_decl;
19347 tree attribs;
19348
19349 /* Look for the `using' keyword. */
19350 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19351 /* And the `namespace' keyword. */
19352 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19353 /* Look for the optional `::' operator. */
19354 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19355 /* And the optional nested-name-specifier. */
19356 cp_parser_nested_name_specifier_opt (parser,
19357 /*typename_keyword_p=*/false,
19358 /*check_dependency_p=*/true,
19359 /*type_p=*/false,
19360 /*is_declaration=*/true);
19361 /* Get the namespace being used. */
19362 namespace_decl = cp_parser_namespace_name (parser);
19363 /* And any specified attributes. */
19364 attribs = cp_parser_attributes_opt (parser);
19365
19366 /* Update the symbol table. */
19367 if (namespace_bindings_p ())
19368 finish_namespace_using_directive (namespace_decl, attribs);
19369 else
19370 finish_local_using_directive (namespace_decl, attribs);
19371
19372 /* Look for the final `;'. */
19373 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19374 }
19375
19376 /* Parse an asm-definition.
19377
19378 asm-definition:
19379 asm ( string-literal ) ;
19380
19381 GNU Extension:
19382
19383 asm-definition:
19384 asm volatile [opt] ( string-literal ) ;
19385 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19386 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19387 : asm-operand-list [opt] ) ;
19388 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19389 : asm-operand-list [opt]
19390 : asm-clobber-list [opt] ) ;
19391 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19392 : asm-clobber-list [opt]
19393 : asm-goto-list ) ; */
19394
19395 static void
19396 cp_parser_asm_definition (cp_parser* parser)
19397 {
19398 tree string;
19399 tree outputs = NULL_TREE;
19400 tree inputs = NULL_TREE;
19401 tree clobbers = NULL_TREE;
19402 tree labels = NULL_TREE;
19403 tree asm_stmt;
19404 bool volatile_p = false;
19405 bool extended_p = false;
19406 bool invalid_inputs_p = false;
19407 bool invalid_outputs_p = false;
19408 bool goto_p = false;
19409 required_token missing = RT_NONE;
19410
19411 /* Look for the `asm' keyword. */
19412 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19413
19414 if (parser->in_function_body
19415 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19416 {
19417 error ("%<asm%> in %<constexpr%> function");
19418 cp_function_chain->invalid_constexpr = true;
19419 }
19420
19421 /* See if the next token is `volatile'. */
19422 if (cp_parser_allow_gnu_extensions_p (parser)
19423 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19424 {
19425 /* Remember that we saw the `volatile' keyword. */
19426 volatile_p = true;
19427 /* Consume the token. */
19428 cp_lexer_consume_token (parser->lexer);
19429 }
19430 if (cp_parser_allow_gnu_extensions_p (parser)
19431 && parser->in_function_body
19432 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19433 {
19434 /* Remember that we saw the `goto' keyword. */
19435 goto_p = true;
19436 /* Consume the token. */
19437 cp_lexer_consume_token (parser->lexer);
19438 }
19439 /* Look for the opening `('. */
19440 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19441 return;
19442 /* Look for the string. */
19443 string = cp_parser_string_literal (parser, false, false);
19444 if (string == error_mark_node)
19445 {
19446 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19447 /*consume_paren=*/true);
19448 return;
19449 }
19450
19451 /* If we're allowing GNU extensions, check for the extended assembly
19452 syntax. Unfortunately, the `:' tokens need not be separated by
19453 a space in C, and so, for compatibility, we tolerate that here
19454 too. Doing that means that we have to treat the `::' operator as
19455 two `:' tokens. */
19456 if (cp_parser_allow_gnu_extensions_p (parser)
19457 && parser->in_function_body
19458 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19459 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19460 {
19461 bool inputs_p = false;
19462 bool clobbers_p = false;
19463 bool labels_p = false;
19464
19465 /* The extended syntax was used. */
19466 extended_p = true;
19467
19468 /* Look for outputs. */
19469 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19470 {
19471 /* Consume the `:'. */
19472 cp_lexer_consume_token (parser->lexer);
19473 /* Parse the output-operands. */
19474 if (cp_lexer_next_token_is_not (parser->lexer,
19475 CPP_COLON)
19476 && cp_lexer_next_token_is_not (parser->lexer,
19477 CPP_SCOPE)
19478 && cp_lexer_next_token_is_not (parser->lexer,
19479 CPP_CLOSE_PAREN)
19480 && !goto_p)
19481 {
19482 outputs = cp_parser_asm_operand_list (parser);
19483 if (outputs == error_mark_node)
19484 invalid_outputs_p = true;
19485 }
19486 }
19487 /* If the next token is `::', there are no outputs, and the
19488 next token is the beginning of the inputs. */
19489 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19490 /* The inputs are coming next. */
19491 inputs_p = true;
19492
19493 /* Look for inputs. */
19494 if (inputs_p
19495 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19496 {
19497 /* Consume the `:' or `::'. */
19498 cp_lexer_consume_token (parser->lexer);
19499 /* Parse the output-operands. */
19500 if (cp_lexer_next_token_is_not (parser->lexer,
19501 CPP_COLON)
19502 && cp_lexer_next_token_is_not (parser->lexer,
19503 CPP_SCOPE)
19504 && cp_lexer_next_token_is_not (parser->lexer,
19505 CPP_CLOSE_PAREN))
19506 {
19507 inputs = cp_parser_asm_operand_list (parser);
19508 if (inputs == error_mark_node)
19509 invalid_inputs_p = true;
19510 }
19511 }
19512 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19513 /* The clobbers are coming next. */
19514 clobbers_p = true;
19515
19516 /* Look for clobbers. */
19517 if (clobbers_p
19518 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19519 {
19520 clobbers_p = true;
19521 /* Consume the `:' or `::'. */
19522 cp_lexer_consume_token (parser->lexer);
19523 /* Parse the clobbers. */
19524 if (cp_lexer_next_token_is_not (parser->lexer,
19525 CPP_COLON)
19526 && cp_lexer_next_token_is_not (parser->lexer,
19527 CPP_CLOSE_PAREN))
19528 clobbers = cp_parser_asm_clobber_list (parser);
19529 }
19530 else if (goto_p
19531 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19532 /* The labels are coming next. */
19533 labels_p = true;
19534
19535 /* Look for labels. */
19536 if (labels_p
19537 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19538 {
19539 labels_p = true;
19540 /* Consume the `:' or `::'. */
19541 cp_lexer_consume_token (parser->lexer);
19542 /* Parse the labels. */
19543 labels = cp_parser_asm_label_list (parser);
19544 }
19545
19546 if (goto_p && !labels_p)
19547 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19548 }
19549 else if (goto_p)
19550 missing = RT_COLON_SCOPE;
19551
19552 /* Look for the closing `)'. */
19553 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19554 missing ? missing : RT_CLOSE_PAREN))
19555 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19556 /*consume_paren=*/true);
19557 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19558
19559 if (!invalid_inputs_p && !invalid_outputs_p)
19560 {
19561 /* Create the ASM_EXPR. */
19562 if (parser->in_function_body)
19563 {
19564 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19565 inputs, clobbers, labels);
19566 /* If the extended syntax was not used, mark the ASM_EXPR. */
19567 if (!extended_p)
19568 {
19569 tree temp = asm_stmt;
19570 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19571 temp = TREE_OPERAND (temp, 0);
19572
19573 ASM_INPUT_P (temp) = 1;
19574 }
19575 }
19576 else
19577 symtab->finalize_toplevel_asm (string);
19578 }
19579 }
19580
19581 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19582 type that comes from the decl-specifier-seq. */
19583
19584 static tree
19585 strip_declarator_types (tree type, cp_declarator *declarator)
19586 {
19587 for (cp_declarator *d = declarator; d;)
19588 switch (d->kind)
19589 {
19590 case cdk_id:
19591 case cdk_decomp:
19592 case cdk_error:
19593 d = NULL;
19594 break;
19595
19596 default:
19597 if (TYPE_PTRMEMFUNC_P (type))
19598 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19599 type = TREE_TYPE (type);
19600 d = d->declarator;
19601 break;
19602 }
19603
19604 return type;
19605 }
19606
19607 /* Declarators [gram.dcl.decl] */
19608
19609 /* Parse an init-declarator.
19610
19611 init-declarator:
19612 declarator initializer [opt]
19613
19614 GNU Extension:
19615
19616 init-declarator:
19617 declarator asm-specification [opt] attributes [opt] initializer [opt]
19618
19619 function-definition:
19620 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19621 function-body
19622 decl-specifier-seq [opt] declarator function-try-block
19623
19624 GNU Extension:
19625
19626 function-definition:
19627 __extension__ function-definition
19628
19629 TM Extension:
19630
19631 function-definition:
19632 decl-specifier-seq [opt] declarator function-transaction-block
19633
19634 The DECL_SPECIFIERS apply to this declarator. Returns a
19635 representation of the entity declared. If MEMBER_P is TRUE, then
19636 this declarator appears in a class scope. The new DECL created by
19637 this declarator is returned.
19638
19639 The CHECKS are access checks that should be performed once we know
19640 what entity is being declared (and, therefore, what classes have
19641 befriended it).
19642
19643 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19644 for a function-definition here as well. If the declarator is a
19645 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19646 be TRUE upon return. By that point, the function-definition will
19647 have been completely parsed.
19648
19649 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19650 is FALSE.
19651
19652 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19653 parsed declaration if it is an uninitialized single declarator not followed
19654 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19655 if present, will not be consumed. If returned, this declarator will be
19656 created with SD_INITIALIZED but will not call cp_finish_decl.
19657
19658 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19659 and there is an initializer, the pointed location_t is set to the
19660 location of the '=' or `(', or '{' in C++11 token introducing the
19661 initializer. */
19662
19663 static tree
19664 cp_parser_init_declarator (cp_parser* parser,
19665 cp_decl_specifier_seq *decl_specifiers,
19666 vec<deferred_access_check, va_gc> *checks,
19667 bool function_definition_allowed_p,
19668 bool member_p,
19669 int declares_class_or_enum,
19670 bool* function_definition_p,
19671 tree* maybe_range_for_decl,
19672 location_t* init_loc,
19673 tree* auto_result)
19674 {
19675 cp_token *token = NULL, *asm_spec_start_token = NULL,
19676 *attributes_start_token = NULL;
19677 cp_declarator *declarator;
19678 tree prefix_attributes;
19679 tree attributes = NULL;
19680 tree asm_specification;
19681 tree initializer;
19682 tree decl = NULL_TREE;
19683 tree scope;
19684 int is_initialized;
19685 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19686 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19687 "(...)". */
19688 enum cpp_ttype initialization_kind;
19689 bool is_direct_init = false;
19690 bool is_non_constant_init;
19691 int ctor_dtor_or_conv_p;
19692 bool friend_p = cp_parser_friend_p (decl_specifiers);
19693 tree pushed_scope = NULL_TREE;
19694 bool range_for_decl_p = false;
19695 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19696 location_t tmp_init_loc = UNKNOWN_LOCATION;
19697
19698 /* Gather the attributes that were provided with the
19699 decl-specifiers. */
19700 prefix_attributes = decl_specifiers->attributes;
19701
19702 /* Assume that this is not the declarator for a function
19703 definition. */
19704 if (function_definition_p)
19705 *function_definition_p = false;
19706
19707 /* Default arguments are only permitted for function parameters. */
19708 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19709 parser->default_arg_ok_p = false;
19710
19711 /* Defer access checks while parsing the declarator; we cannot know
19712 what names are accessible until we know what is being
19713 declared. */
19714 resume_deferring_access_checks ();
19715
19716 token = cp_lexer_peek_token (parser->lexer);
19717
19718 /* Parse the declarator. */
19719 declarator
19720 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19721 &ctor_dtor_or_conv_p,
19722 /*parenthesized_p=*/NULL,
19723 member_p, friend_p);
19724 /* Gather up the deferred checks. */
19725 stop_deferring_access_checks ();
19726
19727 parser->default_arg_ok_p = saved_default_arg_ok_p;
19728
19729 /* If the DECLARATOR was erroneous, there's no need to go
19730 further. */
19731 if (declarator == cp_error_declarator)
19732 return error_mark_node;
19733
19734 /* Check that the number of template-parameter-lists is OK. */
19735 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19736 token->location))
19737 return error_mark_node;
19738
19739 if (declares_class_or_enum & 2)
19740 cp_parser_check_for_definition_in_return_type (declarator,
19741 decl_specifiers->type,
19742 decl_specifiers->locations[ds_type_spec]);
19743
19744 /* Figure out what scope the entity declared by the DECLARATOR is
19745 located in. `grokdeclarator' sometimes changes the scope, so
19746 we compute it now. */
19747 scope = get_scope_of_declarator (declarator);
19748
19749 /* Perform any lookups in the declared type which were thought to be
19750 dependent, but are not in the scope of the declarator. */
19751 decl_specifiers->type
19752 = maybe_update_decl_type (decl_specifiers->type, scope);
19753
19754 /* If we're allowing GNU extensions, look for an
19755 asm-specification. */
19756 if (cp_parser_allow_gnu_extensions_p (parser))
19757 {
19758 /* Look for an asm-specification. */
19759 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19760 asm_specification = cp_parser_asm_specification_opt (parser);
19761 }
19762 else
19763 asm_specification = NULL_TREE;
19764
19765 /* Look for attributes. */
19766 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19767 attributes = cp_parser_attributes_opt (parser);
19768
19769 /* Peek at the next token. */
19770 token = cp_lexer_peek_token (parser->lexer);
19771
19772 bool bogus_implicit_tmpl = false;
19773
19774 if (function_declarator_p (declarator))
19775 {
19776 /* Handle C++17 deduction guides. */
19777 if (!decl_specifiers->type
19778 && ctor_dtor_or_conv_p <= 0
19779 && cxx_dialect >= cxx17)
19780 {
19781 cp_declarator *id = get_id_declarator (declarator);
19782 tree name = id->u.id.unqualified_name;
19783 parser->scope = id->u.id.qualifying_scope;
19784 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19785 if (tmpl
19786 && (DECL_CLASS_TEMPLATE_P (tmpl)
19787 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19788 {
19789 id->u.id.unqualified_name = dguide_name (tmpl);
19790 id->u.id.sfk = sfk_deduction_guide;
19791 ctor_dtor_or_conv_p = 1;
19792 }
19793 }
19794
19795 /* Check to see if the token indicates the start of a
19796 function-definition. */
19797 if (cp_parser_token_starts_function_definition_p (token))
19798 {
19799 if (!function_definition_allowed_p)
19800 {
19801 /* If a function-definition should not appear here, issue an
19802 error message. */
19803 cp_parser_error (parser,
19804 "a function-definition is not allowed here");
19805 return error_mark_node;
19806 }
19807
19808 location_t func_brace_location
19809 = cp_lexer_peek_token (parser->lexer)->location;
19810
19811 /* Neither attributes nor an asm-specification are allowed
19812 on a function-definition. */
19813 if (asm_specification)
19814 error_at (asm_spec_start_token->location,
19815 "an asm-specification is not allowed "
19816 "on a function-definition");
19817 if (attributes)
19818 error_at (attributes_start_token->location,
19819 "attributes are not allowed "
19820 "on a function-definition");
19821 /* This is a function-definition. */
19822 *function_definition_p = true;
19823
19824 /* Parse the function definition. */
19825 if (member_p)
19826 decl = cp_parser_save_member_function_body (parser,
19827 decl_specifiers,
19828 declarator,
19829 prefix_attributes);
19830 else
19831 decl =
19832 (cp_parser_function_definition_from_specifiers_and_declarator
19833 (parser, decl_specifiers, prefix_attributes, declarator));
19834
19835 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19836 {
19837 /* This is where the prologue starts... */
19838 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19839 = func_brace_location;
19840 }
19841
19842 return decl;
19843 }
19844 }
19845 else if (parser->fully_implicit_function_template_p)
19846 {
19847 /* A non-template declaration involving a function parameter list
19848 containing an implicit template parameter will be made into a
19849 template. If the resulting declaration is not going to be an
19850 actual function then finish the template scope here to prevent it.
19851 An error message will be issued once we have a decl to talk about.
19852
19853 FIXME probably we should do type deduction rather than create an
19854 implicit template, but the standard currently doesn't allow it. */
19855 bogus_implicit_tmpl = true;
19856 finish_fully_implicit_template (parser, NULL_TREE);
19857 }
19858
19859 /* [dcl.dcl]
19860
19861 Only in function declarations for constructors, destructors, type
19862 conversions, and deduction guides can the decl-specifier-seq be omitted.
19863
19864 We explicitly postpone this check past the point where we handle
19865 function-definitions because we tolerate function-definitions
19866 that are missing their return types in some modes. */
19867 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19868 {
19869 cp_parser_error (parser,
19870 "expected constructor, destructor, or type conversion");
19871 return error_mark_node;
19872 }
19873
19874 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19875 if (token->type == CPP_EQ
19876 || token->type == CPP_OPEN_PAREN
19877 || token->type == CPP_OPEN_BRACE)
19878 {
19879 is_initialized = SD_INITIALIZED;
19880 initialization_kind = token->type;
19881 if (maybe_range_for_decl)
19882 *maybe_range_for_decl = error_mark_node;
19883 tmp_init_loc = token->location;
19884 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19885 *init_loc = tmp_init_loc;
19886
19887 if (token->type == CPP_EQ
19888 && function_declarator_p (declarator))
19889 {
19890 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19891 if (t2->keyword == RID_DEFAULT)
19892 is_initialized = SD_DEFAULTED;
19893 else if (t2->keyword == RID_DELETE)
19894 is_initialized = SD_DELETED;
19895 }
19896 }
19897 else
19898 {
19899 /* If the init-declarator isn't initialized and isn't followed by a
19900 `,' or `;', it's not a valid init-declarator. */
19901 if (token->type != CPP_COMMA
19902 && token->type != CPP_SEMICOLON)
19903 {
19904 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19905 range_for_decl_p = true;
19906 else
19907 {
19908 if (!maybe_range_for_decl)
19909 cp_parser_error (parser, "expected initializer");
19910 return error_mark_node;
19911 }
19912 }
19913 is_initialized = SD_UNINITIALIZED;
19914 initialization_kind = CPP_EOF;
19915 }
19916
19917 /* Because start_decl has side-effects, we should only call it if we
19918 know we're going ahead. By this point, we know that we cannot
19919 possibly be looking at any other construct. */
19920 cp_parser_commit_to_tentative_parse (parser);
19921
19922 /* Enter the newly declared entry in the symbol table. If we're
19923 processing a declaration in a class-specifier, we wait until
19924 after processing the initializer. */
19925 if (!member_p)
19926 {
19927 if (parser->in_unbraced_linkage_specification_p)
19928 decl_specifiers->storage_class = sc_extern;
19929 decl = start_decl (declarator, decl_specifiers,
19930 range_for_decl_p? SD_INITIALIZED : is_initialized,
19931 attributes, prefix_attributes, &pushed_scope);
19932 cp_finalize_omp_declare_simd (parser, decl);
19933 cp_finalize_oacc_routine (parser, decl, false);
19934 /* Adjust location of decl if declarator->id_loc is more appropriate:
19935 set, and decl wasn't merged with another decl, in which case its
19936 location would be different from input_location, and more accurate. */
19937 if (DECL_P (decl)
19938 && declarator->id_loc != UNKNOWN_LOCATION
19939 && DECL_SOURCE_LOCATION (decl) == input_location)
19940 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19941 }
19942 else if (scope)
19943 /* Enter the SCOPE. That way unqualified names appearing in the
19944 initializer will be looked up in SCOPE. */
19945 pushed_scope = push_scope (scope);
19946
19947 /* Perform deferred access control checks, now that we know in which
19948 SCOPE the declared entity resides. */
19949 if (!member_p && decl)
19950 {
19951 tree saved_current_function_decl = NULL_TREE;
19952
19953 /* If the entity being declared is a function, pretend that we
19954 are in its scope. If it is a `friend', it may have access to
19955 things that would not otherwise be accessible. */
19956 if (TREE_CODE (decl) == FUNCTION_DECL)
19957 {
19958 saved_current_function_decl = current_function_decl;
19959 current_function_decl = decl;
19960 }
19961
19962 /* Perform access checks for template parameters. */
19963 cp_parser_perform_template_parameter_access_checks (checks);
19964
19965 /* Perform the access control checks for the declarator and the
19966 decl-specifiers. */
19967 perform_deferred_access_checks (tf_warning_or_error);
19968
19969 /* Restore the saved value. */
19970 if (TREE_CODE (decl) == FUNCTION_DECL)
19971 current_function_decl = saved_current_function_decl;
19972 }
19973
19974 /* Parse the initializer. */
19975 initializer = NULL_TREE;
19976 is_direct_init = false;
19977 is_non_constant_init = true;
19978 if (is_initialized)
19979 {
19980 if (function_declarator_p (declarator))
19981 {
19982 if (initialization_kind == CPP_EQ)
19983 initializer = cp_parser_pure_specifier (parser);
19984 else
19985 {
19986 /* If the declaration was erroneous, we don't really
19987 know what the user intended, so just silently
19988 consume the initializer. */
19989 if (decl != error_mark_node)
19990 error_at (tmp_init_loc, "initializer provided for function");
19991 cp_parser_skip_to_closing_parenthesis (parser,
19992 /*recovering=*/true,
19993 /*or_comma=*/false,
19994 /*consume_paren=*/true);
19995 }
19996 }
19997 else
19998 {
19999 /* We want to record the extra mangling scope for in-class
20000 initializers of class members and initializers of static data
20001 member templates. The former involves deferring
20002 parsing of the initializer until end of class as with default
20003 arguments. So right here we only handle the latter. */
20004 if (!member_p && processing_template_decl && decl != error_mark_node)
20005 start_lambda_scope (decl);
20006 initializer = cp_parser_initializer (parser,
20007 &is_direct_init,
20008 &is_non_constant_init);
20009 if (!member_p && processing_template_decl && decl != error_mark_node)
20010 finish_lambda_scope ();
20011 if (initializer == error_mark_node)
20012 cp_parser_skip_to_end_of_statement (parser);
20013 }
20014 }
20015
20016 /* The old parser allows attributes to appear after a parenthesized
20017 initializer. Mark Mitchell proposed removing this functionality
20018 on the GCC mailing lists on 2002-08-13. This parser accepts the
20019 attributes -- but ignores them. Made a permerror in GCC 8. */
20020 if (cp_parser_allow_gnu_extensions_p (parser)
20021 && initialization_kind == CPP_OPEN_PAREN
20022 && cp_parser_attributes_opt (parser)
20023 && permerror (input_location,
20024 "attributes after parenthesized initializer ignored"))
20025 {
20026 static bool hint;
20027 if (flag_permissive && !hint)
20028 {
20029 hint = true;
20030 inform (input_location,
20031 "this flexibility is deprecated and will be removed");
20032 }
20033 }
20034
20035 /* And now complain about a non-function implicit template. */
20036 if (bogus_implicit_tmpl && decl != error_mark_node)
20037 error_at (DECL_SOURCE_LOCATION (decl),
20038 "non-function %qD declared as implicit template", decl);
20039
20040 /* For an in-class declaration, use `grokfield' to create the
20041 declaration. */
20042 if (member_p)
20043 {
20044 if (pushed_scope)
20045 {
20046 pop_scope (pushed_scope);
20047 pushed_scope = NULL_TREE;
20048 }
20049 decl = grokfield (declarator, decl_specifiers,
20050 initializer, !is_non_constant_init,
20051 /*asmspec=*/NULL_TREE,
20052 attr_chainon (attributes, prefix_attributes));
20053 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20054 cp_parser_save_default_args (parser, decl);
20055 cp_finalize_omp_declare_simd (parser, decl);
20056 cp_finalize_oacc_routine (parser, decl, false);
20057 }
20058
20059 /* Finish processing the declaration. But, skip member
20060 declarations. */
20061 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20062 {
20063 cp_finish_decl (decl,
20064 initializer, !is_non_constant_init,
20065 asm_specification,
20066 /* If the initializer is in parentheses, then this is
20067 a direct-initialization, which means that an
20068 `explicit' constructor is OK. Otherwise, an
20069 `explicit' constructor cannot be used. */
20070 ((is_direct_init || !is_initialized)
20071 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20072 }
20073 else if ((cxx_dialect != cxx98) && friend_p
20074 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20075 /* Core issue #226 (C++0x only): A default template-argument
20076 shall not be specified in a friend class template
20077 declaration. */
20078 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20079 /*is_partial=*/false, /*is_friend_decl=*/1);
20080
20081 if (!friend_p && pushed_scope)
20082 pop_scope (pushed_scope);
20083
20084 if (function_declarator_p (declarator)
20085 && parser->fully_implicit_function_template_p)
20086 {
20087 if (member_p)
20088 decl = finish_fully_implicit_template (parser, decl);
20089 else
20090 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20091 }
20092
20093 if (auto_result && is_initialized && decl_specifiers->type
20094 && type_uses_auto (decl_specifiers->type))
20095 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20096
20097 return decl;
20098 }
20099
20100 /* Parse a declarator.
20101
20102 declarator:
20103 direct-declarator
20104 ptr-operator declarator
20105
20106 abstract-declarator:
20107 ptr-operator abstract-declarator [opt]
20108 direct-abstract-declarator
20109
20110 GNU Extensions:
20111
20112 declarator:
20113 attributes [opt] direct-declarator
20114 attributes [opt] ptr-operator declarator
20115
20116 abstract-declarator:
20117 attributes [opt] ptr-operator abstract-declarator [opt]
20118 attributes [opt] direct-abstract-declarator
20119
20120 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20121 detect constructors, destructors, deduction guides, or conversion operators.
20122 It is set to -1 if the declarator is a name, and +1 if it is a
20123 function. Otherwise it is set to zero. Usually you just want to
20124 test for >0, but internally the negative value is used.
20125
20126 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20127 a decl-specifier-seq unless it declares a constructor, destructor,
20128 or conversion. It might seem that we could check this condition in
20129 semantic analysis, rather than parsing, but that makes it difficult
20130 to handle something like `f()'. We want to notice that there are
20131 no decl-specifiers, and therefore realize that this is an
20132 expression, not a declaration.)
20133
20134 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20135 the declarator is a direct-declarator of the form "(...)".
20136
20137 MEMBER_P is true iff this declarator is a member-declarator.
20138
20139 FRIEND_P is true iff this declarator is a friend. */
20140
20141 static cp_declarator *
20142 cp_parser_declarator (cp_parser* parser,
20143 cp_parser_declarator_kind dcl_kind,
20144 int* ctor_dtor_or_conv_p,
20145 bool* parenthesized_p,
20146 bool member_p, bool friend_p)
20147 {
20148 cp_declarator *declarator;
20149 enum tree_code code;
20150 cp_cv_quals cv_quals;
20151 tree class_type;
20152 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20153
20154 /* Assume this is not a constructor, destructor, or type-conversion
20155 operator. */
20156 if (ctor_dtor_or_conv_p)
20157 *ctor_dtor_or_conv_p = 0;
20158
20159 if (cp_parser_allow_gnu_extensions_p (parser))
20160 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20161
20162 /* Check for the ptr-operator production. */
20163 cp_parser_parse_tentatively (parser);
20164 /* Parse the ptr-operator. */
20165 code = cp_parser_ptr_operator (parser,
20166 &class_type,
20167 &cv_quals,
20168 &std_attributes);
20169
20170 /* If that worked, then we have a ptr-operator. */
20171 if (cp_parser_parse_definitely (parser))
20172 {
20173 /* If a ptr-operator was found, then this declarator was not
20174 parenthesized. */
20175 if (parenthesized_p)
20176 *parenthesized_p = true;
20177 /* The dependent declarator is optional if we are parsing an
20178 abstract-declarator. */
20179 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20180 cp_parser_parse_tentatively (parser);
20181
20182 /* Parse the dependent declarator. */
20183 declarator = cp_parser_declarator (parser, dcl_kind,
20184 /*ctor_dtor_or_conv_p=*/NULL,
20185 /*parenthesized_p=*/NULL,
20186 /*member_p=*/false,
20187 friend_p);
20188
20189 /* If we are parsing an abstract-declarator, we must handle the
20190 case where the dependent declarator is absent. */
20191 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20192 && !cp_parser_parse_definitely (parser))
20193 declarator = NULL;
20194
20195 declarator = cp_parser_make_indirect_declarator
20196 (code, class_type, cv_quals, declarator, std_attributes);
20197 }
20198 /* Everything else is a direct-declarator. */
20199 else
20200 {
20201 if (parenthesized_p)
20202 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20203 CPP_OPEN_PAREN);
20204 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20205 ctor_dtor_or_conv_p,
20206 member_p, friend_p);
20207 }
20208
20209 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20210 declarator->attributes = gnu_attributes;
20211 return declarator;
20212 }
20213
20214 /* Parse a direct-declarator or direct-abstract-declarator.
20215
20216 direct-declarator:
20217 declarator-id
20218 direct-declarator ( parameter-declaration-clause )
20219 cv-qualifier-seq [opt]
20220 ref-qualifier [opt]
20221 exception-specification [opt]
20222 direct-declarator [ constant-expression [opt] ]
20223 ( declarator )
20224
20225 direct-abstract-declarator:
20226 direct-abstract-declarator [opt]
20227 ( parameter-declaration-clause )
20228 cv-qualifier-seq [opt]
20229 ref-qualifier [opt]
20230 exception-specification [opt]
20231 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20232 ( abstract-declarator )
20233
20234 Returns a representation of the declarator. DCL_KIND is
20235 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20236 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20237 we are parsing a direct-declarator. It is
20238 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20239 of ambiguity we prefer an abstract declarator, as per
20240 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20241 as for cp_parser_declarator. */
20242
20243 static cp_declarator *
20244 cp_parser_direct_declarator (cp_parser* parser,
20245 cp_parser_declarator_kind dcl_kind,
20246 int* ctor_dtor_or_conv_p,
20247 bool member_p, bool friend_p)
20248 {
20249 cp_token *token;
20250 cp_declarator *declarator = NULL;
20251 tree scope = NULL_TREE;
20252 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20253 bool saved_in_declarator_p = parser->in_declarator_p;
20254 bool first = true;
20255 tree pushed_scope = NULL_TREE;
20256 cp_token *open_paren = NULL, *close_paren = NULL;
20257
20258 while (true)
20259 {
20260 /* Peek at the next token. */
20261 token = cp_lexer_peek_token (parser->lexer);
20262 if (token->type == CPP_OPEN_PAREN)
20263 {
20264 /* This is either a parameter-declaration-clause, or a
20265 parenthesized declarator. When we know we are parsing a
20266 named declarator, it must be a parenthesized declarator
20267 if FIRST is true. For instance, `(int)' is a
20268 parameter-declaration-clause, with an omitted
20269 direct-abstract-declarator. But `((*))', is a
20270 parenthesized abstract declarator. Finally, when T is a
20271 template parameter `(T)' is a
20272 parameter-declaration-clause, and not a parenthesized
20273 named declarator.
20274
20275 We first try and parse a parameter-declaration-clause,
20276 and then try a nested declarator (if FIRST is true).
20277
20278 It is not an error for it not to be a
20279 parameter-declaration-clause, even when FIRST is
20280 false. Consider,
20281
20282 int i (int);
20283 int i (3);
20284
20285 The first is the declaration of a function while the
20286 second is the definition of a variable, including its
20287 initializer.
20288
20289 Having seen only the parenthesis, we cannot know which of
20290 these two alternatives should be selected. Even more
20291 complex are examples like:
20292
20293 int i (int (a));
20294 int i (int (3));
20295
20296 The former is a function-declaration; the latter is a
20297 variable initialization.
20298
20299 Thus again, we try a parameter-declaration-clause, and if
20300 that fails, we back out and return. */
20301
20302 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20303 {
20304 tree params;
20305 bool is_declarator = false;
20306
20307 open_paren = NULL;
20308
20309 /* In a member-declarator, the only valid interpretation
20310 of a parenthesis is the start of a
20311 parameter-declaration-clause. (It is invalid to
20312 initialize a static data member with a parenthesized
20313 initializer; only the "=" form of initialization is
20314 permitted.) */
20315 if (!member_p)
20316 cp_parser_parse_tentatively (parser);
20317
20318 /* Consume the `('. */
20319 matching_parens parens;
20320 parens.consume_open (parser);
20321 if (first)
20322 {
20323 /* If this is going to be an abstract declarator, we're
20324 in a declarator and we can't have default args. */
20325 parser->default_arg_ok_p = false;
20326 parser->in_declarator_p = true;
20327 }
20328
20329 begin_scope (sk_function_parms, NULL_TREE);
20330
20331 /* Parse the parameter-declaration-clause. */
20332 params = cp_parser_parameter_declaration_clause (parser);
20333
20334 /* Consume the `)'. */
20335 parens.require_close (parser);
20336
20337 /* If all went well, parse the cv-qualifier-seq,
20338 ref-qualifier and the exception-specification. */
20339 if (member_p || cp_parser_parse_definitely (parser))
20340 {
20341 cp_cv_quals cv_quals;
20342 cp_virt_specifiers virt_specifiers;
20343 cp_ref_qualifier ref_qual;
20344 tree exception_specification;
20345 tree late_return;
20346 tree attrs;
20347 bool memfn = (member_p || (pushed_scope
20348 && CLASS_TYPE_P (pushed_scope)));
20349
20350 is_declarator = true;
20351
20352 if (ctor_dtor_or_conv_p)
20353 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20354 first = false;
20355
20356 /* Parse the cv-qualifier-seq. */
20357 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20358 /* Parse the ref-qualifier. */
20359 ref_qual = cp_parser_ref_qualifier_opt (parser);
20360 /* Parse the tx-qualifier. */
20361 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20362 /* And the exception-specification. */
20363 exception_specification
20364 = cp_parser_exception_specification_opt (parser);
20365
20366 attrs = cp_parser_std_attribute_spec_seq (parser);
20367
20368 /* In here, we handle cases where attribute is used after
20369 the function declaration. For example:
20370 void func (int x) __attribute__((vector(..))); */
20371 tree gnu_attrs = NULL_TREE;
20372 tree requires_clause = NULL_TREE;
20373 late_return = (cp_parser_late_return_type_opt
20374 (parser, declarator, requires_clause,
20375 memfn ? cv_quals : -1));
20376
20377 /* Parse the virt-specifier-seq. */
20378 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20379
20380 /* Create the function-declarator. */
20381 declarator = make_call_declarator (declarator,
20382 params,
20383 cv_quals,
20384 virt_specifiers,
20385 ref_qual,
20386 tx_qual,
20387 exception_specification,
20388 late_return,
20389 requires_clause);
20390 declarator->std_attributes = attrs;
20391 declarator->attributes = gnu_attrs;
20392 /* Any subsequent parameter lists are to do with
20393 return type, so are not those of the declared
20394 function. */
20395 parser->default_arg_ok_p = false;
20396 }
20397
20398 /* Remove the function parms from scope. */
20399 pop_bindings_and_leave_scope ();
20400
20401 if (is_declarator)
20402 /* Repeat the main loop. */
20403 continue;
20404 }
20405
20406 /* If this is the first, we can try a parenthesized
20407 declarator. */
20408 if (first)
20409 {
20410 bool saved_in_type_id_in_expr_p;
20411
20412 parser->default_arg_ok_p = saved_default_arg_ok_p;
20413 parser->in_declarator_p = saved_in_declarator_p;
20414
20415 open_paren = token;
20416 /* Consume the `('. */
20417 matching_parens parens;
20418 parens.consume_open (parser);
20419 /* Parse the nested declarator. */
20420 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20421 parser->in_type_id_in_expr_p = true;
20422 declarator
20423 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20424 /*parenthesized_p=*/NULL,
20425 member_p, friend_p);
20426 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20427 first = false;
20428 /* Expect a `)'. */
20429 close_paren = cp_lexer_peek_token (parser->lexer);
20430 if (!parens.require_close (parser))
20431 declarator = cp_error_declarator;
20432 if (declarator == cp_error_declarator)
20433 break;
20434
20435 goto handle_declarator;
20436 }
20437 /* Otherwise, we must be done. */
20438 else
20439 break;
20440 }
20441 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20442 && token->type == CPP_OPEN_SQUARE
20443 && !cp_next_tokens_can_be_attribute_p (parser))
20444 {
20445 /* Parse an array-declarator. */
20446 tree bounds, attrs;
20447
20448 if (ctor_dtor_or_conv_p)
20449 *ctor_dtor_or_conv_p = 0;
20450
20451 open_paren = NULL;
20452 first = false;
20453 parser->default_arg_ok_p = false;
20454 parser->in_declarator_p = true;
20455 /* Consume the `['. */
20456 cp_lexer_consume_token (parser->lexer);
20457 /* Peek at the next token. */
20458 token = cp_lexer_peek_token (parser->lexer);
20459 /* If the next token is `]', then there is no
20460 constant-expression. */
20461 if (token->type != CPP_CLOSE_SQUARE)
20462 {
20463 bool non_constant_p;
20464 bounds
20465 = cp_parser_constant_expression (parser,
20466 /*allow_non_constant=*/true,
20467 &non_constant_p);
20468 if (!non_constant_p)
20469 /* OK */;
20470 else if (error_operand_p (bounds))
20471 /* Already gave an error. */;
20472 else if (!parser->in_function_body
20473 || current_binding_level->kind == sk_function_parms)
20474 {
20475 /* Normally, the array bound must be an integral constant
20476 expression. However, as an extension, we allow VLAs
20477 in function scopes as long as they aren't part of a
20478 parameter declaration. */
20479 cp_parser_error (parser,
20480 "array bound is not an integer constant");
20481 bounds = error_mark_node;
20482 }
20483 else if (processing_template_decl
20484 && !type_dependent_expression_p (bounds))
20485 {
20486 /* Remember this wasn't a constant-expression. */
20487 bounds = build_nop (TREE_TYPE (bounds), bounds);
20488 TREE_SIDE_EFFECTS (bounds) = 1;
20489 }
20490 }
20491 else
20492 bounds = NULL_TREE;
20493 /* Look for the closing `]'. */
20494 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20495 {
20496 declarator = cp_error_declarator;
20497 break;
20498 }
20499
20500 attrs = cp_parser_std_attribute_spec_seq (parser);
20501 declarator = make_array_declarator (declarator, bounds);
20502 declarator->std_attributes = attrs;
20503 }
20504 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20505 {
20506 {
20507 tree qualifying_scope;
20508 tree unqualified_name;
20509 tree attrs;
20510 special_function_kind sfk;
20511 bool abstract_ok;
20512 bool pack_expansion_p = false;
20513 cp_token *declarator_id_start_token;
20514
20515 /* Parse a declarator-id */
20516 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20517 if (abstract_ok)
20518 {
20519 cp_parser_parse_tentatively (parser);
20520
20521 /* If we see an ellipsis, we should be looking at a
20522 parameter pack. */
20523 if (token->type == CPP_ELLIPSIS)
20524 {
20525 /* Consume the `...' */
20526 cp_lexer_consume_token (parser->lexer);
20527
20528 pack_expansion_p = true;
20529 }
20530 }
20531
20532 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20533 unqualified_name
20534 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20535 qualifying_scope = parser->scope;
20536 if (abstract_ok)
20537 {
20538 bool okay = false;
20539
20540 if (!unqualified_name && pack_expansion_p)
20541 {
20542 /* Check whether an error occurred. */
20543 okay = !cp_parser_error_occurred (parser);
20544
20545 /* We already consumed the ellipsis to mark a
20546 parameter pack, but we have no way to report it,
20547 so abort the tentative parse. We will be exiting
20548 immediately anyway. */
20549 cp_parser_abort_tentative_parse (parser);
20550 }
20551 else
20552 okay = cp_parser_parse_definitely (parser);
20553
20554 if (!okay)
20555 unqualified_name = error_mark_node;
20556 else if (unqualified_name
20557 && (qualifying_scope
20558 || (!identifier_p (unqualified_name))))
20559 {
20560 cp_parser_error (parser, "expected unqualified-id");
20561 unqualified_name = error_mark_node;
20562 }
20563 }
20564
20565 if (!unqualified_name)
20566 return NULL;
20567 if (unqualified_name == error_mark_node)
20568 {
20569 declarator = cp_error_declarator;
20570 pack_expansion_p = false;
20571 declarator->parameter_pack_p = false;
20572 break;
20573 }
20574
20575 attrs = cp_parser_std_attribute_spec_seq (parser);
20576
20577 if (qualifying_scope && at_namespace_scope_p ()
20578 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20579 {
20580 /* In the declaration of a member of a template class
20581 outside of the class itself, the SCOPE will sometimes
20582 be a TYPENAME_TYPE. For example, given:
20583
20584 template <typename T>
20585 int S<T>::R::i = 3;
20586
20587 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20588 this context, we must resolve S<T>::R to an ordinary
20589 type, rather than a typename type.
20590
20591 The reason we normally avoid resolving TYPENAME_TYPEs
20592 is that a specialization of `S' might render
20593 `S<T>::R' not a type. However, if `S' is
20594 specialized, then this `i' will not be used, so there
20595 is no harm in resolving the types here. */
20596 tree type;
20597
20598 /* Resolve the TYPENAME_TYPE. */
20599 type = resolve_typename_type (qualifying_scope,
20600 /*only_current_p=*/false);
20601 /* If that failed, the declarator is invalid. */
20602 if (TREE_CODE (type) == TYPENAME_TYPE)
20603 {
20604 if (typedef_variant_p (type))
20605 error_at (declarator_id_start_token->location,
20606 "cannot define member of dependent typedef "
20607 "%qT", type);
20608 else
20609 error_at (declarator_id_start_token->location,
20610 "%<%T::%E%> is not a type",
20611 TYPE_CONTEXT (qualifying_scope),
20612 TYPE_IDENTIFIER (qualifying_scope));
20613 }
20614 qualifying_scope = type;
20615 }
20616
20617 sfk = sfk_none;
20618
20619 if (unqualified_name)
20620 {
20621 tree class_type;
20622
20623 if (qualifying_scope
20624 && CLASS_TYPE_P (qualifying_scope))
20625 class_type = qualifying_scope;
20626 else
20627 class_type = current_class_type;
20628
20629 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20630 {
20631 tree name_type = TREE_TYPE (unqualified_name);
20632
20633 if (!class_type || !same_type_p (name_type, class_type))
20634 {
20635 /* We do not attempt to print the declarator
20636 here because we do not have enough
20637 information about its original syntactic
20638 form. */
20639 cp_parser_error (parser, "invalid declarator");
20640 declarator = cp_error_declarator;
20641 break;
20642 }
20643 else if (qualifying_scope
20644 && CLASSTYPE_USE_TEMPLATE (name_type))
20645 {
20646 error_at (declarator_id_start_token->location,
20647 "invalid use of constructor as a template");
20648 inform (declarator_id_start_token->location,
20649 "use %<%T::%D%> instead of %<%T::%D%> to "
20650 "name the constructor in a qualified name",
20651 class_type,
20652 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20653 class_type, name_type);
20654 declarator = cp_error_declarator;
20655 break;
20656 }
20657 unqualified_name = constructor_name (class_type);
20658 }
20659
20660 if (class_type)
20661 {
20662 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20663 sfk = sfk_destructor;
20664 else if (identifier_p (unqualified_name)
20665 && IDENTIFIER_CONV_OP_P (unqualified_name))
20666 sfk = sfk_conversion;
20667 else if (/* There's no way to declare a constructor
20668 for an unnamed type, even if the type
20669 got a name for linkage purposes. */
20670 !TYPE_WAS_UNNAMED (class_type)
20671 /* Handle correctly (c++/19200):
20672
20673 struct S {
20674 struct T{};
20675 friend void S(T);
20676 };
20677
20678 and also:
20679
20680 namespace N {
20681 void S();
20682 }
20683
20684 struct S {
20685 friend void N::S();
20686 }; */
20687 && (!friend_p || class_type == qualifying_scope)
20688 && constructor_name_p (unqualified_name,
20689 class_type))
20690 sfk = sfk_constructor;
20691 else if (is_overloaded_fn (unqualified_name)
20692 && DECL_CONSTRUCTOR_P (get_first_fn
20693 (unqualified_name)))
20694 sfk = sfk_constructor;
20695
20696 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20697 *ctor_dtor_or_conv_p = -1;
20698 }
20699 }
20700 declarator = make_id_declarator (qualifying_scope,
20701 unqualified_name,
20702 sfk);
20703 declarator->std_attributes = attrs;
20704 declarator->id_loc = token->location;
20705 declarator->parameter_pack_p = pack_expansion_p;
20706
20707 if (pack_expansion_p)
20708 maybe_warn_variadic_templates ();
20709 }
20710
20711 handle_declarator:;
20712 scope = get_scope_of_declarator (declarator);
20713 if (scope)
20714 {
20715 /* Any names that appear after the declarator-id for a
20716 member are looked up in the containing scope. */
20717 if (at_function_scope_p ())
20718 {
20719 /* But declarations with qualified-ids can't appear in a
20720 function. */
20721 cp_parser_error (parser, "qualified-id in declaration");
20722 declarator = cp_error_declarator;
20723 break;
20724 }
20725 pushed_scope = push_scope (scope);
20726 }
20727 parser->in_declarator_p = true;
20728 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20729 || (declarator && declarator->kind == cdk_id))
20730 /* Default args are only allowed on function
20731 declarations. */
20732 parser->default_arg_ok_p = saved_default_arg_ok_p;
20733 else
20734 parser->default_arg_ok_p = false;
20735
20736 first = false;
20737 }
20738 /* We're done. */
20739 else
20740 break;
20741 }
20742
20743 /* For an abstract declarator, we might wind up with nothing at this
20744 point. That's an error; the declarator is not optional. */
20745 if (!declarator)
20746 cp_parser_error (parser, "expected declarator");
20747 else if (open_paren)
20748 {
20749 /* Record overly parenthesized declarator so we can give a
20750 diagnostic about confusing decl/expr disambiguation. */
20751 if (declarator->kind == cdk_array)
20752 {
20753 /* If the open and close parens are on different lines, this
20754 is probably a formatting thing, so ignore. */
20755 expanded_location open = expand_location (open_paren->location);
20756 expanded_location close = expand_location (close_paren->location);
20757 if (open.line != close.line || open.file != close.file)
20758 open_paren = NULL;
20759 }
20760 if (open_paren)
20761 declarator->parenthesized = open_paren->location;
20762 }
20763
20764 /* If we entered a scope, we must exit it now. */
20765 if (pushed_scope)
20766 pop_scope (pushed_scope);
20767
20768 parser->default_arg_ok_p = saved_default_arg_ok_p;
20769 parser->in_declarator_p = saved_in_declarator_p;
20770
20771 return declarator;
20772 }
20773
20774 /* Parse a ptr-operator.
20775
20776 ptr-operator:
20777 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20778 * cv-qualifier-seq [opt]
20779 &
20780 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20781 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20782
20783 GNU Extension:
20784
20785 ptr-operator:
20786 & cv-qualifier-seq [opt]
20787
20788 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20789 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20790 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20791 filled in with the TYPE containing the member. *CV_QUALS is
20792 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20793 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20794 Note that the tree codes returned by this function have nothing
20795 to do with the types of trees that will be eventually be created
20796 to represent the pointer or reference type being parsed. They are
20797 just constants with suggestive names. */
20798 static enum tree_code
20799 cp_parser_ptr_operator (cp_parser* parser,
20800 tree* type,
20801 cp_cv_quals *cv_quals,
20802 tree *attributes)
20803 {
20804 enum tree_code code = ERROR_MARK;
20805 cp_token *token;
20806 tree attrs = NULL_TREE;
20807
20808 /* Assume that it's not a pointer-to-member. */
20809 *type = NULL_TREE;
20810 /* And that there are no cv-qualifiers. */
20811 *cv_quals = TYPE_UNQUALIFIED;
20812
20813 /* Peek at the next token. */
20814 token = cp_lexer_peek_token (parser->lexer);
20815
20816 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20817 if (token->type == CPP_MULT)
20818 code = INDIRECT_REF;
20819 else if (token->type == CPP_AND)
20820 code = ADDR_EXPR;
20821 else if ((cxx_dialect != cxx98) &&
20822 token->type == CPP_AND_AND) /* C++0x only */
20823 code = NON_LVALUE_EXPR;
20824
20825 if (code != ERROR_MARK)
20826 {
20827 /* Consume the `*', `&' or `&&'. */
20828 cp_lexer_consume_token (parser->lexer);
20829
20830 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20831 `&', if we are allowing GNU extensions. (The only qualifier
20832 that can legally appear after `&' is `restrict', but that is
20833 enforced during semantic analysis. */
20834 if (code == INDIRECT_REF
20835 || cp_parser_allow_gnu_extensions_p (parser))
20836 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20837
20838 attrs = cp_parser_std_attribute_spec_seq (parser);
20839 if (attributes != NULL)
20840 *attributes = attrs;
20841 }
20842 else
20843 {
20844 /* Try the pointer-to-member case. */
20845 cp_parser_parse_tentatively (parser);
20846 /* Look for the optional `::' operator. */
20847 cp_parser_global_scope_opt (parser,
20848 /*current_scope_valid_p=*/false);
20849 /* Look for the nested-name specifier. */
20850 token = cp_lexer_peek_token (parser->lexer);
20851 cp_parser_nested_name_specifier (parser,
20852 /*typename_keyword_p=*/false,
20853 /*check_dependency_p=*/true,
20854 /*type_p=*/false,
20855 /*is_declaration=*/false);
20856 /* If we found it, and the next token is a `*', then we are
20857 indeed looking at a pointer-to-member operator. */
20858 if (!cp_parser_error_occurred (parser)
20859 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20860 {
20861 /* Indicate that the `*' operator was used. */
20862 code = INDIRECT_REF;
20863
20864 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20865 error_at (token->location, "%qD is a namespace", parser->scope);
20866 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20867 error_at (token->location, "cannot form pointer to member of "
20868 "non-class %q#T", parser->scope);
20869 else
20870 {
20871 /* The type of which the member is a member is given by the
20872 current SCOPE. */
20873 *type = parser->scope;
20874 /* The next name will not be qualified. */
20875 parser->scope = NULL_TREE;
20876 parser->qualifying_scope = NULL_TREE;
20877 parser->object_scope = NULL_TREE;
20878 /* Look for optional c++11 attributes. */
20879 attrs = cp_parser_std_attribute_spec_seq (parser);
20880 if (attributes != NULL)
20881 *attributes = attrs;
20882 /* Look for the optional cv-qualifier-seq. */
20883 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20884 }
20885 }
20886 /* If that didn't work we don't have a ptr-operator. */
20887 if (!cp_parser_parse_definitely (parser))
20888 cp_parser_error (parser, "expected ptr-operator");
20889 }
20890
20891 return code;
20892 }
20893
20894 /* Parse an (optional) cv-qualifier-seq.
20895
20896 cv-qualifier-seq:
20897 cv-qualifier cv-qualifier-seq [opt]
20898
20899 cv-qualifier:
20900 const
20901 volatile
20902
20903 GNU Extension:
20904
20905 cv-qualifier:
20906 __restrict__
20907
20908 Returns a bitmask representing the cv-qualifiers. */
20909
20910 static cp_cv_quals
20911 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20912 {
20913 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20914
20915 while (true)
20916 {
20917 cp_token *token;
20918 cp_cv_quals cv_qualifier;
20919
20920 /* Peek at the next token. */
20921 token = cp_lexer_peek_token (parser->lexer);
20922 /* See if it's a cv-qualifier. */
20923 switch (token->keyword)
20924 {
20925 case RID_CONST:
20926 cv_qualifier = TYPE_QUAL_CONST;
20927 break;
20928
20929 case RID_VOLATILE:
20930 cv_qualifier = TYPE_QUAL_VOLATILE;
20931 break;
20932
20933 case RID_RESTRICT:
20934 cv_qualifier = TYPE_QUAL_RESTRICT;
20935 break;
20936
20937 default:
20938 cv_qualifier = TYPE_UNQUALIFIED;
20939 break;
20940 }
20941
20942 if (!cv_qualifier)
20943 break;
20944
20945 if (cv_quals & cv_qualifier)
20946 {
20947 gcc_rich_location richloc (token->location);
20948 richloc.add_fixit_remove ();
20949 error_at (&richloc, "duplicate cv-qualifier");
20950 cp_lexer_purge_token (parser->lexer);
20951 }
20952 else
20953 {
20954 cp_lexer_consume_token (parser->lexer);
20955 cv_quals |= cv_qualifier;
20956 }
20957 }
20958
20959 return cv_quals;
20960 }
20961
20962 /* Parse an (optional) ref-qualifier
20963
20964 ref-qualifier:
20965 &
20966 &&
20967
20968 Returns cp_ref_qualifier representing ref-qualifier. */
20969
20970 static cp_ref_qualifier
20971 cp_parser_ref_qualifier_opt (cp_parser* parser)
20972 {
20973 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20974
20975 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20976 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20977 return ref_qual;
20978
20979 while (true)
20980 {
20981 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20982 cp_token *token = cp_lexer_peek_token (parser->lexer);
20983
20984 switch (token->type)
20985 {
20986 case CPP_AND:
20987 curr_ref_qual = REF_QUAL_LVALUE;
20988 break;
20989
20990 case CPP_AND_AND:
20991 curr_ref_qual = REF_QUAL_RVALUE;
20992 break;
20993
20994 default:
20995 curr_ref_qual = REF_QUAL_NONE;
20996 break;
20997 }
20998
20999 if (!curr_ref_qual)
21000 break;
21001 else if (ref_qual)
21002 {
21003 error_at (token->location, "multiple ref-qualifiers");
21004 cp_lexer_purge_token (parser->lexer);
21005 }
21006 else
21007 {
21008 ref_qual = curr_ref_qual;
21009 cp_lexer_consume_token (parser->lexer);
21010 }
21011 }
21012
21013 return ref_qual;
21014 }
21015
21016 /* Parse an optional tx-qualifier.
21017
21018 tx-qualifier:
21019 transaction_safe
21020 transaction_safe_dynamic */
21021
21022 static tree
21023 cp_parser_tx_qualifier_opt (cp_parser *parser)
21024 {
21025 cp_token *token = cp_lexer_peek_token (parser->lexer);
21026 if (token->type == CPP_NAME)
21027 {
21028 tree name = token->u.value;
21029 const char *p = IDENTIFIER_POINTER (name);
21030 const int len = strlen ("transaction_safe");
21031 if (!strncmp (p, "transaction_safe", len))
21032 {
21033 p += len;
21034 if (*p == '\0'
21035 || !strcmp (p, "_dynamic"))
21036 {
21037 cp_lexer_consume_token (parser->lexer);
21038 if (!flag_tm)
21039 {
21040 error ("%qE requires %<-fgnu-tm%>", name);
21041 return NULL_TREE;
21042 }
21043 else
21044 return name;
21045 }
21046 }
21047 }
21048 return NULL_TREE;
21049 }
21050
21051 /* Parse an (optional) virt-specifier-seq.
21052
21053 virt-specifier-seq:
21054 virt-specifier virt-specifier-seq [opt]
21055
21056 virt-specifier:
21057 override
21058 final
21059
21060 Returns a bitmask representing the virt-specifiers. */
21061
21062 static cp_virt_specifiers
21063 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21064 {
21065 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21066
21067 while (true)
21068 {
21069 cp_token *token;
21070 cp_virt_specifiers virt_specifier;
21071
21072 /* Peek at the next token. */
21073 token = cp_lexer_peek_token (parser->lexer);
21074 /* See if it's a virt-specifier-qualifier. */
21075 if (token->type != CPP_NAME)
21076 break;
21077 if (id_equal (token->u.value, "override"))
21078 {
21079 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21080 virt_specifier = VIRT_SPEC_OVERRIDE;
21081 }
21082 else if (id_equal (token->u.value, "final"))
21083 {
21084 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21085 virt_specifier = VIRT_SPEC_FINAL;
21086 }
21087 else if (id_equal (token->u.value, "__final"))
21088 {
21089 virt_specifier = VIRT_SPEC_FINAL;
21090 }
21091 else
21092 break;
21093
21094 if (virt_specifiers & virt_specifier)
21095 {
21096 gcc_rich_location richloc (token->location);
21097 richloc.add_fixit_remove ();
21098 error_at (&richloc, "duplicate virt-specifier");
21099 cp_lexer_purge_token (parser->lexer);
21100 }
21101 else
21102 {
21103 cp_lexer_consume_token (parser->lexer);
21104 virt_specifiers |= virt_specifier;
21105 }
21106 }
21107 return virt_specifiers;
21108 }
21109
21110 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21111 is in scope even though it isn't real. */
21112
21113 void
21114 inject_this_parameter (tree ctype, cp_cv_quals quals)
21115 {
21116 tree this_parm;
21117
21118 if (current_class_ptr)
21119 {
21120 /* We don't clear this between NSDMIs. Is it already what we want? */
21121 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21122 if (DECL_P (current_class_ptr)
21123 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21124 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21125 && cp_type_quals (type) == quals)
21126 return;
21127 }
21128
21129 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21130 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21131 current_class_ptr = NULL_TREE;
21132 current_class_ref
21133 = cp_build_fold_indirect_ref (this_parm);
21134 current_class_ptr = this_parm;
21135 }
21136
21137 /* Return true iff our current scope is a non-static data member
21138 initializer. */
21139
21140 bool
21141 parsing_nsdmi (void)
21142 {
21143 /* We recognize NSDMI context by the context-less 'this' pointer set up
21144 by the function above. */
21145 if (current_class_ptr
21146 && TREE_CODE (current_class_ptr) == PARM_DECL
21147 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21148 return true;
21149 return false;
21150 }
21151
21152 /* Parse a late-specified return type, if any. This is not a separate
21153 non-terminal, but part of a function declarator, which looks like
21154
21155 -> trailing-type-specifier-seq abstract-declarator(opt)
21156
21157 Returns the type indicated by the type-id.
21158
21159 In addition to this, parse any queued up #pragma omp declare simd
21160 clauses, and #pragma acc routine clauses.
21161
21162 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21163 function. */
21164
21165 static tree
21166 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21167 tree& requires_clause, cp_cv_quals quals)
21168 {
21169 cp_token *token;
21170 tree type = NULL_TREE;
21171 bool declare_simd_p = (parser->omp_declare_simd
21172 && declarator
21173 && declarator->kind == cdk_id);
21174
21175 bool oacc_routine_p = (parser->oacc_routine
21176 && declarator
21177 && declarator->kind == cdk_id);
21178
21179 /* Peek at the next token. */
21180 token = cp_lexer_peek_token (parser->lexer);
21181 /* A late-specified return type is indicated by an initial '->'. */
21182 if (token->type != CPP_DEREF
21183 && token->keyword != RID_REQUIRES
21184 && !(token->type == CPP_NAME
21185 && token->u.value == ridpointers[RID_REQUIRES])
21186 && !(declare_simd_p || oacc_routine_p))
21187 return NULL_TREE;
21188
21189 tree save_ccp = current_class_ptr;
21190 tree save_ccr = current_class_ref;
21191 if (quals >= 0)
21192 {
21193 /* DR 1207: 'this' is in scope in the trailing return type. */
21194 inject_this_parameter (current_class_type, quals);
21195 }
21196
21197 if (token->type == CPP_DEREF)
21198 {
21199 /* Consume the ->. */
21200 cp_lexer_consume_token (parser->lexer);
21201
21202 type = cp_parser_trailing_type_id (parser);
21203 }
21204
21205 /* Function declarations may be followed by a trailing
21206 requires-clause. */
21207 requires_clause = cp_parser_requires_clause_opt (parser);
21208
21209 if (declare_simd_p)
21210 declarator->attributes
21211 = cp_parser_late_parsing_omp_declare_simd (parser,
21212 declarator->attributes);
21213 if (oacc_routine_p)
21214 declarator->attributes
21215 = cp_parser_late_parsing_oacc_routine (parser,
21216 declarator->attributes);
21217
21218 if (quals >= 0)
21219 {
21220 current_class_ptr = save_ccp;
21221 current_class_ref = save_ccr;
21222 }
21223
21224 return type;
21225 }
21226
21227 /* Parse a declarator-id.
21228
21229 declarator-id:
21230 id-expression
21231 :: [opt] nested-name-specifier [opt] type-name
21232
21233 In the `id-expression' case, the value returned is as for
21234 cp_parser_id_expression if the id-expression was an unqualified-id.
21235 If the id-expression was a qualified-id, then a SCOPE_REF is
21236 returned. The first operand is the scope (either a NAMESPACE_DECL
21237 or TREE_TYPE), but the second is still just a representation of an
21238 unqualified-id. */
21239
21240 static tree
21241 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21242 {
21243 tree id;
21244 /* The expression must be an id-expression. Assume that qualified
21245 names are the names of types so that:
21246
21247 template <class T>
21248 int S<T>::R::i = 3;
21249
21250 will work; we must treat `S<T>::R' as the name of a type.
21251 Similarly, assume that qualified names are templates, where
21252 required, so that:
21253
21254 template <class T>
21255 int S<T>::R<T>::i = 3;
21256
21257 will work, too. */
21258 id = cp_parser_id_expression (parser,
21259 /*template_keyword_p=*/false,
21260 /*check_dependency_p=*/false,
21261 /*template_p=*/NULL,
21262 /*declarator_p=*/true,
21263 optional_p);
21264 if (id && BASELINK_P (id))
21265 id = BASELINK_FUNCTIONS (id);
21266 return id;
21267 }
21268
21269 /* Parse a type-id.
21270
21271 type-id:
21272 type-specifier-seq abstract-declarator [opt]
21273
21274 Returns the TYPE specified. */
21275
21276 static tree
21277 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21278 bool is_trailing_return, location_t * type_location)
21279 {
21280 cp_decl_specifier_seq type_specifier_seq;
21281 cp_declarator *abstract_declarator;
21282
21283 /* Parse the type-specifier-seq. */
21284 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21285 is_trailing_return,
21286 &type_specifier_seq);
21287 if (type_location)
21288 *type_location = type_specifier_seq.locations[ds_type_spec];
21289
21290 if (is_template_arg && type_specifier_seq.type
21291 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21292 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21293 /* A bare template name as a template argument is a template template
21294 argument, not a placeholder, so fail parsing it as a type argument. */
21295 {
21296 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21297 cp_parser_simulate_error (parser);
21298 return error_mark_node;
21299 }
21300 if (type_specifier_seq.type == error_mark_node)
21301 return error_mark_node;
21302
21303 /* There might or might not be an abstract declarator. */
21304 cp_parser_parse_tentatively (parser);
21305 /* Look for the declarator. */
21306 abstract_declarator
21307 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21308 /*parenthesized_p=*/NULL,
21309 /*member_p=*/false,
21310 /*friend_p=*/false);
21311 /* Check to see if there really was a declarator. */
21312 if (!cp_parser_parse_definitely (parser))
21313 abstract_declarator = NULL;
21314
21315 if (type_specifier_seq.type
21316 /* The concepts TS allows 'auto' as a type-id. */
21317 && (!flag_concepts || parser->in_type_id_in_expr_p)
21318 /* None of the valid uses of 'auto' in C++14 involve the type-id
21319 nonterminal, but it is valid in a trailing-return-type. */
21320 && !(cxx_dialect >= cxx14 && is_trailing_return))
21321 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21322 {
21323 /* A type-id with type 'auto' is only ok if the abstract declarator
21324 is a function declarator with a late-specified return type.
21325
21326 A type-id with 'auto' is also valid in a trailing-return-type
21327 in a compound-requirement. */
21328 if (abstract_declarator
21329 && abstract_declarator->kind == cdk_function
21330 && abstract_declarator->u.function.late_return_type)
21331 /* OK */;
21332 else if (parser->in_result_type_constraint_p)
21333 /* OK */;
21334 else
21335 {
21336 location_t loc = type_specifier_seq.locations[ds_type_spec];
21337 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21338 {
21339 error_at (loc, "missing template arguments after %qT",
21340 auto_node);
21341 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21342 tmpl);
21343 }
21344 else
21345 error_at (loc, "invalid use of %qT", auto_node);
21346 return error_mark_node;
21347 }
21348 }
21349
21350 return groktypename (&type_specifier_seq, abstract_declarator,
21351 is_template_arg);
21352 }
21353
21354 static tree
21355 cp_parser_type_id (cp_parser *parser, location_t * type_location)
21356 {
21357 return cp_parser_type_id_1 (parser, false, false, type_location);
21358 }
21359
21360 static tree
21361 cp_parser_template_type_arg (cp_parser *parser)
21362 {
21363 tree r;
21364 const char *saved_message = parser->type_definition_forbidden_message;
21365 parser->type_definition_forbidden_message
21366 = G_("types may not be defined in template arguments");
21367 r = cp_parser_type_id_1 (parser, true, false, NULL);
21368 parser->type_definition_forbidden_message = saved_message;
21369 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21370 {
21371 error ("invalid use of %<auto%> in template argument");
21372 r = error_mark_node;
21373 }
21374 return r;
21375 }
21376
21377 static tree
21378 cp_parser_trailing_type_id (cp_parser *parser)
21379 {
21380 return cp_parser_type_id_1 (parser, false, true, NULL);
21381 }
21382
21383 /* Parse a type-specifier-seq.
21384
21385 type-specifier-seq:
21386 type-specifier type-specifier-seq [opt]
21387
21388 GNU extension:
21389
21390 type-specifier-seq:
21391 attributes type-specifier-seq [opt]
21392
21393 If IS_DECLARATION is true, we are at the start of a "condition" or
21394 exception-declaration, so we might be followed by a declarator-id.
21395
21396 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21397 i.e. we've just seen "->".
21398
21399 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21400
21401 static void
21402 cp_parser_type_specifier_seq (cp_parser* parser,
21403 bool is_declaration,
21404 bool is_trailing_return,
21405 cp_decl_specifier_seq *type_specifier_seq)
21406 {
21407 bool seen_type_specifier = false;
21408 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21409 cp_token *start_token = NULL;
21410
21411 /* Clear the TYPE_SPECIFIER_SEQ. */
21412 clear_decl_specs (type_specifier_seq);
21413
21414 /* In the context of a trailing return type, enum E { } is an
21415 elaborated-type-specifier followed by a function-body, not an
21416 enum-specifier. */
21417 if (is_trailing_return)
21418 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21419
21420 /* Parse the type-specifiers and attributes. */
21421 while (true)
21422 {
21423 tree type_specifier;
21424 bool is_cv_qualifier;
21425
21426 /* Check for attributes first. */
21427 if (cp_next_tokens_can_be_attribute_p (parser))
21428 {
21429 type_specifier_seq->attributes
21430 = attr_chainon (type_specifier_seq->attributes,
21431 cp_parser_attributes_opt (parser));
21432 continue;
21433 }
21434
21435 /* record the token of the beginning of the type specifier seq,
21436 for error reporting purposes*/
21437 if (!start_token)
21438 start_token = cp_lexer_peek_token (parser->lexer);
21439
21440 /* Look for the type-specifier. */
21441 type_specifier = cp_parser_type_specifier (parser,
21442 flags,
21443 type_specifier_seq,
21444 /*is_declaration=*/false,
21445 NULL,
21446 &is_cv_qualifier);
21447 if (!type_specifier)
21448 {
21449 /* If the first type-specifier could not be found, this is not a
21450 type-specifier-seq at all. */
21451 if (!seen_type_specifier)
21452 {
21453 /* Set in_declarator_p to avoid skipping to the semicolon. */
21454 int in_decl = parser->in_declarator_p;
21455 parser->in_declarator_p = true;
21456
21457 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21458 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21459 cp_parser_error (parser, "expected type-specifier");
21460
21461 parser->in_declarator_p = in_decl;
21462
21463 type_specifier_seq->type = error_mark_node;
21464 return;
21465 }
21466 /* If subsequent type-specifiers could not be found, the
21467 type-specifier-seq is complete. */
21468 break;
21469 }
21470
21471 seen_type_specifier = true;
21472 /* The standard says that a condition can be:
21473
21474 type-specifier-seq declarator = assignment-expression
21475
21476 However, given:
21477
21478 struct S {};
21479 if (int S = ...)
21480
21481 we should treat the "S" as a declarator, not as a
21482 type-specifier. The standard doesn't say that explicitly for
21483 type-specifier-seq, but it does say that for
21484 decl-specifier-seq in an ordinary declaration. Perhaps it
21485 would be clearer just to allow a decl-specifier-seq here, and
21486 then add a semantic restriction that if any decl-specifiers
21487 that are not type-specifiers appear, the program is invalid. */
21488 if (is_declaration && !is_cv_qualifier)
21489 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21490 }
21491 }
21492
21493 /* Return whether the function currently being declared has an associated
21494 template parameter list. */
21495
21496 static bool
21497 function_being_declared_is_template_p (cp_parser* parser)
21498 {
21499 if (!current_template_parms || processing_template_parmlist)
21500 return false;
21501
21502 if (parser->implicit_template_scope)
21503 return true;
21504
21505 if (at_class_scope_p ()
21506 && TYPE_BEING_DEFINED (current_class_type))
21507 return parser->num_template_parameter_lists != 0;
21508
21509 return ((int) parser->num_template_parameter_lists > template_class_depth
21510 (current_class_type));
21511 }
21512
21513 /* Parse a parameter-declaration-clause.
21514
21515 parameter-declaration-clause:
21516 parameter-declaration-list [opt] ... [opt]
21517 parameter-declaration-list , ...
21518
21519 Returns a representation for the parameter declarations. A return
21520 value of NULL indicates a parameter-declaration-clause consisting
21521 only of an ellipsis. */
21522
21523 static tree
21524 cp_parser_parameter_declaration_clause (cp_parser* parser)
21525 {
21526 tree parameters;
21527 cp_token *token;
21528 bool ellipsis_p;
21529
21530 temp_override<bool> cleanup
21531 (parser->auto_is_implicit_function_template_parm_p);
21532
21533 if (!processing_specialization
21534 && !processing_template_parmlist
21535 && !processing_explicit_instantiation
21536 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21537 actual function or a random abstract declarator. */
21538 && parser->default_arg_ok_p)
21539 if (!current_function_decl
21540 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21541 parser->auto_is_implicit_function_template_parm_p = true;
21542
21543 /* Peek at the next token. */
21544 token = cp_lexer_peek_token (parser->lexer);
21545 /* Check for trivial parameter-declaration-clauses. */
21546 if (token->type == CPP_ELLIPSIS)
21547 {
21548 /* Consume the `...' token. */
21549 cp_lexer_consume_token (parser->lexer);
21550 return NULL_TREE;
21551 }
21552 else if (token->type == CPP_CLOSE_PAREN)
21553 /* There are no parameters. */
21554 return void_list_node;
21555 /* Check for `(void)', too, which is a special case. */
21556 else if (token->keyword == RID_VOID
21557 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21558 == CPP_CLOSE_PAREN))
21559 {
21560 /* Consume the `void' token. */
21561 cp_lexer_consume_token (parser->lexer);
21562 /* There are no parameters. */
21563 return void_list_node;
21564 }
21565
21566 /* Parse the parameter-declaration-list. */
21567 parameters = cp_parser_parameter_declaration_list (parser);
21568 /* If a parse error occurred while parsing the
21569 parameter-declaration-list, then the entire
21570 parameter-declaration-clause is erroneous. */
21571 if (parameters == error_mark_node)
21572 return NULL_TREE;
21573
21574 /* Peek at the next token. */
21575 token = cp_lexer_peek_token (parser->lexer);
21576 /* If it's a `,', the clause should terminate with an ellipsis. */
21577 if (token->type == CPP_COMMA)
21578 {
21579 /* Consume the `,'. */
21580 cp_lexer_consume_token (parser->lexer);
21581 /* Expect an ellipsis. */
21582 ellipsis_p
21583 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21584 }
21585 /* It might also be `...' if the optional trailing `,' was
21586 omitted. */
21587 else if (token->type == CPP_ELLIPSIS)
21588 {
21589 /* Consume the `...' token. */
21590 cp_lexer_consume_token (parser->lexer);
21591 /* And remember that we saw it. */
21592 ellipsis_p = true;
21593 }
21594 else
21595 ellipsis_p = false;
21596
21597 /* Finish the parameter list. */
21598 if (!ellipsis_p)
21599 parameters = chainon (parameters, void_list_node);
21600
21601 return parameters;
21602 }
21603
21604 /* Parse a parameter-declaration-list.
21605
21606 parameter-declaration-list:
21607 parameter-declaration
21608 parameter-declaration-list , parameter-declaration
21609
21610 Returns a representation of the parameter-declaration-list, as for
21611 cp_parser_parameter_declaration_clause. However, the
21612 `void_list_node' is never appended to the list. */
21613
21614 static tree
21615 cp_parser_parameter_declaration_list (cp_parser* parser)
21616 {
21617 tree parameters = NULL_TREE;
21618 tree *tail = &parameters;
21619 bool saved_in_unbraced_linkage_specification_p;
21620 int index = 0;
21621
21622 /* The special considerations that apply to a function within an
21623 unbraced linkage specifications do not apply to the parameters
21624 to the function. */
21625 saved_in_unbraced_linkage_specification_p
21626 = parser->in_unbraced_linkage_specification_p;
21627 parser->in_unbraced_linkage_specification_p = false;
21628
21629 /* Look for more parameters. */
21630 while (true)
21631 {
21632 cp_parameter_declarator *parameter;
21633 tree decl = error_mark_node;
21634 bool parenthesized_p = false;
21635
21636 /* Parse the parameter. */
21637 parameter
21638 = cp_parser_parameter_declaration (parser,
21639 /*template_parm_p=*/false,
21640 &parenthesized_p);
21641
21642 /* We don't know yet if the enclosing context is deprecated, so wait
21643 and warn in grokparms if appropriate. */
21644 deprecated_state = DEPRECATED_SUPPRESS;
21645
21646 if (parameter)
21647 {
21648 decl = grokdeclarator (parameter->declarator,
21649 &parameter->decl_specifiers,
21650 PARM,
21651 parameter->default_argument != NULL_TREE,
21652 &parameter->decl_specifiers.attributes);
21653 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21654 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21655 }
21656
21657 deprecated_state = DEPRECATED_NORMAL;
21658
21659 /* If a parse error occurred parsing the parameter declaration,
21660 then the entire parameter-declaration-list is erroneous. */
21661 if (decl == error_mark_node)
21662 {
21663 parameters = error_mark_node;
21664 break;
21665 }
21666
21667 if (parameter->decl_specifiers.attributes)
21668 cplus_decl_attributes (&decl,
21669 parameter->decl_specifiers.attributes,
21670 0);
21671 if (DECL_NAME (decl))
21672 decl = pushdecl (decl);
21673
21674 if (decl != error_mark_node)
21675 {
21676 retrofit_lang_decl (decl);
21677 DECL_PARM_INDEX (decl) = ++index;
21678 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21679 }
21680
21681 /* Add the new parameter to the list. */
21682 *tail = build_tree_list (parameter->default_argument, decl);
21683 tail = &TREE_CHAIN (*tail);
21684
21685 /* Peek at the next token. */
21686 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21687 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21688 /* These are for Objective-C++ */
21689 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21690 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21691 /* The parameter-declaration-list is complete. */
21692 break;
21693 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21694 {
21695 cp_token *token;
21696
21697 /* Peek at the next token. */
21698 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21699 /* If it's an ellipsis, then the list is complete. */
21700 if (token->type == CPP_ELLIPSIS)
21701 break;
21702 /* Otherwise, there must be more parameters. Consume the
21703 `,'. */
21704 cp_lexer_consume_token (parser->lexer);
21705 /* When parsing something like:
21706
21707 int i(float f, double d)
21708
21709 we can tell after seeing the declaration for "f" that we
21710 are not looking at an initialization of a variable "i",
21711 but rather at the declaration of a function "i".
21712
21713 Due to the fact that the parsing of template arguments
21714 (as specified to a template-id) requires backtracking we
21715 cannot use this technique when inside a template argument
21716 list. */
21717 if (!parser->in_template_argument_list_p
21718 && !parser->in_type_id_in_expr_p
21719 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21720 /* However, a parameter-declaration of the form
21721 "float(f)" (which is a valid declaration of a
21722 parameter "f") can also be interpreted as an
21723 expression (the conversion of "f" to "float"). */
21724 && !parenthesized_p)
21725 cp_parser_commit_to_tentative_parse (parser);
21726 }
21727 else
21728 {
21729 cp_parser_error (parser, "expected %<,%> or %<...%>");
21730 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21731 cp_parser_skip_to_closing_parenthesis (parser,
21732 /*recovering=*/true,
21733 /*or_comma=*/false,
21734 /*consume_paren=*/false);
21735 break;
21736 }
21737 }
21738
21739 parser->in_unbraced_linkage_specification_p
21740 = saved_in_unbraced_linkage_specification_p;
21741
21742 /* Reset implicit_template_scope if we are about to leave the function
21743 parameter list that introduced it. Note that for out-of-line member
21744 definitions, there will be one or more class scopes before we get to
21745 the template parameter scope. */
21746
21747 if (cp_binding_level *its = parser->implicit_template_scope)
21748 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21749 {
21750 while (maybe_its->kind == sk_class)
21751 maybe_its = maybe_its->level_chain;
21752 if (maybe_its == its)
21753 {
21754 parser->implicit_template_parms = 0;
21755 parser->implicit_template_scope = 0;
21756 }
21757 }
21758
21759 return parameters;
21760 }
21761
21762 /* Parse a parameter declaration.
21763
21764 parameter-declaration:
21765 decl-specifier-seq ... [opt] declarator
21766 decl-specifier-seq declarator = assignment-expression
21767 decl-specifier-seq ... [opt] abstract-declarator [opt]
21768 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21769
21770 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21771 declares a template parameter. (In that case, a non-nested `>'
21772 token encountered during the parsing of the assignment-expression
21773 is not interpreted as a greater-than operator.)
21774
21775 Returns a representation of the parameter, or NULL if an error
21776 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21777 true iff the declarator is of the form "(p)". */
21778
21779 static cp_parameter_declarator *
21780 cp_parser_parameter_declaration (cp_parser *parser,
21781 bool template_parm_p,
21782 bool *parenthesized_p)
21783 {
21784 int declares_class_or_enum;
21785 cp_decl_specifier_seq decl_specifiers;
21786 cp_declarator *declarator;
21787 tree default_argument;
21788 cp_token *token = NULL, *declarator_token_start = NULL;
21789 const char *saved_message;
21790 bool template_parameter_pack_p = false;
21791
21792 /* In a template parameter, `>' is not an operator.
21793
21794 [temp.param]
21795
21796 When parsing a default template-argument for a non-type
21797 template-parameter, the first non-nested `>' is taken as the end
21798 of the template parameter-list rather than a greater-than
21799 operator. */
21800
21801 /* Type definitions may not appear in parameter types. */
21802 saved_message = parser->type_definition_forbidden_message;
21803 parser->type_definition_forbidden_message
21804 = G_("types may not be defined in parameter types");
21805
21806 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21807 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21808 (current_template_parms)) : 0);
21809
21810 /* Parse the declaration-specifiers. */
21811 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21812 cp_parser_decl_specifier_seq (parser,
21813 CP_PARSER_FLAGS_NONE,
21814 &decl_specifiers,
21815 &declares_class_or_enum);
21816
21817 /* Complain about missing 'typename' or other invalid type names. */
21818 if (!decl_specifiers.any_type_specifiers_p
21819 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21820 decl_specifiers.type = error_mark_node;
21821
21822 /* If an error occurred, there's no reason to attempt to parse the
21823 rest of the declaration. */
21824 if (cp_parser_error_occurred (parser))
21825 {
21826 parser->type_definition_forbidden_message = saved_message;
21827 return NULL;
21828 }
21829
21830 /* Peek at the next token. */
21831 token = cp_lexer_peek_token (parser->lexer);
21832
21833 /* If the next token is a `)', `,', `=', `>', or `...', then there
21834 is no declarator. However, when variadic templates are enabled,
21835 there may be a declarator following `...'. */
21836 if (token->type == CPP_CLOSE_PAREN
21837 || token->type == CPP_COMMA
21838 || token->type == CPP_EQ
21839 || token->type == CPP_GREATER)
21840 {
21841 declarator = NULL;
21842 if (parenthesized_p)
21843 *parenthesized_p = false;
21844 }
21845 /* Otherwise, there should be a declarator. */
21846 else
21847 {
21848 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21849 parser->default_arg_ok_p = false;
21850
21851 /* After seeing a decl-specifier-seq, if the next token is not a
21852 "(", there is no possibility that the code is a valid
21853 expression. Therefore, if parsing tentatively, we commit at
21854 this point. */
21855 if (!parser->in_template_argument_list_p
21856 /* In an expression context, having seen:
21857
21858 (int((char ...
21859
21860 we cannot be sure whether we are looking at a
21861 function-type (taking a "char" as a parameter) or a cast
21862 of some object of type "char" to "int". */
21863 && !parser->in_type_id_in_expr_p
21864 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21865 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21866 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21867 cp_parser_commit_to_tentative_parse (parser);
21868 /* Parse the declarator. */
21869 declarator_token_start = token;
21870 declarator = cp_parser_declarator (parser,
21871 CP_PARSER_DECLARATOR_EITHER,
21872 /*ctor_dtor_or_conv_p=*/NULL,
21873 parenthesized_p,
21874 /*member_p=*/false,
21875 /*friend_p=*/false);
21876 parser->default_arg_ok_p = saved_default_arg_ok_p;
21877 /* After the declarator, allow more attributes. */
21878 decl_specifiers.attributes
21879 = attr_chainon (decl_specifiers.attributes,
21880 cp_parser_attributes_opt (parser));
21881
21882 /* If the declarator is a template parameter pack, remember that and
21883 clear the flag in the declarator itself so we don't get errors
21884 from grokdeclarator. */
21885 if (template_parm_p && declarator && declarator->parameter_pack_p)
21886 {
21887 declarator->parameter_pack_p = false;
21888 template_parameter_pack_p = true;
21889 }
21890 }
21891
21892 /* If the next token is an ellipsis, and we have not seen a declarator
21893 name, and if either the type of the declarator contains parameter
21894 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21895 for, eg, abbreviated integral type names), then we actually have a
21896 parameter pack expansion expression. Otherwise, leave the ellipsis
21897 for a C-style variadic function. */
21898 token = cp_lexer_peek_token (parser->lexer);
21899
21900 /* If a function parameter pack was specified and an implicit template
21901 parameter was introduced during cp_parser_parameter_declaration,
21902 change any implicit parameters introduced into packs. */
21903 if (parser->implicit_template_parms
21904 && ((token->type == CPP_ELLIPSIS
21905 && declarator_can_be_parameter_pack (declarator))
21906 || (declarator && declarator->parameter_pack_p)))
21907 {
21908 int latest_template_parm_idx = TREE_VEC_LENGTH
21909 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21910
21911 if (latest_template_parm_idx != template_parm_idx)
21912 decl_specifiers.type = convert_generic_types_to_packs
21913 (decl_specifiers.type,
21914 template_parm_idx, latest_template_parm_idx);
21915 }
21916
21917 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21918 {
21919 tree type = decl_specifiers.type;
21920
21921 if (type && DECL_P (type))
21922 type = TREE_TYPE (type);
21923
21924 if (((type
21925 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21926 && (template_parm_p || uses_parameter_packs (type)))
21927 || (!type && template_parm_p))
21928 && declarator_can_be_parameter_pack (declarator))
21929 {
21930 /* Consume the `...'. */
21931 cp_lexer_consume_token (parser->lexer);
21932 maybe_warn_variadic_templates ();
21933
21934 /* Build a pack expansion type */
21935 if (template_parm_p)
21936 template_parameter_pack_p = true;
21937 else if (declarator)
21938 declarator->parameter_pack_p = true;
21939 else
21940 decl_specifiers.type = make_pack_expansion (type);
21941 }
21942 }
21943
21944 /* The restriction on defining new types applies only to the type
21945 of the parameter, not to the default argument. */
21946 parser->type_definition_forbidden_message = saved_message;
21947
21948 /* If the next token is `=', then process a default argument. */
21949 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21950 {
21951 tree type = decl_specifiers.type;
21952 token = cp_lexer_peek_token (parser->lexer);
21953 /* If we are defining a class, then the tokens that make up the
21954 default argument must be saved and processed later. */
21955 if (!template_parm_p && at_class_scope_p ()
21956 && TYPE_BEING_DEFINED (current_class_type)
21957 && !LAMBDA_TYPE_P (current_class_type))
21958 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21959
21960 // A constrained-type-specifier may declare a type template-parameter.
21961 else if (declares_constrained_type_template_parameter (type))
21962 default_argument
21963 = cp_parser_default_type_template_argument (parser);
21964
21965 // A constrained-type-specifier may declare a template-template-parameter.
21966 else if (declares_constrained_template_template_parameter (type))
21967 default_argument
21968 = cp_parser_default_template_template_argument (parser);
21969
21970 /* Outside of a class definition, we can just parse the
21971 assignment-expression. */
21972 else
21973 default_argument
21974 = cp_parser_default_argument (parser, template_parm_p);
21975
21976 if (!parser->default_arg_ok_p)
21977 {
21978 permerror (token->location,
21979 "default arguments are only "
21980 "permitted for function parameters");
21981 }
21982 else if ((declarator && declarator->parameter_pack_p)
21983 || template_parameter_pack_p
21984 || (decl_specifiers.type
21985 && PACK_EXPANSION_P (decl_specifiers.type)))
21986 {
21987 /* Find the name of the parameter pack. */
21988 cp_declarator *id_declarator = declarator;
21989 while (id_declarator && id_declarator->kind != cdk_id)
21990 id_declarator = id_declarator->declarator;
21991
21992 if (id_declarator && id_declarator->kind == cdk_id)
21993 error_at (declarator_token_start->location,
21994 template_parm_p
21995 ? G_("template parameter pack %qD "
21996 "cannot have a default argument")
21997 : G_("parameter pack %qD cannot have "
21998 "a default argument"),
21999 id_declarator->u.id.unqualified_name);
22000 else
22001 error_at (declarator_token_start->location,
22002 template_parm_p
22003 ? G_("template parameter pack cannot have "
22004 "a default argument")
22005 : G_("parameter pack cannot have a "
22006 "default argument"));
22007
22008 default_argument = NULL_TREE;
22009 }
22010 }
22011 else
22012 default_argument = NULL_TREE;
22013
22014 /* Generate a location for the parameter, ranging from the start of the
22015 initial token to the end of the final token (using input_location for
22016 the latter, set up by cp_lexer_set_source_position_from_token when
22017 consuming tokens).
22018
22019 If we have a identifier, then use it for the caret location, e.g.
22020
22021 extern int callee (int one, int (*two)(int, int), float three);
22022 ~~~~~~^~~~~~~~~~~~~~
22023
22024 otherwise, reuse the start location for the caret location e.g.:
22025
22026 extern int callee (int one, int (*)(int, int), float three);
22027 ^~~~~~~~~~~~~~~~~
22028
22029 */
22030 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22031 ? declarator->id_loc
22032 : decl_spec_token_start->location);
22033 location_t param_loc = make_location (caret_loc,
22034 decl_spec_token_start->location,
22035 input_location);
22036
22037 return make_parameter_declarator (&decl_specifiers,
22038 declarator,
22039 default_argument,
22040 param_loc,
22041 template_parameter_pack_p);
22042 }
22043
22044 /* Parse a default argument and return it.
22045
22046 TEMPLATE_PARM_P is true if this is a default argument for a
22047 non-type template parameter. */
22048 static tree
22049 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22050 {
22051 tree default_argument = NULL_TREE;
22052 bool saved_greater_than_is_operator_p;
22053 bool saved_local_variables_forbidden_p;
22054 bool non_constant_p, is_direct_init;
22055
22056 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22057 set correctly. */
22058 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22059 parser->greater_than_is_operator_p = !template_parm_p;
22060 /* Local variable names (and the `this' keyword) may not
22061 appear in a default argument. */
22062 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22063 parser->local_variables_forbidden_p = true;
22064 /* Parse the assignment-expression. */
22065 if (template_parm_p)
22066 push_deferring_access_checks (dk_no_deferred);
22067 tree saved_class_ptr = NULL_TREE;
22068 tree saved_class_ref = NULL_TREE;
22069 /* The "this" pointer is not valid in a default argument. */
22070 if (cfun)
22071 {
22072 saved_class_ptr = current_class_ptr;
22073 cp_function_chain->x_current_class_ptr = NULL_TREE;
22074 saved_class_ref = current_class_ref;
22075 cp_function_chain->x_current_class_ref = NULL_TREE;
22076 }
22077 default_argument
22078 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22079 /* Restore the "this" pointer. */
22080 if (cfun)
22081 {
22082 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22083 cp_function_chain->x_current_class_ref = saved_class_ref;
22084 }
22085 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22086 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22087 if (template_parm_p)
22088 pop_deferring_access_checks ();
22089 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22090 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22091
22092 return default_argument;
22093 }
22094
22095 /* Parse a function-body.
22096
22097 function-body:
22098 compound_statement */
22099
22100 static void
22101 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22102 {
22103 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22104 ? BCS_TRY_BLOCK : BCS_NORMAL),
22105 true);
22106 }
22107
22108 /* Parse a ctor-initializer-opt followed by a function-body. Return
22109 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22110 is true we are parsing a function-try-block. */
22111
22112 static void
22113 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22114 bool in_function_try_block)
22115 {
22116 tree body, list;
22117 const bool check_body_p =
22118 DECL_CONSTRUCTOR_P (current_function_decl)
22119 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22120 tree last = NULL;
22121
22122 /* Begin the function body. */
22123 body = begin_function_body ();
22124 /* Parse the optional ctor-initializer. */
22125 cp_parser_ctor_initializer_opt (parser);
22126
22127 /* If we're parsing a constexpr constructor definition, we need
22128 to check that the constructor body is indeed empty. However,
22129 before we get to cp_parser_function_body lot of junk has been
22130 generated, so we can't just check that we have an empty block.
22131 Rather we take a snapshot of the outermost block, and check whether
22132 cp_parser_function_body changed its state. */
22133 if (check_body_p)
22134 {
22135 list = cur_stmt_list;
22136 if (STATEMENT_LIST_TAIL (list))
22137 last = STATEMENT_LIST_TAIL (list)->stmt;
22138 }
22139 /* Parse the function-body. */
22140 cp_parser_function_body (parser, in_function_try_block);
22141 if (check_body_p)
22142 check_constexpr_ctor_body (last, list, /*complain=*/true);
22143 /* Finish the function body. */
22144 finish_function_body (body);
22145 }
22146
22147 /* Parse an initializer.
22148
22149 initializer:
22150 = initializer-clause
22151 ( expression-list )
22152
22153 Returns an expression representing the initializer. If no
22154 initializer is present, NULL_TREE is returned.
22155
22156 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22157 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22158 set to TRUE if there is no initializer present. If there is an
22159 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22160 is set to true; otherwise it is set to false. */
22161
22162 static tree
22163 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22164 bool* non_constant_p, bool subexpression_p)
22165 {
22166 cp_token *token;
22167 tree init;
22168
22169 /* Peek at the next token. */
22170 token = cp_lexer_peek_token (parser->lexer);
22171
22172 /* Let our caller know whether or not this initializer was
22173 parenthesized. */
22174 *is_direct_init = (token->type != CPP_EQ);
22175 /* Assume that the initializer is constant. */
22176 *non_constant_p = false;
22177
22178 if (token->type == CPP_EQ)
22179 {
22180 /* Consume the `='. */
22181 cp_lexer_consume_token (parser->lexer);
22182 /* Parse the initializer-clause. */
22183 init = cp_parser_initializer_clause (parser, non_constant_p);
22184 }
22185 else if (token->type == CPP_OPEN_PAREN)
22186 {
22187 vec<tree, va_gc> *vec;
22188 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22189 /*cast_p=*/false,
22190 /*allow_expansion_p=*/true,
22191 non_constant_p);
22192 if (vec == NULL)
22193 return error_mark_node;
22194 init = build_tree_list_vec (vec);
22195 release_tree_vector (vec);
22196 }
22197 else if (token->type == CPP_OPEN_BRACE)
22198 {
22199 cp_lexer_set_source_position (parser->lexer);
22200 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22201 init = cp_parser_braced_list (parser, non_constant_p);
22202 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22203 }
22204 else
22205 {
22206 /* Anything else is an error. */
22207 cp_parser_error (parser, "expected initializer");
22208 init = error_mark_node;
22209 }
22210
22211 if (!subexpression_p && check_for_bare_parameter_packs (init))
22212 init = error_mark_node;
22213
22214 return init;
22215 }
22216
22217 /* Parse an initializer-clause.
22218
22219 initializer-clause:
22220 assignment-expression
22221 braced-init-list
22222
22223 Returns an expression representing the initializer.
22224
22225 If the `assignment-expression' production is used the value
22226 returned is simply a representation for the expression.
22227
22228 Otherwise, calls cp_parser_braced_list. */
22229
22230 static cp_expr
22231 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22232 {
22233 cp_expr initializer;
22234
22235 /* Assume the expression is constant. */
22236 *non_constant_p = false;
22237
22238 /* If it is not a `{', then we are looking at an
22239 assignment-expression. */
22240 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22241 {
22242 initializer
22243 = cp_parser_constant_expression (parser,
22244 /*allow_non_constant_p=*/true,
22245 non_constant_p);
22246 }
22247 else
22248 initializer = cp_parser_braced_list (parser, non_constant_p);
22249
22250 return initializer;
22251 }
22252
22253 /* Parse a brace-enclosed initializer list.
22254
22255 braced-init-list:
22256 { initializer-list , [opt] }
22257 { designated-initializer-list , [opt] }
22258 { }
22259
22260 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22261 the elements of the initializer-list (or NULL, if the last
22262 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22263 NULL_TREE. There is no way to detect whether or not the optional
22264 trailing `,' was provided. NON_CONSTANT_P is as for
22265 cp_parser_initializer. */
22266
22267 static cp_expr
22268 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22269 {
22270 tree initializer;
22271 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22272
22273 /* Consume the `{' token. */
22274 matching_braces braces;
22275 braces.require_open (parser);
22276 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22277 initializer = make_node (CONSTRUCTOR);
22278 /* If it's not a `}', then there is a non-trivial initializer. */
22279 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22280 {
22281 /* Parse the initializer list. */
22282 CONSTRUCTOR_ELTS (initializer)
22283 = cp_parser_initializer_list (parser, non_constant_p);
22284 /* A trailing `,' token is allowed. */
22285 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22286 cp_lexer_consume_token (parser->lexer);
22287 }
22288 else
22289 *non_constant_p = false;
22290 /* Now, there should be a trailing `}'. */
22291 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22292 braces.require_close (parser);
22293 TREE_TYPE (initializer) = init_list_type_node;
22294
22295 cp_expr result (initializer);
22296 /* Build a location of the form:
22297 { ... }
22298 ^~~~~~~
22299 with caret==start at the open brace, finish at the close brace. */
22300 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22301 result.set_location (combined_loc);
22302 return result;
22303 }
22304
22305 /* Consume tokens up to, and including, the next non-nested closing `]'.
22306 Returns true iff we found a closing `]'. */
22307
22308 static bool
22309 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22310 {
22311 unsigned square_depth = 0;
22312
22313 while (true)
22314 {
22315 cp_token * token = cp_lexer_peek_token (parser->lexer);
22316
22317 switch (token->type)
22318 {
22319 case CPP_EOF:
22320 case CPP_PRAGMA_EOL:
22321 /* If we've run out of tokens, then there is no closing `]'. */
22322 return false;
22323
22324 case CPP_OPEN_SQUARE:
22325 ++square_depth;
22326 break;
22327
22328 case CPP_CLOSE_SQUARE:
22329 if (!square_depth--)
22330 {
22331 cp_lexer_consume_token (parser->lexer);
22332 return true;
22333 }
22334 break;
22335
22336 default:
22337 break;
22338 }
22339
22340 /* Consume the token. */
22341 cp_lexer_consume_token (parser->lexer);
22342 }
22343 }
22344
22345 /* Return true if we are looking at an array-designator, false otherwise. */
22346
22347 static bool
22348 cp_parser_array_designator_p (cp_parser *parser)
22349 {
22350 /* Consume the `['. */
22351 cp_lexer_consume_token (parser->lexer);
22352
22353 cp_lexer_save_tokens (parser->lexer);
22354
22355 /* Skip tokens until the next token is a closing square bracket.
22356 If we find the closing `]', and the next token is a `=', then
22357 we are looking at an array designator. */
22358 bool array_designator_p
22359 = (cp_parser_skip_to_closing_square_bracket (parser)
22360 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22361
22362 /* Roll back the tokens we skipped. */
22363 cp_lexer_rollback_tokens (parser->lexer);
22364
22365 return array_designator_p;
22366 }
22367
22368 /* Parse an initializer-list.
22369
22370 initializer-list:
22371 initializer-clause ... [opt]
22372 initializer-list , initializer-clause ... [opt]
22373
22374 C++2A Extension:
22375
22376 designated-initializer-list:
22377 designated-initializer-clause
22378 designated-initializer-list , designated-initializer-clause
22379
22380 designated-initializer-clause:
22381 designator brace-or-equal-initializer
22382
22383 designator:
22384 . identifier
22385
22386 GNU Extension:
22387
22388 initializer-list:
22389 designation initializer-clause ...[opt]
22390 initializer-list , designation initializer-clause ...[opt]
22391
22392 designation:
22393 . identifier =
22394 identifier :
22395 [ constant-expression ] =
22396
22397 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22398 for the initializer. If the INDEX of the elt is non-NULL, it is the
22399 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22400 as for cp_parser_initializer. */
22401
22402 static vec<constructor_elt, va_gc> *
22403 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22404 {
22405 vec<constructor_elt, va_gc> *v = NULL;
22406 bool first_p = true;
22407 tree first_designator = NULL_TREE;
22408
22409 /* Assume all of the expressions are constant. */
22410 *non_constant_p = false;
22411
22412 /* Parse the rest of the list. */
22413 while (true)
22414 {
22415 cp_token *token;
22416 tree designator;
22417 tree initializer;
22418 bool clause_non_constant_p;
22419 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22420
22421 /* Handle the C++2A syntax, '. id ='. */
22422 if ((cxx_dialect >= cxx2a
22423 || cp_parser_allow_gnu_extensions_p (parser))
22424 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22425 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22426 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22427 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22428 == CPP_OPEN_BRACE)))
22429 {
22430 if (cxx_dialect < cxx2a)
22431 pedwarn (loc, OPT_Wpedantic,
22432 "C++ designated initializers only available with "
22433 "-std=c++2a or -std=gnu++2a");
22434 /* Consume the `.'. */
22435 cp_lexer_consume_token (parser->lexer);
22436 /* Consume the identifier. */
22437 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22438 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22439 /* Consume the `='. */
22440 cp_lexer_consume_token (parser->lexer);
22441 }
22442 /* Also, if the next token is an identifier and the following one is a
22443 colon, we are looking at the GNU designated-initializer
22444 syntax. */
22445 else if (cp_parser_allow_gnu_extensions_p (parser)
22446 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22447 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22448 == CPP_COLON))
22449 {
22450 /* Warn the user that they are using an extension. */
22451 pedwarn (loc, OPT_Wpedantic,
22452 "ISO C++ does not allow GNU designated initializers");
22453 /* Consume the identifier. */
22454 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22455 /* Consume the `:'. */
22456 cp_lexer_consume_token (parser->lexer);
22457 }
22458 /* Also handle C99 array designators, '[ const ] ='. */
22459 else if (cp_parser_allow_gnu_extensions_p (parser)
22460 && !c_dialect_objc ()
22461 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22462 {
22463 /* In C++11, [ could start a lambda-introducer. */
22464 bool non_const = false;
22465
22466 cp_parser_parse_tentatively (parser);
22467
22468 if (!cp_parser_array_designator_p (parser))
22469 {
22470 cp_parser_simulate_error (parser);
22471 designator = NULL_TREE;
22472 }
22473 else
22474 {
22475 designator = cp_parser_constant_expression (parser, true,
22476 &non_const);
22477 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22478 cp_parser_require (parser, CPP_EQ, RT_EQ);
22479 }
22480
22481 if (!cp_parser_parse_definitely (parser))
22482 designator = NULL_TREE;
22483 else if (non_const
22484 && (!require_potential_rvalue_constant_expression
22485 (designator)))
22486 designator = NULL_TREE;
22487 if (designator)
22488 /* Warn the user that they are using an extension. */
22489 pedwarn (loc, OPT_Wpedantic,
22490 "ISO C++ does not allow C99 designated initializers");
22491 }
22492 else
22493 designator = NULL_TREE;
22494
22495 if (first_p)
22496 {
22497 first_designator = designator;
22498 first_p = false;
22499 }
22500 else if (cxx_dialect >= cxx2a
22501 && first_designator != error_mark_node
22502 && (!first_designator != !designator))
22503 {
22504 error_at (loc, "either all initializer clauses should be designated "
22505 "or none of them should be");
22506 first_designator = error_mark_node;
22507 }
22508 else if (cxx_dialect < cxx2a && !first_designator)
22509 first_designator = designator;
22510
22511 /* Parse the initializer. */
22512 initializer = cp_parser_initializer_clause (parser,
22513 &clause_non_constant_p);
22514 /* If any clause is non-constant, so is the entire initializer. */
22515 if (clause_non_constant_p)
22516 *non_constant_p = true;
22517
22518 /* If we have an ellipsis, this is an initializer pack
22519 expansion. */
22520 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22521 {
22522 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22523
22524 /* Consume the `...'. */
22525 cp_lexer_consume_token (parser->lexer);
22526
22527 if (designator && cxx_dialect >= cxx2a)
22528 error_at (loc,
22529 "%<...%> not allowed in designated initializer list");
22530
22531 /* Turn the initializer into an initializer expansion. */
22532 initializer = make_pack_expansion (initializer);
22533 }
22534
22535 /* Add it to the vector. */
22536 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22537
22538 /* If the next token is not a comma, we have reached the end of
22539 the list. */
22540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22541 break;
22542
22543 /* Peek at the next token. */
22544 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22545 /* If the next token is a `}', then we're still done. An
22546 initializer-clause can have a trailing `,' after the
22547 initializer-list and before the closing `}'. */
22548 if (token->type == CPP_CLOSE_BRACE)
22549 break;
22550
22551 /* Consume the `,' token. */
22552 cp_lexer_consume_token (parser->lexer);
22553 }
22554
22555 /* The same identifier shall not appear in multiple designators
22556 of a designated-initializer-list. */
22557 if (first_designator)
22558 {
22559 unsigned int i;
22560 tree designator, val;
22561 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22562 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22563 {
22564 if (IDENTIFIER_MARKED (designator))
22565 {
22566 error_at (cp_expr_loc_or_loc (val, input_location),
22567 "%<.%s%> designator used multiple times in "
22568 "the same initializer list",
22569 IDENTIFIER_POINTER (designator));
22570 (*v)[i].index = error_mark_node;
22571 }
22572 else
22573 IDENTIFIER_MARKED (designator) = 1;
22574 }
22575 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22576 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22577 IDENTIFIER_MARKED (designator) = 0;
22578 }
22579
22580 return v;
22581 }
22582
22583 /* Classes [gram.class] */
22584
22585 /* Parse a class-name.
22586
22587 class-name:
22588 identifier
22589 template-id
22590
22591 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22592 to indicate that names looked up in dependent types should be
22593 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22594 keyword has been used to indicate that the name that appears next
22595 is a template. TAG_TYPE indicates the explicit tag given before
22596 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22597 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22598 is the class being defined in a class-head. If ENUM_OK is TRUE,
22599 enum-names are also accepted.
22600
22601 Returns the TYPE_DECL representing the class. */
22602
22603 static tree
22604 cp_parser_class_name (cp_parser *parser,
22605 bool typename_keyword_p,
22606 bool template_keyword_p,
22607 enum tag_types tag_type,
22608 bool check_dependency_p,
22609 bool class_head_p,
22610 bool is_declaration,
22611 bool enum_ok)
22612 {
22613 tree decl;
22614 tree scope;
22615 bool typename_p;
22616 cp_token *token;
22617 tree identifier = NULL_TREE;
22618
22619 /* All class-names start with an identifier. */
22620 token = cp_lexer_peek_token (parser->lexer);
22621 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22622 {
22623 cp_parser_error (parser, "expected class-name");
22624 return error_mark_node;
22625 }
22626
22627 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22628 to a template-id, so we save it here. */
22629 scope = parser->scope;
22630 if (scope == error_mark_node)
22631 return error_mark_node;
22632
22633 /* Any name names a type if we're following the `typename' keyword
22634 in a qualified name where the enclosing scope is type-dependent. */
22635 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22636 && dependent_type_p (scope));
22637 /* Handle the common case (an identifier, but not a template-id)
22638 efficiently. */
22639 if (token->type == CPP_NAME
22640 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22641 {
22642 cp_token *identifier_token;
22643 bool ambiguous_p;
22644
22645 /* Look for the identifier. */
22646 identifier_token = cp_lexer_peek_token (parser->lexer);
22647 ambiguous_p = identifier_token->error_reported;
22648 identifier = cp_parser_identifier (parser);
22649 /* If the next token isn't an identifier, we are certainly not
22650 looking at a class-name. */
22651 if (identifier == error_mark_node)
22652 decl = error_mark_node;
22653 /* If we know this is a type-name, there's no need to look it
22654 up. */
22655 else if (typename_p)
22656 decl = identifier;
22657 else
22658 {
22659 tree ambiguous_decls;
22660 /* If we already know that this lookup is ambiguous, then
22661 we've already issued an error message; there's no reason
22662 to check again. */
22663 if (ambiguous_p)
22664 {
22665 cp_parser_simulate_error (parser);
22666 return error_mark_node;
22667 }
22668 /* If the next token is a `::', then the name must be a type
22669 name.
22670
22671 [basic.lookup.qual]
22672
22673 During the lookup for a name preceding the :: scope
22674 resolution operator, object, function, and enumerator
22675 names are ignored. */
22676 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22677 tag_type = scope_type;
22678 /* Look up the name. */
22679 decl = cp_parser_lookup_name (parser, identifier,
22680 tag_type,
22681 /*is_template=*/false,
22682 /*is_namespace=*/false,
22683 check_dependency_p,
22684 &ambiguous_decls,
22685 identifier_token->location);
22686 if (ambiguous_decls)
22687 {
22688 if (cp_parser_parsing_tentatively (parser))
22689 cp_parser_simulate_error (parser);
22690 return error_mark_node;
22691 }
22692 }
22693 }
22694 else
22695 {
22696 /* Try a template-id. */
22697 decl = cp_parser_template_id (parser, template_keyword_p,
22698 check_dependency_p,
22699 tag_type,
22700 is_declaration);
22701 if (decl == error_mark_node)
22702 return error_mark_node;
22703 }
22704
22705 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22706
22707 /* If this is a typename, create a TYPENAME_TYPE. */
22708 if (typename_p && decl != error_mark_node)
22709 {
22710 decl = make_typename_type (scope, decl, typename_type,
22711 /*complain=*/tf_error);
22712 if (decl != error_mark_node)
22713 decl = TYPE_NAME (decl);
22714 }
22715
22716 decl = strip_using_decl (decl);
22717
22718 /* Check to see that it is really the name of a class. */
22719 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22720 && identifier_p (TREE_OPERAND (decl, 0))
22721 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22722 /* Situations like this:
22723
22724 template <typename T> struct A {
22725 typename T::template X<int>::I i;
22726 };
22727
22728 are problematic. Is `T::template X<int>' a class-name? The
22729 standard does not seem to be definitive, but there is no other
22730 valid interpretation of the following `::'. Therefore, those
22731 names are considered class-names. */
22732 {
22733 decl = make_typename_type (scope, decl, tag_type, tf_error);
22734 if (decl != error_mark_node)
22735 decl = TYPE_NAME (decl);
22736 }
22737 else if (TREE_CODE (decl) != TYPE_DECL
22738 || TREE_TYPE (decl) == error_mark_node
22739 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22740 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22741 /* In Objective-C 2.0, a classname followed by '.' starts a
22742 dot-syntax expression, and it's not a type-name. */
22743 || (c_dialect_objc ()
22744 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22745 && objc_is_class_name (decl)))
22746 decl = error_mark_node;
22747
22748 if (decl == error_mark_node)
22749 cp_parser_error (parser, "expected class-name");
22750 else if (identifier && !parser->scope)
22751 maybe_note_name_used_in_class (identifier, decl);
22752
22753 return decl;
22754 }
22755
22756 /* Parse a class-specifier.
22757
22758 class-specifier:
22759 class-head { member-specification [opt] }
22760
22761 Returns the TREE_TYPE representing the class. */
22762
22763 static tree
22764 cp_parser_class_specifier_1 (cp_parser* parser)
22765 {
22766 tree type;
22767 tree attributes = NULL_TREE;
22768 bool nested_name_specifier_p;
22769 unsigned saved_num_template_parameter_lists;
22770 bool saved_in_function_body;
22771 unsigned char in_statement;
22772 bool in_switch_statement_p;
22773 bool saved_in_unbraced_linkage_specification_p;
22774 tree old_scope = NULL_TREE;
22775 tree scope = NULL_TREE;
22776 cp_token *closing_brace;
22777
22778 push_deferring_access_checks (dk_no_deferred);
22779
22780 /* Parse the class-head. */
22781 type = cp_parser_class_head (parser,
22782 &nested_name_specifier_p);
22783 /* If the class-head was a semantic disaster, skip the entire body
22784 of the class. */
22785 if (!type)
22786 {
22787 cp_parser_skip_to_end_of_block_or_statement (parser);
22788 pop_deferring_access_checks ();
22789 return error_mark_node;
22790 }
22791
22792 /* Look for the `{'. */
22793 matching_braces braces;
22794 if (!braces.require_open (parser))
22795 {
22796 pop_deferring_access_checks ();
22797 return error_mark_node;
22798 }
22799
22800 cp_ensure_no_omp_declare_simd (parser);
22801 cp_ensure_no_oacc_routine (parser);
22802
22803 /* Issue an error message if type-definitions are forbidden here. */
22804 cp_parser_check_type_definition (parser);
22805 /* Remember that we are defining one more class. */
22806 ++parser->num_classes_being_defined;
22807 /* Inside the class, surrounding template-parameter-lists do not
22808 apply. */
22809 saved_num_template_parameter_lists
22810 = parser->num_template_parameter_lists;
22811 parser->num_template_parameter_lists = 0;
22812 /* We are not in a function body. */
22813 saved_in_function_body = parser->in_function_body;
22814 parser->in_function_body = false;
22815 /* Or in a loop. */
22816 in_statement = parser->in_statement;
22817 parser->in_statement = 0;
22818 /* Or in a switch. */
22819 in_switch_statement_p = parser->in_switch_statement_p;
22820 parser->in_switch_statement_p = false;
22821 /* We are not immediately inside an extern "lang" block. */
22822 saved_in_unbraced_linkage_specification_p
22823 = parser->in_unbraced_linkage_specification_p;
22824 parser->in_unbraced_linkage_specification_p = false;
22825
22826 // Associate constraints with the type.
22827 if (flag_concepts)
22828 type = associate_classtype_constraints (type);
22829
22830 /* Start the class. */
22831 if (nested_name_specifier_p)
22832 {
22833 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22834 old_scope = push_inner_scope (scope);
22835 }
22836 type = begin_class_definition (type);
22837
22838 if (type == error_mark_node)
22839 /* If the type is erroneous, skip the entire body of the class. */
22840 cp_parser_skip_to_closing_brace (parser);
22841 else
22842 /* Parse the member-specification. */
22843 cp_parser_member_specification_opt (parser);
22844
22845 /* Look for the trailing `}'. */
22846 closing_brace = braces.require_close (parser);
22847 /* Look for trailing attributes to apply to this class. */
22848 if (cp_parser_allow_gnu_extensions_p (parser))
22849 attributes = cp_parser_gnu_attributes_opt (parser);
22850 if (type != error_mark_node)
22851 type = finish_struct (type, attributes);
22852 if (nested_name_specifier_p)
22853 pop_inner_scope (old_scope, scope);
22854
22855 /* We've finished a type definition. Check for the common syntax
22856 error of forgetting a semicolon after the definition. We need to
22857 be careful, as we can't just check for not-a-semicolon and be done
22858 with it; the user might have typed:
22859
22860 class X { } c = ...;
22861 class X { } *p = ...;
22862
22863 and so forth. Instead, enumerate all the possible tokens that
22864 might follow this production; if we don't see one of them, then
22865 complain and silently insert the semicolon. */
22866 {
22867 cp_token *token = cp_lexer_peek_token (parser->lexer);
22868 bool want_semicolon = true;
22869
22870 if (cp_next_tokens_can_be_std_attribute_p (parser))
22871 /* Don't try to parse c++11 attributes here. As per the
22872 grammar, that should be a task for
22873 cp_parser_decl_specifier_seq. */
22874 want_semicolon = false;
22875
22876 switch (token->type)
22877 {
22878 case CPP_NAME:
22879 case CPP_SEMICOLON:
22880 case CPP_MULT:
22881 case CPP_AND:
22882 case CPP_OPEN_PAREN:
22883 case CPP_CLOSE_PAREN:
22884 case CPP_COMMA:
22885 want_semicolon = false;
22886 break;
22887
22888 /* While it's legal for type qualifiers and storage class
22889 specifiers to follow type definitions in the grammar, only
22890 compiler testsuites contain code like that. Assume that if
22891 we see such code, then what we're really seeing is a case
22892 like:
22893
22894 class X { }
22895 const <type> var = ...;
22896
22897 or
22898
22899 class Y { }
22900 static <type> func (...) ...
22901
22902 i.e. the qualifier or specifier applies to the next
22903 declaration. To do so, however, we need to look ahead one
22904 more token to see if *that* token is a type specifier.
22905
22906 This code could be improved to handle:
22907
22908 class Z { }
22909 static const <type> var = ...; */
22910 case CPP_KEYWORD:
22911 if (keyword_is_decl_specifier (token->keyword))
22912 {
22913 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22914
22915 /* Handling user-defined types here would be nice, but very
22916 tricky. */
22917 want_semicolon
22918 = (lookahead->type == CPP_KEYWORD
22919 && keyword_begins_type_specifier (lookahead->keyword));
22920 }
22921 break;
22922 default:
22923 break;
22924 }
22925
22926 /* If we don't have a type, then something is very wrong and we
22927 shouldn't try to do anything clever. Likewise for not seeing the
22928 closing brace. */
22929 if (closing_brace && TYPE_P (type) && want_semicolon)
22930 {
22931 /* Locate the closing brace. */
22932 cp_token_position prev
22933 = cp_lexer_previous_token_position (parser->lexer);
22934 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22935 location_t loc = prev_token->location;
22936
22937 /* We want to suggest insertion of a ';' immediately *after* the
22938 closing brace, so, if we can, offset the location by 1 column. */
22939 location_t next_loc = loc;
22940 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22941 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22942
22943 rich_location richloc (line_table, next_loc);
22944
22945 /* If we successfully offset the location, suggest the fix-it. */
22946 if (next_loc != loc)
22947 richloc.add_fixit_insert_before (next_loc, ";");
22948
22949 if (CLASSTYPE_DECLARED_CLASS (type))
22950 error_at (&richloc,
22951 "expected %<;%> after class definition");
22952 else if (TREE_CODE (type) == RECORD_TYPE)
22953 error_at (&richloc,
22954 "expected %<;%> after struct definition");
22955 else if (TREE_CODE (type) == UNION_TYPE)
22956 error_at (&richloc,
22957 "expected %<;%> after union definition");
22958 else
22959 gcc_unreachable ();
22960
22961 /* Unget one token and smash it to look as though we encountered
22962 a semicolon in the input stream. */
22963 cp_lexer_set_token_position (parser->lexer, prev);
22964 token = cp_lexer_peek_token (parser->lexer);
22965 token->type = CPP_SEMICOLON;
22966 token->keyword = RID_MAX;
22967 }
22968 }
22969
22970 /* If this class is not itself within the scope of another class,
22971 then we need to parse the bodies of all of the queued function
22972 definitions. Note that the queued functions defined in a class
22973 are not always processed immediately following the
22974 class-specifier for that class. Consider:
22975
22976 struct A {
22977 struct B { void f() { sizeof (A); } };
22978 };
22979
22980 If `f' were processed before the processing of `A' were
22981 completed, there would be no way to compute the size of `A'.
22982 Note that the nesting we are interested in here is lexical --
22983 not the semantic nesting given by TYPE_CONTEXT. In particular,
22984 for:
22985
22986 struct A { struct B; };
22987 struct A::B { void f() { } };
22988
22989 there is no need to delay the parsing of `A::B::f'. */
22990 if (--parser->num_classes_being_defined == 0)
22991 {
22992 tree decl;
22993 tree class_type = NULL_TREE;
22994 tree pushed_scope = NULL_TREE;
22995 unsigned ix;
22996 cp_default_arg_entry *e;
22997 tree save_ccp, save_ccr;
22998
22999 if (any_erroneous_template_args_p (type))
23000 {
23001 /* Skip default arguments, NSDMIs, etc, in order to improve
23002 error recovery (c++/71169, c++/71832). */
23003 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23004 vec_safe_truncate (unparsed_nsdmis, 0);
23005 vec_safe_truncate (unparsed_classes, 0);
23006 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23007 }
23008
23009 /* In a first pass, parse default arguments to the functions.
23010 Then, in a second pass, parse the bodies of the functions.
23011 This two-phased approach handles cases like:
23012
23013 struct S {
23014 void f() { g(); }
23015 void g(int i = 3);
23016 };
23017
23018 */
23019 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23020 {
23021 decl = e->decl;
23022 /* If there are default arguments that have not yet been processed,
23023 take care of them now. */
23024 if (class_type != e->class_type)
23025 {
23026 if (pushed_scope)
23027 pop_scope (pushed_scope);
23028 class_type = e->class_type;
23029 pushed_scope = push_scope (class_type);
23030 }
23031 /* Make sure that any template parameters are in scope. */
23032 maybe_begin_member_template_processing (decl);
23033 /* Parse the default argument expressions. */
23034 cp_parser_late_parsing_default_args (parser, decl);
23035 /* Remove any template parameters from the symbol table. */
23036 maybe_end_member_template_processing ();
23037 }
23038 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23039 /* Now parse any NSDMIs. */
23040 save_ccp = current_class_ptr;
23041 save_ccr = current_class_ref;
23042 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23043 {
23044 if (class_type != DECL_CONTEXT (decl))
23045 {
23046 if (pushed_scope)
23047 pop_scope (pushed_scope);
23048 class_type = DECL_CONTEXT (decl);
23049 pushed_scope = push_scope (class_type);
23050 }
23051 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23052 cp_parser_late_parsing_nsdmi (parser, decl);
23053 }
23054 vec_safe_truncate (unparsed_nsdmis, 0);
23055 current_class_ptr = save_ccp;
23056 current_class_ref = save_ccr;
23057 if (pushed_scope)
23058 pop_scope (pushed_scope);
23059
23060 /* Now do some post-NSDMI bookkeeping. */
23061 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23062 after_nsdmi_defaulted_late_checks (class_type);
23063 vec_safe_truncate (unparsed_classes, 0);
23064 after_nsdmi_defaulted_late_checks (type);
23065
23066 /* Now parse the body of the functions. */
23067 if (flag_openmp)
23068 {
23069 /* OpenMP UDRs need to be parsed before all other functions. */
23070 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23071 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23072 cp_parser_late_parsing_for_member (parser, decl);
23073 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23074 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23075 cp_parser_late_parsing_for_member (parser, decl);
23076 }
23077 else
23078 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23079 cp_parser_late_parsing_for_member (parser, decl);
23080 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23081 }
23082 else
23083 vec_safe_push (unparsed_classes, type);
23084
23085 /* Put back any saved access checks. */
23086 pop_deferring_access_checks ();
23087
23088 /* Restore saved state. */
23089 parser->in_switch_statement_p = in_switch_statement_p;
23090 parser->in_statement = in_statement;
23091 parser->in_function_body = saved_in_function_body;
23092 parser->num_template_parameter_lists
23093 = saved_num_template_parameter_lists;
23094 parser->in_unbraced_linkage_specification_p
23095 = saved_in_unbraced_linkage_specification_p;
23096
23097 return type;
23098 }
23099
23100 static tree
23101 cp_parser_class_specifier (cp_parser* parser)
23102 {
23103 tree ret;
23104 timevar_push (TV_PARSE_STRUCT);
23105 ret = cp_parser_class_specifier_1 (parser);
23106 timevar_pop (TV_PARSE_STRUCT);
23107 return ret;
23108 }
23109
23110 /* Parse a class-head.
23111
23112 class-head:
23113 class-key identifier [opt] base-clause [opt]
23114 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23115 class-key nested-name-specifier [opt] template-id
23116 base-clause [opt]
23117
23118 class-virt-specifier:
23119 final
23120
23121 GNU Extensions:
23122 class-key attributes identifier [opt] base-clause [opt]
23123 class-key attributes nested-name-specifier identifier base-clause [opt]
23124 class-key attributes nested-name-specifier [opt] template-id
23125 base-clause [opt]
23126
23127 Upon return BASES is initialized to the list of base classes (or
23128 NULL, if there are none) in the same form returned by
23129 cp_parser_base_clause.
23130
23131 Returns the TYPE of the indicated class. Sets
23132 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23133 involving a nested-name-specifier was used, and FALSE otherwise.
23134
23135 Returns error_mark_node if this is not a class-head.
23136
23137 Returns NULL_TREE if the class-head is syntactically valid, but
23138 semantically invalid in a way that means we should skip the entire
23139 body of the class. */
23140
23141 static tree
23142 cp_parser_class_head (cp_parser* parser,
23143 bool* nested_name_specifier_p)
23144 {
23145 tree nested_name_specifier;
23146 enum tag_types class_key;
23147 tree id = NULL_TREE;
23148 tree type = NULL_TREE;
23149 tree attributes;
23150 tree bases;
23151 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23152 bool template_id_p = false;
23153 bool qualified_p = false;
23154 bool invalid_nested_name_p = false;
23155 bool invalid_explicit_specialization_p = false;
23156 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23157 tree pushed_scope = NULL_TREE;
23158 unsigned num_templates;
23159 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23160 /* Assume no nested-name-specifier will be present. */
23161 *nested_name_specifier_p = false;
23162 /* Assume no template parameter lists will be used in defining the
23163 type. */
23164 num_templates = 0;
23165 parser->colon_corrects_to_scope_p = false;
23166
23167 /* Look for the class-key. */
23168 class_key = cp_parser_class_key (parser);
23169 if (class_key == none_type)
23170 return error_mark_node;
23171
23172 location_t class_head_start_location = input_location;
23173
23174 /* Parse the attributes. */
23175 attributes = cp_parser_attributes_opt (parser);
23176
23177 /* If the next token is `::', that is invalid -- but sometimes
23178 people do try to write:
23179
23180 struct ::S {};
23181
23182 Handle this gracefully by accepting the extra qualifier, and then
23183 issuing an error about it later if this really is a
23184 class-head. If it turns out just to be an elaborated type
23185 specifier, remain silent. */
23186 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23187 qualified_p = true;
23188
23189 push_deferring_access_checks (dk_no_check);
23190
23191 /* Determine the name of the class. Begin by looking for an
23192 optional nested-name-specifier. */
23193 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23194 nested_name_specifier
23195 = cp_parser_nested_name_specifier_opt (parser,
23196 /*typename_keyword_p=*/false,
23197 /*check_dependency_p=*/false,
23198 /*type_p=*/true,
23199 /*is_declaration=*/false);
23200 /* If there was a nested-name-specifier, then there *must* be an
23201 identifier. */
23202
23203 cp_token *bad_template_keyword = NULL;
23204
23205 if (nested_name_specifier)
23206 {
23207 type_start_token = cp_lexer_peek_token (parser->lexer);
23208 /* Although the grammar says `identifier', it really means
23209 `class-name' or `template-name'. You are only allowed to
23210 define a class that has already been declared with this
23211 syntax.
23212
23213 The proposed resolution for Core Issue 180 says that wherever
23214 you see `class T::X' you should treat `X' as a type-name.
23215
23216 It is OK to define an inaccessible class; for example:
23217
23218 class A { class B; };
23219 class A::B {};
23220
23221 We do not know if we will see a class-name, or a
23222 template-name. We look for a class-name first, in case the
23223 class-name is a template-id; if we looked for the
23224 template-name first we would stop after the template-name. */
23225 cp_parser_parse_tentatively (parser);
23226 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23227 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23228 type = cp_parser_class_name (parser,
23229 /*typename_keyword_p=*/false,
23230 /*template_keyword_p=*/false,
23231 class_type,
23232 /*check_dependency_p=*/false,
23233 /*class_head_p=*/true,
23234 /*is_declaration=*/false);
23235 /* If that didn't work, ignore the nested-name-specifier. */
23236 if (!cp_parser_parse_definitely (parser))
23237 {
23238 invalid_nested_name_p = true;
23239 type_start_token = cp_lexer_peek_token (parser->lexer);
23240 id = cp_parser_identifier (parser);
23241 if (id == error_mark_node)
23242 id = NULL_TREE;
23243 }
23244 /* If we could not find a corresponding TYPE, treat this
23245 declaration like an unqualified declaration. */
23246 if (type == error_mark_node)
23247 nested_name_specifier = NULL_TREE;
23248 /* Otherwise, count the number of templates used in TYPE and its
23249 containing scopes. */
23250 else
23251 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23252 }
23253 /* Otherwise, the identifier is optional. */
23254 else
23255 {
23256 /* We don't know whether what comes next is a template-id,
23257 an identifier, or nothing at all. */
23258 cp_parser_parse_tentatively (parser);
23259 /* Check for a template-id. */
23260 type_start_token = cp_lexer_peek_token (parser->lexer);
23261 id = cp_parser_template_id (parser,
23262 /*template_keyword_p=*/false,
23263 /*check_dependency_p=*/true,
23264 class_key,
23265 /*is_declaration=*/true);
23266 /* If that didn't work, it could still be an identifier. */
23267 if (!cp_parser_parse_definitely (parser))
23268 {
23269 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23270 {
23271 type_start_token = cp_lexer_peek_token (parser->lexer);
23272 id = cp_parser_identifier (parser);
23273 }
23274 else
23275 id = NULL_TREE;
23276 }
23277 else
23278 {
23279 template_id_p = true;
23280 ++num_templates;
23281 }
23282 }
23283
23284 pop_deferring_access_checks ();
23285
23286 if (id)
23287 {
23288 cp_parser_check_for_invalid_template_id (parser, id,
23289 class_key,
23290 type_start_token->location);
23291 }
23292 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23293
23294 /* If it's not a `:' or a `{' then we can't really be looking at a
23295 class-head, since a class-head only appears as part of a
23296 class-specifier. We have to detect this situation before calling
23297 xref_tag, since that has irreversible side-effects. */
23298 if (!cp_parser_next_token_starts_class_definition_p (parser))
23299 {
23300 cp_parser_error (parser, "expected %<{%> or %<:%>");
23301 type = error_mark_node;
23302 goto out;
23303 }
23304
23305 /* At this point, we're going ahead with the class-specifier, even
23306 if some other problem occurs. */
23307 cp_parser_commit_to_tentative_parse (parser);
23308 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23309 {
23310 cp_parser_error (parser,
23311 "cannot specify %<override%> for a class");
23312 type = error_mark_node;
23313 goto out;
23314 }
23315 /* Issue the error about the overly-qualified name now. */
23316 if (qualified_p)
23317 {
23318 cp_parser_error (parser,
23319 "global qualification of class name is invalid");
23320 type = error_mark_node;
23321 goto out;
23322 }
23323 else if (invalid_nested_name_p)
23324 {
23325 cp_parser_error (parser,
23326 "qualified name does not name a class");
23327 type = error_mark_node;
23328 goto out;
23329 }
23330 else if (nested_name_specifier)
23331 {
23332 tree scope;
23333
23334 if (bad_template_keyword)
23335 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23336 keyword template shall not appear at the top level. */
23337 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23338 "keyword %<template%> not allowed in class-head-name");
23339
23340 /* Reject typedef-names in class heads. */
23341 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23342 {
23343 error_at (type_start_token->location,
23344 "invalid class name in declaration of %qD",
23345 type);
23346 type = NULL_TREE;
23347 goto done;
23348 }
23349
23350 /* Figure out in what scope the declaration is being placed. */
23351 scope = current_scope ();
23352 /* If that scope does not contain the scope in which the
23353 class was originally declared, the program is invalid. */
23354 if (scope && !is_ancestor (scope, nested_name_specifier))
23355 {
23356 if (at_namespace_scope_p ())
23357 error_at (type_start_token->location,
23358 "declaration of %qD in namespace %qD which does not "
23359 "enclose %qD",
23360 type, scope, nested_name_specifier);
23361 else
23362 error_at (type_start_token->location,
23363 "declaration of %qD in %qD which does not enclose %qD",
23364 type, scope, nested_name_specifier);
23365 type = NULL_TREE;
23366 goto done;
23367 }
23368 /* [dcl.meaning]
23369
23370 A declarator-id shall not be qualified except for the
23371 definition of a ... nested class outside of its class
23372 ... [or] the definition or explicit instantiation of a
23373 class member of a namespace outside of its namespace. */
23374 if (scope == nested_name_specifier)
23375 {
23376 permerror (nested_name_specifier_token_start->location,
23377 "extra qualification not allowed");
23378 nested_name_specifier = NULL_TREE;
23379 num_templates = 0;
23380 }
23381 }
23382 /* An explicit-specialization must be preceded by "template <>". If
23383 it is not, try to recover gracefully. */
23384 if (at_namespace_scope_p ()
23385 && parser->num_template_parameter_lists == 0
23386 && !processing_template_parmlist
23387 && template_id_p)
23388 {
23389 /* Build a location of this form:
23390 struct typename <ARGS>
23391 ^~~~~~~~~~~~~~~~~~~~~~
23392 with caret==start at the start token, and
23393 finishing at the end of the type. */
23394 location_t reported_loc
23395 = make_location (class_head_start_location,
23396 class_head_start_location,
23397 get_finish (type_start_token->location));
23398 rich_location richloc (line_table, reported_loc);
23399 richloc.add_fixit_insert_before (class_head_start_location,
23400 "template <> ");
23401 error_at (&richloc,
23402 "an explicit specialization must be preceded by"
23403 " %<template <>%>");
23404 invalid_explicit_specialization_p = true;
23405 /* Take the same action that would have been taken by
23406 cp_parser_explicit_specialization. */
23407 ++parser->num_template_parameter_lists;
23408 begin_specialization ();
23409 }
23410 /* There must be no "return" statements between this point and the
23411 end of this function; set "type "to the correct return value and
23412 use "goto done;" to return. */
23413 /* Make sure that the right number of template parameters were
23414 present. */
23415 if (!cp_parser_check_template_parameters (parser, num_templates,
23416 template_id_p,
23417 type_start_token->location,
23418 /*declarator=*/NULL))
23419 {
23420 /* If something went wrong, there is no point in even trying to
23421 process the class-definition. */
23422 type = NULL_TREE;
23423 goto done;
23424 }
23425
23426 /* Look up the type. */
23427 if (template_id_p)
23428 {
23429 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23430 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23431 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23432 {
23433 error_at (type_start_token->location,
23434 "function template %qD redeclared as a class template", id);
23435 type = error_mark_node;
23436 }
23437 else
23438 {
23439 type = TREE_TYPE (id);
23440 type = maybe_process_partial_specialization (type);
23441
23442 /* Check the scope while we still know whether or not we had a
23443 nested-name-specifier. */
23444 if (type != error_mark_node)
23445 check_unqualified_spec_or_inst (type, type_start_token->location);
23446 }
23447 if (nested_name_specifier)
23448 pushed_scope = push_scope (nested_name_specifier);
23449 }
23450 else if (nested_name_specifier)
23451 {
23452 tree class_type;
23453
23454 /* Given:
23455
23456 template <typename T> struct S { struct T };
23457 template <typename T> struct S<T>::T { };
23458
23459 we will get a TYPENAME_TYPE when processing the definition of
23460 `S::T'. We need to resolve it to the actual type before we
23461 try to define it. */
23462 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23463 {
23464 class_type = resolve_typename_type (TREE_TYPE (type),
23465 /*only_current_p=*/false);
23466 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23467 type = TYPE_NAME (class_type);
23468 else
23469 {
23470 cp_parser_error (parser, "could not resolve typename type");
23471 type = error_mark_node;
23472 }
23473 }
23474
23475 if (maybe_process_partial_specialization (TREE_TYPE (type))
23476 == error_mark_node)
23477 {
23478 type = NULL_TREE;
23479 goto done;
23480 }
23481
23482 class_type = current_class_type;
23483 /* Enter the scope indicated by the nested-name-specifier. */
23484 pushed_scope = push_scope (nested_name_specifier);
23485 /* Get the canonical version of this type. */
23486 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23487 /* Call push_template_decl if it seems like we should be defining a
23488 template either from the template headers or the type we're
23489 defining, so that we diagnose both extra and missing headers. */
23490 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23491 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23492 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23493 {
23494 type = push_template_decl (type);
23495 if (type == error_mark_node)
23496 {
23497 type = NULL_TREE;
23498 goto done;
23499 }
23500 }
23501
23502 type = TREE_TYPE (type);
23503 *nested_name_specifier_p = true;
23504 }
23505 else /* The name is not a nested name. */
23506 {
23507 /* If the class was unnamed, create a dummy name. */
23508 if (!id)
23509 id = make_anon_name ();
23510 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23511 ? ts_within_enclosing_non_class
23512 : ts_current);
23513 type = xref_tag (class_key, id, tag_scope,
23514 parser->num_template_parameter_lists);
23515 }
23516
23517 /* Indicate whether this class was declared as a `class' or as a
23518 `struct'. */
23519 if (TREE_CODE (type) == RECORD_TYPE)
23520 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23521 cp_parser_check_class_key (class_key, type);
23522
23523 /* If this type was already complete, and we see another definition,
23524 that's an error. */
23525 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23526 {
23527 error_at (type_start_token->location, "redefinition of %q#T",
23528 type);
23529 inform (location_of (type), "previous definition of %q#T",
23530 type);
23531 type = NULL_TREE;
23532 goto done;
23533 }
23534 else if (type == error_mark_node)
23535 type = NULL_TREE;
23536
23537 if (type)
23538 {
23539 /* Apply attributes now, before any use of the class as a template
23540 argument in its base list. */
23541 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23542 fixup_attribute_variants (type);
23543 }
23544
23545 /* We will have entered the scope containing the class; the names of
23546 base classes should be looked up in that context. For example:
23547
23548 struct A { struct B {}; struct C; };
23549 struct A::C : B {};
23550
23551 is valid. */
23552
23553 /* Get the list of base-classes, if there is one. */
23554 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23555 {
23556 /* PR59482: enter the class scope so that base-specifiers are looked
23557 up correctly. */
23558 if (type)
23559 pushclass (type);
23560 bases = cp_parser_base_clause (parser);
23561 /* PR59482: get out of the previously pushed class scope so that the
23562 subsequent pops pop the right thing. */
23563 if (type)
23564 popclass ();
23565 }
23566 else
23567 bases = NULL_TREE;
23568
23569 /* If we're really defining a class, process the base classes.
23570 If they're invalid, fail. */
23571 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23572 xref_basetypes (type, bases);
23573
23574 done:
23575 /* Leave the scope given by the nested-name-specifier. We will
23576 enter the class scope itself while processing the members. */
23577 if (pushed_scope)
23578 pop_scope (pushed_scope);
23579
23580 if (invalid_explicit_specialization_p)
23581 {
23582 end_specialization ();
23583 --parser->num_template_parameter_lists;
23584 }
23585
23586 if (type)
23587 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23588 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23589 CLASSTYPE_FINAL (type) = 1;
23590 out:
23591 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23592 return type;
23593 }
23594
23595 /* Parse a class-key.
23596
23597 class-key:
23598 class
23599 struct
23600 union
23601
23602 Returns the kind of class-key specified, or none_type to indicate
23603 error. */
23604
23605 static enum tag_types
23606 cp_parser_class_key (cp_parser* parser)
23607 {
23608 cp_token *token;
23609 enum tag_types tag_type;
23610
23611 /* Look for the class-key. */
23612 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23613 if (!token)
23614 return none_type;
23615
23616 /* Check to see if the TOKEN is a class-key. */
23617 tag_type = cp_parser_token_is_class_key (token);
23618 if (!tag_type)
23619 cp_parser_error (parser, "expected class-key");
23620 return tag_type;
23621 }
23622
23623 /* Parse a type-parameter-key.
23624
23625 type-parameter-key:
23626 class
23627 typename
23628 */
23629
23630 static void
23631 cp_parser_type_parameter_key (cp_parser* parser)
23632 {
23633 /* Look for the type-parameter-key. */
23634 enum tag_types tag_type = none_type;
23635 cp_token *token = cp_lexer_peek_token (parser->lexer);
23636 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23637 {
23638 cp_lexer_consume_token (parser->lexer);
23639 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23640 /* typename is not allowed in a template template parameter
23641 by the standard until C++17. */
23642 pedwarn (token->location, OPT_Wpedantic,
23643 "ISO C++ forbids typename key in template template parameter;"
23644 " use -std=c++17 or -std=gnu++17");
23645 }
23646 else
23647 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23648
23649 return;
23650 }
23651
23652 /* Parse an (optional) member-specification.
23653
23654 member-specification:
23655 member-declaration member-specification [opt]
23656 access-specifier : member-specification [opt] */
23657
23658 static void
23659 cp_parser_member_specification_opt (cp_parser* parser)
23660 {
23661 while (true)
23662 {
23663 cp_token *token;
23664 enum rid keyword;
23665
23666 /* Peek at the next token. */
23667 token = cp_lexer_peek_token (parser->lexer);
23668 /* If it's a `}', or EOF then we've seen all the members. */
23669 if (token->type == CPP_CLOSE_BRACE
23670 || token->type == CPP_EOF
23671 || token->type == CPP_PRAGMA_EOL)
23672 break;
23673
23674 /* See if this token is a keyword. */
23675 keyword = token->keyword;
23676 switch (keyword)
23677 {
23678 case RID_PUBLIC:
23679 case RID_PROTECTED:
23680 case RID_PRIVATE:
23681 /* Consume the access-specifier. */
23682 cp_lexer_consume_token (parser->lexer);
23683 /* Remember which access-specifier is active. */
23684 current_access_specifier = token->u.value;
23685 /* Look for the `:'. */
23686 cp_parser_require (parser, CPP_COLON, RT_COLON);
23687 break;
23688
23689 default:
23690 /* Accept #pragmas at class scope. */
23691 if (token->type == CPP_PRAGMA)
23692 {
23693 cp_parser_pragma (parser, pragma_member, NULL);
23694 break;
23695 }
23696
23697 /* Otherwise, the next construction must be a
23698 member-declaration. */
23699 cp_parser_member_declaration (parser);
23700 }
23701 }
23702 }
23703
23704 /* Parse a member-declaration.
23705
23706 member-declaration:
23707 decl-specifier-seq [opt] member-declarator-list [opt] ;
23708 function-definition ; [opt]
23709 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23710 using-declaration
23711 template-declaration
23712 alias-declaration
23713
23714 member-declarator-list:
23715 member-declarator
23716 member-declarator-list , member-declarator
23717
23718 member-declarator:
23719 declarator pure-specifier [opt]
23720 declarator constant-initializer [opt]
23721 identifier [opt] : constant-expression
23722
23723 GNU Extensions:
23724
23725 member-declaration:
23726 __extension__ member-declaration
23727
23728 member-declarator:
23729 declarator attributes [opt] pure-specifier [opt]
23730 declarator attributes [opt] constant-initializer [opt]
23731 identifier [opt] attributes [opt] : constant-expression
23732
23733 C++0x Extensions:
23734
23735 member-declaration:
23736 static_assert-declaration */
23737
23738 static void
23739 cp_parser_member_declaration (cp_parser* parser)
23740 {
23741 cp_decl_specifier_seq decl_specifiers;
23742 tree prefix_attributes;
23743 tree decl;
23744 int declares_class_or_enum;
23745 bool friend_p;
23746 cp_token *token = NULL;
23747 cp_token *decl_spec_token_start = NULL;
23748 cp_token *initializer_token_start = NULL;
23749 int saved_pedantic;
23750 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23751
23752 /* Check for the `__extension__' keyword. */
23753 if (cp_parser_extension_opt (parser, &saved_pedantic))
23754 {
23755 /* Recurse. */
23756 cp_parser_member_declaration (parser);
23757 /* Restore the old value of the PEDANTIC flag. */
23758 pedantic = saved_pedantic;
23759
23760 return;
23761 }
23762
23763 /* Check for a template-declaration. */
23764 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23765 {
23766 /* An explicit specialization here is an error condition, and we
23767 expect the specialization handler to detect and report this. */
23768 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23769 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23770 cp_parser_explicit_specialization (parser);
23771 else
23772 cp_parser_template_declaration (parser, /*member_p=*/true);
23773
23774 return;
23775 }
23776 /* Check for a template introduction. */
23777 else if (cp_parser_template_declaration_after_export (parser, true))
23778 return;
23779
23780 /* Check for a using-declaration. */
23781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23782 {
23783 if (cxx_dialect < cxx11)
23784 {
23785 /* Parse the using-declaration. */
23786 cp_parser_using_declaration (parser,
23787 /*access_declaration_p=*/false);
23788 return;
23789 }
23790 else
23791 {
23792 tree decl;
23793 bool alias_decl_expected;
23794 cp_parser_parse_tentatively (parser);
23795 decl = cp_parser_alias_declaration (parser);
23796 /* Note that if we actually see the '=' token after the
23797 identifier, cp_parser_alias_declaration commits the
23798 tentative parse. In that case, we really expect an
23799 alias-declaration. Otherwise, we expect a using
23800 declaration. */
23801 alias_decl_expected =
23802 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23803 cp_parser_parse_definitely (parser);
23804
23805 if (alias_decl_expected)
23806 finish_member_declaration (decl);
23807 else
23808 cp_parser_using_declaration (parser,
23809 /*access_declaration_p=*/false);
23810 return;
23811 }
23812 }
23813
23814 /* Check for @defs. */
23815 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23816 {
23817 tree ivar, member;
23818 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23819 ivar = ivar_chains;
23820 while (ivar)
23821 {
23822 member = ivar;
23823 ivar = TREE_CHAIN (member);
23824 TREE_CHAIN (member) = NULL_TREE;
23825 finish_member_declaration (member);
23826 }
23827 return;
23828 }
23829
23830 /* If the next token is `static_assert' we have a static assertion. */
23831 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23832 {
23833 cp_parser_static_assert (parser, /*member_p=*/true);
23834 return;
23835 }
23836
23837 parser->colon_corrects_to_scope_p = false;
23838
23839 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23840 goto out;
23841
23842 /* Parse the decl-specifier-seq. */
23843 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23844 cp_parser_decl_specifier_seq (parser,
23845 CP_PARSER_FLAGS_OPTIONAL,
23846 &decl_specifiers,
23847 &declares_class_or_enum);
23848 /* Check for an invalid type-name. */
23849 if (!decl_specifiers.any_type_specifiers_p
23850 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23851 goto out;
23852 /* If there is no declarator, then the decl-specifier-seq should
23853 specify a type. */
23854 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23855 {
23856 /* If there was no decl-specifier-seq, and the next token is a
23857 `;', then we have something like:
23858
23859 struct S { ; };
23860
23861 [class.mem]
23862
23863 Each member-declaration shall declare at least one member
23864 name of the class. */
23865 if (!decl_specifiers.any_specifiers_p)
23866 {
23867 cp_token *token = cp_lexer_peek_token (parser->lexer);
23868 if (!in_system_header_at (token->location))
23869 {
23870 gcc_rich_location richloc (token->location);
23871 richloc.add_fixit_remove ();
23872 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23873 }
23874 }
23875 else
23876 {
23877 tree type;
23878
23879 /* See if this declaration is a friend. */
23880 friend_p = cp_parser_friend_p (&decl_specifiers);
23881 /* If there were decl-specifiers, check to see if there was
23882 a class-declaration. */
23883 type = check_tag_decl (&decl_specifiers,
23884 /*explicit_type_instantiation_p=*/false);
23885 /* Nested classes have already been added to the class, but
23886 a `friend' needs to be explicitly registered. */
23887 if (friend_p)
23888 {
23889 /* If the `friend' keyword was present, the friend must
23890 be introduced with a class-key. */
23891 if (!declares_class_or_enum && cxx_dialect < cxx11)
23892 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23893 "in C++03 a class-key must be used "
23894 "when declaring a friend");
23895 /* In this case:
23896
23897 template <typename T> struct A {
23898 friend struct A<T>::B;
23899 };
23900
23901 A<T>::B will be represented by a TYPENAME_TYPE, and
23902 therefore not recognized by check_tag_decl. */
23903 if (!type)
23904 {
23905 type = decl_specifiers.type;
23906 if (type && TREE_CODE (type) == TYPE_DECL)
23907 type = TREE_TYPE (type);
23908 }
23909 if (!type || !TYPE_P (type))
23910 error_at (decl_spec_token_start->location,
23911 "friend declaration does not name a class or "
23912 "function");
23913 else
23914 make_friend_class (current_class_type, type,
23915 /*complain=*/true);
23916 }
23917 /* If there is no TYPE, an error message will already have
23918 been issued. */
23919 else if (!type || type == error_mark_node)
23920 ;
23921 /* An anonymous aggregate has to be handled specially; such
23922 a declaration really declares a data member (with a
23923 particular type), as opposed to a nested class. */
23924 else if (ANON_AGGR_TYPE_P (type))
23925 {
23926 /* C++11 9.5/6. */
23927 if (decl_specifiers.storage_class != sc_none)
23928 error_at (decl_spec_token_start->location,
23929 "a storage class on an anonymous aggregate "
23930 "in class scope is not allowed");
23931
23932 /* Remove constructors and such from TYPE, now that we
23933 know it is an anonymous aggregate. */
23934 fixup_anonymous_aggr (type);
23935 /* And make the corresponding data member. */
23936 decl = build_decl (decl_spec_token_start->location,
23937 FIELD_DECL, NULL_TREE, type);
23938 /* Add it to the class. */
23939 finish_member_declaration (decl);
23940 }
23941 else
23942 cp_parser_check_access_in_redeclaration
23943 (TYPE_NAME (type),
23944 decl_spec_token_start->location);
23945 }
23946 }
23947 else
23948 {
23949 bool assume_semicolon = false;
23950
23951 /* Clear attributes from the decl_specifiers but keep them
23952 around as prefix attributes that apply them to the entity
23953 being declared. */
23954 prefix_attributes = decl_specifiers.attributes;
23955 decl_specifiers.attributes = NULL_TREE;
23956
23957 /* See if these declarations will be friends. */
23958 friend_p = cp_parser_friend_p (&decl_specifiers);
23959
23960 /* Keep going until we hit the `;' at the end of the
23961 declaration. */
23962 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23963 {
23964 tree attributes = NULL_TREE;
23965 tree first_attribute;
23966 tree initializer;
23967 bool named_bitfld = false;
23968
23969 /* Peek at the next token. */
23970 token = cp_lexer_peek_token (parser->lexer);
23971
23972 /* The following code wants to know early if it is a bit-field
23973 or some other declaration. Attributes can appear before
23974 the `:' token. Skip over them without consuming any tokens
23975 to peek if they are followed by `:'. */
23976 if (cp_next_tokens_can_be_attribute_p (parser)
23977 || (token->type == CPP_NAME
23978 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23979 && (named_bitfld = true)))
23980 {
23981 size_t n
23982 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
23983 token = cp_lexer_peek_nth_token (parser->lexer, n);
23984 }
23985
23986 /* Check for a bitfield declaration. */
23987 if (token->type == CPP_COLON
23988 || (token->type == CPP_NAME
23989 && token == cp_lexer_peek_token (parser->lexer)
23990 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23991 && (named_bitfld = true)))
23992 {
23993 tree identifier;
23994 tree width;
23995 tree late_attributes = NULL_TREE;
23996
23997 if (named_bitfld)
23998 identifier = cp_parser_identifier (parser);
23999 else
24000 identifier = NULL_TREE;
24001
24002 /* Look for attributes that apply to the bitfield. */
24003 attributes = cp_parser_attributes_opt (parser);
24004
24005 /* Consume the `:' token. */
24006 cp_lexer_consume_token (parser->lexer);
24007
24008 /* Get the width of the bitfield. */
24009 width = cp_parser_constant_expression (parser, false, NULL,
24010 cxx_dialect >= cxx11);
24011
24012 /* In C++2A and as extension for C++11 and above we allow
24013 default member initializers for bit-fields. */
24014 initializer = NULL_TREE;
24015 if (cxx_dialect >= cxx11
24016 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24017 || cp_lexer_next_token_is (parser->lexer,
24018 CPP_OPEN_BRACE)))
24019 {
24020 location_t loc
24021 = cp_lexer_peek_token (parser->lexer)->location;
24022 if (cxx_dialect < cxx2a
24023 && !in_system_header_at (loc)
24024 && identifier != NULL_TREE)
24025 pedwarn (loc, 0,
24026 "default member initializers for bit-fields "
24027 "only available with -std=c++2a or "
24028 "-std=gnu++2a");
24029
24030 initializer = cp_parser_save_nsdmi (parser);
24031 if (identifier == NULL_TREE)
24032 {
24033 error_at (loc, "default member initializer for "
24034 "unnamed bit-field");
24035 initializer = NULL_TREE;
24036 }
24037 }
24038 else
24039 {
24040 /* Look for attributes that apply to the bitfield after
24041 the `:' token and width. This is where GCC used to
24042 parse attributes in the past, pedwarn if there is
24043 a std attribute. */
24044 if (cp_next_tokens_can_be_std_attribute_p (parser))
24045 pedwarn (input_location, OPT_Wpedantic,
24046 "ISO C++ allows bit-field attributes only "
24047 "before the %<:%> token");
24048
24049 late_attributes = cp_parser_attributes_opt (parser);
24050 }
24051
24052 attributes = attr_chainon (attributes, late_attributes);
24053
24054 /* Remember which attributes are prefix attributes and
24055 which are not. */
24056 first_attribute = attributes;
24057 /* Combine the attributes. */
24058 attributes = attr_chainon (prefix_attributes, attributes);
24059
24060 /* Create the bitfield declaration. */
24061 decl = grokbitfield (identifier
24062 ? make_id_declarator (NULL_TREE,
24063 identifier,
24064 sfk_none)
24065 : NULL,
24066 &decl_specifiers,
24067 width, initializer,
24068 attributes);
24069 }
24070 else
24071 {
24072 cp_declarator *declarator;
24073 tree asm_specification;
24074 int ctor_dtor_or_conv_p;
24075
24076 /* Parse the declarator. */
24077 declarator
24078 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24079 &ctor_dtor_or_conv_p,
24080 /*parenthesized_p=*/NULL,
24081 /*member_p=*/true,
24082 friend_p);
24083
24084 /* If something went wrong parsing the declarator, make sure
24085 that we at least consume some tokens. */
24086 if (declarator == cp_error_declarator)
24087 {
24088 /* Skip to the end of the statement. */
24089 cp_parser_skip_to_end_of_statement (parser);
24090 /* If the next token is not a semicolon, that is
24091 probably because we just skipped over the body of
24092 a function. So, we consume a semicolon if
24093 present, but do not issue an error message if it
24094 is not present. */
24095 if (cp_lexer_next_token_is (parser->lexer,
24096 CPP_SEMICOLON))
24097 cp_lexer_consume_token (parser->lexer);
24098 goto out;
24099 }
24100
24101 if (declares_class_or_enum & 2)
24102 cp_parser_check_for_definition_in_return_type
24103 (declarator, decl_specifiers.type,
24104 decl_specifiers.locations[ds_type_spec]);
24105
24106 /* Look for an asm-specification. */
24107 asm_specification = cp_parser_asm_specification_opt (parser);
24108 /* Look for attributes that apply to the declaration. */
24109 attributes = cp_parser_attributes_opt (parser);
24110 /* Remember which attributes are prefix attributes and
24111 which are not. */
24112 first_attribute = attributes;
24113 /* Combine the attributes. */
24114 attributes = attr_chainon (prefix_attributes, attributes);
24115
24116 /* If it's an `=', then we have a constant-initializer or a
24117 pure-specifier. It is not correct to parse the
24118 initializer before registering the member declaration
24119 since the member declaration should be in scope while
24120 its initializer is processed. However, the rest of the
24121 front end does not yet provide an interface that allows
24122 us to handle this correctly. */
24123 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24124 {
24125 /* In [class.mem]:
24126
24127 A pure-specifier shall be used only in the declaration of
24128 a virtual function.
24129
24130 A member-declarator can contain a constant-initializer
24131 only if it declares a static member of integral or
24132 enumeration type.
24133
24134 Therefore, if the DECLARATOR is for a function, we look
24135 for a pure-specifier; otherwise, we look for a
24136 constant-initializer. When we call `grokfield', it will
24137 perform more stringent semantics checks. */
24138 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24139 if (function_declarator_p (declarator)
24140 || (decl_specifiers.type
24141 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24142 && declarator->kind == cdk_id
24143 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24144 == FUNCTION_TYPE)))
24145 initializer = cp_parser_pure_specifier (parser);
24146 else if (decl_specifiers.storage_class != sc_static)
24147 initializer = cp_parser_save_nsdmi (parser);
24148 else if (cxx_dialect >= cxx11)
24149 {
24150 bool nonconst;
24151 /* Don't require a constant rvalue in C++11, since we
24152 might want a reference constant. We'll enforce
24153 constancy later. */
24154 cp_lexer_consume_token (parser->lexer);
24155 /* Parse the initializer. */
24156 initializer = cp_parser_initializer_clause (parser,
24157 &nonconst);
24158 }
24159 else
24160 /* Parse the initializer. */
24161 initializer = cp_parser_constant_initializer (parser);
24162 }
24163 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24164 && !function_declarator_p (declarator))
24165 {
24166 bool x;
24167 if (decl_specifiers.storage_class != sc_static)
24168 initializer = cp_parser_save_nsdmi (parser);
24169 else
24170 initializer = cp_parser_initializer (parser, &x, &x);
24171 }
24172 /* Otherwise, there is no initializer. */
24173 else
24174 initializer = NULL_TREE;
24175
24176 /* See if we are probably looking at a function
24177 definition. We are certainly not looking at a
24178 member-declarator. Calling `grokfield' has
24179 side-effects, so we must not do it unless we are sure
24180 that we are looking at a member-declarator. */
24181 if (cp_parser_token_starts_function_definition_p
24182 (cp_lexer_peek_token (parser->lexer)))
24183 {
24184 /* The grammar does not allow a pure-specifier to be
24185 used when a member function is defined. (It is
24186 possible that this fact is an oversight in the
24187 standard, since a pure function may be defined
24188 outside of the class-specifier. */
24189 if (initializer && initializer_token_start)
24190 error_at (initializer_token_start->location,
24191 "pure-specifier on function-definition");
24192 decl = cp_parser_save_member_function_body (parser,
24193 &decl_specifiers,
24194 declarator,
24195 attributes);
24196 if (parser->fully_implicit_function_template_p)
24197 decl = finish_fully_implicit_template (parser, decl);
24198 /* If the member was not a friend, declare it here. */
24199 if (!friend_p)
24200 finish_member_declaration (decl);
24201 /* Peek at the next token. */
24202 token = cp_lexer_peek_token (parser->lexer);
24203 /* If the next token is a semicolon, consume it. */
24204 if (token->type == CPP_SEMICOLON)
24205 {
24206 location_t semicolon_loc
24207 = cp_lexer_consume_token (parser->lexer)->location;
24208 gcc_rich_location richloc (semicolon_loc);
24209 richloc.add_fixit_remove ();
24210 warning_at (&richloc, OPT_Wextra_semi,
24211 "extra %<;%> after in-class "
24212 "function definition");
24213 }
24214 goto out;
24215 }
24216 else
24217 if (declarator->kind == cdk_function)
24218 declarator->id_loc = token->location;
24219 /* Create the declaration. */
24220 decl = grokfield (declarator, &decl_specifiers,
24221 initializer, /*init_const_expr_p=*/true,
24222 asm_specification, attributes);
24223 if (parser->fully_implicit_function_template_p)
24224 {
24225 if (friend_p)
24226 finish_fully_implicit_template (parser, 0);
24227 else
24228 decl = finish_fully_implicit_template (parser, decl);
24229 }
24230 }
24231
24232 cp_finalize_omp_declare_simd (parser, decl);
24233 cp_finalize_oacc_routine (parser, decl, false);
24234
24235 /* Reset PREFIX_ATTRIBUTES. */
24236 if (attributes != error_mark_node)
24237 {
24238 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24239 attributes = TREE_CHAIN (attributes);
24240 if (attributes)
24241 TREE_CHAIN (attributes) = NULL_TREE;
24242 }
24243
24244 /* If there is any qualification still in effect, clear it
24245 now; we will be starting fresh with the next declarator. */
24246 parser->scope = NULL_TREE;
24247 parser->qualifying_scope = NULL_TREE;
24248 parser->object_scope = NULL_TREE;
24249 /* If it's a `,', then there are more declarators. */
24250 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24251 {
24252 cp_lexer_consume_token (parser->lexer);
24253 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24254 {
24255 cp_token *token = cp_lexer_previous_token (parser->lexer);
24256 gcc_rich_location richloc (token->location);
24257 richloc.add_fixit_remove ();
24258 error_at (&richloc, "stray %<,%> at end of "
24259 "member declaration");
24260 }
24261 }
24262 /* If the next token isn't a `;', then we have a parse error. */
24263 else if (cp_lexer_next_token_is_not (parser->lexer,
24264 CPP_SEMICOLON))
24265 {
24266 /* The next token might be a ways away from where the
24267 actual semicolon is missing. Find the previous token
24268 and use that for our error position. */
24269 cp_token *token = cp_lexer_previous_token (parser->lexer);
24270 gcc_rich_location richloc (token->location);
24271 richloc.add_fixit_insert_after (";");
24272 error_at (&richloc, "expected %<;%> at end of "
24273 "member declaration");
24274
24275 /* Assume that the user meant to provide a semicolon. If
24276 we were to cp_parser_skip_to_end_of_statement, we might
24277 skip to a semicolon inside a member function definition
24278 and issue nonsensical error messages. */
24279 assume_semicolon = true;
24280 }
24281
24282 if (decl)
24283 {
24284 /* Add DECL to the list of members. */
24285 if (!friend_p
24286 /* Explicitly include, eg, NSDMIs, for better error
24287 recovery (c++/58650). */
24288 || !DECL_DECLARES_FUNCTION_P (decl))
24289 finish_member_declaration (decl);
24290
24291 if (TREE_CODE (decl) == FUNCTION_DECL)
24292 cp_parser_save_default_args (parser, decl);
24293 else if (TREE_CODE (decl) == FIELD_DECL
24294 && DECL_INITIAL (decl))
24295 /* Add DECL to the queue of NSDMI to be parsed later. */
24296 vec_safe_push (unparsed_nsdmis, decl);
24297 }
24298
24299 if (assume_semicolon)
24300 goto out;
24301 }
24302 }
24303
24304 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24305 out:
24306 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24307 }
24308
24309 /* Parse a pure-specifier.
24310
24311 pure-specifier:
24312 = 0
24313
24314 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24315 Otherwise, ERROR_MARK_NODE is returned. */
24316
24317 static tree
24318 cp_parser_pure_specifier (cp_parser* parser)
24319 {
24320 cp_token *token;
24321
24322 /* Look for the `=' token. */
24323 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24324 return error_mark_node;
24325 /* Look for the `0' token. */
24326 token = cp_lexer_peek_token (parser->lexer);
24327
24328 if (token->type == CPP_EOF
24329 || token->type == CPP_PRAGMA_EOL)
24330 return error_mark_node;
24331
24332 cp_lexer_consume_token (parser->lexer);
24333
24334 /* Accept = default or = delete in c++0x mode. */
24335 if (token->keyword == RID_DEFAULT
24336 || token->keyword == RID_DELETE)
24337 {
24338 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24339 return token->u.value;
24340 }
24341
24342 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24343 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24344 {
24345 cp_parser_error (parser,
24346 "invalid pure specifier (only %<= 0%> is allowed)");
24347 cp_parser_skip_to_end_of_statement (parser);
24348 return error_mark_node;
24349 }
24350 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24351 {
24352 error_at (token->location, "templates may not be %<virtual%>");
24353 return error_mark_node;
24354 }
24355
24356 return integer_zero_node;
24357 }
24358
24359 /* Parse a constant-initializer.
24360
24361 constant-initializer:
24362 = constant-expression
24363
24364 Returns a representation of the constant-expression. */
24365
24366 static tree
24367 cp_parser_constant_initializer (cp_parser* parser)
24368 {
24369 /* Look for the `=' token. */
24370 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24371 return error_mark_node;
24372
24373 /* It is invalid to write:
24374
24375 struct S { static const int i = { 7 }; };
24376
24377 */
24378 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24379 {
24380 cp_parser_error (parser,
24381 "a brace-enclosed initializer is not allowed here");
24382 /* Consume the opening brace. */
24383 matching_braces braces;
24384 braces.consume_open (parser);
24385 /* Skip the initializer. */
24386 cp_parser_skip_to_closing_brace (parser);
24387 /* Look for the trailing `}'. */
24388 braces.require_close (parser);
24389
24390 return error_mark_node;
24391 }
24392
24393 return cp_parser_constant_expression (parser);
24394 }
24395
24396 /* Derived classes [gram.class.derived] */
24397
24398 /* Parse a base-clause.
24399
24400 base-clause:
24401 : base-specifier-list
24402
24403 base-specifier-list:
24404 base-specifier ... [opt]
24405 base-specifier-list , base-specifier ... [opt]
24406
24407 Returns a TREE_LIST representing the base-classes, in the order in
24408 which they were declared. The representation of each node is as
24409 described by cp_parser_base_specifier.
24410
24411 In the case that no bases are specified, this function will return
24412 NULL_TREE, not ERROR_MARK_NODE. */
24413
24414 static tree
24415 cp_parser_base_clause (cp_parser* parser)
24416 {
24417 tree bases = NULL_TREE;
24418
24419 /* Look for the `:' that begins the list. */
24420 cp_parser_require (parser, CPP_COLON, RT_COLON);
24421
24422 /* Scan the base-specifier-list. */
24423 while (true)
24424 {
24425 cp_token *token;
24426 tree base;
24427 bool pack_expansion_p = false;
24428
24429 /* Look for the base-specifier. */
24430 base = cp_parser_base_specifier (parser);
24431 /* Look for the (optional) ellipsis. */
24432 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24433 {
24434 /* Consume the `...'. */
24435 cp_lexer_consume_token (parser->lexer);
24436
24437 pack_expansion_p = true;
24438 }
24439
24440 /* Add BASE to the front of the list. */
24441 if (base && base != error_mark_node)
24442 {
24443 if (pack_expansion_p)
24444 /* Make this a pack expansion type. */
24445 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24446
24447 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24448 {
24449 TREE_CHAIN (base) = bases;
24450 bases = base;
24451 }
24452 }
24453 /* Peek at the next token. */
24454 token = cp_lexer_peek_token (parser->lexer);
24455 /* If it's not a comma, then the list is complete. */
24456 if (token->type != CPP_COMMA)
24457 break;
24458 /* Consume the `,'. */
24459 cp_lexer_consume_token (parser->lexer);
24460 }
24461
24462 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24463 base class had a qualified name. However, the next name that
24464 appears is certainly not qualified. */
24465 parser->scope = NULL_TREE;
24466 parser->qualifying_scope = NULL_TREE;
24467 parser->object_scope = NULL_TREE;
24468
24469 return nreverse (bases);
24470 }
24471
24472 /* Parse a base-specifier.
24473
24474 base-specifier:
24475 :: [opt] nested-name-specifier [opt] class-name
24476 virtual access-specifier [opt] :: [opt] nested-name-specifier
24477 [opt] class-name
24478 access-specifier virtual [opt] :: [opt] nested-name-specifier
24479 [opt] class-name
24480
24481 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24482 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24483 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24484 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24485
24486 static tree
24487 cp_parser_base_specifier (cp_parser* parser)
24488 {
24489 cp_token *token;
24490 bool done = false;
24491 bool virtual_p = false;
24492 bool duplicate_virtual_error_issued_p = false;
24493 bool duplicate_access_error_issued_p = false;
24494 bool class_scope_p, template_p;
24495 tree access = access_default_node;
24496 tree type;
24497
24498 /* Process the optional `virtual' and `access-specifier'. */
24499 while (!done)
24500 {
24501 /* Peek at the next token. */
24502 token = cp_lexer_peek_token (parser->lexer);
24503 /* Process `virtual'. */
24504 switch (token->keyword)
24505 {
24506 case RID_VIRTUAL:
24507 /* If `virtual' appears more than once, issue an error. */
24508 if (virtual_p && !duplicate_virtual_error_issued_p)
24509 {
24510 cp_parser_error (parser,
24511 "%<virtual%> specified more than once in base-specifier");
24512 duplicate_virtual_error_issued_p = true;
24513 }
24514
24515 virtual_p = true;
24516
24517 /* Consume the `virtual' token. */
24518 cp_lexer_consume_token (parser->lexer);
24519
24520 break;
24521
24522 case RID_PUBLIC:
24523 case RID_PROTECTED:
24524 case RID_PRIVATE:
24525 /* If more than one access specifier appears, issue an
24526 error. */
24527 if (access != access_default_node
24528 && !duplicate_access_error_issued_p)
24529 {
24530 cp_parser_error (parser,
24531 "more than one access specifier in base-specifier");
24532 duplicate_access_error_issued_p = true;
24533 }
24534
24535 access = ridpointers[(int) token->keyword];
24536
24537 /* Consume the access-specifier. */
24538 cp_lexer_consume_token (parser->lexer);
24539
24540 break;
24541
24542 default:
24543 done = true;
24544 break;
24545 }
24546 }
24547 /* It is not uncommon to see programs mechanically, erroneously, use
24548 the 'typename' keyword to denote (dependent) qualified types
24549 as base classes. */
24550 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24551 {
24552 token = cp_lexer_peek_token (parser->lexer);
24553 if (!processing_template_decl)
24554 error_at (token->location,
24555 "keyword %<typename%> not allowed outside of templates");
24556 else
24557 error_at (token->location,
24558 "keyword %<typename%> not allowed in this context "
24559 "(the base class is implicitly a type)");
24560 cp_lexer_consume_token (parser->lexer);
24561 }
24562
24563 /* Look for the optional `::' operator. */
24564 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24565 /* Look for the nested-name-specifier. The simplest way to
24566 implement:
24567
24568 [temp.res]
24569
24570 The keyword `typename' is not permitted in a base-specifier or
24571 mem-initializer; in these contexts a qualified name that
24572 depends on a template-parameter is implicitly assumed to be a
24573 type name.
24574
24575 is to pretend that we have seen the `typename' keyword at this
24576 point. */
24577 cp_parser_nested_name_specifier_opt (parser,
24578 /*typename_keyword_p=*/true,
24579 /*check_dependency_p=*/true,
24580 /*type_p=*/true,
24581 /*is_declaration=*/true);
24582 /* If the base class is given by a qualified name, assume that names
24583 we see are type names or templates, as appropriate. */
24584 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24585 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24586
24587 if (!parser->scope
24588 && cp_lexer_next_token_is_decltype (parser->lexer))
24589 /* DR 950 allows decltype as a base-specifier. */
24590 type = cp_parser_decltype (parser);
24591 else
24592 {
24593 /* Otherwise, look for the class-name. */
24594 type = cp_parser_class_name (parser,
24595 class_scope_p,
24596 template_p,
24597 typename_type,
24598 /*check_dependency_p=*/true,
24599 /*class_head_p=*/false,
24600 /*is_declaration=*/true);
24601 type = TREE_TYPE (type);
24602 }
24603
24604 if (type == error_mark_node)
24605 return error_mark_node;
24606
24607 return finish_base_specifier (type, access, virtual_p);
24608 }
24609
24610 /* Exception handling [gram.exception] */
24611
24612 /* Parse an (optional) noexcept-specification.
24613
24614 noexcept-specification:
24615 noexcept ( constant-expression ) [opt]
24616
24617 If no noexcept-specification is present, returns NULL_TREE.
24618 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24619 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24620 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24621 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24622 in which case a boolean condition is returned instead. */
24623
24624 static tree
24625 cp_parser_noexcept_specification_opt (cp_parser* parser,
24626 bool require_constexpr,
24627 bool* consumed_expr,
24628 bool return_cond)
24629 {
24630 cp_token *token;
24631 const char *saved_message;
24632
24633 /* Peek at the next token. */
24634 token = cp_lexer_peek_token (parser->lexer);
24635
24636 /* Is it a noexcept-specification? */
24637 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24638 {
24639 tree expr;
24640 cp_lexer_consume_token (parser->lexer);
24641
24642 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24643 {
24644 matching_parens parens;
24645 parens.consume_open (parser);
24646
24647 if (require_constexpr)
24648 {
24649 /* Types may not be defined in an exception-specification. */
24650 saved_message = parser->type_definition_forbidden_message;
24651 parser->type_definition_forbidden_message
24652 = G_("types may not be defined in an exception-specification");
24653
24654 expr = cp_parser_constant_expression (parser);
24655
24656 /* Restore the saved message. */
24657 parser->type_definition_forbidden_message = saved_message;
24658 }
24659 else
24660 {
24661 expr = cp_parser_expression (parser);
24662 *consumed_expr = true;
24663 }
24664
24665 parens.require_close (parser);
24666 }
24667 else
24668 {
24669 expr = boolean_true_node;
24670 if (!require_constexpr)
24671 *consumed_expr = false;
24672 }
24673
24674 /* We cannot build a noexcept-spec right away because this will check
24675 that expr is a constexpr. */
24676 if (!return_cond)
24677 return build_noexcept_spec (expr, tf_warning_or_error);
24678 else
24679 return expr;
24680 }
24681 else
24682 return NULL_TREE;
24683 }
24684
24685 /* Parse an (optional) exception-specification.
24686
24687 exception-specification:
24688 throw ( type-id-list [opt] )
24689
24690 Returns a TREE_LIST representing the exception-specification. The
24691 TREE_VALUE of each node is a type. */
24692
24693 static tree
24694 cp_parser_exception_specification_opt (cp_parser* parser)
24695 {
24696 cp_token *token;
24697 tree type_id_list;
24698 const char *saved_message;
24699
24700 /* Peek at the next token. */
24701 token = cp_lexer_peek_token (parser->lexer);
24702
24703 /* Is it a noexcept-specification? */
24704 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24705 false);
24706 if (type_id_list != NULL_TREE)
24707 return type_id_list;
24708
24709 /* If it's not `throw', then there's no exception-specification. */
24710 if (!cp_parser_is_keyword (token, RID_THROW))
24711 return NULL_TREE;
24712
24713 location_t loc = token->location;
24714
24715 /* Consume the `throw'. */
24716 cp_lexer_consume_token (parser->lexer);
24717
24718 /* Look for the `('. */
24719 matching_parens parens;
24720 parens.require_open (parser);
24721
24722 /* Peek at the next token. */
24723 token = cp_lexer_peek_token (parser->lexer);
24724 /* If it's not a `)', then there is a type-id-list. */
24725 if (token->type != CPP_CLOSE_PAREN)
24726 {
24727 /* Types may not be defined in an exception-specification. */
24728 saved_message = parser->type_definition_forbidden_message;
24729 parser->type_definition_forbidden_message
24730 = G_("types may not be defined in an exception-specification");
24731 /* Parse the type-id-list. */
24732 type_id_list = cp_parser_type_id_list (parser);
24733 /* Restore the saved message. */
24734 parser->type_definition_forbidden_message = saved_message;
24735
24736 if (cxx_dialect >= cxx17)
24737 {
24738 error_at (loc, "ISO C++17 does not allow dynamic exception "
24739 "specifications");
24740 type_id_list = NULL_TREE;
24741 }
24742 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24743 warning_at (loc, OPT_Wdeprecated,
24744 "dynamic exception specifications are deprecated in "
24745 "C++11");
24746 }
24747 /* In C++17, throw() is equivalent to noexcept (true). throw()
24748 is deprecated in C++11 and above as well, but is still widely used,
24749 so don't warn about it yet. */
24750 else if (cxx_dialect >= cxx17)
24751 type_id_list = noexcept_true_spec;
24752 else
24753 type_id_list = empty_except_spec;
24754
24755 /* Look for the `)'. */
24756 parens.require_close (parser);
24757
24758 return type_id_list;
24759 }
24760
24761 /* Parse an (optional) type-id-list.
24762
24763 type-id-list:
24764 type-id ... [opt]
24765 type-id-list , type-id ... [opt]
24766
24767 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24768 in the order that the types were presented. */
24769
24770 static tree
24771 cp_parser_type_id_list (cp_parser* parser)
24772 {
24773 tree types = NULL_TREE;
24774
24775 while (true)
24776 {
24777 cp_token *token;
24778 tree type;
24779
24780 token = cp_lexer_peek_token (parser->lexer);
24781
24782 /* Get the next type-id. */
24783 type = cp_parser_type_id (parser);
24784 /* Check for invalid 'auto'. */
24785 if (flag_concepts && type_uses_auto (type))
24786 {
24787 error_at (token->location,
24788 "invalid use of %<auto%> in exception-specification");
24789 type = error_mark_node;
24790 }
24791 /* Parse the optional ellipsis. */
24792 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24793 {
24794 /* Consume the `...'. */
24795 cp_lexer_consume_token (parser->lexer);
24796
24797 /* Turn the type into a pack expansion expression. */
24798 type = make_pack_expansion (type);
24799 }
24800 /* Add it to the list. */
24801 types = add_exception_specifier (types, type, /*complain=*/1);
24802 /* Peek at the next token. */
24803 token = cp_lexer_peek_token (parser->lexer);
24804 /* If it is not a `,', we are done. */
24805 if (token->type != CPP_COMMA)
24806 break;
24807 /* Consume the `,'. */
24808 cp_lexer_consume_token (parser->lexer);
24809 }
24810
24811 return nreverse (types);
24812 }
24813
24814 /* Parse a try-block.
24815
24816 try-block:
24817 try compound-statement handler-seq */
24818
24819 static tree
24820 cp_parser_try_block (cp_parser* parser)
24821 {
24822 tree try_block;
24823
24824 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24825 if (parser->in_function_body
24826 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24827 error ("%<try%> in %<constexpr%> function");
24828
24829 try_block = begin_try_block ();
24830 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24831 finish_try_block (try_block);
24832 cp_parser_handler_seq (parser);
24833 finish_handler_sequence (try_block);
24834
24835 return try_block;
24836 }
24837
24838 /* Parse a function-try-block.
24839
24840 function-try-block:
24841 try ctor-initializer [opt] function-body handler-seq */
24842
24843 static void
24844 cp_parser_function_try_block (cp_parser* parser)
24845 {
24846 tree compound_stmt;
24847 tree try_block;
24848
24849 /* Look for the `try' keyword. */
24850 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24851 return;
24852 /* Let the rest of the front end know where we are. */
24853 try_block = begin_function_try_block (&compound_stmt);
24854 /* Parse the function-body. */
24855 cp_parser_ctor_initializer_opt_and_function_body
24856 (parser, /*in_function_try_block=*/true);
24857 /* We're done with the `try' part. */
24858 finish_function_try_block (try_block);
24859 /* Parse the handlers. */
24860 cp_parser_handler_seq (parser);
24861 /* We're done with the handlers. */
24862 finish_function_handler_sequence (try_block, compound_stmt);
24863 }
24864
24865 /* Parse a handler-seq.
24866
24867 handler-seq:
24868 handler handler-seq [opt] */
24869
24870 static void
24871 cp_parser_handler_seq (cp_parser* parser)
24872 {
24873 while (true)
24874 {
24875 cp_token *token;
24876
24877 /* Parse the handler. */
24878 cp_parser_handler (parser);
24879 /* Peek at the next token. */
24880 token = cp_lexer_peek_token (parser->lexer);
24881 /* If it's not `catch' then there are no more handlers. */
24882 if (!cp_parser_is_keyword (token, RID_CATCH))
24883 break;
24884 }
24885 }
24886
24887 /* Parse a handler.
24888
24889 handler:
24890 catch ( exception-declaration ) compound-statement */
24891
24892 static void
24893 cp_parser_handler (cp_parser* parser)
24894 {
24895 tree handler;
24896 tree declaration;
24897
24898 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24899 handler = begin_handler ();
24900 matching_parens parens;
24901 parens.require_open (parser);
24902 declaration = cp_parser_exception_declaration (parser);
24903 finish_handler_parms (declaration, handler);
24904 parens.require_close (parser);
24905 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24906 finish_handler (handler);
24907 }
24908
24909 /* Parse an exception-declaration.
24910
24911 exception-declaration:
24912 type-specifier-seq declarator
24913 type-specifier-seq abstract-declarator
24914 type-specifier-seq
24915 ...
24916
24917 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24918 ellipsis variant is used. */
24919
24920 static tree
24921 cp_parser_exception_declaration (cp_parser* parser)
24922 {
24923 cp_decl_specifier_seq type_specifiers;
24924 cp_declarator *declarator;
24925 const char *saved_message;
24926
24927 /* If it's an ellipsis, it's easy to handle. */
24928 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24929 {
24930 /* Consume the `...' token. */
24931 cp_lexer_consume_token (parser->lexer);
24932 return NULL_TREE;
24933 }
24934
24935 /* Types may not be defined in exception-declarations. */
24936 saved_message = parser->type_definition_forbidden_message;
24937 parser->type_definition_forbidden_message
24938 = G_("types may not be defined in exception-declarations");
24939
24940 /* Parse the type-specifier-seq. */
24941 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24942 /*is_trailing_return=*/false,
24943 &type_specifiers);
24944 /* If it's a `)', then there is no declarator. */
24945 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24946 declarator = NULL;
24947 else
24948 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24949 /*ctor_dtor_or_conv_p=*/NULL,
24950 /*parenthesized_p=*/NULL,
24951 /*member_p=*/false,
24952 /*friend_p=*/false);
24953
24954 /* Restore the saved message. */
24955 parser->type_definition_forbidden_message = saved_message;
24956
24957 if (!type_specifiers.any_specifiers_p)
24958 return error_mark_node;
24959
24960 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24961 }
24962
24963 /* Parse a throw-expression.
24964
24965 throw-expression:
24966 throw assignment-expression [opt]
24967
24968 Returns a THROW_EXPR representing the throw-expression. */
24969
24970 static tree
24971 cp_parser_throw_expression (cp_parser* parser)
24972 {
24973 tree expression;
24974 cp_token* token;
24975
24976 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24977 token = cp_lexer_peek_token (parser->lexer);
24978 /* Figure out whether or not there is an assignment-expression
24979 following the "throw" keyword. */
24980 if (token->type == CPP_COMMA
24981 || token->type == CPP_SEMICOLON
24982 || token->type == CPP_CLOSE_PAREN
24983 || token->type == CPP_CLOSE_SQUARE
24984 || token->type == CPP_CLOSE_BRACE
24985 || token->type == CPP_COLON)
24986 expression = NULL_TREE;
24987 else
24988 expression = cp_parser_assignment_expression (parser);
24989
24990 return build_throw (expression);
24991 }
24992
24993 /* GNU Extensions */
24994
24995 /* Parse an (optional) asm-specification.
24996
24997 asm-specification:
24998 asm ( string-literal )
24999
25000 If the asm-specification is present, returns a STRING_CST
25001 corresponding to the string-literal. Otherwise, returns
25002 NULL_TREE. */
25003
25004 static tree
25005 cp_parser_asm_specification_opt (cp_parser* parser)
25006 {
25007 cp_token *token;
25008 tree asm_specification;
25009
25010 /* Peek at the next token. */
25011 token = cp_lexer_peek_token (parser->lexer);
25012 /* If the next token isn't the `asm' keyword, then there's no
25013 asm-specification. */
25014 if (!cp_parser_is_keyword (token, RID_ASM))
25015 return NULL_TREE;
25016
25017 /* Consume the `asm' token. */
25018 cp_lexer_consume_token (parser->lexer);
25019 /* Look for the `('. */
25020 matching_parens parens;
25021 parens.require_open (parser);
25022
25023 /* Look for the string-literal. */
25024 asm_specification = cp_parser_string_literal (parser, false, false);
25025
25026 /* Look for the `)'. */
25027 parens.require_close (parser);
25028
25029 return asm_specification;
25030 }
25031
25032 /* Parse an asm-operand-list.
25033
25034 asm-operand-list:
25035 asm-operand
25036 asm-operand-list , asm-operand
25037
25038 asm-operand:
25039 string-literal ( expression )
25040 [ string-literal ] string-literal ( expression )
25041
25042 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25043 each node is the expression. The TREE_PURPOSE is itself a
25044 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25045 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25046 is a STRING_CST for the string literal before the parenthesis. Returns
25047 ERROR_MARK_NODE if any of the operands are invalid. */
25048
25049 static tree
25050 cp_parser_asm_operand_list (cp_parser* parser)
25051 {
25052 tree asm_operands = NULL_TREE;
25053 bool invalid_operands = false;
25054
25055 while (true)
25056 {
25057 tree string_literal;
25058 tree expression;
25059 tree name;
25060
25061 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25062 {
25063 /* Consume the `[' token. */
25064 cp_lexer_consume_token (parser->lexer);
25065 /* Read the operand name. */
25066 name = cp_parser_identifier (parser);
25067 if (name != error_mark_node)
25068 name = build_string (IDENTIFIER_LENGTH (name),
25069 IDENTIFIER_POINTER (name));
25070 /* Look for the closing `]'. */
25071 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25072 }
25073 else
25074 name = NULL_TREE;
25075 /* Look for the string-literal. */
25076 string_literal = cp_parser_string_literal (parser, false, false);
25077
25078 /* Look for the `('. */
25079 matching_parens parens;
25080 parens.require_open (parser);
25081 /* Parse the expression. */
25082 expression = cp_parser_expression (parser);
25083 /* Look for the `)'. */
25084 parens.require_close (parser);
25085
25086 if (name == error_mark_node
25087 || string_literal == error_mark_node
25088 || expression == error_mark_node)
25089 invalid_operands = true;
25090
25091 /* Add this operand to the list. */
25092 asm_operands = tree_cons (build_tree_list (name, string_literal),
25093 expression,
25094 asm_operands);
25095 /* If the next token is not a `,', there are no more
25096 operands. */
25097 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25098 break;
25099 /* Consume the `,'. */
25100 cp_lexer_consume_token (parser->lexer);
25101 }
25102
25103 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25104 }
25105
25106 /* Parse an asm-clobber-list.
25107
25108 asm-clobber-list:
25109 string-literal
25110 asm-clobber-list , string-literal
25111
25112 Returns a TREE_LIST, indicating the clobbers in the order that they
25113 appeared. The TREE_VALUE of each node is a STRING_CST. */
25114
25115 static tree
25116 cp_parser_asm_clobber_list (cp_parser* parser)
25117 {
25118 tree clobbers = NULL_TREE;
25119
25120 while (true)
25121 {
25122 tree string_literal;
25123
25124 /* Look for the string literal. */
25125 string_literal = cp_parser_string_literal (parser, false, false);
25126 /* Add it to the list. */
25127 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25128 /* If the next token is not a `,', then the list is
25129 complete. */
25130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25131 break;
25132 /* Consume the `,' token. */
25133 cp_lexer_consume_token (parser->lexer);
25134 }
25135
25136 return clobbers;
25137 }
25138
25139 /* Parse an asm-label-list.
25140
25141 asm-label-list:
25142 identifier
25143 asm-label-list , identifier
25144
25145 Returns a TREE_LIST, indicating the labels in the order that they
25146 appeared. The TREE_VALUE of each node is a label. */
25147
25148 static tree
25149 cp_parser_asm_label_list (cp_parser* parser)
25150 {
25151 tree labels = NULL_TREE;
25152
25153 while (true)
25154 {
25155 tree identifier, label, name;
25156
25157 /* Look for the identifier. */
25158 identifier = cp_parser_identifier (parser);
25159 if (!error_operand_p (identifier))
25160 {
25161 label = lookup_label (identifier);
25162 if (TREE_CODE (label) == LABEL_DECL)
25163 {
25164 TREE_USED (label) = 1;
25165 check_goto (label);
25166 name = build_string (IDENTIFIER_LENGTH (identifier),
25167 IDENTIFIER_POINTER (identifier));
25168 labels = tree_cons (name, label, labels);
25169 }
25170 }
25171 /* If the next token is not a `,', then the list is
25172 complete. */
25173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25174 break;
25175 /* Consume the `,' token. */
25176 cp_lexer_consume_token (parser->lexer);
25177 }
25178
25179 return nreverse (labels);
25180 }
25181
25182 /* Return TRUE iff the next tokens in the stream are possibly the
25183 beginning of a GNU extension attribute. */
25184
25185 static bool
25186 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25187 {
25188 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25189 }
25190
25191 /* Return TRUE iff the next tokens in the stream are possibly the
25192 beginning of a standard C++-11 attribute specifier. */
25193
25194 static bool
25195 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25196 {
25197 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25198 }
25199
25200 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25201 beginning of a standard C++-11 attribute specifier. */
25202
25203 static bool
25204 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25205 {
25206 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25207
25208 return (cxx_dialect >= cxx11
25209 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25210 || (token->type == CPP_OPEN_SQUARE
25211 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25212 && token->type == CPP_OPEN_SQUARE)));
25213 }
25214
25215 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25216 beginning of a GNU extension attribute. */
25217
25218 static bool
25219 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25220 {
25221 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25222
25223 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25224 }
25225
25226 /* Return true iff the next tokens can be the beginning of either a
25227 GNU attribute list, or a standard C++11 attribute sequence. */
25228
25229 static bool
25230 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25231 {
25232 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25233 || cp_next_tokens_can_be_std_attribute_p (parser));
25234 }
25235
25236 /* Return true iff the next Nth tokens can be the beginning of either
25237 a GNU attribute list, or a standard C++11 attribute sequence. */
25238
25239 static bool
25240 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25241 {
25242 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25243 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25244 }
25245
25246 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25247 of GNU attributes, or return NULL. */
25248
25249 static tree
25250 cp_parser_attributes_opt (cp_parser *parser)
25251 {
25252 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25253 return cp_parser_gnu_attributes_opt (parser);
25254 return cp_parser_std_attribute_spec_seq (parser);
25255 }
25256
25257 /* Parse an (optional) series of attributes.
25258
25259 attributes:
25260 attributes attribute
25261
25262 attribute:
25263 __attribute__ (( attribute-list [opt] ))
25264
25265 The return value is as for cp_parser_gnu_attribute_list. */
25266
25267 static tree
25268 cp_parser_gnu_attributes_opt (cp_parser* parser)
25269 {
25270 tree attributes = NULL_TREE;
25271
25272 temp_override<bool> cleanup
25273 (parser->auto_is_implicit_function_template_parm_p, false);
25274
25275 while (true)
25276 {
25277 cp_token *token;
25278 tree attribute_list;
25279 bool ok = true;
25280
25281 /* Peek at the next token. */
25282 token = cp_lexer_peek_token (parser->lexer);
25283 /* If it's not `__attribute__', then we're done. */
25284 if (token->keyword != RID_ATTRIBUTE)
25285 break;
25286
25287 /* Consume the `__attribute__' keyword. */
25288 cp_lexer_consume_token (parser->lexer);
25289 /* Look for the two `(' tokens. */
25290 matching_parens outer_parens;
25291 outer_parens.require_open (parser);
25292 matching_parens inner_parens;
25293 inner_parens.require_open (parser);
25294
25295 /* Peek at the next token. */
25296 token = cp_lexer_peek_token (parser->lexer);
25297 if (token->type != CPP_CLOSE_PAREN)
25298 /* Parse the attribute-list. */
25299 attribute_list = cp_parser_gnu_attribute_list (parser);
25300 else
25301 /* If the next token is a `)', then there is no attribute
25302 list. */
25303 attribute_list = NULL;
25304
25305 /* Look for the two `)' tokens. */
25306 if (!inner_parens.require_close (parser))
25307 ok = false;
25308 if (!outer_parens.require_close (parser))
25309 ok = false;
25310 if (!ok)
25311 cp_parser_skip_to_end_of_statement (parser);
25312
25313 /* Add these new attributes to the list. */
25314 attributes = attr_chainon (attributes, attribute_list);
25315 }
25316
25317 return attributes;
25318 }
25319
25320 /* Parse a GNU attribute-list.
25321
25322 attribute-list:
25323 attribute
25324 attribute-list , attribute
25325
25326 attribute:
25327 identifier
25328 identifier ( identifier )
25329 identifier ( identifier , expression-list )
25330 identifier ( expression-list )
25331
25332 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25333 to an attribute. The TREE_PURPOSE of each node is the identifier
25334 indicating which attribute is in use. The TREE_VALUE represents
25335 the arguments, if any. */
25336
25337 static tree
25338 cp_parser_gnu_attribute_list (cp_parser* parser)
25339 {
25340 tree attribute_list = NULL_TREE;
25341 bool save_translate_strings_p = parser->translate_strings_p;
25342
25343 parser->translate_strings_p = false;
25344 while (true)
25345 {
25346 cp_token *token;
25347 tree identifier;
25348 tree attribute;
25349
25350 /* Look for the identifier. We also allow keywords here; for
25351 example `__attribute__ ((const))' is legal. */
25352 token = cp_lexer_peek_token (parser->lexer);
25353 if (token->type == CPP_NAME
25354 || token->type == CPP_KEYWORD)
25355 {
25356 tree arguments = NULL_TREE;
25357
25358 /* Consume the token, but save it since we need it for the
25359 SIMD enabled function parsing. */
25360 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25361
25362 /* Save away the identifier that indicates which attribute
25363 this is. */
25364 identifier = (token->type == CPP_KEYWORD)
25365 /* For keywords, use the canonical spelling, not the
25366 parsed identifier. */
25367 ? ridpointers[(int) token->keyword]
25368 : id_token->u.value;
25369
25370 identifier = canonicalize_attr_name (identifier);
25371 attribute = build_tree_list (identifier, NULL_TREE);
25372
25373 /* Peek at the next token. */
25374 token = cp_lexer_peek_token (parser->lexer);
25375 /* If it's an `(', then parse the attribute arguments. */
25376 if (token->type == CPP_OPEN_PAREN)
25377 {
25378 vec<tree, va_gc> *vec;
25379 int attr_flag = (attribute_takes_identifier_p (identifier)
25380 ? id_attr : normal_attr);
25381 vec = cp_parser_parenthesized_expression_list
25382 (parser, attr_flag, /*cast_p=*/false,
25383 /*allow_expansion_p=*/false,
25384 /*non_constant_p=*/NULL);
25385 if (vec == NULL)
25386 arguments = error_mark_node;
25387 else
25388 {
25389 arguments = build_tree_list_vec (vec);
25390 release_tree_vector (vec);
25391 }
25392 /* Save the arguments away. */
25393 TREE_VALUE (attribute) = arguments;
25394 }
25395
25396 if (arguments != error_mark_node)
25397 {
25398 /* Add this attribute to the list. */
25399 TREE_CHAIN (attribute) = attribute_list;
25400 attribute_list = attribute;
25401 }
25402
25403 token = cp_lexer_peek_token (parser->lexer);
25404 }
25405 /* Now, look for more attributes. If the next token isn't a
25406 `,', we're done. */
25407 if (token->type != CPP_COMMA)
25408 break;
25409
25410 /* Consume the comma and keep going. */
25411 cp_lexer_consume_token (parser->lexer);
25412 }
25413 parser->translate_strings_p = save_translate_strings_p;
25414
25415 /* We built up the list in reverse order. */
25416 return nreverse (attribute_list);
25417 }
25418
25419 /* Parse a standard C++11 attribute.
25420
25421 The returned representation is a TREE_LIST which TREE_PURPOSE is
25422 the scoped name of the attribute, and the TREE_VALUE is its
25423 arguments list.
25424
25425 Note that the scoped name of the attribute is itself a TREE_LIST
25426 which TREE_PURPOSE is the namespace of the attribute, and
25427 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25428 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25429 and which TREE_PURPOSE is directly the attribute name.
25430
25431 Clients of the attribute code should use get_attribute_namespace
25432 and get_attribute_name to get the actual namespace and name of
25433 attributes, regardless of their being GNU or C++11 attributes.
25434
25435 attribute:
25436 attribute-token attribute-argument-clause [opt]
25437
25438 attribute-token:
25439 identifier
25440 attribute-scoped-token
25441
25442 attribute-scoped-token:
25443 attribute-namespace :: identifier
25444
25445 attribute-namespace:
25446 identifier
25447
25448 attribute-argument-clause:
25449 ( balanced-token-seq )
25450
25451 balanced-token-seq:
25452 balanced-token [opt]
25453 balanced-token-seq balanced-token
25454
25455 balanced-token:
25456 ( balanced-token-seq )
25457 [ balanced-token-seq ]
25458 { balanced-token-seq }. */
25459
25460 static tree
25461 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25462 {
25463 tree attribute, attr_id = NULL_TREE, arguments;
25464 cp_token *token;
25465
25466 temp_override<bool> cleanup
25467 (parser->auto_is_implicit_function_template_parm_p, false);
25468
25469 /* First, parse name of the attribute, a.k.a attribute-token. */
25470
25471 token = cp_lexer_peek_token (parser->lexer);
25472 if (token->type == CPP_NAME)
25473 attr_id = token->u.value;
25474 else if (token->type == CPP_KEYWORD)
25475 attr_id = ridpointers[(int) token->keyword];
25476 else if (token->flags & NAMED_OP)
25477 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25478
25479 if (attr_id == NULL_TREE)
25480 return NULL_TREE;
25481
25482 cp_lexer_consume_token (parser->lexer);
25483
25484 token = cp_lexer_peek_token (parser->lexer);
25485 if (token->type == CPP_SCOPE)
25486 {
25487 /* We are seeing a scoped attribute token. */
25488
25489 cp_lexer_consume_token (parser->lexer);
25490 if (attr_ns)
25491 error_at (token->location, "attribute using prefix used together "
25492 "with scoped attribute token");
25493 attr_ns = attr_id;
25494
25495 token = cp_lexer_consume_token (parser->lexer);
25496 if (token->type == CPP_NAME)
25497 attr_id = token->u.value;
25498 else if (token->type == CPP_KEYWORD)
25499 attr_id = ridpointers[(int) token->keyword];
25500 else if (token->flags & NAMED_OP)
25501 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25502 else
25503 {
25504 error_at (token->location,
25505 "expected an identifier for the attribute name");
25506 return error_mark_node;
25507 }
25508
25509 attr_ns = canonicalize_attr_name (attr_ns);
25510 attr_id = canonicalize_attr_name (attr_id);
25511 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25512 NULL_TREE);
25513 token = cp_lexer_peek_token (parser->lexer);
25514 }
25515 else if (attr_ns)
25516 {
25517 attr_ns = canonicalize_attr_name (attr_ns);
25518 attr_id = canonicalize_attr_name (attr_id);
25519 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25520 NULL_TREE);
25521 }
25522 else
25523 {
25524 attr_id = canonicalize_attr_name (attr_id);
25525 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25526 NULL_TREE);
25527 /* C++11 noreturn attribute is equivalent to GNU's. */
25528 if (is_attribute_p ("noreturn", attr_id))
25529 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25530 /* C++14 deprecated attribute is equivalent to GNU's. */
25531 else if (is_attribute_p ("deprecated", attr_id))
25532 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25533 /* C++17 fallthrough attribute is equivalent to GNU's. */
25534 else if (is_attribute_p ("fallthrough", attr_id))
25535 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25536 /* Transactional Memory TS optimize_for_synchronized attribute is
25537 equivalent to GNU transaction_callable. */
25538 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25539 TREE_PURPOSE (attribute)
25540 = get_identifier ("transaction_callable");
25541 /* Transactional Memory attributes are GNU attributes. */
25542 else if (tm_attr_to_mask (attr_id))
25543 TREE_PURPOSE (attribute) = attr_id;
25544 }
25545
25546 /* Now parse the optional argument clause of the attribute. */
25547
25548 if (token->type != CPP_OPEN_PAREN)
25549 return attribute;
25550
25551 {
25552 vec<tree, va_gc> *vec;
25553 int attr_flag = normal_attr;
25554
25555 if (attr_ns == gnu_identifier
25556 && attribute_takes_identifier_p (attr_id))
25557 /* A GNU attribute that takes an identifier in parameter. */
25558 attr_flag = id_attr;
25559
25560 vec = cp_parser_parenthesized_expression_list
25561 (parser, attr_flag, /*cast_p=*/false,
25562 /*allow_expansion_p=*/true,
25563 /*non_constant_p=*/NULL);
25564 if (vec == NULL)
25565 arguments = error_mark_node;
25566 else
25567 {
25568 arguments = build_tree_list_vec (vec);
25569 release_tree_vector (vec);
25570 }
25571
25572 if (arguments == error_mark_node)
25573 attribute = error_mark_node;
25574 else
25575 TREE_VALUE (attribute) = arguments;
25576 }
25577
25578 return attribute;
25579 }
25580
25581 /* Check that the attribute ATTRIBUTE appears at most once in the
25582 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25583 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25584 isn't implemented yet in GCC. */
25585
25586 static void
25587 cp_parser_check_std_attribute (tree attributes, tree attribute)
25588 {
25589 if (attributes)
25590 {
25591 tree name = get_attribute_name (attribute);
25592 if (is_attribute_p ("noreturn", name)
25593 && lookup_attribute ("noreturn", attributes))
25594 error ("attribute %<noreturn%> can appear at most once "
25595 "in an attribute-list");
25596 else if (is_attribute_p ("deprecated", name)
25597 && lookup_attribute ("deprecated", attributes))
25598 error ("attribute %<deprecated%> can appear at most once "
25599 "in an attribute-list");
25600 }
25601 }
25602
25603 /* Parse a list of standard C++-11 attributes.
25604
25605 attribute-list:
25606 attribute [opt]
25607 attribute-list , attribute[opt]
25608 attribute ...
25609 attribute-list , attribute ...
25610 */
25611
25612 static tree
25613 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25614 {
25615 tree attributes = NULL_TREE, attribute = NULL_TREE;
25616 cp_token *token = NULL;
25617
25618 while (true)
25619 {
25620 attribute = cp_parser_std_attribute (parser, attr_ns);
25621 if (attribute == error_mark_node)
25622 break;
25623 if (attribute != NULL_TREE)
25624 {
25625 cp_parser_check_std_attribute (attributes, attribute);
25626 TREE_CHAIN (attribute) = attributes;
25627 attributes = attribute;
25628 }
25629 token = cp_lexer_peek_token (parser->lexer);
25630 if (token->type == CPP_ELLIPSIS)
25631 {
25632 cp_lexer_consume_token (parser->lexer);
25633 if (attribute == NULL_TREE)
25634 error_at (token->location,
25635 "expected attribute before %<...%>");
25636 else
25637 {
25638 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25639 if (pack == error_mark_node)
25640 return error_mark_node;
25641 TREE_VALUE (attribute) = pack;
25642 }
25643 token = cp_lexer_peek_token (parser->lexer);
25644 }
25645 if (token->type != CPP_COMMA)
25646 break;
25647 cp_lexer_consume_token (parser->lexer);
25648 }
25649 attributes = nreverse (attributes);
25650 return attributes;
25651 }
25652
25653 /* Parse a standard C++-11 attribute specifier.
25654
25655 attribute-specifier:
25656 [ [ attribute-using-prefix [opt] attribute-list ] ]
25657 alignment-specifier
25658
25659 attribute-using-prefix:
25660 using attribute-namespace :
25661
25662 alignment-specifier:
25663 alignas ( type-id ... [opt] )
25664 alignas ( alignment-expression ... [opt] ). */
25665
25666 static tree
25667 cp_parser_std_attribute_spec (cp_parser *parser)
25668 {
25669 tree attributes = NULL_TREE;
25670 cp_token *token = cp_lexer_peek_token (parser->lexer);
25671
25672 if (token->type == CPP_OPEN_SQUARE
25673 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25674 {
25675 tree attr_ns = NULL_TREE;
25676
25677 cp_lexer_consume_token (parser->lexer);
25678 cp_lexer_consume_token (parser->lexer);
25679
25680 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25681 {
25682 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25683 if (token->type == CPP_NAME)
25684 attr_ns = token->u.value;
25685 else if (token->type == CPP_KEYWORD)
25686 attr_ns = ridpointers[(int) token->keyword];
25687 else if (token->flags & NAMED_OP)
25688 attr_ns = get_identifier (cpp_type2name (token->type,
25689 token->flags));
25690 if (attr_ns
25691 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25692 {
25693 if (cxx_dialect < cxx17
25694 && !in_system_header_at (input_location))
25695 pedwarn (input_location, 0,
25696 "attribute using prefix only available "
25697 "with -std=c++17 or -std=gnu++17");
25698
25699 cp_lexer_consume_token (parser->lexer);
25700 cp_lexer_consume_token (parser->lexer);
25701 cp_lexer_consume_token (parser->lexer);
25702 }
25703 else
25704 attr_ns = NULL_TREE;
25705 }
25706
25707 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25708
25709 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25710 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25711 cp_parser_skip_to_end_of_statement (parser);
25712 else
25713 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25714 when we are sure that we have actually parsed them. */
25715 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25716 }
25717 else
25718 {
25719 tree alignas_expr;
25720
25721 /* Look for an alignment-specifier. */
25722
25723 token = cp_lexer_peek_token (parser->lexer);
25724
25725 if (token->type != CPP_KEYWORD
25726 || token->keyword != RID_ALIGNAS)
25727 return NULL_TREE;
25728
25729 cp_lexer_consume_token (parser->lexer);
25730 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25731
25732 matching_parens parens;
25733 if (!parens.require_open (parser))
25734 return error_mark_node;
25735
25736 cp_parser_parse_tentatively (parser);
25737 alignas_expr = cp_parser_type_id (parser);
25738
25739 if (!cp_parser_parse_definitely (parser))
25740 {
25741 alignas_expr = cp_parser_assignment_expression (parser);
25742 if (alignas_expr == error_mark_node)
25743 cp_parser_skip_to_end_of_statement (parser);
25744 if (alignas_expr == NULL_TREE
25745 || alignas_expr == error_mark_node)
25746 return alignas_expr;
25747 }
25748
25749 alignas_expr = cxx_alignas_expr (alignas_expr);
25750 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25751
25752 /* Handle alignas (pack...). */
25753 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25754 {
25755 cp_lexer_consume_token (parser->lexer);
25756 alignas_expr = make_pack_expansion (alignas_expr);
25757 }
25758
25759 /* Something went wrong, so don't build the attribute. */
25760 if (alignas_expr == error_mark_node)
25761 return error_mark_node;
25762
25763 if (!parens.require_close (parser))
25764 return error_mark_node;
25765
25766 /* Build the C++-11 representation of an 'aligned'
25767 attribute. */
25768 attributes
25769 = build_tree_list (build_tree_list (gnu_identifier,
25770 aligned_identifier), alignas_expr);
25771 }
25772
25773 return attributes;
25774 }
25775
25776 /* Parse a standard C++-11 attribute-specifier-seq.
25777
25778 attribute-specifier-seq:
25779 attribute-specifier-seq [opt] attribute-specifier
25780 */
25781
25782 static tree
25783 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25784 {
25785 tree attr_specs = NULL_TREE;
25786 tree attr_last = NULL_TREE;
25787
25788 while (true)
25789 {
25790 tree attr_spec = cp_parser_std_attribute_spec (parser);
25791 if (attr_spec == NULL_TREE)
25792 break;
25793 if (attr_spec == error_mark_node)
25794 return error_mark_node;
25795
25796 if (attr_last)
25797 TREE_CHAIN (attr_last) = attr_spec;
25798 else
25799 attr_specs = attr_last = attr_spec;
25800 attr_last = tree_last (attr_last);
25801 }
25802
25803 return attr_specs;
25804 }
25805
25806 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25807 return index of the first token after balanced-token, or N on failure. */
25808
25809 static size_t
25810 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25811 {
25812 size_t orig_n = n;
25813 int nparens = 0, nbraces = 0, nsquares = 0;
25814 do
25815 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25816 {
25817 case CPP_EOF:
25818 case CPP_PRAGMA_EOL:
25819 /* Ran out of tokens. */
25820 return orig_n;
25821 case CPP_OPEN_PAREN:
25822 ++nparens;
25823 break;
25824 case CPP_OPEN_BRACE:
25825 ++nbraces;
25826 break;
25827 case CPP_OPEN_SQUARE:
25828 ++nsquares;
25829 break;
25830 case CPP_CLOSE_PAREN:
25831 --nparens;
25832 break;
25833 case CPP_CLOSE_BRACE:
25834 --nbraces;
25835 break;
25836 case CPP_CLOSE_SQUARE:
25837 --nsquares;
25838 break;
25839 default:
25840 break;
25841 }
25842 while (nparens || nbraces || nsquares);
25843 return n;
25844 }
25845
25846 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25847 return index of the first token after the GNU attribute tokens, or N on
25848 failure. */
25849
25850 static size_t
25851 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25852 {
25853 while (true)
25854 {
25855 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25856 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25857 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25858 break;
25859
25860 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25861 if (n2 == n + 2)
25862 break;
25863 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25864 break;
25865 n = n2 + 1;
25866 }
25867 return n;
25868 }
25869
25870 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25871 next token), return index of the first token after the standard C++11
25872 attribute tokens, or N on failure. */
25873
25874 static size_t
25875 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25876 {
25877 while (true)
25878 {
25879 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25880 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25881 {
25882 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25883 if (n2 == n + 1)
25884 break;
25885 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25886 break;
25887 n = n2 + 1;
25888 }
25889 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25890 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25891 {
25892 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25893 if (n2 == n + 1)
25894 break;
25895 n = n2;
25896 }
25897 else
25898 break;
25899 }
25900 return n;
25901 }
25902
25903 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25904 as the next token), return index of the first token after the attribute
25905 tokens, or N on failure. */
25906
25907 static size_t
25908 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25909 {
25910 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25911 return cp_parser_skip_gnu_attributes_opt (parser, n);
25912 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25913 }
25914
25915 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25916 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25917 current value of the PEDANTIC flag, regardless of whether or not
25918 the `__extension__' keyword is present. The caller is responsible
25919 for restoring the value of the PEDANTIC flag. */
25920
25921 static bool
25922 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25923 {
25924 /* Save the old value of the PEDANTIC flag. */
25925 *saved_pedantic = pedantic;
25926
25927 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25928 {
25929 /* Consume the `__extension__' token. */
25930 cp_lexer_consume_token (parser->lexer);
25931 /* We're not being pedantic while the `__extension__' keyword is
25932 in effect. */
25933 pedantic = 0;
25934
25935 return true;
25936 }
25937
25938 return false;
25939 }
25940
25941 /* Parse a label declaration.
25942
25943 label-declaration:
25944 __label__ label-declarator-seq ;
25945
25946 label-declarator-seq:
25947 identifier , label-declarator-seq
25948 identifier */
25949
25950 static void
25951 cp_parser_label_declaration (cp_parser* parser)
25952 {
25953 /* Look for the `__label__' keyword. */
25954 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25955
25956 while (true)
25957 {
25958 tree identifier;
25959
25960 /* Look for an identifier. */
25961 identifier = cp_parser_identifier (parser);
25962 /* If we failed, stop. */
25963 if (identifier == error_mark_node)
25964 break;
25965 /* Declare it as a label. */
25966 finish_label_decl (identifier);
25967 /* If the next token is a `;', stop. */
25968 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25969 break;
25970 /* Look for the `,' separating the label declarations. */
25971 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25972 }
25973
25974 /* Look for the final `;'. */
25975 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25976 }
25977
25978 // -------------------------------------------------------------------------- //
25979 // Requires Clause
25980
25981 // Parse a requires clause.
25982 //
25983 // requires-clause:
25984 // 'requires' logical-or-expression
25985 //
25986 // The required logical-or-expression must be a constant expression. Note
25987 // that we don't check that the expression is constepxr here. We defer until
25988 // we analyze constraints and then, we only check atomic constraints.
25989 static tree
25990 cp_parser_requires_clause (cp_parser *parser)
25991 {
25992 // Parse the requires clause so that it is not automatically folded.
25993 ++processing_template_decl;
25994 tree expr = cp_parser_binary_expression (parser, false, false,
25995 PREC_NOT_OPERATOR, NULL);
25996 if (check_for_bare_parameter_packs (expr))
25997 expr = error_mark_node;
25998 --processing_template_decl;
25999 return expr;
26000 }
26001
26002 // Optionally parse a requires clause:
26003 static tree
26004 cp_parser_requires_clause_opt (cp_parser *parser)
26005 {
26006 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26007 if (tok->keyword != RID_REQUIRES)
26008 {
26009 if (!flag_concepts && tok->type == CPP_NAME
26010 && tok->u.value == ridpointers[RID_REQUIRES])
26011 {
26012 error_at (cp_lexer_peek_token (parser->lexer)->location,
26013 "%<requires%> only available with -fconcepts");
26014 /* Parse and discard the requires-clause. */
26015 cp_lexer_consume_token (parser->lexer);
26016 cp_parser_requires_clause (parser);
26017 }
26018 return NULL_TREE;
26019 }
26020 cp_lexer_consume_token (parser->lexer);
26021 return cp_parser_requires_clause (parser);
26022 }
26023
26024
26025 /*---------------------------------------------------------------------------
26026 Requires expressions
26027 ---------------------------------------------------------------------------*/
26028
26029 /* Parse a requires expression
26030
26031 requirement-expression:
26032 'requires' requirement-parameter-list [opt] requirement-body */
26033 static tree
26034 cp_parser_requires_expression (cp_parser *parser)
26035 {
26036 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26037 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26038
26039 /* A requires-expression shall appear only within a concept
26040 definition or a requires-clause.
26041
26042 TODO: Implement this diagnostic correctly. */
26043 if (!processing_template_decl)
26044 {
26045 error_at (loc, "a requires expression cannot appear outside a template");
26046 cp_parser_skip_to_end_of_statement (parser);
26047 return error_mark_node;
26048 }
26049
26050 tree parms, reqs;
26051 {
26052 /* Local parameters are delared as variables within the scope
26053 of the expression. They are not visible past the end of
26054 the expression. Expressions within the requires-expression
26055 are unevaluated. */
26056 struct scope_sentinel
26057 {
26058 scope_sentinel ()
26059 {
26060 ++cp_unevaluated_operand;
26061 begin_scope (sk_block, NULL_TREE);
26062 }
26063
26064 ~scope_sentinel ()
26065 {
26066 pop_bindings_and_leave_scope ();
26067 --cp_unevaluated_operand;
26068 }
26069 } s;
26070
26071 /* Parse the optional parameter list. */
26072 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26073 {
26074 parms = cp_parser_requirement_parameter_list (parser);
26075 if (parms == error_mark_node)
26076 return error_mark_node;
26077 }
26078 else
26079 parms = NULL_TREE;
26080
26081 /* Parse the requirement body. */
26082 reqs = cp_parser_requirement_body (parser);
26083 if (reqs == error_mark_node)
26084 return error_mark_node;
26085 }
26086
26087 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26088 the parm chain. */
26089 grokparms (parms, &parms);
26090 return finish_requires_expr (parms, reqs);
26091 }
26092
26093 /* Parse a parameterized requirement.
26094
26095 requirement-parameter-list:
26096 '(' parameter-declaration-clause ')' */
26097 static tree
26098 cp_parser_requirement_parameter_list (cp_parser *parser)
26099 {
26100 matching_parens parens;
26101 if (!parens.require_open (parser))
26102 return error_mark_node;
26103
26104 tree parms = cp_parser_parameter_declaration_clause (parser);
26105
26106 if (!parens.require_close (parser))
26107 return error_mark_node;
26108
26109 return parms;
26110 }
26111
26112 /* Parse the body of a requirement.
26113
26114 requirement-body:
26115 '{' requirement-list '}' */
26116 static tree
26117 cp_parser_requirement_body (cp_parser *parser)
26118 {
26119 matching_braces braces;
26120 if (!braces.require_open (parser))
26121 return error_mark_node;
26122
26123 tree reqs = cp_parser_requirement_list (parser);
26124
26125 if (!braces.require_close (parser))
26126 return error_mark_node;
26127
26128 return reqs;
26129 }
26130
26131 /* Parse a list of requirements.
26132
26133 requirement-list:
26134 requirement
26135 requirement-list ';' requirement[opt] */
26136 static tree
26137 cp_parser_requirement_list (cp_parser *parser)
26138 {
26139 tree result = NULL_TREE;
26140 while (true)
26141 {
26142 tree req = cp_parser_requirement (parser);
26143 if (req == error_mark_node)
26144 return error_mark_node;
26145
26146 result = tree_cons (NULL_TREE, req, result);
26147
26148 /* If we see a semi-colon, consume it. */
26149 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26150 cp_lexer_consume_token (parser->lexer);
26151
26152 /* Stop processing at the end of the list. */
26153 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26154 break;
26155 }
26156
26157 /* Reverse the order of requirements so they are analyzed in
26158 declaration order. */
26159 return nreverse (result);
26160 }
26161
26162 /* Parse a syntactic requirement or type requirement.
26163
26164 requirement:
26165 simple-requirement
26166 compound-requirement
26167 type-requirement
26168 nested-requirement */
26169 static tree
26170 cp_parser_requirement (cp_parser *parser)
26171 {
26172 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26173 return cp_parser_compound_requirement (parser);
26174 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26175 return cp_parser_type_requirement (parser);
26176 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26177 return cp_parser_nested_requirement (parser);
26178 else
26179 return cp_parser_simple_requirement (parser);
26180 }
26181
26182 /* Parse a simple requirement.
26183
26184 simple-requirement:
26185 expression ';' */
26186 static tree
26187 cp_parser_simple_requirement (cp_parser *parser)
26188 {
26189 tree expr = cp_parser_expression (parser, NULL, false, false);
26190 if (!expr || expr == error_mark_node)
26191 return error_mark_node;
26192
26193 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26194 return error_mark_node;
26195
26196 return finish_simple_requirement (expr);
26197 }
26198
26199 /* Parse a type requirement
26200
26201 type-requirement
26202 nested-name-specifier [opt] required-type-name ';'
26203
26204 required-type-name:
26205 type-name
26206 'template' [opt] simple-template-id */
26207 static tree
26208 cp_parser_type_requirement (cp_parser *parser)
26209 {
26210 cp_lexer_consume_token (parser->lexer);
26211
26212 // Save the scope before parsing name specifiers.
26213 tree saved_scope = parser->scope;
26214 tree saved_object_scope = parser->object_scope;
26215 tree saved_qualifying_scope = parser->qualifying_scope;
26216 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26217 cp_parser_nested_name_specifier_opt (parser,
26218 /*typename_keyword_p=*/true,
26219 /*check_dependency_p=*/false,
26220 /*type_p=*/true,
26221 /*is_declaration=*/false);
26222
26223 tree type;
26224 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26225 {
26226 cp_lexer_consume_token (parser->lexer);
26227 type = cp_parser_template_id (parser,
26228 /*template_keyword_p=*/true,
26229 /*check_dependency=*/false,
26230 /*tag_type=*/none_type,
26231 /*is_declaration=*/false);
26232 type = make_typename_type (parser->scope, type, typename_type,
26233 /*complain=*/tf_error);
26234 }
26235 else
26236 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26237
26238 if (TREE_CODE (type) == TYPE_DECL)
26239 type = TREE_TYPE (type);
26240
26241 parser->scope = saved_scope;
26242 parser->object_scope = saved_object_scope;
26243 parser->qualifying_scope = saved_qualifying_scope;
26244
26245 if (type == error_mark_node)
26246 cp_parser_skip_to_end_of_statement (parser);
26247
26248 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26249 return error_mark_node;
26250 if (type == error_mark_node)
26251 return error_mark_node;
26252
26253 return finish_type_requirement (type);
26254 }
26255
26256 /* Parse a compound requirement
26257
26258 compound-requirement:
26259 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26260 static tree
26261 cp_parser_compound_requirement (cp_parser *parser)
26262 {
26263 /* Parse an expression enclosed in '{ }'s. */
26264 matching_braces braces;
26265 if (!braces.require_open (parser))
26266 return error_mark_node;
26267
26268 tree expr = cp_parser_expression (parser, NULL, false, false);
26269 if (!expr || expr == error_mark_node)
26270 return error_mark_node;
26271
26272 if (!braces.require_close (parser))
26273 return error_mark_node;
26274
26275 /* Parse the optional noexcept. */
26276 bool noexcept_p = false;
26277 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26278 {
26279 cp_lexer_consume_token (parser->lexer);
26280 noexcept_p = true;
26281 }
26282
26283 /* Parse the optional trailing return type. */
26284 tree type = NULL_TREE;
26285 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26286 {
26287 cp_lexer_consume_token (parser->lexer);
26288 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26289 parser->in_result_type_constraint_p = true;
26290 type = cp_parser_trailing_type_id (parser);
26291 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26292 if (type == error_mark_node)
26293 return error_mark_node;
26294 }
26295
26296 return finish_compound_requirement (expr, type, noexcept_p);
26297 }
26298
26299 /* Parse a nested requirement. This is the same as a requires clause.
26300
26301 nested-requirement:
26302 requires-clause */
26303 static tree
26304 cp_parser_nested_requirement (cp_parser *parser)
26305 {
26306 cp_lexer_consume_token (parser->lexer);
26307 tree req = cp_parser_requires_clause (parser);
26308 if (req == error_mark_node)
26309 return error_mark_node;
26310 return finish_nested_requirement (req);
26311 }
26312
26313 /* Support Functions */
26314
26315 /* Return the appropriate prefer_type argument for lookup_name_real based on
26316 tag_type and template_mem_access. */
26317
26318 static inline int
26319 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26320 {
26321 /* DR 141: When looking in the current enclosing context for a template-name
26322 after -> or ., only consider class templates. */
26323 if (template_mem_access)
26324 return 2;
26325 switch (tag_type)
26326 {
26327 case none_type: return 0; // No preference.
26328 case scope_type: return 1; // Type or namespace.
26329 default: return 2; // Type only.
26330 }
26331 }
26332
26333 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26334 NAME should have one of the representations used for an
26335 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26336 is returned. If PARSER->SCOPE is a dependent type, then a
26337 SCOPE_REF is returned.
26338
26339 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26340 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26341 was formed. Abstractly, such entities should not be passed to this
26342 function, because they do not need to be looked up, but it is
26343 simpler to check for this special case here, rather than at the
26344 call-sites.
26345
26346 In cases not explicitly covered above, this function returns a
26347 DECL, OVERLOAD, or baselink representing the result of the lookup.
26348 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26349 is returned.
26350
26351 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26352 (e.g., "struct") that was used. In that case bindings that do not
26353 refer to types are ignored.
26354
26355 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26356 ignored.
26357
26358 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26359 are ignored.
26360
26361 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26362 types.
26363
26364 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26365 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26366 NULL_TREE otherwise. */
26367
26368 static cp_expr
26369 cp_parser_lookup_name (cp_parser *parser, tree name,
26370 enum tag_types tag_type,
26371 bool is_template,
26372 bool is_namespace,
26373 bool check_dependency,
26374 tree *ambiguous_decls,
26375 location_t name_location)
26376 {
26377 tree decl;
26378 tree object_type = parser->context->object_type;
26379
26380 /* Assume that the lookup will be unambiguous. */
26381 if (ambiguous_decls)
26382 *ambiguous_decls = NULL_TREE;
26383
26384 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26385 no longer valid. Note that if we are parsing tentatively, and
26386 the parse fails, OBJECT_TYPE will be automatically restored. */
26387 parser->context->object_type = NULL_TREE;
26388
26389 if (name == error_mark_node)
26390 return error_mark_node;
26391
26392 /* A template-id has already been resolved; there is no lookup to
26393 do. */
26394 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26395 return name;
26396 if (BASELINK_P (name))
26397 {
26398 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26399 == TEMPLATE_ID_EXPR);
26400 return name;
26401 }
26402
26403 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26404 it should already have been checked to make sure that the name
26405 used matches the type being destroyed. */
26406 if (TREE_CODE (name) == BIT_NOT_EXPR)
26407 {
26408 tree type;
26409
26410 /* Figure out to which type this destructor applies. */
26411 if (parser->scope)
26412 type = parser->scope;
26413 else if (object_type)
26414 type = object_type;
26415 else
26416 type = current_class_type;
26417 /* If that's not a class type, there is no destructor. */
26418 if (!type || !CLASS_TYPE_P (type))
26419 return error_mark_node;
26420
26421 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26422 lazily_declare_fn (sfk_destructor, type);
26423
26424 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26425 return dtor;
26426
26427 return error_mark_node;
26428 }
26429
26430 /* By this point, the NAME should be an ordinary identifier. If
26431 the id-expression was a qualified name, the qualifying scope is
26432 stored in PARSER->SCOPE at this point. */
26433 gcc_assert (identifier_p (name));
26434
26435 /* Perform the lookup. */
26436 if (parser->scope)
26437 {
26438 bool dependent_p;
26439
26440 if (parser->scope == error_mark_node)
26441 return error_mark_node;
26442
26443 /* If the SCOPE is dependent, the lookup must be deferred until
26444 the template is instantiated -- unless we are explicitly
26445 looking up names in uninstantiated templates. Even then, we
26446 cannot look up the name if the scope is not a class type; it
26447 might, for example, be a template type parameter. */
26448 dependent_p = (TYPE_P (parser->scope)
26449 && dependent_scope_p (parser->scope));
26450 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26451 && dependent_p)
26452 /* Defer lookup. */
26453 decl = error_mark_node;
26454 else
26455 {
26456 tree pushed_scope = NULL_TREE;
26457
26458 /* If PARSER->SCOPE is a dependent type, then it must be a
26459 class type, and we must not be checking dependencies;
26460 otherwise, we would have processed this lookup above. So
26461 that PARSER->SCOPE is not considered a dependent base by
26462 lookup_member, we must enter the scope here. */
26463 if (dependent_p)
26464 pushed_scope = push_scope (parser->scope);
26465
26466 /* If the PARSER->SCOPE is a template specialization, it
26467 may be instantiated during name lookup. In that case,
26468 errors may be issued. Even if we rollback the current
26469 tentative parse, those errors are valid. */
26470 decl = lookup_qualified_name (parser->scope, name,
26471 prefer_type_arg (tag_type),
26472 /*complain=*/true);
26473
26474 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26475 lookup result and the nested-name-specifier nominates a class C:
26476 * if the name specified after the nested-name-specifier, when
26477 looked up in C, is the injected-class-name of C (Clause 9), or
26478 * if the name specified after the nested-name-specifier is the
26479 same as the identifier or the simple-template-id's template-
26480 name in the last component of the nested-name-specifier,
26481 the name is instead considered to name the constructor of
26482 class C. [ Note: for example, the constructor is not an
26483 acceptable lookup result in an elaborated-type-specifier so
26484 the constructor would not be used in place of the
26485 injected-class-name. --end note ] Such a constructor name
26486 shall be used only in the declarator-id of a declaration that
26487 names a constructor or in a using-declaration. */
26488 if (tag_type == none_type
26489 && DECL_SELF_REFERENCE_P (decl)
26490 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26491 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26492 prefer_type_arg (tag_type),
26493 /*complain=*/true);
26494
26495 /* If we have a single function from a using decl, pull it out. */
26496 if (TREE_CODE (decl) == OVERLOAD
26497 && !really_overloaded_fn (decl))
26498 decl = OVL_FUNCTION (decl);
26499
26500 if (pushed_scope)
26501 pop_scope (pushed_scope);
26502 }
26503
26504 /* If the scope is a dependent type and either we deferred lookup or
26505 we did lookup but didn't find the name, rememeber the name. */
26506 if (decl == error_mark_node && TYPE_P (parser->scope)
26507 && dependent_type_p (parser->scope))
26508 {
26509 if (tag_type)
26510 {
26511 tree type;
26512
26513 /* The resolution to Core Issue 180 says that `struct
26514 A::B' should be considered a type-name, even if `A'
26515 is dependent. */
26516 type = make_typename_type (parser->scope, name, tag_type,
26517 /*complain=*/tf_error);
26518 if (type != error_mark_node)
26519 decl = TYPE_NAME (type);
26520 }
26521 else if (is_template
26522 && (cp_parser_next_token_ends_template_argument_p (parser)
26523 || cp_lexer_next_token_is (parser->lexer,
26524 CPP_CLOSE_PAREN)))
26525 decl = make_unbound_class_template (parser->scope,
26526 name, NULL_TREE,
26527 /*complain=*/tf_error);
26528 else
26529 decl = build_qualified_name (/*type=*/NULL_TREE,
26530 parser->scope, name,
26531 is_template);
26532 }
26533 parser->qualifying_scope = parser->scope;
26534 parser->object_scope = NULL_TREE;
26535 }
26536 else if (object_type)
26537 {
26538 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26539 OBJECT_TYPE is not a class. */
26540 if (CLASS_TYPE_P (object_type))
26541 /* If the OBJECT_TYPE is a template specialization, it may
26542 be instantiated during name lookup. In that case, errors
26543 may be issued. Even if we rollback the current tentative
26544 parse, those errors are valid. */
26545 decl = lookup_member (object_type,
26546 name,
26547 /*protect=*/0,
26548 prefer_type_arg (tag_type),
26549 tf_warning_or_error);
26550 else
26551 decl = NULL_TREE;
26552
26553 if (!decl)
26554 /* Look it up in the enclosing context. DR 141: When looking for a
26555 template-name after -> or ., only consider class templates. */
26556 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26557 /*nonclass=*/0,
26558 /*block_p=*/true, is_namespace, 0);
26559 if (object_type == unknown_type_node)
26560 /* The object is type-dependent, so we can't look anything up; we used
26561 this to get the DR 141 behavior. */
26562 object_type = NULL_TREE;
26563 parser->object_scope = object_type;
26564 parser->qualifying_scope = NULL_TREE;
26565 }
26566 else
26567 {
26568 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26569 /*nonclass=*/0,
26570 /*block_p=*/true, is_namespace, 0);
26571 parser->qualifying_scope = NULL_TREE;
26572 parser->object_scope = NULL_TREE;
26573 }
26574
26575 /* If the lookup failed, let our caller know. */
26576 if (!decl || decl == error_mark_node)
26577 return error_mark_node;
26578
26579 /* Pull out the template from an injected-class-name (or multiple). */
26580 if (is_template)
26581 decl = maybe_get_template_decl_from_type_decl (decl);
26582
26583 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26584 if (TREE_CODE (decl) == TREE_LIST)
26585 {
26586 if (ambiguous_decls)
26587 *ambiguous_decls = decl;
26588 /* The error message we have to print is too complicated for
26589 cp_parser_error, so we incorporate its actions directly. */
26590 if (!cp_parser_simulate_error (parser))
26591 {
26592 error_at (name_location, "reference to %qD is ambiguous",
26593 name);
26594 print_candidates (decl);
26595 }
26596 return error_mark_node;
26597 }
26598
26599 gcc_assert (DECL_P (decl)
26600 || TREE_CODE (decl) == OVERLOAD
26601 || TREE_CODE (decl) == SCOPE_REF
26602 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26603 || BASELINK_P (decl));
26604
26605 /* If we have resolved the name of a member declaration, check to
26606 see if the declaration is accessible. When the name resolves to
26607 set of overloaded functions, accessibility is checked when
26608 overload resolution is done.
26609
26610 During an explicit instantiation, access is not checked at all,
26611 as per [temp.explicit]. */
26612 if (DECL_P (decl))
26613 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26614
26615 maybe_record_typedef_use (decl);
26616
26617 return cp_expr (decl, name_location);
26618 }
26619
26620 /* Like cp_parser_lookup_name, but for use in the typical case where
26621 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26622 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26623
26624 static tree
26625 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26626 {
26627 return cp_parser_lookup_name (parser, name,
26628 none_type,
26629 /*is_template=*/false,
26630 /*is_namespace=*/false,
26631 /*check_dependency=*/true,
26632 /*ambiguous_decls=*/NULL,
26633 location);
26634 }
26635
26636 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26637 the current context, return the TYPE_DECL. If TAG_NAME_P is
26638 true, the DECL indicates the class being defined in a class-head,
26639 or declared in an elaborated-type-specifier.
26640
26641 Otherwise, return DECL. */
26642
26643 static tree
26644 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26645 {
26646 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26647 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26648
26649 struct A {
26650 template <typename T> struct B;
26651 };
26652
26653 template <typename T> struct A::B {};
26654
26655 Similarly, in an elaborated-type-specifier:
26656
26657 namespace N { struct X{}; }
26658
26659 struct A {
26660 template <typename T> friend struct N::X;
26661 };
26662
26663 However, if the DECL refers to a class type, and we are in
26664 the scope of the class, then the name lookup automatically
26665 finds the TYPE_DECL created by build_self_reference rather
26666 than a TEMPLATE_DECL. For example, in:
26667
26668 template <class T> struct S {
26669 S s;
26670 };
26671
26672 there is no need to handle such case. */
26673
26674 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26675 return DECL_TEMPLATE_RESULT (decl);
26676
26677 return decl;
26678 }
26679
26680 /* If too many, or too few, template-parameter lists apply to the
26681 declarator, issue an error message. Returns TRUE if all went well,
26682 and FALSE otherwise. */
26683
26684 static bool
26685 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26686 cp_declarator *declarator,
26687 location_t declarator_location)
26688 {
26689 switch (declarator->kind)
26690 {
26691 case cdk_id:
26692 {
26693 unsigned num_templates = 0;
26694 tree scope = declarator->u.id.qualifying_scope;
26695 bool template_id_p = false;
26696
26697 if (scope)
26698 num_templates = num_template_headers_for_class (scope);
26699 else if (TREE_CODE (declarator->u.id.unqualified_name)
26700 == TEMPLATE_ID_EXPR)
26701 {
26702 /* If the DECLARATOR has the form `X<y>' then it uses one
26703 additional level of template parameters. */
26704 ++num_templates;
26705 template_id_p = true;
26706 }
26707
26708 return cp_parser_check_template_parameters
26709 (parser, num_templates, template_id_p, declarator_location,
26710 declarator);
26711 }
26712
26713 case cdk_function:
26714 case cdk_array:
26715 case cdk_pointer:
26716 case cdk_reference:
26717 case cdk_ptrmem:
26718 return (cp_parser_check_declarator_template_parameters
26719 (parser, declarator->declarator, declarator_location));
26720
26721 case cdk_decomp:
26722 case cdk_error:
26723 return true;
26724
26725 default:
26726 gcc_unreachable ();
26727 }
26728 return false;
26729 }
26730
26731 /* NUM_TEMPLATES were used in the current declaration. If that is
26732 invalid, return FALSE and issue an error messages. Otherwise,
26733 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26734 declarator and we can print more accurate diagnostics. */
26735
26736 static bool
26737 cp_parser_check_template_parameters (cp_parser* parser,
26738 unsigned num_templates,
26739 bool template_id_p,
26740 location_t location,
26741 cp_declarator *declarator)
26742 {
26743 /* If there are the same number of template classes and parameter
26744 lists, that's OK. */
26745 if (parser->num_template_parameter_lists == num_templates)
26746 return true;
26747 /* If there are more, but only one more, and the name ends in an identifier,
26748 then we are declaring a primary template. That's OK too. */
26749 if (!template_id_p
26750 && parser->num_template_parameter_lists == num_templates + 1)
26751 return true;
26752 /* If there are more template classes than parameter lists, we have
26753 something like:
26754
26755 template <class T> void S<T>::R<T>::f (); */
26756 if (parser->num_template_parameter_lists < num_templates)
26757 {
26758 if (declarator && !current_function_decl)
26759 error_at (location, "specializing member %<%T::%E%> "
26760 "requires %<template<>%> syntax",
26761 declarator->u.id.qualifying_scope,
26762 declarator->u.id.unqualified_name);
26763 else if (declarator)
26764 error_at (location, "invalid declaration of %<%T::%E%>",
26765 declarator->u.id.qualifying_scope,
26766 declarator->u.id.unqualified_name);
26767 else
26768 error_at (location, "too few template-parameter-lists");
26769 return false;
26770 }
26771 /* Otherwise, there are too many template parameter lists. We have
26772 something like:
26773
26774 template <class T> template <class U> void S::f(); */
26775 error_at (location, "too many template-parameter-lists");
26776 return false;
26777 }
26778
26779 /* Parse an optional `::' token indicating that the following name is
26780 from the global namespace. If so, PARSER->SCOPE is set to the
26781 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26782 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26783 Returns the new value of PARSER->SCOPE, if the `::' token is
26784 present, and NULL_TREE otherwise. */
26785
26786 static tree
26787 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26788 {
26789 cp_token *token;
26790
26791 /* Peek at the next token. */
26792 token = cp_lexer_peek_token (parser->lexer);
26793 /* If we're looking at a `::' token then we're starting from the
26794 global namespace, not our current location. */
26795 if (token->type == CPP_SCOPE)
26796 {
26797 /* Consume the `::' token. */
26798 cp_lexer_consume_token (parser->lexer);
26799 /* Set the SCOPE so that we know where to start the lookup. */
26800 parser->scope = global_namespace;
26801 parser->qualifying_scope = global_namespace;
26802 parser->object_scope = NULL_TREE;
26803
26804 return parser->scope;
26805 }
26806 else if (!current_scope_valid_p)
26807 {
26808 parser->scope = NULL_TREE;
26809 parser->qualifying_scope = NULL_TREE;
26810 parser->object_scope = NULL_TREE;
26811 }
26812
26813 return NULL_TREE;
26814 }
26815
26816 /* Returns TRUE if the upcoming token sequence is the start of a
26817 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26818 declarator is preceded by the `friend' specifier. */
26819
26820 static bool
26821 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26822 {
26823 bool constructor_p;
26824 bool outside_class_specifier_p;
26825 tree nested_name_specifier;
26826 cp_token *next_token;
26827
26828 /* The common case is that this is not a constructor declarator, so
26829 try to avoid doing lots of work if at all possible. It's not
26830 valid declare a constructor at function scope. */
26831 if (parser->in_function_body)
26832 return false;
26833 /* And only certain tokens can begin a constructor declarator. */
26834 next_token = cp_lexer_peek_token (parser->lexer);
26835 if (next_token->type != CPP_NAME
26836 && next_token->type != CPP_SCOPE
26837 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26838 && next_token->type != CPP_TEMPLATE_ID)
26839 return false;
26840
26841 /* Parse tentatively; we are going to roll back all of the tokens
26842 consumed here. */
26843 cp_parser_parse_tentatively (parser);
26844 /* Assume that we are looking at a constructor declarator. */
26845 constructor_p = true;
26846
26847 /* Look for the optional `::' operator. */
26848 cp_parser_global_scope_opt (parser,
26849 /*current_scope_valid_p=*/false);
26850 /* Look for the nested-name-specifier. */
26851 nested_name_specifier
26852 = (cp_parser_nested_name_specifier_opt (parser,
26853 /*typename_keyword_p=*/false,
26854 /*check_dependency_p=*/false,
26855 /*type_p=*/false,
26856 /*is_declaration=*/false));
26857
26858 outside_class_specifier_p = (!at_class_scope_p ()
26859 || !TYPE_BEING_DEFINED (current_class_type)
26860 || friend_p);
26861
26862 /* Outside of a class-specifier, there must be a
26863 nested-name-specifier. Except in C++17 mode, where we
26864 might be declaring a guiding declaration. */
26865 if (!nested_name_specifier && outside_class_specifier_p
26866 && cxx_dialect < cxx17)
26867 constructor_p = false;
26868 else if (nested_name_specifier == error_mark_node)
26869 constructor_p = false;
26870
26871 /* If we have a class scope, this is easy; DR 147 says that S::S always
26872 names the constructor, and no other qualified name could. */
26873 if (constructor_p && nested_name_specifier
26874 && CLASS_TYPE_P (nested_name_specifier))
26875 {
26876 tree id = cp_parser_unqualified_id (parser,
26877 /*template_keyword_p=*/false,
26878 /*check_dependency_p=*/false,
26879 /*declarator_p=*/true,
26880 /*optional_p=*/false);
26881 if (is_overloaded_fn (id))
26882 id = DECL_NAME (get_first_fn (id));
26883 if (!constructor_name_p (id, nested_name_specifier))
26884 constructor_p = false;
26885 }
26886 /* If we still think that this might be a constructor-declarator,
26887 look for a class-name. */
26888 else if (constructor_p)
26889 {
26890 /* If we have:
26891
26892 template <typename T> struct S {
26893 S();
26894 };
26895
26896 we must recognize that the nested `S' names a class. */
26897 if (cxx_dialect >= cxx17)
26898 cp_parser_parse_tentatively (parser);
26899
26900 tree type_decl;
26901 type_decl = cp_parser_class_name (parser,
26902 /*typename_keyword_p=*/false,
26903 /*template_keyword_p=*/false,
26904 none_type,
26905 /*check_dependency_p=*/false,
26906 /*class_head_p=*/false,
26907 /*is_declaration=*/false);
26908
26909 if (cxx_dialect >= cxx17
26910 && !cp_parser_parse_definitely (parser))
26911 {
26912 type_decl = NULL_TREE;
26913 tree tmpl = cp_parser_template_name (parser,
26914 /*template_keyword*/false,
26915 /*check_dependency_p*/false,
26916 /*is_declaration*/false,
26917 none_type,
26918 /*is_identifier*/NULL);
26919 if (DECL_CLASS_TEMPLATE_P (tmpl)
26920 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26921 /* It's a deduction guide, return true. */;
26922 else
26923 cp_parser_simulate_error (parser);
26924 }
26925
26926 /* If there was no class-name, then this is not a constructor.
26927 Otherwise, if we are in a class-specifier and we aren't
26928 handling a friend declaration, check that its type matches
26929 current_class_type (c++/38313). Note: error_mark_node
26930 is left alone for error recovery purposes. */
26931 constructor_p = (!cp_parser_error_occurred (parser)
26932 && (outside_class_specifier_p
26933 || type_decl == NULL_TREE
26934 || type_decl == error_mark_node
26935 || same_type_p (current_class_type,
26936 TREE_TYPE (type_decl))));
26937
26938 /* If we're still considering a constructor, we have to see a `(',
26939 to begin the parameter-declaration-clause, followed by either a
26940 `)', an `...', or a decl-specifier. We need to check for a
26941 type-specifier to avoid being fooled into thinking that:
26942
26943 S (f) (int);
26944
26945 is a constructor. (It is actually a function named `f' that
26946 takes one parameter (of type `int') and returns a value of type
26947 `S'. */
26948 if (constructor_p
26949 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26950 constructor_p = false;
26951
26952 if (constructor_p
26953 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26954 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26955 /* A parameter declaration begins with a decl-specifier,
26956 which is either the "attribute" keyword, a storage class
26957 specifier, or (usually) a type-specifier. */
26958 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26959 {
26960 tree type;
26961 tree pushed_scope = NULL_TREE;
26962 unsigned saved_num_template_parameter_lists;
26963
26964 /* Names appearing in the type-specifier should be looked up
26965 in the scope of the class. */
26966 if (current_class_type)
26967 type = NULL_TREE;
26968 else if (type_decl)
26969 {
26970 type = TREE_TYPE (type_decl);
26971 if (TREE_CODE (type) == TYPENAME_TYPE)
26972 {
26973 type = resolve_typename_type (type,
26974 /*only_current_p=*/false);
26975 if (TREE_CODE (type) == TYPENAME_TYPE)
26976 {
26977 cp_parser_abort_tentative_parse (parser);
26978 return false;
26979 }
26980 }
26981 pushed_scope = push_scope (type);
26982 }
26983
26984 /* Inside the constructor parameter list, surrounding
26985 template-parameter-lists do not apply. */
26986 saved_num_template_parameter_lists
26987 = parser->num_template_parameter_lists;
26988 parser->num_template_parameter_lists = 0;
26989
26990 /* Look for the type-specifier. */
26991 cp_parser_type_specifier (parser,
26992 CP_PARSER_FLAGS_NONE,
26993 /*decl_specs=*/NULL,
26994 /*is_declarator=*/true,
26995 /*declares_class_or_enum=*/NULL,
26996 /*is_cv_qualifier=*/NULL);
26997
26998 parser->num_template_parameter_lists
26999 = saved_num_template_parameter_lists;
27000
27001 /* Leave the scope of the class. */
27002 if (pushed_scope)
27003 pop_scope (pushed_scope);
27004
27005 constructor_p = !cp_parser_error_occurred (parser);
27006 }
27007 }
27008
27009 /* We did not really want to consume any tokens. */
27010 cp_parser_abort_tentative_parse (parser);
27011
27012 return constructor_p;
27013 }
27014
27015 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27016 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27017 they must be performed once we are in the scope of the function.
27018
27019 Returns the function defined. */
27020
27021 static tree
27022 cp_parser_function_definition_from_specifiers_and_declarator
27023 (cp_parser* parser,
27024 cp_decl_specifier_seq *decl_specifiers,
27025 tree attributes,
27026 const cp_declarator *declarator)
27027 {
27028 tree fn;
27029 bool success_p;
27030
27031 /* Begin the function-definition. */
27032 success_p = start_function (decl_specifiers, declarator, attributes);
27033
27034 /* The things we're about to see are not directly qualified by any
27035 template headers we've seen thus far. */
27036 reset_specialization ();
27037
27038 /* If there were names looked up in the decl-specifier-seq that we
27039 did not check, check them now. We must wait until we are in the
27040 scope of the function to perform the checks, since the function
27041 might be a friend. */
27042 perform_deferred_access_checks (tf_warning_or_error);
27043
27044 if (success_p)
27045 {
27046 cp_finalize_omp_declare_simd (parser, current_function_decl);
27047 parser->omp_declare_simd = NULL;
27048 cp_finalize_oacc_routine (parser, current_function_decl, true);
27049 parser->oacc_routine = NULL;
27050 }
27051
27052 if (!success_p)
27053 {
27054 /* Skip the entire function. */
27055 cp_parser_skip_to_end_of_block_or_statement (parser);
27056 fn = error_mark_node;
27057 }
27058 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27059 {
27060 /* Seen already, skip it. An error message has already been output. */
27061 cp_parser_skip_to_end_of_block_or_statement (parser);
27062 fn = current_function_decl;
27063 current_function_decl = NULL_TREE;
27064 /* If this is a function from a class, pop the nested class. */
27065 if (current_class_name)
27066 pop_nested_class ();
27067 }
27068 else
27069 {
27070 timevar_id_t tv;
27071 if (DECL_DECLARED_INLINE_P (current_function_decl))
27072 tv = TV_PARSE_INLINE;
27073 else
27074 tv = TV_PARSE_FUNC;
27075 timevar_push (tv);
27076 fn = cp_parser_function_definition_after_declarator (parser,
27077 /*inline_p=*/false);
27078 timevar_pop (tv);
27079 }
27080
27081 return fn;
27082 }
27083
27084 /* Parse the part of a function-definition that follows the
27085 declarator. INLINE_P is TRUE iff this function is an inline
27086 function defined within a class-specifier.
27087
27088 Returns the function defined. */
27089
27090 static tree
27091 cp_parser_function_definition_after_declarator (cp_parser* parser,
27092 bool inline_p)
27093 {
27094 tree fn;
27095 bool saved_in_unbraced_linkage_specification_p;
27096 bool saved_in_function_body;
27097 unsigned saved_num_template_parameter_lists;
27098 cp_token *token;
27099 bool fully_implicit_function_template_p
27100 = parser->fully_implicit_function_template_p;
27101 parser->fully_implicit_function_template_p = false;
27102 tree implicit_template_parms
27103 = parser->implicit_template_parms;
27104 parser->implicit_template_parms = 0;
27105 cp_binding_level* implicit_template_scope
27106 = parser->implicit_template_scope;
27107 parser->implicit_template_scope = 0;
27108
27109 saved_in_function_body = parser->in_function_body;
27110 parser->in_function_body = true;
27111 /* If the next token is `return', then the code may be trying to
27112 make use of the "named return value" extension that G++ used to
27113 support. */
27114 token = cp_lexer_peek_token (parser->lexer);
27115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27116 {
27117 /* Consume the `return' keyword. */
27118 cp_lexer_consume_token (parser->lexer);
27119 /* Look for the identifier that indicates what value is to be
27120 returned. */
27121 cp_parser_identifier (parser);
27122 /* Issue an error message. */
27123 error_at (token->location,
27124 "named return values are no longer supported");
27125 /* Skip tokens until we reach the start of the function body. */
27126 while (true)
27127 {
27128 cp_token *token = cp_lexer_peek_token (parser->lexer);
27129 if (token->type == CPP_OPEN_BRACE
27130 || token->type == CPP_EOF
27131 || token->type == CPP_PRAGMA_EOL)
27132 break;
27133 cp_lexer_consume_token (parser->lexer);
27134 }
27135 }
27136 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27137 anything declared inside `f'. */
27138 saved_in_unbraced_linkage_specification_p
27139 = parser->in_unbraced_linkage_specification_p;
27140 parser->in_unbraced_linkage_specification_p = false;
27141 /* Inside the function, surrounding template-parameter-lists do not
27142 apply. */
27143 saved_num_template_parameter_lists
27144 = parser->num_template_parameter_lists;
27145 parser->num_template_parameter_lists = 0;
27146
27147 /* If the next token is `try', `__transaction_atomic', or
27148 `__transaction_relaxed`, then we are looking at either function-try-block
27149 or function-transaction-block. Note that all of these include the
27150 function-body. */
27151 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27152 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27153 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27154 RID_TRANSACTION_RELAXED))
27155 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27156 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27157 cp_parser_function_try_block (parser);
27158 else
27159 cp_parser_ctor_initializer_opt_and_function_body
27160 (parser, /*in_function_try_block=*/false);
27161
27162 /* Finish the function. */
27163 fn = finish_function (inline_p);
27164 /* Generate code for it, if necessary. */
27165 expand_or_defer_fn (fn);
27166 /* Restore the saved values. */
27167 parser->in_unbraced_linkage_specification_p
27168 = saved_in_unbraced_linkage_specification_p;
27169 parser->num_template_parameter_lists
27170 = saved_num_template_parameter_lists;
27171 parser->in_function_body = saved_in_function_body;
27172
27173 parser->fully_implicit_function_template_p
27174 = fully_implicit_function_template_p;
27175 parser->implicit_template_parms
27176 = implicit_template_parms;
27177 parser->implicit_template_scope
27178 = implicit_template_scope;
27179
27180 if (parser->fully_implicit_function_template_p)
27181 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27182
27183 return fn;
27184 }
27185
27186 /* Parse a template-declaration body (following argument list). */
27187
27188 static void
27189 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27190 tree parameter_list,
27191 bool member_p)
27192 {
27193 tree decl = NULL_TREE;
27194 bool friend_p = false;
27195
27196 /* We just processed one more parameter list. */
27197 ++parser->num_template_parameter_lists;
27198
27199 /* Get the deferred access checks from the parameter list. These
27200 will be checked once we know what is being declared, as for a
27201 member template the checks must be performed in the scope of the
27202 class containing the member. */
27203 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27204
27205 /* Tentatively parse for a new template parameter list, which can either be
27206 the template keyword or a template introduction. */
27207 if (cp_parser_template_declaration_after_export (parser, member_p))
27208 /* OK */;
27209 else if (cxx_dialect >= cxx11
27210 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27211 decl = cp_parser_alias_declaration (parser);
27212 else
27213 {
27214 /* There are no access checks when parsing a template, as we do not
27215 know if a specialization will be a friend. */
27216 push_deferring_access_checks (dk_no_check);
27217 cp_token *token = cp_lexer_peek_token (parser->lexer);
27218 decl = cp_parser_single_declaration (parser,
27219 checks,
27220 member_p,
27221 /*explicit_specialization_p=*/false,
27222 &friend_p);
27223 pop_deferring_access_checks ();
27224
27225 /* If this is a member template declaration, let the front
27226 end know. */
27227 if (member_p && !friend_p && decl)
27228 {
27229 if (TREE_CODE (decl) == TYPE_DECL)
27230 cp_parser_check_access_in_redeclaration (decl, token->location);
27231
27232 decl = finish_member_template_decl (decl);
27233 }
27234 else if (friend_p && decl
27235 && DECL_DECLARES_TYPE_P (decl))
27236 make_friend_class (current_class_type, TREE_TYPE (decl),
27237 /*complain=*/true);
27238 }
27239 /* We are done with the current parameter list. */
27240 --parser->num_template_parameter_lists;
27241
27242 pop_deferring_access_checks ();
27243
27244 /* Finish up. */
27245 finish_template_decl (parameter_list);
27246
27247 /* Check the template arguments for a literal operator template. */
27248 if (decl
27249 && DECL_DECLARES_FUNCTION_P (decl)
27250 && UDLIT_OPER_P (DECL_NAME (decl)))
27251 {
27252 bool ok = true;
27253 if (parameter_list == NULL_TREE)
27254 ok = false;
27255 else
27256 {
27257 int num_parms = TREE_VEC_LENGTH (parameter_list);
27258 if (num_parms == 1)
27259 {
27260 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27261 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27262 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27263 /* OK, C++20 string literal operator template. We don't need
27264 to warn in lower dialects here because we will have already
27265 warned about the template parameter. */;
27266 else if (TREE_TYPE (parm) != char_type_node
27267 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27268 ok = false;
27269 }
27270 else if (num_parms == 2 && cxx_dialect >= cxx14)
27271 {
27272 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27273 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27274 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27275 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27276 if (parm == error_mark_node
27277 || TREE_TYPE (parm) != TREE_TYPE (type)
27278 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27279 ok = false;
27280 else
27281 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27282 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27283 "ISO C++ did not adopt string literal operator templa"
27284 "tes taking an argument pack of characters");
27285 }
27286 else
27287 ok = false;
27288 }
27289 if (!ok)
27290 {
27291 if (cxx_dialect > cxx17)
27292 error ("literal operator template %qD has invalid parameter list;"
27293 " Expected non-type template parameter pack <char...> "
27294 " or single non-type parameter of class type",
27295 decl);
27296 else
27297 error ("literal operator template %qD has invalid parameter list."
27298 " Expected non-type template parameter pack <char...>",
27299 decl);
27300 }
27301 }
27302
27303 /* Register member declarations. */
27304 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27305 finish_member_declaration (decl);
27306 /* If DECL is a function template, we must return to parse it later.
27307 (Even though there is no definition, there might be default
27308 arguments that need handling.) */
27309 if (member_p && decl
27310 && DECL_DECLARES_FUNCTION_P (decl))
27311 vec_safe_push (unparsed_funs_with_definitions, decl);
27312 }
27313
27314 /* Parse a template introduction header for a template-declaration. Returns
27315 false if tentative parse fails. */
27316
27317 static bool
27318 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27319 {
27320 cp_parser_parse_tentatively (parser);
27321
27322 tree saved_scope = parser->scope;
27323 tree saved_object_scope = parser->object_scope;
27324 tree saved_qualifying_scope = parser->qualifying_scope;
27325
27326 /* Look for the optional `::' operator. */
27327 cp_parser_global_scope_opt (parser,
27328 /*current_scope_valid_p=*/false);
27329 /* Look for the nested-name-specifier. */
27330 cp_parser_nested_name_specifier_opt (parser,
27331 /*typename_keyword_p=*/false,
27332 /*check_dependency_p=*/true,
27333 /*type_p=*/false,
27334 /*is_declaration=*/false);
27335
27336 cp_token *token = cp_lexer_peek_token (parser->lexer);
27337 tree concept_name = cp_parser_identifier (parser);
27338
27339 /* Look up the concept for which we will be matching
27340 template parameters. */
27341 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27342 token->location);
27343 parser->scope = saved_scope;
27344 parser->object_scope = saved_object_scope;
27345 parser->qualifying_scope = saved_qualifying_scope;
27346
27347 if (concept_name == error_mark_node)
27348 cp_parser_simulate_error (parser);
27349
27350 /* Look for opening brace for introduction. */
27351 matching_braces braces;
27352 braces.require_open (parser);
27353
27354 if (!cp_parser_parse_definitely (parser))
27355 return false;
27356
27357 push_deferring_access_checks (dk_deferred);
27358
27359 /* Build vector of placeholder parameters and grab
27360 matching identifiers. */
27361 tree introduction_list = cp_parser_introduction_list (parser);
27362
27363 /* Look for closing brace for introduction. */
27364 if (!braces.require_close (parser))
27365 return true;
27366
27367 /* The introduction-list shall not be empty. */
27368 int nargs = TREE_VEC_LENGTH (introduction_list);
27369 if (nargs == 0)
27370 {
27371 /* In cp_parser_introduction_list we have already issued an error. */
27372 return true;
27373 }
27374
27375 if (tmpl_decl == error_mark_node)
27376 {
27377 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27378 token->location);
27379 return true;
27380 }
27381
27382 /* Build and associate the constraint. */
27383 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27384 if (parms && parms != error_mark_node)
27385 {
27386 cp_parser_template_declaration_after_parameters (parser, parms,
27387 member_p);
27388 return true;
27389 }
27390
27391 error_at (token->location, "no matching concept for template-introduction");
27392 return true;
27393 }
27394
27395 /* Parse a normal template-declaration following the template keyword. */
27396
27397 static void
27398 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27399 {
27400 tree parameter_list;
27401 bool need_lang_pop;
27402 location_t location = input_location;
27403
27404 /* Look for the `<' token. */
27405 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27406 return;
27407 if (at_class_scope_p () && current_function_decl)
27408 {
27409 /* 14.5.2.2 [temp.mem]
27410
27411 A local class shall not have member templates. */
27412 error_at (location,
27413 "invalid declaration of member template in local class");
27414 cp_parser_skip_to_end_of_block_or_statement (parser);
27415 return;
27416 }
27417 /* [temp]
27418
27419 A template ... shall not have C linkage. */
27420 if (current_lang_name == lang_name_c)
27421 {
27422 error_at (location, "template with C linkage");
27423 maybe_show_extern_c_location ();
27424 /* Give it C++ linkage to avoid confusing other parts of the
27425 front end. */
27426 push_lang_context (lang_name_cplusplus);
27427 need_lang_pop = true;
27428 }
27429 else
27430 need_lang_pop = false;
27431
27432 /* We cannot perform access checks on the template parameter
27433 declarations until we know what is being declared, just as we
27434 cannot check the decl-specifier list. */
27435 push_deferring_access_checks (dk_deferred);
27436
27437 /* If the next token is `>', then we have an invalid
27438 specialization. Rather than complain about an invalid template
27439 parameter, issue an error message here. */
27440 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27441 {
27442 cp_parser_error (parser, "invalid explicit specialization");
27443 begin_specialization ();
27444 parameter_list = NULL_TREE;
27445 }
27446 else
27447 {
27448 /* Parse the template parameters. */
27449 parameter_list = cp_parser_template_parameter_list (parser);
27450 }
27451
27452 /* Look for the `>'. */
27453 cp_parser_skip_to_end_of_template_parameter_list (parser);
27454
27455 /* Manage template requirements */
27456 if (flag_concepts)
27457 {
27458 tree reqs = get_shorthand_constraints (current_template_parms);
27459 if (tree r = cp_parser_requires_clause_opt (parser))
27460 reqs = conjoin_constraints (reqs, normalize_expression (r));
27461 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27462 }
27463
27464 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27465 member_p);
27466
27467 /* For the erroneous case of a template with C linkage, we pushed an
27468 implicit C++ linkage scope; exit that scope now. */
27469 if (need_lang_pop)
27470 pop_lang_context ();
27471 }
27472
27473 /* Parse a template-declaration, assuming that the `export' (and
27474 `extern') keywords, if present, has already been scanned. MEMBER_P
27475 is as for cp_parser_template_declaration. */
27476
27477 static bool
27478 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27479 {
27480 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27481 {
27482 cp_lexer_consume_token (parser->lexer);
27483 cp_parser_explicit_template_declaration (parser, member_p);
27484 return true;
27485 }
27486 else if (flag_concepts)
27487 return cp_parser_template_introduction (parser, member_p);
27488
27489 return false;
27490 }
27491
27492 /* Perform the deferred access checks from a template-parameter-list.
27493 CHECKS is a TREE_LIST of access checks, as returned by
27494 get_deferred_access_checks. */
27495
27496 static void
27497 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27498 {
27499 ++processing_template_parmlist;
27500 perform_access_checks (checks, tf_warning_or_error);
27501 --processing_template_parmlist;
27502 }
27503
27504 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27505 `function-definition' sequence that follows a template header.
27506 If MEMBER_P is true, this declaration appears in a class scope.
27507
27508 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27509 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27510
27511 static tree
27512 cp_parser_single_declaration (cp_parser* parser,
27513 vec<deferred_access_check, va_gc> *checks,
27514 bool member_p,
27515 bool explicit_specialization_p,
27516 bool* friend_p)
27517 {
27518 int declares_class_or_enum;
27519 tree decl = NULL_TREE;
27520 cp_decl_specifier_seq decl_specifiers;
27521 bool function_definition_p = false;
27522 cp_token *decl_spec_token_start;
27523
27524 /* This function is only used when processing a template
27525 declaration. */
27526 gcc_assert (innermost_scope_kind () == sk_template_parms
27527 || innermost_scope_kind () == sk_template_spec);
27528
27529 /* Defer access checks until we know what is being declared. */
27530 push_deferring_access_checks (dk_deferred);
27531
27532 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27533 alternative. */
27534 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27535 cp_parser_decl_specifier_seq (parser,
27536 CP_PARSER_FLAGS_OPTIONAL,
27537 &decl_specifiers,
27538 &declares_class_or_enum);
27539 if (friend_p)
27540 *friend_p = cp_parser_friend_p (&decl_specifiers);
27541
27542 /* There are no template typedefs. */
27543 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27544 {
27545 error_at (decl_spec_token_start->location,
27546 "template declaration of %<typedef%>");
27547 decl = error_mark_node;
27548 }
27549
27550 /* Gather up the access checks that occurred the
27551 decl-specifier-seq. */
27552 stop_deferring_access_checks ();
27553
27554 /* Check for the declaration of a template class. */
27555 if (declares_class_or_enum)
27556 {
27557 if (cp_parser_declares_only_class_p (parser)
27558 || (declares_class_or_enum & 2))
27559 {
27560 // If this is a declaration, but not a definition, associate
27561 // any constraints with the type declaration. Constraints
27562 // are associated with definitions in cp_parser_class_specifier.
27563 if (declares_class_or_enum == 1)
27564 associate_classtype_constraints (decl_specifiers.type);
27565
27566 decl = shadow_tag (&decl_specifiers);
27567
27568 /* In this case:
27569
27570 struct C {
27571 friend template <typename T> struct A<T>::B;
27572 };
27573
27574 A<T>::B will be represented by a TYPENAME_TYPE, and
27575 therefore not recognized by shadow_tag. */
27576 if (friend_p && *friend_p
27577 && !decl
27578 && decl_specifiers.type
27579 && TYPE_P (decl_specifiers.type))
27580 decl = decl_specifiers.type;
27581
27582 if (decl && decl != error_mark_node)
27583 decl = TYPE_NAME (decl);
27584 else
27585 decl = error_mark_node;
27586
27587 /* Perform access checks for template parameters. */
27588 cp_parser_perform_template_parameter_access_checks (checks);
27589
27590 /* Give a helpful diagnostic for
27591 template <class T> struct A { } a;
27592 if we aren't already recovering from an error. */
27593 if (!cp_parser_declares_only_class_p (parser)
27594 && !seen_error ())
27595 {
27596 error_at (cp_lexer_peek_token (parser->lexer)->location,
27597 "a class template declaration must not declare "
27598 "anything else");
27599 cp_parser_skip_to_end_of_block_or_statement (parser);
27600 goto out;
27601 }
27602 }
27603 }
27604
27605 /* Complain about missing 'typename' or other invalid type names. */
27606 if (!decl_specifiers.any_type_specifiers_p
27607 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27608 {
27609 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27610 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27611 the rest of this declaration. */
27612 decl = error_mark_node;
27613 goto out;
27614 }
27615
27616 /* If it's not a template class, try for a template function. If
27617 the next token is a `;', then this declaration does not declare
27618 anything. But, if there were errors in the decl-specifiers, then
27619 the error might well have come from an attempted class-specifier.
27620 In that case, there's no need to warn about a missing declarator. */
27621 if (!decl
27622 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27623 || decl_specifiers.type != error_mark_node))
27624 {
27625 decl = cp_parser_init_declarator (parser,
27626 &decl_specifiers,
27627 checks,
27628 /*function_definition_allowed_p=*/true,
27629 member_p,
27630 declares_class_or_enum,
27631 &function_definition_p,
27632 NULL, NULL, NULL);
27633
27634 /* 7.1.1-1 [dcl.stc]
27635
27636 A storage-class-specifier shall not be specified in an explicit
27637 specialization... */
27638 if (decl
27639 && explicit_specialization_p
27640 && decl_specifiers.storage_class != sc_none)
27641 {
27642 error_at (decl_spec_token_start->location,
27643 "explicit template specialization cannot have a storage class");
27644 decl = error_mark_node;
27645 }
27646
27647 if (decl && VAR_P (decl))
27648 check_template_variable (decl);
27649 }
27650
27651 /* Look for a trailing `;' after the declaration. */
27652 if (!function_definition_p
27653 && (decl == error_mark_node
27654 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27655 cp_parser_skip_to_end_of_block_or_statement (parser);
27656
27657 out:
27658 pop_deferring_access_checks ();
27659
27660 /* Clear any current qualification; whatever comes next is the start
27661 of something new. */
27662 parser->scope = NULL_TREE;
27663 parser->qualifying_scope = NULL_TREE;
27664 parser->object_scope = NULL_TREE;
27665
27666 return decl;
27667 }
27668
27669 /* Parse a cast-expression that is not the operand of a unary "&". */
27670
27671 static cp_expr
27672 cp_parser_simple_cast_expression (cp_parser *parser)
27673 {
27674 return cp_parser_cast_expression (parser, /*address_p=*/false,
27675 /*cast_p=*/false, /*decltype*/false, NULL);
27676 }
27677
27678 /* Parse a functional cast to TYPE. Returns an expression
27679 representing the cast. */
27680
27681 static cp_expr
27682 cp_parser_functional_cast (cp_parser* parser, tree type)
27683 {
27684 vec<tree, va_gc> *vec;
27685 tree expression_list;
27686 cp_expr cast;
27687 bool nonconst_p;
27688
27689 location_t start_loc = input_location;
27690
27691 if (!type)
27692 type = error_mark_node;
27693
27694 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27695 {
27696 cp_lexer_set_source_position (parser->lexer);
27697 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27698 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27699 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27700 if (TREE_CODE (type) == TYPE_DECL)
27701 type = TREE_TYPE (type);
27702
27703 cast = finish_compound_literal (type, expression_list,
27704 tf_warning_or_error, fcl_functional);
27705 /* Create a location of the form:
27706 type_name{i, f}
27707 ^~~~~~~~~~~~~~~
27708 with caret == start at the start of the type name,
27709 finishing at the closing brace. */
27710 location_t finish_loc
27711 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27712 location_t combined_loc = make_location (start_loc, start_loc,
27713 finish_loc);
27714 cast.set_location (combined_loc);
27715 return cast;
27716 }
27717
27718
27719 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27720 /*cast_p=*/true,
27721 /*allow_expansion_p=*/true,
27722 /*non_constant_p=*/NULL);
27723 if (vec == NULL)
27724 expression_list = error_mark_node;
27725 else
27726 {
27727 expression_list = build_tree_list_vec (vec);
27728 release_tree_vector (vec);
27729 }
27730
27731 cast = build_functional_cast (type, expression_list,
27732 tf_warning_or_error);
27733 /* [expr.const]/1: In an integral constant expression "only type
27734 conversions to integral or enumeration type can be used". */
27735 if (TREE_CODE (type) == TYPE_DECL)
27736 type = TREE_TYPE (type);
27737 if (cast != error_mark_node
27738 && !cast_valid_in_integral_constant_expression_p (type)
27739 && cp_parser_non_integral_constant_expression (parser,
27740 NIC_CONSTRUCTOR))
27741 return error_mark_node;
27742
27743 /* Create a location of the form:
27744 float(i)
27745 ^~~~~~~~
27746 with caret == start at the start of the type name,
27747 finishing at the closing paren. */
27748 location_t finish_loc
27749 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27750 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27751 cast.set_location (combined_loc);
27752 return cast;
27753 }
27754
27755 /* Save the tokens that make up the body of a member function defined
27756 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27757 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27758 specifiers applied to the declaration. Returns the FUNCTION_DECL
27759 for the member function. */
27760
27761 static tree
27762 cp_parser_save_member_function_body (cp_parser* parser,
27763 cp_decl_specifier_seq *decl_specifiers,
27764 cp_declarator *declarator,
27765 tree attributes)
27766 {
27767 cp_token *first;
27768 cp_token *last;
27769 tree fn;
27770 bool function_try_block = false;
27771
27772 /* Create the FUNCTION_DECL. */
27773 fn = grokmethod (decl_specifiers, declarator, attributes);
27774 cp_finalize_omp_declare_simd (parser, fn);
27775 cp_finalize_oacc_routine (parser, fn, true);
27776 /* If something went badly wrong, bail out now. */
27777 if (fn == error_mark_node)
27778 {
27779 /* If there's a function-body, skip it. */
27780 if (cp_parser_token_starts_function_definition_p
27781 (cp_lexer_peek_token (parser->lexer)))
27782 cp_parser_skip_to_end_of_block_or_statement (parser);
27783 return error_mark_node;
27784 }
27785
27786 /* Remember it, if there default args to post process. */
27787 cp_parser_save_default_args (parser, fn);
27788
27789 /* Save away the tokens that make up the body of the
27790 function. */
27791 first = parser->lexer->next_token;
27792
27793 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27794 cp_lexer_consume_token (parser->lexer);
27795 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27796 RID_TRANSACTION_ATOMIC))
27797 {
27798 cp_lexer_consume_token (parser->lexer);
27799 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27800 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27801 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27802 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27803 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27804 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27805 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27806 {
27807 cp_lexer_consume_token (parser->lexer);
27808 cp_lexer_consume_token (parser->lexer);
27809 cp_lexer_consume_token (parser->lexer);
27810 cp_lexer_consume_token (parser->lexer);
27811 cp_lexer_consume_token (parser->lexer);
27812 }
27813 else
27814 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27815 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27816 {
27817 cp_lexer_consume_token (parser->lexer);
27818 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27819 break;
27820 }
27821 }
27822
27823 /* Handle function try blocks. */
27824 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27825 {
27826 cp_lexer_consume_token (parser->lexer);
27827 function_try_block = true;
27828 }
27829 /* We can have braced-init-list mem-initializers before the fn body. */
27830 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27831 {
27832 cp_lexer_consume_token (parser->lexer);
27833 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27834 {
27835 /* cache_group will stop after an un-nested { } pair, too. */
27836 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27837 break;
27838
27839 /* variadic mem-inits have ... after the ')'. */
27840 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27841 cp_lexer_consume_token (parser->lexer);
27842 }
27843 }
27844 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27845 /* Handle function try blocks. */
27846 if (function_try_block)
27847 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27848 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27849 last = parser->lexer->next_token;
27850
27851 /* Save away the inline definition; we will process it when the
27852 class is complete. */
27853 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27854 DECL_PENDING_INLINE_P (fn) = 1;
27855
27856 /* We need to know that this was defined in the class, so that
27857 friend templates are handled correctly. */
27858 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27859
27860 /* Add FN to the queue of functions to be parsed later. */
27861 vec_safe_push (unparsed_funs_with_definitions, fn);
27862
27863 return fn;
27864 }
27865
27866 /* Save the tokens that make up the in-class initializer for a non-static
27867 data member. Returns a DEFAULT_ARG. */
27868
27869 static tree
27870 cp_parser_save_nsdmi (cp_parser* parser)
27871 {
27872 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27873 }
27874
27875 /* Parse a template-argument-list, as well as the trailing ">" (but
27876 not the opening "<"). See cp_parser_template_argument_list for the
27877 return value. */
27878
27879 static tree
27880 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27881 {
27882 tree arguments;
27883 tree saved_scope;
27884 tree saved_qualifying_scope;
27885 tree saved_object_scope;
27886 bool saved_greater_than_is_operator_p;
27887
27888 /* [temp.names]
27889
27890 When parsing a template-id, the first non-nested `>' is taken as
27891 the end of the template-argument-list rather than a greater-than
27892 operator. */
27893 saved_greater_than_is_operator_p
27894 = parser->greater_than_is_operator_p;
27895 parser->greater_than_is_operator_p = false;
27896 /* Parsing the argument list may modify SCOPE, so we save it
27897 here. */
27898 saved_scope = parser->scope;
27899 saved_qualifying_scope = parser->qualifying_scope;
27900 saved_object_scope = parser->object_scope;
27901 /* We need to evaluate the template arguments, even though this
27902 template-id may be nested within a "sizeof". */
27903 cp_evaluated ev;
27904 /* Parse the template-argument-list itself. */
27905 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27906 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27907 arguments = NULL_TREE;
27908 else
27909 arguments = cp_parser_template_argument_list (parser);
27910 /* Look for the `>' that ends the template-argument-list. If we find
27911 a '>>' instead, it's probably just a typo. */
27912 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27913 {
27914 if (cxx_dialect != cxx98)
27915 {
27916 /* In C++0x, a `>>' in a template argument list or cast
27917 expression is considered to be two separate `>'
27918 tokens. So, change the current token to a `>', but don't
27919 consume it: it will be consumed later when the outer
27920 template argument list (or cast expression) is parsed.
27921 Note that this replacement of `>' for `>>' is necessary
27922 even if we are parsing tentatively: in the tentative
27923 case, after calling
27924 cp_parser_enclosed_template_argument_list we will always
27925 throw away all of the template arguments and the first
27926 closing `>', either because the template argument list
27927 was erroneous or because we are replacing those tokens
27928 with a CPP_TEMPLATE_ID token. The second `>' (which will
27929 not have been thrown away) is needed either to close an
27930 outer template argument list or to complete a new-style
27931 cast. */
27932 cp_token *token = cp_lexer_peek_token (parser->lexer);
27933 token->type = CPP_GREATER;
27934 }
27935 else if (!saved_greater_than_is_operator_p)
27936 {
27937 /* If we're in a nested template argument list, the '>>' has
27938 to be a typo for '> >'. We emit the error message, but we
27939 continue parsing and we push a '>' as next token, so that
27940 the argument list will be parsed correctly. Note that the
27941 global source location is still on the token before the
27942 '>>', so we need to say explicitly where we want it. */
27943 cp_token *token = cp_lexer_peek_token (parser->lexer);
27944 gcc_rich_location richloc (token->location);
27945 richloc.add_fixit_replace ("> >");
27946 error_at (&richloc, "%<>>%> should be %<> >%> "
27947 "within a nested template argument list");
27948
27949 token->type = CPP_GREATER;
27950 }
27951 else
27952 {
27953 /* If this is not a nested template argument list, the '>>'
27954 is a typo for '>'. Emit an error message and continue.
27955 Same deal about the token location, but here we can get it
27956 right by consuming the '>>' before issuing the diagnostic. */
27957 cp_token *token = cp_lexer_consume_token (parser->lexer);
27958 error_at (token->location,
27959 "spurious %<>>%>, use %<>%> to terminate "
27960 "a template argument list");
27961 }
27962 }
27963 else
27964 cp_parser_skip_to_end_of_template_parameter_list (parser);
27965 /* The `>' token might be a greater-than operator again now. */
27966 parser->greater_than_is_operator_p
27967 = saved_greater_than_is_operator_p;
27968 /* Restore the SAVED_SCOPE. */
27969 parser->scope = saved_scope;
27970 parser->qualifying_scope = saved_qualifying_scope;
27971 parser->object_scope = saved_object_scope;
27972
27973 return arguments;
27974 }
27975
27976 /* MEMBER_FUNCTION is a member function, or a friend. If default
27977 arguments, or the body of the function have not yet been parsed,
27978 parse them now. */
27979
27980 static void
27981 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27982 {
27983 timevar_push (TV_PARSE_INMETH);
27984 /* If this member is a template, get the underlying
27985 FUNCTION_DECL. */
27986 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27987 member_function = DECL_TEMPLATE_RESULT (member_function);
27988
27989 /* There should not be any class definitions in progress at this
27990 point; the bodies of members are only parsed outside of all class
27991 definitions. */
27992 gcc_assert (parser->num_classes_being_defined == 0);
27993 /* While we're parsing the member functions we might encounter more
27994 classes. We want to handle them right away, but we don't want
27995 them getting mixed up with functions that are currently in the
27996 queue. */
27997 push_unparsed_function_queues (parser);
27998
27999 /* Make sure that any template parameters are in scope. */
28000 maybe_begin_member_template_processing (member_function);
28001
28002 /* If the body of the function has not yet been parsed, parse it
28003 now. */
28004 if (DECL_PENDING_INLINE_P (member_function))
28005 {
28006 tree function_scope;
28007 cp_token_cache *tokens;
28008
28009 /* The function is no longer pending; we are processing it. */
28010 tokens = DECL_PENDING_INLINE_INFO (member_function);
28011 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28012 DECL_PENDING_INLINE_P (member_function) = 0;
28013
28014 /* If this is a local class, enter the scope of the containing
28015 function. */
28016 function_scope = current_function_decl;
28017 if (function_scope)
28018 push_function_context ();
28019
28020 /* Push the body of the function onto the lexer stack. */
28021 cp_parser_push_lexer_for_tokens (parser, tokens);
28022
28023 /* Let the front end know that we going to be defining this
28024 function. */
28025 start_preparsed_function (member_function, NULL_TREE,
28026 SF_PRE_PARSED | SF_INCLASS_INLINE);
28027
28028 /* Don't do access checking if it is a templated function. */
28029 if (processing_template_decl)
28030 push_deferring_access_checks (dk_no_check);
28031
28032 /* #pragma omp declare reduction needs special parsing. */
28033 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28034 {
28035 parser->lexer->in_pragma = true;
28036 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28037 finish_function (/*inline_p=*/true);
28038 cp_check_omp_declare_reduction (member_function);
28039 }
28040 else
28041 /* Now, parse the body of the function. */
28042 cp_parser_function_definition_after_declarator (parser,
28043 /*inline_p=*/true);
28044
28045 if (processing_template_decl)
28046 pop_deferring_access_checks ();
28047
28048 /* Leave the scope of the containing function. */
28049 if (function_scope)
28050 pop_function_context ();
28051 cp_parser_pop_lexer (parser);
28052 }
28053
28054 /* Remove any template parameters from the symbol table. */
28055 maybe_end_member_template_processing ();
28056
28057 /* Restore the queue. */
28058 pop_unparsed_function_queues (parser);
28059 timevar_pop (TV_PARSE_INMETH);
28060 }
28061
28062 /* If DECL contains any default args, remember it on the unparsed
28063 functions queue. */
28064
28065 static void
28066 cp_parser_save_default_args (cp_parser* parser, tree decl)
28067 {
28068 tree probe;
28069
28070 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28071 probe;
28072 probe = TREE_CHAIN (probe))
28073 if (TREE_PURPOSE (probe))
28074 {
28075 cp_default_arg_entry entry = {current_class_type, decl};
28076 vec_safe_push (unparsed_funs_with_default_args, entry);
28077 break;
28078 }
28079 }
28080
28081 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28082 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28083 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28084 from the parameter-type-list. */
28085
28086 static tree
28087 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28088 tree default_arg, tree parmtype)
28089 {
28090 cp_token_cache *tokens;
28091 tree parsed_arg;
28092 bool dummy;
28093
28094 if (default_arg == error_mark_node)
28095 return error_mark_node;
28096
28097 /* Push the saved tokens for the default argument onto the parser's
28098 lexer stack. */
28099 tokens = DEFARG_TOKENS (default_arg);
28100 cp_parser_push_lexer_for_tokens (parser, tokens);
28101
28102 start_lambda_scope (decl);
28103
28104 /* Parse the default argument. */
28105 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28106 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28107 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28108
28109 finish_lambda_scope ();
28110
28111 if (parsed_arg == error_mark_node)
28112 cp_parser_skip_to_end_of_statement (parser);
28113
28114 if (!processing_template_decl)
28115 {
28116 /* In a non-template class, check conversions now. In a template,
28117 we'll wait and instantiate these as needed. */
28118 if (TREE_CODE (decl) == PARM_DECL)
28119 parsed_arg = check_default_argument (parmtype, parsed_arg,
28120 tf_warning_or_error);
28121 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28122 parsed_arg = error_mark_node;
28123 else
28124 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28125 }
28126
28127 /* If the token stream has not been completely used up, then
28128 there was extra junk after the end of the default
28129 argument. */
28130 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28131 {
28132 if (TREE_CODE (decl) == PARM_DECL)
28133 cp_parser_error (parser, "expected %<,%>");
28134 else
28135 cp_parser_error (parser, "expected %<;%>");
28136 }
28137
28138 /* Revert to the main lexer. */
28139 cp_parser_pop_lexer (parser);
28140
28141 return parsed_arg;
28142 }
28143
28144 /* FIELD is a non-static data member with an initializer which we saved for
28145 later; parse it now. */
28146
28147 static void
28148 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28149 {
28150 tree def;
28151
28152 maybe_begin_member_template_processing (field);
28153
28154 push_unparsed_function_queues (parser);
28155 def = cp_parser_late_parse_one_default_arg (parser, field,
28156 DECL_INITIAL (field),
28157 NULL_TREE);
28158 pop_unparsed_function_queues (parser);
28159
28160 maybe_end_member_template_processing ();
28161
28162 DECL_INITIAL (field) = def;
28163 }
28164
28165 /* FN is a FUNCTION_DECL which may contains a parameter with an
28166 unparsed DEFAULT_ARG. Parse the default args now. This function
28167 assumes that the current scope is the scope in which the default
28168 argument should be processed. */
28169
28170 static void
28171 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28172 {
28173 bool saved_local_variables_forbidden_p;
28174 tree parm, parmdecl;
28175
28176 /* While we're parsing the default args, we might (due to the
28177 statement expression extension) encounter more classes. We want
28178 to handle them right away, but we don't want them getting mixed
28179 up with default args that are currently in the queue. */
28180 push_unparsed_function_queues (parser);
28181
28182 /* Local variable names (and the `this' keyword) may not appear
28183 in a default argument. */
28184 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28185 parser->local_variables_forbidden_p = true;
28186
28187 push_defarg_context (fn);
28188
28189 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28190 parmdecl = DECL_ARGUMENTS (fn);
28191 parm && parm != void_list_node;
28192 parm = TREE_CHAIN (parm),
28193 parmdecl = DECL_CHAIN (parmdecl))
28194 {
28195 tree default_arg = TREE_PURPOSE (parm);
28196 tree parsed_arg;
28197 vec<tree, va_gc> *insts;
28198 tree copy;
28199 unsigned ix;
28200
28201 if (!default_arg)
28202 continue;
28203
28204 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28205 /* This can happen for a friend declaration for a function
28206 already declared with default arguments. */
28207 continue;
28208
28209 parsed_arg
28210 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28211 default_arg,
28212 TREE_VALUE (parm));
28213 TREE_PURPOSE (parm) = parsed_arg;
28214
28215 /* Update any instantiations we've already created. */
28216 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28217 vec_safe_iterate (insts, ix, &copy); ix++)
28218 TREE_PURPOSE (copy) = parsed_arg;
28219 }
28220
28221 pop_defarg_context ();
28222
28223 /* Make sure no default arg is missing. */
28224 check_default_args (fn);
28225
28226 /* Restore the state of local_variables_forbidden_p. */
28227 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28228
28229 /* Restore the queue. */
28230 pop_unparsed_function_queues (parser);
28231 }
28232
28233 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28234
28235 sizeof ... ( identifier )
28236
28237 where the 'sizeof' token has already been consumed. */
28238
28239 static tree
28240 cp_parser_sizeof_pack (cp_parser *parser)
28241 {
28242 /* Consume the `...'. */
28243 cp_lexer_consume_token (parser->lexer);
28244 maybe_warn_variadic_templates ();
28245
28246 matching_parens parens;
28247 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28248 if (paren)
28249 parens.consume_open (parser);
28250 else
28251 permerror (cp_lexer_peek_token (parser->lexer)->location,
28252 "%<sizeof...%> argument must be surrounded by parentheses");
28253
28254 cp_token *token = cp_lexer_peek_token (parser->lexer);
28255 tree name = cp_parser_identifier (parser);
28256 if (name == error_mark_node)
28257 return error_mark_node;
28258 /* The name is not qualified. */
28259 parser->scope = NULL_TREE;
28260 parser->qualifying_scope = NULL_TREE;
28261 parser->object_scope = NULL_TREE;
28262 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28263 if (expr == error_mark_node)
28264 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28265 token->location);
28266 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28267 expr = TREE_TYPE (expr);
28268 else if (TREE_CODE (expr) == CONST_DECL)
28269 expr = DECL_INITIAL (expr);
28270 expr = make_pack_expansion (expr);
28271 PACK_EXPANSION_SIZEOF_P (expr) = true;
28272
28273 if (paren)
28274 parens.require_close (parser);
28275
28276 return expr;
28277 }
28278
28279 /* Parse the operand of `sizeof' (or a similar operator). Returns
28280 either a TYPE or an expression, depending on the form of the
28281 input. The KEYWORD indicates which kind of expression we have
28282 encountered. */
28283
28284 static tree
28285 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28286 {
28287 tree expr = NULL_TREE;
28288 const char *saved_message;
28289 char *tmp;
28290 bool saved_integral_constant_expression_p;
28291 bool saved_non_integral_constant_expression_p;
28292
28293 /* If it's a `...', then we are computing the length of a parameter
28294 pack. */
28295 if (keyword == RID_SIZEOF
28296 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28297 return cp_parser_sizeof_pack (parser);
28298
28299 /* Types cannot be defined in a `sizeof' expression. Save away the
28300 old message. */
28301 saved_message = parser->type_definition_forbidden_message;
28302 /* And create the new one. */
28303 tmp = concat ("types may not be defined in %<",
28304 IDENTIFIER_POINTER (ridpointers[keyword]),
28305 "%> expressions", NULL);
28306 parser->type_definition_forbidden_message = tmp;
28307
28308 /* The restrictions on constant-expressions do not apply inside
28309 sizeof expressions. */
28310 saved_integral_constant_expression_p
28311 = parser->integral_constant_expression_p;
28312 saved_non_integral_constant_expression_p
28313 = parser->non_integral_constant_expression_p;
28314 parser->integral_constant_expression_p = false;
28315
28316 /* Do not actually evaluate the expression. */
28317 ++cp_unevaluated_operand;
28318 ++c_inhibit_evaluation_warnings;
28319 /* If it's a `(', then we might be looking at the type-id
28320 construction. */
28321 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28322 {
28323 tree type = NULL_TREE;
28324
28325 /* We can't be sure yet whether we're looking at a type-id or an
28326 expression. */
28327 cp_parser_parse_tentatively (parser);
28328
28329 matching_parens parens;
28330 parens.consume_open (parser);
28331
28332 /* Note: as a GNU Extension, compound literals are considered
28333 postfix-expressions as they are in C99, so they are valid
28334 arguments to sizeof. See comment in cp_parser_cast_expression
28335 for details. */
28336 if (cp_parser_compound_literal_p (parser))
28337 cp_parser_simulate_error (parser);
28338 else
28339 {
28340 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28341 parser->in_type_id_in_expr_p = true;
28342 /* Look for the type-id. */
28343 type = cp_parser_type_id (parser);
28344 /* Look for the closing `)'. */
28345 parens.require_close (parser);
28346 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28347 }
28348
28349 /* If all went well, then we're done. */
28350 if (cp_parser_parse_definitely (parser))
28351 expr = type;
28352 }
28353
28354 /* If the type-id production did not work out, then we must be
28355 looking at the unary-expression production. */
28356 if (!expr)
28357 expr = cp_parser_unary_expression (parser);
28358
28359 /* Go back to evaluating expressions. */
28360 --cp_unevaluated_operand;
28361 --c_inhibit_evaluation_warnings;
28362
28363 /* Free the message we created. */
28364 free (tmp);
28365 /* And restore the old one. */
28366 parser->type_definition_forbidden_message = saved_message;
28367 parser->integral_constant_expression_p
28368 = saved_integral_constant_expression_p;
28369 parser->non_integral_constant_expression_p
28370 = saved_non_integral_constant_expression_p;
28371
28372 return expr;
28373 }
28374
28375 /* If the current declaration has no declarator, return true. */
28376
28377 static bool
28378 cp_parser_declares_only_class_p (cp_parser *parser)
28379 {
28380 /* If the next token is a `;' or a `,' then there is no
28381 declarator. */
28382 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28383 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28384 }
28385
28386 /* Update the DECL_SPECS to reflect the storage class indicated by
28387 KEYWORD. */
28388
28389 static void
28390 cp_parser_set_storage_class (cp_parser *parser,
28391 cp_decl_specifier_seq *decl_specs,
28392 enum rid keyword,
28393 cp_token *token)
28394 {
28395 cp_storage_class storage_class;
28396
28397 if (parser->in_unbraced_linkage_specification_p)
28398 {
28399 error_at (token->location, "invalid use of %qD in linkage specification",
28400 ridpointers[keyword]);
28401 return;
28402 }
28403 else if (decl_specs->storage_class != sc_none)
28404 {
28405 decl_specs->conflicting_specifiers_p = true;
28406 return;
28407 }
28408
28409 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28410 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28411 && decl_specs->gnu_thread_keyword_p)
28412 {
28413 pedwarn (decl_specs->locations[ds_thread], 0,
28414 "%<__thread%> before %qD", ridpointers[keyword]);
28415 }
28416
28417 switch (keyword)
28418 {
28419 case RID_AUTO:
28420 storage_class = sc_auto;
28421 break;
28422 case RID_REGISTER:
28423 storage_class = sc_register;
28424 break;
28425 case RID_STATIC:
28426 storage_class = sc_static;
28427 break;
28428 case RID_EXTERN:
28429 storage_class = sc_extern;
28430 break;
28431 case RID_MUTABLE:
28432 storage_class = sc_mutable;
28433 break;
28434 default:
28435 gcc_unreachable ();
28436 }
28437 decl_specs->storage_class = storage_class;
28438 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28439
28440 /* A storage class specifier cannot be applied alongside a typedef
28441 specifier. If there is a typedef specifier present then set
28442 conflicting_specifiers_p which will trigger an error later
28443 on in grokdeclarator. */
28444 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28445 decl_specs->conflicting_specifiers_p = true;
28446 }
28447
28448 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28449 is true, the type is a class or enum definition. */
28450
28451 static void
28452 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28453 tree type_spec,
28454 cp_token *token,
28455 bool type_definition_p)
28456 {
28457 decl_specs->any_specifiers_p = true;
28458
28459 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28460 (with, for example, in "typedef int wchar_t;") we remember that
28461 this is what happened. In system headers, we ignore these
28462 declarations so that G++ can work with system headers that are not
28463 C++-safe. */
28464 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28465 && !type_definition_p
28466 && (type_spec == boolean_type_node
28467 || type_spec == char16_type_node
28468 || type_spec == char32_type_node
28469 || type_spec == wchar_type_node)
28470 && (decl_specs->type
28471 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28472 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28473 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28474 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28475 {
28476 decl_specs->redefined_builtin_type = type_spec;
28477 set_and_check_decl_spec_loc (decl_specs,
28478 ds_redefined_builtin_type_spec,
28479 token);
28480 if (!decl_specs->type)
28481 {
28482 decl_specs->type = type_spec;
28483 decl_specs->type_definition_p = false;
28484 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28485 }
28486 }
28487 else if (decl_specs->type)
28488 decl_specs->multiple_types_p = true;
28489 else
28490 {
28491 decl_specs->type = type_spec;
28492 decl_specs->type_definition_p = type_definition_p;
28493 decl_specs->redefined_builtin_type = NULL_TREE;
28494 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28495 }
28496 }
28497
28498 /* True iff TOKEN is the GNU keyword __thread. */
28499
28500 static bool
28501 token_is__thread (cp_token *token)
28502 {
28503 gcc_assert (token->keyword == RID_THREAD);
28504 return id_equal (token->u.value, "__thread");
28505 }
28506
28507 /* Set the location for a declarator specifier and check if it is
28508 duplicated.
28509
28510 DECL_SPECS is the sequence of declarator specifiers onto which to
28511 set the location.
28512
28513 DS is the single declarator specifier to set which location is to
28514 be set onto the existing sequence of declarators.
28515
28516 LOCATION is the location for the declarator specifier to
28517 consider. */
28518
28519 static void
28520 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28521 cp_decl_spec ds, cp_token *token)
28522 {
28523 gcc_assert (ds < ds_last);
28524
28525 if (decl_specs == NULL)
28526 return;
28527
28528 source_location location = token->location;
28529
28530 if (decl_specs->locations[ds] == 0)
28531 {
28532 decl_specs->locations[ds] = location;
28533 if (ds == ds_thread)
28534 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28535 }
28536 else
28537 {
28538 if (ds == ds_long)
28539 {
28540 if (decl_specs->locations[ds_long_long] != 0)
28541 error_at (location,
28542 "%<long long long%> is too long for GCC");
28543 else
28544 {
28545 decl_specs->locations[ds_long_long] = location;
28546 pedwarn_cxx98 (location,
28547 OPT_Wlong_long,
28548 "ISO C++ 1998 does not support %<long long%>");
28549 }
28550 }
28551 else if (ds == ds_thread)
28552 {
28553 bool gnu = token_is__thread (token);
28554 gcc_rich_location richloc (location);
28555 if (gnu != decl_specs->gnu_thread_keyword_p)
28556 {
28557 richloc.add_range (decl_specs->locations[ds_thread]);
28558 error_at (&richloc,
28559 "both %<__thread%> and %<thread_local%> specified");
28560 }
28561 else
28562 {
28563 richloc.add_fixit_remove ();
28564 error_at (&richloc, "duplicate %qD", token->u.value);
28565 }
28566 }
28567 else
28568 {
28569 static const char *const decl_spec_names[] = {
28570 "signed",
28571 "unsigned",
28572 "short",
28573 "long",
28574 "const",
28575 "volatile",
28576 "restrict",
28577 "inline",
28578 "virtual",
28579 "explicit",
28580 "friend",
28581 "typedef",
28582 "using",
28583 "constexpr",
28584 "__complex"
28585 };
28586 gcc_rich_location richloc (location);
28587 richloc.add_fixit_remove ();
28588 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28589 }
28590 }
28591 }
28592
28593 /* Return true iff the declarator specifier DS is present in the
28594 sequence of declarator specifiers DECL_SPECS. */
28595
28596 bool
28597 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28598 cp_decl_spec ds)
28599 {
28600 gcc_assert (ds < ds_last);
28601
28602 if (decl_specs == NULL)
28603 return false;
28604
28605 return decl_specs->locations[ds] != 0;
28606 }
28607
28608 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28609 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28610
28611 static bool
28612 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28613 {
28614 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28615 }
28616
28617 /* Issue an error message indicating that TOKEN_DESC was expected.
28618 If KEYWORD is true, it indicated this function is called by
28619 cp_parser_require_keword and the required token can only be
28620 a indicated keyword.
28621
28622 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28623 within any error as the location of an "opening" token matching
28624 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28625 RT_CLOSE_PAREN). */
28626
28627 static void
28628 cp_parser_required_error (cp_parser *parser,
28629 required_token token_desc,
28630 bool keyword,
28631 location_t matching_location)
28632 {
28633 if (cp_parser_simulate_error (parser))
28634 return;
28635
28636 const char *gmsgid = NULL;
28637 switch (token_desc)
28638 {
28639 case RT_NEW:
28640 gmsgid = G_("expected %<new%>");
28641 break;
28642 case RT_DELETE:
28643 gmsgid = G_("expected %<delete%>");
28644 break;
28645 case RT_RETURN:
28646 gmsgid = G_("expected %<return%>");
28647 break;
28648 case RT_WHILE:
28649 gmsgid = G_("expected %<while%>");
28650 break;
28651 case RT_EXTERN:
28652 gmsgid = G_("expected %<extern%>");
28653 break;
28654 case RT_STATIC_ASSERT:
28655 gmsgid = G_("expected %<static_assert%>");
28656 break;
28657 case RT_DECLTYPE:
28658 gmsgid = G_("expected %<decltype%>");
28659 break;
28660 case RT_OPERATOR:
28661 gmsgid = G_("expected %<operator%>");
28662 break;
28663 case RT_CLASS:
28664 gmsgid = G_("expected %<class%>");
28665 break;
28666 case RT_TEMPLATE:
28667 gmsgid = G_("expected %<template%>");
28668 break;
28669 case RT_NAMESPACE:
28670 gmsgid = G_("expected %<namespace%>");
28671 break;
28672 case RT_USING:
28673 gmsgid = G_("expected %<using%>");
28674 break;
28675 case RT_ASM:
28676 gmsgid = G_("expected %<asm%>");
28677 break;
28678 case RT_TRY:
28679 gmsgid = G_("expected %<try%>");
28680 break;
28681 case RT_CATCH:
28682 gmsgid = G_("expected %<catch%>");
28683 break;
28684 case RT_THROW:
28685 gmsgid = G_("expected %<throw%>");
28686 break;
28687 case RT_LABEL:
28688 gmsgid = G_("expected %<__label__%>");
28689 break;
28690 case RT_AT_TRY:
28691 gmsgid = G_("expected %<@try%>");
28692 break;
28693 case RT_AT_SYNCHRONIZED:
28694 gmsgid = G_("expected %<@synchronized%>");
28695 break;
28696 case RT_AT_THROW:
28697 gmsgid = G_("expected %<@throw%>");
28698 break;
28699 case RT_TRANSACTION_ATOMIC:
28700 gmsgid = G_("expected %<__transaction_atomic%>");
28701 break;
28702 case RT_TRANSACTION_RELAXED:
28703 gmsgid = G_("expected %<__transaction_relaxed%>");
28704 break;
28705 default:
28706 break;
28707 }
28708
28709 if (!gmsgid && !keyword)
28710 {
28711 switch (token_desc)
28712 {
28713 case RT_SEMICOLON:
28714 gmsgid = G_("expected %<;%>");
28715 break;
28716 case RT_OPEN_PAREN:
28717 gmsgid = G_("expected %<(%>");
28718 break;
28719 case RT_CLOSE_BRACE:
28720 gmsgid = G_("expected %<}%>");
28721 break;
28722 case RT_OPEN_BRACE:
28723 gmsgid = G_("expected %<{%>");
28724 break;
28725 case RT_CLOSE_SQUARE:
28726 gmsgid = G_("expected %<]%>");
28727 break;
28728 case RT_OPEN_SQUARE:
28729 gmsgid = G_("expected %<[%>");
28730 break;
28731 case RT_COMMA:
28732 gmsgid = G_("expected %<,%>");
28733 break;
28734 case RT_SCOPE:
28735 gmsgid = G_("expected %<::%>");
28736 break;
28737 case RT_LESS:
28738 gmsgid = G_("expected %<<%>");
28739 break;
28740 case RT_GREATER:
28741 gmsgid = G_("expected %<>%>");
28742 break;
28743 case RT_EQ:
28744 gmsgid = G_("expected %<=%>");
28745 break;
28746 case RT_ELLIPSIS:
28747 gmsgid = G_("expected %<...%>");
28748 break;
28749 case RT_MULT:
28750 gmsgid = G_("expected %<*%>");
28751 break;
28752 case RT_COMPL:
28753 gmsgid = G_("expected %<~%>");
28754 break;
28755 case RT_COLON:
28756 gmsgid = G_("expected %<:%>");
28757 break;
28758 case RT_COLON_SCOPE:
28759 gmsgid = G_("expected %<:%> or %<::%>");
28760 break;
28761 case RT_CLOSE_PAREN:
28762 gmsgid = G_("expected %<)%>");
28763 break;
28764 case RT_COMMA_CLOSE_PAREN:
28765 gmsgid = G_("expected %<,%> or %<)%>");
28766 break;
28767 case RT_PRAGMA_EOL:
28768 gmsgid = G_("expected end of line");
28769 break;
28770 case RT_NAME:
28771 gmsgid = G_("expected identifier");
28772 break;
28773 case RT_SELECT:
28774 gmsgid = G_("expected selection-statement");
28775 break;
28776 case RT_ITERATION:
28777 gmsgid = G_("expected iteration-statement");
28778 break;
28779 case RT_JUMP:
28780 gmsgid = G_("expected jump-statement");
28781 break;
28782 case RT_CLASS_KEY:
28783 gmsgid = G_("expected class-key");
28784 break;
28785 case RT_CLASS_TYPENAME_TEMPLATE:
28786 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28787 break;
28788 default:
28789 gcc_unreachable ();
28790 }
28791 }
28792
28793 if (gmsgid)
28794 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28795 }
28796
28797
28798 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28799 issue an error message indicating that TOKEN_DESC was expected.
28800
28801 Returns the token consumed, if the token had the appropriate type.
28802 Otherwise, returns NULL.
28803
28804 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28805 within any error as the location of an "opening" token matching
28806 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28807 RT_CLOSE_PAREN). */
28808
28809 static cp_token *
28810 cp_parser_require (cp_parser* parser,
28811 enum cpp_ttype type,
28812 required_token token_desc,
28813 location_t matching_location)
28814 {
28815 if (cp_lexer_next_token_is (parser->lexer, type))
28816 return cp_lexer_consume_token (parser->lexer);
28817 else
28818 {
28819 /* Output the MESSAGE -- unless we're parsing tentatively. */
28820 if (!cp_parser_simulate_error (parser))
28821 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28822 matching_location);
28823 return NULL;
28824 }
28825 }
28826
28827 /* An error message is produced if the next token is not '>'.
28828 All further tokens are skipped until the desired token is
28829 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28830
28831 static void
28832 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28833 {
28834 /* Current level of '< ... >'. */
28835 unsigned level = 0;
28836 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28837 unsigned nesting_depth = 0;
28838
28839 /* Are we ready, yet? If not, issue error message. */
28840 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28841 return;
28842
28843 /* Skip tokens until the desired token is found. */
28844 while (true)
28845 {
28846 /* Peek at the next token. */
28847 switch (cp_lexer_peek_token (parser->lexer)->type)
28848 {
28849 case CPP_LESS:
28850 if (!nesting_depth)
28851 ++level;
28852 break;
28853
28854 case CPP_RSHIFT:
28855 if (cxx_dialect == cxx98)
28856 /* C++0x views the `>>' operator as two `>' tokens, but
28857 C++98 does not. */
28858 break;
28859 else if (!nesting_depth && level-- == 0)
28860 {
28861 /* We've hit a `>>' where the first `>' closes the
28862 template argument list, and the second `>' is
28863 spurious. Just consume the `>>' and stop; we've
28864 already produced at least one error. */
28865 cp_lexer_consume_token (parser->lexer);
28866 return;
28867 }
28868 /* Fall through for C++0x, so we handle the second `>' in
28869 the `>>'. */
28870 gcc_fallthrough ();
28871
28872 case CPP_GREATER:
28873 if (!nesting_depth && level-- == 0)
28874 {
28875 /* We've reached the token we want, consume it and stop. */
28876 cp_lexer_consume_token (parser->lexer);
28877 return;
28878 }
28879 break;
28880
28881 case CPP_OPEN_PAREN:
28882 case CPP_OPEN_SQUARE:
28883 ++nesting_depth;
28884 break;
28885
28886 case CPP_CLOSE_PAREN:
28887 case CPP_CLOSE_SQUARE:
28888 if (nesting_depth-- == 0)
28889 return;
28890 break;
28891
28892 case CPP_EOF:
28893 case CPP_PRAGMA_EOL:
28894 case CPP_SEMICOLON:
28895 case CPP_OPEN_BRACE:
28896 case CPP_CLOSE_BRACE:
28897 /* The '>' was probably forgotten, don't look further. */
28898 return;
28899
28900 default:
28901 break;
28902 }
28903
28904 /* Consume this token. */
28905 cp_lexer_consume_token (parser->lexer);
28906 }
28907 }
28908
28909 /* If the next token is the indicated keyword, consume it. Otherwise,
28910 issue an error message indicating that TOKEN_DESC was expected.
28911
28912 Returns the token consumed, if the token had the appropriate type.
28913 Otherwise, returns NULL. */
28914
28915 static cp_token *
28916 cp_parser_require_keyword (cp_parser* parser,
28917 enum rid keyword,
28918 required_token token_desc)
28919 {
28920 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28921
28922 if (token && token->keyword != keyword)
28923 {
28924 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28925 UNKNOWN_LOCATION);
28926 return NULL;
28927 }
28928
28929 return token;
28930 }
28931
28932 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28933 function-definition. */
28934
28935 static bool
28936 cp_parser_token_starts_function_definition_p (cp_token* token)
28937 {
28938 return (/* An ordinary function-body begins with an `{'. */
28939 token->type == CPP_OPEN_BRACE
28940 /* A ctor-initializer begins with a `:'. */
28941 || token->type == CPP_COLON
28942 /* A function-try-block begins with `try'. */
28943 || token->keyword == RID_TRY
28944 /* A function-transaction-block begins with `__transaction_atomic'
28945 or `__transaction_relaxed'. */
28946 || token->keyword == RID_TRANSACTION_ATOMIC
28947 || token->keyword == RID_TRANSACTION_RELAXED
28948 /* The named return value extension begins with `return'. */
28949 || token->keyword == RID_RETURN);
28950 }
28951
28952 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28953 definition. */
28954
28955 static bool
28956 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28957 {
28958 cp_token *token;
28959
28960 token = cp_lexer_peek_token (parser->lexer);
28961 return (token->type == CPP_OPEN_BRACE
28962 || (token->type == CPP_COLON
28963 && !parser->colon_doesnt_start_class_def_p));
28964 }
28965
28966 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28967 C++0x) ending a template-argument. */
28968
28969 static bool
28970 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28971 {
28972 cp_token *token;
28973
28974 token = cp_lexer_peek_token (parser->lexer);
28975 return (token->type == CPP_COMMA
28976 || token->type == CPP_GREATER
28977 || token->type == CPP_ELLIPSIS
28978 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28979 }
28980
28981 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28982 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28983
28984 static bool
28985 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28986 size_t n)
28987 {
28988 cp_token *token;
28989
28990 token = cp_lexer_peek_nth_token (parser->lexer, n);
28991 if (token->type == CPP_LESS)
28992 return true;
28993 /* Check for the sequence `<::' in the original code. It would be lexed as
28994 `[:', where `[' is a digraph, and there is no whitespace before
28995 `:'. */
28996 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28997 {
28998 cp_token *token2;
28999 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29000 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29001 return true;
29002 }
29003 return false;
29004 }
29005
29006 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29007 or none_type otherwise. */
29008
29009 static enum tag_types
29010 cp_parser_token_is_class_key (cp_token* token)
29011 {
29012 switch (token->keyword)
29013 {
29014 case RID_CLASS:
29015 return class_type;
29016 case RID_STRUCT:
29017 return record_type;
29018 case RID_UNION:
29019 return union_type;
29020
29021 default:
29022 return none_type;
29023 }
29024 }
29025
29026 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29027 or none_type otherwise or if the token is null. */
29028
29029 static enum tag_types
29030 cp_parser_token_is_type_parameter_key (cp_token* token)
29031 {
29032 if (!token)
29033 return none_type;
29034
29035 switch (token->keyword)
29036 {
29037 case RID_CLASS:
29038 return class_type;
29039 case RID_TYPENAME:
29040 return typename_type;
29041
29042 default:
29043 return none_type;
29044 }
29045 }
29046
29047 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29048
29049 static void
29050 cp_parser_check_class_key (enum tag_types class_key, tree type)
29051 {
29052 if (type == error_mark_node)
29053 return;
29054 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29055 {
29056 if (permerror (input_location, "%qs tag used in naming %q#T",
29057 class_key == union_type ? "union"
29058 : class_key == record_type ? "struct" : "class",
29059 type))
29060 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29061 "%q#T was previously declared here", type);
29062 }
29063 }
29064
29065 /* Issue an error message if DECL is redeclared with different
29066 access than its original declaration [class.access.spec/3].
29067 This applies to nested classes, nested class templates and
29068 enumerations [class.mem/1]. */
29069
29070 static void
29071 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29072 {
29073 if (!decl
29074 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29075 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29076 return;
29077
29078 if ((TREE_PRIVATE (decl)
29079 != (current_access_specifier == access_private_node))
29080 || (TREE_PROTECTED (decl)
29081 != (current_access_specifier == access_protected_node)))
29082 error_at (location, "%qD redeclared with different access", decl);
29083 }
29084
29085 /* Look for the `template' keyword, as a syntactic disambiguator.
29086 Return TRUE iff it is present, in which case it will be
29087 consumed. */
29088
29089 static bool
29090 cp_parser_optional_template_keyword (cp_parser *parser)
29091 {
29092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29093 {
29094 /* In C++98 the `template' keyword can only be used within templates;
29095 outside templates the parser can always figure out what is a
29096 template and what is not. In C++11, per the resolution of DR 468,
29097 `template' is allowed in cases where it is not strictly necessary. */
29098 if (!processing_template_decl
29099 && pedantic && cxx_dialect == cxx98)
29100 {
29101 cp_token *token = cp_lexer_peek_token (parser->lexer);
29102 pedwarn (token->location, OPT_Wpedantic,
29103 "in C++98 %<template%> (as a disambiguator) is only "
29104 "allowed within templates");
29105 /* If this part of the token stream is rescanned, the same
29106 error message would be generated. So, we purge the token
29107 from the stream. */
29108 cp_lexer_purge_token (parser->lexer);
29109 return false;
29110 }
29111 else
29112 {
29113 /* Consume the `template' keyword. */
29114 cp_lexer_consume_token (parser->lexer);
29115 return true;
29116 }
29117 }
29118 return false;
29119 }
29120
29121 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29122 set PARSER->SCOPE, and perform other related actions. */
29123
29124 static void
29125 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29126 {
29127 struct tree_check *check_value;
29128
29129 /* Get the stored value. */
29130 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29131 /* Set the scope from the stored value. */
29132 parser->scope = saved_checks_value (check_value);
29133 parser->qualifying_scope = check_value->qualifying_scope;
29134 parser->object_scope = NULL_TREE;
29135 }
29136
29137 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29138 encounter the end of a block before what we were looking for. */
29139
29140 static bool
29141 cp_parser_cache_group (cp_parser *parser,
29142 enum cpp_ttype end,
29143 unsigned depth)
29144 {
29145 while (true)
29146 {
29147 cp_token *token = cp_lexer_peek_token (parser->lexer);
29148
29149 /* Abort a parenthesized expression if we encounter a semicolon. */
29150 if ((end == CPP_CLOSE_PAREN || depth == 0)
29151 && token->type == CPP_SEMICOLON)
29152 return true;
29153 /* If we've reached the end of the file, stop. */
29154 if (token->type == CPP_EOF
29155 || (end != CPP_PRAGMA_EOL
29156 && token->type == CPP_PRAGMA_EOL))
29157 return true;
29158 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29159 /* We've hit the end of an enclosing block, so there's been some
29160 kind of syntax error. */
29161 return true;
29162
29163 /* Consume the token. */
29164 cp_lexer_consume_token (parser->lexer);
29165 /* See if it starts a new group. */
29166 if (token->type == CPP_OPEN_BRACE)
29167 {
29168 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29169 /* In theory this should probably check end == '}', but
29170 cp_parser_save_member_function_body needs it to exit
29171 after either '}' or ')' when called with ')'. */
29172 if (depth == 0)
29173 return false;
29174 }
29175 else if (token->type == CPP_OPEN_PAREN)
29176 {
29177 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29178 if (depth == 0 && end == CPP_CLOSE_PAREN)
29179 return false;
29180 }
29181 else if (token->type == CPP_PRAGMA)
29182 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29183 else if (token->type == end)
29184 return false;
29185 }
29186 }
29187
29188 /* Like above, for caching a default argument or NSDMI. Both of these are
29189 terminated by a non-nested comma, but it can be unclear whether or not a
29190 comma is nested in a template argument list unless we do more parsing.
29191 In order to handle this ambiguity, when we encounter a ',' after a '<'
29192 we try to parse what follows as a parameter-declaration-list (in the
29193 case of a default argument) or a member-declarator (in the case of an
29194 NSDMI). If that succeeds, then we stop caching. */
29195
29196 static tree
29197 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29198 {
29199 unsigned depth = 0;
29200 int maybe_template_id = 0;
29201 cp_token *first_token;
29202 cp_token *token;
29203 tree default_argument;
29204
29205 /* Add tokens until we have processed the entire default
29206 argument. We add the range [first_token, token). */
29207 first_token = cp_lexer_peek_token (parser->lexer);
29208 if (first_token->type == CPP_OPEN_BRACE)
29209 {
29210 /* For list-initialization, this is straightforward. */
29211 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29212 token = cp_lexer_peek_token (parser->lexer);
29213 }
29214 else while (true)
29215 {
29216 bool done = false;
29217
29218 /* Peek at the next token. */
29219 token = cp_lexer_peek_token (parser->lexer);
29220 /* What we do depends on what token we have. */
29221 switch (token->type)
29222 {
29223 /* In valid code, a default argument must be
29224 immediately followed by a `,' `)', or `...'. */
29225 case CPP_COMMA:
29226 if (depth == 0 && maybe_template_id)
29227 {
29228 /* If we've seen a '<', we might be in a
29229 template-argument-list. Until Core issue 325 is
29230 resolved, we don't know how this situation ought
29231 to be handled, so try to DTRT. We check whether
29232 what comes after the comma is a valid parameter
29233 declaration list. If it is, then the comma ends
29234 the default argument; otherwise the default
29235 argument continues. */
29236 bool error = false;
29237 cp_token *peek;
29238
29239 /* Set ITALP so cp_parser_parameter_declaration_list
29240 doesn't decide to commit to this parse. */
29241 bool saved_italp = parser->in_template_argument_list_p;
29242 parser->in_template_argument_list_p = true;
29243
29244 cp_parser_parse_tentatively (parser);
29245
29246 if (nsdmi)
29247 {
29248 /* Parse declarators until we reach a non-comma or
29249 somthing that cannot be an initializer.
29250 Just checking whether we're looking at a single
29251 declarator is insufficient. Consider:
29252 int var = tuple<T,U>::x;
29253 The template parameter 'U' looks exactly like a
29254 declarator. */
29255 do
29256 {
29257 int ctor_dtor_or_conv_p;
29258 cp_lexer_consume_token (parser->lexer);
29259 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29260 &ctor_dtor_or_conv_p,
29261 /*parenthesized_p=*/NULL,
29262 /*member_p=*/true,
29263 /*friend_p=*/false);
29264 peek = cp_lexer_peek_token (parser->lexer);
29265 if (cp_parser_error_occurred (parser))
29266 break;
29267 }
29268 while (peek->type == CPP_COMMA);
29269 /* If we met an '=' or ';' then the original comma
29270 was the end of the NSDMI. Otherwise assume
29271 we're still in the NSDMI. */
29272 error = (peek->type != CPP_EQ
29273 && peek->type != CPP_SEMICOLON);
29274 }
29275 else
29276 {
29277 cp_lexer_consume_token (parser->lexer);
29278 begin_scope (sk_function_parms, NULL_TREE);
29279 if (cp_parser_parameter_declaration_list (parser)
29280 == error_mark_node)
29281 error = true;
29282 pop_bindings_and_leave_scope ();
29283 }
29284 if (!cp_parser_error_occurred (parser) && !error)
29285 done = true;
29286 cp_parser_abort_tentative_parse (parser);
29287
29288 parser->in_template_argument_list_p = saved_italp;
29289 break;
29290 }
29291 /* FALLTHRU */
29292 case CPP_CLOSE_PAREN:
29293 case CPP_ELLIPSIS:
29294 /* If we run into a non-nested `;', `}', or `]',
29295 then the code is invalid -- but the default
29296 argument is certainly over. */
29297 case CPP_SEMICOLON:
29298 case CPP_CLOSE_BRACE:
29299 case CPP_CLOSE_SQUARE:
29300 if (depth == 0
29301 /* Handle correctly int n = sizeof ... ( p ); */
29302 && token->type != CPP_ELLIPSIS)
29303 done = true;
29304 /* Update DEPTH, if necessary. */
29305 else if (token->type == CPP_CLOSE_PAREN
29306 || token->type == CPP_CLOSE_BRACE
29307 || token->type == CPP_CLOSE_SQUARE)
29308 --depth;
29309 break;
29310
29311 case CPP_OPEN_PAREN:
29312 case CPP_OPEN_SQUARE:
29313 case CPP_OPEN_BRACE:
29314 ++depth;
29315 break;
29316
29317 case CPP_LESS:
29318 if (depth == 0)
29319 /* This might be the comparison operator, or it might
29320 start a template argument list. */
29321 ++maybe_template_id;
29322 break;
29323
29324 case CPP_RSHIFT:
29325 if (cxx_dialect == cxx98)
29326 break;
29327 /* Fall through for C++0x, which treats the `>>'
29328 operator like two `>' tokens in certain
29329 cases. */
29330 gcc_fallthrough ();
29331
29332 case CPP_GREATER:
29333 if (depth == 0)
29334 {
29335 /* This might be an operator, or it might close a
29336 template argument list. But if a previous '<'
29337 started a template argument list, this will have
29338 closed it, so we can't be in one anymore. */
29339 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29340 if (maybe_template_id < 0)
29341 maybe_template_id = 0;
29342 }
29343 break;
29344
29345 /* If we run out of tokens, issue an error message. */
29346 case CPP_EOF:
29347 case CPP_PRAGMA_EOL:
29348 error_at (token->location, "file ends in default argument");
29349 return error_mark_node;
29350
29351 case CPP_NAME:
29352 case CPP_SCOPE:
29353 /* In these cases, we should look for template-ids.
29354 For example, if the default argument is
29355 `X<int, double>()', we need to do name lookup to
29356 figure out whether or not `X' is a template; if
29357 so, the `,' does not end the default argument.
29358
29359 That is not yet done. */
29360 break;
29361
29362 default:
29363 break;
29364 }
29365
29366 /* If we've reached the end, stop. */
29367 if (done)
29368 break;
29369
29370 /* Add the token to the token block. */
29371 token = cp_lexer_consume_token (parser->lexer);
29372 }
29373
29374 /* Create a DEFAULT_ARG to represent the unparsed default
29375 argument. */
29376 default_argument = make_node (DEFAULT_ARG);
29377 DEFARG_TOKENS (default_argument)
29378 = cp_token_cache_new (first_token, token);
29379 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29380
29381 return default_argument;
29382 }
29383
29384 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29385
29386 location_t
29387 defarg_location (tree default_argument)
29388 {
29389 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29390 location_t start = tokens->first->location;
29391 location_t end = tokens->last->location;
29392 return make_location (start, start, end);
29393 }
29394
29395 /* Begin parsing tentatively. We always save tokens while parsing
29396 tentatively so that if the tentative parsing fails we can restore the
29397 tokens. */
29398
29399 static void
29400 cp_parser_parse_tentatively (cp_parser* parser)
29401 {
29402 /* Enter a new parsing context. */
29403 parser->context = cp_parser_context_new (parser->context);
29404 /* Begin saving tokens. */
29405 cp_lexer_save_tokens (parser->lexer);
29406 /* In order to avoid repetitive access control error messages,
29407 access checks are queued up until we are no longer parsing
29408 tentatively. */
29409 push_deferring_access_checks (dk_deferred);
29410 }
29411
29412 /* Commit to the currently active tentative parse. */
29413
29414 static void
29415 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29416 {
29417 cp_parser_context *context;
29418 cp_lexer *lexer;
29419
29420 /* Mark all of the levels as committed. */
29421 lexer = parser->lexer;
29422 for (context = parser->context; context->next; context = context->next)
29423 {
29424 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29425 break;
29426 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29427 while (!cp_lexer_saving_tokens (lexer))
29428 lexer = lexer->next;
29429 cp_lexer_commit_tokens (lexer);
29430 }
29431 }
29432
29433 /* Commit to the topmost currently active tentative parse.
29434
29435 Note that this function shouldn't be called when there are
29436 irreversible side-effects while in a tentative state. For
29437 example, we shouldn't create a permanent entry in the symbol
29438 table, or issue an error message that might not apply if the
29439 tentative parse is aborted. */
29440
29441 static void
29442 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29443 {
29444 cp_parser_context *context = parser->context;
29445 cp_lexer *lexer = parser->lexer;
29446
29447 if (context)
29448 {
29449 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29450 return;
29451 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29452
29453 while (!cp_lexer_saving_tokens (lexer))
29454 lexer = lexer->next;
29455 cp_lexer_commit_tokens (lexer);
29456 }
29457 }
29458
29459 /* Abort the currently active tentative parse. All consumed tokens
29460 will be rolled back, and no diagnostics will be issued. */
29461
29462 static void
29463 cp_parser_abort_tentative_parse (cp_parser* parser)
29464 {
29465 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29466 || errorcount > 0);
29467 cp_parser_simulate_error (parser);
29468 /* Now, pretend that we want to see if the construct was
29469 successfully parsed. */
29470 cp_parser_parse_definitely (parser);
29471 }
29472
29473 /* Stop parsing tentatively. If a parse error has occurred, restore the
29474 token stream. Otherwise, commit to the tokens we have consumed.
29475 Returns true if no error occurred; false otherwise. */
29476
29477 static bool
29478 cp_parser_parse_definitely (cp_parser* parser)
29479 {
29480 bool error_occurred;
29481 cp_parser_context *context;
29482
29483 /* Remember whether or not an error occurred, since we are about to
29484 destroy that information. */
29485 error_occurred = cp_parser_error_occurred (parser);
29486 /* Remove the topmost context from the stack. */
29487 context = parser->context;
29488 parser->context = context->next;
29489 /* If no parse errors occurred, commit to the tentative parse. */
29490 if (!error_occurred)
29491 {
29492 /* Commit to the tokens read tentatively, unless that was
29493 already done. */
29494 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29495 cp_lexer_commit_tokens (parser->lexer);
29496
29497 pop_to_parent_deferring_access_checks ();
29498 }
29499 /* Otherwise, if errors occurred, roll back our state so that things
29500 are just as they were before we began the tentative parse. */
29501 else
29502 {
29503 cp_lexer_rollback_tokens (parser->lexer);
29504 pop_deferring_access_checks ();
29505 }
29506 /* Add the context to the front of the free list. */
29507 context->next = cp_parser_context_free_list;
29508 cp_parser_context_free_list = context;
29509
29510 return !error_occurred;
29511 }
29512
29513 /* Returns true if we are parsing tentatively and are not committed to
29514 this tentative parse. */
29515
29516 static bool
29517 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29518 {
29519 return (cp_parser_parsing_tentatively (parser)
29520 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29521 }
29522
29523 /* Returns nonzero iff an error has occurred during the most recent
29524 tentative parse. */
29525
29526 static bool
29527 cp_parser_error_occurred (cp_parser* parser)
29528 {
29529 return (cp_parser_parsing_tentatively (parser)
29530 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29531 }
29532
29533 /* Returns nonzero if GNU extensions are allowed. */
29534
29535 static bool
29536 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29537 {
29538 return parser->allow_gnu_extensions_p;
29539 }
29540 \f
29541 /* Objective-C++ Productions */
29542
29543
29544 /* Parse an Objective-C expression, which feeds into a primary-expression
29545 above.
29546
29547 objc-expression:
29548 objc-message-expression
29549 objc-string-literal
29550 objc-encode-expression
29551 objc-protocol-expression
29552 objc-selector-expression
29553
29554 Returns a tree representation of the expression. */
29555
29556 static cp_expr
29557 cp_parser_objc_expression (cp_parser* parser)
29558 {
29559 /* Try to figure out what kind of declaration is present. */
29560 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29561
29562 switch (kwd->type)
29563 {
29564 case CPP_OPEN_SQUARE:
29565 return cp_parser_objc_message_expression (parser);
29566
29567 case CPP_OBJC_STRING:
29568 kwd = cp_lexer_consume_token (parser->lexer);
29569 return objc_build_string_object (kwd->u.value);
29570
29571 case CPP_KEYWORD:
29572 switch (kwd->keyword)
29573 {
29574 case RID_AT_ENCODE:
29575 return cp_parser_objc_encode_expression (parser);
29576
29577 case RID_AT_PROTOCOL:
29578 return cp_parser_objc_protocol_expression (parser);
29579
29580 case RID_AT_SELECTOR:
29581 return cp_parser_objc_selector_expression (parser);
29582
29583 default:
29584 break;
29585 }
29586 /* FALLTHRU */
29587 default:
29588 error_at (kwd->location,
29589 "misplaced %<@%D%> Objective-C++ construct",
29590 kwd->u.value);
29591 cp_parser_skip_to_end_of_block_or_statement (parser);
29592 }
29593
29594 return error_mark_node;
29595 }
29596
29597 /* Parse an Objective-C message expression.
29598
29599 objc-message-expression:
29600 [ objc-message-receiver objc-message-args ]
29601
29602 Returns a representation of an Objective-C message. */
29603
29604 static tree
29605 cp_parser_objc_message_expression (cp_parser* parser)
29606 {
29607 tree receiver, messageargs;
29608
29609 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29610 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29611 receiver = cp_parser_objc_message_receiver (parser);
29612 messageargs = cp_parser_objc_message_args (parser);
29613 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29614 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29615
29616 tree result = objc_build_message_expr (receiver, messageargs);
29617
29618 /* Construct a location e.g.
29619 [self func1:5]
29620 ^~~~~~~~~~~~~~
29621 ranging from the '[' to the ']', with the caret at the start. */
29622 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29623 protected_set_expr_location (result, combined_loc);
29624
29625 return result;
29626 }
29627
29628 /* Parse an objc-message-receiver.
29629
29630 objc-message-receiver:
29631 expression
29632 simple-type-specifier
29633
29634 Returns a representation of the type or expression. */
29635
29636 static tree
29637 cp_parser_objc_message_receiver (cp_parser* parser)
29638 {
29639 tree rcv;
29640
29641 /* An Objective-C message receiver may be either (1) a type
29642 or (2) an expression. */
29643 cp_parser_parse_tentatively (parser);
29644 rcv = cp_parser_expression (parser);
29645
29646 /* If that worked out, fine. */
29647 if (cp_parser_parse_definitely (parser))
29648 return rcv;
29649
29650 cp_parser_parse_tentatively (parser);
29651 rcv = cp_parser_simple_type_specifier (parser,
29652 /*decl_specs=*/NULL,
29653 CP_PARSER_FLAGS_NONE);
29654
29655 if (cp_parser_parse_definitely (parser))
29656 return objc_get_class_reference (rcv);
29657
29658 cp_parser_error (parser, "objective-c++ message receiver expected");
29659 return error_mark_node;
29660 }
29661
29662 /* Parse the arguments and selectors comprising an Objective-C message.
29663
29664 objc-message-args:
29665 objc-selector
29666 objc-selector-args
29667 objc-selector-args , objc-comma-args
29668
29669 objc-selector-args:
29670 objc-selector [opt] : assignment-expression
29671 objc-selector-args objc-selector [opt] : assignment-expression
29672
29673 objc-comma-args:
29674 assignment-expression
29675 objc-comma-args , assignment-expression
29676
29677 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29678 selector arguments and TREE_VALUE containing a list of comma
29679 arguments. */
29680
29681 static tree
29682 cp_parser_objc_message_args (cp_parser* parser)
29683 {
29684 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29685 bool maybe_unary_selector_p = true;
29686 cp_token *token = cp_lexer_peek_token (parser->lexer);
29687
29688 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29689 {
29690 tree selector = NULL_TREE, arg;
29691
29692 if (token->type != CPP_COLON)
29693 selector = cp_parser_objc_selector (parser);
29694
29695 /* Detect if we have a unary selector. */
29696 if (maybe_unary_selector_p
29697 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29698 return build_tree_list (selector, NULL_TREE);
29699
29700 maybe_unary_selector_p = false;
29701 cp_parser_require (parser, CPP_COLON, RT_COLON);
29702 arg = cp_parser_assignment_expression (parser);
29703
29704 sel_args
29705 = chainon (sel_args,
29706 build_tree_list (selector, arg));
29707
29708 token = cp_lexer_peek_token (parser->lexer);
29709 }
29710
29711 /* Handle non-selector arguments, if any. */
29712 while (token->type == CPP_COMMA)
29713 {
29714 tree arg;
29715
29716 cp_lexer_consume_token (parser->lexer);
29717 arg = cp_parser_assignment_expression (parser);
29718
29719 addl_args
29720 = chainon (addl_args,
29721 build_tree_list (NULL_TREE, arg));
29722
29723 token = cp_lexer_peek_token (parser->lexer);
29724 }
29725
29726 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29727 {
29728 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29729 return build_tree_list (error_mark_node, error_mark_node);
29730 }
29731
29732 return build_tree_list (sel_args, addl_args);
29733 }
29734
29735 /* Parse an Objective-C encode expression.
29736
29737 objc-encode-expression:
29738 @encode objc-typename
29739
29740 Returns an encoded representation of the type argument. */
29741
29742 static cp_expr
29743 cp_parser_objc_encode_expression (cp_parser* parser)
29744 {
29745 tree type;
29746 cp_token *token;
29747 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29748
29749 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29750 matching_parens parens;
29751 parens.require_open (parser);
29752 token = cp_lexer_peek_token (parser->lexer);
29753 type = complete_type (cp_parser_type_id (parser));
29754 parens.require_close (parser);
29755
29756 if (!type)
29757 {
29758 error_at (token->location,
29759 "%<@encode%> must specify a type as an argument");
29760 return error_mark_node;
29761 }
29762
29763 /* This happens if we find @encode(T) (where T is a template
29764 typename or something dependent on a template typename) when
29765 parsing a template. In that case, we can't compile it
29766 immediately, but we rather create an AT_ENCODE_EXPR which will
29767 need to be instantiated when the template is used.
29768 */
29769 if (dependent_type_p (type))
29770 {
29771 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29772 TREE_READONLY (value) = 1;
29773 return value;
29774 }
29775
29776
29777 /* Build a location of the form:
29778 @encode(int)
29779 ^~~~~~~~~~~~
29780 with caret==start at the @ token, finishing at the close paren. */
29781 location_t combined_loc
29782 = make_location (start_loc, start_loc,
29783 cp_lexer_previous_token (parser->lexer)->location);
29784
29785 return cp_expr (objc_build_encode_expr (type), combined_loc);
29786 }
29787
29788 /* Parse an Objective-C @defs expression. */
29789
29790 static tree
29791 cp_parser_objc_defs_expression (cp_parser *parser)
29792 {
29793 tree name;
29794
29795 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29796 matching_parens parens;
29797 parens.require_open (parser);
29798 name = cp_parser_identifier (parser);
29799 parens.require_close (parser);
29800
29801 return objc_get_class_ivars (name);
29802 }
29803
29804 /* Parse an Objective-C protocol expression.
29805
29806 objc-protocol-expression:
29807 @protocol ( identifier )
29808
29809 Returns a representation of the protocol expression. */
29810
29811 static tree
29812 cp_parser_objc_protocol_expression (cp_parser* parser)
29813 {
29814 tree proto;
29815 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29816
29817 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29818 matching_parens parens;
29819 parens.require_open (parser);
29820 proto = cp_parser_identifier (parser);
29821 parens.require_close (parser);
29822
29823 /* Build a location of the form:
29824 @protocol(prot)
29825 ^~~~~~~~~~~~~~~
29826 with caret==start at the @ token, finishing at the close paren. */
29827 location_t combined_loc
29828 = make_location (start_loc, start_loc,
29829 cp_lexer_previous_token (parser->lexer)->location);
29830 tree result = objc_build_protocol_expr (proto);
29831 protected_set_expr_location (result, combined_loc);
29832 return result;
29833 }
29834
29835 /* Parse an Objective-C selector expression.
29836
29837 objc-selector-expression:
29838 @selector ( objc-method-signature )
29839
29840 objc-method-signature:
29841 objc-selector
29842 objc-selector-seq
29843
29844 objc-selector-seq:
29845 objc-selector :
29846 objc-selector-seq objc-selector :
29847
29848 Returns a representation of the method selector. */
29849
29850 static tree
29851 cp_parser_objc_selector_expression (cp_parser* parser)
29852 {
29853 tree sel_seq = NULL_TREE;
29854 bool maybe_unary_selector_p = true;
29855 cp_token *token;
29856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29857
29858 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29859 matching_parens parens;
29860 parens.require_open (parser);
29861 token = cp_lexer_peek_token (parser->lexer);
29862
29863 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29864 || token->type == CPP_SCOPE)
29865 {
29866 tree selector = NULL_TREE;
29867
29868 if (token->type != CPP_COLON
29869 || token->type == CPP_SCOPE)
29870 selector = cp_parser_objc_selector (parser);
29871
29872 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29873 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29874 {
29875 /* Detect if we have a unary selector. */
29876 if (maybe_unary_selector_p)
29877 {
29878 sel_seq = selector;
29879 goto finish_selector;
29880 }
29881 else
29882 {
29883 cp_parser_error (parser, "expected %<:%>");
29884 }
29885 }
29886 maybe_unary_selector_p = false;
29887 token = cp_lexer_consume_token (parser->lexer);
29888
29889 if (token->type == CPP_SCOPE)
29890 {
29891 sel_seq
29892 = chainon (sel_seq,
29893 build_tree_list (selector, NULL_TREE));
29894 sel_seq
29895 = chainon (sel_seq,
29896 build_tree_list (NULL_TREE, NULL_TREE));
29897 }
29898 else
29899 sel_seq
29900 = chainon (sel_seq,
29901 build_tree_list (selector, NULL_TREE));
29902
29903 token = cp_lexer_peek_token (parser->lexer);
29904 }
29905
29906 finish_selector:
29907 parens.require_close (parser);
29908
29909
29910 /* Build a location of the form:
29911 @selector(func)
29912 ^~~~~~~~~~~~~~~
29913 with caret==start at the @ token, finishing at the close paren. */
29914 location_t combined_loc
29915 = make_location (loc, loc,
29916 cp_lexer_previous_token (parser->lexer)->location);
29917 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29918 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29919 protected_set_expr_location (result, combined_loc);
29920 return result;
29921 }
29922
29923 /* Parse a list of identifiers.
29924
29925 objc-identifier-list:
29926 identifier
29927 objc-identifier-list , identifier
29928
29929 Returns a TREE_LIST of identifier nodes. */
29930
29931 static tree
29932 cp_parser_objc_identifier_list (cp_parser* parser)
29933 {
29934 tree identifier;
29935 tree list;
29936 cp_token *sep;
29937
29938 identifier = cp_parser_identifier (parser);
29939 if (identifier == error_mark_node)
29940 return error_mark_node;
29941
29942 list = build_tree_list (NULL_TREE, identifier);
29943 sep = cp_lexer_peek_token (parser->lexer);
29944
29945 while (sep->type == CPP_COMMA)
29946 {
29947 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29948 identifier = cp_parser_identifier (parser);
29949 if (identifier == error_mark_node)
29950 return list;
29951
29952 list = chainon (list, build_tree_list (NULL_TREE,
29953 identifier));
29954 sep = cp_lexer_peek_token (parser->lexer);
29955 }
29956
29957 return list;
29958 }
29959
29960 /* Parse an Objective-C alias declaration.
29961
29962 objc-alias-declaration:
29963 @compatibility_alias identifier identifier ;
29964
29965 This function registers the alias mapping with the Objective-C front end.
29966 It returns nothing. */
29967
29968 static void
29969 cp_parser_objc_alias_declaration (cp_parser* parser)
29970 {
29971 tree alias, orig;
29972
29973 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29974 alias = cp_parser_identifier (parser);
29975 orig = cp_parser_identifier (parser);
29976 objc_declare_alias (alias, orig);
29977 cp_parser_consume_semicolon_at_end_of_statement (parser);
29978 }
29979
29980 /* Parse an Objective-C class forward-declaration.
29981
29982 objc-class-declaration:
29983 @class objc-identifier-list ;
29984
29985 The function registers the forward declarations with the Objective-C
29986 front end. It returns nothing. */
29987
29988 static void
29989 cp_parser_objc_class_declaration (cp_parser* parser)
29990 {
29991 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29992 while (true)
29993 {
29994 tree id;
29995
29996 id = cp_parser_identifier (parser);
29997 if (id == error_mark_node)
29998 break;
29999
30000 objc_declare_class (id);
30001
30002 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30003 cp_lexer_consume_token (parser->lexer);
30004 else
30005 break;
30006 }
30007 cp_parser_consume_semicolon_at_end_of_statement (parser);
30008 }
30009
30010 /* Parse a list of Objective-C protocol references.
30011
30012 objc-protocol-refs-opt:
30013 objc-protocol-refs [opt]
30014
30015 objc-protocol-refs:
30016 < objc-identifier-list >
30017
30018 Returns a TREE_LIST of identifiers, if any. */
30019
30020 static tree
30021 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30022 {
30023 tree protorefs = NULL_TREE;
30024
30025 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30026 {
30027 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30028 protorefs = cp_parser_objc_identifier_list (parser);
30029 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30030 }
30031
30032 return protorefs;
30033 }
30034
30035 /* Parse a Objective-C visibility specification. */
30036
30037 static void
30038 cp_parser_objc_visibility_spec (cp_parser* parser)
30039 {
30040 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30041
30042 switch (vis->keyword)
30043 {
30044 case RID_AT_PRIVATE:
30045 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30046 break;
30047 case RID_AT_PROTECTED:
30048 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30049 break;
30050 case RID_AT_PUBLIC:
30051 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30052 break;
30053 case RID_AT_PACKAGE:
30054 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30055 break;
30056 default:
30057 return;
30058 }
30059
30060 /* Eat '@private'/'@protected'/'@public'. */
30061 cp_lexer_consume_token (parser->lexer);
30062 }
30063
30064 /* Parse an Objective-C method type. Return 'true' if it is a class
30065 (+) method, and 'false' if it is an instance (-) method. */
30066
30067 static inline bool
30068 cp_parser_objc_method_type (cp_parser* parser)
30069 {
30070 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30071 return true;
30072 else
30073 return false;
30074 }
30075
30076 /* Parse an Objective-C protocol qualifier. */
30077
30078 static tree
30079 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30080 {
30081 tree quals = NULL_TREE, node;
30082 cp_token *token = cp_lexer_peek_token (parser->lexer);
30083
30084 node = token->u.value;
30085
30086 while (node && identifier_p (node)
30087 && (node == ridpointers [(int) RID_IN]
30088 || node == ridpointers [(int) RID_OUT]
30089 || node == ridpointers [(int) RID_INOUT]
30090 || node == ridpointers [(int) RID_BYCOPY]
30091 || node == ridpointers [(int) RID_BYREF]
30092 || node == ridpointers [(int) RID_ONEWAY]))
30093 {
30094 quals = tree_cons (NULL_TREE, node, quals);
30095 cp_lexer_consume_token (parser->lexer);
30096 token = cp_lexer_peek_token (parser->lexer);
30097 node = token->u.value;
30098 }
30099
30100 return quals;
30101 }
30102
30103 /* Parse an Objective-C typename. */
30104
30105 static tree
30106 cp_parser_objc_typename (cp_parser* parser)
30107 {
30108 tree type_name = NULL_TREE;
30109
30110 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30111 {
30112 tree proto_quals, cp_type = NULL_TREE;
30113
30114 matching_parens parens;
30115 parens.consume_open (parser); /* Eat '('. */
30116 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30117
30118 /* An ObjC type name may consist of just protocol qualifiers, in which
30119 case the type shall default to 'id'. */
30120 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30121 {
30122 cp_type = cp_parser_type_id (parser);
30123
30124 /* If the type could not be parsed, an error has already
30125 been produced. For error recovery, behave as if it had
30126 not been specified, which will use the default type
30127 'id'. */
30128 if (cp_type == error_mark_node)
30129 {
30130 cp_type = NULL_TREE;
30131 /* We need to skip to the closing parenthesis as
30132 cp_parser_type_id() does not seem to do it for
30133 us. */
30134 cp_parser_skip_to_closing_parenthesis (parser,
30135 /*recovering=*/true,
30136 /*or_comma=*/false,
30137 /*consume_paren=*/false);
30138 }
30139 }
30140
30141 parens.require_close (parser);
30142 type_name = build_tree_list (proto_quals, cp_type);
30143 }
30144
30145 return type_name;
30146 }
30147
30148 /* Check to see if TYPE refers to an Objective-C selector name. */
30149
30150 static bool
30151 cp_parser_objc_selector_p (enum cpp_ttype type)
30152 {
30153 return (type == CPP_NAME || type == CPP_KEYWORD
30154 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30155 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30156 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30157 || type == CPP_XOR || type == CPP_XOR_EQ);
30158 }
30159
30160 /* Parse an Objective-C selector. */
30161
30162 static tree
30163 cp_parser_objc_selector (cp_parser* parser)
30164 {
30165 cp_token *token = cp_lexer_consume_token (parser->lexer);
30166
30167 if (!cp_parser_objc_selector_p (token->type))
30168 {
30169 error_at (token->location, "invalid Objective-C++ selector name");
30170 return error_mark_node;
30171 }
30172
30173 /* C++ operator names are allowed to appear in ObjC selectors. */
30174 switch (token->type)
30175 {
30176 case CPP_AND_AND: return get_identifier ("and");
30177 case CPP_AND_EQ: return get_identifier ("and_eq");
30178 case CPP_AND: return get_identifier ("bitand");
30179 case CPP_OR: return get_identifier ("bitor");
30180 case CPP_COMPL: return get_identifier ("compl");
30181 case CPP_NOT: return get_identifier ("not");
30182 case CPP_NOT_EQ: return get_identifier ("not_eq");
30183 case CPP_OR_OR: return get_identifier ("or");
30184 case CPP_OR_EQ: return get_identifier ("or_eq");
30185 case CPP_XOR: return get_identifier ("xor");
30186 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30187 default: return token->u.value;
30188 }
30189 }
30190
30191 /* Parse an Objective-C params list. */
30192
30193 static tree
30194 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30195 {
30196 tree params = NULL_TREE;
30197 bool maybe_unary_selector_p = true;
30198 cp_token *token = cp_lexer_peek_token (parser->lexer);
30199
30200 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30201 {
30202 tree selector = NULL_TREE, type_name, identifier;
30203 tree parm_attr = NULL_TREE;
30204
30205 if (token->keyword == RID_ATTRIBUTE)
30206 break;
30207
30208 if (token->type != CPP_COLON)
30209 selector = cp_parser_objc_selector (parser);
30210
30211 /* Detect if we have a unary selector. */
30212 if (maybe_unary_selector_p
30213 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30214 {
30215 params = selector; /* Might be followed by attributes. */
30216 break;
30217 }
30218
30219 maybe_unary_selector_p = false;
30220 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30221 {
30222 /* Something went quite wrong. There should be a colon
30223 here, but there is not. Stop parsing parameters. */
30224 break;
30225 }
30226 type_name = cp_parser_objc_typename (parser);
30227 /* New ObjC allows attributes on parameters too. */
30228 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30229 parm_attr = cp_parser_attributes_opt (parser);
30230 identifier = cp_parser_identifier (parser);
30231
30232 params
30233 = chainon (params,
30234 objc_build_keyword_decl (selector,
30235 type_name,
30236 identifier,
30237 parm_attr));
30238
30239 token = cp_lexer_peek_token (parser->lexer);
30240 }
30241
30242 if (params == NULL_TREE)
30243 {
30244 cp_parser_error (parser, "objective-c++ method declaration is expected");
30245 return error_mark_node;
30246 }
30247
30248 /* We allow tail attributes for the method. */
30249 if (token->keyword == RID_ATTRIBUTE)
30250 {
30251 *attributes = cp_parser_attributes_opt (parser);
30252 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30253 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30254 return params;
30255 cp_parser_error (parser,
30256 "method attributes must be specified at the end");
30257 return error_mark_node;
30258 }
30259
30260 if (params == NULL_TREE)
30261 {
30262 cp_parser_error (parser, "objective-c++ method declaration is expected");
30263 return error_mark_node;
30264 }
30265 return params;
30266 }
30267
30268 /* Parse the non-keyword Objective-C params. */
30269
30270 static tree
30271 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30272 tree* attributes)
30273 {
30274 tree params = make_node (TREE_LIST);
30275 cp_token *token = cp_lexer_peek_token (parser->lexer);
30276 *ellipsisp = false; /* Initially, assume no ellipsis. */
30277
30278 while (token->type == CPP_COMMA)
30279 {
30280 cp_parameter_declarator *parmdecl;
30281 tree parm;
30282
30283 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30284 token = cp_lexer_peek_token (parser->lexer);
30285
30286 if (token->type == CPP_ELLIPSIS)
30287 {
30288 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30289 *ellipsisp = true;
30290 token = cp_lexer_peek_token (parser->lexer);
30291 break;
30292 }
30293
30294 /* TODO: parse attributes for tail parameters. */
30295 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30296 parm = grokdeclarator (parmdecl->declarator,
30297 &parmdecl->decl_specifiers,
30298 PARM, /*initialized=*/0,
30299 /*attrlist=*/NULL);
30300
30301 chainon (params, build_tree_list (NULL_TREE, parm));
30302 token = cp_lexer_peek_token (parser->lexer);
30303 }
30304
30305 /* We allow tail attributes for the method. */
30306 if (token->keyword == RID_ATTRIBUTE)
30307 {
30308 if (*attributes == NULL_TREE)
30309 {
30310 *attributes = cp_parser_attributes_opt (parser);
30311 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30312 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30313 return params;
30314 }
30315 else
30316 /* We have an error, but parse the attributes, so that we can
30317 carry on. */
30318 *attributes = cp_parser_attributes_opt (parser);
30319
30320 cp_parser_error (parser,
30321 "method attributes must be specified at the end");
30322 return error_mark_node;
30323 }
30324
30325 return params;
30326 }
30327
30328 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30329
30330 static void
30331 cp_parser_objc_interstitial_code (cp_parser* parser)
30332 {
30333 cp_token *token = cp_lexer_peek_token (parser->lexer);
30334
30335 /* If the next token is `extern' and the following token is a string
30336 literal, then we have a linkage specification. */
30337 if (token->keyword == RID_EXTERN
30338 && cp_parser_is_pure_string_literal
30339 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30340 cp_parser_linkage_specification (parser);
30341 /* Handle #pragma, if any. */
30342 else if (token->type == CPP_PRAGMA)
30343 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30344 /* Allow stray semicolons. */
30345 else if (token->type == CPP_SEMICOLON)
30346 cp_lexer_consume_token (parser->lexer);
30347 /* Mark methods as optional or required, when building protocols. */
30348 else if (token->keyword == RID_AT_OPTIONAL)
30349 {
30350 cp_lexer_consume_token (parser->lexer);
30351 objc_set_method_opt (true);
30352 }
30353 else if (token->keyword == RID_AT_REQUIRED)
30354 {
30355 cp_lexer_consume_token (parser->lexer);
30356 objc_set_method_opt (false);
30357 }
30358 else if (token->keyword == RID_NAMESPACE)
30359 cp_parser_namespace_definition (parser);
30360 /* Other stray characters must generate errors. */
30361 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30362 {
30363 cp_lexer_consume_token (parser->lexer);
30364 error ("stray %qs between Objective-C++ methods",
30365 token->type == CPP_OPEN_BRACE ? "{" : "}");
30366 }
30367 /* Finally, try to parse a block-declaration, or a function-definition. */
30368 else
30369 cp_parser_block_declaration (parser, /*statement_p=*/false);
30370 }
30371
30372 /* Parse a method signature. */
30373
30374 static tree
30375 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30376 {
30377 tree rettype, kwdparms, optparms;
30378 bool ellipsis = false;
30379 bool is_class_method;
30380
30381 is_class_method = cp_parser_objc_method_type (parser);
30382 rettype = cp_parser_objc_typename (parser);
30383 *attributes = NULL_TREE;
30384 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30385 if (kwdparms == error_mark_node)
30386 return error_mark_node;
30387 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30388 if (optparms == error_mark_node)
30389 return error_mark_node;
30390
30391 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30392 }
30393
30394 static bool
30395 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30396 {
30397 tree tattr;
30398 cp_lexer_save_tokens (parser->lexer);
30399 tattr = cp_parser_attributes_opt (parser);
30400 gcc_assert (tattr) ;
30401
30402 /* If the attributes are followed by a method introducer, this is not allowed.
30403 Dump the attributes and flag the situation. */
30404 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30405 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30406 return true;
30407
30408 /* Otherwise, the attributes introduce some interstitial code, possibly so
30409 rewind to allow that check. */
30410 cp_lexer_rollback_tokens (parser->lexer);
30411 return false;
30412 }
30413
30414 /* Parse an Objective-C method prototype list. */
30415
30416 static void
30417 cp_parser_objc_method_prototype_list (cp_parser* parser)
30418 {
30419 cp_token *token = cp_lexer_peek_token (parser->lexer);
30420
30421 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30422 {
30423 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30424 {
30425 tree attributes, sig;
30426 bool is_class_method;
30427 if (token->type == CPP_PLUS)
30428 is_class_method = true;
30429 else
30430 is_class_method = false;
30431 sig = cp_parser_objc_method_signature (parser, &attributes);
30432 if (sig == error_mark_node)
30433 {
30434 cp_parser_skip_to_end_of_block_or_statement (parser);
30435 token = cp_lexer_peek_token (parser->lexer);
30436 continue;
30437 }
30438 objc_add_method_declaration (is_class_method, sig, attributes);
30439 cp_parser_consume_semicolon_at_end_of_statement (parser);
30440 }
30441 else if (token->keyword == RID_AT_PROPERTY)
30442 cp_parser_objc_at_property_declaration (parser);
30443 else if (token->keyword == RID_ATTRIBUTE
30444 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30445 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30446 OPT_Wattributes,
30447 "prefix attributes are ignored for methods");
30448 else
30449 /* Allow for interspersed non-ObjC++ code. */
30450 cp_parser_objc_interstitial_code (parser);
30451
30452 token = cp_lexer_peek_token (parser->lexer);
30453 }
30454
30455 if (token->type != CPP_EOF)
30456 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30457 else
30458 cp_parser_error (parser, "expected %<@end%>");
30459
30460 objc_finish_interface ();
30461 }
30462
30463 /* Parse an Objective-C method definition list. */
30464
30465 static void
30466 cp_parser_objc_method_definition_list (cp_parser* parser)
30467 {
30468 cp_token *token = cp_lexer_peek_token (parser->lexer);
30469
30470 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30471 {
30472 tree meth;
30473
30474 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30475 {
30476 cp_token *ptk;
30477 tree sig, attribute;
30478 bool is_class_method;
30479 if (token->type == CPP_PLUS)
30480 is_class_method = true;
30481 else
30482 is_class_method = false;
30483 push_deferring_access_checks (dk_deferred);
30484 sig = cp_parser_objc_method_signature (parser, &attribute);
30485 if (sig == error_mark_node)
30486 {
30487 cp_parser_skip_to_end_of_block_or_statement (parser);
30488 token = cp_lexer_peek_token (parser->lexer);
30489 continue;
30490 }
30491 objc_start_method_definition (is_class_method, sig, attribute,
30492 NULL_TREE);
30493
30494 /* For historical reasons, we accept an optional semicolon. */
30495 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30496 cp_lexer_consume_token (parser->lexer);
30497
30498 ptk = cp_lexer_peek_token (parser->lexer);
30499 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30500 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30501 {
30502 perform_deferred_access_checks (tf_warning_or_error);
30503 stop_deferring_access_checks ();
30504 meth = cp_parser_function_definition_after_declarator (parser,
30505 false);
30506 pop_deferring_access_checks ();
30507 objc_finish_method_definition (meth);
30508 }
30509 }
30510 /* The following case will be removed once @synthesize is
30511 completely implemented. */
30512 else if (token->keyword == RID_AT_PROPERTY)
30513 cp_parser_objc_at_property_declaration (parser);
30514 else if (token->keyword == RID_AT_SYNTHESIZE)
30515 cp_parser_objc_at_synthesize_declaration (parser);
30516 else if (token->keyword == RID_AT_DYNAMIC)
30517 cp_parser_objc_at_dynamic_declaration (parser);
30518 else if (token->keyword == RID_ATTRIBUTE
30519 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30520 warning_at (token->location, OPT_Wattributes,
30521 "prefix attributes are ignored for methods");
30522 else
30523 /* Allow for interspersed non-ObjC++ code. */
30524 cp_parser_objc_interstitial_code (parser);
30525
30526 token = cp_lexer_peek_token (parser->lexer);
30527 }
30528
30529 if (token->type != CPP_EOF)
30530 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30531 else
30532 cp_parser_error (parser, "expected %<@end%>");
30533
30534 objc_finish_implementation ();
30535 }
30536
30537 /* Parse Objective-C ivars. */
30538
30539 static void
30540 cp_parser_objc_class_ivars (cp_parser* parser)
30541 {
30542 cp_token *token = cp_lexer_peek_token (parser->lexer);
30543
30544 if (token->type != CPP_OPEN_BRACE)
30545 return; /* No ivars specified. */
30546
30547 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30548 token = cp_lexer_peek_token (parser->lexer);
30549
30550 while (token->type != CPP_CLOSE_BRACE
30551 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30552 {
30553 cp_decl_specifier_seq declspecs;
30554 int decl_class_or_enum_p;
30555 tree prefix_attributes;
30556
30557 cp_parser_objc_visibility_spec (parser);
30558
30559 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30560 break;
30561
30562 cp_parser_decl_specifier_seq (parser,
30563 CP_PARSER_FLAGS_OPTIONAL,
30564 &declspecs,
30565 &decl_class_or_enum_p);
30566
30567 /* auto, register, static, extern, mutable. */
30568 if (declspecs.storage_class != sc_none)
30569 {
30570 cp_parser_error (parser, "invalid type for instance variable");
30571 declspecs.storage_class = sc_none;
30572 }
30573
30574 /* thread_local. */
30575 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30576 {
30577 cp_parser_error (parser, "invalid type for instance variable");
30578 declspecs.locations[ds_thread] = 0;
30579 }
30580
30581 /* typedef. */
30582 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30583 {
30584 cp_parser_error (parser, "invalid type for instance variable");
30585 declspecs.locations[ds_typedef] = 0;
30586 }
30587
30588 prefix_attributes = declspecs.attributes;
30589 declspecs.attributes = NULL_TREE;
30590
30591 /* Keep going until we hit the `;' at the end of the
30592 declaration. */
30593 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30594 {
30595 tree width = NULL_TREE, attributes, first_attribute, decl;
30596 cp_declarator *declarator = NULL;
30597 int ctor_dtor_or_conv_p;
30598
30599 /* Check for a (possibly unnamed) bitfield declaration. */
30600 token = cp_lexer_peek_token (parser->lexer);
30601 if (token->type == CPP_COLON)
30602 goto eat_colon;
30603
30604 if (token->type == CPP_NAME
30605 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30606 == CPP_COLON))
30607 {
30608 /* Get the name of the bitfield. */
30609 declarator = make_id_declarator (NULL_TREE,
30610 cp_parser_identifier (parser),
30611 sfk_none);
30612
30613 eat_colon:
30614 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30615 /* Get the width of the bitfield. */
30616 width
30617 = cp_parser_constant_expression (parser);
30618 }
30619 else
30620 {
30621 /* Parse the declarator. */
30622 declarator
30623 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30624 &ctor_dtor_or_conv_p,
30625 /*parenthesized_p=*/NULL,
30626 /*member_p=*/false,
30627 /*friend_p=*/false);
30628 }
30629
30630 /* Look for attributes that apply to the ivar. */
30631 attributes = cp_parser_attributes_opt (parser);
30632 /* Remember which attributes are prefix attributes and
30633 which are not. */
30634 first_attribute = attributes;
30635 /* Combine the attributes. */
30636 attributes = attr_chainon (prefix_attributes, attributes);
30637
30638 if (width)
30639 /* Create the bitfield declaration. */
30640 decl = grokbitfield (declarator, &declspecs,
30641 width, NULL_TREE, attributes);
30642 else
30643 decl = grokfield (declarator, &declspecs,
30644 NULL_TREE, /*init_const_expr_p=*/false,
30645 NULL_TREE, attributes);
30646
30647 /* Add the instance variable. */
30648 if (decl != error_mark_node && decl != NULL_TREE)
30649 objc_add_instance_variable (decl);
30650
30651 /* Reset PREFIX_ATTRIBUTES. */
30652 if (attributes != error_mark_node)
30653 {
30654 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30655 attributes = TREE_CHAIN (attributes);
30656 if (attributes)
30657 TREE_CHAIN (attributes) = NULL_TREE;
30658 }
30659
30660 token = cp_lexer_peek_token (parser->lexer);
30661
30662 if (token->type == CPP_COMMA)
30663 {
30664 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30665 continue;
30666 }
30667 break;
30668 }
30669
30670 cp_parser_consume_semicolon_at_end_of_statement (parser);
30671 token = cp_lexer_peek_token (parser->lexer);
30672 }
30673
30674 if (token->keyword == RID_AT_END)
30675 cp_parser_error (parser, "expected %<}%>");
30676
30677 /* Do not consume the RID_AT_END, so it will be read again as terminating
30678 the @interface of @implementation. */
30679 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30680 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30681
30682 /* For historical reasons, we accept an optional semicolon. */
30683 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30684 cp_lexer_consume_token (parser->lexer);
30685 }
30686
30687 /* Parse an Objective-C protocol declaration. */
30688
30689 static void
30690 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30691 {
30692 tree proto, protorefs;
30693 cp_token *tok;
30694
30695 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30697 {
30698 tok = cp_lexer_peek_token (parser->lexer);
30699 error_at (tok->location, "identifier expected after %<@protocol%>");
30700 cp_parser_consume_semicolon_at_end_of_statement (parser);
30701 return;
30702 }
30703
30704 /* See if we have a forward declaration or a definition. */
30705 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30706
30707 /* Try a forward declaration first. */
30708 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30709 {
30710 while (true)
30711 {
30712 tree id;
30713
30714 id = cp_parser_identifier (parser);
30715 if (id == error_mark_node)
30716 break;
30717
30718 objc_declare_protocol (id, attributes);
30719
30720 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30721 cp_lexer_consume_token (parser->lexer);
30722 else
30723 break;
30724 }
30725 cp_parser_consume_semicolon_at_end_of_statement (parser);
30726 }
30727
30728 /* Ok, we got a full-fledged definition (or at least should). */
30729 else
30730 {
30731 proto = cp_parser_identifier (parser);
30732 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30733 objc_start_protocol (proto, protorefs, attributes);
30734 cp_parser_objc_method_prototype_list (parser);
30735 }
30736 }
30737
30738 /* Parse an Objective-C superclass or category. */
30739
30740 static void
30741 cp_parser_objc_superclass_or_category (cp_parser *parser,
30742 bool iface_p,
30743 tree *super,
30744 tree *categ, bool *is_class_extension)
30745 {
30746 cp_token *next = cp_lexer_peek_token (parser->lexer);
30747
30748 *super = *categ = NULL_TREE;
30749 *is_class_extension = false;
30750 if (next->type == CPP_COLON)
30751 {
30752 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30753 *super = cp_parser_identifier (parser);
30754 }
30755 else if (next->type == CPP_OPEN_PAREN)
30756 {
30757 matching_parens parens;
30758 parens.consume_open (parser); /* Eat '('. */
30759
30760 /* If there is no category name, and this is an @interface, we
30761 have a class extension. */
30762 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30763 {
30764 *categ = NULL_TREE;
30765 *is_class_extension = true;
30766 }
30767 else
30768 *categ = cp_parser_identifier (parser);
30769
30770 parens.require_close (parser);
30771 }
30772 }
30773
30774 /* Parse an Objective-C class interface. */
30775
30776 static void
30777 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30778 {
30779 tree name, super, categ, protos;
30780 bool is_class_extension;
30781
30782 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30783 name = cp_parser_identifier (parser);
30784 if (name == error_mark_node)
30785 {
30786 /* It's hard to recover because even if valid @interface stuff
30787 is to follow, we can't compile it (or validate it) if we
30788 don't even know which class it refers to. Let's assume this
30789 was a stray '@interface' token in the stream and skip it.
30790 */
30791 return;
30792 }
30793 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30794 &is_class_extension);
30795 protos = cp_parser_objc_protocol_refs_opt (parser);
30796
30797 /* We have either a class or a category on our hands. */
30798 if (categ || is_class_extension)
30799 objc_start_category_interface (name, categ, protos, attributes);
30800 else
30801 {
30802 objc_start_class_interface (name, super, protos, attributes);
30803 /* Handle instance variable declarations, if any. */
30804 cp_parser_objc_class_ivars (parser);
30805 objc_continue_interface ();
30806 }
30807
30808 cp_parser_objc_method_prototype_list (parser);
30809 }
30810
30811 /* Parse an Objective-C class implementation. */
30812
30813 static void
30814 cp_parser_objc_class_implementation (cp_parser* parser)
30815 {
30816 tree name, super, categ;
30817 bool is_class_extension;
30818
30819 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30820 name = cp_parser_identifier (parser);
30821 if (name == error_mark_node)
30822 {
30823 /* It's hard to recover because even if valid @implementation
30824 stuff is to follow, we can't compile it (or validate it) if
30825 we don't even know which class it refers to. Let's assume
30826 this was a stray '@implementation' token in the stream and
30827 skip it.
30828 */
30829 return;
30830 }
30831 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30832 &is_class_extension);
30833
30834 /* We have either a class or a category on our hands. */
30835 if (categ)
30836 objc_start_category_implementation (name, categ);
30837 else
30838 {
30839 objc_start_class_implementation (name, super);
30840 /* Handle instance variable declarations, if any. */
30841 cp_parser_objc_class_ivars (parser);
30842 objc_continue_implementation ();
30843 }
30844
30845 cp_parser_objc_method_definition_list (parser);
30846 }
30847
30848 /* Consume the @end token and finish off the implementation. */
30849
30850 static void
30851 cp_parser_objc_end_implementation (cp_parser* parser)
30852 {
30853 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30854 objc_finish_implementation ();
30855 }
30856
30857 /* Parse an Objective-C declaration. */
30858
30859 static void
30860 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30861 {
30862 /* Try to figure out what kind of declaration is present. */
30863 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30864
30865 if (attributes)
30866 switch (kwd->keyword)
30867 {
30868 case RID_AT_ALIAS:
30869 case RID_AT_CLASS:
30870 case RID_AT_END:
30871 error_at (kwd->location, "attributes may not be specified before"
30872 " the %<@%D%> Objective-C++ keyword",
30873 kwd->u.value);
30874 attributes = NULL;
30875 break;
30876 case RID_AT_IMPLEMENTATION:
30877 warning_at (kwd->location, OPT_Wattributes,
30878 "prefix attributes are ignored before %<@%D%>",
30879 kwd->u.value);
30880 attributes = NULL;
30881 default:
30882 break;
30883 }
30884
30885 switch (kwd->keyword)
30886 {
30887 case RID_AT_ALIAS:
30888 cp_parser_objc_alias_declaration (parser);
30889 break;
30890 case RID_AT_CLASS:
30891 cp_parser_objc_class_declaration (parser);
30892 break;
30893 case RID_AT_PROTOCOL:
30894 cp_parser_objc_protocol_declaration (parser, attributes);
30895 break;
30896 case RID_AT_INTERFACE:
30897 cp_parser_objc_class_interface (parser, attributes);
30898 break;
30899 case RID_AT_IMPLEMENTATION:
30900 cp_parser_objc_class_implementation (parser);
30901 break;
30902 case RID_AT_END:
30903 cp_parser_objc_end_implementation (parser);
30904 break;
30905 default:
30906 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30907 kwd->u.value);
30908 cp_parser_skip_to_end_of_block_or_statement (parser);
30909 }
30910 }
30911
30912 /* Parse an Objective-C try-catch-finally statement.
30913
30914 objc-try-catch-finally-stmt:
30915 @try compound-statement objc-catch-clause-seq [opt]
30916 objc-finally-clause [opt]
30917
30918 objc-catch-clause-seq:
30919 objc-catch-clause objc-catch-clause-seq [opt]
30920
30921 objc-catch-clause:
30922 @catch ( objc-exception-declaration ) compound-statement
30923
30924 objc-finally-clause:
30925 @finally compound-statement
30926
30927 objc-exception-declaration:
30928 parameter-declaration
30929 '...'
30930
30931 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30932
30933 Returns NULL_TREE.
30934
30935 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30936 for C. Keep them in sync. */
30937
30938 static tree
30939 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30940 {
30941 location_t location;
30942 tree stmt;
30943
30944 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30945 location = cp_lexer_peek_token (parser->lexer)->location;
30946 objc_maybe_warn_exceptions (location);
30947 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30948 node, lest it get absorbed into the surrounding block. */
30949 stmt = push_stmt_list ();
30950 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30951 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30952
30953 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30954 {
30955 cp_parameter_declarator *parm;
30956 tree parameter_declaration = error_mark_node;
30957 bool seen_open_paren = false;
30958 matching_parens parens;
30959
30960 cp_lexer_consume_token (parser->lexer);
30961 if (parens.require_open (parser))
30962 seen_open_paren = true;
30963 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30964 {
30965 /* We have "@catch (...)" (where the '...' are literally
30966 what is in the code). Skip the '...'.
30967 parameter_declaration is set to NULL_TREE, and
30968 objc_being_catch_clauses() knows that that means
30969 '...'. */
30970 cp_lexer_consume_token (parser->lexer);
30971 parameter_declaration = NULL_TREE;
30972 }
30973 else
30974 {
30975 /* We have "@catch (NSException *exception)" or something
30976 like that. Parse the parameter declaration. */
30977 parm = cp_parser_parameter_declaration (parser, false, NULL);
30978 if (parm == NULL)
30979 parameter_declaration = error_mark_node;
30980 else
30981 parameter_declaration = grokdeclarator (parm->declarator,
30982 &parm->decl_specifiers,
30983 PARM, /*initialized=*/0,
30984 /*attrlist=*/NULL);
30985 }
30986 if (seen_open_paren)
30987 parens.require_close (parser);
30988 else
30989 {
30990 /* If there was no open parenthesis, we are recovering from
30991 an error, and we are trying to figure out what mistake
30992 the user has made. */
30993
30994 /* If there is an immediate closing parenthesis, the user
30995 probably forgot the opening one (ie, they typed "@catch
30996 NSException *e)". Parse the closing parenthesis and keep
30997 going. */
30998 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30999 cp_lexer_consume_token (parser->lexer);
31000
31001 /* If these is no immediate closing parenthesis, the user
31002 probably doesn't know that parenthesis are required at
31003 all (ie, they typed "@catch NSException *e"). So, just
31004 forget about the closing parenthesis and keep going. */
31005 }
31006 objc_begin_catch_clause (parameter_declaration);
31007 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31008 objc_finish_catch_clause ();
31009 }
31010 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31011 {
31012 cp_lexer_consume_token (parser->lexer);
31013 location = cp_lexer_peek_token (parser->lexer)->location;
31014 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31015 node, lest it get absorbed into the surrounding block. */
31016 stmt = push_stmt_list ();
31017 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31018 objc_build_finally_clause (location, pop_stmt_list (stmt));
31019 }
31020
31021 return objc_finish_try_stmt ();
31022 }
31023
31024 /* Parse an Objective-C synchronized statement.
31025
31026 objc-synchronized-stmt:
31027 @synchronized ( expression ) compound-statement
31028
31029 Returns NULL_TREE. */
31030
31031 static tree
31032 cp_parser_objc_synchronized_statement (cp_parser *parser)
31033 {
31034 location_t location;
31035 tree lock, stmt;
31036
31037 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31038
31039 location = cp_lexer_peek_token (parser->lexer)->location;
31040 objc_maybe_warn_exceptions (location);
31041 matching_parens parens;
31042 parens.require_open (parser);
31043 lock = cp_parser_expression (parser);
31044 parens.require_close (parser);
31045
31046 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31047 node, lest it get absorbed into the surrounding block. */
31048 stmt = push_stmt_list ();
31049 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31050
31051 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31052 }
31053
31054 /* Parse an Objective-C throw statement.
31055
31056 objc-throw-stmt:
31057 @throw assignment-expression [opt] ;
31058
31059 Returns a constructed '@throw' statement. */
31060
31061 static tree
31062 cp_parser_objc_throw_statement (cp_parser *parser)
31063 {
31064 tree expr = NULL_TREE;
31065 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31066
31067 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31068
31069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31070 expr = cp_parser_expression (parser);
31071
31072 cp_parser_consume_semicolon_at_end_of_statement (parser);
31073
31074 return objc_build_throw_stmt (loc, expr);
31075 }
31076
31077 /* Parse an Objective-C statement. */
31078
31079 static tree
31080 cp_parser_objc_statement (cp_parser * parser)
31081 {
31082 /* Try to figure out what kind of declaration is present. */
31083 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31084
31085 switch (kwd->keyword)
31086 {
31087 case RID_AT_TRY:
31088 return cp_parser_objc_try_catch_finally_statement (parser);
31089 case RID_AT_SYNCHRONIZED:
31090 return cp_parser_objc_synchronized_statement (parser);
31091 case RID_AT_THROW:
31092 return cp_parser_objc_throw_statement (parser);
31093 default:
31094 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31095 kwd->u.value);
31096 cp_parser_skip_to_end_of_block_or_statement (parser);
31097 }
31098
31099 return error_mark_node;
31100 }
31101
31102 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31103 look ahead to see if an objc keyword follows the attributes. This
31104 is to detect the use of prefix attributes on ObjC @interface and
31105 @protocol. */
31106
31107 static bool
31108 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31109 {
31110 cp_lexer_save_tokens (parser->lexer);
31111 *attrib = cp_parser_attributes_opt (parser);
31112 gcc_assert (*attrib);
31113 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31114 {
31115 cp_lexer_commit_tokens (parser->lexer);
31116 return true;
31117 }
31118 cp_lexer_rollback_tokens (parser->lexer);
31119 return false;
31120 }
31121
31122 /* This routine is a minimal replacement for
31123 c_parser_struct_declaration () used when parsing the list of
31124 types/names or ObjC++ properties. For example, when parsing the
31125 code
31126
31127 @property (readonly) int a, b, c;
31128
31129 this function is responsible for parsing "int a, int b, int c" and
31130 returning the declarations as CHAIN of DECLs.
31131
31132 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31133 similar parsing. */
31134 static tree
31135 cp_parser_objc_struct_declaration (cp_parser *parser)
31136 {
31137 tree decls = NULL_TREE;
31138 cp_decl_specifier_seq declspecs;
31139 int decl_class_or_enum_p;
31140 tree prefix_attributes;
31141
31142 cp_parser_decl_specifier_seq (parser,
31143 CP_PARSER_FLAGS_NONE,
31144 &declspecs,
31145 &decl_class_or_enum_p);
31146
31147 if (declspecs.type == error_mark_node)
31148 return error_mark_node;
31149
31150 /* auto, register, static, extern, mutable. */
31151 if (declspecs.storage_class != sc_none)
31152 {
31153 cp_parser_error (parser, "invalid type for property");
31154 declspecs.storage_class = sc_none;
31155 }
31156
31157 /* thread_local. */
31158 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31159 {
31160 cp_parser_error (parser, "invalid type for property");
31161 declspecs.locations[ds_thread] = 0;
31162 }
31163
31164 /* typedef. */
31165 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31166 {
31167 cp_parser_error (parser, "invalid type for property");
31168 declspecs.locations[ds_typedef] = 0;
31169 }
31170
31171 prefix_attributes = declspecs.attributes;
31172 declspecs.attributes = NULL_TREE;
31173
31174 /* Keep going until we hit the `;' at the end of the declaration. */
31175 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31176 {
31177 tree attributes, first_attribute, decl;
31178 cp_declarator *declarator;
31179 cp_token *token;
31180
31181 /* Parse the declarator. */
31182 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31183 NULL, NULL, false, false);
31184
31185 /* Look for attributes that apply to the ivar. */
31186 attributes = cp_parser_attributes_opt (parser);
31187 /* Remember which attributes are prefix attributes and
31188 which are not. */
31189 first_attribute = attributes;
31190 /* Combine the attributes. */
31191 attributes = attr_chainon (prefix_attributes, attributes);
31192
31193 decl = grokfield (declarator, &declspecs,
31194 NULL_TREE, /*init_const_expr_p=*/false,
31195 NULL_TREE, attributes);
31196
31197 if (decl == error_mark_node || decl == NULL_TREE)
31198 return error_mark_node;
31199
31200 /* Reset PREFIX_ATTRIBUTES. */
31201 if (attributes != error_mark_node)
31202 {
31203 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31204 attributes = TREE_CHAIN (attributes);
31205 if (attributes)
31206 TREE_CHAIN (attributes) = NULL_TREE;
31207 }
31208
31209 DECL_CHAIN (decl) = decls;
31210 decls = decl;
31211
31212 token = cp_lexer_peek_token (parser->lexer);
31213 if (token->type == CPP_COMMA)
31214 {
31215 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31216 continue;
31217 }
31218 else
31219 break;
31220 }
31221 return decls;
31222 }
31223
31224 /* Parse an Objective-C @property declaration. The syntax is:
31225
31226 objc-property-declaration:
31227 '@property' objc-property-attributes[opt] struct-declaration ;
31228
31229 objc-property-attributes:
31230 '(' objc-property-attribute-list ')'
31231
31232 objc-property-attribute-list:
31233 objc-property-attribute
31234 objc-property-attribute-list, objc-property-attribute
31235
31236 objc-property-attribute
31237 'getter' = identifier
31238 'setter' = identifier
31239 'readonly'
31240 'readwrite'
31241 'assign'
31242 'retain'
31243 'copy'
31244 'nonatomic'
31245
31246 For example:
31247 @property NSString *name;
31248 @property (readonly) id object;
31249 @property (retain, nonatomic, getter=getTheName) id name;
31250 @property int a, b, c;
31251
31252 PS: This function is identical to
31253 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31254 static void
31255 cp_parser_objc_at_property_declaration (cp_parser *parser)
31256 {
31257 /* The following variables hold the attributes of the properties as
31258 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31259 seen. When we see an attribute, we set them to 'true' (if they
31260 are boolean properties) or to the identifier (if they have an
31261 argument, ie, for getter and setter). Note that here we only
31262 parse the list of attributes, check the syntax and accumulate the
31263 attributes that we find. objc_add_property_declaration() will
31264 then process the information. */
31265 bool property_assign = false;
31266 bool property_copy = false;
31267 tree property_getter_ident = NULL_TREE;
31268 bool property_nonatomic = false;
31269 bool property_readonly = false;
31270 bool property_readwrite = false;
31271 bool property_retain = false;
31272 tree property_setter_ident = NULL_TREE;
31273
31274 /* 'properties' is the list of properties that we read. Usually a
31275 single one, but maybe more (eg, in "@property int a, b, c;" there
31276 are three). */
31277 tree properties;
31278 location_t loc;
31279
31280 loc = cp_lexer_peek_token (parser->lexer)->location;
31281
31282 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31283
31284 /* Parse the optional attribute list... */
31285 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31286 {
31287 /* Eat the '('. */
31288 matching_parens parens;
31289 parens.consume_open (parser);
31290
31291 while (true)
31292 {
31293 bool syntax_error = false;
31294 cp_token *token = cp_lexer_peek_token (parser->lexer);
31295 enum rid keyword;
31296
31297 if (token->type != CPP_NAME)
31298 {
31299 cp_parser_error (parser, "expected identifier");
31300 break;
31301 }
31302 keyword = C_RID_CODE (token->u.value);
31303 cp_lexer_consume_token (parser->lexer);
31304 switch (keyword)
31305 {
31306 case RID_ASSIGN: property_assign = true; break;
31307 case RID_COPY: property_copy = true; break;
31308 case RID_NONATOMIC: property_nonatomic = true; break;
31309 case RID_READONLY: property_readonly = true; break;
31310 case RID_READWRITE: property_readwrite = true; break;
31311 case RID_RETAIN: property_retain = true; break;
31312
31313 case RID_GETTER:
31314 case RID_SETTER:
31315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31316 {
31317 if (keyword == RID_GETTER)
31318 cp_parser_error (parser,
31319 "missing %<=%> (after %<getter%> attribute)");
31320 else
31321 cp_parser_error (parser,
31322 "missing %<=%> (after %<setter%> attribute)");
31323 syntax_error = true;
31324 break;
31325 }
31326 cp_lexer_consume_token (parser->lexer); /* eat the = */
31327 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31328 {
31329 cp_parser_error (parser, "expected identifier");
31330 syntax_error = true;
31331 break;
31332 }
31333 if (keyword == RID_SETTER)
31334 {
31335 if (property_setter_ident != NULL_TREE)
31336 {
31337 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31338 cp_lexer_consume_token (parser->lexer);
31339 }
31340 else
31341 property_setter_ident = cp_parser_objc_selector (parser);
31342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31343 cp_parser_error (parser, "setter name must terminate with %<:%>");
31344 else
31345 cp_lexer_consume_token (parser->lexer);
31346 }
31347 else
31348 {
31349 if (property_getter_ident != NULL_TREE)
31350 {
31351 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31352 cp_lexer_consume_token (parser->lexer);
31353 }
31354 else
31355 property_getter_ident = cp_parser_objc_selector (parser);
31356 }
31357 break;
31358 default:
31359 cp_parser_error (parser, "unknown property attribute");
31360 syntax_error = true;
31361 break;
31362 }
31363
31364 if (syntax_error)
31365 break;
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
31373 /* FIXME: "@property (setter, assign);" will generate a spurious
31374 "error: expected ‘)’ before ‘,’ token". This is because
31375 cp_parser_require, unlike the C counterpart, will produce an
31376 error even if we are in error recovery. */
31377 if (!parens.require_close (parser))
31378 {
31379 cp_parser_skip_to_closing_parenthesis (parser,
31380 /*recovering=*/true,
31381 /*or_comma=*/false,
31382 /*consume_paren=*/true);
31383 }
31384 }
31385
31386 /* ... and the property declaration(s). */
31387 properties = cp_parser_objc_struct_declaration (parser);
31388
31389 if (properties == error_mark_node)
31390 {
31391 cp_parser_skip_to_end_of_statement (parser);
31392 /* If the next token is now a `;', consume it. */
31393 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31394 cp_lexer_consume_token (parser->lexer);
31395 return;
31396 }
31397
31398 if (properties == NULL_TREE)
31399 cp_parser_error (parser, "expected identifier");
31400 else
31401 {
31402 /* Comma-separated properties are chained together in
31403 reverse order; add them one by one. */
31404 properties = nreverse (properties);
31405
31406 for (; properties; properties = TREE_CHAIN (properties))
31407 objc_add_property_declaration (loc, copy_node (properties),
31408 property_readonly, property_readwrite,
31409 property_assign, property_retain,
31410 property_copy, property_nonatomic,
31411 property_getter_ident, property_setter_ident);
31412 }
31413
31414 cp_parser_consume_semicolon_at_end_of_statement (parser);
31415 }
31416
31417 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31418
31419 objc-synthesize-declaration:
31420 @synthesize objc-synthesize-identifier-list ;
31421
31422 objc-synthesize-identifier-list:
31423 objc-synthesize-identifier
31424 objc-synthesize-identifier-list, objc-synthesize-identifier
31425
31426 objc-synthesize-identifier
31427 identifier
31428 identifier = identifier
31429
31430 For example:
31431 @synthesize MyProperty;
31432 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31433
31434 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31435 for C. Keep them in sync.
31436 */
31437 static void
31438 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31439 {
31440 tree list = NULL_TREE;
31441 location_t loc;
31442 loc = cp_lexer_peek_token (parser->lexer)->location;
31443
31444 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31445 while (true)
31446 {
31447 tree property, ivar;
31448 property = cp_parser_identifier (parser);
31449 if (property == error_mark_node)
31450 {
31451 cp_parser_consume_semicolon_at_end_of_statement (parser);
31452 return;
31453 }
31454 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31455 {
31456 cp_lexer_consume_token (parser->lexer);
31457 ivar = cp_parser_identifier (parser);
31458 if (ivar == error_mark_node)
31459 {
31460 cp_parser_consume_semicolon_at_end_of_statement (parser);
31461 return;
31462 }
31463 }
31464 else
31465 ivar = NULL_TREE;
31466 list = chainon (list, build_tree_list (ivar, property));
31467 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31468 cp_lexer_consume_token (parser->lexer);
31469 else
31470 break;
31471 }
31472 cp_parser_consume_semicolon_at_end_of_statement (parser);
31473 objc_add_synthesize_declaration (loc, list);
31474 }
31475
31476 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31477
31478 objc-dynamic-declaration:
31479 @dynamic identifier-list ;
31480
31481 For example:
31482 @dynamic MyProperty;
31483 @dynamic MyProperty, AnotherProperty;
31484
31485 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31486 for C. Keep them in sync.
31487 */
31488 static void
31489 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31490 {
31491 tree list = NULL_TREE;
31492 location_t loc;
31493 loc = cp_lexer_peek_token (parser->lexer)->location;
31494
31495 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31496 while (true)
31497 {
31498 tree property;
31499 property = cp_parser_identifier (parser);
31500 if (property == error_mark_node)
31501 {
31502 cp_parser_consume_semicolon_at_end_of_statement (parser);
31503 return;
31504 }
31505 list = chainon (list, build_tree_list (NULL, property));
31506 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31507 cp_lexer_consume_token (parser->lexer);
31508 else
31509 break;
31510 }
31511 cp_parser_consume_semicolon_at_end_of_statement (parser);
31512 objc_add_dynamic_declaration (loc, list);
31513 }
31514
31515 \f
31516 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31517
31518 /* Returns name of the next clause.
31519 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31520 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31521 returned and the token is consumed. */
31522
31523 static pragma_omp_clause
31524 cp_parser_omp_clause_name (cp_parser *parser)
31525 {
31526 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31527
31528 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31529 result = PRAGMA_OACC_CLAUSE_AUTO;
31530 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31531 result = PRAGMA_OMP_CLAUSE_IF;
31532 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31533 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31534 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31535 result = PRAGMA_OACC_CLAUSE_DELETE;
31536 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31537 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31538 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31539 result = PRAGMA_OMP_CLAUSE_FOR;
31540 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31541 {
31542 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31543 const char *p = IDENTIFIER_POINTER (id);
31544
31545 switch (p[0])
31546 {
31547 case 'a':
31548 if (!strcmp ("aligned", p))
31549 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31550 else if (!strcmp ("async", p))
31551 result = PRAGMA_OACC_CLAUSE_ASYNC;
31552 break;
31553 case 'c':
31554 if (!strcmp ("collapse", p))
31555 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31556 else if (!strcmp ("copy", p))
31557 result = PRAGMA_OACC_CLAUSE_COPY;
31558 else if (!strcmp ("copyin", p))
31559 result = PRAGMA_OMP_CLAUSE_COPYIN;
31560 else if (!strcmp ("copyout", p))
31561 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31562 else if (!strcmp ("copyprivate", p))
31563 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31564 else if (!strcmp ("create", p))
31565 result = PRAGMA_OACC_CLAUSE_CREATE;
31566 break;
31567 case 'd':
31568 if (!strcmp ("defaultmap", p))
31569 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31570 else if (!strcmp ("depend", p))
31571 result = PRAGMA_OMP_CLAUSE_DEPEND;
31572 else if (!strcmp ("device", p))
31573 result = PRAGMA_OMP_CLAUSE_DEVICE;
31574 else if (!strcmp ("deviceptr", p))
31575 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31576 else if (!strcmp ("device_resident", p))
31577 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31578 else if (!strcmp ("dist_schedule", p))
31579 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31580 break;
31581 case 'f':
31582 if (!strcmp ("final", p))
31583 result = PRAGMA_OMP_CLAUSE_FINAL;
31584 else if (!strcmp ("finalize", p))
31585 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31586 else if (!strcmp ("firstprivate", p))
31587 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31588 else if (!strcmp ("from", p))
31589 result = PRAGMA_OMP_CLAUSE_FROM;
31590 break;
31591 case 'g':
31592 if (!strcmp ("gang", p))
31593 result = PRAGMA_OACC_CLAUSE_GANG;
31594 else if (!strcmp ("grainsize", p))
31595 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31596 break;
31597 case 'h':
31598 if (!strcmp ("hint", p))
31599 result = PRAGMA_OMP_CLAUSE_HINT;
31600 else if (!strcmp ("host", p))
31601 result = PRAGMA_OACC_CLAUSE_HOST;
31602 break;
31603 case 'i':
31604 if (!strcmp ("if_present", p))
31605 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31606 else if (!strcmp ("in_reduction", p))
31607 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
31608 else if (!strcmp ("inbranch", p))
31609 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31610 else if (!strcmp ("independent", p))
31611 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31612 else if (!strcmp ("is_device_ptr", p))
31613 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31614 break;
31615 case 'l':
31616 if (!strcmp ("lastprivate", p))
31617 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31618 else if (!strcmp ("linear", p))
31619 result = PRAGMA_OMP_CLAUSE_LINEAR;
31620 else if (!strcmp ("link", p))
31621 result = PRAGMA_OMP_CLAUSE_LINK;
31622 break;
31623 case 'm':
31624 if (!strcmp ("map", p))
31625 result = PRAGMA_OMP_CLAUSE_MAP;
31626 else if (!strcmp ("mergeable", p))
31627 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31628 break;
31629 case 'n':
31630 if (!strcmp ("nogroup", p))
31631 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31632 else if (!strcmp ("nontemporal", p))
31633 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
31634 else if (!strcmp ("notinbranch", p))
31635 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31636 else if (!strcmp ("nowait", p))
31637 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31638 else if (!strcmp ("num_gangs", p))
31639 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31640 else if (!strcmp ("num_tasks", p))
31641 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31642 else if (!strcmp ("num_teams", p))
31643 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31644 else if (!strcmp ("num_threads", p))
31645 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31646 else if (!strcmp ("num_workers", p))
31647 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31648 break;
31649 case 'o':
31650 if (!strcmp ("ordered", p))
31651 result = PRAGMA_OMP_CLAUSE_ORDERED;
31652 break;
31653 case 'p':
31654 if (!strcmp ("parallel", p))
31655 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31656 else if (!strcmp ("present", p))
31657 result = PRAGMA_OACC_CLAUSE_PRESENT;
31658 else if (!strcmp ("present_or_copy", p)
31659 || !strcmp ("pcopy", p))
31660 result = PRAGMA_OACC_CLAUSE_COPY;
31661 else if (!strcmp ("present_or_copyin", p)
31662 || !strcmp ("pcopyin", p))
31663 result = PRAGMA_OACC_CLAUSE_COPYIN;
31664 else if (!strcmp ("present_or_copyout", p)
31665 || !strcmp ("pcopyout", p))
31666 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31667 else if (!strcmp ("present_or_create", p)
31668 || !strcmp ("pcreate", p))
31669 result = PRAGMA_OACC_CLAUSE_CREATE;
31670 else if (!strcmp ("priority", p))
31671 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31672 else if (!strcmp ("proc_bind", p))
31673 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31674 break;
31675 case 'r':
31676 if (!strcmp ("reduction", p))
31677 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31678 break;
31679 case 's':
31680 if (!strcmp ("safelen", p))
31681 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31682 else if (!strcmp ("schedule", p))
31683 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31684 else if (!strcmp ("sections", p))
31685 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31686 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31687 result = PRAGMA_OACC_CLAUSE_HOST;
31688 else if (!strcmp ("seq", p))
31689 result = PRAGMA_OACC_CLAUSE_SEQ;
31690 else if (!strcmp ("shared", p))
31691 result = PRAGMA_OMP_CLAUSE_SHARED;
31692 else if (!strcmp ("simd", p))
31693 result = PRAGMA_OMP_CLAUSE_SIMD;
31694 else if (!strcmp ("simdlen", p))
31695 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31696 break;
31697 case 't':
31698 if (!strcmp ("task_reduction", p))
31699 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
31700 else if (!strcmp ("taskgroup", p))
31701 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31702 else if (!strcmp ("thread_limit", p))
31703 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31704 else if (!strcmp ("threads", p))
31705 result = PRAGMA_OMP_CLAUSE_THREADS;
31706 else if (!strcmp ("tile", p))
31707 result = PRAGMA_OACC_CLAUSE_TILE;
31708 else if (!strcmp ("to", p))
31709 result = PRAGMA_OMP_CLAUSE_TO;
31710 break;
31711 case 'u':
31712 if (!strcmp ("uniform", p))
31713 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31714 else if (!strcmp ("untied", p))
31715 result = PRAGMA_OMP_CLAUSE_UNTIED;
31716 else if (!strcmp ("use_device", p))
31717 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31718 else if (!strcmp ("use_device_ptr", p))
31719 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31720 break;
31721 case 'v':
31722 if (!strcmp ("vector", p))
31723 result = PRAGMA_OACC_CLAUSE_VECTOR;
31724 else if (!strcmp ("vector_length", p))
31725 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31726 break;
31727 case 'w':
31728 if (!strcmp ("wait", p))
31729 result = PRAGMA_OACC_CLAUSE_WAIT;
31730 else if (!strcmp ("worker", p))
31731 result = PRAGMA_OACC_CLAUSE_WORKER;
31732 break;
31733 }
31734 }
31735
31736 if (result != PRAGMA_OMP_CLAUSE_NONE)
31737 cp_lexer_consume_token (parser->lexer);
31738
31739 return result;
31740 }
31741
31742 /* Validate that a clause of the given type does not already exist. */
31743
31744 static void
31745 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31746 const char *name, location_t location)
31747 {
31748 tree c;
31749
31750 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31751 if (OMP_CLAUSE_CODE (c) == code)
31752 {
31753 error_at (location, "too many %qs clauses", name);
31754 break;
31755 }
31756 }
31757
31758 /* OpenMP 2.5:
31759 variable-list:
31760 identifier
31761 variable-list , identifier
31762
31763 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31764 colon). An opening parenthesis will have been consumed by the caller.
31765
31766 If KIND is nonzero, create the appropriate node and install the decl
31767 in OMP_CLAUSE_DECL and add the node to the head of the list.
31768
31769 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31770 return the list created.
31771
31772 COLON can be NULL if only closing parenthesis should end the list,
31773 or pointer to bool which will receive false if the list is terminated
31774 by closing parenthesis or true if the list is terminated by colon. */
31775
31776 static tree
31777 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31778 tree list, bool *colon)
31779 {
31780 cp_token *token;
31781 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31782 if (colon)
31783 {
31784 parser->colon_corrects_to_scope_p = false;
31785 *colon = false;
31786 }
31787 while (1)
31788 {
31789 tree name, decl;
31790
31791 if (kind == OMP_CLAUSE_DEPEND)
31792 cp_parser_parse_tentatively (parser);
31793 token = cp_lexer_peek_token (parser->lexer);
31794 if (kind != 0
31795 && current_class_ptr
31796 && cp_parser_is_keyword (token, RID_THIS))
31797 {
31798 decl = finish_this_expr ();
31799 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31800 || CONVERT_EXPR_P (decl))
31801 decl = TREE_OPERAND (decl, 0);
31802 cp_lexer_consume_token (parser->lexer);
31803 }
31804 else
31805 {
31806 name = cp_parser_id_expression (parser, /*template_p=*/false,
31807 /*check_dependency_p=*/true,
31808 /*template_p=*/NULL,
31809 /*declarator_p=*/false,
31810 /*optional_p=*/false);
31811 if (name == error_mark_node)
31812 {
31813 if (kind == OMP_CLAUSE_DEPEND
31814 && cp_parser_simulate_error (parser))
31815 goto depend_lvalue;
31816 goto skip_comma;
31817 }
31818
31819 if (identifier_p (name))
31820 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31821 else
31822 decl = name;
31823 if (decl == error_mark_node)
31824 {
31825 if (kind == OMP_CLAUSE_DEPEND
31826 && cp_parser_simulate_error (parser))
31827 goto depend_lvalue;
31828 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31829 token->location);
31830 }
31831 }
31832 if (decl == error_mark_node)
31833 ;
31834 else if (kind != 0)
31835 {
31836 switch (kind)
31837 {
31838 case OMP_CLAUSE__CACHE_:
31839 /* The OpenACC cache directive explicitly only allows "array
31840 elements or subarrays". */
31841 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31842 {
31843 error_at (token->location, "expected %<[%>");
31844 decl = error_mark_node;
31845 break;
31846 }
31847 /* FALLTHROUGH. */
31848 case OMP_CLAUSE_MAP:
31849 case OMP_CLAUSE_FROM:
31850 case OMP_CLAUSE_TO:
31851 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31852 {
31853 location_t loc
31854 = cp_lexer_peek_token (parser->lexer)->location;
31855 cp_id_kind idk = CP_ID_KIND_NONE;
31856 cp_lexer_consume_token (parser->lexer);
31857 decl = convert_from_reference (decl);
31858 decl
31859 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31860 decl, false,
31861 &idk, loc);
31862 }
31863 /* FALLTHROUGH. */
31864 case OMP_CLAUSE_DEPEND:
31865 case OMP_CLAUSE_REDUCTION:
31866 case OMP_CLAUSE_IN_REDUCTION:
31867 case OMP_CLAUSE_TASK_REDUCTION:
31868 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31869 {
31870 tree low_bound = NULL_TREE, length = NULL_TREE;
31871
31872 parser->colon_corrects_to_scope_p = false;
31873 cp_lexer_consume_token (parser->lexer);
31874 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31875 low_bound = cp_parser_expression (parser);
31876 if (!colon)
31877 parser->colon_corrects_to_scope_p
31878 = saved_colon_corrects_to_scope_p;
31879 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31880 length = integer_one_node;
31881 else
31882 {
31883 /* Look for `:'. */
31884 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31885 {
31886 if (kind == OMP_CLAUSE_DEPEND
31887 && cp_parser_simulate_error (parser))
31888 goto depend_lvalue;
31889 goto skip_comma;
31890 }
31891 if (kind == OMP_CLAUSE_DEPEND)
31892 cp_parser_commit_to_tentative_parse (parser);
31893 if (!cp_lexer_next_token_is (parser->lexer,
31894 CPP_CLOSE_SQUARE))
31895 length = cp_parser_expression (parser);
31896 }
31897 /* Look for the closing `]'. */
31898 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31899 RT_CLOSE_SQUARE))
31900 {
31901 if (kind == OMP_CLAUSE_DEPEND
31902 && cp_parser_simulate_error (parser))
31903 goto depend_lvalue;
31904 goto skip_comma;
31905 }
31906
31907 decl = tree_cons (low_bound, length, decl);
31908 }
31909 break;
31910 default:
31911 break;
31912 }
31913
31914 if (kind == OMP_CLAUSE_DEPEND)
31915 {
31916 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
31917 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31918 && cp_parser_simulate_error (parser))
31919 {
31920 depend_lvalue:
31921 cp_parser_abort_tentative_parse (parser);
31922 decl = cp_parser_assignment_expression (parser, NULL,
31923 false, false);
31924 }
31925 else
31926 cp_parser_parse_definitely (parser);
31927 }
31928
31929 tree u = build_omp_clause (token->location, kind);
31930 OMP_CLAUSE_DECL (u) = decl;
31931 OMP_CLAUSE_CHAIN (u) = list;
31932 list = u;
31933 }
31934 else
31935 list = tree_cons (decl, NULL_TREE, list);
31936
31937 get_comma:
31938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31939 break;
31940 cp_lexer_consume_token (parser->lexer);
31941 }
31942
31943 if (colon)
31944 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31945
31946 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31947 {
31948 *colon = true;
31949 cp_parser_require (parser, CPP_COLON, RT_COLON);
31950 return list;
31951 }
31952
31953 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31954 {
31955 int ending;
31956
31957 /* Try to resync to an unnested comma. Copied from
31958 cp_parser_parenthesized_expression_list. */
31959 skip_comma:
31960 if (colon)
31961 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31962 ending = cp_parser_skip_to_closing_parenthesis (parser,
31963 /*recovering=*/true,
31964 /*or_comma=*/true,
31965 /*consume_paren=*/true);
31966 if (ending < 0)
31967 goto get_comma;
31968 }
31969
31970 return list;
31971 }
31972
31973 /* Similarly, but expect leading and trailing parenthesis. This is a very
31974 common case for omp clauses. */
31975
31976 static tree
31977 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31978 {
31979 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31980 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31981 return list;
31982 }
31983
31984 /* OpenACC 2.0:
31985 copy ( variable-list )
31986 copyin ( variable-list )
31987 copyout ( variable-list )
31988 create ( variable-list )
31989 delete ( variable-list )
31990 present ( variable-list ) */
31991
31992 static tree
31993 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31994 tree list)
31995 {
31996 enum gomp_map_kind kind;
31997 switch (c_kind)
31998 {
31999 case PRAGMA_OACC_CLAUSE_COPY:
32000 kind = GOMP_MAP_TOFROM;
32001 break;
32002 case PRAGMA_OACC_CLAUSE_COPYIN:
32003 kind = GOMP_MAP_TO;
32004 break;
32005 case PRAGMA_OACC_CLAUSE_COPYOUT:
32006 kind = GOMP_MAP_FROM;
32007 break;
32008 case PRAGMA_OACC_CLAUSE_CREATE:
32009 kind = GOMP_MAP_ALLOC;
32010 break;
32011 case PRAGMA_OACC_CLAUSE_DELETE:
32012 kind = GOMP_MAP_RELEASE;
32013 break;
32014 case PRAGMA_OACC_CLAUSE_DEVICE:
32015 kind = GOMP_MAP_FORCE_TO;
32016 break;
32017 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32018 kind = GOMP_MAP_DEVICE_RESIDENT;
32019 break;
32020 case PRAGMA_OACC_CLAUSE_HOST:
32021 kind = GOMP_MAP_FORCE_FROM;
32022 break;
32023 case PRAGMA_OACC_CLAUSE_LINK:
32024 kind = GOMP_MAP_LINK;
32025 break;
32026 case PRAGMA_OACC_CLAUSE_PRESENT:
32027 kind = GOMP_MAP_FORCE_PRESENT;
32028 break;
32029 default:
32030 gcc_unreachable ();
32031 }
32032 tree nl, c;
32033 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32034
32035 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32036 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32037
32038 return nl;
32039 }
32040
32041 /* OpenACC 2.0:
32042 deviceptr ( variable-list ) */
32043
32044 static tree
32045 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32046 {
32047 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32048 tree vars, t;
32049
32050 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32051 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32052 variable-list must only allow for pointer variables. */
32053 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32054 for (t = vars; t; t = TREE_CHAIN (t))
32055 {
32056 tree v = TREE_PURPOSE (t);
32057 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32058 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32059 OMP_CLAUSE_DECL (u) = v;
32060 OMP_CLAUSE_CHAIN (u) = list;
32061 list = u;
32062 }
32063
32064 return list;
32065 }
32066
32067 /* OpenACC 2.5:
32068 auto
32069 finalize
32070 independent
32071 nohost
32072 seq */
32073
32074 static tree
32075 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
32076 enum omp_clause_code code,
32077 tree list, location_t location)
32078 {
32079 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32080 tree c = build_omp_clause (location, code);
32081 OMP_CLAUSE_CHAIN (c) = list;
32082 return c;
32083 }
32084
32085 /* OpenACC:
32086 num_gangs ( expression )
32087 num_workers ( expression )
32088 vector_length ( expression ) */
32089
32090 static tree
32091 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32092 const char *str, tree list)
32093 {
32094 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32095
32096 matching_parens parens;
32097 if (!parens.require_open (parser))
32098 return list;
32099
32100 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32101
32102 if (t == error_mark_node
32103 || !parens.require_close (parser))
32104 {
32105 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32106 /*or_comma=*/false,
32107 /*consume_paren=*/true);
32108 return list;
32109 }
32110
32111 check_no_duplicate_clause (list, code, str, loc);
32112
32113 tree c = build_omp_clause (loc, code);
32114 OMP_CLAUSE_OPERAND (c, 0) = t;
32115 OMP_CLAUSE_CHAIN (c) = list;
32116 return c;
32117 }
32118
32119 /* OpenACC:
32120
32121 gang [( gang-arg-list )]
32122 worker [( [num:] int-expr )]
32123 vector [( [length:] int-expr )]
32124
32125 where gang-arg is one of:
32126
32127 [num:] int-expr
32128 static: size-expr
32129
32130 and size-expr may be:
32131
32132 *
32133 int-expr
32134 */
32135
32136 static tree
32137 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
32138 const char *str, tree list)
32139 {
32140 const char *id = "num";
32141 cp_lexer *lexer = parser->lexer;
32142 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32143 location_t loc = cp_lexer_peek_token (lexer)->location;
32144
32145 if (kind == OMP_CLAUSE_VECTOR)
32146 id = "length";
32147
32148 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32149 {
32150 matching_parens parens;
32151 parens.consume_open (parser);
32152
32153 do
32154 {
32155 cp_token *next = cp_lexer_peek_token (lexer);
32156 int idx = 0;
32157
32158 /* Gang static argument. */
32159 if (kind == OMP_CLAUSE_GANG
32160 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32161 {
32162 cp_lexer_consume_token (lexer);
32163
32164 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32165 goto cleanup_error;
32166
32167 idx = 1;
32168 if (ops[idx] != NULL)
32169 {
32170 cp_parser_error (parser, "too many %<static%> arguments");
32171 goto cleanup_error;
32172 }
32173
32174 /* Check for the '*' argument. */
32175 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32176 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32177 || cp_lexer_nth_token_is (parser->lexer, 2,
32178 CPP_CLOSE_PAREN)))
32179 {
32180 cp_lexer_consume_token (lexer);
32181 ops[idx] = integer_minus_one_node;
32182
32183 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32184 {
32185 cp_lexer_consume_token (lexer);
32186 continue;
32187 }
32188 else break;
32189 }
32190 }
32191 /* Worker num: argument and vector length: arguments. */
32192 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32193 && id_equal (next->u.value, id)
32194 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32195 {
32196 cp_lexer_consume_token (lexer); /* id */
32197 cp_lexer_consume_token (lexer); /* ':' */
32198 }
32199
32200 /* Now collect the actual argument. */
32201 if (ops[idx] != NULL_TREE)
32202 {
32203 cp_parser_error (parser, "unexpected argument");
32204 goto cleanup_error;
32205 }
32206
32207 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32208 false);
32209 if (expr == error_mark_node)
32210 goto cleanup_error;
32211
32212 mark_exp_read (expr);
32213 ops[idx] = expr;
32214
32215 if (kind == OMP_CLAUSE_GANG
32216 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32217 {
32218 cp_lexer_consume_token (lexer);
32219 continue;
32220 }
32221 break;
32222 }
32223 while (1);
32224
32225 if (!parens.require_close (parser))
32226 goto cleanup_error;
32227 }
32228
32229 check_no_duplicate_clause (list, kind, str, loc);
32230
32231 c = build_omp_clause (loc, kind);
32232
32233 if (ops[1])
32234 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32235
32236 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32237 OMP_CLAUSE_CHAIN (c) = list;
32238
32239 return c;
32240
32241 cleanup_error:
32242 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32243 return list;
32244 }
32245
32246 /* OpenACC 2.0:
32247 tile ( size-expr-list ) */
32248
32249 static tree
32250 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32251 {
32252 tree c, expr = error_mark_node;
32253 tree tile = NULL_TREE;
32254
32255 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32256 so, but the spec authors never considered such a case and have
32257 differing opinions on what it might mean, including 'not
32258 allowed'.) */
32259 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32260 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32261 clause_loc);
32262
32263 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32264 return list;
32265
32266 do
32267 {
32268 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32269 return list;
32270
32271 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32272 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32273 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32274 {
32275 cp_lexer_consume_token (parser->lexer);
32276 expr = integer_zero_node;
32277 }
32278 else
32279 expr = cp_parser_constant_expression (parser);
32280
32281 tile = tree_cons (NULL_TREE, expr, tile);
32282 }
32283 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32284
32285 /* Consume the trailing ')'. */
32286 cp_lexer_consume_token (parser->lexer);
32287
32288 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32289 tile = nreverse (tile);
32290 OMP_CLAUSE_TILE_LIST (c) = tile;
32291 OMP_CLAUSE_CHAIN (c) = list;
32292 return c;
32293 }
32294
32295 /* OpenACC 2.0
32296 Parse wait clause or directive parameters. */
32297
32298 static tree
32299 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32300 {
32301 vec<tree, va_gc> *args;
32302 tree t, args_tree;
32303
32304 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32305 /*cast_p=*/false,
32306 /*allow_expansion_p=*/true,
32307 /*non_constant_p=*/NULL);
32308
32309 if (args == NULL || args->length () == 0)
32310 {
32311 cp_parser_error (parser, "expected integer expression before ')'");
32312 if (args != NULL)
32313 release_tree_vector (args);
32314 return list;
32315 }
32316
32317 args_tree = build_tree_list_vec (args);
32318
32319 release_tree_vector (args);
32320
32321 for (t = args_tree; t; t = TREE_CHAIN (t))
32322 {
32323 tree targ = TREE_VALUE (t);
32324
32325 if (targ != error_mark_node)
32326 {
32327 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32328 error ("%<wait%> expression must be integral");
32329 else
32330 {
32331 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32332
32333 targ = mark_rvalue_use (targ);
32334 OMP_CLAUSE_DECL (c) = targ;
32335 OMP_CLAUSE_CHAIN (c) = list;
32336 list = c;
32337 }
32338 }
32339 }
32340
32341 return list;
32342 }
32343
32344 /* OpenACC:
32345 wait ( int-expr-list ) */
32346
32347 static tree
32348 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32349 {
32350 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32351
32352 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32353 return list;
32354
32355 list = cp_parser_oacc_wait_list (parser, location, list);
32356
32357 return list;
32358 }
32359
32360 /* OpenMP 3.0:
32361 collapse ( constant-expression ) */
32362
32363 static tree
32364 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32365 {
32366 tree c, num;
32367 location_t loc;
32368 HOST_WIDE_INT n;
32369
32370 loc = cp_lexer_peek_token (parser->lexer)->location;
32371 matching_parens parens;
32372 if (!parens.require_open (parser))
32373 return list;
32374
32375 num = cp_parser_constant_expression (parser);
32376
32377 if (!parens.require_close (parser))
32378 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32379 /*or_comma=*/false,
32380 /*consume_paren=*/true);
32381
32382 if (num == error_mark_node)
32383 return list;
32384 num = fold_non_dependent_expr (num);
32385 if (!tree_fits_shwi_p (num)
32386 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32387 || (n = tree_to_shwi (num)) <= 0
32388 || (int) n != n)
32389 {
32390 error_at (loc, "collapse argument needs positive constant integer expression");
32391 return list;
32392 }
32393
32394 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32395 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32396 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32397 OMP_CLAUSE_CHAIN (c) = list;
32398 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32399
32400 return c;
32401 }
32402
32403 /* OpenMP 2.5:
32404 default ( none | shared )
32405
32406 OpenACC:
32407 default ( none | present ) */
32408
32409 static tree
32410 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32411 location_t location, bool is_oacc)
32412 {
32413 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32414 tree c;
32415
32416 matching_parens parens;
32417 if (!parens.require_open (parser))
32418 return list;
32419 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32420 {
32421 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32422 const char *p = IDENTIFIER_POINTER (id);
32423
32424 switch (p[0])
32425 {
32426 case 'n':
32427 if (strcmp ("none", p) != 0)
32428 goto invalid_kind;
32429 kind = OMP_CLAUSE_DEFAULT_NONE;
32430 break;
32431
32432 case 'p':
32433 if (strcmp ("present", p) != 0 || !is_oacc)
32434 goto invalid_kind;
32435 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32436 break;
32437
32438 case 's':
32439 if (strcmp ("shared", p) != 0 || is_oacc)
32440 goto invalid_kind;
32441 kind = OMP_CLAUSE_DEFAULT_SHARED;
32442 break;
32443
32444 default:
32445 goto invalid_kind;
32446 }
32447
32448 cp_lexer_consume_token (parser->lexer);
32449 }
32450 else
32451 {
32452 invalid_kind:
32453 if (is_oacc)
32454 cp_parser_error (parser, "expected %<none%> or %<present%>");
32455 else
32456 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32457 }
32458
32459 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32460 || !parens.require_close (parser))
32461 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32462 /*or_comma=*/false,
32463 /*consume_paren=*/true);
32464
32465 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32466 return list;
32467
32468 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32469 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32470 OMP_CLAUSE_CHAIN (c) = list;
32471 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32472
32473 return c;
32474 }
32475
32476 /* OpenMP 3.1:
32477 final ( expression ) */
32478
32479 static tree
32480 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32481 {
32482 tree t, c;
32483
32484 matching_parens parens;
32485 if (!parens.require_open (parser))
32486 return list;
32487
32488 t = cp_parser_assignment_expression (parser);
32489
32490 if (t == error_mark_node
32491 || !parens.require_close (parser))
32492 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32493 /*or_comma=*/false,
32494 /*consume_paren=*/true);
32495
32496 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32497
32498 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32499 OMP_CLAUSE_FINAL_EXPR (c) = t;
32500 OMP_CLAUSE_CHAIN (c) = list;
32501
32502 return c;
32503 }
32504
32505 /* OpenMP 2.5:
32506 if ( expression )
32507
32508 OpenMP 4.5:
32509 if ( directive-name-modifier : expression )
32510
32511 directive-name-modifier:
32512 parallel | task | taskloop | target data | target | target update
32513 | target enter data | target exit data
32514
32515 OpenMP 5.0:
32516 directive-name-modifier:
32517 ... | simd | cancel */
32518
32519 static tree
32520 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32521 bool is_omp)
32522 {
32523 tree t, c;
32524 enum tree_code if_modifier = ERROR_MARK;
32525
32526 matching_parens parens;
32527 if (!parens.require_open (parser))
32528 return list;
32529
32530 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32531 {
32532 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32533 const char *p = IDENTIFIER_POINTER (id);
32534 int n = 2;
32535
32536 if (strcmp ("cancel", p) == 0)
32537 if_modifier = VOID_CST;
32538 else if (strcmp ("parallel", p) == 0)
32539 if_modifier = OMP_PARALLEL;
32540 else if (strcmp ("simd", p) == 0)
32541 if_modifier = OMP_SIMD;
32542 else if (strcmp ("task", p) == 0)
32543 if_modifier = OMP_TASK;
32544 else if (strcmp ("taskloop", p) == 0)
32545 if_modifier = OMP_TASKLOOP;
32546 else if (strcmp ("target", p) == 0)
32547 {
32548 if_modifier = OMP_TARGET;
32549 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32550 {
32551 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32552 p = IDENTIFIER_POINTER (id);
32553 if (strcmp ("data", p) == 0)
32554 if_modifier = OMP_TARGET_DATA;
32555 else if (strcmp ("update", p) == 0)
32556 if_modifier = OMP_TARGET_UPDATE;
32557 else if (strcmp ("enter", p) == 0)
32558 if_modifier = OMP_TARGET_ENTER_DATA;
32559 else if (strcmp ("exit", p) == 0)
32560 if_modifier = OMP_TARGET_EXIT_DATA;
32561 if (if_modifier != OMP_TARGET)
32562 n = 3;
32563 else
32564 {
32565 location_t loc
32566 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32567 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32568 "or %<exit%>");
32569 if_modifier = ERROR_MARK;
32570 }
32571 if (if_modifier == OMP_TARGET_ENTER_DATA
32572 || if_modifier == OMP_TARGET_EXIT_DATA)
32573 {
32574 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32575 {
32576 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32577 p = IDENTIFIER_POINTER (id);
32578 if (strcmp ("data", p) == 0)
32579 n = 4;
32580 }
32581 if (n != 4)
32582 {
32583 location_t loc
32584 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32585 error_at (loc, "expected %<data%>");
32586 if_modifier = ERROR_MARK;
32587 }
32588 }
32589 }
32590 }
32591 if (if_modifier != ERROR_MARK)
32592 {
32593 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32594 {
32595 while (n-- > 0)
32596 cp_lexer_consume_token (parser->lexer);
32597 }
32598 else
32599 {
32600 if (n > 2)
32601 {
32602 location_t loc
32603 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32604 error_at (loc, "expected %<:%>");
32605 }
32606 if_modifier = ERROR_MARK;
32607 }
32608 }
32609 }
32610
32611 t = cp_parser_assignment_expression (parser);
32612
32613 if (t == error_mark_node
32614 || !parens.require_close (parser))
32615 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32616 /*or_comma=*/false,
32617 /*consume_paren=*/true);
32618
32619 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32620 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32621 {
32622 if (if_modifier != ERROR_MARK
32623 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32624 {
32625 const char *p = NULL;
32626 switch (if_modifier)
32627 {
32628 case VOID_CST: p = "cancel"; break;
32629 case OMP_PARALLEL: p = "parallel"; break;
32630 case OMP_SIMD: p = "simd"; break;
32631 case OMP_TASK: p = "task"; break;
32632 case OMP_TASKLOOP: p = "taskloop"; break;
32633 case OMP_TARGET_DATA: p = "target data"; break;
32634 case OMP_TARGET: p = "target"; break;
32635 case OMP_TARGET_UPDATE: p = "target update"; break;
32636 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32637 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32638 default: gcc_unreachable ();
32639 }
32640 error_at (location, "too many %<if%> clauses with %qs modifier",
32641 p);
32642 return list;
32643 }
32644 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32645 {
32646 if (!is_omp)
32647 error_at (location, "too many %<if%> clauses");
32648 else
32649 error_at (location, "too many %<if%> clauses without modifier");
32650 return list;
32651 }
32652 else if (if_modifier == ERROR_MARK
32653 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32654 {
32655 error_at (location, "if any %<if%> clause has modifier, then all "
32656 "%<if%> clauses have to use modifier");
32657 return list;
32658 }
32659 }
32660
32661 c = build_omp_clause (location, OMP_CLAUSE_IF);
32662 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32663 OMP_CLAUSE_IF_EXPR (c) = t;
32664 OMP_CLAUSE_CHAIN (c) = list;
32665
32666 return c;
32667 }
32668
32669 /* OpenMP 3.1:
32670 mergeable */
32671
32672 static tree
32673 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32674 tree list, location_t location)
32675 {
32676 tree c;
32677
32678 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32679 location);
32680
32681 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32682 OMP_CLAUSE_CHAIN (c) = list;
32683 return c;
32684 }
32685
32686 /* OpenMP 2.5:
32687 nowait */
32688
32689 static tree
32690 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32691 tree list, location_t location)
32692 {
32693 tree c;
32694
32695 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32696
32697 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32698 OMP_CLAUSE_CHAIN (c) = list;
32699 return c;
32700 }
32701
32702 /* OpenMP 2.5:
32703 num_threads ( expression ) */
32704
32705 static tree
32706 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32707 location_t location)
32708 {
32709 tree t, c;
32710
32711 matching_parens parens;
32712 if (!parens.require_open (parser))
32713 return list;
32714
32715 t = cp_parser_assignment_expression (parser);
32716
32717 if (t == error_mark_node
32718 || !parens.require_close (parser))
32719 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32720 /*or_comma=*/false,
32721 /*consume_paren=*/true);
32722
32723 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32724 "num_threads", location);
32725
32726 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32727 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32728 OMP_CLAUSE_CHAIN (c) = list;
32729
32730 return c;
32731 }
32732
32733 /* OpenMP 4.5:
32734 num_tasks ( expression ) */
32735
32736 static tree
32737 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32738 location_t location)
32739 {
32740 tree t, c;
32741
32742 matching_parens parens;
32743 if (!parens.require_open (parser))
32744 return list;
32745
32746 t = cp_parser_assignment_expression (parser);
32747
32748 if (t == error_mark_node
32749 || !parens.require_close (parser))
32750 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32751 /*or_comma=*/false,
32752 /*consume_paren=*/true);
32753
32754 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32755 "num_tasks", location);
32756
32757 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32758 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32759 OMP_CLAUSE_CHAIN (c) = list;
32760
32761 return c;
32762 }
32763
32764 /* OpenMP 4.5:
32765 grainsize ( expression ) */
32766
32767 static tree
32768 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32769 location_t location)
32770 {
32771 tree t, c;
32772
32773 matching_parens parens;
32774 if (!parens.require_open (parser))
32775 return list;
32776
32777 t = cp_parser_assignment_expression (parser);
32778
32779 if (t == error_mark_node
32780 || !parens.require_close (parser))
32781 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32782 /*or_comma=*/false,
32783 /*consume_paren=*/true);
32784
32785 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32786 "grainsize", location);
32787
32788 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32789 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32790 OMP_CLAUSE_CHAIN (c) = list;
32791
32792 return c;
32793 }
32794
32795 /* OpenMP 4.5:
32796 priority ( expression ) */
32797
32798 static tree
32799 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32800 location_t location)
32801 {
32802 tree t, c;
32803
32804 matching_parens parens;
32805 if (!parens.require_open (parser))
32806 return list;
32807
32808 t = cp_parser_assignment_expression (parser);
32809
32810 if (t == error_mark_node
32811 || !parens.require_close (parser))
32812 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32813 /*or_comma=*/false,
32814 /*consume_paren=*/true);
32815
32816 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32817 "priority", location);
32818
32819 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32820 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32821 OMP_CLAUSE_CHAIN (c) = list;
32822
32823 return c;
32824 }
32825
32826 /* OpenMP 4.5:
32827 hint ( expression ) */
32828
32829 static tree
32830 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
32831 {
32832 tree t, c;
32833
32834 matching_parens parens;
32835 if (!parens.require_open (parser))
32836 return list;
32837
32838 t = cp_parser_assignment_expression (parser);
32839
32840 if (t == error_mark_node
32841 || !parens.require_close (parser))
32842 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32843 /*or_comma=*/false,
32844 /*consume_paren=*/true);
32845
32846 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32847
32848 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32849 OMP_CLAUSE_HINT_EXPR (c) = t;
32850 OMP_CLAUSE_CHAIN (c) = list;
32851
32852 return c;
32853 }
32854
32855 /* OpenMP 4.5:
32856 defaultmap ( tofrom : scalar )
32857
32858 OpenMP 5.0:
32859 defaultmap ( implicit-behavior [ : variable-category ] ) */
32860
32861 static tree
32862 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32863 location_t location)
32864 {
32865 tree c, id;
32866 const char *p;
32867 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
32868 enum omp_clause_defaultmap_kind category
32869 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
32870
32871 matching_parens parens;
32872 if (!parens.require_open (parser))
32873 return list;
32874
32875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32876 p = "default";
32877 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32878 {
32879 invalid_behavior:
32880 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
32881 "%<tofrom%>, %<firstprivate%>, %<none%> "
32882 "or %<default%>");
32883 goto out_err;
32884 }
32885 else
32886 {
32887 id = cp_lexer_peek_token (parser->lexer)->u.value;
32888 p = IDENTIFIER_POINTER (id);
32889 }
32890
32891 switch (p[0])
32892 {
32893 case 'a':
32894 if (strcmp ("alloc", p) == 0)
32895 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
32896 else
32897 goto invalid_behavior;
32898 break;
32899
32900 case 'd':
32901 if (strcmp ("default", p) == 0)
32902 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
32903 else
32904 goto invalid_behavior;
32905 break;
32906
32907 case 'f':
32908 if (strcmp ("firstprivate", p) == 0)
32909 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
32910 else if (strcmp ("from", p) == 0)
32911 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
32912 else
32913 goto invalid_behavior;
32914 break;
32915
32916 case 'n':
32917 if (strcmp ("none", p) == 0)
32918 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
32919 else
32920 goto invalid_behavior;
32921 break;
32922
32923 case 't':
32924 if (strcmp ("tofrom", p) == 0)
32925 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
32926 else if (strcmp ("to", p) == 0)
32927 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
32928 else
32929 goto invalid_behavior;
32930 break;
32931
32932 default:
32933 goto invalid_behavior;
32934 }
32935 cp_lexer_consume_token (parser->lexer);
32936
32937 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32938 {
32939 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32940 goto out_err;
32941
32942 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32943 {
32944 invalid_category:
32945 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
32946 "%<pointer%>");
32947 goto out_err;
32948 }
32949 id = cp_lexer_peek_token (parser->lexer)->u.value;
32950 p = IDENTIFIER_POINTER (id);
32951
32952 switch (p[0])
32953 {
32954 case 'a':
32955 if (strcmp ("aggregate", p) == 0)
32956 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
32957 else
32958 goto invalid_category;
32959 break;
32960
32961 case 'p':
32962 if (strcmp ("pointer", p) == 0)
32963 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
32964 else
32965 goto invalid_category;
32966 break;
32967
32968 case 's':
32969 if (strcmp ("scalar", p) == 0)
32970 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
32971 else
32972 goto invalid_category;
32973 break;
32974
32975 default:
32976 goto invalid_category;
32977 }
32978
32979 cp_lexer_consume_token (parser->lexer);
32980 }
32981 if (!parens.require_close (parser))
32982 goto out_err;
32983
32984 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32985 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
32986 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
32987 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
32988 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
32989 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
32990 {
32991 enum omp_clause_defaultmap_kind cat = category;
32992 location_t loc = OMP_CLAUSE_LOCATION (c);
32993 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
32994 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
32995 p = NULL;
32996 switch (cat)
32997 {
32998 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
32999 p = NULL;
33000 break;
33001 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33002 p = "aggregate";
33003 break;
33004 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33005 p = "pointer";
33006 break;
33007 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33008 p = "scalar";
33009 break;
33010 default:
33011 gcc_unreachable ();
33012 }
33013 if (p)
33014 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33015 p);
33016 else
33017 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33018 "category");
33019 break;
33020 }
33021
33022 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33023 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33024 OMP_CLAUSE_CHAIN (c) = list;
33025 return c;
33026
33027 out_err:
33028 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33029 /*or_comma=*/false,
33030 /*consume_paren=*/true);
33031 return list;
33032 }
33033
33034 /* OpenMP 2.5:
33035 ordered
33036
33037 OpenMP 4.5:
33038 ordered ( constant-expression ) */
33039
33040 static tree
33041 cp_parser_omp_clause_ordered (cp_parser *parser,
33042 tree list, location_t location)
33043 {
33044 tree c, num = NULL_TREE;
33045 HOST_WIDE_INT n;
33046
33047 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33048 "ordered", location);
33049
33050 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33051 {
33052 matching_parens parens;
33053 parens.consume_open (parser);
33054
33055 num = cp_parser_constant_expression (parser);
33056
33057 if (!parens.require_close (parser))
33058 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33059 /*or_comma=*/false,
33060 /*consume_paren=*/true);
33061
33062 if (num == error_mark_node)
33063 return list;
33064 num = fold_non_dependent_expr (num);
33065 if (!tree_fits_shwi_p (num)
33066 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33067 || (n = tree_to_shwi (num)) <= 0
33068 || (int) n != n)
33069 {
33070 error_at (location,
33071 "ordered argument needs positive constant integer "
33072 "expression");
33073 return list;
33074 }
33075 }
33076
33077 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33078 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33079 OMP_CLAUSE_CHAIN (c) = list;
33080 return c;
33081 }
33082
33083 /* OpenMP 2.5:
33084 reduction ( reduction-operator : variable-list )
33085
33086 reduction-operator:
33087 One of: + * - & ^ | && ||
33088
33089 OpenMP 3.1:
33090
33091 reduction-operator:
33092 One of: + * - & ^ | && || min max
33093
33094 OpenMP 4.0:
33095
33096 reduction-operator:
33097 One of: + * - & ^ | && ||
33098 id-expression
33099
33100 OpenMP 5.0:
33101 reduction ( reduction-modifier, reduction-operator : variable-list )
33102 in_reduction ( reduction-operator : variable-list )
33103 task_reduction ( reduction-operator : variable-list ) */
33104
33105 static tree
33106 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33107 bool is_omp, tree list)
33108 {
33109 enum tree_code code = ERROR_MARK;
33110 tree nlist, c, id = NULL_TREE;
33111 bool task = false;
33112 bool inscan = false;
33113
33114 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33115 return list;
33116
33117 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33118 {
33119 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33120 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33121 {
33122 cp_lexer_consume_token (parser->lexer);
33123 cp_lexer_consume_token (parser->lexer);
33124 }
33125 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33126 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33127 {
33128 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33129 const char *p = IDENTIFIER_POINTER (id);
33130 if (strcmp (p, "task") == 0)
33131 task = true;
33132 else if (strcmp (p, "inscan") == 0)
33133 {
33134 inscan = true;
33135 sorry ("%<inscan%> modifier on %<reduction%> clause "
33136 "not supported yet");
33137 }
33138 if (task || inscan)
33139 {
33140 cp_lexer_consume_token (parser->lexer);
33141 cp_lexer_consume_token (parser->lexer);
33142 }
33143 }
33144 }
33145
33146 switch (cp_lexer_peek_token (parser->lexer)->type)
33147 {
33148 case CPP_PLUS: code = PLUS_EXPR; break;
33149 case CPP_MULT: code = MULT_EXPR; break;
33150 case CPP_MINUS: code = MINUS_EXPR; break;
33151 case CPP_AND: code = BIT_AND_EXPR; break;
33152 case CPP_XOR: code = BIT_XOR_EXPR; break;
33153 case CPP_OR: code = BIT_IOR_EXPR; break;
33154 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33155 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33156 default: break;
33157 }
33158
33159 if (code != ERROR_MARK)
33160 cp_lexer_consume_token (parser->lexer);
33161 else
33162 {
33163 bool saved_colon_corrects_to_scope_p;
33164 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33165 parser->colon_corrects_to_scope_p = false;
33166 id = cp_parser_id_expression (parser, /*template_p=*/false,
33167 /*check_dependency_p=*/true,
33168 /*template_p=*/NULL,
33169 /*declarator_p=*/false,
33170 /*optional_p=*/false);
33171 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33172 if (identifier_p (id))
33173 {
33174 const char *p = IDENTIFIER_POINTER (id);
33175
33176 if (strcmp (p, "min") == 0)
33177 code = MIN_EXPR;
33178 else if (strcmp (p, "max") == 0)
33179 code = MAX_EXPR;
33180 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33181 code = PLUS_EXPR;
33182 else if (id == ovl_op_identifier (false, MULT_EXPR))
33183 code = MULT_EXPR;
33184 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33185 code = MINUS_EXPR;
33186 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33187 code = BIT_AND_EXPR;
33188 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33189 code = BIT_IOR_EXPR;
33190 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33191 code = BIT_XOR_EXPR;
33192 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33193 code = TRUTH_ANDIF_EXPR;
33194 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33195 code = TRUTH_ORIF_EXPR;
33196 id = omp_reduction_id (code, id, NULL_TREE);
33197 tree scope = parser->scope;
33198 if (scope)
33199 id = build_qualified_name (NULL_TREE, scope, id, false);
33200 parser->scope = NULL_TREE;
33201 parser->qualifying_scope = NULL_TREE;
33202 parser->object_scope = NULL_TREE;
33203 }
33204 else
33205 {
33206 error ("invalid reduction-identifier");
33207 resync_fail:
33208 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33209 /*or_comma=*/false,
33210 /*consume_paren=*/true);
33211 return list;
33212 }
33213 }
33214
33215 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33216 goto resync_fail;
33217
33218 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33219 NULL);
33220 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33221 {
33222 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33223 if (task)
33224 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33225 else if (inscan)
33226 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33227 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33228 }
33229
33230 return nlist;
33231 }
33232
33233 /* OpenMP 2.5:
33234 schedule ( schedule-kind )
33235 schedule ( schedule-kind , expression )
33236
33237 schedule-kind:
33238 static | dynamic | guided | runtime | auto
33239
33240 OpenMP 4.5:
33241 schedule ( schedule-modifier : schedule-kind )
33242 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33243
33244 schedule-modifier:
33245 simd
33246 monotonic
33247 nonmonotonic */
33248
33249 static tree
33250 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33251 {
33252 tree c, t;
33253 int modifiers = 0, nmodifiers = 0;
33254
33255 matching_parens parens;
33256 if (!parens.require_open (parser))
33257 return list;
33258
33259 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33260
33261 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33262 {
33263 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33264 const char *p = IDENTIFIER_POINTER (id);
33265 if (strcmp ("simd", p) == 0)
33266 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33267 else if (strcmp ("monotonic", p) == 0)
33268 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33269 else if (strcmp ("nonmonotonic", p) == 0)
33270 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33271 else
33272 break;
33273 cp_lexer_consume_token (parser->lexer);
33274 if (nmodifiers++ == 0
33275 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33276 cp_lexer_consume_token (parser->lexer);
33277 else
33278 {
33279 cp_parser_require (parser, CPP_COLON, RT_COLON);
33280 break;
33281 }
33282 }
33283
33284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33285 {
33286 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33287 const char *p = IDENTIFIER_POINTER (id);
33288
33289 switch (p[0])
33290 {
33291 case 'd':
33292 if (strcmp ("dynamic", p) != 0)
33293 goto invalid_kind;
33294 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33295 break;
33296
33297 case 'g':
33298 if (strcmp ("guided", p) != 0)
33299 goto invalid_kind;
33300 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33301 break;
33302
33303 case 'r':
33304 if (strcmp ("runtime", p) != 0)
33305 goto invalid_kind;
33306 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33307 break;
33308
33309 default:
33310 goto invalid_kind;
33311 }
33312 }
33313 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33314 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33315 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33316 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33317 else
33318 goto invalid_kind;
33319 cp_lexer_consume_token (parser->lexer);
33320
33321 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33322 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33323 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33324 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33325 {
33326 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33327 "specified");
33328 modifiers = 0;
33329 }
33330
33331 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33332 {
33333 cp_token *token;
33334 cp_lexer_consume_token (parser->lexer);
33335
33336 token = cp_lexer_peek_token (parser->lexer);
33337 t = cp_parser_assignment_expression (parser);
33338
33339 if (t == error_mark_node)
33340 goto resync_fail;
33341 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33342 error_at (token->location, "schedule %<runtime%> does not take "
33343 "a %<chunk_size%> parameter");
33344 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33345 error_at (token->location, "schedule %<auto%> does not take "
33346 "a %<chunk_size%> parameter");
33347 else
33348 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33349
33350 if (!parens.require_close (parser))
33351 goto resync_fail;
33352 }
33353 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33354 goto resync_fail;
33355
33356 OMP_CLAUSE_SCHEDULE_KIND (c)
33357 = (enum omp_clause_schedule_kind)
33358 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33359
33360 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33361 OMP_CLAUSE_CHAIN (c) = list;
33362 return c;
33363
33364 invalid_kind:
33365 cp_parser_error (parser, "invalid schedule kind");
33366 resync_fail:
33367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33368 /*or_comma=*/false,
33369 /*consume_paren=*/true);
33370 return list;
33371 }
33372
33373 /* OpenMP 3.0:
33374 untied */
33375
33376 static tree
33377 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33378 tree list, location_t location)
33379 {
33380 tree c;
33381
33382 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33383
33384 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33385 OMP_CLAUSE_CHAIN (c) = list;
33386 return c;
33387 }
33388
33389 /* OpenMP 4.0:
33390 inbranch
33391 notinbranch */
33392
33393 static tree
33394 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33395 tree list, location_t location)
33396 {
33397 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33398 tree c = build_omp_clause (location, code);
33399 OMP_CLAUSE_CHAIN (c) = list;
33400 return c;
33401 }
33402
33403 /* OpenMP 4.0:
33404 parallel
33405 for
33406 sections
33407 taskgroup */
33408
33409 static tree
33410 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33411 enum omp_clause_code code,
33412 tree list, location_t location)
33413 {
33414 tree c = build_omp_clause (location, code);
33415 OMP_CLAUSE_CHAIN (c) = list;
33416 return c;
33417 }
33418
33419 /* OpenMP 4.5:
33420 nogroup */
33421
33422 static tree
33423 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33424 tree list, location_t location)
33425 {
33426 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33427 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33428 OMP_CLAUSE_CHAIN (c) = list;
33429 return c;
33430 }
33431
33432 /* OpenMP 4.5:
33433 simd
33434 threads */
33435
33436 static tree
33437 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33438 enum omp_clause_code code,
33439 tree list, location_t location)
33440 {
33441 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33442 tree c = build_omp_clause (location, code);
33443 OMP_CLAUSE_CHAIN (c) = list;
33444 return c;
33445 }
33446
33447 /* OpenMP 4.0:
33448 num_teams ( expression ) */
33449
33450 static tree
33451 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33452 location_t location)
33453 {
33454 tree t, c;
33455
33456 matching_parens parens;
33457 if (!parens.require_open (parser))
33458 return list;
33459
33460 t = cp_parser_assignment_expression (parser);
33461
33462 if (t == error_mark_node
33463 || !parens.require_close (parser))
33464 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33465 /*or_comma=*/false,
33466 /*consume_paren=*/true);
33467
33468 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33469 "num_teams", location);
33470
33471 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33472 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33473 OMP_CLAUSE_CHAIN (c) = list;
33474
33475 return c;
33476 }
33477
33478 /* OpenMP 4.0:
33479 thread_limit ( expression ) */
33480
33481 static tree
33482 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33483 location_t location)
33484 {
33485 tree t, c;
33486
33487 matching_parens parens;
33488 if (!parens.require_open (parser))
33489 return list;
33490
33491 t = cp_parser_assignment_expression (parser);
33492
33493 if (t == error_mark_node
33494 || !parens.require_close (parser))
33495 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33496 /*or_comma=*/false,
33497 /*consume_paren=*/true);
33498
33499 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33500 "thread_limit", location);
33501
33502 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33503 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33504 OMP_CLAUSE_CHAIN (c) = list;
33505
33506 return c;
33507 }
33508
33509 /* OpenMP 4.0:
33510 aligned ( variable-list )
33511 aligned ( variable-list : constant-expression ) */
33512
33513 static tree
33514 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33515 {
33516 tree nlist, c, alignment = NULL_TREE;
33517 bool colon;
33518
33519 matching_parens parens;
33520 if (!parens.require_open (parser))
33521 return list;
33522
33523 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33524 &colon);
33525
33526 if (colon)
33527 {
33528 alignment = cp_parser_constant_expression (parser);
33529
33530 if (!parens.require_close (parser))
33531 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33532 /*or_comma=*/false,
33533 /*consume_paren=*/true);
33534
33535 if (alignment == error_mark_node)
33536 alignment = NULL_TREE;
33537 }
33538
33539 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33540 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33541
33542 return nlist;
33543 }
33544
33545 /* OpenMP 2.5:
33546 lastprivate ( variable-list )
33547
33548 OpenMP 5.0:
33549 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33550
33551 static tree
33552 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
33553 {
33554 bool conditional = false;
33555
33556 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33557 return list;
33558
33559 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33560 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
33561 {
33562 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33563 const char *p = IDENTIFIER_POINTER (id);
33564
33565 if (strcmp ("conditional", p) == 0)
33566 {
33567 conditional = true;
33568 cp_lexer_consume_token (parser->lexer);
33569 cp_lexer_consume_token (parser->lexer);
33570 }
33571 }
33572
33573 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
33574 list, NULL);
33575
33576 if (conditional)
33577 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33578 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
33579 return nlist;
33580 }
33581
33582 /* OpenMP 4.0:
33583 linear ( variable-list )
33584 linear ( variable-list : expression )
33585
33586 OpenMP 4.5:
33587 linear ( modifier ( variable-list ) )
33588 linear ( modifier ( variable-list ) : expression ) */
33589
33590 static tree
33591 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33592 bool declare_simd)
33593 {
33594 tree nlist, c, step = integer_one_node;
33595 bool colon;
33596 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33597
33598 matching_parens parens;
33599 if (!parens.require_open (parser))
33600 return list;
33601
33602 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33603 {
33604 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33605 const char *p = IDENTIFIER_POINTER (id);
33606
33607 if (strcmp ("ref", p) == 0)
33608 kind = OMP_CLAUSE_LINEAR_REF;
33609 else if (strcmp ("val", p) == 0)
33610 kind = OMP_CLAUSE_LINEAR_VAL;
33611 else if (strcmp ("uval", p) == 0)
33612 kind = OMP_CLAUSE_LINEAR_UVAL;
33613 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33614 cp_lexer_consume_token (parser->lexer);
33615 else
33616 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33617 }
33618
33619 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33620 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33621 &colon);
33622 else
33623 {
33624 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33625 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33626 if (colon)
33627 cp_parser_require (parser, CPP_COLON, RT_COLON);
33628 else if (!parens.require_close (parser))
33629 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33630 /*or_comma=*/false,
33631 /*consume_paren=*/true);
33632 }
33633
33634 if (colon)
33635 {
33636 step = NULL_TREE;
33637 if (declare_simd
33638 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33639 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33640 {
33641 cp_token *token = cp_lexer_peek_token (parser->lexer);
33642 cp_parser_parse_tentatively (parser);
33643 step = cp_parser_id_expression (parser, /*template_p=*/false,
33644 /*check_dependency_p=*/true,
33645 /*template_p=*/NULL,
33646 /*declarator_p=*/false,
33647 /*optional_p=*/false);
33648 if (step != error_mark_node)
33649 step = cp_parser_lookup_name_simple (parser, step, token->location);
33650 if (step == error_mark_node)
33651 {
33652 step = NULL_TREE;
33653 cp_parser_abort_tentative_parse (parser);
33654 }
33655 else if (!cp_parser_parse_definitely (parser))
33656 step = NULL_TREE;
33657 }
33658 if (!step)
33659 step = cp_parser_assignment_expression (parser);
33660
33661 if (!parens.require_close (parser))
33662 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33663 /*or_comma=*/false,
33664 /*consume_paren=*/true);
33665
33666 if (step == error_mark_node)
33667 return list;
33668 }
33669
33670 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33671 {
33672 OMP_CLAUSE_LINEAR_STEP (c) = step;
33673 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33674 }
33675
33676 return nlist;
33677 }
33678
33679 /* OpenMP 4.0:
33680 safelen ( constant-expression ) */
33681
33682 static tree
33683 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33684 location_t location)
33685 {
33686 tree t, c;
33687
33688 matching_parens parens;
33689 if (!parens.require_open (parser))
33690 return list;
33691
33692 t = cp_parser_constant_expression (parser);
33693
33694 if (t == error_mark_node
33695 || !parens.require_close (parser))
33696 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33697 /*or_comma=*/false,
33698 /*consume_paren=*/true);
33699
33700 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33701
33702 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33703 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33704 OMP_CLAUSE_CHAIN (c) = list;
33705
33706 return c;
33707 }
33708
33709 /* OpenMP 4.0:
33710 simdlen ( constant-expression ) */
33711
33712 static tree
33713 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33714 location_t location)
33715 {
33716 tree t, c;
33717
33718 matching_parens parens;
33719 if (!parens.require_open (parser))
33720 return list;
33721
33722 t = cp_parser_constant_expression (parser);
33723
33724 if (t == error_mark_node
33725 || !parens.require_close (parser))
33726 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33727 /*or_comma=*/false,
33728 /*consume_paren=*/true);
33729
33730 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33731
33732 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33733 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33734 OMP_CLAUSE_CHAIN (c) = list;
33735
33736 return c;
33737 }
33738
33739 /* OpenMP 4.5:
33740 vec:
33741 identifier [+/- integer]
33742 vec , identifier [+/- integer]
33743 */
33744
33745 static tree
33746 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33747 tree list)
33748 {
33749 tree vec = NULL;
33750
33751 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33752 {
33753 cp_parser_error (parser, "expected identifier");
33754 return list;
33755 }
33756
33757 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33758 {
33759 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33760 tree t, identifier = cp_parser_identifier (parser);
33761 tree addend = NULL;
33762
33763 if (identifier == error_mark_node)
33764 t = error_mark_node;
33765 else
33766 {
33767 t = cp_parser_lookup_name_simple
33768 (parser, identifier,
33769 cp_lexer_peek_token (parser->lexer)->location);
33770 if (t == error_mark_node)
33771 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33772 id_loc);
33773 }
33774
33775 bool neg = false;
33776 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33777 neg = true;
33778 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33779 {
33780 addend = integer_zero_node;
33781 goto add_to_vector;
33782 }
33783 cp_lexer_consume_token (parser->lexer);
33784
33785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33786 {
33787 cp_parser_error (parser, "expected integer");
33788 return list;
33789 }
33790
33791 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33792 if (TREE_CODE (addend) != INTEGER_CST)
33793 {
33794 cp_parser_error (parser, "expected integer");
33795 return list;
33796 }
33797 cp_lexer_consume_token (parser->lexer);
33798
33799 add_to_vector:
33800 if (t != error_mark_node)
33801 {
33802 vec = tree_cons (addend, t, vec);
33803 if (neg)
33804 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33805 }
33806
33807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33808 break;
33809
33810 cp_lexer_consume_token (parser->lexer);
33811 }
33812
33813 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33814 {
33815 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33816 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33817 OMP_CLAUSE_DECL (u) = nreverse (vec);
33818 OMP_CLAUSE_CHAIN (u) = list;
33819 return u;
33820 }
33821 return list;
33822 }
33823
33824 /* OpenMP 5.0:
33825 iterators ( iterators-definition )
33826
33827 iterators-definition:
33828 iterator-specifier
33829 iterator-specifier , iterators-definition
33830
33831 iterator-specifier:
33832 identifier = range-specification
33833 iterator-type identifier = range-specification
33834
33835 range-specification:
33836 begin : end
33837 begin : end : step */
33838
33839 static tree
33840 cp_parser_omp_iterators (cp_parser *parser)
33841 {
33842 tree ret = NULL_TREE, *last = &ret;
33843 cp_lexer_consume_token (parser->lexer);
33844
33845 matching_parens parens;
33846 if (!parens.require_open (parser))
33847 return error_mark_node;
33848
33849 bool saved_colon_corrects_to_scope_p
33850 = parser->colon_corrects_to_scope_p;
33851 bool saved_colon_doesnt_start_class_def_p
33852 = parser->colon_doesnt_start_class_def_p;
33853
33854 do
33855 {
33856 tree iter_type;
33857 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33858 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
33859 iter_type = integer_type_node;
33860 else
33861 {
33862 const char *saved_message
33863 = parser->type_definition_forbidden_message;
33864 parser->type_definition_forbidden_message
33865 = G_("types may not be defined in iterator type");
33866
33867 iter_type = cp_parser_type_id (parser);
33868
33869 parser->type_definition_forbidden_message = saved_message;
33870 }
33871
33872 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33874 {
33875 cp_parser_error (parser, "expected identifier");
33876 break;
33877 }
33878
33879 tree id = cp_parser_identifier (parser);
33880 if (id == error_mark_node)
33881 break;
33882
33883 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33884 break;
33885
33886 parser->colon_corrects_to_scope_p = false;
33887 parser->colon_doesnt_start_class_def_p = true;
33888 tree begin = cp_parser_assignment_expression (parser);
33889
33890 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33891 break;
33892
33893 tree end = cp_parser_assignment_expression (parser);
33894
33895 tree step = integer_one_node;
33896 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33897 {
33898 cp_lexer_consume_token (parser->lexer);
33899 step = cp_parser_assignment_expression (parser);
33900 }
33901
33902 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
33903 DECL_ARTIFICIAL (iter_var) = 1;
33904 DECL_CONTEXT (iter_var) = current_function_decl;
33905 pushdecl (iter_var);
33906
33907 *last = make_tree_vec (6);
33908 TREE_VEC_ELT (*last, 0) = iter_var;
33909 TREE_VEC_ELT (*last, 1) = begin;
33910 TREE_VEC_ELT (*last, 2) = end;
33911 TREE_VEC_ELT (*last, 3) = step;
33912 last = &TREE_CHAIN (*last);
33913
33914 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33915 {
33916 cp_lexer_consume_token (parser->lexer);
33917 continue;
33918 }
33919 break;
33920 }
33921 while (1);
33922
33923 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33924 parser->colon_doesnt_start_class_def_p
33925 = saved_colon_doesnt_start_class_def_p;
33926
33927 if (!parens.require_close (parser))
33928 cp_parser_skip_to_closing_parenthesis (parser,
33929 /*recovering=*/true,
33930 /*or_comma=*/false,
33931 /*consume_paren=*/true);
33932
33933 return ret ? ret : error_mark_node;
33934 }
33935
33936 /* OpenMP 4.0:
33937 depend ( depend-kind : variable-list )
33938
33939 depend-kind:
33940 in | out | inout
33941
33942 OpenMP 4.5:
33943 depend ( source )
33944
33945 depend ( sink : vec )
33946
33947 OpenMP 5.0:
33948 depend ( depend-modifier , depend-kind: variable-list )
33949
33950 depend-kind:
33951 in | out | inout | mutexinoutset | depobj
33952
33953 depend-modifier:
33954 iterator ( iterators-definition ) */
33955
33956 static tree
33957 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33958 {
33959 tree nlist, c, iterators = NULL_TREE;
33960 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
33961
33962 matching_parens parens;
33963 if (!parens.require_open (parser))
33964 return list;
33965
33966 do
33967 {
33968 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33969 goto invalid_kind;
33970
33971 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33972 const char *p = IDENTIFIER_POINTER (id);
33973
33974 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
33975 {
33976 begin_scope (sk_omp, NULL);
33977 iterators = cp_parser_omp_iterators (parser);
33978 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
33979 continue;
33980 }
33981 if (strcmp ("in", p) == 0)
33982 kind = OMP_CLAUSE_DEPEND_IN;
33983 else if (strcmp ("inout", p) == 0)
33984 kind = OMP_CLAUSE_DEPEND_INOUT;
33985 else if (strcmp ("mutexinoutset", p) == 0)
33986 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
33987 else if (strcmp ("out", p) == 0)
33988 kind = OMP_CLAUSE_DEPEND_OUT;
33989 else if (strcmp ("depobj", p) == 0)
33990 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
33991 else if (strcmp ("sink", p) == 0)
33992 kind = OMP_CLAUSE_DEPEND_SINK;
33993 else if (strcmp ("source", p) == 0)
33994 kind = OMP_CLAUSE_DEPEND_SOURCE;
33995 else
33996 goto invalid_kind;
33997 break;
33998 }
33999 while (1);
34000
34001 cp_lexer_consume_token (parser->lexer);
34002
34003 if (iterators
34004 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34005 {
34006 poplevel (0, 1, 0);
34007 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34008 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34009 iterators = NULL_TREE;
34010 }
34011
34012 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34013 {
34014 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34015 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34016 OMP_CLAUSE_DECL (c) = NULL_TREE;
34017 OMP_CLAUSE_CHAIN (c) = list;
34018 if (!parens.require_close (parser))
34019 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34020 /*or_comma=*/false,
34021 /*consume_paren=*/true);
34022 return c;
34023 }
34024
34025 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34026 goto resync_fail;
34027
34028 if (kind == OMP_CLAUSE_DEPEND_SINK)
34029 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34030 else
34031 {
34032 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34033 list, NULL);
34034
34035 if (iterators)
34036 {
34037 tree block = poplevel (1, 1, 0);
34038 if (iterators == error_mark_node)
34039 iterators = NULL_TREE;
34040 else
34041 TREE_VEC_ELT (iterators, 5) = block;
34042 }
34043
34044 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34045 {
34046 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34047 if (iterators)
34048 OMP_CLAUSE_DECL (c)
34049 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34050 }
34051 }
34052 return nlist;
34053
34054 invalid_kind:
34055 cp_parser_error (parser, "invalid depend kind");
34056 resync_fail:
34057 if (iterators)
34058 poplevel (0, 1, 0);
34059 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34060 /*or_comma=*/false,
34061 /*consume_paren=*/true);
34062 return list;
34063 }
34064
34065 /* OpenMP 4.0:
34066 map ( map-kind : variable-list )
34067 map ( variable-list )
34068
34069 map-kind:
34070 alloc | to | from | tofrom
34071
34072 OpenMP 4.5:
34073 map-kind:
34074 alloc | to | from | tofrom | release | delete
34075
34076 map ( always [,] map-kind: variable-list ) */
34077
34078 static tree
34079 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34080 {
34081 tree nlist, c;
34082 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34083 bool always = false;
34084
34085 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34086 return list;
34087
34088 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34089 {
34090 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34091 const char *p = IDENTIFIER_POINTER (id);
34092
34093 if (strcmp ("always", p) == 0)
34094 {
34095 int nth = 2;
34096 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34097 nth++;
34098 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34099 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34100 == RID_DELETE))
34101 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34102 == CPP_COLON))
34103 {
34104 always = true;
34105 cp_lexer_consume_token (parser->lexer);
34106 if (nth == 3)
34107 cp_lexer_consume_token (parser->lexer);
34108 }
34109 }
34110 }
34111
34112 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34113 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34114 {
34115 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34116 const char *p = IDENTIFIER_POINTER (id);
34117
34118 if (strcmp ("alloc", p) == 0)
34119 kind = GOMP_MAP_ALLOC;
34120 else if (strcmp ("to", p) == 0)
34121 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34122 else if (strcmp ("from", p) == 0)
34123 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34124 else if (strcmp ("tofrom", p) == 0)
34125 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34126 else if (strcmp ("release", p) == 0)
34127 kind = GOMP_MAP_RELEASE;
34128 else
34129 {
34130 cp_parser_error (parser, "invalid map kind");
34131 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34132 /*or_comma=*/false,
34133 /*consume_paren=*/true);
34134 return list;
34135 }
34136 cp_lexer_consume_token (parser->lexer);
34137 cp_lexer_consume_token (parser->lexer);
34138 }
34139 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34140 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34141 {
34142 kind = GOMP_MAP_DELETE;
34143 cp_lexer_consume_token (parser->lexer);
34144 cp_lexer_consume_token (parser->lexer);
34145 }
34146
34147 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34148 NULL);
34149
34150 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34151 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34152
34153 return nlist;
34154 }
34155
34156 /* OpenMP 4.0:
34157 device ( expression ) */
34158
34159 static tree
34160 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34161 location_t location)
34162 {
34163 tree t, c;
34164
34165 matching_parens parens;
34166 if (!parens.require_open (parser))
34167 return list;
34168
34169 t = cp_parser_assignment_expression (parser);
34170
34171 if (t == error_mark_node
34172 || !parens.require_close (parser))
34173 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34174 /*or_comma=*/false,
34175 /*consume_paren=*/true);
34176
34177 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34178 "device", location);
34179
34180 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34181 OMP_CLAUSE_DEVICE_ID (c) = t;
34182 OMP_CLAUSE_CHAIN (c) = list;
34183
34184 return c;
34185 }
34186
34187 /* OpenMP 4.0:
34188 dist_schedule ( static )
34189 dist_schedule ( static , expression ) */
34190
34191 static tree
34192 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34193 location_t location)
34194 {
34195 tree c, t;
34196
34197 matching_parens parens;
34198 if (!parens.require_open (parser))
34199 return list;
34200
34201 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34202
34203 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34204 goto invalid_kind;
34205 cp_lexer_consume_token (parser->lexer);
34206
34207 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34208 {
34209 cp_lexer_consume_token (parser->lexer);
34210
34211 t = cp_parser_assignment_expression (parser);
34212
34213 if (t == error_mark_node)
34214 goto resync_fail;
34215 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34216
34217 if (!parens.require_close (parser))
34218 goto resync_fail;
34219 }
34220 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34221 goto resync_fail;
34222
34223 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34224 location);
34225 OMP_CLAUSE_CHAIN (c) = list;
34226 return c;
34227
34228 invalid_kind:
34229 cp_parser_error (parser, "invalid dist_schedule kind");
34230 resync_fail:
34231 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34232 /*or_comma=*/false,
34233 /*consume_paren=*/true);
34234 return list;
34235 }
34236
34237 /* OpenMP 4.0:
34238 proc_bind ( proc-bind-kind )
34239
34240 proc-bind-kind:
34241 master | close | spread */
34242
34243 static tree
34244 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34245 location_t location)
34246 {
34247 tree c;
34248 enum omp_clause_proc_bind_kind kind;
34249
34250 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34251 return list;
34252
34253 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34254 {
34255 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34256 const char *p = IDENTIFIER_POINTER (id);
34257
34258 if (strcmp ("master", p) == 0)
34259 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34260 else if (strcmp ("close", p) == 0)
34261 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34262 else if (strcmp ("spread", p) == 0)
34263 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34264 else
34265 goto invalid_kind;
34266 }
34267 else
34268 goto invalid_kind;
34269
34270 cp_lexer_consume_token (parser->lexer);
34271 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34272 goto resync_fail;
34273
34274 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34275 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34276 location);
34277 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34278 OMP_CLAUSE_CHAIN (c) = list;
34279 return c;
34280
34281 invalid_kind:
34282 cp_parser_error (parser, "invalid depend kind");
34283 resync_fail:
34284 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34285 /*or_comma=*/false,
34286 /*consume_paren=*/true);
34287 return list;
34288 }
34289
34290 /* OpenACC:
34291 async [( int-expr )] */
34292
34293 static tree
34294 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34295 {
34296 tree c, t;
34297 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34298
34299 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34300
34301 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34302 {
34303 matching_parens parens;
34304 parens.consume_open (parser);
34305
34306 t = cp_parser_expression (parser);
34307 if (t == error_mark_node
34308 || !parens.require_close (parser))
34309 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34310 /*or_comma=*/false,
34311 /*consume_paren=*/true);
34312 }
34313
34314 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34315
34316 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34317 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34318 OMP_CLAUSE_CHAIN (c) = list;
34319 list = c;
34320
34321 return list;
34322 }
34323
34324 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34325 is a bitmask in MASK. Return the list of clauses found. */
34326
34327 static tree
34328 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34329 const char *where, cp_token *pragma_tok,
34330 bool finish_p = true)
34331 {
34332 tree clauses = NULL;
34333 bool first = true;
34334
34335 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34336 {
34337 location_t here;
34338 pragma_omp_clause c_kind;
34339 omp_clause_code code;
34340 const char *c_name;
34341 tree prev = clauses;
34342
34343 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34344 cp_lexer_consume_token (parser->lexer);
34345
34346 here = cp_lexer_peek_token (parser->lexer)->location;
34347 c_kind = cp_parser_omp_clause_name (parser);
34348
34349 switch (c_kind)
34350 {
34351 case PRAGMA_OACC_CLAUSE_ASYNC:
34352 clauses = cp_parser_oacc_clause_async (parser, clauses);
34353 c_name = "async";
34354 break;
34355 case PRAGMA_OACC_CLAUSE_AUTO:
34356 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
34357 clauses, here);
34358 c_name = "auto";
34359 break;
34360 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34361 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34362 c_name = "collapse";
34363 break;
34364 case PRAGMA_OACC_CLAUSE_COPY:
34365 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34366 c_name = "copy";
34367 break;
34368 case PRAGMA_OACC_CLAUSE_COPYIN:
34369 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34370 c_name = "copyin";
34371 break;
34372 case PRAGMA_OACC_CLAUSE_COPYOUT:
34373 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34374 c_name = "copyout";
34375 break;
34376 case PRAGMA_OACC_CLAUSE_CREATE:
34377 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34378 c_name = "create";
34379 break;
34380 case PRAGMA_OACC_CLAUSE_DELETE:
34381 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34382 c_name = "delete";
34383 break;
34384 case PRAGMA_OMP_CLAUSE_DEFAULT:
34385 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34386 c_name = "default";
34387 break;
34388 case PRAGMA_OACC_CLAUSE_DEVICE:
34389 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34390 c_name = "device";
34391 break;
34392 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34393 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34394 c_name = "deviceptr";
34395 break;
34396 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34397 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34398 c_name = "device_resident";
34399 break;
34400 case PRAGMA_OACC_CLAUSE_FINALIZE:
34401 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
34402 clauses, here);
34403 c_name = "finalize";
34404 break;
34405 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34406 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34407 clauses);
34408 c_name = "firstprivate";
34409 break;
34410 case PRAGMA_OACC_CLAUSE_GANG:
34411 c_name = "gang";
34412 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
34413 c_name, clauses);
34414 break;
34415 case PRAGMA_OACC_CLAUSE_HOST:
34416 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34417 c_name = "host";
34418 break;
34419 case PRAGMA_OACC_CLAUSE_IF:
34420 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34421 c_name = "if";
34422 break;
34423 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34424 clauses = cp_parser_oacc_simple_clause (parser,
34425 OMP_CLAUSE_IF_PRESENT,
34426 clauses, here);
34427 c_name = "if_present";
34428 break;
34429 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34430 clauses = cp_parser_oacc_simple_clause (parser,
34431 OMP_CLAUSE_INDEPENDENT,
34432 clauses, here);
34433 c_name = "independent";
34434 break;
34435 case PRAGMA_OACC_CLAUSE_LINK:
34436 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34437 c_name = "link";
34438 break;
34439 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34440 code = OMP_CLAUSE_NUM_GANGS;
34441 c_name = "num_gangs";
34442 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34443 clauses);
34444 break;
34445 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34446 c_name = "num_workers";
34447 code = OMP_CLAUSE_NUM_WORKERS;
34448 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34449 clauses);
34450 break;
34451 case PRAGMA_OACC_CLAUSE_PRESENT:
34452 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34453 c_name = "present";
34454 break;
34455 case PRAGMA_OACC_CLAUSE_PRIVATE:
34456 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34457 clauses);
34458 c_name = "private";
34459 break;
34460 case PRAGMA_OACC_CLAUSE_REDUCTION:
34461 clauses
34462 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34463 false, clauses);
34464 c_name = "reduction";
34465 break;
34466 case PRAGMA_OACC_CLAUSE_SEQ:
34467 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
34468 clauses, here);
34469 c_name = "seq";
34470 break;
34471 case PRAGMA_OACC_CLAUSE_TILE:
34472 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
34473 c_name = "tile";
34474 break;
34475 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
34476 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34477 clauses);
34478 c_name = "use_device";
34479 break;
34480 case PRAGMA_OACC_CLAUSE_VECTOR:
34481 c_name = "vector";
34482 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
34483 c_name, clauses);
34484 break;
34485 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
34486 c_name = "vector_length";
34487 code = OMP_CLAUSE_VECTOR_LENGTH;
34488 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34489 clauses);
34490 break;
34491 case PRAGMA_OACC_CLAUSE_WAIT:
34492 clauses = cp_parser_oacc_clause_wait (parser, clauses);
34493 c_name = "wait";
34494 break;
34495 case PRAGMA_OACC_CLAUSE_WORKER:
34496 c_name = "worker";
34497 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
34498 c_name, clauses);
34499 break;
34500 default:
34501 cp_parser_error (parser, "expected %<#pragma acc%> clause");
34502 goto saw_error;
34503 }
34504
34505 first = false;
34506
34507 if (((mask >> c_kind) & 1) == 0)
34508 {
34509 /* Remove the invalid clause(s) from the list to avoid
34510 confusing the rest of the compiler. */
34511 clauses = prev;
34512 error_at (here, "%qs is not valid for %qs", c_name, where);
34513 }
34514 }
34515
34516 saw_error:
34517 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34518
34519 if (finish_p)
34520 return finish_omp_clauses (clauses, C_ORT_ACC);
34521
34522 return clauses;
34523 }
34524
34525 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34526 is a bitmask in MASK. Return the list of clauses found; the result
34527 of clause default goes in *pdefault. */
34528
34529 static tree
34530 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
34531 const char *where, cp_token *pragma_tok,
34532 bool finish_p = true)
34533 {
34534 tree clauses = NULL;
34535 bool first = true;
34536 cp_token *token = NULL;
34537
34538 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34539 {
34540 pragma_omp_clause c_kind;
34541 const char *c_name;
34542 tree prev = clauses;
34543
34544 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34545 cp_lexer_consume_token (parser->lexer);
34546
34547 token = cp_lexer_peek_token (parser->lexer);
34548 c_kind = cp_parser_omp_clause_name (parser);
34549
34550 switch (c_kind)
34551 {
34552 case PRAGMA_OMP_CLAUSE_COLLAPSE:
34553 clauses = cp_parser_omp_clause_collapse (parser, clauses,
34554 token->location);
34555 c_name = "collapse";
34556 break;
34557 case PRAGMA_OMP_CLAUSE_COPYIN:
34558 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
34559 c_name = "copyin";
34560 break;
34561 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
34562 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
34563 clauses);
34564 c_name = "copyprivate";
34565 break;
34566 case PRAGMA_OMP_CLAUSE_DEFAULT:
34567 clauses = cp_parser_omp_clause_default (parser, clauses,
34568 token->location, false);
34569 c_name = "default";
34570 break;
34571 case PRAGMA_OMP_CLAUSE_FINAL:
34572 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
34573 c_name = "final";
34574 break;
34575 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
34576 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34577 clauses);
34578 c_name = "firstprivate";
34579 break;
34580 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34581 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34582 token->location);
34583 c_name = "grainsize";
34584 break;
34585 case PRAGMA_OMP_CLAUSE_HINT:
34586 clauses = cp_parser_omp_clause_hint (parser, clauses,
34587 token->location);
34588 c_name = "hint";
34589 break;
34590 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34591 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34592 token->location);
34593 c_name = "defaultmap";
34594 break;
34595 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34596 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34597 clauses);
34598 c_name = "use_device_ptr";
34599 break;
34600 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34601 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34602 clauses);
34603 c_name = "is_device_ptr";
34604 break;
34605 case PRAGMA_OMP_CLAUSE_IF:
34606 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34607 true);
34608 c_name = "if";
34609 break;
34610 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
34611 clauses
34612 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
34613 true, clauses);
34614 c_name = "in_reduction";
34615 break;
34616 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34617 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
34618 c_name = "lastprivate";
34619 break;
34620 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34621 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34622 token->location);
34623 c_name = "mergeable";
34624 break;
34625 case PRAGMA_OMP_CLAUSE_NOWAIT:
34626 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34627 c_name = "nowait";
34628 break;
34629 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34630 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34631 token->location);
34632 c_name = "num_tasks";
34633 break;
34634 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34635 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34636 token->location);
34637 c_name = "num_threads";
34638 break;
34639 case PRAGMA_OMP_CLAUSE_ORDERED:
34640 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34641 token->location);
34642 c_name = "ordered";
34643 break;
34644 case PRAGMA_OMP_CLAUSE_PRIORITY:
34645 clauses = cp_parser_omp_clause_priority (parser, clauses,
34646 token->location);
34647 c_name = "priority";
34648 break;
34649 case PRAGMA_OMP_CLAUSE_PRIVATE:
34650 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34651 clauses);
34652 c_name = "private";
34653 break;
34654 case PRAGMA_OMP_CLAUSE_REDUCTION:
34655 clauses
34656 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34657 true, clauses);
34658 c_name = "reduction";
34659 break;
34660 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34661 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34662 token->location);
34663 c_name = "schedule";
34664 break;
34665 case PRAGMA_OMP_CLAUSE_SHARED:
34666 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34667 clauses);
34668 c_name = "shared";
34669 break;
34670 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
34671 clauses
34672 = cp_parser_omp_clause_reduction (parser,
34673 OMP_CLAUSE_TASK_REDUCTION,
34674 true, clauses);
34675 c_name = "task_reduction";
34676 break;
34677 case PRAGMA_OMP_CLAUSE_UNTIED:
34678 clauses = cp_parser_omp_clause_untied (parser, clauses,
34679 token->location);
34680 c_name = "untied";
34681 break;
34682 case PRAGMA_OMP_CLAUSE_INBRANCH:
34683 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34684 clauses, token->location);
34685 c_name = "inbranch";
34686 break;
34687 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
34688 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
34689 clauses);
34690 c_name = "nontemporal";
34691 break;
34692 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34693 clauses = cp_parser_omp_clause_branch (parser,
34694 OMP_CLAUSE_NOTINBRANCH,
34695 clauses, token->location);
34696 c_name = "notinbranch";
34697 break;
34698 case PRAGMA_OMP_CLAUSE_PARALLEL:
34699 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34700 clauses, token->location);
34701 c_name = "parallel";
34702 if (!first)
34703 {
34704 clause_not_first:
34705 error_at (token->location, "%qs must be the first clause of %qs",
34706 c_name, where);
34707 clauses = prev;
34708 }
34709 break;
34710 case PRAGMA_OMP_CLAUSE_FOR:
34711 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34712 clauses, token->location);
34713 c_name = "for";
34714 if (!first)
34715 goto clause_not_first;
34716 break;
34717 case PRAGMA_OMP_CLAUSE_SECTIONS:
34718 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34719 clauses, token->location);
34720 c_name = "sections";
34721 if (!first)
34722 goto clause_not_first;
34723 break;
34724 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34725 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34726 clauses, token->location);
34727 c_name = "taskgroup";
34728 if (!first)
34729 goto clause_not_first;
34730 break;
34731 case PRAGMA_OMP_CLAUSE_LINK:
34732 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34733 c_name = "to";
34734 break;
34735 case PRAGMA_OMP_CLAUSE_TO:
34736 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34737 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34738 clauses);
34739 else
34740 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34741 c_name = "to";
34742 break;
34743 case PRAGMA_OMP_CLAUSE_FROM:
34744 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34745 c_name = "from";
34746 break;
34747 case PRAGMA_OMP_CLAUSE_UNIFORM:
34748 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34749 clauses);
34750 c_name = "uniform";
34751 break;
34752 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34753 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34754 token->location);
34755 c_name = "num_teams";
34756 break;
34757 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34758 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34759 token->location);
34760 c_name = "thread_limit";
34761 break;
34762 case PRAGMA_OMP_CLAUSE_ALIGNED:
34763 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34764 c_name = "aligned";
34765 break;
34766 case PRAGMA_OMP_CLAUSE_LINEAR:
34767 {
34768 bool declare_simd = false;
34769 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34770 declare_simd = true;
34771 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34772 }
34773 c_name = "linear";
34774 break;
34775 case PRAGMA_OMP_CLAUSE_DEPEND:
34776 clauses = cp_parser_omp_clause_depend (parser, clauses,
34777 token->location);
34778 c_name = "depend";
34779 break;
34780 case PRAGMA_OMP_CLAUSE_MAP:
34781 clauses = cp_parser_omp_clause_map (parser, clauses);
34782 c_name = "map";
34783 break;
34784 case PRAGMA_OMP_CLAUSE_DEVICE:
34785 clauses = cp_parser_omp_clause_device (parser, clauses,
34786 token->location);
34787 c_name = "device";
34788 break;
34789 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34790 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34791 token->location);
34792 c_name = "dist_schedule";
34793 break;
34794 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34795 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34796 token->location);
34797 c_name = "proc_bind";
34798 break;
34799 case PRAGMA_OMP_CLAUSE_SAFELEN:
34800 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34801 token->location);
34802 c_name = "safelen";
34803 break;
34804 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34805 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34806 token->location);
34807 c_name = "simdlen";
34808 break;
34809 case PRAGMA_OMP_CLAUSE_NOGROUP:
34810 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34811 token->location);
34812 c_name = "nogroup";
34813 break;
34814 case PRAGMA_OMP_CLAUSE_THREADS:
34815 clauses
34816 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34817 clauses, token->location);
34818 c_name = "threads";
34819 break;
34820 case PRAGMA_OMP_CLAUSE_SIMD:
34821 clauses
34822 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34823 clauses, token->location);
34824 c_name = "simd";
34825 break;
34826 default:
34827 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34828 goto saw_error;
34829 }
34830
34831 first = false;
34832
34833 if (((mask >> c_kind) & 1) == 0)
34834 {
34835 /* Remove the invalid clause(s) from the list to avoid
34836 confusing the rest of the compiler. */
34837 clauses = prev;
34838 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34839 }
34840 }
34841 saw_error:
34842 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34843 if (finish_p)
34844 {
34845 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34846 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34847 else
34848 return finish_omp_clauses (clauses, C_ORT_OMP);
34849 }
34850 return clauses;
34851 }
34852
34853 /* OpenMP 2.5:
34854 structured-block:
34855 statement
34856
34857 In practice, we're also interested in adding the statement to an
34858 outer node. So it is convenient if we work around the fact that
34859 cp_parser_statement calls add_stmt. */
34860
34861 static unsigned
34862 cp_parser_begin_omp_structured_block (cp_parser *parser)
34863 {
34864 unsigned save = parser->in_statement;
34865
34866 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34867 This preserves the "not within loop or switch" style error messages
34868 for nonsense cases like
34869 void foo() {
34870 #pragma omp single
34871 break;
34872 }
34873 */
34874 if (parser->in_statement)
34875 parser->in_statement = IN_OMP_BLOCK;
34876
34877 return save;
34878 }
34879
34880 static void
34881 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34882 {
34883 parser->in_statement = save;
34884 }
34885
34886 static tree
34887 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34888 {
34889 tree stmt = begin_omp_structured_block ();
34890 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34891
34892 cp_parser_statement (parser, NULL_TREE, false, if_p);
34893
34894 cp_parser_end_omp_structured_block (parser, save);
34895 return finish_omp_structured_block (stmt);
34896 }
34897
34898 /* OpenMP 2.5:
34899 # pragma omp atomic new-line
34900 expression-stmt
34901
34902 expression-stmt:
34903 x binop= expr | x++ | ++x | x-- | --x
34904 binop:
34905 +, *, -, /, &, ^, |, <<, >>
34906
34907 where x is an lvalue expression with scalar type.
34908
34909 OpenMP 3.1:
34910 # pragma omp atomic new-line
34911 update-stmt
34912
34913 # pragma omp atomic read new-line
34914 read-stmt
34915
34916 # pragma omp atomic write new-line
34917 write-stmt
34918
34919 # pragma omp atomic update new-line
34920 update-stmt
34921
34922 # pragma omp atomic capture new-line
34923 capture-stmt
34924
34925 # pragma omp atomic capture new-line
34926 capture-block
34927
34928 read-stmt:
34929 v = x
34930 write-stmt:
34931 x = expr
34932 update-stmt:
34933 expression-stmt | x = x binop expr
34934 capture-stmt:
34935 v = expression-stmt
34936 capture-block:
34937 { v = x; update-stmt; } | { update-stmt; v = x; }
34938
34939 OpenMP 4.0:
34940 update-stmt:
34941 expression-stmt | x = x binop expr | x = expr binop x
34942 capture-stmt:
34943 v = update-stmt
34944 capture-block:
34945 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34946
34947 where x and v are lvalue expressions with scalar type. */
34948
34949 static void
34950 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34951 {
34952 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34953 tree rhs1 = NULL_TREE, orig_lhs;
34954 location_t loc = pragma_tok->location;
34955 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
34956 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
34957 bool structured_block = false;
34958 bool first = true;
34959 tree clauses = NULL_TREE;
34960
34961 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34962 {
34963 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34964 cp_lexer_consume_token (parser->lexer);
34965
34966 first = false;
34967
34968 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34969 {
34970 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34971 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
34972 const char *p = IDENTIFIER_POINTER (id);
34973 enum tree_code new_code = ERROR_MARK;
34974 enum omp_memory_order new_memory_order
34975 = OMP_MEMORY_ORDER_UNSPECIFIED;
34976
34977 if (!strcmp (p, "read"))
34978 new_code = OMP_ATOMIC_READ;
34979 else if (!strcmp (p, "write"))
34980 new_code = NOP_EXPR;
34981 else if (!strcmp (p, "update"))
34982 new_code = OMP_ATOMIC;
34983 else if (!strcmp (p, "capture"))
34984 new_code = OMP_ATOMIC_CAPTURE_NEW;
34985 else if (!strcmp (p, "seq_cst"))
34986 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
34987 else if (!strcmp (p, "acq_rel"))
34988 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
34989 else if (!strcmp (p, "release"))
34990 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
34991 else if (!strcmp (p, "acquire"))
34992 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
34993 else if (!strcmp (p, "relaxed"))
34994 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
34995 else if (!strcmp (p, "hint"))
34996 {
34997 cp_lexer_consume_token (parser->lexer);
34998 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
34999 continue;
35000 }
35001 else
35002 {
35003 p = NULL;
35004 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35005 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35006 "%<release%>, %<relaxed%> or %<hint%> clause");
35007 }
35008 if (p)
35009 {
35010 if (new_code != ERROR_MARK)
35011 {
35012 if (code != ERROR_MARK)
35013 error_at (cloc, "too many atomic clauses");
35014 else
35015 code = new_code;
35016 }
35017 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35018 {
35019 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35020 error_at (cloc, "too many memory order clauses");
35021 else
35022 memory_order = new_memory_order;
35023 }
35024 cp_lexer_consume_token (parser->lexer);
35025 continue;
35026 }
35027 }
35028 break;
35029 }
35030 cp_parser_require_pragma_eol (parser, pragma_tok);
35031
35032 if (code == ERROR_MARK)
35033 code = OMP_ATOMIC;
35034 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35035 {
35036 omp_requires_mask
35037 = (enum omp_requires) (omp_requires_mask
35038 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35039 switch ((enum omp_memory_order)
35040 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35041 {
35042 case OMP_MEMORY_ORDER_UNSPECIFIED:
35043 case OMP_MEMORY_ORDER_RELAXED:
35044 memory_order = OMP_MEMORY_ORDER_RELAXED;
35045 break;
35046 case OMP_MEMORY_ORDER_SEQ_CST:
35047 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35048 break;
35049 case OMP_MEMORY_ORDER_ACQ_REL:
35050 switch (code)
35051 {
35052 case OMP_ATOMIC_READ:
35053 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35054 break;
35055 case NOP_EXPR: /* atomic write */
35056 case OMP_ATOMIC:
35057 memory_order = OMP_MEMORY_ORDER_RELEASE;
35058 break;
35059 default:
35060 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35061 break;
35062 }
35063 break;
35064 default:
35065 gcc_unreachable ();
35066 }
35067 }
35068 else
35069 switch (code)
35070 {
35071 case OMP_ATOMIC_READ:
35072 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35073 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35074 {
35075 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35076 "%<acq_rel%> or %<release%> clauses");
35077 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35078 }
35079 break;
35080 case NOP_EXPR: /* atomic write */
35081 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35082 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35083 {
35084 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35085 "%<acq_rel%> or %<acquire%> clauses");
35086 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35087 }
35088 break;
35089 case OMP_ATOMIC:
35090 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35091 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35092 {
35093 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35094 "%<acq_rel%> or %<acquire%> clauses");
35095 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35096 }
35097 break;
35098 default:
35099 break;
35100 }
35101
35102 switch (code)
35103 {
35104 case OMP_ATOMIC_READ:
35105 case NOP_EXPR: /* atomic write */
35106 v = cp_parser_unary_expression (parser);
35107 if (v == error_mark_node)
35108 goto saw_error;
35109 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35110 goto saw_error;
35111 if (code == NOP_EXPR)
35112 lhs = cp_parser_expression (parser);
35113 else
35114 lhs = cp_parser_unary_expression (parser);
35115 if (lhs == error_mark_node)
35116 goto saw_error;
35117 if (code == NOP_EXPR)
35118 {
35119 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35120 opcode. */
35121 code = OMP_ATOMIC;
35122 rhs = lhs;
35123 lhs = v;
35124 v = NULL_TREE;
35125 }
35126 goto done;
35127 case OMP_ATOMIC_CAPTURE_NEW:
35128 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35129 {
35130 cp_lexer_consume_token (parser->lexer);
35131 structured_block = true;
35132 }
35133 else
35134 {
35135 v = cp_parser_unary_expression (parser);
35136 if (v == error_mark_node)
35137 goto saw_error;
35138 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35139 goto saw_error;
35140 }
35141 default:
35142 break;
35143 }
35144
35145 restart:
35146 lhs = cp_parser_unary_expression (parser);
35147 orig_lhs = lhs;
35148 switch (TREE_CODE (lhs))
35149 {
35150 case ERROR_MARK:
35151 goto saw_error;
35152
35153 case POSTINCREMENT_EXPR:
35154 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35155 code = OMP_ATOMIC_CAPTURE_OLD;
35156 /* FALLTHROUGH */
35157 case PREINCREMENT_EXPR:
35158 lhs = TREE_OPERAND (lhs, 0);
35159 opcode = PLUS_EXPR;
35160 rhs = integer_one_node;
35161 break;
35162
35163 case POSTDECREMENT_EXPR:
35164 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35165 code = OMP_ATOMIC_CAPTURE_OLD;
35166 /* FALLTHROUGH */
35167 case PREDECREMENT_EXPR:
35168 lhs = TREE_OPERAND (lhs, 0);
35169 opcode = MINUS_EXPR;
35170 rhs = integer_one_node;
35171 break;
35172
35173 case COMPOUND_EXPR:
35174 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35175 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35176 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35177 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35178 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35179 (TREE_OPERAND (lhs, 1), 0), 0)))
35180 == BOOLEAN_TYPE)
35181 /* Undo effects of boolean_increment for post {in,de}crement. */
35182 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35183 /* FALLTHRU */
35184 case MODIFY_EXPR:
35185 if (TREE_CODE (lhs) == MODIFY_EXPR
35186 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35187 {
35188 /* Undo effects of boolean_increment. */
35189 if (integer_onep (TREE_OPERAND (lhs, 1)))
35190 {
35191 /* This is pre or post increment. */
35192 rhs = TREE_OPERAND (lhs, 1);
35193 lhs = TREE_OPERAND (lhs, 0);
35194 opcode = NOP_EXPR;
35195 if (code == OMP_ATOMIC_CAPTURE_NEW
35196 && !structured_block
35197 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35198 code = OMP_ATOMIC_CAPTURE_OLD;
35199 break;
35200 }
35201 }
35202 /* FALLTHRU */
35203 default:
35204 switch (cp_lexer_peek_token (parser->lexer)->type)
35205 {
35206 case CPP_MULT_EQ:
35207 opcode = MULT_EXPR;
35208 break;
35209 case CPP_DIV_EQ:
35210 opcode = TRUNC_DIV_EXPR;
35211 break;
35212 case CPP_PLUS_EQ:
35213 opcode = PLUS_EXPR;
35214 break;
35215 case CPP_MINUS_EQ:
35216 opcode = MINUS_EXPR;
35217 break;
35218 case CPP_LSHIFT_EQ:
35219 opcode = LSHIFT_EXPR;
35220 break;
35221 case CPP_RSHIFT_EQ:
35222 opcode = RSHIFT_EXPR;
35223 break;
35224 case CPP_AND_EQ:
35225 opcode = BIT_AND_EXPR;
35226 break;
35227 case CPP_OR_EQ:
35228 opcode = BIT_IOR_EXPR;
35229 break;
35230 case CPP_XOR_EQ:
35231 opcode = BIT_XOR_EXPR;
35232 break;
35233 case CPP_EQ:
35234 enum cp_parser_prec oprec;
35235 cp_token *token;
35236 cp_lexer_consume_token (parser->lexer);
35237 cp_parser_parse_tentatively (parser);
35238 rhs1 = cp_parser_simple_cast_expression (parser);
35239 if (rhs1 == error_mark_node)
35240 {
35241 cp_parser_abort_tentative_parse (parser);
35242 cp_parser_simple_cast_expression (parser);
35243 goto saw_error;
35244 }
35245 token = cp_lexer_peek_token (parser->lexer);
35246 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35247 {
35248 cp_parser_abort_tentative_parse (parser);
35249 cp_parser_parse_tentatively (parser);
35250 rhs = cp_parser_binary_expression (parser, false, true,
35251 PREC_NOT_OPERATOR, NULL);
35252 if (rhs == error_mark_node)
35253 {
35254 cp_parser_abort_tentative_parse (parser);
35255 cp_parser_binary_expression (parser, false, true,
35256 PREC_NOT_OPERATOR, NULL);
35257 goto saw_error;
35258 }
35259 switch (TREE_CODE (rhs))
35260 {
35261 case MULT_EXPR:
35262 case TRUNC_DIV_EXPR:
35263 case RDIV_EXPR:
35264 case PLUS_EXPR:
35265 case MINUS_EXPR:
35266 case LSHIFT_EXPR:
35267 case RSHIFT_EXPR:
35268 case BIT_AND_EXPR:
35269 case BIT_IOR_EXPR:
35270 case BIT_XOR_EXPR:
35271 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35272 {
35273 if (cp_parser_parse_definitely (parser))
35274 {
35275 opcode = TREE_CODE (rhs);
35276 rhs1 = TREE_OPERAND (rhs, 0);
35277 rhs = TREE_OPERAND (rhs, 1);
35278 goto stmt_done;
35279 }
35280 else
35281 goto saw_error;
35282 }
35283 break;
35284 default:
35285 break;
35286 }
35287 cp_parser_abort_tentative_parse (parser);
35288 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35289 {
35290 rhs = cp_parser_expression (parser);
35291 if (rhs == error_mark_node)
35292 goto saw_error;
35293 opcode = NOP_EXPR;
35294 rhs1 = NULL_TREE;
35295 goto stmt_done;
35296 }
35297 cp_parser_error (parser,
35298 "invalid form of %<#pragma omp atomic%>");
35299 goto saw_error;
35300 }
35301 if (!cp_parser_parse_definitely (parser))
35302 goto saw_error;
35303 switch (token->type)
35304 {
35305 case CPP_SEMICOLON:
35306 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35307 {
35308 code = OMP_ATOMIC_CAPTURE_OLD;
35309 v = lhs;
35310 lhs = NULL_TREE;
35311 lhs1 = rhs1;
35312 rhs1 = NULL_TREE;
35313 cp_lexer_consume_token (parser->lexer);
35314 goto restart;
35315 }
35316 else if (structured_block)
35317 {
35318 opcode = NOP_EXPR;
35319 rhs = rhs1;
35320 rhs1 = NULL_TREE;
35321 goto stmt_done;
35322 }
35323 cp_parser_error (parser,
35324 "invalid form of %<#pragma omp atomic%>");
35325 goto saw_error;
35326 case CPP_MULT:
35327 opcode = MULT_EXPR;
35328 break;
35329 case CPP_DIV:
35330 opcode = TRUNC_DIV_EXPR;
35331 break;
35332 case CPP_PLUS:
35333 opcode = PLUS_EXPR;
35334 break;
35335 case CPP_MINUS:
35336 opcode = MINUS_EXPR;
35337 break;
35338 case CPP_LSHIFT:
35339 opcode = LSHIFT_EXPR;
35340 break;
35341 case CPP_RSHIFT:
35342 opcode = RSHIFT_EXPR;
35343 break;
35344 case CPP_AND:
35345 opcode = BIT_AND_EXPR;
35346 break;
35347 case CPP_OR:
35348 opcode = BIT_IOR_EXPR;
35349 break;
35350 case CPP_XOR:
35351 opcode = BIT_XOR_EXPR;
35352 break;
35353 default:
35354 cp_parser_error (parser,
35355 "invalid operator for %<#pragma omp atomic%>");
35356 goto saw_error;
35357 }
35358 oprec = TOKEN_PRECEDENCE (token);
35359 gcc_assert (oprec != PREC_NOT_OPERATOR);
35360 if (commutative_tree_code (opcode))
35361 oprec = (enum cp_parser_prec) (oprec - 1);
35362 cp_lexer_consume_token (parser->lexer);
35363 rhs = cp_parser_binary_expression (parser, false, false,
35364 oprec, NULL);
35365 if (rhs == error_mark_node)
35366 goto saw_error;
35367 goto stmt_done;
35368 /* FALLTHROUGH */
35369 default:
35370 cp_parser_error (parser,
35371 "invalid operator for %<#pragma omp atomic%>");
35372 goto saw_error;
35373 }
35374 cp_lexer_consume_token (parser->lexer);
35375
35376 rhs = cp_parser_expression (parser);
35377 if (rhs == error_mark_node)
35378 goto saw_error;
35379 break;
35380 }
35381 stmt_done:
35382 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35383 {
35384 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35385 goto saw_error;
35386 v = cp_parser_unary_expression (parser);
35387 if (v == error_mark_node)
35388 goto saw_error;
35389 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35390 goto saw_error;
35391 lhs1 = cp_parser_unary_expression (parser);
35392 if (lhs1 == error_mark_node)
35393 goto saw_error;
35394 }
35395 if (structured_block)
35396 {
35397 cp_parser_consume_semicolon_at_end_of_statement (parser);
35398 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35399 }
35400 done:
35401 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35402 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35403 rhs1, clauses, memory_order);
35404 if (!structured_block)
35405 cp_parser_consume_semicolon_at_end_of_statement (parser);
35406 return;
35407
35408 saw_error:
35409 cp_parser_skip_to_end_of_block_or_statement (parser);
35410 if (structured_block)
35411 {
35412 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35413 cp_lexer_consume_token (parser->lexer);
35414 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35415 {
35416 cp_parser_skip_to_end_of_block_or_statement (parser);
35417 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35418 cp_lexer_consume_token (parser->lexer);
35419 }
35420 }
35421 }
35422
35423
35424 /* OpenMP 2.5:
35425 # pragma omp barrier new-line */
35426
35427 static void
35428 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35429 {
35430 cp_parser_require_pragma_eol (parser, pragma_tok);
35431 finish_omp_barrier ();
35432 }
35433
35434 /* OpenMP 2.5:
35435 # pragma omp critical [(name)] new-line
35436 structured-block
35437
35438 OpenMP 4.5:
35439 # pragma omp critical [(name) [hint(expression)]] new-line
35440 structured-block */
35441
35442 #define OMP_CRITICAL_CLAUSE_MASK \
35443 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35444
35445 static tree
35446 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35447 {
35448 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
35449
35450 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35451 {
35452 matching_parens parens;
35453 parens.consume_open (parser);
35454
35455 name = cp_parser_identifier (parser);
35456
35457 if (name == error_mark_node
35458 || !parens.require_close (parser))
35459 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35460 /*or_comma=*/false,
35461 /*consume_paren=*/true);
35462 if (name == error_mark_node)
35463 name = NULL;
35464
35465 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
35466 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35467 cp_lexer_consume_token (parser->lexer);
35468
35469 clauses = cp_parser_omp_all_clauses (parser,
35470 OMP_CRITICAL_CLAUSE_MASK,
35471 "#pragma omp critical", pragma_tok);
35472 }
35473 else
35474 cp_parser_require_pragma_eol (parser, pragma_tok);
35475
35476 stmt = cp_parser_omp_structured_block (parser, if_p);
35477 return c_finish_omp_critical (input_location, stmt, name, clauses);
35478 }
35479
35480 /* OpenMP 5.0:
35481 # pragma omp depobj ( depobj ) depobj-clause new-line
35482
35483 depobj-clause:
35484 depend (dependence-type : locator)
35485 destroy
35486 update (dependence-type)
35487
35488 dependence-type:
35489 in
35490 out
35491 inout
35492 mutexinout */
35493
35494 static void
35495 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
35496 {
35497 location_t loc = pragma_tok->location;
35498 matching_parens parens;
35499 if (!parens.require_open (parser))
35500 {
35501 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35502 return;
35503 }
35504
35505 tree depobj = cp_parser_assignment_expression (parser);
35506
35507 if (!parens.require_close (parser))
35508 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35509 /*or_comma=*/false,
35510 /*consume_paren=*/true);
35511
35512 tree clause = NULL_TREE;
35513 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
35514 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
35515 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35516 {
35517 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35518 const char *p = IDENTIFIER_POINTER (id);
35519
35520 cp_lexer_consume_token (parser->lexer);
35521 if (!strcmp ("depend", p))
35522 {
35523 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
35524 if (clause)
35525 clause = finish_omp_clauses (clause, C_ORT_OMP);
35526 if (!clause)
35527 clause = error_mark_node;
35528 }
35529 else if (!strcmp ("destroy", p))
35530 kind = OMP_CLAUSE_DEPEND_LAST;
35531 else if (!strcmp ("update", p))
35532 {
35533 matching_parens c_parens;
35534 if (c_parens.require_open (parser))
35535 {
35536 location_t c2_loc
35537 = cp_lexer_peek_token (parser->lexer)->location;
35538 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35539 {
35540 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
35541 const char *p2 = IDENTIFIER_POINTER (id2);
35542
35543 cp_lexer_consume_token (parser->lexer);
35544 if (!strcmp ("in", p2))
35545 kind = OMP_CLAUSE_DEPEND_IN;
35546 else if (!strcmp ("out", p2))
35547 kind = OMP_CLAUSE_DEPEND_OUT;
35548 else if (!strcmp ("inout", p2))
35549 kind = OMP_CLAUSE_DEPEND_INOUT;
35550 else if (!strcmp ("mutexinoutset", p2))
35551 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
35552 }
35553 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
35554 {
35555 clause = error_mark_node;
35556 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
35557 "%<mutexinoutset%>");
35558 }
35559 if (!c_parens.require_close (parser))
35560 cp_parser_skip_to_closing_parenthesis (parser,
35561 /*recovering=*/true,
35562 /*or_comma=*/false,
35563 /*consume_paren=*/true);
35564 }
35565 else
35566 clause = error_mark_node;
35567 }
35568 }
35569 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
35570 {
35571 clause = error_mark_node;
35572 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
35573 }
35574 cp_parser_require_pragma_eol (parser, pragma_tok);
35575
35576 finish_omp_depobj (loc, depobj, kind, clause);
35577 }
35578
35579
35580 /* OpenMP 2.5:
35581 # pragma omp flush flush-vars[opt] new-line
35582
35583 flush-vars:
35584 ( variable-list )
35585
35586 OpenMP 5.0:
35587 # pragma omp flush memory-order-clause new-line */
35588
35589 static void
35590 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
35591 {
35592 enum memmodel mo = MEMMODEL_LAST;
35593 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35594 {
35595 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35596 const char *p = IDENTIFIER_POINTER (id);
35597 if (!strcmp (p, "acq_rel"))
35598 mo = MEMMODEL_ACQ_REL;
35599 else if (!strcmp (p, "release"))
35600 mo = MEMMODEL_RELEASE;
35601 else if (!strcmp (p, "acquire"))
35602 mo = MEMMODEL_ACQUIRE;
35603 else
35604 error_at (cp_lexer_peek_token (parser->lexer)->location,
35605 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35606 cp_lexer_consume_token (parser->lexer);
35607 }
35608 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35609 {
35610 if (mo != MEMMODEL_LAST)
35611 error_at (cp_lexer_peek_token (parser->lexer)->location,
35612 "%<flush%> list specified together with memory order "
35613 "clause");
35614 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35615 }
35616 cp_parser_require_pragma_eol (parser, pragma_tok);
35617
35618 finish_omp_flush (mo);
35619 }
35620
35621 /* Helper function, to parse omp for increment expression. */
35622
35623 static tree
35624 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
35625 {
35626 tree cond = cp_parser_binary_expression (parser, false, true,
35627 PREC_NOT_OPERATOR, NULL);
35628 if (cond == error_mark_node
35629 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35630 {
35631 cp_parser_skip_to_end_of_statement (parser);
35632 return error_mark_node;
35633 }
35634
35635 switch (TREE_CODE (cond))
35636 {
35637 case GT_EXPR:
35638 case GE_EXPR:
35639 case LT_EXPR:
35640 case LE_EXPR:
35641 break;
35642 case NE_EXPR:
35643 if (code != OACC_LOOP)
35644 break;
35645 gcc_fallthrough ();
35646 default:
35647 return error_mark_node;
35648 }
35649
35650 /* If decl is an iterator, preserve LHS and RHS of the relational
35651 expr until finish_omp_for. */
35652 if (decl
35653 && (type_dependent_expression_p (decl)
35654 || CLASS_TYPE_P (TREE_TYPE (decl))))
35655 return cond;
35656
35657 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
35658 TREE_CODE (cond),
35659 TREE_OPERAND (cond, 0), ERROR_MARK,
35660 TREE_OPERAND (cond, 1), ERROR_MARK,
35661 /*overload=*/NULL, tf_warning_or_error);
35662 }
35663
35664 /* Helper function, to parse omp for increment expression. */
35665
35666 static tree
35667 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
35668 {
35669 cp_token *token = cp_lexer_peek_token (parser->lexer);
35670 enum tree_code op;
35671 tree lhs, rhs;
35672 cp_id_kind idk;
35673 bool decl_first;
35674
35675 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35676 {
35677 op = (token->type == CPP_PLUS_PLUS
35678 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
35679 cp_lexer_consume_token (parser->lexer);
35680 lhs = cp_parser_simple_cast_expression (parser);
35681 if (lhs != decl
35682 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35683 return error_mark_node;
35684 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35685 }
35686
35687 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
35688 if (lhs != decl
35689 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35690 return error_mark_node;
35691
35692 token = cp_lexer_peek_token (parser->lexer);
35693 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35694 {
35695 op = (token->type == CPP_PLUS_PLUS
35696 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
35697 cp_lexer_consume_token (parser->lexer);
35698 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35699 }
35700
35701 op = cp_parser_assignment_operator_opt (parser);
35702 if (op == ERROR_MARK)
35703 return error_mark_node;
35704
35705 if (op != NOP_EXPR)
35706 {
35707 rhs = cp_parser_assignment_expression (parser);
35708 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
35709 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35710 }
35711
35712 lhs = cp_parser_binary_expression (parser, false, false,
35713 PREC_ADDITIVE_EXPRESSION, NULL);
35714 token = cp_lexer_peek_token (parser->lexer);
35715 decl_first = (lhs == decl
35716 || (processing_template_decl && cp_tree_equal (lhs, decl)));
35717 if (decl_first)
35718 lhs = NULL_TREE;
35719 if (token->type != CPP_PLUS
35720 && token->type != CPP_MINUS)
35721 return error_mark_node;
35722
35723 do
35724 {
35725 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
35726 cp_lexer_consume_token (parser->lexer);
35727 rhs = cp_parser_binary_expression (parser, false, false,
35728 PREC_ADDITIVE_EXPRESSION, NULL);
35729 token = cp_lexer_peek_token (parser->lexer);
35730 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
35731 {
35732 if (lhs == NULL_TREE)
35733 {
35734 if (op == PLUS_EXPR)
35735 lhs = rhs;
35736 else
35737 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
35738 tf_warning_or_error);
35739 }
35740 else
35741 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
35742 ERROR_MARK, NULL, tf_warning_or_error);
35743 }
35744 }
35745 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
35746
35747 if (!decl_first)
35748 {
35749 if ((rhs != decl
35750 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
35751 || op == MINUS_EXPR)
35752 return error_mark_node;
35753 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
35754 }
35755 else
35756 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
35757
35758 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35759 }
35760
35761 /* Parse the initialization statement of an OpenMP for loop.
35762
35763 Return true if the resulting construct should have an
35764 OMP_CLAUSE_PRIVATE added to it. */
35765
35766 static tree
35767 cp_parser_omp_for_loop_init (cp_parser *parser,
35768 tree &this_pre_body,
35769 vec<tree, va_gc> *&for_block,
35770 tree &init,
35771 tree &orig_init,
35772 tree &decl,
35773 tree &real_decl)
35774 {
35775 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35776 return NULL_TREE;
35777
35778 tree add_private_clause = NULL_TREE;
35779
35780 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35781
35782 init-expr:
35783 var = lb
35784 integer-type var = lb
35785 random-access-iterator-type var = lb
35786 pointer-type var = lb
35787 */
35788 cp_decl_specifier_seq type_specifiers;
35789
35790 /* First, try to parse as an initialized declaration. See
35791 cp_parser_condition, from whence the bulk of this is copied. */
35792
35793 cp_parser_parse_tentatively (parser);
35794 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
35795 /*is_trailing_return=*/false,
35796 &type_specifiers);
35797 if (cp_parser_parse_definitely (parser))
35798 {
35799 /* If parsing a type specifier seq succeeded, then this
35800 MUST be a initialized declaration. */
35801 tree asm_specification, attributes;
35802 cp_declarator *declarator;
35803
35804 declarator = cp_parser_declarator (parser,
35805 CP_PARSER_DECLARATOR_NAMED,
35806 /*ctor_dtor_or_conv_p=*/NULL,
35807 /*parenthesized_p=*/NULL,
35808 /*member_p=*/false,
35809 /*friend_p=*/false);
35810 attributes = cp_parser_attributes_opt (parser);
35811 asm_specification = cp_parser_asm_specification_opt (parser);
35812
35813 if (declarator == cp_error_declarator)
35814 cp_parser_skip_to_end_of_statement (parser);
35815
35816 else
35817 {
35818 tree pushed_scope, auto_node;
35819
35820 decl = start_decl (declarator, &type_specifiers,
35821 SD_INITIALIZED, attributes,
35822 /*prefix_attributes=*/NULL_TREE,
35823 &pushed_scope);
35824
35825 auto_node = type_uses_auto (TREE_TYPE (decl));
35826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35827 {
35828 if (cp_lexer_next_token_is (parser->lexer,
35829 CPP_OPEN_PAREN))
35830 error ("parenthesized initialization is not allowed in "
35831 "OpenMP %<for%> loop");
35832 else
35833 /* Trigger an error. */
35834 cp_parser_require (parser, CPP_EQ, RT_EQ);
35835
35836 init = error_mark_node;
35837 cp_parser_skip_to_end_of_statement (parser);
35838 }
35839 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35840 || type_dependent_expression_p (decl)
35841 || auto_node)
35842 {
35843 bool is_direct_init, is_non_constant_init;
35844
35845 init = cp_parser_initializer (parser,
35846 &is_direct_init,
35847 &is_non_constant_init);
35848
35849 if (auto_node)
35850 {
35851 TREE_TYPE (decl)
35852 = do_auto_deduction (TREE_TYPE (decl), init,
35853 auto_node);
35854
35855 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35856 && !type_dependent_expression_p (decl))
35857 goto non_class;
35858 }
35859
35860 cp_finish_decl (decl, init, !is_non_constant_init,
35861 asm_specification,
35862 LOOKUP_ONLYCONVERTING);
35863 orig_init = init;
35864 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35865 {
35866 vec_safe_push (for_block, this_pre_body);
35867 init = NULL_TREE;
35868 }
35869 else
35870 {
35871 init = pop_stmt_list (this_pre_body);
35872 if (init && TREE_CODE (init) == STATEMENT_LIST)
35873 {
35874 tree_stmt_iterator i = tsi_start (init);
35875 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35876 while (!tsi_end_p (i))
35877 {
35878 tree t = tsi_stmt (i);
35879 if (TREE_CODE (t) == DECL_EXPR
35880 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35881 {
35882 tsi_delink (&i);
35883 vec_safe_push (for_block, t);
35884 continue;
35885 }
35886 break;
35887 }
35888 if (tsi_one_before_end_p (i))
35889 {
35890 tree t = tsi_stmt (i);
35891 tsi_delink (&i);
35892 free_stmt_list (init);
35893 init = t;
35894 }
35895 }
35896 }
35897 this_pre_body = NULL_TREE;
35898 }
35899 else
35900 {
35901 /* Consume '='. */
35902 cp_lexer_consume_token (parser->lexer);
35903 init = cp_parser_assignment_expression (parser);
35904
35905 non_class:
35906 if (TYPE_REF_P (TREE_TYPE (decl)))
35907 init = error_mark_node;
35908 else
35909 cp_finish_decl (decl, NULL_TREE,
35910 /*init_const_expr_p=*/false,
35911 asm_specification,
35912 LOOKUP_ONLYCONVERTING);
35913 }
35914
35915 if (pushed_scope)
35916 pop_scope (pushed_scope);
35917 }
35918 }
35919 else
35920 {
35921 cp_id_kind idk;
35922 /* If parsing a type specifier sequence failed, then
35923 this MUST be a simple expression. */
35924 cp_parser_parse_tentatively (parser);
35925 decl = cp_parser_primary_expression (parser, false, false,
35926 false, &idk);
35927 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35928 if (!cp_parser_error_occurred (parser)
35929 && decl
35930 && (TREE_CODE (decl) == COMPONENT_REF
35931 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35932 {
35933 cp_parser_abort_tentative_parse (parser);
35934 cp_parser_parse_tentatively (parser);
35935 cp_token *token = cp_lexer_peek_token (parser->lexer);
35936 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35937 /*check_dependency_p=*/true,
35938 /*template_p=*/NULL,
35939 /*declarator_p=*/false,
35940 /*optional_p=*/false);
35941 if (name != error_mark_node
35942 && last_tok == cp_lexer_peek_token (parser->lexer))
35943 {
35944 decl = cp_parser_lookup_name_simple (parser, name,
35945 token->location);
35946 if (TREE_CODE (decl) == FIELD_DECL)
35947 add_private_clause = omp_privatize_field (decl, false);
35948 }
35949 cp_parser_abort_tentative_parse (parser);
35950 cp_parser_parse_tentatively (parser);
35951 decl = cp_parser_primary_expression (parser, false, false,
35952 false, &idk);
35953 }
35954 if (!cp_parser_error_occurred (parser)
35955 && decl
35956 && DECL_P (decl)
35957 && CLASS_TYPE_P (TREE_TYPE (decl)))
35958 {
35959 tree rhs;
35960
35961 cp_parser_parse_definitely (parser);
35962 cp_parser_require (parser, CPP_EQ, RT_EQ);
35963 rhs = cp_parser_assignment_expression (parser);
35964 orig_init = rhs;
35965 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
35966 decl, NOP_EXPR,
35967 rhs,
35968 tf_warning_or_error));
35969 if (!add_private_clause)
35970 add_private_clause = decl;
35971 }
35972 else
35973 {
35974 decl = NULL;
35975 cp_parser_abort_tentative_parse (parser);
35976 init = cp_parser_expression (parser);
35977 if (init)
35978 {
35979 if (TREE_CODE (init) == MODIFY_EXPR
35980 || TREE_CODE (init) == MODOP_EXPR)
35981 real_decl = TREE_OPERAND (init, 0);
35982 }
35983 }
35984 }
35985 return add_private_clause;
35986 }
35987
35988 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
35989
35990 void
35991 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
35992 tree &decl, tree &orig_decl, tree &init,
35993 tree &orig_init, tree &cond, tree &incr)
35994 {
35995 tree begin, end, range_temp_decl = NULL_TREE;
35996 tree iter_type, begin_expr, end_expr;
35997
35998 if (processing_template_decl)
35999 {
36000 if (check_for_bare_parameter_packs (init))
36001 init = error_mark_node;
36002 if (!type_dependent_expression_p (init)
36003 /* do_auto_deduction doesn't mess with template init-lists. */
36004 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36005 {
36006 tree d = decl;
36007 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36008 {
36009 tree v = DECL_VALUE_EXPR (decl);
36010 if (TREE_CODE (v) == ARRAY_REF
36011 && VAR_P (TREE_OPERAND (v, 0))
36012 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36013 d = TREE_OPERAND (v, 0);
36014 }
36015 do_range_for_auto_deduction (d, init);
36016 }
36017 cond = global_namespace;
36018 incr = NULL_TREE;
36019 orig_init = init;
36020 if (this_pre_body)
36021 this_pre_body = pop_stmt_list (this_pre_body);
36022 return;
36023 }
36024
36025 init = mark_lvalue_use (init);
36026
36027 if (decl == error_mark_node || init == error_mark_node)
36028 /* If an error happened previously do nothing or else a lot of
36029 unhelpful errors would be issued. */
36030 begin_expr = end_expr = iter_type = error_mark_node;
36031 else
36032 {
36033 tree range_temp;
36034
36035 if (VAR_P (init)
36036 && array_of_runtime_bound_p (TREE_TYPE (init)))
36037 /* Can't bind a reference to an array of runtime bound. */
36038 range_temp = init;
36039 else
36040 {
36041 range_temp = build_range_temp (init);
36042 DECL_NAME (range_temp) = NULL_TREE;
36043 pushdecl (range_temp);
36044 cp_finish_decl (range_temp, init,
36045 /*is_constant_init*/false, NULL_TREE,
36046 LOOKUP_ONLYCONVERTING);
36047 range_temp_decl = range_temp;
36048 range_temp = convert_from_reference (range_temp);
36049 }
36050 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36051 &begin_expr, &end_expr);
36052 }
36053
36054 tree end_iter_type = iter_type;
36055 if (cxx_dialect >= cxx17)
36056 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36057 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36058 TREE_USED (end) = 1;
36059 DECL_ARTIFICIAL (end) = 1;
36060 pushdecl (end);
36061 cp_finish_decl (end, end_expr,
36062 /*is_constant_init*/false, NULL_TREE,
36063 LOOKUP_ONLYCONVERTING);
36064
36065 /* The new for initialization statement. */
36066 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36067 TREE_USED (begin) = 1;
36068 DECL_ARTIFICIAL (begin) = 1;
36069 pushdecl (begin);
36070 orig_init = init;
36071 if (CLASS_TYPE_P (iter_type))
36072 init = NULL_TREE;
36073 else
36074 {
36075 init = begin_expr;
36076 begin_expr = NULL_TREE;
36077 }
36078 cp_finish_decl (begin, begin_expr,
36079 /*is_constant_init*/false, NULL_TREE,
36080 LOOKUP_ONLYCONVERTING);
36081
36082 /* The new for condition. */
36083 if (CLASS_TYPE_P (iter_type))
36084 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36085 else
36086 cond = build_x_binary_op (input_location, NE_EXPR,
36087 begin, ERROR_MARK,
36088 end, ERROR_MARK,
36089 NULL, tf_warning_or_error);
36090
36091 /* The new increment expression. */
36092 if (CLASS_TYPE_P (iter_type))
36093 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36094 else
36095 incr = finish_unary_op_expr (input_location,
36096 PREINCREMENT_EXPR, begin,
36097 tf_warning_or_error);
36098
36099 orig_decl = decl;
36100 decl = begin;
36101 if (for_block)
36102 {
36103 vec_safe_push (for_block, this_pre_body);
36104 this_pre_body = NULL_TREE;
36105 }
36106
36107 tree decomp_first_name = NULL_TREE;
36108 unsigned decomp_cnt = 0;
36109 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36110 {
36111 tree v = DECL_VALUE_EXPR (orig_decl);
36112 if (TREE_CODE (v) == ARRAY_REF
36113 && VAR_P (TREE_OPERAND (v, 0))
36114 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36115 {
36116 tree d = orig_decl;
36117 orig_decl = TREE_OPERAND (v, 0);
36118 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36119 decomp_first_name = d;
36120 }
36121 }
36122
36123 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36124 if (auto_node)
36125 {
36126 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36127 tf_none);
36128 if (!error_operand_p (t))
36129 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36130 t, auto_node);
36131 }
36132
36133 tree v = make_tree_vec (decomp_cnt + 3);
36134 TREE_VEC_ELT (v, 0) = range_temp_decl;
36135 TREE_VEC_ELT (v, 1) = end;
36136 TREE_VEC_ELT (v, 2) = orig_decl;
36137 for (unsigned i = 0; i < decomp_cnt; i++)
36138 {
36139 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36140 decomp_first_name = DECL_CHAIN (decomp_first_name);
36141 }
36142 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36143 }
36144
36145 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36146 inside of the collapsed body. */
36147
36148 void
36149 cp_finish_omp_range_for (tree orig, tree begin)
36150 {
36151 gcc_assert (TREE_CODE (orig) == TREE_LIST
36152 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36153 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36154 tree decomp_first_name = NULL_TREE;
36155 unsigned int decomp_cnt = 0;
36156
36157 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36158 {
36159 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36160 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36161 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36162 }
36163
36164 /* The declaration is initialized with *__begin inside the loop body. */
36165 cp_finish_decl (decl,
36166 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36167 tf_warning_or_error),
36168 /*is_constant_init*/false, NULL_TREE,
36169 LOOKUP_ONLYCONVERTING);
36170 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36171 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36172 }
36173
36174 /* Parse the restricted form of the for statement allowed by OpenMP. */
36175
36176 static tree
36177 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36178 tree *cclauses, bool *if_p)
36179 {
36180 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36181 tree orig_decl;
36182 tree real_decl, initv, condv, incrv, declv, orig_declv;
36183 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36184 location_t loc_first;
36185 bool collapse_err = false;
36186 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36187 vec<tree, va_gc> *for_block = make_tree_vector ();
36188 auto_vec<tree, 4> orig_inits;
36189 bool tiling = false;
36190
36191 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36192 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36193 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36194 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36195 {
36196 tiling = true;
36197 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36198 }
36199 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36200 && OMP_CLAUSE_ORDERED_EXPR (cl))
36201 {
36202 ordered_cl = cl;
36203 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36204 }
36205
36206 if (ordered && ordered < collapse)
36207 {
36208 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36209 "%<ordered%> clause parameter is less than %<collapse%>");
36210 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36211 = build_int_cst (NULL_TREE, collapse);
36212 ordered = collapse;
36213 }
36214 if (ordered)
36215 {
36216 for (tree *pc = &clauses; *pc; )
36217 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36218 {
36219 error_at (OMP_CLAUSE_LOCATION (*pc),
36220 "%<linear%> clause may not be specified together "
36221 "with %<ordered%> clause with a parameter");
36222 *pc = OMP_CLAUSE_CHAIN (*pc);
36223 }
36224 else
36225 pc = &OMP_CLAUSE_CHAIN (*pc);
36226 }
36227
36228 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36229 count = ordered ? ordered : collapse;
36230
36231 declv = make_tree_vec (count);
36232 initv = make_tree_vec (count);
36233 condv = make_tree_vec (count);
36234 incrv = make_tree_vec (count);
36235 orig_declv = NULL_TREE;
36236
36237 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36238
36239 for (i = 0; i < count; i++)
36240 {
36241 int bracecount = 0;
36242 tree add_private_clause = NULL_TREE;
36243 location_t loc;
36244
36245 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36246 {
36247 if (!collapse_err)
36248 cp_parser_error (parser, "for statement expected");
36249 return NULL;
36250 }
36251 loc = cp_lexer_consume_token (parser->lexer)->location;
36252
36253 matching_parens parens;
36254 if (!parens.require_open (parser))
36255 return NULL;
36256
36257 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36258 this_pre_body = push_stmt_list ();
36259
36260 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36261 {
36262 /* Save tokens so that we can put them back. */
36263 cp_lexer_save_tokens (parser->lexer);
36264
36265 /* Look for ':' that is not nested in () or {}. */
36266 bool is_range_for
36267 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36268 /*recovering=*/false,
36269 CPP_COLON,
36270 /*consume_paren=*/
36271 false) == -1);
36272
36273 /* Roll back the tokens we skipped. */
36274 cp_lexer_rollback_tokens (parser->lexer);
36275
36276 if (is_range_for)
36277 {
36278 bool saved_colon_corrects_to_scope_p
36279 = parser->colon_corrects_to_scope_p;
36280
36281 /* A colon is used in range-based for. */
36282 parser->colon_corrects_to_scope_p = false;
36283
36284 /* Parse the declaration. */
36285 cp_parser_simple_declaration (parser,
36286 /*function_definition_allowed_p=*/
36287 false, &decl);
36288 parser->colon_corrects_to_scope_p
36289 = saved_colon_corrects_to_scope_p;
36290
36291 cp_parser_require (parser, CPP_COLON, RT_COLON);
36292
36293 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36294 false, 0, true);
36295
36296 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36297 orig_decl, init, orig_init,
36298 cond, incr);
36299 if (this_pre_body)
36300 {
36301 if (pre_body)
36302 {
36303 tree t = pre_body;
36304 pre_body = push_stmt_list ();
36305 add_stmt (t);
36306 add_stmt (this_pre_body);
36307 pre_body = pop_stmt_list (pre_body);
36308 }
36309 else
36310 pre_body = this_pre_body;
36311 }
36312
36313 if (ordered_cl)
36314 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36315 "%<ordered%> clause with parameter on "
36316 "range-based %<for%> loop");
36317
36318 goto parse_close_paren;
36319 }
36320 }
36321
36322 add_private_clause
36323 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36324 init, orig_init, decl, real_decl);
36325
36326 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36327 if (this_pre_body)
36328 {
36329 this_pre_body = pop_stmt_list (this_pre_body);
36330 if (pre_body)
36331 {
36332 tree t = pre_body;
36333 pre_body = push_stmt_list ();
36334 add_stmt (t);
36335 add_stmt (this_pre_body);
36336 pre_body = pop_stmt_list (pre_body);
36337 }
36338 else
36339 pre_body = this_pre_body;
36340 }
36341
36342 if (decl)
36343 real_decl = decl;
36344 if (cclauses != NULL
36345 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36346 && real_decl != NULL_TREE)
36347 {
36348 tree *c;
36349 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36350 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36351 && OMP_CLAUSE_DECL (*c) == real_decl)
36352 {
36353 error_at (loc, "iteration variable %qD"
36354 " should not be firstprivate", real_decl);
36355 *c = OMP_CLAUSE_CHAIN (*c);
36356 }
36357 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36358 && OMP_CLAUSE_DECL (*c) == real_decl)
36359 {
36360 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36361 tree l = *c;
36362 *c = OMP_CLAUSE_CHAIN (*c);
36363 if (code == OMP_SIMD)
36364 {
36365 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36366 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36367 }
36368 else
36369 {
36370 OMP_CLAUSE_CHAIN (l) = clauses;
36371 clauses = l;
36372 }
36373 add_private_clause = NULL_TREE;
36374 }
36375 else
36376 {
36377 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36378 && OMP_CLAUSE_DECL (*c) == real_decl)
36379 add_private_clause = NULL_TREE;
36380 c = &OMP_CLAUSE_CHAIN (*c);
36381 }
36382 }
36383
36384 if (add_private_clause)
36385 {
36386 tree c;
36387 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36388 {
36389 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36390 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36391 && OMP_CLAUSE_DECL (c) == decl)
36392 break;
36393 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36394 && OMP_CLAUSE_DECL (c) == decl)
36395 error_at (loc, "iteration variable %qD "
36396 "should not be firstprivate",
36397 decl);
36398 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36399 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36400 && OMP_CLAUSE_DECL (c) == decl)
36401 error_at (loc, "iteration variable %qD should not be reduction",
36402 decl);
36403 }
36404 if (c == NULL)
36405 {
36406 if (code != OMP_SIMD)
36407 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36408 else if (collapse == 1)
36409 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36410 else
36411 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36412 OMP_CLAUSE_DECL (c) = add_private_clause;
36413 c = finish_omp_clauses (c, C_ORT_OMP);
36414 if (c)
36415 {
36416 OMP_CLAUSE_CHAIN (c) = clauses;
36417 clauses = c;
36418 /* For linear, signal that we need to fill up
36419 the so far unknown linear step. */
36420 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36421 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36422 }
36423 }
36424 }
36425
36426 cond = NULL;
36427 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36428 cond = cp_parser_omp_for_cond (parser, decl, code);
36429 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36430
36431 incr = NULL;
36432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36433 {
36434 /* If decl is an iterator, preserve the operator on decl
36435 until finish_omp_for. */
36436 if (real_decl
36437 && ((processing_template_decl
36438 && (TREE_TYPE (real_decl) == NULL_TREE
36439 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
36440 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
36441 incr = cp_parser_omp_for_incr (parser, real_decl);
36442 else
36443 incr = cp_parser_expression (parser);
36444 if (!EXPR_HAS_LOCATION (incr))
36445 protected_set_expr_location (incr, input_location);
36446 }
36447
36448 parse_close_paren:
36449 if (!parens.require_close (parser))
36450 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36451 /*or_comma=*/false,
36452 /*consume_paren=*/true);
36453
36454 TREE_VEC_ELT (declv, i) = decl;
36455 TREE_VEC_ELT (initv, i) = init;
36456 TREE_VEC_ELT (condv, i) = cond;
36457 TREE_VEC_ELT (incrv, i) = incr;
36458 if (orig_init)
36459 {
36460 orig_inits.safe_grow_cleared (i + 1);
36461 orig_inits[i] = orig_init;
36462 }
36463 if (orig_decl)
36464 {
36465 if (!orig_declv)
36466 orig_declv = copy_node (declv);
36467 TREE_VEC_ELT (orig_declv, i) = orig_decl;
36468 }
36469 else if (orig_declv)
36470 TREE_VEC_ELT (orig_declv, i) = decl;
36471
36472 if (i == count - 1)
36473 break;
36474
36475 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36476 in between the collapsed for loops to be still considered perfectly
36477 nested. Hopefully the final version clarifies this.
36478 For now handle (multiple) {'s and empty statements. */
36479 cp_parser_parse_tentatively (parser);
36480 for (;;)
36481 {
36482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36483 break;
36484 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36485 {
36486 cp_lexer_consume_token (parser->lexer);
36487 bracecount++;
36488 }
36489 else if (bracecount
36490 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36491 cp_lexer_consume_token (parser->lexer);
36492 else
36493 {
36494 loc = cp_lexer_peek_token (parser->lexer)->location;
36495 error_at (loc, "not enough for loops to collapse");
36496 collapse_err = true;
36497 cp_parser_abort_tentative_parse (parser);
36498 declv = NULL_TREE;
36499 break;
36500 }
36501 }
36502
36503 if (declv)
36504 {
36505 cp_parser_parse_definitely (parser);
36506 nbraces += bracecount;
36507 }
36508 }
36509
36510 if (nbraces)
36511 if_p = NULL;
36512
36513 /* Note that we saved the original contents of this flag when we entered
36514 the structured block, and so we don't need to re-save it here. */
36515 parser->in_statement = IN_OMP_FOR;
36516
36517 /* Note that the grammar doesn't call for a structured block here,
36518 though the loop as a whole is a structured block. */
36519 if (orig_declv)
36520 {
36521 body = begin_omp_structured_block ();
36522 for (i = 0; i < count; i++)
36523 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
36524 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
36525 TREE_VEC_ELT (declv, i));
36526 }
36527 else
36528 body = push_stmt_list ();
36529 cp_parser_statement (parser, NULL_TREE, false, if_p);
36530 if (orig_declv)
36531 body = finish_omp_structured_block (body);
36532 else
36533 body = pop_stmt_list (body);
36534
36535 if (declv == NULL_TREE)
36536 ret = NULL_TREE;
36537 else
36538 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
36539 incrv, body, pre_body, &orig_inits, clauses);
36540
36541 while (nbraces)
36542 {
36543 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36544 {
36545 cp_lexer_consume_token (parser->lexer);
36546 nbraces--;
36547 }
36548 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36549 cp_lexer_consume_token (parser->lexer);
36550 else
36551 {
36552 if (!collapse_err)
36553 {
36554 error_at (cp_lexer_peek_token (parser->lexer)->location,
36555 "collapsed loops not perfectly nested");
36556 }
36557 collapse_err = true;
36558 cp_parser_statement_seq_opt (parser, NULL);
36559 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
36560 break;
36561 }
36562 }
36563
36564 while (!for_block->is_empty ())
36565 {
36566 tree t = for_block->pop ();
36567 if (TREE_CODE (t) == STATEMENT_LIST)
36568 add_stmt (pop_stmt_list (t));
36569 else
36570 add_stmt (t);
36571 }
36572 release_tree_vector (for_block);
36573
36574 return ret;
36575 }
36576
36577 /* Helper function for OpenMP parsing, split clauses and call
36578 finish_omp_clauses on each of the set of clauses afterwards. */
36579
36580 static void
36581 cp_omp_split_clauses (location_t loc, enum tree_code code,
36582 omp_clause_mask mask, tree clauses, tree *cclauses)
36583 {
36584 int i;
36585 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
36586 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
36587 if (cclauses[i])
36588 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
36589 }
36590
36591 /* OpenMP 4.0:
36592 #pragma omp simd simd-clause[optseq] new-line
36593 for-loop */
36594
36595 #define OMP_SIMD_CLAUSE_MASK \
36596 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36603 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36604 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36605 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36606
36607 static tree
36608 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
36609 char *p_name, omp_clause_mask mask, tree *cclauses,
36610 bool *if_p)
36611 {
36612 tree clauses, sb, ret;
36613 unsigned int save;
36614 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36615
36616 strcat (p_name, " simd");
36617 mask |= OMP_SIMD_CLAUSE_MASK;
36618
36619 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36620 cclauses == NULL);
36621 if (cclauses)
36622 {
36623 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
36624 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
36625 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
36626 OMP_CLAUSE_ORDERED);
36627 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
36628 {
36629 error_at (OMP_CLAUSE_LOCATION (c),
36630 "%<ordered%> clause with parameter may not be specified "
36631 "on %qs construct", p_name);
36632 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
36633 }
36634 }
36635
36636 keep_next_level (true);
36637 sb = begin_omp_structured_block ();
36638 save = cp_parser_begin_omp_structured_block (parser);
36639
36640 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
36641
36642 cp_parser_end_omp_structured_block (parser, save);
36643 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36644
36645 return ret;
36646 }
36647
36648 /* OpenMP 2.5:
36649 #pragma omp for for-clause[optseq] new-line
36650 for-loop
36651
36652 OpenMP 4.0:
36653 #pragma omp for simd for-simd-clause[optseq] new-line
36654 for-loop */
36655
36656 #define OMP_FOR_CLAUSE_MASK \
36657 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
36663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
36664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36666
36667 static tree
36668 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
36669 char *p_name, omp_clause_mask mask, tree *cclauses,
36670 bool *if_p)
36671 {
36672 tree clauses, sb, ret;
36673 unsigned int save;
36674 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36675
36676 strcat (p_name, " for");
36677 mask |= OMP_FOR_CLAUSE_MASK;
36678 /* parallel for{, simd} disallows nowait clause, but for
36679 target {teams distribute ,}parallel for{, simd} it should be accepted. */
36680 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
36681 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36682 /* Composite distribute parallel for{, simd} disallows ordered clause. */
36683 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36684 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
36685
36686 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36687 {
36688 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36689 const char *p = IDENTIFIER_POINTER (id);
36690
36691 if (strcmp (p, "simd") == 0)
36692 {
36693 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36694 if (cclauses == NULL)
36695 cclauses = cclauses_buf;
36696
36697 cp_lexer_consume_token (parser->lexer);
36698 if (!flag_openmp) /* flag_openmp_simd */
36699 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36700 cclauses, if_p);
36701 sb = begin_omp_structured_block ();
36702 save = cp_parser_begin_omp_structured_block (parser);
36703 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36704 cclauses, if_p);
36705 cp_parser_end_omp_structured_block (parser, save);
36706 tree body = finish_omp_structured_block (sb);
36707 if (ret == NULL)
36708 return ret;
36709 ret = make_node (OMP_FOR);
36710 TREE_TYPE (ret) = void_type_node;
36711 OMP_FOR_BODY (ret) = body;
36712 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36713 SET_EXPR_LOCATION (ret, loc);
36714 add_stmt (ret);
36715 return ret;
36716 }
36717 }
36718 if (!flag_openmp) /* flag_openmp_simd */
36719 {
36720 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36721 return NULL_TREE;
36722 }
36723
36724 /* Composite distribute parallel for disallows linear clause. */
36725 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36726 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
36727
36728 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36729 cclauses == NULL);
36730 if (cclauses)
36731 {
36732 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
36733 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36734 }
36735
36736 keep_next_level (true);
36737 sb = begin_omp_structured_block ();
36738 save = cp_parser_begin_omp_structured_block (parser);
36739
36740 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
36741
36742 cp_parser_end_omp_structured_block (parser, save);
36743 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36744
36745 return ret;
36746 }
36747
36748 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
36749 omp_clause_mask, tree *, bool *);
36750
36751 /* OpenMP 2.5:
36752 # pragma omp master new-line
36753 structured-block */
36754
36755 static tree
36756 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
36757 char *p_name, omp_clause_mask mask, tree *cclauses,
36758 bool *if_p)
36759 {
36760 tree clauses, sb, ret;
36761 unsigned int save;
36762 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36763
36764 strcat (p_name, " master");
36765
36766 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36767 {
36768 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36769 const char *p = IDENTIFIER_POINTER (id);
36770
36771 if (strcmp (p, "taskloop") == 0)
36772 {
36773 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36774 if (cclauses == NULL)
36775 cclauses = cclauses_buf;
36776
36777 cp_lexer_consume_token (parser->lexer);
36778 if (!flag_openmp) /* flag_openmp_simd */
36779 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36780 cclauses, if_p);
36781 sb = begin_omp_structured_block ();
36782 save = cp_parser_begin_omp_structured_block (parser);
36783 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36784 cclauses, if_p);
36785 cp_parser_end_omp_structured_block (parser, save);
36786 tree body = finish_omp_structured_block (sb);
36787 if (ret == NULL)
36788 return ret;
36789 return c_finish_omp_master (loc, body);
36790 }
36791 }
36792 if (!flag_openmp) /* flag_openmp_simd */
36793 {
36794 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36795 return NULL_TREE;
36796 }
36797
36798 if (cclauses)
36799 {
36800 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36801 false);
36802 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
36803 }
36804 else
36805 cp_parser_require_pragma_eol (parser, pragma_tok);
36806
36807 return c_finish_omp_master (loc,
36808 cp_parser_omp_structured_block (parser, if_p));
36809 }
36810
36811 /* OpenMP 2.5:
36812 # pragma omp ordered new-line
36813 structured-block
36814
36815 OpenMP 4.5:
36816 # pragma omp ordered ordered-clauses new-line
36817 structured-block */
36818
36819 #define OMP_ORDERED_CLAUSE_MASK \
36820 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
36821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
36822
36823 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
36824 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
36825
36826 static bool
36827 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
36828 enum pragma_context context, bool *if_p)
36829 {
36830 location_t loc = pragma_tok->location;
36831
36832 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36833 {
36834 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36835 const char *p = IDENTIFIER_POINTER (id);
36836
36837 if (strcmp (p, "depend") == 0)
36838 {
36839 if (!flag_openmp) /* flag_openmp_simd */
36840 {
36841 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36842 return false;
36843 }
36844 if (context == pragma_stmt)
36845 {
36846 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
36847 "%<depend%> clause may only be used in compound "
36848 "statements");
36849 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36850 return false;
36851 }
36852 tree clauses
36853 = cp_parser_omp_all_clauses (parser,
36854 OMP_ORDERED_DEPEND_CLAUSE_MASK,
36855 "#pragma omp ordered", pragma_tok);
36856 c_finish_omp_ordered (loc, clauses, NULL_TREE);
36857 return false;
36858 }
36859 }
36860
36861 tree clauses
36862 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
36863 "#pragma omp ordered", pragma_tok);
36864
36865 if (!flag_openmp /* flag_openmp_simd */
36866 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
36867 return false;
36868
36869 c_finish_omp_ordered (loc, clauses,
36870 cp_parser_omp_structured_block (parser, if_p));
36871 return true;
36872 }
36873
36874 /* OpenMP 2.5:
36875
36876 section-scope:
36877 { section-sequence }
36878
36879 section-sequence:
36880 section-directive[opt] structured-block
36881 section-sequence section-directive structured-block */
36882
36883 static tree
36884 cp_parser_omp_sections_scope (cp_parser *parser)
36885 {
36886 tree stmt, substmt;
36887 bool error_suppress = false;
36888 cp_token *tok;
36889
36890 matching_braces braces;
36891 if (!braces.require_open (parser))
36892 return NULL_TREE;
36893
36894 stmt = push_stmt_list ();
36895
36896 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
36897 != PRAGMA_OMP_SECTION)
36898 {
36899 substmt = cp_parser_omp_structured_block (parser, NULL);
36900 substmt = build1 (OMP_SECTION, void_type_node, substmt);
36901 add_stmt (substmt);
36902 }
36903
36904 while (1)
36905 {
36906 tok = cp_lexer_peek_token (parser->lexer);
36907 if (tok->type == CPP_CLOSE_BRACE)
36908 break;
36909 if (tok->type == CPP_EOF)
36910 break;
36911
36912 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
36913 {
36914 cp_lexer_consume_token (parser->lexer);
36915 cp_parser_require_pragma_eol (parser, tok);
36916 error_suppress = false;
36917 }
36918 else if (!error_suppress)
36919 {
36920 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
36921 error_suppress = true;
36922 }
36923
36924 substmt = cp_parser_omp_structured_block (parser, NULL);
36925 substmt = build1 (OMP_SECTION, void_type_node, substmt);
36926 add_stmt (substmt);
36927 }
36928 braces.require_close (parser);
36929
36930 substmt = pop_stmt_list (stmt);
36931
36932 stmt = make_node (OMP_SECTIONS);
36933 TREE_TYPE (stmt) = void_type_node;
36934 OMP_SECTIONS_BODY (stmt) = substmt;
36935
36936 add_stmt (stmt);
36937 return stmt;
36938 }
36939
36940 /* OpenMP 2.5:
36941 # pragma omp sections sections-clause[optseq] newline
36942 sections-scope */
36943
36944 #define OMP_SECTIONS_CLAUSE_MASK \
36945 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36950
36951 static tree
36952 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
36953 char *p_name, omp_clause_mask mask, tree *cclauses)
36954 {
36955 tree clauses, ret;
36956 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36957
36958 strcat (p_name, " sections");
36959 mask |= OMP_SECTIONS_CLAUSE_MASK;
36960 if (cclauses)
36961 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36962
36963 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36964 cclauses == NULL);
36965 if (cclauses)
36966 {
36967 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
36968 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
36969 }
36970
36971 ret = cp_parser_omp_sections_scope (parser);
36972 if (ret)
36973 OMP_SECTIONS_CLAUSES (ret) = clauses;
36974
36975 return ret;
36976 }
36977
36978 /* OpenMP 2.5:
36979 # pragma omp parallel parallel-clause[optseq] new-line
36980 structured-block
36981 # pragma omp parallel for parallel-for-clause[optseq] new-line
36982 structured-block
36983 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
36984 structured-block
36985
36986 OpenMP 4.0:
36987 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
36988 structured-block */
36989
36990 #define OMP_PARALLEL_CLAUSE_MASK \
36991 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37000
37001 static tree
37002 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37003 char *p_name, omp_clause_mask mask, tree *cclauses,
37004 bool *if_p)
37005 {
37006 tree stmt, clauses, block;
37007 unsigned int save;
37008 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37009
37010 strcat (p_name, " parallel");
37011 mask |= OMP_PARALLEL_CLAUSE_MASK;
37012 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37013 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37014 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37015 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37016
37017 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37018 {
37019 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37020 if (cclauses == NULL)
37021 cclauses = cclauses_buf;
37022
37023 cp_lexer_consume_token (parser->lexer);
37024 if (!flag_openmp) /* flag_openmp_simd */
37025 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37026 if_p);
37027 block = begin_omp_parallel ();
37028 save = cp_parser_begin_omp_structured_block (parser);
37029 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37030 if_p);
37031 cp_parser_end_omp_structured_block (parser, save);
37032 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37033 block);
37034 if (ret == NULL_TREE)
37035 return ret;
37036 OMP_PARALLEL_COMBINED (stmt) = 1;
37037 return stmt;
37038 }
37039 /* When combined with distribute, parallel has to be followed by for.
37040 #pragma omp target parallel is allowed though. */
37041 else if (cclauses
37042 && (mask & (OMP_CLAUSE_MASK_1
37043 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37044 {
37045 error_at (loc, "expected %<for%> after %qs", p_name);
37046 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37047 return NULL_TREE;
37048 }
37049 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37050 {
37051 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37052 const char *p = IDENTIFIER_POINTER (id);
37053 if (strcmp (p, "master") == 0)
37054 {
37055 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37056 cclauses = cclauses_buf;
37057
37058 cp_lexer_consume_token (parser->lexer);
37059 block = begin_omp_parallel ();
37060 save = cp_parser_begin_omp_structured_block (parser);
37061 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37062 cclauses, if_p);
37063 cp_parser_end_omp_structured_block (parser, save);
37064 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37065 block);
37066 OMP_PARALLEL_COMBINED (stmt) = 1;
37067 if (ret == NULL_TREE)
37068 return ret;
37069 return stmt;
37070 }
37071 else if (!flag_openmp) /* flag_openmp_simd */
37072 {
37073 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37074 return NULL_TREE;
37075 }
37076 else if (strcmp (p, "sections") == 0)
37077 {
37078 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37079 cclauses = cclauses_buf;
37080
37081 cp_lexer_consume_token (parser->lexer);
37082 block = begin_omp_parallel ();
37083 save = cp_parser_begin_omp_structured_block (parser);
37084 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37085 cp_parser_end_omp_structured_block (parser, save);
37086 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37087 block);
37088 OMP_PARALLEL_COMBINED (stmt) = 1;
37089 return stmt;
37090 }
37091 }
37092 else if (!flag_openmp) /* flag_openmp_simd */
37093 {
37094 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37095 return NULL_TREE;
37096 }
37097
37098 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37099 cclauses == NULL);
37100 if (cclauses)
37101 {
37102 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37103 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37104 }
37105
37106 block = begin_omp_parallel ();
37107 save = cp_parser_begin_omp_structured_block (parser);
37108 cp_parser_statement (parser, NULL_TREE, false, if_p);
37109 cp_parser_end_omp_structured_block (parser, save);
37110 stmt = finish_omp_parallel (clauses, block);
37111 return stmt;
37112 }
37113
37114 /* OpenMP 2.5:
37115 # pragma omp single single-clause[optseq] new-line
37116 structured-block */
37117
37118 #define OMP_SINGLE_CLAUSE_MASK \
37119 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37123
37124 static tree
37125 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37126 {
37127 tree stmt = make_node (OMP_SINGLE);
37128 TREE_TYPE (stmt) = void_type_node;
37129 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37130
37131 OMP_SINGLE_CLAUSES (stmt)
37132 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37133 "#pragma omp single", pragma_tok);
37134 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37135
37136 return add_stmt (stmt);
37137 }
37138
37139 /* OpenMP 3.0:
37140 # pragma omp task task-clause[optseq] new-line
37141 structured-block */
37142
37143 #define OMP_TASK_CLAUSE_MASK \
37144 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37148 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37155
37156 static tree
37157 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37158 {
37159 tree clauses, block;
37160 unsigned int save;
37161
37162 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37163 "#pragma omp task", pragma_tok);
37164 block = begin_omp_task ();
37165 save = cp_parser_begin_omp_structured_block (parser);
37166 cp_parser_statement (parser, NULL_TREE, false, if_p);
37167 cp_parser_end_omp_structured_block (parser, save);
37168 return finish_omp_task (clauses, block);
37169 }
37170
37171 /* OpenMP 3.0:
37172 # pragma omp taskwait new-line
37173
37174 OpenMP 5.0:
37175 # pragma omp taskwait taskwait-clause[opt] new-line */
37176
37177 #define OMP_TASKWAIT_CLAUSE_MASK \
37178 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37179
37180 static void
37181 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37182 {
37183 tree clauses
37184 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37185 "#pragma omp taskwait", pragma_tok);
37186
37187 if (clauses)
37188 {
37189 tree stmt = make_node (OMP_TASK);
37190 TREE_TYPE (stmt) = void_node;
37191 OMP_TASK_CLAUSES (stmt) = clauses;
37192 OMP_TASK_BODY (stmt) = NULL_TREE;
37193 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37194 add_stmt (stmt);
37195 }
37196 else
37197 finish_omp_taskwait ();
37198 }
37199
37200 /* OpenMP 3.1:
37201 # pragma omp taskyield new-line */
37202
37203 static void
37204 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37205 {
37206 cp_parser_require_pragma_eol (parser, pragma_tok);
37207 finish_omp_taskyield ();
37208 }
37209
37210 /* OpenMP 4.0:
37211 # pragma omp taskgroup new-line
37212 structured-block
37213
37214 OpenMP 5.0:
37215 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37216
37217 #define OMP_TASKGROUP_CLAUSE_MASK \
37218 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37219
37220 static tree
37221 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37222 {
37223 tree clauses
37224 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37225 "#pragma omp taskgroup", pragma_tok);
37226 return c_finish_omp_taskgroup (input_location,
37227 cp_parser_omp_structured_block (parser,
37228 if_p),
37229 clauses);
37230 }
37231
37232
37233 /* OpenMP 2.5:
37234 # pragma omp threadprivate (variable-list) */
37235
37236 static void
37237 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37238 {
37239 tree vars;
37240
37241 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37242 cp_parser_require_pragma_eol (parser, pragma_tok);
37243
37244 finish_omp_threadprivate (vars);
37245 }
37246
37247 /* OpenMP 4.0:
37248 # pragma omp cancel cancel-clause[optseq] new-line */
37249
37250 #define OMP_CANCEL_CLAUSE_MASK \
37251 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37255 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37256
37257 static void
37258 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37259 {
37260 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37261 "#pragma omp cancel", pragma_tok);
37262 finish_omp_cancel (clauses);
37263 }
37264
37265 /* OpenMP 4.0:
37266 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37267
37268 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37269 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37273
37274 static void
37275 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37276 enum pragma_context context)
37277 {
37278 tree clauses;
37279 bool point_seen = false;
37280
37281 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37282 {
37283 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37284 const char *p = IDENTIFIER_POINTER (id);
37285
37286 if (strcmp (p, "point") == 0)
37287 {
37288 cp_lexer_consume_token (parser->lexer);
37289 point_seen = true;
37290 }
37291 }
37292 if (!point_seen)
37293 {
37294 cp_parser_error (parser, "expected %<point%>");
37295 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37296 return;
37297 }
37298
37299 if (context != pragma_compound)
37300 {
37301 if (context == pragma_stmt)
37302 error_at (pragma_tok->location,
37303 "%<#pragma %s%> may only be used in compound statements",
37304 "omp cancellation point");
37305 else
37306 cp_parser_error (parser, "expected declaration specifiers");
37307 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37308 return;
37309 }
37310
37311 clauses = cp_parser_omp_all_clauses (parser,
37312 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37313 "#pragma omp cancellation point",
37314 pragma_tok);
37315 finish_omp_cancellation_point (clauses);
37316 }
37317
37318 /* OpenMP 4.0:
37319 #pragma omp distribute distribute-clause[optseq] new-line
37320 for-loop */
37321
37322 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37323 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37328
37329 static tree
37330 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37331 char *p_name, omp_clause_mask mask, tree *cclauses,
37332 bool *if_p)
37333 {
37334 tree clauses, sb, ret;
37335 unsigned int save;
37336 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37337
37338 strcat (p_name, " distribute");
37339 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37340
37341 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37342 {
37343 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37344 const char *p = IDENTIFIER_POINTER (id);
37345 bool simd = false;
37346 bool parallel = false;
37347
37348 if (strcmp (p, "simd") == 0)
37349 simd = true;
37350 else
37351 parallel = strcmp (p, "parallel") == 0;
37352 if (parallel || simd)
37353 {
37354 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37355 if (cclauses == NULL)
37356 cclauses = cclauses_buf;
37357 cp_lexer_consume_token (parser->lexer);
37358 if (!flag_openmp) /* flag_openmp_simd */
37359 {
37360 if (simd)
37361 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37362 cclauses, if_p);
37363 else
37364 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37365 cclauses, if_p);
37366 }
37367 sb = begin_omp_structured_block ();
37368 save = cp_parser_begin_omp_structured_block (parser);
37369 if (simd)
37370 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37371 cclauses, if_p);
37372 else
37373 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37374 cclauses, if_p);
37375 cp_parser_end_omp_structured_block (parser, save);
37376 tree body = finish_omp_structured_block (sb);
37377 if (ret == NULL)
37378 return ret;
37379 ret = make_node (OMP_DISTRIBUTE);
37380 TREE_TYPE (ret) = void_type_node;
37381 OMP_FOR_BODY (ret) = body;
37382 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37383 SET_EXPR_LOCATION (ret, loc);
37384 add_stmt (ret);
37385 return ret;
37386 }
37387 }
37388 if (!flag_openmp) /* flag_openmp_simd */
37389 {
37390 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37391 return NULL_TREE;
37392 }
37393
37394 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37395 cclauses == NULL);
37396 if (cclauses)
37397 {
37398 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37399 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37400 }
37401
37402 keep_next_level (true);
37403 sb = begin_omp_structured_block ();
37404 save = cp_parser_begin_omp_structured_block (parser);
37405
37406 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37407
37408 cp_parser_end_omp_structured_block (parser, save);
37409 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37410
37411 return ret;
37412 }
37413
37414 /* OpenMP 4.0:
37415 # pragma omp teams teams-clause[optseq] new-line
37416 structured-block */
37417
37418 #define OMP_TEAMS_CLAUSE_MASK \
37419 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37426
37427 static tree
37428 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37429 char *p_name, omp_clause_mask mask, tree *cclauses,
37430 bool *if_p)
37431 {
37432 tree clauses, sb, ret;
37433 unsigned int save;
37434 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37435
37436 strcat (p_name, " teams");
37437 mask |= OMP_TEAMS_CLAUSE_MASK;
37438
37439 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37440 {
37441 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37442 const char *p = IDENTIFIER_POINTER (id);
37443 if (strcmp (p, "distribute") == 0)
37444 {
37445 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37446 if (cclauses == NULL)
37447 cclauses = cclauses_buf;
37448
37449 cp_lexer_consume_token (parser->lexer);
37450 if (!flag_openmp) /* flag_openmp_simd */
37451 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37452 cclauses, if_p);
37453 keep_next_level (true);
37454 sb = begin_omp_structured_block ();
37455 save = cp_parser_begin_omp_structured_block (parser);
37456 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37457 cclauses, if_p);
37458 cp_parser_end_omp_structured_block (parser, save);
37459 tree body = finish_omp_structured_block (sb);
37460 if (ret == NULL)
37461 return ret;
37462 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37463 ret = make_node (OMP_TEAMS);
37464 TREE_TYPE (ret) = void_type_node;
37465 OMP_TEAMS_CLAUSES (ret) = clauses;
37466 OMP_TEAMS_BODY (ret) = body;
37467 OMP_TEAMS_COMBINED (ret) = 1;
37468 SET_EXPR_LOCATION (ret, loc);
37469 return add_stmt (ret);
37470 }
37471 }
37472 if (!flag_openmp) /* flag_openmp_simd */
37473 {
37474 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37475 return NULL_TREE;
37476 }
37477
37478 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37479 cclauses == NULL);
37480 if (cclauses)
37481 {
37482 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
37483 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37484 }
37485
37486 tree stmt = make_node (OMP_TEAMS);
37487 TREE_TYPE (stmt) = void_type_node;
37488 OMP_TEAMS_CLAUSES (stmt) = clauses;
37489 keep_next_level (true);
37490 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37491 SET_EXPR_LOCATION (stmt, loc);
37492
37493 return add_stmt (stmt);
37494 }
37495
37496 /* OpenMP 4.0:
37497 # pragma omp target data target-data-clause[optseq] new-line
37498 structured-block */
37499
37500 #define OMP_TARGET_DATA_CLAUSE_MASK \
37501 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37505
37506 static tree
37507 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37508 {
37509 tree clauses
37510 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
37511 "#pragma omp target data", pragma_tok);
37512 int map_seen = 0;
37513 for (tree *pc = &clauses; *pc;)
37514 {
37515 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37516 switch (OMP_CLAUSE_MAP_KIND (*pc))
37517 {
37518 case GOMP_MAP_TO:
37519 case GOMP_MAP_ALWAYS_TO:
37520 case GOMP_MAP_FROM:
37521 case GOMP_MAP_ALWAYS_FROM:
37522 case GOMP_MAP_TOFROM:
37523 case GOMP_MAP_ALWAYS_TOFROM:
37524 case GOMP_MAP_ALLOC:
37525 map_seen = 3;
37526 break;
37527 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37528 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37529 case GOMP_MAP_ALWAYS_POINTER:
37530 break;
37531 default:
37532 map_seen |= 1;
37533 error_at (OMP_CLAUSE_LOCATION (*pc),
37534 "%<#pragma omp target data%> with map-type other "
37535 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37536 "on %<map%> clause");
37537 *pc = OMP_CLAUSE_CHAIN (*pc);
37538 continue;
37539 }
37540 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
37541 map_seen = 3;
37542 pc = &OMP_CLAUSE_CHAIN (*pc);
37543 }
37544
37545 if (map_seen != 3)
37546 {
37547 if (map_seen == 0)
37548 error_at (pragma_tok->location,
37549 "%<#pragma omp target data%> must contain at least "
37550 "one %<map%> or %<use_device_ptr%> clause");
37551 return NULL_TREE;
37552 }
37553
37554 tree stmt = make_node (OMP_TARGET_DATA);
37555 TREE_TYPE (stmt) = void_type_node;
37556 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
37557
37558 keep_next_level (true);
37559 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37560
37561 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37562 return add_stmt (stmt);
37563 }
37564
37565 /* OpenMP 4.5:
37566 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37567 structured-block */
37568
37569 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37570 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37575
37576 static tree
37577 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
37578 enum pragma_context context)
37579 {
37580 bool data_seen = false;
37581 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37582 {
37583 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37584 const char *p = IDENTIFIER_POINTER (id);
37585
37586 if (strcmp (p, "data") == 0)
37587 {
37588 cp_lexer_consume_token (parser->lexer);
37589 data_seen = true;
37590 }
37591 }
37592 if (!data_seen)
37593 {
37594 cp_parser_error (parser, "expected %<data%>");
37595 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37596 return NULL_TREE;
37597 }
37598
37599 if (context == pragma_stmt)
37600 {
37601 error_at (pragma_tok->location,
37602 "%<#pragma %s%> may only be used in compound statements",
37603 "omp target enter data");
37604 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37605 return NULL_TREE;
37606 }
37607
37608 tree clauses
37609 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
37610 "#pragma omp target enter data", pragma_tok);
37611 int map_seen = 0;
37612 for (tree *pc = &clauses; *pc;)
37613 {
37614 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37615 switch (OMP_CLAUSE_MAP_KIND (*pc))
37616 {
37617 case GOMP_MAP_TO:
37618 case GOMP_MAP_ALWAYS_TO:
37619 case GOMP_MAP_ALLOC:
37620 map_seen = 3;
37621 break;
37622 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37623 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37624 case GOMP_MAP_ALWAYS_POINTER:
37625 break;
37626 default:
37627 map_seen |= 1;
37628 error_at (OMP_CLAUSE_LOCATION (*pc),
37629 "%<#pragma omp target enter data%> with map-type other "
37630 "than %<to%> or %<alloc%> on %<map%> clause");
37631 *pc = OMP_CLAUSE_CHAIN (*pc);
37632 continue;
37633 }
37634 pc = &OMP_CLAUSE_CHAIN (*pc);
37635 }
37636
37637 if (map_seen != 3)
37638 {
37639 if (map_seen == 0)
37640 error_at (pragma_tok->location,
37641 "%<#pragma omp target enter data%> must contain at least "
37642 "one %<map%> clause");
37643 return NULL_TREE;
37644 }
37645
37646 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
37647 TREE_TYPE (stmt) = void_type_node;
37648 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
37649 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37650 return add_stmt (stmt);
37651 }
37652
37653 /* OpenMP 4.5:
37654 # pragma omp target exit data target-enter-data-clause[optseq] new-line
37655 structured-block */
37656
37657 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
37658 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37663
37664 static tree
37665 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
37666 enum pragma_context context)
37667 {
37668 bool data_seen = false;
37669 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37670 {
37671 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37672 const char *p = IDENTIFIER_POINTER (id);
37673
37674 if (strcmp (p, "data") == 0)
37675 {
37676 cp_lexer_consume_token (parser->lexer);
37677 data_seen = true;
37678 }
37679 }
37680 if (!data_seen)
37681 {
37682 cp_parser_error (parser, "expected %<data%>");
37683 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37684 return NULL_TREE;
37685 }
37686
37687 if (context == pragma_stmt)
37688 {
37689 error_at (pragma_tok->location,
37690 "%<#pragma %s%> may only be used in compound statements",
37691 "omp target exit data");
37692 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37693 return NULL_TREE;
37694 }
37695
37696 tree clauses
37697 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
37698 "#pragma omp target exit data", pragma_tok);
37699 int map_seen = 0;
37700 for (tree *pc = &clauses; *pc;)
37701 {
37702 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37703 switch (OMP_CLAUSE_MAP_KIND (*pc))
37704 {
37705 case GOMP_MAP_FROM:
37706 case GOMP_MAP_ALWAYS_FROM:
37707 case GOMP_MAP_RELEASE:
37708 case GOMP_MAP_DELETE:
37709 map_seen = 3;
37710 break;
37711 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37712 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37713 case GOMP_MAP_ALWAYS_POINTER:
37714 break;
37715 default:
37716 map_seen |= 1;
37717 error_at (OMP_CLAUSE_LOCATION (*pc),
37718 "%<#pragma omp target exit data%> with map-type other "
37719 "than %<from%>, %<release%> or %<delete%> on %<map%>"
37720 " clause");
37721 *pc = OMP_CLAUSE_CHAIN (*pc);
37722 continue;
37723 }
37724 pc = &OMP_CLAUSE_CHAIN (*pc);
37725 }
37726
37727 if (map_seen != 3)
37728 {
37729 if (map_seen == 0)
37730 error_at (pragma_tok->location,
37731 "%<#pragma omp target exit data%> must contain at least "
37732 "one %<map%> clause");
37733 return NULL_TREE;
37734 }
37735
37736 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
37737 TREE_TYPE (stmt) = void_type_node;
37738 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
37739 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37740 return add_stmt (stmt);
37741 }
37742
37743 /* OpenMP 4.0:
37744 # pragma omp target update target-update-clause[optseq] new-line */
37745
37746 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
37747 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
37748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37753
37754 static bool
37755 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
37756 enum pragma_context context)
37757 {
37758 if (context == pragma_stmt)
37759 {
37760 error_at (pragma_tok->location,
37761 "%<#pragma %s%> may only be used in compound statements",
37762 "omp target update");
37763 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37764 return false;
37765 }
37766
37767 tree clauses
37768 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
37769 "#pragma omp target update", pragma_tok);
37770 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
37771 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
37772 {
37773 error_at (pragma_tok->location,
37774 "%<#pragma omp target update%> must contain at least one "
37775 "%<from%> or %<to%> clauses");
37776 return false;
37777 }
37778
37779 tree stmt = make_node (OMP_TARGET_UPDATE);
37780 TREE_TYPE (stmt) = void_type_node;
37781 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
37782 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37783 add_stmt (stmt);
37784 return false;
37785 }
37786
37787 /* OpenMP 4.0:
37788 # pragma omp target target-clause[optseq] new-line
37789 structured-block */
37790
37791 #define OMP_TARGET_CLAUSE_MASK \
37792 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
37800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
37801
37802 static bool
37803 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
37804 enum pragma_context context, bool *if_p)
37805 {
37806 tree *pc = NULL, stmt;
37807
37808 if (flag_openmp)
37809 omp_requires_mask
37810 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
37811
37812 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37813 {
37814 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37815 const char *p = IDENTIFIER_POINTER (id);
37816 enum tree_code ccode = ERROR_MARK;
37817
37818 if (strcmp (p, "teams") == 0)
37819 ccode = OMP_TEAMS;
37820 else if (strcmp (p, "parallel") == 0)
37821 ccode = OMP_PARALLEL;
37822 else if (strcmp (p, "simd") == 0)
37823 ccode = OMP_SIMD;
37824 if (ccode != ERROR_MARK)
37825 {
37826 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
37827 char p_name[sizeof ("#pragma omp target teams distribute "
37828 "parallel for simd")];
37829
37830 cp_lexer_consume_token (parser->lexer);
37831 strcpy (p_name, "#pragma omp target");
37832 if (!flag_openmp) /* flag_openmp_simd */
37833 {
37834 tree stmt;
37835 switch (ccode)
37836 {
37837 case OMP_TEAMS:
37838 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
37839 OMP_TARGET_CLAUSE_MASK,
37840 cclauses, if_p);
37841 break;
37842 case OMP_PARALLEL:
37843 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
37844 OMP_TARGET_CLAUSE_MASK,
37845 cclauses, if_p);
37846 break;
37847 case OMP_SIMD:
37848 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
37849 OMP_TARGET_CLAUSE_MASK,
37850 cclauses, if_p);
37851 break;
37852 default:
37853 gcc_unreachable ();
37854 }
37855 return stmt != NULL_TREE;
37856 }
37857 keep_next_level (true);
37858 tree sb = begin_omp_structured_block (), ret;
37859 unsigned save = cp_parser_begin_omp_structured_block (parser);
37860 switch (ccode)
37861 {
37862 case OMP_TEAMS:
37863 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
37864 OMP_TARGET_CLAUSE_MASK, cclauses,
37865 if_p);
37866 break;
37867 case OMP_PARALLEL:
37868 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
37869 OMP_TARGET_CLAUSE_MASK, cclauses,
37870 if_p);
37871 break;
37872 case OMP_SIMD:
37873 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
37874 OMP_TARGET_CLAUSE_MASK, cclauses,
37875 if_p);
37876 break;
37877 default:
37878 gcc_unreachable ();
37879 }
37880 cp_parser_end_omp_structured_block (parser, save);
37881 tree body = finish_omp_structured_block (sb);
37882 if (ret == NULL_TREE)
37883 return false;
37884 if (ccode == OMP_TEAMS && !processing_template_decl)
37885 {
37886 /* For combined target teams, ensure the num_teams and
37887 thread_limit clause expressions are evaluated on the host,
37888 before entering the target construct. */
37889 tree c;
37890 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37891 c; c = OMP_CLAUSE_CHAIN (c))
37892 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
37893 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
37894 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
37895 {
37896 tree expr = OMP_CLAUSE_OPERAND (c, 0);
37897 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
37898 if (expr == error_mark_node)
37899 continue;
37900 tree tmp = TARGET_EXPR_SLOT (expr);
37901 add_stmt (expr);
37902 OMP_CLAUSE_OPERAND (c, 0) = expr;
37903 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
37904 OMP_CLAUSE_FIRSTPRIVATE);
37905 OMP_CLAUSE_DECL (tc) = tmp;
37906 OMP_CLAUSE_CHAIN (tc)
37907 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
37908 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
37909 }
37910 }
37911 tree stmt = make_node (OMP_TARGET);
37912 TREE_TYPE (stmt) = void_type_node;
37913 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
37914 OMP_TARGET_BODY (stmt) = body;
37915 OMP_TARGET_COMBINED (stmt) = 1;
37916 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37917 add_stmt (stmt);
37918 pc = &OMP_TARGET_CLAUSES (stmt);
37919 goto check_clauses;
37920 }
37921 else if (!flag_openmp) /* flag_openmp_simd */
37922 {
37923 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37924 return false;
37925 }
37926 else if (strcmp (p, "data") == 0)
37927 {
37928 cp_lexer_consume_token (parser->lexer);
37929 cp_parser_omp_target_data (parser, pragma_tok, if_p);
37930 return true;
37931 }
37932 else if (strcmp (p, "enter") == 0)
37933 {
37934 cp_lexer_consume_token (parser->lexer);
37935 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
37936 return false;
37937 }
37938 else if (strcmp (p, "exit") == 0)
37939 {
37940 cp_lexer_consume_token (parser->lexer);
37941 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
37942 return false;
37943 }
37944 else if (strcmp (p, "update") == 0)
37945 {
37946 cp_lexer_consume_token (parser->lexer);
37947 return cp_parser_omp_target_update (parser, pragma_tok, context);
37948 }
37949 }
37950 if (!flag_openmp) /* flag_openmp_simd */
37951 {
37952 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37953 return false;
37954 }
37955
37956 stmt = make_node (OMP_TARGET);
37957 TREE_TYPE (stmt) = void_type_node;
37958
37959 OMP_TARGET_CLAUSES (stmt)
37960 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
37961 "#pragma omp target", pragma_tok);
37962 pc = &OMP_TARGET_CLAUSES (stmt);
37963 keep_next_level (true);
37964 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37965
37966 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37967 add_stmt (stmt);
37968
37969 check_clauses:
37970 while (*pc)
37971 {
37972 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37973 switch (OMP_CLAUSE_MAP_KIND (*pc))
37974 {
37975 case GOMP_MAP_TO:
37976 case GOMP_MAP_ALWAYS_TO:
37977 case GOMP_MAP_FROM:
37978 case GOMP_MAP_ALWAYS_FROM:
37979 case GOMP_MAP_TOFROM:
37980 case GOMP_MAP_ALWAYS_TOFROM:
37981 case GOMP_MAP_ALLOC:
37982 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37983 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37984 case GOMP_MAP_ALWAYS_POINTER:
37985 break;
37986 default:
37987 error_at (OMP_CLAUSE_LOCATION (*pc),
37988 "%<#pragma omp target%> with map-type other "
37989 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37990 "on %<map%> clause");
37991 *pc = OMP_CLAUSE_CHAIN (*pc);
37992 continue;
37993 }
37994 pc = &OMP_CLAUSE_CHAIN (*pc);
37995 }
37996 return true;
37997 }
37998
37999 /* OpenACC 2.0:
38000 # pragma acc cache (variable-list) new-line
38001 */
38002
38003 static tree
38004 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38005 {
38006 tree stmt, clauses;
38007
38008 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38009 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38010
38011 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38012
38013 stmt = make_node (OACC_CACHE);
38014 TREE_TYPE (stmt) = void_type_node;
38015 OACC_CACHE_CLAUSES (stmt) = clauses;
38016 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38017 add_stmt (stmt);
38018
38019 return stmt;
38020 }
38021
38022 /* OpenACC 2.0:
38023 # pragma acc data oacc-data-clause[optseq] new-line
38024 structured-block */
38025
38026 #define OACC_DATA_CLAUSE_MASK \
38027 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38028 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38029 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38030 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38034
38035 static tree
38036 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38037 {
38038 tree stmt, clauses, block;
38039 unsigned int save;
38040
38041 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38042 "#pragma acc data", pragma_tok);
38043
38044 block = begin_omp_parallel ();
38045 save = cp_parser_begin_omp_structured_block (parser);
38046 cp_parser_statement (parser, NULL_TREE, false, if_p);
38047 cp_parser_end_omp_structured_block (parser, save);
38048 stmt = finish_oacc_data (clauses, block);
38049 return stmt;
38050 }
38051
38052 /* OpenACC 2.0:
38053 # pragma acc host_data <clauses> new-line
38054 structured-block */
38055
38056 #define OACC_HOST_DATA_CLAUSE_MASK \
38057 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38058
38059 static tree
38060 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38061 {
38062 tree stmt, clauses, block;
38063 unsigned int save;
38064
38065 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38066 "#pragma acc host_data", pragma_tok);
38067
38068 block = begin_omp_parallel ();
38069 save = cp_parser_begin_omp_structured_block (parser);
38070 cp_parser_statement (parser, NULL_TREE, false, if_p);
38071 cp_parser_end_omp_structured_block (parser, save);
38072 stmt = finish_oacc_host_data (clauses, block);
38073 return stmt;
38074 }
38075
38076 /* OpenACC 2.0:
38077 # pragma acc declare oacc-data-clause[optseq] new-line
38078 */
38079
38080 #define OACC_DECLARE_CLAUSE_MASK \
38081 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38089
38090 static tree
38091 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38092 {
38093 tree clauses, stmt;
38094 bool error = false;
38095
38096 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38097 "#pragma acc declare", pragma_tok, true);
38098
38099
38100 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38101 {
38102 error_at (pragma_tok->location,
38103 "no valid clauses specified in %<#pragma acc declare%>");
38104 return NULL_TREE;
38105 }
38106
38107 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38108 {
38109 location_t loc = OMP_CLAUSE_LOCATION (t);
38110 tree decl = OMP_CLAUSE_DECL (t);
38111 if (!DECL_P (decl))
38112 {
38113 error_at (loc, "array section in %<#pragma acc declare%>");
38114 error = true;
38115 continue;
38116 }
38117 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38118 switch (OMP_CLAUSE_MAP_KIND (t))
38119 {
38120 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38121 case GOMP_MAP_ALLOC:
38122 case GOMP_MAP_TO:
38123 case GOMP_MAP_FORCE_DEVICEPTR:
38124 case GOMP_MAP_DEVICE_RESIDENT:
38125 break;
38126
38127 case GOMP_MAP_LINK:
38128 if (!global_bindings_p ()
38129 && (TREE_STATIC (decl)
38130 || !DECL_EXTERNAL (decl)))
38131 {
38132 error_at (loc,
38133 "%qD must be a global variable in "
38134 "%<#pragma acc declare link%>",
38135 decl);
38136 error = true;
38137 continue;
38138 }
38139 break;
38140
38141 default:
38142 if (global_bindings_p ())
38143 {
38144 error_at (loc, "invalid OpenACC clause at file scope");
38145 error = true;
38146 continue;
38147 }
38148 if (DECL_EXTERNAL (decl))
38149 {
38150 error_at (loc,
38151 "invalid use of %<extern%> variable %qD "
38152 "in %<#pragma acc declare%>", decl);
38153 error = true;
38154 continue;
38155 }
38156 else if (TREE_PUBLIC (decl))
38157 {
38158 error_at (loc,
38159 "invalid use of %<global%> variable %qD "
38160 "in %<#pragma acc declare%>", decl);
38161 error = true;
38162 continue;
38163 }
38164 break;
38165 }
38166
38167 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38168 || lookup_attribute ("omp declare target link",
38169 DECL_ATTRIBUTES (decl)))
38170 {
38171 error_at (loc, "variable %qD used more than once with "
38172 "%<#pragma acc declare%>", decl);
38173 error = true;
38174 continue;
38175 }
38176
38177 if (!error)
38178 {
38179 tree id;
38180
38181 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38182 id = get_identifier ("omp declare target link");
38183 else
38184 id = get_identifier ("omp declare target");
38185
38186 DECL_ATTRIBUTES (decl)
38187 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38188 if (global_bindings_p ())
38189 {
38190 symtab_node *node = symtab_node::get (decl);
38191 if (node != NULL)
38192 {
38193 node->offloadable = 1;
38194 if (ENABLE_OFFLOADING)
38195 {
38196 g->have_offload = true;
38197 if (is_a <varpool_node *> (node))
38198 vec_safe_push (offload_vars, decl);
38199 }
38200 }
38201 }
38202 }
38203 }
38204
38205 if (error || global_bindings_p ())
38206 return NULL_TREE;
38207
38208 stmt = make_node (OACC_DECLARE);
38209 TREE_TYPE (stmt) = void_type_node;
38210 OACC_DECLARE_CLAUSES (stmt) = clauses;
38211 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38212
38213 add_stmt (stmt);
38214
38215 return NULL_TREE;
38216 }
38217
38218 /* OpenACC 2.0:
38219 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38220
38221 or
38222
38223 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38224
38225 LOC is the location of the #pragma token.
38226 */
38227
38228 #define OACC_ENTER_DATA_CLAUSE_MASK \
38229 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38234
38235 #define OACC_EXIT_DATA_CLAUSE_MASK \
38236 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38242
38243 static tree
38244 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38245 bool enter)
38246 {
38247 location_t loc = pragma_tok->location;
38248 tree stmt, clauses;
38249 const char *p = "";
38250
38251 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38252 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38253
38254 if (strcmp (p, "data") != 0)
38255 {
38256 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38257 enter ? "enter" : "exit");
38258 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38259 return NULL_TREE;
38260 }
38261
38262 cp_lexer_consume_token (parser->lexer);
38263
38264 if (enter)
38265 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38266 "#pragma acc enter data", pragma_tok);
38267 else
38268 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38269 "#pragma acc exit data", pragma_tok);
38270
38271 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38272 {
38273 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38274 enter ? "enter" : "exit");
38275 return NULL_TREE;
38276 }
38277
38278 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38279 TREE_TYPE (stmt) = void_type_node;
38280 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38281 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38282 add_stmt (stmt);
38283 return stmt;
38284 }
38285
38286 /* OpenACC 2.0:
38287 # pragma acc loop oacc-loop-clause[optseq] new-line
38288 structured-block */
38289
38290 #define OACC_LOOP_CLAUSE_MASK \
38291 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38301
38302 static tree
38303 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38304 omp_clause_mask mask, tree *cclauses, bool *if_p)
38305 {
38306 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38307
38308 strcat (p_name, " loop");
38309 mask |= OACC_LOOP_CLAUSE_MASK;
38310
38311 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38312 cclauses == NULL);
38313 if (cclauses)
38314 {
38315 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38316 if (*cclauses)
38317 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38318 if (clauses)
38319 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38320 }
38321
38322 tree block = begin_omp_structured_block ();
38323 int save = cp_parser_begin_omp_structured_block (parser);
38324 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38325 cp_parser_end_omp_structured_block (parser, save);
38326 add_stmt (finish_omp_structured_block (block));
38327
38328 return stmt;
38329 }
38330
38331 /* OpenACC 2.0:
38332 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38333 structured-block
38334
38335 or
38336
38337 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38338 structured-block
38339 */
38340
38341 #define OACC_KERNELS_CLAUSE_MASK \
38342 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38355
38356 #define OACC_PARALLEL_CLAUSE_MASK \
38357 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38373
38374 static tree
38375 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38376 char *p_name, bool *if_p)
38377 {
38378 omp_clause_mask mask;
38379 enum tree_code code;
38380 switch (cp_parser_pragma_kind (pragma_tok))
38381 {
38382 case PRAGMA_OACC_KERNELS:
38383 strcat (p_name, " kernels");
38384 mask = OACC_KERNELS_CLAUSE_MASK;
38385 code = OACC_KERNELS;
38386 break;
38387 case PRAGMA_OACC_PARALLEL:
38388 strcat (p_name, " parallel");
38389 mask = OACC_PARALLEL_CLAUSE_MASK;
38390 code = OACC_PARALLEL;
38391 break;
38392 default:
38393 gcc_unreachable ();
38394 }
38395
38396 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38397 {
38398 const char *p
38399 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38400 if (strcmp (p, "loop") == 0)
38401 {
38402 cp_lexer_consume_token (parser->lexer);
38403 tree block = begin_omp_parallel ();
38404 tree clauses;
38405 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
38406 if_p);
38407 return finish_omp_construct (code, block, clauses);
38408 }
38409 }
38410
38411 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38412
38413 tree block = begin_omp_parallel ();
38414 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38415 cp_parser_statement (parser, NULL_TREE, false, if_p);
38416 cp_parser_end_omp_structured_block (parser, save);
38417 return finish_omp_construct (code, block, clauses);
38418 }
38419
38420 /* OpenACC 2.0:
38421 # pragma acc update oacc-update-clause[optseq] new-line
38422 */
38423
38424 #define OACC_UPDATE_CLAUSE_MASK \
38425 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38431
38432 static tree
38433 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38434 {
38435 tree stmt, clauses;
38436
38437 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
38438 "#pragma acc update", pragma_tok);
38439
38440 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38441 {
38442 error_at (pragma_tok->location,
38443 "%<#pragma acc update%> must contain at least one "
38444 "%<device%> or %<host%> or %<self%> clause");
38445 return NULL_TREE;
38446 }
38447
38448 stmt = make_node (OACC_UPDATE);
38449 TREE_TYPE (stmt) = void_type_node;
38450 OACC_UPDATE_CLAUSES (stmt) = clauses;
38451 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38452 add_stmt (stmt);
38453 return stmt;
38454 }
38455
38456 /* OpenACC 2.0:
38457 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38458
38459 LOC is the location of the #pragma token.
38460 */
38461
38462 #define OACC_WAIT_CLAUSE_MASK \
38463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38464
38465 static tree
38466 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
38467 {
38468 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
38469 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38470
38471 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38472 list = cp_parser_oacc_wait_list (parser, loc, list);
38473
38474 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
38475 "#pragma acc wait", pragma_tok);
38476
38477 stmt = c_finish_oacc_wait (loc, list, clauses);
38478 stmt = finish_expr_stmt (stmt);
38479
38480 return stmt;
38481 }
38482
38483 /* OpenMP 4.0:
38484 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38485
38486 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38487 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38493
38494 static void
38495 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
38496 enum pragma_context context)
38497 {
38498 bool first_p = parser->omp_declare_simd == NULL;
38499 cp_omp_declare_simd_data data;
38500 if (first_p)
38501 {
38502 data.error_seen = false;
38503 data.fndecl_seen = false;
38504 data.tokens = vNULL;
38505 data.clauses = NULL_TREE;
38506 /* It is safe to take the address of a local variable; it will only be
38507 used while this scope is live. */
38508 parser->omp_declare_simd = &data;
38509 }
38510
38511 /* Store away all pragma tokens. */
38512 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38513 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38514 cp_lexer_consume_token (parser->lexer);
38515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38516 parser->omp_declare_simd->error_seen = true;
38517 cp_parser_require_pragma_eol (parser, pragma_tok);
38518 struct cp_token_cache *cp
38519 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38520 parser->omp_declare_simd->tokens.safe_push (cp);
38521
38522 if (first_p)
38523 {
38524 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38525 cp_parser_pragma (parser, context, NULL);
38526 switch (context)
38527 {
38528 case pragma_external:
38529 cp_parser_declaration (parser);
38530 break;
38531 case pragma_member:
38532 cp_parser_member_declaration (parser);
38533 break;
38534 case pragma_objc_icode:
38535 cp_parser_block_declaration (parser, /*statement_p=*/false);
38536 break;
38537 default:
38538 cp_parser_declaration_statement (parser);
38539 break;
38540 }
38541 if (parser->omp_declare_simd
38542 && !parser->omp_declare_simd->error_seen
38543 && !parser->omp_declare_simd->fndecl_seen)
38544 error_at (pragma_tok->location,
38545 "%<#pragma omp declare simd%> not immediately followed by "
38546 "function declaration or definition");
38547 data.tokens.release ();
38548 parser->omp_declare_simd = NULL;
38549 }
38550 }
38551
38552 /* Finalize #pragma omp declare simd clauses after direct declarator has
38553 been parsed, and put that into "omp declare simd" attribute. */
38554
38555 static tree
38556 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
38557 {
38558 struct cp_token_cache *ce;
38559 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
38560 int i;
38561
38562 if (!data->error_seen && data->fndecl_seen)
38563 {
38564 error ("%<#pragma omp declare simd%> not immediately followed by "
38565 "a single function declaration or definition");
38566 data->error_seen = true;
38567 }
38568 if (data->error_seen)
38569 return attrs;
38570
38571 FOR_EACH_VEC_ELT (data->tokens, i, ce)
38572 {
38573 tree c, cl;
38574
38575 cp_parser_push_lexer_for_tokens (parser, ce);
38576 parser->lexer->in_pragma = true;
38577 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38578 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38579 cp_lexer_consume_token (parser->lexer);
38580 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
38581 "#pragma omp declare simd", pragma_tok);
38582 cp_parser_pop_lexer (parser);
38583 if (cl)
38584 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
38585 c = build_tree_list (get_identifier ("omp declare simd"), cl);
38586 TREE_CHAIN (c) = attrs;
38587 if (processing_template_decl)
38588 ATTR_IS_DEPENDENT (c) = 1;
38589 attrs = c;
38590 }
38591
38592 data->fndecl_seen = true;
38593 return attrs;
38594 }
38595
38596
38597 /* OpenMP 4.0:
38598 # pragma omp declare target new-line
38599 declarations and definitions
38600 # pragma omp end declare target new-line
38601
38602 OpenMP 4.5:
38603 # pragma omp declare target ( extended-list ) new-line
38604
38605 # pragma omp declare target declare-target-clauses[seq] new-line */
38606
38607 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38608 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38610
38611 static void
38612 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
38613 {
38614 tree clauses = NULL_TREE;
38615 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38616 clauses
38617 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
38618 "#pragma omp declare target", pragma_tok);
38619 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38620 {
38621 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
38622 clauses);
38623 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38624 cp_parser_require_pragma_eol (parser, pragma_tok);
38625 }
38626 else
38627 {
38628 cp_parser_require_pragma_eol (parser, pragma_tok);
38629 scope_chain->omp_declare_target_attribute++;
38630 return;
38631 }
38632 if (scope_chain->omp_declare_target_attribute)
38633 error_at (pragma_tok->location,
38634 "%<#pragma omp declare target%> with clauses in between "
38635 "%<#pragma omp declare target%> without clauses and "
38636 "%<#pragma omp end declare target%>");
38637 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
38638 {
38639 tree t = OMP_CLAUSE_DECL (c), id;
38640 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
38641 tree at2 = lookup_attribute ("omp declare target link",
38642 DECL_ATTRIBUTES (t));
38643 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
38644 {
38645 id = get_identifier ("omp declare target link");
38646 std::swap (at1, at2);
38647 }
38648 else
38649 id = get_identifier ("omp declare target");
38650 if (at2)
38651 {
38652 error_at (OMP_CLAUSE_LOCATION (c),
38653 "%qD specified both in declare target %<link%> and %<to%>"
38654 " clauses", t);
38655 continue;
38656 }
38657 if (!at1)
38658 {
38659 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
38660 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
38661 continue;
38662
38663 symtab_node *node = symtab_node::get (t);
38664 if (node != NULL)
38665 {
38666 node->offloadable = 1;
38667 if (ENABLE_OFFLOADING)
38668 {
38669 g->have_offload = true;
38670 if (is_a <varpool_node *> (node))
38671 vec_safe_push (offload_vars, t);
38672 }
38673 }
38674 }
38675 }
38676 }
38677
38678 static void
38679 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
38680 {
38681 const char *p = "";
38682 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38683 {
38684 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38685 p = IDENTIFIER_POINTER (id);
38686 }
38687 if (strcmp (p, "declare") == 0)
38688 {
38689 cp_lexer_consume_token (parser->lexer);
38690 p = "";
38691 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38692 {
38693 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38694 p = IDENTIFIER_POINTER (id);
38695 }
38696 if (strcmp (p, "target") == 0)
38697 cp_lexer_consume_token (parser->lexer);
38698 else
38699 {
38700 cp_parser_error (parser, "expected %<target%>");
38701 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38702 return;
38703 }
38704 }
38705 else
38706 {
38707 cp_parser_error (parser, "expected %<declare%>");
38708 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38709 return;
38710 }
38711 cp_parser_require_pragma_eol (parser, pragma_tok);
38712 if (!scope_chain->omp_declare_target_attribute)
38713 error_at (pragma_tok->location,
38714 "%<#pragma omp end declare target%> without corresponding "
38715 "%<#pragma omp declare target%>");
38716 else
38717 scope_chain->omp_declare_target_attribute--;
38718 }
38719
38720 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
38721 expression and optional initializer clause of
38722 #pragma omp declare reduction. We store the expression(s) as
38723 either 3, 6 or 7 special statements inside of the artificial function's
38724 body. The first two statements are DECL_EXPRs for the artificial
38725 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
38726 expression that uses those variables.
38727 If there was any INITIALIZER clause, this is followed by further statements,
38728 the fourth and fifth statements are DECL_EXPRs for the artificial
38729 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
38730 constructor variant (first token after open paren is not omp_priv),
38731 then the sixth statement is a statement with the function call expression
38732 that uses the OMP_PRIV and optionally OMP_ORIG variable.
38733 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
38734 to initialize the OMP_PRIV artificial variable and there is seventh
38735 statement, a DECL_EXPR of the OMP_PRIV statement again. */
38736
38737 static bool
38738 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
38739 {
38740 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
38741 gcc_assert (TYPE_REF_P (type));
38742 type = TREE_TYPE (type);
38743 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
38744 DECL_ARTIFICIAL (omp_out) = 1;
38745 pushdecl (omp_out);
38746 add_decl_expr (omp_out);
38747 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
38748 DECL_ARTIFICIAL (omp_in) = 1;
38749 pushdecl (omp_in);
38750 add_decl_expr (omp_in);
38751 tree combiner;
38752 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
38753
38754 keep_next_level (true);
38755 tree block = begin_omp_structured_block ();
38756 combiner = cp_parser_expression (parser);
38757 finish_expr_stmt (combiner);
38758 block = finish_omp_structured_block (block);
38759 add_stmt (block);
38760
38761 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38762 return false;
38763
38764 const char *p = "";
38765 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38766 {
38767 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38768 p = IDENTIFIER_POINTER (id);
38769 }
38770
38771 if (strcmp (p, "initializer") == 0)
38772 {
38773 cp_lexer_consume_token (parser->lexer);
38774 matching_parens parens;
38775 if (!parens.require_open (parser))
38776 return false;
38777
38778 p = "";
38779 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38780 {
38781 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38782 p = IDENTIFIER_POINTER (id);
38783 }
38784
38785 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
38786 DECL_ARTIFICIAL (omp_priv) = 1;
38787 pushdecl (omp_priv);
38788 add_decl_expr (omp_priv);
38789 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
38790 DECL_ARTIFICIAL (omp_orig) = 1;
38791 pushdecl (omp_orig);
38792 add_decl_expr (omp_orig);
38793
38794 keep_next_level (true);
38795 block = begin_omp_structured_block ();
38796
38797 bool ctor = false;
38798 if (strcmp (p, "omp_priv") == 0)
38799 {
38800 bool is_direct_init, is_non_constant_init;
38801 ctor = true;
38802 cp_lexer_consume_token (parser->lexer);
38803 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
38804 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
38805 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38806 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
38807 == CPP_CLOSE_PAREN
38808 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
38809 == CPP_CLOSE_PAREN))
38810 {
38811 finish_omp_structured_block (block);
38812 error ("invalid initializer clause");
38813 return false;
38814 }
38815 initializer = cp_parser_initializer (parser, &is_direct_init,
38816 &is_non_constant_init);
38817 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
38818 NULL_TREE, LOOKUP_ONLYCONVERTING);
38819 }
38820 else
38821 {
38822 cp_parser_parse_tentatively (parser);
38823 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
38824 /*check_dependency_p=*/true,
38825 /*template_p=*/NULL,
38826 /*declarator_p=*/false,
38827 /*optional_p=*/false);
38828 vec<tree, va_gc> *args;
38829 if (fn_name == error_mark_node
38830 || cp_parser_error_occurred (parser)
38831 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38832 || ((args = cp_parser_parenthesized_expression_list
38833 (parser, non_attr, /*cast_p=*/false,
38834 /*allow_expansion_p=*/true,
38835 /*non_constant_p=*/NULL)),
38836 cp_parser_error_occurred (parser)))
38837 {
38838 finish_omp_structured_block (block);
38839 cp_parser_abort_tentative_parse (parser);
38840 cp_parser_error (parser, "expected id-expression (arguments)");
38841 return false;
38842 }
38843 unsigned int i;
38844 tree arg;
38845 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
38846 if (arg == omp_priv
38847 || (TREE_CODE (arg) == ADDR_EXPR
38848 && TREE_OPERAND (arg, 0) == omp_priv))
38849 break;
38850 cp_parser_abort_tentative_parse (parser);
38851 if (arg == NULL_TREE)
38852 error ("one of the initializer call arguments should be %<omp_priv%>"
38853 " or %<&omp_priv%>");
38854 initializer = cp_parser_postfix_expression (parser, false, false, false,
38855 false, NULL);
38856 finish_expr_stmt (initializer);
38857 }
38858
38859 block = finish_omp_structured_block (block);
38860 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
38861 add_stmt (block);
38862
38863 if (ctor)
38864 add_decl_expr (omp_orig);
38865
38866 if (!parens.require_close (parser))
38867 return false;
38868 }
38869
38870 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
38871 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
38872 UNKNOWN_LOCATION);
38873
38874 return true;
38875 }
38876
38877 /* OpenMP 4.0
38878 #pragma omp declare reduction (reduction-id : typename-list : expression) \
38879 initializer-clause[opt] new-line
38880
38881 initializer-clause:
38882 initializer (omp_priv initializer)
38883 initializer (function-name (argument-list)) */
38884
38885 static void
38886 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
38887 enum pragma_context)
38888 {
38889 auto_vec<tree> types;
38890 enum tree_code reduc_code = ERROR_MARK;
38891 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
38892 unsigned int i;
38893 cp_token *first_token;
38894 cp_token_cache *cp;
38895 int errs;
38896 void *p;
38897
38898 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
38899 p = obstack_alloc (&declarator_obstack, 0);
38900
38901 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38902 goto fail;
38903
38904 switch (cp_lexer_peek_token (parser->lexer)->type)
38905 {
38906 case CPP_PLUS:
38907 reduc_code = PLUS_EXPR;
38908 break;
38909 case CPP_MULT:
38910 reduc_code = MULT_EXPR;
38911 break;
38912 case CPP_MINUS:
38913 reduc_code = MINUS_EXPR;
38914 break;
38915 case CPP_AND:
38916 reduc_code = BIT_AND_EXPR;
38917 break;
38918 case CPP_XOR:
38919 reduc_code = BIT_XOR_EXPR;
38920 break;
38921 case CPP_OR:
38922 reduc_code = BIT_IOR_EXPR;
38923 break;
38924 case CPP_AND_AND:
38925 reduc_code = TRUTH_ANDIF_EXPR;
38926 break;
38927 case CPP_OR_OR:
38928 reduc_code = TRUTH_ORIF_EXPR;
38929 break;
38930 case CPP_NAME:
38931 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
38932 break;
38933 default:
38934 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
38935 "%<|%>, %<&&%>, %<||%> or identifier");
38936 goto fail;
38937 }
38938
38939 if (reduc_code != ERROR_MARK)
38940 cp_lexer_consume_token (parser->lexer);
38941
38942 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
38943 if (reduc_id == error_mark_node)
38944 goto fail;
38945
38946 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38947 goto fail;
38948
38949 /* Types may not be defined in declare reduction type list. */
38950 const char *saved_message;
38951 saved_message = parser->type_definition_forbidden_message;
38952 parser->type_definition_forbidden_message
38953 = G_("types may not be defined in declare reduction type list");
38954 bool saved_colon_corrects_to_scope_p;
38955 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38956 parser->colon_corrects_to_scope_p = false;
38957 bool saved_colon_doesnt_start_class_def_p;
38958 saved_colon_doesnt_start_class_def_p
38959 = parser->colon_doesnt_start_class_def_p;
38960 parser->colon_doesnt_start_class_def_p = true;
38961
38962 while (true)
38963 {
38964 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38965 type = cp_parser_type_id (parser);
38966 if (type == error_mark_node)
38967 ;
38968 else if (ARITHMETIC_TYPE_P (type)
38969 && (orig_reduc_id == NULL_TREE
38970 || (TREE_CODE (type) != COMPLEX_TYPE
38971 && (id_equal (orig_reduc_id, "min")
38972 || id_equal (orig_reduc_id, "max")))))
38973 error_at (loc, "predeclared arithmetic type %qT in "
38974 "%<#pragma omp declare reduction%>", type);
38975 else if (TREE_CODE (type) == FUNCTION_TYPE
38976 || TREE_CODE (type) == METHOD_TYPE
38977 || TREE_CODE (type) == ARRAY_TYPE)
38978 error_at (loc, "function or array type %qT in "
38979 "%<#pragma omp declare reduction%>", type);
38980 else if (TYPE_REF_P (type))
38981 error_at (loc, "reference type %qT in "
38982 "%<#pragma omp declare reduction%>", type);
38983 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
38984 error_at (loc, "const, volatile or __restrict qualified type %qT in "
38985 "%<#pragma omp declare reduction%>", type);
38986 else
38987 types.safe_push (type);
38988
38989 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38990 cp_lexer_consume_token (parser->lexer);
38991 else
38992 break;
38993 }
38994
38995 /* Restore the saved message. */
38996 parser->type_definition_forbidden_message = saved_message;
38997 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38998 parser->colon_doesnt_start_class_def_p
38999 = saved_colon_doesnt_start_class_def_p;
39000
39001 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39002 || types.is_empty ())
39003 {
39004 fail:
39005 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39006 goto done;
39007 }
39008
39009 first_token = cp_lexer_peek_token (parser->lexer);
39010 cp = NULL;
39011 errs = errorcount;
39012 FOR_EACH_VEC_ELT (types, i, type)
39013 {
39014 tree fntype
39015 = build_function_type_list (void_type_node,
39016 cp_build_reference_type (type, false),
39017 NULL_TREE);
39018 tree this_reduc_id = reduc_id;
39019 if (!dependent_type_p (type))
39020 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39021 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39022 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39023 DECL_ARTIFICIAL (fndecl) = 1;
39024 DECL_EXTERNAL (fndecl) = 1;
39025 DECL_DECLARED_INLINE_P (fndecl) = 1;
39026 DECL_IGNORED_P (fndecl) = 1;
39027 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39028 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39029 DECL_ATTRIBUTES (fndecl)
39030 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39031 DECL_ATTRIBUTES (fndecl));
39032 if (processing_template_decl)
39033 fndecl = push_template_decl (fndecl);
39034 bool block_scope = false;
39035 tree block = NULL_TREE;
39036 if (current_function_decl)
39037 {
39038 block_scope = true;
39039 DECL_CONTEXT (fndecl) = global_namespace;
39040 if (!processing_template_decl)
39041 pushdecl (fndecl);
39042 }
39043 else if (current_class_type)
39044 {
39045 if (cp == NULL)
39046 {
39047 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39048 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39049 cp_lexer_consume_token (parser->lexer);
39050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39051 goto fail;
39052 cp = cp_token_cache_new (first_token,
39053 cp_lexer_peek_nth_token (parser->lexer,
39054 2));
39055 }
39056 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39057 finish_member_declaration (fndecl);
39058 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39059 DECL_PENDING_INLINE_P (fndecl) = 1;
39060 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39061 continue;
39062 }
39063 else
39064 {
39065 DECL_CONTEXT (fndecl) = current_namespace;
39066 pushdecl (fndecl);
39067 }
39068 if (!block_scope)
39069 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39070 else
39071 block = begin_omp_structured_block ();
39072 if (cp)
39073 {
39074 cp_parser_push_lexer_for_tokens (parser, cp);
39075 parser->lexer->in_pragma = true;
39076 }
39077 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39078 {
39079 if (!block_scope)
39080 finish_function (/*inline_p=*/false);
39081 else
39082 DECL_CONTEXT (fndecl) = current_function_decl;
39083 if (cp)
39084 cp_parser_pop_lexer (parser);
39085 goto fail;
39086 }
39087 if (cp)
39088 cp_parser_pop_lexer (parser);
39089 if (!block_scope)
39090 finish_function (/*inline_p=*/false);
39091 else
39092 {
39093 DECL_CONTEXT (fndecl) = current_function_decl;
39094 block = finish_omp_structured_block (block);
39095 if (TREE_CODE (block) == BIND_EXPR)
39096 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39097 else if (TREE_CODE (block) == STATEMENT_LIST)
39098 DECL_SAVED_TREE (fndecl) = block;
39099 if (processing_template_decl)
39100 add_decl_expr (fndecl);
39101 }
39102 cp_check_omp_declare_reduction (fndecl);
39103 if (cp == NULL && types.length () > 1)
39104 cp = cp_token_cache_new (first_token,
39105 cp_lexer_peek_nth_token (parser->lexer, 2));
39106 if (errs != errorcount)
39107 break;
39108 }
39109
39110 cp_parser_require_pragma_eol (parser, pragma_tok);
39111
39112 done:
39113 /* Free any declarators allocated. */
39114 obstack_free (&declarator_obstack, p);
39115 }
39116
39117 /* OpenMP 4.0
39118 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39119 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39120 initializer-clause[opt] new-line
39121 #pragma omp declare target new-line */
39122
39123 static bool
39124 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39125 enum pragma_context context)
39126 {
39127 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39128 {
39129 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39130 const char *p = IDENTIFIER_POINTER (id);
39131
39132 if (strcmp (p, "simd") == 0)
39133 {
39134 cp_lexer_consume_token (parser->lexer);
39135 cp_parser_omp_declare_simd (parser, pragma_tok,
39136 context);
39137 return true;
39138 }
39139 cp_ensure_no_omp_declare_simd (parser);
39140 if (strcmp (p, "reduction") == 0)
39141 {
39142 cp_lexer_consume_token (parser->lexer);
39143 cp_parser_omp_declare_reduction (parser, pragma_tok,
39144 context);
39145 return false;
39146 }
39147 if (!flag_openmp) /* flag_openmp_simd */
39148 {
39149 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39150 return false;
39151 }
39152 if (strcmp (p, "target") == 0)
39153 {
39154 cp_lexer_consume_token (parser->lexer);
39155 cp_parser_omp_declare_target (parser, pragma_tok);
39156 return false;
39157 }
39158 }
39159 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39160 "or %<target%>");
39161 cp_parser_require_pragma_eol (parser, pragma_tok);
39162 return false;
39163 }
39164
39165 /* OpenMP 5.0
39166 #pragma omp requires clauses[optseq] new-line */
39167
39168 static bool
39169 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39170 {
39171 bool first = true;
39172 enum omp_requires new_req = (enum omp_requires) 0;
39173
39174 location_t loc = pragma_tok->location;
39175 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39176 {
39177 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39178 cp_lexer_consume_token (parser->lexer);
39179
39180 first = false;
39181
39182 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39183 {
39184 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39185 const char *p = IDENTIFIER_POINTER (id);
39186 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39187 enum omp_requires this_req = (enum omp_requires) 0;
39188
39189 if (!strcmp (p, "unified_address"))
39190 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39191 else if (!strcmp (p, "unified_shared_memory"))
39192 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39193 else if (!strcmp (p, "dynamic_allocators"))
39194 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39195 else if (!strcmp (p, "reverse_offload"))
39196 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39197 else if (!strcmp (p, "atomic_default_mem_order"))
39198 {
39199 cp_lexer_consume_token (parser->lexer);
39200
39201 matching_parens parens;
39202 if (parens.require_open (parser))
39203 {
39204 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39205 {
39206 id = cp_lexer_peek_token (parser->lexer)->u.value;
39207 p = IDENTIFIER_POINTER (id);
39208
39209 if (!strcmp (p, "seq_cst"))
39210 this_req
39211 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39212 else if (!strcmp (p, "relaxed"))
39213 this_req
39214 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39215 else if (!strcmp (p, "acq_rel"))
39216 this_req
39217 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39218 }
39219 if (this_req == 0)
39220 {
39221 error_at (cp_lexer_peek_token (parser->lexer)->location,
39222 "expected %<seq_cst%>, %<relaxed%> or "
39223 "%<acq_rel%>");
39224 if (cp_lexer_nth_token_is (parser->lexer, 2,
39225 CPP_CLOSE_PAREN))
39226 cp_lexer_consume_token (parser->lexer);
39227 }
39228 else
39229 cp_lexer_consume_token (parser->lexer);
39230
39231 if (!parens.require_close (parser))
39232 cp_parser_skip_to_closing_parenthesis (parser,
39233 /*recovering=*/true,
39234 /*or_comma=*/false,
39235 /*consume_paren=*/
39236 true);
39237
39238 if (this_req == 0)
39239 {
39240 cp_parser_require_pragma_eol (parser, pragma_tok);
39241 return false;
39242 }
39243 }
39244 p = NULL;
39245 }
39246 else
39247 {
39248 error_at (cloc, "expected %<unified_address%>, "
39249 "%<unified_shared_memory%>, "
39250 "%<dynamic_allocators%>, "
39251 "%<reverse_offload%> "
39252 "or %<atomic_default_mem_order%> clause");
39253 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39254 return false;
39255 }
39256 if (p)
39257 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39258 "supported yet", p);
39259 if (p)
39260 cp_lexer_consume_token (parser->lexer);
39261 if (this_req)
39262 {
39263 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39264 {
39265 if ((this_req & new_req) != 0)
39266 error_at (cloc, "too many %qs clauses", p);
39267 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39268 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39269 error_at (cloc, "%qs clause used lexically after first "
39270 "target construct or offloading API", p);
39271 }
39272 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39273 {
39274 error_at (cloc, "too many %qs clauses",
39275 "atomic_default_mem_order");
39276 this_req = (enum omp_requires) 0;
39277 }
39278 else if ((omp_requires_mask
39279 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39280 {
39281 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39282 " clause in a single compilation unit");
39283 this_req
39284 = (enum omp_requires)
39285 (omp_requires_mask
39286 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39287 }
39288 else if ((omp_requires_mask
39289 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39290 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39291 "lexically after first %<atomic%> construct "
39292 "without memory order clause");
39293 new_req = (enum omp_requires) (new_req | this_req);
39294 omp_requires_mask
39295 = (enum omp_requires) (omp_requires_mask | this_req);
39296 continue;
39297 }
39298 }
39299 break;
39300 }
39301 cp_parser_require_pragma_eol (parser, pragma_tok);
39302
39303 if (new_req == 0)
39304 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39305 return false;
39306 }
39307
39308
39309 /* OpenMP 4.5:
39310 #pragma omp taskloop taskloop-clause[optseq] new-line
39311 for-loop
39312
39313 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39314 for-loop */
39315
39316 #define OMP_TASKLOOP_CLAUSE_MASK \
39317 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39333
39334 static tree
39335 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39336 char *p_name, omp_clause_mask mask, tree *cclauses,
39337 bool *if_p)
39338 {
39339 tree clauses, sb, ret;
39340 unsigned int save;
39341 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39342
39343 strcat (p_name, " taskloop");
39344 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39345 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39346 clause. */
39347 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39348 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39349
39350 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39351 {
39352 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39353 const char *p = IDENTIFIER_POINTER (id);
39354
39355 if (strcmp (p, "simd") == 0)
39356 {
39357 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39358 if (cclauses == NULL)
39359 cclauses = cclauses_buf;
39360
39361 cp_lexer_consume_token (parser->lexer);
39362 if (!flag_openmp) /* flag_openmp_simd */
39363 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39364 cclauses, if_p);
39365 sb = begin_omp_structured_block ();
39366 save = cp_parser_begin_omp_structured_block (parser);
39367 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39368 cclauses, if_p);
39369 cp_parser_end_omp_structured_block (parser, save);
39370 tree body = finish_omp_structured_block (sb);
39371 if (ret == NULL)
39372 return ret;
39373 ret = make_node (OMP_TASKLOOP);
39374 TREE_TYPE (ret) = void_type_node;
39375 OMP_FOR_BODY (ret) = body;
39376 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39377 SET_EXPR_LOCATION (ret, loc);
39378 add_stmt (ret);
39379 return ret;
39380 }
39381 }
39382 if (!flag_openmp) /* flag_openmp_simd */
39383 {
39384 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39385 return NULL_TREE;
39386 }
39387
39388 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39389 cclauses == NULL);
39390 if (cclauses)
39391 {
39392 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39393 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39394 }
39395
39396 keep_next_level (true);
39397 sb = begin_omp_structured_block ();
39398 save = cp_parser_begin_omp_structured_block (parser);
39399
39400 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39401 if_p);
39402
39403 cp_parser_end_omp_structured_block (parser, save);
39404 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39405
39406 return ret;
39407 }
39408
39409
39410 /* OpenACC 2.0:
39411 # pragma acc routine oacc-routine-clause[optseq] new-line
39412 function-definition
39413
39414 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39415 */
39416
39417 #define OACC_ROUTINE_CLAUSE_MASK \
39418 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39422
39423
39424 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39425 component, which must resolve to a declared namespace-scope
39426 function. The clauses are either processed directly (for a named
39427 function), or defered until the immediatley following declaration
39428 is parsed. */
39429
39430 static void
39431 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39432 enum pragma_context context)
39433 {
39434 gcc_checking_assert (context == pragma_external);
39435 /* The checking for "another pragma following this one" in the "no optional
39436 '( name )'" case makes sure that we dont re-enter. */
39437 gcc_checking_assert (parser->oacc_routine == NULL);
39438
39439 cp_oacc_routine_data data;
39440 data.error_seen = false;
39441 data.fndecl_seen = false;
39442 data.tokens = vNULL;
39443 data.clauses = NULL_TREE;
39444 data.loc = pragma_tok->location;
39445 /* It is safe to take the address of a local variable; it will only be
39446 used while this scope is live. */
39447 parser->oacc_routine = &data;
39448
39449 /* Look for optional '( name )'. */
39450 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39451 {
39452 matching_parens parens;
39453 parens.consume_open (parser); /* '(' */
39454
39455 /* We parse the name as an id-expression. If it resolves to
39456 anything other than a non-overloaded function at namespace
39457 scope, it's an error. */
39458 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
39459 tree name = cp_parser_id_expression (parser,
39460 /*template_keyword_p=*/false,
39461 /*check_dependency_p=*/false,
39462 /*template_p=*/NULL,
39463 /*declarator_p=*/false,
39464 /*optional_p=*/false);
39465 tree decl = (identifier_p (name)
39466 ? cp_parser_lookup_name_simple (parser, name, name_loc)
39467 : name);
39468 if (name != error_mark_node && decl == error_mark_node)
39469 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
39470
39471 if (decl == error_mark_node
39472 || !parens.require_close (parser))
39473 {
39474 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39475 parser->oacc_routine = NULL;
39476 return;
39477 }
39478
39479 data.clauses
39480 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39481 "#pragma acc routine",
39482 cp_lexer_peek_token (parser->lexer));
39483
39484 if (decl && is_overloaded_fn (decl)
39485 && (TREE_CODE (decl) != FUNCTION_DECL
39486 || DECL_FUNCTION_TEMPLATE_P (decl)))
39487 {
39488 error_at (name_loc,
39489 "%<#pragma acc routine%> names a set of overloads");
39490 parser->oacc_routine = NULL;
39491 return;
39492 }
39493
39494 /* Perhaps we should use the same rule as declarations in different
39495 namespaces? */
39496 if (!DECL_NAMESPACE_SCOPE_P (decl))
39497 {
39498 error_at (name_loc,
39499 "%qD does not refer to a namespace scope function", decl);
39500 parser->oacc_routine = NULL;
39501 return;
39502 }
39503
39504 if (TREE_CODE (decl) != FUNCTION_DECL)
39505 {
39506 error_at (name_loc, "%qD does not refer to a function", decl);
39507 parser->oacc_routine = NULL;
39508 return;
39509 }
39510
39511 cp_finalize_oacc_routine (parser, decl, false);
39512 parser->oacc_routine = NULL;
39513 }
39514 else /* No optional '( name )'. */
39515 {
39516 /* Store away all pragma tokens. */
39517 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39518 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39519 cp_lexer_consume_token (parser->lexer);
39520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39521 parser->oacc_routine->error_seen = true;
39522 cp_parser_require_pragma_eol (parser, pragma_tok);
39523 struct cp_token_cache *cp
39524 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39525 parser->oacc_routine->tokens.safe_push (cp);
39526
39527 /* Emit a helpful diagnostic if there's another pragma following this
39528 one. */
39529 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39530 {
39531 cp_ensure_no_oacc_routine (parser);
39532 data.tokens.release ();
39533 /* ..., and then just keep going. */
39534 return;
39535 }
39536
39537 /* We only have to consider the pragma_external case here. */
39538 cp_parser_declaration (parser);
39539 if (parser->oacc_routine
39540 && !parser->oacc_routine->fndecl_seen)
39541 cp_ensure_no_oacc_routine (parser);
39542 else
39543 parser->oacc_routine = NULL;
39544 data.tokens.release ();
39545 }
39546 }
39547
39548 /* Finalize #pragma acc routine clauses after direct declarator has
39549 been parsed. */
39550
39551 static tree
39552 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
39553 {
39554 struct cp_token_cache *ce;
39555 cp_oacc_routine_data *data = parser->oacc_routine;
39556
39557 if (!data->error_seen && data->fndecl_seen)
39558 {
39559 error_at (data->loc,
39560 "%<#pragma acc routine%> not immediately followed by "
39561 "a single function declaration or definition");
39562 data->error_seen = true;
39563 }
39564 if (data->error_seen)
39565 return attrs;
39566
39567 gcc_checking_assert (data->tokens.length () == 1);
39568 ce = data->tokens[0];
39569
39570 cp_parser_push_lexer_for_tokens (parser, ce);
39571 parser->lexer->in_pragma = true;
39572 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39573
39574 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39575 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
39576 parser->oacc_routine->clauses
39577 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39578 "#pragma acc routine", pragma_tok);
39579 cp_parser_pop_lexer (parser);
39580 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39581 fndecl_seen. */
39582
39583 return attrs;
39584 }
39585
39586 /* Apply any saved OpenACC routine clauses to a just-parsed
39587 declaration. */
39588
39589 static void
39590 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
39591 {
39592 if (__builtin_expect (parser->oacc_routine != NULL, 0))
39593 {
39594 /* Keep going if we're in error reporting mode. */
39595 if (parser->oacc_routine->error_seen
39596 || fndecl == error_mark_node)
39597 return;
39598
39599 if (parser->oacc_routine->fndecl_seen)
39600 {
39601 error_at (parser->oacc_routine->loc,
39602 "%<#pragma acc routine%> not immediately followed by"
39603 " a single function declaration or definition");
39604 parser->oacc_routine = NULL;
39605 return;
39606 }
39607 if (TREE_CODE (fndecl) != FUNCTION_DECL)
39608 {
39609 cp_ensure_no_oacc_routine (parser);
39610 return;
39611 }
39612
39613 if (oacc_get_fn_attrib (fndecl))
39614 {
39615 error_at (parser->oacc_routine->loc,
39616 "%<#pragma acc routine%> already applied to %qD", fndecl);
39617 parser->oacc_routine = NULL;
39618 return;
39619 }
39620
39621 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
39622 {
39623 error_at (parser->oacc_routine->loc,
39624 TREE_USED (fndecl)
39625 ? G_("%<#pragma acc routine%> must be applied before use")
39626 : G_("%<#pragma acc routine%> must be applied before "
39627 "definition"));
39628 parser->oacc_routine = NULL;
39629 return;
39630 }
39631
39632 /* Process the routine's dimension clauses. */
39633 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
39634 oacc_replace_fn_attrib (fndecl, dims);
39635
39636 /* Add an "omp declare target" attribute. */
39637 DECL_ATTRIBUTES (fndecl)
39638 = tree_cons (get_identifier ("omp declare target"),
39639 NULL_TREE, DECL_ATTRIBUTES (fndecl));
39640
39641 /* Don't unset parser->oacc_routine here: we may still need it to
39642 diagnose wrong usage. But, remember that we've used this "#pragma acc
39643 routine". */
39644 parser->oacc_routine->fndecl_seen = true;
39645 }
39646 }
39647
39648 /* Main entry point to OpenMP statement pragmas. */
39649
39650 static void
39651 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39652 {
39653 tree stmt;
39654 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
39655 omp_clause_mask mask (0);
39656
39657 switch (cp_parser_pragma_kind (pragma_tok))
39658 {
39659 case PRAGMA_OACC_ATOMIC:
39660 cp_parser_omp_atomic (parser, pragma_tok);
39661 return;
39662 case PRAGMA_OACC_CACHE:
39663 stmt = cp_parser_oacc_cache (parser, pragma_tok);
39664 break;
39665 case PRAGMA_OACC_DATA:
39666 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
39667 break;
39668 case PRAGMA_OACC_ENTER_DATA:
39669 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
39670 break;
39671 case PRAGMA_OACC_EXIT_DATA:
39672 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
39673 break;
39674 case PRAGMA_OACC_HOST_DATA:
39675 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
39676 break;
39677 case PRAGMA_OACC_KERNELS:
39678 case PRAGMA_OACC_PARALLEL:
39679 strcpy (p_name, "#pragma acc");
39680 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
39681 if_p);
39682 break;
39683 case PRAGMA_OACC_LOOP:
39684 strcpy (p_name, "#pragma acc");
39685 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
39686 if_p);
39687 break;
39688 case PRAGMA_OACC_UPDATE:
39689 stmt = cp_parser_oacc_update (parser, pragma_tok);
39690 break;
39691 case PRAGMA_OACC_WAIT:
39692 stmt = cp_parser_oacc_wait (parser, pragma_tok);
39693 break;
39694 case PRAGMA_OMP_ATOMIC:
39695 cp_parser_omp_atomic (parser, pragma_tok);
39696 return;
39697 case PRAGMA_OMP_CRITICAL:
39698 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
39699 break;
39700 case PRAGMA_OMP_DISTRIBUTE:
39701 strcpy (p_name, "#pragma omp");
39702 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
39703 if_p);
39704 break;
39705 case PRAGMA_OMP_FOR:
39706 strcpy (p_name, "#pragma omp");
39707 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
39708 if_p);
39709 break;
39710 case PRAGMA_OMP_MASTER:
39711 strcpy (p_name, "#pragma omp");
39712 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
39713 if_p);
39714 break;
39715 case PRAGMA_OMP_PARALLEL:
39716 strcpy (p_name, "#pragma omp");
39717 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
39718 if_p);
39719 break;
39720 case PRAGMA_OMP_SECTIONS:
39721 strcpy (p_name, "#pragma omp");
39722 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
39723 break;
39724 case PRAGMA_OMP_SIMD:
39725 strcpy (p_name, "#pragma omp");
39726 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
39727 if_p);
39728 break;
39729 case PRAGMA_OMP_SINGLE:
39730 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
39731 break;
39732 case PRAGMA_OMP_TASK:
39733 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
39734 break;
39735 case PRAGMA_OMP_TASKGROUP:
39736 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
39737 break;
39738 case PRAGMA_OMP_TASKLOOP:
39739 strcpy (p_name, "#pragma omp");
39740 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
39741 if_p);
39742 break;
39743 case PRAGMA_OMP_TEAMS:
39744 strcpy (p_name, "#pragma omp");
39745 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
39746 if_p);
39747 break;
39748 default:
39749 gcc_unreachable ();
39750 }
39751
39752 protected_set_expr_location (stmt, pragma_tok->location);
39753 }
39754 \f
39755 /* Transactional Memory parsing routines. */
39756
39757 /* Parse a transaction attribute.
39758
39759 txn-attribute:
39760 attribute
39761 [ [ identifier ] ]
39762
39763 We use this instead of cp_parser_attributes_opt for transactions to avoid
39764 the pedwarn in C++98 mode. */
39765
39766 static tree
39767 cp_parser_txn_attribute_opt (cp_parser *parser)
39768 {
39769 cp_token *token;
39770 tree attr_name, attr = NULL;
39771
39772 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
39773 return cp_parser_attributes_opt (parser);
39774
39775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
39776 return NULL_TREE;
39777 cp_lexer_consume_token (parser->lexer);
39778 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
39779 goto error1;
39780
39781 token = cp_lexer_peek_token (parser->lexer);
39782 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
39783 {
39784 token = cp_lexer_consume_token (parser->lexer);
39785
39786 attr_name = (token->type == CPP_KEYWORD
39787 /* For keywords, use the canonical spelling,
39788 not the parsed identifier. */
39789 ? ridpointers[(int) token->keyword]
39790 : token->u.value);
39791 attr = build_tree_list (attr_name, NULL_TREE);
39792 }
39793 else
39794 cp_parser_error (parser, "expected identifier");
39795
39796 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39797 error1:
39798 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39799 return attr;
39800 }
39801
39802 /* Parse a __transaction_atomic or __transaction_relaxed statement.
39803
39804 transaction-statement:
39805 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
39806 compound-statement
39807 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
39808 */
39809
39810 static tree
39811 cp_parser_transaction (cp_parser *parser, cp_token *token)
39812 {
39813 unsigned char old_in = parser->in_transaction;
39814 unsigned char this_in = 1, new_in;
39815 enum rid keyword = token->keyword;
39816 tree stmt, attrs, noex;
39817
39818 cp_lexer_consume_token (parser->lexer);
39819
39820 if (keyword == RID_TRANSACTION_RELAXED
39821 || keyword == RID_SYNCHRONIZED)
39822 this_in |= TM_STMT_ATTR_RELAXED;
39823 else
39824 {
39825 attrs = cp_parser_txn_attribute_opt (parser);
39826 if (attrs)
39827 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
39828 }
39829
39830 /* Parse a noexcept specification. */
39831 if (keyword == RID_ATOMIC_NOEXCEPT)
39832 noex = boolean_true_node;
39833 else if (keyword == RID_ATOMIC_CANCEL)
39834 {
39835 /* cancel-and-throw is unimplemented. */
39836 sorry ("atomic_cancel");
39837 noex = NULL_TREE;
39838 }
39839 else
39840 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
39841
39842 /* Keep track if we're in the lexical scope of an outer transaction. */
39843 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
39844
39845 stmt = begin_transaction_stmt (token->location, NULL, this_in);
39846
39847 parser->in_transaction = new_in;
39848 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
39849 parser->in_transaction = old_in;
39850
39851 finish_transaction_stmt (stmt, NULL, this_in, noex);
39852
39853 return stmt;
39854 }
39855
39856 /* Parse a __transaction_atomic or __transaction_relaxed expression.
39857
39858 transaction-expression:
39859 __transaction_atomic txn-noexcept-spec[opt] ( expression )
39860 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
39861 */
39862
39863 static tree
39864 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
39865 {
39866 unsigned char old_in = parser->in_transaction;
39867 unsigned char this_in = 1;
39868 cp_token *token;
39869 tree expr, noex;
39870 bool noex_expr;
39871 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39872
39873 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
39874 || keyword == RID_TRANSACTION_RELAXED);
39875
39876 if (!flag_tm)
39877 error_at (loc,
39878 keyword == RID_TRANSACTION_RELAXED
39879 ? G_("%<__transaction_relaxed%> without transactional memory "
39880 "support enabled")
39881 : G_("%<__transaction_atomic%> without transactional memory "
39882 "support enabled"));
39883
39884 token = cp_parser_require_keyword (parser, keyword,
39885 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
39886 : RT_TRANSACTION_RELAXED));
39887 gcc_assert (token != NULL);
39888
39889 if (keyword == RID_TRANSACTION_RELAXED)
39890 this_in |= TM_STMT_ATTR_RELAXED;
39891
39892 /* Set this early. This might mean that we allow transaction_cancel in
39893 an expression that we find out later actually has to be a constexpr.
39894 However, we expect that cxx_constant_value will be able to deal with
39895 this; also, if the noexcept has no constexpr, then what we parse next
39896 really is a transaction's body. */
39897 parser->in_transaction = this_in;
39898
39899 /* Parse a noexcept specification. */
39900 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
39901 true);
39902
39903 if (!noex || !noex_expr
39904 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39905 {
39906 matching_parens parens;
39907 parens.require_open (parser);
39908
39909 expr = cp_parser_expression (parser);
39910 expr = finish_parenthesized_expr (expr);
39911
39912 parens.require_close (parser);
39913 }
39914 else
39915 {
39916 /* The only expression that is available got parsed for the noexcept
39917 already. noexcept is true then. */
39918 expr = noex;
39919 noex = boolean_true_node;
39920 }
39921
39922 expr = build_transaction_expr (token->location, expr, this_in, noex);
39923 parser->in_transaction = old_in;
39924
39925 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
39926 return error_mark_node;
39927
39928 return (flag_tm ? expr : error_mark_node);
39929 }
39930
39931 /* Parse a function-transaction-block.
39932
39933 function-transaction-block:
39934 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
39935 function-body
39936 __transaction_atomic txn-attribute[opt] function-try-block
39937 __transaction_relaxed ctor-initializer[opt] function-body
39938 __transaction_relaxed function-try-block
39939 */
39940
39941 static void
39942 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
39943 {
39944 unsigned char old_in = parser->in_transaction;
39945 unsigned char new_in = 1;
39946 tree compound_stmt, stmt, attrs;
39947 cp_token *token;
39948
39949 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
39950 || keyword == RID_TRANSACTION_RELAXED);
39951 token = cp_parser_require_keyword (parser, keyword,
39952 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
39953 : RT_TRANSACTION_RELAXED));
39954 gcc_assert (token != NULL);
39955
39956 if (keyword == RID_TRANSACTION_RELAXED)
39957 new_in |= TM_STMT_ATTR_RELAXED;
39958 else
39959 {
39960 attrs = cp_parser_txn_attribute_opt (parser);
39961 if (attrs)
39962 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
39963 }
39964
39965 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
39966
39967 parser->in_transaction = new_in;
39968
39969 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
39970 cp_parser_function_try_block (parser);
39971 else
39972 cp_parser_ctor_initializer_opt_and_function_body
39973 (parser, /*in_function_try_block=*/false);
39974
39975 parser->in_transaction = old_in;
39976
39977 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
39978 }
39979
39980 /* Parse a __transaction_cancel statement.
39981
39982 cancel-statement:
39983 __transaction_cancel txn-attribute[opt] ;
39984 __transaction_cancel txn-attribute[opt] throw-expression ;
39985
39986 ??? Cancel and throw is not yet implemented. */
39987
39988 static tree
39989 cp_parser_transaction_cancel (cp_parser *parser)
39990 {
39991 cp_token *token;
39992 bool is_outer = false;
39993 tree stmt, attrs;
39994
39995 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
39996 RT_TRANSACTION_CANCEL);
39997 gcc_assert (token != NULL);
39998
39999 attrs = cp_parser_txn_attribute_opt (parser);
40000 if (attrs)
40001 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40002
40003 /* ??? Parse cancel-and-throw here. */
40004
40005 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40006
40007 if (!flag_tm)
40008 {
40009 error_at (token->location, "%<__transaction_cancel%> without "
40010 "transactional memory support enabled");
40011 return error_mark_node;
40012 }
40013 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40014 {
40015 error_at (token->location, "%<__transaction_cancel%> within a "
40016 "%<__transaction_relaxed%>");
40017 return error_mark_node;
40018 }
40019 else if (is_outer)
40020 {
40021 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40022 && !is_tm_may_cancel_outer (current_function_decl))
40023 {
40024 error_at (token->location, "outer %<__transaction_cancel%> not "
40025 "within outer %<__transaction_atomic%>");
40026 error_at (token->location,
40027 " or a %<transaction_may_cancel_outer%> function");
40028 return error_mark_node;
40029 }
40030 }
40031 else if (parser->in_transaction == 0)
40032 {
40033 error_at (token->location, "%<__transaction_cancel%> not within "
40034 "%<__transaction_atomic%>");
40035 return error_mark_node;
40036 }
40037
40038 stmt = build_tm_abort_call (token->location, is_outer);
40039 add_stmt (stmt);
40040
40041 return stmt;
40042 }
40043 \f
40044 /* The parser. */
40045
40046 static GTY (()) cp_parser *the_parser;
40047
40048 \f
40049 /* Special handling for the first token or line in the file. The first
40050 thing in the file might be #pragma GCC pch_preprocess, which loads a
40051 PCH file, which is a GC collection point. So we need to handle this
40052 first pragma without benefit of an existing lexer structure.
40053
40054 Always returns one token to the caller in *FIRST_TOKEN. This is
40055 either the true first token of the file, or the first token after
40056 the initial pragma. */
40057
40058 static void
40059 cp_parser_initial_pragma (cp_token *first_token)
40060 {
40061 tree name = NULL;
40062
40063 cp_lexer_get_preprocessor_token (NULL, first_token);
40064 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40065 return;
40066
40067 cp_lexer_get_preprocessor_token (NULL, first_token);
40068 if (first_token->type == CPP_STRING)
40069 {
40070 name = first_token->u.value;
40071
40072 cp_lexer_get_preprocessor_token (NULL, first_token);
40073 if (first_token->type != CPP_PRAGMA_EOL)
40074 error_at (first_token->location,
40075 "junk at end of %<#pragma GCC pch_preprocess%>");
40076 }
40077 else
40078 error_at (first_token->location, "expected string literal");
40079
40080 /* Skip to the end of the pragma. */
40081 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40082 cp_lexer_get_preprocessor_token (NULL, first_token);
40083
40084 /* Now actually load the PCH file. */
40085 if (name)
40086 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40087
40088 /* Read one more token to return to our caller. We have to do this
40089 after reading the PCH file in, since its pointers have to be
40090 live. */
40091 cp_lexer_get_preprocessor_token (NULL, first_token);
40092 }
40093
40094 /* Parse a pragma GCC ivdep. */
40095
40096 static bool
40097 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40098 {
40099 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40100 return true;
40101 }
40102
40103 /* Parse a pragma GCC unroll. */
40104
40105 static unsigned short
40106 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40107 {
40108 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40109 tree expr = cp_parser_constant_expression (parser);
40110 unsigned short unroll;
40111 expr = maybe_constant_value (expr);
40112 HOST_WIDE_INT lunroll = 0;
40113 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40114 || TREE_CODE (expr) != INTEGER_CST
40115 || (lunroll = tree_to_shwi (expr)) < 0
40116 || lunroll >= USHRT_MAX)
40117 {
40118 error_at (location, "%<#pragma GCC unroll%> requires an"
40119 " assignment-expression that evaluates to a non-negative"
40120 " integral constant less than %u", USHRT_MAX);
40121 unroll = 0;
40122 }
40123 else
40124 {
40125 unroll = (unsigned short)lunroll;
40126 if (unroll == 0)
40127 unroll = 1;
40128 }
40129 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40130 return unroll;
40131 }
40132
40133 /* Normal parsing of a pragma token. Here we can (and must) use the
40134 regular lexer. */
40135
40136 static bool
40137 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40138 {
40139 cp_token *pragma_tok;
40140 unsigned int id;
40141 tree stmt;
40142 bool ret;
40143
40144 pragma_tok = cp_lexer_consume_token (parser->lexer);
40145 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40146 parser->lexer->in_pragma = true;
40147
40148 id = cp_parser_pragma_kind (pragma_tok);
40149 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40150 cp_ensure_no_omp_declare_simd (parser);
40151 switch (id)
40152 {
40153 case PRAGMA_GCC_PCH_PREPROCESS:
40154 error_at (pragma_tok->location,
40155 "%<#pragma GCC pch_preprocess%> must be first");
40156 break;
40157
40158 case PRAGMA_OMP_BARRIER:
40159 switch (context)
40160 {
40161 case pragma_compound:
40162 cp_parser_omp_barrier (parser, pragma_tok);
40163 return false;
40164 case pragma_stmt:
40165 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40166 "used in compound statements", "omp barrier");
40167 break;
40168 default:
40169 goto bad_stmt;
40170 }
40171 break;
40172
40173 case PRAGMA_OMP_DEPOBJ:
40174 switch (context)
40175 {
40176 case pragma_compound:
40177 cp_parser_omp_depobj (parser, pragma_tok);
40178 return false;
40179 case pragma_stmt:
40180 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40181 "used in compound statements", "omp depobj");
40182 break;
40183 default:
40184 goto bad_stmt;
40185 }
40186 break;
40187
40188 case PRAGMA_OMP_FLUSH:
40189 switch (context)
40190 {
40191 case pragma_compound:
40192 cp_parser_omp_flush (parser, pragma_tok);
40193 return false;
40194 case pragma_stmt:
40195 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40196 "used in compound statements", "omp flush");
40197 break;
40198 default:
40199 goto bad_stmt;
40200 }
40201 break;
40202
40203 case PRAGMA_OMP_TASKWAIT:
40204 switch (context)
40205 {
40206 case pragma_compound:
40207 cp_parser_omp_taskwait (parser, pragma_tok);
40208 return false;
40209 case pragma_stmt:
40210 error_at (pragma_tok->location,
40211 "%<#pragma %s%> may only be used in compound statements",
40212 "omp taskwait");
40213 break;
40214 default:
40215 goto bad_stmt;
40216 }
40217 break;
40218
40219 case PRAGMA_OMP_TASKYIELD:
40220 switch (context)
40221 {
40222 case pragma_compound:
40223 cp_parser_omp_taskyield (parser, pragma_tok);
40224 return false;
40225 case pragma_stmt:
40226 error_at (pragma_tok->location,
40227 "%<#pragma %s%> may only be used in compound statements",
40228 "omp taskyield");
40229 break;
40230 default:
40231 goto bad_stmt;
40232 }
40233 break;
40234
40235 case PRAGMA_OMP_CANCEL:
40236 switch (context)
40237 {
40238 case pragma_compound:
40239 cp_parser_omp_cancel (parser, pragma_tok);
40240 return false;
40241 case pragma_stmt:
40242 error_at (pragma_tok->location,
40243 "%<#pragma %s%> may only be used in compound statements",
40244 "omp cancel");
40245 break;
40246 default:
40247 goto bad_stmt;
40248 }
40249 break;
40250
40251 case PRAGMA_OMP_CANCELLATION_POINT:
40252 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40253 return false;
40254
40255 case PRAGMA_OMP_THREADPRIVATE:
40256 cp_parser_omp_threadprivate (parser, pragma_tok);
40257 return false;
40258
40259 case PRAGMA_OMP_DECLARE:
40260 return cp_parser_omp_declare (parser, pragma_tok, context);
40261
40262 case PRAGMA_OACC_DECLARE:
40263 cp_parser_oacc_declare (parser, pragma_tok);
40264 return false;
40265
40266 case PRAGMA_OACC_ENTER_DATA:
40267 if (context == pragma_stmt)
40268 {
40269 error_at (pragma_tok->location,
40270 "%<#pragma %s%> may only be used in compound statements",
40271 "acc enter data");
40272 break;
40273 }
40274 else if (context != pragma_compound)
40275 goto bad_stmt;
40276 cp_parser_omp_construct (parser, pragma_tok, if_p);
40277 return true;
40278
40279 case PRAGMA_OACC_EXIT_DATA:
40280 if (context == pragma_stmt)
40281 {
40282 error_at (pragma_tok->location,
40283 "%<#pragma %s%> may only be used in compound statements",
40284 "acc exit data");
40285 break;
40286 }
40287 else if (context != pragma_compound)
40288 goto bad_stmt;
40289 cp_parser_omp_construct (parser, pragma_tok, if_p);
40290 return true;
40291
40292 case PRAGMA_OACC_ROUTINE:
40293 if (context != pragma_external)
40294 {
40295 error_at (pragma_tok->location,
40296 "%<#pragma acc routine%> must be at file scope");
40297 break;
40298 }
40299 cp_parser_oacc_routine (parser, pragma_tok, context);
40300 return false;
40301
40302 case PRAGMA_OACC_UPDATE:
40303 if (context == pragma_stmt)
40304 {
40305 error_at (pragma_tok->location,
40306 "%<#pragma %s%> may only be used in compound statements",
40307 "acc update");
40308 break;
40309 }
40310 else if (context != pragma_compound)
40311 goto bad_stmt;
40312 cp_parser_omp_construct (parser, pragma_tok, if_p);
40313 return true;
40314
40315 case PRAGMA_OACC_WAIT:
40316 if (context == pragma_stmt)
40317 {
40318 error_at (pragma_tok->location,
40319 "%<#pragma %s%> may only be used in compound statements",
40320 "acc wait");
40321 break;
40322 }
40323 else if (context != pragma_compound)
40324 goto bad_stmt;
40325 cp_parser_omp_construct (parser, pragma_tok, if_p);
40326 return true;
40327
40328 case PRAGMA_OACC_ATOMIC:
40329 case PRAGMA_OACC_CACHE:
40330 case PRAGMA_OACC_DATA:
40331 case PRAGMA_OACC_HOST_DATA:
40332 case PRAGMA_OACC_KERNELS:
40333 case PRAGMA_OACC_PARALLEL:
40334 case PRAGMA_OACC_LOOP:
40335 case PRAGMA_OMP_ATOMIC:
40336 case PRAGMA_OMP_CRITICAL:
40337 case PRAGMA_OMP_DISTRIBUTE:
40338 case PRAGMA_OMP_FOR:
40339 case PRAGMA_OMP_MASTER:
40340 case PRAGMA_OMP_PARALLEL:
40341 case PRAGMA_OMP_SECTIONS:
40342 case PRAGMA_OMP_SIMD:
40343 case PRAGMA_OMP_SINGLE:
40344 case PRAGMA_OMP_TASK:
40345 case PRAGMA_OMP_TASKGROUP:
40346 case PRAGMA_OMP_TASKLOOP:
40347 case PRAGMA_OMP_TEAMS:
40348 if (context != pragma_stmt && context != pragma_compound)
40349 goto bad_stmt;
40350 stmt = push_omp_privatization_clauses (false);
40351 cp_parser_omp_construct (parser, pragma_tok, if_p);
40352 pop_omp_privatization_clauses (stmt);
40353 return true;
40354
40355 case PRAGMA_OMP_REQUIRES:
40356 return cp_parser_omp_requires (parser, pragma_tok);
40357
40358 case PRAGMA_OMP_ORDERED:
40359 if (context != pragma_stmt && context != pragma_compound)
40360 goto bad_stmt;
40361 stmt = push_omp_privatization_clauses (false);
40362 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40363 pop_omp_privatization_clauses (stmt);
40364 return ret;
40365
40366 case PRAGMA_OMP_TARGET:
40367 if (context != pragma_stmt && context != pragma_compound)
40368 goto bad_stmt;
40369 stmt = push_omp_privatization_clauses (false);
40370 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40371 pop_omp_privatization_clauses (stmt);
40372 return ret;
40373
40374 case PRAGMA_OMP_END_DECLARE_TARGET:
40375 cp_parser_omp_end_declare_target (parser, pragma_tok);
40376 return false;
40377
40378 case PRAGMA_OMP_SECTION:
40379 error_at (pragma_tok->location,
40380 "%<#pragma omp section%> may only be used in "
40381 "%<#pragma omp sections%> construct");
40382 break;
40383
40384 case PRAGMA_IVDEP:
40385 {
40386 if (context == pragma_external)
40387 {
40388 error_at (pragma_tok->location,
40389 "%<#pragma GCC ivdep%> must be inside a function");
40390 break;
40391 }
40392 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40393 unsigned short unroll;
40394 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40395 if (tok->type == CPP_PRAGMA
40396 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40397 {
40398 tok = cp_lexer_consume_token (parser->lexer);
40399 unroll = cp_parser_pragma_unroll (parser, tok);
40400 tok = cp_lexer_peek_token (the_parser->lexer);
40401 }
40402 else
40403 unroll = 0;
40404 if (tok->type != CPP_KEYWORD
40405 || (tok->keyword != RID_FOR
40406 && tok->keyword != RID_WHILE
40407 && tok->keyword != RID_DO))
40408 {
40409 cp_parser_error (parser, "for, while or do statement expected");
40410 return false;
40411 }
40412 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40413 return true;
40414 }
40415
40416 case PRAGMA_UNROLL:
40417 {
40418 if (context == pragma_external)
40419 {
40420 error_at (pragma_tok->location,
40421 "%<#pragma GCC unroll%> must be inside a function");
40422 break;
40423 }
40424 const unsigned short unroll
40425 = cp_parser_pragma_unroll (parser, pragma_tok);
40426 bool ivdep;
40427 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40428 if (tok->type == CPP_PRAGMA
40429 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40430 {
40431 tok = cp_lexer_consume_token (parser->lexer);
40432 ivdep = cp_parser_pragma_ivdep (parser, tok);
40433 tok = cp_lexer_peek_token (the_parser->lexer);
40434 }
40435 else
40436 ivdep = false;
40437 if (tok->type != CPP_KEYWORD
40438 || (tok->keyword != RID_FOR
40439 && tok->keyword != RID_WHILE
40440 && tok->keyword != RID_DO))
40441 {
40442 cp_parser_error (parser, "for, while or do statement expected");
40443 return false;
40444 }
40445 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40446 return true;
40447 }
40448
40449 default:
40450 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
40451 c_invoke_pragma_handler (id);
40452 break;
40453
40454 bad_stmt:
40455 cp_parser_error (parser, "expected declaration specifiers");
40456 break;
40457 }
40458
40459 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40460 return false;
40461 }
40462
40463 /* The interface the pragma parsers have to the lexer. */
40464
40465 enum cpp_ttype
40466 pragma_lex (tree *value, location_t *loc)
40467 {
40468 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40469 enum cpp_ttype ret = tok->type;
40470
40471 *value = tok->u.value;
40472 if (loc)
40473 *loc = tok->location;
40474
40475 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
40476 ret = CPP_EOF;
40477 else if (ret == CPP_STRING)
40478 *value = cp_parser_string_literal (the_parser, false, false);
40479 else
40480 {
40481 if (ret == CPP_KEYWORD)
40482 ret = CPP_NAME;
40483 cp_lexer_consume_token (the_parser->lexer);
40484 }
40485
40486 return ret;
40487 }
40488
40489 \f
40490 /* External interface. */
40491
40492 /* Parse one entire translation unit. */
40493
40494 void
40495 c_parse_file (void)
40496 {
40497 static bool already_called = false;
40498
40499 if (already_called)
40500 fatal_error (input_location,
40501 "inter-module optimizations not implemented for C++");
40502 already_called = true;
40503
40504 the_parser = cp_parser_new ();
40505 push_deferring_access_checks (flag_access_control
40506 ? dk_no_deferred : dk_no_check);
40507 cp_parser_translation_unit (the_parser);
40508 the_parser = NULL;
40509
40510 finish_translation_unit ();
40511 }
40512
40513 /* Create an identifier for a generic parameter type (a synthesized
40514 template parameter implied by `auto' or a concept identifier). */
40515
40516 static GTY(()) int generic_parm_count;
40517 static tree
40518 make_generic_type_name ()
40519 {
40520 char buf[32];
40521 sprintf (buf, "auto:%d", ++generic_parm_count);
40522 return get_identifier (buf);
40523 }
40524
40525 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40526 (creating a new template parameter list if necessary). Returns the newly
40527 created template type parm. */
40528
40529 static tree
40530 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
40531 {
40532 gcc_assert (current_binding_level->kind == sk_function_parms);
40533
40534 /* Before committing to modifying any scope, if we're in an
40535 implicit template scope, and we're trying to synthesize a
40536 constrained parameter, try to find a previous parameter with
40537 the same name. This is the same-type rule for abbreviated
40538 function templates.
40539
40540 NOTE: We can generate implicit parameters when tentatively
40541 parsing a nested name specifier, only to reject that parse
40542 later. However, matching the same template-id as part of a
40543 direct-declarator should generate an identical template
40544 parameter, so this rule will merge them. */
40545 if (parser->implicit_template_scope && constr)
40546 {
40547 tree t = parser->implicit_template_parms;
40548 while (t)
40549 {
40550 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
40551 {
40552 tree d = TREE_VALUE (t);
40553 if (TREE_CODE (d) == PARM_DECL)
40554 /* Return the TEMPLATE_PARM_INDEX. */
40555 d = DECL_INITIAL (d);
40556 return d;
40557 }
40558 t = TREE_CHAIN (t);
40559 }
40560 }
40561
40562 /* We are either continuing a function template that already contains implicit
40563 template parameters, creating a new fully-implicit function template, or
40564 extending an existing explicit function template with implicit template
40565 parameters. */
40566
40567 cp_binding_level *const entry_scope = current_binding_level;
40568
40569 bool become_template = false;
40570 cp_binding_level *parent_scope = 0;
40571
40572 if (parser->implicit_template_scope)
40573 {
40574 gcc_assert (parser->implicit_template_parms);
40575
40576 current_binding_level = parser->implicit_template_scope;
40577 }
40578 else
40579 {
40580 /* Roll back to the existing template parameter scope (in the case of
40581 extending an explicit function template) or introduce a new template
40582 parameter scope ahead of the function parameter scope (or class scope
40583 in the case of out-of-line member definitions). The function scope is
40584 added back after template parameter synthesis below. */
40585
40586 cp_binding_level *scope = entry_scope;
40587
40588 while (scope->kind == sk_function_parms)
40589 {
40590 parent_scope = scope;
40591 scope = scope->level_chain;
40592 }
40593 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
40594 {
40595 /* If not defining a class, then any class scope is a scope level in
40596 an out-of-line member definition. In this case simply wind back
40597 beyond the first such scope to inject the template parameter list.
40598 Otherwise wind back to the class being defined. The latter can
40599 occur in class member friend declarations such as:
40600
40601 class A {
40602 void foo (auto);
40603 };
40604 class B {
40605 friend void A::foo (auto);
40606 };
40607
40608 The template parameter list synthesized for the friend declaration
40609 must be injected in the scope of 'B'. This can also occur in
40610 erroneous cases such as:
40611
40612 struct A {
40613 struct B {
40614 void foo (auto);
40615 };
40616 void B::foo (auto) {}
40617 };
40618
40619 Here the attempted definition of 'B::foo' within 'A' is ill-formed
40620 but, nevertheless, the template parameter list synthesized for the
40621 declarator should be injected into the scope of 'A' as if the
40622 ill-formed template was specified explicitly. */
40623
40624 while (scope->kind == sk_class && !scope->defining_class_p)
40625 {
40626 parent_scope = scope;
40627 scope = scope->level_chain;
40628 }
40629 }
40630
40631 current_binding_level = scope;
40632
40633 if (scope->kind != sk_template_parms
40634 || !function_being_declared_is_template_p (parser))
40635 {
40636 /* Introduce a new template parameter list for implicit template
40637 parameters. */
40638
40639 become_template = true;
40640
40641 parser->implicit_template_scope
40642 = begin_scope (sk_template_parms, NULL);
40643
40644 ++processing_template_decl;
40645
40646 parser->fully_implicit_function_template_p = true;
40647 ++parser->num_template_parameter_lists;
40648 }
40649 else
40650 {
40651 /* Synthesize implicit template parameters at the end of the explicit
40652 template parameter list. */
40653
40654 gcc_assert (current_template_parms);
40655
40656 parser->implicit_template_scope = scope;
40657
40658 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40659 parser->implicit_template_parms
40660 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
40661 }
40662 }
40663
40664 /* Synthesize a new template parameter and track the current template
40665 parameter chain with implicit_template_parms. */
40666
40667 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
40668 tree synth_id = make_generic_type_name ();
40669 tree synth_tmpl_parm;
40670 bool non_type = false;
40671
40672 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
40673 synth_tmpl_parm
40674 = finish_template_type_parm (class_type_node, synth_id);
40675 else if (TREE_CODE (proto) == TEMPLATE_DECL)
40676 synth_tmpl_parm
40677 = finish_constrained_template_template_parm (proto, synth_id);
40678 else
40679 {
40680 synth_tmpl_parm = copy_decl (proto);
40681 DECL_NAME (synth_tmpl_parm) = synth_id;
40682 non_type = true;
40683 }
40684
40685 // Attach the constraint to the parm before processing.
40686 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
40687 TREE_TYPE (node) = constr;
40688 tree new_parm
40689 = process_template_parm (parser->implicit_template_parms,
40690 input_location,
40691 node,
40692 /*non_type=*/non_type,
40693 /*param_pack=*/false);
40694
40695 // Chain the new parameter to the list of implicit parameters.
40696 if (parser->implicit_template_parms)
40697 parser->implicit_template_parms
40698 = TREE_CHAIN (parser->implicit_template_parms);
40699 else
40700 parser->implicit_template_parms = new_parm;
40701
40702 tree new_decl = get_local_decls ();
40703 if (non_type)
40704 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
40705 new_decl = DECL_INITIAL (new_decl);
40706
40707 /* If creating a fully implicit function template, start the new implicit
40708 template parameter list with this synthesized type, otherwise grow the
40709 current template parameter list. */
40710
40711 if (become_template)
40712 {
40713 parent_scope->level_chain = current_binding_level;
40714
40715 tree new_parms = make_tree_vec (1);
40716 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
40717 current_template_parms = tree_cons (size_int (processing_template_decl),
40718 new_parms, current_template_parms);
40719 }
40720 else
40721 {
40722 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40723 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
40724 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
40725 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
40726 }
40727
40728 // If the new parameter was constrained, we need to add that to the
40729 // constraints in the template parameter list.
40730 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
40731 {
40732 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
40733 reqs = conjoin_constraints (reqs, req);
40734 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
40735 }
40736
40737 current_binding_level = entry_scope;
40738
40739 return new_decl;
40740 }
40741
40742 /* Finish the declaration of a fully implicit function template. Such a
40743 template has no explicit template parameter list so has not been through the
40744 normal template head and tail processing. synthesize_implicit_template_parm
40745 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
40746 provided if the declaration is a class member such that its template
40747 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
40748 form is returned. Otherwise NULL_TREE is returned. */
40749
40750 static tree
40751 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
40752 {
40753 gcc_assert (parser->fully_implicit_function_template_p);
40754
40755 if (member_decl_opt && member_decl_opt != error_mark_node
40756 && DECL_VIRTUAL_P (member_decl_opt))
40757 {
40758 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
40759 "implicit templates may not be %<virtual%>");
40760 DECL_VIRTUAL_P (member_decl_opt) = false;
40761 }
40762
40763 if (member_decl_opt)
40764 member_decl_opt = finish_member_template_decl (member_decl_opt);
40765 end_template_decl ();
40766
40767 parser->fully_implicit_function_template_p = false;
40768 parser->implicit_template_parms = 0;
40769 parser->implicit_template_scope = 0;
40770 --parser->num_template_parameter_lists;
40771
40772 return member_decl_opt;
40773 }
40774
40775 /* Like finish_fully_implicit_template, but to be used in error
40776 recovery, rearranging scopes so that we restore the state we had
40777 before synthesize_implicit_template_parm inserted the implement
40778 template parms scope. */
40779
40780 static void
40781 abort_fully_implicit_template (cp_parser *parser)
40782 {
40783 cp_binding_level *return_to_scope = current_binding_level;
40784
40785 if (parser->implicit_template_scope
40786 && return_to_scope != parser->implicit_template_scope)
40787 {
40788 cp_binding_level *child = return_to_scope;
40789 for (cp_binding_level *scope = child->level_chain;
40790 scope != parser->implicit_template_scope;
40791 scope = child->level_chain)
40792 child = scope;
40793 child->level_chain = parser->implicit_template_scope->level_chain;
40794 parser->implicit_template_scope->level_chain = return_to_scope;
40795 current_binding_level = parser->implicit_template_scope;
40796 }
40797 else
40798 return_to_scope = return_to_scope->level_chain;
40799
40800 finish_fully_implicit_template (parser, NULL);
40801
40802 gcc_assert (current_binding_level == return_to_scope);
40803 }
40804
40805 /* Helper function for diagnostics that have complained about things
40806 being used with 'extern "C"' linkage.
40807
40808 Attempt to issue a note showing where the 'extern "C"' linkage began. */
40809
40810 void
40811 maybe_show_extern_c_location (void)
40812 {
40813 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
40814 inform (the_parser->innermost_linkage_specification_location,
40815 "%<extern \"C\"%> linkage started here");
40816 }
40817
40818 #include "gt-cp-parser.h"