PR c++/87781 - detect invalid elaborated-type-specifier.
[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, location_t id_location)
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 declarator->id_loc = id_location;
1482
1483 return declarator;
1484 }
1485
1486 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1487 of modifiers such as const or volatile to apply to the pointer
1488 type, represented as identifiers. ATTRIBUTES represent the attributes that
1489 appertain to the pointer or reference. */
1490
1491 cp_declarator *
1492 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1493 tree attributes)
1494 {
1495 cp_declarator *declarator;
1496
1497 declarator = make_declarator (cdk_pointer);
1498 declarator->declarator = target;
1499 declarator->u.pointer.qualifiers = cv_qualifiers;
1500 declarator->u.pointer.class_type = NULL_TREE;
1501 if (target)
1502 {
1503 declarator->id_loc = target->id_loc;
1504 declarator->parameter_pack_p = target->parameter_pack_p;
1505 target->parameter_pack_p = false;
1506 }
1507 else
1508 declarator->parameter_pack_p = false;
1509
1510 declarator->std_attributes = attributes;
1511
1512 return declarator;
1513 }
1514
1515 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1516 represent the attributes that appertain to the pointer or
1517 reference. */
1518
1519 cp_declarator *
1520 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1521 bool rvalue_ref, tree attributes)
1522 {
1523 cp_declarator *declarator;
1524
1525 declarator = make_declarator (cdk_reference);
1526 declarator->declarator = target;
1527 declarator->u.reference.qualifiers = cv_qualifiers;
1528 declarator->u.reference.rvalue_ref = rvalue_ref;
1529 if (target)
1530 {
1531 declarator->id_loc = target->id_loc;
1532 declarator->parameter_pack_p = target->parameter_pack_p;
1533 target->parameter_pack_p = false;
1534 }
1535 else
1536 declarator->parameter_pack_p = false;
1537
1538 declarator->std_attributes = attributes;
1539
1540 return declarator;
1541 }
1542
1543 /* Like make_pointer_declarator -- but for a pointer to a non-static
1544 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1545 appertain to the pointer or reference. */
1546
1547 cp_declarator *
1548 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1549 cp_declarator *pointee,
1550 tree attributes)
1551 {
1552 cp_declarator *declarator;
1553
1554 declarator = make_declarator (cdk_ptrmem);
1555 declarator->declarator = pointee;
1556 declarator->u.pointer.qualifiers = cv_qualifiers;
1557 declarator->u.pointer.class_type = class_type;
1558
1559 if (pointee)
1560 {
1561 declarator->parameter_pack_p = pointee->parameter_pack_p;
1562 pointee->parameter_pack_p = false;
1563 }
1564 else
1565 declarator->parameter_pack_p = false;
1566
1567 declarator->std_attributes = attributes;
1568
1569 return declarator;
1570 }
1571
1572 /* Make a declarator for the function given by TARGET, with the
1573 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1574 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1575 indicates what exceptions can be thrown. */
1576
1577 cp_declarator *
1578 make_call_declarator (cp_declarator *target,
1579 tree parms,
1580 cp_cv_quals cv_qualifiers,
1581 cp_virt_specifiers virt_specifiers,
1582 cp_ref_qualifier ref_qualifier,
1583 tree tx_qualifier,
1584 tree exception_specification,
1585 tree late_return_type,
1586 tree requires_clause)
1587 {
1588 cp_declarator *declarator;
1589
1590 declarator = make_declarator (cdk_function);
1591 declarator->declarator = target;
1592 declarator->u.function.parameters = parms;
1593 declarator->u.function.qualifiers = cv_qualifiers;
1594 declarator->u.function.virt_specifiers = virt_specifiers;
1595 declarator->u.function.ref_qualifier = ref_qualifier;
1596 declarator->u.function.tx_qualifier = tx_qualifier;
1597 declarator->u.function.exception_specification = exception_specification;
1598 declarator->u.function.late_return_type = late_return_type;
1599 declarator->u.function.requires_clause = requires_clause;
1600 if (target)
1601 {
1602 declarator->id_loc = target->id_loc;
1603 declarator->parameter_pack_p = target->parameter_pack_p;
1604 target->parameter_pack_p = false;
1605 }
1606 else
1607 declarator->parameter_pack_p = false;
1608
1609 return declarator;
1610 }
1611
1612 /* Make a declarator for an array of BOUNDS elements, each of which is
1613 defined by ELEMENT. */
1614
1615 cp_declarator *
1616 make_array_declarator (cp_declarator *element, tree bounds)
1617 {
1618 cp_declarator *declarator;
1619
1620 declarator = make_declarator (cdk_array);
1621 declarator->declarator = element;
1622 declarator->u.array.bounds = bounds;
1623 if (element)
1624 {
1625 declarator->id_loc = element->id_loc;
1626 declarator->parameter_pack_p = element->parameter_pack_p;
1627 element->parameter_pack_p = false;
1628 }
1629 else
1630 declarator->parameter_pack_p = false;
1631
1632 return declarator;
1633 }
1634
1635 /* Determine whether the declarator we've seen so far can be a
1636 parameter pack, when followed by an ellipsis. */
1637 static bool
1638 declarator_can_be_parameter_pack (cp_declarator *declarator)
1639 {
1640 if (declarator && declarator->parameter_pack_p)
1641 /* We already saw an ellipsis. */
1642 return false;
1643
1644 /* Search for a declarator name, or any other declarator that goes
1645 after the point where the ellipsis could appear in a parameter
1646 pack. If we find any of these, then this declarator can not be
1647 made into a parameter pack. */
1648 bool found = false;
1649 while (declarator && !found)
1650 {
1651 switch ((int)declarator->kind)
1652 {
1653 case cdk_id:
1654 case cdk_array:
1655 case cdk_decomp:
1656 found = true;
1657 break;
1658
1659 case cdk_error:
1660 return true;
1661
1662 default:
1663 declarator = declarator->declarator;
1664 break;
1665 }
1666 }
1667
1668 return !found;
1669 }
1670
1671 cp_parameter_declarator *no_parameters;
1672
1673 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1674 DECLARATOR and DEFAULT_ARGUMENT. */
1675
1676 cp_parameter_declarator *
1677 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1678 cp_declarator *declarator,
1679 tree default_argument,
1680 location_t loc,
1681 bool template_parameter_pack_p = false)
1682 {
1683 cp_parameter_declarator *parameter;
1684
1685 parameter = ((cp_parameter_declarator *)
1686 alloc_declarator (sizeof (cp_parameter_declarator)));
1687 parameter->next = NULL;
1688 if (decl_specifiers)
1689 parameter->decl_specifiers = *decl_specifiers;
1690 else
1691 clear_decl_specs (&parameter->decl_specifiers);
1692 parameter->declarator = declarator;
1693 parameter->default_argument = default_argument;
1694 parameter->template_parameter_pack_p = template_parameter_pack_p;
1695 parameter->loc = loc;
1696
1697 return parameter;
1698 }
1699
1700 /* Returns true iff DECLARATOR is a declaration for a function. */
1701
1702 static bool
1703 function_declarator_p (const cp_declarator *declarator)
1704 {
1705 while (declarator)
1706 {
1707 if (declarator->kind == cdk_function
1708 && declarator->declarator->kind == cdk_id)
1709 return true;
1710 if (declarator->kind == cdk_id
1711 || declarator->kind == cdk_decomp
1712 || declarator->kind == cdk_error)
1713 return false;
1714 declarator = declarator->declarator;
1715 }
1716 return false;
1717 }
1718
1719 /* The parser. */
1720
1721 /* Overview
1722 --------
1723
1724 A cp_parser parses the token stream as specified by the C++
1725 grammar. Its job is purely parsing, not semantic analysis. For
1726 example, the parser breaks the token stream into declarators,
1727 expressions, statements, and other similar syntactic constructs.
1728 It does not check that the types of the expressions on either side
1729 of an assignment-statement are compatible, or that a function is
1730 not declared with a parameter of type `void'.
1731
1732 The parser invokes routines elsewhere in the compiler to perform
1733 semantic analysis and to build up the abstract syntax tree for the
1734 code processed.
1735
1736 The parser (and the template instantiation code, which is, in a
1737 way, a close relative of parsing) are the only parts of the
1738 compiler that should be calling push_scope and pop_scope, or
1739 related functions. The parser (and template instantiation code)
1740 keeps track of what scope is presently active; everything else
1741 should simply honor that. (The code that generates static
1742 initializers may also need to set the scope, in order to check
1743 access control correctly when emitting the initializers.)
1744
1745 Methodology
1746 -----------
1747
1748 The parser is of the standard recursive-descent variety. Upcoming
1749 tokens in the token stream are examined in order to determine which
1750 production to use when parsing a non-terminal. Some C++ constructs
1751 require arbitrary look ahead to disambiguate. For example, it is
1752 impossible, in the general case, to tell whether a statement is an
1753 expression or declaration without scanning the entire statement.
1754 Therefore, the parser is capable of "parsing tentatively." When the
1755 parser is not sure what construct comes next, it enters this mode.
1756 Then, while we attempt to parse the construct, the parser queues up
1757 error messages, rather than issuing them immediately, and saves the
1758 tokens it consumes. If the construct is parsed successfully, the
1759 parser "commits", i.e., it issues any queued error messages and
1760 the tokens that were being preserved are permanently discarded.
1761 If, however, the construct is not parsed successfully, the parser
1762 rolls back its state completely so that it can resume parsing using
1763 a different alternative.
1764
1765 Future Improvements
1766 -------------------
1767
1768 The performance of the parser could probably be improved substantially.
1769 We could often eliminate the need to parse tentatively by looking ahead
1770 a little bit. In some places, this approach might not entirely eliminate
1771 the need to parse tentatively, but it might still speed up the average
1772 case. */
1773
1774 /* Flags that are passed to some parsing functions. These values can
1775 be bitwise-ored together. */
1776
1777 enum
1778 {
1779 /* No flags. */
1780 CP_PARSER_FLAGS_NONE = 0x0,
1781 /* The construct is optional. If it is not present, then no error
1782 should be issued. */
1783 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1784 /* When parsing a type-specifier, treat user-defined type-names
1785 as non-type identifiers. */
1786 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1787 /* When parsing a type-specifier, do not try to parse a class-specifier
1788 or enum-specifier. */
1789 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1790 /* When parsing a decl-specifier-seq, only allow type-specifier or
1791 constexpr. */
1792 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1793 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1794 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1795 };
1796
1797 /* This type is used for parameters and variables which hold
1798 combinations of the above flags. */
1799 typedef int cp_parser_flags;
1800
1801 /* The different kinds of declarators we want to parse. */
1802
1803 enum cp_parser_declarator_kind
1804 {
1805 /* We want an abstract declarator. */
1806 CP_PARSER_DECLARATOR_ABSTRACT,
1807 /* We want a named declarator. */
1808 CP_PARSER_DECLARATOR_NAMED,
1809 /* We don't mind, but the name must be an unqualified-id. */
1810 CP_PARSER_DECLARATOR_EITHER
1811 };
1812
1813 /* The precedence values used to parse binary expressions. The minimum value
1814 of PREC must be 1, because zero is reserved to quickly discriminate
1815 binary operators from other tokens. */
1816
1817 enum cp_parser_prec
1818 {
1819 PREC_NOT_OPERATOR,
1820 PREC_LOGICAL_OR_EXPRESSION,
1821 PREC_LOGICAL_AND_EXPRESSION,
1822 PREC_INCLUSIVE_OR_EXPRESSION,
1823 PREC_EXCLUSIVE_OR_EXPRESSION,
1824 PREC_AND_EXPRESSION,
1825 PREC_EQUALITY_EXPRESSION,
1826 PREC_RELATIONAL_EXPRESSION,
1827 PREC_SHIFT_EXPRESSION,
1828 PREC_ADDITIVE_EXPRESSION,
1829 PREC_MULTIPLICATIVE_EXPRESSION,
1830 PREC_PM_EXPRESSION,
1831 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1832 };
1833
1834 /* A mapping from a token type to a corresponding tree node type, with a
1835 precedence value. */
1836
1837 struct cp_parser_binary_operations_map_node
1838 {
1839 /* The token type. */
1840 enum cpp_ttype token_type;
1841 /* The corresponding tree code. */
1842 enum tree_code tree_type;
1843 /* The precedence of this operator. */
1844 enum cp_parser_prec prec;
1845 };
1846
1847 struct cp_parser_expression_stack_entry
1848 {
1849 /* Left hand side of the binary operation we are currently
1850 parsing. */
1851 cp_expr lhs;
1852 /* Original tree code for left hand side, if it was a binary
1853 expression itself (used for -Wparentheses). */
1854 enum tree_code lhs_type;
1855 /* Tree code for the binary operation we are parsing. */
1856 enum tree_code tree_type;
1857 /* Precedence of the binary operation we are parsing. */
1858 enum cp_parser_prec prec;
1859 /* Location of the binary operation we are parsing. */
1860 location_t loc;
1861 };
1862
1863 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1864 entries because precedence levels on the stack are monotonically
1865 increasing. */
1866 typedef struct cp_parser_expression_stack_entry
1867 cp_parser_expression_stack[NUM_PREC_VALUES];
1868
1869 /* Prototypes. */
1870
1871 /* Constructors and destructors. */
1872
1873 static cp_parser_context *cp_parser_context_new
1874 (cp_parser_context *);
1875
1876 /* Class variables. */
1877
1878 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1879
1880 /* The operator-precedence table used by cp_parser_binary_expression.
1881 Transformed into an associative array (binops_by_token) by
1882 cp_parser_new. */
1883
1884 static const cp_parser_binary_operations_map_node binops[] = {
1885 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1886 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1887
1888 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1889 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1891
1892 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1893 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1894
1895 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1896 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1897
1898 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1899 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1900 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1901 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1902
1903 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1904 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1905
1906 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1907
1908 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1909
1910 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1911
1912 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1913
1914 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1915 };
1916
1917 /* The same as binops, but initialized by cp_parser_new so that
1918 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1919 for speed. */
1920 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1921
1922 /* Constructors and destructors. */
1923
1924 /* Construct a new context. The context below this one on the stack
1925 is given by NEXT. */
1926
1927 static cp_parser_context *
1928 cp_parser_context_new (cp_parser_context* next)
1929 {
1930 cp_parser_context *context;
1931
1932 /* Allocate the storage. */
1933 if (cp_parser_context_free_list != NULL)
1934 {
1935 /* Pull the first entry from the free list. */
1936 context = cp_parser_context_free_list;
1937 cp_parser_context_free_list = context->next;
1938 memset (context, 0, sizeof (*context));
1939 }
1940 else
1941 context = ggc_cleared_alloc<cp_parser_context> ();
1942
1943 /* No errors have occurred yet in this context. */
1944 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1945 /* If this is not the bottommost context, copy information that we
1946 need from the previous context. */
1947 if (next)
1948 {
1949 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1950 expression, then we are parsing one in this context, too. */
1951 context->object_type = next->object_type;
1952 /* Thread the stack. */
1953 context->next = next;
1954 }
1955
1956 return context;
1957 }
1958
1959 /* Managing the unparsed function queues. */
1960
1961 #define unparsed_funs_with_default_args \
1962 parser->unparsed_queues->last ().funs_with_default_args
1963 #define unparsed_funs_with_definitions \
1964 parser->unparsed_queues->last ().funs_with_definitions
1965 #define unparsed_nsdmis \
1966 parser->unparsed_queues->last ().nsdmis
1967 #define unparsed_classes \
1968 parser->unparsed_queues->last ().classes
1969
1970 static void
1971 push_unparsed_function_queues (cp_parser *parser)
1972 {
1973 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1974 vec_safe_push (parser->unparsed_queues, e);
1975 }
1976
1977 static void
1978 pop_unparsed_function_queues (cp_parser *parser)
1979 {
1980 release_tree_vector (unparsed_funs_with_definitions);
1981 parser->unparsed_queues->pop ();
1982 }
1983
1984 /* Prototypes. */
1985
1986 /* Constructors and destructors. */
1987
1988 static cp_parser *cp_parser_new
1989 (void);
1990
1991 /* Routines to parse various constructs.
1992
1993 Those that return `tree' will return the error_mark_node (rather
1994 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1995 Sometimes, they will return an ordinary node if error-recovery was
1996 attempted, even though a parse error occurred. So, to check
1997 whether or not a parse error occurred, you should always use
1998 cp_parser_error_occurred. If the construct is optional (indicated
1999 either by an `_opt' in the name of the function that does the
2000 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2001 the construct is not present. */
2002
2003 /* Lexical conventions [gram.lex] */
2004
2005 static cp_expr cp_parser_identifier
2006 (cp_parser *);
2007 static cp_expr cp_parser_string_literal
2008 (cp_parser *, bool, bool, bool);
2009 static cp_expr cp_parser_userdef_char_literal
2010 (cp_parser *);
2011 static tree cp_parser_userdef_string_literal
2012 (tree);
2013 static cp_expr cp_parser_userdef_numeric_literal
2014 (cp_parser *);
2015
2016 /* Basic concepts [gram.basic] */
2017
2018 static void cp_parser_translation_unit (cp_parser *);
2019
2020 /* Expressions [gram.expr] */
2021
2022 static cp_expr cp_parser_primary_expression
2023 (cp_parser *, bool, bool, bool, cp_id_kind *);
2024 static cp_expr cp_parser_id_expression
2025 (cp_parser *, bool, bool, bool *, bool, bool);
2026 static cp_expr cp_parser_unqualified_id
2027 (cp_parser *, bool, bool, bool, bool);
2028 static tree cp_parser_nested_name_specifier_opt
2029 (cp_parser *, bool, bool, bool, bool, bool = false);
2030 static tree cp_parser_nested_name_specifier
2031 (cp_parser *, bool, bool, bool, bool);
2032 static tree cp_parser_qualifying_entity
2033 (cp_parser *, bool, bool, bool, bool, bool);
2034 static cp_expr cp_parser_postfix_expression
2035 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2036 static tree cp_parser_postfix_open_square_expression
2037 (cp_parser *, tree, bool, bool);
2038 static tree cp_parser_postfix_dot_deref_expression
2039 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2040 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2041 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2042 bool = false);
2043 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2044 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2045 static void cp_parser_pseudo_destructor_name
2046 (cp_parser *, tree, tree *, tree *);
2047 static cp_expr cp_parser_unary_expression
2048 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2049 static enum tree_code cp_parser_unary_operator
2050 (cp_token *);
2051 static tree cp_parser_new_expression
2052 (cp_parser *);
2053 static vec<tree, va_gc> *cp_parser_new_placement
2054 (cp_parser *);
2055 static tree cp_parser_new_type_id
2056 (cp_parser *, tree *);
2057 static cp_declarator *cp_parser_new_declarator_opt
2058 (cp_parser *);
2059 static cp_declarator *cp_parser_direct_new_declarator
2060 (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_initializer
2062 (cp_parser *);
2063 static tree cp_parser_delete_expression
2064 (cp_parser *);
2065 static cp_expr cp_parser_cast_expression
2066 (cp_parser *, bool, bool, bool, cp_id_kind *);
2067 static cp_expr cp_parser_binary_expression
2068 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2069 static tree cp_parser_question_colon_clause
2070 (cp_parser *, cp_expr);
2071 static cp_expr cp_parser_assignment_expression
2072 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2073 static enum tree_code cp_parser_assignment_operator_opt
2074 (cp_parser *);
2075 static cp_expr cp_parser_expression
2076 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2077 static cp_expr cp_parser_constant_expression
2078 (cp_parser *, bool = false, bool * = NULL, bool = false);
2079 static cp_expr cp_parser_builtin_offsetof
2080 (cp_parser *);
2081 static cp_expr cp_parser_lambda_expression
2082 (cp_parser *);
2083 static void cp_parser_lambda_introducer
2084 (cp_parser *, tree);
2085 static bool cp_parser_lambda_declarator_opt
2086 (cp_parser *, tree);
2087 static void cp_parser_lambda_body
2088 (cp_parser *, tree);
2089
2090 /* Statements [gram.stmt.stmt] */
2091
2092 static void cp_parser_statement
2093 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2094 static void cp_parser_label_for_labeled_statement
2095 (cp_parser *, tree);
2096 static tree cp_parser_expression_statement
2097 (cp_parser *, tree);
2098 static tree cp_parser_compound_statement
2099 (cp_parser *, tree, int, bool);
2100 static void cp_parser_statement_seq_opt
2101 (cp_parser *, tree);
2102 static tree cp_parser_selection_statement
2103 (cp_parser *, bool *, vec<tree> *);
2104 static tree cp_parser_condition
2105 (cp_parser *);
2106 static tree cp_parser_iteration_statement
2107 (cp_parser *, bool *, bool, unsigned short);
2108 static bool cp_parser_init_statement
2109 (cp_parser *, tree *decl);
2110 static tree cp_parser_for
2111 (cp_parser *, bool, unsigned short);
2112 static tree cp_parser_c_for
2113 (cp_parser *, tree, tree, bool, unsigned short);
2114 static tree cp_parser_range_for
2115 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2116 static void do_range_for_auto_deduction
2117 (tree, tree);
2118 static tree cp_parser_perform_range_for_lookup
2119 (tree, tree *, tree *);
2120 static tree cp_parser_range_for_member_function
2121 (tree, tree);
2122 static tree cp_parser_jump_statement
2123 (cp_parser *);
2124 static void cp_parser_declaration_statement
2125 (cp_parser *);
2126
2127 static tree cp_parser_implicitly_scoped_statement
2128 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2129 static void cp_parser_already_scoped_statement
2130 (cp_parser *, bool *, const token_indent_info &);
2131
2132 /* Declarations [gram.dcl.dcl] */
2133
2134 static void cp_parser_declaration_seq_opt
2135 (cp_parser *);
2136 static void cp_parser_declaration
2137 (cp_parser *);
2138 static void cp_parser_toplevel_declaration
2139 (cp_parser *);
2140 static void cp_parser_block_declaration
2141 (cp_parser *, bool);
2142 static void cp_parser_simple_declaration
2143 (cp_parser *, bool, tree *);
2144 static void cp_parser_decl_specifier_seq
2145 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2146 static tree cp_parser_storage_class_specifier_opt
2147 (cp_parser *);
2148 static tree cp_parser_function_specifier_opt
2149 (cp_parser *, cp_decl_specifier_seq *);
2150 static tree cp_parser_type_specifier
2151 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2152 int *, bool *);
2153 static tree cp_parser_simple_type_specifier
2154 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2155 static tree cp_parser_type_name
2156 (cp_parser *, bool);
2157 static tree cp_parser_type_name
2158 (cp_parser *);
2159 static tree cp_parser_nonclass_name
2160 (cp_parser* parser);
2161 static tree cp_parser_elaborated_type_specifier
2162 (cp_parser *, bool, bool);
2163 static tree cp_parser_enum_specifier
2164 (cp_parser *);
2165 static void cp_parser_enumerator_list
2166 (cp_parser *, tree);
2167 static void cp_parser_enumerator_definition
2168 (cp_parser *, tree);
2169 static tree cp_parser_namespace_name
2170 (cp_parser *);
2171 static void cp_parser_namespace_definition
2172 (cp_parser *);
2173 static void cp_parser_namespace_body
2174 (cp_parser *);
2175 static tree cp_parser_qualified_namespace_specifier
2176 (cp_parser *);
2177 static void cp_parser_namespace_alias_definition
2178 (cp_parser *);
2179 static bool cp_parser_using_declaration
2180 (cp_parser *, bool);
2181 static void cp_parser_using_directive
2182 (cp_parser *);
2183 static tree cp_parser_alias_declaration
2184 (cp_parser *);
2185 static void cp_parser_asm_definition
2186 (cp_parser *);
2187 static void cp_parser_linkage_specification
2188 (cp_parser *);
2189 static void cp_parser_static_assert
2190 (cp_parser *, bool);
2191 static tree cp_parser_decltype
2192 (cp_parser *);
2193 static tree cp_parser_decomposition_declaration
2194 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2195
2196 /* Declarators [gram.dcl.decl] */
2197
2198 static tree cp_parser_init_declarator
2199 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2200 bool, bool, int, bool *, tree *, location_t *, tree *);
2201 static cp_declarator *cp_parser_declarator
2202 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2203 static cp_declarator *cp_parser_direct_declarator
2204 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2205 static enum tree_code cp_parser_ptr_operator
2206 (cp_parser *, tree *, cp_cv_quals *, tree *);
2207 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2208 (cp_parser *);
2209 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2210 (cp_parser *);
2211 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2212 (cp_parser *);
2213 static tree cp_parser_tx_qualifier_opt
2214 (cp_parser *);
2215 static tree cp_parser_late_return_type_opt
2216 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2217 static tree cp_parser_declarator_id
2218 (cp_parser *, bool);
2219 static tree cp_parser_type_id
2220 (cp_parser *, location_t * = NULL);
2221 static tree cp_parser_template_type_arg
2222 (cp_parser *);
2223 static tree cp_parser_trailing_type_id (cp_parser *);
2224 static tree cp_parser_type_id_1
2225 (cp_parser *, bool, bool, location_t *);
2226 static void cp_parser_type_specifier_seq
2227 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2228 static tree cp_parser_parameter_declaration_clause
2229 (cp_parser *);
2230 static tree cp_parser_parameter_declaration_list
2231 (cp_parser *);
2232 static cp_parameter_declarator *cp_parser_parameter_declaration
2233 (cp_parser *, bool, bool *);
2234 static tree cp_parser_default_argument
2235 (cp_parser *, bool);
2236 static void cp_parser_function_body
2237 (cp_parser *, bool);
2238 static tree cp_parser_initializer
2239 (cp_parser *, bool *, bool *, bool = false);
2240 static cp_expr cp_parser_initializer_clause
2241 (cp_parser *, bool *);
2242 static cp_expr cp_parser_braced_list
2243 (cp_parser*, bool*);
2244 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2245 (cp_parser *, bool *);
2246
2247 static void cp_parser_ctor_initializer_opt_and_function_body
2248 (cp_parser *, bool);
2249
2250 static tree cp_parser_late_parsing_omp_declare_simd
2251 (cp_parser *, tree);
2252
2253 static tree cp_parser_late_parsing_oacc_routine
2254 (cp_parser *, tree);
2255
2256 static tree synthesize_implicit_template_parm
2257 (cp_parser *, tree);
2258 static tree finish_fully_implicit_template
2259 (cp_parser *, tree);
2260 static void abort_fully_implicit_template
2261 (cp_parser *);
2262
2263 /* Classes [gram.class] */
2264
2265 static tree cp_parser_class_name
2266 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2267 static tree cp_parser_class_specifier
2268 (cp_parser *);
2269 static tree cp_parser_class_head
2270 (cp_parser *, bool *);
2271 static enum tag_types cp_parser_class_key
2272 (cp_parser *);
2273 static void cp_parser_type_parameter_key
2274 (cp_parser* parser);
2275 static void cp_parser_member_specification_opt
2276 (cp_parser *);
2277 static void cp_parser_member_declaration
2278 (cp_parser *);
2279 static tree cp_parser_pure_specifier
2280 (cp_parser *);
2281 static tree cp_parser_constant_initializer
2282 (cp_parser *);
2283
2284 /* Derived classes [gram.class.derived] */
2285
2286 static tree cp_parser_base_clause
2287 (cp_parser *);
2288 static tree cp_parser_base_specifier
2289 (cp_parser *);
2290
2291 /* Special member functions [gram.special] */
2292
2293 static tree cp_parser_conversion_function_id
2294 (cp_parser *);
2295 static tree cp_parser_conversion_type_id
2296 (cp_parser *);
2297 static cp_declarator *cp_parser_conversion_declarator_opt
2298 (cp_parser *);
2299 static void cp_parser_ctor_initializer_opt
2300 (cp_parser *);
2301 static void cp_parser_mem_initializer_list
2302 (cp_parser *);
2303 static tree cp_parser_mem_initializer
2304 (cp_parser *);
2305 static tree cp_parser_mem_initializer_id
2306 (cp_parser *);
2307
2308 /* Overloading [gram.over] */
2309
2310 static cp_expr cp_parser_operator_function_id
2311 (cp_parser *);
2312 static cp_expr cp_parser_operator
2313 (cp_parser *);
2314
2315 /* Templates [gram.temp] */
2316
2317 static void cp_parser_template_declaration
2318 (cp_parser *, bool);
2319 static tree cp_parser_template_parameter_list
2320 (cp_parser *);
2321 static tree cp_parser_template_parameter
2322 (cp_parser *, bool *, bool *);
2323 static tree cp_parser_type_parameter
2324 (cp_parser *, bool *);
2325 static tree cp_parser_template_id
2326 (cp_parser *, bool, bool, enum tag_types, bool);
2327 static tree cp_parser_template_name
2328 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2329 static tree cp_parser_template_argument_list
2330 (cp_parser *);
2331 static tree cp_parser_template_argument
2332 (cp_parser *);
2333 static void cp_parser_explicit_instantiation
2334 (cp_parser *);
2335 static void cp_parser_explicit_specialization
2336 (cp_parser *);
2337
2338 /* Exception handling [gram.exception] */
2339
2340 static tree cp_parser_try_block
2341 (cp_parser *);
2342 static void cp_parser_function_try_block
2343 (cp_parser *);
2344 static void cp_parser_handler_seq
2345 (cp_parser *);
2346 static void cp_parser_handler
2347 (cp_parser *);
2348 static tree cp_parser_exception_declaration
2349 (cp_parser *);
2350 static tree cp_parser_throw_expression
2351 (cp_parser *);
2352 static tree cp_parser_exception_specification_opt
2353 (cp_parser *);
2354 static tree cp_parser_type_id_list
2355 (cp_parser *);
2356
2357 /* GNU Extensions */
2358
2359 static tree cp_parser_asm_specification_opt
2360 (cp_parser *);
2361 static tree cp_parser_asm_operand_list
2362 (cp_parser *);
2363 static tree cp_parser_asm_clobber_list
2364 (cp_parser *);
2365 static tree cp_parser_asm_label_list
2366 (cp_parser *);
2367 static bool cp_next_tokens_can_be_attribute_p
2368 (cp_parser *);
2369 static bool cp_next_tokens_can_be_gnu_attribute_p
2370 (cp_parser *);
2371 static bool cp_next_tokens_can_be_std_attribute_p
2372 (cp_parser *);
2373 static bool cp_nth_tokens_can_be_std_attribute_p
2374 (cp_parser *, size_t);
2375 static bool cp_nth_tokens_can_be_gnu_attribute_p
2376 (cp_parser *, size_t);
2377 static bool cp_nth_tokens_can_be_attribute_p
2378 (cp_parser *, size_t);
2379 static tree cp_parser_attributes_opt
2380 (cp_parser *);
2381 static tree cp_parser_gnu_attributes_opt
2382 (cp_parser *);
2383 static tree cp_parser_gnu_attribute_list
2384 (cp_parser *);
2385 static tree cp_parser_std_attribute
2386 (cp_parser *, tree);
2387 static tree cp_parser_std_attribute_spec
2388 (cp_parser *);
2389 static tree cp_parser_std_attribute_spec_seq
2390 (cp_parser *);
2391 static size_t cp_parser_skip_attributes_opt
2392 (cp_parser *, size_t);
2393 static bool cp_parser_extension_opt
2394 (cp_parser *, int *);
2395 static void cp_parser_label_declaration
2396 (cp_parser *);
2397
2398 /* Concept Extensions */
2399
2400 static tree cp_parser_requires_clause
2401 (cp_parser *);
2402 static tree cp_parser_requires_clause_opt
2403 (cp_parser *);
2404 static tree cp_parser_requires_expression
2405 (cp_parser *);
2406 static tree cp_parser_requirement_parameter_list
2407 (cp_parser *);
2408 static tree cp_parser_requirement_body
2409 (cp_parser *);
2410 static tree cp_parser_requirement_list
2411 (cp_parser *);
2412 static tree cp_parser_requirement
2413 (cp_parser *);
2414 static tree cp_parser_simple_requirement
2415 (cp_parser *);
2416 static tree cp_parser_compound_requirement
2417 (cp_parser *);
2418 static tree cp_parser_type_requirement
2419 (cp_parser *);
2420 static tree cp_parser_nested_requirement
2421 (cp_parser *);
2422
2423 /* Transactional Memory Extensions */
2424
2425 static tree cp_parser_transaction
2426 (cp_parser *, cp_token *);
2427 static tree cp_parser_transaction_expression
2428 (cp_parser *, enum rid);
2429 static void cp_parser_function_transaction
2430 (cp_parser *, enum rid);
2431 static tree cp_parser_transaction_cancel
2432 (cp_parser *);
2433
2434 enum pragma_context {
2435 pragma_external,
2436 pragma_member,
2437 pragma_objc_icode,
2438 pragma_stmt,
2439 pragma_compound
2440 };
2441 static bool cp_parser_pragma
2442 (cp_parser *, enum pragma_context, bool *);
2443
2444 /* Objective-C++ Productions */
2445
2446 static tree cp_parser_objc_message_receiver
2447 (cp_parser *);
2448 static tree cp_parser_objc_message_args
2449 (cp_parser *);
2450 static tree cp_parser_objc_message_expression
2451 (cp_parser *);
2452 static cp_expr cp_parser_objc_encode_expression
2453 (cp_parser *);
2454 static tree cp_parser_objc_defs_expression
2455 (cp_parser *);
2456 static tree cp_parser_objc_protocol_expression
2457 (cp_parser *);
2458 static tree cp_parser_objc_selector_expression
2459 (cp_parser *);
2460 static cp_expr cp_parser_objc_expression
2461 (cp_parser *);
2462 static bool cp_parser_objc_selector_p
2463 (enum cpp_ttype);
2464 static tree cp_parser_objc_selector
2465 (cp_parser *);
2466 static tree cp_parser_objc_protocol_refs_opt
2467 (cp_parser *);
2468 static void cp_parser_objc_declaration
2469 (cp_parser *, tree);
2470 static tree cp_parser_objc_statement
2471 (cp_parser *);
2472 static bool cp_parser_objc_valid_prefix_attributes
2473 (cp_parser *, tree *);
2474 static void cp_parser_objc_at_property_declaration
2475 (cp_parser *) ;
2476 static void cp_parser_objc_at_synthesize_declaration
2477 (cp_parser *) ;
2478 static void cp_parser_objc_at_dynamic_declaration
2479 (cp_parser *) ;
2480 static tree cp_parser_objc_struct_declaration
2481 (cp_parser *) ;
2482
2483 /* Utility Routines */
2484
2485 static cp_expr cp_parser_lookup_name
2486 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2487 static tree cp_parser_lookup_name_simple
2488 (cp_parser *, tree, location_t);
2489 static tree cp_parser_maybe_treat_template_as_class
2490 (tree, bool);
2491 static bool cp_parser_check_declarator_template_parameters
2492 (cp_parser *, cp_declarator *, location_t);
2493 static bool cp_parser_check_template_parameters
2494 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2495 static cp_expr cp_parser_simple_cast_expression
2496 (cp_parser *);
2497 static tree cp_parser_global_scope_opt
2498 (cp_parser *, bool);
2499 static bool cp_parser_constructor_declarator_p
2500 (cp_parser *, bool);
2501 static tree cp_parser_function_definition_from_specifiers_and_declarator
2502 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2503 static tree cp_parser_function_definition_after_declarator
2504 (cp_parser *, bool);
2505 static bool cp_parser_template_declaration_after_export
2506 (cp_parser *, bool);
2507 static void cp_parser_perform_template_parameter_access_checks
2508 (vec<deferred_access_check, va_gc> *);
2509 static tree cp_parser_single_declaration
2510 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2511 static cp_expr cp_parser_functional_cast
2512 (cp_parser *, tree);
2513 static tree cp_parser_save_member_function_body
2514 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2515 static tree cp_parser_save_nsdmi
2516 (cp_parser *);
2517 static tree cp_parser_enclosed_template_argument_list
2518 (cp_parser *);
2519 static void cp_parser_save_default_args
2520 (cp_parser *, tree);
2521 static void cp_parser_late_parsing_for_member
2522 (cp_parser *, tree);
2523 static tree cp_parser_late_parse_one_default_arg
2524 (cp_parser *, tree, tree, tree);
2525 static void cp_parser_late_parsing_nsdmi
2526 (cp_parser *, tree);
2527 static void cp_parser_late_parsing_default_args
2528 (cp_parser *, tree);
2529 static tree cp_parser_sizeof_operand
2530 (cp_parser *, enum rid);
2531 static cp_expr cp_parser_trait_expr
2532 (cp_parser *, enum rid);
2533 static bool cp_parser_declares_only_class_p
2534 (cp_parser *);
2535 static void cp_parser_set_storage_class
2536 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2537 static void cp_parser_set_decl_spec_type
2538 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2539 static void set_and_check_decl_spec_loc
2540 (cp_decl_specifier_seq *decl_specs,
2541 cp_decl_spec ds, cp_token *);
2542 static bool cp_parser_friend_p
2543 (const cp_decl_specifier_seq *);
2544 static void cp_parser_required_error
2545 (cp_parser *, required_token, bool, location_t);
2546 static cp_token *cp_parser_require
2547 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2548 static cp_token *cp_parser_require_keyword
2549 (cp_parser *, enum rid, required_token);
2550 static bool cp_parser_token_starts_function_definition_p
2551 (cp_token *);
2552 static bool cp_parser_next_token_starts_class_definition_p
2553 (cp_parser *);
2554 static bool cp_parser_next_token_ends_template_argument_p
2555 (cp_parser *);
2556 static bool cp_parser_nth_token_starts_template_argument_list_p
2557 (cp_parser *, size_t);
2558 static enum tag_types cp_parser_token_is_class_key
2559 (cp_token *);
2560 static enum tag_types cp_parser_token_is_type_parameter_key
2561 (cp_token *);
2562 static void cp_parser_check_class_key
2563 (enum tag_types, tree type);
2564 static void cp_parser_check_access_in_redeclaration
2565 (tree type, location_t location);
2566 static bool cp_parser_optional_template_keyword
2567 (cp_parser *);
2568 static void cp_parser_pre_parsed_nested_name_specifier
2569 (cp_parser *);
2570 static bool cp_parser_cache_group
2571 (cp_parser *, enum cpp_ttype, unsigned);
2572 static tree cp_parser_cache_defarg
2573 (cp_parser *parser, bool nsdmi);
2574 static void cp_parser_parse_tentatively
2575 (cp_parser *);
2576 static void cp_parser_commit_to_tentative_parse
2577 (cp_parser *);
2578 static void cp_parser_commit_to_topmost_tentative_parse
2579 (cp_parser *);
2580 static void cp_parser_abort_tentative_parse
2581 (cp_parser *);
2582 static bool cp_parser_parse_definitely
2583 (cp_parser *);
2584 static inline bool cp_parser_parsing_tentatively
2585 (cp_parser *);
2586 static bool cp_parser_uncommitted_to_tentative_parse_p
2587 (cp_parser *);
2588 static void cp_parser_error
2589 (cp_parser *, const char *);
2590 static void cp_parser_name_lookup_error
2591 (cp_parser *, tree, tree, name_lookup_error, location_t);
2592 static bool cp_parser_simulate_error
2593 (cp_parser *);
2594 static bool cp_parser_check_type_definition
2595 (cp_parser *);
2596 static void cp_parser_check_for_definition_in_return_type
2597 (cp_declarator *, tree, location_t type_location);
2598 static void cp_parser_check_for_invalid_template_id
2599 (cp_parser *, tree, enum tag_types, location_t location);
2600 static bool cp_parser_non_integral_constant_expression
2601 (cp_parser *, non_integral_constant);
2602 static void cp_parser_diagnose_invalid_type_name
2603 (cp_parser *, tree, location_t);
2604 static bool cp_parser_parse_and_diagnose_invalid_type_name
2605 (cp_parser *);
2606 static int cp_parser_skip_to_closing_parenthesis
2607 (cp_parser *, bool, bool, bool);
2608 static void cp_parser_skip_to_end_of_statement
2609 (cp_parser *);
2610 static void cp_parser_consume_semicolon_at_end_of_statement
2611 (cp_parser *);
2612 static void cp_parser_skip_to_end_of_block_or_statement
2613 (cp_parser *);
2614 static bool cp_parser_skip_to_closing_brace
2615 (cp_parser *);
2616 static void cp_parser_skip_to_end_of_template_parameter_list
2617 (cp_parser *);
2618 static void cp_parser_skip_to_pragma_eol
2619 (cp_parser*, cp_token *);
2620 static bool cp_parser_error_occurred
2621 (cp_parser *);
2622 static bool cp_parser_allow_gnu_extensions_p
2623 (cp_parser *);
2624 static bool cp_parser_is_pure_string_literal
2625 (cp_token *);
2626 static bool cp_parser_is_string_literal
2627 (cp_token *);
2628 static bool cp_parser_is_keyword
2629 (cp_token *, enum rid);
2630 static tree cp_parser_make_typename_type
2631 (cp_parser *, tree, location_t location);
2632 static cp_declarator * cp_parser_make_indirect_declarator
2633 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2634 static bool cp_parser_compound_literal_p
2635 (cp_parser *);
2636 static bool cp_parser_array_designator_p
2637 (cp_parser *);
2638 static bool cp_parser_init_statement_p
2639 (cp_parser *);
2640 static bool cp_parser_skip_to_closing_square_bracket
2641 (cp_parser *);
2642
2643 /* Concept-related syntactic transformations */
2644
2645 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2646 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2647
2648 // -------------------------------------------------------------------------- //
2649 // Unevaluated Operand Guard
2650 //
2651 // Implementation of an RAII helper for unevaluated operand parsing.
2652 cp_unevaluated::cp_unevaluated ()
2653 {
2654 ++cp_unevaluated_operand;
2655 ++c_inhibit_evaluation_warnings;
2656 }
2657
2658 cp_unevaluated::~cp_unevaluated ()
2659 {
2660 --c_inhibit_evaluation_warnings;
2661 --cp_unevaluated_operand;
2662 }
2663
2664 // -------------------------------------------------------------------------- //
2665 // Tentative Parsing
2666
2667 /* Returns nonzero if we are parsing tentatively. */
2668
2669 static inline bool
2670 cp_parser_parsing_tentatively (cp_parser* parser)
2671 {
2672 return parser->context->next != NULL;
2673 }
2674
2675 /* Returns nonzero if TOKEN is a string literal. */
2676
2677 static bool
2678 cp_parser_is_pure_string_literal (cp_token* token)
2679 {
2680 return (token->type == CPP_STRING ||
2681 token->type == CPP_STRING16 ||
2682 token->type == CPP_STRING32 ||
2683 token->type == CPP_WSTRING ||
2684 token->type == CPP_UTF8STRING);
2685 }
2686
2687 /* Returns nonzero if TOKEN is a string literal
2688 of a user-defined string literal. */
2689
2690 static bool
2691 cp_parser_is_string_literal (cp_token* token)
2692 {
2693 return (cp_parser_is_pure_string_literal (token) ||
2694 token->type == CPP_STRING_USERDEF ||
2695 token->type == CPP_STRING16_USERDEF ||
2696 token->type == CPP_STRING32_USERDEF ||
2697 token->type == CPP_WSTRING_USERDEF ||
2698 token->type == CPP_UTF8STRING_USERDEF);
2699 }
2700
2701 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2702
2703 static bool
2704 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2705 {
2706 return token->keyword == keyword;
2707 }
2708
2709 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2710 PRAGMA_NONE. */
2711
2712 static enum pragma_kind
2713 cp_parser_pragma_kind (cp_token *token)
2714 {
2715 if (token->type != CPP_PRAGMA)
2716 return PRAGMA_NONE;
2717 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2718 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2719 }
2720
2721 /* Helper function for cp_parser_error.
2722 Having peeked a token of kind TOK1_KIND that might signify
2723 a conflict marker, peek successor tokens to determine
2724 if we actually do have a conflict marker.
2725 Specifically, we consider a run of 7 '<', '=' or '>' characters
2726 at the start of a line as a conflict marker.
2727 These come through the lexer as three pairs and a single,
2728 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2729 If it returns true, *OUT_LOC is written to with the location/range
2730 of the marker. */
2731
2732 static bool
2733 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2734 location_t *out_loc)
2735 {
2736 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2737 if (token2->type != tok1_kind)
2738 return false;
2739 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2740 if (token3->type != tok1_kind)
2741 return false;
2742 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2743 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2744 return false;
2745
2746 /* It must be at the start of the line. */
2747 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2748 if (LOCATION_COLUMN (start_loc) != 1)
2749 return false;
2750
2751 /* We have a conflict marker. Construct a location of the form:
2752 <<<<<<<
2753 ^~~~~~~
2754 with start == caret, finishing at the end of the marker. */
2755 location_t finish_loc = get_finish (token4->location);
2756 *out_loc = make_location (start_loc, start_loc, finish_loc);
2757
2758 return true;
2759 }
2760
2761 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2762 RT_CLOSE_PAREN. */
2763
2764 static const char *
2765 get_matching_symbol (required_token token_desc)
2766 {
2767 switch (token_desc)
2768 {
2769 default:
2770 gcc_unreachable ();
2771 return "";
2772 case RT_CLOSE_BRACE:
2773 return "{";
2774 case RT_CLOSE_PAREN:
2775 return "(";
2776 }
2777 }
2778
2779 /* Attempt to convert TOKEN_DESC from a required_token to an
2780 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2781
2782 static enum cpp_ttype
2783 get_required_cpp_ttype (required_token token_desc)
2784 {
2785 switch (token_desc)
2786 {
2787 case RT_SEMICOLON:
2788 return CPP_SEMICOLON;
2789 case RT_OPEN_PAREN:
2790 return CPP_OPEN_PAREN;
2791 case RT_CLOSE_BRACE:
2792 return CPP_CLOSE_BRACE;
2793 case RT_OPEN_BRACE:
2794 return CPP_OPEN_BRACE;
2795 case RT_CLOSE_SQUARE:
2796 return CPP_CLOSE_SQUARE;
2797 case RT_OPEN_SQUARE:
2798 return CPP_OPEN_SQUARE;
2799 case RT_COMMA:
2800 return CPP_COMMA;
2801 case RT_COLON:
2802 return CPP_COLON;
2803 case RT_CLOSE_PAREN:
2804 return CPP_CLOSE_PAREN;
2805
2806 default:
2807 /* Use CPP_EOF as a "no completions possible" code. */
2808 return CPP_EOF;
2809 }
2810 }
2811
2812
2813 /* Subroutine of cp_parser_error and cp_parser_required_error.
2814
2815 Issue a diagnostic of the form
2816 FILE:LINE: MESSAGE before TOKEN
2817 where TOKEN is the next token in the input stream. MESSAGE
2818 (specified by the caller) is usually of the form "expected
2819 OTHER-TOKEN".
2820
2821 This bypasses the check for tentative passing, and potentially
2822 adds material needed by cp_parser_required_error.
2823
2824 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2825 suggesting insertion of the missing token.
2826
2827 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2828 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2829 location. */
2830
2831 static void
2832 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2833 required_token missing_token_desc,
2834 location_t matching_location)
2835 {
2836 cp_token *token = cp_lexer_peek_token (parser->lexer);
2837 /* This diagnostic makes more sense if it is tagged to the line
2838 of the token we just peeked at. */
2839 cp_lexer_set_source_position_from_token (token);
2840
2841 if (token->type == CPP_PRAGMA)
2842 {
2843 error_at (token->location,
2844 "%<#pragma%> is not allowed here");
2845 cp_parser_skip_to_pragma_eol (parser, token);
2846 return;
2847 }
2848
2849 /* If this is actually a conflict marker, report it as such. */
2850 if (token->type == CPP_LSHIFT
2851 || token->type == CPP_RSHIFT
2852 || token->type == CPP_EQ_EQ)
2853 {
2854 location_t loc;
2855 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2856 {
2857 error_at (loc, "version control conflict marker in file");
2858 expanded_location token_exploc = expand_location (token->location);
2859 /* Consume tokens until the end of the source line. */
2860 while (1)
2861 {
2862 cp_lexer_consume_token (parser->lexer);
2863 cp_token *next = cp_lexer_peek_token (parser->lexer);
2864 if (next == NULL)
2865 break;
2866 expanded_location next_exploc = expand_location (next->location);
2867 if (next_exploc.file != token_exploc.file)
2868 break;
2869 if (next_exploc.line != token_exploc.line)
2870 break;
2871 }
2872 return;
2873 }
2874 }
2875
2876 gcc_rich_location richloc (input_location);
2877
2878 bool added_matching_location = false;
2879
2880 if (missing_token_desc != RT_NONE)
2881 {
2882 /* Potentially supply a fix-it hint, suggesting to add the
2883 missing token immediately after the *previous* token.
2884 This may move the primary location within richloc. */
2885 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2886 location_t prev_token_loc
2887 = cp_lexer_previous_token (parser->lexer)->location;
2888 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2889
2890 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2891 Attempt to consolidate diagnostics by printing it as a
2892 secondary range within the main diagnostic. */
2893 if (matching_location != UNKNOWN_LOCATION)
2894 added_matching_location
2895 = richloc.add_location_if_nearby (matching_location);
2896 }
2897
2898 /* Actually emit the error. */
2899 c_parse_error (gmsgid,
2900 /* Because c_parser_error does not understand
2901 CPP_KEYWORD, keywords are treated like
2902 identifiers. */
2903 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2904 token->u.value, token->flags, &richloc);
2905
2906 if (missing_token_desc != RT_NONE)
2907 {
2908 /* If we weren't able to consolidate matching_location, then
2909 print it as a secondary diagnostic. */
2910 if (matching_location != UNKNOWN_LOCATION
2911 && !added_matching_location)
2912 inform (matching_location, "to match this %qs",
2913 get_matching_symbol (missing_token_desc));
2914 }
2915 }
2916
2917 /* If not parsing tentatively, issue a diagnostic of the form
2918 FILE:LINE: MESSAGE before TOKEN
2919 where TOKEN is the next token in the input stream. MESSAGE
2920 (specified by the caller) is usually of the form "expected
2921 OTHER-TOKEN". */
2922
2923 static void
2924 cp_parser_error (cp_parser* parser, const char* gmsgid)
2925 {
2926 if (!cp_parser_simulate_error (parser))
2927 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2928 }
2929
2930 /* Issue an error about name-lookup failing. NAME is the
2931 IDENTIFIER_NODE DECL is the result of
2932 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2933 the thing that we hoped to find. */
2934
2935 static void
2936 cp_parser_name_lookup_error (cp_parser* parser,
2937 tree name,
2938 tree decl,
2939 name_lookup_error desired,
2940 location_t location)
2941 {
2942 /* If name lookup completely failed, tell the user that NAME was not
2943 declared. */
2944 if (decl == error_mark_node)
2945 {
2946 if (parser->scope && parser->scope != global_namespace)
2947 error_at (location, "%<%E::%E%> has not been declared",
2948 parser->scope, name);
2949 else if (parser->scope == global_namespace)
2950 error_at (location, "%<::%E%> has not been declared", name);
2951 else if (parser->object_scope
2952 && !CLASS_TYPE_P (parser->object_scope))
2953 error_at (location, "request for member %qE in non-class type %qT",
2954 name, parser->object_scope);
2955 else if (parser->object_scope)
2956 error_at (location, "%<%T::%E%> has not been declared",
2957 parser->object_scope, name);
2958 else
2959 error_at (location, "%qE has not been declared", name);
2960 }
2961 else if (parser->scope && parser->scope != global_namespace)
2962 {
2963 switch (desired)
2964 {
2965 case NLE_TYPE:
2966 error_at (location, "%<%E::%E%> is not a type",
2967 parser->scope, name);
2968 break;
2969 case NLE_CXX98:
2970 error_at (location, "%<%E::%E%> is not a class or namespace",
2971 parser->scope, name);
2972 break;
2973 case NLE_NOT_CXX98:
2974 error_at (location,
2975 "%<%E::%E%> is not a class, namespace, or enumeration",
2976 parser->scope, name);
2977 break;
2978 default:
2979 gcc_unreachable ();
2980
2981 }
2982 }
2983 else if (parser->scope == global_namespace)
2984 {
2985 switch (desired)
2986 {
2987 case NLE_TYPE:
2988 error_at (location, "%<::%E%> is not a type", name);
2989 break;
2990 case NLE_CXX98:
2991 error_at (location, "%<::%E%> is not a class or namespace", name);
2992 break;
2993 case NLE_NOT_CXX98:
2994 error_at (location,
2995 "%<::%E%> is not a class, namespace, or enumeration",
2996 name);
2997 break;
2998 default:
2999 gcc_unreachable ();
3000 }
3001 }
3002 else
3003 {
3004 switch (desired)
3005 {
3006 case NLE_TYPE:
3007 error_at (location, "%qE is not a type", name);
3008 break;
3009 case NLE_CXX98:
3010 error_at (location, "%qE is not a class or namespace", name);
3011 break;
3012 case NLE_NOT_CXX98:
3013 error_at (location,
3014 "%qE is not a class, namespace, or enumeration", name);
3015 break;
3016 default:
3017 gcc_unreachable ();
3018 }
3019 }
3020 }
3021
3022 /* If we are parsing tentatively, remember that an error has occurred
3023 during this tentative parse. Returns true if the error was
3024 simulated; false if a message should be issued by the caller. */
3025
3026 static bool
3027 cp_parser_simulate_error (cp_parser* parser)
3028 {
3029 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3030 {
3031 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3032 return true;
3033 }
3034 return false;
3035 }
3036
3037 /* This function is called when a type is defined. If type
3038 definitions are forbidden at this point, an error message is
3039 issued. */
3040
3041 static bool
3042 cp_parser_check_type_definition (cp_parser* parser)
3043 {
3044 /* If types are forbidden here, issue a message. */
3045 if (parser->type_definition_forbidden_message)
3046 {
3047 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3048 in the message need to be interpreted. */
3049 error (parser->type_definition_forbidden_message);
3050 return false;
3051 }
3052 return true;
3053 }
3054
3055 /* This function is called when the DECLARATOR is processed. The TYPE
3056 was a type defined in the decl-specifiers. If it is invalid to
3057 define a type in the decl-specifiers for DECLARATOR, an error is
3058 issued. TYPE_LOCATION is the location of TYPE and is used
3059 for error reporting. */
3060
3061 static void
3062 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3063 tree type, location_t type_location)
3064 {
3065 /* [dcl.fct] forbids type definitions in return types.
3066 Unfortunately, it's not easy to know whether or not we are
3067 processing a return type until after the fact. */
3068 while (declarator
3069 && (declarator->kind == cdk_pointer
3070 || declarator->kind == cdk_reference
3071 || declarator->kind == cdk_ptrmem))
3072 declarator = declarator->declarator;
3073 if (declarator
3074 && declarator->kind == cdk_function)
3075 {
3076 error_at (type_location,
3077 "new types may not be defined in a return type");
3078 inform (type_location,
3079 "(perhaps a semicolon is missing after the definition of %qT)",
3080 type);
3081 }
3082 }
3083
3084 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3085 "<" in any valid C++ program. If the next token is indeed "<",
3086 issue a message warning the user about what appears to be an
3087 invalid attempt to form a template-id. LOCATION is the location
3088 of the type-specifier (TYPE) */
3089
3090 static void
3091 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3092 tree type,
3093 enum tag_types tag_type,
3094 location_t location)
3095 {
3096 cp_token_position start = 0;
3097
3098 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3099 {
3100 if (TREE_CODE (type) == TYPE_DECL)
3101 type = TREE_TYPE (type);
3102 if (TYPE_P (type) && !template_placeholder_p (type))
3103 error_at (location, "%qT is not a template", type);
3104 else if (identifier_p (type))
3105 {
3106 if (tag_type != none_type)
3107 error_at (location, "%qE is not a class template", type);
3108 else
3109 error_at (location, "%qE is not a template", type);
3110 }
3111 else
3112 error_at (location, "invalid template-id");
3113 /* Remember the location of the invalid "<". */
3114 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3115 start = cp_lexer_token_position (parser->lexer, true);
3116 /* Consume the "<". */
3117 cp_lexer_consume_token (parser->lexer);
3118 /* Parse the template arguments. */
3119 cp_parser_enclosed_template_argument_list (parser);
3120 /* Permanently remove the invalid template arguments so that
3121 this error message is not issued again. */
3122 if (start)
3123 cp_lexer_purge_tokens_after (parser->lexer, start);
3124 }
3125 }
3126
3127 /* If parsing an integral constant-expression, issue an error message
3128 about the fact that THING appeared and return true. Otherwise,
3129 return false. In either case, set
3130 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3131
3132 static bool
3133 cp_parser_non_integral_constant_expression (cp_parser *parser,
3134 non_integral_constant thing)
3135 {
3136 parser->non_integral_constant_expression_p = true;
3137 if (parser->integral_constant_expression_p)
3138 {
3139 if (!parser->allow_non_integral_constant_expression_p)
3140 {
3141 const char *msg = NULL;
3142 switch (thing)
3143 {
3144 case NIC_FLOAT:
3145 pedwarn (input_location, OPT_Wpedantic,
3146 "ISO C++ forbids using a floating-point literal "
3147 "in a constant-expression");
3148 return true;
3149 case NIC_CAST:
3150 error ("a cast to a type other than an integral or "
3151 "enumeration type cannot appear in a "
3152 "constant-expression");
3153 return true;
3154 case NIC_TYPEID:
3155 error ("%<typeid%> operator "
3156 "cannot appear in a constant-expression");
3157 return true;
3158 case NIC_NCC:
3159 error ("non-constant compound literals "
3160 "cannot appear in a constant-expression");
3161 return true;
3162 case NIC_FUNC_CALL:
3163 error ("a function call "
3164 "cannot appear in a constant-expression");
3165 return true;
3166 case NIC_INC:
3167 error ("an increment "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_DEC:
3171 error ("an decrement "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_ARRAY_REF:
3175 error ("an array reference "
3176 "cannot appear in a constant-expression");
3177 return true;
3178 case NIC_ADDR_LABEL:
3179 error ("the address of a label "
3180 "cannot appear in a constant-expression");
3181 return true;
3182 case NIC_OVERLOADED:
3183 error ("calls to overloaded operators "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_ASSIGNMENT:
3187 error ("an assignment cannot appear in a constant-expression");
3188 return true;
3189 case NIC_COMMA:
3190 error ("a comma operator "
3191 "cannot appear in a constant-expression");
3192 return true;
3193 case NIC_CONSTRUCTOR:
3194 error ("a call to a constructor "
3195 "cannot appear in a constant-expression");
3196 return true;
3197 case NIC_TRANSACTION:
3198 error ("a transaction expression "
3199 "cannot appear in a constant-expression");
3200 return true;
3201 case NIC_THIS:
3202 msg = "this";
3203 break;
3204 case NIC_FUNC_NAME:
3205 msg = "__FUNCTION__";
3206 break;
3207 case NIC_PRETTY_FUNC:
3208 msg = "__PRETTY_FUNCTION__";
3209 break;
3210 case NIC_C99_FUNC:
3211 msg = "__func__";
3212 break;
3213 case NIC_VA_ARG:
3214 msg = "va_arg";
3215 break;
3216 case NIC_ARROW:
3217 msg = "->";
3218 break;
3219 case NIC_POINT:
3220 msg = ".";
3221 break;
3222 case NIC_STAR:
3223 msg = "*";
3224 break;
3225 case NIC_ADDR:
3226 msg = "&";
3227 break;
3228 case NIC_PREINCREMENT:
3229 msg = "++";
3230 break;
3231 case NIC_PREDECREMENT:
3232 msg = "--";
3233 break;
3234 case NIC_NEW:
3235 msg = "new";
3236 break;
3237 case NIC_DEL:
3238 msg = "delete";
3239 break;
3240 default:
3241 gcc_unreachable ();
3242 }
3243 if (msg)
3244 error ("%qs cannot appear in a constant-expression", msg);
3245 return true;
3246 }
3247 }
3248 return false;
3249 }
3250
3251 /* Emit a diagnostic for an invalid type name. This function commits
3252 to the current active tentative parse, if any. (Otherwise, the
3253 problematic construct might be encountered again later, resulting
3254 in duplicate error messages.) LOCATION is the location of ID. */
3255
3256 static void
3257 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3258 location_t location)
3259 {
3260 tree decl, ambiguous_decls;
3261 cp_parser_commit_to_tentative_parse (parser);
3262 /* Try to lookup the identifier. */
3263 decl = cp_parser_lookup_name (parser, id, none_type,
3264 /*is_template=*/false,
3265 /*is_namespace=*/false,
3266 /*check_dependency=*/true,
3267 &ambiguous_decls, location);
3268 if (ambiguous_decls)
3269 /* If the lookup was ambiguous, an error will already have
3270 been issued. */
3271 return;
3272 /* If the lookup found a template-name, it means that the user forgot
3273 to specify an argument list. Emit a useful error message. */
3274 if (DECL_TYPE_TEMPLATE_P (decl))
3275 {
3276 auto_diagnostic_group d;
3277 error_at (location,
3278 "invalid use of template-name %qE without an argument list",
3279 decl);
3280 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3281 inform (location, "class template argument deduction is only available "
3282 "with -std=c++17 or -std=gnu++17");
3283 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3284 }
3285 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3286 error_at (location, "invalid use of destructor %qD as a type", id);
3287 else if (TREE_CODE (decl) == TYPE_DECL)
3288 /* Something like 'unsigned A a;' */
3289 error_at (location, "invalid combination of multiple type-specifiers");
3290 else if (!parser->scope)
3291 {
3292 /* Issue an error message. */
3293 auto_diagnostic_group d;
3294 name_hint hint;
3295 if (TREE_CODE (id) == IDENTIFIER_NODE)
3296 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3297 if (const char *suggestion = hint.suggestion ())
3298 {
3299 gcc_rich_location richloc (location);
3300 richloc.add_fixit_replace (suggestion);
3301 error_at (&richloc,
3302 "%qE does not name a type; did you mean %qs?",
3303 id, suggestion);
3304 }
3305 else
3306 error_at (location, "%qE does not name a type", id);
3307 /* If we're in a template class, it's possible that the user was
3308 referring to a type from a base class. For example:
3309
3310 template <typename T> struct A { typedef T X; };
3311 template <typename T> struct B : public A<T> { X x; };
3312
3313 The user should have said "typename A<T>::X". */
3314 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3315 inform (location, "C++11 %<constexpr%> only available with "
3316 "-std=c++11 or -std=gnu++11");
3317 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3318 inform (location, "C++11 %<noexcept%> only available with "
3319 "-std=c++11 or -std=gnu++11");
3320 else if (cxx_dialect < cxx11
3321 && TREE_CODE (id) == IDENTIFIER_NODE
3322 && id_equal (id, "thread_local"))
3323 inform (location, "C++11 %<thread_local%> only available with "
3324 "-std=c++11 or -std=gnu++11");
3325 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3326 inform (location, "%<concept%> only available with -fconcepts");
3327 else if (processing_template_decl && current_class_type
3328 && TYPE_BINFO (current_class_type))
3329 {
3330 tree b;
3331
3332 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3333 b;
3334 b = TREE_CHAIN (b))
3335 {
3336 tree base_type = BINFO_TYPE (b);
3337 if (CLASS_TYPE_P (base_type)
3338 && dependent_type_p (base_type))
3339 {
3340 tree field;
3341 /* Go from a particular instantiation of the
3342 template (which will have an empty TYPE_FIELDs),
3343 to the main version. */
3344 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3345 for (field = TYPE_FIELDS (base_type);
3346 field;
3347 field = DECL_CHAIN (field))
3348 if (TREE_CODE (field) == TYPE_DECL
3349 && DECL_NAME (field) == id)
3350 {
3351 inform (location,
3352 "(perhaps %<typename %T::%E%> was intended)",
3353 BINFO_TYPE (b), id);
3354 break;
3355 }
3356 if (field)
3357 break;
3358 }
3359 }
3360 }
3361 }
3362 /* Here we diagnose qualified-ids where the scope is actually correct,
3363 but the identifier does not resolve to a valid type name. */
3364 else if (parser->scope != error_mark_node)
3365 {
3366 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3367 {
3368 auto_diagnostic_group d;
3369 name_hint hint;
3370 if (decl == error_mark_node)
3371 hint = suggest_alternative_in_explicit_scope (location, id,
3372 parser->scope);
3373 const char *suggestion = hint.suggestion ();
3374 gcc_rich_location richloc (location_of (id));
3375 if (suggestion)
3376 richloc.add_fixit_replace (suggestion);
3377 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3378 {
3379 if (suggestion)
3380 error_at (&richloc,
3381 "%qE in namespace %qE does not name a template"
3382 " type; did you mean %qs?",
3383 id, parser->scope, suggestion);
3384 else
3385 error_at (&richloc,
3386 "%qE in namespace %qE does not name a template type",
3387 id, parser->scope);
3388 }
3389 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3390 {
3391 if (suggestion)
3392 error_at (&richloc,
3393 "%qE in namespace %qE does not name a template"
3394 " type; did you mean %qs?",
3395 TREE_OPERAND (id, 0), parser->scope, suggestion);
3396 else
3397 error_at (&richloc,
3398 "%qE in namespace %qE does not name a template"
3399 " type",
3400 TREE_OPERAND (id, 0), parser->scope);
3401 }
3402 else
3403 {
3404 if (suggestion)
3405 error_at (&richloc,
3406 "%qE in namespace %qE does not name a type"
3407 "; did you mean %qs?",
3408 id, parser->scope, suggestion);
3409 else
3410 error_at (&richloc,
3411 "%qE in namespace %qE does not name a type",
3412 id, parser->scope);
3413 }
3414 if (DECL_P (decl))
3415 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3416 }
3417 else if (CLASS_TYPE_P (parser->scope)
3418 && constructor_name_p (id, parser->scope))
3419 {
3420 /* A<T>::A<T>() */
3421 auto_diagnostic_group d;
3422 error_at (location, "%<%T::%E%> names the constructor, not"
3423 " the type", parser->scope, id);
3424 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3425 error_at (location, "and %qT has no template constructors",
3426 parser->scope);
3427 }
3428 else if (TYPE_P (parser->scope)
3429 && dependent_scope_p (parser->scope))
3430 {
3431 gcc_rich_location richloc (location);
3432 richloc.add_fixit_insert_before ("typename ");
3433 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3434 error_at (&richloc,
3435 "need %<typename%> before %<%T::%D::%E%> because "
3436 "%<%T::%D%> is a dependent scope",
3437 TYPE_CONTEXT (parser->scope),
3438 TYPENAME_TYPE_FULLNAME (parser->scope),
3439 id,
3440 TYPE_CONTEXT (parser->scope),
3441 TYPENAME_TYPE_FULLNAME (parser->scope));
3442 else
3443 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3444 "%qT is a dependent scope",
3445 parser->scope, id, parser->scope);
3446 }
3447 else if (TYPE_P (parser->scope))
3448 {
3449 auto_diagnostic_group d;
3450 if (!COMPLETE_TYPE_P (parser->scope))
3451 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3452 parser->scope);
3453 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3454 error_at (location_of (id),
3455 "%qE in %q#T does not name a template type",
3456 id, parser->scope);
3457 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3458 error_at (location_of (id),
3459 "%qE in %q#T does not name a template type",
3460 TREE_OPERAND (id, 0), parser->scope);
3461 else
3462 error_at (location_of (id),
3463 "%qE in %q#T does not name a type",
3464 id, parser->scope);
3465 if (DECL_P (decl))
3466 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3467 }
3468 else
3469 gcc_unreachable ();
3470 }
3471 }
3472
3473 /* Check for a common situation where a type-name should be present,
3474 but is not, and issue a sensible error message. Returns true if an
3475 invalid type-name was detected.
3476
3477 The situation handled by this function are variable declarations of the
3478 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3479 Usually, `ID' should name a type, but if we got here it means that it
3480 does not. We try to emit the best possible error message depending on
3481 how exactly the id-expression looks like. */
3482
3483 static bool
3484 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3485 {
3486 tree id;
3487 cp_token *token = cp_lexer_peek_token (parser->lexer);
3488
3489 /* Avoid duplicate error about ambiguous lookup. */
3490 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3491 {
3492 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3493 if (next->type == CPP_NAME && next->error_reported)
3494 goto out;
3495 }
3496
3497 cp_parser_parse_tentatively (parser);
3498 id = cp_parser_id_expression (parser,
3499 /*template_keyword_p=*/false,
3500 /*check_dependency_p=*/true,
3501 /*template_p=*/NULL,
3502 /*declarator_p=*/false,
3503 /*optional_p=*/false);
3504 /* If the next token is a (, this is a function with no explicit return
3505 type, i.e. constructor, destructor or conversion op. */
3506 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3507 || TREE_CODE (id) == TYPE_DECL)
3508 {
3509 cp_parser_abort_tentative_parse (parser);
3510 return false;
3511 }
3512 if (!cp_parser_parse_definitely (parser))
3513 return false;
3514
3515 /* Emit a diagnostic for the invalid type. */
3516 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3517 out:
3518 /* If we aren't in the middle of a declarator (i.e. in a
3519 parameter-declaration-clause), skip to the end of the declaration;
3520 there's no point in trying to process it. */
3521 if (!parser->in_declarator_p)
3522 cp_parser_skip_to_end_of_block_or_statement (parser);
3523 return true;
3524 }
3525
3526 /* Consume tokens up to, and including, the next non-nested closing `)'.
3527 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3528 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3529 found an unnested token of that type. */
3530
3531 static int
3532 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3533 bool recovering,
3534 cpp_ttype or_ttype,
3535 bool consume_paren)
3536 {
3537 unsigned paren_depth = 0;
3538 unsigned brace_depth = 0;
3539 unsigned square_depth = 0;
3540 unsigned condop_depth = 0;
3541
3542 if (recovering && or_ttype == CPP_EOF
3543 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3544 return 0;
3545
3546 while (true)
3547 {
3548 cp_token * token = cp_lexer_peek_token (parser->lexer);
3549
3550 /* Have we found what we're looking for before the closing paren? */
3551 if (token->type == or_ttype && or_ttype != CPP_EOF
3552 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3553 return -1;
3554
3555 switch (token->type)
3556 {
3557 case CPP_EOF:
3558 case CPP_PRAGMA_EOL:
3559 /* If we've run out of tokens, then there is no closing `)'. */
3560 return 0;
3561
3562 /* This is good for lambda expression capture-lists. */
3563 case CPP_OPEN_SQUARE:
3564 ++square_depth;
3565 break;
3566 case CPP_CLOSE_SQUARE:
3567 if (!square_depth--)
3568 return 0;
3569 break;
3570
3571 case CPP_SEMICOLON:
3572 /* This matches the processing in skip_to_end_of_statement. */
3573 if (!brace_depth)
3574 return 0;
3575 break;
3576
3577 case CPP_OPEN_BRACE:
3578 ++brace_depth;
3579 break;
3580 case CPP_CLOSE_BRACE:
3581 if (!brace_depth--)
3582 return 0;
3583 break;
3584
3585 case CPP_OPEN_PAREN:
3586 if (!brace_depth)
3587 ++paren_depth;
3588 break;
3589
3590 case CPP_CLOSE_PAREN:
3591 if (!brace_depth && !paren_depth--)
3592 {
3593 if (consume_paren)
3594 cp_lexer_consume_token (parser->lexer);
3595 return 1;
3596 }
3597 break;
3598
3599 case CPP_QUERY:
3600 if (!brace_depth && !paren_depth && !square_depth)
3601 ++condop_depth;
3602 break;
3603
3604 case CPP_COLON:
3605 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3606 condop_depth--;
3607 break;
3608
3609 default:
3610 break;
3611 }
3612
3613 /* Consume the token. */
3614 cp_lexer_consume_token (parser->lexer);
3615 }
3616 }
3617
3618 /* Consume tokens up to, and including, the next non-nested closing `)'.
3619 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3620 are doing error recovery. Returns -1 if OR_COMMA is true and we
3621 found an unnested token of that type. */
3622
3623 static int
3624 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3625 bool recovering,
3626 bool or_comma,
3627 bool consume_paren)
3628 {
3629 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3630 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3631 ttype, consume_paren);
3632 }
3633
3634 /* Consume tokens until we reach the end of the current statement.
3635 Normally, that will be just before consuming a `;'. However, if a
3636 non-nested `}' comes first, then we stop before consuming that. */
3637
3638 static void
3639 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3640 {
3641 unsigned nesting_depth = 0;
3642
3643 /* Unwind generic function template scope if necessary. */
3644 if (parser->fully_implicit_function_template_p)
3645 abort_fully_implicit_template (parser);
3646
3647 while (true)
3648 {
3649 cp_token *token = cp_lexer_peek_token (parser->lexer);
3650
3651 switch (token->type)
3652 {
3653 case CPP_EOF:
3654 case CPP_PRAGMA_EOL:
3655 /* If we've run out of tokens, stop. */
3656 return;
3657
3658 case CPP_SEMICOLON:
3659 /* If the next token is a `;', we have reached the end of the
3660 statement. */
3661 if (!nesting_depth)
3662 return;
3663 break;
3664
3665 case CPP_CLOSE_BRACE:
3666 /* If this is a non-nested '}', stop before consuming it.
3667 That way, when confronted with something like:
3668
3669 { 3 + }
3670
3671 we stop before consuming the closing '}', even though we
3672 have not yet reached a `;'. */
3673 if (nesting_depth == 0)
3674 return;
3675
3676 /* If it is the closing '}' for a block that we have
3677 scanned, stop -- but only after consuming the token.
3678 That way given:
3679
3680 void f g () { ... }
3681 typedef int I;
3682
3683 we will stop after the body of the erroneously declared
3684 function, but before consuming the following `typedef'
3685 declaration. */
3686 if (--nesting_depth == 0)
3687 {
3688 cp_lexer_consume_token (parser->lexer);
3689 return;
3690 }
3691 break;
3692
3693 case CPP_OPEN_BRACE:
3694 ++nesting_depth;
3695 break;
3696
3697 default:
3698 break;
3699 }
3700
3701 /* Consume the token. */
3702 cp_lexer_consume_token (parser->lexer);
3703 }
3704 }
3705
3706 /* This function is called at the end of a statement or declaration.
3707 If the next token is a semicolon, it is consumed; otherwise, error
3708 recovery is attempted. */
3709
3710 static void
3711 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3712 {
3713 /* Look for the trailing `;'. */
3714 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3715 {
3716 /* If there is additional (erroneous) input, skip to the end of
3717 the statement. */
3718 cp_parser_skip_to_end_of_statement (parser);
3719 /* If the next token is now a `;', consume it. */
3720 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3721 cp_lexer_consume_token (parser->lexer);
3722 }
3723 }
3724
3725 /* Skip tokens until we have consumed an entire block, or until we
3726 have consumed a non-nested `;'. */
3727
3728 static void
3729 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3730 {
3731 int nesting_depth = 0;
3732
3733 /* Unwind generic function template scope if necessary. */
3734 if (parser->fully_implicit_function_template_p)
3735 abort_fully_implicit_template (parser);
3736
3737 while (nesting_depth >= 0)
3738 {
3739 cp_token *token = cp_lexer_peek_token (parser->lexer);
3740
3741 switch (token->type)
3742 {
3743 case CPP_EOF:
3744 case CPP_PRAGMA_EOL:
3745 /* If we've run out of tokens, stop. */
3746 return;
3747
3748 case CPP_SEMICOLON:
3749 /* Stop if this is an unnested ';'. */
3750 if (!nesting_depth)
3751 nesting_depth = -1;
3752 break;
3753
3754 case CPP_CLOSE_BRACE:
3755 /* Stop if this is an unnested '}', or closes the outermost
3756 nesting level. */
3757 nesting_depth--;
3758 if (nesting_depth < 0)
3759 return;
3760 if (!nesting_depth)
3761 nesting_depth = -1;
3762 break;
3763
3764 case CPP_OPEN_BRACE:
3765 /* Nest. */
3766 nesting_depth++;
3767 break;
3768
3769 default:
3770 break;
3771 }
3772
3773 /* Consume the token. */
3774 cp_lexer_consume_token (parser->lexer);
3775 }
3776 }
3777
3778 /* Skip tokens until a non-nested closing curly brace is the next
3779 token, or there are no more tokens. Return true in the first case,
3780 false otherwise. */
3781
3782 static bool
3783 cp_parser_skip_to_closing_brace (cp_parser *parser)
3784 {
3785 unsigned nesting_depth = 0;
3786
3787 while (true)
3788 {
3789 cp_token *token = cp_lexer_peek_token (parser->lexer);
3790
3791 switch (token->type)
3792 {
3793 case CPP_EOF:
3794 case CPP_PRAGMA_EOL:
3795 /* If we've run out of tokens, stop. */
3796 return false;
3797
3798 case CPP_CLOSE_BRACE:
3799 /* If the next token is a non-nested `}', then we have reached
3800 the end of the current block. */
3801 if (nesting_depth-- == 0)
3802 return true;
3803 break;
3804
3805 case CPP_OPEN_BRACE:
3806 /* If it the next token is a `{', then we are entering a new
3807 block. Consume the entire block. */
3808 ++nesting_depth;
3809 break;
3810
3811 default:
3812 break;
3813 }
3814
3815 /* Consume the token. */
3816 cp_lexer_consume_token (parser->lexer);
3817 }
3818 }
3819
3820 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3821 parameter is the PRAGMA token, allowing us to purge the entire pragma
3822 sequence. */
3823
3824 static void
3825 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3826 {
3827 cp_token *token;
3828
3829 parser->lexer->in_pragma = false;
3830
3831 do
3832 token = cp_lexer_consume_token (parser->lexer);
3833 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3834
3835 /* Ensure that the pragma is not parsed again. */
3836 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3837 }
3838
3839 /* Require pragma end of line, resyncing with it as necessary. The
3840 arguments are as for cp_parser_skip_to_pragma_eol. */
3841
3842 static void
3843 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3844 {
3845 parser->lexer->in_pragma = false;
3846 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3847 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3848 }
3849
3850 /* This is a simple wrapper around make_typename_type. When the id is
3851 an unresolved identifier node, we can provide a superior diagnostic
3852 using cp_parser_diagnose_invalid_type_name. */
3853
3854 static tree
3855 cp_parser_make_typename_type (cp_parser *parser, tree id,
3856 location_t id_location)
3857 {
3858 tree result;
3859 if (identifier_p (id))
3860 {
3861 result = make_typename_type (parser->scope, id, typename_type,
3862 /*complain=*/tf_none);
3863 if (result == error_mark_node)
3864 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3865 return result;
3866 }
3867 return make_typename_type (parser->scope, id, typename_type, tf_error);
3868 }
3869
3870 /* This is a wrapper around the
3871 make_{pointer,ptrmem,reference}_declarator functions that decides
3872 which one to call based on the CODE and CLASS_TYPE arguments. The
3873 CODE argument should be one of the values returned by
3874 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3875 appertain to the pointer or reference. */
3876
3877 static cp_declarator *
3878 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3879 cp_cv_quals cv_qualifiers,
3880 cp_declarator *target,
3881 tree attributes)
3882 {
3883 if (code == ERROR_MARK || target == cp_error_declarator)
3884 return cp_error_declarator;
3885
3886 if (code == INDIRECT_REF)
3887 if (class_type == NULL_TREE)
3888 return make_pointer_declarator (cv_qualifiers, target, attributes);
3889 else
3890 return make_ptrmem_declarator (cv_qualifiers, class_type,
3891 target, attributes);
3892 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3893 return make_reference_declarator (cv_qualifiers, target,
3894 false, attributes);
3895 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3896 return make_reference_declarator (cv_qualifiers, target,
3897 true, attributes);
3898 gcc_unreachable ();
3899 }
3900
3901 /* Create a new C++ parser. */
3902
3903 static cp_parser *
3904 cp_parser_new (void)
3905 {
3906 cp_parser *parser;
3907 cp_lexer *lexer;
3908 unsigned i;
3909
3910 /* cp_lexer_new_main is called before doing GC allocation because
3911 cp_lexer_new_main might load a PCH file. */
3912 lexer = cp_lexer_new_main ();
3913
3914 /* Initialize the binops_by_token so that we can get the tree
3915 directly from the token. */
3916 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3917 binops_by_token[binops[i].token_type] = binops[i];
3918
3919 parser = ggc_cleared_alloc<cp_parser> ();
3920 parser->lexer = lexer;
3921 parser->context = cp_parser_context_new (NULL);
3922
3923 /* For now, we always accept GNU extensions. */
3924 parser->allow_gnu_extensions_p = 1;
3925
3926 /* The `>' token is a greater-than operator, not the end of a
3927 template-id. */
3928 parser->greater_than_is_operator_p = true;
3929
3930 parser->default_arg_ok_p = true;
3931
3932 /* We are not parsing a constant-expression. */
3933 parser->integral_constant_expression_p = false;
3934 parser->allow_non_integral_constant_expression_p = false;
3935 parser->non_integral_constant_expression_p = false;
3936
3937 /* Local variable names are not forbidden. */
3938 parser->local_variables_forbidden_p = false;
3939
3940 /* We are not processing an `extern "C"' declaration. */
3941 parser->in_unbraced_linkage_specification_p = false;
3942
3943 /* We are not processing a declarator. */
3944 parser->in_declarator_p = false;
3945
3946 /* We are not processing a template-argument-list. */
3947 parser->in_template_argument_list_p = false;
3948
3949 /* We are not in an iteration statement. */
3950 parser->in_statement = 0;
3951
3952 /* We are not in a switch statement. */
3953 parser->in_switch_statement_p = false;
3954
3955 /* We are not parsing a type-id inside an expression. */
3956 parser->in_type_id_in_expr_p = false;
3957
3958 /* String literals should be translated to the execution character set. */
3959 parser->translate_strings_p = true;
3960
3961 /* We are not parsing a function body. */
3962 parser->in_function_body = false;
3963
3964 /* We can correct until told otherwise. */
3965 parser->colon_corrects_to_scope_p = true;
3966
3967 /* The unparsed function queue is empty. */
3968 push_unparsed_function_queues (parser);
3969
3970 /* There are no classes being defined. */
3971 parser->num_classes_being_defined = 0;
3972
3973 /* No template parameters apply. */
3974 parser->num_template_parameter_lists = 0;
3975
3976 /* Special parsing data structures. */
3977 parser->omp_declare_simd = NULL;
3978 parser->oacc_routine = NULL;
3979
3980 /* Not declaring an implicit function template. */
3981 parser->auto_is_implicit_function_template_parm_p = false;
3982 parser->fully_implicit_function_template_p = false;
3983 parser->implicit_template_parms = 0;
3984 parser->implicit_template_scope = 0;
3985
3986 /* Allow constrained-type-specifiers. */
3987 parser->prevent_constrained_type_specifiers = 0;
3988
3989 /* We haven't yet seen an 'extern "C"'. */
3990 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3991
3992 return parser;
3993 }
3994
3995 /* Create a cp_lexer structure which will emit the tokens in CACHE
3996 and push it onto the parser's lexer stack. This is used for delayed
3997 parsing of in-class method bodies and default arguments, and should
3998 not be confused with tentative parsing. */
3999 static void
4000 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4001 {
4002 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4003 lexer->next = parser->lexer;
4004 parser->lexer = lexer;
4005
4006 /* Move the current source position to that of the first token in the
4007 new lexer. */
4008 cp_lexer_set_source_position_from_token (lexer->next_token);
4009 }
4010
4011 /* Pop the top lexer off the parser stack. This is never used for the
4012 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4013 static void
4014 cp_parser_pop_lexer (cp_parser *parser)
4015 {
4016 cp_lexer *lexer = parser->lexer;
4017 parser->lexer = lexer->next;
4018 cp_lexer_destroy (lexer);
4019
4020 /* Put the current source position back where it was before this
4021 lexer was pushed. */
4022 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4023 }
4024
4025 /* Lexical conventions [gram.lex] */
4026
4027 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4028 identifier. */
4029
4030 static cp_expr
4031 cp_parser_identifier (cp_parser* parser)
4032 {
4033 cp_token *token;
4034
4035 /* Look for the identifier. */
4036 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4037 /* Return the value. */
4038 if (token)
4039 return cp_expr (token->u.value, token->location);
4040 else
4041 return error_mark_node;
4042 }
4043
4044 /* Parse a sequence of adjacent string constants. Returns a
4045 TREE_STRING representing the combined, nul-terminated string
4046 constant. If TRANSLATE is true, translate the string to the
4047 execution character set. If WIDE_OK is true, a wide string is
4048 invalid here.
4049
4050 C++98 [lex.string] says that if a narrow string literal token is
4051 adjacent to a wide string literal token, the behavior is undefined.
4052 However, C99 6.4.5p4 says that this results in a wide string literal.
4053 We follow C99 here, for consistency with the C front end.
4054
4055 This code is largely lifted from lex_string() in c-lex.c.
4056
4057 FUTURE: ObjC++ will need to handle @-strings here. */
4058 static cp_expr
4059 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4060 bool lookup_udlit = true)
4061 {
4062 tree value;
4063 size_t count;
4064 struct obstack str_ob;
4065 struct obstack loc_ob;
4066 cpp_string str, istr, *strs;
4067 cp_token *tok;
4068 enum cpp_ttype type, curr_type;
4069 int have_suffix_p = 0;
4070 tree string_tree;
4071 tree suffix_id = NULL_TREE;
4072 bool curr_tok_is_userdef_p = false;
4073
4074 tok = cp_lexer_peek_token (parser->lexer);
4075 if (!cp_parser_is_string_literal (tok))
4076 {
4077 cp_parser_error (parser, "expected string-literal");
4078 return error_mark_node;
4079 }
4080
4081 location_t loc = tok->location;
4082
4083 if (cpp_userdef_string_p (tok->type))
4084 {
4085 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4086 curr_type = cpp_userdef_string_remove_type (tok->type);
4087 curr_tok_is_userdef_p = true;
4088 }
4089 else
4090 {
4091 string_tree = tok->u.value;
4092 curr_type = tok->type;
4093 }
4094 type = curr_type;
4095
4096 /* Try to avoid the overhead of creating and destroying an obstack
4097 for the common case of just one string. */
4098 if (!cp_parser_is_string_literal
4099 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4100 {
4101 cp_lexer_consume_token (parser->lexer);
4102
4103 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4104 str.len = TREE_STRING_LENGTH (string_tree);
4105 count = 1;
4106
4107 if (curr_tok_is_userdef_p)
4108 {
4109 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4110 have_suffix_p = 1;
4111 curr_type = cpp_userdef_string_remove_type (tok->type);
4112 }
4113 else
4114 curr_type = tok->type;
4115
4116 strs = &str;
4117 }
4118 else
4119 {
4120 location_t last_tok_loc = tok->location;
4121 gcc_obstack_init (&str_ob);
4122 gcc_obstack_init (&loc_ob);
4123 count = 0;
4124
4125 do
4126 {
4127 cp_lexer_consume_token (parser->lexer);
4128 count++;
4129 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4130 str.len = TREE_STRING_LENGTH (string_tree);
4131
4132 if (curr_tok_is_userdef_p)
4133 {
4134 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4135 if (have_suffix_p == 0)
4136 {
4137 suffix_id = curr_suffix_id;
4138 have_suffix_p = 1;
4139 }
4140 else if (have_suffix_p == 1
4141 && curr_suffix_id != suffix_id)
4142 {
4143 error ("inconsistent user-defined literal suffixes"
4144 " %qD and %qD in string literal",
4145 suffix_id, curr_suffix_id);
4146 have_suffix_p = -1;
4147 }
4148 curr_type = cpp_userdef_string_remove_type (tok->type);
4149 }
4150 else
4151 curr_type = tok->type;
4152
4153 if (type != curr_type)
4154 {
4155 if (type == CPP_STRING)
4156 type = curr_type;
4157 else if (curr_type != CPP_STRING)
4158 {
4159 rich_location rich_loc (line_table, tok->location);
4160 rich_loc.add_range (last_tok_loc);
4161 error_at (&rich_loc,
4162 "unsupported non-standard concatenation "
4163 "of string literals");
4164 }
4165 }
4166
4167 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4168 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4169
4170 last_tok_loc = tok->location;
4171
4172 tok = cp_lexer_peek_token (parser->lexer);
4173 if (cpp_userdef_string_p (tok->type))
4174 {
4175 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4176 curr_type = cpp_userdef_string_remove_type (tok->type);
4177 curr_tok_is_userdef_p = true;
4178 }
4179 else
4180 {
4181 string_tree = tok->u.value;
4182 curr_type = tok->type;
4183 curr_tok_is_userdef_p = false;
4184 }
4185 }
4186 while (cp_parser_is_string_literal (tok));
4187
4188 /* A string literal built by concatenation has its caret=start at
4189 the start of the initial string, and its finish at the finish of
4190 the final string literal. */
4191 loc = make_location (loc, loc, get_finish (last_tok_loc));
4192
4193 strs = (cpp_string *) obstack_finish (&str_ob);
4194 }
4195
4196 if (type != CPP_STRING && !wide_ok)
4197 {
4198 cp_parser_error (parser, "a wide string is invalid in this context");
4199 type = CPP_STRING;
4200 }
4201
4202 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4203 (parse_in, strs, count, &istr, type))
4204 {
4205 value = build_string (istr.len, (const char *)istr.text);
4206 free (CONST_CAST (unsigned char *, istr.text));
4207 if (count > 1)
4208 {
4209 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4210 gcc_assert (g_string_concat_db);
4211 g_string_concat_db->record_string_concatenation (count, locs);
4212 }
4213
4214 switch (type)
4215 {
4216 default:
4217 case CPP_STRING:
4218 case CPP_UTF8STRING:
4219 TREE_TYPE (value) = char_array_type_node;
4220 break;
4221 case CPP_STRING16:
4222 TREE_TYPE (value) = char16_array_type_node;
4223 break;
4224 case CPP_STRING32:
4225 TREE_TYPE (value) = char32_array_type_node;
4226 break;
4227 case CPP_WSTRING:
4228 TREE_TYPE (value) = wchar_array_type_node;
4229 break;
4230 }
4231
4232 value = fix_string_type (value);
4233
4234 if (have_suffix_p)
4235 {
4236 tree literal = build_userdef_literal (suffix_id, value,
4237 OT_NONE, NULL_TREE);
4238 if (lookup_udlit)
4239 value = cp_parser_userdef_string_literal (literal);
4240 else
4241 value = literal;
4242 }
4243 }
4244 else
4245 /* cpp_interpret_string has issued an error. */
4246 value = error_mark_node;
4247
4248 if (count > 1)
4249 {
4250 obstack_free (&str_ob, 0);
4251 obstack_free (&loc_ob, 0);
4252 }
4253
4254 return cp_expr (value, loc);
4255 }
4256
4257 /* Look up a literal operator with the name and the exact arguments. */
4258
4259 static tree
4260 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4261 {
4262 tree 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 tree fn = *iter;
4269
4270 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4271 {
4272 unsigned int ix;
4273 bool found = true;
4274
4275 for (ix = 0;
4276 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4277 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4278 {
4279 tree tparm = TREE_VALUE (parmtypes);
4280 tree targ = TREE_TYPE ((*args)[ix]);
4281 bool ptr = TYPE_PTR_P (tparm);
4282 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4283 if ((ptr || arr || !same_type_p (tparm, targ))
4284 && (!ptr || !arr
4285 || !same_type_p (TREE_TYPE (tparm),
4286 TREE_TYPE (targ))))
4287 found = false;
4288 }
4289
4290 if (found
4291 && ix == vec_safe_length (args)
4292 /* May be this should be sufficient_parms_p instead,
4293 depending on how exactly should user-defined literals
4294 work in presence of default arguments on the literal
4295 operator parameters. */
4296 && parmtypes == void_list_node)
4297 return decl;
4298 }
4299 }
4300
4301 return error_mark_node;
4302 }
4303
4304 /* Parse a user-defined char constant. Returns a call to a user-defined
4305 literal operator taking the character as an argument. */
4306
4307 static cp_expr
4308 cp_parser_userdef_char_literal (cp_parser *parser)
4309 {
4310 cp_token *token = cp_lexer_consume_token (parser->lexer);
4311 tree literal = token->u.value;
4312 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4313 tree value = USERDEF_LITERAL_VALUE (literal);
4314 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4315 tree decl, result;
4316
4317 /* Build up a call to the user-defined operator */
4318 /* Lookup the name we got back from the id-expression. */
4319 vec<tree, va_gc> *args = make_tree_vector ();
4320 vec_safe_push (args, value);
4321 decl = lookup_literal_operator (name, args);
4322 if (!decl || decl == error_mark_node)
4323 {
4324 error ("unable to find character literal operator %qD with %qT argument",
4325 name, TREE_TYPE (value));
4326 release_tree_vector (args);
4327 return error_mark_node;
4328 }
4329 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4330 release_tree_vector (args);
4331 return result;
4332 }
4333
4334 /* A subroutine of cp_parser_userdef_numeric_literal to
4335 create a char... template parameter pack from a string node. */
4336
4337 static tree
4338 make_char_string_pack (tree value)
4339 {
4340 tree charvec;
4341 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4342 const char *str = TREE_STRING_POINTER (value);
4343 int i, len = TREE_STRING_LENGTH (value) - 1;
4344 tree argvec = make_tree_vec (1);
4345
4346 /* Fill in CHARVEC with all of the parameters. */
4347 charvec = make_tree_vec (len);
4348 for (i = 0; i < len; ++i)
4349 {
4350 unsigned char s[3] = { '\'', str[i], '\'' };
4351 cpp_string in = { 3, s };
4352 cpp_string out = { 0, 0 };
4353 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4354 return NULL_TREE;
4355 gcc_assert (out.len == 2);
4356 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4357 out.text[0]);
4358 }
4359
4360 /* Build the argument packs. */
4361 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4362
4363 TREE_VEC_ELT (argvec, 0) = argpack;
4364
4365 return argvec;
4366 }
4367
4368 /* A subroutine of cp_parser_userdef_numeric_literal to
4369 create a char... template parameter pack from a string node. */
4370
4371 static tree
4372 make_string_pack (tree value)
4373 {
4374 tree charvec;
4375 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4376 const unsigned char *str
4377 = (const unsigned char *) TREE_STRING_POINTER (value);
4378 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4379 int len = TREE_STRING_LENGTH (value) / sz - 1;
4380 tree argvec = make_tree_vec (2);
4381
4382 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4383 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4384
4385 /* First template parm is character type. */
4386 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4387
4388 /* Fill in CHARVEC with all of the parameters. */
4389 charvec = make_tree_vec (len);
4390 for (int i = 0; i < len; ++i)
4391 TREE_VEC_ELT (charvec, i)
4392 = double_int_to_tree (str_char_type_node,
4393 double_int::from_buffer (str + i * sz, sz));
4394
4395 /* Build the argument packs. */
4396 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4397
4398 TREE_VEC_ELT (argvec, 1) = argpack;
4399
4400 return argvec;
4401 }
4402
4403 /* Parse a user-defined numeric constant. returns a call to a user-defined
4404 literal operator. */
4405
4406 static cp_expr
4407 cp_parser_userdef_numeric_literal (cp_parser *parser)
4408 {
4409 cp_token *token = cp_lexer_consume_token (parser->lexer);
4410 tree literal = token->u.value;
4411 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4412 tree value = USERDEF_LITERAL_VALUE (literal);
4413 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4414 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4415 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4416 tree decl, result;
4417 vec<tree, va_gc> *args;
4418
4419 /* Look for a literal operator taking the exact type of numeric argument
4420 as the literal value. */
4421 args = make_tree_vector ();
4422 vec_safe_push (args, value);
4423 decl = lookup_literal_operator (name, args);
4424 if (decl && decl != error_mark_node)
4425 {
4426 result = finish_call_expr (decl, &args, false, true,
4427 tf_warning_or_error);
4428
4429 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4430 {
4431 warning_at (token->location, OPT_Woverflow,
4432 "integer literal exceeds range of %qT type",
4433 long_long_unsigned_type_node);
4434 }
4435 else
4436 {
4437 if (overflow > 0)
4438 warning_at (token->location, OPT_Woverflow,
4439 "floating literal exceeds range of %qT type",
4440 long_double_type_node);
4441 else if (overflow < 0)
4442 warning_at (token->location, OPT_Woverflow,
4443 "floating literal truncated to zero");
4444 }
4445
4446 release_tree_vector (args);
4447 return result;
4448 }
4449 release_tree_vector (args);
4450
4451 /* If the numeric argument didn't work, look for a raw literal
4452 operator taking a const char* argument consisting of the number
4453 in string format. */
4454 args = make_tree_vector ();
4455 vec_safe_push (args, num_string);
4456 decl = lookup_literal_operator (name, args);
4457 if (decl && decl != error_mark_node)
4458 {
4459 result = finish_call_expr (decl, &args, false, true,
4460 tf_warning_or_error);
4461 release_tree_vector (args);
4462 return result;
4463 }
4464 release_tree_vector (args);
4465
4466 /* If the raw literal didn't work, look for a non-type template
4467 function with parameter pack char.... Call the function with
4468 template parameter characters representing the number. */
4469 args = make_tree_vector ();
4470 decl = lookup_literal_operator (name, args);
4471 if (decl && decl != error_mark_node)
4472 {
4473 tree tmpl_args = make_char_string_pack (num_string);
4474 if (tmpl_args == NULL_TREE)
4475 {
4476 error ("failed to translate literal to execution character set %qT",
4477 num_string);
4478 return error_mark_node;
4479 }
4480 decl = lookup_template_function (decl, tmpl_args);
4481 result = finish_call_expr (decl, &args, false, true,
4482 tf_warning_or_error);
4483 release_tree_vector (args);
4484 return result;
4485 }
4486
4487 release_tree_vector (args);
4488
4489 /* In C++14 the standard library defines complex number suffixes that
4490 conflict with GNU extensions. Prefer them if <complex> is #included. */
4491 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4492 bool i14 = (cxx_dialect > cxx11
4493 && (id_equal (suffix_id, "i")
4494 || id_equal (suffix_id, "if")
4495 || id_equal (suffix_id, "il")));
4496 diagnostic_t kind = DK_ERROR;
4497 int opt = 0;
4498
4499 if (i14 && ext)
4500 {
4501 tree cxlit = lookup_qualified_name (std_node,
4502 get_identifier ("complex_literals"),
4503 0, false, false);
4504 if (cxlit == error_mark_node)
4505 {
4506 /* No <complex>, so pedwarn and use GNU semantics. */
4507 kind = DK_PEDWARN;
4508 opt = OPT_Wpedantic;
4509 }
4510 }
4511
4512 bool complained
4513 = emit_diagnostic (kind, input_location, opt,
4514 "unable to find numeric literal operator %qD", name);
4515
4516 if (!complained)
4517 /* Don't inform either. */;
4518 else if (i14)
4519 {
4520 inform (token->location, "add %<using namespace std::complex_literals%> "
4521 "(from <complex>) to enable the C++14 user-defined literal "
4522 "suffixes");
4523 if (ext)
4524 inform (token->location, "or use %<j%> instead of %<i%> for the "
4525 "GNU built-in suffix");
4526 }
4527 else if (!ext)
4528 inform (token->location, "use -fext-numeric-literals "
4529 "to enable more built-in suffixes");
4530
4531 if (kind == DK_ERROR)
4532 value = error_mark_node;
4533 else
4534 {
4535 /* Use the built-in semantics. */
4536 tree type;
4537 if (id_equal (suffix_id, "i"))
4538 {
4539 if (TREE_CODE (value) == INTEGER_CST)
4540 type = integer_type_node;
4541 else
4542 type = double_type_node;
4543 }
4544 else if (id_equal (suffix_id, "if"))
4545 type = float_type_node;
4546 else /* if (id_equal (suffix_id, "il")) */
4547 type = long_double_type_node;
4548
4549 value = build_complex (build_complex_type (type),
4550 fold_convert (type, integer_zero_node),
4551 fold_convert (type, value));
4552 }
4553
4554 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4555 /* Avoid repeated diagnostics. */
4556 token->u.value = value;
4557 return value;
4558 }
4559
4560 /* Parse a user-defined string constant. Returns a call to a user-defined
4561 literal operator taking a character pointer and the length of the string
4562 as arguments. */
4563
4564 static tree
4565 cp_parser_userdef_string_literal (tree literal)
4566 {
4567 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4568 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4569 tree value = USERDEF_LITERAL_VALUE (literal);
4570 int len = TREE_STRING_LENGTH (value)
4571 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4572 tree decl;
4573
4574 /* Build up a call to the user-defined operator. */
4575 /* Lookup the name we got back from the id-expression. */
4576 releasing_vec rargs;
4577 vec<tree, va_gc> *&args = rargs.get_ref();
4578 vec_safe_push (args, value);
4579 vec_safe_push (args, build_int_cst (size_type_node, len));
4580 decl = lookup_literal_operator (name, args);
4581
4582 if (decl && decl != error_mark_node)
4583 return finish_call_expr (decl, &args, false, true,
4584 tf_warning_or_error);
4585
4586 /* Look for a suitable template function, either (C++20) with a single
4587 parameter of class type, or (N3599) with typename parameter CharT and
4588 parameter pack CharT... */
4589 args->truncate (0);
4590 decl = lookup_literal_operator (name, args);
4591 if (decl && decl != error_mark_node)
4592 {
4593 /* Use resolve_nondeduced_context to try to choose one form of template
4594 or the other. */
4595 tree tmpl_args = make_tree_vec (1);
4596 TREE_VEC_ELT (tmpl_args, 0) = value;
4597 decl = lookup_template_function (decl, tmpl_args);
4598 tree res = resolve_nondeduced_context (decl, tf_none);
4599 if (DECL_P (res))
4600 decl = res;
4601 else
4602 {
4603 TREE_OPERAND (decl, 1) = make_string_pack (value);
4604 res = resolve_nondeduced_context (decl, tf_none);
4605 if (DECL_P (res))
4606 decl = res;
4607 }
4608 if (!DECL_P (decl) && cxx_dialect > cxx17)
4609 TREE_OPERAND (decl, 1) = tmpl_args;
4610 return finish_call_expr (decl, &args, false, true,
4611 tf_warning_or_error);
4612 }
4613
4614 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4615 name, TREE_TYPE (value), size_type_node);
4616 return error_mark_node;
4617 }
4618
4619
4620 /* Basic concepts [gram.basic] */
4621
4622 /* Parse a translation-unit.
4623
4624 translation-unit:
4625 declaration-seq [opt] */
4626
4627 static void
4628 cp_parser_translation_unit (cp_parser* parser)
4629 {
4630 gcc_checking_assert (!cp_error_declarator);
4631
4632 /* Create the declarator obstack. */
4633 gcc_obstack_init (&declarator_obstack);
4634 /* Create the error declarator. */
4635 cp_error_declarator = make_declarator (cdk_error);
4636 /* Create the empty parameter list. */
4637 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4638 UNKNOWN_LOCATION);
4639 /* Remember where the base of the declarator obstack lies. */
4640 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4641
4642 bool implicit_extern_c = false;
4643
4644 for (;;)
4645 {
4646 cp_token *token = cp_lexer_peek_token (parser->lexer);
4647
4648 /* If we're entering or exiting a region that's implicitly
4649 extern "C", modify the lang context appropriately. */
4650 if (implicit_extern_c
4651 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4652 {
4653 implicit_extern_c = !implicit_extern_c;
4654 if (implicit_extern_c)
4655 push_lang_context (lang_name_c);
4656 else
4657 pop_lang_context ();
4658 }
4659
4660 if (token->type == CPP_EOF)
4661 break;
4662
4663 if (token->type == CPP_CLOSE_BRACE)
4664 {
4665 cp_parser_error (parser, "expected declaration");
4666 cp_lexer_consume_token (parser->lexer);
4667 /* If the next token is now a `;', consume it. */
4668 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4669 cp_lexer_consume_token (parser->lexer);
4670 }
4671 else
4672 cp_parser_toplevel_declaration (parser);
4673 }
4674
4675 /* Get rid of the token array; we don't need it any more. */
4676 cp_lexer_destroy (parser->lexer);
4677 parser->lexer = NULL;
4678
4679 /* The EOF should have reset this. */
4680 gcc_checking_assert (!implicit_extern_c);
4681
4682 /* Make sure the declarator obstack was fully cleaned up. */
4683 gcc_assert (obstack_next_free (&declarator_obstack)
4684 == declarator_obstack_base);
4685 }
4686
4687 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4688 decltype context. */
4689
4690 static inline tsubst_flags_t
4691 complain_flags (bool decltype_p)
4692 {
4693 tsubst_flags_t complain = tf_warning_or_error;
4694 if (decltype_p)
4695 complain |= tf_decltype;
4696 return complain;
4697 }
4698
4699 /* We're about to parse a collection of statements. If we're currently
4700 parsing tentatively, set up a firewall so that any nested
4701 cp_parser_commit_to_tentative_parse won't affect the current context. */
4702
4703 static cp_token_position
4704 cp_parser_start_tentative_firewall (cp_parser *parser)
4705 {
4706 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4707 return 0;
4708
4709 cp_parser_parse_tentatively (parser);
4710 cp_parser_commit_to_topmost_tentative_parse (parser);
4711 return cp_lexer_token_position (parser->lexer, false);
4712 }
4713
4714 /* We've finished parsing the collection of statements. Wrap up the
4715 firewall and replace the relevant tokens with the parsed form. */
4716
4717 static void
4718 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4719 tree expr)
4720 {
4721 if (!start)
4722 return;
4723
4724 /* Finish the firewall level. */
4725 cp_parser_parse_definitely (parser);
4726 /* And remember the result of the parse for when we try again. */
4727 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4728 token->type = CPP_PREPARSED_EXPR;
4729 token->u.value = expr;
4730 token->keyword = RID_MAX;
4731 cp_lexer_purge_tokens_after (parser->lexer, start);
4732 }
4733
4734 /* Like the above functions, but let the user modify the tokens. Used by
4735 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4736 later parses, so it makes sense to localize the effects of
4737 cp_parser_commit_to_tentative_parse. */
4738
4739 struct tentative_firewall
4740 {
4741 cp_parser *parser;
4742 bool set;
4743
4744 tentative_firewall (cp_parser *p): parser(p)
4745 {
4746 /* If we're currently parsing tentatively, start a committed level as a
4747 firewall and then an inner tentative parse. */
4748 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4749 {
4750 cp_parser_parse_tentatively (parser);
4751 cp_parser_commit_to_topmost_tentative_parse (parser);
4752 cp_parser_parse_tentatively (parser);
4753 }
4754 }
4755
4756 ~tentative_firewall()
4757 {
4758 if (set)
4759 {
4760 /* Finish the inner tentative parse and the firewall, propagating any
4761 uncommitted error state to the outer tentative parse. */
4762 bool err = cp_parser_error_occurred (parser);
4763 cp_parser_parse_definitely (parser);
4764 cp_parser_parse_definitely (parser);
4765 if (err)
4766 cp_parser_simulate_error (parser);
4767 }
4768 }
4769 };
4770
4771 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4772 This class is for tracking such a matching pair of symbols.
4773 In particular, it tracks the location of the first token,
4774 so that if the second token is missing, we can highlight the
4775 location of the first token when notifying the user about the
4776 problem. */
4777
4778 template <typename traits_t>
4779 class token_pair
4780 {
4781 public:
4782 /* token_pair's ctor. */
4783 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4784
4785 /* If the next token is the opening symbol for this pair, consume it and
4786 return true.
4787 Otherwise, issue an error and return false.
4788 In either case, record the location of the opening token. */
4789
4790 bool require_open (cp_parser *parser)
4791 {
4792 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4793 return cp_parser_require (parser, traits_t::open_token_type,
4794 traits_t::required_token_open);
4795 }
4796
4797 /* Consume the next token from PARSER, recording its location as
4798 that of the opening token within the pair. */
4799
4800 cp_token * consume_open (cp_parser *parser)
4801 {
4802 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4803 gcc_assert (tok->type == traits_t::open_token_type);
4804 m_open_loc = tok->location;
4805 return tok;
4806 }
4807
4808 /* If the next token is the closing symbol for this pair, consume it
4809 and return it.
4810 Otherwise, issue an error, highlighting the location of the
4811 corresponding opening token, and return NULL. */
4812
4813 cp_token *require_close (cp_parser *parser) const
4814 {
4815 return cp_parser_require (parser, traits_t::close_token_type,
4816 traits_t::required_token_close,
4817 m_open_loc);
4818 }
4819
4820 private:
4821 location_t m_open_loc;
4822 };
4823
4824 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4825
4826 struct matching_paren_traits
4827 {
4828 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4829 static const enum required_token required_token_open = RT_OPEN_PAREN;
4830 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4831 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4832 };
4833
4834 /* "matching_parens" is a token_pair<T> class for tracking matching
4835 pairs of parentheses. */
4836
4837 typedef token_pair<matching_paren_traits> matching_parens;
4838
4839 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4840
4841 struct matching_brace_traits
4842 {
4843 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4844 static const enum required_token required_token_open = RT_OPEN_BRACE;
4845 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4846 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4847 };
4848
4849 /* "matching_braces" is a token_pair<T> class for tracking matching
4850 pairs of braces. */
4851
4852 typedef token_pair<matching_brace_traits> matching_braces;
4853
4854
4855 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4856 enclosing parentheses. */
4857
4858 static cp_expr
4859 cp_parser_statement_expr (cp_parser *parser)
4860 {
4861 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4862
4863 /* Consume the '('. */
4864 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4865 matching_parens parens;
4866 parens.consume_open (parser);
4867 /* Start the statement-expression. */
4868 tree expr = begin_stmt_expr ();
4869 /* Parse the compound-statement. */
4870 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4871 /* Finish up. */
4872 expr = finish_stmt_expr (expr, false);
4873 /* Consume the ')'. */
4874 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4875 if (!parens.require_close (parser))
4876 cp_parser_skip_to_end_of_statement (parser);
4877
4878 cp_parser_end_tentative_firewall (parser, start, expr);
4879 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4880 return cp_expr (expr, combined_loc);
4881 }
4882
4883 /* Expressions [gram.expr] */
4884
4885 /* Parse a fold-operator.
4886
4887 fold-operator:
4888 - * / % ^ & | = < > << >>
4889 = -= *= /= %= ^= &= |= <<= >>=
4890 == != <= >= && || , .* ->*
4891
4892 This returns the tree code corresponding to the matched operator
4893 as an int. When the current token matches a compound assignment
4894 opertor, the resulting tree code is the negative value of the
4895 non-assignment operator. */
4896
4897 static int
4898 cp_parser_fold_operator (cp_token *token)
4899 {
4900 switch (token->type)
4901 {
4902 case CPP_PLUS: return PLUS_EXPR;
4903 case CPP_MINUS: return MINUS_EXPR;
4904 case CPP_MULT: return MULT_EXPR;
4905 case CPP_DIV: return TRUNC_DIV_EXPR;
4906 case CPP_MOD: return TRUNC_MOD_EXPR;
4907 case CPP_XOR: return BIT_XOR_EXPR;
4908 case CPP_AND: return BIT_AND_EXPR;
4909 case CPP_OR: return BIT_IOR_EXPR;
4910 case CPP_LSHIFT: return LSHIFT_EXPR;
4911 case CPP_RSHIFT: return RSHIFT_EXPR;
4912
4913 case CPP_EQ: return -NOP_EXPR;
4914 case CPP_PLUS_EQ: return -PLUS_EXPR;
4915 case CPP_MINUS_EQ: return -MINUS_EXPR;
4916 case CPP_MULT_EQ: return -MULT_EXPR;
4917 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4918 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4919 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4920 case CPP_AND_EQ: return -BIT_AND_EXPR;
4921 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4922 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4923 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4924
4925 case CPP_EQ_EQ: return EQ_EXPR;
4926 case CPP_NOT_EQ: return NE_EXPR;
4927 case CPP_LESS: return LT_EXPR;
4928 case CPP_GREATER: return GT_EXPR;
4929 case CPP_LESS_EQ: return LE_EXPR;
4930 case CPP_GREATER_EQ: return GE_EXPR;
4931
4932 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4933 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4934
4935 case CPP_COMMA: return COMPOUND_EXPR;
4936
4937 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4938 case CPP_DEREF_STAR: return MEMBER_REF;
4939
4940 default: return ERROR_MARK;
4941 }
4942 }
4943
4944 /* Returns true if CODE indicates a binary expression, which is not allowed in
4945 the LHS of a fold-expression. More codes will need to be added to use this
4946 function in other contexts. */
4947
4948 static bool
4949 is_binary_op (tree_code code)
4950 {
4951 switch (code)
4952 {
4953 case PLUS_EXPR:
4954 case POINTER_PLUS_EXPR:
4955 case MINUS_EXPR:
4956 case MULT_EXPR:
4957 case TRUNC_DIV_EXPR:
4958 case TRUNC_MOD_EXPR:
4959 case BIT_XOR_EXPR:
4960 case BIT_AND_EXPR:
4961 case BIT_IOR_EXPR:
4962 case LSHIFT_EXPR:
4963 case RSHIFT_EXPR:
4964
4965 case MODOP_EXPR:
4966
4967 case EQ_EXPR:
4968 case NE_EXPR:
4969 case LE_EXPR:
4970 case GE_EXPR:
4971 case LT_EXPR:
4972 case GT_EXPR:
4973
4974 case TRUTH_ANDIF_EXPR:
4975 case TRUTH_ORIF_EXPR:
4976
4977 case COMPOUND_EXPR:
4978
4979 case DOTSTAR_EXPR:
4980 case MEMBER_REF:
4981 return true;
4982
4983 default:
4984 return false;
4985 }
4986 }
4987
4988 /* If the next token is a suitable fold operator, consume it and return as
4989 the function above. */
4990
4991 static int
4992 cp_parser_fold_operator (cp_parser *parser)
4993 {
4994 cp_token* token = cp_lexer_peek_token (parser->lexer);
4995 int code = cp_parser_fold_operator (token);
4996 if (code != ERROR_MARK)
4997 cp_lexer_consume_token (parser->lexer);
4998 return code;
4999 }
5000
5001 /* Parse a fold-expression.
5002
5003 fold-expression:
5004 ( ... folding-operator cast-expression)
5005 ( cast-expression folding-operator ... )
5006 ( cast-expression folding operator ... folding-operator cast-expression)
5007
5008 Note that the '(' and ')' are matched in primary expression. */
5009
5010 static cp_expr
5011 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5012 {
5013 cp_id_kind pidk;
5014
5015 // Left fold.
5016 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5017 {
5018 cp_lexer_consume_token (parser->lexer);
5019 int op = cp_parser_fold_operator (parser);
5020 if (op == ERROR_MARK)
5021 {
5022 cp_parser_error (parser, "expected binary operator");
5023 return error_mark_node;
5024 }
5025
5026 tree expr = cp_parser_cast_expression (parser, false, false,
5027 false, &pidk);
5028 if (expr == error_mark_node)
5029 return error_mark_node;
5030 return finish_left_unary_fold_expr (expr, op);
5031 }
5032
5033 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5034 int op = cp_parser_fold_operator (parser);
5035 if (op == ERROR_MARK)
5036 {
5037 cp_parser_error (parser, "expected binary operator");
5038 return error_mark_node;
5039 }
5040
5041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5042 {
5043 cp_parser_error (parser, "expected ...");
5044 return error_mark_node;
5045 }
5046 cp_lexer_consume_token (parser->lexer);
5047
5048 /* The operands of a fold-expression are cast-expressions, so binary or
5049 conditional expressions are not allowed. We check this here to avoid
5050 tentative parsing. */
5051 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5052 /* OK, the expression was parenthesized. */;
5053 else if (is_binary_op (TREE_CODE (expr1)))
5054 error_at (location_of (expr1),
5055 "binary expression in operand of fold-expression");
5056 else if (TREE_CODE (expr1) == COND_EXPR
5057 || (REFERENCE_REF_P (expr1)
5058 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5059 error_at (location_of (expr1),
5060 "conditional expression in operand of fold-expression");
5061
5062 // Right fold.
5063 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5064 return finish_right_unary_fold_expr (expr1, op);
5065
5066 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5067 {
5068 cp_parser_error (parser, "mismatched operator in fold-expression");
5069 return error_mark_node;
5070 }
5071 cp_lexer_consume_token (parser->lexer);
5072
5073 // Binary left or right fold.
5074 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5075 if (expr2 == error_mark_node)
5076 return error_mark_node;
5077 return finish_binary_fold_expr (expr1, expr2, op);
5078 }
5079
5080 /* Parse a primary-expression.
5081
5082 primary-expression:
5083 literal
5084 this
5085 ( expression )
5086 id-expression
5087 lambda-expression (C++11)
5088
5089 GNU Extensions:
5090
5091 primary-expression:
5092 ( compound-statement )
5093 __builtin_va_arg ( assignment-expression , type-id )
5094 __builtin_offsetof ( type-id , offsetof-expression )
5095
5096 C++ Extensions:
5097 __has_nothrow_assign ( type-id )
5098 __has_nothrow_constructor ( type-id )
5099 __has_nothrow_copy ( type-id )
5100 __has_trivial_assign ( type-id )
5101 __has_trivial_constructor ( type-id )
5102 __has_trivial_copy ( type-id )
5103 __has_trivial_destructor ( type-id )
5104 __has_virtual_destructor ( type-id )
5105 __is_abstract ( type-id )
5106 __is_base_of ( type-id , type-id )
5107 __is_class ( type-id )
5108 __is_empty ( type-id )
5109 __is_enum ( type-id )
5110 __is_final ( type-id )
5111 __is_literal_type ( type-id )
5112 __is_pod ( type-id )
5113 __is_polymorphic ( type-id )
5114 __is_std_layout ( type-id )
5115 __is_trivial ( type-id )
5116 __is_union ( type-id )
5117
5118 Objective-C++ Extension:
5119
5120 primary-expression:
5121 objc-expression
5122
5123 literal:
5124 __null
5125
5126 ADDRESS_P is true iff this expression was immediately preceded by
5127 "&" and therefore might denote a pointer-to-member. CAST_P is true
5128 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5129 true iff this expression is a template argument.
5130
5131 Returns a representation of the expression. Upon return, *IDK
5132 indicates what kind of id-expression (if any) was present. */
5133
5134 static cp_expr
5135 cp_parser_primary_expression (cp_parser *parser,
5136 bool address_p,
5137 bool cast_p,
5138 bool template_arg_p,
5139 bool decltype_p,
5140 cp_id_kind *idk)
5141 {
5142 cp_token *token = NULL;
5143
5144 /* Assume the primary expression is not an id-expression. */
5145 *idk = CP_ID_KIND_NONE;
5146
5147 /* Peek at the next token. */
5148 token = cp_lexer_peek_token (parser->lexer);
5149 switch ((int) token->type)
5150 {
5151 /* literal:
5152 integer-literal
5153 character-literal
5154 floating-literal
5155 string-literal
5156 boolean-literal
5157 pointer-literal
5158 user-defined-literal */
5159 case CPP_CHAR:
5160 case CPP_CHAR16:
5161 case CPP_CHAR32:
5162 case CPP_WCHAR:
5163 case CPP_UTF8CHAR:
5164 case CPP_NUMBER:
5165 case CPP_PREPARSED_EXPR:
5166 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5167 return cp_parser_userdef_numeric_literal (parser);
5168 token = cp_lexer_consume_token (parser->lexer);
5169 if (TREE_CODE (token->u.value) == FIXED_CST)
5170 {
5171 error_at (token->location,
5172 "fixed-point types not supported in C++");
5173 return error_mark_node;
5174 }
5175 /* Floating-point literals are only allowed in an integral
5176 constant expression if they are cast to an integral or
5177 enumeration type. */
5178 if (TREE_CODE (token->u.value) == REAL_CST
5179 && parser->integral_constant_expression_p
5180 && pedantic)
5181 {
5182 /* CAST_P will be set even in invalid code like "int(2.7 +
5183 ...)". Therefore, we have to check that the next token
5184 is sure to end the cast. */
5185 if (cast_p)
5186 {
5187 cp_token *next_token;
5188
5189 next_token = cp_lexer_peek_token (parser->lexer);
5190 if (/* The comma at the end of an
5191 enumerator-definition. */
5192 next_token->type != CPP_COMMA
5193 /* The curly brace at the end of an enum-specifier. */
5194 && next_token->type != CPP_CLOSE_BRACE
5195 /* The end of a statement. */
5196 && next_token->type != CPP_SEMICOLON
5197 /* The end of the cast-expression. */
5198 && next_token->type != CPP_CLOSE_PAREN
5199 /* The end of an array bound. */
5200 && next_token->type != CPP_CLOSE_SQUARE
5201 /* The closing ">" in a template-argument-list. */
5202 && (next_token->type != CPP_GREATER
5203 || parser->greater_than_is_operator_p)
5204 /* C++0x only: A ">>" treated like two ">" tokens,
5205 in a template-argument-list. */
5206 && (next_token->type != CPP_RSHIFT
5207 || (cxx_dialect == cxx98)
5208 || parser->greater_than_is_operator_p))
5209 cast_p = false;
5210 }
5211
5212 /* If we are within a cast, then the constraint that the
5213 cast is to an integral or enumeration type will be
5214 checked at that point. If we are not within a cast, then
5215 this code is invalid. */
5216 if (!cast_p)
5217 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5218 }
5219 return cp_expr (token->u.value, token->location);
5220
5221 case CPP_CHAR_USERDEF:
5222 case CPP_CHAR16_USERDEF:
5223 case CPP_CHAR32_USERDEF:
5224 case CPP_WCHAR_USERDEF:
5225 case CPP_UTF8CHAR_USERDEF:
5226 return cp_parser_userdef_char_literal (parser);
5227
5228 case CPP_STRING:
5229 case CPP_STRING16:
5230 case CPP_STRING32:
5231 case CPP_WSTRING:
5232 case CPP_UTF8STRING:
5233 case CPP_STRING_USERDEF:
5234 case CPP_STRING16_USERDEF:
5235 case CPP_STRING32_USERDEF:
5236 case CPP_WSTRING_USERDEF:
5237 case CPP_UTF8STRING_USERDEF:
5238 /* ??? Should wide strings be allowed when parser->translate_strings_p
5239 is false (i.e. in attributes)? If not, we can kill the third
5240 argument to cp_parser_string_literal. */
5241 return cp_parser_string_literal (parser,
5242 parser->translate_strings_p,
5243 true);
5244
5245 case CPP_OPEN_PAREN:
5246 /* If we see `( { ' then we are looking at the beginning of
5247 a GNU statement-expression. */
5248 if (cp_parser_allow_gnu_extensions_p (parser)
5249 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5250 {
5251 /* Statement-expressions are not allowed by the standard. */
5252 pedwarn (token->location, OPT_Wpedantic,
5253 "ISO C++ forbids braced-groups within expressions");
5254
5255 /* And they're not allowed outside of a function-body; you
5256 cannot, for example, write:
5257
5258 int i = ({ int j = 3; j + 1; });
5259
5260 at class or namespace scope. */
5261 if (!parser->in_function_body
5262 || parser->in_template_argument_list_p)
5263 {
5264 error_at (token->location,
5265 "statement-expressions are not allowed outside "
5266 "functions nor in template-argument lists");
5267 cp_parser_skip_to_end_of_block_or_statement (parser);
5268 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5269 cp_lexer_consume_token (parser->lexer);
5270 return error_mark_node;
5271 }
5272 else
5273 return cp_parser_statement_expr (parser);
5274 }
5275 /* Otherwise it's a normal parenthesized expression. */
5276 {
5277 cp_expr expr;
5278 bool saved_greater_than_is_operator_p;
5279
5280 location_t open_paren_loc = token->location;
5281
5282 /* Consume the `('. */
5283 matching_parens parens;
5284 parens.consume_open (parser);
5285 /* Within a parenthesized expression, a `>' token is always
5286 the greater-than operator. */
5287 saved_greater_than_is_operator_p
5288 = parser->greater_than_is_operator_p;
5289 parser->greater_than_is_operator_p = true;
5290
5291 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5292 /* Left fold expression. */
5293 expr = NULL_TREE;
5294 else
5295 /* Parse the parenthesized expression. */
5296 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5297
5298 token = cp_lexer_peek_token (parser->lexer);
5299 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5300 {
5301 expr = cp_parser_fold_expression (parser, expr);
5302 if (expr != error_mark_node
5303 && cxx_dialect < cxx17
5304 && !in_system_header_at (input_location))
5305 pedwarn (input_location, 0, "fold-expressions only available "
5306 "with -std=c++17 or -std=gnu++17");
5307 }
5308 else
5309 /* Let the front end know that this expression was
5310 enclosed in parentheses. This matters in case, for
5311 example, the expression is of the form `A::B', since
5312 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5313 not. */
5314 expr = finish_parenthesized_expr (expr);
5315
5316 /* DR 705: Wrapping an unqualified name in parentheses
5317 suppresses arg-dependent lookup. We want to pass back
5318 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5319 (c++/37862), but none of the others. */
5320 if (*idk != CP_ID_KIND_QUALIFIED)
5321 *idk = CP_ID_KIND_NONE;
5322
5323 /* The `>' token might be the end of a template-id or
5324 template-parameter-list now. */
5325 parser->greater_than_is_operator_p
5326 = saved_greater_than_is_operator_p;
5327
5328 /* Consume the `)'. */
5329 token = cp_lexer_peek_token (parser->lexer);
5330 location_t close_paren_loc = token->location;
5331 expr.set_range (open_paren_loc, close_paren_loc);
5332 if (!parens.require_close (parser)
5333 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5334 cp_parser_skip_to_end_of_statement (parser);
5335
5336 return expr;
5337 }
5338
5339 case CPP_OPEN_SQUARE:
5340 {
5341 if (c_dialect_objc ())
5342 {
5343 /* We might have an Objective-C++ message. */
5344 cp_parser_parse_tentatively (parser);
5345 tree msg = cp_parser_objc_message_expression (parser);
5346 /* If that works out, we're done ... */
5347 if (cp_parser_parse_definitely (parser))
5348 return msg;
5349 /* ... else, fall though to see if it's a lambda. */
5350 }
5351 cp_expr lam = cp_parser_lambda_expression (parser);
5352 /* Don't warn about a failed tentative parse. */
5353 if (cp_parser_error_occurred (parser))
5354 return error_mark_node;
5355 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5356 return lam;
5357 }
5358
5359 case CPP_OBJC_STRING:
5360 if (c_dialect_objc ())
5361 /* We have an Objective-C++ string literal. */
5362 return cp_parser_objc_expression (parser);
5363 cp_parser_error (parser, "expected primary-expression");
5364 return error_mark_node;
5365
5366 case CPP_KEYWORD:
5367 switch (token->keyword)
5368 {
5369 /* These two are the boolean literals. */
5370 case RID_TRUE:
5371 cp_lexer_consume_token (parser->lexer);
5372 return cp_expr (boolean_true_node, token->location);
5373 case RID_FALSE:
5374 cp_lexer_consume_token (parser->lexer);
5375 return cp_expr (boolean_false_node, token->location);
5376
5377 /* The `__null' literal. */
5378 case RID_NULL:
5379 cp_lexer_consume_token (parser->lexer);
5380 return cp_expr (null_node, token->location);
5381
5382 /* The `nullptr' literal. */
5383 case RID_NULLPTR:
5384 cp_lexer_consume_token (parser->lexer);
5385 return cp_expr (nullptr_node, token->location);
5386
5387 /* Recognize the `this' keyword. */
5388 case RID_THIS:
5389 cp_lexer_consume_token (parser->lexer);
5390 if (parser->local_variables_forbidden_p)
5391 {
5392 error_at (token->location,
5393 "%<this%> may not be used in this context");
5394 return error_mark_node;
5395 }
5396 /* Pointers cannot appear in constant-expressions. */
5397 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5398 return error_mark_node;
5399 return cp_expr (finish_this_expr (), token->location);
5400
5401 /* The `operator' keyword can be the beginning of an
5402 id-expression. */
5403 case RID_OPERATOR:
5404 goto id_expression;
5405
5406 case RID_FUNCTION_NAME:
5407 case RID_PRETTY_FUNCTION_NAME:
5408 case RID_C99_FUNCTION_NAME:
5409 {
5410 non_integral_constant name;
5411
5412 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5413 __func__ are the names of variables -- but they are
5414 treated specially. Therefore, they are handled here,
5415 rather than relying on the generic id-expression logic
5416 below. Grammatically, these names are id-expressions.
5417
5418 Consume the token. */
5419 token = cp_lexer_consume_token (parser->lexer);
5420
5421 switch (token->keyword)
5422 {
5423 case RID_FUNCTION_NAME:
5424 name = NIC_FUNC_NAME;
5425 break;
5426 case RID_PRETTY_FUNCTION_NAME:
5427 name = NIC_PRETTY_FUNC;
5428 break;
5429 case RID_C99_FUNCTION_NAME:
5430 name = NIC_C99_FUNC;
5431 break;
5432 default:
5433 gcc_unreachable ();
5434 }
5435
5436 if (cp_parser_non_integral_constant_expression (parser, name))
5437 return error_mark_node;
5438
5439 /* Look up the name. */
5440 return finish_fname (token->u.value);
5441 }
5442
5443 case RID_VA_ARG:
5444 {
5445 tree expression;
5446 tree type;
5447 location_t type_location;
5448 location_t start_loc
5449 = cp_lexer_peek_token (parser->lexer)->location;
5450 /* The `__builtin_va_arg' construct is used to handle
5451 `va_arg'. Consume the `__builtin_va_arg' token. */
5452 cp_lexer_consume_token (parser->lexer);
5453 /* Look for the opening `('. */
5454 matching_parens parens;
5455 parens.require_open (parser);
5456 /* Now, parse the assignment-expression. */
5457 expression = cp_parser_assignment_expression (parser);
5458 /* Look for the `,'. */
5459 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5460 type_location = cp_lexer_peek_token (parser->lexer)->location;
5461 /* Parse the type-id. */
5462 {
5463 type_id_in_expr_sentinel s (parser);
5464 type = cp_parser_type_id (parser);
5465 }
5466 /* Look for the closing `)'. */
5467 location_t finish_loc
5468 = cp_lexer_peek_token (parser->lexer)->location;
5469 parens.require_close (parser);
5470 /* Using `va_arg' in a constant-expression is not
5471 allowed. */
5472 if (cp_parser_non_integral_constant_expression (parser,
5473 NIC_VA_ARG))
5474 return error_mark_node;
5475 /* Construct a location of the form:
5476 __builtin_va_arg (v, int)
5477 ~~~~~~~~~~~~~~~~~~~~~^~~~
5478 with the caret at the type, ranging from the start of the
5479 "__builtin_va_arg" token to the close paren. */
5480 location_t combined_loc
5481 = make_location (type_location, start_loc, finish_loc);
5482 return build_x_va_arg (combined_loc, expression, type);
5483 }
5484
5485 case RID_OFFSETOF:
5486 return cp_parser_builtin_offsetof (parser);
5487
5488 case RID_HAS_NOTHROW_ASSIGN:
5489 case RID_HAS_NOTHROW_CONSTRUCTOR:
5490 case RID_HAS_NOTHROW_COPY:
5491 case RID_HAS_TRIVIAL_ASSIGN:
5492 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5493 case RID_HAS_TRIVIAL_COPY:
5494 case RID_HAS_TRIVIAL_DESTRUCTOR:
5495 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5496 case RID_HAS_VIRTUAL_DESTRUCTOR:
5497 case RID_IS_ABSTRACT:
5498 case RID_IS_AGGREGATE:
5499 case RID_IS_BASE_OF:
5500 case RID_IS_CLASS:
5501 case RID_IS_EMPTY:
5502 case RID_IS_ENUM:
5503 case RID_IS_FINAL:
5504 case RID_IS_LITERAL_TYPE:
5505 case RID_IS_POD:
5506 case RID_IS_POLYMORPHIC:
5507 case RID_IS_SAME_AS:
5508 case RID_IS_STD_LAYOUT:
5509 case RID_IS_TRIVIAL:
5510 case RID_IS_TRIVIALLY_ASSIGNABLE:
5511 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5512 case RID_IS_TRIVIALLY_COPYABLE:
5513 case RID_IS_UNION:
5514 case RID_IS_ASSIGNABLE:
5515 case RID_IS_CONSTRUCTIBLE:
5516 return cp_parser_trait_expr (parser, token->keyword);
5517
5518 // C++ concepts
5519 case RID_REQUIRES:
5520 return cp_parser_requires_expression (parser);
5521
5522 /* Objective-C++ expressions. */
5523 case RID_AT_ENCODE:
5524 case RID_AT_PROTOCOL:
5525 case RID_AT_SELECTOR:
5526 return cp_parser_objc_expression (parser);
5527
5528 case RID_TEMPLATE:
5529 if (parser->in_function_body
5530 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5531 == CPP_LESS))
5532 {
5533 error_at (token->location,
5534 "a template declaration cannot appear at block scope");
5535 cp_parser_skip_to_end_of_block_or_statement (parser);
5536 return error_mark_node;
5537 }
5538 /* FALLTHRU */
5539 default:
5540 cp_parser_error (parser, "expected primary-expression");
5541 return error_mark_node;
5542 }
5543
5544 /* An id-expression can start with either an identifier, a
5545 `::' as the beginning of a qualified-id, or the "operator"
5546 keyword. */
5547 case CPP_NAME:
5548 case CPP_SCOPE:
5549 case CPP_TEMPLATE_ID:
5550 case CPP_NESTED_NAME_SPECIFIER:
5551 {
5552 id_expression:
5553 cp_expr id_expression;
5554 cp_expr decl;
5555 const char *error_msg;
5556 bool template_p;
5557 bool done;
5558 cp_token *id_expr_token;
5559
5560 /* Parse the id-expression. */
5561 id_expression
5562 = cp_parser_id_expression (parser,
5563 /*template_keyword_p=*/false,
5564 /*check_dependency_p=*/true,
5565 &template_p,
5566 /*declarator_p=*/false,
5567 /*optional_p=*/false);
5568 if (id_expression == error_mark_node)
5569 return error_mark_node;
5570 id_expr_token = token;
5571 token = cp_lexer_peek_token (parser->lexer);
5572 done = (token->type != CPP_OPEN_SQUARE
5573 && token->type != CPP_OPEN_PAREN
5574 && token->type != CPP_DOT
5575 && token->type != CPP_DEREF
5576 && token->type != CPP_PLUS_PLUS
5577 && token->type != CPP_MINUS_MINUS);
5578 /* If we have a template-id, then no further lookup is
5579 required. If the template-id was for a template-class, we
5580 will sometimes have a TYPE_DECL at this point. */
5581 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5582 || TREE_CODE (id_expression) == TYPE_DECL)
5583 decl = id_expression;
5584 /* Look up the name. */
5585 else
5586 {
5587 tree ambiguous_decls;
5588
5589 /* If we already know that this lookup is ambiguous, then
5590 we've already issued an error message; there's no reason
5591 to check again. */
5592 if (id_expr_token->type == CPP_NAME
5593 && id_expr_token->error_reported)
5594 {
5595 cp_parser_simulate_error (parser);
5596 return error_mark_node;
5597 }
5598
5599 decl = cp_parser_lookup_name (parser, id_expression,
5600 none_type,
5601 template_p,
5602 /*is_namespace=*/false,
5603 /*check_dependency=*/true,
5604 &ambiguous_decls,
5605 id_expr_token->location);
5606 /* If the lookup was ambiguous, an error will already have
5607 been issued. */
5608 if (ambiguous_decls)
5609 return error_mark_node;
5610
5611 /* In Objective-C++, we may have an Objective-C 2.0
5612 dot-syntax for classes here. */
5613 if (c_dialect_objc ()
5614 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5615 && TREE_CODE (decl) == TYPE_DECL
5616 && objc_is_class_name (decl))
5617 {
5618 tree component;
5619 cp_lexer_consume_token (parser->lexer);
5620 component = cp_parser_identifier (parser);
5621 if (component == error_mark_node)
5622 return error_mark_node;
5623
5624 tree result = objc_build_class_component_ref (id_expression,
5625 component);
5626 /* Build a location of the form:
5627 expr.component
5628 ~~~~~^~~~~~~~~
5629 with caret at the start of the component name (at
5630 input_location), ranging from the start of the id_expression
5631 to the end of the component name. */
5632 location_t combined_loc
5633 = make_location (input_location, id_expression.get_start (),
5634 get_finish (input_location));
5635 protected_set_expr_location (result, combined_loc);
5636 return result;
5637 }
5638
5639 /* In Objective-C++, an instance variable (ivar) may be preferred
5640 to whatever cp_parser_lookup_name() found.
5641 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5642 rest of c-family, we have to do a little extra work to preserve
5643 any location information in cp_expr "decl". Given that
5644 objc_lookup_ivar is implemented in "c-family" and "objc", we
5645 have a trip through the pure "tree" type, rather than cp_expr.
5646 Naively copying it back to "decl" would implicitly give the
5647 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5648 store an EXPR_LOCATION. Hence we only update "decl" (and
5649 hence its location_t) if we get back a different tree node. */
5650 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5651 id_expression);
5652 if (decl_tree != decl.get_value ())
5653 decl = cp_expr (decl_tree);
5654
5655 /* If name lookup gives us a SCOPE_REF, then the
5656 qualifying scope was dependent. */
5657 if (TREE_CODE (decl) == SCOPE_REF)
5658 {
5659 /* At this point, we do not know if DECL is a valid
5660 integral constant expression. We assume that it is
5661 in fact such an expression, so that code like:
5662
5663 template <int N> struct A {
5664 int a[B<N>::i];
5665 };
5666
5667 is accepted. At template-instantiation time, we
5668 will check that B<N>::i is actually a constant. */
5669 return decl;
5670 }
5671 /* Check to see if DECL is a local variable in a context
5672 where that is forbidden. */
5673 if (parser->local_variables_forbidden_p
5674 && local_variable_p (decl))
5675 {
5676 error_at (id_expr_token->location,
5677 "local variable %qD may not appear in this context",
5678 decl.get_value ());
5679 return error_mark_node;
5680 }
5681 }
5682
5683 decl = (finish_id_expression
5684 (id_expression, decl, parser->scope,
5685 idk,
5686 parser->integral_constant_expression_p,
5687 parser->allow_non_integral_constant_expression_p,
5688 &parser->non_integral_constant_expression_p,
5689 template_p, done, address_p,
5690 template_arg_p,
5691 &error_msg,
5692 id_expression.get_location ()));
5693 if (error_msg)
5694 cp_parser_error (parser, error_msg);
5695 decl.set_location (id_expr_token->location);
5696 return decl;
5697 }
5698
5699 /* Anything else is an error. */
5700 default:
5701 cp_parser_error (parser, "expected primary-expression");
5702 return error_mark_node;
5703 }
5704 }
5705
5706 static inline cp_expr
5707 cp_parser_primary_expression (cp_parser *parser,
5708 bool address_p,
5709 bool cast_p,
5710 bool template_arg_p,
5711 cp_id_kind *idk)
5712 {
5713 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5714 /*decltype*/false, idk);
5715 }
5716
5717 /* Parse an id-expression.
5718
5719 id-expression:
5720 unqualified-id
5721 qualified-id
5722
5723 qualified-id:
5724 :: [opt] nested-name-specifier template [opt] unqualified-id
5725 :: identifier
5726 :: operator-function-id
5727 :: template-id
5728
5729 Return a representation of the unqualified portion of the
5730 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5731 a `::' or nested-name-specifier.
5732
5733 Often, if the id-expression was a qualified-id, the caller will
5734 want to make a SCOPE_REF to represent the qualified-id. This
5735 function does not do this in order to avoid wastefully creating
5736 SCOPE_REFs when they are not required.
5737
5738 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5739 `template' keyword.
5740
5741 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5742 uninstantiated templates.
5743
5744 If *TEMPLATE_P is non-NULL, it is set to true iff the
5745 `template' keyword is used to explicitly indicate that the entity
5746 named is a template.
5747
5748 If DECLARATOR_P is true, the id-expression is appearing as part of
5749 a declarator, rather than as part of an expression. */
5750
5751 static cp_expr
5752 cp_parser_id_expression (cp_parser *parser,
5753 bool template_keyword_p,
5754 bool check_dependency_p,
5755 bool *template_p,
5756 bool declarator_p,
5757 bool optional_p)
5758 {
5759 bool global_scope_p;
5760 bool nested_name_specifier_p;
5761
5762 /* Assume the `template' keyword was not used. */
5763 if (template_p)
5764 *template_p = template_keyword_p;
5765
5766 /* Look for the optional `::' operator. */
5767 global_scope_p
5768 = (!template_keyword_p
5769 && (cp_parser_global_scope_opt (parser,
5770 /*current_scope_valid_p=*/false)
5771 != NULL_TREE));
5772
5773 /* Look for the optional nested-name-specifier. */
5774 nested_name_specifier_p
5775 = (cp_parser_nested_name_specifier_opt (parser,
5776 /*typename_keyword_p=*/false,
5777 check_dependency_p,
5778 /*type_p=*/false,
5779 declarator_p,
5780 template_keyword_p)
5781 != NULL_TREE);
5782
5783 /* If there is a nested-name-specifier, then we are looking at
5784 the first qualified-id production. */
5785 if (nested_name_specifier_p)
5786 {
5787 tree saved_scope;
5788 tree saved_object_scope;
5789 tree saved_qualifying_scope;
5790 cp_expr unqualified_id;
5791 bool is_template;
5792
5793 /* See if the next token is the `template' keyword. */
5794 if (!template_p)
5795 template_p = &is_template;
5796 *template_p = cp_parser_optional_template_keyword (parser);
5797 /* Name lookup we do during the processing of the
5798 unqualified-id might obliterate SCOPE. */
5799 saved_scope = parser->scope;
5800 saved_object_scope = parser->object_scope;
5801 saved_qualifying_scope = parser->qualifying_scope;
5802 /* Process the final unqualified-id. */
5803 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5804 check_dependency_p,
5805 declarator_p,
5806 /*optional_p=*/false);
5807 /* Restore the SAVED_SCOPE for our caller. */
5808 parser->scope = saved_scope;
5809 parser->object_scope = saved_object_scope;
5810 parser->qualifying_scope = saved_qualifying_scope;
5811
5812 return unqualified_id;
5813 }
5814 /* Otherwise, if we are in global scope, then we are looking at one
5815 of the other qualified-id productions. */
5816 else if (global_scope_p)
5817 {
5818 cp_token *token;
5819 tree id;
5820
5821 /* Peek at the next token. */
5822 token = cp_lexer_peek_token (parser->lexer);
5823
5824 /* If it's an identifier, and the next token is not a "<", then
5825 we can avoid the template-id case. This is an optimization
5826 for this common case. */
5827 if (token->type == CPP_NAME
5828 && !cp_parser_nth_token_starts_template_argument_list_p
5829 (parser, 2))
5830 return cp_parser_identifier (parser);
5831
5832 cp_parser_parse_tentatively (parser);
5833 /* Try a template-id. */
5834 id = cp_parser_template_id (parser,
5835 /*template_keyword_p=*/false,
5836 /*check_dependency_p=*/true,
5837 none_type,
5838 declarator_p);
5839 /* If that worked, we're done. */
5840 if (cp_parser_parse_definitely (parser))
5841 return id;
5842
5843 /* Peek at the next token. (Changes in the token buffer may
5844 have invalidated the pointer obtained above.) */
5845 token = cp_lexer_peek_token (parser->lexer);
5846
5847 switch (token->type)
5848 {
5849 case CPP_NAME:
5850 return cp_parser_identifier (parser);
5851
5852 case CPP_KEYWORD:
5853 if (token->keyword == RID_OPERATOR)
5854 return cp_parser_operator_function_id (parser);
5855 /* Fall through. */
5856
5857 default:
5858 cp_parser_error (parser, "expected id-expression");
5859 return error_mark_node;
5860 }
5861 }
5862 else
5863 return cp_parser_unqualified_id (parser, template_keyword_p,
5864 /*check_dependency_p=*/true,
5865 declarator_p,
5866 optional_p);
5867 }
5868
5869 /* Parse an unqualified-id.
5870
5871 unqualified-id:
5872 identifier
5873 operator-function-id
5874 conversion-function-id
5875 ~ class-name
5876 template-id
5877
5878 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5879 keyword, in a construct like `A::template ...'.
5880
5881 Returns a representation of unqualified-id. For the `identifier'
5882 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5883 production a BIT_NOT_EXPR is returned; the operand of the
5884 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5885 other productions, see the documentation accompanying the
5886 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5887 names are looked up in uninstantiated templates. If DECLARATOR_P
5888 is true, the unqualified-id is appearing as part of a declarator,
5889 rather than as part of an expression. */
5890
5891 static cp_expr
5892 cp_parser_unqualified_id (cp_parser* parser,
5893 bool template_keyword_p,
5894 bool check_dependency_p,
5895 bool declarator_p,
5896 bool optional_p)
5897 {
5898 cp_token *token;
5899
5900 /* Peek at the next token. */
5901 token = cp_lexer_peek_token (parser->lexer);
5902
5903 switch ((int) token->type)
5904 {
5905 case CPP_NAME:
5906 {
5907 tree id;
5908
5909 /* We don't know yet whether or not this will be a
5910 template-id. */
5911 cp_parser_parse_tentatively (parser);
5912 /* Try a template-id. */
5913 id = cp_parser_template_id (parser, template_keyword_p,
5914 check_dependency_p,
5915 none_type,
5916 declarator_p);
5917 /* If it worked, we're done. */
5918 if (cp_parser_parse_definitely (parser))
5919 return id;
5920 /* Otherwise, it's an ordinary identifier. */
5921 return cp_parser_identifier (parser);
5922 }
5923
5924 case CPP_TEMPLATE_ID:
5925 return cp_parser_template_id (parser, template_keyword_p,
5926 check_dependency_p,
5927 none_type,
5928 declarator_p);
5929
5930 case CPP_COMPL:
5931 {
5932 tree type_decl;
5933 tree qualifying_scope;
5934 tree object_scope;
5935 tree scope;
5936 bool done;
5937
5938 /* Consume the `~' token. */
5939 cp_lexer_consume_token (parser->lexer);
5940 /* Parse the class-name. The standard, as written, seems to
5941 say that:
5942
5943 template <typename T> struct S { ~S (); };
5944 template <typename T> S<T>::~S() {}
5945
5946 is invalid, since `~' must be followed by a class-name, but
5947 `S<T>' is dependent, and so not known to be a class.
5948 That's not right; we need to look in uninstantiated
5949 templates. A further complication arises from:
5950
5951 template <typename T> void f(T t) {
5952 t.T::~T();
5953 }
5954
5955 Here, it is not possible to look up `T' in the scope of `T'
5956 itself. We must look in both the current scope, and the
5957 scope of the containing complete expression.
5958
5959 Yet another issue is:
5960
5961 struct S {
5962 int S;
5963 ~S();
5964 };
5965
5966 S::~S() {}
5967
5968 The standard does not seem to say that the `S' in `~S'
5969 should refer to the type `S' and not the data member
5970 `S::S'. */
5971
5972 /* DR 244 says that we look up the name after the "~" in the
5973 same scope as we looked up the qualifying name. That idea
5974 isn't fully worked out; it's more complicated than that. */
5975 scope = parser->scope;
5976 object_scope = parser->object_scope;
5977 qualifying_scope = parser->qualifying_scope;
5978
5979 /* Check for invalid scopes. */
5980 if (scope == error_mark_node)
5981 {
5982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5983 cp_lexer_consume_token (parser->lexer);
5984 return error_mark_node;
5985 }
5986 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5987 {
5988 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5989 error_at (token->location,
5990 "scope %qT before %<~%> is not a class-name",
5991 scope);
5992 cp_parser_simulate_error (parser);
5993 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5994 cp_lexer_consume_token (parser->lexer);
5995 return error_mark_node;
5996 }
5997 gcc_assert (!scope || TYPE_P (scope));
5998
5999 /* If the name is of the form "X::~X" it's OK even if X is a
6000 typedef. */
6001 token = cp_lexer_peek_token (parser->lexer);
6002 if (scope
6003 && token->type == CPP_NAME
6004 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6005 != CPP_LESS)
6006 && (token->u.value == TYPE_IDENTIFIER (scope)
6007 || (CLASS_TYPE_P (scope)
6008 && constructor_name_p (token->u.value, scope))))
6009 {
6010 cp_lexer_consume_token (parser->lexer);
6011 return build_nt (BIT_NOT_EXPR, scope);
6012 }
6013
6014 /* ~auto means the destructor of whatever the object is. */
6015 if (cp_parser_is_keyword (token, RID_AUTO))
6016 {
6017 if (cxx_dialect < cxx14)
6018 pedwarn (input_location, 0,
6019 "%<~auto%> only available with "
6020 "-std=c++14 or -std=gnu++14");
6021 cp_lexer_consume_token (parser->lexer);
6022 return build_nt (BIT_NOT_EXPR, make_auto ());
6023 }
6024
6025 /* If there was an explicit qualification (S::~T), first look
6026 in the scope given by the qualification (i.e., S).
6027
6028 Note: in the calls to cp_parser_class_name below we pass
6029 typename_type so that lookup finds the injected-class-name
6030 rather than the constructor. */
6031 done = false;
6032 type_decl = NULL_TREE;
6033 if (scope)
6034 {
6035 cp_parser_parse_tentatively (parser);
6036 type_decl = cp_parser_class_name (parser,
6037 /*typename_keyword_p=*/false,
6038 /*template_keyword_p=*/false,
6039 typename_type,
6040 /*check_dependency=*/false,
6041 /*class_head_p=*/false,
6042 declarator_p);
6043 if (cp_parser_parse_definitely (parser))
6044 done = true;
6045 }
6046 /* In "N::S::~S", look in "N" as well. */
6047 if (!done && scope && qualifying_scope)
6048 {
6049 cp_parser_parse_tentatively (parser);
6050 parser->scope = qualifying_scope;
6051 parser->object_scope = NULL_TREE;
6052 parser->qualifying_scope = NULL_TREE;
6053 type_decl
6054 = cp_parser_class_name (parser,
6055 /*typename_keyword_p=*/false,
6056 /*template_keyword_p=*/false,
6057 typename_type,
6058 /*check_dependency=*/false,
6059 /*class_head_p=*/false,
6060 declarator_p);
6061 if (cp_parser_parse_definitely (parser))
6062 done = true;
6063 }
6064 /* In "p->S::~T", look in the scope given by "*p" as well. */
6065 else if (!done && object_scope)
6066 {
6067 cp_parser_parse_tentatively (parser);
6068 parser->scope = object_scope;
6069 parser->object_scope = NULL_TREE;
6070 parser->qualifying_scope = NULL_TREE;
6071 type_decl
6072 = cp_parser_class_name (parser,
6073 /*typename_keyword_p=*/false,
6074 /*template_keyword_p=*/false,
6075 typename_type,
6076 /*check_dependency=*/false,
6077 /*class_head_p=*/false,
6078 declarator_p);
6079 if (cp_parser_parse_definitely (parser))
6080 done = true;
6081 }
6082 /* Look in the surrounding context. */
6083 if (!done)
6084 {
6085 parser->scope = NULL_TREE;
6086 parser->object_scope = NULL_TREE;
6087 parser->qualifying_scope = NULL_TREE;
6088 if (processing_template_decl)
6089 cp_parser_parse_tentatively (parser);
6090 type_decl
6091 = cp_parser_class_name (parser,
6092 /*typename_keyword_p=*/false,
6093 /*template_keyword_p=*/false,
6094 typename_type,
6095 /*check_dependency=*/false,
6096 /*class_head_p=*/false,
6097 declarator_p);
6098 if (processing_template_decl
6099 && ! cp_parser_parse_definitely (parser))
6100 {
6101 /* We couldn't find a type with this name. If we're parsing
6102 tentatively, fail and try something else. */
6103 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6104 {
6105 cp_parser_simulate_error (parser);
6106 return error_mark_node;
6107 }
6108 /* Otherwise, accept it and check for a match at instantiation
6109 time. */
6110 type_decl = cp_parser_identifier (parser);
6111 if (type_decl != error_mark_node)
6112 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6113 return type_decl;
6114 }
6115 }
6116 /* If an error occurred, assume that the name of the
6117 destructor is the same as the name of the qualifying
6118 class. That allows us to keep parsing after running
6119 into ill-formed destructor names. */
6120 if (type_decl == error_mark_node && scope)
6121 return build_nt (BIT_NOT_EXPR, scope);
6122 else if (type_decl == error_mark_node)
6123 return error_mark_node;
6124
6125 /* Check that destructor name and scope match. */
6126 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6127 {
6128 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6129 error_at (token->location,
6130 "declaration of %<~%T%> as member of %qT",
6131 type_decl, scope);
6132 cp_parser_simulate_error (parser);
6133 return error_mark_node;
6134 }
6135
6136 /* [class.dtor]
6137
6138 A typedef-name that names a class shall not be used as the
6139 identifier in the declarator for a destructor declaration. */
6140 if (declarator_p
6141 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6142 && !DECL_SELF_REFERENCE_P (type_decl)
6143 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6144 error_at (token->location,
6145 "typedef-name %qD used as destructor declarator",
6146 type_decl);
6147
6148 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6149 }
6150
6151 case CPP_KEYWORD:
6152 if (token->keyword == RID_OPERATOR)
6153 {
6154 cp_expr id;
6155
6156 /* This could be a template-id, so we try that first. */
6157 cp_parser_parse_tentatively (parser);
6158 /* Try a template-id. */
6159 id = cp_parser_template_id (parser, template_keyword_p,
6160 /*check_dependency_p=*/true,
6161 none_type,
6162 declarator_p);
6163 /* If that worked, we're done. */
6164 if (cp_parser_parse_definitely (parser))
6165 return id;
6166 /* We still don't know whether we're looking at an
6167 operator-function-id or a conversion-function-id. */
6168 cp_parser_parse_tentatively (parser);
6169 /* Try an operator-function-id. */
6170 id = cp_parser_operator_function_id (parser);
6171 /* If that didn't work, try a conversion-function-id. */
6172 if (!cp_parser_parse_definitely (parser))
6173 id = cp_parser_conversion_function_id (parser);
6174
6175 return id;
6176 }
6177 /* Fall through. */
6178
6179 default:
6180 if (optional_p)
6181 return NULL_TREE;
6182 cp_parser_error (parser, "expected unqualified-id");
6183 return error_mark_node;
6184 }
6185 }
6186
6187 /* Parse an (optional) nested-name-specifier.
6188
6189 nested-name-specifier: [C++98]
6190 class-or-namespace-name :: nested-name-specifier [opt]
6191 class-or-namespace-name :: template nested-name-specifier [opt]
6192
6193 nested-name-specifier: [C++0x]
6194 type-name ::
6195 namespace-name ::
6196 nested-name-specifier identifier ::
6197 nested-name-specifier template [opt] simple-template-id ::
6198
6199 PARSER->SCOPE should be set appropriately before this function is
6200 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6201 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6202 in name lookups.
6203
6204 Sets PARSER->SCOPE to the class (TYPE) or namespace
6205 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6206 it unchanged if there is no nested-name-specifier. Returns the new
6207 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6208
6209 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6210 part of a declaration and/or decl-specifier. */
6211
6212 static tree
6213 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6214 bool typename_keyword_p,
6215 bool check_dependency_p,
6216 bool type_p,
6217 bool is_declaration,
6218 bool template_keyword_p /* = false */)
6219 {
6220 bool success = false;
6221 cp_token_position start = 0;
6222 cp_token *token;
6223
6224 /* Remember where the nested-name-specifier starts. */
6225 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6226 {
6227 start = cp_lexer_token_position (parser->lexer, false);
6228 push_deferring_access_checks (dk_deferred);
6229 }
6230
6231 while (true)
6232 {
6233 tree new_scope;
6234 tree old_scope;
6235 tree saved_qualifying_scope;
6236
6237 /* Spot cases that cannot be the beginning of a
6238 nested-name-specifier. */
6239 token = cp_lexer_peek_token (parser->lexer);
6240
6241 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6242 the already parsed nested-name-specifier. */
6243 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6244 {
6245 /* Grab the nested-name-specifier and continue the loop. */
6246 cp_parser_pre_parsed_nested_name_specifier (parser);
6247 /* If we originally encountered this nested-name-specifier
6248 with IS_DECLARATION set to false, we will not have
6249 resolved TYPENAME_TYPEs, so we must do so here. */
6250 if (is_declaration
6251 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6252 {
6253 new_scope = resolve_typename_type (parser->scope,
6254 /*only_current_p=*/false);
6255 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6256 parser->scope = new_scope;
6257 }
6258 success = true;
6259 continue;
6260 }
6261
6262 /* Spot cases that cannot be the beginning of a
6263 nested-name-specifier. On the second and subsequent times
6264 through the loop, we look for the `template' keyword. */
6265 if (success && token->keyword == RID_TEMPLATE)
6266 ;
6267 /* A template-id can start a nested-name-specifier. */
6268 else if (token->type == CPP_TEMPLATE_ID)
6269 ;
6270 /* DR 743: decltype can be used in a nested-name-specifier. */
6271 else if (token_is_decltype (token))
6272 ;
6273 else
6274 {
6275 /* If the next token is not an identifier, then it is
6276 definitely not a type-name or namespace-name. */
6277 if (token->type != CPP_NAME)
6278 break;
6279 /* If the following token is neither a `<' (to begin a
6280 template-id), nor a `::', then we are not looking at a
6281 nested-name-specifier. */
6282 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6283
6284 if (token->type == CPP_COLON
6285 && parser->colon_corrects_to_scope_p
6286 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6287 {
6288 gcc_rich_location richloc (token->location);
6289 richloc.add_fixit_replace ("::");
6290 error_at (&richloc,
6291 "found %<:%> in nested-name-specifier, "
6292 "expected %<::%>");
6293 token->type = CPP_SCOPE;
6294 }
6295
6296 if (token->type != CPP_SCOPE
6297 && !cp_parser_nth_token_starts_template_argument_list_p
6298 (parser, 2))
6299 break;
6300 }
6301
6302 /* The nested-name-specifier is optional, so we parse
6303 tentatively. */
6304 cp_parser_parse_tentatively (parser);
6305
6306 /* Look for the optional `template' keyword, if this isn't the
6307 first time through the loop. */
6308 if (success)
6309 template_keyword_p = cp_parser_optional_template_keyword (parser);
6310
6311 /* Save the old scope since the name lookup we are about to do
6312 might destroy it. */
6313 old_scope = parser->scope;
6314 saved_qualifying_scope = parser->qualifying_scope;
6315 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6316 look up names in "X<T>::I" in order to determine that "Y" is
6317 a template. So, if we have a typename at this point, we make
6318 an effort to look through it. */
6319 if (is_declaration
6320 && !typename_keyword_p
6321 && parser->scope
6322 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6323 parser->scope = resolve_typename_type (parser->scope,
6324 /*only_current_p=*/false);
6325 /* Parse the qualifying entity. */
6326 new_scope
6327 = cp_parser_qualifying_entity (parser,
6328 typename_keyword_p,
6329 template_keyword_p,
6330 check_dependency_p,
6331 type_p,
6332 is_declaration);
6333 /* Look for the `::' token. */
6334 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6335
6336 /* If we found what we wanted, we keep going; otherwise, we're
6337 done. */
6338 if (!cp_parser_parse_definitely (parser))
6339 {
6340 bool error_p = false;
6341
6342 /* Restore the OLD_SCOPE since it was valid before the
6343 failed attempt at finding the last
6344 class-or-namespace-name. */
6345 parser->scope = old_scope;
6346 parser->qualifying_scope = saved_qualifying_scope;
6347
6348 /* If the next token is a decltype, and the one after that is a
6349 `::', then the decltype has failed to resolve to a class or
6350 enumeration type. Give this error even when parsing
6351 tentatively since it can't possibly be valid--and we're going
6352 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6353 won't get another chance.*/
6354 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6355 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6356 == CPP_SCOPE))
6357 {
6358 token = cp_lexer_consume_token (parser->lexer);
6359 error_at (token->location, "decltype evaluates to %qT, "
6360 "which is not a class or enumeration type",
6361 token->u.tree_check_value->value);
6362 parser->scope = error_mark_node;
6363 error_p = true;
6364 /* As below. */
6365 success = true;
6366 cp_lexer_consume_token (parser->lexer);
6367 }
6368
6369 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6370 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6371 {
6372 /* If we have a non-type template-id followed by ::, it can't
6373 possibly be valid. */
6374 token = cp_lexer_peek_token (parser->lexer);
6375 tree tid = token->u.tree_check_value->value;
6376 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6377 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6378 {
6379 tree tmpl = NULL_TREE;
6380 if (is_overloaded_fn (tid))
6381 {
6382 tree fns = get_fns (tid);
6383 if (OVL_SINGLE_P (fns))
6384 tmpl = OVL_FIRST (fns);
6385 error_at (token->location, "function template-id %qD "
6386 "in nested-name-specifier", tid);
6387 }
6388 else
6389 {
6390 /* Variable template. */
6391 tmpl = TREE_OPERAND (tid, 0);
6392 gcc_assert (variable_template_p (tmpl));
6393 error_at (token->location, "variable template-id %qD "
6394 "in nested-name-specifier", tid);
6395 }
6396 if (tmpl)
6397 inform (DECL_SOURCE_LOCATION (tmpl),
6398 "%qD declared here", tmpl);
6399
6400 parser->scope = error_mark_node;
6401 error_p = true;
6402 /* As below. */
6403 success = true;
6404 cp_lexer_consume_token (parser->lexer);
6405 cp_lexer_consume_token (parser->lexer);
6406 }
6407 }
6408
6409 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6410 break;
6411 /* If the next token is an identifier, and the one after
6412 that is a `::', then any valid interpretation would have
6413 found a class-or-namespace-name. */
6414 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6415 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6416 == CPP_SCOPE)
6417 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6418 != CPP_COMPL))
6419 {
6420 token = cp_lexer_consume_token (parser->lexer);
6421 if (!error_p)
6422 {
6423 if (!token->error_reported)
6424 {
6425 tree decl;
6426 tree ambiguous_decls;
6427
6428 decl = cp_parser_lookup_name (parser, token->u.value,
6429 none_type,
6430 /*is_template=*/false,
6431 /*is_namespace=*/false,
6432 /*check_dependency=*/true,
6433 &ambiguous_decls,
6434 token->location);
6435 if (TREE_CODE (decl) == TEMPLATE_DECL)
6436 error_at (token->location,
6437 "%qD used without template arguments",
6438 decl);
6439 else if (ambiguous_decls)
6440 {
6441 // cp_parser_lookup_name has the same diagnostic,
6442 // thus make sure to emit it at most once.
6443 if (cp_parser_uncommitted_to_tentative_parse_p
6444 (parser))
6445 {
6446 error_at (token->location,
6447 "reference to %qD is ambiguous",
6448 token->u.value);
6449 print_candidates (ambiguous_decls);
6450 }
6451 decl = error_mark_node;
6452 }
6453 else
6454 {
6455 if (cxx_dialect != cxx98)
6456 cp_parser_name_lookup_error
6457 (parser, token->u.value, decl, NLE_NOT_CXX98,
6458 token->location);
6459 else
6460 cp_parser_name_lookup_error
6461 (parser, token->u.value, decl, NLE_CXX98,
6462 token->location);
6463 }
6464 }
6465 parser->scope = error_mark_node;
6466 error_p = true;
6467 /* Treat this as a successful nested-name-specifier
6468 due to:
6469
6470 [basic.lookup.qual]
6471
6472 If the name found is not a class-name (clause
6473 _class_) or namespace-name (_namespace.def_), the
6474 program is ill-formed. */
6475 success = true;
6476 }
6477 cp_lexer_consume_token (parser->lexer);
6478 }
6479 break;
6480 }
6481 /* We've found one valid nested-name-specifier. */
6482 success = true;
6483 /* Name lookup always gives us a DECL. */
6484 if (TREE_CODE (new_scope) == TYPE_DECL)
6485 new_scope = TREE_TYPE (new_scope);
6486 /* Uses of "template" must be followed by actual templates. */
6487 if (template_keyword_p
6488 && !(CLASS_TYPE_P (new_scope)
6489 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6490 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6491 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6492 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6493 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6494 == TEMPLATE_ID_EXPR)))
6495 permerror (input_location, TYPE_P (new_scope)
6496 ? G_("%qT is not a template")
6497 : G_("%qD is not a template"),
6498 new_scope);
6499 /* If it is a class scope, try to complete it; we are about to
6500 be looking up names inside the class. */
6501 if (TYPE_P (new_scope)
6502 /* Since checking types for dependency can be expensive,
6503 avoid doing it if the type is already complete. */
6504 && !COMPLETE_TYPE_P (new_scope)
6505 /* Do not try to complete dependent types. */
6506 && !dependent_type_p (new_scope))
6507 {
6508 new_scope = complete_type (new_scope);
6509 /* If it is a typedef to current class, use the current
6510 class instead, as the typedef won't have any names inside
6511 it yet. */
6512 if (!COMPLETE_TYPE_P (new_scope)
6513 && currently_open_class (new_scope))
6514 new_scope = TYPE_MAIN_VARIANT (new_scope);
6515 }
6516 /* Make sure we look in the right scope the next time through
6517 the loop. */
6518 parser->scope = new_scope;
6519 }
6520
6521 /* If parsing tentatively, replace the sequence of tokens that makes
6522 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6523 token. That way, should we re-parse the token stream, we will
6524 not have to repeat the effort required to do the parse, nor will
6525 we issue duplicate error messages. */
6526 if (success && start)
6527 {
6528 cp_token *token;
6529
6530 token = cp_lexer_token_at (parser->lexer, start);
6531 /* Reset the contents of the START token. */
6532 token->type = CPP_NESTED_NAME_SPECIFIER;
6533 /* Retrieve any deferred checks. Do not pop this access checks yet
6534 so the memory will not be reclaimed during token replacing below. */
6535 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6536 token->u.tree_check_value->value = parser->scope;
6537 token->u.tree_check_value->checks = get_deferred_access_checks ();
6538 token->u.tree_check_value->qualifying_scope =
6539 parser->qualifying_scope;
6540 token->keyword = RID_MAX;
6541
6542 /* Purge all subsequent tokens. */
6543 cp_lexer_purge_tokens_after (parser->lexer, start);
6544 }
6545
6546 if (start)
6547 pop_to_parent_deferring_access_checks ();
6548
6549 return success ? parser->scope : NULL_TREE;
6550 }
6551
6552 /* Parse a nested-name-specifier. See
6553 cp_parser_nested_name_specifier_opt for details. This function
6554 behaves identically, except that it will an issue an error if no
6555 nested-name-specifier is present. */
6556
6557 static tree
6558 cp_parser_nested_name_specifier (cp_parser *parser,
6559 bool typename_keyword_p,
6560 bool check_dependency_p,
6561 bool type_p,
6562 bool is_declaration)
6563 {
6564 tree scope;
6565
6566 /* Look for the nested-name-specifier. */
6567 scope = cp_parser_nested_name_specifier_opt (parser,
6568 typename_keyword_p,
6569 check_dependency_p,
6570 type_p,
6571 is_declaration);
6572 /* If it was not present, issue an error message. */
6573 if (!scope)
6574 {
6575 cp_parser_error (parser, "expected nested-name-specifier");
6576 parser->scope = NULL_TREE;
6577 }
6578
6579 return scope;
6580 }
6581
6582 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6583 this is either a class-name or a namespace-name (which corresponds
6584 to the class-or-namespace-name production in the grammar). For
6585 C++0x, it can also be a type-name that refers to an enumeration
6586 type or a simple-template-id.
6587
6588 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6589 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6590 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6591 TYPE_P is TRUE iff the next name should be taken as a class-name,
6592 even the same name is declared to be another entity in the same
6593 scope.
6594
6595 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6596 specified by the class-or-namespace-name. If neither is found the
6597 ERROR_MARK_NODE is returned. */
6598
6599 static tree
6600 cp_parser_qualifying_entity (cp_parser *parser,
6601 bool typename_keyword_p,
6602 bool template_keyword_p,
6603 bool check_dependency_p,
6604 bool type_p,
6605 bool is_declaration)
6606 {
6607 tree saved_scope;
6608 tree saved_qualifying_scope;
6609 tree saved_object_scope;
6610 tree scope;
6611 bool only_class_p;
6612 bool successful_parse_p;
6613
6614 /* DR 743: decltype can appear in a nested-name-specifier. */
6615 if (cp_lexer_next_token_is_decltype (parser->lexer))
6616 {
6617 scope = cp_parser_decltype (parser);
6618 if (TREE_CODE (scope) != ENUMERAL_TYPE
6619 && !MAYBE_CLASS_TYPE_P (scope))
6620 {
6621 cp_parser_simulate_error (parser);
6622 return error_mark_node;
6623 }
6624 if (TYPE_NAME (scope))
6625 scope = TYPE_NAME (scope);
6626 return scope;
6627 }
6628
6629 /* Before we try to parse the class-name, we must save away the
6630 current PARSER->SCOPE since cp_parser_class_name will destroy
6631 it. */
6632 saved_scope = parser->scope;
6633 saved_qualifying_scope = parser->qualifying_scope;
6634 saved_object_scope = parser->object_scope;
6635 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6636 there is no need to look for a namespace-name. */
6637 only_class_p = template_keyword_p
6638 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6639 if (!only_class_p)
6640 cp_parser_parse_tentatively (parser);
6641 scope = cp_parser_class_name (parser,
6642 typename_keyword_p,
6643 template_keyword_p,
6644 type_p ? class_type : none_type,
6645 check_dependency_p,
6646 /*class_head_p=*/false,
6647 is_declaration,
6648 /*enum_ok=*/cxx_dialect > cxx98);
6649 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6650 /* If that didn't work, try for a namespace-name. */
6651 if (!only_class_p && !successful_parse_p)
6652 {
6653 /* Restore the saved scope. */
6654 parser->scope = saved_scope;
6655 parser->qualifying_scope = saved_qualifying_scope;
6656 parser->object_scope = saved_object_scope;
6657 /* If we are not looking at an identifier followed by the scope
6658 resolution operator, then this is not part of a
6659 nested-name-specifier. (Note that this function is only used
6660 to parse the components of a nested-name-specifier.) */
6661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6662 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6663 return error_mark_node;
6664 scope = cp_parser_namespace_name (parser);
6665 }
6666
6667 return scope;
6668 }
6669
6670 /* Return true if we are looking at a compound-literal, false otherwise. */
6671
6672 static bool
6673 cp_parser_compound_literal_p (cp_parser *parser)
6674 {
6675 cp_lexer_save_tokens (parser->lexer);
6676
6677 /* Skip tokens until the next token is a closing parenthesis.
6678 If we find the closing `)', and the next token is a `{', then
6679 we are looking at a compound-literal. */
6680 bool compound_literal_p
6681 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6682 /*consume_paren=*/true)
6683 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6684
6685 /* Roll back the tokens we skipped. */
6686 cp_lexer_rollback_tokens (parser->lexer);
6687
6688 return compound_literal_p;
6689 }
6690
6691 /* Return true if EXPR is the integer constant zero or a complex constant
6692 of zero, without any folding, but ignoring location wrappers. */
6693
6694 bool
6695 literal_integer_zerop (const_tree expr)
6696 {
6697 return (location_wrapper_p (expr)
6698 && integer_zerop (TREE_OPERAND (expr, 0)));
6699 }
6700
6701 /* Parse a postfix-expression.
6702
6703 postfix-expression:
6704 primary-expression
6705 postfix-expression [ expression ]
6706 postfix-expression ( expression-list [opt] )
6707 simple-type-specifier ( expression-list [opt] )
6708 typename :: [opt] nested-name-specifier identifier
6709 ( expression-list [opt] )
6710 typename :: [opt] nested-name-specifier template [opt] template-id
6711 ( expression-list [opt] )
6712 postfix-expression . template [opt] id-expression
6713 postfix-expression -> template [opt] id-expression
6714 postfix-expression . pseudo-destructor-name
6715 postfix-expression -> pseudo-destructor-name
6716 postfix-expression ++
6717 postfix-expression --
6718 dynamic_cast < type-id > ( expression )
6719 static_cast < type-id > ( expression )
6720 reinterpret_cast < type-id > ( expression )
6721 const_cast < type-id > ( expression )
6722 typeid ( expression )
6723 typeid ( type-id )
6724
6725 GNU Extension:
6726
6727 postfix-expression:
6728 ( type-id ) { initializer-list , [opt] }
6729
6730 This extension is a GNU version of the C99 compound-literal
6731 construct. (The C99 grammar uses `type-name' instead of `type-id',
6732 but they are essentially the same concept.)
6733
6734 If ADDRESS_P is true, the postfix expression is the operand of the
6735 `&' operator. CAST_P is true if this expression is the target of a
6736 cast.
6737
6738 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6739 class member access expressions [expr.ref].
6740
6741 Returns a representation of the expression. */
6742
6743 static cp_expr
6744 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6745 bool member_access_only_p, bool decltype_p,
6746 cp_id_kind * pidk_return)
6747 {
6748 cp_token *token;
6749 location_t loc;
6750 enum rid keyword;
6751 cp_id_kind idk = CP_ID_KIND_NONE;
6752 cp_expr postfix_expression = NULL_TREE;
6753 bool is_member_access = false;
6754
6755 /* Peek at the next token. */
6756 token = cp_lexer_peek_token (parser->lexer);
6757 loc = token->location;
6758 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6759
6760 /* Some of the productions are determined by keywords. */
6761 keyword = token->keyword;
6762 switch (keyword)
6763 {
6764 case RID_DYNCAST:
6765 case RID_STATCAST:
6766 case RID_REINTCAST:
6767 case RID_CONSTCAST:
6768 {
6769 tree type;
6770 cp_expr expression;
6771 const char *saved_message;
6772 bool saved_in_type_id_in_expr_p;
6773
6774 /* All of these can be handled in the same way from the point
6775 of view of parsing. Begin by consuming the token
6776 identifying the cast. */
6777 cp_lexer_consume_token (parser->lexer);
6778
6779 /* New types cannot be defined in the cast. */
6780 saved_message = parser->type_definition_forbidden_message;
6781 parser->type_definition_forbidden_message
6782 = G_("types may not be defined in casts");
6783
6784 /* Look for the opening `<'. */
6785 cp_parser_require (parser, CPP_LESS, RT_LESS);
6786 /* Parse the type to which we are casting. */
6787 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6788 parser->in_type_id_in_expr_p = true;
6789 type = cp_parser_type_id (parser);
6790 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6791 /* Look for the closing `>'. */
6792 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6793 /* Restore the old message. */
6794 parser->type_definition_forbidden_message = saved_message;
6795
6796 bool saved_greater_than_is_operator_p
6797 = parser->greater_than_is_operator_p;
6798 parser->greater_than_is_operator_p = true;
6799
6800 /* And the expression which is being cast. */
6801 matching_parens parens;
6802 parens.require_open (parser);
6803 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6804 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6805 RT_CLOSE_PAREN);
6806 location_t end_loc = close_paren ?
6807 close_paren->location : UNKNOWN_LOCATION;
6808
6809 parser->greater_than_is_operator_p
6810 = saved_greater_than_is_operator_p;
6811
6812 /* Only type conversions to integral or enumeration types
6813 can be used in constant-expressions. */
6814 if (!cast_valid_in_integral_constant_expression_p (type)
6815 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6816 {
6817 postfix_expression = error_mark_node;
6818 break;
6819 }
6820
6821 switch (keyword)
6822 {
6823 case RID_DYNCAST:
6824 postfix_expression
6825 = build_dynamic_cast (type, expression, tf_warning_or_error);
6826 break;
6827 case RID_STATCAST:
6828 postfix_expression
6829 = build_static_cast (type, expression, tf_warning_or_error);
6830 break;
6831 case RID_REINTCAST:
6832 postfix_expression
6833 = build_reinterpret_cast (type, expression,
6834 tf_warning_or_error);
6835 break;
6836 case RID_CONSTCAST:
6837 postfix_expression
6838 = build_const_cast (type, expression, tf_warning_or_error);
6839 break;
6840 default:
6841 gcc_unreachable ();
6842 }
6843
6844 /* Construct a location e.g. :
6845 reinterpret_cast <int *> (expr)
6846 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6847 ranging from the start of the "*_cast" token to the final closing
6848 paren, with the caret at the start. */
6849 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6850 postfix_expression.set_location (cp_cast_loc);
6851 }
6852 break;
6853
6854 case RID_TYPEID:
6855 {
6856 tree type;
6857 const char *saved_message;
6858 bool saved_in_type_id_in_expr_p;
6859
6860 /* Consume the `typeid' token. */
6861 cp_lexer_consume_token (parser->lexer);
6862 /* Look for the `(' token. */
6863 matching_parens parens;
6864 parens.require_open (parser);
6865 /* Types cannot be defined in a `typeid' expression. */
6866 saved_message = parser->type_definition_forbidden_message;
6867 parser->type_definition_forbidden_message
6868 = G_("types may not be defined in a %<typeid%> expression");
6869 /* We can't be sure yet whether we're looking at a type-id or an
6870 expression. */
6871 cp_parser_parse_tentatively (parser);
6872 /* Try a type-id first. */
6873 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6874 parser->in_type_id_in_expr_p = true;
6875 type = cp_parser_type_id (parser);
6876 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6877 /* Look for the `)' token. Otherwise, we can't be sure that
6878 we're not looking at an expression: consider `typeid (int
6879 (3))', for example. */
6880 cp_token *close_paren = parens.require_close (parser);
6881 /* If all went well, simply lookup the type-id. */
6882 if (cp_parser_parse_definitely (parser))
6883 postfix_expression = get_typeid (type, tf_warning_or_error);
6884 /* Otherwise, fall back to the expression variant. */
6885 else
6886 {
6887 tree expression;
6888
6889 /* Look for an expression. */
6890 expression = cp_parser_expression (parser, & idk);
6891 /* Compute its typeid. */
6892 postfix_expression = build_typeid (expression, tf_warning_or_error);
6893 /* Look for the `)' token. */
6894 close_paren = parens.require_close (parser);
6895 }
6896 /* Restore the saved message. */
6897 parser->type_definition_forbidden_message = saved_message;
6898 /* `typeid' may not appear in an integral constant expression. */
6899 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6900 postfix_expression = error_mark_node;
6901
6902 /* Construct a location e.g. :
6903 typeid (expr)
6904 ^~~~~~~~~~~~~
6905 ranging from the start of the "typeid" token to the final closing
6906 paren, with the caret at the start. */
6907 if (close_paren)
6908 {
6909 location_t typeid_loc
6910 = make_location (start_loc, start_loc, close_paren->location);
6911 postfix_expression.set_location (typeid_loc);
6912 postfix_expression.maybe_add_location_wrapper ();
6913 }
6914 }
6915 break;
6916
6917 case RID_TYPENAME:
6918 {
6919 tree type;
6920 /* The syntax permitted here is the same permitted for an
6921 elaborated-type-specifier. */
6922 ++parser->prevent_constrained_type_specifiers;
6923 type = cp_parser_elaborated_type_specifier (parser,
6924 /*is_friend=*/false,
6925 /*is_declaration=*/false);
6926 --parser->prevent_constrained_type_specifiers;
6927 postfix_expression = cp_parser_functional_cast (parser, type);
6928 }
6929 break;
6930
6931 case RID_ADDRESSOF:
6932 case RID_BUILTIN_SHUFFLE:
6933 case RID_BUILTIN_LAUNDER:
6934 {
6935 vec<tree, va_gc> *vec;
6936 unsigned int i;
6937 tree p;
6938
6939 cp_lexer_consume_token (parser->lexer);
6940 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6941 /*cast_p=*/false, /*allow_expansion_p=*/true,
6942 /*non_constant_p=*/NULL);
6943 if (vec == NULL)
6944 {
6945 postfix_expression = error_mark_node;
6946 break;
6947 }
6948
6949 FOR_EACH_VEC_ELT (*vec, i, p)
6950 mark_exp_read (p);
6951
6952 switch (keyword)
6953 {
6954 case RID_ADDRESSOF:
6955 if (vec->length () == 1)
6956 postfix_expression
6957 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6958 else
6959 {
6960 error_at (loc, "wrong number of arguments to "
6961 "%<__builtin_addressof%>");
6962 postfix_expression = error_mark_node;
6963 }
6964 break;
6965
6966 case RID_BUILTIN_LAUNDER:
6967 if (vec->length () == 1)
6968 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6969 tf_warning_or_error);
6970 else
6971 {
6972 error_at (loc, "wrong number of arguments to "
6973 "%<__builtin_launder%>");
6974 postfix_expression = error_mark_node;
6975 }
6976 break;
6977
6978 case RID_BUILTIN_SHUFFLE:
6979 if (vec->length () == 2)
6980 postfix_expression
6981 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6982 (*vec)[1], tf_warning_or_error);
6983 else if (vec->length () == 3)
6984 postfix_expression
6985 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6986 (*vec)[2], tf_warning_or_error);
6987 else
6988 {
6989 error_at (loc, "wrong number of arguments to "
6990 "%<__builtin_shuffle%>");
6991 postfix_expression = error_mark_node;
6992 }
6993 break;
6994
6995 default:
6996 gcc_unreachable ();
6997 }
6998 break;
6999 }
7000
7001 default:
7002 {
7003 tree type;
7004
7005 /* If the next thing is a simple-type-specifier, we may be
7006 looking at a functional cast. We could also be looking at
7007 an id-expression. So, we try the functional cast, and if
7008 that doesn't work we fall back to the primary-expression. */
7009 cp_parser_parse_tentatively (parser);
7010 /* Look for the simple-type-specifier. */
7011 ++parser->prevent_constrained_type_specifiers;
7012 type = cp_parser_simple_type_specifier (parser,
7013 /*decl_specs=*/NULL,
7014 CP_PARSER_FLAGS_NONE);
7015 --parser->prevent_constrained_type_specifiers;
7016 /* Parse the cast itself. */
7017 if (!cp_parser_error_occurred (parser))
7018 postfix_expression
7019 = cp_parser_functional_cast (parser, type);
7020 /* If that worked, we're done. */
7021 if (cp_parser_parse_definitely (parser))
7022 break;
7023
7024 /* If the functional-cast didn't work out, try a
7025 compound-literal. */
7026 if (cp_parser_allow_gnu_extensions_p (parser)
7027 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7028 {
7029 cp_expr initializer = NULL_TREE;
7030
7031 cp_parser_parse_tentatively (parser);
7032
7033 matching_parens parens;
7034 parens.consume_open (parser);
7035
7036 /* Avoid calling cp_parser_type_id pointlessly, see comment
7037 in cp_parser_cast_expression about c++/29234. */
7038 if (!cp_parser_compound_literal_p (parser))
7039 cp_parser_simulate_error (parser);
7040 else
7041 {
7042 /* Parse the type. */
7043 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7044 parser->in_type_id_in_expr_p = true;
7045 type = cp_parser_type_id (parser);
7046 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7047 parens.require_close (parser);
7048 }
7049
7050 /* If things aren't going well, there's no need to
7051 keep going. */
7052 if (!cp_parser_error_occurred (parser))
7053 {
7054 bool non_constant_p;
7055 /* Parse the brace-enclosed initializer list. */
7056 initializer = cp_parser_braced_list (parser,
7057 &non_constant_p);
7058 }
7059 /* If that worked, we're definitely looking at a
7060 compound-literal expression. */
7061 if (cp_parser_parse_definitely (parser))
7062 {
7063 /* Warn the user that a compound literal is not
7064 allowed in standard C++. */
7065 pedwarn (input_location, OPT_Wpedantic,
7066 "ISO C++ forbids compound-literals");
7067 /* For simplicity, we disallow compound literals in
7068 constant-expressions. We could
7069 allow compound literals of integer type, whose
7070 initializer was a constant, in constant
7071 expressions. Permitting that usage, as a further
7072 extension, would not change the meaning of any
7073 currently accepted programs. (Of course, as
7074 compound literals are not part of ISO C++, the
7075 standard has nothing to say.) */
7076 if (cp_parser_non_integral_constant_expression (parser,
7077 NIC_NCC))
7078 {
7079 postfix_expression = error_mark_node;
7080 break;
7081 }
7082 /* Form the representation of the compound-literal. */
7083 postfix_expression
7084 = finish_compound_literal (type, initializer,
7085 tf_warning_or_error, fcl_c99);
7086 postfix_expression.set_location (initializer.get_location ());
7087 break;
7088 }
7089 }
7090
7091 /* It must be a primary-expression. */
7092 postfix_expression
7093 = cp_parser_primary_expression (parser, address_p, cast_p,
7094 /*template_arg_p=*/false,
7095 decltype_p,
7096 &idk);
7097 }
7098 break;
7099 }
7100
7101 /* Note that we don't need to worry about calling build_cplus_new on a
7102 class-valued CALL_EXPR in decltype when it isn't the end of the
7103 postfix-expression; unary_complex_lvalue will take care of that for
7104 all these cases. */
7105
7106 /* Keep looping until the postfix-expression is complete. */
7107 while (true)
7108 {
7109 if (idk == CP_ID_KIND_UNQUALIFIED
7110 && identifier_p (postfix_expression)
7111 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7112 /* It is not a Koenig lookup function call. */
7113 postfix_expression
7114 = unqualified_name_lookup_error (postfix_expression);
7115
7116 /* Peek at the next token. */
7117 token = cp_lexer_peek_token (parser->lexer);
7118
7119 switch (token->type)
7120 {
7121 case CPP_OPEN_SQUARE:
7122 if (cp_next_tokens_can_be_std_attribute_p (parser))
7123 {
7124 cp_parser_error (parser,
7125 "two consecutive %<[%> shall "
7126 "only introduce an attribute");
7127 return error_mark_node;
7128 }
7129 postfix_expression
7130 = cp_parser_postfix_open_square_expression (parser,
7131 postfix_expression,
7132 false,
7133 decltype_p);
7134 postfix_expression.set_range (start_loc,
7135 postfix_expression.get_location ());
7136
7137 idk = CP_ID_KIND_NONE;
7138 is_member_access = false;
7139 break;
7140
7141 case CPP_OPEN_PAREN:
7142 /* postfix-expression ( expression-list [opt] ) */
7143 {
7144 bool koenig_p;
7145 bool is_builtin_constant_p;
7146 bool saved_integral_constant_expression_p = false;
7147 bool saved_non_integral_constant_expression_p = false;
7148 tsubst_flags_t complain = complain_flags (decltype_p);
7149 vec<tree, va_gc> *args;
7150 location_t close_paren_loc = UNKNOWN_LOCATION;
7151
7152 is_member_access = false;
7153
7154 is_builtin_constant_p
7155 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7156 if (is_builtin_constant_p)
7157 {
7158 /* The whole point of __builtin_constant_p is to allow
7159 non-constant expressions to appear as arguments. */
7160 saved_integral_constant_expression_p
7161 = parser->integral_constant_expression_p;
7162 saved_non_integral_constant_expression_p
7163 = parser->non_integral_constant_expression_p;
7164 parser->integral_constant_expression_p = false;
7165 }
7166 args = (cp_parser_parenthesized_expression_list
7167 (parser, non_attr,
7168 /*cast_p=*/false, /*allow_expansion_p=*/true,
7169 /*non_constant_p=*/NULL,
7170 /*close_paren_loc=*/&close_paren_loc,
7171 /*wrap_locations_p=*/true));
7172 if (is_builtin_constant_p)
7173 {
7174 parser->integral_constant_expression_p
7175 = saved_integral_constant_expression_p;
7176 parser->non_integral_constant_expression_p
7177 = saved_non_integral_constant_expression_p;
7178 }
7179
7180 if (args == NULL)
7181 {
7182 postfix_expression = error_mark_node;
7183 break;
7184 }
7185
7186 /* Function calls are not permitted in
7187 constant-expressions. */
7188 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7189 && cp_parser_non_integral_constant_expression (parser,
7190 NIC_FUNC_CALL))
7191 {
7192 postfix_expression = error_mark_node;
7193 release_tree_vector (args);
7194 break;
7195 }
7196
7197 koenig_p = false;
7198 if (idk == CP_ID_KIND_UNQUALIFIED
7199 || idk == CP_ID_KIND_TEMPLATE_ID)
7200 {
7201 if (identifier_p (postfix_expression)
7202 /* In C++2A, we may need to perform ADL for a template
7203 name. */
7204 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7205 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7206 {
7207 if (!args->is_empty ())
7208 {
7209 koenig_p = true;
7210 if (!any_type_dependent_arguments_p (args))
7211 postfix_expression
7212 = perform_koenig_lookup (postfix_expression, args,
7213 complain);
7214 }
7215 else
7216 postfix_expression
7217 = unqualified_fn_lookup_error (postfix_expression);
7218 }
7219 /* We do not perform argument-dependent lookup if
7220 normal lookup finds a non-function, in accordance
7221 with the expected resolution of DR 218. */
7222 else if (!args->is_empty ()
7223 && is_overloaded_fn (postfix_expression))
7224 {
7225 tree fn = get_first_fn (postfix_expression);
7226 fn = STRIP_TEMPLATE (fn);
7227
7228 /* Do not do argument dependent lookup if regular
7229 lookup finds a member function or a block-scope
7230 function declaration. [basic.lookup.argdep]/3 */
7231 if (!DECL_FUNCTION_MEMBER_P (fn)
7232 && !DECL_LOCAL_FUNCTION_P (fn))
7233 {
7234 koenig_p = true;
7235 if (!any_type_dependent_arguments_p (args))
7236 postfix_expression
7237 = perform_koenig_lookup (postfix_expression, args,
7238 complain);
7239 }
7240 }
7241 }
7242
7243 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7244 {
7245 tree instance = TREE_OPERAND (postfix_expression, 0);
7246 tree fn = TREE_OPERAND (postfix_expression, 1);
7247
7248 if (processing_template_decl
7249 && (type_dependent_object_expression_p (instance)
7250 || (!BASELINK_P (fn)
7251 && TREE_CODE (fn) != FIELD_DECL)
7252 || type_dependent_expression_p (fn)
7253 || any_type_dependent_arguments_p (args)))
7254 {
7255 maybe_generic_this_capture (instance, fn);
7256 postfix_expression
7257 = build_min_nt_call_vec (postfix_expression, args);
7258 release_tree_vector (args);
7259 break;
7260 }
7261
7262 if (BASELINK_P (fn))
7263 {
7264 postfix_expression
7265 = (build_new_method_call
7266 (instance, fn, &args, NULL_TREE,
7267 (idk == CP_ID_KIND_QUALIFIED
7268 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7269 : LOOKUP_NORMAL),
7270 /*fn_p=*/NULL,
7271 complain));
7272 }
7273 else
7274 postfix_expression
7275 = finish_call_expr (postfix_expression, &args,
7276 /*disallow_virtual=*/false,
7277 /*koenig_p=*/false,
7278 complain);
7279 }
7280 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7281 || TREE_CODE (postfix_expression) == MEMBER_REF
7282 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7283 postfix_expression = (build_offset_ref_call_from_tree
7284 (postfix_expression, &args,
7285 complain));
7286 else if (idk == CP_ID_KIND_QUALIFIED)
7287 /* A call to a static class member, or a namespace-scope
7288 function. */
7289 postfix_expression
7290 = finish_call_expr (postfix_expression, &args,
7291 /*disallow_virtual=*/true,
7292 koenig_p,
7293 complain);
7294 else
7295 /* All other function calls. */
7296 postfix_expression
7297 = finish_call_expr (postfix_expression, &args,
7298 /*disallow_virtual=*/false,
7299 koenig_p,
7300 complain);
7301
7302 if (close_paren_loc != UNKNOWN_LOCATION)
7303 {
7304 location_t combined_loc = make_location (token->location,
7305 start_loc,
7306 close_paren_loc);
7307 postfix_expression.set_location (combined_loc);
7308 }
7309
7310 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7311 idk = CP_ID_KIND_NONE;
7312
7313 release_tree_vector (args);
7314 }
7315 break;
7316
7317 case CPP_DOT:
7318 case CPP_DEREF:
7319 /* postfix-expression . template [opt] id-expression
7320 postfix-expression . pseudo-destructor-name
7321 postfix-expression -> template [opt] id-expression
7322 postfix-expression -> pseudo-destructor-name */
7323
7324 /* Consume the `.' or `->' operator. */
7325 cp_lexer_consume_token (parser->lexer);
7326
7327 postfix_expression
7328 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7329 postfix_expression,
7330 false, &idk, loc);
7331
7332 is_member_access = true;
7333 break;
7334
7335 case CPP_PLUS_PLUS:
7336 /* postfix-expression ++ */
7337 /* Consume the `++' token. */
7338 cp_lexer_consume_token (parser->lexer);
7339 /* Generate a representation for the complete expression. */
7340 postfix_expression
7341 = finish_increment_expr (postfix_expression,
7342 POSTINCREMENT_EXPR);
7343 /* Increments may not appear in constant-expressions. */
7344 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7345 postfix_expression = error_mark_node;
7346 idk = CP_ID_KIND_NONE;
7347 is_member_access = false;
7348 break;
7349
7350 case CPP_MINUS_MINUS:
7351 /* postfix-expression -- */
7352 /* Consume the `--' token. */
7353 cp_lexer_consume_token (parser->lexer);
7354 /* Generate a representation for the complete expression. */
7355 postfix_expression
7356 = finish_increment_expr (postfix_expression,
7357 POSTDECREMENT_EXPR);
7358 /* Decrements may not appear in constant-expressions. */
7359 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7360 postfix_expression = error_mark_node;
7361 idk = CP_ID_KIND_NONE;
7362 is_member_access = false;
7363 break;
7364
7365 default:
7366 if (pidk_return != NULL)
7367 * pidk_return = idk;
7368 if (member_access_only_p)
7369 return is_member_access
7370 ? postfix_expression
7371 : cp_expr (error_mark_node);
7372 else
7373 return postfix_expression;
7374 }
7375 }
7376
7377 /* We should never get here. */
7378 gcc_unreachable ();
7379 return error_mark_node;
7380 }
7381
7382 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7383 by cp_parser_builtin_offsetof. We're looking for
7384
7385 postfix-expression [ expression ]
7386 postfix-expression [ braced-init-list ] (C++11)
7387
7388 FOR_OFFSETOF is set if we're being called in that context, which
7389 changes how we deal with integer constant expressions. */
7390
7391 static tree
7392 cp_parser_postfix_open_square_expression (cp_parser *parser,
7393 tree postfix_expression,
7394 bool for_offsetof,
7395 bool decltype_p)
7396 {
7397 tree index = NULL_TREE;
7398 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7399 bool saved_greater_than_is_operator_p;
7400
7401 /* Consume the `[' token. */
7402 cp_lexer_consume_token (parser->lexer);
7403
7404 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7405 parser->greater_than_is_operator_p = true;
7406
7407 /* Parse the index expression. */
7408 /* ??? For offsetof, there is a question of what to allow here. If
7409 offsetof is not being used in an integral constant expression context,
7410 then we *could* get the right answer by computing the value at runtime.
7411 If we are in an integral constant expression context, then we might
7412 could accept any constant expression; hard to say without analysis.
7413 Rather than open the barn door too wide right away, allow only integer
7414 constant expressions here. */
7415 if (for_offsetof)
7416 index = cp_parser_constant_expression (parser);
7417 else
7418 {
7419 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7420 {
7421 bool expr_nonconst_p;
7422 cp_lexer_set_source_position (parser->lexer);
7423 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7424 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7425 }
7426 else
7427 index = cp_parser_expression (parser);
7428 }
7429
7430 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7431
7432 /* Look for the closing `]'. */
7433 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7434
7435 /* Build the ARRAY_REF. */
7436 postfix_expression = grok_array_decl (loc, postfix_expression,
7437 index, decltype_p);
7438
7439 /* When not doing offsetof, array references are not permitted in
7440 constant-expressions. */
7441 if (!for_offsetof
7442 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7443 postfix_expression = error_mark_node;
7444
7445 return postfix_expression;
7446 }
7447
7448 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7449 dereference of incomplete type, returns true if error_mark_node should
7450 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7451 and *DEPENDENT_P. */
7452
7453 bool
7454 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7455 bool *dependent_p)
7456 {
7457 /* In a template, be permissive by treating an object expression
7458 of incomplete type as dependent (after a pedwarn). */
7459 diagnostic_t kind = (processing_template_decl
7460 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7461
7462 switch (TREE_CODE (*postfix_expression))
7463 {
7464 case CAST_EXPR:
7465 case REINTERPRET_CAST_EXPR:
7466 case CONST_CAST_EXPR:
7467 case STATIC_CAST_EXPR:
7468 case DYNAMIC_CAST_EXPR:
7469 case IMPLICIT_CONV_EXPR:
7470 case VIEW_CONVERT_EXPR:
7471 case NON_LVALUE_EXPR:
7472 kind = DK_ERROR;
7473 break;
7474 case OVERLOAD:
7475 /* Don't emit any diagnostic for OVERLOADs. */
7476 kind = DK_IGNORED;
7477 break;
7478 default:
7479 /* Avoid clobbering e.g. DECLs. */
7480 if (!EXPR_P (*postfix_expression))
7481 kind = DK_ERROR;
7482 break;
7483 }
7484
7485 if (kind == DK_IGNORED)
7486 return false;
7487
7488 location_t exploc = location_of (*postfix_expression);
7489 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7490 if (!MAYBE_CLASS_TYPE_P (*scope))
7491 return true;
7492 if (kind == DK_ERROR)
7493 *scope = *postfix_expression = error_mark_node;
7494 else if (processing_template_decl)
7495 {
7496 *dependent_p = true;
7497 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7498 }
7499 return false;
7500 }
7501
7502 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7503 by cp_parser_builtin_offsetof. We're looking for
7504
7505 postfix-expression . template [opt] id-expression
7506 postfix-expression . pseudo-destructor-name
7507 postfix-expression -> template [opt] id-expression
7508 postfix-expression -> pseudo-destructor-name
7509
7510 FOR_OFFSETOF is set if we're being called in that context. That sorta
7511 limits what of the above we'll actually accept, but nevermind.
7512 TOKEN_TYPE is the "." or "->" token, which will already have been
7513 removed from the stream. */
7514
7515 static tree
7516 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7517 enum cpp_ttype token_type,
7518 cp_expr postfix_expression,
7519 bool for_offsetof, cp_id_kind *idk,
7520 location_t location)
7521 {
7522 tree name;
7523 bool dependent_p;
7524 bool pseudo_destructor_p;
7525 tree scope = NULL_TREE;
7526 location_t start_loc = postfix_expression.get_start ();
7527
7528 /* If this is a `->' operator, dereference the pointer. */
7529 if (token_type == CPP_DEREF)
7530 postfix_expression = build_x_arrow (location, postfix_expression,
7531 tf_warning_or_error);
7532 /* Check to see whether or not the expression is type-dependent and
7533 not the current instantiation. */
7534 dependent_p = type_dependent_object_expression_p (postfix_expression);
7535 /* The identifier following the `->' or `.' is not qualified. */
7536 parser->scope = NULL_TREE;
7537 parser->qualifying_scope = NULL_TREE;
7538 parser->object_scope = NULL_TREE;
7539 *idk = CP_ID_KIND_NONE;
7540
7541 /* Enter the scope corresponding to the type of the object
7542 given by the POSTFIX_EXPRESSION. */
7543 if (!dependent_p)
7544 {
7545 scope = TREE_TYPE (postfix_expression);
7546 /* According to the standard, no expression should ever have
7547 reference type. Unfortunately, we do not currently match
7548 the standard in this respect in that our internal representation
7549 of an expression may have reference type even when the standard
7550 says it does not. Therefore, we have to manually obtain the
7551 underlying type here. */
7552 scope = non_reference (scope);
7553 /* The type of the POSTFIX_EXPRESSION must be complete. */
7554 /* Unlike the object expression in other contexts, *this is not
7555 required to be of complete type for purposes of class member
7556 access (5.2.5) outside the member function body. */
7557 if (postfix_expression != current_class_ref
7558 && scope != error_mark_node
7559 && !currently_open_class (scope))
7560 {
7561 scope = complete_type (scope);
7562 if (!COMPLETE_TYPE_P (scope)
7563 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7564 &dependent_p))
7565 return error_mark_node;
7566 }
7567
7568 if (!dependent_p)
7569 {
7570 /* Let the name lookup machinery know that we are processing a
7571 class member access expression. */
7572 parser->context->object_type = scope;
7573 /* If something went wrong, we want to be able to discern that case,
7574 as opposed to the case where there was no SCOPE due to the type
7575 of expression being dependent. */
7576 if (!scope)
7577 scope = error_mark_node;
7578 /* If the SCOPE was erroneous, make the various semantic analysis
7579 functions exit quickly -- and without issuing additional error
7580 messages. */
7581 if (scope == error_mark_node)
7582 postfix_expression = error_mark_node;
7583 }
7584 }
7585
7586 if (dependent_p)
7587 /* Tell cp_parser_lookup_name that there was an object, even though it's
7588 type-dependent. */
7589 parser->context->object_type = unknown_type_node;
7590
7591 /* Assume this expression is not a pseudo-destructor access. */
7592 pseudo_destructor_p = false;
7593
7594 /* If the SCOPE is a scalar type, then, if this is a valid program,
7595 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7596 is type dependent, it can be pseudo-destructor-name or something else.
7597 Try to parse it as pseudo-destructor-name first. */
7598 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7599 {
7600 tree s;
7601 tree type;
7602
7603 cp_parser_parse_tentatively (parser);
7604 /* Parse the pseudo-destructor-name. */
7605 s = NULL_TREE;
7606 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7607 &s, &type);
7608 if (dependent_p
7609 && (cp_parser_error_occurred (parser)
7610 || !SCALAR_TYPE_P (type)))
7611 cp_parser_abort_tentative_parse (parser);
7612 else if (cp_parser_parse_definitely (parser))
7613 {
7614 pseudo_destructor_p = true;
7615 postfix_expression
7616 = finish_pseudo_destructor_expr (postfix_expression,
7617 s, type, location);
7618 }
7619 }
7620
7621 if (!pseudo_destructor_p)
7622 {
7623 /* If the SCOPE is not a scalar type, we are looking at an
7624 ordinary class member access expression, rather than a
7625 pseudo-destructor-name. */
7626 bool template_p;
7627 cp_token *token = cp_lexer_peek_token (parser->lexer);
7628 /* Parse the id-expression. */
7629 name = (cp_parser_id_expression
7630 (parser,
7631 cp_parser_optional_template_keyword (parser),
7632 /*check_dependency_p=*/true,
7633 &template_p,
7634 /*declarator_p=*/false,
7635 /*optional_p=*/false));
7636 /* In general, build a SCOPE_REF if the member name is qualified.
7637 However, if the name was not dependent and has already been
7638 resolved; there is no need to build the SCOPE_REF. For example;
7639
7640 struct X { void f(); };
7641 template <typename T> void f(T* t) { t->X::f(); }
7642
7643 Even though "t" is dependent, "X::f" is not and has been resolved
7644 to a BASELINK; there is no need to include scope information. */
7645
7646 /* But we do need to remember that there was an explicit scope for
7647 virtual function calls. */
7648 if (parser->scope)
7649 *idk = CP_ID_KIND_QUALIFIED;
7650
7651 /* If the name is a template-id that names a type, we will get a
7652 TYPE_DECL here. That is invalid code. */
7653 if (TREE_CODE (name) == TYPE_DECL)
7654 {
7655 error_at (token->location, "invalid use of %qD", name);
7656 postfix_expression = error_mark_node;
7657 }
7658 else
7659 {
7660 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7661 {
7662 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7663 {
7664 error_at (token->location, "%<%D::%D%> is not a class member",
7665 parser->scope, name);
7666 postfix_expression = error_mark_node;
7667 }
7668 else
7669 name = build_qualified_name (/*type=*/NULL_TREE,
7670 parser->scope,
7671 name,
7672 template_p);
7673 parser->scope = NULL_TREE;
7674 parser->qualifying_scope = NULL_TREE;
7675 parser->object_scope = NULL_TREE;
7676 }
7677 if (parser->scope && name && BASELINK_P (name))
7678 adjust_result_of_qualified_name_lookup
7679 (name, parser->scope, scope);
7680 postfix_expression
7681 = finish_class_member_access_expr (postfix_expression, name,
7682 template_p,
7683 tf_warning_or_error);
7684 /* Build a location e.g.:
7685 ptr->access_expr
7686 ~~~^~~~~~~~~~~~~
7687 where the caret is at the deref token, ranging from
7688 the start of postfix_expression to the end of the access expr. */
7689 location_t end_loc
7690 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7691 location_t combined_loc
7692 = make_location (input_location, start_loc, end_loc);
7693 protected_set_expr_location (postfix_expression, combined_loc);
7694 }
7695 }
7696
7697 /* We no longer need to look up names in the scope of the object on
7698 the left-hand side of the `.' or `->' operator. */
7699 parser->context->object_type = NULL_TREE;
7700
7701 /* Outside of offsetof, these operators may not appear in
7702 constant-expressions. */
7703 if (!for_offsetof
7704 && (cp_parser_non_integral_constant_expression
7705 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7706 postfix_expression = error_mark_node;
7707
7708 return postfix_expression;
7709 }
7710
7711 /* Parse a parenthesized expression-list.
7712
7713 expression-list:
7714 assignment-expression
7715 expression-list, assignment-expression
7716
7717 attribute-list:
7718 expression-list
7719 identifier
7720 identifier, expression-list
7721
7722 CAST_P is true if this expression is the target of a cast.
7723
7724 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7725 argument pack.
7726
7727 WRAP_LOCATIONS_P is true if expressions within this list for which
7728 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7729 their source locations.
7730
7731 Returns a vector of trees. Each element is a representation of an
7732 assignment-expression. NULL is returned if the ( and or ) are
7733 missing. An empty, but allocated, vector is returned on no
7734 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7735 if we are parsing an attribute list for an attribute that wants a
7736 plain identifier argument, normal_attr for an attribute that wants
7737 an expression, or non_attr if we aren't parsing an attribute list. If
7738 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7739 not all of the expressions in the list were constant.
7740 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7741 will be written to with the location of the closing parenthesis. If
7742 an error occurs, it may or may not be written to. */
7743
7744 static vec<tree, va_gc> *
7745 cp_parser_parenthesized_expression_list (cp_parser* parser,
7746 int is_attribute_list,
7747 bool cast_p,
7748 bool allow_expansion_p,
7749 bool *non_constant_p,
7750 location_t *close_paren_loc,
7751 bool wrap_locations_p)
7752 {
7753 vec<tree, va_gc> *expression_list;
7754 bool fold_expr_p = is_attribute_list != non_attr;
7755 tree identifier = NULL_TREE;
7756 bool saved_greater_than_is_operator_p;
7757
7758 /* Assume all the expressions will be constant. */
7759 if (non_constant_p)
7760 *non_constant_p = false;
7761
7762 matching_parens parens;
7763 if (!parens.require_open (parser))
7764 return NULL;
7765
7766 expression_list = make_tree_vector ();
7767
7768 /* Within a parenthesized expression, a `>' token is always
7769 the greater-than operator. */
7770 saved_greater_than_is_operator_p
7771 = parser->greater_than_is_operator_p;
7772 parser->greater_than_is_operator_p = true;
7773
7774 cp_expr expr (NULL_TREE);
7775
7776 /* Consume expressions until there are no more. */
7777 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7778 while (true)
7779 {
7780 /* At the beginning of attribute lists, check to see if the
7781 next token is an identifier. */
7782 if (is_attribute_list == id_attr
7783 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7784 {
7785 cp_token *token;
7786
7787 /* Consume the identifier. */
7788 token = cp_lexer_consume_token (parser->lexer);
7789 /* Save the identifier. */
7790 identifier = token->u.value;
7791 }
7792 else
7793 {
7794 bool expr_non_constant_p;
7795
7796 /* Parse the next assignment-expression. */
7797 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7798 {
7799 /* A braced-init-list. */
7800 cp_lexer_set_source_position (parser->lexer);
7801 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7802 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7803 if (non_constant_p && expr_non_constant_p)
7804 *non_constant_p = true;
7805 }
7806 else if (non_constant_p)
7807 {
7808 expr = (cp_parser_constant_expression
7809 (parser, /*allow_non_constant_p=*/true,
7810 &expr_non_constant_p));
7811 if (expr_non_constant_p)
7812 *non_constant_p = true;
7813 }
7814 else
7815 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7816 cast_p);
7817
7818 if (fold_expr_p)
7819 expr = instantiate_non_dependent_expr (expr);
7820
7821 /* If we have an ellipsis, then this is an expression
7822 expansion. */
7823 if (allow_expansion_p
7824 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7825 {
7826 /* Consume the `...'. */
7827 cp_lexer_consume_token (parser->lexer);
7828
7829 /* Build the argument pack. */
7830 expr = make_pack_expansion (expr);
7831 }
7832
7833 if (wrap_locations_p)
7834 expr.maybe_add_location_wrapper ();
7835
7836 /* Add it to the list. We add error_mark_node
7837 expressions to the list, so that we can still tell if
7838 the correct form for a parenthesized expression-list
7839 is found. That gives better errors. */
7840 vec_safe_push (expression_list, expr.get_value ());
7841
7842 if (expr == error_mark_node)
7843 goto skip_comma;
7844 }
7845
7846 /* After the first item, attribute lists look the same as
7847 expression lists. */
7848 is_attribute_list = non_attr;
7849
7850 get_comma:;
7851 /* If the next token isn't a `,', then we are done. */
7852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7853 break;
7854
7855 /* Otherwise, consume the `,' and keep going. */
7856 cp_lexer_consume_token (parser->lexer);
7857 }
7858
7859 if (close_paren_loc)
7860 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7861
7862 if (!parens.require_close (parser))
7863 {
7864 int ending;
7865
7866 skip_comma:;
7867 /* We try and resync to an unnested comma, as that will give the
7868 user better diagnostics. */
7869 ending = cp_parser_skip_to_closing_parenthesis (parser,
7870 /*recovering=*/true,
7871 /*or_comma=*/true,
7872 /*consume_paren=*/true);
7873 if (ending < 0)
7874 goto get_comma;
7875 if (!ending)
7876 {
7877 parser->greater_than_is_operator_p
7878 = saved_greater_than_is_operator_p;
7879 return NULL;
7880 }
7881 }
7882
7883 parser->greater_than_is_operator_p
7884 = saved_greater_than_is_operator_p;
7885
7886 if (identifier)
7887 vec_safe_insert (expression_list, 0, identifier);
7888
7889 return expression_list;
7890 }
7891
7892 /* Parse a pseudo-destructor-name.
7893
7894 pseudo-destructor-name:
7895 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7896 :: [opt] nested-name-specifier template template-id :: ~ type-name
7897 :: [opt] nested-name-specifier [opt] ~ type-name
7898
7899 If either of the first two productions is used, sets *SCOPE to the
7900 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7901 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7902 or ERROR_MARK_NODE if the parse fails. */
7903
7904 static void
7905 cp_parser_pseudo_destructor_name (cp_parser* parser,
7906 tree object,
7907 tree* scope,
7908 tree* type)
7909 {
7910 bool nested_name_specifier_p;
7911
7912 /* Handle ~auto. */
7913 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7914 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7915 && !type_dependent_expression_p (object))
7916 {
7917 if (cxx_dialect < cxx14)
7918 pedwarn (input_location, 0,
7919 "%<~auto%> only available with "
7920 "-std=c++14 or -std=gnu++14");
7921 cp_lexer_consume_token (parser->lexer);
7922 cp_lexer_consume_token (parser->lexer);
7923 *scope = NULL_TREE;
7924 *type = TREE_TYPE (object);
7925 return;
7926 }
7927
7928 /* Assume that things will not work out. */
7929 *type = error_mark_node;
7930
7931 /* Look for the optional `::' operator. */
7932 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7933 /* Look for the optional nested-name-specifier. */
7934 nested_name_specifier_p
7935 = (cp_parser_nested_name_specifier_opt (parser,
7936 /*typename_keyword_p=*/false,
7937 /*check_dependency_p=*/true,
7938 /*type_p=*/false,
7939 /*is_declaration=*/false)
7940 != NULL_TREE);
7941 /* Now, if we saw a nested-name-specifier, we might be doing the
7942 second production. */
7943 if (nested_name_specifier_p
7944 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7945 {
7946 /* Consume the `template' keyword. */
7947 cp_lexer_consume_token (parser->lexer);
7948 /* Parse the template-id. */
7949 cp_parser_template_id (parser,
7950 /*template_keyword_p=*/true,
7951 /*check_dependency_p=*/false,
7952 class_type,
7953 /*is_declaration=*/true);
7954 /* Look for the `::' token. */
7955 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7956 }
7957 /* If the next token is not a `~', then there might be some
7958 additional qualification. */
7959 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7960 {
7961 /* At this point, we're looking for "type-name :: ~". The type-name
7962 must not be a class-name, since this is a pseudo-destructor. So,
7963 it must be either an enum-name, or a typedef-name -- both of which
7964 are just identifiers. So, we peek ahead to check that the "::"
7965 and "~" tokens are present; if they are not, then we can avoid
7966 calling type_name. */
7967 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7968 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7969 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7970 {
7971 cp_parser_error (parser, "non-scalar type");
7972 return;
7973 }
7974
7975 /* Look for the type-name. */
7976 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7977 if (*scope == error_mark_node)
7978 return;
7979
7980 /* Look for the `::' token. */
7981 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7982 }
7983 else
7984 *scope = NULL_TREE;
7985
7986 /* Look for the `~'. */
7987 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7988
7989 /* Once we see the ~, this has to be a pseudo-destructor. */
7990 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7991 cp_parser_commit_to_topmost_tentative_parse (parser);
7992
7993 /* Look for the type-name again. We are not responsible for
7994 checking that it matches the first type-name. */
7995 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7996 }
7997
7998 /* Parse a unary-expression.
7999
8000 unary-expression:
8001 postfix-expression
8002 ++ cast-expression
8003 -- cast-expression
8004 unary-operator cast-expression
8005 sizeof unary-expression
8006 sizeof ( type-id )
8007 alignof ( type-id ) [C++0x]
8008 new-expression
8009 delete-expression
8010
8011 GNU Extensions:
8012
8013 unary-expression:
8014 __extension__ cast-expression
8015 __alignof__ unary-expression
8016 __alignof__ ( type-id )
8017 alignof unary-expression [C++0x]
8018 __real__ cast-expression
8019 __imag__ cast-expression
8020 && identifier
8021 sizeof ( type-id ) { initializer-list , [opt] }
8022 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8023 __alignof__ ( type-id ) { initializer-list , [opt] }
8024
8025 ADDRESS_P is true iff the unary-expression is appearing as the
8026 operand of the `&' operator. CAST_P is true if this expression is
8027 the target of a cast.
8028
8029 Returns a representation of the expression. */
8030
8031 static cp_expr
8032 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8033 bool address_p, bool cast_p, bool decltype_p)
8034 {
8035 cp_token *token;
8036 enum tree_code unary_operator;
8037
8038 /* Peek at the next token. */
8039 token = cp_lexer_peek_token (parser->lexer);
8040 /* Some keywords give away the kind of expression. */
8041 if (token->type == CPP_KEYWORD)
8042 {
8043 enum rid keyword = token->keyword;
8044
8045 switch (keyword)
8046 {
8047 case RID_ALIGNOF:
8048 case RID_SIZEOF:
8049 {
8050 tree operand, ret;
8051 enum tree_code op;
8052 location_t start_loc = token->location;
8053
8054 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8055 bool std_alignof = id_equal (token->u.value, "alignof");
8056
8057 /* Consume the token. */
8058 cp_lexer_consume_token (parser->lexer);
8059 /* Parse the operand. */
8060 operand = cp_parser_sizeof_operand (parser, keyword);
8061
8062 if (TYPE_P (operand))
8063 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8064 true);
8065 else
8066 {
8067 /* ISO C++ defines alignof only with types, not with
8068 expressions. So pedwarn if alignof is used with a non-
8069 type expression. However, __alignof__ is ok. */
8070 if (std_alignof)
8071 pedwarn (token->location, OPT_Wpedantic,
8072 "ISO C++ does not allow %<alignof%> "
8073 "with a non-type");
8074
8075 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8076 }
8077 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8078 SIZEOF_EXPR with the original operand. */
8079 if (op == SIZEOF_EXPR && ret != error_mark_node)
8080 {
8081 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8082 {
8083 if (!processing_template_decl && TYPE_P (operand))
8084 {
8085 ret = build_min (SIZEOF_EXPR, size_type_node,
8086 build1 (NOP_EXPR, operand,
8087 error_mark_node));
8088 SIZEOF_EXPR_TYPE_P (ret) = 1;
8089 }
8090 else
8091 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8092 TREE_SIDE_EFFECTS (ret) = 0;
8093 TREE_READONLY (ret) = 1;
8094 }
8095 }
8096
8097 /* Construct a location e.g. :
8098 alignof (expr)
8099 ^~~~~~~~~~~~~~
8100 with start == caret at the start of the "alignof"/"sizeof"
8101 token, with the endpoint at the final closing paren. */
8102 location_t finish_loc
8103 = cp_lexer_previous_token (parser->lexer)->location;
8104 location_t compound_loc
8105 = make_location (start_loc, start_loc, finish_loc);
8106
8107 cp_expr ret_expr (ret);
8108 ret_expr.set_location (compound_loc);
8109 ret_expr = ret_expr.maybe_add_location_wrapper ();
8110 return ret_expr;
8111 }
8112
8113 case RID_NEW:
8114 return cp_parser_new_expression (parser);
8115
8116 case RID_DELETE:
8117 return cp_parser_delete_expression (parser);
8118
8119 case RID_EXTENSION:
8120 {
8121 /* The saved value of the PEDANTIC flag. */
8122 int saved_pedantic;
8123 tree expr;
8124
8125 /* Save away the PEDANTIC flag. */
8126 cp_parser_extension_opt (parser, &saved_pedantic);
8127 /* Parse the cast-expression. */
8128 expr = cp_parser_simple_cast_expression (parser);
8129 /* Restore the PEDANTIC flag. */
8130 pedantic = saved_pedantic;
8131
8132 return expr;
8133 }
8134
8135 case RID_REALPART:
8136 case RID_IMAGPART:
8137 {
8138 tree expression;
8139
8140 /* Consume the `__real__' or `__imag__' token. */
8141 cp_lexer_consume_token (parser->lexer);
8142 /* Parse the cast-expression. */
8143 expression = cp_parser_simple_cast_expression (parser);
8144 /* Create the complete representation. */
8145 return build_x_unary_op (token->location,
8146 (keyword == RID_REALPART
8147 ? REALPART_EXPR : IMAGPART_EXPR),
8148 expression,
8149 tf_warning_or_error);
8150 }
8151 break;
8152
8153 case RID_TRANSACTION_ATOMIC:
8154 case RID_TRANSACTION_RELAXED:
8155 return cp_parser_transaction_expression (parser, keyword);
8156
8157 case RID_NOEXCEPT:
8158 {
8159 tree expr;
8160 const char *saved_message;
8161 bool saved_integral_constant_expression_p;
8162 bool saved_non_integral_constant_expression_p;
8163 bool saved_greater_than_is_operator_p;
8164
8165 location_t start_loc = token->location;
8166
8167 cp_lexer_consume_token (parser->lexer);
8168 matching_parens parens;
8169 parens.require_open (parser);
8170
8171 saved_message = parser->type_definition_forbidden_message;
8172 parser->type_definition_forbidden_message
8173 = G_("types may not be defined in %<noexcept%> expressions");
8174
8175 saved_integral_constant_expression_p
8176 = parser->integral_constant_expression_p;
8177 saved_non_integral_constant_expression_p
8178 = parser->non_integral_constant_expression_p;
8179 parser->integral_constant_expression_p = false;
8180
8181 saved_greater_than_is_operator_p
8182 = parser->greater_than_is_operator_p;
8183 parser->greater_than_is_operator_p = true;
8184
8185 ++cp_unevaluated_operand;
8186 ++c_inhibit_evaluation_warnings;
8187 ++cp_noexcept_operand;
8188 expr = cp_parser_expression (parser);
8189 --cp_noexcept_operand;
8190 --c_inhibit_evaluation_warnings;
8191 --cp_unevaluated_operand;
8192
8193 parser->greater_than_is_operator_p
8194 = saved_greater_than_is_operator_p;
8195
8196 parser->integral_constant_expression_p
8197 = saved_integral_constant_expression_p;
8198 parser->non_integral_constant_expression_p
8199 = saved_non_integral_constant_expression_p;
8200
8201 parser->type_definition_forbidden_message = saved_message;
8202
8203 location_t finish_loc
8204 = cp_lexer_peek_token (parser->lexer)->location;
8205 parens.require_close (parser);
8206
8207 /* Construct a location of the form:
8208 noexcept (expr)
8209 ^~~~~~~~~~~~~~~
8210 with start == caret, finishing at the close-paren. */
8211 location_t noexcept_loc
8212 = make_location (start_loc, start_loc, finish_loc);
8213
8214 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8215 noexcept_loc);
8216 }
8217
8218 default:
8219 break;
8220 }
8221 }
8222
8223 /* Look for the `:: new' and `:: delete', which also signal the
8224 beginning of a new-expression, or delete-expression,
8225 respectively. If the next token is `::', then it might be one of
8226 these. */
8227 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8228 {
8229 enum rid keyword;
8230
8231 /* See if the token after the `::' is one of the keywords in
8232 which we're interested. */
8233 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8234 /* If it's `new', we have a new-expression. */
8235 if (keyword == RID_NEW)
8236 return cp_parser_new_expression (parser);
8237 /* Similarly, for `delete'. */
8238 else if (keyword == RID_DELETE)
8239 return cp_parser_delete_expression (parser);
8240 }
8241
8242 /* Look for a unary operator. */
8243 unary_operator = cp_parser_unary_operator (token);
8244 /* The `++' and `--' operators can be handled similarly, even though
8245 they are not technically unary-operators in the grammar. */
8246 if (unary_operator == ERROR_MARK)
8247 {
8248 if (token->type == CPP_PLUS_PLUS)
8249 unary_operator = PREINCREMENT_EXPR;
8250 else if (token->type == CPP_MINUS_MINUS)
8251 unary_operator = PREDECREMENT_EXPR;
8252 /* Handle the GNU address-of-label extension. */
8253 else if (cp_parser_allow_gnu_extensions_p (parser)
8254 && token->type == CPP_AND_AND)
8255 {
8256 tree identifier;
8257 tree expression;
8258 location_t start_loc = token->location;
8259
8260 /* Consume the '&&' token. */
8261 cp_lexer_consume_token (parser->lexer);
8262 /* Look for the identifier. */
8263 location_t finish_loc
8264 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8265 identifier = cp_parser_identifier (parser);
8266 /* Construct a location of the form:
8267 &&label
8268 ^~~~~~~
8269 with caret==start at the "&&", finish at the end of the label. */
8270 location_t combined_loc
8271 = make_location (start_loc, start_loc, finish_loc);
8272 /* Create an expression representing the address. */
8273 expression = finish_label_address_expr (identifier, combined_loc);
8274 if (cp_parser_non_integral_constant_expression (parser,
8275 NIC_ADDR_LABEL))
8276 expression = error_mark_node;
8277 return expression;
8278 }
8279 }
8280 if (unary_operator != ERROR_MARK)
8281 {
8282 cp_expr cast_expression;
8283 cp_expr expression = error_mark_node;
8284 non_integral_constant non_constant_p = NIC_NONE;
8285 location_t loc = token->location;
8286 tsubst_flags_t complain = complain_flags (decltype_p);
8287
8288 /* Consume the operator token. */
8289 token = cp_lexer_consume_token (parser->lexer);
8290 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8291
8292 /* Parse the cast-expression. */
8293 cast_expression
8294 = cp_parser_cast_expression (parser,
8295 unary_operator == ADDR_EXPR,
8296 /*cast_p=*/false,
8297 /*decltype*/false,
8298 pidk);
8299
8300 /* Make a location:
8301 OP_TOKEN CAST_EXPRESSION
8302 ^~~~~~~~~~~~~~~~~~~~~~~~~
8303 with start==caret at the operator token, and
8304 extending to the end of the cast_expression. */
8305 loc = make_location (loc, loc, cast_expression.get_finish ());
8306
8307 /* Now, build an appropriate representation. */
8308 switch (unary_operator)
8309 {
8310 case INDIRECT_REF:
8311 non_constant_p = NIC_STAR;
8312 expression = build_x_indirect_ref (loc, cast_expression,
8313 RO_UNARY_STAR,
8314 complain);
8315 /* TODO: build_x_indirect_ref does not always honor the
8316 location, so ensure it is set. */
8317 expression.set_location (loc);
8318 break;
8319
8320 case ADDR_EXPR:
8321 non_constant_p = NIC_ADDR;
8322 /* Fall through. */
8323 case BIT_NOT_EXPR:
8324 expression = build_x_unary_op (loc, unary_operator,
8325 cast_expression,
8326 complain);
8327 /* TODO: build_x_unary_op does not always honor the location,
8328 so ensure it is set. */
8329 expression.set_location (loc);
8330 break;
8331
8332 case PREINCREMENT_EXPR:
8333 case PREDECREMENT_EXPR:
8334 non_constant_p = unary_operator == PREINCREMENT_EXPR
8335 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8336 /* Fall through. */
8337 case NEGATE_EXPR:
8338 /* Immediately fold negation of a constant, unless the constant is 0
8339 (since -0 == 0) or it would overflow. */
8340 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8341 && CONSTANT_CLASS_P (cast_expression)
8342 && !integer_zerop (cast_expression)
8343 && !TREE_OVERFLOW (cast_expression))
8344 {
8345 tree folded = fold_build1 (unary_operator,
8346 TREE_TYPE (cast_expression),
8347 cast_expression);
8348 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8349 {
8350 expression = cp_expr (folded, loc);
8351 break;
8352 }
8353 }
8354 /* Fall through. */
8355 case UNARY_PLUS_EXPR:
8356 case TRUTH_NOT_EXPR:
8357 expression = finish_unary_op_expr (loc, unary_operator,
8358 cast_expression, complain);
8359 break;
8360
8361 default:
8362 gcc_unreachable ();
8363 }
8364
8365 if (non_constant_p != NIC_NONE
8366 && cp_parser_non_integral_constant_expression (parser,
8367 non_constant_p))
8368 expression = error_mark_node;
8369
8370 return expression;
8371 }
8372
8373 return cp_parser_postfix_expression (parser, address_p, cast_p,
8374 /*member_access_only_p=*/false,
8375 decltype_p,
8376 pidk);
8377 }
8378
8379 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8380 unary-operator, the corresponding tree code is returned. */
8381
8382 static enum tree_code
8383 cp_parser_unary_operator (cp_token* token)
8384 {
8385 switch (token->type)
8386 {
8387 case CPP_MULT:
8388 return INDIRECT_REF;
8389
8390 case CPP_AND:
8391 return ADDR_EXPR;
8392
8393 case CPP_PLUS:
8394 return UNARY_PLUS_EXPR;
8395
8396 case CPP_MINUS:
8397 return NEGATE_EXPR;
8398
8399 case CPP_NOT:
8400 return TRUTH_NOT_EXPR;
8401
8402 case CPP_COMPL:
8403 return BIT_NOT_EXPR;
8404
8405 default:
8406 return ERROR_MARK;
8407 }
8408 }
8409
8410 /* Parse a new-expression.
8411
8412 new-expression:
8413 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8414 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8415
8416 Returns a representation of the expression. */
8417
8418 static tree
8419 cp_parser_new_expression (cp_parser* parser)
8420 {
8421 bool global_scope_p;
8422 vec<tree, va_gc> *placement;
8423 tree type;
8424 vec<tree, va_gc> *initializer;
8425 tree nelts = NULL_TREE;
8426 tree ret;
8427
8428 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8429
8430 /* Look for the optional `::' operator. */
8431 global_scope_p
8432 = (cp_parser_global_scope_opt (parser,
8433 /*current_scope_valid_p=*/false)
8434 != NULL_TREE);
8435 /* Look for the `new' operator. */
8436 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8437 /* There's no easy way to tell a new-placement from the
8438 `( type-id )' construct. */
8439 cp_parser_parse_tentatively (parser);
8440 /* Look for a new-placement. */
8441 placement = cp_parser_new_placement (parser);
8442 /* If that didn't work out, there's no new-placement. */
8443 if (!cp_parser_parse_definitely (parser))
8444 {
8445 if (placement != NULL)
8446 release_tree_vector (placement);
8447 placement = NULL;
8448 }
8449
8450 /* If the next token is a `(', then we have a parenthesized
8451 type-id. */
8452 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8453 {
8454 cp_token *token;
8455 const char *saved_message = parser->type_definition_forbidden_message;
8456
8457 /* Consume the `('. */
8458 matching_parens parens;
8459 parens.consume_open (parser);
8460
8461 /* Parse the type-id. */
8462 parser->type_definition_forbidden_message
8463 = G_("types may not be defined in a new-expression");
8464 {
8465 type_id_in_expr_sentinel s (parser);
8466 type = cp_parser_type_id (parser);
8467 }
8468 parser->type_definition_forbidden_message = saved_message;
8469
8470 /* Look for the closing `)'. */
8471 parens.require_close (parser);
8472 token = cp_lexer_peek_token (parser->lexer);
8473 /* There should not be a direct-new-declarator in this production,
8474 but GCC used to allowed this, so we check and emit a sensible error
8475 message for this case. */
8476 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8477 {
8478 error_at (token->location,
8479 "array bound forbidden after parenthesized type-id");
8480 inform (token->location,
8481 "try removing the parentheses around the type-id");
8482 cp_parser_direct_new_declarator (parser);
8483 }
8484 }
8485 /* Otherwise, there must be a new-type-id. */
8486 else
8487 type = cp_parser_new_type_id (parser, &nelts);
8488
8489 /* If the next token is a `(' or '{', then we have a new-initializer. */
8490 cp_token *token = cp_lexer_peek_token (parser->lexer);
8491 if (token->type == CPP_OPEN_PAREN
8492 || token->type == CPP_OPEN_BRACE)
8493 initializer = cp_parser_new_initializer (parser);
8494 else
8495 initializer = NULL;
8496
8497 /* A new-expression may not appear in an integral constant
8498 expression. */
8499 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8500 ret = error_mark_node;
8501 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8502 of a new-type-id or type-id of a new-expression, the new-expression shall
8503 contain a new-initializer of the form ( assignment-expression )".
8504 Additionally, consistently with the spirit of DR 1467, we want to accept
8505 'new auto { 2 }' too. */
8506 else if ((ret = type_uses_auto (type))
8507 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8508 && (vec_safe_length (initializer) != 1
8509 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8510 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8511 {
8512 error_at (token->location,
8513 "initialization of new-expression for type %<auto%> "
8514 "requires exactly one element");
8515 ret = error_mark_node;
8516 }
8517 else
8518 {
8519 /* Construct a location e.g.:
8520 ptr = new int[100]
8521 ^~~~~~~~~~~~
8522 with caret == start at the start of the "new" token, and the end
8523 at the end of the final token we consumed. */
8524 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8525 location_t end_loc = get_finish (end_tok->location);
8526 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8527
8528 /* Create a representation of the new-expression. */
8529 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8530 tf_warning_or_error);
8531 protected_set_expr_location (ret, combined_loc);
8532 }
8533
8534 if (placement != NULL)
8535 release_tree_vector (placement);
8536 if (initializer != NULL)
8537 release_tree_vector (initializer);
8538
8539 return ret;
8540 }
8541
8542 /* Parse a new-placement.
8543
8544 new-placement:
8545 ( expression-list )
8546
8547 Returns the same representation as for an expression-list. */
8548
8549 static vec<tree, va_gc> *
8550 cp_parser_new_placement (cp_parser* parser)
8551 {
8552 vec<tree, va_gc> *expression_list;
8553
8554 /* Parse the expression-list. */
8555 expression_list = (cp_parser_parenthesized_expression_list
8556 (parser, non_attr, /*cast_p=*/false,
8557 /*allow_expansion_p=*/true,
8558 /*non_constant_p=*/NULL));
8559
8560 if (expression_list && expression_list->is_empty ())
8561 error ("expected expression-list or type-id");
8562
8563 return expression_list;
8564 }
8565
8566 /* Parse a new-type-id.
8567
8568 new-type-id:
8569 type-specifier-seq new-declarator [opt]
8570
8571 Returns the TYPE allocated. If the new-type-id indicates an array
8572 type, *NELTS is set to the number of elements in the last array
8573 bound; the TYPE will not include the last array bound. */
8574
8575 static tree
8576 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8577 {
8578 cp_decl_specifier_seq type_specifier_seq;
8579 cp_declarator *new_declarator;
8580 cp_declarator *declarator;
8581 cp_declarator *outer_declarator;
8582 const char *saved_message;
8583
8584 /* The type-specifier sequence must not contain type definitions.
8585 (It cannot contain declarations of new types either, but if they
8586 are not definitions we will catch that because they are not
8587 complete.) */
8588 saved_message = parser->type_definition_forbidden_message;
8589 parser->type_definition_forbidden_message
8590 = G_("types may not be defined in a new-type-id");
8591 /* Parse the type-specifier-seq. */
8592 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8593 /*is_trailing_return=*/false,
8594 &type_specifier_seq);
8595 /* Restore the old message. */
8596 parser->type_definition_forbidden_message = saved_message;
8597
8598 if (type_specifier_seq.type == error_mark_node)
8599 return error_mark_node;
8600
8601 /* Parse the new-declarator. */
8602 new_declarator = cp_parser_new_declarator_opt (parser);
8603
8604 /* Determine the number of elements in the last array dimension, if
8605 any. */
8606 *nelts = NULL_TREE;
8607 /* Skip down to the last array dimension. */
8608 declarator = new_declarator;
8609 outer_declarator = NULL;
8610 while (declarator && (declarator->kind == cdk_pointer
8611 || declarator->kind == cdk_ptrmem))
8612 {
8613 outer_declarator = declarator;
8614 declarator = declarator->declarator;
8615 }
8616 while (declarator
8617 && declarator->kind == cdk_array
8618 && declarator->declarator
8619 && declarator->declarator->kind == cdk_array)
8620 {
8621 outer_declarator = declarator;
8622 declarator = declarator->declarator;
8623 }
8624
8625 if (declarator && declarator->kind == cdk_array)
8626 {
8627 *nelts = declarator->u.array.bounds;
8628 if (*nelts == error_mark_node)
8629 *nelts = integer_one_node;
8630
8631 if (outer_declarator)
8632 outer_declarator->declarator = declarator->declarator;
8633 else
8634 new_declarator = NULL;
8635 }
8636
8637 return groktypename (&type_specifier_seq, new_declarator, false);
8638 }
8639
8640 /* Parse an (optional) new-declarator.
8641
8642 new-declarator:
8643 ptr-operator new-declarator [opt]
8644 direct-new-declarator
8645
8646 Returns the declarator. */
8647
8648 static cp_declarator *
8649 cp_parser_new_declarator_opt (cp_parser* parser)
8650 {
8651 enum tree_code code;
8652 tree type, std_attributes = NULL_TREE;
8653 cp_cv_quals cv_quals;
8654
8655 /* We don't know if there's a ptr-operator next, or not. */
8656 cp_parser_parse_tentatively (parser);
8657 /* Look for a ptr-operator. */
8658 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8659 /* If that worked, look for more new-declarators. */
8660 if (cp_parser_parse_definitely (parser))
8661 {
8662 cp_declarator *declarator;
8663
8664 /* Parse another optional declarator. */
8665 declarator = cp_parser_new_declarator_opt (parser);
8666
8667 declarator = cp_parser_make_indirect_declarator
8668 (code, type, cv_quals, declarator, std_attributes);
8669
8670 return declarator;
8671 }
8672
8673 /* If the next token is a `[', there is a direct-new-declarator. */
8674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8675 return cp_parser_direct_new_declarator (parser);
8676
8677 return NULL;
8678 }
8679
8680 /* Parse a direct-new-declarator.
8681
8682 direct-new-declarator:
8683 [ expression ]
8684 direct-new-declarator [constant-expression]
8685
8686 */
8687
8688 static cp_declarator *
8689 cp_parser_direct_new_declarator (cp_parser* parser)
8690 {
8691 cp_declarator *declarator = NULL;
8692
8693 while (true)
8694 {
8695 tree expression;
8696 cp_token *token;
8697
8698 /* Look for the opening `['. */
8699 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8700
8701 token = cp_lexer_peek_token (parser->lexer);
8702 expression = cp_parser_expression (parser);
8703 /* The standard requires that the expression have integral
8704 type. DR 74 adds enumeration types. We believe that the
8705 real intent is that these expressions be handled like the
8706 expression in a `switch' condition, which also allows
8707 classes with a single conversion to integral or
8708 enumeration type. */
8709 if (!processing_template_decl)
8710 {
8711 expression
8712 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8713 expression,
8714 /*complain=*/true);
8715 if (!expression)
8716 {
8717 error_at (token->location,
8718 "expression in new-declarator must have integral "
8719 "or enumeration type");
8720 expression = error_mark_node;
8721 }
8722 }
8723
8724 /* Look for the closing `]'. */
8725 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8726
8727 /* Add this bound to the declarator. */
8728 declarator = make_array_declarator (declarator, expression);
8729
8730 /* If the next token is not a `[', then there are no more
8731 bounds. */
8732 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8733 break;
8734 }
8735
8736 return declarator;
8737 }
8738
8739 /* Parse a new-initializer.
8740
8741 new-initializer:
8742 ( expression-list [opt] )
8743 braced-init-list
8744
8745 Returns a representation of the expression-list. */
8746
8747 static vec<tree, va_gc> *
8748 cp_parser_new_initializer (cp_parser* parser)
8749 {
8750 vec<tree, va_gc> *expression_list;
8751
8752 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8753 {
8754 tree t;
8755 bool expr_non_constant_p;
8756 cp_lexer_set_source_position (parser->lexer);
8757 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8758 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8759 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8760 expression_list = make_tree_vector_single (t);
8761 }
8762 else
8763 expression_list = (cp_parser_parenthesized_expression_list
8764 (parser, non_attr, /*cast_p=*/false,
8765 /*allow_expansion_p=*/true,
8766 /*non_constant_p=*/NULL));
8767
8768 return expression_list;
8769 }
8770
8771 /* Parse a delete-expression.
8772
8773 delete-expression:
8774 :: [opt] delete cast-expression
8775 :: [opt] delete [ ] cast-expression
8776
8777 Returns a representation of the expression. */
8778
8779 static tree
8780 cp_parser_delete_expression (cp_parser* parser)
8781 {
8782 bool global_scope_p;
8783 bool array_p;
8784 tree expression;
8785
8786 /* Look for the optional `::' operator. */
8787 global_scope_p
8788 = (cp_parser_global_scope_opt (parser,
8789 /*current_scope_valid_p=*/false)
8790 != NULL_TREE);
8791 /* Look for the `delete' keyword. */
8792 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8793 /* See if the array syntax is in use. */
8794 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8795 {
8796 /* Consume the `[' token. */
8797 cp_lexer_consume_token (parser->lexer);
8798 /* Look for the `]' token. */
8799 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8800 /* Remember that this is the `[]' construct. */
8801 array_p = true;
8802 }
8803 else
8804 array_p = false;
8805
8806 /* Parse the cast-expression. */
8807 expression = cp_parser_simple_cast_expression (parser);
8808
8809 /* A delete-expression may not appear in an integral constant
8810 expression. */
8811 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8812 return error_mark_node;
8813
8814 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8815 tf_warning_or_error);
8816 }
8817
8818 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8819 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8820 0 otherwise. */
8821
8822 static int
8823 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8824 {
8825 cp_token *token = cp_lexer_peek_token (parser->lexer);
8826 switch (token->type)
8827 {
8828 case CPP_COMMA:
8829 case CPP_SEMICOLON:
8830 case CPP_QUERY:
8831 case CPP_COLON:
8832 case CPP_CLOSE_SQUARE:
8833 case CPP_CLOSE_PAREN:
8834 case CPP_CLOSE_BRACE:
8835 case CPP_OPEN_BRACE:
8836 case CPP_DOT:
8837 case CPP_DOT_STAR:
8838 case CPP_DEREF:
8839 case CPP_DEREF_STAR:
8840 case CPP_DIV:
8841 case CPP_MOD:
8842 case CPP_LSHIFT:
8843 case CPP_RSHIFT:
8844 case CPP_LESS:
8845 case CPP_GREATER:
8846 case CPP_LESS_EQ:
8847 case CPP_GREATER_EQ:
8848 case CPP_EQ_EQ:
8849 case CPP_NOT_EQ:
8850 case CPP_EQ:
8851 case CPP_MULT_EQ:
8852 case CPP_DIV_EQ:
8853 case CPP_MOD_EQ:
8854 case CPP_PLUS_EQ:
8855 case CPP_MINUS_EQ:
8856 case CPP_RSHIFT_EQ:
8857 case CPP_LSHIFT_EQ:
8858 case CPP_AND_EQ:
8859 case CPP_XOR_EQ:
8860 case CPP_OR_EQ:
8861 case CPP_XOR:
8862 case CPP_OR:
8863 case CPP_OR_OR:
8864 case CPP_EOF:
8865 case CPP_ELLIPSIS:
8866 return 0;
8867
8868 case CPP_OPEN_PAREN:
8869 /* In ((type ()) () the last () isn't a valid cast-expression,
8870 so the whole must be parsed as postfix-expression. */
8871 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8872 != CPP_CLOSE_PAREN;
8873
8874 case CPP_OPEN_SQUARE:
8875 /* '[' may start a primary-expression in obj-c++ and in C++11,
8876 as a lambda-expression, eg, '(void)[]{}'. */
8877 if (cxx_dialect >= cxx11)
8878 return -1;
8879 return c_dialect_objc ();
8880
8881 case CPP_PLUS_PLUS:
8882 case CPP_MINUS_MINUS:
8883 /* '++' and '--' may or may not start a cast-expression:
8884
8885 struct T { void operator++(int); };
8886 void f() { (T())++; }
8887
8888 vs
8889
8890 int a;
8891 (int)++a; */
8892 return -1;
8893
8894 default:
8895 return 1;
8896 }
8897 }
8898
8899 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8900 in the order: const_cast, static_cast, reinterpret_cast.
8901
8902 Don't suggest dynamic_cast.
8903
8904 Return the first legal cast kind found, or NULL otherwise. */
8905
8906 static const char *
8907 get_cast_suggestion (tree dst_type, tree orig_expr)
8908 {
8909 tree trial;
8910
8911 /* Reuse the parser logic by attempting to build the various kinds of
8912 cast, with "complain" disabled.
8913 Identify the first such cast that is valid. */
8914
8915 /* Don't attempt to run such logic within template processing. */
8916 if (processing_template_decl)
8917 return NULL;
8918
8919 /* First try const_cast. */
8920 trial = build_const_cast (dst_type, orig_expr, tf_none);
8921 if (trial != error_mark_node)
8922 return "const_cast";
8923
8924 /* If that fails, try static_cast. */
8925 trial = build_static_cast (dst_type, orig_expr, tf_none);
8926 if (trial != error_mark_node)
8927 return "static_cast";
8928
8929 /* Finally, try reinterpret_cast. */
8930 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8931 if (trial != error_mark_node)
8932 return "reinterpret_cast";
8933
8934 /* No such cast possible. */
8935 return NULL;
8936 }
8937
8938 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8939 suggesting how to convert a C-style cast of the form:
8940
8941 (DST_TYPE)ORIG_EXPR
8942
8943 to a C++-style cast.
8944
8945 The primary range of RICHLOC is asssumed to be that of the original
8946 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8947 of the parens in the C-style cast. */
8948
8949 static void
8950 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8951 location_t close_paren_loc, tree orig_expr,
8952 tree dst_type)
8953 {
8954 /* This function is non-trivial, so bail out now if the warning isn't
8955 going to be emitted. */
8956 if (!warn_old_style_cast)
8957 return;
8958
8959 /* Try to find a legal C++ cast, trying them in order:
8960 const_cast, static_cast, reinterpret_cast. */
8961 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8962 if (!cast_suggestion)
8963 return;
8964
8965 /* Replace the open paren with "CAST_SUGGESTION<". */
8966 pretty_printer pp;
8967 pp_printf (&pp, "%s<", cast_suggestion);
8968 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8969
8970 /* Replace the close paren with "> (". */
8971 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8972
8973 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8974 rich_loc->add_fixit_insert_after (")");
8975 }
8976
8977
8978 /* Parse a cast-expression.
8979
8980 cast-expression:
8981 unary-expression
8982 ( type-id ) cast-expression
8983
8984 ADDRESS_P is true iff the unary-expression is appearing as the
8985 operand of the `&' operator. CAST_P is true if this expression is
8986 the target of a cast.
8987
8988 Returns a representation of the expression. */
8989
8990 static cp_expr
8991 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8992 bool decltype_p, cp_id_kind * pidk)
8993 {
8994 /* If it's a `(', then we might be looking at a cast. */
8995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8996 {
8997 tree type = NULL_TREE;
8998 cp_expr expr (NULL_TREE);
8999 int cast_expression = 0;
9000 const char *saved_message;
9001
9002 /* There's no way to know yet whether or not this is a cast.
9003 For example, `(int (3))' is a unary-expression, while `(int)
9004 3' is a cast. So, we resort to parsing tentatively. */
9005 cp_parser_parse_tentatively (parser);
9006 /* Types may not be defined in a cast. */
9007 saved_message = parser->type_definition_forbidden_message;
9008 parser->type_definition_forbidden_message
9009 = G_("types may not be defined in casts");
9010 /* Consume the `('. */
9011 matching_parens parens;
9012 cp_token *open_paren = parens.consume_open (parser);
9013 location_t open_paren_loc = open_paren->location;
9014 location_t close_paren_loc = UNKNOWN_LOCATION;
9015
9016 /* A very tricky bit is that `(struct S) { 3 }' is a
9017 compound-literal (which we permit in C++ as an extension).
9018 But, that construct is not a cast-expression -- it is a
9019 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9020 is legal; if the compound-literal were a cast-expression,
9021 you'd need an extra set of parentheses.) But, if we parse
9022 the type-id, and it happens to be a class-specifier, then we
9023 will commit to the parse at that point, because we cannot
9024 undo the action that is done when creating a new class. So,
9025 then we cannot back up and do a postfix-expression.
9026
9027 Another tricky case is the following (c++/29234):
9028
9029 struct S { void operator () (); };
9030
9031 void foo ()
9032 {
9033 ( S()() );
9034 }
9035
9036 As a type-id we parse the parenthesized S()() as a function
9037 returning a function, groktypename complains and we cannot
9038 back up in this case either.
9039
9040 Therefore, we scan ahead to the closing `)', and check to see
9041 if the tokens after the `)' can start a cast-expression. Otherwise
9042 we are dealing with an unary-expression, a postfix-expression
9043 or something else.
9044
9045 Yet another tricky case, in C++11, is the following (c++/54891):
9046
9047 (void)[]{};
9048
9049 The issue is that usually, besides the case of lambda-expressions,
9050 the parenthesized type-id cannot be followed by '[', and, eg, we
9051 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9052 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9053 we don't commit, we try a cast-expression, then an unary-expression.
9054
9055 Save tokens so that we can put them back. */
9056 cp_lexer_save_tokens (parser->lexer);
9057
9058 /* We may be looking at a cast-expression. */
9059 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9060 /*consume_paren=*/true))
9061 cast_expression
9062 = cp_parser_tokens_start_cast_expression (parser);
9063
9064 /* Roll back the tokens we skipped. */
9065 cp_lexer_rollback_tokens (parser->lexer);
9066 /* If we aren't looking at a cast-expression, simulate an error so
9067 that the call to cp_parser_error_occurred below returns true. */
9068 if (!cast_expression)
9069 cp_parser_simulate_error (parser);
9070 else
9071 {
9072 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9073 parser->in_type_id_in_expr_p = true;
9074 /* Look for the type-id. */
9075 type = cp_parser_type_id (parser);
9076 /* Look for the closing `)'. */
9077 cp_token *close_paren = parens.require_close (parser);
9078 if (close_paren)
9079 close_paren_loc = close_paren->location;
9080 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9081 }
9082
9083 /* Restore the saved message. */
9084 parser->type_definition_forbidden_message = saved_message;
9085
9086 /* At this point this can only be either a cast or a
9087 parenthesized ctor such as `(T ())' that looks like a cast to
9088 function returning T. */
9089 if (!cp_parser_error_occurred (parser))
9090 {
9091 /* Only commit if the cast-expression doesn't start with
9092 '++', '--', or '[' in C++11. */
9093 if (cast_expression > 0)
9094 cp_parser_commit_to_topmost_tentative_parse (parser);
9095
9096 expr = cp_parser_cast_expression (parser,
9097 /*address_p=*/false,
9098 /*cast_p=*/true,
9099 /*decltype_p=*/false,
9100 pidk);
9101
9102 if (cp_parser_parse_definitely (parser))
9103 {
9104 /* Warn about old-style casts, if so requested. */
9105 if (warn_old_style_cast
9106 && !in_system_header_at (input_location)
9107 && !VOID_TYPE_P (type)
9108 && current_lang_name != lang_name_c)
9109 {
9110 gcc_rich_location rich_loc (input_location);
9111 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9112 expr, type);
9113 warning_at (&rich_loc, OPT_Wold_style_cast,
9114 "use of old-style cast to %q#T", type);
9115 }
9116
9117 /* Only type conversions to integral or enumeration types
9118 can be used in constant-expressions. */
9119 if (!cast_valid_in_integral_constant_expression_p (type)
9120 && cp_parser_non_integral_constant_expression (parser,
9121 NIC_CAST))
9122 return error_mark_node;
9123
9124 /* Perform the cast. */
9125 /* Make a location:
9126 (TYPE) EXPR
9127 ^~~~~~~~~~~
9128 with start==caret at the open paren, extending to the
9129 end of "expr". */
9130 location_t cast_loc = make_location (open_paren_loc,
9131 open_paren_loc,
9132 expr.get_finish ());
9133 expr = build_c_cast (cast_loc, type, expr);
9134 return expr;
9135 }
9136 }
9137 else
9138 cp_parser_abort_tentative_parse (parser);
9139 }
9140
9141 /* If we get here, then it's not a cast, so it must be a
9142 unary-expression. */
9143 return cp_parser_unary_expression (parser, pidk, address_p,
9144 cast_p, decltype_p);
9145 }
9146
9147 /* Parse a binary expression of the general form:
9148
9149 pm-expression:
9150 cast-expression
9151 pm-expression .* cast-expression
9152 pm-expression ->* cast-expression
9153
9154 multiplicative-expression:
9155 pm-expression
9156 multiplicative-expression * pm-expression
9157 multiplicative-expression / pm-expression
9158 multiplicative-expression % pm-expression
9159
9160 additive-expression:
9161 multiplicative-expression
9162 additive-expression + multiplicative-expression
9163 additive-expression - multiplicative-expression
9164
9165 shift-expression:
9166 additive-expression
9167 shift-expression << additive-expression
9168 shift-expression >> additive-expression
9169
9170 relational-expression:
9171 shift-expression
9172 relational-expression < shift-expression
9173 relational-expression > shift-expression
9174 relational-expression <= shift-expression
9175 relational-expression >= shift-expression
9176
9177 GNU Extension:
9178
9179 relational-expression:
9180 relational-expression <? shift-expression
9181 relational-expression >? shift-expression
9182
9183 equality-expression:
9184 relational-expression
9185 equality-expression == relational-expression
9186 equality-expression != relational-expression
9187
9188 and-expression:
9189 equality-expression
9190 and-expression & equality-expression
9191
9192 exclusive-or-expression:
9193 and-expression
9194 exclusive-or-expression ^ and-expression
9195
9196 inclusive-or-expression:
9197 exclusive-or-expression
9198 inclusive-or-expression | exclusive-or-expression
9199
9200 logical-and-expression:
9201 inclusive-or-expression
9202 logical-and-expression && inclusive-or-expression
9203
9204 logical-or-expression:
9205 logical-and-expression
9206 logical-or-expression || logical-and-expression
9207
9208 All these are implemented with a single function like:
9209
9210 binary-expression:
9211 simple-cast-expression
9212 binary-expression <token> binary-expression
9213
9214 CAST_P is true if this expression is the target of a cast.
9215
9216 The binops_by_token map is used to get the tree codes for each <token> type.
9217 binary-expressions are associated according to a precedence table. */
9218
9219 #define TOKEN_PRECEDENCE(token) \
9220 (((token->type == CPP_GREATER \
9221 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9222 && !parser->greater_than_is_operator_p) \
9223 ? PREC_NOT_OPERATOR \
9224 : binops_by_token[token->type].prec)
9225
9226 static cp_expr
9227 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9228 bool no_toplevel_fold_p,
9229 bool decltype_p,
9230 enum cp_parser_prec prec,
9231 cp_id_kind * pidk)
9232 {
9233 cp_parser_expression_stack stack;
9234 cp_parser_expression_stack_entry *sp = &stack[0];
9235 cp_parser_expression_stack_entry current;
9236 cp_expr rhs;
9237 cp_token *token;
9238 enum tree_code rhs_type;
9239 enum cp_parser_prec new_prec, lookahead_prec;
9240 tree overload;
9241
9242 /* Parse the first expression. */
9243 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9244 ? TRUTH_NOT_EXPR : ERROR_MARK);
9245 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9246 cast_p, decltype_p, pidk);
9247 current.prec = prec;
9248
9249 if (cp_parser_error_occurred (parser))
9250 return error_mark_node;
9251
9252 for (;;)
9253 {
9254 /* Get an operator token. */
9255 token = cp_lexer_peek_token (parser->lexer);
9256
9257 if (warn_cxx11_compat
9258 && token->type == CPP_RSHIFT
9259 && !parser->greater_than_is_operator_p)
9260 {
9261 if (warning_at (token->location, OPT_Wc__11_compat,
9262 "%<>>%> operator is treated"
9263 " as two right angle brackets in C++11"))
9264 inform (token->location,
9265 "suggest parentheses around %<>>%> expression");
9266 }
9267
9268 new_prec = TOKEN_PRECEDENCE (token);
9269 if (new_prec != PREC_NOT_OPERATOR
9270 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9271 /* This is a fold-expression; handle it later. */
9272 new_prec = PREC_NOT_OPERATOR;
9273
9274 /* Popping an entry off the stack means we completed a subexpression:
9275 - either we found a token which is not an operator (`>' where it is not
9276 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9277 will happen repeatedly;
9278 - or, we found an operator which has lower priority. This is the case
9279 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9280 parsing `3 * 4'. */
9281 if (new_prec <= current.prec)
9282 {
9283 if (sp == stack)
9284 break;
9285 else
9286 goto pop;
9287 }
9288
9289 get_rhs:
9290 current.tree_type = binops_by_token[token->type].tree_type;
9291 current.loc = token->location;
9292
9293 /* We used the operator token. */
9294 cp_lexer_consume_token (parser->lexer);
9295
9296 /* For "false && x" or "true || x", x will never be executed;
9297 disable warnings while evaluating it. */
9298 if (current.tree_type == TRUTH_ANDIF_EXPR)
9299 c_inhibit_evaluation_warnings +=
9300 cp_fully_fold (current.lhs) == truthvalue_false_node;
9301 else if (current.tree_type == TRUTH_ORIF_EXPR)
9302 c_inhibit_evaluation_warnings +=
9303 cp_fully_fold (current.lhs) == truthvalue_true_node;
9304
9305 /* Extract another operand. It may be the RHS of this expression
9306 or the LHS of a new, higher priority expression. */
9307 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9308 ? TRUTH_NOT_EXPR : ERROR_MARK);
9309 rhs = cp_parser_simple_cast_expression (parser);
9310
9311 /* Get another operator token. Look up its precedence to avoid
9312 building a useless (immediately popped) stack entry for common
9313 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9314 token = cp_lexer_peek_token (parser->lexer);
9315 lookahead_prec = TOKEN_PRECEDENCE (token);
9316 if (lookahead_prec != PREC_NOT_OPERATOR
9317 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9318 lookahead_prec = PREC_NOT_OPERATOR;
9319 if (lookahead_prec > new_prec)
9320 {
9321 /* ... and prepare to parse the RHS of the new, higher priority
9322 expression. Since precedence levels on the stack are
9323 monotonically increasing, we do not have to care about
9324 stack overflows. */
9325 *sp = current;
9326 ++sp;
9327 current.lhs = rhs;
9328 current.lhs_type = rhs_type;
9329 current.prec = new_prec;
9330 new_prec = lookahead_prec;
9331 goto get_rhs;
9332
9333 pop:
9334 lookahead_prec = new_prec;
9335 /* If the stack is not empty, we have parsed into LHS the right side
9336 (`4' in the example above) of an expression we had suspended.
9337 We can use the information on the stack to recover the LHS (`3')
9338 from the stack together with the tree code (`MULT_EXPR'), and
9339 the precedence of the higher level subexpression
9340 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9341 which will be used to actually build the additive expression. */
9342 rhs = current.lhs;
9343 rhs_type = current.lhs_type;
9344 --sp;
9345 current = *sp;
9346 }
9347
9348 /* Undo the disabling of warnings done above. */
9349 if (current.tree_type == TRUTH_ANDIF_EXPR)
9350 c_inhibit_evaluation_warnings -=
9351 cp_fully_fold (current.lhs) == truthvalue_false_node;
9352 else if (current.tree_type == TRUTH_ORIF_EXPR)
9353 c_inhibit_evaluation_warnings -=
9354 cp_fully_fold (current.lhs) == truthvalue_true_node;
9355
9356 if (warn_logical_not_paren
9357 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9358 && current.lhs_type == TRUTH_NOT_EXPR
9359 /* Avoid warning for !!x == y. */
9360 && (TREE_CODE (current.lhs) != NE_EXPR
9361 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9362 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9363 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9364 /* Avoid warning for !b == y where b is boolean. */
9365 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9366 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9367 != BOOLEAN_TYPE))))
9368 /* Avoid warning for !!b == y where b is boolean. */
9369 && (!DECL_P (current.lhs)
9370 || TREE_TYPE (current.lhs) == NULL_TREE
9371 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9372 warn_logical_not_parentheses (current.loc, current.tree_type,
9373 current.lhs, maybe_constant_value (rhs));
9374
9375 overload = NULL;
9376
9377 location_t combined_loc = make_location (current.loc,
9378 current.lhs.get_start (),
9379 rhs.get_finish ());
9380
9381 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9382 ERROR_MARK for everything that is not a binary expression.
9383 This makes warn_about_parentheses miss some warnings that
9384 involve unary operators. For unary expressions we should
9385 pass the correct tree_code unless the unary expression was
9386 surrounded by parentheses.
9387 */
9388 if (no_toplevel_fold_p
9389 && lookahead_prec <= current.prec
9390 && sp == stack)
9391 {
9392 if (current.lhs == error_mark_node || rhs == error_mark_node)
9393 current.lhs = error_mark_node;
9394 else
9395 {
9396 current.lhs
9397 = build_min (current.tree_type,
9398 TREE_CODE_CLASS (current.tree_type)
9399 == tcc_comparison
9400 ? boolean_type_node : TREE_TYPE (current.lhs),
9401 current.lhs.get_value (), rhs.get_value ());
9402 SET_EXPR_LOCATION (current.lhs, combined_loc);
9403 }
9404 }
9405 else
9406 {
9407 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9408 current.lhs, current.lhs_type,
9409 rhs, rhs_type, &overload,
9410 complain_flags (decltype_p));
9411 /* TODO: build_x_binary_op doesn't always honor the location. */
9412 current.lhs.set_location (combined_loc);
9413 }
9414 current.lhs_type = current.tree_type;
9415
9416 /* If the binary operator required the use of an overloaded operator,
9417 then this expression cannot be an integral constant-expression.
9418 An overloaded operator can be used even if both operands are
9419 otherwise permissible in an integral constant-expression if at
9420 least one of the operands is of enumeration type. */
9421
9422 if (overload
9423 && cp_parser_non_integral_constant_expression (parser,
9424 NIC_OVERLOADED))
9425 return error_mark_node;
9426 }
9427
9428 return current.lhs;
9429 }
9430
9431 static cp_expr
9432 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9433 bool no_toplevel_fold_p,
9434 enum cp_parser_prec prec,
9435 cp_id_kind * pidk)
9436 {
9437 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9438 /*decltype*/false, prec, pidk);
9439 }
9440
9441 /* Parse the `? expression : assignment-expression' part of a
9442 conditional-expression. The LOGICAL_OR_EXPR is the
9443 logical-or-expression that started the conditional-expression.
9444 Returns a representation of the entire conditional-expression.
9445
9446 This routine is used by cp_parser_assignment_expression.
9447
9448 ? expression : assignment-expression
9449
9450 GNU Extensions:
9451
9452 ? : assignment-expression */
9453
9454 static tree
9455 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9456 {
9457 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9458 cp_expr assignment_expr;
9459 struct cp_token *token;
9460 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9461
9462 /* Consume the `?' token. */
9463 cp_lexer_consume_token (parser->lexer);
9464 token = cp_lexer_peek_token (parser->lexer);
9465 if (cp_parser_allow_gnu_extensions_p (parser)
9466 && token->type == CPP_COLON)
9467 {
9468 pedwarn (token->location, OPT_Wpedantic,
9469 "ISO C++ does not allow ?: with omitted middle operand");
9470 /* Implicit true clause. */
9471 expr = NULL_TREE;
9472 c_inhibit_evaluation_warnings +=
9473 folded_logical_or_expr == truthvalue_true_node;
9474 warn_for_omitted_condop (token->location, logical_or_expr);
9475 }
9476 else
9477 {
9478 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9479 parser->colon_corrects_to_scope_p = false;
9480 /* Parse the expression. */
9481 c_inhibit_evaluation_warnings +=
9482 folded_logical_or_expr == truthvalue_false_node;
9483 expr = cp_parser_expression (parser);
9484 c_inhibit_evaluation_warnings +=
9485 ((folded_logical_or_expr == truthvalue_true_node)
9486 - (folded_logical_or_expr == truthvalue_false_node));
9487 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9488 }
9489
9490 /* The next token should be a `:'. */
9491 cp_parser_require (parser, CPP_COLON, RT_COLON);
9492 /* Parse the assignment-expression. */
9493 assignment_expr = cp_parser_assignment_expression (parser);
9494 c_inhibit_evaluation_warnings -=
9495 folded_logical_or_expr == truthvalue_true_node;
9496
9497 /* Make a location:
9498 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9499 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9500 with the caret at the "?", ranging from the start of
9501 the logical_or_expr to the end of the assignment_expr. */
9502 loc = make_location (loc,
9503 logical_or_expr.get_start (),
9504 assignment_expr.get_finish ());
9505
9506 /* Build the conditional-expression. */
9507 return build_x_conditional_expr (loc, logical_or_expr,
9508 expr,
9509 assignment_expr,
9510 tf_warning_or_error);
9511 }
9512
9513 /* Parse an assignment-expression.
9514
9515 assignment-expression:
9516 conditional-expression
9517 logical-or-expression assignment-operator assignment_expression
9518 throw-expression
9519
9520 CAST_P is true if this expression is the target of a cast.
9521 DECLTYPE_P is true if this expression is the operand of decltype.
9522
9523 Returns a representation for the expression. */
9524
9525 static cp_expr
9526 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9527 bool cast_p, bool decltype_p)
9528 {
9529 cp_expr expr;
9530
9531 /* If the next token is the `throw' keyword, then we're looking at
9532 a throw-expression. */
9533 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9534 expr = cp_parser_throw_expression (parser);
9535 /* Otherwise, it must be that we are looking at a
9536 logical-or-expression. */
9537 else
9538 {
9539 /* Parse the binary expressions (logical-or-expression). */
9540 expr = cp_parser_binary_expression (parser, cast_p, false,
9541 decltype_p,
9542 PREC_NOT_OPERATOR, pidk);
9543 /* If the next token is a `?' then we're actually looking at a
9544 conditional-expression. */
9545 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9546 return cp_parser_question_colon_clause (parser, expr);
9547 else
9548 {
9549 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9550
9551 /* If it's an assignment-operator, we're using the second
9552 production. */
9553 enum tree_code assignment_operator
9554 = cp_parser_assignment_operator_opt (parser);
9555 if (assignment_operator != ERROR_MARK)
9556 {
9557 bool non_constant_p;
9558
9559 /* Parse the right-hand side of the assignment. */
9560 cp_expr rhs = cp_parser_initializer_clause (parser,
9561 &non_constant_p);
9562
9563 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9564 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9565
9566 /* An assignment may not appear in a
9567 constant-expression. */
9568 if (cp_parser_non_integral_constant_expression (parser,
9569 NIC_ASSIGNMENT))
9570 return error_mark_node;
9571 /* Build the assignment expression. Its default
9572 location:
9573 LHS = RHS
9574 ~~~~^~~~~
9575 is the location of the '=' token as the
9576 caret, ranging from the start of the lhs to the
9577 end of the rhs. */
9578 loc = make_location (loc,
9579 expr.get_start (),
9580 rhs.get_finish ());
9581 expr = build_x_modify_expr (loc, expr,
9582 assignment_operator,
9583 rhs,
9584 complain_flags (decltype_p));
9585 /* TODO: build_x_modify_expr doesn't honor the location,
9586 so we must set it here. */
9587 expr.set_location (loc);
9588 }
9589 }
9590 }
9591
9592 return expr;
9593 }
9594
9595 /* Parse an (optional) assignment-operator.
9596
9597 assignment-operator: one of
9598 = *= /= %= += -= >>= <<= &= ^= |=
9599
9600 GNU Extension:
9601
9602 assignment-operator: one of
9603 <?= >?=
9604
9605 If the next token is an assignment operator, the corresponding tree
9606 code is returned, and the token is consumed. For example, for
9607 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9608 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9609 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9610 operator, ERROR_MARK is returned. */
9611
9612 static enum tree_code
9613 cp_parser_assignment_operator_opt (cp_parser* parser)
9614 {
9615 enum tree_code op;
9616 cp_token *token;
9617
9618 /* Peek at the next token. */
9619 token = cp_lexer_peek_token (parser->lexer);
9620
9621 switch (token->type)
9622 {
9623 case CPP_EQ:
9624 op = NOP_EXPR;
9625 break;
9626
9627 case CPP_MULT_EQ:
9628 op = MULT_EXPR;
9629 break;
9630
9631 case CPP_DIV_EQ:
9632 op = TRUNC_DIV_EXPR;
9633 break;
9634
9635 case CPP_MOD_EQ:
9636 op = TRUNC_MOD_EXPR;
9637 break;
9638
9639 case CPP_PLUS_EQ:
9640 op = PLUS_EXPR;
9641 break;
9642
9643 case CPP_MINUS_EQ:
9644 op = MINUS_EXPR;
9645 break;
9646
9647 case CPP_RSHIFT_EQ:
9648 op = RSHIFT_EXPR;
9649 break;
9650
9651 case CPP_LSHIFT_EQ:
9652 op = LSHIFT_EXPR;
9653 break;
9654
9655 case CPP_AND_EQ:
9656 op = BIT_AND_EXPR;
9657 break;
9658
9659 case CPP_XOR_EQ:
9660 op = BIT_XOR_EXPR;
9661 break;
9662
9663 case CPP_OR_EQ:
9664 op = BIT_IOR_EXPR;
9665 break;
9666
9667 default:
9668 /* Nothing else is an assignment operator. */
9669 op = ERROR_MARK;
9670 }
9671
9672 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9673 if (op != ERROR_MARK
9674 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9675 op = ERROR_MARK;
9676
9677 /* If it was an assignment operator, consume it. */
9678 if (op != ERROR_MARK)
9679 cp_lexer_consume_token (parser->lexer);
9680
9681 return op;
9682 }
9683
9684 /* Parse an expression.
9685
9686 expression:
9687 assignment-expression
9688 expression , assignment-expression
9689
9690 CAST_P is true if this expression is the target of a cast.
9691 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9692 except possibly parenthesized or on the RHS of a comma (N3276).
9693
9694 Returns a representation of the expression. */
9695
9696 static cp_expr
9697 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9698 bool cast_p, bool decltype_p)
9699 {
9700 cp_expr expression = NULL_TREE;
9701 location_t loc = UNKNOWN_LOCATION;
9702
9703 while (true)
9704 {
9705 cp_expr assignment_expression;
9706
9707 /* Parse the next assignment-expression. */
9708 assignment_expression
9709 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9710
9711 /* We don't create a temporary for a call that is the immediate operand
9712 of decltype or on the RHS of a comma. But when we see a comma, we
9713 need to create a temporary for a call on the LHS. */
9714 if (decltype_p && !processing_template_decl
9715 && TREE_CODE (assignment_expression) == CALL_EXPR
9716 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9717 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9718 assignment_expression
9719 = build_cplus_new (TREE_TYPE (assignment_expression),
9720 assignment_expression, tf_warning_or_error);
9721
9722 /* If this is the first assignment-expression, we can just
9723 save it away. */
9724 if (!expression)
9725 expression = assignment_expression;
9726 else
9727 {
9728 /* Create a location with caret at the comma, ranging
9729 from the start of the LHS to the end of the RHS. */
9730 loc = make_location (loc,
9731 expression.get_start (),
9732 assignment_expression.get_finish ());
9733 expression = build_x_compound_expr (loc, expression,
9734 assignment_expression,
9735 complain_flags (decltype_p));
9736 expression.set_location (loc);
9737 }
9738 /* If the next token is not a comma, or we're in a fold-expression, then
9739 we are done with the expression. */
9740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9741 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9742 break;
9743 /* Consume the `,'. */
9744 loc = cp_lexer_peek_token (parser->lexer)->location;
9745 cp_lexer_consume_token (parser->lexer);
9746 /* A comma operator cannot appear in a constant-expression. */
9747 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9748 expression = error_mark_node;
9749 }
9750
9751 return expression;
9752 }
9753
9754 /* Parse a constant-expression.
9755
9756 constant-expression:
9757 conditional-expression
9758
9759 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9760 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9761 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9762 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9763 only parse a conditional-expression, otherwise parse an
9764 assignment-expression. See below for rationale. */
9765
9766 static cp_expr
9767 cp_parser_constant_expression (cp_parser* parser,
9768 bool allow_non_constant_p,
9769 bool *non_constant_p,
9770 bool strict_p)
9771 {
9772 bool saved_integral_constant_expression_p;
9773 bool saved_allow_non_integral_constant_expression_p;
9774 bool saved_non_integral_constant_expression_p;
9775 cp_expr expression;
9776
9777 /* It might seem that we could simply parse the
9778 conditional-expression, and then check to see if it were
9779 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9780 one that the compiler can figure out is constant, possibly after
9781 doing some simplifications or optimizations. The standard has a
9782 precise definition of constant-expression, and we must honor
9783 that, even though it is somewhat more restrictive.
9784
9785 For example:
9786
9787 int i[(2, 3)];
9788
9789 is not a legal declaration, because `(2, 3)' is not a
9790 constant-expression. The `,' operator is forbidden in a
9791 constant-expression. However, GCC's constant-folding machinery
9792 will fold this operation to an INTEGER_CST for `3'. */
9793
9794 /* Save the old settings. */
9795 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9796 saved_allow_non_integral_constant_expression_p
9797 = parser->allow_non_integral_constant_expression_p;
9798 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9799 /* We are now parsing a constant-expression. */
9800 parser->integral_constant_expression_p = true;
9801 parser->allow_non_integral_constant_expression_p
9802 = (allow_non_constant_p || cxx_dialect >= cxx11);
9803 parser->non_integral_constant_expression_p = false;
9804 /* Although the grammar says "conditional-expression", when not STRICT_P,
9805 we parse an "assignment-expression", which also permits
9806 "throw-expression" and the use of assignment operators. In the case
9807 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9808 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9809 actually essential that we look for an assignment-expression.
9810 For example, cp_parser_initializer_clauses uses this function to
9811 determine whether a particular assignment-expression is in fact
9812 constant. */
9813 if (strict_p)
9814 {
9815 /* Parse the binary expressions (logical-or-expression). */
9816 expression = cp_parser_binary_expression (parser, false, false, false,
9817 PREC_NOT_OPERATOR, NULL);
9818 /* If the next token is a `?' then we're actually looking at
9819 a conditional-expression; otherwise we're done. */
9820 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9821 expression = cp_parser_question_colon_clause (parser, expression);
9822 }
9823 else
9824 expression = cp_parser_assignment_expression (parser);
9825 /* Restore the old settings. */
9826 parser->integral_constant_expression_p
9827 = saved_integral_constant_expression_p;
9828 parser->allow_non_integral_constant_expression_p
9829 = saved_allow_non_integral_constant_expression_p;
9830 if (cxx_dialect >= cxx11)
9831 {
9832 /* Require an rvalue constant expression here; that's what our
9833 callers expect. Reference constant expressions are handled
9834 separately in e.g. cp_parser_template_argument. */
9835 tree decay = expression;
9836 if (TREE_TYPE (expression)
9837 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9838 decay = build_address (expression);
9839 bool is_const = potential_rvalue_constant_expression (decay);
9840 parser->non_integral_constant_expression_p = !is_const;
9841 if (!is_const && !allow_non_constant_p)
9842 require_potential_rvalue_constant_expression (decay);
9843 }
9844 if (allow_non_constant_p)
9845 *non_constant_p = parser->non_integral_constant_expression_p;
9846 parser->non_integral_constant_expression_p
9847 = saved_non_integral_constant_expression_p;
9848
9849 return expression;
9850 }
9851
9852 /* Parse __builtin_offsetof.
9853
9854 offsetof-expression:
9855 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9856
9857 offsetof-member-designator:
9858 id-expression
9859 | offsetof-member-designator "." id-expression
9860 | offsetof-member-designator "[" expression "]"
9861 | offsetof-member-designator "->" id-expression */
9862
9863 static cp_expr
9864 cp_parser_builtin_offsetof (cp_parser *parser)
9865 {
9866 int save_ice_p, save_non_ice_p;
9867 tree type;
9868 cp_expr expr;
9869 cp_id_kind dummy;
9870 cp_token *token;
9871 location_t finish_loc;
9872
9873 /* We're about to accept non-integral-constant things, but will
9874 definitely yield an integral constant expression. Save and
9875 restore these values around our local parsing. */
9876 save_ice_p = parser->integral_constant_expression_p;
9877 save_non_ice_p = parser->non_integral_constant_expression_p;
9878
9879 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9880
9881 /* Consume the "__builtin_offsetof" token. */
9882 cp_lexer_consume_token (parser->lexer);
9883 /* Consume the opening `('. */
9884 matching_parens parens;
9885 parens.require_open (parser);
9886 /* Parse the type-id. */
9887 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9888 {
9889 const char *saved_message = parser->type_definition_forbidden_message;
9890 parser->type_definition_forbidden_message
9891 = G_("types may not be defined within __builtin_offsetof");
9892 type = cp_parser_type_id (parser);
9893 parser->type_definition_forbidden_message = saved_message;
9894 }
9895 /* Look for the `,'. */
9896 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9897 token = cp_lexer_peek_token (parser->lexer);
9898
9899 /* Build the (type *)null that begins the traditional offsetof macro. */
9900 tree object_ptr
9901 = build_static_cast (build_pointer_type (type), null_pointer_node,
9902 tf_warning_or_error);
9903
9904 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9905 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9906 true, &dummy, token->location);
9907 while (true)
9908 {
9909 token = cp_lexer_peek_token (parser->lexer);
9910 switch (token->type)
9911 {
9912 case CPP_OPEN_SQUARE:
9913 /* offsetof-member-designator "[" expression "]" */
9914 expr = cp_parser_postfix_open_square_expression (parser, expr,
9915 true, false);
9916 break;
9917
9918 case CPP_DEREF:
9919 /* offsetof-member-designator "->" identifier */
9920 expr = grok_array_decl (token->location, expr,
9921 integer_zero_node, false);
9922 /* FALLTHRU */
9923
9924 case CPP_DOT:
9925 /* offsetof-member-designator "." identifier */
9926 cp_lexer_consume_token (parser->lexer);
9927 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9928 expr, true, &dummy,
9929 token->location);
9930 break;
9931
9932 case CPP_CLOSE_PAREN:
9933 /* Consume the ")" token. */
9934 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9935 cp_lexer_consume_token (parser->lexer);
9936 goto success;
9937
9938 default:
9939 /* Error. We know the following require will fail, but
9940 that gives the proper error message. */
9941 parens.require_close (parser);
9942 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9943 expr = error_mark_node;
9944 goto failure;
9945 }
9946 }
9947
9948 success:
9949 /* Make a location of the form:
9950 __builtin_offsetof (struct s, f)
9951 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9952 with caret at the type-id, ranging from the start of the
9953 "_builtin_offsetof" token to the close paren. */
9954 loc = make_location (loc, start_loc, finish_loc);
9955 /* The result will be an INTEGER_CST, so we need to explicitly
9956 preserve the location. */
9957 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9958
9959 failure:
9960 parser->integral_constant_expression_p = save_ice_p;
9961 parser->non_integral_constant_expression_p = save_non_ice_p;
9962
9963 expr = expr.maybe_add_location_wrapper ();
9964 return expr;
9965 }
9966
9967 /* Parse a trait expression.
9968
9969 Returns a representation of the expression, the underlying type
9970 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9971
9972 static cp_expr
9973 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9974 {
9975 cp_trait_kind kind;
9976 tree type1, type2 = NULL_TREE;
9977 bool binary = false;
9978 bool variadic = false;
9979
9980 switch (keyword)
9981 {
9982 case RID_HAS_NOTHROW_ASSIGN:
9983 kind = CPTK_HAS_NOTHROW_ASSIGN;
9984 break;
9985 case RID_HAS_NOTHROW_CONSTRUCTOR:
9986 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9987 break;
9988 case RID_HAS_NOTHROW_COPY:
9989 kind = CPTK_HAS_NOTHROW_COPY;
9990 break;
9991 case RID_HAS_TRIVIAL_ASSIGN:
9992 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9993 break;
9994 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9995 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9996 break;
9997 case RID_HAS_TRIVIAL_COPY:
9998 kind = CPTK_HAS_TRIVIAL_COPY;
9999 break;
10000 case RID_HAS_TRIVIAL_DESTRUCTOR:
10001 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10002 break;
10003 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10004 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10005 break;
10006 case RID_HAS_VIRTUAL_DESTRUCTOR:
10007 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10008 break;
10009 case RID_IS_ABSTRACT:
10010 kind = CPTK_IS_ABSTRACT;
10011 break;
10012 case RID_IS_AGGREGATE:
10013 kind = CPTK_IS_AGGREGATE;
10014 break;
10015 case RID_IS_BASE_OF:
10016 kind = CPTK_IS_BASE_OF;
10017 binary = true;
10018 break;
10019 case RID_IS_CLASS:
10020 kind = CPTK_IS_CLASS;
10021 break;
10022 case RID_IS_EMPTY:
10023 kind = CPTK_IS_EMPTY;
10024 break;
10025 case RID_IS_ENUM:
10026 kind = CPTK_IS_ENUM;
10027 break;
10028 case RID_IS_FINAL:
10029 kind = CPTK_IS_FINAL;
10030 break;
10031 case RID_IS_LITERAL_TYPE:
10032 kind = CPTK_IS_LITERAL_TYPE;
10033 break;
10034 case RID_IS_POD:
10035 kind = CPTK_IS_POD;
10036 break;
10037 case RID_IS_POLYMORPHIC:
10038 kind = CPTK_IS_POLYMORPHIC;
10039 break;
10040 case RID_IS_SAME_AS:
10041 kind = CPTK_IS_SAME_AS;
10042 binary = true;
10043 break;
10044 case RID_IS_STD_LAYOUT:
10045 kind = CPTK_IS_STD_LAYOUT;
10046 break;
10047 case RID_IS_TRIVIAL:
10048 kind = CPTK_IS_TRIVIAL;
10049 break;
10050 case RID_IS_TRIVIALLY_ASSIGNABLE:
10051 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10052 binary = true;
10053 break;
10054 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10055 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10056 variadic = true;
10057 break;
10058 case RID_IS_TRIVIALLY_COPYABLE:
10059 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10060 break;
10061 case RID_IS_UNION:
10062 kind = CPTK_IS_UNION;
10063 break;
10064 case RID_UNDERLYING_TYPE:
10065 kind = CPTK_UNDERLYING_TYPE;
10066 break;
10067 case RID_BASES:
10068 kind = CPTK_BASES;
10069 break;
10070 case RID_DIRECT_BASES:
10071 kind = CPTK_DIRECT_BASES;
10072 break;
10073 case RID_IS_ASSIGNABLE:
10074 kind = CPTK_IS_ASSIGNABLE;
10075 binary = true;
10076 break;
10077 case RID_IS_CONSTRUCTIBLE:
10078 kind = CPTK_IS_CONSTRUCTIBLE;
10079 variadic = true;
10080 break;
10081 default:
10082 gcc_unreachable ();
10083 }
10084
10085 /* Get location of initial token. */
10086 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10087
10088 /* Consume the token. */
10089 cp_lexer_consume_token (parser->lexer);
10090
10091 matching_parens parens;
10092 parens.require_open (parser);
10093
10094 {
10095 type_id_in_expr_sentinel s (parser);
10096 type1 = cp_parser_type_id (parser);
10097 }
10098
10099 if (type1 == error_mark_node)
10100 return error_mark_node;
10101
10102 if (binary)
10103 {
10104 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10105
10106 {
10107 type_id_in_expr_sentinel s (parser);
10108 type2 = cp_parser_type_id (parser);
10109 }
10110
10111 if (type2 == error_mark_node)
10112 return error_mark_node;
10113 }
10114 else if (variadic)
10115 {
10116 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10117 {
10118 cp_lexer_consume_token (parser->lexer);
10119 tree elt = cp_parser_type_id (parser);
10120 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10121 {
10122 cp_lexer_consume_token (parser->lexer);
10123 elt = make_pack_expansion (elt);
10124 }
10125 if (elt == error_mark_node)
10126 return error_mark_node;
10127 type2 = tree_cons (NULL_TREE, elt, type2);
10128 }
10129 }
10130
10131 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10132 parens.require_close (parser);
10133
10134 /* Construct a location of the form:
10135 __is_trivially_copyable(_Tp)
10136 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10137 with start == caret, finishing at the close-paren. */
10138 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10139
10140 /* Complete the trait expression, which may mean either processing
10141 the trait expr now or saving it for template instantiation. */
10142 switch (kind)
10143 {
10144 case CPTK_UNDERLYING_TYPE:
10145 return cp_expr (finish_underlying_type (type1), trait_loc);
10146 case CPTK_BASES:
10147 return cp_expr (finish_bases (type1, false), trait_loc);
10148 case CPTK_DIRECT_BASES:
10149 return cp_expr (finish_bases (type1, true), trait_loc);
10150 default:
10151 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10152 }
10153 }
10154
10155 /* Parse a lambda expression.
10156
10157 lambda-expression:
10158 lambda-introducer lambda-declarator [opt] compound-statement
10159
10160 Returns a representation of the expression. */
10161
10162 static cp_expr
10163 cp_parser_lambda_expression (cp_parser* parser)
10164 {
10165 tree lambda_expr = build_lambda_expr ();
10166 tree type;
10167 bool ok = true;
10168 cp_token *token = cp_lexer_peek_token (parser->lexer);
10169 cp_token_position start = 0;
10170
10171 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10172
10173 if (cxx_dialect >= cxx2a)
10174 /* C++20 allows lambdas in unevaluated context. */;
10175 else if (cp_unevaluated_operand)
10176 {
10177 if (!token->error_reported)
10178 {
10179 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10180 "lambda-expression in unevaluated context"
10181 " only available with -std=c++2a or -std=gnu++2a");
10182 token->error_reported = true;
10183 }
10184 ok = false;
10185 }
10186 else if (parser->in_template_argument_list_p)
10187 {
10188 if (!token->error_reported)
10189 {
10190 error_at (token->location, "lambda-expression in template-argument"
10191 " only available with -std=c++2a or -std=gnu++2a");
10192 token->error_reported = true;
10193 }
10194 ok = false;
10195 }
10196
10197 /* We may be in the middle of deferred access check. Disable
10198 it now. */
10199 push_deferring_access_checks (dk_no_deferred);
10200
10201 cp_parser_lambda_introducer (parser, lambda_expr);
10202 if (cp_parser_error_occurred (parser))
10203 return error_mark_node;
10204
10205 type = begin_lambda_type (lambda_expr);
10206 if (type == error_mark_node)
10207 return error_mark_node;
10208
10209 record_lambda_scope (lambda_expr);
10210
10211 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10212 determine_visibility (TYPE_NAME (type));
10213
10214 /* Now that we've started the type, add the capture fields for any
10215 explicit captures. */
10216 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10217
10218 {
10219 /* Inside the class, surrounding template-parameter-lists do not apply. */
10220 unsigned int saved_num_template_parameter_lists
10221 = parser->num_template_parameter_lists;
10222 unsigned char in_statement = parser->in_statement;
10223 bool in_switch_statement_p = parser->in_switch_statement_p;
10224 bool fully_implicit_function_template_p
10225 = parser->fully_implicit_function_template_p;
10226 tree implicit_template_parms = parser->implicit_template_parms;
10227 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10228 bool auto_is_implicit_function_template_parm_p
10229 = parser->auto_is_implicit_function_template_parm_p;
10230
10231 parser->num_template_parameter_lists = 0;
10232 parser->in_statement = 0;
10233 parser->in_switch_statement_p = false;
10234 parser->fully_implicit_function_template_p = false;
10235 parser->implicit_template_parms = 0;
10236 parser->implicit_template_scope = 0;
10237 parser->auto_is_implicit_function_template_parm_p = false;
10238
10239 /* By virtue of defining a local class, a lambda expression has access to
10240 the private variables of enclosing classes. */
10241
10242 if (cp_parser_start_tentative_firewall (parser))
10243 start = token;
10244
10245 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10246
10247 if (ok && cp_parser_error_occurred (parser))
10248 ok = false;
10249
10250 if (ok)
10251 {
10252 cp_parser_lambda_body (parser, lambda_expr);
10253 }
10254 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10255 {
10256 if (cp_parser_skip_to_closing_brace (parser))
10257 cp_lexer_consume_token (parser->lexer);
10258 }
10259
10260 /* The capture list was built up in reverse order; fix that now. */
10261 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10262 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10263
10264 if (ok)
10265 maybe_add_lambda_conv_op (type);
10266
10267 type = finish_struct (type, /*attributes=*/NULL_TREE);
10268
10269 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10270 parser->in_statement = in_statement;
10271 parser->in_switch_statement_p = in_switch_statement_p;
10272 parser->fully_implicit_function_template_p
10273 = fully_implicit_function_template_p;
10274 parser->implicit_template_parms = implicit_template_parms;
10275 parser->implicit_template_scope = implicit_template_scope;
10276 parser->auto_is_implicit_function_template_parm_p
10277 = auto_is_implicit_function_template_parm_p;
10278 }
10279
10280 /* This field is only used during parsing of the lambda. */
10281 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10282
10283 /* This lambda shouldn't have any proxies left at this point. */
10284 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10285 /* And now that we're done, push proxies for an enclosing lambda. */
10286 insert_pending_capture_proxies ();
10287
10288 /* Update the lambda expression to a range. */
10289 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10290 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10291 token->location,
10292 end_tok->location);
10293
10294 if (ok)
10295 lambda_expr = build_lambda_object (lambda_expr);
10296 else
10297 lambda_expr = error_mark_node;
10298
10299 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10300
10301 pop_deferring_access_checks ();
10302
10303 return lambda_expr;
10304 }
10305
10306 /* Parse the beginning of a lambda expression.
10307
10308 lambda-introducer:
10309 [ lambda-capture [opt] ]
10310
10311 LAMBDA_EXPR is the current representation of the lambda expression. */
10312
10313 static void
10314 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10315 {
10316 /* Need commas after the first capture. */
10317 bool first = true;
10318
10319 /* Eat the leading `['. */
10320 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10321
10322 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10323 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10324 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10325 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10326 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10327 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10328
10329 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10330 {
10331 cp_lexer_consume_token (parser->lexer);
10332 first = false;
10333
10334 if (!(at_function_scope_p () || parsing_nsdmi ()))
10335 error ("non-local lambda expression cannot have a capture-default");
10336 }
10337
10338 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10339 {
10340 cp_token* capture_token;
10341 tree capture_id;
10342 tree capture_init_expr;
10343 cp_id_kind idk = CP_ID_KIND_NONE;
10344 bool explicit_init_p = false;
10345
10346 enum capture_kind_type
10347 {
10348 BY_COPY,
10349 BY_REFERENCE
10350 };
10351 enum capture_kind_type capture_kind = BY_COPY;
10352
10353 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10354 {
10355 error ("expected end of capture-list");
10356 return;
10357 }
10358
10359 if (first)
10360 first = false;
10361 else
10362 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10363
10364 /* Possibly capture `this'. */
10365 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10366 {
10367 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10368 if (cxx_dialect < cxx2a
10369 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10370 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10371 "with by-copy capture default");
10372 cp_lexer_consume_token (parser->lexer);
10373 add_capture (lambda_expr,
10374 /*id=*/this_identifier,
10375 /*initializer=*/finish_this_expr (),
10376 /*by_reference_p=*/true,
10377 explicit_init_p);
10378 continue;
10379 }
10380
10381 /* Possibly capture `*this'. */
10382 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10383 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10384 {
10385 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10386 if (cxx_dialect < cxx17)
10387 pedwarn (loc, 0, "%<*this%> capture only available with "
10388 "-std=c++17 or -std=gnu++17");
10389 cp_lexer_consume_token (parser->lexer);
10390 cp_lexer_consume_token (parser->lexer);
10391 add_capture (lambda_expr,
10392 /*id=*/this_identifier,
10393 /*initializer=*/finish_this_expr (),
10394 /*by_reference_p=*/false,
10395 explicit_init_p);
10396 continue;
10397 }
10398
10399 bool init_pack_expansion = false;
10400 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10401 {
10402 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10403 if (cxx_dialect < cxx2a)
10404 pedwarn (loc, 0, "pack init-capture only available with "
10405 "-std=c++2a or -std=gnu++2a");
10406 cp_lexer_consume_token (parser->lexer);
10407 init_pack_expansion = true;
10408 }
10409
10410 /* Remember whether we want to capture as a reference or not. */
10411 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10412 {
10413 capture_kind = BY_REFERENCE;
10414 cp_lexer_consume_token (parser->lexer);
10415 }
10416
10417 /* Get the identifier. */
10418 capture_token = cp_lexer_peek_token (parser->lexer);
10419 capture_id = cp_parser_identifier (parser);
10420
10421 if (capture_id == error_mark_node)
10422 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10423 delimiters, but I modified this to stop on unnested ']' as well. It
10424 was already changed to stop on unnested '}', so the
10425 "closing_parenthesis" name is no more misleading with my change. */
10426 {
10427 cp_parser_skip_to_closing_parenthesis (parser,
10428 /*recovering=*/true,
10429 /*or_comma=*/true,
10430 /*consume_paren=*/true);
10431 break;
10432 }
10433
10434 /* Find the initializer for this capture. */
10435 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10436 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10437 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10438 {
10439 bool direct, non_constant;
10440 /* An explicit initializer exists. */
10441 if (cxx_dialect < cxx14)
10442 pedwarn (input_location, 0,
10443 "lambda capture initializers "
10444 "only available with -std=c++14 or -std=gnu++14");
10445 capture_init_expr = cp_parser_initializer (parser, &direct,
10446 &non_constant, true);
10447 explicit_init_p = true;
10448 if (capture_init_expr == NULL_TREE)
10449 {
10450 error ("empty initializer for lambda init-capture");
10451 capture_init_expr = error_mark_node;
10452 }
10453 if (init_pack_expansion)
10454 capture_init_expr = make_pack_expansion (capture_init_expr);
10455 }
10456 else
10457 {
10458 const char* error_msg;
10459
10460 /* Turn the identifier into an id-expression. */
10461 capture_init_expr
10462 = cp_parser_lookup_name_simple (parser, capture_id,
10463 capture_token->location);
10464
10465 if (capture_init_expr == error_mark_node)
10466 {
10467 unqualified_name_lookup_error (capture_id);
10468 continue;
10469 }
10470 else if (!VAR_P (capture_init_expr)
10471 && TREE_CODE (capture_init_expr) != PARM_DECL)
10472 {
10473 error_at (capture_token->location,
10474 "capture of non-variable %qE",
10475 capture_init_expr);
10476 if (DECL_P (capture_init_expr))
10477 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10478 "%q#D declared here", capture_init_expr);
10479 continue;
10480 }
10481 if (VAR_P (capture_init_expr)
10482 && decl_storage_duration (capture_init_expr) != dk_auto)
10483 {
10484 if (pedwarn (capture_token->location, 0, "capture of variable "
10485 "%qD with non-automatic storage duration",
10486 capture_init_expr))
10487 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10488 "%q#D declared here", capture_init_expr);
10489 continue;
10490 }
10491
10492 capture_init_expr
10493 = finish_id_expression
10494 (capture_id,
10495 capture_init_expr,
10496 parser->scope,
10497 &idk,
10498 /*integral_constant_expression_p=*/false,
10499 /*allow_non_integral_constant_expression_p=*/false,
10500 /*non_integral_constant_expression_p=*/NULL,
10501 /*template_p=*/false,
10502 /*done=*/true,
10503 /*address_p=*/false,
10504 /*template_arg_p=*/false,
10505 &error_msg,
10506 capture_token->location);
10507
10508 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10509 {
10510 cp_lexer_consume_token (parser->lexer);
10511 capture_init_expr = make_pack_expansion (capture_init_expr);
10512 }
10513 }
10514
10515 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10516 && !explicit_init_p)
10517 {
10518 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10519 && capture_kind == BY_COPY)
10520 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10521 "of %qD redundant with by-copy capture default",
10522 capture_id);
10523 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10524 && capture_kind == BY_REFERENCE)
10525 pedwarn (capture_token->location, 0, "explicit by-reference "
10526 "capture of %qD redundant with by-reference capture "
10527 "default", capture_id);
10528 }
10529
10530 add_capture (lambda_expr,
10531 capture_id,
10532 capture_init_expr,
10533 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10534 explicit_init_p);
10535
10536 /* If there is any qualification still in effect, clear it
10537 now; we will be starting fresh with the next capture. */
10538 parser->scope = NULL_TREE;
10539 parser->qualifying_scope = NULL_TREE;
10540 parser->object_scope = NULL_TREE;
10541 }
10542
10543 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10544 }
10545
10546 /* Parse the (optional) middle of a lambda expression.
10547
10548 lambda-declarator:
10549 < template-parameter-list [opt] >
10550 ( parameter-declaration-clause [opt] )
10551 attribute-specifier [opt]
10552 decl-specifier-seq [opt]
10553 exception-specification [opt]
10554 lambda-return-type-clause [opt]
10555
10556 LAMBDA_EXPR is the current representation of the lambda expression. */
10557
10558 static bool
10559 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10560 {
10561 /* 5.1.1.4 of the standard says:
10562 If a lambda-expression does not include a lambda-declarator, it is as if
10563 the lambda-declarator were ().
10564 This means an empty parameter list, no attributes, and no exception
10565 specification. */
10566 tree param_list = void_list_node;
10567 tree attributes = NULL_TREE;
10568 tree exception_spec = NULL_TREE;
10569 tree template_param_list = NULL_TREE;
10570 tree tx_qual = NULL_TREE;
10571 tree return_type = NULL_TREE;
10572 cp_decl_specifier_seq lambda_specs;
10573 clear_decl_specs (&lambda_specs);
10574
10575 /* The template-parameter-list is optional, but must begin with
10576 an opening angle if present. */
10577 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10578 {
10579 if (cxx_dialect < cxx14)
10580 pedwarn (parser->lexer->next_token->location, 0,
10581 "lambda templates are only available with "
10582 "-std=c++14 or -std=gnu++14");
10583 else if (cxx_dialect < cxx2a)
10584 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10585 "lambda templates are only available with "
10586 "-std=c++2a or -std=gnu++2a");
10587
10588 cp_lexer_consume_token (parser->lexer);
10589
10590 template_param_list = cp_parser_template_parameter_list (parser);
10591
10592 cp_parser_skip_to_end_of_template_parameter_list (parser);
10593
10594 /* We just processed one more parameter list. */
10595 ++parser->num_template_parameter_lists;
10596 }
10597
10598 /* The parameter-declaration-clause is optional (unless
10599 template-parameter-list was given), but must begin with an
10600 opening parenthesis if present. */
10601 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10602 {
10603 matching_parens parens;
10604 parens.consume_open (parser);
10605
10606 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10607
10608 /* Parse parameters. */
10609 param_list = cp_parser_parameter_declaration_clause (parser);
10610
10611 /* Default arguments shall not be specified in the
10612 parameter-declaration-clause of a lambda-declarator. */
10613 if (cxx_dialect < cxx14)
10614 for (tree t = param_list; t; t = TREE_CHAIN (t))
10615 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10616 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10617 "default argument specified for lambda parameter");
10618
10619 parens.require_close (parser);
10620
10621 /* In the decl-specifier-seq of the lambda-declarator, each
10622 decl-specifier shall either be mutable or constexpr. */
10623 int declares_class_or_enum;
10624 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10625 cp_parser_decl_specifier_seq (parser,
10626 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10627 &lambda_specs, &declares_class_or_enum);
10628 if (lambda_specs.storage_class == sc_mutable)
10629 {
10630 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10631 if (lambda_specs.conflicting_specifiers_p)
10632 error_at (lambda_specs.locations[ds_storage_class],
10633 "duplicate %<mutable%>");
10634 }
10635
10636 tx_qual = cp_parser_tx_qualifier_opt (parser);
10637
10638 /* Parse optional exception specification. */
10639 exception_spec = cp_parser_exception_specification_opt (parser);
10640
10641 attributes = cp_parser_std_attribute_spec_seq (parser);
10642
10643 /* Parse optional trailing return type. */
10644 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10645 {
10646 cp_lexer_consume_token (parser->lexer);
10647 return_type = cp_parser_trailing_type_id (parser);
10648 }
10649
10650 /* The function parameters must be in scope all the way until after the
10651 trailing-return-type in case of decltype. */
10652 pop_bindings_and_leave_scope ();
10653 }
10654 else if (template_param_list != NULL_TREE) // generate diagnostic
10655 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10656
10657 /* Create the function call operator.
10658
10659 Messing with declarators like this is no uglier than building up the
10660 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10661 other code. */
10662 {
10663 cp_decl_specifier_seq return_type_specs;
10664 cp_declarator* declarator;
10665 tree fco;
10666 int quals;
10667 void *p;
10668
10669 clear_decl_specs (&return_type_specs);
10670 return_type_specs.type = make_auto ();
10671
10672 if (lambda_specs.locations[ds_constexpr])
10673 {
10674 if (cxx_dialect >= cxx17)
10675 return_type_specs.locations[ds_constexpr]
10676 = lambda_specs.locations[ds_constexpr];
10677 else
10678 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10679 "lambda only available with -std=c++17 or -std=gnu++17");
10680 }
10681
10682 p = obstack_alloc (&declarator_obstack, 0);
10683
10684 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10685 LAMBDA_EXPR_LOCATION (lambda_expr));
10686
10687 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10688 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10689 declarator = make_call_declarator (declarator, param_list, quals,
10690 VIRT_SPEC_UNSPECIFIED,
10691 REF_QUAL_NONE,
10692 tx_qual,
10693 exception_spec,
10694 return_type,
10695 /*requires_clause*/NULL_TREE);
10696 declarator->std_attributes = attributes;
10697
10698 fco = grokmethod (&return_type_specs,
10699 declarator,
10700 NULL_TREE);
10701 if (fco != error_mark_node)
10702 {
10703 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10704 DECL_ARTIFICIAL (fco) = 1;
10705 /* Give the object parameter a different name. */
10706 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10707 DECL_LAMBDA_FUNCTION (fco) = 1;
10708 }
10709 if (template_param_list)
10710 {
10711 fco = finish_member_template_decl (fco);
10712 finish_template_decl (template_param_list);
10713 --parser->num_template_parameter_lists;
10714 }
10715 else if (parser->fully_implicit_function_template_p)
10716 fco = finish_fully_implicit_template (parser, fco);
10717
10718 finish_member_declaration (fco);
10719
10720 obstack_free (&declarator_obstack, p);
10721
10722 return (fco != error_mark_node);
10723 }
10724 }
10725
10726 /* Parse the body of a lambda expression, which is simply
10727
10728 compound-statement
10729
10730 but which requires special handling.
10731 LAMBDA_EXPR is the current representation of the lambda expression. */
10732
10733 static void
10734 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10735 {
10736 bool nested = (current_function_decl != NULL_TREE);
10737 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10738 bool in_function_body = parser->in_function_body;
10739
10740 /* The body of a lambda-expression is not a subexpression of the enclosing
10741 expression. */
10742 cp_evaluated ev;
10743
10744 if (nested)
10745 push_function_context ();
10746 else
10747 /* Still increment function_depth so that we don't GC in the
10748 middle of an expression. */
10749 ++function_depth;
10750
10751 vec<tree> omp_privatization_save;
10752 save_omp_privatization_clauses (omp_privatization_save);
10753 /* Clear this in case we're in the middle of a default argument. */
10754 parser->local_variables_forbidden_p = false;
10755 parser->in_function_body = true;
10756
10757 {
10758 local_specialization_stack s (lss_copy);
10759 tree fco = lambda_function (lambda_expr);
10760 tree body = start_lambda_function (fco, lambda_expr);
10761 matching_braces braces;
10762
10763 if (braces.require_open (parser))
10764 {
10765 tree compound_stmt = begin_compound_stmt (0);
10766
10767 /* Originally C++11 required us to peek for 'return expr'; and
10768 process it specially here to deduce the return type. N3638
10769 removed the need for that. */
10770
10771 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10772 cp_parser_label_declaration (parser);
10773 cp_parser_statement_seq_opt (parser, NULL_TREE);
10774 braces.require_close (parser);
10775
10776 finish_compound_stmt (compound_stmt);
10777 }
10778
10779 finish_lambda_function (body);
10780 }
10781
10782 restore_omp_privatization_clauses (omp_privatization_save);
10783 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10784 parser->in_function_body = in_function_body;
10785 if (nested)
10786 pop_function_context();
10787 else
10788 --function_depth;
10789 }
10790
10791 /* Statements [gram.stmt.stmt] */
10792
10793 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10794
10795 static void
10796 add_debug_begin_stmt (location_t loc)
10797 {
10798 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10799 return;
10800 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10801 /* A concept is never expanded normally. */
10802 return;
10803
10804 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10805 SET_EXPR_LOCATION (stmt, loc);
10806 add_stmt (stmt);
10807 }
10808
10809 /* Parse a statement.
10810
10811 statement:
10812 labeled-statement
10813 expression-statement
10814 compound-statement
10815 selection-statement
10816 iteration-statement
10817 jump-statement
10818 declaration-statement
10819 try-block
10820
10821 C++11:
10822
10823 statement:
10824 labeled-statement
10825 attribute-specifier-seq (opt) expression-statement
10826 attribute-specifier-seq (opt) compound-statement
10827 attribute-specifier-seq (opt) selection-statement
10828 attribute-specifier-seq (opt) iteration-statement
10829 attribute-specifier-seq (opt) jump-statement
10830 declaration-statement
10831 attribute-specifier-seq (opt) try-block
10832
10833 init-statement:
10834 expression-statement
10835 simple-declaration
10836
10837 TM Extension:
10838
10839 statement:
10840 atomic-statement
10841
10842 IN_COMPOUND is true when the statement is nested inside a
10843 cp_parser_compound_statement; this matters for certain pragmas.
10844
10845 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10846 is a (possibly labeled) if statement which is not enclosed in braces
10847 and has an else clause. This is used to implement -Wparentheses.
10848
10849 CHAIN is a vector of if-else-if conditions. */
10850
10851 static void
10852 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10853 bool in_compound, bool *if_p, vec<tree> *chain,
10854 location_t *loc_after_labels)
10855 {
10856 tree statement, std_attrs = NULL_TREE;
10857 cp_token *token;
10858 location_t statement_location, attrs_location;
10859
10860 restart:
10861 if (if_p != NULL)
10862 *if_p = false;
10863 /* There is no statement yet. */
10864 statement = NULL_TREE;
10865
10866 saved_token_sentinel saved_tokens (parser->lexer);
10867 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10868 if (c_dialect_objc ())
10869 /* In obj-c++, seeing '[[' might be the either the beginning of
10870 c++11 attributes, or a nested objc-message-expression. So
10871 let's parse the c++11 attributes tentatively. */
10872 cp_parser_parse_tentatively (parser);
10873 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10874 if (c_dialect_objc ())
10875 {
10876 if (!cp_parser_parse_definitely (parser))
10877 std_attrs = NULL_TREE;
10878 }
10879
10880 /* Peek at the next token. */
10881 token = cp_lexer_peek_token (parser->lexer);
10882 /* Remember the location of the first token in the statement. */
10883 cp_token *statement_token = token;
10884 statement_location = token->location;
10885 add_debug_begin_stmt (statement_location);
10886 /* If this is a keyword, then that will often determine what kind of
10887 statement we have. */
10888 if (token->type == CPP_KEYWORD)
10889 {
10890 enum rid keyword = token->keyword;
10891
10892 switch (keyword)
10893 {
10894 case RID_CASE:
10895 case RID_DEFAULT:
10896 /* Looks like a labeled-statement with a case label.
10897 Parse the label, and then use tail recursion to parse
10898 the statement. */
10899 cp_parser_label_for_labeled_statement (parser, std_attrs);
10900 in_compound = false;
10901 goto restart;
10902
10903 case RID_IF:
10904 case RID_SWITCH:
10905 std_attrs = process_stmt_hotness_attribute (std_attrs);
10906 statement = cp_parser_selection_statement (parser, if_p, chain);
10907 break;
10908
10909 case RID_WHILE:
10910 case RID_DO:
10911 case RID_FOR:
10912 std_attrs = process_stmt_hotness_attribute (std_attrs);
10913 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
10914 break;
10915
10916 case RID_BREAK:
10917 case RID_CONTINUE:
10918 case RID_RETURN:
10919 case RID_GOTO:
10920 std_attrs = process_stmt_hotness_attribute (std_attrs);
10921 statement = cp_parser_jump_statement (parser);
10922 break;
10923
10924 /* Objective-C++ exception-handling constructs. */
10925 case RID_AT_TRY:
10926 case RID_AT_CATCH:
10927 case RID_AT_FINALLY:
10928 case RID_AT_SYNCHRONIZED:
10929 case RID_AT_THROW:
10930 std_attrs = process_stmt_hotness_attribute (std_attrs);
10931 statement = cp_parser_objc_statement (parser);
10932 break;
10933
10934 case RID_TRY:
10935 std_attrs = process_stmt_hotness_attribute (std_attrs);
10936 statement = cp_parser_try_block (parser);
10937 break;
10938
10939 case RID_NAMESPACE:
10940 /* This must be a namespace alias definition. */
10941 if (std_attrs != NULL_TREE)
10942 {
10943 /* Attributes should be parsed as part of the the
10944 declaration, so let's un-parse them. */
10945 saved_tokens.rollback();
10946 std_attrs = NULL_TREE;
10947 }
10948 cp_parser_declaration_statement (parser);
10949 return;
10950
10951 case RID_TRANSACTION_ATOMIC:
10952 case RID_TRANSACTION_RELAXED:
10953 case RID_SYNCHRONIZED:
10954 case RID_ATOMIC_NOEXCEPT:
10955 case RID_ATOMIC_CANCEL:
10956 std_attrs = process_stmt_hotness_attribute (std_attrs);
10957 statement = cp_parser_transaction (parser, token);
10958 break;
10959 case RID_TRANSACTION_CANCEL:
10960 std_attrs = process_stmt_hotness_attribute (std_attrs);
10961 statement = cp_parser_transaction_cancel (parser);
10962 break;
10963
10964 default:
10965 /* It might be a keyword like `int' that can start a
10966 declaration-statement. */
10967 break;
10968 }
10969 }
10970 else if (token->type == CPP_NAME)
10971 {
10972 /* If the next token is a `:', then we are looking at a
10973 labeled-statement. */
10974 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10975 if (token->type == CPP_COLON)
10976 {
10977 /* Looks like a labeled-statement with an ordinary label.
10978 Parse the label, and then use tail recursion to parse
10979 the statement. */
10980
10981 cp_parser_label_for_labeled_statement (parser, std_attrs);
10982 in_compound = false;
10983 goto restart;
10984 }
10985 }
10986 /* Anything that starts with a `{' must be a compound-statement. */
10987 else if (token->type == CPP_OPEN_BRACE)
10988 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10989 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10990 a statement all its own. */
10991 else if (token->type == CPP_PRAGMA)
10992 {
10993 /* Only certain OpenMP pragmas are attached to statements, and thus
10994 are considered statements themselves. All others are not. In
10995 the context of a compound, accept the pragma as a "statement" and
10996 return so that we can check for a close brace. Otherwise we
10997 require a real statement and must go back and read one. */
10998 if (in_compound)
10999 cp_parser_pragma (parser, pragma_compound, if_p);
11000 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11001 goto restart;
11002 return;
11003 }
11004 else if (token->type == CPP_EOF)
11005 {
11006 cp_parser_error (parser, "expected statement");
11007 return;
11008 }
11009
11010 /* Everything else must be a declaration-statement or an
11011 expression-statement. Try for the declaration-statement
11012 first, unless we are looking at a `;', in which case we know that
11013 we have an expression-statement. */
11014 if (!statement)
11015 {
11016 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11017 {
11018 if (std_attrs != NULL_TREE)
11019 /* Attributes should be parsed as part of the declaration,
11020 so let's un-parse them. */
11021 saved_tokens.rollback();
11022
11023 cp_parser_parse_tentatively (parser);
11024 /* Try to parse the declaration-statement. */
11025 cp_parser_declaration_statement (parser);
11026 /* If that worked, we're done. */
11027 if (cp_parser_parse_definitely (parser))
11028 return;
11029 /* It didn't work, restore the post-attribute position. */
11030 if (std_attrs)
11031 cp_lexer_set_token_position (parser->lexer, statement_token);
11032 }
11033 /* All preceding labels have been parsed at this point. */
11034 if (loc_after_labels != NULL)
11035 *loc_after_labels = statement_location;
11036
11037 std_attrs = process_stmt_hotness_attribute (std_attrs);
11038
11039 /* Look for an expression-statement instead. */
11040 statement = cp_parser_expression_statement (parser, in_statement_expr);
11041
11042 /* Handle [[fallthrough]];. */
11043 if (attribute_fallthrough_p (std_attrs))
11044 {
11045 /* The next token after the fallthrough attribute is ';'. */
11046 if (statement == NULL_TREE)
11047 {
11048 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11049 statement = build_call_expr_internal_loc (statement_location,
11050 IFN_FALLTHROUGH,
11051 void_type_node, 0);
11052 finish_expr_stmt (statement);
11053 }
11054 else
11055 warning_at (statement_location, OPT_Wattributes,
11056 "%<fallthrough%> attribute not followed by %<;%>");
11057 std_attrs = NULL_TREE;
11058 }
11059 }
11060
11061 /* Set the line number for the statement. */
11062 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11063 SET_EXPR_LOCATION (statement, statement_location);
11064
11065 /* Allow "[[fallthrough]];", but warn otherwise. */
11066 if (std_attrs != NULL_TREE)
11067 warning_at (attrs_location,
11068 OPT_Wattributes,
11069 "attributes at the beginning of statement are ignored");
11070 }
11071
11072 /* Append ATTR to attribute list ATTRS. */
11073
11074 static tree
11075 attr_chainon (tree attrs, tree attr)
11076 {
11077 if (attrs == error_mark_node)
11078 return error_mark_node;
11079 if (attr == error_mark_node)
11080 return error_mark_node;
11081 return chainon (attrs, attr);
11082 }
11083
11084 /* Parse the label for a labeled-statement, i.e.
11085
11086 identifier :
11087 case constant-expression :
11088 default :
11089
11090 GNU Extension:
11091 case constant-expression ... constant-expression : statement
11092
11093 When a label is parsed without errors, the label is added to the
11094 parse tree by the finish_* functions, so this function doesn't
11095 have to return the label. */
11096
11097 static void
11098 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11099 {
11100 cp_token *token;
11101 tree label = NULL_TREE;
11102 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11103
11104 /* The next token should be an identifier. */
11105 token = cp_lexer_peek_token (parser->lexer);
11106 if (token->type != CPP_NAME
11107 && token->type != CPP_KEYWORD)
11108 {
11109 cp_parser_error (parser, "expected labeled-statement");
11110 return;
11111 }
11112
11113 /* Remember whether this case or a user-defined label is allowed to fall
11114 through to. */
11115 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11116
11117 parser->colon_corrects_to_scope_p = false;
11118 switch (token->keyword)
11119 {
11120 case RID_CASE:
11121 {
11122 tree expr, expr_hi;
11123 cp_token *ellipsis;
11124
11125 /* Consume the `case' token. */
11126 cp_lexer_consume_token (parser->lexer);
11127 /* Parse the constant-expression. */
11128 expr = cp_parser_constant_expression (parser);
11129 if (check_for_bare_parameter_packs (expr))
11130 expr = error_mark_node;
11131
11132 ellipsis = cp_lexer_peek_token (parser->lexer);
11133 if (ellipsis->type == CPP_ELLIPSIS)
11134 {
11135 /* Consume the `...' token. */
11136 cp_lexer_consume_token (parser->lexer);
11137 expr_hi = cp_parser_constant_expression (parser);
11138 if (check_for_bare_parameter_packs (expr_hi))
11139 expr_hi = error_mark_node;
11140
11141 /* We don't need to emit warnings here, as the common code
11142 will do this for us. */
11143 }
11144 else
11145 expr_hi = NULL_TREE;
11146
11147 if (parser->in_switch_statement_p)
11148 {
11149 tree l = finish_case_label (token->location, expr, expr_hi);
11150 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11151 {
11152 label = CASE_LABEL (l);
11153 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11154 }
11155 }
11156 else
11157 error_at (token->location,
11158 "case label %qE not within a switch statement",
11159 expr);
11160 }
11161 break;
11162
11163 case RID_DEFAULT:
11164 /* Consume the `default' token. */
11165 cp_lexer_consume_token (parser->lexer);
11166
11167 if (parser->in_switch_statement_p)
11168 {
11169 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11170 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11171 {
11172 label = CASE_LABEL (l);
11173 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11174 }
11175 }
11176 else
11177 error_at (token->location, "case label not within a switch statement");
11178 break;
11179
11180 default:
11181 /* Anything else must be an ordinary label. */
11182 label = finish_label_stmt (cp_parser_identifier (parser));
11183 if (label && TREE_CODE (label) == LABEL_DECL)
11184 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11185 break;
11186 }
11187
11188 /* Require the `:' token. */
11189 cp_parser_require (parser, CPP_COLON, RT_COLON);
11190
11191 /* An ordinary label may optionally be followed by attributes.
11192 However, this is only permitted if the attributes are then
11193 followed by a semicolon. This is because, for backward
11194 compatibility, when parsing
11195 lab: __attribute__ ((unused)) int i;
11196 we want the attribute to attach to "i", not "lab". */
11197 if (label != NULL_TREE
11198 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11199 {
11200 tree attrs;
11201 cp_parser_parse_tentatively (parser);
11202 attrs = cp_parser_gnu_attributes_opt (parser);
11203 if (attrs == NULL_TREE
11204 /* And fallthrough always binds to the expression-statement. */
11205 || attribute_fallthrough_p (attrs)
11206 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11207 cp_parser_abort_tentative_parse (parser);
11208 else if (!cp_parser_parse_definitely (parser))
11209 ;
11210 else
11211 attributes = attr_chainon (attributes, attrs);
11212 }
11213
11214 if (attributes != NULL_TREE)
11215 cplus_decl_attributes (&label, attributes, 0);
11216
11217 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11218 }
11219
11220 /* Parse an expression-statement.
11221
11222 expression-statement:
11223 expression [opt] ;
11224
11225 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11226 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11227 indicates whether this expression-statement is part of an
11228 expression statement. */
11229
11230 static tree
11231 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11232 {
11233 tree statement = NULL_TREE;
11234 cp_token *token = cp_lexer_peek_token (parser->lexer);
11235 location_t loc = token->location;
11236
11237 /* There might be attribute fallthrough. */
11238 tree attr = cp_parser_gnu_attributes_opt (parser);
11239
11240 /* If the next token is a ';', then there is no expression
11241 statement. */
11242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11243 {
11244 statement = cp_parser_expression (parser);
11245 if (statement == error_mark_node
11246 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11247 {
11248 cp_parser_skip_to_end_of_block_or_statement (parser);
11249 return error_mark_node;
11250 }
11251 }
11252
11253 /* Handle [[fallthrough]];. */
11254 if (attribute_fallthrough_p (attr))
11255 {
11256 /* The next token after the fallthrough attribute is ';'. */
11257 if (statement == NULL_TREE)
11258 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11259 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11260 void_type_node, 0);
11261 else
11262 warning_at (loc, OPT_Wattributes,
11263 "%<fallthrough%> attribute not followed by %<;%>");
11264 attr = NULL_TREE;
11265 }
11266
11267 /* Allow "[[fallthrough]];", but warn otherwise. */
11268 if (attr != NULL_TREE)
11269 warning_at (loc, OPT_Wattributes,
11270 "attributes at the beginning of statement are ignored");
11271
11272 /* Give a helpful message for "A<T>::type t;" and the like. */
11273 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11274 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11275 {
11276 if (TREE_CODE (statement) == SCOPE_REF)
11277 error_at (token->location, "need %<typename%> before %qE because "
11278 "%qT is a dependent scope",
11279 statement, TREE_OPERAND (statement, 0));
11280 else if (is_overloaded_fn (statement)
11281 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11282 {
11283 /* A::A a; */
11284 tree fn = get_first_fn (statement);
11285 error_at (token->location,
11286 "%<%T::%D%> names the constructor, not the type",
11287 DECL_CONTEXT (fn), DECL_NAME (fn));
11288 }
11289 }
11290
11291 /* Consume the final `;'. */
11292 cp_parser_consume_semicolon_at_end_of_statement (parser);
11293
11294 if (in_statement_expr
11295 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11296 /* This is the final expression statement of a statement
11297 expression. */
11298 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11299 else if (statement)
11300 statement = finish_expr_stmt (statement);
11301
11302 return statement;
11303 }
11304
11305 /* Parse a compound-statement.
11306
11307 compound-statement:
11308 { statement-seq [opt] }
11309
11310 GNU extension:
11311
11312 compound-statement:
11313 { label-declaration-seq [opt] statement-seq [opt] }
11314
11315 label-declaration-seq:
11316 label-declaration
11317 label-declaration-seq label-declaration
11318
11319 Returns a tree representing the statement. */
11320
11321 static tree
11322 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11323 int bcs_flags, bool function_body)
11324 {
11325 tree compound_stmt;
11326 matching_braces braces;
11327
11328 /* Consume the `{'. */
11329 if (!braces.require_open (parser))
11330 return error_mark_node;
11331 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11332 && !function_body && cxx_dialect < cxx14)
11333 pedwarn (input_location, OPT_Wpedantic,
11334 "compound-statement in %<constexpr%> function");
11335 /* Begin the compound-statement. */
11336 compound_stmt = begin_compound_stmt (bcs_flags);
11337 /* If the next keyword is `__label__' we have a label declaration. */
11338 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11339 cp_parser_label_declaration (parser);
11340 /* Parse an (optional) statement-seq. */
11341 cp_parser_statement_seq_opt (parser, in_statement_expr);
11342 /* Finish the compound-statement. */
11343 finish_compound_stmt (compound_stmt);
11344 /* Consume the `}'. */
11345 braces.require_close (parser);
11346
11347 return compound_stmt;
11348 }
11349
11350 /* Parse an (optional) statement-seq.
11351
11352 statement-seq:
11353 statement
11354 statement-seq [opt] statement */
11355
11356 static void
11357 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11358 {
11359 /* Scan statements until there aren't any more. */
11360 while (true)
11361 {
11362 cp_token *token = cp_lexer_peek_token (parser->lexer);
11363
11364 /* If we are looking at a `}', then we have run out of
11365 statements; the same is true if we have reached the end
11366 of file, or have stumbled upon a stray '@end'. */
11367 if (token->type == CPP_CLOSE_BRACE
11368 || token->type == CPP_EOF
11369 || token->type == CPP_PRAGMA_EOL
11370 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11371 break;
11372
11373 /* If we are in a compound statement and find 'else' then
11374 something went wrong. */
11375 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11376 {
11377 if (parser->in_statement & IN_IF_STMT)
11378 break;
11379 else
11380 {
11381 token = cp_lexer_consume_token (parser->lexer);
11382 error_at (token->location, "%<else%> without a previous %<if%>");
11383 }
11384 }
11385
11386 /* Parse the statement. */
11387 cp_parser_statement (parser, in_statement_expr, true, NULL);
11388 }
11389 }
11390
11391 /* Return true if this is the C++20 version of range-based-for with
11392 init-statement. */
11393
11394 static bool
11395 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11396 {
11397 bool r = false;
11398
11399 /* Save tokens so that we can put them back. */
11400 cp_lexer_save_tokens (parser->lexer);
11401
11402 /* There has to be an unnested ; followed by an unnested :. */
11403 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11404 /*recovering=*/false,
11405 CPP_SEMICOLON,
11406 /*consume_paren=*/false) != -1)
11407 goto out;
11408
11409 /* We found the semicolon, eat it now. */
11410 cp_lexer_consume_token (parser->lexer);
11411
11412 /* Now look for ':' that is not nested in () or {}. */
11413 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11414 /*recovering=*/false,
11415 CPP_COLON,
11416 /*consume_paren=*/false) == -1);
11417
11418 out:
11419 /* Roll back the tokens we skipped. */
11420 cp_lexer_rollback_tokens (parser->lexer);
11421
11422 return r;
11423 }
11424
11425 /* Return true if we're looking at (init; cond), false otherwise. */
11426
11427 static bool
11428 cp_parser_init_statement_p (cp_parser *parser)
11429 {
11430 /* Save tokens so that we can put them back. */
11431 cp_lexer_save_tokens (parser->lexer);
11432
11433 /* Look for ';' that is not nested in () or {}. */
11434 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11435 /*recovering=*/false,
11436 CPP_SEMICOLON,
11437 /*consume_paren=*/false);
11438
11439 /* Roll back the tokens we skipped. */
11440 cp_lexer_rollback_tokens (parser->lexer);
11441
11442 return ret == -1;
11443 }
11444
11445 /* Parse a selection-statement.
11446
11447 selection-statement:
11448 if ( init-statement [opt] condition ) statement
11449 if ( init-statement [opt] condition ) statement else statement
11450 switch ( init-statement [opt] condition ) statement
11451
11452 Returns the new IF_STMT or SWITCH_STMT.
11453
11454 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11455 is a (possibly labeled) if statement which is not enclosed in
11456 braces and has an else clause. This is used to implement
11457 -Wparentheses.
11458
11459 CHAIN is a vector of if-else-if conditions. This is used to implement
11460 -Wduplicated-cond. */
11461
11462 static tree
11463 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11464 vec<tree> *chain)
11465 {
11466 cp_token *token;
11467 enum rid keyword;
11468 token_indent_info guard_tinfo;
11469
11470 if (if_p != NULL)
11471 *if_p = false;
11472
11473 /* Peek at the next token. */
11474 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11475 guard_tinfo = get_token_indent_info (token);
11476
11477 /* See what kind of keyword it is. */
11478 keyword = token->keyword;
11479 switch (keyword)
11480 {
11481 case RID_IF:
11482 case RID_SWITCH:
11483 {
11484 tree statement;
11485 tree condition;
11486
11487 bool cx = false;
11488 if (keyword == RID_IF
11489 && cp_lexer_next_token_is_keyword (parser->lexer,
11490 RID_CONSTEXPR))
11491 {
11492 cx = true;
11493 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11494 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11495 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11496 "with -std=c++17 or -std=gnu++17");
11497 }
11498
11499 /* Look for the `('. */
11500 matching_parens parens;
11501 if (!parens.require_open (parser))
11502 {
11503 cp_parser_skip_to_end_of_statement (parser);
11504 return error_mark_node;
11505 }
11506
11507 /* Begin the selection-statement. */
11508 if (keyword == RID_IF)
11509 {
11510 statement = begin_if_stmt ();
11511 IF_STMT_CONSTEXPR_P (statement) = cx;
11512 }
11513 else
11514 statement = begin_switch_stmt ();
11515
11516 /* Parse the optional init-statement. */
11517 if (cp_parser_init_statement_p (parser))
11518 {
11519 tree decl;
11520 if (cxx_dialect < cxx17)
11521 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11522 "init-statement in selection statements only available "
11523 "with -std=c++17 or -std=gnu++17");
11524 cp_parser_init_statement (parser, &decl);
11525 }
11526
11527 /* Parse the condition. */
11528 condition = cp_parser_condition (parser);
11529 /* Look for the `)'. */
11530 if (!parens.require_close (parser))
11531 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11532 /*consume_paren=*/true);
11533
11534 if (keyword == RID_IF)
11535 {
11536 bool nested_if;
11537 unsigned char in_statement;
11538
11539 /* Add the condition. */
11540 condition = finish_if_stmt_cond (condition, statement);
11541
11542 if (warn_duplicated_cond)
11543 warn_duplicated_cond_add_or_warn (token->location, condition,
11544 &chain);
11545
11546 /* Parse the then-clause. */
11547 in_statement = parser->in_statement;
11548 parser->in_statement |= IN_IF_STMT;
11549
11550 /* Outside a template, the non-selected branch of a constexpr
11551 if is a 'discarded statement', i.e. unevaluated. */
11552 bool was_discarded = in_discarded_stmt;
11553 bool discard_then = (cx && !processing_template_decl
11554 && integer_zerop (condition));
11555 if (discard_then)
11556 {
11557 in_discarded_stmt = true;
11558 ++c_inhibit_evaluation_warnings;
11559 }
11560
11561 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11562 guard_tinfo);
11563
11564 parser->in_statement = in_statement;
11565
11566 finish_then_clause (statement);
11567
11568 if (discard_then)
11569 {
11570 THEN_CLAUSE (statement) = NULL_TREE;
11571 in_discarded_stmt = was_discarded;
11572 --c_inhibit_evaluation_warnings;
11573 }
11574
11575 /* If the next token is `else', parse the else-clause. */
11576 if (cp_lexer_next_token_is_keyword (parser->lexer,
11577 RID_ELSE))
11578 {
11579 bool discard_else = (cx && !processing_template_decl
11580 && integer_nonzerop (condition));
11581 if (discard_else)
11582 {
11583 in_discarded_stmt = true;
11584 ++c_inhibit_evaluation_warnings;
11585 }
11586
11587 guard_tinfo
11588 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11589 /* Consume the `else' keyword. */
11590 cp_lexer_consume_token (parser->lexer);
11591 if (warn_duplicated_cond)
11592 {
11593 if (cp_lexer_next_token_is_keyword (parser->lexer,
11594 RID_IF)
11595 && chain == NULL)
11596 {
11597 /* We've got "if (COND) else if (COND2)". Start
11598 the condition chain and add COND as the first
11599 element. */
11600 chain = new vec<tree> ();
11601 if (!CONSTANT_CLASS_P (condition)
11602 && !TREE_SIDE_EFFECTS (condition))
11603 {
11604 /* Wrap it in a NOP_EXPR so that we can set the
11605 location of the condition. */
11606 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11607 condition);
11608 SET_EXPR_LOCATION (e, token->location);
11609 chain->safe_push (e);
11610 }
11611 }
11612 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11613 RID_IF))
11614 {
11615 /* This is if-else without subsequent if. Zap the
11616 condition chain; we would have already warned at
11617 this point. */
11618 delete chain;
11619 chain = NULL;
11620 }
11621 }
11622 begin_else_clause (statement);
11623 /* Parse the else-clause. */
11624 cp_parser_implicitly_scoped_statement (parser, NULL,
11625 guard_tinfo, chain);
11626
11627 finish_else_clause (statement);
11628
11629 /* If we are currently parsing a then-clause, then
11630 IF_P will not be NULL. We set it to true to
11631 indicate that this if statement has an else clause.
11632 This may trigger the Wparentheses warning below
11633 when we get back up to the parent if statement. */
11634 if (if_p != NULL)
11635 *if_p = true;
11636
11637 if (discard_else)
11638 {
11639 ELSE_CLAUSE (statement) = NULL_TREE;
11640 in_discarded_stmt = was_discarded;
11641 --c_inhibit_evaluation_warnings;
11642 }
11643 }
11644 else
11645 {
11646 /* This if statement does not have an else clause. If
11647 NESTED_IF is true, then the then-clause has an if
11648 statement which does have an else clause. We warn
11649 about the potential ambiguity. */
11650 if (nested_if)
11651 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11652 "suggest explicit braces to avoid ambiguous"
11653 " %<else%>");
11654 if (warn_duplicated_cond)
11655 {
11656 /* We don't need the condition chain anymore. */
11657 delete chain;
11658 chain = NULL;
11659 }
11660 }
11661
11662 /* Now we're all done with the if-statement. */
11663 finish_if_stmt (statement);
11664 }
11665 else
11666 {
11667 bool in_switch_statement_p;
11668 unsigned char in_statement;
11669
11670 /* Add the condition. */
11671 finish_switch_cond (condition, statement);
11672
11673 /* Parse the body of the switch-statement. */
11674 in_switch_statement_p = parser->in_switch_statement_p;
11675 in_statement = parser->in_statement;
11676 parser->in_switch_statement_p = true;
11677 parser->in_statement |= IN_SWITCH_STMT;
11678 cp_parser_implicitly_scoped_statement (parser, if_p,
11679 guard_tinfo);
11680 parser->in_switch_statement_p = in_switch_statement_p;
11681 parser->in_statement = in_statement;
11682
11683 /* Now we're all done with the switch-statement. */
11684 finish_switch_stmt (statement);
11685 }
11686
11687 return statement;
11688 }
11689 break;
11690
11691 default:
11692 cp_parser_error (parser, "expected selection-statement");
11693 return error_mark_node;
11694 }
11695 }
11696
11697 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11698 If we have seen at least one decl-specifier, and the next token
11699 is not a parenthesis, then we must be looking at a declaration.
11700 (After "int (" we might be looking at a functional cast.) */
11701
11702 static void
11703 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11704 bool any_specifiers_p)
11705 {
11706 if (any_specifiers_p
11707 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11708 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11709 && !cp_parser_error_occurred (parser))
11710 cp_parser_commit_to_tentative_parse (parser);
11711 }
11712
11713 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11714 The declarator shall not specify a function or an array. Returns
11715 TRUE if the declarator is valid, FALSE otherwise. */
11716
11717 static bool
11718 cp_parser_check_condition_declarator (cp_parser* parser,
11719 cp_declarator *declarator,
11720 location_t loc)
11721 {
11722 if (declarator == cp_error_declarator
11723 || function_declarator_p (declarator)
11724 || declarator->kind == cdk_array)
11725 {
11726 if (declarator == cp_error_declarator)
11727 /* Already complained. */;
11728 else if (declarator->kind == cdk_array)
11729 error_at (loc, "condition declares an array");
11730 else
11731 error_at (loc, "condition declares a function");
11732 if (parser->fully_implicit_function_template_p)
11733 abort_fully_implicit_template (parser);
11734 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11735 /*or_comma=*/false,
11736 /*consume_paren=*/false);
11737 return false;
11738 }
11739 else
11740 return true;
11741 }
11742
11743 /* Parse a condition.
11744
11745 condition:
11746 expression
11747 type-specifier-seq declarator = initializer-clause
11748 type-specifier-seq declarator braced-init-list
11749
11750 GNU Extension:
11751
11752 condition:
11753 type-specifier-seq declarator asm-specification [opt]
11754 attributes [opt] = assignment-expression
11755
11756 Returns the expression that should be tested. */
11757
11758 static tree
11759 cp_parser_condition (cp_parser* parser)
11760 {
11761 cp_decl_specifier_seq type_specifiers;
11762 const char *saved_message;
11763 int declares_class_or_enum;
11764
11765 /* Try the declaration first. */
11766 cp_parser_parse_tentatively (parser);
11767 /* New types are not allowed in the type-specifier-seq for a
11768 condition. */
11769 saved_message = parser->type_definition_forbidden_message;
11770 parser->type_definition_forbidden_message
11771 = G_("types may not be defined in conditions");
11772 /* Parse the type-specifier-seq. */
11773 cp_parser_decl_specifier_seq (parser,
11774 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11775 &type_specifiers,
11776 &declares_class_or_enum);
11777 /* Restore the saved message. */
11778 parser->type_definition_forbidden_message = saved_message;
11779
11780 cp_parser_maybe_commit_to_declaration (parser,
11781 type_specifiers.any_specifiers_p);
11782
11783 /* If all is well, we might be looking at a declaration. */
11784 if (!cp_parser_error_occurred (parser))
11785 {
11786 tree decl;
11787 tree asm_specification;
11788 tree attributes;
11789 cp_declarator *declarator;
11790 tree initializer = NULL_TREE;
11791 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11792
11793 /* Parse the declarator. */
11794 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11795 /*ctor_dtor_or_conv_p=*/NULL,
11796 /*parenthesized_p=*/NULL,
11797 /*member_p=*/false,
11798 /*friend_p=*/false);
11799 /* Parse the attributes. */
11800 attributes = cp_parser_attributes_opt (parser);
11801 /* Parse the asm-specification. */
11802 asm_specification = cp_parser_asm_specification_opt (parser);
11803 /* If the next token is not an `=' or '{', then we might still be
11804 looking at an expression. For example:
11805
11806 if (A(a).x)
11807
11808 looks like a decl-specifier-seq and a declarator -- but then
11809 there is no `=', so this is an expression. */
11810 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11811 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11812 cp_parser_simulate_error (parser);
11813
11814 /* If we did see an `=' or '{', then we are looking at a declaration
11815 for sure. */
11816 if (cp_parser_parse_definitely (parser))
11817 {
11818 tree pushed_scope;
11819 bool non_constant_p = false;
11820 int flags = LOOKUP_ONLYCONVERTING;
11821
11822 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11823 return error_mark_node;
11824
11825 /* Create the declaration. */
11826 decl = start_decl (declarator, &type_specifiers,
11827 /*initialized_p=*/true,
11828 attributes, /*prefix_attributes=*/NULL_TREE,
11829 &pushed_scope);
11830
11831 /* Parse the initializer. */
11832 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11833 {
11834 initializer = cp_parser_braced_list (parser, &non_constant_p);
11835 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11836 flags = 0;
11837 }
11838 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11839 {
11840 /* Consume the `='. */
11841 cp_lexer_consume_token (parser->lexer);
11842 initializer = cp_parser_initializer_clause (parser,
11843 &non_constant_p);
11844 }
11845 else
11846 {
11847 cp_parser_error (parser, "expected initializer");
11848 initializer = error_mark_node;
11849 }
11850 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11851 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11852
11853 /* Process the initializer. */
11854 cp_finish_decl (decl,
11855 initializer, !non_constant_p,
11856 asm_specification,
11857 flags);
11858
11859 if (pushed_scope)
11860 pop_scope (pushed_scope);
11861
11862 return convert_from_reference (decl);
11863 }
11864 }
11865 /* If we didn't even get past the declarator successfully, we are
11866 definitely not looking at a declaration. */
11867 else
11868 cp_parser_abort_tentative_parse (parser);
11869
11870 /* Otherwise, we are looking at an expression. */
11871 return cp_parser_expression (parser);
11872 }
11873
11874 /* Parses a for-statement or range-for-statement until the closing ')',
11875 not included. */
11876
11877 static tree
11878 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
11879 {
11880 tree init, scope, decl;
11881 bool is_range_for;
11882
11883 /* Begin the for-statement. */
11884 scope = begin_for_scope (&init);
11885
11886 /* Parse the initialization. */
11887 is_range_for = cp_parser_init_statement (parser, &decl);
11888
11889 if (is_range_for)
11890 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
11891 false);
11892 else
11893 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
11894 }
11895
11896 static tree
11897 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
11898 unsigned short unroll)
11899 {
11900 /* Normal for loop */
11901 tree condition = NULL_TREE;
11902 tree expression = NULL_TREE;
11903 tree stmt;
11904
11905 stmt = begin_for_stmt (scope, init);
11906 /* The init-statement has already been parsed in
11907 cp_parser_init_statement, so no work is needed here. */
11908 finish_init_stmt (stmt);
11909
11910 /* If there's a condition, process it. */
11911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11912 condition = cp_parser_condition (parser);
11913 else if (ivdep)
11914 {
11915 cp_parser_error (parser, "missing loop condition in loop with "
11916 "%<GCC ivdep%> pragma");
11917 condition = error_mark_node;
11918 }
11919 else if (unroll)
11920 {
11921 cp_parser_error (parser, "missing loop condition in loop with "
11922 "%<GCC unroll%> pragma");
11923 condition = error_mark_node;
11924 }
11925 finish_for_cond (condition, stmt, ivdep, unroll);
11926 /* Look for the `;'. */
11927 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11928
11929 /* If there's an expression, process it. */
11930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11931 expression = cp_parser_expression (parser);
11932 finish_for_expr (expression, stmt);
11933
11934 return stmt;
11935 }
11936
11937 /* Tries to parse a range-based for-statement:
11938
11939 range-based-for:
11940 decl-specifier-seq declarator : expression
11941
11942 The decl-specifier-seq declarator and the `:' are already parsed by
11943 cp_parser_init_statement. If processing_template_decl it returns a
11944 newly created RANGE_FOR_STMT; if not, it is converted to a
11945 regular FOR_STMT. */
11946
11947 static tree
11948 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11949 bool ivdep, unsigned short unroll, bool is_omp)
11950 {
11951 tree stmt, range_expr;
11952 auto_vec <cxx_binding *, 16> bindings;
11953 auto_vec <tree, 16> names;
11954 tree decomp_first_name = NULL_TREE;
11955 unsigned int decomp_cnt = 0;
11956
11957 /* Get the range declaration momentarily out of the way so that
11958 the range expression doesn't clash with it. */
11959 if (range_decl != error_mark_node)
11960 {
11961 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11962 {
11963 tree v = DECL_VALUE_EXPR (range_decl);
11964 /* For decomposition declaration get all of the corresponding
11965 declarations out of the way. */
11966 if (TREE_CODE (v) == ARRAY_REF
11967 && VAR_P (TREE_OPERAND (v, 0))
11968 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11969 {
11970 tree d = range_decl;
11971 range_decl = TREE_OPERAND (v, 0);
11972 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11973 decomp_first_name = d;
11974 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11975 {
11976 tree name = DECL_NAME (d);
11977 names.safe_push (name);
11978 bindings.safe_push (IDENTIFIER_BINDING (name));
11979 IDENTIFIER_BINDING (name)
11980 = IDENTIFIER_BINDING (name)->previous;
11981 }
11982 }
11983 }
11984 if (names.is_empty ())
11985 {
11986 tree name = DECL_NAME (range_decl);
11987 names.safe_push (name);
11988 bindings.safe_push (IDENTIFIER_BINDING (name));
11989 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11990 }
11991 }
11992
11993 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11994 {
11995 bool expr_non_constant_p;
11996 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11997 }
11998 else
11999 range_expr = cp_parser_expression (parser);
12000
12001 /* Put the range declaration(s) back into scope. */
12002 for (unsigned int i = 0; i < names.length (); i++)
12003 {
12004 cxx_binding *binding = bindings[i];
12005 binding->previous = IDENTIFIER_BINDING (names[i]);
12006 IDENTIFIER_BINDING (names[i]) = binding;
12007 }
12008
12009 /* finish_omp_for has its own code for the following, so just
12010 return the range_expr instead. */
12011 if (is_omp)
12012 return range_expr;
12013
12014 /* If in template, STMT is converted to a normal for-statement
12015 at instantiation. If not, it is done just ahead. */
12016 if (processing_template_decl)
12017 {
12018 if (check_for_bare_parameter_packs (range_expr))
12019 range_expr = error_mark_node;
12020 stmt = begin_range_for_stmt (scope, init);
12021 if (ivdep)
12022 RANGE_FOR_IVDEP (stmt) = 1;
12023 if (unroll)
12024 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12025 finish_range_for_decl (stmt, range_decl, range_expr);
12026 if (!type_dependent_expression_p (range_expr)
12027 /* do_auto_deduction doesn't mess with template init-lists. */
12028 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12029 do_range_for_auto_deduction (range_decl, range_expr);
12030 }
12031 else
12032 {
12033 stmt = begin_for_stmt (scope, init);
12034 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12035 decomp_first_name, decomp_cnt, ivdep,
12036 unroll);
12037 }
12038 return stmt;
12039 }
12040
12041 /* Subroutine of cp_convert_range_for: given the initializer expression,
12042 builds up the range temporary. */
12043
12044 static tree
12045 build_range_temp (tree range_expr)
12046 {
12047 tree range_type, range_temp;
12048
12049 /* Find out the type deduced by the declaration
12050 `auto &&__range = range_expr'. */
12051 range_type = cp_build_reference_type (make_auto (), true);
12052 range_type = do_auto_deduction (range_type, range_expr,
12053 type_uses_auto (range_type));
12054
12055 /* Create the __range variable. */
12056 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12057 range_type);
12058 TREE_USED (range_temp) = 1;
12059 DECL_ARTIFICIAL (range_temp) = 1;
12060
12061 return range_temp;
12062 }
12063
12064 /* Used by cp_parser_range_for in template context: we aren't going to
12065 do a full conversion yet, but we still need to resolve auto in the
12066 type of the for-range-declaration if present. This is basically
12067 a shortcut version of cp_convert_range_for. */
12068
12069 static void
12070 do_range_for_auto_deduction (tree decl, tree range_expr)
12071 {
12072 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12073 if (auto_node)
12074 {
12075 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12076 range_temp = convert_from_reference (build_range_temp (range_expr));
12077 iter_type = (cp_parser_perform_range_for_lookup
12078 (range_temp, &begin_dummy, &end_dummy));
12079 if (iter_type)
12080 {
12081 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12082 iter_type);
12083 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12084 RO_UNARY_STAR,
12085 tf_warning_or_error);
12086 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12087 iter_decl, auto_node);
12088 }
12089 }
12090 }
12091
12092 /* Converts a range-based for-statement into a normal
12093 for-statement, as per the definition.
12094
12095 for (RANGE_DECL : RANGE_EXPR)
12096 BLOCK
12097
12098 should be equivalent to:
12099
12100 {
12101 auto &&__range = RANGE_EXPR;
12102 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12103 __begin != __end;
12104 ++__begin)
12105 {
12106 RANGE_DECL = *__begin;
12107 BLOCK
12108 }
12109 }
12110
12111 If RANGE_EXPR is an array:
12112 BEGIN_EXPR = __range
12113 END_EXPR = __range + ARRAY_SIZE(__range)
12114 Else if RANGE_EXPR has a member 'begin' or 'end':
12115 BEGIN_EXPR = __range.begin()
12116 END_EXPR = __range.end()
12117 Else:
12118 BEGIN_EXPR = begin(__range)
12119 END_EXPR = end(__range);
12120
12121 If __range has a member 'begin' but not 'end', or vice versa, we must
12122 still use the second alternative (it will surely fail, however).
12123 When calling begin()/end() in the third alternative we must use
12124 argument dependent lookup, but always considering 'std' as an associated
12125 namespace. */
12126
12127 tree
12128 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12129 tree decomp_first_name, unsigned int decomp_cnt,
12130 bool ivdep, unsigned short unroll)
12131 {
12132 tree begin, end;
12133 tree iter_type, begin_expr, end_expr;
12134 tree condition, expression;
12135
12136 range_expr = mark_lvalue_use (range_expr);
12137
12138 if (range_decl == error_mark_node || range_expr == error_mark_node)
12139 /* If an error happened previously do nothing or else a lot of
12140 unhelpful errors would be issued. */
12141 begin_expr = end_expr = iter_type = error_mark_node;
12142 else
12143 {
12144 tree range_temp;
12145
12146 if (VAR_P (range_expr)
12147 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12148 /* Can't bind a reference to an array of runtime bound. */
12149 range_temp = range_expr;
12150 else
12151 {
12152 range_temp = build_range_temp (range_expr);
12153 pushdecl (range_temp);
12154 cp_finish_decl (range_temp, range_expr,
12155 /*is_constant_init*/false, NULL_TREE,
12156 LOOKUP_ONLYCONVERTING);
12157 range_temp = convert_from_reference (range_temp);
12158 }
12159 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12160 &begin_expr, &end_expr);
12161 }
12162
12163 /* The new for initialization statement. */
12164 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12165 iter_type);
12166 TREE_USED (begin) = 1;
12167 DECL_ARTIFICIAL (begin) = 1;
12168 pushdecl (begin);
12169 cp_finish_decl (begin, begin_expr,
12170 /*is_constant_init*/false, NULL_TREE,
12171 LOOKUP_ONLYCONVERTING);
12172
12173 if (cxx_dialect >= cxx17)
12174 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12175 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12176 TREE_USED (end) = 1;
12177 DECL_ARTIFICIAL (end) = 1;
12178 pushdecl (end);
12179 cp_finish_decl (end, end_expr,
12180 /*is_constant_init*/false, NULL_TREE,
12181 LOOKUP_ONLYCONVERTING);
12182
12183 finish_init_stmt (statement);
12184
12185 /* The new for condition. */
12186 condition = build_x_binary_op (input_location, NE_EXPR,
12187 begin, ERROR_MARK,
12188 end, ERROR_MARK,
12189 NULL, tf_warning_or_error);
12190 finish_for_cond (condition, statement, ivdep, unroll);
12191
12192 /* The new increment expression. */
12193 expression = finish_unary_op_expr (input_location,
12194 PREINCREMENT_EXPR, begin,
12195 tf_warning_or_error);
12196 finish_for_expr (expression, statement);
12197
12198 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12199 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12200
12201 /* The declaration is initialized with *__begin inside the loop body. */
12202 cp_finish_decl (range_decl,
12203 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12204 tf_warning_or_error),
12205 /*is_constant_init*/false, NULL_TREE,
12206 LOOKUP_ONLYCONVERTING);
12207 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12208 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12209
12210 return statement;
12211 }
12212
12213 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12214 We need to solve both at the same time because the method used
12215 depends on the existence of members begin or end.
12216 Returns the type deduced for the iterator expression. */
12217
12218 static tree
12219 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12220 {
12221 if (error_operand_p (range))
12222 {
12223 *begin = *end = error_mark_node;
12224 return error_mark_node;
12225 }
12226
12227 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12228 {
12229 error ("range-based %<for%> expression of type %qT "
12230 "has incomplete type", TREE_TYPE (range));
12231 *begin = *end = error_mark_node;
12232 return error_mark_node;
12233 }
12234 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12235 {
12236 /* If RANGE is an array, we will use pointer arithmetic. */
12237 *begin = decay_conversion (range, tf_warning_or_error);
12238 *end = build_binary_op (input_location, PLUS_EXPR,
12239 range,
12240 array_type_nelts_top (TREE_TYPE (range)),
12241 false);
12242 return TREE_TYPE (*begin);
12243 }
12244 else
12245 {
12246 /* If it is not an array, we must do a bit of magic. */
12247 tree id_begin, id_end;
12248 tree member_begin, member_end;
12249
12250 *begin = *end = error_mark_node;
12251
12252 id_begin = get_identifier ("begin");
12253 id_end = get_identifier ("end");
12254 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12255 /*protect=*/2, /*want_type=*/false,
12256 tf_warning_or_error);
12257 member_end = lookup_member (TREE_TYPE (range), id_end,
12258 /*protect=*/2, /*want_type=*/false,
12259 tf_warning_or_error);
12260
12261 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12262 {
12263 /* Use the member functions. */
12264 *begin = cp_parser_range_for_member_function (range, id_begin);
12265 *end = cp_parser_range_for_member_function (range, id_end);
12266 }
12267 else
12268 {
12269 /* Use global functions with ADL. */
12270 vec<tree, va_gc> *vec;
12271 vec = make_tree_vector ();
12272
12273 vec_safe_push (vec, range);
12274
12275 member_begin = perform_koenig_lookup (id_begin, vec,
12276 tf_warning_or_error);
12277 *begin = finish_call_expr (member_begin, &vec, false, true,
12278 tf_warning_or_error);
12279 member_end = perform_koenig_lookup (id_end, vec,
12280 tf_warning_or_error);
12281 *end = finish_call_expr (member_end, &vec, false, true,
12282 tf_warning_or_error);
12283
12284 release_tree_vector (vec);
12285 }
12286
12287 /* Last common checks. */
12288 if (*begin == error_mark_node || *end == error_mark_node)
12289 {
12290 /* If one of the expressions is an error do no more checks. */
12291 *begin = *end = error_mark_node;
12292 return error_mark_node;
12293 }
12294 else if (type_dependent_expression_p (*begin)
12295 || type_dependent_expression_p (*end))
12296 /* Can happen, when, eg, in a template context, Koenig lookup
12297 can't resolve begin/end (c++/58503). */
12298 return NULL_TREE;
12299 else
12300 {
12301 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12302 /* The unqualified type of the __begin and __end temporaries should
12303 be the same, as required by the multiple auto declaration. */
12304 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12305 {
12306 if (cxx_dialect >= cxx17
12307 && (build_x_binary_op (input_location, NE_EXPR,
12308 *begin, ERROR_MARK,
12309 *end, ERROR_MARK,
12310 NULL, tf_none)
12311 != error_mark_node))
12312 /* P0184R0 allows __begin and __end to have different types,
12313 but make sure they are comparable so we can give a better
12314 diagnostic. */;
12315 else
12316 error ("inconsistent begin/end types in range-based %<for%> "
12317 "statement: %qT and %qT",
12318 TREE_TYPE (*begin), TREE_TYPE (*end));
12319 }
12320 return iter_type;
12321 }
12322 }
12323 }
12324
12325 /* Helper function for cp_parser_perform_range_for_lookup.
12326 Builds a tree for RANGE.IDENTIFIER(). */
12327
12328 static tree
12329 cp_parser_range_for_member_function (tree range, tree identifier)
12330 {
12331 tree member, res;
12332 vec<tree, va_gc> *vec;
12333
12334 member = finish_class_member_access_expr (range, identifier,
12335 false, tf_warning_or_error);
12336 if (member == error_mark_node)
12337 return error_mark_node;
12338
12339 vec = make_tree_vector ();
12340 res = finish_call_expr (member, &vec,
12341 /*disallow_virtual=*/false,
12342 /*koenig_p=*/false,
12343 tf_warning_or_error);
12344 release_tree_vector (vec);
12345 return res;
12346 }
12347
12348 /* Parse an iteration-statement.
12349
12350 iteration-statement:
12351 while ( condition ) statement
12352 do statement while ( expression ) ;
12353 for ( init-statement condition [opt] ; expression [opt] )
12354 statement
12355
12356 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12357
12358 static tree
12359 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12360 unsigned short unroll)
12361 {
12362 cp_token *token;
12363 enum rid keyword;
12364 tree statement;
12365 unsigned char in_statement;
12366 token_indent_info guard_tinfo;
12367
12368 /* Peek at the next token. */
12369 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12370 if (!token)
12371 return error_mark_node;
12372
12373 guard_tinfo = get_token_indent_info (token);
12374
12375 /* Remember whether or not we are already within an iteration
12376 statement. */
12377 in_statement = parser->in_statement;
12378
12379 /* See what kind of keyword it is. */
12380 keyword = token->keyword;
12381 switch (keyword)
12382 {
12383 case RID_WHILE:
12384 {
12385 tree condition;
12386
12387 /* Begin the while-statement. */
12388 statement = begin_while_stmt ();
12389 /* Look for the `('. */
12390 matching_parens parens;
12391 parens.require_open (parser);
12392 /* Parse the condition. */
12393 condition = cp_parser_condition (parser);
12394 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12395 /* Look for the `)'. */
12396 parens.require_close (parser);
12397 /* Parse the dependent statement. */
12398 parser->in_statement = IN_ITERATION_STMT;
12399 bool prev = note_iteration_stmt_body_start ();
12400 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12401 note_iteration_stmt_body_end (prev);
12402 parser->in_statement = in_statement;
12403 /* We're done with the while-statement. */
12404 finish_while_stmt (statement);
12405 }
12406 break;
12407
12408 case RID_DO:
12409 {
12410 tree expression;
12411
12412 /* Begin the do-statement. */
12413 statement = begin_do_stmt ();
12414 /* Parse the body of the do-statement. */
12415 parser->in_statement = IN_ITERATION_STMT;
12416 bool prev = note_iteration_stmt_body_start ();
12417 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12418 note_iteration_stmt_body_end (prev);
12419 parser->in_statement = in_statement;
12420 finish_do_body (statement);
12421 /* Look for the `while' keyword. */
12422 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12423 /* Look for the `('. */
12424 matching_parens parens;
12425 parens.require_open (parser);
12426 /* Parse the expression. */
12427 expression = cp_parser_expression (parser);
12428 /* We're done with the do-statement. */
12429 finish_do_stmt (expression, statement, ivdep, unroll);
12430 /* Look for the `)'. */
12431 parens.require_close (parser);
12432 /* Look for the `;'. */
12433 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12434 }
12435 break;
12436
12437 case RID_FOR:
12438 {
12439 /* Look for the `('. */
12440 matching_parens parens;
12441 parens.require_open (parser);
12442
12443 statement = cp_parser_for (parser, ivdep, unroll);
12444
12445 /* Look for the `)'. */
12446 parens.require_close (parser);
12447
12448 /* Parse the body of the for-statement. */
12449 parser->in_statement = IN_ITERATION_STMT;
12450 bool prev = note_iteration_stmt_body_start ();
12451 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12452 note_iteration_stmt_body_end (prev);
12453 parser->in_statement = in_statement;
12454
12455 /* We're done with the for-statement. */
12456 finish_for_stmt (statement);
12457 }
12458 break;
12459
12460 default:
12461 cp_parser_error (parser, "expected iteration-statement");
12462 statement = error_mark_node;
12463 break;
12464 }
12465
12466 return statement;
12467 }
12468
12469 /* Parse a init-statement or the declarator of a range-based-for.
12470 Returns true if a range-based-for declaration is seen.
12471
12472 init-statement:
12473 expression-statement
12474 simple-declaration */
12475
12476 static bool
12477 cp_parser_init_statement (cp_parser *parser, tree *decl)
12478 {
12479 /* If the next token is a `;', then we have an empty
12480 expression-statement. Grammatically, this is also a
12481 simple-declaration, but an invalid one, because it does not
12482 declare anything. Therefore, if we did not handle this case
12483 specially, we would issue an error message about an invalid
12484 declaration. */
12485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12486 {
12487 bool is_range_for = false;
12488 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12489
12490 /* Try to parse the init-statement. */
12491 if (cp_parser_range_based_for_with_init_p (parser))
12492 {
12493 tree dummy;
12494 cp_parser_parse_tentatively (parser);
12495 /* Parse the declaration. */
12496 cp_parser_simple_declaration (parser,
12497 /*function_definition_allowed_p=*/false,
12498 &dummy);
12499 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12500 if (!cp_parser_parse_definitely (parser))
12501 /* That didn't work, try to parse it as an expression-statement. */
12502 cp_parser_expression_statement (parser, NULL_TREE);
12503
12504 if (cxx_dialect < cxx2a)
12505 {
12506 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12507 "range-based %<for%> loops with initializer only "
12508 "available with -std=c++2a or -std=gnu++2a");
12509 *decl = error_mark_node;
12510 }
12511 }
12512
12513 /* A colon is used in range-based for. */
12514 parser->colon_corrects_to_scope_p = false;
12515
12516 /* We're going to speculatively look for a declaration, falling back
12517 to an expression, if necessary. */
12518 cp_parser_parse_tentatively (parser);
12519 /* Parse the declaration. */
12520 cp_parser_simple_declaration (parser,
12521 /*function_definition_allowed_p=*/false,
12522 decl);
12523 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12524 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12525 {
12526 /* It is a range-for, consume the ':'. */
12527 cp_lexer_consume_token (parser->lexer);
12528 is_range_for = true;
12529 if (cxx_dialect < cxx11)
12530 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12531 "range-based %<for%> loops only available with "
12532 "-std=c++11 or -std=gnu++11");
12533 }
12534 else
12535 /* The ';' is not consumed yet because we told
12536 cp_parser_simple_declaration not to. */
12537 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12538
12539 if (cp_parser_parse_definitely (parser))
12540 return is_range_for;
12541 /* If the tentative parse failed, then we shall need to look for an
12542 expression-statement. */
12543 }
12544 /* If we are here, it is an expression-statement. */
12545 cp_parser_expression_statement (parser, NULL_TREE);
12546 return false;
12547 }
12548
12549 /* Parse a jump-statement.
12550
12551 jump-statement:
12552 break ;
12553 continue ;
12554 return expression [opt] ;
12555 return braced-init-list ;
12556 goto identifier ;
12557
12558 GNU extension:
12559
12560 jump-statement:
12561 goto * expression ;
12562
12563 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12564
12565 static tree
12566 cp_parser_jump_statement (cp_parser* parser)
12567 {
12568 tree statement = error_mark_node;
12569 cp_token *token;
12570 enum rid keyword;
12571 unsigned char in_statement;
12572
12573 /* Peek at the next token. */
12574 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12575 if (!token)
12576 return error_mark_node;
12577
12578 /* See what kind of keyword it is. */
12579 keyword = token->keyword;
12580 switch (keyword)
12581 {
12582 case RID_BREAK:
12583 in_statement = parser->in_statement & ~IN_IF_STMT;
12584 switch (in_statement)
12585 {
12586 case 0:
12587 error_at (token->location, "break statement not within loop or switch");
12588 break;
12589 default:
12590 gcc_assert ((in_statement & IN_SWITCH_STMT)
12591 || in_statement == IN_ITERATION_STMT);
12592 statement = finish_break_stmt ();
12593 if (in_statement == IN_ITERATION_STMT)
12594 break_maybe_infinite_loop ();
12595 break;
12596 case IN_OMP_BLOCK:
12597 error_at (token->location, "invalid exit from OpenMP structured block");
12598 break;
12599 case IN_OMP_FOR:
12600 error_at (token->location, "break statement used with OpenMP for loop");
12601 break;
12602 }
12603 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12604 break;
12605
12606 case RID_CONTINUE:
12607 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12608 {
12609 case 0:
12610 error_at (token->location, "continue statement not within a loop");
12611 break;
12612 /* Fall through. */
12613 case IN_ITERATION_STMT:
12614 case IN_OMP_FOR:
12615 statement = finish_continue_stmt ();
12616 break;
12617 case IN_OMP_BLOCK:
12618 error_at (token->location, "invalid exit from OpenMP structured block");
12619 break;
12620 default:
12621 gcc_unreachable ();
12622 }
12623 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12624 break;
12625
12626 case RID_RETURN:
12627 {
12628 tree expr;
12629 bool expr_non_constant_p;
12630
12631 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12632 {
12633 cp_lexer_set_source_position (parser->lexer);
12634 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12635 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12636 }
12637 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12638 expr = cp_parser_expression (parser);
12639 else
12640 /* If the next token is a `;', then there is no
12641 expression. */
12642 expr = NULL_TREE;
12643 /* Build the return-statement. */
12644 if (current_function_auto_return_pattern && in_discarded_stmt)
12645 /* Don't deduce from a discarded return statement. */;
12646 else
12647 statement = finish_return_stmt (expr);
12648 /* Look for the final `;'. */
12649 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12650 }
12651 break;
12652
12653 case RID_GOTO:
12654 if (parser->in_function_body
12655 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12656 {
12657 error ("%<goto%> in %<constexpr%> function");
12658 cp_function_chain->invalid_constexpr = true;
12659 }
12660
12661 /* Create the goto-statement. */
12662 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12663 {
12664 /* Issue a warning about this use of a GNU extension. */
12665 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12666 /* Consume the '*' token. */
12667 cp_lexer_consume_token (parser->lexer);
12668 /* Parse the dependent expression. */
12669 finish_goto_stmt (cp_parser_expression (parser));
12670 }
12671 else
12672 finish_goto_stmt (cp_parser_identifier (parser));
12673 /* Look for the final `;'. */
12674 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12675 break;
12676
12677 default:
12678 cp_parser_error (parser, "expected jump-statement");
12679 break;
12680 }
12681
12682 return statement;
12683 }
12684
12685 /* Parse a declaration-statement.
12686
12687 declaration-statement:
12688 block-declaration */
12689
12690 static void
12691 cp_parser_declaration_statement (cp_parser* parser)
12692 {
12693 void *p;
12694
12695 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12696 p = obstack_alloc (&declarator_obstack, 0);
12697
12698 /* Parse the block-declaration. */
12699 cp_parser_block_declaration (parser, /*statement_p=*/true);
12700
12701 /* Free any declarators allocated. */
12702 obstack_free (&declarator_obstack, p);
12703 }
12704
12705 /* Some dependent statements (like `if (cond) statement'), are
12706 implicitly in their own scope. In other words, if the statement is
12707 a single statement (as opposed to a compound-statement), it is
12708 none-the-less treated as if it were enclosed in braces. Any
12709 declarations appearing in the dependent statement are out of scope
12710 after control passes that point. This function parses a statement,
12711 but ensures that is in its own scope, even if it is not a
12712 compound-statement.
12713
12714 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12715 is a (possibly labeled) if statement which is not enclosed in
12716 braces and has an else clause. This is used to implement
12717 -Wparentheses.
12718
12719 CHAIN is a vector of if-else-if conditions. This is used to implement
12720 -Wduplicated-cond.
12721
12722 Returns the new statement. */
12723
12724 static tree
12725 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12726 const token_indent_info &guard_tinfo,
12727 vec<tree> *chain)
12728 {
12729 tree statement;
12730 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12731 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12732 token_indent_info body_tinfo
12733 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12734
12735 if (if_p != NULL)
12736 *if_p = false;
12737
12738 /* Mark if () ; with a special NOP_EXPR. */
12739 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12740 {
12741 cp_lexer_consume_token (parser->lexer);
12742 statement = add_stmt (build_empty_stmt (body_loc));
12743
12744 if (guard_tinfo.keyword == RID_IF
12745 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12746 warning_at (body_loc, OPT_Wempty_body,
12747 "suggest braces around empty body in an %<if%> statement");
12748 else if (guard_tinfo.keyword == RID_ELSE)
12749 warning_at (body_loc, OPT_Wempty_body,
12750 "suggest braces around empty body in an %<else%> statement");
12751 }
12752 /* if a compound is opened, we simply parse the statement directly. */
12753 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12754 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12755 /* If the token is not a `{', then we must take special action. */
12756 else
12757 {
12758 /* Create a compound-statement. */
12759 statement = begin_compound_stmt (0);
12760 /* Parse the dependent-statement. */
12761 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12762 &body_loc_after_labels);
12763 /* Finish the dummy compound-statement. */
12764 finish_compound_stmt (statement);
12765 }
12766
12767 token_indent_info next_tinfo
12768 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12769 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12770
12771 if (body_loc_after_labels != UNKNOWN_LOCATION
12772 && next_tinfo.type != CPP_SEMICOLON)
12773 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12774 guard_tinfo.location, guard_tinfo.keyword);
12775
12776 /* Return the statement. */
12777 return statement;
12778 }
12779
12780 /* For some dependent statements (like `while (cond) statement'), we
12781 have already created a scope. Therefore, even if the dependent
12782 statement is a compound-statement, we do not want to create another
12783 scope. */
12784
12785 static void
12786 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12787 const token_indent_info &guard_tinfo)
12788 {
12789 /* If the token is a `{', then we must take special action. */
12790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12791 {
12792 token_indent_info body_tinfo
12793 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12794 location_t loc_after_labels = UNKNOWN_LOCATION;
12795
12796 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12797 &loc_after_labels);
12798 token_indent_info next_tinfo
12799 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12800 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12801
12802 if (loc_after_labels != UNKNOWN_LOCATION
12803 && next_tinfo.type != CPP_SEMICOLON)
12804 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12805 guard_tinfo.location,
12806 guard_tinfo.keyword);
12807 }
12808 else
12809 {
12810 /* Avoid calling cp_parser_compound_statement, so that we
12811 don't create a new scope. Do everything else by hand. */
12812 matching_braces braces;
12813 braces.require_open (parser);
12814 /* If the next keyword is `__label__' we have a label declaration. */
12815 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12816 cp_parser_label_declaration (parser);
12817 /* Parse an (optional) statement-seq. */
12818 cp_parser_statement_seq_opt (parser, NULL_TREE);
12819 braces.require_close (parser);
12820 }
12821 }
12822
12823 /* Declarations [gram.dcl.dcl] */
12824
12825 /* Parse an optional declaration-sequence.
12826
12827 declaration-seq:
12828 declaration
12829 declaration-seq declaration */
12830
12831 static void
12832 cp_parser_declaration_seq_opt (cp_parser* parser)
12833 {
12834 while (true)
12835 {
12836 cp_token *token = cp_lexer_peek_token (parser->lexer);
12837
12838 if (token->type == CPP_CLOSE_BRACE
12839 || token->type == CPP_EOF)
12840 break;
12841 else
12842 cp_parser_toplevel_declaration (parser);
12843 }
12844 }
12845
12846 /* Parse a declaration.
12847
12848 declaration:
12849 block-declaration
12850 function-definition
12851 template-declaration
12852 explicit-instantiation
12853 explicit-specialization
12854 linkage-specification
12855 namespace-definition
12856
12857 C++17:
12858 deduction-guide
12859
12860 GNU extension:
12861
12862 declaration:
12863 __extension__ declaration */
12864
12865 static void
12866 cp_parser_declaration (cp_parser* parser)
12867 {
12868 cp_token token1;
12869 cp_token token2;
12870 int saved_pedantic;
12871 void *p;
12872 tree attributes = NULL_TREE;
12873
12874 /* Check for the `__extension__' keyword. */
12875 if (cp_parser_extension_opt (parser, &saved_pedantic))
12876 {
12877 /* Parse the qualified declaration. */
12878 cp_parser_declaration (parser);
12879 /* Restore the PEDANTIC flag. */
12880 pedantic = saved_pedantic;
12881
12882 return;
12883 }
12884
12885 /* Try to figure out what kind of declaration is present. */
12886 token1 = *cp_lexer_peek_token (parser->lexer);
12887
12888 if (token1.type != CPP_EOF)
12889 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12890 else
12891 {
12892 token2.type = CPP_EOF;
12893 token2.keyword = RID_MAX;
12894 }
12895
12896 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12897 p = obstack_alloc (&declarator_obstack, 0);
12898
12899 /* If the next token is `extern' and the following token is a string
12900 literal, then we have a linkage specification. */
12901 if (token1.keyword == RID_EXTERN
12902 && cp_parser_is_pure_string_literal (&token2))
12903 cp_parser_linkage_specification (parser);
12904 /* If the next token is `template', then we have either a template
12905 declaration, an explicit instantiation, or an explicit
12906 specialization. */
12907 else if (token1.keyword == RID_TEMPLATE)
12908 {
12909 /* `template <>' indicates a template specialization. */
12910 if (token2.type == CPP_LESS
12911 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12912 cp_parser_explicit_specialization (parser);
12913 /* `template <' indicates a template declaration. */
12914 else if (token2.type == CPP_LESS)
12915 cp_parser_template_declaration (parser, /*member_p=*/false);
12916 /* Anything else must be an explicit instantiation. */
12917 else
12918 cp_parser_explicit_instantiation (parser);
12919 }
12920 /* If the next token is `export', then we have a template
12921 declaration. */
12922 else if (token1.keyword == RID_EXPORT)
12923 cp_parser_template_declaration (parser, /*member_p=*/false);
12924 /* If the next token is `extern', 'static' or 'inline' and the one
12925 after that is `template', we have a GNU extended explicit
12926 instantiation directive. */
12927 else if (cp_parser_allow_gnu_extensions_p (parser)
12928 && (token1.keyword == RID_EXTERN
12929 || token1.keyword == RID_STATIC
12930 || token1.keyword == RID_INLINE)
12931 && token2.keyword == RID_TEMPLATE)
12932 cp_parser_explicit_instantiation (parser);
12933 /* If the next token is `namespace', check for a named or unnamed
12934 namespace definition. */
12935 else if (token1.keyword == RID_NAMESPACE
12936 && (/* A named namespace definition. */
12937 (token2.type == CPP_NAME
12938 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12939 != CPP_EQ))
12940 || (token2.type == CPP_OPEN_SQUARE
12941 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12942 == CPP_OPEN_SQUARE)
12943 /* An unnamed namespace definition. */
12944 || token2.type == CPP_OPEN_BRACE
12945 || token2.keyword == RID_ATTRIBUTE))
12946 cp_parser_namespace_definition (parser);
12947 /* An inline (associated) namespace definition. */
12948 else if (token1.keyword == RID_INLINE
12949 && token2.keyword == RID_NAMESPACE)
12950 cp_parser_namespace_definition (parser);
12951 /* Objective-C++ declaration/definition. */
12952 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12953 cp_parser_objc_declaration (parser, NULL_TREE);
12954 else if (c_dialect_objc ()
12955 && token1.keyword == RID_ATTRIBUTE
12956 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12957 cp_parser_objc_declaration (parser, attributes);
12958 /* At this point we may have a template declared by a concept
12959 introduction. */
12960 else if (flag_concepts
12961 && cp_parser_template_declaration_after_export (parser,
12962 /*member_p=*/false))
12963 /* We did. */;
12964 else
12965 /* Try to parse a block-declaration, or a function-definition. */
12966 cp_parser_block_declaration (parser, /*statement_p=*/false);
12967
12968 /* Free any declarators allocated. */
12969 obstack_free (&declarator_obstack, p);
12970 }
12971
12972 /* Parse a namespace-scope declaration. */
12973
12974 static void
12975 cp_parser_toplevel_declaration (cp_parser* parser)
12976 {
12977 cp_token *token = cp_lexer_peek_token (parser->lexer);
12978
12979 if (token->type == CPP_PRAGMA)
12980 /* A top-level declaration can consist solely of a #pragma. A
12981 nested declaration cannot, so this is done here and not in
12982 cp_parser_declaration. (A #pragma at block scope is
12983 handled in cp_parser_statement.) */
12984 cp_parser_pragma (parser, pragma_external, NULL);
12985 else if (token->type == CPP_SEMICOLON)
12986 {
12987 /* A declaration consisting of a single semicolon is
12988 invalid. Allow it unless we're being pedantic. */
12989 cp_lexer_consume_token (parser->lexer);
12990 if (!in_system_header_at (input_location))
12991 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12992 }
12993 else
12994 /* Parse the declaration itself. */
12995 cp_parser_declaration (parser);
12996 }
12997
12998 /* Parse a block-declaration.
12999
13000 block-declaration:
13001 simple-declaration
13002 asm-definition
13003 namespace-alias-definition
13004 using-declaration
13005 using-directive
13006
13007 GNU Extension:
13008
13009 block-declaration:
13010 __extension__ block-declaration
13011
13012 C++0x Extension:
13013
13014 block-declaration:
13015 static_assert-declaration
13016
13017 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13018 part of a declaration-statement. */
13019
13020 static void
13021 cp_parser_block_declaration (cp_parser *parser,
13022 bool statement_p)
13023 {
13024 cp_token *token1;
13025 int saved_pedantic;
13026
13027 /* Check for the `__extension__' keyword. */
13028 if (cp_parser_extension_opt (parser, &saved_pedantic))
13029 {
13030 /* Parse the qualified declaration. */
13031 cp_parser_block_declaration (parser, statement_p);
13032 /* Restore the PEDANTIC flag. */
13033 pedantic = saved_pedantic;
13034
13035 return;
13036 }
13037
13038 /* Peek at the next token to figure out which kind of declaration is
13039 present. */
13040 token1 = cp_lexer_peek_token (parser->lexer);
13041
13042 /* If the next keyword is `asm', we have an asm-definition. */
13043 if (token1->keyword == RID_ASM)
13044 {
13045 if (statement_p)
13046 cp_parser_commit_to_tentative_parse (parser);
13047 cp_parser_asm_definition (parser);
13048 }
13049 /* If the next keyword is `namespace', we have a
13050 namespace-alias-definition. */
13051 else if (token1->keyword == RID_NAMESPACE)
13052 cp_parser_namespace_alias_definition (parser);
13053 /* If the next keyword is `using', we have a
13054 using-declaration, a using-directive, or an alias-declaration. */
13055 else if (token1->keyword == RID_USING)
13056 {
13057 cp_token *token2;
13058
13059 if (statement_p)
13060 cp_parser_commit_to_tentative_parse (parser);
13061 /* If the token after `using' is `namespace', then we have a
13062 using-directive. */
13063 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13064 if (token2->keyword == RID_NAMESPACE)
13065 cp_parser_using_directive (parser);
13066 /* If the second token after 'using' is '=', then we have an
13067 alias-declaration. */
13068 else if (cxx_dialect >= cxx11
13069 && token2->type == CPP_NAME
13070 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13071 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13072 cp_parser_alias_declaration (parser);
13073 /* Otherwise, it's a using-declaration. */
13074 else
13075 cp_parser_using_declaration (parser,
13076 /*access_declaration_p=*/false);
13077 }
13078 /* If the next keyword is `__label__' we have a misplaced label
13079 declaration. */
13080 else if (token1->keyword == RID_LABEL)
13081 {
13082 cp_lexer_consume_token (parser->lexer);
13083 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13084 cp_parser_skip_to_end_of_statement (parser);
13085 /* If the next token is now a `;', consume it. */
13086 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13087 cp_lexer_consume_token (parser->lexer);
13088 }
13089 /* If the next token is `static_assert' we have a static assertion. */
13090 else if (token1->keyword == RID_STATIC_ASSERT)
13091 cp_parser_static_assert (parser, /*member_p=*/false);
13092 /* Anything else must be a simple-declaration. */
13093 else
13094 cp_parser_simple_declaration (parser, !statement_p,
13095 /*maybe_range_for_decl*/NULL);
13096 }
13097
13098 /* Parse a simple-declaration.
13099
13100 simple-declaration:
13101 decl-specifier-seq [opt] init-declarator-list [opt] ;
13102 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13103 brace-or-equal-initializer ;
13104
13105 init-declarator-list:
13106 init-declarator
13107 init-declarator-list , init-declarator
13108
13109 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13110 function-definition as a simple-declaration.
13111
13112 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13113 parsed declaration if it is an uninitialized single declarator not followed
13114 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13115 if present, will not be consumed. */
13116
13117 static void
13118 cp_parser_simple_declaration (cp_parser* parser,
13119 bool function_definition_allowed_p,
13120 tree *maybe_range_for_decl)
13121 {
13122 cp_decl_specifier_seq decl_specifiers;
13123 int declares_class_or_enum;
13124 bool saw_declarator;
13125 location_t comma_loc = UNKNOWN_LOCATION;
13126 location_t init_loc = UNKNOWN_LOCATION;
13127
13128 if (maybe_range_for_decl)
13129 *maybe_range_for_decl = NULL_TREE;
13130
13131 /* Defer access checks until we know what is being declared; the
13132 checks for names appearing in the decl-specifier-seq should be
13133 done as if we were in the scope of the thing being declared. */
13134 push_deferring_access_checks (dk_deferred);
13135
13136 /* Parse the decl-specifier-seq. We have to keep track of whether
13137 or not the decl-specifier-seq declares a named class or
13138 enumeration type, since that is the only case in which the
13139 init-declarator-list is allowed to be empty.
13140
13141 [dcl.dcl]
13142
13143 In a simple-declaration, the optional init-declarator-list can be
13144 omitted only when declaring a class or enumeration, that is when
13145 the decl-specifier-seq contains either a class-specifier, an
13146 elaborated-type-specifier, or an enum-specifier. */
13147 cp_parser_decl_specifier_seq (parser,
13148 CP_PARSER_FLAGS_OPTIONAL,
13149 &decl_specifiers,
13150 &declares_class_or_enum);
13151 /* We no longer need to defer access checks. */
13152 stop_deferring_access_checks ();
13153
13154 /* In a block scope, a valid declaration must always have a
13155 decl-specifier-seq. By not trying to parse declarators, we can
13156 resolve the declaration/expression ambiguity more quickly. */
13157 if (!function_definition_allowed_p
13158 && !decl_specifiers.any_specifiers_p)
13159 {
13160 cp_parser_error (parser, "expected declaration");
13161 goto done;
13162 }
13163
13164 /* If the next two tokens are both identifiers, the code is
13165 erroneous. The usual cause of this situation is code like:
13166
13167 T t;
13168
13169 where "T" should name a type -- but does not. */
13170 if (!decl_specifiers.any_type_specifiers_p
13171 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13172 {
13173 /* If parsing tentatively, we should commit; we really are
13174 looking at a declaration. */
13175 cp_parser_commit_to_tentative_parse (parser);
13176 /* Give up. */
13177 goto done;
13178 }
13179
13180 cp_parser_maybe_commit_to_declaration (parser,
13181 decl_specifiers.any_specifiers_p);
13182
13183 /* Look for C++17 decomposition declaration. */
13184 for (size_t n = 1; ; n++)
13185 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13186 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13187 continue;
13188 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13189 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13190 && decl_specifiers.any_specifiers_p)
13191 {
13192 tree decl
13193 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13194 maybe_range_for_decl,
13195 &init_loc);
13196
13197 /* The next token should be either a `,' or a `;'. */
13198 cp_token *token = cp_lexer_peek_token (parser->lexer);
13199 /* If it's a `;', we are done. */
13200 if (token->type == CPP_SEMICOLON)
13201 goto finish;
13202 else if (maybe_range_for_decl)
13203 {
13204 if (*maybe_range_for_decl == NULL_TREE)
13205 *maybe_range_for_decl = error_mark_node;
13206 goto finish;
13207 }
13208 /* Anything else is an error. */
13209 else
13210 {
13211 /* If we have already issued an error message we don't need
13212 to issue another one. */
13213 if ((decl != error_mark_node
13214 && DECL_INITIAL (decl) != error_mark_node)
13215 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13216 cp_parser_error (parser, "expected %<,%> or %<;%>");
13217 /* Skip tokens until we reach the end of the statement. */
13218 cp_parser_skip_to_end_of_statement (parser);
13219 /* If the next token is now a `;', consume it. */
13220 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13221 cp_lexer_consume_token (parser->lexer);
13222 goto done;
13223 }
13224 }
13225 else
13226 break;
13227
13228 tree last_type;
13229 bool auto_specifier_p;
13230 /* NULL_TREE if both variable and function declaration are allowed,
13231 error_mark_node if function declaration are not allowed and
13232 a FUNCTION_DECL that should be diagnosed if it is followed by
13233 variable declarations. */
13234 tree auto_function_declaration;
13235
13236 last_type = NULL_TREE;
13237 auto_specifier_p
13238 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13239 auto_function_declaration = NULL_TREE;
13240
13241 /* Keep going until we hit the `;' at the end of the simple
13242 declaration. */
13243 saw_declarator = false;
13244 while (cp_lexer_next_token_is_not (parser->lexer,
13245 CPP_SEMICOLON))
13246 {
13247 cp_token *token;
13248 bool function_definition_p;
13249 tree decl;
13250 tree auto_result = NULL_TREE;
13251
13252 if (saw_declarator)
13253 {
13254 /* If we are processing next declarator, comma is expected */
13255 token = cp_lexer_peek_token (parser->lexer);
13256 gcc_assert (token->type == CPP_COMMA);
13257 cp_lexer_consume_token (parser->lexer);
13258 if (maybe_range_for_decl)
13259 {
13260 *maybe_range_for_decl = error_mark_node;
13261 if (comma_loc == UNKNOWN_LOCATION)
13262 comma_loc = token->location;
13263 }
13264 }
13265 else
13266 saw_declarator = true;
13267
13268 /* Parse the init-declarator. */
13269 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13270 /*checks=*/NULL,
13271 function_definition_allowed_p,
13272 /*member_p=*/false,
13273 declares_class_or_enum,
13274 &function_definition_p,
13275 maybe_range_for_decl,
13276 &init_loc,
13277 &auto_result);
13278 /* If an error occurred while parsing tentatively, exit quickly.
13279 (That usually happens when in the body of a function; each
13280 statement is treated as a declaration-statement until proven
13281 otherwise.) */
13282 if (cp_parser_error_occurred (parser))
13283 goto done;
13284
13285 if (auto_specifier_p && cxx_dialect >= cxx14)
13286 {
13287 /* If the init-declarator-list contains more than one
13288 init-declarator, they shall all form declarations of
13289 variables. */
13290 if (auto_function_declaration == NULL_TREE)
13291 auto_function_declaration
13292 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13293 else if (TREE_CODE (decl) == FUNCTION_DECL
13294 || auto_function_declaration != error_mark_node)
13295 {
13296 error_at (decl_specifiers.locations[ds_type_spec],
13297 "non-variable %qD in declaration with more than one "
13298 "declarator with placeholder type",
13299 TREE_CODE (decl) == FUNCTION_DECL
13300 ? decl : auto_function_declaration);
13301 auto_function_declaration = error_mark_node;
13302 }
13303 }
13304
13305 if (auto_result
13306 && (!processing_template_decl || !type_uses_auto (auto_result)))
13307 {
13308 if (last_type
13309 && last_type != error_mark_node
13310 && !same_type_p (auto_result, last_type))
13311 {
13312 /* If the list of declarators contains more than one declarator,
13313 the type of each declared variable is determined as described
13314 above. If the type deduced for the template parameter U is not
13315 the same in each deduction, the program is ill-formed. */
13316 error_at (decl_specifiers.locations[ds_type_spec],
13317 "inconsistent deduction for %qT: %qT and then %qT",
13318 decl_specifiers.type, last_type, auto_result);
13319 last_type = error_mark_node;
13320 }
13321 else
13322 last_type = auto_result;
13323 }
13324
13325 /* Handle function definitions specially. */
13326 if (function_definition_p)
13327 {
13328 /* If the next token is a `,', then we are probably
13329 processing something like:
13330
13331 void f() {}, *p;
13332
13333 which is erroneous. */
13334 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13335 {
13336 cp_token *token = cp_lexer_peek_token (parser->lexer);
13337 error_at (token->location,
13338 "mixing"
13339 " declarations and function-definitions is forbidden");
13340 }
13341 /* Otherwise, we're done with the list of declarators. */
13342 else
13343 {
13344 pop_deferring_access_checks ();
13345 return;
13346 }
13347 }
13348 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13349 *maybe_range_for_decl = decl;
13350 /* The next token should be either a `,' or a `;'. */
13351 token = cp_lexer_peek_token (parser->lexer);
13352 /* If it's a `,', there are more declarators to come. */
13353 if (token->type == CPP_COMMA)
13354 /* will be consumed next time around */;
13355 /* If it's a `;', we are done. */
13356 else if (token->type == CPP_SEMICOLON)
13357 break;
13358 else if (maybe_range_for_decl)
13359 {
13360 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13361 permerror (decl_specifiers.locations[ds_type_spec],
13362 "types may not be defined in a for-range-declaration");
13363 break;
13364 }
13365 /* Anything else is an error. */
13366 else
13367 {
13368 /* If we have already issued an error message we don't need
13369 to issue another one. */
13370 if ((decl != error_mark_node
13371 && DECL_INITIAL (decl) != error_mark_node)
13372 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13373 cp_parser_error (parser, "expected %<,%> or %<;%>");
13374 /* Skip tokens until we reach the end of the statement. */
13375 cp_parser_skip_to_end_of_statement (parser);
13376 /* If the next token is now a `;', consume it. */
13377 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13378 cp_lexer_consume_token (parser->lexer);
13379 goto done;
13380 }
13381 /* After the first time around, a function-definition is not
13382 allowed -- even if it was OK at first. For example:
13383
13384 int i, f() {}
13385
13386 is not valid. */
13387 function_definition_allowed_p = false;
13388 }
13389
13390 /* Issue an error message if no declarators are present, and the
13391 decl-specifier-seq does not itself declare a class or
13392 enumeration: [dcl.dcl]/3. */
13393 if (!saw_declarator)
13394 {
13395 if (cp_parser_declares_only_class_p (parser))
13396 {
13397 if (!declares_class_or_enum
13398 && decl_specifiers.type
13399 && OVERLOAD_TYPE_P (decl_specifiers.type))
13400 /* Ensure an error is issued anyway when finish_decltype_type,
13401 called via cp_parser_decl_specifier_seq, returns a class or
13402 an enumeration (c++/51786). */
13403 decl_specifiers.type = NULL_TREE;
13404 shadow_tag (&decl_specifiers);
13405 }
13406 /* Perform any deferred access checks. */
13407 perform_deferred_access_checks (tf_warning_or_error);
13408 }
13409
13410 /* Consume the `;'. */
13411 finish:
13412 if (!maybe_range_for_decl)
13413 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13414 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13415 {
13416 if (init_loc != UNKNOWN_LOCATION)
13417 error_at (init_loc, "initializer in range-based %<for%> loop");
13418 if (comma_loc != UNKNOWN_LOCATION)
13419 error_at (comma_loc,
13420 "multiple declarations in range-based %<for%> loop");
13421 }
13422
13423 done:
13424 pop_deferring_access_checks ();
13425 }
13426
13427 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13428 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13429 initializer ; */
13430
13431 static tree
13432 cp_parser_decomposition_declaration (cp_parser *parser,
13433 cp_decl_specifier_seq *decl_specifiers,
13434 tree *maybe_range_for_decl,
13435 location_t *init_loc)
13436 {
13437 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13438 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13439 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13440
13441 /* Parse the identifier-list. */
13442 auto_vec<cp_expr, 10> v;
13443 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13444 while (true)
13445 {
13446 cp_expr e = cp_parser_identifier (parser);
13447 if (e.get_value () == error_mark_node)
13448 break;
13449 v.safe_push (e);
13450 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13451 break;
13452 cp_lexer_consume_token (parser->lexer);
13453 }
13454
13455 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13456 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13457 {
13458 end_loc = UNKNOWN_LOCATION;
13459 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13460 false);
13461 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13462 cp_lexer_consume_token (parser->lexer);
13463 else
13464 {
13465 cp_parser_skip_to_end_of_statement (parser);
13466 return error_mark_node;
13467 }
13468 }
13469
13470 if (cxx_dialect < cxx17)
13471 pedwarn (loc, 0, "structured bindings only available with "
13472 "-std=c++17 or -std=gnu++17");
13473
13474 tree pushed_scope;
13475 cp_declarator *declarator = make_declarator (cdk_decomp);
13476 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13477 declarator->id_loc = loc;
13478 if (ref_qual != REF_QUAL_NONE)
13479 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13480 ref_qual == REF_QUAL_RVALUE,
13481 NULL_TREE);
13482 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13483 NULL_TREE, decl_specifiers->attributes,
13484 &pushed_scope);
13485 tree orig_decl = decl;
13486
13487 unsigned int i;
13488 cp_expr e;
13489 cp_decl_specifier_seq decl_specs;
13490 clear_decl_specs (&decl_specs);
13491 decl_specs.type = make_auto ();
13492 tree prev = decl;
13493 FOR_EACH_VEC_ELT (v, i, e)
13494 {
13495 if (i == 0)
13496 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13497 sfk_none, e.get_location ());
13498 else
13499 {
13500 declarator->u.id.unqualified_name = e.get_value ();
13501 declarator->id_loc = e.get_location ();
13502 }
13503 tree elt_pushed_scope;
13504 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13505 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13506 if (decl2 == error_mark_node)
13507 decl = error_mark_node;
13508 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13509 {
13510 /* Ensure we've diagnosed redeclaration if we aren't creating
13511 a new VAR_DECL. */
13512 gcc_assert (errorcount);
13513 decl = error_mark_node;
13514 }
13515 else
13516 prev = decl2;
13517 if (elt_pushed_scope)
13518 pop_scope (elt_pushed_scope);
13519 }
13520
13521 if (v.is_empty ())
13522 {
13523 error_at (loc, "empty structured binding declaration");
13524 decl = error_mark_node;
13525 }
13526
13527 if (maybe_range_for_decl == NULL
13528 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13529 {
13530 bool non_constant_p = false, is_direct_init = false;
13531 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13532 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13533 &non_constant_p);
13534 if (initializer == NULL_TREE
13535 || (TREE_CODE (initializer) == TREE_LIST
13536 && TREE_CHAIN (initializer))
13537 || (is_direct_init
13538 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13539 && CONSTRUCTOR_NELTS (initializer) != 1))
13540 {
13541 error_at (loc, "invalid initializer for structured binding "
13542 "declaration");
13543 initializer = error_mark_node;
13544 }
13545
13546 if (decl != error_mark_node)
13547 {
13548 cp_maybe_mangle_decomp (decl, prev, v.length ());
13549 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13550 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13551 cp_finish_decomp (decl, prev, v.length ());
13552 }
13553 }
13554 else if (decl != error_mark_node)
13555 {
13556 *maybe_range_for_decl = prev;
13557 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13558 the underlying DECL. */
13559 cp_finish_decomp (decl, prev, v.length ());
13560 }
13561
13562 if (pushed_scope)
13563 pop_scope (pushed_scope);
13564
13565 if (decl == error_mark_node && DECL_P (orig_decl))
13566 {
13567 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13568 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13569 }
13570
13571 return decl;
13572 }
13573
13574 /* Parse a decl-specifier-seq.
13575
13576 decl-specifier-seq:
13577 decl-specifier-seq [opt] decl-specifier
13578 decl-specifier attribute-specifier-seq [opt] (C++11)
13579
13580 decl-specifier:
13581 storage-class-specifier
13582 type-specifier
13583 function-specifier
13584 friend
13585 typedef
13586
13587 GNU Extension:
13588
13589 decl-specifier:
13590 attributes
13591
13592 Concepts Extension:
13593
13594 decl-specifier:
13595 concept
13596
13597 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13598
13599 The parser flags FLAGS is used to control type-specifier parsing.
13600
13601 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13602 flags:
13603
13604 1: one of the decl-specifiers is an elaborated-type-specifier
13605 (i.e., a type declaration)
13606 2: one of the decl-specifiers is an enum-specifier or a
13607 class-specifier (i.e., a type definition)
13608
13609 */
13610
13611 static void
13612 cp_parser_decl_specifier_seq (cp_parser* parser,
13613 cp_parser_flags flags,
13614 cp_decl_specifier_seq *decl_specs,
13615 int* declares_class_or_enum)
13616 {
13617 bool constructor_possible_p = !parser->in_declarator_p;
13618 bool found_decl_spec = false;
13619 cp_token *start_token = NULL;
13620 cp_decl_spec ds;
13621
13622 /* Clear DECL_SPECS. */
13623 clear_decl_specs (decl_specs);
13624
13625 /* Assume no class or enumeration type is declared. */
13626 *declares_class_or_enum = 0;
13627
13628 /* Keep reading specifiers until there are no more to read. */
13629 while (true)
13630 {
13631 bool constructor_p;
13632 cp_token *token;
13633 ds = ds_last;
13634
13635 /* Peek at the next token. */
13636 token = cp_lexer_peek_token (parser->lexer);
13637
13638 /* Save the first token of the decl spec list for error
13639 reporting. */
13640 if (!start_token)
13641 start_token = token;
13642 /* Handle attributes. */
13643 if (cp_next_tokens_can_be_attribute_p (parser))
13644 {
13645 /* Parse the attributes. */
13646 tree attrs = cp_parser_attributes_opt (parser);
13647
13648 /* In a sequence of declaration specifiers, c++11 attributes
13649 appertain to the type that precede them. In that case
13650 [dcl.spec]/1 says:
13651
13652 The attribute-specifier-seq affects the type only for
13653 the declaration it appears in, not other declarations
13654 involving the same type.
13655
13656 But for now let's force the user to position the
13657 attribute either at the beginning of the declaration or
13658 after the declarator-id, which would clearly mean that it
13659 applies to the declarator. */
13660 if (cxx11_attribute_p (attrs))
13661 {
13662 if (!found_decl_spec)
13663 /* The c++11 attribute is at the beginning of the
13664 declaration. It appertains to the entity being
13665 declared. */;
13666 else
13667 {
13668 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13669 {
13670 /* This is an attribute following a
13671 class-specifier. */
13672 if (decl_specs->type_definition_p)
13673 warn_misplaced_attr_for_class_type (token->location,
13674 decl_specs->type);
13675 attrs = NULL_TREE;
13676 }
13677 else
13678 {
13679 decl_specs->std_attributes
13680 = attr_chainon (decl_specs->std_attributes, attrs);
13681 if (decl_specs->locations[ds_std_attribute] == 0)
13682 decl_specs->locations[ds_std_attribute] = token->location;
13683 }
13684 continue;
13685 }
13686 }
13687
13688 decl_specs->attributes
13689 = attr_chainon (decl_specs->attributes, attrs);
13690 if (decl_specs->locations[ds_attribute] == 0)
13691 decl_specs->locations[ds_attribute] = token->location;
13692 continue;
13693 }
13694 /* Assume we will find a decl-specifier keyword. */
13695 found_decl_spec = true;
13696 /* If the next token is an appropriate keyword, we can simply
13697 add it to the list. */
13698 switch (token->keyword)
13699 {
13700 /* decl-specifier:
13701 friend
13702 constexpr */
13703 case RID_FRIEND:
13704 if (!at_class_scope_p ())
13705 {
13706 gcc_rich_location richloc (token->location);
13707 richloc.add_fixit_remove ();
13708 error_at (&richloc, "%<friend%> used outside of class");
13709 cp_lexer_purge_token (parser->lexer);
13710 }
13711 else
13712 {
13713 ds = ds_friend;
13714 /* Consume the token. */
13715 cp_lexer_consume_token (parser->lexer);
13716 }
13717 break;
13718
13719 case RID_CONSTEXPR:
13720 ds = ds_constexpr;
13721 cp_lexer_consume_token (parser->lexer);
13722 break;
13723
13724 case RID_CONCEPT:
13725 ds = ds_concept;
13726 cp_lexer_consume_token (parser->lexer);
13727 break;
13728
13729 /* function-specifier:
13730 inline
13731 virtual
13732 explicit */
13733 case RID_INLINE:
13734 case RID_VIRTUAL:
13735 case RID_EXPLICIT:
13736 cp_parser_function_specifier_opt (parser, decl_specs);
13737 break;
13738
13739 /* decl-specifier:
13740 typedef */
13741 case RID_TYPEDEF:
13742 ds = ds_typedef;
13743 /* Consume the token. */
13744 cp_lexer_consume_token (parser->lexer);
13745 /* A constructor declarator cannot appear in a typedef. */
13746 constructor_possible_p = false;
13747 /* The "typedef" keyword can only occur in a declaration; we
13748 may as well commit at this point. */
13749 cp_parser_commit_to_tentative_parse (parser);
13750
13751 if (decl_specs->storage_class != sc_none)
13752 decl_specs->conflicting_specifiers_p = true;
13753 break;
13754
13755 /* storage-class-specifier:
13756 auto
13757 register
13758 static
13759 extern
13760 mutable
13761
13762 GNU Extension:
13763 thread */
13764 case RID_AUTO:
13765 if (cxx_dialect == cxx98)
13766 {
13767 /* Consume the token. */
13768 cp_lexer_consume_token (parser->lexer);
13769
13770 /* Complain about `auto' as a storage specifier, if
13771 we're complaining about C++0x compatibility. */
13772 gcc_rich_location richloc (token->location);
13773 richloc.add_fixit_remove ();
13774 warning_at (&richloc, OPT_Wc__11_compat,
13775 "%<auto%> changes meaning in C++11; "
13776 "please remove it");
13777
13778 /* Set the storage class anyway. */
13779 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13780 token);
13781 }
13782 else
13783 /* C++0x auto type-specifier. */
13784 found_decl_spec = false;
13785 break;
13786
13787 case RID_REGISTER:
13788 case RID_STATIC:
13789 case RID_EXTERN:
13790 case RID_MUTABLE:
13791 /* Consume the token. */
13792 cp_lexer_consume_token (parser->lexer);
13793 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13794 token);
13795 break;
13796 case RID_THREAD:
13797 /* Consume the token. */
13798 ds = ds_thread;
13799 cp_lexer_consume_token (parser->lexer);
13800 break;
13801
13802 default:
13803 /* We did not yet find a decl-specifier yet. */
13804 found_decl_spec = false;
13805 break;
13806 }
13807
13808 if (found_decl_spec
13809 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13810 && token->keyword != RID_CONSTEXPR)
13811 error ("decl-specifier invalid in condition");
13812
13813 if (found_decl_spec
13814 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13815 && token->keyword != RID_MUTABLE
13816 && token->keyword != RID_CONSTEXPR)
13817 error_at (token->location, "%qD invalid in lambda",
13818 ridpointers[token->keyword]);
13819
13820 if (ds != ds_last)
13821 set_and_check_decl_spec_loc (decl_specs, ds, token);
13822
13823 /* Constructors are a special case. The `S' in `S()' is not a
13824 decl-specifier; it is the beginning of the declarator. */
13825 constructor_p
13826 = (!found_decl_spec
13827 && constructor_possible_p
13828 && (cp_parser_constructor_declarator_p
13829 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13830
13831 /* If we don't have a DECL_SPEC yet, then we must be looking at
13832 a type-specifier. */
13833 if (!found_decl_spec && !constructor_p)
13834 {
13835 int decl_spec_declares_class_or_enum;
13836 bool is_cv_qualifier;
13837 tree type_spec;
13838
13839 type_spec
13840 = cp_parser_type_specifier (parser, flags,
13841 decl_specs,
13842 /*is_declaration=*/true,
13843 &decl_spec_declares_class_or_enum,
13844 &is_cv_qualifier);
13845 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13846
13847 /* If this type-specifier referenced a user-defined type
13848 (a typedef, class-name, etc.), then we can't allow any
13849 more such type-specifiers henceforth.
13850
13851 [dcl.spec]
13852
13853 The longest sequence of decl-specifiers that could
13854 possibly be a type name is taken as the
13855 decl-specifier-seq of a declaration. The sequence shall
13856 be self-consistent as described below.
13857
13858 [dcl.type]
13859
13860 As a general rule, at most one type-specifier is allowed
13861 in the complete decl-specifier-seq of a declaration. The
13862 only exceptions are the following:
13863
13864 -- const or volatile can be combined with any other
13865 type-specifier.
13866
13867 -- signed or unsigned can be combined with char, long,
13868 short, or int.
13869
13870 -- ..
13871
13872 Example:
13873
13874 typedef char* Pc;
13875 void g (const int Pc);
13876
13877 Here, Pc is *not* part of the decl-specifier seq; it's
13878 the declarator. Therefore, once we see a type-specifier
13879 (other than a cv-qualifier), we forbid any additional
13880 user-defined types. We *do* still allow things like `int
13881 int' to be considered a decl-specifier-seq, and issue the
13882 error message later. */
13883 if (type_spec && !is_cv_qualifier)
13884 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13885 /* A constructor declarator cannot follow a type-specifier. */
13886 if (type_spec)
13887 {
13888 constructor_possible_p = false;
13889 found_decl_spec = true;
13890 if (!is_cv_qualifier)
13891 decl_specs->any_type_specifiers_p = true;
13892
13893 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
13894 error_at (token->location, "type-specifier invalid in lambda");
13895 }
13896 }
13897
13898 /* If we still do not have a DECL_SPEC, then there are no more
13899 decl-specifiers. */
13900 if (!found_decl_spec)
13901 break;
13902
13903 decl_specs->any_specifiers_p = true;
13904 /* After we see one decl-specifier, further decl-specifiers are
13905 always optional. */
13906 flags |= CP_PARSER_FLAGS_OPTIONAL;
13907 }
13908
13909 /* Don't allow a friend specifier with a class definition. */
13910 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13911 && (*declares_class_or_enum & 2))
13912 error_at (decl_specs->locations[ds_friend],
13913 "class definition may not be declared a friend");
13914 }
13915
13916 /* Parse an (optional) storage-class-specifier.
13917
13918 storage-class-specifier:
13919 auto
13920 register
13921 static
13922 extern
13923 mutable
13924
13925 GNU Extension:
13926
13927 storage-class-specifier:
13928 thread
13929
13930 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13931
13932 static tree
13933 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13934 {
13935 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13936 {
13937 case RID_AUTO:
13938 if (cxx_dialect != cxx98)
13939 return NULL_TREE;
13940 /* Fall through for C++98. */
13941 gcc_fallthrough ();
13942
13943 case RID_REGISTER:
13944 case RID_STATIC:
13945 case RID_EXTERN:
13946 case RID_MUTABLE:
13947 case RID_THREAD:
13948 /* Consume the token. */
13949 return cp_lexer_consume_token (parser->lexer)->u.value;
13950
13951 default:
13952 return NULL_TREE;
13953 }
13954 }
13955
13956 /* Parse an (optional) function-specifier.
13957
13958 function-specifier:
13959 inline
13960 virtual
13961 explicit
13962
13963 C++2A Extension:
13964 explicit(constant-expression)
13965
13966 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13967 Updates DECL_SPECS, if it is non-NULL. */
13968
13969 static tree
13970 cp_parser_function_specifier_opt (cp_parser* parser,
13971 cp_decl_specifier_seq *decl_specs)
13972 {
13973 cp_token *token = cp_lexer_peek_token (parser->lexer);
13974 switch (token->keyword)
13975 {
13976 case RID_INLINE:
13977 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13978 break;
13979
13980 case RID_VIRTUAL:
13981 /* 14.5.2.3 [temp.mem]
13982
13983 A member function template shall not be virtual. */
13984 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13985 && current_class_type)
13986 error_at (token->location, "templates may not be %<virtual%>");
13987 else
13988 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13989 break;
13990
13991 case RID_EXPLICIT:
13992 {
13993 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
13994 /* If we see '(', it's C++20 explicit(bool). */
13995 tree expr;
13996 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13997 {
13998 matching_parens parens;
13999 parens.consume_open (parser);
14000
14001 /* New types are not allowed in an explicit-specifier. */
14002 const char *saved_message
14003 = parser->type_definition_forbidden_message;
14004 parser->type_definition_forbidden_message
14005 = G_("types may not be defined in explicit-specifier");
14006
14007 if (cxx_dialect < cxx2a)
14008 pedwarn (token->location, 0,
14009 "%<explicit(bool)%> only available with -std=c++2a "
14010 "or -std=gnu++2a");
14011
14012 /* Parse the constant-expression. */
14013 expr = cp_parser_constant_expression (parser);
14014
14015 /* Restore the saved message. */
14016 parser->type_definition_forbidden_message = saved_message;
14017 parens.require_close (parser);
14018 }
14019 else
14020 /* The explicit-specifier explicit without a constant-expression is
14021 equivalent to the explicit-specifier explicit(true). */
14022 expr = boolean_true_node;
14023
14024 /* [dcl.fct.spec]
14025 "the constant-expression, if supplied, shall be a contextually
14026 converted constant expression of type bool." */
14027 expr = build_explicit_specifier (expr, tf_warning_or_error);
14028 /* We could evaluate it -- mark the decl as appropriate. */
14029 if (expr == boolean_true_node)
14030 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14031 else if (expr == boolean_false_node)
14032 /* Don't mark the decl as explicit. */;
14033 else if (decl_specs)
14034 /* The expression was value-dependent. Remember it so that we can
14035 substitute it later. */
14036 decl_specs->explicit_specifier = expr;
14037 return id;
14038 }
14039
14040 default:
14041 return NULL_TREE;
14042 }
14043
14044 /* Consume the token. */
14045 return cp_lexer_consume_token (parser->lexer)->u.value;
14046 }
14047
14048 /* Parse a linkage-specification.
14049
14050 linkage-specification:
14051 extern string-literal { declaration-seq [opt] }
14052 extern string-literal declaration */
14053
14054 static void
14055 cp_parser_linkage_specification (cp_parser* parser)
14056 {
14057 tree linkage;
14058
14059 /* Look for the `extern' keyword. */
14060 cp_token *extern_token
14061 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14062
14063 /* Look for the string-literal. */
14064 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14065 linkage = cp_parser_string_literal (parser, false, false);
14066
14067 /* Transform the literal into an identifier. If the literal is a
14068 wide-character string, or contains embedded NULs, then we can't
14069 handle it as the user wants. */
14070 if (strlen (TREE_STRING_POINTER (linkage))
14071 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14072 {
14073 cp_parser_error (parser, "invalid linkage-specification");
14074 /* Assume C++ linkage. */
14075 linkage = lang_name_cplusplus;
14076 }
14077 else
14078 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14079
14080 /* We're now using the new linkage. */
14081 push_lang_context (linkage);
14082
14083 /* Preserve the location of the the innermost linkage specification,
14084 tracking the locations of nested specifications via a local. */
14085 location_t saved_location
14086 = parser->innermost_linkage_specification_location;
14087 /* Construct a location ranging from the start of the "extern" to
14088 the end of the string-literal, with the caret at the start, e.g.:
14089 extern "C" {
14090 ^~~~~~~~~~
14091 */
14092 parser->innermost_linkage_specification_location
14093 = make_location (extern_token->location,
14094 extern_token->location,
14095 get_finish (string_token->location));
14096
14097 /* If the next token is a `{', then we're using the first
14098 production. */
14099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14100 {
14101 cp_ensure_no_omp_declare_simd (parser);
14102 cp_ensure_no_oacc_routine (parser);
14103
14104 /* Consume the `{' token. */
14105 matching_braces braces;
14106 braces.consume_open (parser)->location;
14107 /* Parse the declarations. */
14108 cp_parser_declaration_seq_opt (parser);
14109 /* Look for the closing `}'. */
14110 braces.require_close (parser);
14111 }
14112 /* Otherwise, there's just one declaration. */
14113 else
14114 {
14115 bool saved_in_unbraced_linkage_specification_p;
14116
14117 saved_in_unbraced_linkage_specification_p
14118 = parser->in_unbraced_linkage_specification_p;
14119 parser->in_unbraced_linkage_specification_p = true;
14120 cp_parser_declaration (parser);
14121 parser->in_unbraced_linkage_specification_p
14122 = saved_in_unbraced_linkage_specification_p;
14123 }
14124
14125 /* We're done with the linkage-specification. */
14126 pop_lang_context ();
14127
14128 /* Restore location of parent linkage specification, if any. */
14129 parser->innermost_linkage_specification_location = saved_location;
14130 }
14131
14132 /* Parse a static_assert-declaration.
14133
14134 static_assert-declaration:
14135 static_assert ( constant-expression , string-literal ) ;
14136 static_assert ( constant-expression ) ; (C++17)
14137
14138 If MEMBER_P, this static_assert is a class member. */
14139
14140 static void
14141 cp_parser_static_assert(cp_parser *parser, bool member_p)
14142 {
14143 cp_expr condition;
14144 location_t token_loc;
14145 tree message;
14146 bool dummy;
14147
14148 /* Peek at the `static_assert' token so we can keep track of exactly
14149 where the static assertion started. */
14150 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14151
14152 /* Look for the `static_assert' keyword. */
14153 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14154 RT_STATIC_ASSERT))
14155 return;
14156
14157 /* We know we are in a static assertion; commit to any tentative
14158 parse. */
14159 if (cp_parser_parsing_tentatively (parser))
14160 cp_parser_commit_to_tentative_parse (parser);
14161
14162 /* Parse the `(' starting the static assertion condition. */
14163 matching_parens parens;
14164 parens.require_open (parser);
14165
14166 /* Parse the constant-expression. Allow a non-constant expression
14167 here in order to give better diagnostics in finish_static_assert. */
14168 condition =
14169 cp_parser_constant_expression (parser,
14170 /*allow_non_constant_p=*/true,
14171 /*non_constant_p=*/&dummy);
14172
14173 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14174 {
14175 if (cxx_dialect < cxx17)
14176 pedwarn (input_location, OPT_Wpedantic,
14177 "static_assert without a message "
14178 "only available with -std=c++17 or -std=gnu++17");
14179 /* Eat the ')' */
14180 cp_lexer_consume_token (parser->lexer);
14181 message = build_string (1, "");
14182 TREE_TYPE (message) = char_array_type_node;
14183 fix_string_type (message);
14184 }
14185 else
14186 {
14187 /* Parse the separating `,'. */
14188 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14189
14190 /* Parse the string-literal message. */
14191 message = cp_parser_string_literal (parser,
14192 /*translate=*/false,
14193 /*wide_ok=*/true);
14194
14195 /* A `)' completes the static assertion. */
14196 if (!parens.require_close (parser))
14197 cp_parser_skip_to_closing_parenthesis (parser,
14198 /*recovering=*/true,
14199 /*or_comma=*/false,
14200 /*consume_paren=*/true);
14201 }
14202
14203 /* A semicolon terminates the declaration. */
14204 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14205
14206 /* Get the location for the static assertion. Use that of the
14207 condition if available, otherwise, use that of the "static_assert"
14208 token. */
14209 location_t assert_loc = condition.get_location ();
14210 if (assert_loc == UNKNOWN_LOCATION)
14211 assert_loc = token_loc;
14212
14213 /* Complete the static assertion, which may mean either processing
14214 the static assert now or saving it for template instantiation. */
14215 finish_static_assert (condition, message, assert_loc, member_p);
14216 }
14217
14218 /* Parse the expression in decltype ( expression ). */
14219
14220 static tree
14221 cp_parser_decltype_expr (cp_parser *parser,
14222 bool &id_expression_or_member_access_p)
14223 {
14224 cp_token *id_expr_start_token;
14225 tree expr;
14226
14227 /* Since we're going to preserve any side-effects from this parse, set up a
14228 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14229 in the expression. */
14230 tentative_firewall firewall (parser);
14231
14232 /* First, try parsing an id-expression. */
14233 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14234 cp_parser_parse_tentatively (parser);
14235 expr = cp_parser_id_expression (parser,
14236 /*template_keyword_p=*/false,
14237 /*check_dependency_p=*/true,
14238 /*template_p=*/NULL,
14239 /*declarator_p=*/false,
14240 /*optional_p=*/false);
14241
14242 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14243 {
14244 bool non_integral_constant_expression_p = false;
14245 tree id_expression = expr;
14246 cp_id_kind idk;
14247 const char *error_msg;
14248
14249 if (identifier_p (expr))
14250 /* Lookup the name we got back from the id-expression. */
14251 expr = cp_parser_lookup_name_simple (parser, expr,
14252 id_expr_start_token->location);
14253
14254 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14255 /* A template without args is not a complete id-expression. */
14256 expr = error_mark_node;
14257
14258 if (expr
14259 && expr != error_mark_node
14260 && TREE_CODE (expr) != TYPE_DECL
14261 && (TREE_CODE (expr) != BIT_NOT_EXPR
14262 || !TYPE_P (TREE_OPERAND (expr, 0)))
14263 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14264 {
14265 /* Complete lookup of the id-expression. */
14266 expr = (finish_id_expression
14267 (id_expression, expr, parser->scope, &idk,
14268 /*integral_constant_expression_p=*/false,
14269 /*allow_non_integral_constant_expression_p=*/true,
14270 &non_integral_constant_expression_p,
14271 /*template_p=*/false,
14272 /*done=*/true,
14273 /*address_p=*/false,
14274 /*template_arg_p=*/false,
14275 &error_msg,
14276 id_expr_start_token->location));
14277
14278 if (expr == error_mark_node)
14279 /* We found an id-expression, but it was something that we
14280 should not have found. This is an error, not something
14281 we can recover from, so note that we found an
14282 id-expression and we'll recover as gracefully as
14283 possible. */
14284 id_expression_or_member_access_p = true;
14285 }
14286
14287 if (expr
14288 && expr != error_mark_node
14289 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14290 /* We have an id-expression. */
14291 id_expression_or_member_access_p = true;
14292 }
14293
14294 if (!id_expression_or_member_access_p)
14295 {
14296 /* Abort the id-expression parse. */
14297 cp_parser_abort_tentative_parse (parser);
14298
14299 /* Parsing tentatively, again. */
14300 cp_parser_parse_tentatively (parser);
14301
14302 /* Parse a class member access. */
14303 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14304 /*cast_p=*/false, /*decltype*/true,
14305 /*member_access_only_p=*/true, NULL);
14306
14307 if (expr
14308 && expr != error_mark_node
14309 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14310 /* We have an id-expression. */
14311 id_expression_or_member_access_p = true;
14312 }
14313
14314 if (id_expression_or_member_access_p)
14315 /* We have parsed the complete id-expression or member access. */
14316 cp_parser_parse_definitely (parser);
14317 else
14318 {
14319 /* Abort our attempt to parse an id-expression or member access
14320 expression. */
14321 cp_parser_abort_tentative_parse (parser);
14322
14323 /* Commit to the tentative_firewall so we get syntax errors. */
14324 cp_parser_commit_to_tentative_parse (parser);
14325
14326 /* Parse a full expression. */
14327 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14328 /*decltype_p=*/true);
14329 }
14330
14331 return expr;
14332 }
14333
14334 /* Parse a `decltype' type. Returns the type.
14335
14336 simple-type-specifier:
14337 decltype ( expression )
14338 C++14 proposal:
14339 decltype ( auto ) */
14340
14341 static tree
14342 cp_parser_decltype (cp_parser *parser)
14343 {
14344 bool id_expression_or_member_access_p = false;
14345 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14346
14347 if (start_token->type == CPP_DECLTYPE)
14348 {
14349 /* Already parsed. */
14350 cp_lexer_consume_token (parser->lexer);
14351 return saved_checks_value (start_token->u.tree_check_value);
14352 }
14353
14354 /* Look for the `decltype' token. */
14355 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14356 return error_mark_node;
14357
14358 /* Parse the opening `('. */
14359 matching_parens parens;
14360 if (!parens.require_open (parser))
14361 return error_mark_node;
14362
14363 push_deferring_access_checks (dk_deferred);
14364
14365 tree expr = NULL_TREE;
14366
14367 if (cxx_dialect >= cxx14
14368 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14369 /* decltype (auto) */
14370 cp_lexer_consume_token (parser->lexer);
14371 else
14372 {
14373 /* decltype (expression) */
14374
14375 /* Types cannot be defined in a `decltype' expression. Save away the
14376 old message and set the new one. */
14377 const char *saved_message = parser->type_definition_forbidden_message;
14378 parser->type_definition_forbidden_message
14379 = G_("types may not be defined in %<decltype%> expressions");
14380
14381 /* The restrictions on constant-expressions do not apply inside
14382 decltype expressions. */
14383 bool saved_integral_constant_expression_p
14384 = parser->integral_constant_expression_p;
14385 bool saved_non_integral_constant_expression_p
14386 = parser->non_integral_constant_expression_p;
14387 parser->integral_constant_expression_p = false;
14388
14389 /* Within a parenthesized expression, a `>' token is always
14390 the greater-than operator. */
14391 bool saved_greater_than_is_operator_p
14392 = parser->greater_than_is_operator_p;
14393 parser->greater_than_is_operator_p = true;
14394
14395 /* Do not actually evaluate the expression. */
14396 ++cp_unevaluated_operand;
14397
14398 /* Do not warn about problems with the expression. */
14399 ++c_inhibit_evaluation_warnings;
14400
14401 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14402
14403 /* Go back to evaluating expressions. */
14404 --cp_unevaluated_operand;
14405 --c_inhibit_evaluation_warnings;
14406
14407 /* The `>' token might be the end of a template-id or
14408 template-parameter-list now. */
14409 parser->greater_than_is_operator_p
14410 = saved_greater_than_is_operator_p;
14411
14412 /* Restore the old message and the integral constant expression
14413 flags. */
14414 parser->type_definition_forbidden_message = saved_message;
14415 parser->integral_constant_expression_p
14416 = saved_integral_constant_expression_p;
14417 parser->non_integral_constant_expression_p
14418 = saved_non_integral_constant_expression_p;
14419 }
14420
14421 /* Parse to the closing `)'. */
14422 if (!parens.require_close (parser))
14423 {
14424 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14425 /*consume_paren=*/true);
14426 pop_deferring_access_checks ();
14427 return error_mark_node;
14428 }
14429
14430 if (!expr)
14431 {
14432 /* Build auto. */
14433 expr = make_decltype_auto ();
14434 AUTO_IS_DECLTYPE (expr) = true;
14435 }
14436 else
14437 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14438 tf_warning_or_error);
14439
14440 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14441 it again. */
14442 start_token->type = CPP_DECLTYPE;
14443 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14444 start_token->u.tree_check_value->value = expr;
14445 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14446 start_token->keyword = RID_MAX;
14447 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14448
14449 pop_to_parent_deferring_access_checks ();
14450
14451 return expr;
14452 }
14453
14454 /* Special member functions [gram.special] */
14455
14456 /* Parse a conversion-function-id.
14457
14458 conversion-function-id:
14459 operator conversion-type-id
14460
14461 Returns an IDENTIFIER_NODE representing the operator. */
14462
14463 static tree
14464 cp_parser_conversion_function_id (cp_parser* parser)
14465 {
14466 tree type;
14467 tree saved_scope;
14468 tree saved_qualifying_scope;
14469 tree saved_object_scope;
14470 tree pushed_scope = NULL_TREE;
14471
14472 /* Look for the `operator' token. */
14473 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14474 return error_mark_node;
14475 /* When we parse the conversion-type-id, the current scope will be
14476 reset. However, we need that information in able to look up the
14477 conversion function later, so we save it here. */
14478 saved_scope = parser->scope;
14479 saved_qualifying_scope = parser->qualifying_scope;
14480 saved_object_scope = parser->object_scope;
14481 /* We must enter the scope of the class so that the names of
14482 entities declared within the class are available in the
14483 conversion-type-id. For example, consider:
14484
14485 struct S {
14486 typedef int I;
14487 operator I();
14488 };
14489
14490 S::operator I() { ... }
14491
14492 In order to see that `I' is a type-name in the definition, we
14493 must be in the scope of `S'. */
14494 if (saved_scope)
14495 pushed_scope = push_scope (saved_scope);
14496 /* Parse the conversion-type-id. */
14497 type = cp_parser_conversion_type_id (parser);
14498 /* Leave the scope of the class, if any. */
14499 if (pushed_scope)
14500 pop_scope (pushed_scope);
14501 /* Restore the saved scope. */
14502 parser->scope = saved_scope;
14503 parser->qualifying_scope = saved_qualifying_scope;
14504 parser->object_scope = saved_object_scope;
14505 /* If the TYPE is invalid, indicate failure. */
14506 if (type == error_mark_node)
14507 return error_mark_node;
14508 return make_conv_op_name (type);
14509 }
14510
14511 /* Parse a conversion-type-id:
14512
14513 conversion-type-id:
14514 type-specifier-seq conversion-declarator [opt]
14515
14516 Returns the TYPE specified. */
14517
14518 static tree
14519 cp_parser_conversion_type_id (cp_parser* parser)
14520 {
14521 tree attributes;
14522 cp_decl_specifier_seq type_specifiers;
14523 cp_declarator *declarator;
14524 tree type_specified;
14525 const char *saved_message;
14526
14527 /* Parse the attributes. */
14528 attributes = cp_parser_attributes_opt (parser);
14529
14530 saved_message = parser->type_definition_forbidden_message;
14531 parser->type_definition_forbidden_message
14532 = G_("types may not be defined in a conversion-type-id");
14533
14534 /* Parse the type-specifiers. */
14535 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14536 /*is_trailing_return=*/false,
14537 &type_specifiers);
14538
14539 parser->type_definition_forbidden_message = saved_message;
14540
14541 /* If that didn't work, stop. */
14542 if (type_specifiers.type == error_mark_node)
14543 return error_mark_node;
14544 /* Parse the conversion-declarator. */
14545 declarator = cp_parser_conversion_declarator_opt (parser);
14546
14547 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14548 /*initialized=*/0, &attributes);
14549 if (attributes)
14550 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14551
14552 /* Don't give this error when parsing tentatively. This happens to
14553 work because we always parse this definitively once. */
14554 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14555 && type_uses_auto (type_specified))
14556 {
14557 if (cxx_dialect < cxx14)
14558 {
14559 error ("invalid use of %<auto%> in conversion operator");
14560 return error_mark_node;
14561 }
14562 else if (template_parm_scope_p ())
14563 warning (0, "use of %<auto%> in member template "
14564 "conversion operator can never be deduced");
14565 }
14566
14567 return type_specified;
14568 }
14569
14570 /* Parse an (optional) conversion-declarator.
14571
14572 conversion-declarator:
14573 ptr-operator conversion-declarator [opt]
14574
14575 */
14576
14577 static cp_declarator *
14578 cp_parser_conversion_declarator_opt (cp_parser* parser)
14579 {
14580 enum tree_code code;
14581 tree class_type, std_attributes = NULL_TREE;
14582 cp_cv_quals cv_quals;
14583
14584 /* We don't know if there's a ptr-operator next, or not. */
14585 cp_parser_parse_tentatively (parser);
14586 /* Try the ptr-operator. */
14587 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14588 &std_attributes);
14589 /* If it worked, look for more conversion-declarators. */
14590 if (cp_parser_parse_definitely (parser))
14591 {
14592 cp_declarator *declarator;
14593
14594 /* Parse another optional declarator. */
14595 declarator = cp_parser_conversion_declarator_opt (parser);
14596
14597 declarator = cp_parser_make_indirect_declarator
14598 (code, class_type, cv_quals, declarator, std_attributes);
14599
14600 return declarator;
14601 }
14602
14603 return NULL;
14604 }
14605
14606 /* Parse an (optional) ctor-initializer.
14607
14608 ctor-initializer:
14609 : mem-initializer-list */
14610
14611 static void
14612 cp_parser_ctor_initializer_opt (cp_parser* parser)
14613 {
14614 /* If the next token is not a `:', then there is no
14615 ctor-initializer. */
14616 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14617 {
14618 /* Do default initialization of any bases and members. */
14619 if (DECL_CONSTRUCTOR_P (current_function_decl))
14620 finish_mem_initializers (NULL_TREE);
14621 return;
14622 }
14623
14624 /* Consume the `:' token. */
14625 cp_lexer_consume_token (parser->lexer);
14626 /* And the mem-initializer-list. */
14627 cp_parser_mem_initializer_list (parser);
14628 }
14629
14630 /* Parse a mem-initializer-list.
14631
14632 mem-initializer-list:
14633 mem-initializer ... [opt]
14634 mem-initializer ... [opt] , mem-initializer-list */
14635
14636 static void
14637 cp_parser_mem_initializer_list (cp_parser* parser)
14638 {
14639 tree mem_initializer_list = NULL_TREE;
14640 tree target_ctor = error_mark_node;
14641 cp_token *token = cp_lexer_peek_token (parser->lexer);
14642
14643 /* Let the semantic analysis code know that we are starting the
14644 mem-initializer-list. */
14645 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14646 error_at (token->location,
14647 "only constructors take member initializers");
14648
14649 /* Loop through the list. */
14650 while (true)
14651 {
14652 tree mem_initializer;
14653
14654 token = cp_lexer_peek_token (parser->lexer);
14655 /* Parse the mem-initializer. */
14656 mem_initializer = cp_parser_mem_initializer (parser);
14657 /* If the next token is a `...', we're expanding member initializers. */
14658 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14659 if (ellipsis
14660 || (mem_initializer != error_mark_node
14661 && check_for_bare_parameter_packs (TREE_PURPOSE
14662 (mem_initializer))))
14663 {
14664 /* Consume the `...'. */
14665 if (ellipsis)
14666 cp_lexer_consume_token (parser->lexer);
14667
14668 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14669 can be expanded but members cannot. */
14670 if (mem_initializer != error_mark_node
14671 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14672 {
14673 error_at (token->location,
14674 "cannot expand initializer for member %qD",
14675 TREE_PURPOSE (mem_initializer));
14676 mem_initializer = error_mark_node;
14677 }
14678
14679 /* Construct the pack expansion type. */
14680 if (mem_initializer != error_mark_node)
14681 mem_initializer = make_pack_expansion (mem_initializer);
14682 }
14683 if (target_ctor != error_mark_node
14684 && mem_initializer != error_mark_node)
14685 {
14686 error ("mem-initializer for %qD follows constructor delegation",
14687 TREE_PURPOSE (mem_initializer));
14688 mem_initializer = error_mark_node;
14689 }
14690 /* Look for a target constructor. */
14691 if (mem_initializer != error_mark_node
14692 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14693 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14694 {
14695 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14696 if (mem_initializer_list)
14697 {
14698 error ("constructor delegation follows mem-initializer for %qD",
14699 TREE_PURPOSE (mem_initializer_list));
14700 mem_initializer = error_mark_node;
14701 }
14702 target_ctor = mem_initializer;
14703 }
14704 /* Add it to the list, unless it was erroneous. */
14705 if (mem_initializer != error_mark_node)
14706 {
14707 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14708 mem_initializer_list = mem_initializer;
14709 }
14710 /* If the next token is not a `,', we're done. */
14711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14712 break;
14713 /* Consume the `,' token. */
14714 cp_lexer_consume_token (parser->lexer);
14715 }
14716
14717 /* Perform semantic analysis. */
14718 if (DECL_CONSTRUCTOR_P (current_function_decl))
14719 finish_mem_initializers (mem_initializer_list);
14720 }
14721
14722 /* Parse a mem-initializer.
14723
14724 mem-initializer:
14725 mem-initializer-id ( expression-list [opt] )
14726 mem-initializer-id braced-init-list
14727
14728 GNU extension:
14729
14730 mem-initializer:
14731 ( expression-list [opt] )
14732
14733 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14734 class) or FIELD_DECL (for a non-static data member) to initialize;
14735 the TREE_VALUE is the expression-list. An empty initialization
14736 list is represented by void_list_node. */
14737
14738 static tree
14739 cp_parser_mem_initializer (cp_parser* parser)
14740 {
14741 tree mem_initializer_id;
14742 tree expression_list;
14743 tree member;
14744 cp_token *token = cp_lexer_peek_token (parser->lexer);
14745
14746 /* Find out what is being initialized. */
14747 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14748 {
14749 permerror (token->location,
14750 "anachronistic old-style base class initializer");
14751 mem_initializer_id = NULL_TREE;
14752 }
14753 else
14754 {
14755 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14756 if (mem_initializer_id == error_mark_node)
14757 return mem_initializer_id;
14758 }
14759 member = expand_member_init (mem_initializer_id);
14760 if (member && !DECL_P (member))
14761 in_base_initializer = 1;
14762
14763 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14764 {
14765 bool expr_non_constant_p;
14766 cp_lexer_set_source_position (parser->lexer);
14767 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14768 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14769 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14770 expression_list = build_tree_list (NULL_TREE, expression_list);
14771 }
14772 else
14773 {
14774 vec<tree, va_gc> *vec;
14775 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14776 /*cast_p=*/false,
14777 /*allow_expansion_p=*/true,
14778 /*non_constant_p=*/NULL);
14779 if (vec == NULL)
14780 return error_mark_node;
14781 expression_list = build_tree_list_vec (vec);
14782 release_tree_vector (vec);
14783 }
14784
14785 if (expression_list == error_mark_node)
14786 return error_mark_node;
14787 if (!expression_list)
14788 expression_list = void_type_node;
14789
14790 in_base_initializer = 0;
14791
14792 return member ? build_tree_list (member, expression_list) : error_mark_node;
14793 }
14794
14795 /* Parse a mem-initializer-id.
14796
14797 mem-initializer-id:
14798 :: [opt] nested-name-specifier [opt] class-name
14799 decltype-specifier (C++11)
14800 identifier
14801
14802 Returns a TYPE indicating the class to be initialized for the first
14803 production (and the second in C++11). Returns an IDENTIFIER_NODE
14804 indicating the data member to be initialized for the last production. */
14805
14806 static tree
14807 cp_parser_mem_initializer_id (cp_parser* parser)
14808 {
14809 bool global_scope_p;
14810 bool nested_name_specifier_p;
14811 bool template_p = false;
14812 tree id;
14813
14814 cp_token *token = cp_lexer_peek_token (parser->lexer);
14815
14816 /* `typename' is not allowed in this context ([temp.res]). */
14817 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14818 {
14819 error_at (token->location,
14820 "keyword %<typename%> not allowed in this context (a qualified "
14821 "member initializer is implicitly a type)");
14822 cp_lexer_consume_token (parser->lexer);
14823 }
14824 /* Look for the optional `::' operator. */
14825 global_scope_p
14826 = (cp_parser_global_scope_opt (parser,
14827 /*current_scope_valid_p=*/false)
14828 != NULL_TREE);
14829 /* Look for the optional nested-name-specifier. The simplest way to
14830 implement:
14831
14832 [temp.res]
14833
14834 The keyword `typename' is not permitted in a base-specifier or
14835 mem-initializer; in these contexts a qualified name that
14836 depends on a template-parameter is implicitly assumed to be a
14837 type name.
14838
14839 is to assume that we have seen the `typename' keyword at this
14840 point. */
14841 nested_name_specifier_p
14842 = (cp_parser_nested_name_specifier_opt (parser,
14843 /*typename_keyword_p=*/true,
14844 /*check_dependency_p=*/true,
14845 /*type_p=*/true,
14846 /*is_declaration=*/true)
14847 != NULL_TREE);
14848 if (nested_name_specifier_p)
14849 template_p = cp_parser_optional_template_keyword (parser);
14850 /* If there is a `::' operator or a nested-name-specifier, then we
14851 are definitely looking for a class-name. */
14852 if (global_scope_p || nested_name_specifier_p)
14853 return cp_parser_class_name (parser,
14854 /*typename_keyword_p=*/true,
14855 /*template_keyword_p=*/template_p,
14856 typename_type,
14857 /*check_dependency_p=*/true,
14858 /*class_head_p=*/false,
14859 /*is_declaration=*/true);
14860 /* Otherwise, we could also be looking for an ordinary identifier. */
14861 cp_parser_parse_tentatively (parser);
14862 if (cp_lexer_next_token_is_decltype (parser->lexer))
14863 /* Try a decltype-specifier. */
14864 id = cp_parser_decltype (parser);
14865 else
14866 /* Otherwise, try a class-name. */
14867 id = cp_parser_class_name (parser,
14868 /*typename_keyword_p=*/true,
14869 /*template_keyword_p=*/false,
14870 none_type,
14871 /*check_dependency_p=*/true,
14872 /*class_head_p=*/false,
14873 /*is_declaration=*/true);
14874 /* If we found one, we're done. */
14875 if (cp_parser_parse_definitely (parser))
14876 return id;
14877 /* Otherwise, look for an ordinary identifier. */
14878 return cp_parser_identifier (parser);
14879 }
14880
14881 /* Overloading [gram.over] */
14882
14883 /* Parse an operator-function-id.
14884
14885 operator-function-id:
14886 operator operator
14887
14888 Returns an IDENTIFIER_NODE for the operator which is a
14889 human-readable spelling of the identifier, e.g., `operator +'. */
14890
14891 static cp_expr
14892 cp_parser_operator_function_id (cp_parser* parser)
14893 {
14894 /* Look for the `operator' keyword. */
14895 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14896 return error_mark_node;
14897 /* And then the name of the operator itself. */
14898 return cp_parser_operator (parser);
14899 }
14900
14901 /* Return an identifier node for a user-defined literal operator.
14902 The suffix identifier is chained to the operator name identifier. */
14903
14904 tree
14905 cp_literal_operator_id (const char* name)
14906 {
14907 tree identifier;
14908 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14909 + strlen (name) + 10);
14910 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14911 identifier = get_identifier (buffer);
14912
14913 return identifier;
14914 }
14915
14916 /* Parse an operator.
14917
14918 operator:
14919 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14920 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14921 || ++ -- , ->* -> () []
14922
14923 GNU Extensions:
14924
14925 operator:
14926 <? >? <?= >?=
14927
14928 Returns an IDENTIFIER_NODE for the operator which is a
14929 human-readable spelling of the identifier, e.g., `operator +'. */
14930
14931 static cp_expr
14932 cp_parser_operator (cp_parser* parser)
14933 {
14934 tree id = NULL_TREE;
14935 cp_token *token;
14936 bool utf8 = false;
14937
14938 /* Peek at the next token. */
14939 token = cp_lexer_peek_token (parser->lexer);
14940
14941 location_t start_loc = token->location;
14942
14943 /* Figure out which operator we have. */
14944 enum tree_code op = ERROR_MARK;
14945 bool assop = false;
14946 bool consumed = false;
14947 switch (token->type)
14948 {
14949 case CPP_KEYWORD:
14950 {
14951 /* The keyword should be either `new' or `delete'. */
14952 if (token->keyword == RID_NEW)
14953 op = NEW_EXPR;
14954 else if (token->keyword == RID_DELETE)
14955 op = DELETE_EXPR;
14956 else
14957 break;
14958
14959 /* Consume the `new' or `delete' token. */
14960 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14961
14962 /* Peek at the next token. */
14963 token = cp_lexer_peek_token (parser->lexer);
14964 /* If it's a `[' token then this is the array variant of the
14965 operator. */
14966 if (token->type == CPP_OPEN_SQUARE)
14967 {
14968 /* Consume the `[' token. */
14969 cp_lexer_consume_token (parser->lexer);
14970 /* Look for the `]' token. */
14971 if (cp_token *close_token
14972 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14973 end_loc = close_token->location;
14974 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
14975 }
14976 start_loc = make_location (start_loc, start_loc, end_loc);
14977 consumed = true;
14978 break;
14979 }
14980
14981 case CPP_PLUS:
14982 op = PLUS_EXPR;
14983 break;
14984
14985 case CPP_MINUS:
14986 op = MINUS_EXPR;
14987 break;
14988
14989 case CPP_MULT:
14990 op = MULT_EXPR;
14991 break;
14992
14993 case CPP_DIV:
14994 op = TRUNC_DIV_EXPR;
14995 break;
14996
14997 case CPP_MOD:
14998 op = TRUNC_MOD_EXPR;
14999 break;
15000
15001 case CPP_XOR:
15002 op = BIT_XOR_EXPR;
15003 break;
15004
15005 case CPP_AND:
15006 op = BIT_AND_EXPR;
15007 break;
15008
15009 case CPP_OR:
15010 op = BIT_IOR_EXPR;
15011 break;
15012
15013 case CPP_COMPL:
15014 op = BIT_NOT_EXPR;
15015 break;
15016
15017 case CPP_NOT:
15018 op = TRUTH_NOT_EXPR;
15019 break;
15020
15021 case CPP_EQ:
15022 assop = true;
15023 op = NOP_EXPR;
15024 break;
15025
15026 case CPP_LESS:
15027 op = LT_EXPR;
15028 break;
15029
15030 case CPP_GREATER:
15031 op = GT_EXPR;
15032 break;
15033
15034 case CPP_PLUS_EQ:
15035 assop = true;
15036 op = PLUS_EXPR;
15037 break;
15038
15039 case CPP_MINUS_EQ:
15040 assop = true;
15041 op = MINUS_EXPR;
15042 break;
15043
15044 case CPP_MULT_EQ:
15045 assop = true;
15046 op = MULT_EXPR;
15047 break;
15048
15049 case CPP_DIV_EQ:
15050 assop = true;
15051 op = TRUNC_DIV_EXPR;
15052 break;
15053
15054 case CPP_MOD_EQ:
15055 assop = true;
15056 op = TRUNC_MOD_EXPR;
15057 break;
15058
15059 case CPP_XOR_EQ:
15060 assop = true;
15061 op = BIT_XOR_EXPR;
15062 break;
15063
15064 case CPP_AND_EQ:
15065 assop = true;
15066 op = BIT_AND_EXPR;
15067 break;
15068
15069 case CPP_OR_EQ:
15070 assop = true;
15071 op = BIT_IOR_EXPR;
15072 break;
15073
15074 case CPP_LSHIFT:
15075 op = LSHIFT_EXPR;
15076 break;
15077
15078 case CPP_RSHIFT:
15079 op = RSHIFT_EXPR;
15080 break;
15081
15082 case CPP_LSHIFT_EQ:
15083 assop = true;
15084 op = LSHIFT_EXPR;
15085 break;
15086
15087 case CPP_RSHIFT_EQ:
15088 assop = true;
15089 op = RSHIFT_EXPR;
15090 break;
15091
15092 case CPP_EQ_EQ:
15093 op = EQ_EXPR;
15094 break;
15095
15096 case CPP_NOT_EQ:
15097 op = NE_EXPR;
15098 break;
15099
15100 case CPP_LESS_EQ:
15101 op = LE_EXPR;
15102 break;
15103
15104 case CPP_GREATER_EQ:
15105 op = GE_EXPR;
15106 break;
15107
15108 case CPP_AND_AND:
15109 op = TRUTH_ANDIF_EXPR;
15110 break;
15111
15112 case CPP_OR_OR:
15113 op = TRUTH_ORIF_EXPR;
15114 break;
15115
15116 case CPP_PLUS_PLUS:
15117 op = POSTINCREMENT_EXPR;
15118 break;
15119
15120 case CPP_MINUS_MINUS:
15121 op = PREDECREMENT_EXPR;
15122 break;
15123
15124 case CPP_COMMA:
15125 op = COMPOUND_EXPR;
15126 break;
15127
15128 case CPP_DEREF_STAR:
15129 op = MEMBER_REF;
15130 break;
15131
15132 case CPP_DEREF:
15133 op = COMPONENT_REF;
15134 break;
15135
15136 case CPP_OPEN_PAREN:
15137 {
15138 /* Consume the `('. */
15139 matching_parens parens;
15140 parens.consume_open (parser);
15141 /* Look for the matching `)'. */
15142 parens.require_close (parser);
15143 op = CALL_EXPR;
15144 consumed = true;
15145 break;
15146 }
15147
15148 case CPP_OPEN_SQUARE:
15149 /* Consume the `['. */
15150 cp_lexer_consume_token (parser->lexer);
15151 /* Look for the matching `]'. */
15152 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15153 op = ARRAY_REF;
15154 consumed = true;
15155 break;
15156
15157 case CPP_UTF8STRING:
15158 case CPP_UTF8STRING_USERDEF:
15159 utf8 = true;
15160 /* FALLTHRU */
15161 case CPP_STRING:
15162 case CPP_WSTRING:
15163 case CPP_STRING16:
15164 case CPP_STRING32:
15165 case CPP_STRING_USERDEF:
15166 case CPP_WSTRING_USERDEF:
15167 case CPP_STRING16_USERDEF:
15168 case CPP_STRING32_USERDEF:
15169 {
15170 tree str, string_tree;
15171 int sz, len;
15172
15173 if (cxx_dialect == cxx98)
15174 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15175
15176 /* Consume the string. */
15177 str = cp_parser_string_literal (parser, /*translate=*/true,
15178 /*wide_ok=*/true, /*lookup_udlit=*/false);
15179 if (str == error_mark_node)
15180 return error_mark_node;
15181 else if (TREE_CODE (str) == USERDEF_LITERAL)
15182 {
15183 string_tree = USERDEF_LITERAL_VALUE (str);
15184 id = USERDEF_LITERAL_SUFFIX_ID (str);
15185 }
15186 else
15187 {
15188 string_tree = str;
15189 /* Look for the suffix identifier. */
15190 token = cp_lexer_peek_token (parser->lexer);
15191 if (token->type == CPP_NAME)
15192 id = cp_parser_identifier (parser);
15193 else if (token->type == CPP_KEYWORD)
15194 {
15195 error ("unexpected keyword;"
15196 " remove space between quotes and suffix identifier");
15197 return error_mark_node;
15198 }
15199 else
15200 {
15201 error ("expected suffix identifier");
15202 return error_mark_node;
15203 }
15204 }
15205 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15206 (TREE_TYPE (TREE_TYPE (string_tree))));
15207 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15208 if (len != 0)
15209 {
15210 error ("expected empty string after %<operator%> keyword");
15211 return error_mark_node;
15212 }
15213 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15214 != char_type_node)
15215 {
15216 error ("invalid encoding prefix in literal operator");
15217 return error_mark_node;
15218 }
15219 if (id != error_mark_node)
15220 {
15221 const char *name = IDENTIFIER_POINTER (id);
15222 id = cp_literal_operator_id (name);
15223 }
15224 return id;
15225 }
15226
15227 default:
15228 /* Anything else is an error. */
15229 break;
15230 }
15231
15232 /* If we have selected an identifier, we need to consume the
15233 operator token. */
15234 if (op != ERROR_MARK)
15235 {
15236 id = ovl_op_identifier (assop, op);
15237 if (!consumed)
15238 cp_lexer_consume_token (parser->lexer);
15239 }
15240 /* Otherwise, no valid operator name was present. */
15241 else
15242 {
15243 cp_parser_error (parser, "expected operator");
15244 id = error_mark_node;
15245 }
15246
15247 return cp_expr (id, start_loc);
15248 }
15249
15250 /* Parse a template-declaration.
15251
15252 template-declaration:
15253 export [opt] template < template-parameter-list > declaration
15254
15255 If MEMBER_P is TRUE, this template-declaration occurs within a
15256 class-specifier.
15257
15258 The grammar rule given by the standard isn't correct. What
15259 is really meant is:
15260
15261 template-declaration:
15262 export [opt] template-parameter-list-seq
15263 decl-specifier-seq [opt] init-declarator [opt] ;
15264 export [opt] template-parameter-list-seq
15265 function-definition
15266
15267 template-parameter-list-seq:
15268 template-parameter-list-seq [opt]
15269 template < template-parameter-list >
15270
15271 Concept Extensions:
15272
15273 template-parameter-list-seq:
15274 template < template-parameter-list > requires-clause [opt]
15275
15276 requires-clause:
15277 requires logical-or-expression */
15278
15279 static void
15280 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15281 {
15282 /* Check for `export'. */
15283 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15284 {
15285 /* Consume the `export' token. */
15286 cp_lexer_consume_token (parser->lexer);
15287 /* Warn that we do not support `export'. */
15288 warning (0, "keyword %<export%> not implemented, and will be ignored");
15289 }
15290
15291 cp_parser_template_declaration_after_export (parser, member_p);
15292 }
15293
15294 /* Parse a template-parameter-list.
15295
15296 template-parameter-list:
15297 template-parameter
15298 template-parameter-list , template-parameter
15299
15300 Returns a TREE_LIST. Each node represents a template parameter.
15301 The nodes are connected via their TREE_CHAINs. */
15302
15303 static tree
15304 cp_parser_template_parameter_list (cp_parser* parser)
15305 {
15306 tree parameter_list = NULL_TREE;
15307
15308 begin_template_parm_list ();
15309
15310 /* The loop below parses the template parms. We first need to know
15311 the total number of template parms to be able to compute proper
15312 canonical types of each dependent type. So after the loop, when
15313 we know the total number of template parms,
15314 end_template_parm_list computes the proper canonical types and
15315 fixes up the dependent types accordingly. */
15316 while (true)
15317 {
15318 tree parameter;
15319 bool is_non_type;
15320 bool is_parameter_pack;
15321 location_t parm_loc;
15322
15323 /* Parse the template-parameter. */
15324 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15325 parameter = cp_parser_template_parameter (parser,
15326 &is_non_type,
15327 &is_parameter_pack);
15328 /* Add it to the list. */
15329 if (parameter != error_mark_node)
15330 parameter_list = process_template_parm (parameter_list,
15331 parm_loc,
15332 parameter,
15333 is_non_type,
15334 is_parameter_pack);
15335 else
15336 {
15337 tree err_parm = build_tree_list (parameter, parameter);
15338 parameter_list = chainon (parameter_list, err_parm);
15339 }
15340
15341 /* If the next token is not a `,', we're done. */
15342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15343 break;
15344 /* Otherwise, consume the `,' token. */
15345 cp_lexer_consume_token (parser->lexer);
15346 }
15347
15348 return end_template_parm_list (parameter_list);
15349 }
15350
15351 /* Parse a introduction-list.
15352
15353 introduction-list:
15354 introduced-parameter
15355 introduction-list , introduced-parameter
15356
15357 introduced-parameter:
15358 ...[opt] identifier
15359
15360 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15361 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15362 WILDCARD_DECL will also have DECL_NAME set and token location in
15363 DECL_SOURCE_LOCATION. */
15364
15365 static tree
15366 cp_parser_introduction_list (cp_parser *parser)
15367 {
15368 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15369
15370 while (true)
15371 {
15372 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15373 if (is_pack)
15374 cp_lexer_consume_token (parser->lexer);
15375
15376 tree identifier = cp_parser_identifier (parser);
15377 if (identifier == error_mark_node)
15378 break;
15379
15380 /* Build placeholder. */
15381 tree parm = build_nt (WILDCARD_DECL);
15382 DECL_SOURCE_LOCATION (parm)
15383 = cp_lexer_peek_token (parser->lexer)->location;
15384 DECL_NAME (parm) = identifier;
15385 WILDCARD_PACK_P (parm) = is_pack;
15386 vec_safe_push (introduction_vec, parm);
15387
15388 /* If the next token is not a `,', we're done. */
15389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15390 break;
15391 /* Otherwise, consume the `,' token. */
15392 cp_lexer_consume_token (parser->lexer);
15393 }
15394
15395 /* Convert the vec into a TREE_VEC. */
15396 tree introduction_list = make_tree_vec (introduction_vec->length ());
15397 unsigned int n;
15398 tree parm;
15399 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15400 TREE_VEC_ELT (introduction_list, n) = parm;
15401
15402 release_tree_vector (introduction_vec);
15403 return introduction_list;
15404 }
15405
15406 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15407 is an abstract declarator. */
15408
15409 static inline cp_declarator*
15410 get_id_declarator (cp_declarator *declarator)
15411 {
15412 cp_declarator *d = declarator;
15413 while (d && d->kind != cdk_id)
15414 d = d->declarator;
15415 return d;
15416 }
15417
15418 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15419 is an abstract declarator. */
15420
15421 static inline tree
15422 get_unqualified_id (cp_declarator *declarator)
15423 {
15424 declarator = get_id_declarator (declarator);
15425 if (declarator)
15426 return declarator->u.id.unqualified_name;
15427 else
15428 return NULL_TREE;
15429 }
15430
15431 /* Returns true if DECL represents a constrained-parameter. */
15432
15433 static inline bool
15434 is_constrained_parameter (tree decl)
15435 {
15436 return (decl
15437 && TREE_CODE (decl) == TYPE_DECL
15438 && CONSTRAINED_PARM_CONCEPT (decl)
15439 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15440 }
15441
15442 /* Returns true if PARM declares a constrained-parameter. */
15443
15444 static inline bool
15445 is_constrained_parameter (cp_parameter_declarator *parm)
15446 {
15447 return is_constrained_parameter (parm->decl_specifiers.type);
15448 }
15449
15450 /* Check that the type parameter is only a declarator-id, and that its
15451 type is not cv-qualified. */
15452
15453 bool
15454 cp_parser_check_constrained_type_parm (cp_parser *parser,
15455 cp_parameter_declarator *parm)
15456 {
15457 if (!parm->declarator)
15458 return true;
15459
15460 if (parm->declarator->kind != cdk_id)
15461 {
15462 cp_parser_error (parser, "invalid constrained type parameter");
15463 return false;
15464 }
15465
15466 /* Don't allow cv-qualified type parameters. */
15467 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15468 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15469 {
15470 cp_parser_error (parser, "cv-qualified type parameter");
15471 return false;
15472 }
15473
15474 return true;
15475 }
15476
15477 /* Finish parsing/processing a template type parameter and checking
15478 various restrictions. */
15479
15480 static inline tree
15481 cp_parser_constrained_type_template_parm (cp_parser *parser,
15482 tree id,
15483 cp_parameter_declarator* parmdecl)
15484 {
15485 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15486 return finish_template_type_parm (class_type_node, id);
15487 else
15488 return error_mark_node;
15489 }
15490
15491 static tree
15492 finish_constrained_template_template_parm (tree proto, tree id)
15493 {
15494 /* FIXME: This should probably be copied, and we may need to adjust
15495 the template parameter depths. */
15496 tree saved_parms = current_template_parms;
15497 begin_template_parm_list ();
15498 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15499 end_template_parm_list ();
15500
15501 tree parm = finish_template_template_parm (class_type_node, id);
15502 current_template_parms = saved_parms;
15503
15504 return parm;
15505 }
15506
15507 /* Finish parsing/processing a template template parameter by borrowing
15508 the template parameter list from the prototype parameter. */
15509
15510 static tree
15511 cp_parser_constrained_template_template_parm (cp_parser *parser,
15512 tree proto,
15513 tree id,
15514 cp_parameter_declarator *parmdecl)
15515 {
15516 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15517 return error_mark_node;
15518 return finish_constrained_template_template_parm (proto, id);
15519 }
15520
15521 /* Create a new non-type template parameter from the given PARM
15522 declarator. */
15523
15524 static tree
15525 constrained_non_type_template_parm (bool *is_non_type,
15526 cp_parameter_declarator *parm)
15527 {
15528 *is_non_type = true;
15529 cp_declarator *decl = parm->declarator;
15530 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15531 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15532 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15533 }
15534
15535 /* Build a constrained template parameter based on the PARMDECL
15536 declarator. The type of PARMDECL is the constrained type, which
15537 refers to the prototype template parameter that ultimately
15538 specifies the type of the declared parameter. */
15539
15540 static tree
15541 finish_constrained_parameter (cp_parser *parser,
15542 cp_parameter_declarator *parmdecl,
15543 bool *is_non_type,
15544 bool *is_parameter_pack)
15545 {
15546 tree decl = parmdecl->decl_specifiers.type;
15547 tree id = get_unqualified_id (parmdecl->declarator);
15548 tree def = parmdecl->default_argument;
15549 tree proto = DECL_INITIAL (decl);
15550
15551 /* A template parameter constrained by a variadic concept shall also
15552 be declared as a template parameter pack. */
15553 bool is_variadic = template_parameter_pack_p (proto);
15554 if (is_variadic && !*is_parameter_pack)
15555 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15556
15557 /* Build the parameter. Return an error if the declarator was invalid. */
15558 tree parm;
15559 if (TREE_CODE (proto) == TYPE_DECL)
15560 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15561 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15562 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15563 parmdecl);
15564 else
15565 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15566 if (parm == error_mark_node)
15567 return error_mark_node;
15568
15569 /* Finish the parameter decl and create a node attaching the
15570 default argument and constraint. */
15571 parm = build_tree_list (def, parm);
15572 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15573
15574 return parm;
15575 }
15576
15577 /* Returns true if the parsed type actually represents the declaration
15578 of a type template-parameter. */
15579
15580 static inline bool
15581 declares_constrained_type_template_parameter (tree type)
15582 {
15583 return (is_constrained_parameter (type)
15584 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15585 }
15586
15587
15588 /* Returns true if the parsed type actually represents the declaration of
15589 a template template-parameter. */
15590
15591 static bool
15592 declares_constrained_template_template_parameter (tree type)
15593 {
15594 return (is_constrained_parameter (type)
15595 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15596 }
15597
15598 /* Parse a default argument for a type template-parameter.
15599 Note that diagnostics are handled in cp_parser_template_parameter. */
15600
15601 static tree
15602 cp_parser_default_type_template_argument (cp_parser *parser)
15603 {
15604 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15605
15606 /* Consume the `=' token. */
15607 cp_lexer_consume_token (parser->lexer);
15608
15609 cp_token *token = cp_lexer_peek_token (parser->lexer);
15610
15611 /* Parse the default-argument. */
15612 push_deferring_access_checks (dk_no_deferred);
15613 tree default_argument = cp_parser_type_id (parser);
15614 pop_deferring_access_checks ();
15615
15616 if (flag_concepts && type_uses_auto (default_argument))
15617 {
15618 error_at (token->location,
15619 "invalid use of %<auto%> in default template argument");
15620 return error_mark_node;
15621 }
15622
15623 return default_argument;
15624 }
15625
15626 /* Parse a default argument for a template template-parameter. */
15627
15628 static tree
15629 cp_parser_default_template_template_argument (cp_parser *parser)
15630 {
15631 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15632
15633 bool is_template;
15634
15635 /* Consume the `='. */
15636 cp_lexer_consume_token (parser->lexer);
15637 /* Parse the id-expression. */
15638 push_deferring_access_checks (dk_no_deferred);
15639 /* save token before parsing the id-expression, for error
15640 reporting */
15641 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15642 tree default_argument
15643 = cp_parser_id_expression (parser,
15644 /*template_keyword_p=*/false,
15645 /*check_dependency_p=*/true,
15646 /*template_p=*/&is_template,
15647 /*declarator_p=*/false,
15648 /*optional_p=*/false);
15649 if (TREE_CODE (default_argument) == TYPE_DECL)
15650 /* If the id-expression was a template-id that refers to
15651 a template-class, we already have the declaration here,
15652 so no further lookup is needed. */
15653 ;
15654 else
15655 /* Look up the name. */
15656 default_argument
15657 = cp_parser_lookup_name (parser, default_argument,
15658 none_type,
15659 /*is_template=*/is_template,
15660 /*is_namespace=*/false,
15661 /*check_dependency=*/true,
15662 /*ambiguous_decls=*/NULL,
15663 token->location);
15664 /* See if the default argument is valid. */
15665 default_argument = check_template_template_default_arg (default_argument);
15666 pop_deferring_access_checks ();
15667 return default_argument;
15668 }
15669
15670 /* Parse a template-parameter.
15671
15672 template-parameter:
15673 type-parameter
15674 parameter-declaration
15675
15676 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15677 the parameter. The TREE_PURPOSE is the default value, if any.
15678 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15679 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15680 set to true iff this parameter is a parameter pack. */
15681
15682 static tree
15683 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15684 bool *is_parameter_pack)
15685 {
15686 cp_token *token;
15687 cp_parameter_declarator *parameter_declarator;
15688 tree parm;
15689
15690 /* Assume it is a type parameter or a template parameter. */
15691 *is_non_type = false;
15692 /* Assume it not a parameter pack. */
15693 *is_parameter_pack = false;
15694 /* Peek at the next token. */
15695 token = cp_lexer_peek_token (parser->lexer);
15696 /* If it is `template', we have a type-parameter. */
15697 if (token->keyword == RID_TEMPLATE)
15698 return cp_parser_type_parameter (parser, is_parameter_pack);
15699 /* If it is `class' or `typename' we do not know yet whether it is a
15700 type parameter or a non-type parameter. Consider:
15701
15702 template <typename T, typename T::X X> ...
15703
15704 or:
15705
15706 template <class C, class D*> ...
15707
15708 Here, the first parameter is a type parameter, and the second is
15709 a non-type parameter. We can tell by looking at the token after
15710 the identifier -- if it is a `,', `=', or `>' then we have a type
15711 parameter. */
15712 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15713 {
15714 /* Peek at the token after `class' or `typename'. */
15715 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15716 /* If it's an ellipsis, we have a template type parameter
15717 pack. */
15718 if (token->type == CPP_ELLIPSIS)
15719 return cp_parser_type_parameter (parser, is_parameter_pack);
15720 /* If it's an identifier, skip it. */
15721 if (token->type == CPP_NAME)
15722 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15723 /* Now, see if the token looks like the end of a template
15724 parameter. */
15725 if (token->type == CPP_COMMA
15726 || token->type == CPP_EQ
15727 || token->type == CPP_GREATER)
15728 return cp_parser_type_parameter (parser, is_parameter_pack);
15729 }
15730
15731 /* Otherwise, it is a non-type parameter or a constrained parameter.
15732
15733 [temp.param]
15734
15735 When parsing a default template-argument for a non-type
15736 template-parameter, the first non-nested `>' is taken as the end
15737 of the template parameter-list rather than a greater-than
15738 operator. */
15739 parameter_declarator
15740 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15741 /*parenthesized_p=*/NULL);
15742
15743 if (!parameter_declarator)
15744 return error_mark_node;
15745
15746 /* If the parameter declaration is marked as a parameter pack, set
15747 *IS_PARAMETER_PACK to notify the caller. */
15748 if (parameter_declarator->template_parameter_pack_p)
15749 *is_parameter_pack = true;
15750
15751 if (parameter_declarator->default_argument)
15752 {
15753 /* Can happen in some cases of erroneous input (c++/34892). */
15754 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15755 /* Consume the `...' for better error recovery. */
15756 cp_lexer_consume_token (parser->lexer);
15757 }
15758
15759 // The parameter may have been constrained.
15760 if (is_constrained_parameter (parameter_declarator))
15761 return finish_constrained_parameter (parser,
15762 parameter_declarator,
15763 is_non_type,
15764 is_parameter_pack);
15765
15766 // Now we're sure that the parameter is a non-type parameter.
15767 *is_non_type = true;
15768
15769 parm = grokdeclarator (parameter_declarator->declarator,
15770 &parameter_declarator->decl_specifiers,
15771 TPARM, /*initialized=*/0,
15772 /*attrlist=*/NULL);
15773 if (parm == error_mark_node)
15774 return error_mark_node;
15775
15776 return build_tree_list (parameter_declarator->default_argument, parm);
15777 }
15778
15779 /* Parse a type-parameter.
15780
15781 type-parameter:
15782 class identifier [opt]
15783 class identifier [opt] = type-id
15784 typename identifier [opt]
15785 typename identifier [opt] = type-id
15786 template < template-parameter-list > class identifier [opt]
15787 template < template-parameter-list > class identifier [opt]
15788 = id-expression
15789
15790 GNU Extension (variadic templates):
15791
15792 type-parameter:
15793 class ... identifier [opt]
15794 typename ... identifier [opt]
15795
15796 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15797 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15798 the declaration of the parameter.
15799
15800 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15801
15802 static tree
15803 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15804 {
15805 cp_token *token;
15806 tree parameter;
15807
15808 /* Look for a keyword to tell us what kind of parameter this is. */
15809 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15810 if (!token)
15811 return error_mark_node;
15812
15813 switch (token->keyword)
15814 {
15815 case RID_CLASS:
15816 case RID_TYPENAME:
15817 {
15818 tree identifier;
15819 tree default_argument;
15820
15821 /* If the next token is an ellipsis, we have a template
15822 argument pack. */
15823 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15824 {
15825 /* Consume the `...' token. */
15826 cp_lexer_consume_token (parser->lexer);
15827 maybe_warn_variadic_templates ();
15828
15829 *is_parameter_pack = true;
15830 }
15831
15832 /* If the next token is an identifier, then it names the
15833 parameter. */
15834 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15835 identifier = cp_parser_identifier (parser);
15836 else
15837 identifier = NULL_TREE;
15838
15839 /* Create the parameter. */
15840 parameter = finish_template_type_parm (class_type_node, identifier);
15841
15842 /* If the next token is an `=', we have a default argument. */
15843 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15844 {
15845 default_argument
15846 = cp_parser_default_type_template_argument (parser);
15847
15848 /* Template parameter packs cannot have default
15849 arguments. */
15850 if (*is_parameter_pack)
15851 {
15852 if (identifier)
15853 error_at (token->location,
15854 "template parameter pack %qD cannot have a "
15855 "default argument", identifier);
15856 else
15857 error_at (token->location,
15858 "template parameter packs cannot have "
15859 "default arguments");
15860 default_argument = NULL_TREE;
15861 }
15862 else if (check_for_bare_parameter_packs (default_argument))
15863 default_argument = error_mark_node;
15864 }
15865 else
15866 default_argument = NULL_TREE;
15867
15868 /* Create the combined representation of the parameter and the
15869 default argument. */
15870 parameter = build_tree_list (default_argument, parameter);
15871 }
15872 break;
15873
15874 case RID_TEMPLATE:
15875 {
15876 tree identifier;
15877 tree default_argument;
15878
15879 /* Look for the `<'. */
15880 cp_parser_require (parser, CPP_LESS, RT_LESS);
15881 /* Parse the template-parameter-list. */
15882 cp_parser_template_parameter_list (parser);
15883 /* Look for the `>'. */
15884 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15885
15886 // If template requirements are present, parse them.
15887 if (flag_concepts)
15888 {
15889 tree reqs = get_shorthand_constraints (current_template_parms);
15890 if (tree r = cp_parser_requires_clause_opt (parser))
15891 reqs = conjoin_constraints (reqs, normalize_expression (r));
15892 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15893 }
15894
15895 /* Look for the `class' or 'typename' keywords. */
15896 cp_parser_type_parameter_key (parser);
15897 /* If the next token is an ellipsis, we have a template
15898 argument pack. */
15899 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15900 {
15901 /* Consume the `...' token. */
15902 cp_lexer_consume_token (parser->lexer);
15903 maybe_warn_variadic_templates ();
15904
15905 *is_parameter_pack = true;
15906 }
15907 /* If the next token is an `=', then there is a
15908 default-argument. If the next token is a `>', we are at
15909 the end of the parameter-list. If the next token is a `,',
15910 then we are at the end of this parameter. */
15911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15912 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15913 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15914 {
15915 identifier = cp_parser_identifier (parser);
15916 /* Treat invalid names as if the parameter were nameless. */
15917 if (identifier == error_mark_node)
15918 identifier = NULL_TREE;
15919 }
15920 else
15921 identifier = NULL_TREE;
15922
15923 /* Create the template parameter. */
15924 parameter = finish_template_template_parm (class_type_node,
15925 identifier);
15926
15927 /* If the next token is an `=', then there is a
15928 default-argument. */
15929 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15930 {
15931 default_argument
15932 = cp_parser_default_template_template_argument (parser);
15933
15934 /* Template parameter packs cannot have default
15935 arguments. */
15936 if (*is_parameter_pack)
15937 {
15938 if (identifier)
15939 error_at (token->location,
15940 "template parameter pack %qD cannot "
15941 "have a default argument",
15942 identifier);
15943 else
15944 error_at (token->location, "template parameter packs cannot "
15945 "have default arguments");
15946 default_argument = NULL_TREE;
15947 }
15948 }
15949 else
15950 default_argument = NULL_TREE;
15951
15952 /* Create the combined representation of the parameter and the
15953 default argument. */
15954 parameter = build_tree_list (default_argument, parameter);
15955 }
15956 break;
15957
15958 default:
15959 gcc_unreachable ();
15960 break;
15961 }
15962
15963 return parameter;
15964 }
15965
15966 /* Parse a template-id.
15967
15968 template-id:
15969 template-name < template-argument-list [opt] >
15970
15971 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15972 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15973 returned. Otherwise, if the template-name names a function, or set
15974 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15975 names a class, returns a TYPE_DECL for the specialization.
15976
15977 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15978 uninstantiated templates. */
15979
15980 static tree
15981 cp_parser_template_id (cp_parser *parser,
15982 bool template_keyword_p,
15983 bool check_dependency_p,
15984 enum tag_types tag_type,
15985 bool is_declaration)
15986 {
15987 tree templ;
15988 tree arguments;
15989 tree template_id;
15990 cp_token_position start_of_id = 0;
15991 cp_token *next_token = NULL, *next_token_2 = NULL;
15992 bool is_identifier;
15993
15994 /* If the next token corresponds to a template-id, there is no need
15995 to reparse it. */
15996 cp_token *token = cp_lexer_peek_token (parser->lexer);
15997 if (token->type == CPP_TEMPLATE_ID)
15998 {
15999 cp_lexer_consume_token (parser->lexer);
16000 return saved_checks_value (token->u.tree_check_value);
16001 }
16002
16003 /* Avoid performing name lookup if there is no possibility of
16004 finding a template-id. */
16005 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16006 || (token->type == CPP_NAME
16007 && !cp_parser_nth_token_starts_template_argument_list_p
16008 (parser, 2)))
16009 {
16010 cp_parser_error (parser, "expected template-id");
16011 return error_mark_node;
16012 }
16013
16014 /* Remember where the template-id starts. */
16015 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16016 start_of_id = cp_lexer_token_position (parser->lexer, false);
16017
16018 push_deferring_access_checks (dk_deferred);
16019
16020 /* Parse the template-name. */
16021 is_identifier = false;
16022 templ = cp_parser_template_name (parser, template_keyword_p,
16023 check_dependency_p,
16024 is_declaration,
16025 tag_type,
16026 &is_identifier);
16027 if (templ == error_mark_node || is_identifier)
16028 {
16029 pop_deferring_access_checks ();
16030 return templ;
16031 }
16032
16033 /* Since we're going to preserve any side-effects from this parse, set up a
16034 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16035 in the template arguments. */
16036 tentative_firewall firewall (parser);
16037
16038 /* If we find the sequence `[:' after a template-name, it's probably
16039 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16040 parse correctly the argument list. */
16041 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16042 == CPP_OPEN_SQUARE)
16043 && next_token->flags & DIGRAPH
16044 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16045 == CPP_COLON)
16046 && !(next_token_2->flags & PREV_WHITE))
16047 {
16048 cp_parser_parse_tentatively (parser);
16049 /* Change `:' into `::'. */
16050 next_token_2->type = CPP_SCOPE;
16051 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16052 CPP_LESS. */
16053 cp_lexer_consume_token (parser->lexer);
16054
16055 /* Parse the arguments. */
16056 arguments = cp_parser_enclosed_template_argument_list (parser);
16057 if (!cp_parser_parse_definitely (parser))
16058 {
16059 /* If we couldn't parse an argument list, then we revert our changes
16060 and return simply an error. Maybe this is not a template-id
16061 after all. */
16062 next_token_2->type = CPP_COLON;
16063 cp_parser_error (parser, "expected %<<%>");
16064 pop_deferring_access_checks ();
16065 return error_mark_node;
16066 }
16067 /* Otherwise, emit an error about the invalid digraph, but continue
16068 parsing because we got our argument list. */
16069 if (permerror (next_token->location,
16070 "%<<::%> cannot begin a template-argument list"))
16071 {
16072 static bool hint = false;
16073 inform (next_token->location,
16074 "%<<:%> is an alternate spelling for %<[%>."
16075 " Insert whitespace between %<<%> and %<::%>");
16076 if (!hint && !flag_permissive)
16077 {
16078 inform (next_token->location, "(if you use %<-fpermissive%> "
16079 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16080 "accept your code)");
16081 hint = true;
16082 }
16083 }
16084 }
16085 else
16086 {
16087 /* Look for the `<' that starts the template-argument-list. */
16088 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16089 {
16090 pop_deferring_access_checks ();
16091 return error_mark_node;
16092 }
16093 /* Parse the arguments. */
16094 arguments = cp_parser_enclosed_template_argument_list (parser);
16095
16096 if ((cxx_dialect > cxx17)
16097 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16098 && !template_keyword_p
16099 && (cp_parser_error_occurred (parser)
16100 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16101 {
16102 /* This didn't go well. */
16103 if (TREE_CODE (templ) == FUNCTION_DECL)
16104 {
16105 /* C++2A says that "function-name < a;" is now ill-formed. */
16106 if (cp_parser_error_occurred (parser))
16107 {
16108 error_at (token->location, "invalid template-argument-list");
16109 inform (token->location, "function name as the left hand "
16110 "operand of %<<%> is ill-formed in C++2a; wrap the "
16111 "function name in %<()%>");
16112 }
16113 else
16114 /* We expect "f<targs>" to be followed by "(args)". */
16115 error_at (cp_lexer_peek_token (parser->lexer)->location,
16116 "expected %<(%> after template-argument-list");
16117 if (start_of_id)
16118 /* Purge all subsequent tokens. */
16119 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16120 }
16121 else
16122 cp_parser_simulate_error (parser);
16123 pop_deferring_access_checks ();
16124 return error_mark_node;
16125 }
16126 }
16127
16128 /* Set the location to be of the form:
16129 template-name < template-argument-list [opt] >
16130 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16131 with caret == start at the start of the template-name,
16132 ranging until the closing '>'. */
16133 location_t finish_loc
16134 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16135 location_t combined_loc
16136 = make_location (token->location, token->location, finish_loc);
16137
16138 /* Check for concepts autos where they don't belong. We could
16139 identify types in some cases of idnetifier TEMPL, looking ahead
16140 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16141 types. We reject them in functions, but if what we have is an
16142 identifier, even with none_type we can't conclude it's NOT a
16143 type, we have to wait for template substitution. */
16144 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16145 template_id = error_mark_node;
16146 /* Build a representation of the specialization. */
16147 else if (identifier_p (templ))
16148 template_id = build_min_nt_loc (combined_loc,
16149 TEMPLATE_ID_EXPR,
16150 templ, arguments);
16151 else if (DECL_TYPE_TEMPLATE_P (templ)
16152 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16153 {
16154 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16155 template (rather than some instantiation thereof) only if
16156 is not nested within some other construct. For example, in
16157 "template <typename T> void f(T) { A<T>::", A<T> is just an
16158 instantiation of A. */
16159 bool entering_scope
16160 = (template_parm_scope_p ()
16161 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16162 template_id
16163 = finish_template_type (templ, arguments, entering_scope);
16164 }
16165 /* A template-like identifier may be a partial concept id. */
16166 else if (flag_concepts
16167 && (template_id = (cp_parser_maybe_partial_concept_id
16168 (parser, templ, arguments))))
16169 return template_id;
16170 else if (variable_template_p (templ))
16171 {
16172 template_id = lookup_template_variable (templ, arguments);
16173 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16174 SET_EXPR_LOCATION (template_id, combined_loc);
16175 }
16176 else
16177 {
16178 /* If it's not a class-template or a template-template, it should be
16179 a function-template. */
16180 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16181 || TREE_CODE (templ) == OVERLOAD
16182 || TREE_CODE (templ) == FUNCTION_DECL
16183 || BASELINK_P (templ)));
16184
16185 template_id = lookup_template_function (templ, arguments);
16186 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16187 SET_EXPR_LOCATION (template_id, combined_loc);
16188 }
16189
16190 /* If parsing tentatively, replace the sequence of tokens that makes
16191 up the template-id with a CPP_TEMPLATE_ID token. That way,
16192 should we re-parse the token stream, we will not have to repeat
16193 the effort required to do the parse, nor will we issue duplicate
16194 error messages about problems during instantiation of the
16195 template. */
16196 if (start_of_id
16197 /* Don't do this if we had a parse error in a declarator; re-parsing
16198 might succeed if a name changes meaning (60361). */
16199 && !(cp_parser_error_occurred (parser)
16200 && cp_parser_parsing_tentatively (parser)
16201 && parser->in_declarator_p))
16202 {
16203 /* Reset the contents of the START_OF_ID token. */
16204 token->type = CPP_TEMPLATE_ID;
16205 token->location = combined_loc;
16206
16207 /* Retrieve any deferred checks. Do not pop this access checks yet
16208 so the memory will not be reclaimed during token replacing below. */
16209 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16210 token->u.tree_check_value->value = template_id;
16211 token->u.tree_check_value->checks = get_deferred_access_checks ();
16212 token->keyword = RID_MAX;
16213
16214 /* Purge all subsequent tokens. */
16215 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16216
16217 /* ??? Can we actually assume that, if template_id ==
16218 error_mark_node, we will have issued a diagnostic to the
16219 user, as opposed to simply marking the tentative parse as
16220 failed? */
16221 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16222 error_at (token->location, "parse error in template argument list");
16223 }
16224
16225 pop_to_parent_deferring_access_checks ();
16226 return template_id;
16227 }
16228
16229 /* Parse a template-name.
16230
16231 template-name:
16232 identifier
16233
16234 The standard should actually say:
16235
16236 template-name:
16237 identifier
16238 operator-function-id
16239
16240 A defect report has been filed about this issue.
16241
16242 A conversion-function-id cannot be a template name because they cannot
16243 be part of a template-id. In fact, looking at this code:
16244
16245 a.operator K<int>()
16246
16247 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16248 It is impossible to call a templated conversion-function-id with an
16249 explicit argument list, since the only allowed template parameter is
16250 the type to which it is converting.
16251
16252 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16253 `template' keyword, in a construction like:
16254
16255 T::template f<3>()
16256
16257 In that case `f' is taken to be a template-name, even though there
16258 is no way of knowing for sure.
16259
16260 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16261 name refers to a set of overloaded functions, at least one of which
16262 is a template, or an IDENTIFIER_NODE with the name of the template,
16263 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16264 names are looked up inside uninstantiated templates. */
16265
16266 static tree
16267 cp_parser_template_name (cp_parser* parser,
16268 bool template_keyword_p,
16269 bool check_dependency_p,
16270 bool is_declaration,
16271 enum tag_types tag_type,
16272 bool *is_identifier)
16273 {
16274 tree identifier;
16275 tree decl;
16276 cp_token *token = cp_lexer_peek_token (parser->lexer);
16277
16278 /* If the next token is `operator', then we have either an
16279 operator-function-id or a conversion-function-id. */
16280 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16281 {
16282 /* We don't know whether we're looking at an
16283 operator-function-id or a conversion-function-id. */
16284 cp_parser_parse_tentatively (parser);
16285 /* Try an operator-function-id. */
16286 identifier = cp_parser_operator_function_id (parser);
16287 /* If that didn't work, try a conversion-function-id. */
16288 if (!cp_parser_parse_definitely (parser))
16289 {
16290 cp_parser_error (parser, "expected template-name");
16291 return error_mark_node;
16292 }
16293 }
16294 /* Look for the identifier. */
16295 else
16296 identifier = cp_parser_identifier (parser);
16297
16298 /* If we didn't find an identifier, we don't have a template-id. */
16299 if (identifier == error_mark_node)
16300 return error_mark_node;
16301
16302 /* If the name immediately followed the `template' keyword, then it
16303 is a template-name. However, if the next token is not `<', then
16304 we do not treat it as a template-name, since it is not being used
16305 as part of a template-id. This enables us to handle constructs
16306 like:
16307
16308 template <typename T> struct S { S(); };
16309 template <typename T> S<T>::S();
16310
16311 correctly. We would treat `S' as a template -- if it were `S<T>'
16312 -- but we do not if there is no `<'. */
16313
16314 if (processing_template_decl
16315 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16316 {
16317 /* In a declaration, in a dependent context, we pretend that the
16318 "template" keyword was present in order to improve error
16319 recovery. For example, given:
16320
16321 template <typename T> void f(T::X<int>);
16322
16323 we want to treat "X<int>" as a template-id. */
16324 if (is_declaration
16325 && !template_keyword_p
16326 && parser->scope && TYPE_P (parser->scope)
16327 && check_dependency_p
16328 && dependent_scope_p (parser->scope)
16329 /* Do not do this for dtors (or ctors), since they never
16330 need the template keyword before their name. */
16331 && !constructor_name_p (identifier, parser->scope))
16332 {
16333 cp_token_position start = 0;
16334
16335 /* Explain what went wrong. */
16336 error_at (token->location, "non-template %qD used as template",
16337 identifier);
16338 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16339 parser->scope, identifier);
16340 /* If parsing tentatively, find the location of the "<" token. */
16341 if (cp_parser_simulate_error (parser))
16342 start = cp_lexer_token_position (parser->lexer, true);
16343 /* Parse the template arguments so that we can issue error
16344 messages about them. */
16345 cp_lexer_consume_token (parser->lexer);
16346 cp_parser_enclosed_template_argument_list (parser);
16347 /* Skip tokens until we find a good place from which to
16348 continue parsing. */
16349 cp_parser_skip_to_closing_parenthesis (parser,
16350 /*recovering=*/true,
16351 /*or_comma=*/true,
16352 /*consume_paren=*/false);
16353 /* If parsing tentatively, permanently remove the
16354 template argument list. That will prevent duplicate
16355 error messages from being issued about the missing
16356 "template" keyword. */
16357 if (start)
16358 cp_lexer_purge_tokens_after (parser->lexer, start);
16359 if (is_identifier)
16360 *is_identifier = true;
16361 parser->context->object_type = NULL_TREE;
16362 return identifier;
16363 }
16364
16365 /* If the "template" keyword is present, then there is generally
16366 no point in doing name-lookup, so we just return IDENTIFIER.
16367 But, if the qualifying scope is non-dependent then we can
16368 (and must) do name-lookup normally. */
16369 if (template_keyword_p)
16370 {
16371 tree scope = (parser->scope ? parser->scope
16372 : parser->context->object_type);
16373 if (scope && TYPE_P (scope)
16374 && (!CLASS_TYPE_P (scope)
16375 || (check_dependency_p && dependent_type_p (scope))))
16376 {
16377 /* We're optimizing away the call to cp_parser_lookup_name, but
16378 we still need to do this. */
16379 parser->context->object_type = NULL_TREE;
16380 return identifier;
16381 }
16382 }
16383 }
16384
16385 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16386 const bool scoped_p = ((parser->scope ? parser->scope
16387 : parser->context->object_type) != NULL_TREE);
16388
16389 /* Look up the name. */
16390 decl = cp_parser_lookup_name (parser, identifier,
16391 tag_type,
16392 /*is_template=*/true,
16393 /*is_namespace=*/false,
16394 check_dependency_p,
16395 /*ambiguous_decls=*/NULL,
16396 token->location);
16397
16398 decl = strip_using_decl (decl);
16399
16400 /* If DECL is a template, then the name was a template-name. */
16401 if (TREE_CODE (decl) == TEMPLATE_DECL)
16402 {
16403 if (TREE_DEPRECATED (decl)
16404 && deprecated_state != DEPRECATED_SUPPRESS)
16405 warn_deprecated_use (decl, NULL_TREE);
16406 }
16407 else
16408 {
16409 /* The standard does not explicitly indicate whether a name that
16410 names a set of overloaded declarations, some of which are
16411 templates, is a template-name. However, such a name should
16412 be a template-name; otherwise, there is no way to form a
16413 template-id for the overloaded templates. */
16414 bool found = false;
16415
16416 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16417 !found && iter; ++iter)
16418 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16419 found = true;
16420
16421 if (!found
16422 && (cxx_dialect > cxx17)
16423 && !scoped_p
16424 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16425 {
16426 /* [temp.names] says "A name is also considered to refer to a template
16427 if it is an unqualified-id followed by a < and name lookup finds
16428 either one or more functions or finds nothing." */
16429
16430 /* The "more functions" case. Just use the OVERLOAD as normally.
16431 We don't use is_overloaded_fn here to avoid considering
16432 BASELINKs. */
16433 if (TREE_CODE (decl) == OVERLOAD
16434 /* Name lookup found one function. */
16435 || TREE_CODE (decl) == FUNCTION_DECL)
16436 found = true;
16437 /* Name lookup found nothing. */
16438 else if (decl == error_mark_node)
16439 return identifier;
16440 }
16441
16442 if (!found)
16443 {
16444 /* The name does not name a template. */
16445 cp_parser_error (parser, "expected template-name");
16446 return error_mark_node;
16447 }
16448 }
16449
16450 return decl;
16451 }
16452
16453 /* Parse a template-argument-list.
16454
16455 template-argument-list:
16456 template-argument ... [opt]
16457 template-argument-list , template-argument ... [opt]
16458
16459 Returns a TREE_VEC containing the arguments. */
16460
16461 static tree
16462 cp_parser_template_argument_list (cp_parser* parser)
16463 {
16464 tree fixed_args[10];
16465 unsigned n_args = 0;
16466 unsigned alloced = 10;
16467 tree *arg_ary = fixed_args;
16468 tree vec;
16469 bool saved_in_template_argument_list_p;
16470 bool saved_ice_p;
16471 bool saved_non_ice_p;
16472
16473 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16474 parser->in_template_argument_list_p = true;
16475 /* Even if the template-id appears in an integral
16476 constant-expression, the contents of the argument list do
16477 not. */
16478 saved_ice_p = parser->integral_constant_expression_p;
16479 parser->integral_constant_expression_p = false;
16480 saved_non_ice_p = parser->non_integral_constant_expression_p;
16481 parser->non_integral_constant_expression_p = false;
16482
16483 /* Parse the arguments. */
16484 do
16485 {
16486 tree argument;
16487
16488 if (n_args)
16489 /* Consume the comma. */
16490 cp_lexer_consume_token (parser->lexer);
16491
16492 /* Parse the template-argument. */
16493 argument = cp_parser_template_argument (parser);
16494
16495 /* If the next token is an ellipsis, we're expanding a template
16496 argument pack. */
16497 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16498 {
16499 if (argument == error_mark_node)
16500 {
16501 cp_token *token = cp_lexer_peek_token (parser->lexer);
16502 error_at (token->location,
16503 "expected parameter pack before %<...%>");
16504 }
16505 /* Consume the `...' token. */
16506 cp_lexer_consume_token (parser->lexer);
16507
16508 /* Make the argument into a TYPE_PACK_EXPANSION or
16509 EXPR_PACK_EXPANSION. */
16510 argument = make_pack_expansion (argument);
16511 }
16512
16513 if (n_args == alloced)
16514 {
16515 alloced *= 2;
16516
16517 if (arg_ary == fixed_args)
16518 {
16519 arg_ary = XNEWVEC (tree, alloced);
16520 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16521 }
16522 else
16523 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16524 }
16525 arg_ary[n_args++] = argument;
16526 }
16527 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16528
16529 vec = make_tree_vec (n_args);
16530
16531 while (n_args--)
16532 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16533
16534 if (arg_ary != fixed_args)
16535 free (arg_ary);
16536 parser->non_integral_constant_expression_p = saved_non_ice_p;
16537 parser->integral_constant_expression_p = saved_ice_p;
16538 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16539 if (CHECKING_P)
16540 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16541 return vec;
16542 }
16543
16544 /* Parse a template-argument.
16545
16546 template-argument:
16547 assignment-expression
16548 type-id
16549 id-expression
16550
16551 The representation is that of an assignment-expression, type-id, or
16552 id-expression -- except that the qualified id-expression is
16553 evaluated, so that the value returned is either a DECL or an
16554 OVERLOAD.
16555
16556 Although the standard says "assignment-expression", it forbids
16557 throw-expressions or assignments in the template argument.
16558 Therefore, we use "conditional-expression" instead. */
16559
16560 static tree
16561 cp_parser_template_argument (cp_parser* parser)
16562 {
16563 tree argument;
16564 bool template_p;
16565 bool address_p;
16566 bool maybe_type_id = false;
16567 cp_token *token = NULL, *argument_start_token = NULL;
16568 location_t loc = 0;
16569 cp_id_kind idk;
16570
16571 /* There's really no way to know what we're looking at, so we just
16572 try each alternative in order.
16573
16574 [temp.arg]
16575
16576 In a template-argument, an ambiguity between a type-id and an
16577 expression is resolved to a type-id, regardless of the form of
16578 the corresponding template-parameter.
16579
16580 Therefore, we try a type-id first. */
16581 cp_parser_parse_tentatively (parser);
16582 argument = cp_parser_template_type_arg (parser);
16583 /* If there was no error parsing the type-id but the next token is a
16584 '>>', our behavior depends on which dialect of C++ we're
16585 parsing. In C++98, we probably found a typo for '> >'. But there
16586 are type-id which are also valid expressions. For instance:
16587
16588 struct X { int operator >> (int); };
16589 template <int V> struct Foo {};
16590 Foo<X () >> 5> r;
16591
16592 Here 'X()' is a valid type-id of a function type, but the user just
16593 wanted to write the expression "X() >> 5". Thus, we remember that we
16594 found a valid type-id, but we still try to parse the argument as an
16595 expression to see what happens.
16596
16597 In C++0x, the '>>' will be considered two separate '>'
16598 tokens. */
16599 if (!cp_parser_error_occurred (parser)
16600 && cxx_dialect == cxx98
16601 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16602 {
16603 maybe_type_id = true;
16604 cp_parser_abort_tentative_parse (parser);
16605 }
16606 else
16607 {
16608 /* If the next token isn't a `,' or a `>', then this argument wasn't
16609 really finished. This means that the argument is not a valid
16610 type-id. */
16611 if (!cp_parser_next_token_ends_template_argument_p (parser))
16612 cp_parser_error (parser, "expected template-argument");
16613 /* If that worked, we're done. */
16614 if (cp_parser_parse_definitely (parser))
16615 return argument;
16616 }
16617 /* We're still not sure what the argument will be. */
16618 cp_parser_parse_tentatively (parser);
16619 /* Try a template. */
16620 argument_start_token = cp_lexer_peek_token (parser->lexer);
16621 argument = cp_parser_id_expression (parser,
16622 /*template_keyword_p=*/false,
16623 /*check_dependency_p=*/true,
16624 &template_p,
16625 /*declarator_p=*/false,
16626 /*optional_p=*/false);
16627 /* If the next token isn't a `,' or a `>', then this argument wasn't
16628 really finished. */
16629 if (!cp_parser_next_token_ends_template_argument_p (parser))
16630 cp_parser_error (parser, "expected template-argument");
16631 if (!cp_parser_error_occurred (parser))
16632 {
16633 /* Figure out what is being referred to. If the id-expression
16634 was for a class template specialization, then we will have a
16635 TYPE_DECL at this point. There is no need to do name lookup
16636 at this point in that case. */
16637 if (TREE_CODE (argument) != TYPE_DECL)
16638 argument = cp_parser_lookup_name (parser, argument,
16639 none_type,
16640 /*is_template=*/template_p,
16641 /*is_namespace=*/false,
16642 /*check_dependency=*/true,
16643 /*ambiguous_decls=*/NULL,
16644 argument_start_token->location);
16645 /* Handle a constrained-type-specifier for a non-type template
16646 parameter. */
16647 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16648 argument = decl;
16649 else if (TREE_CODE (argument) != TEMPLATE_DECL
16650 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16651 cp_parser_error (parser, "expected template-name");
16652 }
16653 if (cp_parser_parse_definitely (parser))
16654 {
16655 if (TREE_DEPRECATED (argument))
16656 warn_deprecated_use (argument, NULL_TREE);
16657 return argument;
16658 }
16659 /* It must be a non-type argument. In C++17 any constant-expression is
16660 allowed. */
16661 if (cxx_dialect > cxx14)
16662 goto general_expr;
16663
16664 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16665
16666 -- an integral constant-expression of integral or enumeration
16667 type; or
16668
16669 -- the name of a non-type template-parameter; or
16670
16671 -- the name of an object or function with external linkage...
16672
16673 -- the address of an object or function with external linkage...
16674
16675 -- a pointer to member... */
16676 /* Look for a non-type template parameter. */
16677 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16678 {
16679 cp_parser_parse_tentatively (parser);
16680 argument = cp_parser_primary_expression (parser,
16681 /*address_p=*/false,
16682 /*cast_p=*/false,
16683 /*template_arg_p=*/true,
16684 &idk);
16685 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16686 || !cp_parser_next_token_ends_template_argument_p (parser))
16687 cp_parser_simulate_error (parser);
16688 if (cp_parser_parse_definitely (parser))
16689 return argument;
16690 }
16691
16692 /* If the next token is "&", the argument must be the address of an
16693 object or function with external linkage. */
16694 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16695 if (address_p)
16696 {
16697 loc = cp_lexer_peek_token (parser->lexer)->location;
16698 cp_lexer_consume_token (parser->lexer);
16699 }
16700 /* See if we might have an id-expression. */
16701 token = cp_lexer_peek_token (parser->lexer);
16702 if (token->type == CPP_NAME
16703 || token->keyword == RID_OPERATOR
16704 || token->type == CPP_SCOPE
16705 || token->type == CPP_TEMPLATE_ID
16706 || token->type == CPP_NESTED_NAME_SPECIFIER)
16707 {
16708 cp_parser_parse_tentatively (parser);
16709 argument = cp_parser_primary_expression (parser,
16710 address_p,
16711 /*cast_p=*/false,
16712 /*template_arg_p=*/true,
16713 &idk);
16714 if (cp_parser_error_occurred (parser)
16715 || !cp_parser_next_token_ends_template_argument_p (parser))
16716 cp_parser_abort_tentative_parse (parser);
16717 else
16718 {
16719 tree probe;
16720
16721 if (INDIRECT_REF_P (argument))
16722 {
16723 /* Strip the dereference temporarily. */
16724 gcc_assert (REFERENCE_REF_P (argument));
16725 argument = TREE_OPERAND (argument, 0);
16726 }
16727
16728 /* If we're in a template, we represent a qualified-id referring
16729 to a static data member as a SCOPE_REF even if the scope isn't
16730 dependent so that we can check access control later. */
16731 probe = argument;
16732 if (TREE_CODE (probe) == SCOPE_REF)
16733 probe = TREE_OPERAND (probe, 1);
16734 if (VAR_P (probe))
16735 {
16736 /* A variable without external linkage might still be a
16737 valid constant-expression, so no error is issued here
16738 if the external-linkage check fails. */
16739 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16740 cp_parser_simulate_error (parser);
16741 }
16742 else if (is_overloaded_fn (argument))
16743 /* All overloaded functions are allowed; if the external
16744 linkage test does not pass, an error will be issued
16745 later. */
16746 ;
16747 else if (address_p
16748 && (TREE_CODE (argument) == OFFSET_REF
16749 || TREE_CODE (argument) == SCOPE_REF))
16750 /* A pointer-to-member. */
16751 ;
16752 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16753 ;
16754 else
16755 cp_parser_simulate_error (parser);
16756
16757 if (cp_parser_parse_definitely (parser))
16758 {
16759 if (address_p)
16760 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16761 tf_warning_or_error);
16762 else
16763 argument = convert_from_reference (argument);
16764 return argument;
16765 }
16766 }
16767 }
16768 /* If the argument started with "&", there are no other valid
16769 alternatives at this point. */
16770 if (address_p)
16771 {
16772 cp_parser_error (parser, "invalid non-type template argument");
16773 return error_mark_node;
16774 }
16775
16776 general_expr:
16777 /* If the argument wasn't successfully parsed as a type-id followed
16778 by '>>', the argument can only be a constant expression now.
16779 Otherwise, we try parsing the constant-expression tentatively,
16780 because the argument could really be a type-id. */
16781 if (maybe_type_id)
16782 cp_parser_parse_tentatively (parser);
16783
16784 if (cxx_dialect <= cxx14)
16785 argument = cp_parser_constant_expression (parser);
16786 else
16787 {
16788 /* With C++17 generalized non-type template arguments we need to handle
16789 lvalue constant expressions, too. */
16790 argument = cp_parser_assignment_expression (parser);
16791 require_potential_constant_expression (argument);
16792 }
16793
16794 if (!maybe_type_id)
16795 return argument;
16796 if (!cp_parser_next_token_ends_template_argument_p (parser))
16797 cp_parser_error (parser, "expected template-argument");
16798 if (cp_parser_parse_definitely (parser))
16799 return argument;
16800 /* We did our best to parse the argument as a non type-id, but that
16801 was the only alternative that matched (albeit with a '>' after
16802 it). We can assume it's just a typo from the user, and a
16803 diagnostic will then be issued. */
16804 return cp_parser_template_type_arg (parser);
16805 }
16806
16807 /* Parse an explicit-instantiation.
16808
16809 explicit-instantiation:
16810 template declaration
16811
16812 Although the standard says `declaration', what it really means is:
16813
16814 explicit-instantiation:
16815 template decl-specifier-seq [opt] declarator [opt] ;
16816
16817 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16818 supposed to be allowed. A defect report has been filed about this
16819 issue.
16820
16821 GNU Extension:
16822
16823 explicit-instantiation:
16824 storage-class-specifier template
16825 decl-specifier-seq [opt] declarator [opt] ;
16826 function-specifier template
16827 decl-specifier-seq [opt] declarator [opt] ; */
16828
16829 static void
16830 cp_parser_explicit_instantiation (cp_parser* parser)
16831 {
16832 int declares_class_or_enum;
16833 cp_decl_specifier_seq decl_specifiers;
16834 tree extension_specifier = NULL_TREE;
16835
16836 timevar_push (TV_TEMPLATE_INST);
16837
16838 /* Look for an (optional) storage-class-specifier or
16839 function-specifier. */
16840 if (cp_parser_allow_gnu_extensions_p (parser))
16841 {
16842 extension_specifier
16843 = cp_parser_storage_class_specifier_opt (parser);
16844 if (!extension_specifier)
16845 extension_specifier
16846 = cp_parser_function_specifier_opt (parser,
16847 /*decl_specs=*/NULL);
16848 }
16849
16850 /* Look for the `template' keyword. */
16851 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16852 /* Let the front end know that we are processing an explicit
16853 instantiation. */
16854 begin_explicit_instantiation ();
16855 /* [temp.explicit] says that we are supposed to ignore access
16856 control while processing explicit instantiation directives. */
16857 push_deferring_access_checks (dk_no_check);
16858 /* Parse a decl-specifier-seq. */
16859 cp_parser_decl_specifier_seq (parser,
16860 CP_PARSER_FLAGS_OPTIONAL,
16861 &decl_specifiers,
16862 &declares_class_or_enum);
16863 /* If there was exactly one decl-specifier, and it declared a class,
16864 and there's no declarator, then we have an explicit type
16865 instantiation. */
16866 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16867 {
16868 tree type;
16869
16870 type = check_tag_decl (&decl_specifiers,
16871 /*explicit_type_instantiation_p=*/true);
16872 /* Turn access control back on for names used during
16873 template instantiation. */
16874 pop_deferring_access_checks ();
16875 if (type)
16876 do_type_instantiation (type, extension_specifier,
16877 /*complain=*/tf_error);
16878 }
16879 else
16880 {
16881 cp_declarator *declarator;
16882 tree decl;
16883
16884 /* Parse the declarator. */
16885 declarator
16886 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16887 /*ctor_dtor_or_conv_p=*/NULL,
16888 /*parenthesized_p=*/NULL,
16889 /*member_p=*/false,
16890 /*friend_p=*/false);
16891 if (declares_class_or_enum & 2)
16892 cp_parser_check_for_definition_in_return_type (declarator,
16893 decl_specifiers.type,
16894 decl_specifiers.locations[ds_type_spec]);
16895 if (declarator != cp_error_declarator)
16896 {
16897 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16898 permerror (decl_specifiers.locations[ds_inline],
16899 "explicit instantiation shall not use"
16900 " %<inline%> specifier");
16901 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16902 permerror (decl_specifiers.locations[ds_constexpr],
16903 "explicit instantiation shall not use"
16904 " %<constexpr%> specifier");
16905
16906 decl = grokdeclarator (declarator, &decl_specifiers,
16907 NORMAL, 0, &decl_specifiers.attributes);
16908 /* Turn access control back on for names used during
16909 template instantiation. */
16910 pop_deferring_access_checks ();
16911 /* Do the explicit instantiation. */
16912 do_decl_instantiation (decl, extension_specifier);
16913 }
16914 else
16915 {
16916 pop_deferring_access_checks ();
16917 /* Skip the body of the explicit instantiation. */
16918 cp_parser_skip_to_end_of_statement (parser);
16919 }
16920 }
16921 /* We're done with the instantiation. */
16922 end_explicit_instantiation ();
16923
16924 cp_parser_consume_semicolon_at_end_of_statement (parser);
16925
16926 timevar_pop (TV_TEMPLATE_INST);
16927 }
16928
16929 /* Parse an explicit-specialization.
16930
16931 explicit-specialization:
16932 template < > declaration
16933
16934 Although the standard says `declaration', what it really means is:
16935
16936 explicit-specialization:
16937 template <> decl-specifier [opt] init-declarator [opt] ;
16938 template <> function-definition
16939 template <> explicit-specialization
16940 template <> template-declaration */
16941
16942 static void
16943 cp_parser_explicit_specialization (cp_parser* parser)
16944 {
16945 bool need_lang_pop;
16946 cp_token *token = cp_lexer_peek_token (parser->lexer);
16947
16948 /* Look for the `template' keyword. */
16949 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16950 /* Look for the `<'. */
16951 cp_parser_require (parser, CPP_LESS, RT_LESS);
16952 /* Look for the `>'. */
16953 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16954 /* We have processed another parameter list. */
16955 ++parser->num_template_parameter_lists;
16956 /* [temp]
16957
16958 A template ... explicit specialization ... shall not have C
16959 linkage. */
16960 if (current_lang_name == lang_name_c)
16961 {
16962 error_at (token->location, "template specialization with C linkage");
16963 maybe_show_extern_c_location ();
16964 /* Give it C++ linkage to avoid confusing other parts of the
16965 front end. */
16966 push_lang_context (lang_name_cplusplus);
16967 need_lang_pop = true;
16968 }
16969 else
16970 need_lang_pop = false;
16971 /* Let the front end know that we are beginning a specialization. */
16972 if (!begin_specialization ())
16973 {
16974 end_specialization ();
16975 return;
16976 }
16977
16978 /* If the next keyword is `template', we need to figure out whether
16979 or not we're looking a template-declaration. */
16980 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16981 {
16982 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16983 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16984 cp_parser_template_declaration_after_export (parser,
16985 /*member_p=*/false);
16986 else
16987 cp_parser_explicit_specialization (parser);
16988 }
16989 else
16990 /* Parse the dependent declaration. */
16991 cp_parser_single_declaration (parser,
16992 /*checks=*/NULL,
16993 /*member_p=*/false,
16994 /*explicit_specialization_p=*/true,
16995 /*friend_p=*/NULL);
16996 /* We're done with the specialization. */
16997 end_specialization ();
16998 /* For the erroneous case of a template with C linkage, we pushed an
16999 implicit C++ linkage scope; exit that scope now. */
17000 if (need_lang_pop)
17001 pop_lang_context ();
17002 /* We're done with this parameter list. */
17003 --parser->num_template_parameter_lists;
17004 }
17005
17006 /* Parse a type-specifier.
17007
17008 type-specifier:
17009 simple-type-specifier
17010 class-specifier
17011 enum-specifier
17012 elaborated-type-specifier
17013 cv-qualifier
17014
17015 GNU Extension:
17016
17017 type-specifier:
17018 __complex__
17019
17020 Returns a representation of the type-specifier. For a
17021 class-specifier, enum-specifier, or elaborated-type-specifier, a
17022 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17023
17024 The parser flags FLAGS is used to control type-specifier parsing.
17025
17026 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17027 in a decl-specifier-seq.
17028
17029 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17030 class-specifier, enum-specifier, or elaborated-type-specifier, then
17031 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17032 if a type is declared; 2 if it is defined. Otherwise, it is set to
17033 zero.
17034
17035 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17036 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17037 is set to FALSE. */
17038
17039 static tree
17040 cp_parser_type_specifier (cp_parser* parser,
17041 cp_parser_flags flags,
17042 cp_decl_specifier_seq *decl_specs,
17043 bool is_declaration,
17044 int* declares_class_or_enum,
17045 bool* is_cv_qualifier)
17046 {
17047 tree type_spec = NULL_TREE;
17048 cp_token *token;
17049 enum rid keyword;
17050 cp_decl_spec ds = ds_last;
17051
17052 /* Assume this type-specifier does not declare a new type. */
17053 if (declares_class_or_enum)
17054 *declares_class_or_enum = 0;
17055 /* And that it does not specify a cv-qualifier. */
17056 if (is_cv_qualifier)
17057 *is_cv_qualifier = false;
17058 /* Peek at the next token. */
17059 token = cp_lexer_peek_token (parser->lexer);
17060
17061 /* If we're looking at a keyword, we can use that to guide the
17062 production we choose. */
17063 keyword = token->keyword;
17064 switch (keyword)
17065 {
17066 case RID_ENUM:
17067 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17068 goto elaborated_type_specifier;
17069
17070 /* Look for the enum-specifier. */
17071 type_spec = cp_parser_enum_specifier (parser);
17072 /* If that worked, we're done. */
17073 if (type_spec)
17074 {
17075 if (declares_class_or_enum)
17076 *declares_class_or_enum = 2;
17077 if (decl_specs)
17078 cp_parser_set_decl_spec_type (decl_specs,
17079 type_spec,
17080 token,
17081 /*type_definition_p=*/true);
17082 return type_spec;
17083 }
17084 else
17085 goto elaborated_type_specifier;
17086
17087 /* Any of these indicate either a class-specifier, or an
17088 elaborated-type-specifier. */
17089 case RID_CLASS:
17090 case RID_STRUCT:
17091 case RID_UNION:
17092 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17093 goto elaborated_type_specifier;
17094
17095 /* Parse tentatively so that we can back up if we don't find a
17096 class-specifier. */
17097 cp_parser_parse_tentatively (parser);
17098 /* Look for the class-specifier. */
17099 type_spec = cp_parser_class_specifier (parser);
17100 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17101 /* If that worked, we're done. */
17102 if (cp_parser_parse_definitely (parser))
17103 {
17104 if (declares_class_or_enum)
17105 *declares_class_or_enum = 2;
17106 if (decl_specs)
17107 cp_parser_set_decl_spec_type (decl_specs,
17108 type_spec,
17109 token,
17110 /*type_definition_p=*/true);
17111 return type_spec;
17112 }
17113
17114 /* Fall through. */
17115 elaborated_type_specifier:
17116 /* We're declaring (not defining) a class or enum. */
17117 if (declares_class_or_enum)
17118 *declares_class_or_enum = 1;
17119
17120 /* Fall through. */
17121 case RID_TYPENAME:
17122 /* Look for an elaborated-type-specifier. */
17123 type_spec
17124 = (cp_parser_elaborated_type_specifier
17125 (parser,
17126 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17127 is_declaration));
17128 if (decl_specs)
17129 cp_parser_set_decl_spec_type (decl_specs,
17130 type_spec,
17131 token,
17132 /*type_definition_p=*/false);
17133 return type_spec;
17134
17135 case RID_CONST:
17136 ds = ds_const;
17137 if (is_cv_qualifier)
17138 *is_cv_qualifier = true;
17139 break;
17140
17141 case RID_VOLATILE:
17142 ds = ds_volatile;
17143 if (is_cv_qualifier)
17144 *is_cv_qualifier = true;
17145 break;
17146
17147 case RID_RESTRICT:
17148 ds = ds_restrict;
17149 if (is_cv_qualifier)
17150 *is_cv_qualifier = true;
17151 break;
17152
17153 case RID_COMPLEX:
17154 /* The `__complex__' keyword is a GNU extension. */
17155 ds = ds_complex;
17156 break;
17157
17158 default:
17159 break;
17160 }
17161
17162 /* Handle simple keywords. */
17163 if (ds != ds_last)
17164 {
17165 if (decl_specs)
17166 {
17167 set_and_check_decl_spec_loc (decl_specs, ds, token);
17168 decl_specs->any_specifiers_p = true;
17169 }
17170 return cp_lexer_consume_token (parser->lexer)->u.value;
17171 }
17172
17173 /* If we do not already have a type-specifier, assume we are looking
17174 at a simple-type-specifier. */
17175 type_spec = cp_parser_simple_type_specifier (parser,
17176 decl_specs,
17177 flags);
17178
17179 /* If we didn't find a type-specifier, and a type-specifier was not
17180 optional in this context, issue an error message. */
17181 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17182 {
17183 cp_parser_error (parser, "expected type specifier");
17184 return error_mark_node;
17185 }
17186
17187 return type_spec;
17188 }
17189
17190 /* Parse a simple-type-specifier.
17191
17192 simple-type-specifier:
17193 :: [opt] nested-name-specifier [opt] type-name
17194 :: [opt] nested-name-specifier template template-id
17195 char
17196 wchar_t
17197 bool
17198 short
17199 int
17200 long
17201 signed
17202 unsigned
17203 float
17204 double
17205 void
17206
17207 C++11 Extension:
17208
17209 simple-type-specifier:
17210 auto
17211 decltype ( expression )
17212 char16_t
17213 char32_t
17214 __underlying_type ( type-id )
17215
17216 C++17 extension:
17217
17218 nested-name-specifier(opt) template-name
17219
17220 GNU Extension:
17221
17222 simple-type-specifier:
17223 __int128
17224 __typeof__ unary-expression
17225 __typeof__ ( type-id )
17226 __typeof__ ( type-id ) { initializer-list , [opt] }
17227
17228 Concepts Extension:
17229
17230 simple-type-specifier:
17231 constrained-type-specifier
17232
17233 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17234 appropriately updated. */
17235
17236 static tree
17237 cp_parser_simple_type_specifier (cp_parser* parser,
17238 cp_decl_specifier_seq *decl_specs,
17239 cp_parser_flags flags)
17240 {
17241 tree type = NULL_TREE;
17242 cp_token *token;
17243 int idx;
17244
17245 /* Peek at the next token. */
17246 token = cp_lexer_peek_token (parser->lexer);
17247
17248 /* If we're looking at a keyword, things are easy. */
17249 switch (token->keyword)
17250 {
17251 case RID_CHAR:
17252 if (decl_specs)
17253 decl_specs->explicit_char_p = true;
17254 type = char_type_node;
17255 break;
17256 case RID_CHAR16:
17257 type = char16_type_node;
17258 break;
17259 case RID_CHAR32:
17260 type = char32_type_node;
17261 break;
17262 case RID_WCHAR:
17263 type = wchar_type_node;
17264 break;
17265 case RID_BOOL:
17266 type = boolean_type_node;
17267 break;
17268 case RID_SHORT:
17269 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17270 type = short_integer_type_node;
17271 break;
17272 case RID_INT:
17273 if (decl_specs)
17274 decl_specs->explicit_int_p = true;
17275 type = integer_type_node;
17276 break;
17277 case RID_INT_N_0:
17278 case RID_INT_N_1:
17279 case RID_INT_N_2:
17280 case RID_INT_N_3:
17281 idx = token->keyword - RID_INT_N_0;
17282 if (! int_n_enabled_p [idx])
17283 break;
17284 if (decl_specs)
17285 {
17286 decl_specs->explicit_intN_p = true;
17287 decl_specs->int_n_idx = idx;
17288 }
17289 type = int_n_trees [idx].signed_type;
17290 break;
17291 case RID_LONG:
17292 if (decl_specs)
17293 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17294 type = long_integer_type_node;
17295 break;
17296 case RID_SIGNED:
17297 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17298 type = integer_type_node;
17299 break;
17300 case RID_UNSIGNED:
17301 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17302 type = unsigned_type_node;
17303 break;
17304 case RID_FLOAT:
17305 type = float_type_node;
17306 break;
17307 case RID_DOUBLE:
17308 type = double_type_node;
17309 break;
17310 case RID_VOID:
17311 type = void_type_node;
17312 break;
17313
17314 case RID_AUTO:
17315 maybe_warn_cpp0x (CPP0X_AUTO);
17316 if (parser->auto_is_implicit_function_template_parm_p)
17317 {
17318 /* The 'auto' might be the placeholder return type for a function decl
17319 with trailing return type. */
17320 bool have_trailing_return_fn_decl = false;
17321
17322 cp_parser_parse_tentatively (parser);
17323 cp_lexer_consume_token (parser->lexer);
17324 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17325 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17326 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17327 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17328 {
17329 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17330 {
17331 cp_lexer_consume_token (parser->lexer);
17332 cp_parser_skip_to_closing_parenthesis (parser,
17333 /*recovering*/false,
17334 /*or_comma*/false,
17335 /*consume_paren*/true);
17336 continue;
17337 }
17338
17339 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17340 {
17341 have_trailing_return_fn_decl = true;
17342 break;
17343 }
17344
17345 cp_lexer_consume_token (parser->lexer);
17346 }
17347 cp_parser_abort_tentative_parse (parser);
17348
17349 if (have_trailing_return_fn_decl)
17350 {
17351 type = make_auto ();
17352 break;
17353 }
17354
17355 if (cxx_dialect >= cxx14)
17356 {
17357 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17358 type = TREE_TYPE (type);
17359 }
17360 else
17361 type = error_mark_node;
17362
17363 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17364 {
17365 if (cxx_dialect < cxx14)
17366 error_at (token->location,
17367 "use of %<auto%> in lambda parameter declaration "
17368 "only available with "
17369 "-std=c++14 or -std=gnu++14");
17370 }
17371 else if (cxx_dialect < cxx14)
17372 error_at (token->location,
17373 "use of %<auto%> in parameter declaration "
17374 "only available with "
17375 "-std=c++14 or -std=gnu++14");
17376 else if (!flag_concepts)
17377 pedwarn (token->location, 0,
17378 "use of %<auto%> in parameter declaration "
17379 "only available with -fconcepts");
17380 }
17381 else
17382 type = make_auto ();
17383 break;
17384
17385 case RID_DECLTYPE:
17386 /* Since DR 743, decltype can either be a simple-type-specifier by
17387 itself or begin a nested-name-specifier. Parsing it will replace
17388 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17389 handling below decide what to do. */
17390 cp_parser_decltype (parser);
17391 cp_lexer_set_token_position (parser->lexer, token);
17392 break;
17393
17394 case RID_TYPEOF:
17395 /* Consume the `typeof' token. */
17396 cp_lexer_consume_token (parser->lexer);
17397 /* Parse the operand to `typeof'. */
17398 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17399 /* If it is not already a TYPE, take its type. */
17400 if (!TYPE_P (type))
17401 type = finish_typeof (type);
17402
17403 if (decl_specs)
17404 cp_parser_set_decl_spec_type (decl_specs, type,
17405 token,
17406 /*type_definition_p=*/false);
17407
17408 return type;
17409
17410 case RID_UNDERLYING_TYPE:
17411 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17412 if (decl_specs)
17413 cp_parser_set_decl_spec_type (decl_specs, type,
17414 token,
17415 /*type_definition_p=*/false);
17416
17417 return type;
17418
17419 case RID_BASES:
17420 case RID_DIRECT_BASES:
17421 type = cp_parser_trait_expr (parser, token->keyword);
17422 if (decl_specs)
17423 cp_parser_set_decl_spec_type (decl_specs, type,
17424 token,
17425 /*type_definition_p=*/false);
17426 return type;
17427 default:
17428 break;
17429 }
17430
17431 /* If token is an already-parsed decltype not followed by ::,
17432 it's a simple-type-specifier. */
17433 if (token->type == CPP_DECLTYPE
17434 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17435 {
17436 type = saved_checks_value (token->u.tree_check_value);
17437 if (decl_specs)
17438 {
17439 cp_parser_set_decl_spec_type (decl_specs, type,
17440 token,
17441 /*type_definition_p=*/false);
17442 /* Remember that we are handling a decltype in order to
17443 implement the resolution of DR 1510 when the argument
17444 isn't instantiation dependent. */
17445 decl_specs->decltype_p = true;
17446 }
17447 cp_lexer_consume_token (parser->lexer);
17448 return type;
17449 }
17450
17451 /* If the type-specifier was for a built-in type, we're done. */
17452 if (type)
17453 {
17454 /* Record the type. */
17455 if (decl_specs
17456 && (token->keyword != RID_SIGNED
17457 && token->keyword != RID_UNSIGNED
17458 && token->keyword != RID_SHORT
17459 && token->keyword != RID_LONG))
17460 cp_parser_set_decl_spec_type (decl_specs,
17461 type,
17462 token,
17463 /*type_definition_p=*/false);
17464 if (decl_specs)
17465 decl_specs->any_specifiers_p = true;
17466
17467 /* Consume the token. */
17468 cp_lexer_consume_token (parser->lexer);
17469
17470 if (type == error_mark_node)
17471 return error_mark_node;
17472
17473 /* There is no valid C++ program where a non-template type is
17474 followed by a "<". That usually indicates that the user thought
17475 that the type was a template. */
17476 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17477 token->location);
17478
17479 return TYPE_NAME (type);
17480 }
17481
17482 /* The type-specifier must be a user-defined type. */
17483 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17484 {
17485 bool qualified_p;
17486 bool global_p;
17487
17488 /* Don't gobble tokens or issue error messages if this is an
17489 optional type-specifier. */
17490 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17491 cp_parser_parse_tentatively (parser);
17492
17493 token = cp_lexer_peek_token (parser->lexer);
17494
17495 /* Look for the optional `::' operator. */
17496 global_p
17497 = (cp_parser_global_scope_opt (parser,
17498 /*current_scope_valid_p=*/false)
17499 != NULL_TREE);
17500 /* Look for the nested-name specifier. */
17501 qualified_p
17502 = (cp_parser_nested_name_specifier_opt (parser,
17503 /*typename_keyword_p=*/false,
17504 /*check_dependency_p=*/true,
17505 /*type_p=*/false,
17506 /*is_declaration=*/false)
17507 != NULL_TREE);
17508 /* If we have seen a nested-name-specifier, and the next token
17509 is `template', then we are using the template-id production. */
17510 if (parser->scope
17511 && cp_parser_optional_template_keyword (parser))
17512 {
17513 /* Look for the template-id. */
17514 type = cp_parser_template_id (parser,
17515 /*template_keyword_p=*/true,
17516 /*check_dependency_p=*/true,
17517 none_type,
17518 /*is_declaration=*/false);
17519 /* If the template-id did not name a type, we are out of
17520 luck. */
17521 if (TREE_CODE (type) != TYPE_DECL)
17522 {
17523 cp_parser_error (parser, "expected template-id for type");
17524 type = NULL_TREE;
17525 }
17526 }
17527 /* Otherwise, look for a type-name. */
17528 else
17529 type = cp_parser_type_name (parser);
17530 /* Keep track of all name-lookups performed in class scopes. */
17531 if (type
17532 && !global_p
17533 && !qualified_p
17534 && TREE_CODE (type) == TYPE_DECL
17535 && identifier_p (DECL_NAME (type)))
17536 maybe_note_name_used_in_class (DECL_NAME (type), type);
17537 /* If it didn't work out, we don't have a TYPE. */
17538 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17539 && !cp_parser_parse_definitely (parser))
17540 type = NULL_TREE;
17541 if (!type && cxx_dialect >= cxx17)
17542 {
17543 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17544 cp_parser_parse_tentatively (parser);
17545
17546 cp_parser_global_scope_opt (parser,
17547 /*current_scope_valid_p=*/false);
17548 cp_parser_nested_name_specifier_opt (parser,
17549 /*typename_keyword_p=*/false,
17550 /*check_dependency_p=*/true,
17551 /*type_p=*/false,
17552 /*is_declaration=*/false);
17553 tree name = cp_parser_identifier (parser);
17554 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17555 && parser->scope != error_mark_node)
17556 {
17557 tree tmpl = cp_parser_lookup_name (parser, name,
17558 none_type,
17559 /*is_template=*/false,
17560 /*is_namespace=*/false,
17561 /*check_dependency=*/true,
17562 /*ambiguous_decls=*/NULL,
17563 token->location);
17564 if (tmpl && tmpl != error_mark_node
17565 && (DECL_CLASS_TEMPLATE_P (tmpl)
17566 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17567 type = make_template_placeholder (tmpl);
17568 else
17569 {
17570 type = error_mark_node;
17571 if (!cp_parser_simulate_error (parser))
17572 cp_parser_name_lookup_error (parser, name, tmpl,
17573 NLE_TYPE, token->location);
17574 }
17575 }
17576 else
17577 type = error_mark_node;
17578
17579 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17580 && !cp_parser_parse_definitely (parser))
17581 type = NULL_TREE;
17582 }
17583 if (type && decl_specs)
17584 cp_parser_set_decl_spec_type (decl_specs, type,
17585 token,
17586 /*type_definition_p=*/false);
17587 }
17588
17589 /* If we didn't get a type-name, issue an error message. */
17590 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17591 {
17592 cp_parser_error (parser, "expected type-name");
17593 return error_mark_node;
17594 }
17595
17596 if (type && type != error_mark_node)
17597 {
17598 /* See if TYPE is an Objective-C type, and if so, parse and
17599 accept any protocol references following it. Do this before
17600 the cp_parser_check_for_invalid_template_id() call, because
17601 Objective-C types can be followed by '<...>' which would
17602 enclose protocol names rather than template arguments, and so
17603 everything is fine. */
17604 if (c_dialect_objc () && !parser->scope
17605 && (objc_is_id (type) || objc_is_class_name (type)))
17606 {
17607 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17608 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17609
17610 /* Clobber the "unqualified" type previously entered into
17611 DECL_SPECS with the new, improved protocol-qualified version. */
17612 if (decl_specs)
17613 decl_specs->type = qual_type;
17614
17615 return qual_type;
17616 }
17617
17618 /* There is no valid C++ program where a non-template type is
17619 followed by a "<". That usually indicates that the user
17620 thought that the type was a template. */
17621 cp_parser_check_for_invalid_template_id (parser, type,
17622 none_type,
17623 token->location);
17624 }
17625
17626 return type;
17627 }
17628
17629 /* Parse a type-name.
17630
17631 type-name:
17632 class-name
17633 enum-name
17634 typedef-name
17635 simple-template-id [in c++0x]
17636
17637 enum-name:
17638 identifier
17639
17640 typedef-name:
17641 identifier
17642
17643 Concepts:
17644
17645 type-name:
17646 concept-name
17647 partial-concept-id
17648
17649 concept-name:
17650 identifier
17651
17652 Returns a TYPE_DECL for the type. */
17653
17654 static tree
17655 cp_parser_type_name (cp_parser* parser)
17656 {
17657 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17658 }
17659
17660 /* See above. */
17661 static tree
17662 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17663 {
17664 tree type_decl;
17665
17666 /* We can't know yet whether it is a class-name or not. */
17667 cp_parser_parse_tentatively (parser);
17668 /* Try a class-name. */
17669 type_decl = cp_parser_class_name (parser,
17670 typename_keyword_p,
17671 /*template_keyword_p=*/false,
17672 none_type,
17673 /*check_dependency_p=*/true,
17674 /*class_head_p=*/false,
17675 /*is_declaration=*/false);
17676 /* If it's not a class-name, keep looking. */
17677 if (!cp_parser_parse_definitely (parser))
17678 {
17679 if (cxx_dialect < cxx11)
17680 /* It must be a typedef-name or an enum-name. */
17681 return cp_parser_nonclass_name (parser);
17682
17683 cp_parser_parse_tentatively (parser);
17684 /* It is either a simple-template-id representing an
17685 instantiation of an alias template... */
17686 type_decl = cp_parser_template_id (parser,
17687 /*template_keyword_p=*/false,
17688 /*check_dependency_p=*/true,
17689 none_type,
17690 /*is_declaration=*/false);
17691 /* Note that this must be an instantiation of an alias template
17692 because [temp.names]/6 says:
17693
17694 A template-id that names an alias template specialization
17695 is a type-name.
17696
17697 Whereas [temp.names]/7 says:
17698
17699 A simple-template-id that names a class template
17700 specialization is a class-name.
17701
17702 With concepts, this could also be a partial-concept-id that
17703 declares a non-type template parameter. */
17704 if (type_decl != NULL_TREE
17705 && TREE_CODE (type_decl) == TYPE_DECL
17706 && TYPE_DECL_ALIAS_P (type_decl))
17707 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17708 else if (is_constrained_parameter (type_decl))
17709 /* Don't do anything. */ ;
17710 else
17711 cp_parser_simulate_error (parser);
17712
17713 if (!cp_parser_parse_definitely (parser))
17714 /* ... Or a typedef-name or an enum-name. */
17715 return cp_parser_nonclass_name (parser);
17716 }
17717
17718 return type_decl;
17719 }
17720
17721 /* Check if DECL and ARGS can form a constrained-type-specifier.
17722 If ARGS is non-null, we try to form a concept check of the
17723 form DECL<?, ARGS> where ? is a wildcard that matches any
17724 kind of template argument. If ARGS is NULL, then we try to
17725 form a concept check of the form DECL<?>. */
17726
17727 static tree
17728 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17729 tree decl, tree args)
17730 {
17731 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17732
17733 /* If we a constrained-type-specifier cannot be deduced. */
17734 if (parser->prevent_constrained_type_specifiers)
17735 return NULL_TREE;
17736
17737 /* A constrained type specifier can only be found in an
17738 overload set or as a reference to a template declaration.
17739
17740 FIXME: This might be masking a bug. It's possible that
17741 that the deduction below is causing template specializations
17742 to be formed with the wildcard as an argument. */
17743 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17744 return NULL_TREE;
17745
17746 /* Try to build a call expression that evaluates the
17747 concept. This can fail if the overload set refers
17748 only to non-templates. */
17749 tree placeholder = build_nt (WILDCARD_DECL);
17750 tree check = build_concept_check (decl, placeholder, args);
17751 if (check == error_mark_node)
17752 return NULL_TREE;
17753
17754 /* Deduce the checked constraint and the prototype parameter.
17755
17756 FIXME: In certain cases, failure to deduce should be a
17757 diagnosable error. */
17758 tree conc;
17759 tree proto;
17760 if (!deduce_constrained_parameter (check, conc, proto))
17761 return NULL_TREE;
17762
17763 /* In template parameter scope, this results in a constrained
17764 parameter. Return a descriptor of that parm. */
17765 if (processing_template_parmlist)
17766 return build_constrained_parameter (conc, proto, args);
17767
17768 /* In a parameter-declaration-clause, constrained-type
17769 specifiers result in invented template parameters. */
17770 if (parser->auto_is_implicit_function_template_parm_p)
17771 {
17772 tree x = build_constrained_parameter (conc, proto, args);
17773 return synthesize_implicit_template_parm (parser, x);
17774 }
17775 else
17776 {
17777 /* Otherwise, we're in a context where the constrained
17778 type name is deduced and the constraint applies
17779 after deduction. */
17780 return make_constrained_auto (conc, args);
17781 }
17782
17783 return NULL_TREE;
17784 }
17785
17786 /* If DECL refers to a concept, return a TYPE_DECL representing
17787 the result of using the constrained type specifier in the
17788 current context. DECL refers to a concept if
17789
17790 - it is an overload set containing a function concept taking a single
17791 type argument, or
17792
17793 - it is a variable concept taking a single type argument. */
17794
17795 static tree
17796 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17797 {
17798 if (flag_concepts
17799 && (TREE_CODE (decl) == OVERLOAD
17800 || BASELINK_P (decl)
17801 || variable_concept_p (decl)))
17802 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17803 else
17804 return NULL_TREE;
17805 }
17806
17807 /* Check if DECL and ARGS form a partial-concept-id. If so,
17808 assign ID to the resulting constrained placeholder.
17809
17810 Returns true if the partial-concept-id designates a placeholder
17811 and false otherwise. Note that *id is set to NULL_TREE in
17812 this case. */
17813
17814 static tree
17815 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17816 {
17817 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17818 }
17819
17820 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17821 or a concept-name.
17822
17823 enum-name:
17824 identifier
17825
17826 typedef-name:
17827 identifier
17828
17829 concept-name:
17830 identifier
17831
17832 Returns a TYPE_DECL for the type. */
17833
17834 static tree
17835 cp_parser_nonclass_name (cp_parser* parser)
17836 {
17837 tree type_decl;
17838 tree identifier;
17839
17840 cp_token *token = cp_lexer_peek_token (parser->lexer);
17841 identifier = cp_parser_identifier (parser);
17842 if (identifier == error_mark_node)
17843 return error_mark_node;
17844
17845 /* Look up the type-name. */
17846 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17847
17848 type_decl = strip_using_decl (type_decl);
17849
17850 /* If we found an overload set, then it may refer to a concept-name. */
17851 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17852 type_decl = decl;
17853
17854 if (TREE_CODE (type_decl) != TYPE_DECL
17855 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17856 {
17857 /* See if this is an Objective-C type. */
17858 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17859 tree type = objc_get_protocol_qualified_type (identifier, protos);
17860 if (type)
17861 type_decl = TYPE_NAME (type);
17862 }
17863
17864 /* Issue an error if we did not find a type-name. */
17865 if (TREE_CODE (type_decl) != TYPE_DECL
17866 /* In Objective-C, we have the complication that class names are
17867 normally type names and start declarations (eg, the
17868 "NSObject" in "NSObject *object;"), but can be used in an
17869 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17870 is an expression. So, a classname followed by a dot is not a
17871 valid type-name. */
17872 || (objc_is_class_name (TREE_TYPE (type_decl))
17873 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17874 {
17875 if (!cp_parser_simulate_error (parser))
17876 cp_parser_name_lookup_error (parser, identifier, type_decl,
17877 NLE_TYPE, token->location);
17878 return error_mark_node;
17879 }
17880 /* Remember that the name was used in the definition of the
17881 current class so that we can check later to see if the
17882 meaning would have been different after the class was
17883 entirely defined. */
17884 else if (type_decl != error_mark_node
17885 && !parser->scope)
17886 maybe_note_name_used_in_class (identifier, type_decl);
17887
17888 return type_decl;
17889 }
17890
17891 /* Parse an elaborated-type-specifier. Note that the grammar given
17892 here incorporates the resolution to DR68.
17893
17894 elaborated-type-specifier:
17895 class-key :: [opt] nested-name-specifier [opt] identifier
17896 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17897 enum-key :: [opt] nested-name-specifier [opt] identifier
17898 typename :: [opt] nested-name-specifier identifier
17899 typename :: [opt] nested-name-specifier template [opt]
17900 template-id
17901
17902 GNU extension:
17903
17904 elaborated-type-specifier:
17905 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17906 class-key attributes :: [opt] nested-name-specifier [opt]
17907 template [opt] template-id
17908 enum attributes :: [opt] nested-name-specifier [opt] identifier
17909
17910 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17911 declared `friend'. If IS_DECLARATION is TRUE, then this
17912 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17913 something is being declared.
17914
17915 Returns the TYPE specified. */
17916
17917 static tree
17918 cp_parser_elaborated_type_specifier (cp_parser* parser,
17919 bool is_friend,
17920 bool is_declaration)
17921 {
17922 enum tag_types tag_type;
17923 tree identifier;
17924 tree type = NULL_TREE;
17925 tree attributes = NULL_TREE;
17926 tree globalscope;
17927 cp_token *token = NULL;
17928
17929 /* See if we're looking at the `enum' keyword. */
17930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17931 {
17932 /* Consume the `enum' token. */
17933 cp_lexer_consume_token (parser->lexer);
17934 /* Remember that it's an enumeration type. */
17935 tag_type = enum_type;
17936 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17937 enums) is used here. */
17938 cp_token *token = cp_lexer_peek_token (parser->lexer);
17939 if (cp_parser_is_keyword (token, RID_CLASS)
17940 || cp_parser_is_keyword (token, RID_STRUCT))
17941 {
17942 gcc_rich_location richloc (token->location);
17943 richloc.add_range (input_location);
17944 richloc.add_fixit_remove ();
17945 pedwarn (&richloc, 0, "elaborated-type-specifier for "
17946 "a scoped enum must not use the %qD keyword",
17947 token->u.value);
17948 /* Consume the `struct' or `class' and parse it anyway. */
17949 cp_lexer_consume_token (parser->lexer);
17950 }
17951 /* Parse the attributes. */
17952 attributes = cp_parser_attributes_opt (parser);
17953 }
17954 /* Or, it might be `typename'. */
17955 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17956 RID_TYPENAME))
17957 {
17958 /* Consume the `typename' token. */
17959 cp_lexer_consume_token (parser->lexer);
17960 /* Remember that it's a `typename' type. */
17961 tag_type = typename_type;
17962 }
17963 /* Otherwise it must be a class-key. */
17964 else
17965 {
17966 tag_type = cp_parser_class_key (parser);
17967 if (tag_type == none_type)
17968 return error_mark_node;
17969 /* Parse the attributes. */
17970 attributes = cp_parser_attributes_opt (parser);
17971 }
17972
17973 /* Look for the `::' operator. */
17974 globalscope = cp_parser_global_scope_opt (parser,
17975 /*current_scope_valid_p=*/false);
17976 /* Look for the nested-name-specifier. */
17977 tree nested_name_specifier;
17978 if (tag_type == typename_type && !globalscope)
17979 {
17980 nested_name_specifier
17981 = cp_parser_nested_name_specifier (parser,
17982 /*typename_keyword_p=*/true,
17983 /*check_dependency_p=*/true,
17984 /*type_p=*/true,
17985 is_declaration);
17986 if (!nested_name_specifier)
17987 return error_mark_node;
17988 }
17989 else
17990 /* Even though `typename' is not present, the proposed resolution
17991 to Core Issue 180 says that in `class A<T>::B', `B' should be
17992 considered a type-name, even if `A<T>' is dependent. */
17993 nested_name_specifier
17994 = cp_parser_nested_name_specifier_opt (parser,
17995 /*typename_keyword_p=*/true,
17996 /*check_dependency_p=*/true,
17997 /*type_p=*/true,
17998 is_declaration);
17999 /* For everything but enumeration types, consider a template-id.
18000 For an enumeration type, consider only a plain identifier. */
18001 if (tag_type != enum_type)
18002 {
18003 bool template_p = false;
18004 tree decl;
18005
18006 /* Allow the `template' keyword. */
18007 template_p = cp_parser_optional_template_keyword (parser);
18008 /* If we didn't see `template', we don't know if there's a
18009 template-id or not. */
18010 if (!template_p)
18011 cp_parser_parse_tentatively (parser);
18012 /* The `template' keyword must follow a nested-name-specifier. */
18013 else if (!nested_name_specifier)
18014 {
18015 cp_parser_error (parser, "%<template%> must follow a nested-"
18016 "name-specifier");
18017 return error_mark_node;
18018 }
18019
18020 /* Parse the template-id. */
18021 token = cp_lexer_peek_token (parser->lexer);
18022 decl = cp_parser_template_id (parser, template_p,
18023 /*check_dependency_p=*/true,
18024 tag_type,
18025 is_declaration);
18026 /* If we didn't find a template-id, look for an ordinary
18027 identifier. */
18028 if (!template_p && !cp_parser_parse_definitely (parser))
18029 ;
18030 /* We can get here when cp_parser_template_id, called by
18031 cp_parser_class_name with tag_type == none_type, succeeds
18032 and caches a BASELINK. Then, when called again here,
18033 instead of failing and returning an error_mark_node
18034 returns it (see template/typename17.C in C++11).
18035 ??? Could we diagnose this earlier? */
18036 else if (tag_type == typename_type && BASELINK_P (decl))
18037 {
18038 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18039 type = error_mark_node;
18040 }
18041 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18042 in effect, then we must assume that, upon instantiation, the
18043 template will correspond to a class. */
18044 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18045 && tag_type == typename_type)
18046 type = make_typename_type (parser->scope, decl,
18047 typename_type,
18048 /*complain=*/tf_error);
18049 /* If the `typename' keyword is in effect and DECL is not a type
18050 decl, then type is non existent. */
18051 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18052 ;
18053 else if (TREE_CODE (decl) == TYPE_DECL)
18054 {
18055 type = check_elaborated_type_specifier (tag_type, decl,
18056 /*allow_template_p=*/true);
18057
18058 /* If the next token is a semicolon, this must be a specialization,
18059 instantiation, or friend declaration. Check the scope while we
18060 still know whether or not we had a nested-name-specifier. */
18061 if (type != error_mark_node
18062 && !nested_name_specifier && !is_friend
18063 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18064 check_unqualified_spec_or_inst (type, token->location);
18065 }
18066 else if (decl == error_mark_node)
18067 type = error_mark_node;
18068 }
18069
18070 if (!type)
18071 {
18072 token = cp_lexer_peek_token (parser->lexer);
18073 identifier = cp_parser_identifier (parser);
18074
18075 if (identifier == error_mark_node)
18076 {
18077 parser->scope = NULL_TREE;
18078 return error_mark_node;
18079 }
18080
18081 /* For a `typename', we needn't call xref_tag. */
18082 if (tag_type == typename_type
18083 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18084 return cp_parser_make_typename_type (parser, identifier,
18085 token->location);
18086
18087 /* Template parameter lists apply only if we are not within a
18088 function parameter list. */
18089 bool template_parm_lists_apply
18090 = parser->num_template_parameter_lists;
18091 if (template_parm_lists_apply)
18092 for (cp_binding_level *s = current_binding_level;
18093 s && s->kind != sk_template_parms;
18094 s = s->level_chain)
18095 if (s->kind == sk_function_parms)
18096 template_parm_lists_apply = false;
18097
18098 /* Look up a qualified name in the usual way. */
18099 if (parser->scope)
18100 {
18101 tree decl;
18102 tree ambiguous_decls;
18103
18104 decl = cp_parser_lookup_name (parser, identifier,
18105 tag_type,
18106 /*is_template=*/false,
18107 /*is_namespace=*/false,
18108 /*check_dependency=*/true,
18109 &ambiguous_decls,
18110 token->location);
18111
18112 /* If the lookup was ambiguous, an error will already have been
18113 issued. */
18114 if (ambiguous_decls)
18115 return error_mark_node;
18116
18117 /* If we are parsing friend declaration, DECL may be a
18118 TEMPLATE_DECL tree node here. However, we need to check
18119 whether this TEMPLATE_DECL results in valid code. Consider
18120 the following example:
18121
18122 namespace N {
18123 template <class T> class C {};
18124 }
18125 class X {
18126 template <class T> friend class N::C; // #1, valid code
18127 };
18128 template <class T> class Y {
18129 friend class N::C; // #2, invalid code
18130 };
18131
18132 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18133 name lookup of `N::C'. We see that friend declaration must
18134 be template for the code to be valid. Note that
18135 processing_template_decl does not work here since it is
18136 always 1 for the above two cases. */
18137
18138 decl = (cp_parser_maybe_treat_template_as_class
18139 (decl, /*tag_name_p=*/is_friend
18140 && template_parm_lists_apply));
18141
18142 if (TREE_CODE (decl) != TYPE_DECL)
18143 {
18144 cp_parser_diagnose_invalid_type_name (parser,
18145 identifier,
18146 token->location);
18147 return error_mark_node;
18148 }
18149
18150 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18151 {
18152 bool allow_template = (template_parm_lists_apply
18153 || DECL_SELF_REFERENCE_P (decl));
18154 type = check_elaborated_type_specifier (tag_type, decl,
18155 allow_template);
18156
18157 if (type == error_mark_node)
18158 return error_mark_node;
18159 }
18160
18161 /* Forward declarations of nested types, such as
18162
18163 class C1::C2;
18164 class C1::C2::C3;
18165
18166 are invalid unless all components preceding the final '::'
18167 are complete. If all enclosing types are complete, these
18168 declarations become merely pointless.
18169
18170 Invalid forward declarations of nested types are errors
18171 caught elsewhere in parsing. Those that are pointless arrive
18172 here. */
18173
18174 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18175 && !is_friend && !processing_explicit_instantiation)
18176 warning (0, "declaration %qD does not declare anything", decl);
18177
18178 type = TREE_TYPE (decl);
18179 }
18180 else
18181 {
18182 /* An elaborated-type-specifier sometimes introduces a new type and
18183 sometimes names an existing type. Normally, the rule is that it
18184 introduces a new type only if there is not an existing type of
18185 the same name already in scope. For example, given:
18186
18187 struct S {};
18188 void f() { struct S s; }
18189
18190 the `struct S' in the body of `f' is the same `struct S' as in
18191 the global scope; the existing definition is used. However, if
18192 there were no global declaration, this would introduce a new
18193 local class named `S'.
18194
18195 An exception to this rule applies to the following code:
18196
18197 namespace N { struct S; }
18198
18199 Here, the elaborated-type-specifier names a new type
18200 unconditionally; even if there is already an `S' in the
18201 containing scope this declaration names a new type.
18202 This exception only applies if the elaborated-type-specifier
18203 forms the complete declaration:
18204
18205 [class.name]
18206
18207 A declaration consisting solely of `class-key identifier ;' is
18208 either a redeclaration of the name in the current scope or a
18209 forward declaration of the identifier as a class name. It
18210 introduces the name into the current scope.
18211
18212 We are in this situation precisely when the next token is a `;'.
18213
18214 An exception to the exception is that a `friend' declaration does
18215 *not* name a new type; i.e., given:
18216
18217 struct S { friend struct T; };
18218
18219 `T' is not a new type in the scope of `S'.
18220
18221 Also, `new struct S' or `sizeof (struct S)' never results in the
18222 definition of a new type; a new type can only be declared in a
18223 declaration context. */
18224
18225 tag_scope ts;
18226 bool template_p;
18227
18228 if (is_friend)
18229 /* Friends have special name lookup rules. */
18230 ts = ts_within_enclosing_non_class;
18231 else if (is_declaration
18232 && cp_lexer_next_token_is (parser->lexer,
18233 CPP_SEMICOLON))
18234 /* This is a `class-key identifier ;' */
18235 ts = ts_current;
18236 else
18237 ts = ts_global;
18238
18239 template_p =
18240 (template_parm_lists_apply
18241 && (cp_parser_next_token_starts_class_definition_p (parser)
18242 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18243 /* An unqualified name was used to reference this type, so
18244 there were no qualifying templates. */
18245 if (template_parm_lists_apply
18246 && !cp_parser_check_template_parameters (parser,
18247 /*num_templates=*/0,
18248 /*template_id*/false,
18249 token->location,
18250 /*declarator=*/NULL))
18251 return error_mark_node;
18252 type = xref_tag (tag_type, identifier, ts, template_p);
18253 }
18254 }
18255
18256 if (type == error_mark_node)
18257 return error_mark_node;
18258
18259 /* Allow attributes on forward declarations of classes. */
18260 if (attributes)
18261 {
18262 if (TREE_CODE (type) == TYPENAME_TYPE)
18263 warning (OPT_Wattributes,
18264 "attributes ignored on uninstantiated type");
18265 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18266 && ! processing_explicit_instantiation)
18267 warning (OPT_Wattributes,
18268 "attributes ignored on template instantiation");
18269 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18270 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18271 else
18272 warning (OPT_Wattributes,
18273 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18274 }
18275
18276 if (tag_type != enum_type)
18277 {
18278 /* Indicate whether this class was declared as a `class' or as a
18279 `struct'. */
18280 if (CLASS_TYPE_P (type))
18281 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18282 cp_parser_check_class_key (tag_type, type);
18283 }
18284
18285 /* A "<" cannot follow an elaborated type specifier. If that
18286 happens, the user was probably trying to form a template-id. */
18287 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18288 token->location);
18289
18290 return type;
18291 }
18292
18293 /* Parse an enum-specifier.
18294
18295 enum-specifier:
18296 enum-head { enumerator-list [opt] }
18297 enum-head { enumerator-list , } [C++0x]
18298
18299 enum-head:
18300 enum-key identifier [opt] enum-base [opt]
18301 enum-key nested-name-specifier identifier enum-base [opt]
18302
18303 enum-key:
18304 enum
18305 enum class [C++0x]
18306 enum struct [C++0x]
18307
18308 enum-base: [C++0x]
18309 : type-specifier-seq
18310
18311 opaque-enum-specifier:
18312 enum-key identifier enum-base [opt] ;
18313
18314 GNU Extensions:
18315 enum-key attributes[opt] identifier [opt] enum-base [opt]
18316 { enumerator-list [opt] }attributes[opt]
18317 enum-key attributes[opt] identifier [opt] enum-base [opt]
18318 { enumerator-list, }attributes[opt] [C++0x]
18319
18320 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18321 if the token stream isn't an enum-specifier after all. */
18322
18323 static tree
18324 cp_parser_enum_specifier (cp_parser* parser)
18325 {
18326 tree identifier;
18327 tree type = NULL_TREE;
18328 tree prev_scope;
18329 tree nested_name_specifier = NULL_TREE;
18330 tree attributes;
18331 bool scoped_enum_p = false;
18332 bool has_underlying_type = false;
18333 bool nested_being_defined = false;
18334 bool new_value_list = false;
18335 bool is_new_type = false;
18336 bool is_unnamed = false;
18337 tree underlying_type = NULL_TREE;
18338 cp_token *type_start_token = NULL;
18339 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18340
18341 parser->colon_corrects_to_scope_p = false;
18342
18343 /* Parse tentatively so that we can back up if we don't find a
18344 enum-specifier. */
18345 cp_parser_parse_tentatively (parser);
18346
18347 /* Caller guarantees that the current token is 'enum', an identifier
18348 possibly follows, and the token after that is an opening brace.
18349 If we don't have an identifier, fabricate an anonymous name for
18350 the enumeration being defined. */
18351 cp_lexer_consume_token (parser->lexer);
18352
18353 /* Parse the "class" or "struct", which indicates a scoped
18354 enumeration type in C++0x. */
18355 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18356 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18357 {
18358 if (cxx_dialect < cxx11)
18359 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18360
18361 /* Consume the `struct' or `class' token. */
18362 cp_lexer_consume_token (parser->lexer);
18363
18364 scoped_enum_p = true;
18365 }
18366
18367 attributes = cp_parser_attributes_opt (parser);
18368
18369 /* Clear the qualification. */
18370 parser->scope = NULL_TREE;
18371 parser->qualifying_scope = NULL_TREE;
18372 parser->object_scope = NULL_TREE;
18373
18374 /* Figure out in what scope the declaration is being placed. */
18375 prev_scope = current_scope ();
18376
18377 type_start_token = cp_lexer_peek_token (parser->lexer);
18378
18379 push_deferring_access_checks (dk_no_check);
18380 nested_name_specifier
18381 = cp_parser_nested_name_specifier_opt (parser,
18382 /*typename_keyword_p=*/true,
18383 /*check_dependency_p=*/false,
18384 /*type_p=*/false,
18385 /*is_declaration=*/false);
18386
18387 if (nested_name_specifier)
18388 {
18389 tree name;
18390
18391 identifier = cp_parser_identifier (parser);
18392 name = cp_parser_lookup_name (parser, identifier,
18393 enum_type,
18394 /*is_template=*/false,
18395 /*is_namespace=*/false,
18396 /*check_dependency=*/true,
18397 /*ambiguous_decls=*/NULL,
18398 input_location);
18399 if (name && name != error_mark_node)
18400 {
18401 type = TREE_TYPE (name);
18402 if (TREE_CODE (type) == TYPENAME_TYPE)
18403 {
18404 /* Are template enums allowed in ISO? */
18405 if (template_parm_scope_p ())
18406 pedwarn (type_start_token->location, OPT_Wpedantic,
18407 "%qD is an enumeration template", name);
18408 /* ignore a typename reference, for it will be solved by name
18409 in start_enum. */
18410 type = NULL_TREE;
18411 }
18412 }
18413 else if (nested_name_specifier == error_mark_node)
18414 /* We already issued an error. */;
18415 else
18416 {
18417 error_at (type_start_token->location,
18418 "%qD does not name an enumeration in %qT",
18419 identifier, nested_name_specifier);
18420 nested_name_specifier = error_mark_node;
18421 }
18422 }
18423 else
18424 {
18425 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18426 identifier = cp_parser_identifier (parser);
18427 else
18428 {
18429 identifier = make_anon_name ();
18430 is_unnamed = true;
18431 if (scoped_enum_p)
18432 error_at (type_start_token->location,
18433 "unnamed scoped enum is not allowed");
18434 }
18435 }
18436 pop_deferring_access_checks ();
18437
18438 /* Check for the `:' that denotes a specified underlying type in C++0x.
18439 Note that a ':' could also indicate a bitfield width, however. */
18440 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18441 {
18442 cp_decl_specifier_seq type_specifiers;
18443
18444 /* Consume the `:'. */
18445 cp_lexer_consume_token (parser->lexer);
18446
18447 /* Parse the type-specifier-seq. */
18448 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18449 /*is_trailing_return=*/false,
18450 &type_specifiers);
18451
18452 /* At this point this is surely not elaborated type specifier. */
18453 if (!cp_parser_parse_definitely (parser))
18454 return NULL_TREE;
18455
18456 if (cxx_dialect < cxx11)
18457 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18458
18459 has_underlying_type = true;
18460
18461 /* If that didn't work, stop. */
18462 if (type_specifiers.type != error_mark_node)
18463 {
18464 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18465 /*initialized=*/0, NULL);
18466 if (underlying_type == error_mark_node
18467 || check_for_bare_parameter_packs (underlying_type))
18468 underlying_type = NULL_TREE;
18469 }
18470 }
18471
18472 /* Look for the `{' but don't consume it yet. */
18473 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18474 {
18475 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18476 {
18477 cp_parser_error (parser, "expected %<{%>");
18478 if (has_underlying_type)
18479 {
18480 type = NULL_TREE;
18481 goto out;
18482 }
18483 }
18484 /* An opaque-enum-specifier must have a ';' here. */
18485 if ((scoped_enum_p || underlying_type)
18486 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18487 {
18488 cp_parser_error (parser, "expected %<;%> or %<{%>");
18489 if (has_underlying_type)
18490 {
18491 type = NULL_TREE;
18492 goto out;
18493 }
18494 }
18495 }
18496
18497 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18498 return NULL_TREE;
18499
18500 if (nested_name_specifier)
18501 {
18502 if (CLASS_TYPE_P (nested_name_specifier))
18503 {
18504 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18505 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18506 push_scope (nested_name_specifier);
18507 }
18508 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18509 {
18510 push_nested_namespace (nested_name_specifier);
18511 }
18512 }
18513
18514 /* Issue an error message if type-definitions are forbidden here. */
18515 if (!cp_parser_check_type_definition (parser))
18516 type = error_mark_node;
18517 else
18518 /* Create the new type. We do this before consuming the opening
18519 brace so the enum will be recorded as being on the line of its
18520 tag (or the 'enum' keyword, if there is no tag). */
18521 type = start_enum (identifier, type, underlying_type,
18522 attributes, scoped_enum_p, &is_new_type);
18523
18524 /* If the next token is not '{' it is an opaque-enum-specifier or an
18525 elaborated-type-specifier. */
18526 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18527 {
18528 timevar_push (TV_PARSE_ENUM);
18529 if (nested_name_specifier
18530 && nested_name_specifier != error_mark_node)
18531 {
18532 /* The following catches invalid code such as:
18533 enum class S<int>::E { A, B, C }; */
18534 if (!processing_specialization
18535 && CLASS_TYPE_P (nested_name_specifier)
18536 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18537 error_at (type_start_token->location, "cannot add an enumerator "
18538 "list to a template instantiation");
18539
18540 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18541 {
18542 error_at (type_start_token->location,
18543 "%<%T::%E%> has not been declared",
18544 TYPE_CONTEXT (nested_name_specifier),
18545 nested_name_specifier);
18546 type = error_mark_node;
18547 }
18548 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18549 && !CLASS_TYPE_P (nested_name_specifier))
18550 {
18551 error_at (type_start_token->location, "nested name specifier "
18552 "%qT for enum declaration does not name a class "
18553 "or namespace", nested_name_specifier);
18554 type = error_mark_node;
18555 }
18556 /* If that scope does not contain the scope in which the
18557 class was originally declared, the program is invalid. */
18558 else if (prev_scope && !is_ancestor (prev_scope,
18559 nested_name_specifier))
18560 {
18561 if (at_namespace_scope_p ())
18562 error_at (type_start_token->location,
18563 "declaration of %qD in namespace %qD which does not "
18564 "enclose %qD",
18565 type, prev_scope, nested_name_specifier);
18566 else
18567 error_at (type_start_token->location,
18568 "declaration of %qD in %qD which does not "
18569 "enclose %qD",
18570 type, prev_scope, nested_name_specifier);
18571 type = error_mark_node;
18572 }
18573 /* If that scope is the scope where the declaration is being placed
18574 the program is invalid. */
18575 else if (CLASS_TYPE_P (nested_name_specifier)
18576 && CLASS_TYPE_P (prev_scope)
18577 && same_type_p (nested_name_specifier, prev_scope))
18578 {
18579 permerror (type_start_token->location,
18580 "extra qualification not allowed");
18581 nested_name_specifier = NULL_TREE;
18582 }
18583 }
18584
18585 if (scoped_enum_p)
18586 begin_scope (sk_scoped_enum, type);
18587
18588 /* Consume the opening brace. */
18589 matching_braces braces;
18590 braces.consume_open (parser);
18591
18592 if (type == error_mark_node)
18593 ; /* Nothing to add */
18594 else if (OPAQUE_ENUM_P (type)
18595 || (cxx_dialect > cxx98 && processing_specialization))
18596 {
18597 new_value_list = true;
18598 SET_OPAQUE_ENUM_P (type, false);
18599 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18600 }
18601 else
18602 {
18603 error_at (type_start_token->location,
18604 "multiple definition of %q#T", type);
18605 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18606 "previous definition here");
18607 type = error_mark_node;
18608 }
18609
18610 if (type == error_mark_node)
18611 cp_parser_skip_to_end_of_block_or_statement (parser);
18612 /* If the next token is not '}', then there are some enumerators. */
18613 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18614 {
18615 if (is_unnamed && !scoped_enum_p)
18616 pedwarn (type_start_token->location, OPT_Wpedantic,
18617 "ISO C++ forbids empty unnamed enum");
18618 }
18619 else
18620 cp_parser_enumerator_list (parser, type);
18621
18622 /* Consume the final '}'. */
18623 braces.require_close (parser);
18624
18625 if (scoped_enum_p)
18626 finish_scope ();
18627 timevar_pop (TV_PARSE_ENUM);
18628 }
18629 else
18630 {
18631 /* If a ';' follows, then it is an opaque-enum-specifier
18632 and additional restrictions apply. */
18633 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18634 {
18635 if (is_unnamed)
18636 error_at (type_start_token->location,
18637 "opaque-enum-specifier without name");
18638 else if (nested_name_specifier)
18639 error_at (type_start_token->location,
18640 "opaque-enum-specifier must use a simple identifier");
18641 }
18642 }
18643
18644 /* Look for trailing attributes to apply to this enumeration, and
18645 apply them if appropriate. */
18646 if (cp_parser_allow_gnu_extensions_p (parser))
18647 {
18648 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18649 cplus_decl_attributes (&type,
18650 trailing_attr,
18651 (int) ATTR_FLAG_TYPE_IN_PLACE);
18652 }
18653
18654 /* Finish up the enumeration. */
18655 if (type != error_mark_node)
18656 {
18657 if (new_value_list)
18658 finish_enum_value_list (type);
18659 if (is_new_type)
18660 finish_enum (type);
18661 }
18662
18663 if (nested_name_specifier)
18664 {
18665 if (CLASS_TYPE_P (nested_name_specifier))
18666 {
18667 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18668 pop_scope (nested_name_specifier);
18669 }
18670 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18671 {
18672 pop_nested_namespace (nested_name_specifier);
18673 }
18674 }
18675 out:
18676 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18677 return type;
18678 }
18679
18680 /* Parse an enumerator-list. The enumerators all have the indicated
18681 TYPE.
18682
18683 enumerator-list:
18684 enumerator-definition
18685 enumerator-list , enumerator-definition */
18686
18687 static void
18688 cp_parser_enumerator_list (cp_parser* parser, tree type)
18689 {
18690 while (true)
18691 {
18692 /* Parse an enumerator-definition. */
18693 cp_parser_enumerator_definition (parser, type);
18694
18695 /* If the next token is not a ',', we've reached the end of
18696 the list. */
18697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18698 break;
18699 /* Otherwise, consume the `,' and keep going. */
18700 cp_lexer_consume_token (parser->lexer);
18701 /* If the next token is a `}', there is a trailing comma. */
18702 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18703 {
18704 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18705 pedwarn (input_location, OPT_Wpedantic,
18706 "comma at end of enumerator list");
18707 break;
18708 }
18709 }
18710 }
18711
18712 /* Parse an enumerator-definition. The enumerator has the indicated
18713 TYPE.
18714
18715 enumerator-definition:
18716 enumerator
18717 enumerator = constant-expression
18718
18719 enumerator:
18720 identifier
18721
18722 GNU Extensions:
18723
18724 enumerator-definition:
18725 enumerator attributes [opt]
18726 enumerator attributes [opt] = constant-expression */
18727
18728 static void
18729 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18730 {
18731 tree identifier;
18732 tree value;
18733 location_t loc;
18734
18735 /* Save the input location because we are interested in the location
18736 of the identifier and not the location of the explicit value. */
18737 loc = cp_lexer_peek_token (parser->lexer)->location;
18738
18739 /* Look for the identifier. */
18740 identifier = cp_parser_identifier (parser);
18741 if (identifier == error_mark_node)
18742 return;
18743
18744 /* Parse any specified attributes. */
18745 tree attrs = cp_parser_attributes_opt (parser);
18746
18747 /* If the next token is an '=', then there is an explicit value. */
18748 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18749 {
18750 /* Consume the `=' token. */
18751 cp_lexer_consume_token (parser->lexer);
18752 /* Parse the value. */
18753 value = cp_parser_constant_expression (parser);
18754 }
18755 else
18756 value = NULL_TREE;
18757
18758 /* If we are processing a template, make sure the initializer of the
18759 enumerator doesn't contain any bare template parameter pack. */
18760 if (check_for_bare_parameter_packs (value))
18761 value = error_mark_node;
18762
18763 /* Create the enumerator. */
18764 build_enumerator (identifier, value, type, attrs, loc);
18765 }
18766
18767 /* Parse a namespace-name.
18768
18769 namespace-name:
18770 original-namespace-name
18771 namespace-alias
18772
18773 Returns the NAMESPACE_DECL for the namespace. */
18774
18775 static tree
18776 cp_parser_namespace_name (cp_parser* parser)
18777 {
18778 tree identifier;
18779 tree namespace_decl;
18780
18781 cp_token *token = cp_lexer_peek_token (parser->lexer);
18782
18783 /* Get the name of the namespace. */
18784 identifier = cp_parser_identifier (parser);
18785 if (identifier == error_mark_node)
18786 return error_mark_node;
18787
18788 /* Look up the identifier in the currently active scope. Look only
18789 for namespaces, due to:
18790
18791 [basic.lookup.udir]
18792
18793 When looking up a namespace-name in a using-directive or alias
18794 definition, only namespace names are considered.
18795
18796 And:
18797
18798 [basic.lookup.qual]
18799
18800 During the lookup of a name preceding the :: scope resolution
18801 operator, object, function, and enumerator names are ignored.
18802
18803 (Note that cp_parser_qualifying_entity only calls this
18804 function if the token after the name is the scope resolution
18805 operator.) */
18806 namespace_decl = cp_parser_lookup_name (parser, identifier,
18807 none_type,
18808 /*is_template=*/false,
18809 /*is_namespace=*/true,
18810 /*check_dependency=*/true,
18811 /*ambiguous_decls=*/NULL,
18812 token->location);
18813 /* If it's not a namespace, issue an error. */
18814 if (namespace_decl == error_mark_node
18815 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18816 {
18817 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18818 {
18819 auto_diagnostic_group d;
18820 name_hint hint;
18821 if (namespace_decl == error_mark_node
18822 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18823 hint = suggest_alternative_in_explicit_scope (token->location,
18824 identifier,
18825 parser->scope);
18826 if (const char *suggestion = hint.suggestion ())
18827 {
18828 gcc_rich_location richloc (token->location);
18829 richloc.add_fixit_replace (suggestion);
18830 error_at (&richloc,
18831 "%qD is not a namespace-name; did you mean %qs?",
18832 identifier, suggestion);
18833 }
18834 else
18835 error_at (token->location, "%qD is not a namespace-name",
18836 identifier);
18837 }
18838 else
18839 cp_parser_error (parser, "expected namespace-name");
18840 namespace_decl = error_mark_node;
18841 }
18842
18843 return namespace_decl;
18844 }
18845
18846 /* Parse a namespace-definition.
18847
18848 namespace-definition:
18849 named-namespace-definition
18850 unnamed-namespace-definition
18851
18852 named-namespace-definition:
18853 original-namespace-definition
18854 extension-namespace-definition
18855
18856 original-namespace-definition:
18857 namespace identifier { namespace-body }
18858
18859 extension-namespace-definition:
18860 namespace original-namespace-name { namespace-body }
18861
18862 unnamed-namespace-definition:
18863 namespace { namespace-body } */
18864
18865 static void
18866 cp_parser_namespace_definition (cp_parser* parser)
18867 {
18868 tree identifier;
18869 int nested_definition_count = 0;
18870
18871 cp_ensure_no_omp_declare_simd (parser);
18872 cp_ensure_no_oacc_routine (parser);
18873
18874 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18875
18876 if (is_inline)
18877 {
18878 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18879 cp_lexer_consume_token (parser->lexer);
18880 }
18881
18882 /* Look for the `namespace' keyword. */
18883 cp_token* token
18884 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18885
18886 /* Parse any specified attributes before the identifier. */
18887 tree attribs = cp_parser_attributes_opt (parser);
18888
18889 for (;;)
18890 {
18891 identifier = NULL_TREE;
18892
18893 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18894 {
18895 identifier = cp_parser_identifier (parser);
18896
18897 if (cp_next_tokens_can_be_std_attribute_p (parser))
18898 pedwarn (input_location, OPT_Wpedantic,
18899 "standard attributes on namespaces must precede "
18900 "the namespace name");
18901
18902 /* Parse any attributes specified after the identifier. */
18903 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
18904 }
18905
18906 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18907 break;
18908
18909 if (!nested_definition_count && cxx_dialect < cxx17)
18910 pedwarn (input_location, OPT_Wpedantic,
18911 "nested namespace definitions only available with "
18912 "-std=c++17 or -std=gnu++17");
18913
18914 /* Nested namespace names can create new namespaces (unlike
18915 other qualified-ids). */
18916 if (int count = identifier ? push_namespace (identifier) : 0)
18917 nested_definition_count += count;
18918 else
18919 cp_parser_error (parser, "nested namespace name required");
18920 cp_lexer_consume_token (parser->lexer);
18921 }
18922
18923 if (nested_definition_count && !identifier)
18924 cp_parser_error (parser, "namespace name required");
18925
18926 if (nested_definition_count && attribs)
18927 error_at (token->location,
18928 "a nested namespace definition cannot have attributes");
18929 if (nested_definition_count && is_inline)
18930 error_at (token->location,
18931 "a nested namespace definition cannot be inline");
18932
18933 /* Start the namespace. */
18934 nested_definition_count += push_namespace (identifier, is_inline);
18935
18936 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18937
18938 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18939
18940 /* Look for the `{' to validate starting the namespace. */
18941 matching_braces braces;
18942 if (braces.require_open (parser))
18943 {
18944 /* Parse the body of the namespace. */
18945 cp_parser_namespace_body (parser);
18946
18947 /* Look for the final `}'. */
18948 braces.require_close (parser);
18949 }
18950
18951 if (has_visibility)
18952 pop_visibility (1);
18953
18954 /* Pop the nested namespace definitions. */
18955 while (nested_definition_count--)
18956 pop_namespace ();
18957 }
18958
18959 /* Parse a namespace-body.
18960
18961 namespace-body:
18962 declaration-seq [opt] */
18963
18964 static void
18965 cp_parser_namespace_body (cp_parser* parser)
18966 {
18967 cp_parser_declaration_seq_opt (parser);
18968 }
18969
18970 /* Parse a namespace-alias-definition.
18971
18972 namespace-alias-definition:
18973 namespace identifier = qualified-namespace-specifier ; */
18974
18975 static void
18976 cp_parser_namespace_alias_definition (cp_parser* parser)
18977 {
18978 tree identifier;
18979 tree namespace_specifier;
18980
18981 cp_token *token = cp_lexer_peek_token (parser->lexer);
18982
18983 /* Look for the `namespace' keyword. */
18984 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18985 /* Look for the identifier. */
18986 identifier = cp_parser_identifier (parser);
18987 if (identifier == error_mark_node)
18988 return;
18989 /* Look for the `=' token. */
18990 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18991 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18992 {
18993 error_at (token->location, "%<namespace%> definition is not allowed here");
18994 /* Skip the definition. */
18995 cp_lexer_consume_token (parser->lexer);
18996 if (cp_parser_skip_to_closing_brace (parser))
18997 cp_lexer_consume_token (parser->lexer);
18998 return;
18999 }
19000 cp_parser_require (parser, CPP_EQ, RT_EQ);
19001 /* Look for the qualified-namespace-specifier. */
19002 namespace_specifier
19003 = cp_parser_qualified_namespace_specifier (parser);
19004 /* Look for the `;' token. */
19005 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19006
19007 /* Register the alias in the symbol table. */
19008 do_namespace_alias (identifier, namespace_specifier);
19009 }
19010
19011 /* Parse a qualified-namespace-specifier.
19012
19013 qualified-namespace-specifier:
19014 :: [opt] nested-name-specifier [opt] namespace-name
19015
19016 Returns a NAMESPACE_DECL corresponding to the specified
19017 namespace. */
19018
19019 static tree
19020 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19021 {
19022 /* Look for the optional `::'. */
19023 cp_parser_global_scope_opt (parser,
19024 /*current_scope_valid_p=*/false);
19025
19026 /* Look for the optional nested-name-specifier. */
19027 cp_parser_nested_name_specifier_opt (parser,
19028 /*typename_keyword_p=*/false,
19029 /*check_dependency_p=*/true,
19030 /*type_p=*/false,
19031 /*is_declaration=*/true);
19032
19033 return cp_parser_namespace_name (parser);
19034 }
19035
19036 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19037 access declaration.
19038
19039 using-declaration:
19040 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19041 using :: unqualified-id ;
19042
19043 access-declaration:
19044 qualified-id ;
19045
19046 */
19047
19048 static bool
19049 cp_parser_using_declaration (cp_parser* parser,
19050 bool access_declaration_p)
19051 {
19052 cp_token *token;
19053 bool typename_p = false;
19054 bool global_scope_p;
19055 tree decl;
19056 tree identifier;
19057 tree qscope;
19058 int oldcount = errorcount;
19059 cp_token *diag_token = NULL;
19060
19061 if (access_declaration_p)
19062 {
19063 diag_token = cp_lexer_peek_token (parser->lexer);
19064 cp_parser_parse_tentatively (parser);
19065 }
19066 else
19067 {
19068 /* Look for the `using' keyword. */
19069 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19070
19071 again:
19072 /* Peek at the next token. */
19073 token = cp_lexer_peek_token (parser->lexer);
19074 /* See if it's `typename'. */
19075 if (token->keyword == RID_TYPENAME)
19076 {
19077 /* Remember that we've seen it. */
19078 typename_p = true;
19079 /* Consume the `typename' token. */
19080 cp_lexer_consume_token (parser->lexer);
19081 }
19082 }
19083
19084 /* Look for the optional global scope qualification. */
19085 global_scope_p
19086 = (cp_parser_global_scope_opt (parser,
19087 /*current_scope_valid_p=*/false)
19088 != NULL_TREE);
19089
19090 /* If we saw `typename', or didn't see `::', then there must be a
19091 nested-name-specifier present. */
19092 if (typename_p || !global_scope_p)
19093 {
19094 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19095 /*check_dependency_p=*/true,
19096 /*type_p=*/false,
19097 /*is_declaration=*/true);
19098 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19099 {
19100 cp_parser_skip_to_end_of_block_or_statement (parser);
19101 return false;
19102 }
19103 }
19104 /* Otherwise, we could be in either of the two productions. In that
19105 case, treat the nested-name-specifier as optional. */
19106 else
19107 qscope = cp_parser_nested_name_specifier_opt (parser,
19108 /*typename_keyword_p=*/false,
19109 /*check_dependency_p=*/true,
19110 /*type_p=*/false,
19111 /*is_declaration=*/true);
19112 if (!qscope)
19113 qscope = global_namespace;
19114 else if (UNSCOPED_ENUM_P (qscope))
19115 qscope = CP_TYPE_CONTEXT (qscope);
19116
19117 if (access_declaration_p && cp_parser_error_occurred (parser))
19118 /* Something has already gone wrong; there's no need to parse
19119 further. Since an error has occurred, the return value of
19120 cp_parser_parse_definitely will be false, as required. */
19121 return cp_parser_parse_definitely (parser);
19122
19123 token = cp_lexer_peek_token (parser->lexer);
19124 /* Parse the unqualified-id. */
19125 identifier = cp_parser_unqualified_id (parser,
19126 /*template_keyword_p=*/false,
19127 /*check_dependency_p=*/true,
19128 /*declarator_p=*/true,
19129 /*optional_p=*/false);
19130
19131 if (access_declaration_p)
19132 {
19133 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19134 cp_parser_simulate_error (parser);
19135 if (!cp_parser_parse_definitely (parser))
19136 return false;
19137 }
19138 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19139 {
19140 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19141 if (cxx_dialect < cxx17
19142 && !in_system_header_at (ell->location))
19143 pedwarn (ell->location, 0,
19144 "pack expansion in using-declaration only available "
19145 "with -std=c++17 or -std=gnu++17");
19146 qscope = make_pack_expansion (qscope);
19147 }
19148
19149 /* The function we call to handle a using-declaration is different
19150 depending on what scope we are in. */
19151 if (qscope == error_mark_node || identifier == error_mark_node)
19152 ;
19153 else if (!identifier_p (identifier)
19154 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19155 /* [namespace.udecl]
19156
19157 A using declaration shall not name a template-id. */
19158 error_at (token->location,
19159 "a template-id may not appear in a using-declaration");
19160 else
19161 {
19162 if (at_class_scope_p ())
19163 {
19164 /* Create the USING_DECL. */
19165 decl = do_class_using_decl (qscope, identifier);
19166
19167 if (decl && typename_p)
19168 USING_DECL_TYPENAME_P (decl) = 1;
19169
19170 if (check_for_bare_parameter_packs (decl))
19171 {
19172 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19173 return false;
19174 }
19175 else
19176 /* Add it to the list of members in this class. */
19177 finish_member_declaration (decl);
19178 }
19179 else
19180 {
19181 decl = cp_parser_lookup_name_simple (parser,
19182 identifier,
19183 token->location);
19184 if (decl == error_mark_node)
19185 cp_parser_name_lookup_error (parser, identifier,
19186 decl, NLE_NULL,
19187 token->location);
19188 else if (check_for_bare_parameter_packs (decl))
19189 {
19190 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19191 return false;
19192 }
19193 else if (!at_namespace_scope_p ())
19194 finish_local_using_decl (decl, qscope, identifier);
19195 else
19196 finish_namespace_using_decl (decl, qscope, identifier);
19197 }
19198 }
19199
19200 if (!access_declaration_p
19201 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19202 {
19203 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19204 if (cxx_dialect < cxx17)
19205 pedwarn (comma->location, 0,
19206 "comma-separated list in using-declaration only available "
19207 "with -std=c++17 or -std=gnu++17");
19208 goto again;
19209 }
19210
19211 /* Look for the final `;'. */
19212 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19213
19214 if (access_declaration_p && errorcount == oldcount)
19215 warning_at (diag_token->location, OPT_Wdeprecated,
19216 "access declarations are deprecated "
19217 "in favour of using-declarations; "
19218 "suggestion: add the %<using%> keyword");
19219
19220 return true;
19221 }
19222
19223 /* Parse an alias-declaration.
19224
19225 alias-declaration:
19226 using identifier attribute-specifier-seq [opt] = type-id */
19227
19228 static tree
19229 cp_parser_alias_declaration (cp_parser* parser)
19230 {
19231 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19232 location_t id_location, type_location;
19233 cp_declarator *declarator;
19234 cp_decl_specifier_seq decl_specs;
19235 bool member_p;
19236 const char *saved_message = NULL;
19237
19238 /* Look for the `using' keyword. */
19239 cp_token *using_token
19240 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19241 if (using_token == NULL)
19242 return error_mark_node;
19243
19244 id_location = cp_lexer_peek_token (parser->lexer)->location;
19245 id = cp_parser_identifier (parser);
19246 if (id == error_mark_node)
19247 return error_mark_node;
19248
19249 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19250 attributes = cp_parser_attributes_opt (parser);
19251 if (attributes == error_mark_node)
19252 return error_mark_node;
19253
19254 cp_parser_require (parser, CPP_EQ, RT_EQ);
19255
19256 if (cp_parser_error_occurred (parser))
19257 return error_mark_node;
19258
19259 cp_parser_commit_to_tentative_parse (parser);
19260
19261 /* Now we are going to parse the type-id of the declaration. */
19262
19263 /*
19264 [dcl.type]/3 says:
19265
19266 "A type-specifier-seq shall not define a class or enumeration
19267 unless it appears in the type-id of an alias-declaration (7.1.3) that
19268 is not the declaration of a template-declaration."
19269
19270 In other words, if we currently are in an alias template, the
19271 type-id should not define a type.
19272
19273 So let's set parser->type_definition_forbidden_message in that
19274 case; cp_parser_check_type_definition (called by
19275 cp_parser_class_specifier) will then emit an error if a type is
19276 defined in the type-id. */
19277 if (parser->num_template_parameter_lists)
19278 {
19279 saved_message = parser->type_definition_forbidden_message;
19280 parser->type_definition_forbidden_message =
19281 G_("types may not be defined in alias template declarations");
19282 }
19283
19284 type = cp_parser_type_id (parser, &type_location);
19285
19286 /* Restore the error message if need be. */
19287 if (parser->num_template_parameter_lists)
19288 parser->type_definition_forbidden_message = saved_message;
19289
19290 if (type == error_mark_node
19291 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19292 {
19293 cp_parser_skip_to_end_of_block_or_statement (parser);
19294 return error_mark_node;
19295 }
19296
19297 /* A typedef-name can also be introduced by an alias-declaration. The
19298 identifier following the using keyword becomes a typedef-name. It has
19299 the same semantics as if it were introduced by the typedef
19300 specifier. In particular, it does not define a new type and it shall
19301 not appear in the type-id. */
19302
19303 clear_decl_specs (&decl_specs);
19304 decl_specs.type = type;
19305 if (attributes != NULL_TREE)
19306 {
19307 decl_specs.attributes = attributes;
19308 set_and_check_decl_spec_loc (&decl_specs,
19309 ds_attribute,
19310 attrs_token);
19311 }
19312 set_and_check_decl_spec_loc (&decl_specs,
19313 ds_typedef,
19314 using_token);
19315 set_and_check_decl_spec_loc (&decl_specs,
19316 ds_alias,
19317 using_token);
19318 decl_specs.locations[ds_type_spec] = type_location;
19319
19320 if (parser->num_template_parameter_lists
19321 && !cp_parser_check_template_parameters (parser,
19322 /*num_templates=*/0,
19323 /*template_id*/false,
19324 id_location,
19325 /*declarator=*/NULL))
19326 return error_mark_node;
19327
19328 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19329
19330 member_p = at_class_scope_p ();
19331 if (member_p)
19332 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19333 NULL_TREE, attributes);
19334 else
19335 decl = start_decl (declarator, &decl_specs, 0,
19336 attributes, NULL_TREE, &pushed_scope);
19337 if (decl == error_mark_node)
19338 return decl;
19339
19340 // Attach constraints to the alias declaration.
19341 if (flag_concepts && current_template_parms)
19342 {
19343 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19344 tree constr = build_constraints (reqs, NULL_TREE);
19345 set_constraints (decl, constr);
19346 }
19347
19348 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19349
19350 if (pushed_scope)
19351 pop_scope (pushed_scope);
19352
19353 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19354 added into the symbol table; otherwise, return the TYPE_DECL. */
19355 if (DECL_LANG_SPECIFIC (decl)
19356 && DECL_TEMPLATE_INFO (decl)
19357 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19358 {
19359 decl = DECL_TI_TEMPLATE (decl);
19360 if (member_p)
19361 check_member_template (decl);
19362 }
19363
19364 return decl;
19365 }
19366
19367 /* Parse a using-directive.
19368
19369 using-directive:
19370 using namespace :: [opt] nested-name-specifier [opt]
19371 namespace-name ; */
19372
19373 static void
19374 cp_parser_using_directive (cp_parser* parser)
19375 {
19376 tree namespace_decl;
19377 tree attribs;
19378
19379 /* Look for the `using' keyword. */
19380 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19381 /* And the `namespace' keyword. */
19382 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19383 /* Look for the optional `::' operator. */
19384 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19385 /* And the optional nested-name-specifier. */
19386 cp_parser_nested_name_specifier_opt (parser,
19387 /*typename_keyword_p=*/false,
19388 /*check_dependency_p=*/true,
19389 /*type_p=*/false,
19390 /*is_declaration=*/true);
19391 /* Get the namespace being used. */
19392 namespace_decl = cp_parser_namespace_name (parser);
19393 /* And any specified attributes. */
19394 attribs = cp_parser_attributes_opt (parser);
19395
19396 /* Update the symbol table. */
19397 if (namespace_bindings_p ())
19398 finish_namespace_using_directive (namespace_decl, attribs);
19399 else
19400 finish_local_using_directive (namespace_decl, attribs);
19401
19402 /* Look for the final `;'. */
19403 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19404 }
19405
19406 /* Parse an asm-definition.
19407
19408 asm-definition:
19409 asm ( string-literal ) ;
19410
19411 GNU Extension:
19412
19413 asm-definition:
19414 asm volatile [opt] ( string-literal ) ;
19415 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19416 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19417 : asm-operand-list [opt] ) ;
19418 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19419 : asm-operand-list [opt]
19420 : asm-clobber-list [opt] ) ;
19421 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19422 : asm-clobber-list [opt]
19423 : asm-goto-list ) ; */
19424
19425 static void
19426 cp_parser_asm_definition (cp_parser* parser)
19427 {
19428 tree string;
19429 tree outputs = NULL_TREE;
19430 tree inputs = NULL_TREE;
19431 tree clobbers = NULL_TREE;
19432 tree labels = NULL_TREE;
19433 tree asm_stmt;
19434 bool volatile_p = false;
19435 bool extended_p = false;
19436 bool invalid_inputs_p = false;
19437 bool invalid_outputs_p = false;
19438 bool goto_p = false;
19439 required_token missing = RT_NONE;
19440
19441 /* Look for the `asm' keyword. */
19442 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19443
19444 if (parser->in_function_body
19445 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19446 {
19447 error ("%<asm%> in %<constexpr%> function");
19448 cp_function_chain->invalid_constexpr = true;
19449 }
19450
19451 /* See if the next token is `volatile'. */
19452 if (cp_parser_allow_gnu_extensions_p (parser)
19453 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19454 {
19455 /* Remember that we saw the `volatile' keyword. */
19456 volatile_p = true;
19457 /* Consume the token. */
19458 cp_lexer_consume_token (parser->lexer);
19459 }
19460 if (cp_parser_allow_gnu_extensions_p (parser)
19461 && parser->in_function_body
19462 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19463 {
19464 /* Remember that we saw the `goto' keyword. */
19465 goto_p = true;
19466 /* Consume the token. */
19467 cp_lexer_consume_token (parser->lexer);
19468 }
19469 /* Look for the opening `('. */
19470 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19471 return;
19472 /* Look for the string. */
19473 string = cp_parser_string_literal (parser, false, false);
19474 if (string == error_mark_node)
19475 {
19476 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19477 /*consume_paren=*/true);
19478 return;
19479 }
19480
19481 /* If we're allowing GNU extensions, check for the extended assembly
19482 syntax. Unfortunately, the `:' tokens need not be separated by
19483 a space in C, and so, for compatibility, we tolerate that here
19484 too. Doing that means that we have to treat the `::' operator as
19485 two `:' tokens. */
19486 if (cp_parser_allow_gnu_extensions_p (parser)
19487 && parser->in_function_body
19488 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19489 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19490 {
19491 bool inputs_p = false;
19492 bool clobbers_p = false;
19493 bool labels_p = false;
19494
19495 /* The extended syntax was used. */
19496 extended_p = true;
19497
19498 /* Look for outputs. */
19499 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19500 {
19501 /* Consume the `:'. */
19502 cp_lexer_consume_token (parser->lexer);
19503 /* Parse the output-operands. */
19504 if (cp_lexer_next_token_is_not (parser->lexer,
19505 CPP_COLON)
19506 && cp_lexer_next_token_is_not (parser->lexer,
19507 CPP_SCOPE)
19508 && cp_lexer_next_token_is_not (parser->lexer,
19509 CPP_CLOSE_PAREN)
19510 && !goto_p)
19511 {
19512 outputs = cp_parser_asm_operand_list (parser);
19513 if (outputs == error_mark_node)
19514 invalid_outputs_p = true;
19515 }
19516 }
19517 /* If the next token is `::', there are no outputs, and the
19518 next token is the beginning of the inputs. */
19519 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19520 /* The inputs are coming next. */
19521 inputs_p = true;
19522
19523 /* Look for inputs. */
19524 if (inputs_p
19525 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19526 {
19527 /* Consume the `:' or `::'. */
19528 cp_lexer_consume_token (parser->lexer);
19529 /* Parse the output-operands. */
19530 if (cp_lexer_next_token_is_not (parser->lexer,
19531 CPP_COLON)
19532 && cp_lexer_next_token_is_not (parser->lexer,
19533 CPP_SCOPE)
19534 && cp_lexer_next_token_is_not (parser->lexer,
19535 CPP_CLOSE_PAREN))
19536 {
19537 inputs = cp_parser_asm_operand_list (parser);
19538 if (inputs == error_mark_node)
19539 invalid_inputs_p = true;
19540 }
19541 }
19542 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19543 /* The clobbers are coming next. */
19544 clobbers_p = true;
19545
19546 /* Look for clobbers. */
19547 if (clobbers_p
19548 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19549 {
19550 clobbers_p = true;
19551 /* Consume the `:' or `::'. */
19552 cp_lexer_consume_token (parser->lexer);
19553 /* Parse the clobbers. */
19554 if (cp_lexer_next_token_is_not (parser->lexer,
19555 CPP_COLON)
19556 && cp_lexer_next_token_is_not (parser->lexer,
19557 CPP_CLOSE_PAREN))
19558 clobbers = cp_parser_asm_clobber_list (parser);
19559 }
19560 else if (goto_p
19561 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19562 /* The labels are coming next. */
19563 labels_p = true;
19564
19565 /* Look for labels. */
19566 if (labels_p
19567 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19568 {
19569 labels_p = true;
19570 /* Consume the `:' or `::'. */
19571 cp_lexer_consume_token (parser->lexer);
19572 /* Parse the labels. */
19573 labels = cp_parser_asm_label_list (parser);
19574 }
19575
19576 if (goto_p && !labels_p)
19577 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19578 }
19579 else if (goto_p)
19580 missing = RT_COLON_SCOPE;
19581
19582 /* Look for the closing `)'. */
19583 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19584 missing ? missing : RT_CLOSE_PAREN))
19585 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19586 /*consume_paren=*/true);
19587 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19588
19589 if (!invalid_inputs_p && !invalid_outputs_p)
19590 {
19591 /* Create the ASM_EXPR. */
19592 if (parser->in_function_body)
19593 {
19594 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19595 inputs, clobbers, labels);
19596 /* If the extended syntax was not used, mark the ASM_EXPR. */
19597 if (!extended_p)
19598 {
19599 tree temp = asm_stmt;
19600 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19601 temp = TREE_OPERAND (temp, 0);
19602
19603 ASM_INPUT_P (temp) = 1;
19604 }
19605 }
19606 else
19607 symtab->finalize_toplevel_asm (string);
19608 }
19609 }
19610
19611 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19612 type that comes from the decl-specifier-seq. */
19613
19614 static tree
19615 strip_declarator_types (tree type, cp_declarator *declarator)
19616 {
19617 for (cp_declarator *d = declarator; d;)
19618 switch (d->kind)
19619 {
19620 case cdk_id:
19621 case cdk_decomp:
19622 case cdk_error:
19623 d = NULL;
19624 break;
19625
19626 default:
19627 if (TYPE_PTRMEMFUNC_P (type))
19628 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19629 type = TREE_TYPE (type);
19630 d = d->declarator;
19631 break;
19632 }
19633
19634 return type;
19635 }
19636
19637 /* Declarators [gram.dcl.decl] */
19638
19639 /* Parse an init-declarator.
19640
19641 init-declarator:
19642 declarator initializer [opt]
19643
19644 GNU Extension:
19645
19646 init-declarator:
19647 declarator asm-specification [opt] attributes [opt] initializer [opt]
19648
19649 function-definition:
19650 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19651 function-body
19652 decl-specifier-seq [opt] declarator function-try-block
19653
19654 GNU Extension:
19655
19656 function-definition:
19657 __extension__ function-definition
19658
19659 TM Extension:
19660
19661 function-definition:
19662 decl-specifier-seq [opt] declarator function-transaction-block
19663
19664 The DECL_SPECIFIERS apply to this declarator. Returns a
19665 representation of the entity declared. If MEMBER_P is TRUE, then
19666 this declarator appears in a class scope. The new DECL created by
19667 this declarator is returned.
19668
19669 The CHECKS are access checks that should be performed once we know
19670 what entity is being declared (and, therefore, what classes have
19671 befriended it).
19672
19673 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19674 for a function-definition here as well. If the declarator is a
19675 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19676 be TRUE upon return. By that point, the function-definition will
19677 have been completely parsed.
19678
19679 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19680 is FALSE.
19681
19682 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19683 parsed declaration if it is an uninitialized single declarator not followed
19684 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19685 if present, will not be consumed. If returned, this declarator will be
19686 created with SD_INITIALIZED but will not call cp_finish_decl.
19687
19688 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19689 and there is an initializer, the pointed location_t is set to the
19690 location of the '=' or `(', or '{' in C++11 token introducing the
19691 initializer. */
19692
19693 static tree
19694 cp_parser_init_declarator (cp_parser* parser,
19695 cp_decl_specifier_seq *decl_specifiers,
19696 vec<deferred_access_check, va_gc> *checks,
19697 bool function_definition_allowed_p,
19698 bool member_p,
19699 int declares_class_or_enum,
19700 bool* function_definition_p,
19701 tree* maybe_range_for_decl,
19702 location_t* init_loc,
19703 tree* auto_result)
19704 {
19705 cp_token *token = NULL, *asm_spec_start_token = NULL,
19706 *attributes_start_token = NULL;
19707 cp_declarator *declarator;
19708 tree prefix_attributes;
19709 tree attributes = NULL;
19710 tree asm_specification;
19711 tree initializer;
19712 tree decl = NULL_TREE;
19713 tree scope;
19714 int is_initialized;
19715 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19716 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19717 "(...)". */
19718 enum cpp_ttype initialization_kind;
19719 bool is_direct_init = false;
19720 bool is_non_constant_init;
19721 int ctor_dtor_or_conv_p;
19722 bool friend_p = cp_parser_friend_p (decl_specifiers);
19723 tree pushed_scope = NULL_TREE;
19724 bool range_for_decl_p = false;
19725 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19726 location_t tmp_init_loc = UNKNOWN_LOCATION;
19727
19728 /* Gather the attributes that were provided with the
19729 decl-specifiers. */
19730 prefix_attributes = decl_specifiers->attributes;
19731
19732 /* Assume that this is not the declarator for a function
19733 definition. */
19734 if (function_definition_p)
19735 *function_definition_p = false;
19736
19737 /* Default arguments are only permitted for function parameters. */
19738 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19739 parser->default_arg_ok_p = false;
19740
19741 /* Defer access checks while parsing the declarator; we cannot know
19742 what names are accessible until we know what is being
19743 declared. */
19744 resume_deferring_access_checks ();
19745
19746 token = cp_lexer_peek_token (parser->lexer);
19747
19748 /* Parse the declarator. */
19749 declarator
19750 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19751 &ctor_dtor_or_conv_p,
19752 /*parenthesized_p=*/NULL,
19753 member_p, friend_p);
19754 /* Gather up the deferred checks. */
19755 stop_deferring_access_checks ();
19756
19757 parser->default_arg_ok_p = saved_default_arg_ok_p;
19758
19759 /* If the DECLARATOR was erroneous, there's no need to go
19760 further. */
19761 if (declarator == cp_error_declarator)
19762 return error_mark_node;
19763
19764 /* Check that the number of template-parameter-lists is OK. */
19765 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19766 token->location))
19767 return error_mark_node;
19768
19769 if (declares_class_or_enum & 2)
19770 cp_parser_check_for_definition_in_return_type (declarator,
19771 decl_specifiers->type,
19772 decl_specifiers->locations[ds_type_spec]);
19773
19774 /* Figure out what scope the entity declared by the DECLARATOR is
19775 located in. `grokdeclarator' sometimes changes the scope, so
19776 we compute it now. */
19777 scope = get_scope_of_declarator (declarator);
19778
19779 /* Perform any lookups in the declared type which were thought to be
19780 dependent, but are not in the scope of the declarator. */
19781 decl_specifiers->type
19782 = maybe_update_decl_type (decl_specifiers->type, scope);
19783
19784 /* If we're allowing GNU extensions, look for an
19785 asm-specification. */
19786 if (cp_parser_allow_gnu_extensions_p (parser))
19787 {
19788 /* Look for an asm-specification. */
19789 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19790 asm_specification = cp_parser_asm_specification_opt (parser);
19791 }
19792 else
19793 asm_specification = NULL_TREE;
19794
19795 /* Look for attributes. */
19796 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19797 attributes = cp_parser_attributes_opt (parser);
19798
19799 /* Peek at the next token. */
19800 token = cp_lexer_peek_token (parser->lexer);
19801
19802 bool bogus_implicit_tmpl = false;
19803
19804 if (function_declarator_p (declarator))
19805 {
19806 /* Handle C++17 deduction guides. */
19807 if (!decl_specifiers->type
19808 && ctor_dtor_or_conv_p <= 0
19809 && cxx_dialect >= cxx17)
19810 {
19811 cp_declarator *id = get_id_declarator (declarator);
19812 tree name = id->u.id.unqualified_name;
19813 parser->scope = id->u.id.qualifying_scope;
19814 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19815 if (tmpl
19816 && (DECL_CLASS_TEMPLATE_P (tmpl)
19817 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19818 {
19819 id->u.id.unqualified_name = dguide_name (tmpl);
19820 id->u.id.sfk = sfk_deduction_guide;
19821 ctor_dtor_or_conv_p = 1;
19822 }
19823 }
19824
19825 /* Check to see if the token indicates the start of a
19826 function-definition. */
19827 if (cp_parser_token_starts_function_definition_p (token))
19828 {
19829 if (!function_definition_allowed_p)
19830 {
19831 /* If a function-definition should not appear here, issue an
19832 error message. */
19833 cp_parser_error (parser,
19834 "a function-definition is not allowed here");
19835 return error_mark_node;
19836 }
19837
19838 location_t func_brace_location
19839 = cp_lexer_peek_token (parser->lexer)->location;
19840
19841 /* Neither attributes nor an asm-specification are allowed
19842 on a function-definition. */
19843 if (asm_specification)
19844 error_at (asm_spec_start_token->location,
19845 "an asm-specification is not allowed "
19846 "on a function-definition");
19847 if (attributes)
19848 error_at (attributes_start_token->location,
19849 "attributes are not allowed "
19850 "on a function-definition");
19851 /* This is a function-definition. */
19852 *function_definition_p = true;
19853
19854 /* Parse the function definition. */
19855 if (member_p)
19856 decl = cp_parser_save_member_function_body (parser,
19857 decl_specifiers,
19858 declarator,
19859 prefix_attributes);
19860 else
19861 decl =
19862 (cp_parser_function_definition_from_specifiers_and_declarator
19863 (parser, decl_specifiers, prefix_attributes, declarator));
19864
19865 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19866 {
19867 /* This is where the prologue starts... */
19868 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19869 = func_brace_location;
19870 }
19871
19872 return decl;
19873 }
19874 }
19875 else if (parser->fully_implicit_function_template_p)
19876 {
19877 /* A non-template declaration involving a function parameter list
19878 containing an implicit template parameter will be made into a
19879 template. If the resulting declaration is not going to be an
19880 actual function then finish the template scope here to prevent it.
19881 An error message will be issued once we have a decl to talk about.
19882
19883 FIXME probably we should do type deduction rather than create an
19884 implicit template, but the standard currently doesn't allow it. */
19885 bogus_implicit_tmpl = true;
19886 finish_fully_implicit_template (parser, NULL_TREE);
19887 }
19888
19889 /* [dcl.dcl]
19890
19891 Only in function declarations for constructors, destructors, type
19892 conversions, and deduction guides can the decl-specifier-seq be omitted.
19893
19894 We explicitly postpone this check past the point where we handle
19895 function-definitions because we tolerate function-definitions
19896 that are missing their return types in some modes. */
19897 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19898 {
19899 cp_parser_error (parser,
19900 "expected constructor, destructor, or type conversion");
19901 return error_mark_node;
19902 }
19903
19904 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19905 if (token->type == CPP_EQ
19906 || token->type == CPP_OPEN_PAREN
19907 || token->type == CPP_OPEN_BRACE)
19908 {
19909 is_initialized = SD_INITIALIZED;
19910 initialization_kind = token->type;
19911 if (maybe_range_for_decl)
19912 *maybe_range_for_decl = error_mark_node;
19913 tmp_init_loc = token->location;
19914 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19915 *init_loc = tmp_init_loc;
19916
19917 if (token->type == CPP_EQ
19918 && function_declarator_p (declarator))
19919 {
19920 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19921 if (t2->keyword == RID_DEFAULT)
19922 is_initialized = SD_DEFAULTED;
19923 else if (t2->keyword == RID_DELETE)
19924 is_initialized = SD_DELETED;
19925 }
19926 }
19927 else
19928 {
19929 /* If the init-declarator isn't initialized and isn't followed by a
19930 `,' or `;', it's not a valid init-declarator. */
19931 if (token->type != CPP_COMMA
19932 && token->type != CPP_SEMICOLON)
19933 {
19934 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19935 range_for_decl_p = true;
19936 else
19937 {
19938 if (!maybe_range_for_decl)
19939 cp_parser_error (parser, "expected initializer");
19940 return error_mark_node;
19941 }
19942 }
19943 is_initialized = SD_UNINITIALIZED;
19944 initialization_kind = CPP_EOF;
19945 }
19946
19947 /* Because start_decl has side-effects, we should only call it if we
19948 know we're going ahead. By this point, we know that we cannot
19949 possibly be looking at any other construct. */
19950 cp_parser_commit_to_tentative_parse (parser);
19951
19952 /* Enter the newly declared entry in the symbol table. If we're
19953 processing a declaration in a class-specifier, we wait until
19954 after processing the initializer. */
19955 if (!member_p)
19956 {
19957 if (parser->in_unbraced_linkage_specification_p)
19958 decl_specifiers->storage_class = sc_extern;
19959 decl = start_decl (declarator, decl_specifiers,
19960 range_for_decl_p? SD_INITIALIZED : is_initialized,
19961 attributes, prefix_attributes, &pushed_scope);
19962 cp_finalize_omp_declare_simd (parser, decl);
19963 cp_finalize_oacc_routine (parser, decl, false);
19964 /* Adjust location of decl if declarator->id_loc is more appropriate:
19965 set, and decl wasn't merged with another decl, in which case its
19966 location would be different from input_location, and more accurate. */
19967 if (DECL_P (decl)
19968 && declarator->id_loc != UNKNOWN_LOCATION
19969 && DECL_SOURCE_LOCATION (decl) == input_location)
19970 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19971 }
19972 else if (scope)
19973 /* Enter the SCOPE. That way unqualified names appearing in the
19974 initializer will be looked up in SCOPE. */
19975 pushed_scope = push_scope (scope);
19976
19977 /* Perform deferred access control checks, now that we know in which
19978 SCOPE the declared entity resides. */
19979 if (!member_p && decl)
19980 {
19981 tree saved_current_function_decl = NULL_TREE;
19982
19983 /* If the entity being declared is a function, pretend that we
19984 are in its scope. If it is a `friend', it may have access to
19985 things that would not otherwise be accessible. */
19986 if (TREE_CODE (decl) == FUNCTION_DECL)
19987 {
19988 saved_current_function_decl = current_function_decl;
19989 current_function_decl = decl;
19990 }
19991
19992 /* Perform access checks for template parameters. */
19993 cp_parser_perform_template_parameter_access_checks (checks);
19994
19995 /* Perform the access control checks for the declarator and the
19996 decl-specifiers. */
19997 perform_deferred_access_checks (tf_warning_or_error);
19998
19999 /* Restore the saved value. */
20000 if (TREE_CODE (decl) == FUNCTION_DECL)
20001 current_function_decl = saved_current_function_decl;
20002 }
20003
20004 /* Parse the initializer. */
20005 initializer = NULL_TREE;
20006 is_direct_init = false;
20007 is_non_constant_init = true;
20008 if (is_initialized)
20009 {
20010 if (function_declarator_p (declarator))
20011 {
20012 if (initialization_kind == CPP_EQ)
20013 initializer = cp_parser_pure_specifier (parser);
20014 else
20015 {
20016 /* If the declaration was erroneous, we don't really
20017 know what the user intended, so just silently
20018 consume the initializer. */
20019 if (decl != error_mark_node)
20020 error_at (tmp_init_loc, "initializer provided for function");
20021 cp_parser_skip_to_closing_parenthesis (parser,
20022 /*recovering=*/true,
20023 /*or_comma=*/false,
20024 /*consume_paren=*/true);
20025 }
20026 }
20027 else
20028 {
20029 /* We want to record the extra mangling scope for in-class
20030 initializers of class members and initializers of static data
20031 member templates. The former involves deferring
20032 parsing of the initializer until end of class as with default
20033 arguments. So right here we only handle the latter. */
20034 if (!member_p && processing_template_decl && decl != error_mark_node)
20035 start_lambda_scope (decl);
20036 initializer = cp_parser_initializer (parser,
20037 &is_direct_init,
20038 &is_non_constant_init);
20039 if (!member_p && processing_template_decl && decl != error_mark_node)
20040 finish_lambda_scope ();
20041 if (initializer == error_mark_node)
20042 cp_parser_skip_to_end_of_statement (parser);
20043 }
20044 }
20045
20046 /* The old parser allows attributes to appear after a parenthesized
20047 initializer. Mark Mitchell proposed removing this functionality
20048 on the GCC mailing lists on 2002-08-13. This parser accepts the
20049 attributes -- but ignores them. Made a permerror in GCC 8. */
20050 if (cp_parser_allow_gnu_extensions_p (parser)
20051 && initialization_kind == CPP_OPEN_PAREN
20052 && cp_parser_attributes_opt (parser)
20053 && permerror (input_location,
20054 "attributes after parenthesized initializer ignored"))
20055 {
20056 static bool hint;
20057 if (flag_permissive && !hint)
20058 {
20059 hint = true;
20060 inform (input_location,
20061 "this flexibility is deprecated and will be removed");
20062 }
20063 }
20064
20065 /* And now complain about a non-function implicit template. */
20066 if (bogus_implicit_tmpl && decl != error_mark_node)
20067 error_at (DECL_SOURCE_LOCATION (decl),
20068 "non-function %qD declared as implicit template", decl);
20069
20070 /* For an in-class declaration, use `grokfield' to create the
20071 declaration. */
20072 if (member_p)
20073 {
20074 if (pushed_scope)
20075 {
20076 pop_scope (pushed_scope);
20077 pushed_scope = NULL_TREE;
20078 }
20079 decl = grokfield (declarator, decl_specifiers,
20080 initializer, !is_non_constant_init,
20081 /*asmspec=*/NULL_TREE,
20082 attr_chainon (attributes, prefix_attributes));
20083 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20084 cp_parser_save_default_args (parser, decl);
20085 cp_finalize_omp_declare_simd (parser, decl);
20086 cp_finalize_oacc_routine (parser, decl, false);
20087 }
20088
20089 /* Finish processing the declaration. But, skip member
20090 declarations. */
20091 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20092 {
20093 cp_finish_decl (decl,
20094 initializer, !is_non_constant_init,
20095 asm_specification,
20096 /* If the initializer is in parentheses, then this is
20097 a direct-initialization, which means that an
20098 `explicit' constructor is OK. Otherwise, an
20099 `explicit' constructor cannot be used. */
20100 ((is_direct_init || !is_initialized)
20101 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20102 }
20103 else if ((cxx_dialect != cxx98) && friend_p
20104 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20105 /* Core issue #226 (C++0x only): A default template-argument
20106 shall not be specified in a friend class template
20107 declaration. */
20108 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20109 /*is_partial=*/false, /*is_friend_decl=*/1);
20110
20111 if (!friend_p && pushed_scope)
20112 pop_scope (pushed_scope);
20113
20114 if (function_declarator_p (declarator)
20115 && parser->fully_implicit_function_template_p)
20116 {
20117 if (member_p)
20118 decl = finish_fully_implicit_template (parser, decl);
20119 else
20120 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20121 }
20122
20123 if (auto_result && is_initialized && decl_specifiers->type
20124 && type_uses_auto (decl_specifiers->type))
20125 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20126
20127 return decl;
20128 }
20129
20130 /* Parse a declarator.
20131
20132 declarator:
20133 direct-declarator
20134 ptr-operator declarator
20135
20136 abstract-declarator:
20137 ptr-operator abstract-declarator [opt]
20138 direct-abstract-declarator
20139
20140 GNU Extensions:
20141
20142 declarator:
20143 attributes [opt] direct-declarator
20144 attributes [opt] ptr-operator declarator
20145
20146 abstract-declarator:
20147 attributes [opt] ptr-operator abstract-declarator [opt]
20148 attributes [opt] direct-abstract-declarator
20149
20150 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20151 detect constructors, destructors, deduction guides, or conversion operators.
20152 It is set to -1 if the declarator is a name, and +1 if it is a
20153 function. Otherwise it is set to zero. Usually you just want to
20154 test for >0, but internally the negative value is used.
20155
20156 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20157 a decl-specifier-seq unless it declares a constructor, destructor,
20158 or conversion. It might seem that we could check this condition in
20159 semantic analysis, rather than parsing, but that makes it difficult
20160 to handle something like `f()'. We want to notice that there are
20161 no decl-specifiers, and therefore realize that this is an
20162 expression, not a declaration.)
20163
20164 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20165 the declarator is a direct-declarator of the form "(...)".
20166
20167 MEMBER_P is true iff this declarator is a member-declarator.
20168
20169 FRIEND_P is true iff this declarator is a friend. */
20170
20171 static cp_declarator *
20172 cp_parser_declarator (cp_parser* parser,
20173 cp_parser_declarator_kind dcl_kind,
20174 int* ctor_dtor_or_conv_p,
20175 bool* parenthesized_p,
20176 bool member_p, bool friend_p)
20177 {
20178 cp_declarator *declarator;
20179 enum tree_code code;
20180 cp_cv_quals cv_quals;
20181 tree class_type;
20182 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20183
20184 /* Assume this is not a constructor, destructor, or type-conversion
20185 operator. */
20186 if (ctor_dtor_or_conv_p)
20187 *ctor_dtor_or_conv_p = 0;
20188
20189 if (cp_parser_allow_gnu_extensions_p (parser))
20190 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20191
20192 /* Check for the ptr-operator production. */
20193 cp_parser_parse_tentatively (parser);
20194 /* Parse the ptr-operator. */
20195 code = cp_parser_ptr_operator (parser,
20196 &class_type,
20197 &cv_quals,
20198 &std_attributes);
20199
20200 /* If that worked, then we have a ptr-operator. */
20201 if (cp_parser_parse_definitely (parser))
20202 {
20203 /* If a ptr-operator was found, then this declarator was not
20204 parenthesized. */
20205 if (parenthesized_p)
20206 *parenthesized_p = true;
20207 /* The dependent declarator is optional if we are parsing an
20208 abstract-declarator. */
20209 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20210 cp_parser_parse_tentatively (parser);
20211
20212 /* Parse the dependent declarator. */
20213 declarator = cp_parser_declarator (parser, dcl_kind,
20214 /*ctor_dtor_or_conv_p=*/NULL,
20215 /*parenthesized_p=*/NULL,
20216 /*member_p=*/false,
20217 friend_p);
20218
20219 /* If we are parsing an abstract-declarator, we must handle the
20220 case where the dependent declarator is absent. */
20221 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20222 && !cp_parser_parse_definitely (parser))
20223 declarator = NULL;
20224
20225 declarator = cp_parser_make_indirect_declarator
20226 (code, class_type, cv_quals, declarator, std_attributes);
20227 }
20228 /* Everything else is a direct-declarator. */
20229 else
20230 {
20231 if (parenthesized_p)
20232 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20233 CPP_OPEN_PAREN);
20234 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20235 ctor_dtor_or_conv_p,
20236 member_p, friend_p);
20237 }
20238
20239 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20240 declarator->attributes = gnu_attributes;
20241 return declarator;
20242 }
20243
20244 /* Parse a direct-declarator or direct-abstract-declarator.
20245
20246 direct-declarator:
20247 declarator-id
20248 direct-declarator ( parameter-declaration-clause )
20249 cv-qualifier-seq [opt]
20250 ref-qualifier [opt]
20251 exception-specification [opt]
20252 direct-declarator [ constant-expression [opt] ]
20253 ( declarator )
20254
20255 direct-abstract-declarator:
20256 direct-abstract-declarator [opt]
20257 ( parameter-declaration-clause )
20258 cv-qualifier-seq [opt]
20259 ref-qualifier [opt]
20260 exception-specification [opt]
20261 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20262 ( abstract-declarator )
20263
20264 Returns a representation of the declarator. DCL_KIND is
20265 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20266 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20267 we are parsing a direct-declarator. It is
20268 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20269 of ambiguity we prefer an abstract declarator, as per
20270 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20271 as for cp_parser_declarator. */
20272
20273 static cp_declarator *
20274 cp_parser_direct_declarator (cp_parser* parser,
20275 cp_parser_declarator_kind dcl_kind,
20276 int* ctor_dtor_or_conv_p,
20277 bool member_p, bool friend_p)
20278 {
20279 cp_token *token;
20280 cp_declarator *declarator = NULL;
20281 tree scope = NULL_TREE;
20282 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20283 bool saved_in_declarator_p = parser->in_declarator_p;
20284 bool first = true;
20285 tree pushed_scope = NULL_TREE;
20286 cp_token *open_paren = NULL, *close_paren = NULL;
20287
20288 while (true)
20289 {
20290 /* Peek at the next token. */
20291 token = cp_lexer_peek_token (parser->lexer);
20292 if (token->type == CPP_OPEN_PAREN)
20293 {
20294 /* This is either a parameter-declaration-clause, or a
20295 parenthesized declarator. When we know we are parsing a
20296 named declarator, it must be a parenthesized declarator
20297 if FIRST is true. For instance, `(int)' is a
20298 parameter-declaration-clause, with an omitted
20299 direct-abstract-declarator. But `((*))', is a
20300 parenthesized abstract declarator. Finally, when T is a
20301 template parameter `(T)' is a
20302 parameter-declaration-clause, and not a parenthesized
20303 named declarator.
20304
20305 We first try and parse a parameter-declaration-clause,
20306 and then try a nested declarator (if FIRST is true).
20307
20308 It is not an error for it not to be a
20309 parameter-declaration-clause, even when FIRST is
20310 false. Consider,
20311
20312 int i (int);
20313 int i (3);
20314
20315 The first is the declaration of a function while the
20316 second is the definition of a variable, including its
20317 initializer.
20318
20319 Having seen only the parenthesis, we cannot know which of
20320 these two alternatives should be selected. Even more
20321 complex are examples like:
20322
20323 int i (int (a));
20324 int i (int (3));
20325
20326 The former is a function-declaration; the latter is a
20327 variable initialization.
20328
20329 Thus again, we try a parameter-declaration-clause, and if
20330 that fails, we back out and return. */
20331
20332 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20333 {
20334 tree params;
20335 bool is_declarator = false;
20336
20337 open_paren = NULL;
20338
20339 /* In a member-declarator, the only valid interpretation
20340 of a parenthesis is the start of a
20341 parameter-declaration-clause. (It is invalid to
20342 initialize a static data member with a parenthesized
20343 initializer; only the "=" form of initialization is
20344 permitted.) */
20345 if (!member_p)
20346 cp_parser_parse_tentatively (parser);
20347
20348 /* Consume the `('. */
20349 matching_parens parens;
20350 parens.consume_open (parser);
20351 if (first)
20352 {
20353 /* If this is going to be an abstract declarator, we're
20354 in a declarator and we can't have default args. */
20355 parser->default_arg_ok_p = false;
20356 parser->in_declarator_p = true;
20357 }
20358
20359 begin_scope (sk_function_parms, NULL_TREE);
20360
20361 /* Parse the parameter-declaration-clause. */
20362 params = cp_parser_parameter_declaration_clause (parser);
20363
20364 /* Consume the `)'. */
20365 parens.require_close (parser);
20366
20367 /* If all went well, parse the cv-qualifier-seq,
20368 ref-qualifier and the exception-specification. */
20369 if (member_p || cp_parser_parse_definitely (parser))
20370 {
20371 cp_cv_quals cv_quals;
20372 cp_virt_specifiers virt_specifiers;
20373 cp_ref_qualifier ref_qual;
20374 tree exception_specification;
20375 tree late_return;
20376 tree attrs;
20377 bool memfn = (member_p || (pushed_scope
20378 && CLASS_TYPE_P (pushed_scope)));
20379
20380 is_declarator = true;
20381
20382 if (ctor_dtor_or_conv_p)
20383 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20384 first = false;
20385
20386 /* Parse the cv-qualifier-seq. */
20387 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20388 /* Parse the ref-qualifier. */
20389 ref_qual = cp_parser_ref_qualifier_opt (parser);
20390 /* Parse the tx-qualifier. */
20391 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20392 /* And the exception-specification. */
20393 exception_specification
20394 = cp_parser_exception_specification_opt (parser);
20395
20396 attrs = cp_parser_std_attribute_spec_seq (parser);
20397
20398 /* In here, we handle cases where attribute is used after
20399 the function declaration. For example:
20400 void func (int x) __attribute__((vector(..))); */
20401 tree gnu_attrs = NULL_TREE;
20402 tree requires_clause = NULL_TREE;
20403 late_return = (cp_parser_late_return_type_opt
20404 (parser, declarator, requires_clause,
20405 memfn ? cv_quals : -1));
20406
20407 /* Parse the virt-specifier-seq. */
20408 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20409
20410 /* Create the function-declarator. */
20411 declarator = make_call_declarator (declarator,
20412 params,
20413 cv_quals,
20414 virt_specifiers,
20415 ref_qual,
20416 tx_qual,
20417 exception_specification,
20418 late_return,
20419 requires_clause);
20420 declarator->std_attributes = attrs;
20421 declarator->attributes = gnu_attrs;
20422 /* Any subsequent parameter lists are to do with
20423 return type, so are not those of the declared
20424 function. */
20425 parser->default_arg_ok_p = false;
20426 }
20427
20428 /* Remove the function parms from scope. */
20429 pop_bindings_and_leave_scope ();
20430
20431 if (is_declarator)
20432 /* Repeat the main loop. */
20433 continue;
20434 }
20435
20436 /* If this is the first, we can try a parenthesized
20437 declarator. */
20438 if (first)
20439 {
20440 bool saved_in_type_id_in_expr_p;
20441
20442 parser->default_arg_ok_p = saved_default_arg_ok_p;
20443 parser->in_declarator_p = saved_in_declarator_p;
20444
20445 open_paren = token;
20446 /* Consume the `('. */
20447 matching_parens parens;
20448 parens.consume_open (parser);
20449 /* Parse the nested declarator. */
20450 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20451 parser->in_type_id_in_expr_p = true;
20452 declarator
20453 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20454 /*parenthesized_p=*/NULL,
20455 member_p, friend_p);
20456 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20457 first = false;
20458 /* Expect a `)'. */
20459 close_paren = cp_lexer_peek_token (parser->lexer);
20460 if (!parens.require_close (parser))
20461 declarator = cp_error_declarator;
20462 if (declarator == cp_error_declarator)
20463 break;
20464
20465 goto handle_declarator;
20466 }
20467 /* Otherwise, we must be done. */
20468 else
20469 break;
20470 }
20471 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20472 && token->type == CPP_OPEN_SQUARE
20473 && !cp_next_tokens_can_be_attribute_p (parser))
20474 {
20475 /* Parse an array-declarator. */
20476 tree bounds, attrs;
20477
20478 if (ctor_dtor_or_conv_p)
20479 *ctor_dtor_or_conv_p = 0;
20480
20481 open_paren = NULL;
20482 first = false;
20483 parser->default_arg_ok_p = false;
20484 parser->in_declarator_p = true;
20485 /* Consume the `['. */
20486 cp_lexer_consume_token (parser->lexer);
20487 /* Peek at the next token. */
20488 token = cp_lexer_peek_token (parser->lexer);
20489 /* If the next token is `]', then there is no
20490 constant-expression. */
20491 if (token->type != CPP_CLOSE_SQUARE)
20492 {
20493 bool non_constant_p;
20494 bounds
20495 = cp_parser_constant_expression (parser,
20496 /*allow_non_constant=*/true,
20497 &non_constant_p);
20498 if (!non_constant_p)
20499 /* OK */;
20500 else if (error_operand_p (bounds))
20501 /* Already gave an error. */;
20502 else if (!parser->in_function_body
20503 || current_binding_level->kind == sk_function_parms)
20504 {
20505 /* Normally, the array bound must be an integral constant
20506 expression. However, as an extension, we allow VLAs
20507 in function scopes as long as they aren't part of a
20508 parameter declaration. */
20509 cp_parser_error (parser,
20510 "array bound is not an integer constant");
20511 bounds = error_mark_node;
20512 }
20513 else if (processing_template_decl
20514 && !type_dependent_expression_p (bounds))
20515 {
20516 /* Remember this wasn't a constant-expression. */
20517 bounds = build_nop (TREE_TYPE (bounds), bounds);
20518 TREE_SIDE_EFFECTS (bounds) = 1;
20519 }
20520 }
20521 else
20522 bounds = NULL_TREE;
20523 /* Look for the closing `]'. */
20524 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20525 {
20526 declarator = cp_error_declarator;
20527 break;
20528 }
20529
20530 attrs = cp_parser_std_attribute_spec_seq (parser);
20531 declarator = make_array_declarator (declarator, bounds);
20532 declarator->std_attributes = attrs;
20533 }
20534 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20535 {
20536 {
20537 tree qualifying_scope;
20538 tree unqualified_name;
20539 tree attrs;
20540 special_function_kind sfk;
20541 bool abstract_ok;
20542 bool pack_expansion_p = false;
20543 cp_token *declarator_id_start_token;
20544
20545 /* Parse a declarator-id */
20546 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20547 if (abstract_ok)
20548 {
20549 cp_parser_parse_tentatively (parser);
20550
20551 /* If we see an ellipsis, we should be looking at a
20552 parameter pack. */
20553 if (token->type == CPP_ELLIPSIS)
20554 {
20555 /* Consume the `...' */
20556 cp_lexer_consume_token (parser->lexer);
20557
20558 pack_expansion_p = true;
20559 }
20560 }
20561
20562 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20563 unqualified_name
20564 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20565 qualifying_scope = parser->scope;
20566 if (abstract_ok)
20567 {
20568 bool okay = false;
20569
20570 if (!unqualified_name && pack_expansion_p)
20571 {
20572 /* Check whether an error occurred. */
20573 okay = !cp_parser_error_occurred (parser);
20574
20575 /* We already consumed the ellipsis to mark a
20576 parameter pack, but we have no way to report it,
20577 so abort the tentative parse. We will be exiting
20578 immediately anyway. */
20579 cp_parser_abort_tentative_parse (parser);
20580 }
20581 else
20582 okay = cp_parser_parse_definitely (parser);
20583
20584 if (!okay)
20585 unqualified_name = error_mark_node;
20586 else if (unqualified_name
20587 && (qualifying_scope
20588 || (!identifier_p (unqualified_name))))
20589 {
20590 cp_parser_error (parser, "expected unqualified-id");
20591 unqualified_name = error_mark_node;
20592 }
20593 }
20594
20595 if (!unqualified_name)
20596 return NULL;
20597 if (unqualified_name == error_mark_node)
20598 {
20599 declarator = cp_error_declarator;
20600 pack_expansion_p = false;
20601 declarator->parameter_pack_p = false;
20602 break;
20603 }
20604
20605 attrs = cp_parser_std_attribute_spec_seq (parser);
20606
20607 if (qualifying_scope && at_namespace_scope_p ()
20608 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20609 {
20610 /* In the declaration of a member of a template class
20611 outside of the class itself, the SCOPE will sometimes
20612 be a TYPENAME_TYPE. For example, given:
20613
20614 template <typename T>
20615 int S<T>::R::i = 3;
20616
20617 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20618 this context, we must resolve S<T>::R to an ordinary
20619 type, rather than a typename type.
20620
20621 The reason we normally avoid resolving TYPENAME_TYPEs
20622 is that a specialization of `S' might render
20623 `S<T>::R' not a type. However, if `S' is
20624 specialized, then this `i' will not be used, so there
20625 is no harm in resolving the types here. */
20626 tree type;
20627
20628 /* Resolve the TYPENAME_TYPE. */
20629 type = resolve_typename_type (qualifying_scope,
20630 /*only_current_p=*/false);
20631 /* If that failed, the declarator is invalid. */
20632 if (TREE_CODE (type) == TYPENAME_TYPE)
20633 {
20634 if (typedef_variant_p (type))
20635 error_at (declarator_id_start_token->location,
20636 "cannot define member of dependent typedef "
20637 "%qT", type);
20638 else
20639 error_at (declarator_id_start_token->location,
20640 "%<%T::%E%> is not a type",
20641 TYPE_CONTEXT (qualifying_scope),
20642 TYPE_IDENTIFIER (qualifying_scope));
20643 }
20644 qualifying_scope = type;
20645 }
20646
20647 sfk = sfk_none;
20648
20649 if (unqualified_name)
20650 {
20651 tree class_type;
20652
20653 if (qualifying_scope
20654 && CLASS_TYPE_P (qualifying_scope))
20655 class_type = qualifying_scope;
20656 else
20657 class_type = current_class_type;
20658
20659 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20660 {
20661 tree name_type = TREE_TYPE (unqualified_name);
20662
20663 if (!class_type || !same_type_p (name_type, class_type))
20664 {
20665 /* We do not attempt to print the declarator
20666 here because we do not have enough
20667 information about its original syntactic
20668 form. */
20669 cp_parser_error (parser, "invalid declarator");
20670 declarator = cp_error_declarator;
20671 break;
20672 }
20673 else if (qualifying_scope
20674 && CLASSTYPE_USE_TEMPLATE (name_type))
20675 {
20676 error_at (declarator_id_start_token->location,
20677 "invalid use of constructor as a template");
20678 inform (declarator_id_start_token->location,
20679 "use %<%T::%D%> instead of %<%T::%D%> to "
20680 "name the constructor in a qualified name",
20681 class_type,
20682 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20683 class_type, name_type);
20684 declarator = cp_error_declarator;
20685 break;
20686 }
20687 unqualified_name = constructor_name (class_type);
20688 }
20689
20690 if (class_type)
20691 {
20692 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20693 sfk = sfk_destructor;
20694 else if (identifier_p (unqualified_name)
20695 && IDENTIFIER_CONV_OP_P (unqualified_name))
20696 sfk = sfk_conversion;
20697 else if (/* There's no way to declare a constructor
20698 for an unnamed type, even if the type
20699 got a name for linkage purposes. */
20700 !TYPE_WAS_UNNAMED (class_type)
20701 /* Handle correctly (c++/19200):
20702
20703 struct S {
20704 struct T{};
20705 friend void S(T);
20706 };
20707
20708 and also:
20709
20710 namespace N {
20711 void S();
20712 }
20713
20714 struct S {
20715 friend void N::S();
20716 }; */
20717 && (!friend_p || class_type == qualifying_scope)
20718 && constructor_name_p (unqualified_name,
20719 class_type))
20720 sfk = sfk_constructor;
20721 else if (is_overloaded_fn (unqualified_name)
20722 && DECL_CONSTRUCTOR_P (get_first_fn
20723 (unqualified_name)))
20724 sfk = sfk_constructor;
20725
20726 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20727 *ctor_dtor_or_conv_p = -1;
20728 }
20729 }
20730 declarator = make_id_declarator (qualifying_scope,
20731 unqualified_name,
20732 sfk, token->location);
20733 declarator->std_attributes = attrs;
20734 declarator->parameter_pack_p = pack_expansion_p;
20735
20736 if (pack_expansion_p)
20737 maybe_warn_variadic_templates ();
20738 }
20739
20740 handle_declarator:;
20741 scope = get_scope_of_declarator (declarator);
20742 if (scope)
20743 {
20744 /* Any names that appear after the declarator-id for a
20745 member are looked up in the containing scope. */
20746 if (at_function_scope_p ())
20747 {
20748 /* But declarations with qualified-ids can't appear in a
20749 function. */
20750 cp_parser_error (parser, "qualified-id in declaration");
20751 declarator = cp_error_declarator;
20752 break;
20753 }
20754 pushed_scope = push_scope (scope);
20755 }
20756 parser->in_declarator_p = true;
20757 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20758 || (declarator && declarator->kind == cdk_id))
20759 /* Default args are only allowed on function
20760 declarations. */
20761 parser->default_arg_ok_p = saved_default_arg_ok_p;
20762 else
20763 parser->default_arg_ok_p = false;
20764
20765 first = false;
20766 }
20767 /* We're done. */
20768 else
20769 break;
20770 }
20771
20772 /* For an abstract declarator, we might wind up with nothing at this
20773 point. That's an error; the declarator is not optional. */
20774 if (!declarator)
20775 cp_parser_error (parser, "expected declarator");
20776 else if (open_paren)
20777 {
20778 /* Record overly parenthesized declarator so we can give a
20779 diagnostic about confusing decl/expr disambiguation. */
20780 if (declarator->kind == cdk_array)
20781 {
20782 /* If the open and close parens are on different lines, this
20783 is probably a formatting thing, so ignore. */
20784 expanded_location open = expand_location (open_paren->location);
20785 expanded_location close = expand_location (close_paren->location);
20786 if (open.line != close.line || open.file != close.file)
20787 open_paren = NULL;
20788 }
20789 if (open_paren)
20790 declarator->parenthesized = open_paren->location;
20791 }
20792
20793 /* If we entered a scope, we must exit it now. */
20794 if (pushed_scope)
20795 pop_scope (pushed_scope);
20796
20797 parser->default_arg_ok_p = saved_default_arg_ok_p;
20798 parser->in_declarator_p = saved_in_declarator_p;
20799
20800 return declarator;
20801 }
20802
20803 /* Parse a ptr-operator.
20804
20805 ptr-operator:
20806 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20807 * cv-qualifier-seq [opt]
20808 &
20809 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20810 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20811
20812 GNU Extension:
20813
20814 ptr-operator:
20815 & cv-qualifier-seq [opt]
20816
20817 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20818 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20819 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20820 filled in with the TYPE containing the member. *CV_QUALS is
20821 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20822 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20823 Note that the tree codes returned by this function have nothing
20824 to do with the types of trees that will be eventually be created
20825 to represent the pointer or reference type being parsed. They are
20826 just constants with suggestive names. */
20827 static enum tree_code
20828 cp_parser_ptr_operator (cp_parser* parser,
20829 tree* type,
20830 cp_cv_quals *cv_quals,
20831 tree *attributes)
20832 {
20833 enum tree_code code = ERROR_MARK;
20834 cp_token *token;
20835 tree attrs = NULL_TREE;
20836
20837 /* Assume that it's not a pointer-to-member. */
20838 *type = NULL_TREE;
20839 /* And that there are no cv-qualifiers. */
20840 *cv_quals = TYPE_UNQUALIFIED;
20841
20842 /* Peek at the next token. */
20843 token = cp_lexer_peek_token (parser->lexer);
20844
20845 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20846 if (token->type == CPP_MULT)
20847 code = INDIRECT_REF;
20848 else if (token->type == CPP_AND)
20849 code = ADDR_EXPR;
20850 else if ((cxx_dialect != cxx98) &&
20851 token->type == CPP_AND_AND) /* C++0x only */
20852 code = NON_LVALUE_EXPR;
20853
20854 if (code != ERROR_MARK)
20855 {
20856 /* Consume the `*', `&' or `&&'. */
20857 cp_lexer_consume_token (parser->lexer);
20858
20859 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20860 `&', if we are allowing GNU extensions. (The only qualifier
20861 that can legally appear after `&' is `restrict', but that is
20862 enforced during semantic analysis. */
20863 if (code == INDIRECT_REF
20864 || cp_parser_allow_gnu_extensions_p (parser))
20865 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20866
20867 attrs = cp_parser_std_attribute_spec_seq (parser);
20868 if (attributes != NULL)
20869 *attributes = attrs;
20870 }
20871 else
20872 {
20873 /* Try the pointer-to-member case. */
20874 cp_parser_parse_tentatively (parser);
20875 /* Look for the optional `::' operator. */
20876 cp_parser_global_scope_opt (parser,
20877 /*current_scope_valid_p=*/false);
20878 /* Look for the nested-name specifier. */
20879 token = cp_lexer_peek_token (parser->lexer);
20880 cp_parser_nested_name_specifier (parser,
20881 /*typename_keyword_p=*/false,
20882 /*check_dependency_p=*/true,
20883 /*type_p=*/false,
20884 /*is_declaration=*/false);
20885 /* If we found it, and the next token is a `*', then we are
20886 indeed looking at a pointer-to-member operator. */
20887 if (!cp_parser_error_occurred (parser)
20888 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20889 {
20890 /* Indicate that the `*' operator was used. */
20891 code = INDIRECT_REF;
20892
20893 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20894 error_at (token->location, "%qD is a namespace", parser->scope);
20895 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20896 error_at (token->location, "cannot form pointer to member of "
20897 "non-class %q#T", parser->scope);
20898 else
20899 {
20900 /* The type of which the member is a member is given by the
20901 current SCOPE. */
20902 *type = parser->scope;
20903 /* The next name will not be qualified. */
20904 parser->scope = NULL_TREE;
20905 parser->qualifying_scope = NULL_TREE;
20906 parser->object_scope = NULL_TREE;
20907 /* Look for optional c++11 attributes. */
20908 attrs = cp_parser_std_attribute_spec_seq (parser);
20909 if (attributes != NULL)
20910 *attributes = attrs;
20911 /* Look for the optional cv-qualifier-seq. */
20912 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20913 }
20914 }
20915 /* If that didn't work we don't have a ptr-operator. */
20916 if (!cp_parser_parse_definitely (parser))
20917 cp_parser_error (parser, "expected ptr-operator");
20918 }
20919
20920 return code;
20921 }
20922
20923 /* Parse an (optional) cv-qualifier-seq.
20924
20925 cv-qualifier-seq:
20926 cv-qualifier cv-qualifier-seq [opt]
20927
20928 cv-qualifier:
20929 const
20930 volatile
20931
20932 GNU Extension:
20933
20934 cv-qualifier:
20935 __restrict__
20936
20937 Returns a bitmask representing the cv-qualifiers. */
20938
20939 static cp_cv_quals
20940 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20941 {
20942 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20943
20944 while (true)
20945 {
20946 cp_token *token;
20947 cp_cv_quals cv_qualifier;
20948
20949 /* Peek at the next token. */
20950 token = cp_lexer_peek_token (parser->lexer);
20951 /* See if it's a cv-qualifier. */
20952 switch (token->keyword)
20953 {
20954 case RID_CONST:
20955 cv_qualifier = TYPE_QUAL_CONST;
20956 break;
20957
20958 case RID_VOLATILE:
20959 cv_qualifier = TYPE_QUAL_VOLATILE;
20960 break;
20961
20962 case RID_RESTRICT:
20963 cv_qualifier = TYPE_QUAL_RESTRICT;
20964 break;
20965
20966 default:
20967 cv_qualifier = TYPE_UNQUALIFIED;
20968 break;
20969 }
20970
20971 if (!cv_qualifier)
20972 break;
20973
20974 if (cv_quals & cv_qualifier)
20975 {
20976 gcc_rich_location richloc (token->location);
20977 richloc.add_fixit_remove ();
20978 error_at (&richloc, "duplicate cv-qualifier");
20979 cp_lexer_purge_token (parser->lexer);
20980 }
20981 else
20982 {
20983 cp_lexer_consume_token (parser->lexer);
20984 cv_quals |= cv_qualifier;
20985 }
20986 }
20987
20988 return cv_quals;
20989 }
20990
20991 /* Parse an (optional) ref-qualifier
20992
20993 ref-qualifier:
20994 &
20995 &&
20996
20997 Returns cp_ref_qualifier representing ref-qualifier. */
20998
20999 static cp_ref_qualifier
21000 cp_parser_ref_qualifier_opt (cp_parser* parser)
21001 {
21002 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21003
21004 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21005 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21006 return ref_qual;
21007
21008 while (true)
21009 {
21010 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21011 cp_token *token = cp_lexer_peek_token (parser->lexer);
21012
21013 switch (token->type)
21014 {
21015 case CPP_AND:
21016 curr_ref_qual = REF_QUAL_LVALUE;
21017 break;
21018
21019 case CPP_AND_AND:
21020 curr_ref_qual = REF_QUAL_RVALUE;
21021 break;
21022
21023 default:
21024 curr_ref_qual = REF_QUAL_NONE;
21025 break;
21026 }
21027
21028 if (!curr_ref_qual)
21029 break;
21030 else if (ref_qual)
21031 {
21032 error_at (token->location, "multiple ref-qualifiers");
21033 cp_lexer_purge_token (parser->lexer);
21034 }
21035 else
21036 {
21037 ref_qual = curr_ref_qual;
21038 cp_lexer_consume_token (parser->lexer);
21039 }
21040 }
21041
21042 return ref_qual;
21043 }
21044
21045 /* Parse an optional tx-qualifier.
21046
21047 tx-qualifier:
21048 transaction_safe
21049 transaction_safe_dynamic */
21050
21051 static tree
21052 cp_parser_tx_qualifier_opt (cp_parser *parser)
21053 {
21054 cp_token *token = cp_lexer_peek_token (parser->lexer);
21055 if (token->type == CPP_NAME)
21056 {
21057 tree name = token->u.value;
21058 const char *p = IDENTIFIER_POINTER (name);
21059 const int len = strlen ("transaction_safe");
21060 if (!strncmp (p, "transaction_safe", len))
21061 {
21062 p += len;
21063 if (*p == '\0'
21064 || !strcmp (p, "_dynamic"))
21065 {
21066 cp_lexer_consume_token (parser->lexer);
21067 if (!flag_tm)
21068 {
21069 error ("%qE requires %<-fgnu-tm%>", name);
21070 return NULL_TREE;
21071 }
21072 else
21073 return name;
21074 }
21075 }
21076 }
21077 return NULL_TREE;
21078 }
21079
21080 /* Parse an (optional) virt-specifier-seq.
21081
21082 virt-specifier-seq:
21083 virt-specifier virt-specifier-seq [opt]
21084
21085 virt-specifier:
21086 override
21087 final
21088
21089 Returns a bitmask representing the virt-specifiers. */
21090
21091 static cp_virt_specifiers
21092 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21093 {
21094 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21095
21096 while (true)
21097 {
21098 cp_token *token;
21099 cp_virt_specifiers virt_specifier;
21100
21101 /* Peek at the next token. */
21102 token = cp_lexer_peek_token (parser->lexer);
21103 /* See if it's a virt-specifier-qualifier. */
21104 if (token->type != CPP_NAME)
21105 break;
21106 if (id_equal (token->u.value, "override"))
21107 {
21108 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21109 virt_specifier = VIRT_SPEC_OVERRIDE;
21110 }
21111 else if (id_equal (token->u.value, "final"))
21112 {
21113 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21114 virt_specifier = VIRT_SPEC_FINAL;
21115 }
21116 else if (id_equal (token->u.value, "__final"))
21117 {
21118 virt_specifier = VIRT_SPEC_FINAL;
21119 }
21120 else
21121 break;
21122
21123 if (virt_specifiers & virt_specifier)
21124 {
21125 gcc_rich_location richloc (token->location);
21126 richloc.add_fixit_remove ();
21127 error_at (&richloc, "duplicate virt-specifier");
21128 cp_lexer_purge_token (parser->lexer);
21129 }
21130 else
21131 {
21132 cp_lexer_consume_token (parser->lexer);
21133 virt_specifiers |= virt_specifier;
21134 }
21135 }
21136 return virt_specifiers;
21137 }
21138
21139 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21140 is in scope even though it isn't real. */
21141
21142 void
21143 inject_this_parameter (tree ctype, cp_cv_quals quals)
21144 {
21145 tree this_parm;
21146
21147 if (current_class_ptr)
21148 {
21149 /* We don't clear this between NSDMIs. Is it already what we want? */
21150 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21151 if (DECL_P (current_class_ptr)
21152 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21153 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21154 && cp_type_quals (type) == quals)
21155 return;
21156 }
21157
21158 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21159 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21160 current_class_ptr = NULL_TREE;
21161 current_class_ref
21162 = cp_build_fold_indirect_ref (this_parm);
21163 current_class_ptr = this_parm;
21164 }
21165
21166 /* Return true iff our current scope is a non-static data member
21167 initializer. */
21168
21169 bool
21170 parsing_nsdmi (void)
21171 {
21172 /* We recognize NSDMI context by the context-less 'this' pointer set up
21173 by the function above. */
21174 if (current_class_ptr
21175 && TREE_CODE (current_class_ptr) == PARM_DECL
21176 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21177 return true;
21178 return false;
21179 }
21180
21181 /* Parse a late-specified return type, if any. This is not a separate
21182 non-terminal, but part of a function declarator, which looks like
21183
21184 -> trailing-type-specifier-seq abstract-declarator(opt)
21185
21186 Returns the type indicated by the type-id.
21187
21188 In addition to this, parse any queued up #pragma omp declare simd
21189 clauses, and #pragma acc routine clauses.
21190
21191 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21192 function. */
21193
21194 static tree
21195 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21196 tree& requires_clause, cp_cv_quals quals)
21197 {
21198 cp_token *token;
21199 tree type = NULL_TREE;
21200 bool declare_simd_p = (parser->omp_declare_simd
21201 && declarator
21202 && declarator->kind == cdk_id);
21203
21204 bool oacc_routine_p = (parser->oacc_routine
21205 && declarator
21206 && declarator->kind == cdk_id);
21207
21208 /* Peek at the next token. */
21209 token = cp_lexer_peek_token (parser->lexer);
21210 /* A late-specified return type is indicated by an initial '->'. */
21211 if (token->type != CPP_DEREF
21212 && token->keyword != RID_REQUIRES
21213 && !(token->type == CPP_NAME
21214 && token->u.value == ridpointers[RID_REQUIRES])
21215 && !(declare_simd_p || oacc_routine_p))
21216 return NULL_TREE;
21217
21218 tree save_ccp = current_class_ptr;
21219 tree save_ccr = current_class_ref;
21220 if (quals >= 0)
21221 {
21222 /* DR 1207: 'this' is in scope in the trailing return type. */
21223 inject_this_parameter (current_class_type, quals);
21224 }
21225
21226 if (token->type == CPP_DEREF)
21227 {
21228 /* Consume the ->. */
21229 cp_lexer_consume_token (parser->lexer);
21230
21231 type = cp_parser_trailing_type_id (parser);
21232 }
21233
21234 /* Function declarations may be followed by a trailing
21235 requires-clause. */
21236 requires_clause = cp_parser_requires_clause_opt (parser);
21237
21238 if (declare_simd_p)
21239 declarator->attributes
21240 = cp_parser_late_parsing_omp_declare_simd (parser,
21241 declarator->attributes);
21242 if (oacc_routine_p)
21243 declarator->attributes
21244 = cp_parser_late_parsing_oacc_routine (parser,
21245 declarator->attributes);
21246
21247 if (quals >= 0)
21248 {
21249 current_class_ptr = save_ccp;
21250 current_class_ref = save_ccr;
21251 }
21252
21253 return type;
21254 }
21255
21256 /* Parse a declarator-id.
21257
21258 declarator-id:
21259 id-expression
21260 :: [opt] nested-name-specifier [opt] type-name
21261
21262 In the `id-expression' case, the value returned is as for
21263 cp_parser_id_expression if the id-expression was an unqualified-id.
21264 If the id-expression was a qualified-id, then a SCOPE_REF is
21265 returned. The first operand is the scope (either a NAMESPACE_DECL
21266 or TREE_TYPE), but the second is still just a representation of an
21267 unqualified-id. */
21268
21269 static tree
21270 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21271 {
21272 tree id;
21273 /* The expression must be an id-expression. Assume that qualified
21274 names are the names of types so that:
21275
21276 template <class T>
21277 int S<T>::R::i = 3;
21278
21279 will work; we must treat `S<T>::R' as the name of a type.
21280 Similarly, assume that qualified names are templates, where
21281 required, so that:
21282
21283 template <class T>
21284 int S<T>::R<T>::i = 3;
21285
21286 will work, too. */
21287 id = cp_parser_id_expression (parser,
21288 /*template_keyword_p=*/false,
21289 /*check_dependency_p=*/false,
21290 /*template_p=*/NULL,
21291 /*declarator_p=*/true,
21292 optional_p);
21293 if (id && BASELINK_P (id))
21294 id = BASELINK_FUNCTIONS (id);
21295 return id;
21296 }
21297
21298 /* Parse a type-id.
21299
21300 type-id:
21301 type-specifier-seq abstract-declarator [opt]
21302
21303 Returns the TYPE specified. */
21304
21305 static tree
21306 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21307 bool is_trailing_return, location_t * type_location)
21308 {
21309 cp_decl_specifier_seq type_specifier_seq;
21310 cp_declarator *abstract_declarator;
21311
21312 /* Parse the type-specifier-seq. */
21313 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21314 is_trailing_return,
21315 &type_specifier_seq);
21316 if (type_location)
21317 *type_location = type_specifier_seq.locations[ds_type_spec];
21318
21319 if (is_template_arg && type_specifier_seq.type
21320 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21321 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21322 /* A bare template name as a template argument is a template template
21323 argument, not a placeholder, so fail parsing it as a type argument. */
21324 {
21325 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21326 cp_parser_simulate_error (parser);
21327 return error_mark_node;
21328 }
21329 if (type_specifier_seq.type == error_mark_node)
21330 return error_mark_node;
21331
21332 /* There might or might not be an abstract declarator. */
21333 cp_parser_parse_tentatively (parser);
21334 /* Look for the declarator. */
21335 abstract_declarator
21336 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21337 /*parenthesized_p=*/NULL,
21338 /*member_p=*/false,
21339 /*friend_p=*/false);
21340 /* Check to see if there really was a declarator. */
21341 if (!cp_parser_parse_definitely (parser))
21342 abstract_declarator = NULL;
21343
21344 if (type_specifier_seq.type
21345 /* The concepts TS allows 'auto' as a type-id. */
21346 && (!flag_concepts || parser->in_type_id_in_expr_p)
21347 /* None of the valid uses of 'auto' in C++14 involve the type-id
21348 nonterminal, but it is valid in a trailing-return-type. */
21349 && !(cxx_dialect >= cxx14 && is_trailing_return))
21350 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21351 {
21352 /* A type-id with type 'auto' is only ok if the abstract declarator
21353 is a function declarator with a late-specified return type.
21354
21355 A type-id with 'auto' is also valid in a trailing-return-type
21356 in a compound-requirement. */
21357 if (abstract_declarator
21358 && abstract_declarator->kind == cdk_function
21359 && abstract_declarator->u.function.late_return_type)
21360 /* OK */;
21361 else if (parser->in_result_type_constraint_p)
21362 /* OK */;
21363 else
21364 {
21365 location_t loc = type_specifier_seq.locations[ds_type_spec];
21366 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21367 {
21368 error_at (loc, "missing template arguments after %qT",
21369 auto_node);
21370 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21371 tmpl);
21372 }
21373 else
21374 error_at (loc, "invalid use of %qT", auto_node);
21375 return error_mark_node;
21376 }
21377 }
21378
21379 return groktypename (&type_specifier_seq, abstract_declarator,
21380 is_template_arg);
21381 }
21382
21383 static tree
21384 cp_parser_type_id (cp_parser *parser, location_t * type_location)
21385 {
21386 return cp_parser_type_id_1 (parser, false, false, type_location);
21387 }
21388
21389 static tree
21390 cp_parser_template_type_arg (cp_parser *parser)
21391 {
21392 tree r;
21393 const char *saved_message = parser->type_definition_forbidden_message;
21394 parser->type_definition_forbidden_message
21395 = G_("types may not be defined in template arguments");
21396 r = cp_parser_type_id_1 (parser, true, false, NULL);
21397 parser->type_definition_forbidden_message = saved_message;
21398 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21399 {
21400 error ("invalid use of %<auto%> in template argument");
21401 r = error_mark_node;
21402 }
21403 return r;
21404 }
21405
21406 static tree
21407 cp_parser_trailing_type_id (cp_parser *parser)
21408 {
21409 return cp_parser_type_id_1 (parser, false, true, NULL);
21410 }
21411
21412 /* Parse a type-specifier-seq.
21413
21414 type-specifier-seq:
21415 type-specifier type-specifier-seq [opt]
21416
21417 GNU extension:
21418
21419 type-specifier-seq:
21420 attributes type-specifier-seq [opt]
21421
21422 If IS_DECLARATION is true, we are at the start of a "condition" or
21423 exception-declaration, so we might be followed by a declarator-id.
21424
21425 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21426 i.e. we've just seen "->".
21427
21428 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21429
21430 static void
21431 cp_parser_type_specifier_seq (cp_parser* parser,
21432 bool is_declaration,
21433 bool is_trailing_return,
21434 cp_decl_specifier_seq *type_specifier_seq)
21435 {
21436 bool seen_type_specifier = false;
21437 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21438 cp_token *start_token = NULL;
21439
21440 /* Clear the TYPE_SPECIFIER_SEQ. */
21441 clear_decl_specs (type_specifier_seq);
21442
21443 /* In the context of a trailing return type, enum E { } is an
21444 elaborated-type-specifier followed by a function-body, not an
21445 enum-specifier. */
21446 if (is_trailing_return)
21447 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21448
21449 /* Parse the type-specifiers and attributes. */
21450 while (true)
21451 {
21452 tree type_specifier;
21453 bool is_cv_qualifier;
21454
21455 /* Check for attributes first. */
21456 if (cp_next_tokens_can_be_attribute_p (parser))
21457 {
21458 type_specifier_seq->attributes
21459 = attr_chainon (type_specifier_seq->attributes,
21460 cp_parser_attributes_opt (parser));
21461 continue;
21462 }
21463
21464 /* record the token of the beginning of the type specifier seq,
21465 for error reporting purposes*/
21466 if (!start_token)
21467 start_token = cp_lexer_peek_token (parser->lexer);
21468
21469 /* Look for the type-specifier. */
21470 type_specifier = cp_parser_type_specifier (parser,
21471 flags,
21472 type_specifier_seq,
21473 /*is_declaration=*/false,
21474 NULL,
21475 &is_cv_qualifier);
21476 if (!type_specifier)
21477 {
21478 /* If the first type-specifier could not be found, this is not a
21479 type-specifier-seq at all. */
21480 if (!seen_type_specifier)
21481 {
21482 /* Set in_declarator_p to avoid skipping to the semicolon. */
21483 int in_decl = parser->in_declarator_p;
21484 parser->in_declarator_p = true;
21485
21486 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21487 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21488 cp_parser_error (parser, "expected type-specifier");
21489
21490 parser->in_declarator_p = in_decl;
21491
21492 type_specifier_seq->type = error_mark_node;
21493 return;
21494 }
21495 /* If subsequent type-specifiers could not be found, the
21496 type-specifier-seq is complete. */
21497 break;
21498 }
21499
21500 seen_type_specifier = true;
21501 /* The standard says that a condition can be:
21502
21503 type-specifier-seq declarator = assignment-expression
21504
21505 However, given:
21506
21507 struct S {};
21508 if (int S = ...)
21509
21510 we should treat the "S" as a declarator, not as a
21511 type-specifier. The standard doesn't say that explicitly for
21512 type-specifier-seq, but it does say that for
21513 decl-specifier-seq in an ordinary declaration. Perhaps it
21514 would be clearer just to allow a decl-specifier-seq here, and
21515 then add a semantic restriction that if any decl-specifiers
21516 that are not type-specifiers appear, the program is invalid. */
21517 if (is_declaration && !is_cv_qualifier)
21518 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21519 }
21520 }
21521
21522 /* Return whether the function currently being declared has an associated
21523 template parameter list. */
21524
21525 static bool
21526 function_being_declared_is_template_p (cp_parser* parser)
21527 {
21528 if (!current_template_parms || processing_template_parmlist)
21529 return false;
21530
21531 if (parser->implicit_template_scope)
21532 return true;
21533
21534 if (at_class_scope_p ()
21535 && TYPE_BEING_DEFINED (current_class_type))
21536 return parser->num_template_parameter_lists != 0;
21537
21538 return ((int) parser->num_template_parameter_lists > template_class_depth
21539 (current_class_type));
21540 }
21541
21542 /* Parse a parameter-declaration-clause.
21543
21544 parameter-declaration-clause:
21545 parameter-declaration-list [opt] ... [opt]
21546 parameter-declaration-list , ...
21547
21548 Returns a representation for the parameter declarations. A return
21549 value of NULL indicates a parameter-declaration-clause consisting
21550 only of an ellipsis. */
21551
21552 static tree
21553 cp_parser_parameter_declaration_clause (cp_parser* parser)
21554 {
21555 tree parameters;
21556 cp_token *token;
21557 bool ellipsis_p;
21558
21559 temp_override<bool> cleanup
21560 (parser->auto_is_implicit_function_template_parm_p);
21561
21562 if (!processing_specialization
21563 && !processing_template_parmlist
21564 && !processing_explicit_instantiation
21565 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21566 actual function or a random abstract declarator. */
21567 && parser->default_arg_ok_p)
21568 if (!current_function_decl
21569 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21570 parser->auto_is_implicit_function_template_parm_p = true;
21571
21572 /* Peek at the next token. */
21573 token = cp_lexer_peek_token (parser->lexer);
21574 /* Check for trivial parameter-declaration-clauses. */
21575 if (token->type == CPP_ELLIPSIS)
21576 {
21577 /* Consume the `...' token. */
21578 cp_lexer_consume_token (parser->lexer);
21579 return NULL_TREE;
21580 }
21581 else if (token->type == CPP_CLOSE_PAREN)
21582 /* There are no parameters. */
21583 return void_list_node;
21584 /* Check for `(void)', too, which is a special case. */
21585 else if (token->keyword == RID_VOID
21586 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21587 == CPP_CLOSE_PAREN))
21588 {
21589 /* Consume the `void' token. */
21590 cp_lexer_consume_token (parser->lexer);
21591 /* There are no parameters. */
21592 return void_list_node;
21593 }
21594
21595 /* Parse the parameter-declaration-list. */
21596 parameters = cp_parser_parameter_declaration_list (parser);
21597 /* If a parse error occurred while parsing the
21598 parameter-declaration-list, then the entire
21599 parameter-declaration-clause is erroneous. */
21600 if (parameters == error_mark_node)
21601 return NULL_TREE;
21602
21603 /* Peek at the next token. */
21604 token = cp_lexer_peek_token (parser->lexer);
21605 /* If it's a `,', the clause should terminate with an ellipsis. */
21606 if (token->type == CPP_COMMA)
21607 {
21608 /* Consume the `,'. */
21609 cp_lexer_consume_token (parser->lexer);
21610 /* Expect an ellipsis. */
21611 ellipsis_p
21612 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21613 }
21614 /* It might also be `...' if the optional trailing `,' was
21615 omitted. */
21616 else if (token->type == CPP_ELLIPSIS)
21617 {
21618 /* Consume the `...' token. */
21619 cp_lexer_consume_token (parser->lexer);
21620 /* And remember that we saw it. */
21621 ellipsis_p = true;
21622 }
21623 else
21624 ellipsis_p = false;
21625
21626 /* Finish the parameter list. */
21627 if (!ellipsis_p)
21628 parameters = chainon (parameters, void_list_node);
21629
21630 return parameters;
21631 }
21632
21633 /* Parse a parameter-declaration-list.
21634
21635 parameter-declaration-list:
21636 parameter-declaration
21637 parameter-declaration-list , parameter-declaration
21638
21639 Returns a representation of the parameter-declaration-list, as for
21640 cp_parser_parameter_declaration_clause. However, the
21641 `void_list_node' is never appended to the list. */
21642
21643 static tree
21644 cp_parser_parameter_declaration_list (cp_parser* parser)
21645 {
21646 tree parameters = NULL_TREE;
21647 tree *tail = &parameters;
21648 bool saved_in_unbraced_linkage_specification_p;
21649 int index = 0;
21650
21651 /* The special considerations that apply to a function within an
21652 unbraced linkage specifications do not apply to the parameters
21653 to the function. */
21654 saved_in_unbraced_linkage_specification_p
21655 = parser->in_unbraced_linkage_specification_p;
21656 parser->in_unbraced_linkage_specification_p = false;
21657
21658 /* Look for more parameters. */
21659 while (true)
21660 {
21661 cp_parameter_declarator *parameter;
21662 tree decl = error_mark_node;
21663 bool parenthesized_p = false;
21664
21665 /* Parse the parameter. */
21666 parameter
21667 = cp_parser_parameter_declaration (parser,
21668 /*template_parm_p=*/false,
21669 &parenthesized_p);
21670
21671 /* We don't know yet if the enclosing context is deprecated, so wait
21672 and warn in grokparms if appropriate. */
21673 deprecated_state = DEPRECATED_SUPPRESS;
21674
21675 if (parameter)
21676 {
21677 decl = grokdeclarator (parameter->declarator,
21678 &parameter->decl_specifiers,
21679 PARM,
21680 parameter->default_argument != NULL_TREE,
21681 &parameter->decl_specifiers.attributes);
21682 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21683 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21684 }
21685
21686 deprecated_state = DEPRECATED_NORMAL;
21687
21688 /* If a parse error occurred parsing the parameter declaration,
21689 then the entire parameter-declaration-list is erroneous. */
21690 if (decl == error_mark_node)
21691 {
21692 parameters = error_mark_node;
21693 break;
21694 }
21695
21696 if (parameter->decl_specifiers.attributes)
21697 cplus_decl_attributes (&decl,
21698 parameter->decl_specifiers.attributes,
21699 0);
21700 if (DECL_NAME (decl))
21701 decl = pushdecl (decl);
21702
21703 if (decl != error_mark_node)
21704 {
21705 retrofit_lang_decl (decl);
21706 DECL_PARM_INDEX (decl) = ++index;
21707 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21708 }
21709
21710 /* Add the new parameter to the list. */
21711 *tail = build_tree_list (parameter->default_argument, decl);
21712 tail = &TREE_CHAIN (*tail);
21713
21714 /* Peek at the next token. */
21715 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21716 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21717 /* These are for Objective-C++ */
21718 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21719 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21720 /* The parameter-declaration-list is complete. */
21721 break;
21722 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21723 {
21724 cp_token *token;
21725
21726 /* Peek at the next token. */
21727 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21728 /* If it's an ellipsis, then the list is complete. */
21729 if (token->type == CPP_ELLIPSIS)
21730 break;
21731 /* Otherwise, there must be more parameters. Consume the
21732 `,'. */
21733 cp_lexer_consume_token (parser->lexer);
21734 /* When parsing something like:
21735
21736 int i(float f, double d)
21737
21738 we can tell after seeing the declaration for "f" that we
21739 are not looking at an initialization of a variable "i",
21740 but rather at the declaration of a function "i".
21741
21742 Due to the fact that the parsing of template arguments
21743 (as specified to a template-id) requires backtracking we
21744 cannot use this technique when inside a template argument
21745 list. */
21746 if (!parser->in_template_argument_list_p
21747 && !parser->in_type_id_in_expr_p
21748 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21749 /* However, a parameter-declaration of the form
21750 "float(f)" (which is a valid declaration of a
21751 parameter "f") can also be interpreted as an
21752 expression (the conversion of "f" to "float"). */
21753 && !parenthesized_p)
21754 cp_parser_commit_to_tentative_parse (parser);
21755 }
21756 else
21757 {
21758 cp_parser_error (parser, "expected %<,%> or %<...%>");
21759 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21760 cp_parser_skip_to_closing_parenthesis (parser,
21761 /*recovering=*/true,
21762 /*or_comma=*/false,
21763 /*consume_paren=*/false);
21764 break;
21765 }
21766 }
21767
21768 parser->in_unbraced_linkage_specification_p
21769 = saved_in_unbraced_linkage_specification_p;
21770
21771 /* Reset implicit_template_scope if we are about to leave the function
21772 parameter list that introduced it. Note that for out-of-line member
21773 definitions, there will be one or more class scopes before we get to
21774 the template parameter scope. */
21775
21776 if (cp_binding_level *its = parser->implicit_template_scope)
21777 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21778 {
21779 while (maybe_its->kind == sk_class)
21780 maybe_its = maybe_its->level_chain;
21781 if (maybe_its == its)
21782 {
21783 parser->implicit_template_parms = 0;
21784 parser->implicit_template_scope = 0;
21785 }
21786 }
21787
21788 return parameters;
21789 }
21790
21791 /* Parse a parameter declaration.
21792
21793 parameter-declaration:
21794 decl-specifier-seq ... [opt] declarator
21795 decl-specifier-seq declarator = assignment-expression
21796 decl-specifier-seq ... [opt] abstract-declarator [opt]
21797 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21798
21799 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21800 declares a template parameter. (In that case, a non-nested `>'
21801 token encountered during the parsing of the assignment-expression
21802 is not interpreted as a greater-than operator.)
21803
21804 Returns a representation of the parameter, or NULL if an error
21805 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21806 true iff the declarator is of the form "(p)". */
21807
21808 static cp_parameter_declarator *
21809 cp_parser_parameter_declaration (cp_parser *parser,
21810 bool template_parm_p,
21811 bool *parenthesized_p)
21812 {
21813 int declares_class_or_enum;
21814 cp_decl_specifier_seq decl_specifiers;
21815 cp_declarator *declarator;
21816 tree default_argument;
21817 cp_token *token = NULL, *declarator_token_start = NULL;
21818 const char *saved_message;
21819 bool template_parameter_pack_p = false;
21820
21821 /* In a template parameter, `>' is not an operator.
21822
21823 [temp.param]
21824
21825 When parsing a default template-argument for a non-type
21826 template-parameter, the first non-nested `>' is taken as the end
21827 of the template parameter-list rather than a greater-than
21828 operator. */
21829
21830 /* Type definitions may not appear in parameter types. */
21831 saved_message = parser->type_definition_forbidden_message;
21832 parser->type_definition_forbidden_message
21833 = G_("types may not be defined in parameter types");
21834
21835 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21836 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21837 (current_template_parms)) : 0);
21838
21839 /* Parse the declaration-specifiers. */
21840 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21841 cp_parser_decl_specifier_seq (parser,
21842 CP_PARSER_FLAGS_NONE,
21843 &decl_specifiers,
21844 &declares_class_or_enum);
21845
21846 /* Complain about missing 'typename' or other invalid type names. */
21847 if (!decl_specifiers.any_type_specifiers_p
21848 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21849 decl_specifiers.type = error_mark_node;
21850
21851 /* If an error occurred, there's no reason to attempt to parse the
21852 rest of the declaration. */
21853 if (cp_parser_error_occurred (parser))
21854 {
21855 parser->type_definition_forbidden_message = saved_message;
21856 return NULL;
21857 }
21858
21859 /* Peek at the next token. */
21860 token = cp_lexer_peek_token (parser->lexer);
21861
21862 /* If the next token is a `)', `,', `=', `>', or `...', then there
21863 is no declarator. However, when variadic templates are enabled,
21864 there may be a declarator following `...'. */
21865 if (token->type == CPP_CLOSE_PAREN
21866 || token->type == CPP_COMMA
21867 || token->type == CPP_EQ
21868 || token->type == CPP_GREATER)
21869 {
21870 declarator = NULL;
21871 if (parenthesized_p)
21872 *parenthesized_p = false;
21873 }
21874 /* Otherwise, there should be a declarator. */
21875 else
21876 {
21877 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21878 parser->default_arg_ok_p = false;
21879
21880 /* After seeing a decl-specifier-seq, if the next token is not a
21881 "(", there is no possibility that the code is a valid
21882 expression. Therefore, if parsing tentatively, we commit at
21883 this point. */
21884 if (!parser->in_template_argument_list_p
21885 /* In an expression context, having seen:
21886
21887 (int((char ...
21888
21889 we cannot be sure whether we are looking at a
21890 function-type (taking a "char" as a parameter) or a cast
21891 of some object of type "char" to "int". */
21892 && !parser->in_type_id_in_expr_p
21893 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21894 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21895 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21896 cp_parser_commit_to_tentative_parse (parser);
21897 /* Parse the declarator. */
21898 declarator_token_start = token;
21899 declarator = cp_parser_declarator (parser,
21900 CP_PARSER_DECLARATOR_EITHER,
21901 /*ctor_dtor_or_conv_p=*/NULL,
21902 parenthesized_p,
21903 /*member_p=*/false,
21904 /*friend_p=*/false);
21905 parser->default_arg_ok_p = saved_default_arg_ok_p;
21906 /* After the declarator, allow more attributes. */
21907 decl_specifiers.attributes
21908 = attr_chainon (decl_specifiers.attributes,
21909 cp_parser_attributes_opt (parser));
21910
21911 /* If the declarator is a template parameter pack, remember that and
21912 clear the flag in the declarator itself so we don't get errors
21913 from grokdeclarator. */
21914 if (template_parm_p && declarator && declarator->parameter_pack_p)
21915 {
21916 declarator->parameter_pack_p = false;
21917 template_parameter_pack_p = true;
21918 }
21919 }
21920
21921 /* If the next token is an ellipsis, and we have not seen a declarator
21922 name, and if either the type of the declarator contains parameter
21923 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21924 for, eg, abbreviated integral type names), then we actually have a
21925 parameter pack expansion expression. Otherwise, leave the ellipsis
21926 for a C-style variadic function. */
21927 token = cp_lexer_peek_token (parser->lexer);
21928
21929 /* If a function parameter pack was specified and an implicit template
21930 parameter was introduced during cp_parser_parameter_declaration,
21931 change any implicit parameters introduced into packs. */
21932 if (parser->implicit_template_parms
21933 && ((token->type == CPP_ELLIPSIS
21934 && declarator_can_be_parameter_pack (declarator))
21935 || (declarator && declarator->parameter_pack_p)))
21936 {
21937 int latest_template_parm_idx = TREE_VEC_LENGTH
21938 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21939
21940 if (latest_template_parm_idx != template_parm_idx)
21941 decl_specifiers.type = convert_generic_types_to_packs
21942 (decl_specifiers.type,
21943 template_parm_idx, latest_template_parm_idx);
21944 }
21945
21946 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21947 {
21948 tree type = decl_specifiers.type;
21949
21950 if (type && DECL_P (type))
21951 type = TREE_TYPE (type);
21952
21953 if (((type
21954 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21955 && (template_parm_p || uses_parameter_packs (type)))
21956 || (!type && template_parm_p))
21957 && declarator_can_be_parameter_pack (declarator))
21958 {
21959 /* Consume the `...'. */
21960 cp_lexer_consume_token (parser->lexer);
21961 maybe_warn_variadic_templates ();
21962
21963 /* Build a pack expansion type */
21964 if (template_parm_p)
21965 template_parameter_pack_p = true;
21966 else if (declarator)
21967 declarator->parameter_pack_p = true;
21968 else
21969 decl_specifiers.type = make_pack_expansion (type);
21970 }
21971 }
21972
21973 /* The restriction on defining new types applies only to the type
21974 of the parameter, not to the default argument. */
21975 parser->type_definition_forbidden_message = saved_message;
21976
21977 /* If the next token is `=', then process a default argument. */
21978 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21979 {
21980 tree type = decl_specifiers.type;
21981 token = cp_lexer_peek_token (parser->lexer);
21982 /* If we are defining a class, then the tokens that make up the
21983 default argument must be saved and processed later. */
21984 if (!template_parm_p && at_class_scope_p ()
21985 && TYPE_BEING_DEFINED (current_class_type)
21986 && !LAMBDA_TYPE_P (current_class_type))
21987 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21988
21989 // A constrained-type-specifier may declare a type template-parameter.
21990 else if (declares_constrained_type_template_parameter (type))
21991 default_argument
21992 = cp_parser_default_type_template_argument (parser);
21993
21994 // A constrained-type-specifier may declare a template-template-parameter.
21995 else if (declares_constrained_template_template_parameter (type))
21996 default_argument
21997 = cp_parser_default_template_template_argument (parser);
21998
21999 /* Outside of a class definition, we can just parse the
22000 assignment-expression. */
22001 else
22002 default_argument
22003 = cp_parser_default_argument (parser, template_parm_p);
22004
22005 if (!parser->default_arg_ok_p)
22006 {
22007 permerror (token->location,
22008 "default arguments are only "
22009 "permitted for function parameters");
22010 }
22011 else if ((declarator && declarator->parameter_pack_p)
22012 || template_parameter_pack_p
22013 || (decl_specifiers.type
22014 && PACK_EXPANSION_P (decl_specifiers.type)))
22015 {
22016 /* Find the name of the parameter pack. */
22017 cp_declarator *id_declarator = declarator;
22018 while (id_declarator && id_declarator->kind != cdk_id)
22019 id_declarator = id_declarator->declarator;
22020
22021 if (id_declarator && id_declarator->kind == cdk_id)
22022 error_at (declarator_token_start->location,
22023 template_parm_p
22024 ? G_("template parameter pack %qD "
22025 "cannot have a default argument")
22026 : G_("parameter pack %qD cannot have "
22027 "a default argument"),
22028 id_declarator->u.id.unqualified_name);
22029 else
22030 error_at (declarator_token_start->location,
22031 template_parm_p
22032 ? G_("template parameter pack cannot have "
22033 "a default argument")
22034 : G_("parameter pack cannot have a "
22035 "default argument"));
22036
22037 default_argument = NULL_TREE;
22038 }
22039 }
22040 else
22041 default_argument = NULL_TREE;
22042
22043 /* Generate a location for the parameter, ranging from the start of the
22044 initial token to the end of the final token (using input_location for
22045 the latter, set up by cp_lexer_set_source_position_from_token when
22046 consuming tokens).
22047
22048 If we have a identifier, then use it for the caret location, e.g.
22049
22050 extern int callee (int one, int (*two)(int, int), float three);
22051 ~~~~~~^~~~~~~~~~~~~~
22052
22053 otherwise, reuse the start location for the caret location e.g.:
22054
22055 extern int callee (int one, int (*)(int, int), float three);
22056 ^~~~~~~~~~~~~~~~~
22057
22058 */
22059 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22060 ? declarator->id_loc
22061 : decl_spec_token_start->location);
22062 location_t param_loc = make_location (caret_loc,
22063 decl_spec_token_start->location,
22064 input_location);
22065
22066 return make_parameter_declarator (&decl_specifiers,
22067 declarator,
22068 default_argument,
22069 param_loc,
22070 template_parameter_pack_p);
22071 }
22072
22073 /* Parse a default argument and return it.
22074
22075 TEMPLATE_PARM_P is true if this is a default argument for a
22076 non-type template parameter. */
22077 static tree
22078 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22079 {
22080 tree default_argument = NULL_TREE;
22081 bool saved_greater_than_is_operator_p;
22082 bool saved_local_variables_forbidden_p;
22083 bool non_constant_p, is_direct_init;
22084
22085 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22086 set correctly. */
22087 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22088 parser->greater_than_is_operator_p = !template_parm_p;
22089 /* Local variable names (and the `this' keyword) may not
22090 appear in a default argument. */
22091 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22092 parser->local_variables_forbidden_p = true;
22093 /* Parse the assignment-expression. */
22094 if (template_parm_p)
22095 push_deferring_access_checks (dk_no_deferred);
22096 tree saved_class_ptr = NULL_TREE;
22097 tree saved_class_ref = NULL_TREE;
22098 /* The "this" pointer is not valid in a default argument. */
22099 if (cfun)
22100 {
22101 saved_class_ptr = current_class_ptr;
22102 cp_function_chain->x_current_class_ptr = NULL_TREE;
22103 saved_class_ref = current_class_ref;
22104 cp_function_chain->x_current_class_ref = NULL_TREE;
22105 }
22106 default_argument
22107 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22108 /* Restore the "this" pointer. */
22109 if (cfun)
22110 {
22111 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22112 cp_function_chain->x_current_class_ref = saved_class_ref;
22113 }
22114 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22115 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22116 if (template_parm_p)
22117 pop_deferring_access_checks ();
22118 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22119 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22120
22121 return default_argument;
22122 }
22123
22124 /* Parse a function-body.
22125
22126 function-body:
22127 compound_statement */
22128
22129 static void
22130 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22131 {
22132 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22133 ? BCS_TRY_BLOCK : BCS_NORMAL),
22134 true);
22135 }
22136
22137 /* Parse a ctor-initializer-opt followed by a function-body. Return
22138 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22139 is true we are parsing a function-try-block. */
22140
22141 static void
22142 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22143 bool in_function_try_block)
22144 {
22145 tree body, list;
22146 const bool check_body_p =
22147 DECL_CONSTRUCTOR_P (current_function_decl)
22148 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22149 tree last = NULL;
22150
22151 /* Begin the function body. */
22152 body = begin_function_body ();
22153 /* Parse the optional ctor-initializer. */
22154 cp_parser_ctor_initializer_opt (parser);
22155
22156 /* If we're parsing a constexpr constructor definition, we need
22157 to check that the constructor body is indeed empty. However,
22158 before we get to cp_parser_function_body lot of junk has been
22159 generated, so we can't just check that we have an empty block.
22160 Rather we take a snapshot of the outermost block, and check whether
22161 cp_parser_function_body changed its state. */
22162 if (check_body_p)
22163 {
22164 list = cur_stmt_list;
22165 if (STATEMENT_LIST_TAIL (list))
22166 last = STATEMENT_LIST_TAIL (list)->stmt;
22167 }
22168 /* Parse the function-body. */
22169 cp_parser_function_body (parser, in_function_try_block);
22170 if (check_body_p)
22171 check_constexpr_ctor_body (last, list, /*complain=*/true);
22172 /* Finish the function body. */
22173 finish_function_body (body);
22174 }
22175
22176 /* Parse an initializer.
22177
22178 initializer:
22179 = initializer-clause
22180 ( expression-list )
22181
22182 Returns an expression representing the initializer. If no
22183 initializer is present, NULL_TREE is returned.
22184
22185 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22186 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22187 set to TRUE if there is no initializer present. If there is an
22188 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22189 is set to true; otherwise it is set to false. */
22190
22191 static tree
22192 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22193 bool* non_constant_p, bool subexpression_p)
22194 {
22195 cp_token *token;
22196 tree init;
22197
22198 /* Peek at the next token. */
22199 token = cp_lexer_peek_token (parser->lexer);
22200
22201 /* Let our caller know whether or not this initializer was
22202 parenthesized. */
22203 *is_direct_init = (token->type != CPP_EQ);
22204 /* Assume that the initializer is constant. */
22205 *non_constant_p = false;
22206
22207 if (token->type == CPP_EQ)
22208 {
22209 /* Consume the `='. */
22210 cp_lexer_consume_token (parser->lexer);
22211 /* Parse the initializer-clause. */
22212 init = cp_parser_initializer_clause (parser, non_constant_p);
22213 }
22214 else if (token->type == CPP_OPEN_PAREN)
22215 {
22216 vec<tree, va_gc> *vec;
22217 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22218 /*cast_p=*/false,
22219 /*allow_expansion_p=*/true,
22220 non_constant_p);
22221 if (vec == NULL)
22222 return error_mark_node;
22223 init = build_tree_list_vec (vec);
22224 release_tree_vector (vec);
22225 }
22226 else if (token->type == CPP_OPEN_BRACE)
22227 {
22228 cp_lexer_set_source_position (parser->lexer);
22229 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22230 init = cp_parser_braced_list (parser, non_constant_p);
22231 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22232 }
22233 else
22234 {
22235 /* Anything else is an error. */
22236 cp_parser_error (parser, "expected initializer");
22237 init = error_mark_node;
22238 }
22239
22240 if (!subexpression_p && check_for_bare_parameter_packs (init))
22241 init = error_mark_node;
22242
22243 return init;
22244 }
22245
22246 /* Parse an initializer-clause.
22247
22248 initializer-clause:
22249 assignment-expression
22250 braced-init-list
22251
22252 Returns an expression representing the initializer.
22253
22254 If the `assignment-expression' production is used the value
22255 returned is simply a representation for the expression.
22256
22257 Otherwise, calls cp_parser_braced_list. */
22258
22259 static cp_expr
22260 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22261 {
22262 cp_expr initializer;
22263
22264 /* Assume the expression is constant. */
22265 *non_constant_p = false;
22266
22267 /* If it is not a `{', then we are looking at an
22268 assignment-expression. */
22269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22270 {
22271 initializer
22272 = cp_parser_constant_expression (parser,
22273 /*allow_non_constant_p=*/true,
22274 non_constant_p);
22275 }
22276 else
22277 initializer = cp_parser_braced_list (parser, non_constant_p);
22278
22279 return initializer;
22280 }
22281
22282 /* Parse a brace-enclosed initializer list.
22283
22284 braced-init-list:
22285 { initializer-list , [opt] }
22286 { designated-initializer-list , [opt] }
22287 { }
22288
22289 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22290 the elements of the initializer-list (or NULL, if the last
22291 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22292 NULL_TREE. There is no way to detect whether or not the optional
22293 trailing `,' was provided. NON_CONSTANT_P is as for
22294 cp_parser_initializer. */
22295
22296 static cp_expr
22297 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22298 {
22299 tree initializer;
22300 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22301
22302 /* Consume the `{' token. */
22303 matching_braces braces;
22304 braces.require_open (parser);
22305 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22306 initializer = make_node (CONSTRUCTOR);
22307 /* If it's not a `}', then there is a non-trivial initializer. */
22308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22309 {
22310 /* Parse the initializer list. */
22311 CONSTRUCTOR_ELTS (initializer)
22312 = cp_parser_initializer_list (parser, non_constant_p);
22313 /* A trailing `,' token is allowed. */
22314 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22315 cp_lexer_consume_token (parser->lexer);
22316 }
22317 else
22318 *non_constant_p = false;
22319 /* Now, there should be a trailing `}'. */
22320 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22321 braces.require_close (parser);
22322 TREE_TYPE (initializer) = init_list_type_node;
22323
22324 cp_expr result (initializer);
22325 /* Build a location of the form:
22326 { ... }
22327 ^~~~~~~
22328 with caret==start at the open brace, finish at the close brace. */
22329 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22330 result.set_location (combined_loc);
22331 return result;
22332 }
22333
22334 /* Consume tokens up to, and including, the next non-nested closing `]'.
22335 Returns true iff we found a closing `]'. */
22336
22337 static bool
22338 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22339 {
22340 unsigned square_depth = 0;
22341
22342 while (true)
22343 {
22344 cp_token * token = cp_lexer_peek_token (parser->lexer);
22345
22346 switch (token->type)
22347 {
22348 case CPP_EOF:
22349 case CPP_PRAGMA_EOL:
22350 /* If we've run out of tokens, then there is no closing `]'. */
22351 return false;
22352
22353 case CPP_OPEN_SQUARE:
22354 ++square_depth;
22355 break;
22356
22357 case CPP_CLOSE_SQUARE:
22358 if (!square_depth--)
22359 {
22360 cp_lexer_consume_token (parser->lexer);
22361 return true;
22362 }
22363 break;
22364
22365 default:
22366 break;
22367 }
22368
22369 /* Consume the token. */
22370 cp_lexer_consume_token (parser->lexer);
22371 }
22372 }
22373
22374 /* Return true if we are looking at an array-designator, false otherwise. */
22375
22376 static bool
22377 cp_parser_array_designator_p (cp_parser *parser)
22378 {
22379 /* Consume the `['. */
22380 cp_lexer_consume_token (parser->lexer);
22381
22382 cp_lexer_save_tokens (parser->lexer);
22383
22384 /* Skip tokens until the next token is a closing square bracket.
22385 If we find the closing `]', and the next token is a `=', then
22386 we are looking at an array designator. */
22387 bool array_designator_p
22388 = (cp_parser_skip_to_closing_square_bracket (parser)
22389 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22390
22391 /* Roll back the tokens we skipped. */
22392 cp_lexer_rollback_tokens (parser->lexer);
22393
22394 return array_designator_p;
22395 }
22396
22397 /* Parse an initializer-list.
22398
22399 initializer-list:
22400 initializer-clause ... [opt]
22401 initializer-list , initializer-clause ... [opt]
22402
22403 C++2A Extension:
22404
22405 designated-initializer-list:
22406 designated-initializer-clause
22407 designated-initializer-list , designated-initializer-clause
22408
22409 designated-initializer-clause:
22410 designator brace-or-equal-initializer
22411
22412 designator:
22413 . identifier
22414
22415 GNU Extension:
22416
22417 initializer-list:
22418 designation initializer-clause ...[opt]
22419 initializer-list , designation initializer-clause ...[opt]
22420
22421 designation:
22422 . identifier =
22423 identifier :
22424 [ constant-expression ] =
22425
22426 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22427 for the initializer. If the INDEX of the elt is non-NULL, it is the
22428 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22429 as for cp_parser_initializer. */
22430
22431 static vec<constructor_elt, va_gc> *
22432 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22433 {
22434 vec<constructor_elt, va_gc> *v = NULL;
22435 bool first_p = true;
22436 tree first_designator = NULL_TREE;
22437
22438 /* Assume all of the expressions are constant. */
22439 *non_constant_p = false;
22440
22441 /* Parse the rest of the list. */
22442 while (true)
22443 {
22444 cp_token *token;
22445 tree designator;
22446 tree initializer;
22447 bool clause_non_constant_p;
22448 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22449
22450 /* Handle the C++2A syntax, '. id ='. */
22451 if ((cxx_dialect >= cxx2a
22452 || cp_parser_allow_gnu_extensions_p (parser))
22453 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22454 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22455 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22456 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22457 == CPP_OPEN_BRACE)))
22458 {
22459 if (cxx_dialect < cxx2a)
22460 pedwarn (loc, OPT_Wpedantic,
22461 "C++ designated initializers only available with "
22462 "-std=c++2a or -std=gnu++2a");
22463 /* Consume the `.'. */
22464 cp_lexer_consume_token (parser->lexer);
22465 /* Consume the identifier. */
22466 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22467 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22468 /* Consume the `='. */
22469 cp_lexer_consume_token (parser->lexer);
22470 }
22471 /* Also, if the next token is an identifier and the following one is a
22472 colon, we are looking at the GNU designated-initializer
22473 syntax. */
22474 else if (cp_parser_allow_gnu_extensions_p (parser)
22475 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22476 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22477 == CPP_COLON))
22478 {
22479 /* Warn the user that they are using an extension. */
22480 pedwarn (loc, OPT_Wpedantic,
22481 "ISO C++ does not allow GNU designated initializers");
22482 /* Consume the identifier. */
22483 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22484 /* Consume the `:'. */
22485 cp_lexer_consume_token (parser->lexer);
22486 }
22487 /* Also handle C99 array designators, '[ const ] ='. */
22488 else if (cp_parser_allow_gnu_extensions_p (parser)
22489 && !c_dialect_objc ()
22490 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22491 {
22492 /* In C++11, [ could start a lambda-introducer. */
22493 bool non_const = false;
22494
22495 cp_parser_parse_tentatively (parser);
22496
22497 if (!cp_parser_array_designator_p (parser))
22498 {
22499 cp_parser_simulate_error (parser);
22500 designator = NULL_TREE;
22501 }
22502 else
22503 {
22504 designator = cp_parser_constant_expression (parser, true,
22505 &non_const);
22506 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22507 cp_parser_require (parser, CPP_EQ, RT_EQ);
22508 }
22509
22510 if (!cp_parser_parse_definitely (parser))
22511 designator = NULL_TREE;
22512 else if (non_const
22513 && (!require_potential_rvalue_constant_expression
22514 (designator)))
22515 designator = NULL_TREE;
22516 if (designator)
22517 /* Warn the user that they are using an extension. */
22518 pedwarn (loc, OPT_Wpedantic,
22519 "ISO C++ does not allow C99 designated initializers");
22520 }
22521 else
22522 designator = NULL_TREE;
22523
22524 if (first_p)
22525 {
22526 first_designator = designator;
22527 first_p = false;
22528 }
22529 else if (cxx_dialect >= cxx2a
22530 && first_designator != error_mark_node
22531 && (!first_designator != !designator))
22532 {
22533 error_at (loc, "either all initializer clauses should be designated "
22534 "or none of them should be");
22535 first_designator = error_mark_node;
22536 }
22537 else if (cxx_dialect < cxx2a && !first_designator)
22538 first_designator = designator;
22539
22540 /* Parse the initializer. */
22541 initializer = cp_parser_initializer_clause (parser,
22542 &clause_non_constant_p);
22543 /* If any clause is non-constant, so is the entire initializer. */
22544 if (clause_non_constant_p)
22545 *non_constant_p = true;
22546
22547 /* If we have an ellipsis, this is an initializer pack
22548 expansion. */
22549 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22550 {
22551 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22552
22553 /* Consume the `...'. */
22554 cp_lexer_consume_token (parser->lexer);
22555
22556 if (designator && cxx_dialect >= cxx2a)
22557 error_at (loc,
22558 "%<...%> not allowed in designated initializer list");
22559
22560 /* Turn the initializer into an initializer expansion. */
22561 initializer = make_pack_expansion (initializer);
22562 }
22563
22564 /* Add it to the vector. */
22565 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22566
22567 /* If the next token is not a comma, we have reached the end of
22568 the list. */
22569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22570 break;
22571
22572 /* Peek at the next token. */
22573 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22574 /* If the next token is a `}', then we're still done. An
22575 initializer-clause can have a trailing `,' after the
22576 initializer-list and before the closing `}'. */
22577 if (token->type == CPP_CLOSE_BRACE)
22578 break;
22579
22580 /* Consume the `,' token. */
22581 cp_lexer_consume_token (parser->lexer);
22582 }
22583
22584 /* The same identifier shall not appear in multiple designators
22585 of a designated-initializer-list. */
22586 if (first_designator)
22587 {
22588 unsigned int i;
22589 tree designator, val;
22590 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22591 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22592 {
22593 if (IDENTIFIER_MARKED (designator))
22594 {
22595 error_at (cp_expr_loc_or_loc (val, input_location),
22596 "%<.%s%> designator used multiple times in "
22597 "the same initializer list",
22598 IDENTIFIER_POINTER (designator));
22599 (*v)[i].index = error_mark_node;
22600 }
22601 else
22602 IDENTIFIER_MARKED (designator) = 1;
22603 }
22604 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22605 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22606 IDENTIFIER_MARKED (designator) = 0;
22607 }
22608
22609 return v;
22610 }
22611
22612 /* Classes [gram.class] */
22613
22614 /* Parse a class-name.
22615
22616 class-name:
22617 identifier
22618 template-id
22619
22620 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22621 to indicate that names looked up in dependent types should be
22622 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22623 keyword has been used to indicate that the name that appears next
22624 is a template. TAG_TYPE indicates the explicit tag given before
22625 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22626 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22627 is the class being defined in a class-head. If ENUM_OK is TRUE,
22628 enum-names are also accepted.
22629
22630 Returns the TYPE_DECL representing the class. */
22631
22632 static tree
22633 cp_parser_class_name (cp_parser *parser,
22634 bool typename_keyword_p,
22635 bool template_keyword_p,
22636 enum tag_types tag_type,
22637 bool check_dependency_p,
22638 bool class_head_p,
22639 bool is_declaration,
22640 bool enum_ok)
22641 {
22642 tree decl;
22643 tree scope;
22644 bool typename_p;
22645 cp_token *token;
22646 tree identifier = NULL_TREE;
22647
22648 /* All class-names start with an identifier. */
22649 token = cp_lexer_peek_token (parser->lexer);
22650 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22651 {
22652 cp_parser_error (parser, "expected class-name");
22653 return error_mark_node;
22654 }
22655
22656 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22657 to a template-id, so we save it here. */
22658 scope = parser->scope;
22659 if (scope == error_mark_node)
22660 return error_mark_node;
22661
22662 /* Any name names a type if we're following the `typename' keyword
22663 in a qualified name where the enclosing scope is type-dependent. */
22664 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22665 && dependent_type_p (scope));
22666 /* Handle the common case (an identifier, but not a template-id)
22667 efficiently. */
22668 if (token->type == CPP_NAME
22669 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22670 {
22671 cp_token *identifier_token;
22672 bool ambiguous_p;
22673
22674 /* Look for the identifier. */
22675 identifier_token = cp_lexer_peek_token (parser->lexer);
22676 ambiguous_p = identifier_token->error_reported;
22677 identifier = cp_parser_identifier (parser);
22678 /* If the next token isn't an identifier, we are certainly not
22679 looking at a class-name. */
22680 if (identifier == error_mark_node)
22681 decl = error_mark_node;
22682 /* If we know this is a type-name, there's no need to look it
22683 up. */
22684 else if (typename_p)
22685 decl = identifier;
22686 else
22687 {
22688 tree ambiguous_decls;
22689 /* If we already know that this lookup is ambiguous, then
22690 we've already issued an error message; there's no reason
22691 to check again. */
22692 if (ambiguous_p)
22693 {
22694 cp_parser_simulate_error (parser);
22695 return error_mark_node;
22696 }
22697 /* If the next token is a `::', then the name must be a type
22698 name.
22699
22700 [basic.lookup.qual]
22701
22702 During the lookup for a name preceding the :: scope
22703 resolution operator, object, function, and enumerator
22704 names are ignored. */
22705 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22706 tag_type = scope_type;
22707 /* Look up the name. */
22708 decl = cp_parser_lookup_name (parser, identifier,
22709 tag_type,
22710 /*is_template=*/false,
22711 /*is_namespace=*/false,
22712 check_dependency_p,
22713 &ambiguous_decls,
22714 identifier_token->location);
22715 if (ambiguous_decls)
22716 {
22717 if (cp_parser_parsing_tentatively (parser))
22718 cp_parser_simulate_error (parser);
22719 return error_mark_node;
22720 }
22721 }
22722 }
22723 else
22724 {
22725 /* Try a template-id. */
22726 decl = cp_parser_template_id (parser, template_keyword_p,
22727 check_dependency_p,
22728 tag_type,
22729 is_declaration);
22730 if (decl == error_mark_node)
22731 return error_mark_node;
22732 }
22733
22734 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22735
22736 /* If this is a typename, create a TYPENAME_TYPE. */
22737 if (typename_p && decl != error_mark_node)
22738 {
22739 decl = make_typename_type (scope, decl, typename_type,
22740 /*complain=*/tf_error);
22741 if (decl != error_mark_node)
22742 decl = TYPE_NAME (decl);
22743 }
22744
22745 decl = strip_using_decl (decl);
22746
22747 /* Check to see that it is really the name of a class. */
22748 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22749 && identifier_p (TREE_OPERAND (decl, 0))
22750 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22751 /* Situations like this:
22752
22753 template <typename T> struct A {
22754 typename T::template X<int>::I i;
22755 };
22756
22757 are problematic. Is `T::template X<int>' a class-name? The
22758 standard does not seem to be definitive, but there is no other
22759 valid interpretation of the following `::'. Therefore, those
22760 names are considered class-names. */
22761 {
22762 decl = make_typename_type (scope, decl, tag_type, tf_error);
22763 if (decl != error_mark_node)
22764 decl = TYPE_NAME (decl);
22765 }
22766 else if (TREE_CODE (decl) != TYPE_DECL
22767 || TREE_TYPE (decl) == error_mark_node
22768 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22769 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22770 /* In Objective-C 2.0, a classname followed by '.' starts a
22771 dot-syntax expression, and it's not a type-name. */
22772 || (c_dialect_objc ()
22773 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22774 && objc_is_class_name (decl)))
22775 decl = error_mark_node;
22776
22777 if (decl == error_mark_node)
22778 cp_parser_error (parser, "expected class-name");
22779 else if (identifier && !parser->scope)
22780 maybe_note_name_used_in_class (identifier, decl);
22781
22782 return decl;
22783 }
22784
22785 /* Parse a class-specifier.
22786
22787 class-specifier:
22788 class-head { member-specification [opt] }
22789
22790 Returns the TREE_TYPE representing the class. */
22791
22792 static tree
22793 cp_parser_class_specifier_1 (cp_parser* parser)
22794 {
22795 tree type;
22796 tree attributes = NULL_TREE;
22797 bool nested_name_specifier_p;
22798 unsigned saved_num_template_parameter_lists;
22799 bool saved_in_function_body;
22800 unsigned char in_statement;
22801 bool in_switch_statement_p;
22802 bool saved_in_unbraced_linkage_specification_p;
22803 tree old_scope = NULL_TREE;
22804 tree scope = NULL_TREE;
22805 cp_token *closing_brace;
22806
22807 push_deferring_access_checks (dk_no_deferred);
22808
22809 /* Parse the class-head. */
22810 type = cp_parser_class_head (parser,
22811 &nested_name_specifier_p);
22812 /* If the class-head was a semantic disaster, skip the entire body
22813 of the class. */
22814 if (!type)
22815 {
22816 cp_parser_skip_to_end_of_block_or_statement (parser);
22817 pop_deferring_access_checks ();
22818 return error_mark_node;
22819 }
22820
22821 /* Look for the `{'. */
22822 matching_braces braces;
22823 if (!braces.require_open (parser))
22824 {
22825 pop_deferring_access_checks ();
22826 return error_mark_node;
22827 }
22828
22829 cp_ensure_no_omp_declare_simd (parser);
22830 cp_ensure_no_oacc_routine (parser);
22831
22832 /* Issue an error message if type-definitions are forbidden here. */
22833 cp_parser_check_type_definition (parser);
22834 /* Remember that we are defining one more class. */
22835 ++parser->num_classes_being_defined;
22836 /* Inside the class, surrounding template-parameter-lists do not
22837 apply. */
22838 saved_num_template_parameter_lists
22839 = parser->num_template_parameter_lists;
22840 parser->num_template_parameter_lists = 0;
22841 /* We are not in a function body. */
22842 saved_in_function_body = parser->in_function_body;
22843 parser->in_function_body = false;
22844 /* Or in a loop. */
22845 in_statement = parser->in_statement;
22846 parser->in_statement = 0;
22847 /* Or in a switch. */
22848 in_switch_statement_p = parser->in_switch_statement_p;
22849 parser->in_switch_statement_p = false;
22850 /* We are not immediately inside an extern "lang" block. */
22851 saved_in_unbraced_linkage_specification_p
22852 = parser->in_unbraced_linkage_specification_p;
22853 parser->in_unbraced_linkage_specification_p = false;
22854
22855 // Associate constraints with the type.
22856 if (flag_concepts)
22857 type = associate_classtype_constraints (type);
22858
22859 /* Start the class. */
22860 if (nested_name_specifier_p)
22861 {
22862 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22863 old_scope = push_inner_scope (scope);
22864 }
22865 type = begin_class_definition (type);
22866
22867 if (type == error_mark_node)
22868 /* If the type is erroneous, skip the entire body of the class. */
22869 cp_parser_skip_to_closing_brace (parser);
22870 else
22871 /* Parse the member-specification. */
22872 cp_parser_member_specification_opt (parser);
22873
22874 /* Look for the trailing `}'. */
22875 closing_brace = braces.require_close (parser);
22876 /* Look for trailing attributes to apply to this class. */
22877 if (cp_parser_allow_gnu_extensions_p (parser))
22878 attributes = cp_parser_gnu_attributes_opt (parser);
22879 if (type != error_mark_node)
22880 type = finish_struct (type, attributes);
22881 if (nested_name_specifier_p)
22882 pop_inner_scope (old_scope, scope);
22883
22884 /* We've finished a type definition. Check for the common syntax
22885 error of forgetting a semicolon after the definition. We need to
22886 be careful, as we can't just check for not-a-semicolon and be done
22887 with it; the user might have typed:
22888
22889 class X { } c = ...;
22890 class X { } *p = ...;
22891
22892 and so forth. Instead, enumerate all the possible tokens that
22893 might follow this production; if we don't see one of them, then
22894 complain and silently insert the semicolon. */
22895 {
22896 cp_token *token = cp_lexer_peek_token (parser->lexer);
22897 bool want_semicolon = true;
22898
22899 if (cp_next_tokens_can_be_std_attribute_p (parser))
22900 /* Don't try to parse c++11 attributes here. As per the
22901 grammar, that should be a task for
22902 cp_parser_decl_specifier_seq. */
22903 want_semicolon = false;
22904
22905 switch (token->type)
22906 {
22907 case CPP_NAME:
22908 case CPP_SEMICOLON:
22909 case CPP_MULT:
22910 case CPP_AND:
22911 case CPP_OPEN_PAREN:
22912 case CPP_CLOSE_PAREN:
22913 case CPP_COMMA:
22914 want_semicolon = false;
22915 break;
22916
22917 /* While it's legal for type qualifiers and storage class
22918 specifiers to follow type definitions in the grammar, only
22919 compiler testsuites contain code like that. Assume that if
22920 we see such code, then what we're really seeing is a case
22921 like:
22922
22923 class X { }
22924 const <type> var = ...;
22925
22926 or
22927
22928 class Y { }
22929 static <type> func (...) ...
22930
22931 i.e. the qualifier or specifier applies to the next
22932 declaration. To do so, however, we need to look ahead one
22933 more token to see if *that* token is a type specifier.
22934
22935 This code could be improved to handle:
22936
22937 class Z { }
22938 static const <type> var = ...; */
22939 case CPP_KEYWORD:
22940 if (keyword_is_decl_specifier (token->keyword))
22941 {
22942 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22943
22944 /* Handling user-defined types here would be nice, but very
22945 tricky. */
22946 want_semicolon
22947 = (lookahead->type == CPP_KEYWORD
22948 && keyword_begins_type_specifier (lookahead->keyword));
22949 }
22950 break;
22951 default:
22952 break;
22953 }
22954
22955 /* If we don't have a type, then something is very wrong and we
22956 shouldn't try to do anything clever. Likewise for not seeing the
22957 closing brace. */
22958 if (closing_brace && TYPE_P (type) && want_semicolon)
22959 {
22960 /* Locate the closing brace. */
22961 cp_token_position prev
22962 = cp_lexer_previous_token_position (parser->lexer);
22963 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22964 location_t loc = prev_token->location;
22965
22966 /* We want to suggest insertion of a ';' immediately *after* the
22967 closing brace, so, if we can, offset the location by 1 column. */
22968 location_t next_loc = loc;
22969 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22970 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22971
22972 rich_location richloc (line_table, next_loc);
22973
22974 /* If we successfully offset the location, suggest the fix-it. */
22975 if (next_loc != loc)
22976 richloc.add_fixit_insert_before (next_loc, ";");
22977
22978 if (CLASSTYPE_DECLARED_CLASS (type))
22979 error_at (&richloc,
22980 "expected %<;%> after class definition");
22981 else if (TREE_CODE (type) == RECORD_TYPE)
22982 error_at (&richloc,
22983 "expected %<;%> after struct definition");
22984 else if (TREE_CODE (type) == UNION_TYPE)
22985 error_at (&richloc,
22986 "expected %<;%> after union definition");
22987 else
22988 gcc_unreachable ();
22989
22990 /* Unget one token and smash it to look as though we encountered
22991 a semicolon in the input stream. */
22992 cp_lexer_set_token_position (parser->lexer, prev);
22993 token = cp_lexer_peek_token (parser->lexer);
22994 token->type = CPP_SEMICOLON;
22995 token->keyword = RID_MAX;
22996 }
22997 }
22998
22999 /* If this class is not itself within the scope of another class,
23000 then we need to parse the bodies of all of the queued function
23001 definitions. Note that the queued functions defined in a class
23002 are not always processed immediately following the
23003 class-specifier for that class. Consider:
23004
23005 struct A {
23006 struct B { void f() { sizeof (A); } };
23007 };
23008
23009 If `f' were processed before the processing of `A' were
23010 completed, there would be no way to compute the size of `A'.
23011 Note that the nesting we are interested in here is lexical --
23012 not the semantic nesting given by TYPE_CONTEXT. In particular,
23013 for:
23014
23015 struct A { struct B; };
23016 struct A::B { void f() { } };
23017
23018 there is no need to delay the parsing of `A::B::f'. */
23019 if (--parser->num_classes_being_defined == 0)
23020 {
23021 tree decl;
23022 tree class_type = NULL_TREE;
23023 tree pushed_scope = NULL_TREE;
23024 unsigned ix;
23025 cp_default_arg_entry *e;
23026 tree save_ccp, save_ccr;
23027
23028 if (any_erroneous_template_args_p (type))
23029 {
23030 /* Skip default arguments, NSDMIs, etc, in order to improve
23031 error recovery (c++/71169, c++/71832). */
23032 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23033 vec_safe_truncate (unparsed_nsdmis, 0);
23034 vec_safe_truncate (unparsed_classes, 0);
23035 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23036 }
23037
23038 /* In a first pass, parse default arguments to the functions.
23039 Then, in a second pass, parse the bodies of the functions.
23040 This two-phased approach handles cases like:
23041
23042 struct S {
23043 void f() { g(); }
23044 void g(int i = 3);
23045 };
23046
23047 */
23048 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23049 {
23050 decl = e->decl;
23051 /* If there are default arguments that have not yet been processed,
23052 take care of them now. */
23053 if (class_type != e->class_type)
23054 {
23055 if (pushed_scope)
23056 pop_scope (pushed_scope);
23057 class_type = e->class_type;
23058 pushed_scope = push_scope (class_type);
23059 }
23060 /* Make sure that any template parameters are in scope. */
23061 maybe_begin_member_template_processing (decl);
23062 /* Parse the default argument expressions. */
23063 cp_parser_late_parsing_default_args (parser, decl);
23064 /* Remove any template parameters from the symbol table. */
23065 maybe_end_member_template_processing ();
23066 }
23067 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23068 /* Now parse any NSDMIs. */
23069 save_ccp = current_class_ptr;
23070 save_ccr = current_class_ref;
23071 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23072 {
23073 if (class_type != DECL_CONTEXT (decl))
23074 {
23075 if (pushed_scope)
23076 pop_scope (pushed_scope);
23077 class_type = DECL_CONTEXT (decl);
23078 pushed_scope = push_scope (class_type);
23079 }
23080 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23081 cp_parser_late_parsing_nsdmi (parser, decl);
23082 }
23083 vec_safe_truncate (unparsed_nsdmis, 0);
23084 current_class_ptr = save_ccp;
23085 current_class_ref = save_ccr;
23086 if (pushed_scope)
23087 pop_scope (pushed_scope);
23088
23089 /* Now do some post-NSDMI bookkeeping. */
23090 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23091 after_nsdmi_defaulted_late_checks (class_type);
23092 vec_safe_truncate (unparsed_classes, 0);
23093 after_nsdmi_defaulted_late_checks (type);
23094
23095 /* Now parse the body of the functions. */
23096 if (flag_openmp)
23097 {
23098 /* OpenMP UDRs need to be parsed before all other functions. */
23099 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23100 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23101 cp_parser_late_parsing_for_member (parser, decl);
23102 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23103 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23104 cp_parser_late_parsing_for_member (parser, decl);
23105 }
23106 else
23107 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23108 cp_parser_late_parsing_for_member (parser, decl);
23109 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23110 }
23111 else
23112 vec_safe_push (unparsed_classes, type);
23113
23114 /* Put back any saved access checks. */
23115 pop_deferring_access_checks ();
23116
23117 /* Restore saved state. */
23118 parser->in_switch_statement_p = in_switch_statement_p;
23119 parser->in_statement = in_statement;
23120 parser->in_function_body = saved_in_function_body;
23121 parser->num_template_parameter_lists
23122 = saved_num_template_parameter_lists;
23123 parser->in_unbraced_linkage_specification_p
23124 = saved_in_unbraced_linkage_specification_p;
23125
23126 return type;
23127 }
23128
23129 static tree
23130 cp_parser_class_specifier (cp_parser* parser)
23131 {
23132 tree ret;
23133 timevar_push (TV_PARSE_STRUCT);
23134 ret = cp_parser_class_specifier_1 (parser);
23135 timevar_pop (TV_PARSE_STRUCT);
23136 return ret;
23137 }
23138
23139 /* Parse a class-head.
23140
23141 class-head:
23142 class-key identifier [opt] base-clause [opt]
23143 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23144 class-key nested-name-specifier [opt] template-id
23145 base-clause [opt]
23146
23147 class-virt-specifier:
23148 final
23149
23150 GNU Extensions:
23151 class-key attributes identifier [opt] base-clause [opt]
23152 class-key attributes nested-name-specifier identifier base-clause [opt]
23153 class-key attributes nested-name-specifier [opt] template-id
23154 base-clause [opt]
23155
23156 Upon return BASES is initialized to the list of base classes (or
23157 NULL, if there are none) in the same form returned by
23158 cp_parser_base_clause.
23159
23160 Returns the TYPE of the indicated class. Sets
23161 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23162 involving a nested-name-specifier was used, and FALSE otherwise.
23163
23164 Returns error_mark_node if this is not a class-head.
23165
23166 Returns NULL_TREE if the class-head is syntactically valid, but
23167 semantically invalid in a way that means we should skip the entire
23168 body of the class. */
23169
23170 static tree
23171 cp_parser_class_head (cp_parser* parser,
23172 bool* nested_name_specifier_p)
23173 {
23174 tree nested_name_specifier;
23175 enum tag_types class_key;
23176 tree id = NULL_TREE;
23177 tree type = NULL_TREE;
23178 tree attributes;
23179 tree bases;
23180 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23181 bool template_id_p = false;
23182 bool qualified_p = false;
23183 bool invalid_nested_name_p = false;
23184 bool invalid_explicit_specialization_p = false;
23185 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23186 tree pushed_scope = NULL_TREE;
23187 unsigned num_templates;
23188 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23189 /* Assume no nested-name-specifier will be present. */
23190 *nested_name_specifier_p = false;
23191 /* Assume no template parameter lists will be used in defining the
23192 type. */
23193 num_templates = 0;
23194 parser->colon_corrects_to_scope_p = false;
23195
23196 /* Look for the class-key. */
23197 class_key = cp_parser_class_key (parser);
23198 if (class_key == none_type)
23199 return error_mark_node;
23200
23201 location_t class_head_start_location = input_location;
23202
23203 /* Parse the attributes. */
23204 attributes = cp_parser_attributes_opt (parser);
23205
23206 /* If the next token is `::', that is invalid -- but sometimes
23207 people do try to write:
23208
23209 struct ::S {};
23210
23211 Handle this gracefully by accepting the extra qualifier, and then
23212 issuing an error about it later if this really is a
23213 class-head. If it turns out just to be an elaborated type
23214 specifier, remain silent. */
23215 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23216 qualified_p = true;
23217
23218 push_deferring_access_checks (dk_no_check);
23219
23220 /* Determine the name of the class. Begin by looking for an
23221 optional nested-name-specifier. */
23222 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23223 nested_name_specifier
23224 = cp_parser_nested_name_specifier_opt (parser,
23225 /*typename_keyword_p=*/false,
23226 /*check_dependency_p=*/false,
23227 /*type_p=*/true,
23228 /*is_declaration=*/false);
23229 /* If there was a nested-name-specifier, then there *must* be an
23230 identifier. */
23231
23232 cp_token *bad_template_keyword = NULL;
23233
23234 if (nested_name_specifier)
23235 {
23236 type_start_token = cp_lexer_peek_token (parser->lexer);
23237 /* Although the grammar says `identifier', it really means
23238 `class-name' or `template-name'. You are only allowed to
23239 define a class that has already been declared with this
23240 syntax.
23241
23242 The proposed resolution for Core Issue 180 says that wherever
23243 you see `class T::X' you should treat `X' as a type-name.
23244
23245 It is OK to define an inaccessible class; for example:
23246
23247 class A { class B; };
23248 class A::B {};
23249
23250 We do not know if we will see a class-name, or a
23251 template-name. We look for a class-name first, in case the
23252 class-name is a template-id; if we looked for the
23253 template-name first we would stop after the template-name. */
23254 cp_parser_parse_tentatively (parser);
23255 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23256 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23257 type = cp_parser_class_name (parser,
23258 /*typename_keyword_p=*/false,
23259 /*template_keyword_p=*/false,
23260 class_type,
23261 /*check_dependency_p=*/false,
23262 /*class_head_p=*/true,
23263 /*is_declaration=*/false);
23264 /* If that didn't work, ignore the nested-name-specifier. */
23265 if (!cp_parser_parse_definitely (parser))
23266 {
23267 invalid_nested_name_p = true;
23268 type_start_token = cp_lexer_peek_token (parser->lexer);
23269 id = cp_parser_identifier (parser);
23270 if (id == error_mark_node)
23271 id = NULL_TREE;
23272 }
23273 /* If we could not find a corresponding TYPE, treat this
23274 declaration like an unqualified declaration. */
23275 if (type == error_mark_node)
23276 nested_name_specifier = NULL_TREE;
23277 /* Otherwise, count the number of templates used in TYPE and its
23278 containing scopes. */
23279 else
23280 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23281 }
23282 /* Otherwise, the identifier is optional. */
23283 else
23284 {
23285 /* We don't know whether what comes next is a template-id,
23286 an identifier, or nothing at all. */
23287 cp_parser_parse_tentatively (parser);
23288 /* Check for a template-id. */
23289 type_start_token = cp_lexer_peek_token (parser->lexer);
23290 id = cp_parser_template_id (parser,
23291 /*template_keyword_p=*/false,
23292 /*check_dependency_p=*/true,
23293 class_key,
23294 /*is_declaration=*/true);
23295 /* If that didn't work, it could still be an identifier. */
23296 if (!cp_parser_parse_definitely (parser))
23297 {
23298 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23299 {
23300 type_start_token = cp_lexer_peek_token (parser->lexer);
23301 id = cp_parser_identifier (parser);
23302 }
23303 else
23304 id = NULL_TREE;
23305 }
23306 else
23307 {
23308 template_id_p = true;
23309 ++num_templates;
23310 }
23311 }
23312
23313 pop_deferring_access_checks ();
23314
23315 if (id)
23316 {
23317 cp_parser_check_for_invalid_template_id (parser, id,
23318 class_key,
23319 type_start_token->location);
23320 }
23321 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23322
23323 /* If it's not a `:' or a `{' then we can't really be looking at a
23324 class-head, since a class-head only appears as part of a
23325 class-specifier. We have to detect this situation before calling
23326 xref_tag, since that has irreversible side-effects. */
23327 if (!cp_parser_next_token_starts_class_definition_p (parser))
23328 {
23329 cp_parser_error (parser, "expected %<{%> or %<:%>");
23330 type = error_mark_node;
23331 goto out;
23332 }
23333
23334 /* At this point, we're going ahead with the class-specifier, even
23335 if some other problem occurs. */
23336 cp_parser_commit_to_tentative_parse (parser);
23337 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23338 {
23339 cp_parser_error (parser,
23340 "cannot specify %<override%> for a class");
23341 type = error_mark_node;
23342 goto out;
23343 }
23344 /* Issue the error about the overly-qualified name now. */
23345 if (qualified_p)
23346 {
23347 cp_parser_error (parser,
23348 "global qualification of class name is invalid");
23349 type = error_mark_node;
23350 goto out;
23351 }
23352 else if (invalid_nested_name_p)
23353 {
23354 cp_parser_error (parser,
23355 "qualified name does not name a class");
23356 type = error_mark_node;
23357 goto out;
23358 }
23359 else if (nested_name_specifier)
23360 {
23361 tree scope;
23362
23363 if (bad_template_keyword)
23364 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23365 keyword template shall not appear at the top level. */
23366 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23367 "keyword %<template%> not allowed in class-head-name");
23368
23369 /* Reject typedef-names in class heads. */
23370 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23371 {
23372 error_at (type_start_token->location,
23373 "invalid class name in declaration of %qD",
23374 type);
23375 type = NULL_TREE;
23376 goto done;
23377 }
23378
23379 /* Figure out in what scope the declaration is being placed. */
23380 scope = current_scope ();
23381 /* If that scope does not contain the scope in which the
23382 class was originally declared, the program is invalid. */
23383 if (scope && !is_ancestor (scope, nested_name_specifier))
23384 {
23385 if (at_namespace_scope_p ())
23386 error_at (type_start_token->location,
23387 "declaration of %qD in namespace %qD which does not "
23388 "enclose %qD",
23389 type, scope, nested_name_specifier);
23390 else
23391 error_at (type_start_token->location,
23392 "declaration of %qD in %qD which does not enclose %qD",
23393 type, scope, nested_name_specifier);
23394 type = NULL_TREE;
23395 goto done;
23396 }
23397 /* [dcl.meaning]
23398
23399 A declarator-id shall not be qualified except for the
23400 definition of a ... nested class outside of its class
23401 ... [or] the definition or explicit instantiation of a
23402 class member of a namespace outside of its namespace. */
23403 if (scope == nested_name_specifier)
23404 {
23405 permerror (nested_name_specifier_token_start->location,
23406 "extra qualification not allowed");
23407 nested_name_specifier = NULL_TREE;
23408 num_templates = 0;
23409 }
23410 }
23411 /* An explicit-specialization must be preceded by "template <>". If
23412 it is not, try to recover gracefully. */
23413 if (at_namespace_scope_p ()
23414 && parser->num_template_parameter_lists == 0
23415 && !processing_template_parmlist
23416 && template_id_p)
23417 {
23418 /* Build a location of this form:
23419 struct typename <ARGS>
23420 ^~~~~~~~~~~~~~~~~~~~~~
23421 with caret==start at the start token, and
23422 finishing at the end of the type. */
23423 location_t reported_loc
23424 = make_location (class_head_start_location,
23425 class_head_start_location,
23426 get_finish (type_start_token->location));
23427 rich_location richloc (line_table, reported_loc);
23428 richloc.add_fixit_insert_before (class_head_start_location,
23429 "template <> ");
23430 error_at (&richloc,
23431 "an explicit specialization must be preceded by"
23432 " %<template <>%>");
23433 invalid_explicit_specialization_p = true;
23434 /* Take the same action that would have been taken by
23435 cp_parser_explicit_specialization. */
23436 ++parser->num_template_parameter_lists;
23437 begin_specialization ();
23438 }
23439 /* There must be no "return" statements between this point and the
23440 end of this function; set "type "to the correct return value and
23441 use "goto done;" to return. */
23442 /* Make sure that the right number of template parameters were
23443 present. */
23444 if (!cp_parser_check_template_parameters (parser, num_templates,
23445 template_id_p,
23446 type_start_token->location,
23447 /*declarator=*/NULL))
23448 {
23449 /* If something went wrong, there is no point in even trying to
23450 process the class-definition. */
23451 type = NULL_TREE;
23452 goto done;
23453 }
23454
23455 /* Look up the type. */
23456 if (template_id_p)
23457 {
23458 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23459 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23460 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23461 {
23462 error_at (type_start_token->location,
23463 "function template %qD redeclared as a class template", id);
23464 type = error_mark_node;
23465 }
23466 else
23467 {
23468 type = TREE_TYPE (id);
23469 type = maybe_process_partial_specialization (type);
23470
23471 /* Check the scope while we still know whether or not we had a
23472 nested-name-specifier. */
23473 if (type != error_mark_node)
23474 check_unqualified_spec_or_inst (type, type_start_token->location);
23475 }
23476 if (nested_name_specifier)
23477 pushed_scope = push_scope (nested_name_specifier);
23478 }
23479 else if (nested_name_specifier)
23480 {
23481 tree class_type;
23482
23483 /* Given:
23484
23485 template <typename T> struct S { struct T };
23486 template <typename T> struct S<T>::T { };
23487
23488 we will get a TYPENAME_TYPE when processing the definition of
23489 `S::T'. We need to resolve it to the actual type before we
23490 try to define it. */
23491 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23492 {
23493 class_type = resolve_typename_type (TREE_TYPE (type),
23494 /*only_current_p=*/false);
23495 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23496 type = TYPE_NAME (class_type);
23497 else
23498 {
23499 cp_parser_error (parser, "could not resolve typename type");
23500 type = error_mark_node;
23501 }
23502 }
23503
23504 if (maybe_process_partial_specialization (TREE_TYPE (type))
23505 == error_mark_node)
23506 {
23507 type = NULL_TREE;
23508 goto done;
23509 }
23510
23511 class_type = current_class_type;
23512 /* Enter the scope indicated by the nested-name-specifier. */
23513 pushed_scope = push_scope (nested_name_specifier);
23514 /* Get the canonical version of this type. */
23515 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23516 /* Call push_template_decl if it seems like we should be defining a
23517 template either from the template headers or the type we're
23518 defining, so that we diagnose both extra and missing headers. */
23519 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23520 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23521 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23522 {
23523 type = push_template_decl (type);
23524 if (type == error_mark_node)
23525 {
23526 type = NULL_TREE;
23527 goto done;
23528 }
23529 }
23530
23531 type = TREE_TYPE (type);
23532 *nested_name_specifier_p = true;
23533 }
23534 else /* The name is not a nested name. */
23535 {
23536 /* If the class was unnamed, create a dummy name. */
23537 if (!id)
23538 id = make_anon_name ();
23539 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23540 ? ts_within_enclosing_non_class
23541 : ts_current);
23542 type = xref_tag (class_key, id, tag_scope,
23543 parser->num_template_parameter_lists);
23544 }
23545
23546 /* Indicate whether this class was declared as a `class' or as a
23547 `struct'. */
23548 if (TREE_CODE (type) == RECORD_TYPE)
23549 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23550 cp_parser_check_class_key (class_key, type);
23551
23552 /* If this type was already complete, and we see another definition,
23553 that's an error. */
23554 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23555 {
23556 error_at (type_start_token->location, "redefinition of %q#T",
23557 type);
23558 inform (location_of (type), "previous definition of %q#T",
23559 type);
23560 type = NULL_TREE;
23561 goto done;
23562 }
23563 else if (type == error_mark_node)
23564 type = NULL_TREE;
23565
23566 if (type)
23567 {
23568 /* Apply attributes now, before any use of the class as a template
23569 argument in its base list. */
23570 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23571 fixup_attribute_variants (type);
23572 }
23573
23574 /* We will have entered the scope containing the class; the names of
23575 base classes should be looked up in that context. For example:
23576
23577 struct A { struct B {}; struct C; };
23578 struct A::C : B {};
23579
23580 is valid. */
23581
23582 /* Get the list of base-classes, if there is one. */
23583 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23584 {
23585 /* PR59482: enter the class scope so that base-specifiers are looked
23586 up correctly. */
23587 if (type)
23588 pushclass (type);
23589 bases = cp_parser_base_clause (parser);
23590 /* PR59482: get out of the previously pushed class scope so that the
23591 subsequent pops pop the right thing. */
23592 if (type)
23593 popclass ();
23594 }
23595 else
23596 bases = NULL_TREE;
23597
23598 /* If we're really defining a class, process the base classes.
23599 If they're invalid, fail. */
23600 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23601 xref_basetypes (type, bases);
23602
23603 done:
23604 /* Leave the scope given by the nested-name-specifier. We will
23605 enter the class scope itself while processing the members. */
23606 if (pushed_scope)
23607 pop_scope (pushed_scope);
23608
23609 if (invalid_explicit_specialization_p)
23610 {
23611 end_specialization ();
23612 --parser->num_template_parameter_lists;
23613 }
23614
23615 if (type)
23616 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23617 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23618 CLASSTYPE_FINAL (type) = 1;
23619 out:
23620 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23621 return type;
23622 }
23623
23624 /* Parse a class-key.
23625
23626 class-key:
23627 class
23628 struct
23629 union
23630
23631 Returns the kind of class-key specified, or none_type to indicate
23632 error. */
23633
23634 static enum tag_types
23635 cp_parser_class_key (cp_parser* parser)
23636 {
23637 cp_token *token;
23638 enum tag_types tag_type;
23639
23640 /* Look for the class-key. */
23641 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23642 if (!token)
23643 return none_type;
23644
23645 /* Check to see if the TOKEN is a class-key. */
23646 tag_type = cp_parser_token_is_class_key (token);
23647 if (!tag_type)
23648 cp_parser_error (parser, "expected class-key");
23649 return tag_type;
23650 }
23651
23652 /* Parse a type-parameter-key.
23653
23654 type-parameter-key:
23655 class
23656 typename
23657 */
23658
23659 static void
23660 cp_parser_type_parameter_key (cp_parser* parser)
23661 {
23662 /* Look for the type-parameter-key. */
23663 enum tag_types tag_type = none_type;
23664 cp_token *token = cp_lexer_peek_token (parser->lexer);
23665 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23666 {
23667 cp_lexer_consume_token (parser->lexer);
23668 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23669 /* typename is not allowed in a template template parameter
23670 by the standard until C++17. */
23671 pedwarn (token->location, OPT_Wpedantic,
23672 "ISO C++ forbids typename key in template template parameter;"
23673 " use -std=c++17 or -std=gnu++17");
23674 }
23675 else
23676 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23677
23678 return;
23679 }
23680
23681 /* Parse an (optional) member-specification.
23682
23683 member-specification:
23684 member-declaration member-specification [opt]
23685 access-specifier : member-specification [opt] */
23686
23687 static void
23688 cp_parser_member_specification_opt (cp_parser* parser)
23689 {
23690 while (true)
23691 {
23692 cp_token *token;
23693 enum rid keyword;
23694
23695 /* Peek at the next token. */
23696 token = cp_lexer_peek_token (parser->lexer);
23697 /* If it's a `}', or EOF then we've seen all the members. */
23698 if (token->type == CPP_CLOSE_BRACE
23699 || token->type == CPP_EOF
23700 || token->type == CPP_PRAGMA_EOL)
23701 break;
23702
23703 /* See if this token is a keyword. */
23704 keyword = token->keyword;
23705 switch (keyword)
23706 {
23707 case RID_PUBLIC:
23708 case RID_PROTECTED:
23709 case RID_PRIVATE:
23710 /* Consume the access-specifier. */
23711 cp_lexer_consume_token (parser->lexer);
23712 /* Remember which access-specifier is active. */
23713 current_access_specifier = token->u.value;
23714 /* Look for the `:'. */
23715 cp_parser_require (parser, CPP_COLON, RT_COLON);
23716 break;
23717
23718 default:
23719 /* Accept #pragmas at class scope. */
23720 if (token->type == CPP_PRAGMA)
23721 {
23722 cp_parser_pragma (parser, pragma_member, NULL);
23723 break;
23724 }
23725
23726 /* Otherwise, the next construction must be a
23727 member-declaration. */
23728 cp_parser_member_declaration (parser);
23729 }
23730 }
23731 }
23732
23733 /* Parse a member-declaration.
23734
23735 member-declaration:
23736 decl-specifier-seq [opt] member-declarator-list [opt] ;
23737 function-definition ; [opt]
23738 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23739 using-declaration
23740 template-declaration
23741 alias-declaration
23742
23743 member-declarator-list:
23744 member-declarator
23745 member-declarator-list , member-declarator
23746
23747 member-declarator:
23748 declarator pure-specifier [opt]
23749 declarator constant-initializer [opt]
23750 identifier [opt] : constant-expression
23751
23752 GNU Extensions:
23753
23754 member-declaration:
23755 __extension__ member-declaration
23756
23757 member-declarator:
23758 declarator attributes [opt] pure-specifier [opt]
23759 declarator attributes [opt] constant-initializer [opt]
23760 identifier [opt] attributes [opt] : constant-expression
23761
23762 C++0x Extensions:
23763
23764 member-declaration:
23765 static_assert-declaration */
23766
23767 static void
23768 cp_parser_member_declaration (cp_parser* parser)
23769 {
23770 cp_decl_specifier_seq decl_specifiers;
23771 tree prefix_attributes;
23772 tree decl;
23773 int declares_class_or_enum;
23774 bool friend_p;
23775 cp_token *token = NULL;
23776 cp_token *decl_spec_token_start = NULL;
23777 cp_token *initializer_token_start = NULL;
23778 int saved_pedantic;
23779 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23780
23781 /* Check for the `__extension__' keyword. */
23782 if (cp_parser_extension_opt (parser, &saved_pedantic))
23783 {
23784 /* Recurse. */
23785 cp_parser_member_declaration (parser);
23786 /* Restore the old value of the PEDANTIC flag. */
23787 pedantic = saved_pedantic;
23788
23789 return;
23790 }
23791
23792 /* Check for a template-declaration. */
23793 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23794 {
23795 /* An explicit specialization here is an error condition, and we
23796 expect the specialization handler to detect and report this. */
23797 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23798 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23799 cp_parser_explicit_specialization (parser);
23800 else
23801 cp_parser_template_declaration (parser, /*member_p=*/true);
23802
23803 return;
23804 }
23805 /* Check for a template introduction. */
23806 else if (cp_parser_template_declaration_after_export (parser, true))
23807 return;
23808
23809 /* Check for a using-declaration. */
23810 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23811 {
23812 if (cxx_dialect < cxx11)
23813 {
23814 /* Parse the using-declaration. */
23815 cp_parser_using_declaration (parser,
23816 /*access_declaration_p=*/false);
23817 return;
23818 }
23819 else
23820 {
23821 tree decl;
23822 bool alias_decl_expected;
23823 cp_parser_parse_tentatively (parser);
23824 decl = cp_parser_alias_declaration (parser);
23825 /* Note that if we actually see the '=' token after the
23826 identifier, cp_parser_alias_declaration commits the
23827 tentative parse. In that case, we really expect an
23828 alias-declaration. Otherwise, we expect a using
23829 declaration. */
23830 alias_decl_expected =
23831 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23832 cp_parser_parse_definitely (parser);
23833
23834 if (alias_decl_expected)
23835 finish_member_declaration (decl);
23836 else
23837 cp_parser_using_declaration (parser,
23838 /*access_declaration_p=*/false);
23839 return;
23840 }
23841 }
23842
23843 /* Check for @defs. */
23844 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23845 {
23846 tree ivar, member;
23847 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23848 ivar = ivar_chains;
23849 while (ivar)
23850 {
23851 member = ivar;
23852 ivar = TREE_CHAIN (member);
23853 TREE_CHAIN (member) = NULL_TREE;
23854 finish_member_declaration (member);
23855 }
23856 return;
23857 }
23858
23859 /* If the next token is `static_assert' we have a static assertion. */
23860 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23861 {
23862 cp_parser_static_assert (parser, /*member_p=*/true);
23863 return;
23864 }
23865
23866 parser->colon_corrects_to_scope_p = false;
23867
23868 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23869 goto out;
23870
23871 /* Parse the decl-specifier-seq. */
23872 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23873 cp_parser_decl_specifier_seq (parser,
23874 CP_PARSER_FLAGS_OPTIONAL,
23875 &decl_specifiers,
23876 &declares_class_or_enum);
23877 /* Check for an invalid type-name. */
23878 if (!decl_specifiers.any_type_specifiers_p
23879 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23880 goto out;
23881 /* If there is no declarator, then the decl-specifier-seq should
23882 specify a type. */
23883 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23884 {
23885 /* If there was no decl-specifier-seq, and the next token is a
23886 `;', then we have something like:
23887
23888 struct S { ; };
23889
23890 [class.mem]
23891
23892 Each member-declaration shall declare at least one member
23893 name of the class. */
23894 if (!decl_specifiers.any_specifiers_p)
23895 {
23896 cp_token *token = cp_lexer_peek_token (parser->lexer);
23897 if (!in_system_header_at (token->location))
23898 {
23899 gcc_rich_location richloc (token->location);
23900 richloc.add_fixit_remove ();
23901 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
23902 }
23903 }
23904 else
23905 {
23906 tree type;
23907
23908 /* See if this declaration is a friend. */
23909 friend_p = cp_parser_friend_p (&decl_specifiers);
23910 /* If there were decl-specifiers, check to see if there was
23911 a class-declaration. */
23912 type = check_tag_decl (&decl_specifiers,
23913 /*explicit_type_instantiation_p=*/false);
23914 /* Nested classes have already been added to the class, but
23915 a `friend' needs to be explicitly registered. */
23916 if (friend_p)
23917 {
23918 /* If the `friend' keyword was present, the friend must
23919 be introduced with a class-key. */
23920 if (!declares_class_or_enum && cxx_dialect < cxx11)
23921 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23922 "in C++03 a class-key must be used "
23923 "when declaring a friend");
23924 /* In this case:
23925
23926 template <typename T> struct A {
23927 friend struct A<T>::B;
23928 };
23929
23930 A<T>::B will be represented by a TYPENAME_TYPE, and
23931 therefore not recognized by check_tag_decl. */
23932 if (!type)
23933 {
23934 type = decl_specifiers.type;
23935 if (type && TREE_CODE (type) == TYPE_DECL)
23936 type = TREE_TYPE (type);
23937 }
23938 if (!type || !TYPE_P (type))
23939 error_at (decl_spec_token_start->location,
23940 "friend declaration does not name a class or "
23941 "function");
23942 else
23943 make_friend_class (current_class_type, type,
23944 /*complain=*/true);
23945 }
23946 /* If there is no TYPE, an error message will already have
23947 been issued. */
23948 else if (!type || type == error_mark_node)
23949 ;
23950 /* An anonymous aggregate has to be handled specially; such
23951 a declaration really declares a data member (with a
23952 particular type), as opposed to a nested class. */
23953 else if (ANON_AGGR_TYPE_P (type))
23954 {
23955 /* C++11 9.5/6. */
23956 if (decl_specifiers.storage_class != sc_none)
23957 error_at (decl_spec_token_start->location,
23958 "a storage class on an anonymous aggregate "
23959 "in class scope is not allowed");
23960
23961 /* Remove constructors and such from TYPE, now that we
23962 know it is an anonymous aggregate. */
23963 fixup_anonymous_aggr (type);
23964 /* And make the corresponding data member. */
23965 decl = build_decl (decl_spec_token_start->location,
23966 FIELD_DECL, NULL_TREE, type);
23967 /* Add it to the class. */
23968 finish_member_declaration (decl);
23969 }
23970 else
23971 cp_parser_check_access_in_redeclaration
23972 (TYPE_NAME (type),
23973 decl_spec_token_start->location);
23974 }
23975 }
23976 else
23977 {
23978 bool assume_semicolon = false;
23979
23980 /* Clear attributes from the decl_specifiers but keep them
23981 around as prefix attributes that apply them to the entity
23982 being declared. */
23983 prefix_attributes = decl_specifiers.attributes;
23984 decl_specifiers.attributes = NULL_TREE;
23985
23986 /* See if these declarations will be friends. */
23987 friend_p = cp_parser_friend_p (&decl_specifiers);
23988
23989 /* Keep going until we hit the `;' at the end of the
23990 declaration. */
23991 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23992 {
23993 tree attributes = NULL_TREE;
23994 tree first_attribute;
23995 tree initializer;
23996 bool named_bitfld = false;
23997
23998 /* Peek at the next token. */
23999 token = cp_lexer_peek_token (parser->lexer);
24000
24001 /* The following code wants to know early if it is a bit-field
24002 or some other declaration. Attributes can appear before
24003 the `:' token. Skip over them without consuming any tokens
24004 to peek if they are followed by `:'. */
24005 if (cp_next_tokens_can_be_attribute_p (parser)
24006 || (token->type == CPP_NAME
24007 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24008 && (named_bitfld = true)))
24009 {
24010 size_t n
24011 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24012 token = cp_lexer_peek_nth_token (parser->lexer, n);
24013 }
24014
24015 /* Check for a bitfield declaration. */
24016 if (token->type == CPP_COLON
24017 || (token->type == CPP_NAME
24018 && token == cp_lexer_peek_token (parser->lexer)
24019 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24020 && (named_bitfld = true)))
24021 {
24022 tree identifier;
24023 tree width;
24024 tree late_attributes = NULL_TREE;
24025 location_t id_location
24026 = cp_lexer_peek_token (parser->lexer)->location;
24027
24028 if (named_bitfld)
24029 identifier = cp_parser_identifier (parser);
24030 else
24031 identifier = NULL_TREE;
24032
24033 /* Look for attributes that apply to the bitfield. */
24034 attributes = cp_parser_attributes_opt (parser);
24035
24036 /* Consume the `:' token. */
24037 cp_lexer_consume_token (parser->lexer);
24038
24039 /* Get the width of the bitfield. */
24040 width = cp_parser_constant_expression (parser, false, NULL,
24041 cxx_dialect >= cxx11);
24042
24043 /* In C++2A and as extension for C++11 and above we allow
24044 default member initializers for bit-fields. */
24045 initializer = NULL_TREE;
24046 if (cxx_dialect >= cxx11
24047 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24048 || cp_lexer_next_token_is (parser->lexer,
24049 CPP_OPEN_BRACE)))
24050 {
24051 location_t loc
24052 = cp_lexer_peek_token (parser->lexer)->location;
24053 if (cxx_dialect < cxx2a
24054 && !in_system_header_at (loc)
24055 && identifier != NULL_TREE)
24056 pedwarn (loc, 0,
24057 "default member initializers for bit-fields "
24058 "only available with -std=c++2a or "
24059 "-std=gnu++2a");
24060
24061 initializer = cp_parser_save_nsdmi (parser);
24062 if (identifier == NULL_TREE)
24063 {
24064 error_at (loc, "default member initializer for "
24065 "unnamed bit-field");
24066 initializer = NULL_TREE;
24067 }
24068 }
24069 else
24070 {
24071 /* Look for attributes that apply to the bitfield after
24072 the `:' token and width. This is where GCC used to
24073 parse attributes in the past, pedwarn if there is
24074 a std attribute. */
24075 if (cp_next_tokens_can_be_std_attribute_p (parser))
24076 pedwarn (input_location, OPT_Wpedantic,
24077 "ISO C++ allows bit-field attributes only "
24078 "before the %<:%> token");
24079
24080 late_attributes = cp_parser_attributes_opt (parser);
24081 }
24082
24083 attributes = attr_chainon (attributes, late_attributes);
24084
24085 /* Remember which attributes are prefix attributes and
24086 which are not. */
24087 first_attribute = attributes;
24088 /* Combine the attributes. */
24089 attributes = attr_chainon (prefix_attributes, attributes);
24090
24091 /* Create the bitfield declaration. */
24092 decl = grokbitfield (identifier
24093 ? make_id_declarator (NULL_TREE,
24094 identifier,
24095 sfk_none,
24096 id_location)
24097 : NULL,
24098 &decl_specifiers,
24099 width, initializer,
24100 attributes);
24101 }
24102 else
24103 {
24104 cp_declarator *declarator;
24105 tree asm_specification;
24106 int ctor_dtor_or_conv_p;
24107
24108 /* Parse the declarator. */
24109 declarator
24110 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24111 &ctor_dtor_or_conv_p,
24112 /*parenthesized_p=*/NULL,
24113 /*member_p=*/true,
24114 friend_p);
24115
24116 /* If something went wrong parsing the declarator, make sure
24117 that we at least consume some tokens. */
24118 if (declarator == cp_error_declarator)
24119 {
24120 /* Skip to the end of the statement. */
24121 cp_parser_skip_to_end_of_statement (parser);
24122 /* If the next token is not a semicolon, that is
24123 probably because we just skipped over the body of
24124 a function. So, we consume a semicolon if
24125 present, but do not issue an error message if it
24126 is not present. */
24127 if (cp_lexer_next_token_is (parser->lexer,
24128 CPP_SEMICOLON))
24129 cp_lexer_consume_token (parser->lexer);
24130 goto out;
24131 }
24132
24133 if (declares_class_or_enum & 2)
24134 cp_parser_check_for_definition_in_return_type
24135 (declarator, decl_specifiers.type,
24136 decl_specifiers.locations[ds_type_spec]);
24137
24138 /* Look for an asm-specification. */
24139 asm_specification = cp_parser_asm_specification_opt (parser);
24140 /* Look for attributes that apply to the declaration. */
24141 attributes = cp_parser_attributes_opt (parser);
24142 /* Remember which attributes are prefix attributes and
24143 which are not. */
24144 first_attribute = attributes;
24145 /* Combine the attributes. */
24146 attributes = attr_chainon (prefix_attributes, attributes);
24147
24148 /* If it's an `=', then we have a constant-initializer or a
24149 pure-specifier. It is not correct to parse the
24150 initializer before registering the member declaration
24151 since the member declaration should be in scope while
24152 its initializer is processed. However, the rest of the
24153 front end does not yet provide an interface that allows
24154 us to handle this correctly. */
24155 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24156 {
24157 /* In [class.mem]:
24158
24159 A pure-specifier shall be used only in the declaration of
24160 a virtual function.
24161
24162 A member-declarator can contain a constant-initializer
24163 only if it declares a static member of integral or
24164 enumeration type.
24165
24166 Therefore, if the DECLARATOR is for a function, we look
24167 for a pure-specifier; otherwise, we look for a
24168 constant-initializer. When we call `grokfield', it will
24169 perform more stringent semantics checks. */
24170 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24171 if (function_declarator_p (declarator)
24172 || (decl_specifiers.type
24173 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24174 && declarator->kind == cdk_id
24175 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24176 == FUNCTION_TYPE)))
24177 initializer = cp_parser_pure_specifier (parser);
24178 else if (decl_specifiers.storage_class != sc_static)
24179 initializer = cp_parser_save_nsdmi (parser);
24180 else if (cxx_dialect >= cxx11)
24181 {
24182 bool nonconst;
24183 /* Don't require a constant rvalue in C++11, since we
24184 might want a reference constant. We'll enforce
24185 constancy later. */
24186 cp_lexer_consume_token (parser->lexer);
24187 /* Parse the initializer. */
24188 initializer = cp_parser_initializer_clause (parser,
24189 &nonconst);
24190 }
24191 else
24192 /* Parse the initializer. */
24193 initializer = cp_parser_constant_initializer (parser);
24194 }
24195 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24196 && !function_declarator_p (declarator))
24197 {
24198 bool x;
24199 if (decl_specifiers.storage_class != sc_static)
24200 initializer = cp_parser_save_nsdmi (parser);
24201 else
24202 initializer = cp_parser_initializer (parser, &x, &x);
24203 }
24204 /* Otherwise, there is no initializer. */
24205 else
24206 initializer = NULL_TREE;
24207
24208 /* See if we are probably looking at a function
24209 definition. We are certainly not looking at a
24210 member-declarator. Calling `grokfield' has
24211 side-effects, so we must not do it unless we are sure
24212 that we are looking at a member-declarator. */
24213 if (cp_parser_token_starts_function_definition_p
24214 (cp_lexer_peek_token (parser->lexer)))
24215 {
24216 /* The grammar does not allow a pure-specifier to be
24217 used when a member function is defined. (It is
24218 possible that this fact is an oversight in the
24219 standard, since a pure function may be defined
24220 outside of the class-specifier. */
24221 if (initializer && initializer_token_start)
24222 error_at (initializer_token_start->location,
24223 "pure-specifier on function-definition");
24224 decl = cp_parser_save_member_function_body (parser,
24225 &decl_specifiers,
24226 declarator,
24227 attributes);
24228 if (parser->fully_implicit_function_template_p)
24229 decl = finish_fully_implicit_template (parser, decl);
24230 /* If the member was not a friend, declare it here. */
24231 if (!friend_p)
24232 finish_member_declaration (decl);
24233 /* Peek at the next token. */
24234 token = cp_lexer_peek_token (parser->lexer);
24235 /* If the next token is a semicolon, consume it. */
24236 if (token->type == CPP_SEMICOLON)
24237 {
24238 location_t semicolon_loc
24239 = cp_lexer_consume_token (parser->lexer)->location;
24240 gcc_rich_location richloc (semicolon_loc);
24241 richloc.add_fixit_remove ();
24242 warning_at (&richloc, OPT_Wextra_semi,
24243 "extra %<;%> after in-class "
24244 "function definition");
24245 }
24246 goto out;
24247 }
24248 else
24249 if (declarator->kind == cdk_function)
24250 declarator->id_loc = token->location;
24251 /* Create the declaration. */
24252 decl = grokfield (declarator, &decl_specifiers,
24253 initializer, /*init_const_expr_p=*/true,
24254 asm_specification, attributes);
24255 if (parser->fully_implicit_function_template_p)
24256 {
24257 if (friend_p)
24258 finish_fully_implicit_template (parser, 0);
24259 else
24260 decl = finish_fully_implicit_template (parser, decl);
24261 }
24262 }
24263
24264 cp_finalize_omp_declare_simd (parser, decl);
24265 cp_finalize_oacc_routine (parser, decl, false);
24266
24267 /* Reset PREFIX_ATTRIBUTES. */
24268 if (attributes != error_mark_node)
24269 {
24270 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24271 attributes = TREE_CHAIN (attributes);
24272 if (attributes)
24273 TREE_CHAIN (attributes) = NULL_TREE;
24274 }
24275
24276 /* If there is any qualification still in effect, clear it
24277 now; we will be starting fresh with the next declarator. */
24278 parser->scope = NULL_TREE;
24279 parser->qualifying_scope = NULL_TREE;
24280 parser->object_scope = NULL_TREE;
24281 /* If it's a `,', then there are more declarators. */
24282 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24283 {
24284 cp_lexer_consume_token (parser->lexer);
24285 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24286 {
24287 cp_token *token = cp_lexer_previous_token (parser->lexer);
24288 gcc_rich_location richloc (token->location);
24289 richloc.add_fixit_remove ();
24290 error_at (&richloc, "stray %<,%> at end of "
24291 "member declaration");
24292 }
24293 }
24294 /* If the next token isn't a `;', then we have a parse error. */
24295 else if (cp_lexer_next_token_is_not (parser->lexer,
24296 CPP_SEMICOLON))
24297 {
24298 /* The next token might be a ways away from where the
24299 actual semicolon is missing. Find the previous token
24300 and use that for our error position. */
24301 cp_token *token = cp_lexer_previous_token (parser->lexer);
24302 gcc_rich_location richloc (token->location);
24303 richloc.add_fixit_insert_after (";");
24304 error_at (&richloc, "expected %<;%> at end of "
24305 "member declaration");
24306
24307 /* Assume that the user meant to provide a semicolon. If
24308 we were to cp_parser_skip_to_end_of_statement, we might
24309 skip to a semicolon inside a member function definition
24310 and issue nonsensical error messages. */
24311 assume_semicolon = true;
24312 }
24313
24314 if (decl)
24315 {
24316 /* Add DECL to the list of members. */
24317 if (!friend_p
24318 /* Explicitly include, eg, NSDMIs, for better error
24319 recovery (c++/58650). */
24320 || !DECL_DECLARES_FUNCTION_P (decl))
24321 finish_member_declaration (decl);
24322
24323 if (TREE_CODE (decl) == FUNCTION_DECL)
24324 cp_parser_save_default_args (parser, decl);
24325 else if (TREE_CODE (decl) == FIELD_DECL
24326 && DECL_INITIAL (decl))
24327 /* Add DECL to the queue of NSDMI to be parsed later. */
24328 vec_safe_push (unparsed_nsdmis, decl);
24329 }
24330
24331 if (assume_semicolon)
24332 goto out;
24333 }
24334 }
24335
24336 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24337 out:
24338 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24339 }
24340
24341 /* Parse a pure-specifier.
24342
24343 pure-specifier:
24344 = 0
24345
24346 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24347 Otherwise, ERROR_MARK_NODE is returned. */
24348
24349 static tree
24350 cp_parser_pure_specifier (cp_parser* parser)
24351 {
24352 cp_token *token;
24353
24354 /* Look for the `=' token. */
24355 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24356 return error_mark_node;
24357 /* Look for the `0' token. */
24358 token = cp_lexer_peek_token (parser->lexer);
24359
24360 if (token->type == CPP_EOF
24361 || token->type == CPP_PRAGMA_EOL)
24362 return error_mark_node;
24363
24364 cp_lexer_consume_token (parser->lexer);
24365
24366 /* Accept = default or = delete in c++0x mode. */
24367 if (token->keyword == RID_DEFAULT
24368 || token->keyword == RID_DELETE)
24369 {
24370 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24371 return token->u.value;
24372 }
24373
24374 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24375 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24376 {
24377 cp_parser_error (parser,
24378 "invalid pure specifier (only %<= 0%> is allowed)");
24379 cp_parser_skip_to_end_of_statement (parser);
24380 return error_mark_node;
24381 }
24382 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24383 {
24384 error_at (token->location, "templates may not be %<virtual%>");
24385 return error_mark_node;
24386 }
24387
24388 return integer_zero_node;
24389 }
24390
24391 /* Parse a constant-initializer.
24392
24393 constant-initializer:
24394 = constant-expression
24395
24396 Returns a representation of the constant-expression. */
24397
24398 static tree
24399 cp_parser_constant_initializer (cp_parser* parser)
24400 {
24401 /* Look for the `=' token. */
24402 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24403 return error_mark_node;
24404
24405 /* It is invalid to write:
24406
24407 struct S { static const int i = { 7 }; };
24408
24409 */
24410 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24411 {
24412 cp_parser_error (parser,
24413 "a brace-enclosed initializer is not allowed here");
24414 /* Consume the opening brace. */
24415 matching_braces braces;
24416 braces.consume_open (parser);
24417 /* Skip the initializer. */
24418 cp_parser_skip_to_closing_brace (parser);
24419 /* Look for the trailing `}'. */
24420 braces.require_close (parser);
24421
24422 return error_mark_node;
24423 }
24424
24425 return cp_parser_constant_expression (parser);
24426 }
24427
24428 /* Derived classes [gram.class.derived] */
24429
24430 /* Parse a base-clause.
24431
24432 base-clause:
24433 : base-specifier-list
24434
24435 base-specifier-list:
24436 base-specifier ... [opt]
24437 base-specifier-list , base-specifier ... [opt]
24438
24439 Returns a TREE_LIST representing the base-classes, in the order in
24440 which they were declared. The representation of each node is as
24441 described by cp_parser_base_specifier.
24442
24443 In the case that no bases are specified, this function will return
24444 NULL_TREE, not ERROR_MARK_NODE. */
24445
24446 static tree
24447 cp_parser_base_clause (cp_parser* parser)
24448 {
24449 tree bases = NULL_TREE;
24450
24451 /* Look for the `:' that begins the list. */
24452 cp_parser_require (parser, CPP_COLON, RT_COLON);
24453
24454 /* Scan the base-specifier-list. */
24455 while (true)
24456 {
24457 cp_token *token;
24458 tree base;
24459 bool pack_expansion_p = false;
24460
24461 /* Look for the base-specifier. */
24462 base = cp_parser_base_specifier (parser);
24463 /* Look for the (optional) ellipsis. */
24464 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24465 {
24466 /* Consume the `...'. */
24467 cp_lexer_consume_token (parser->lexer);
24468
24469 pack_expansion_p = true;
24470 }
24471
24472 /* Add BASE to the front of the list. */
24473 if (base && base != error_mark_node)
24474 {
24475 if (pack_expansion_p)
24476 /* Make this a pack expansion type. */
24477 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24478
24479 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24480 {
24481 TREE_CHAIN (base) = bases;
24482 bases = base;
24483 }
24484 }
24485 /* Peek at the next token. */
24486 token = cp_lexer_peek_token (parser->lexer);
24487 /* If it's not a comma, then the list is complete. */
24488 if (token->type != CPP_COMMA)
24489 break;
24490 /* Consume the `,'. */
24491 cp_lexer_consume_token (parser->lexer);
24492 }
24493
24494 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24495 base class had a qualified name. However, the next name that
24496 appears is certainly not qualified. */
24497 parser->scope = NULL_TREE;
24498 parser->qualifying_scope = NULL_TREE;
24499 parser->object_scope = NULL_TREE;
24500
24501 return nreverse (bases);
24502 }
24503
24504 /* Parse a base-specifier.
24505
24506 base-specifier:
24507 :: [opt] nested-name-specifier [opt] class-name
24508 virtual access-specifier [opt] :: [opt] nested-name-specifier
24509 [opt] class-name
24510 access-specifier virtual [opt] :: [opt] nested-name-specifier
24511 [opt] class-name
24512
24513 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24514 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24515 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24516 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24517
24518 static tree
24519 cp_parser_base_specifier (cp_parser* parser)
24520 {
24521 cp_token *token;
24522 bool done = false;
24523 bool virtual_p = false;
24524 bool duplicate_virtual_error_issued_p = false;
24525 bool duplicate_access_error_issued_p = false;
24526 bool class_scope_p, template_p;
24527 tree access = access_default_node;
24528 tree type;
24529
24530 /* Process the optional `virtual' and `access-specifier'. */
24531 while (!done)
24532 {
24533 /* Peek at the next token. */
24534 token = cp_lexer_peek_token (parser->lexer);
24535 /* Process `virtual'. */
24536 switch (token->keyword)
24537 {
24538 case RID_VIRTUAL:
24539 /* If `virtual' appears more than once, issue an error. */
24540 if (virtual_p && !duplicate_virtual_error_issued_p)
24541 {
24542 cp_parser_error (parser,
24543 "%<virtual%> specified more than once in base-specifier");
24544 duplicate_virtual_error_issued_p = true;
24545 }
24546
24547 virtual_p = true;
24548
24549 /* Consume the `virtual' token. */
24550 cp_lexer_consume_token (parser->lexer);
24551
24552 break;
24553
24554 case RID_PUBLIC:
24555 case RID_PROTECTED:
24556 case RID_PRIVATE:
24557 /* If more than one access specifier appears, issue an
24558 error. */
24559 if (access != access_default_node
24560 && !duplicate_access_error_issued_p)
24561 {
24562 cp_parser_error (parser,
24563 "more than one access specifier in base-specifier");
24564 duplicate_access_error_issued_p = true;
24565 }
24566
24567 access = ridpointers[(int) token->keyword];
24568
24569 /* Consume the access-specifier. */
24570 cp_lexer_consume_token (parser->lexer);
24571
24572 break;
24573
24574 default:
24575 done = true;
24576 break;
24577 }
24578 }
24579 /* It is not uncommon to see programs mechanically, erroneously, use
24580 the 'typename' keyword to denote (dependent) qualified types
24581 as base classes. */
24582 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24583 {
24584 token = cp_lexer_peek_token (parser->lexer);
24585 if (!processing_template_decl)
24586 error_at (token->location,
24587 "keyword %<typename%> not allowed outside of templates");
24588 else
24589 error_at (token->location,
24590 "keyword %<typename%> not allowed in this context "
24591 "(the base class is implicitly a type)");
24592 cp_lexer_consume_token (parser->lexer);
24593 }
24594
24595 /* Look for the optional `::' operator. */
24596 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24597 /* Look for the nested-name-specifier. The simplest way to
24598 implement:
24599
24600 [temp.res]
24601
24602 The keyword `typename' is not permitted in a base-specifier or
24603 mem-initializer; in these contexts a qualified name that
24604 depends on a template-parameter is implicitly assumed to be a
24605 type name.
24606
24607 is to pretend that we have seen the `typename' keyword at this
24608 point. */
24609 cp_parser_nested_name_specifier_opt (parser,
24610 /*typename_keyword_p=*/true,
24611 /*check_dependency_p=*/true,
24612 /*type_p=*/true,
24613 /*is_declaration=*/true);
24614 /* If the base class is given by a qualified name, assume that names
24615 we see are type names or templates, as appropriate. */
24616 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24617 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24618
24619 if (!parser->scope
24620 && cp_lexer_next_token_is_decltype (parser->lexer))
24621 /* DR 950 allows decltype as a base-specifier. */
24622 type = cp_parser_decltype (parser);
24623 else
24624 {
24625 /* Otherwise, look for the class-name. */
24626 type = cp_parser_class_name (parser,
24627 class_scope_p,
24628 template_p,
24629 typename_type,
24630 /*check_dependency_p=*/true,
24631 /*class_head_p=*/false,
24632 /*is_declaration=*/true);
24633 type = TREE_TYPE (type);
24634 }
24635
24636 if (type == error_mark_node)
24637 return error_mark_node;
24638
24639 return finish_base_specifier (type, access, virtual_p);
24640 }
24641
24642 /* Exception handling [gram.exception] */
24643
24644 /* Parse an (optional) noexcept-specification.
24645
24646 noexcept-specification:
24647 noexcept ( constant-expression ) [opt]
24648
24649 If no noexcept-specification is present, returns NULL_TREE.
24650 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24651 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24652 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24653 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24654 in which case a boolean condition is returned instead. */
24655
24656 static tree
24657 cp_parser_noexcept_specification_opt (cp_parser* parser,
24658 bool require_constexpr,
24659 bool* consumed_expr,
24660 bool return_cond)
24661 {
24662 cp_token *token;
24663 const char *saved_message;
24664
24665 /* Peek at the next token. */
24666 token = cp_lexer_peek_token (parser->lexer);
24667
24668 /* Is it a noexcept-specification? */
24669 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24670 {
24671 tree expr;
24672 cp_lexer_consume_token (parser->lexer);
24673
24674 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24675 {
24676 matching_parens parens;
24677 parens.consume_open (parser);
24678
24679 tree save_ccp = current_class_ptr;
24680 tree save_ccr = current_class_ref;
24681
24682 if (current_class_type)
24683 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
24684
24685 if (require_constexpr)
24686 {
24687 /* Types may not be defined in an exception-specification. */
24688 saved_message = parser->type_definition_forbidden_message;
24689 parser->type_definition_forbidden_message
24690 = G_("types may not be defined in an exception-specification");
24691
24692 expr = cp_parser_constant_expression (parser);
24693
24694 /* Restore the saved message. */
24695 parser->type_definition_forbidden_message = saved_message;
24696 }
24697 else
24698 {
24699 expr = cp_parser_expression (parser);
24700 *consumed_expr = true;
24701 }
24702
24703 parens.require_close (parser);
24704
24705 current_class_ptr = save_ccp;
24706 current_class_ref = save_ccr;
24707 }
24708 else
24709 {
24710 expr = boolean_true_node;
24711 if (!require_constexpr)
24712 *consumed_expr = false;
24713 }
24714
24715 /* We cannot build a noexcept-spec right away because this will check
24716 that expr is a constexpr. */
24717 if (!return_cond)
24718 return build_noexcept_spec (expr, tf_warning_or_error);
24719 else
24720 return expr;
24721 }
24722 else
24723 return NULL_TREE;
24724 }
24725
24726 /* Parse an (optional) exception-specification.
24727
24728 exception-specification:
24729 throw ( type-id-list [opt] )
24730
24731 Returns a TREE_LIST representing the exception-specification. The
24732 TREE_VALUE of each node is a type. */
24733
24734 static tree
24735 cp_parser_exception_specification_opt (cp_parser* parser)
24736 {
24737 cp_token *token;
24738 tree type_id_list;
24739 const char *saved_message;
24740
24741 /* Peek at the next token. */
24742 token = cp_lexer_peek_token (parser->lexer);
24743
24744 /* Is it a noexcept-specification? */
24745 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24746 false);
24747 if (type_id_list != NULL_TREE)
24748 return type_id_list;
24749
24750 /* If it's not `throw', then there's no exception-specification. */
24751 if (!cp_parser_is_keyword (token, RID_THROW))
24752 return NULL_TREE;
24753
24754 location_t loc = token->location;
24755
24756 /* Consume the `throw'. */
24757 cp_lexer_consume_token (parser->lexer);
24758
24759 /* Look for the `('. */
24760 matching_parens parens;
24761 parens.require_open (parser);
24762
24763 /* Peek at the next token. */
24764 token = cp_lexer_peek_token (parser->lexer);
24765 /* If it's not a `)', then there is a type-id-list. */
24766 if (token->type != CPP_CLOSE_PAREN)
24767 {
24768 /* Types may not be defined in an exception-specification. */
24769 saved_message = parser->type_definition_forbidden_message;
24770 parser->type_definition_forbidden_message
24771 = G_("types may not be defined in an exception-specification");
24772 /* Parse the type-id-list. */
24773 type_id_list = cp_parser_type_id_list (parser);
24774 /* Restore the saved message. */
24775 parser->type_definition_forbidden_message = saved_message;
24776
24777 if (cxx_dialect >= cxx17)
24778 {
24779 error_at (loc, "ISO C++17 does not allow dynamic exception "
24780 "specifications");
24781 type_id_list = NULL_TREE;
24782 }
24783 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24784 warning_at (loc, OPT_Wdeprecated,
24785 "dynamic exception specifications are deprecated in "
24786 "C++11");
24787 }
24788 /* In C++17, throw() is equivalent to noexcept (true). throw()
24789 is deprecated in C++11 and above as well, but is still widely used,
24790 so don't warn about it yet. */
24791 else if (cxx_dialect >= cxx17)
24792 type_id_list = noexcept_true_spec;
24793 else
24794 type_id_list = empty_except_spec;
24795
24796 /* Look for the `)'. */
24797 parens.require_close (parser);
24798
24799 return type_id_list;
24800 }
24801
24802 /* Parse an (optional) type-id-list.
24803
24804 type-id-list:
24805 type-id ... [opt]
24806 type-id-list , type-id ... [opt]
24807
24808 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24809 in the order that the types were presented. */
24810
24811 static tree
24812 cp_parser_type_id_list (cp_parser* parser)
24813 {
24814 tree types = NULL_TREE;
24815
24816 while (true)
24817 {
24818 cp_token *token;
24819 tree type;
24820
24821 token = cp_lexer_peek_token (parser->lexer);
24822
24823 /* Get the next type-id. */
24824 type = cp_parser_type_id (parser);
24825 /* Check for invalid 'auto'. */
24826 if (flag_concepts && type_uses_auto (type))
24827 {
24828 error_at (token->location,
24829 "invalid use of %<auto%> in exception-specification");
24830 type = error_mark_node;
24831 }
24832 /* Parse the optional ellipsis. */
24833 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24834 {
24835 /* Consume the `...'. */
24836 cp_lexer_consume_token (parser->lexer);
24837
24838 /* Turn the type into a pack expansion expression. */
24839 type = make_pack_expansion (type);
24840 }
24841 /* Add it to the list. */
24842 types = add_exception_specifier (types, type, /*complain=*/1);
24843 /* Peek at the next token. */
24844 token = cp_lexer_peek_token (parser->lexer);
24845 /* If it is not a `,', we are done. */
24846 if (token->type != CPP_COMMA)
24847 break;
24848 /* Consume the `,'. */
24849 cp_lexer_consume_token (parser->lexer);
24850 }
24851
24852 return nreverse (types);
24853 }
24854
24855 /* Parse a try-block.
24856
24857 try-block:
24858 try compound-statement handler-seq */
24859
24860 static tree
24861 cp_parser_try_block (cp_parser* parser)
24862 {
24863 tree try_block;
24864
24865 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24866 if (parser->in_function_body
24867 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24868 error ("%<try%> in %<constexpr%> function");
24869
24870 try_block = begin_try_block ();
24871 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24872 finish_try_block (try_block);
24873 cp_parser_handler_seq (parser);
24874 finish_handler_sequence (try_block);
24875
24876 return try_block;
24877 }
24878
24879 /* Parse a function-try-block.
24880
24881 function-try-block:
24882 try ctor-initializer [opt] function-body handler-seq */
24883
24884 static void
24885 cp_parser_function_try_block (cp_parser* parser)
24886 {
24887 tree compound_stmt;
24888 tree try_block;
24889
24890 /* Look for the `try' keyword. */
24891 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24892 return;
24893 /* Let the rest of the front end know where we are. */
24894 try_block = begin_function_try_block (&compound_stmt);
24895 /* Parse the function-body. */
24896 cp_parser_ctor_initializer_opt_and_function_body
24897 (parser, /*in_function_try_block=*/true);
24898 /* We're done with the `try' part. */
24899 finish_function_try_block (try_block);
24900 /* Parse the handlers. */
24901 cp_parser_handler_seq (parser);
24902 /* We're done with the handlers. */
24903 finish_function_handler_sequence (try_block, compound_stmt);
24904 }
24905
24906 /* Parse a handler-seq.
24907
24908 handler-seq:
24909 handler handler-seq [opt] */
24910
24911 static void
24912 cp_parser_handler_seq (cp_parser* parser)
24913 {
24914 while (true)
24915 {
24916 cp_token *token;
24917
24918 /* Parse the handler. */
24919 cp_parser_handler (parser);
24920 /* Peek at the next token. */
24921 token = cp_lexer_peek_token (parser->lexer);
24922 /* If it's not `catch' then there are no more handlers. */
24923 if (!cp_parser_is_keyword (token, RID_CATCH))
24924 break;
24925 }
24926 }
24927
24928 /* Parse a handler.
24929
24930 handler:
24931 catch ( exception-declaration ) compound-statement */
24932
24933 static void
24934 cp_parser_handler (cp_parser* parser)
24935 {
24936 tree handler;
24937 tree declaration;
24938
24939 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24940 handler = begin_handler ();
24941 matching_parens parens;
24942 parens.require_open (parser);
24943 declaration = cp_parser_exception_declaration (parser);
24944 finish_handler_parms (declaration, handler);
24945 parens.require_close (parser);
24946 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24947 finish_handler (handler);
24948 }
24949
24950 /* Parse an exception-declaration.
24951
24952 exception-declaration:
24953 type-specifier-seq declarator
24954 type-specifier-seq abstract-declarator
24955 type-specifier-seq
24956 ...
24957
24958 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24959 ellipsis variant is used. */
24960
24961 static tree
24962 cp_parser_exception_declaration (cp_parser* parser)
24963 {
24964 cp_decl_specifier_seq type_specifiers;
24965 cp_declarator *declarator;
24966 const char *saved_message;
24967
24968 /* If it's an ellipsis, it's easy to handle. */
24969 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24970 {
24971 /* Consume the `...' token. */
24972 cp_lexer_consume_token (parser->lexer);
24973 return NULL_TREE;
24974 }
24975
24976 /* Types may not be defined in exception-declarations. */
24977 saved_message = parser->type_definition_forbidden_message;
24978 parser->type_definition_forbidden_message
24979 = G_("types may not be defined in exception-declarations");
24980
24981 /* Parse the type-specifier-seq. */
24982 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24983 /*is_trailing_return=*/false,
24984 &type_specifiers);
24985 /* If it's a `)', then there is no declarator. */
24986 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24987 declarator = NULL;
24988 else
24989 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24990 /*ctor_dtor_or_conv_p=*/NULL,
24991 /*parenthesized_p=*/NULL,
24992 /*member_p=*/false,
24993 /*friend_p=*/false);
24994
24995 /* Restore the saved message. */
24996 parser->type_definition_forbidden_message = saved_message;
24997
24998 if (!type_specifiers.any_specifiers_p)
24999 return error_mark_node;
25000
25001 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25002 }
25003
25004 /* Parse a throw-expression.
25005
25006 throw-expression:
25007 throw assignment-expression [opt]
25008
25009 Returns a THROW_EXPR representing the throw-expression. */
25010
25011 static tree
25012 cp_parser_throw_expression (cp_parser* parser)
25013 {
25014 tree expression;
25015 cp_token* token;
25016
25017 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25018 token = cp_lexer_peek_token (parser->lexer);
25019 /* Figure out whether or not there is an assignment-expression
25020 following the "throw" keyword. */
25021 if (token->type == CPP_COMMA
25022 || token->type == CPP_SEMICOLON
25023 || token->type == CPP_CLOSE_PAREN
25024 || token->type == CPP_CLOSE_SQUARE
25025 || token->type == CPP_CLOSE_BRACE
25026 || token->type == CPP_COLON)
25027 expression = NULL_TREE;
25028 else
25029 expression = cp_parser_assignment_expression (parser);
25030
25031 return build_throw (expression);
25032 }
25033
25034 /* GNU Extensions */
25035
25036 /* Parse an (optional) asm-specification.
25037
25038 asm-specification:
25039 asm ( string-literal )
25040
25041 If the asm-specification is present, returns a STRING_CST
25042 corresponding to the string-literal. Otherwise, returns
25043 NULL_TREE. */
25044
25045 static tree
25046 cp_parser_asm_specification_opt (cp_parser* parser)
25047 {
25048 cp_token *token;
25049 tree asm_specification;
25050
25051 /* Peek at the next token. */
25052 token = cp_lexer_peek_token (parser->lexer);
25053 /* If the next token isn't the `asm' keyword, then there's no
25054 asm-specification. */
25055 if (!cp_parser_is_keyword (token, RID_ASM))
25056 return NULL_TREE;
25057
25058 /* Consume the `asm' token. */
25059 cp_lexer_consume_token (parser->lexer);
25060 /* Look for the `('. */
25061 matching_parens parens;
25062 parens.require_open (parser);
25063
25064 /* Look for the string-literal. */
25065 asm_specification = cp_parser_string_literal (parser, false, false);
25066
25067 /* Look for the `)'. */
25068 parens.require_close (parser);
25069
25070 return asm_specification;
25071 }
25072
25073 /* Parse an asm-operand-list.
25074
25075 asm-operand-list:
25076 asm-operand
25077 asm-operand-list , asm-operand
25078
25079 asm-operand:
25080 string-literal ( expression )
25081 [ string-literal ] string-literal ( expression )
25082
25083 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25084 each node is the expression. The TREE_PURPOSE is itself a
25085 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25086 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25087 is a STRING_CST for the string literal before the parenthesis. Returns
25088 ERROR_MARK_NODE if any of the operands are invalid. */
25089
25090 static tree
25091 cp_parser_asm_operand_list (cp_parser* parser)
25092 {
25093 tree asm_operands = NULL_TREE;
25094 bool invalid_operands = false;
25095
25096 while (true)
25097 {
25098 tree string_literal;
25099 tree expression;
25100 tree name;
25101
25102 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25103 {
25104 /* Consume the `[' token. */
25105 cp_lexer_consume_token (parser->lexer);
25106 /* Read the operand name. */
25107 name = cp_parser_identifier (parser);
25108 if (name != error_mark_node)
25109 name = build_string (IDENTIFIER_LENGTH (name),
25110 IDENTIFIER_POINTER (name));
25111 /* Look for the closing `]'. */
25112 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25113 }
25114 else
25115 name = NULL_TREE;
25116 /* Look for the string-literal. */
25117 string_literal = cp_parser_string_literal (parser, false, false);
25118
25119 /* Look for the `('. */
25120 matching_parens parens;
25121 parens.require_open (parser);
25122 /* Parse the expression. */
25123 expression = cp_parser_expression (parser);
25124 /* Look for the `)'. */
25125 parens.require_close (parser);
25126
25127 if (name == error_mark_node
25128 || string_literal == error_mark_node
25129 || expression == error_mark_node)
25130 invalid_operands = true;
25131
25132 /* Add this operand to the list. */
25133 asm_operands = tree_cons (build_tree_list (name, string_literal),
25134 expression,
25135 asm_operands);
25136 /* If the next token is not a `,', there are no more
25137 operands. */
25138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25139 break;
25140 /* Consume the `,'. */
25141 cp_lexer_consume_token (parser->lexer);
25142 }
25143
25144 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25145 }
25146
25147 /* Parse an asm-clobber-list.
25148
25149 asm-clobber-list:
25150 string-literal
25151 asm-clobber-list , string-literal
25152
25153 Returns a TREE_LIST, indicating the clobbers in the order that they
25154 appeared. The TREE_VALUE of each node is a STRING_CST. */
25155
25156 static tree
25157 cp_parser_asm_clobber_list (cp_parser* parser)
25158 {
25159 tree clobbers = NULL_TREE;
25160
25161 while (true)
25162 {
25163 tree string_literal;
25164
25165 /* Look for the string literal. */
25166 string_literal = cp_parser_string_literal (parser, false, false);
25167 /* Add it to the list. */
25168 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25169 /* If the next token is not a `,', then the list is
25170 complete. */
25171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25172 break;
25173 /* Consume the `,' token. */
25174 cp_lexer_consume_token (parser->lexer);
25175 }
25176
25177 return clobbers;
25178 }
25179
25180 /* Parse an asm-label-list.
25181
25182 asm-label-list:
25183 identifier
25184 asm-label-list , identifier
25185
25186 Returns a TREE_LIST, indicating the labels in the order that they
25187 appeared. The TREE_VALUE of each node is a label. */
25188
25189 static tree
25190 cp_parser_asm_label_list (cp_parser* parser)
25191 {
25192 tree labels = NULL_TREE;
25193
25194 while (true)
25195 {
25196 tree identifier, label, name;
25197
25198 /* Look for the identifier. */
25199 identifier = cp_parser_identifier (parser);
25200 if (!error_operand_p (identifier))
25201 {
25202 label = lookup_label (identifier);
25203 if (TREE_CODE (label) == LABEL_DECL)
25204 {
25205 TREE_USED (label) = 1;
25206 check_goto (label);
25207 name = build_string (IDENTIFIER_LENGTH (identifier),
25208 IDENTIFIER_POINTER (identifier));
25209 labels = tree_cons (name, label, labels);
25210 }
25211 }
25212 /* If the next token is not a `,', then the list is
25213 complete. */
25214 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25215 break;
25216 /* Consume the `,' token. */
25217 cp_lexer_consume_token (parser->lexer);
25218 }
25219
25220 return nreverse (labels);
25221 }
25222
25223 /* Return TRUE iff the next tokens in the stream are possibly the
25224 beginning of a GNU extension attribute. */
25225
25226 static bool
25227 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25228 {
25229 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25230 }
25231
25232 /* Return TRUE iff the next tokens in the stream are possibly the
25233 beginning of a standard C++-11 attribute specifier. */
25234
25235 static bool
25236 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25237 {
25238 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25239 }
25240
25241 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25242 beginning of a standard C++-11 attribute specifier. */
25243
25244 static bool
25245 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25246 {
25247 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25248
25249 return (cxx_dialect >= cxx11
25250 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25251 || (token->type == CPP_OPEN_SQUARE
25252 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25253 && token->type == CPP_OPEN_SQUARE)));
25254 }
25255
25256 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25257 beginning of a GNU extension attribute. */
25258
25259 static bool
25260 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25261 {
25262 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25263
25264 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25265 }
25266
25267 /* Return true iff the next tokens can be the beginning of either a
25268 GNU attribute list, or a standard C++11 attribute sequence. */
25269
25270 static bool
25271 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25272 {
25273 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25274 || cp_next_tokens_can_be_std_attribute_p (parser));
25275 }
25276
25277 /* Return true iff the next Nth tokens can be the beginning of either
25278 a GNU attribute list, or a standard C++11 attribute sequence. */
25279
25280 static bool
25281 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25282 {
25283 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25284 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25285 }
25286
25287 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25288 of GNU attributes, or return NULL. */
25289
25290 static tree
25291 cp_parser_attributes_opt (cp_parser *parser)
25292 {
25293 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25294 return cp_parser_gnu_attributes_opt (parser);
25295 return cp_parser_std_attribute_spec_seq (parser);
25296 }
25297
25298 /* Parse an (optional) series of attributes.
25299
25300 attributes:
25301 attributes attribute
25302
25303 attribute:
25304 __attribute__ (( attribute-list [opt] ))
25305
25306 The return value is as for cp_parser_gnu_attribute_list. */
25307
25308 static tree
25309 cp_parser_gnu_attributes_opt (cp_parser* parser)
25310 {
25311 tree attributes = NULL_TREE;
25312
25313 temp_override<bool> cleanup
25314 (parser->auto_is_implicit_function_template_parm_p, false);
25315
25316 while (true)
25317 {
25318 cp_token *token;
25319 tree attribute_list;
25320 bool ok = true;
25321
25322 /* Peek at the next token. */
25323 token = cp_lexer_peek_token (parser->lexer);
25324 /* If it's not `__attribute__', then we're done. */
25325 if (token->keyword != RID_ATTRIBUTE)
25326 break;
25327
25328 /* Consume the `__attribute__' keyword. */
25329 cp_lexer_consume_token (parser->lexer);
25330 /* Look for the two `(' tokens. */
25331 matching_parens outer_parens;
25332 outer_parens.require_open (parser);
25333 matching_parens inner_parens;
25334 inner_parens.require_open (parser);
25335
25336 /* Peek at the next token. */
25337 token = cp_lexer_peek_token (parser->lexer);
25338 if (token->type != CPP_CLOSE_PAREN)
25339 /* Parse the attribute-list. */
25340 attribute_list = cp_parser_gnu_attribute_list (parser);
25341 else
25342 /* If the next token is a `)', then there is no attribute
25343 list. */
25344 attribute_list = NULL;
25345
25346 /* Look for the two `)' tokens. */
25347 if (!inner_parens.require_close (parser))
25348 ok = false;
25349 if (!outer_parens.require_close (parser))
25350 ok = false;
25351 if (!ok)
25352 cp_parser_skip_to_end_of_statement (parser);
25353
25354 /* Add these new attributes to the list. */
25355 attributes = attr_chainon (attributes, attribute_list);
25356 }
25357
25358 return attributes;
25359 }
25360
25361 /* Parse a GNU attribute-list.
25362
25363 attribute-list:
25364 attribute
25365 attribute-list , attribute
25366
25367 attribute:
25368 identifier
25369 identifier ( identifier )
25370 identifier ( identifier , expression-list )
25371 identifier ( expression-list )
25372
25373 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25374 to an attribute. The TREE_PURPOSE of each node is the identifier
25375 indicating which attribute is in use. The TREE_VALUE represents
25376 the arguments, if any. */
25377
25378 static tree
25379 cp_parser_gnu_attribute_list (cp_parser* parser)
25380 {
25381 tree attribute_list = NULL_TREE;
25382 bool save_translate_strings_p = parser->translate_strings_p;
25383
25384 parser->translate_strings_p = false;
25385 while (true)
25386 {
25387 cp_token *token;
25388 tree identifier;
25389 tree attribute;
25390
25391 /* Look for the identifier. We also allow keywords here; for
25392 example `__attribute__ ((const))' is legal. */
25393 token = cp_lexer_peek_token (parser->lexer);
25394 if (token->type == CPP_NAME
25395 || token->type == CPP_KEYWORD)
25396 {
25397 tree arguments = NULL_TREE;
25398
25399 /* Consume the token, but save it since we need it for the
25400 SIMD enabled function parsing. */
25401 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25402
25403 /* Save away the identifier that indicates which attribute
25404 this is. */
25405 identifier = (token->type == CPP_KEYWORD)
25406 /* For keywords, use the canonical spelling, not the
25407 parsed identifier. */
25408 ? ridpointers[(int) token->keyword]
25409 : id_token->u.value;
25410
25411 identifier = canonicalize_attr_name (identifier);
25412 attribute = build_tree_list (identifier, NULL_TREE);
25413
25414 /* Peek at the next token. */
25415 token = cp_lexer_peek_token (parser->lexer);
25416 /* If it's an `(', then parse the attribute arguments. */
25417 if (token->type == CPP_OPEN_PAREN)
25418 {
25419 vec<tree, va_gc> *vec;
25420 int attr_flag = (attribute_takes_identifier_p (identifier)
25421 ? id_attr : normal_attr);
25422 vec = cp_parser_parenthesized_expression_list
25423 (parser, attr_flag, /*cast_p=*/false,
25424 /*allow_expansion_p=*/false,
25425 /*non_constant_p=*/NULL);
25426 if (vec == NULL)
25427 arguments = error_mark_node;
25428 else
25429 {
25430 arguments = build_tree_list_vec (vec);
25431 release_tree_vector (vec);
25432 }
25433 /* Save the arguments away. */
25434 TREE_VALUE (attribute) = arguments;
25435 }
25436
25437 if (arguments != error_mark_node)
25438 {
25439 /* Add this attribute to the list. */
25440 TREE_CHAIN (attribute) = attribute_list;
25441 attribute_list = attribute;
25442 }
25443
25444 token = cp_lexer_peek_token (parser->lexer);
25445 }
25446 /* Now, look for more attributes. If the next token isn't a
25447 `,', we're done. */
25448 if (token->type != CPP_COMMA)
25449 break;
25450
25451 /* Consume the comma and keep going. */
25452 cp_lexer_consume_token (parser->lexer);
25453 }
25454 parser->translate_strings_p = save_translate_strings_p;
25455
25456 /* We built up the list in reverse order. */
25457 return nreverse (attribute_list);
25458 }
25459
25460 /* Parse a standard C++11 attribute.
25461
25462 The returned representation is a TREE_LIST which TREE_PURPOSE is
25463 the scoped name of the attribute, and the TREE_VALUE is its
25464 arguments list.
25465
25466 Note that the scoped name of the attribute is itself a TREE_LIST
25467 which TREE_PURPOSE is the namespace of the attribute, and
25468 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25469 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25470 and which TREE_PURPOSE is directly the attribute name.
25471
25472 Clients of the attribute code should use get_attribute_namespace
25473 and get_attribute_name to get the actual namespace and name of
25474 attributes, regardless of their being GNU or C++11 attributes.
25475
25476 attribute:
25477 attribute-token attribute-argument-clause [opt]
25478
25479 attribute-token:
25480 identifier
25481 attribute-scoped-token
25482
25483 attribute-scoped-token:
25484 attribute-namespace :: identifier
25485
25486 attribute-namespace:
25487 identifier
25488
25489 attribute-argument-clause:
25490 ( balanced-token-seq )
25491
25492 balanced-token-seq:
25493 balanced-token [opt]
25494 balanced-token-seq balanced-token
25495
25496 balanced-token:
25497 ( balanced-token-seq )
25498 [ balanced-token-seq ]
25499 { balanced-token-seq }. */
25500
25501 static tree
25502 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25503 {
25504 tree attribute, attr_id = NULL_TREE, arguments;
25505 cp_token *token;
25506
25507 temp_override<bool> cleanup
25508 (parser->auto_is_implicit_function_template_parm_p, false);
25509
25510 /* First, parse name of the attribute, a.k.a attribute-token. */
25511
25512 token = cp_lexer_peek_token (parser->lexer);
25513 if (token->type == CPP_NAME)
25514 attr_id = token->u.value;
25515 else if (token->type == CPP_KEYWORD)
25516 attr_id = ridpointers[(int) token->keyword];
25517 else if (token->flags & NAMED_OP)
25518 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25519
25520 if (attr_id == NULL_TREE)
25521 return NULL_TREE;
25522
25523 cp_lexer_consume_token (parser->lexer);
25524
25525 token = cp_lexer_peek_token (parser->lexer);
25526 if (token->type == CPP_SCOPE)
25527 {
25528 /* We are seeing a scoped attribute token. */
25529
25530 cp_lexer_consume_token (parser->lexer);
25531 if (attr_ns)
25532 error_at (token->location, "attribute using prefix used together "
25533 "with scoped attribute token");
25534 attr_ns = attr_id;
25535
25536 token = cp_lexer_consume_token (parser->lexer);
25537 if (token->type == CPP_NAME)
25538 attr_id = token->u.value;
25539 else if (token->type == CPP_KEYWORD)
25540 attr_id = ridpointers[(int) token->keyword];
25541 else if (token->flags & NAMED_OP)
25542 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25543 else
25544 {
25545 error_at (token->location,
25546 "expected an identifier for the attribute name");
25547 return error_mark_node;
25548 }
25549
25550 attr_ns = canonicalize_attr_name (attr_ns);
25551 attr_id = canonicalize_attr_name (attr_id);
25552 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25553 NULL_TREE);
25554 token = cp_lexer_peek_token (parser->lexer);
25555 }
25556 else if (attr_ns)
25557 {
25558 attr_ns = canonicalize_attr_name (attr_ns);
25559 attr_id = canonicalize_attr_name (attr_id);
25560 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25561 NULL_TREE);
25562 }
25563 else
25564 {
25565 attr_id = canonicalize_attr_name (attr_id);
25566 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25567 NULL_TREE);
25568 /* C++11 noreturn attribute is equivalent to GNU's. */
25569 if (is_attribute_p ("noreturn", attr_id))
25570 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25571 /* C++14 deprecated attribute is equivalent to GNU's. */
25572 else if (is_attribute_p ("deprecated", attr_id))
25573 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25574 /* C++17 fallthrough attribute is equivalent to GNU's. */
25575 else if (is_attribute_p ("fallthrough", attr_id))
25576 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25577 /* Transactional Memory TS optimize_for_synchronized attribute is
25578 equivalent to GNU transaction_callable. */
25579 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25580 TREE_PURPOSE (attribute)
25581 = get_identifier ("transaction_callable");
25582 /* Transactional Memory attributes are GNU attributes. */
25583 else if (tm_attr_to_mask (attr_id))
25584 TREE_PURPOSE (attribute) = attr_id;
25585 }
25586
25587 /* Now parse the optional argument clause of the attribute. */
25588
25589 if (token->type != CPP_OPEN_PAREN)
25590 return attribute;
25591
25592 {
25593 vec<tree, va_gc> *vec;
25594 int attr_flag = normal_attr;
25595
25596 if (attr_ns == gnu_identifier
25597 && attribute_takes_identifier_p (attr_id))
25598 /* A GNU attribute that takes an identifier in parameter. */
25599 attr_flag = id_attr;
25600
25601 vec = cp_parser_parenthesized_expression_list
25602 (parser, attr_flag, /*cast_p=*/false,
25603 /*allow_expansion_p=*/true,
25604 /*non_constant_p=*/NULL);
25605 if (vec == NULL)
25606 arguments = error_mark_node;
25607 else
25608 {
25609 arguments = build_tree_list_vec (vec);
25610 release_tree_vector (vec);
25611 }
25612
25613 if (arguments == error_mark_node)
25614 attribute = error_mark_node;
25615 else
25616 TREE_VALUE (attribute) = arguments;
25617 }
25618
25619 return attribute;
25620 }
25621
25622 /* Check that the attribute ATTRIBUTE appears at most once in the
25623 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25624 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25625 isn't implemented yet in GCC. */
25626
25627 static void
25628 cp_parser_check_std_attribute (tree attributes, tree attribute)
25629 {
25630 if (attributes)
25631 {
25632 tree name = get_attribute_name (attribute);
25633 if (is_attribute_p ("noreturn", name)
25634 && lookup_attribute ("noreturn", attributes))
25635 error ("attribute %<noreturn%> can appear at most once "
25636 "in an attribute-list");
25637 else if (is_attribute_p ("deprecated", name)
25638 && lookup_attribute ("deprecated", attributes))
25639 error ("attribute %<deprecated%> can appear at most once "
25640 "in an attribute-list");
25641 }
25642 }
25643
25644 /* Parse a list of standard C++-11 attributes.
25645
25646 attribute-list:
25647 attribute [opt]
25648 attribute-list , attribute[opt]
25649 attribute ...
25650 attribute-list , attribute ...
25651 */
25652
25653 static tree
25654 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25655 {
25656 tree attributes = NULL_TREE, attribute = NULL_TREE;
25657 cp_token *token = NULL;
25658
25659 while (true)
25660 {
25661 attribute = cp_parser_std_attribute (parser, attr_ns);
25662 if (attribute == error_mark_node)
25663 break;
25664 if (attribute != NULL_TREE)
25665 {
25666 cp_parser_check_std_attribute (attributes, attribute);
25667 TREE_CHAIN (attribute) = attributes;
25668 attributes = attribute;
25669 }
25670 token = cp_lexer_peek_token (parser->lexer);
25671 if (token->type == CPP_ELLIPSIS)
25672 {
25673 cp_lexer_consume_token (parser->lexer);
25674 if (attribute == NULL_TREE)
25675 error_at (token->location,
25676 "expected attribute before %<...%>");
25677 else
25678 {
25679 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25680 if (pack == error_mark_node)
25681 return error_mark_node;
25682 TREE_VALUE (attribute) = pack;
25683 }
25684 token = cp_lexer_peek_token (parser->lexer);
25685 }
25686 if (token->type != CPP_COMMA)
25687 break;
25688 cp_lexer_consume_token (parser->lexer);
25689 }
25690 attributes = nreverse (attributes);
25691 return attributes;
25692 }
25693
25694 /* Parse a standard C++-11 attribute specifier.
25695
25696 attribute-specifier:
25697 [ [ attribute-using-prefix [opt] attribute-list ] ]
25698 alignment-specifier
25699
25700 attribute-using-prefix:
25701 using attribute-namespace :
25702
25703 alignment-specifier:
25704 alignas ( type-id ... [opt] )
25705 alignas ( alignment-expression ... [opt] ). */
25706
25707 static tree
25708 cp_parser_std_attribute_spec (cp_parser *parser)
25709 {
25710 tree attributes = NULL_TREE;
25711 cp_token *token = cp_lexer_peek_token (parser->lexer);
25712
25713 if (token->type == CPP_OPEN_SQUARE
25714 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25715 {
25716 tree attr_ns = NULL_TREE;
25717
25718 cp_lexer_consume_token (parser->lexer);
25719 cp_lexer_consume_token (parser->lexer);
25720
25721 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25722 {
25723 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25724 if (token->type == CPP_NAME)
25725 attr_ns = token->u.value;
25726 else if (token->type == CPP_KEYWORD)
25727 attr_ns = ridpointers[(int) token->keyword];
25728 else if (token->flags & NAMED_OP)
25729 attr_ns = get_identifier (cpp_type2name (token->type,
25730 token->flags));
25731 if (attr_ns
25732 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25733 {
25734 if (cxx_dialect < cxx17
25735 && !in_system_header_at (input_location))
25736 pedwarn (input_location, 0,
25737 "attribute using prefix only available "
25738 "with -std=c++17 or -std=gnu++17");
25739
25740 cp_lexer_consume_token (parser->lexer);
25741 cp_lexer_consume_token (parser->lexer);
25742 cp_lexer_consume_token (parser->lexer);
25743 }
25744 else
25745 attr_ns = NULL_TREE;
25746 }
25747
25748 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25749
25750 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25751 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25752 cp_parser_skip_to_end_of_statement (parser);
25753 else
25754 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25755 when we are sure that we have actually parsed them. */
25756 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25757 }
25758 else
25759 {
25760 tree alignas_expr;
25761
25762 /* Look for an alignment-specifier. */
25763
25764 token = cp_lexer_peek_token (parser->lexer);
25765
25766 if (token->type != CPP_KEYWORD
25767 || token->keyword != RID_ALIGNAS)
25768 return NULL_TREE;
25769
25770 cp_lexer_consume_token (parser->lexer);
25771 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25772
25773 matching_parens parens;
25774 if (!parens.require_open (parser))
25775 return error_mark_node;
25776
25777 cp_parser_parse_tentatively (parser);
25778 alignas_expr = cp_parser_type_id (parser);
25779
25780 if (!cp_parser_parse_definitely (parser))
25781 {
25782 alignas_expr = cp_parser_assignment_expression (parser);
25783 if (alignas_expr == error_mark_node)
25784 cp_parser_skip_to_end_of_statement (parser);
25785 if (alignas_expr == NULL_TREE
25786 || alignas_expr == error_mark_node)
25787 return alignas_expr;
25788 }
25789
25790 alignas_expr = cxx_alignas_expr (alignas_expr);
25791 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25792
25793 /* Handle alignas (pack...). */
25794 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25795 {
25796 cp_lexer_consume_token (parser->lexer);
25797 alignas_expr = make_pack_expansion (alignas_expr);
25798 }
25799
25800 /* Something went wrong, so don't build the attribute. */
25801 if (alignas_expr == error_mark_node)
25802 return error_mark_node;
25803
25804 if (!parens.require_close (parser))
25805 return error_mark_node;
25806
25807 /* Build the C++-11 representation of an 'aligned'
25808 attribute. */
25809 attributes
25810 = build_tree_list (build_tree_list (gnu_identifier,
25811 aligned_identifier), alignas_expr);
25812 }
25813
25814 return attributes;
25815 }
25816
25817 /* Parse a standard C++-11 attribute-specifier-seq.
25818
25819 attribute-specifier-seq:
25820 attribute-specifier-seq [opt] attribute-specifier
25821 */
25822
25823 static tree
25824 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25825 {
25826 tree attr_specs = NULL_TREE;
25827 tree attr_last = NULL_TREE;
25828
25829 while (true)
25830 {
25831 tree attr_spec = cp_parser_std_attribute_spec (parser);
25832 if (attr_spec == NULL_TREE)
25833 break;
25834 if (attr_spec == error_mark_node)
25835 return error_mark_node;
25836
25837 if (attr_last)
25838 TREE_CHAIN (attr_last) = attr_spec;
25839 else
25840 attr_specs = attr_last = attr_spec;
25841 attr_last = tree_last (attr_last);
25842 }
25843
25844 return attr_specs;
25845 }
25846
25847 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
25848 return index of the first token after balanced-token, or N on failure. */
25849
25850 static size_t
25851 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
25852 {
25853 size_t orig_n = n;
25854 int nparens = 0, nbraces = 0, nsquares = 0;
25855 do
25856 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
25857 {
25858 case CPP_EOF:
25859 case CPP_PRAGMA_EOL:
25860 /* Ran out of tokens. */
25861 return orig_n;
25862 case CPP_OPEN_PAREN:
25863 ++nparens;
25864 break;
25865 case CPP_OPEN_BRACE:
25866 ++nbraces;
25867 break;
25868 case CPP_OPEN_SQUARE:
25869 ++nsquares;
25870 break;
25871 case CPP_CLOSE_PAREN:
25872 --nparens;
25873 break;
25874 case CPP_CLOSE_BRACE:
25875 --nbraces;
25876 break;
25877 case CPP_CLOSE_SQUARE:
25878 --nsquares;
25879 break;
25880 default:
25881 break;
25882 }
25883 while (nparens || nbraces || nsquares);
25884 return n;
25885 }
25886
25887 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
25888 return index of the first token after the GNU attribute tokens, or N on
25889 failure. */
25890
25891 static size_t
25892 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
25893 {
25894 while (true)
25895 {
25896 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
25897 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
25898 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
25899 break;
25900
25901 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
25902 if (n2 == n + 2)
25903 break;
25904 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
25905 break;
25906 n = n2 + 1;
25907 }
25908 return n;
25909 }
25910
25911 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
25912 next token), return index of the first token after the standard C++11
25913 attribute tokens, or N on failure. */
25914
25915 static size_t
25916 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
25917 {
25918 while (true)
25919 {
25920 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
25921 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
25922 {
25923 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25924 if (n2 == n + 1)
25925 break;
25926 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
25927 break;
25928 n = n2 + 1;
25929 }
25930 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
25931 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
25932 {
25933 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
25934 if (n2 == n + 1)
25935 break;
25936 n = n2;
25937 }
25938 else
25939 break;
25940 }
25941 return n;
25942 }
25943
25944 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
25945 as the next token), return index of the first token after the attribute
25946 tokens, or N on failure. */
25947
25948 static size_t
25949 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
25950 {
25951 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
25952 return cp_parser_skip_gnu_attributes_opt (parser, n);
25953 return cp_parser_skip_std_attribute_spec_seq (parser, n);
25954 }
25955
25956 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25957 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25958 current value of the PEDANTIC flag, regardless of whether or not
25959 the `__extension__' keyword is present. The caller is responsible
25960 for restoring the value of the PEDANTIC flag. */
25961
25962 static bool
25963 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25964 {
25965 /* Save the old value of the PEDANTIC flag. */
25966 *saved_pedantic = pedantic;
25967
25968 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25969 {
25970 /* Consume the `__extension__' token. */
25971 cp_lexer_consume_token (parser->lexer);
25972 /* We're not being pedantic while the `__extension__' keyword is
25973 in effect. */
25974 pedantic = 0;
25975
25976 return true;
25977 }
25978
25979 return false;
25980 }
25981
25982 /* Parse a label declaration.
25983
25984 label-declaration:
25985 __label__ label-declarator-seq ;
25986
25987 label-declarator-seq:
25988 identifier , label-declarator-seq
25989 identifier */
25990
25991 static void
25992 cp_parser_label_declaration (cp_parser* parser)
25993 {
25994 /* Look for the `__label__' keyword. */
25995 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25996
25997 while (true)
25998 {
25999 tree identifier;
26000
26001 /* Look for an identifier. */
26002 identifier = cp_parser_identifier (parser);
26003 /* If we failed, stop. */
26004 if (identifier == error_mark_node)
26005 break;
26006 /* Declare it as a label. */
26007 finish_label_decl (identifier);
26008 /* If the next token is a `;', stop. */
26009 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26010 break;
26011 /* Look for the `,' separating the label declarations. */
26012 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26013 }
26014
26015 /* Look for the final `;'. */
26016 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26017 }
26018
26019 // -------------------------------------------------------------------------- //
26020 // Requires Clause
26021
26022 // Parse a requires clause.
26023 //
26024 // requires-clause:
26025 // 'requires' logical-or-expression
26026 //
26027 // The required logical-or-expression must be a constant expression. Note
26028 // that we don't check that the expression is constepxr here. We defer until
26029 // we analyze constraints and then, we only check atomic constraints.
26030 static tree
26031 cp_parser_requires_clause (cp_parser *parser)
26032 {
26033 // Parse the requires clause so that it is not automatically folded.
26034 ++processing_template_decl;
26035 tree expr = cp_parser_binary_expression (parser, false, false,
26036 PREC_NOT_OPERATOR, NULL);
26037 if (check_for_bare_parameter_packs (expr))
26038 expr = error_mark_node;
26039 --processing_template_decl;
26040 return expr;
26041 }
26042
26043 // Optionally parse a requires clause:
26044 static tree
26045 cp_parser_requires_clause_opt (cp_parser *parser)
26046 {
26047 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26048 if (tok->keyword != RID_REQUIRES)
26049 {
26050 if (!flag_concepts && tok->type == CPP_NAME
26051 && tok->u.value == ridpointers[RID_REQUIRES])
26052 {
26053 error_at (cp_lexer_peek_token (parser->lexer)->location,
26054 "%<requires%> only available with -fconcepts");
26055 /* Parse and discard the requires-clause. */
26056 cp_lexer_consume_token (parser->lexer);
26057 cp_parser_requires_clause (parser);
26058 }
26059 return NULL_TREE;
26060 }
26061 cp_lexer_consume_token (parser->lexer);
26062 return cp_parser_requires_clause (parser);
26063 }
26064
26065
26066 /*---------------------------------------------------------------------------
26067 Requires expressions
26068 ---------------------------------------------------------------------------*/
26069
26070 /* Parse a requires expression
26071
26072 requirement-expression:
26073 'requires' requirement-parameter-list [opt] requirement-body */
26074 static tree
26075 cp_parser_requires_expression (cp_parser *parser)
26076 {
26077 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26078 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26079
26080 /* A requires-expression shall appear only within a concept
26081 definition or a requires-clause.
26082
26083 TODO: Implement this diagnostic correctly. */
26084 if (!processing_template_decl)
26085 {
26086 error_at (loc, "a requires expression cannot appear outside a template");
26087 cp_parser_skip_to_end_of_statement (parser);
26088 return error_mark_node;
26089 }
26090
26091 tree parms, reqs;
26092 {
26093 /* Local parameters are delared as variables within the scope
26094 of the expression. They are not visible past the end of
26095 the expression. Expressions within the requires-expression
26096 are unevaluated. */
26097 struct scope_sentinel
26098 {
26099 scope_sentinel ()
26100 {
26101 ++cp_unevaluated_operand;
26102 begin_scope (sk_block, NULL_TREE);
26103 }
26104
26105 ~scope_sentinel ()
26106 {
26107 pop_bindings_and_leave_scope ();
26108 --cp_unevaluated_operand;
26109 }
26110 } s;
26111
26112 /* Parse the optional parameter list. */
26113 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26114 {
26115 parms = cp_parser_requirement_parameter_list (parser);
26116 if (parms == error_mark_node)
26117 return error_mark_node;
26118 }
26119 else
26120 parms = NULL_TREE;
26121
26122 /* Parse the requirement body. */
26123 reqs = cp_parser_requirement_body (parser);
26124 if (reqs == error_mark_node)
26125 return error_mark_node;
26126 }
26127
26128 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26129 the parm chain. */
26130 grokparms (parms, &parms);
26131 return finish_requires_expr (parms, reqs);
26132 }
26133
26134 /* Parse a parameterized requirement.
26135
26136 requirement-parameter-list:
26137 '(' parameter-declaration-clause ')' */
26138 static tree
26139 cp_parser_requirement_parameter_list (cp_parser *parser)
26140 {
26141 matching_parens parens;
26142 if (!parens.require_open (parser))
26143 return error_mark_node;
26144
26145 tree parms = cp_parser_parameter_declaration_clause (parser);
26146
26147 if (!parens.require_close (parser))
26148 return error_mark_node;
26149
26150 return parms;
26151 }
26152
26153 /* Parse the body of a requirement.
26154
26155 requirement-body:
26156 '{' requirement-list '}' */
26157 static tree
26158 cp_parser_requirement_body (cp_parser *parser)
26159 {
26160 matching_braces braces;
26161 if (!braces.require_open (parser))
26162 return error_mark_node;
26163
26164 tree reqs = cp_parser_requirement_list (parser);
26165
26166 if (!braces.require_close (parser))
26167 return error_mark_node;
26168
26169 return reqs;
26170 }
26171
26172 /* Parse a list of requirements.
26173
26174 requirement-list:
26175 requirement
26176 requirement-list ';' requirement[opt] */
26177 static tree
26178 cp_parser_requirement_list (cp_parser *parser)
26179 {
26180 tree result = NULL_TREE;
26181 while (true)
26182 {
26183 tree req = cp_parser_requirement (parser);
26184 if (req == error_mark_node)
26185 return error_mark_node;
26186
26187 result = tree_cons (NULL_TREE, req, result);
26188
26189 /* If we see a semi-colon, consume it. */
26190 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26191 cp_lexer_consume_token (parser->lexer);
26192
26193 /* Stop processing at the end of the list. */
26194 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26195 break;
26196 }
26197
26198 /* Reverse the order of requirements so they are analyzed in
26199 declaration order. */
26200 return nreverse (result);
26201 }
26202
26203 /* Parse a syntactic requirement or type requirement.
26204
26205 requirement:
26206 simple-requirement
26207 compound-requirement
26208 type-requirement
26209 nested-requirement */
26210 static tree
26211 cp_parser_requirement (cp_parser *parser)
26212 {
26213 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26214 return cp_parser_compound_requirement (parser);
26215 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26216 return cp_parser_type_requirement (parser);
26217 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26218 return cp_parser_nested_requirement (parser);
26219 else
26220 return cp_parser_simple_requirement (parser);
26221 }
26222
26223 /* Parse a simple requirement.
26224
26225 simple-requirement:
26226 expression ';' */
26227 static tree
26228 cp_parser_simple_requirement (cp_parser *parser)
26229 {
26230 tree expr = cp_parser_expression (parser, NULL, false, false);
26231 if (!expr || expr == error_mark_node)
26232 return error_mark_node;
26233
26234 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26235 return error_mark_node;
26236
26237 return finish_simple_requirement (expr);
26238 }
26239
26240 /* Parse a type requirement
26241
26242 type-requirement
26243 nested-name-specifier [opt] required-type-name ';'
26244
26245 required-type-name:
26246 type-name
26247 'template' [opt] simple-template-id */
26248 static tree
26249 cp_parser_type_requirement (cp_parser *parser)
26250 {
26251 cp_lexer_consume_token (parser->lexer);
26252
26253 // Save the scope before parsing name specifiers.
26254 tree saved_scope = parser->scope;
26255 tree saved_object_scope = parser->object_scope;
26256 tree saved_qualifying_scope = parser->qualifying_scope;
26257 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26258 cp_parser_nested_name_specifier_opt (parser,
26259 /*typename_keyword_p=*/true,
26260 /*check_dependency_p=*/false,
26261 /*type_p=*/true,
26262 /*is_declaration=*/false);
26263
26264 tree type;
26265 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26266 {
26267 cp_lexer_consume_token (parser->lexer);
26268 type = cp_parser_template_id (parser,
26269 /*template_keyword_p=*/true,
26270 /*check_dependency=*/false,
26271 /*tag_type=*/none_type,
26272 /*is_declaration=*/false);
26273 type = make_typename_type (parser->scope, type, typename_type,
26274 /*complain=*/tf_error);
26275 }
26276 else
26277 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26278
26279 if (TREE_CODE (type) == TYPE_DECL)
26280 type = TREE_TYPE (type);
26281
26282 parser->scope = saved_scope;
26283 parser->object_scope = saved_object_scope;
26284 parser->qualifying_scope = saved_qualifying_scope;
26285
26286 if (type == error_mark_node)
26287 cp_parser_skip_to_end_of_statement (parser);
26288
26289 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26290 return error_mark_node;
26291 if (type == error_mark_node)
26292 return error_mark_node;
26293
26294 return finish_type_requirement (type);
26295 }
26296
26297 /* Parse a compound requirement
26298
26299 compound-requirement:
26300 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26301 static tree
26302 cp_parser_compound_requirement (cp_parser *parser)
26303 {
26304 /* Parse an expression enclosed in '{ }'s. */
26305 matching_braces braces;
26306 if (!braces.require_open (parser))
26307 return error_mark_node;
26308
26309 tree expr = cp_parser_expression (parser, NULL, false, false);
26310 if (!expr || expr == error_mark_node)
26311 return error_mark_node;
26312
26313 if (!braces.require_close (parser))
26314 return error_mark_node;
26315
26316 /* Parse the optional noexcept. */
26317 bool noexcept_p = false;
26318 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26319 {
26320 cp_lexer_consume_token (parser->lexer);
26321 noexcept_p = true;
26322 }
26323
26324 /* Parse the optional trailing return type. */
26325 tree type = NULL_TREE;
26326 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26327 {
26328 cp_lexer_consume_token (parser->lexer);
26329 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26330 parser->in_result_type_constraint_p = true;
26331 type = cp_parser_trailing_type_id (parser);
26332 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26333 if (type == error_mark_node)
26334 return error_mark_node;
26335 }
26336
26337 return finish_compound_requirement (expr, type, noexcept_p);
26338 }
26339
26340 /* Parse a nested requirement. This is the same as a requires clause.
26341
26342 nested-requirement:
26343 requires-clause */
26344 static tree
26345 cp_parser_nested_requirement (cp_parser *parser)
26346 {
26347 cp_lexer_consume_token (parser->lexer);
26348 tree req = cp_parser_requires_clause (parser);
26349 if (req == error_mark_node)
26350 return error_mark_node;
26351 return finish_nested_requirement (req);
26352 }
26353
26354 /* Support Functions */
26355
26356 /* Return the appropriate prefer_type argument for lookup_name_real based on
26357 tag_type and template_mem_access. */
26358
26359 static inline int
26360 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26361 {
26362 /* DR 141: When looking in the current enclosing context for a template-name
26363 after -> or ., only consider class templates. */
26364 if (template_mem_access)
26365 return 2;
26366 switch (tag_type)
26367 {
26368 case none_type: return 0; // No preference.
26369 case scope_type: return 1; // Type or namespace.
26370 default: return 2; // Type only.
26371 }
26372 }
26373
26374 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26375 NAME should have one of the representations used for an
26376 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26377 is returned. If PARSER->SCOPE is a dependent type, then a
26378 SCOPE_REF is returned.
26379
26380 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26381 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26382 was formed. Abstractly, such entities should not be passed to this
26383 function, because they do not need to be looked up, but it is
26384 simpler to check for this special case here, rather than at the
26385 call-sites.
26386
26387 In cases not explicitly covered above, this function returns a
26388 DECL, OVERLOAD, or baselink representing the result of the lookup.
26389 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26390 is returned.
26391
26392 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26393 (e.g., "struct") that was used. In that case bindings that do not
26394 refer to types are ignored.
26395
26396 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26397 ignored.
26398
26399 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26400 are ignored.
26401
26402 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26403 types.
26404
26405 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26406 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26407 NULL_TREE otherwise. */
26408
26409 static cp_expr
26410 cp_parser_lookup_name (cp_parser *parser, tree name,
26411 enum tag_types tag_type,
26412 bool is_template,
26413 bool is_namespace,
26414 bool check_dependency,
26415 tree *ambiguous_decls,
26416 location_t name_location)
26417 {
26418 tree decl;
26419 tree object_type = parser->context->object_type;
26420
26421 /* Assume that the lookup will be unambiguous. */
26422 if (ambiguous_decls)
26423 *ambiguous_decls = NULL_TREE;
26424
26425 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26426 no longer valid. Note that if we are parsing tentatively, and
26427 the parse fails, OBJECT_TYPE will be automatically restored. */
26428 parser->context->object_type = NULL_TREE;
26429
26430 if (name == error_mark_node)
26431 return error_mark_node;
26432
26433 /* A template-id has already been resolved; there is no lookup to
26434 do. */
26435 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26436 return name;
26437 if (BASELINK_P (name))
26438 {
26439 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26440 == TEMPLATE_ID_EXPR);
26441 return name;
26442 }
26443
26444 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26445 it should already have been checked to make sure that the name
26446 used matches the type being destroyed. */
26447 if (TREE_CODE (name) == BIT_NOT_EXPR)
26448 {
26449 tree type;
26450
26451 /* Figure out to which type this destructor applies. */
26452 if (parser->scope)
26453 type = parser->scope;
26454 else if (object_type)
26455 type = object_type;
26456 else
26457 type = current_class_type;
26458 /* If that's not a class type, there is no destructor. */
26459 if (!type || !CLASS_TYPE_P (type))
26460 return error_mark_node;
26461
26462 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26463 lazily_declare_fn (sfk_destructor, type);
26464
26465 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26466 return dtor;
26467
26468 return error_mark_node;
26469 }
26470
26471 /* By this point, the NAME should be an ordinary identifier. If
26472 the id-expression was a qualified name, the qualifying scope is
26473 stored in PARSER->SCOPE at this point. */
26474 gcc_assert (identifier_p (name));
26475
26476 /* Perform the lookup. */
26477 if (parser->scope)
26478 {
26479 bool dependent_p;
26480
26481 if (parser->scope == error_mark_node)
26482 return error_mark_node;
26483
26484 /* If the SCOPE is dependent, the lookup must be deferred until
26485 the template is instantiated -- unless we are explicitly
26486 looking up names in uninstantiated templates. Even then, we
26487 cannot look up the name if the scope is not a class type; it
26488 might, for example, be a template type parameter. */
26489 dependent_p = (TYPE_P (parser->scope)
26490 && dependent_scope_p (parser->scope));
26491 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26492 && dependent_p)
26493 /* Defer lookup. */
26494 decl = error_mark_node;
26495 else
26496 {
26497 tree pushed_scope = NULL_TREE;
26498
26499 /* If PARSER->SCOPE is a dependent type, then it must be a
26500 class type, and we must not be checking dependencies;
26501 otherwise, we would have processed this lookup above. So
26502 that PARSER->SCOPE is not considered a dependent base by
26503 lookup_member, we must enter the scope here. */
26504 if (dependent_p)
26505 pushed_scope = push_scope (parser->scope);
26506
26507 /* If the PARSER->SCOPE is a template specialization, it
26508 may be instantiated during name lookup. In that case,
26509 errors may be issued. Even if we rollback the current
26510 tentative parse, those errors are valid. */
26511 decl = lookup_qualified_name (parser->scope, name,
26512 prefer_type_arg (tag_type),
26513 /*complain=*/true);
26514
26515 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26516 lookup result and the nested-name-specifier nominates a class C:
26517 * if the name specified after the nested-name-specifier, when
26518 looked up in C, is the injected-class-name of C (Clause 9), or
26519 * if the name specified after the nested-name-specifier is the
26520 same as the identifier or the simple-template-id's template-
26521 name in the last component of the nested-name-specifier,
26522 the name is instead considered to name the constructor of
26523 class C. [ Note: for example, the constructor is not an
26524 acceptable lookup result in an elaborated-type-specifier so
26525 the constructor would not be used in place of the
26526 injected-class-name. --end note ] Such a constructor name
26527 shall be used only in the declarator-id of a declaration that
26528 names a constructor or in a using-declaration. */
26529 if (tag_type == none_type
26530 && DECL_SELF_REFERENCE_P (decl)
26531 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26532 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26533 prefer_type_arg (tag_type),
26534 /*complain=*/true);
26535
26536 /* If we have a single function from a using decl, pull it out. */
26537 if (TREE_CODE (decl) == OVERLOAD
26538 && !really_overloaded_fn (decl))
26539 decl = OVL_FUNCTION (decl);
26540
26541 if (pushed_scope)
26542 pop_scope (pushed_scope);
26543 }
26544
26545 /* If the scope is a dependent type and either we deferred lookup or
26546 we did lookup but didn't find the name, rememeber the name. */
26547 if (decl == error_mark_node && TYPE_P (parser->scope)
26548 && dependent_type_p (parser->scope))
26549 {
26550 if (tag_type)
26551 {
26552 tree type;
26553
26554 /* The resolution to Core Issue 180 says that `struct
26555 A::B' should be considered a type-name, even if `A'
26556 is dependent. */
26557 type = make_typename_type (parser->scope, name, tag_type,
26558 /*complain=*/tf_error);
26559 if (type != error_mark_node)
26560 decl = TYPE_NAME (type);
26561 }
26562 else if (is_template
26563 && (cp_parser_next_token_ends_template_argument_p (parser)
26564 || cp_lexer_next_token_is (parser->lexer,
26565 CPP_CLOSE_PAREN)))
26566 decl = make_unbound_class_template (parser->scope,
26567 name, NULL_TREE,
26568 /*complain=*/tf_error);
26569 else
26570 decl = build_qualified_name (/*type=*/NULL_TREE,
26571 parser->scope, name,
26572 is_template);
26573 }
26574 parser->qualifying_scope = parser->scope;
26575 parser->object_scope = NULL_TREE;
26576 }
26577 else if (object_type)
26578 {
26579 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26580 OBJECT_TYPE is not a class. */
26581 if (CLASS_TYPE_P (object_type))
26582 /* If the OBJECT_TYPE is a template specialization, it may
26583 be instantiated during name lookup. In that case, errors
26584 may be issued. Even if we rollback the current tentative
26585 parse, those errors are valid. */
26586 decl = lookup_member (object_type,
26587 name,
26588 /*protect=*/0,
26589 prefer_type_arg (tag_type),
26590 tf_warning_or_error);
26591 else
26592 decl = NULL_TREE;
26593
26594 if (!decl)
26595 /* Look it up in the enclosing context. DR 141: When looking for a
26596 template-name after -> or ., only consider class templates. */
26597 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26598 /*nonclass=*/0,
26599 /*block_p=*/true, is_namespace, 0);
26600 if (object_type == unknown_type_node)
26601 /* The object is type-dependent, so we can't look anything up; we used
26602 this to get the DR 141 behavior. */
26603 object_type = NULL_TREE;
26604 parser->object_scope = object_type;
26605 parser->qualifying_scope = NULL_TREE;
26606 }
26607 else
26608 {
26609 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26610 /*nonclass=*/0,
26611 /*block_p=*/true, is_namespace, 0);
26612 parser->qualifying_scope = NULL_TREE;
26613 parser->object_scope = NULL_TREE;
26614 }
26615
26616 /* If the lookup failed, let our caller know. */
26617 if (!decl || decl == error_mark_node)
26618 return error_mark_node;
26619
26620 /* Pull out the template from an injected-class-name (or multiple). */
26621 if (is_template)
26622 decl = maybe_get_template_decl_from_type_decl (decl);
26623
26624 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26625 if (TREE_CODE (decl) == TREE_LIST)
26626 {
26627 if (ambiguous_decls)
26628 *ambiguous_decls = decl;
26629 /* The error message we have to print is too complicated for
26630 cp_parser_error, so we incorporate its actions directly. */
26631 if (!cp_parser_simulate_error (parser))
26632 {
26633 error_at (name_location, "reference to %qD is ambiguous",
26634 name);
26635 print_candidates (decl);
26636 }
26637 return error_mark_node;
26638 }
26639
26640 gcc_assert (DECL_P (decl)
26641 || TREE_CODE (decl) == OVERLOAD
26642 || TREE_CODE (decl) == SCOPE_REF
26643 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26644 || BASELINK_P (decl));
26645
26646 /* If we have resolved the name of a member declaration, check to
26647 see if the declaration is accessible. When the name resolves to
26648 set of overloaded functions, accessibility is checked when
26649 overload resolution is done.
26650
26651 During an explicit instantiation, access is not checked at all,
26652 as per [temp.explicit]. */
26653 if (DECL_P (decl))
26654 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26655
26656 maybe_record_typedef_use (decl);
26657
26658 return cp_expr (decl, name_location);
26659 }
26660
26661 /* Like cp_parser_lookup_name, but for use in the typical case where
26662 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26663 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26664
26665 static tree
26666 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26667 {
26668 return cp_parser_lookup_name (parser, name,
26669 none_type,
26670 /*is_template=*/false,
26671 /*is_namespace=*/false,
26672 /*check_dependency=*/true,
26673 /*ambiguous_decls=*/NULL,
26674 location);
26675 }
26676
26677 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26678 the current context, return the TYPE_DECL. If TAG_NAME_P is
26679 true, the DECL indicates the class being defined in a class-head,
26680 or declared in an elaborated-type-specifier.
26681
26682 Otherwise, return DECL. */
26683
26684 static tree
26685 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26686 {
26687 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26688 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26689
26690 struct A {
26691 template <typename T> struct B;
26692 };
26693
26694 template <typename T> struct A::B {};
26695
26696 Similarly, in an elaborated-type-specifier:
26697
26698 namespace N { struct X{}; }
26699
26700 struct A {
26701 template <typename T> friend struct N::X;
26702 };
26703
26704 However, if the DECL refers to a class type, and we are in
26705 the scope of the class, then the name lookup automatically
26706 finds the TYPE_DECL created by build_self_reference rather
26707 than a TEMPLATE_DECL. For example, in:
26708
26709 template <class T> struct S {
26710 S s;
26711 };
26712
26713 there is no need to handle such case. */
26714
26715 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26716 return DECL_TEMPLATE_RESULT (decl);
26717
26718 return decl;
26719 }
26720
26721 /* If too many, or too few, template-parameter lists apply to the
26722 declarator, issue an error message. Returns TRUE if all went well,
26723 and FALSE otherwise. */
26724
26725 static bool
26726 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26727 cp_declarator *declarator,
26728 location_t declarator_location)
26729 {
26730 switch (declarator->kind)
26731 {
26732 case cdk_id:
26733 {
26734 unsigned num_templates = 0;
26735 tree scope = declarator->u.id.qualifying_scope;
26736 bool template_id_p = false;
26737
26738 if (scope)
26739 num_templates = num_template_headers_for_class (scope);
26740 else if (TREE_CODE (declarator->u.id.unqualified_name)
26741 == TEMPLATE_ID_EXPR)
26742 {
26743 /* If the DECLARATOR has the form `X<y>' then it uses one
26744 additional level of template parameters. */
26745 ++num_templates;
26746 template_id_p = true;
26747 }
26748
26749 return cp_parser_check_template_parameters
26750 (parser, num_templates, template_id_p, declarator_location,
26751 declarator);
26752 }
26753
26754 case cdk_function:
26755 case cdk_array:
26756 case cdk_pointer:
26757 case cdk_reference:
26758 case cdk_ptrmem:
26759 return (cp_parser_check_declarator_template_parameters
26760 (parser, declarator->declarator, declarator_location));
26761
26762 case cdk_decomp:
26763 case cdk_error:
26764 return true;
26765
26766 default:
26767 gcc_unreachable ();
26768 }
26769 return false;
26770 }
26771
26772 /* NUM_TEMPLATES were used in the current declaration. If that is
26773 invalid, return FALSE and issue an error messages. Otherwise,
26774 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26775 declarator and we can print more accurate diagnostics. */
26776
26777 static bool
26778 cp_parser_check_template_parameters (cp_parser* parser,
26779 unsigned num_templates,
26780 bool template_id_p,
26781 location_t location,
26782 cp_declarator *declarator)
26783 {
26784 /* If there are the same number of template classes and parameter
26785 lists, that's OK. */
26786 if (parser->num_template_parameter_lists == num_templates)
26787 return true;
26788 /* If there are more, but only one more, and the name ends in an identifier,
26789 then we are declaring a primary template. That's OK too. */
26790 if (!template_id_p
26791 && parser->num_template_parameter_lists == num_templates + 1)
26792 return true;
26793 /* If there are more template classes than parameter lists, we have
26794 something like:
26795
26796 template <class T> void S<T>::R<T>::f (); */
26797 if (parser->num_template_parameter_lists < num_templates)
26798 {
26799 if (declarator && !current_function_decl)
26800 error_at (location, "specializing member %<%T::%E%> "
26801 "requires %<template<>%> syntax",
26802 declarator->u.id.qualifying_scope,
26803 declarator->u.id.unqualified_name);
26804 else if (declarator)
26805 error_at (location, "invalid declaration of %<%T::%E%>",
26806 declarator->u.id.qualifying_scope,
26807 declarator->u.id.unqualified_name);
26808 else
26809 error_at (location, "too few template-parameter-lists");
26810 return false;
26811 }
26812 /* Otherwise, there are too many template parameter lists. We have
26813 something like:
26814
26815 template <class T> template <class U> void S::f(); */
26816 error_at (location, "too many template-parameter-lists");
26817 return false;
26818 }
26819
26820 /* Parse an optional `::' token indicating that the following name is
26821 from the global namespace. If so, PARSER->SCOPE is set to the
26822 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26823 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26824 Returns the new value of PARSER->SCOPE, if the `::' token is
26825 present, and NULL_TREE otherwise. */
26826
26827 static tree
26828 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26829 {
26830 cp_token *token;
26831
26832 /* Peek at the next token. */
26833 token = cp_lexer_peek_token (parser->lexer);
26834 /* If we're looking at a `::' token then we're starting from the
26835 global namespace, not our current location. */
26836 if (token->type == CPP_SCOPE)
26837 {
26838 /* Consume the `::' token. */
26839 cp_lexer_consume_token (parser->lexer);
26840 /* Set the SCOPE so that we know where to start the lookup. */
26841 parser->scope = global_namespace;
26842 parser->qualifying_scope = global_namespace;
26843 parser->object_scope = NULL_TREE;
26844
26845 return parser->scope;
26846 }
26847 else if (!current_scope_valid_p)
26848 {
26849 parser->scope = NULL_TREE;
26850 parser->qualifying_scope = NULL_TREE;
26851 parser->object_scope = NULL_TREE;
26852 }
26853
26854 return NULL_TREE;
26855 }
26856
26857 /* Returns TRUE if the upcoming token sequence is the start of a
26858 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26859 declarator is preceded by the `friend' specifier. */
26860
26861 static bool
26862 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26863 {
26864 bool constructor_p;
26865 bool outside_class_specifier_p;
26866 tree nested_name_specifier;
26867 cp_token *next_token;
26868
26869 /* The common case is that this is not a constructor declarator, so
26870 try to avoid doing lots of work if at all possible. It's not
26871 valid declare a constructor at function scope. */
26872 if (parser->in_function_body)
26873 return false;
26874 /* And only certain tokens can begin a constructor declarator. */
26875 next_token = cp_lexer_peek_token (parser->lexer);
26876 if (next_token->type != CPP_NAME
26877 && next_token->type != CPP_SCOPE
26878 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26879 && next_token->type != CPP_TEMPLATE_ID)
26880 return false;
26881
26882 /* Parse tentatively; we are going to roll back all of the tokens
26883 consumed here. */
26884 cp_parser_parse_tentatively (parser);
26885 /* Assume that we are looking at a constructor declarator. */
26886 constructor_p = true;
26887
26888 /* Look for the optional `::' operator. */
26889 cp_parser_global_scope_opt (parser,
26890 /*current_scope_valid_p=*/false);
26891 /* Look for the nested-name-specifier. */
26892 nested_name_specifier
26893 = (cp_parser_nested_name_specifier_opt (parser,
26894 /*typename_keyword_p=*/false,
26895 /*check_dependency_p=*/false,
26896 /*type_p=*/false,
26897 /*is_declaration=*/false));
26898
26899 outside_class_specifier_p = (!at_class_scope_p ()
26900 || !TYPE_BEING_DEFINED (current_class_type)
26901 || friend_p);
26902
26903 /* Outside of a class-specifier, there must be a
26904 nested-name-specifier. Except in C++17 mode, where we
26905 might be declaring a guiding declaration. */
26906 if (!nested_name_specifier && outside_class_specifier_p
26907 && cxx_dialect < cxx17)
26908 constructor_p = false;
26909 else if (nested_name_specifier == error_mark_node)
26910 constructor_p = false;
26911
26912 /* If we have a class scope, this is easy; DR 147 says that S::S always
26913 names the constructor, and no other qualified name could. */
26914 if (constructor_p && nested_name_specifier
26915 && CLASS_TYPE_P (nested_name_specifier))
26916 {
26917 tree id = cp_parser_unqualified_id (parser,
26918 /*template_keyword_p=*/false,
26919 /*check_dependency_p=*/false,
26920 /*declarator_p=*/true,
26921 /*optional_p=*/false);
26922 if (is_overloaded_fn (id))
26923 id = DECL_NAME (get_first_fn (id));
26924 if (!constructor_name_p (id, nested_name_specifier))
26925 constructor_p = false;
26926 }
26927 /* If we still think that this might be a constructor-declarator,
26928 look for a class-name. */
26929 else if (constructor_p)
26930 {
26931 /* If we have:
26932
26933 template <typename T> struct S {
26934 S();
26935 };
26936
26937 we must recognize that the nested `S' names a class. */
26938 if (cxx_dialect >= cxx17)
26939 cp_parser_parse_tentatively (parser);
26940
26941 tree type_decl;
26942 type_decl = cp_parser_class_name (parser,
26943 /*typename_keyword_p=*/false,
26944 /*template_keyword_p=*/false,
26945 none_type,
26946 /*check_dependency_p=*/false,
26947 /*class_head_p=*/false,
26948 /*is_declaration=*/false);
26949
26950 if (cxx_dialect >= cxx17
26951 && !cp_parser_parse_definitely (parser))
26952 {
26953 type_decl = NULL_TREE;
26954 tree tmpl = cp_parser_template_name (parser,
26955 /*template_keyword*/false,
26956 /*check_dependency_p*/false,
26957 /*is_declaration*/false,
26958 none_type,
26959 /*is_identifier*/NULL);
26960 if (DECL_CLASS_TEMPLATE_P (tmpl)
26961 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26962 /* It's a deduction guide, return true. */;
26963 else
26964 cp_parser_simulate_error (parser);
26965 }
26966
26967 /* If there was no class-name, then this is not a constructor.
26968 Otherwise, if we are in a class-specifier and we aren't
26969 handling a friend declaration, check that its type matches
26970 current_class_type (c++/38313). Note: error_mark_node
26971 is left alone for error recovery purposes. */
26972 constructor_p = (!cp_parser_error_occurred (parser)
26973 && (outside_class_specifier_p
26974 || type_decl == NULL_TREE
26975 || type_decl == error_mark_node
26976 || same_type_p (current_class_type,
26977 TREE_TYPE (type_decl))));
26978
26979 /* If we're still considering a constructor, we have to see a `(',
26980 to begin the parameter-declaration-clause, followed by either a
26981 `)', an `...', or a decl-specifier. We need to check for a
26982 type-specifier to avoid being fooled into thinking that:
26983
26984 S (f) (int);
26985
26986 is a constructor. (It is actually a function named `f' that
26987 takes one parameter (of type `int') and returns a value of type
26988 `S'. */
26989 if (constructor_p
26990 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26991 constructor_p = false;
26992
26993 if (constructor_p
26994 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26995 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26996 /* A parameter declaration begins with a decl-specifier,
26997 which is either the "attribute" keyword, a storage class
26998 specifier, or (usually) a type-specifier. */
26999 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27000 {
27001 tree type;
27002 tree pushed_scope = NULL_TREE;
27003 unsigned saved_num_template_parameter_lists;
27004
27005 /* Names appearing in the type-specifier should be looked up
27006 in the scope of the class. */
27007 if (current_class_type)
27008 type = NULL_TREE;
27009 else if (type_decl)
27010 {
27011 type = TREE_TYPE (type_decl);
27012 if (TREE_CODE (type) == TYPENAME_TYPE)
27013 {
27014 type = resolve_typename_type (type,
27015 /*only_current_p=*/false);
27016 if (TREE_CODE (type) == TYPENAME_TYPE)
27017 {
27018 cp_parser_abort_tentative_parse (parser);
27019 return false;
27020 }
27021 }
27022 pushed_scope = push_scope (type);
27023 }
27024
27025 /* Inside the constructor parameter list, surrounding
27026 template-parameter-lists do not apply. */
27027 saved_num_template_parameter_lists
27028 = parser->num_template_parameter_lists;
27029 parser->num_template_parameter_lists = 0;
27030
27031 /* Look for the type-specifier. */
27032 cp_parser_type_specifier (parser,
27033 CP_PARSER_FLAGS_NONE,
27034 /*decl_specs=*/NULL,
27035 /*is_declarator=*/true,
27036 /*declares_class_or_enum=*/NULL,
27037 /*is_cv_qualifier=*/NULL);
27038
27039 parser->num_template_parameter_lists
27040 = saved_num_template_parameter_lists;
27041
27042 /* Leave the scope of the class. */
27043 if (pushed_scope)
27044 pop_scope (pushed_scope);
27045
27046 constructor_p = !cp_parser_error_occurred (parser);
27047 }
27048 }
27049
27050 /* We did not really want to consume any tokens. */
27051 cp_parser_abort_tentative_parse (parser);
27052
27053 return constructor_p;
27054 }
27055
27056 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27057 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27058 they must be performed once we are in the scope of the function.
27059
27060 Returns the function defined. */
27061
27062 static tree
27063 cp_parser_function_definition_from_specifiers_and_declarator
27064 (cp_parser* parser,
27065 cp_decl_specifier_seq *decl_specifiers,
27066 tree attributes,
27067 const cp_declarator *declarator)
27068 {
27069 tree fn;
27070 bool success_p;
27071
27072 /* Begin the function-definition. */
27073 success_p = start_function (decl_specifiers, declarator, attributes);
27074
27075 /* The things we're about to see are not directly qualified by any
27076 template headers we've seen thus far. */
27077 reset_specialization ();
27078
27079 /* If there were names looked up in the decl-specifier-seq that we
27080 did not check, check them now. We must wait until we are in the
27081 scope of the function to perform the checks, since the function
27082 might be a friend. */
27083 perform_deferred_access_checks (tf_warning_or_error);
27084
27085 if (success_p)
27086 {
27087 cp_finalize_omp_declare_simd (parser, current_function_decl);
27088 parser->omp_declare_simd = NULL;
27089 cp_finalize_oacc_routine (parser, current_function_decl, true);
27090 parser->oacc_routine = NULL;
27091 }
27092
27093 if (!success_p)
27094 {
27095 /* Skip the entire function. */
27096 cp_parser_skip_to_end_of_block_or_statement (parser);
27097 fn = error_mark_node;
27098 }
27099 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27100 {
27101 /* Seen already, skip it. An error message has already been output. */
27102 cp_parser_skip_to_end_of_block_or_statement (parser);
27103 fn = current_function_decl;
27104 current_function_decl = NULL_TREE;
27105 /* If this is a function from a class, pop the nested class. */
27106 if (current_class_name)
27107 pop_nested_class ();
27108 }
27109 else
27110 {
27111 timevar_id_t tv;
27112 if (DECL_DECLARED_INLINE_P (current_function_decl))
27113 tv = TV_PARSE_INLINE;
27114 else
27115 tv = TV_PARSE_FUNC;
27116 timevar_push (tv);
27117 fn = cp_parser_function_definition_after_declarator (parser,
27118 /*inline_p=*/false);
27119 timevar_pop (tv);
27120 }
27121
27122 return fn;
27123 }
27124
27125 /* Parse the part of a function-definition that follows the
27126 declarator. INLINE_P is TRUE iff this function is an inline
27127 function defined within a class-specifier.
27128
27129 Returns the function defined. */
27130
27131 static tree
27132 cp_parser_function_definition_after_declarator (cp_parser* parser,
27133 bool inline_p)
27134 {
27135 tree fn;
27136 bool saved_in_unbraced_linkage_specification_p;
27137 bool saved_in_function_body;
27138 unsigned saved_num_template_parameter_lists;
27139 cp_token *token;
27140 bool fully_implicit_function_template_p
27141 = parser->fully_implicit_function_template_p;
27142 parser->fully_implicit_function_template_p = false;
27143 tree implicit_template_parms
27144 = parser->implicit_template_parms;
27145 parser->implicit_template_parms = 0;
27146 cp_binding_level* implicit_template_scope
27147 = parser->implicit_template_scope;
27148 parser->implicit_template_scope = 0;
27149
27150 saved_in_function_body = parser->in_function_body;
27151 parser->in_function_body = true;
27152 /* If the next token is `return', then the code may be trying to
27153 make use of the "named return value" extension that G++ used to
27154 support. */
27155 token = cp_lexer_peek_token (parser->lexer);
27156 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27157 {
27158 /* Consume the `return' keyword. */
27159 cp_lexer_consume_token (parser->lexer);
27160 /* Look for the identifier that indicates what value is to be
27161 returned. */
27162 cp_parser_identifier (parser);
27163 /* Issue an error message. */
27164 error_at (token->location,
27165 "named return values are no longer supported");
27166 /* Skip tokens until we reach the start of the function body. */
27167 while (true)
27168 {
27169 cp_token *token = cp_lexer_peek_token (parser->lexer);
27170 if (token->type == CPP_OPEN_BRACE
27171 || token->type == CPP_EOF
27172 || token->type == CPP_PRAGMA_EOL)
27173 break;
27174 cp_lexer_consume_token (parser->lexer);
27175 }
27176 }
27177 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27178 anything declared inside `f'. */
27179 saved_in_unbraced_linkage_specification_p
27180 = parser->in_unbraced_linkage_specification_p;
27181 parser->in_unbraced_linkage_specification_p = false;
27182 /* Inside the function, surrounding template-parameter-lists do not
27183 apply. */
27184 saved_num_template_parameter_lists
27185 = parser->num_template_parameter_lists;
27186 parser->num_template_parameter_lists = 0;
27187
27188 /* If the next token is `try', `__transaction_atomic', or
27189 `__transaction_relaxed`, then we are looking at either function-try-block
27190 or function-transaction-block. Note that all of these include the
27191 function-body. */
27192 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27193 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27194 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27195 RID_TRANSACTION_RELAXED))
27196 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27197 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27198 cp_parser_function_try_block (parser);
27199 else
27200 cp_parser_ctor_initializer_opt_and_function_body
27201 (parser, /*in_function_try_block=*/false);
27202
27203 /* Finish the function. */
27204 fn = finish_function (inline_p);
27205 /* Generate code for it, if necessary. */
27206 expand_or_defer_fn (fn);
27207 /* Restore the saved values. */
27208 parser->in_unbraced_linkage_specification_p
27209 = saved_in_unbraced_linkage_specification_p;
27210 parser->num_template_parameter_lists
27211 = saved_num_template_parameter_lists;
27212 parser->in_function_body = saved_in_function_body;
27213
27214 parser->fully_implicit_function_template_p
27215 = fully_implicit_function_template_p;
27216 parser->implicit_template_parms
27217 = implicit_template_parms;
27218 parser->implicit_template_scope
27219 = implicit_template_scope;
27220
27221 if (parser->fully_implicit_function_template_p)
27222 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27223
27224 return fn;
27225 }
27226
27227 /* Parse a template-declaration body (following argument list). */
27228
27229 static void
27230 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27231 tree parameter_list,
27232 bool member_p)
27233 {
27234 tree decl = NULL_TREE;
27235 bool friend_p = false;
27236
27237 /* We just processed one more parameter list. */
27238 ++parser->num_template_parameter_lists;
27239
27240 /* Get the deferred access checks from the parameter list. These
27241 will be checked once we know what is being declared, as for a
27242 member template the checks must be performed in the scope of the
27243 class containing the member. */
27244 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27245
27246 /* Tentatively parse for a new template parameter list, which can either be
27247 the template keyword or a template introduction. */
27248 if (cp_parser_template_declaration_after_export (parser, member_p))
27249 /* OK */;
27250 else if (cxx_dialect >= cxx11
27251 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27252 decl = cp_parser_alias_declaration (parser);
27253 else
27254 {
27255 /* There are no access checks when parsing a template, as we do not
27256 know if a specialization will be a friend. */
27257 push_deferring_access_checks (dk_no_check);
27258 cp_token *token = cp_lexer_peek_token (parser->lexer);
27259 decl = cp_parser_single_declaration (parser,
27260 checks,
27261 member_p,
27262 /*explicit_specialization_p=*/false,
27263 &friend_p);
27264 pop_deferring_access_checks ();
27265
27266 /* If this is a member template declaration, let the front
27267 end know. */
27268 if (member_p && !friend_p && decl)
27269 {
27270 if (TREE_CODE (decl) == TYPE_DECL)
27271 cp_parser_check_access_in_redeclaration (decl, token->location);
27272
27273 decl = finish_member_template_decl (decl);
27274 }
27275 else if (friend_p && decl
27276 && DECL_DECLARES_TYPE_P (decl))
27277 make_friend_class (current_class_type, TREE_TYPE (decl),
27278 /*complain=*/true);
27279 }
27280 /* We are done with the current parameter list. */
27281 --parser->num_template_parameter_lists;
27282
27283 pop_deferring_access_checks ();
27284
27285 /* Finish up. */
27286 finish_template_decl (parameter_list);
27287
27288 /* Check the template arguments for a literal operator template. */
27289 if (decl
27290 && DECL_DECLARES_FUNCTION_P (decl)
27291 && UDLIT_OPER_P (DECL_NAME (decl)))
27292 {
27293 bool ok = true;
27294 if (parameter_list == NULL_TREE)
27295 ok = false;
27296 else
27297 {
27298 int num_parms = TREE_VEC_LENGTH (parameter_list);
27299 if (num_parms == 1)
27300 {
27301 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27302 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27303 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27304 /* OK, C++20 string literal operator template. We don't need
27305 to warn in lower dialects here because we will have already
27306 warned about the template parameter. */;
27307 else if (TREE_TYPE (parm) != char_type_node
27308 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27309 ok = false;
27310 }
27311 else if (num_parms == 2 && cxx_dialect >= cxx14)
27312 {
27313 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27314 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27315 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27316 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27317 if (parm == error_mark_node
27318 || TREE_TYPE (parm) != TREE_TYPE (type)
27319 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27320 ok = false;
27321 else
27322 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27323 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27324 "ISO C++ did not adopt string literal operator templa"
27325 "tes taking an argument pack of characters");
27326 }
27327 else
27328 ok = false;
27329 }
27330 if (!ok)
27331 {
27332 if (cxx_dialect > cxx17)
27333 error ("literal operator template %qD has invalid parameter list;"
27334 " Expected non-type template parameter pack <char...> "
27335 " or single non-type parameter of class type",
27336 decl);
27337 else
27338 error ("literal operator template %qD has invalid parameter list."
27339 " Expected non-type template parameter pack <char...>",
27340 decl);
27341 }
27342 }
27343
27344 /* Register member declarations. */
27345 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27346 finish_member_declaration (decl);
27347 /* If DECL is a function template, we must return to parse it later.
27348 (Even though there is no definition, there might be default
27349 arguments that need handling.) */
27350 if (member_p && decl
27351 && DECL_DECLARES_FUNCTION_P (decl))
27352 vec_safe_push (unparsed_funs_with_definitions, decl);
27353 }
27354
27355 /* Parse a template introduction header for a template-declaration. Returns
27356 false if tentative parse fails. */
27357
27358 static bool
27359 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27360 {
27361 cp_parser_parse_tentatively (parser);
27362
27363 tree saved_scope = parser->scope;
27364 tree saved_object_scope = parser->object_scope;
27365 tree saved_qualifying_scope = parser->qualifying_scope;
27366
27367 /* Look for the optional `::' operator. */
27368 cp_parser_global_scope_opt (parser,
27369 /*current_scope_valid_p=*/false);
27370 /* Look for the nested-name-specifier. */
27371 cp_parser_nested_name_specifier_opt (parser,
27372 /*typename_keyword_p=*/false,
27373 /*check_dependency_p=*/true,
27374 /*type_p=*/false,
27375 /*is_declaration=*/false);
27376
27377 cp_token *token = cp_lexer_peek_token (parser->lexer);
27378 tree concept_name = cp_parser_identifier (parser);
27379
27380 /* Look up the concept for which we will be matching
27381 template parameters. */
27382 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27383 token->location);
27384 parser->scope = saved_scope;
27385 parser->object_scope = saved_object_scope;
27386 parser->qualifying_scope = saved_qualifying_scope;
27387
27388 if (concept_name == error_mark_node)
27389 cp_parser_simulate_error (parser);
27390
27391 /* Look for opening brace for introduction. */
27392 matching_braces braces;
27393 braces.require_open (parser);
27394
27395 if (!cp_parser_parse_definitely (parser))
27396 return false;
27397
27398 push_deferring_access_checks (dk_deferred);
27399
27400 /* Build vector of placeholder parameters and grab
27401 matching identifiers. */
27402 tree introduction_list = cp_parser_introduction_list (parser);
27403
27404 /* Look for closing brace for introduction. */
27405 if (!braces.require_close (parser))
27406 return true;
27407
27408 /* The introduction-list shall not be empty. */
27409 int nargs = TREE_VEC_LENGTH (introduction_list);
27410 if (nargs == 0)
27411 {
27412 /* In cp_parser_introduction_list we have already issued an error. */
27413 return true;
27414 }
27415
27416 if (tmpl_decl == error_mark_node)
27417 {
27418 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27419 token->location);
27420 return true;
27421 }
27422
27423 /* Build and associate the constraint. */
27424 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27425 if (parms && parms != error_mark_node)
27426 {
27427 cp_parser_template_declaration_after_parameters (parser, parms,
27428 member_p);
27429 return true;
27430 }
27431
27432 error_at (token->location, "no matching concept for template-introduction");
27433 return true;
27434 }
27435
27436 /* Parse a normal template-declaration following the template keyword. */
27437
27438 static void
27439 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27440 {
27441 tree parameter_list;
27442 bool need_lang_pop;
27443 location_t location = input_location;
27444
27445 /* Look for the `<' token. */
27446 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27447 return;
27448 if (at_class_scope_p () && current_function_decl)
27449 {
27450 /* 14.5.2.2 [temp.mem]
27451
27452 A local class shall not have member templates. */
27453 error_at (location,
27454 "invalid declaration of member template in local class");
27455 cp_parser_skip_to_end_of_block_or_statement (parser);
27456 return;
27457 }
27458 /* [temp]
27459
27460 A template ... shall not have C linkage. */
27461 if (current_lang_name == lang_name_c)
27462 {
27463 error_at (location, "template with C linkage");
27464 maybe_show_extern_c_location ();
27465 /* Give it C++ linkage to avoid confusing other parts of the
27466 front end. */
27467 push_lang_context (lang_name_cplusplus);
27468 need_lang_pop = true;
27469 }
27470 else
27471 need_lang_pop = false;
27472
27473 /* We cannot perform access checks on the template parameter
27474 declarations until we know what is being declared, just as we
27475 cannot check the decl-specifier list. */
27476 push_deferring_access_checks (dk_deferred);
27477
27478 /* If the next token is `>', then we have an invalid
27479 specialization. Rather than complain about an invalid template
27480 parameter, issue an error message here. */
27481 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27482 {
27483 cp_parser_error (parser, "invalid explicit specialization");
27484 begin_specialization ();
27485 parameter_list = NULL_TREE;
27486 }
27487 else
27488 {
27489 /* Parse the template parameters. */
27490 parameter_list = cp_parser_template_parameter_list (parser);
27491 }
27492
27493 /* Look for the `>'. */
27494 cp_parser_skip_to_end_of_template_parameter_list (parser);
27495
27496 /* Manage template requirements */
27497 if (flag_concepts)
27498 {
27499 tree reqs = get_shorthand_constraints (current_template_parms);
27500 if (tree r = cp_parser_requires_clause_opt (parser))
27501 reqs = conjoin_constraints (reqs, normalize_expression (r));
27502 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27503 }
27504
27505 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27506 member_p);
27507
27508 /* For the erroneous case of a template with C linkage, we pushed an
27509 implicit C++ linkage scope; exit that scope now. */
27510 if (need_lang_pop)
27511 pop_lang_context ();
27512 }
27513
27514 /* Parse a template-declaration, assuming that the `export' (and
27515 `extern') keywords, if present, has already been scanned. MEMBER_P
27516 is as for cp_parser_template_declaration. */
27517
27518 static bool
27519 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27520 {
27521 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27522 {
27523 cp_lexer_consume_token (parser->lexer);
27524 cp_parser_explicit_template_declaration (parser, member_p);
27525 return true;
27526 }
27527 else if (flag_concepts)
27528 return cp_parser_template_introduction (parser, member_p);
27529
27530 return false;
27531 }
27532
27533 /* Perform the deferred access checks from a template-parameter-list.
27534 CHECKS is a TREE_LIST of access checks, as returned by
27535 get_deferred_access_checks. */
27536
27537 static void
27538 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27539 {
27540 ++processing_template_parmlist;
27541 perform_access_checks (checks, tf_warning_or_error);
27542 --processing_template_parmlist;
27543 }
27544
27545 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27546 `function-definition' sequence that follows a template header.
27547 If MEMBER_P is true, this declaration appears in a class scope.
27548
27549 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27550 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27551
27552 static tree
27553 cp_parser_single_declaration (cp_parser* parser,
27554 vec<deferred_access_check, va_gc> *checks,
27555 bool member_p,
27556 bool explicit_specialization_p,
27557 bool* friend_p)
27558 {
27559 int declares_class_or_enum;
27560 tree decl = NULL_TREE;
27561 cp_decl_specifier_seq decl_specifiers;
27562 bool function_definition_p = false;
27563 cp_token *decl_spec_token_start;
27564
27565 /* This function is only used when processing a template
27566 declaration. */
27567 gcc_assert (innermost_scope_kind () == sk_template_parms
27568 || innermost_scope_kind () == sk_template_spec);
27569
27570 /* Defer access checks until we know what is being declared. */
27571 push_deferring_access_checks (dk_deferred);
27572
27573 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27574 alternative. */
27575 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27576 cp_parser_decl_specifier_seq (parser,
27577 CP_PARSER_FLAGS_OPTIONAL,
27578 &decl_specifiers,
27579 &declares_class_or_enum);
27580 if (friend_p)
27581 *friend_p = cp_parser_friend_p (&decl_specifiers);
27582
27583 /* There are no template typedefs. */
27584 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27585 {
27586 error_at (decl_spec_token_start->location,
27587 "template declaration of %<typedef%>");
27588 decl = error_mark_node;
27589 }
27590
27591 /* Gather up the access checks that occurred the
27592 decl-specifier-seq. */
27593 stop_deferring_access_checks ();
27594
27595 /* Check for the declaration of a template class. */
27596 if (declares_class_or_enum)
27597 {
27598 if (cp_parser_declares_only_class_p (parser)
27599 || (declares_class_or_enum & 2))
27600 {
27601 // If this is a declaration, but not a definition, associate
27602 // any constraints with the type declaration. Constraints
27603 // are associated with definitions in cp_parser_class_specifier.
27604 if (declares_class_or_enum == 1)
27605 associate_classtype_constraints (decl_specifiers.type);
27606
27607 decl = shadow_tag (&decl_specifiers);
27608
27609 /* In this case:
27610
27611 struct C {
27612 friend template <typename T> struct A<T>::B;
27613 };
27614
27615 A<T>::B will be represented by a TYPENAME_TYPE, and
27616 therefore not recognized by shadow_tag. */
27617 if (friend_p && *friend_p
27618 && !decl
27619 && decl_specifiers.type
27620 && TYPE_P (decl_specifiers.type))
27621 decl = decl_specifiers.type;
27622
27623 if (decl && decl != error_mark_node)
27624 decl = TYPE_NAME (decl);
27625 else
27626 decl = error_mark_node;
27627
27628 /* Perform access checks for template parameters. */
27629 cp_parser_perform_template_parameter_access_checks (checks);
27630
27631 /* Give a helpful diagnostic for
27632 template <class T> struct A { } a;
27633 if we aren't already recovering from an error. */
27634 if (!cp_parser_declares_only_class_p (parser)
27635 && !seen_error ())
27636 {
27637 error_at (cp_lexer_peek_token (parser->lexer)->location,
27638 "a class template declaration must not declare "
27639 "anything else");
27640 cp_parser_skip_to_end_of_block_or_statement (parser);
27641 goto out;
27642 }
27643 }
27644 }
27645
27646 /* Complain about missing 'typename' or other invalid type names. */
27647 if (!decl_specifiers.any_type_specifiers_p
27648 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27649 {
27650 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27651 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27652 the rest of this declaration. */
27653 decl = error_mark_node;
27654 goto out;
27655 }
27656
27657 /* If it's not a template class, try for a template function. If
27658 the next token is a `;', then this declaration does not declare
27659 anything. But, if there were errors in the decl-specifiers, then
27660 the error might well have come from an attempted class-specifier.
27661 In that case, there's no need to warn about a missing declarator. */
27662 if (!decl
27663 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27664 || decl_specifiers.type != error_mark_node))
27665 {
27666 decl = cp_parser_init_declarator (parser,
27667 &decl_specifiers,
27668 checks,
27669 /*function_definition_allowed_p=*/true,
27670 member_p,
27671 declares_class_or_enum,
27672 &function_definition_p,
27673 NULL, NULL, NULL);
27674
27675 /* 7.1.1-1 [dcl.stc]
27676
27677 A storage-class-specifier shall not be specified in an explicit
27678 specialization... */
27679 if (decl
27680 && explicit_specialization_p
27681 && decl_specifiers.storage_class != sc_none)
27682 {
27683 error_at (decl_spec_token_start->location,
27684 "explicit template specialization cannot have a storage class");
27685 decl = error_mark_node;
27686 }
27687
27688 if (decl && VAR_P (decl))
27689 check_template_variable (decl);
27690 }
27691
27692 /* Look for a trailing `;' after the declaration. */
27693 if (!function_definition_p
27694 && (decl == error_mark_node
27695 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27696 cp_parser_skip_to_end_of_block_or_statement (parser);
27697
27698 out:
27699 pop_deferring_access_checks ();
27700
27701 /* Clear any current qualification; whatever comes next is the start
27702 of something new. */
27703 parser->scope = NULL_TREE;
27704 parser->qualifying_scope = NULL_TREE;
27705 parser->object_scope = NULL_TREE;
27706
27707 return decl;
27708 }
27709
27710 /* Parse a cast-expression that is not the operand of a unary "&". */
27711
27712 static cp_expr
27713 cp_parser_simple_cast_expression (cp_parser *parser)
27714 {
27715 return cp_parser_cast_expression (parser, /*address_p=*/false,
27716 /*cast_p=*/false, /*decltype*/false, NULL);
27717 }
27718
27719 /* Parse a functional cast to TYPE. Returns an expression
27720 representing the cast. */
27721
27722 static cp_expr
27723 cp_parser_functional_cast (cp_parser* parser, tree type)
27724 {
27725 vec<tree, va_gc> *vec;
27726 tree expression_list;
27727 cp_expr cast;
27728 bool nonconst_p;
27729
27730 location_t start_loc = input_location;
27731
27732 if (!type)
27733 type = error_mark_node;
27734
27735 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27736 {
27737 cp_lexer_set_source_position (parser->lexer);
27738 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27739 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27740 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27741 if (TREE_CODE (type) == TYPE_DECL)
27742 type = TREE_TYPE (type);
27743
27744 cast = finish_compound_literal (type, expression_list,
27745 tf_warning_or_error, fcl_functional);
27746 /* Create a location of the form:
27747 type_name{i, f}
27748 ^~~~~~~~~~~~~~~
27749 with caret == start at the start of the type name,
27750 finishing at the closing brace. */
27751 location_t finish_loc
27752 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27753 location_t combined_loc = make_location (start_loc, start_loc,
27754 finish_loc);
27755 cast.set_location (combined_loc);
27756 return cast;
27757 }
27758
27759
27760 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27761 /*cast_p=*/true,
27762 /*allow_expansion_p=*/true,
27763 /*non_constant_p=*/NULL);
27764 if (vec == NULL)
27765 expression_list = error_mark_node;
27766 else
27767 {
27768 expression_list = build_tree_list_vec (vec);
27769 release_tree_vector (vec);
27770 }
27771
27772 cast = build_functional_cast (type, expression_list,
27773 tf_warning_or_error);
27774 /* [expr.const]/1: In an integral constant expression "only type
27775 conversions to integral or enumeration type can be used". */
27776 if (TREE_CODE (type) == TYPE_DECL)
27777 type = TREE_TYPE (type);
27778 if (cast != error_mark_node
27779 && !cast_valid_in_integral_constant_expression_p (type)
27780 && cp_parser_non_integral_constant_expression (parser,
27781 NIC_CONSTRUCTOR))
27782 return error_mark_node;
27783
27784 /* Create a location of the form:
27785 float(i)
27786 ^~~~~~~~
27787 with caret == start at the start of the type name,
27788 finishing at the closing paren. */
27789 location_t finish_loc
27790 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27791 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27792 cast.set_location (combined_loc);
27793 return cast;
27794 }
27795
27796 /* Save the tokens that make up the body of a member function defined
27797 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27798 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27799 specifiers applied to the declaration. Returns the FUNCTION_DECL
27800 for the member function. */
27801
27802 static tree
27803 cp_parser_save_member_function_body (cp_parser* parser,
27804 cp_decl_specifier_seq *decl_specifiers,
27805 cp_declarator *declarator,
27806 tree attributes)
27807 {
27808 cp_token *first;
27809 cp_token *last;
27810 tree fn;
27811 bool function_try_block = false;
27812
27813 /* Create the FUNCTION_DECL. */
27814 fn = grokmethod (decl_specifiers, declarator, attributes);
27815 cp_finalize_omp_declare_simd (parser, fn);
27816 cp_finalize_oacc_routine (parser, fn, true);
27817 /* If something went badly wrong, bail out now. */
27818 if (fn == error_mark_node)
27819 {
27820 /* If there's a function-body, skip it. */
27821 if (cp_parser_token_starts_function_definition_p
27822 (cp_lexer_peek_token (parser->lexer)))
27823 cp_parser_skip_to_end_of_block_or_statement (parser);
27824 return error_mark_node;
27825 }
27826
27827 /* Remember it, if there default args to post process. */
27828 cp_parser_save_default_args (parser, fn);
27829
27830 /* Save away the tokens that make up the body of the
27831 function. */
27832 first = parser->lexer->next_token;
27833
27834 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27835 cp_lexer_consume_token (parser->lexer);
27836 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27837 RID_TRANSACTION_ATOMIC))
27838 {
27839 cp_lexer_consume_token (parser->lexer);
27840 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27841 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27842 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27843 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27844 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27845 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27846 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27847 {
27848 cp_lexer_consume_token (parser->lexer);
27849 cp_lexer_consume_token (parser->lexer);
27850 cp_lexer_consume_token (parser->lexer);
27851 cp_lexer_consume_token (parser->lexer);
27852 cp_lexer_consume_token (parser->lexer);
27853 }
27854 else
27855 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27856 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27857 {
27858 cp_lexer_consume_token (parser->lexer);
27859 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27860 break;
27861 }
27862 }
27863
27864 /* Handle function try blocks. */
27865 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27866 {
27867 cp_lexer_consume_token (parser->lexer);
27868 function_try_block = true;
27869 }
27870 /* We can have braced-init-list mem-initializers before the fn body. */
27871 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27872 {
27873 cp_lexer_consume_token (parser->lexer);
27874 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27875 {
27876 /* cache_group will stop after an un-nested { } pair, too. */
27877 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27878 break;
27879
27880 /* variadic mem-inits have ... after the ')'. */
27881 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27882 cp_lexer_consume_token (parser->lexer);
27883 }
27884 }
27885 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27886 /* Handle function try blocks. */
27887 if (function_try_block)
27888 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27889 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27890 last = parser->lexer->next_token;
27891
27892 /* Save away the inline definition; we will process it when the
27893 class is complete. */
27894 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27895 DECL_PENDING_INLINE_P (fn) = 1;
27896
27897 /* We need to know that this was defined in the class, so that
27898 friend templates are handled correctly. */
27899 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27900
27901 /* Add FN to the queue of functions to be parsed later. */
27902 vec_safe_push (unparsed_funs_with_definitions, fn);
27903
27904 return fn;
27905 }
27906
27907 /* Save the tokens that make up the in-class initializer for a non-static
27908 data member. Returns a DEFAULT_ARG. */
27909
27910 static tree
27911 cp_parser_save_nsdmi (cp_parser* parser)
27912 {
27913 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27914 }
27915
27916 /* Parse a template-argument-list, as well as the trailing ">" (but
27917 not the opening "<"). See cp_parser_template_argument_list for the
27918 return value. */
27919
27920 static tree
27921 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27922 {
27923 tree arguments;
27924 tree saved_scope;
27925 tree saved_qualifying_scope;
27926 tree saved_object_scope;
27927 bool saved_greater_than_is_operator_p;
27928
27929 /* [temp.names]
27930
27931 When parsing a template-id, the first non-nested `>' is taken as
27932 the end of the template-argument-list rather than a greater-than
27933 operator. */
27934 saved_greater_than_is_operator_p
27935 = parser->greater_than_is_operator_p;
27936 parser->greater_than_is_operator_p = false;
27937 /* Parsing the argument list may modify SCOPE, so we save it
27938 here. */
27939 saved_scope = parser->scope;
27940 saved_qualifying_scope = parser->qualifying_scope;
27941 saved_object_scope = parser->object_scope;
27942 /* We need to evaluate the template arguments, even though this
27943 template-id may be nested within a "sizeof". */
27944 cp_evaluated ev;
27945 /* Parse the template-argument-list itself. */
27946 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27947 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27948 arguments = NULL_TREE;
27949 else
27950 arguments = cp_parser_template_argument_list (parser);
27951 /* Look for the `>' that ends the template-argument-list. If we find
27952 a '>>' instead, it's probably just a typo. */
27953 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27954 {
27955 if (cxx_dialect != cxx98)
27956 {
27957 /* In C++0x, a `>>' in a template argument list or cast
27958 expression is considered to be two separate `>'
27959 tokens. So, change the current token to a `>', but don't
27960 consume it: it will be consumed later when the outer
27961 template argument list (or cast expression) is parsed.
27962 Note that this replacement of `>' for `>>' is necessary
27963 even if we are parsing tentatively: in the tentative
27964 case, after calling
27965 cp_parser_enclosed_template_argument_list we will always
27966 throw away all of the template arguments and the first
27967 closing `>', either because the template argument list
27968 was erroneous or because we are replacing those tokens
27969 with a CPP_TEMPLATE_ID token. The second `>' (which will
27970 not have been thrown away) is needed either to close an
27971 outer template argument list or to complete a new-style
27972 cast. */
27973 cp_token *token = cp_lexer_peek_token (parser->lexer);
27974 token->type = CPP_GREATER;
27975 }
27976 else if (!saved_greater_than_is_operator_p)
27977 {
27978 /* If we're in a nested template argument list, the '>>' has
27979 to be a typo for '> >'. We emit the error message, but we
27980 continue parsing and we push a '>' as next token, so that
27981 the argument list will be parsed correctly. Note that the
27982 global source location is still on the token before the
27983 '>>', so we need to say explicitly where we want it. */
27984 cp_token *token = cp_lexer_peek_token (parser->lexer);
27985 gcc_rich_location richloc (token->location);
27986 richloc.add_fixit_replace ("> >");
27987 error_at (&richloc, "%<>>%> should be %<> >%> "
27988 "within a nested template argument list");
27989
27990 token->type = CPP_GREATER;
27991 }
27992 else
27993 {
27994 /* If this is not a nested template argument list, the '>>'
27995 is a typo for '>'. Emit an error message and continue.
27996 Same deal about the token location, but here we can get it
27997 right by consuming the '>>' before issuing the diagnostic. */
27998 cp_token *token = cp_lexer_consume_token (parser->lexer);
27999 error_at (token->location,
28000 "spurious %<>>%>, use %<>%> to terminate "
28001 "a template argument list");
28002 }
28003 }
28004 else
28005 cp_parser_skip_to_end_of_template_parameter_list (parser);
28006 /* The `>' token might be a greater-than operator again now. */
28007 parser->greater_than_is_operator_p
28008 = saved_greater_than_is_operator_p;
28009 /* Restore the SAVED_SCOPE. */
28010 parser->scope = saved_scope;
28011 parser->qualifying_scope = saved_qualifying_scope;
28012 parser->object_scope = saved_object_scope;
28013
28014 return arguments;
28015 }
28016
28017 /* MEMBER_FUNCTION is a member function, or a friend. If default
28018 arguments, or the body of the function have not yet been parsed,
28019 parse them now. */
28020
28021 static void
28022 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28023 {
28024 timevar_push (TV_PARSE_INMETH);
28025 /* If this member is a template, get the underlying
28026 FUNCTION_DECL. */
28027 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28028 member_function = DECL_TEMPLATE_RESULT (member_function);
28029
28030 /* There should not be any class definitions in progress at this
28031 point; the bodies of members are only parsed outside of all class
28032 definitions. */
28033 gcc_assert (parser->num_classes_being_defined == 0);
28034 /* While we're parsing the member functions we might encounter more
28035 classes. We want to handle them right away, but we don't want
28036 them getting mixed up with functions that are currently in the
28037 queue. */
28038 push_unparsed_function_queues (parser);
28039
28040 /* Make sure that any template parameters are in scope. */
28041 maybe_begin_member_template_processing (member_function);
28042
28043 /* If the body of the function has not yet been parsed, parse it
28044 now. */
28045 if (DECL_PENDING_INLINE_P (member_function))
28046 {
28047 tree function_scope;
28048 cp_token_cache *tokens;
28049
28050 /* The function is no longer pending; we are processing it. */
28051 tokens = DECL_PENDING_INLINE_INFO (member_function);
28052 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28053 DECL_PENDING_INLINE_P (member_function) = 0;
28054
28055 /* If this is a local class, enter the scope of the containing
28056 function. */
28057 function_scope = current_function_decl;
28058 if (function_scope)
28059 push_function_context ();
28060
28061 /* Push the body of the function onto the lexer stack. */
28062 cp_parser_push_lexer_for_tokens (parser, tokens);
28063
28064 /* Let the front end know that we going to be defining this
28065 function. */
28066 start_preparsed_function (member_function, NULL_TREE,
28067 SF_PRE_PARSED | SF_INCLASS_INLINE);
28068
28069 /* Don't do access checking if it is a templated function. */
28070 if (processing_template_decl)
28071 push_deferring_access_checks (dk_no_check);
28072
28073 /* #pragma omp declare reduction needs special parsing. */
28074 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28075 {
28076 parser->lexer->in_pragma = true;
28077 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28078 finish_function (/*inline_p=*/true);
28079 cp_check_omp_declare_reduction (member_function);
28080 }
28081 else
28082 /* Now, parse the body of the function. */
28083 cp_parser_function_definition_after_declarator (parser,
28084 /*inline_p=*/true);
28085
28086 if (processing_template_decl)
28087 pop_deferring_access_checks ();
28088
28089 /* Leave the scope of the containing function. */
28090 if (function_scope)
28091 pop_function_context ();
28092 cp_parser_pop_lexer (parser);
28093 }
28094
28095 /* Remove any template parameters from the symbol table. */
28096 maybe_end_member_template_processing ();
28097
28098 /* Restore the queue. */
28099 pop_unparsed_function_queues (parser);
28100 timevar_pop (TV_PARSE_INMETH);
28101 }
28102
28103 /* If DECL contains any default args, remember it on the unparsed
28104 functions queue. */
28105
28106 static void
28107 cp_parser_save_default_args (cp_parser* parser, tree decl)
28108 {
28109 tree probe;
28110
28111 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28112 probe;
28113 probe = TREE_CHAIN (probe))
28114 if (TREE_PURPOSE (probe))
28115 {
28116 cp_default_arg_entry entry = {current_class_type, decl};
28117 vec_safe_push (unparsed_funs_with_default_args, entry);
28118 break;
28119 }
28120 }
28121
28122 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28123 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28124 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28125 from the parameter-type-list. */
28126
28127 static tree
28128 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28129 tree default_arg, tree parmtype)
28130 {
28131 cp_token_cache *tokens;
28132 tree parsed_arg;
28133 bool dummy;
28134
28135 if (default_arg == error_mark_node)
28136 return error_mark_node;
28137
28138 /* Push the saved tokens for the default argument onto the parser's
28139 lexer stack. */
28140 tokens = DEFARG_TOKENS (default_arg);
28141 cp_parser_push_lexer_for_tokens (parser, tokens);
28142
28143 start_lambda_scope (decl);
28144
28145 /* Parse the default argument. */
28146 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28147 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28148 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28149
28150 finish_lambda_scope ();
28151
28152 if (parsed_arg == error_mark_node)
28153 cp_parser_skip_to_end_of_statement (parser);
28154
28155 if (!processing_template_decl)
28156 {
28157 /* In a non-template class, check conversions now. In a template,
28158 we'll wait and instantiate these as needed. */
28159 if (TREE_CODE (decl) == PARM_DECL)
28160 parsed_arg = check_default_argument (parmtype, parsed_arg,
28161 tf_warning_or_error);
28162 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28163 parsed_arg = error_mark_node;
28164 else
28165 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28166 }
28167
28168 /* If the token stream has not been completely used up, then
28169 there was extra junk after the end of the default
28170 argument. */
28171 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28172 {
28173 if (TREE_CODE (decl) == PARM_DECL)
28174 cp_parser_error (parser, "expected %<,%>");
28175 else
28176 cp_parser_error (parser, "expected %<;%>");
28177 }
28178
28179 /* Revert to the main lexer. */
28180 cp_parser_pop_lexer (parser);
28181
28182 return parsed_arg;
28183 }
28184
28185 /* FIELD is a non-static data member with an initializer which we saved for
28186 later; parse it now. */
28187
28188 static void
28189 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28190 {
28191 tree def;
28192
28193 maybe_begin_member_template_processing (field);
28194
28195 push_unparsed_function_queues (parser);
28196 def = cp_parser_late_parse_one_default_arg (parser, field,
28197 DECL_INITIAL (field),
28198 NULL_TREE);
28199 pop_unparsed_function_queues (parser);
28200
28201 maybe_end_member_template_processing ();
28202
28203 DECL_INITIAL (field) = def;
28204 }
28205
28206 /* FN is a FUNCTION_DECL which may contains a parameter with an
28207 unparsed DEFAULT_ARG. Parse the default args now. This function
28208 assumes that the current scope is the scope in which the default
28209 argument should be processed. */
28210
28211 static void
28212 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28213 {
28214 bool saved_local_variables_forbidden_p;
28215 tree parm, parmdecl;
28216
28217 /* While we're parsing the default args, we might (due to the
28218 statement expression extension) encounter more classes. We want
28219 to handle them right away, but we don't want them getting mixed
28220 up with default args that are currently in the queue. */
28221 push_unparsed_function_queues (parser);
28222
28223 /* Local variable names (and the `this' keyword) may not appear
28224 in a default argument. */
28225 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28226 parser->local_variables_forbidden_p = true;
28227
28228 push_defarg_context (fn);
28229
28230 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28231 parmdecl = DECL_ARGUMENTS (fn);
28232 parm && parm != void_list_node;
28233 parm = TREE_CHAIN (parm),
28234 parmdecl = DECL_CHAIN (parmdecl))
28235 {
28236 tree default_arg = TREE_PURPOSE (parm);
28237 tree parsed_arg;
28238 vec<tree, va_gc> *insts;
28239 tree copy;
28240 unsigned ix;
28241
28242 if (!default_arg)
28243 continue;
28244
28245 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28246 /* This can happen for a friend declaration for a function
28247 already declared with default arguments. */
28248 continue;
28249
28250 parsed_arg
28251 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28252 default_arg,
28253 TREE_VALUE (parm));
28254 TREE_PURPOSE (parm) = parsed_arg;
28255
28256 /* Update any instantiations we've already created. */
28257 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28258 vec_safe_iterate (insts, ix, &copy); ix++)
28259 TREE_PURPOSE (copy) = parsed_arg;
28260 }
28261
28262 pop_defarg_context ();
28263
28264 /* Make sure no default arg is missing. */
28265 check_default_args (fn);
28266
28267 /* Restore the state of local_variables_forbidden_p. */
28268 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28269
28270 /* Restore the queue. */
28271 pop_unparsed_function_queues (parser);
28272 }
28273
28274 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28275
28276 sizeof ... ( identifier )
28277
28278 where the 'sizeof' token has already been consumed. */
28279
28280 static tree
28281 cp_parser_sizeof_pack (cp_parser *parser)
28282 {
28283 /* Consume the `...'. */
28284 cp_lexer_consume_token (parser->lexer);
28285 maybe_warn_variadic_templates ();
28286
28287 matching_parens parens;
28288 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28289 if (paren)
28290 parens.consume_open (parser);
28291 else
28292 permerror (cp_lexer_peek_token (parser->lexer)->location,
28293 "%<sizeof...%> argument must be surrounded by parentheses");
28294
28295 cp_token *token = cp_lexer_peek_token (parser->lexer);
28296 tree name = cp_parser_identifier (parser);
28297 if (name == error_mark_node)
28298 return error_mark_node;
28299 /* The name is not qualified. */
28300 parser->scope = NULL_TREE;
28301 parser->qualifying_scope = NULL_TREE;
28302 parser->object_scope = NULL_TREE;
28303 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28304 if (expr == error_mark_node)
28305 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28306 token->location);
28307 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28308 expr = TREE_TYPE (expr);
28309 else if (TREE_CODE (expr) == CONST_DECL)
28310 expr = DECL_INITIAL (expr);
28311 expr = make_pack_expansion (expr);
28312 PACK_EXPANSION_SIZEOF_P (expr) = true;
28313
28314 if (paren)
28315 parens.require_close (parser);
28316
28317 return expr;
28318 }
28319
28320 /* Parse the operand of `sizeof' (or a similar operator). Returns
28321 either a TYPE or an expression, depending on the form of the
28322 input. The KEYWORD indicates which kind of expression we have
28323 encountered. */
28324
28325 static tree
28326 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28327 {
28328 tree expr = NULL_TREE;
28329 const char *saved_message;
28330 char *tmp;
28331 bool saved_integral_constant_expression_p;
28332 bool saved_non_integral_constant_expression_p;
28333
28334 /* If it's a `...', then we are computing the length of a parameter
28335 pack. */
28336 if (keyword == RID_SIZEOF
28337 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28338 return cp_parser_sizeof_pack (parser);
28339
28340 /* Types cannot be defined in a `sizeof' expression. Save away the
28341 old message. */
28342 saved_message = parser->type_definition_forbidden_message;
28343 /* And create the new one. */
28344 tmp = concat ("types may not be defined in %<",
28345 IDENTIFIER_POINTER (ridpointers[keyword]),
28346 "%> expressions", NULL);
28347 parser->type_definition_forbidden_message = tmp;
28348
28349 /* The restrictions on constant-expressions do not apply inside
28350 sizeof expressions. */
28351 saved_integral_constant_expression_p
28352 = parser->integral_constant_expression_p;
28353 saved_non_integral_constant_expression_p
28354 = parser->non_integral_constant_expression_p;
28355 parser->integral_constant_expression_p = false;
28356
28357 /* Do not actually evaluate the expression. */
28358 ++cp_unevaluated_operand;
28359 ++c_inhibit_evaluation_warnings;
28360 /* If it's a `(', then we might be looking at the type-id
28361 construction. */
28362 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28363 {
28364 tree type = NULL_TREE;
28365
28366 /* We can't be sure yet whether we're looking at a type-id or an
28367 expression. */
28368 cp_parser_parse_tentatively (parser);
28369
28370 matching_parens parens;
28371 parens.consume_open (parser);
28372
28373 /* Note: as a GNU Extension, compound literals are considered
28374 postfix-expressions as they are in C99, so they are valid
28375 arguments to sizeof. See comment in cp_parser_cast_expression
28376 for details. */
28377 if (cp_parser_compound_literal_p (parser))
28378 cp_parser_simulate_error (parser);
28379 else
28380 {
28381 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28382 parser->in_type_id_in_expr_p = true;
28383 /* Look for the type-id. */
28384 type = cp_parser_type_id (parser);
28385 /* Look for the closing `)'. */
28386 parens.require_close (parser);
28387 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28388 }
28389
28390 /* If all went well, then we're done. */
28391 if (cp_parser_parse_definitely (parser))
28392 expr = type;
28393 }
28394
28395 /* If the type-id production did not work out, then we must be
28396 looking at the unary-expression production. */
28397 if (!expr)
28398 expr = cp_parser_unary_expression (parser);
28399
28400 /* Go back to evaluating expressions. */
28401 --cp_unevaluated_operand;
28402 --c_inhibit_evaluation_warnings;
28403
28404 /* Free the message we created. */
28405 free (tmp);
28406 /* And restore the old one. */
28407 parser->type_definition_forbidden_message = saved_message;
28408 parser->integral_constant_expression_p
28409 = saved_integral_constant_expression_p;
28410 parser->non_integral_constant_expression_p
28411 = saved_non_integral_constant_expression_p;
28412
28413 return expr;
28414 }
28415
28416 /* If the current declaration has no declarator, return true. */
28417
28418 static bool
28419 cp_parser_declares_only_class_p (cp_parser *parser)
28420 {
28421 /* If the next token is a `;' or a `,' then there is no
28422 declarator. */
28423 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28424 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28425 }
28426
28427 /* Update the DECL_SPECS to reflect the storage class indicated by
28428 KEYWORD. */
28429
28430 static void
28431 cp_parser_set_storage_class (cp_parser *parser,
28432 cp_decl_specifier_seq *decl_specs,
28433 enum rid keyword,
28434 cp_token *token)
28435 {
28436 cp_storage_class storage_class;
28437
28438 if (parser->in_unbraced_linkage_specification_p)
28439 {
28440 error_at (token->location, "invalid use of %qD in linkage specification",
28441 ridpointers[keyword]);
28442 return;
28443 }
28444 else if (decl_specs->storage_class != sc_none)
28445 {
28446 decl_specs->conflicting_specifiers_p = true;
28447 return;
28448 }
28449
28450 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28451 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28452 && decl_specs->gnu_thread_keyword_p)
28453 {
28454 pedwarn (decl_specs->locations[ds_thread], 0,
28455 "%<__thread%> before %qD", ridpointers[keyword]);
28456 }
28457
28458 switch (keyword)
28459 {
28460 case RID_AUTO:
28461 storage_class = sc_auto;
28462 break;
28463 case RID_REGISTER:
28464 storage_class = sc_register;
28465 break;
28466 case RID_STATIC:
28467 storage_class = sc_static;
28468 break;
28469 case RID_EXTERN:
28470 storage_class = sc_extern;
28471 break;
28472 case RID_MUTABLE:
28473 storage_class = sc_mutable;
28474 break;
28475 default:
28476 gcc_unreachable ();
28477 }
28478 decl_specs->storage_class = storage_class;
28479 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28480
28481 /* A storage class specifier cannot be applied alongside a typedef
28482 specifier. If there is a typedef specifier present then set
28483 conflicting_specifiers_p which will trigger an error later
28484 on in grokdeclarator. */
28485 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28486 decl_specs->conflicting_specifiers_p = true;
28487 }
28488
28489 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28490 is true, the type is a class or enum definition. */
28491
28492 static void
28493 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28494 tree type_spec,
28495 cp_token *token,
28496 bool type_definition_p)
28497 {
28498 decl_specs->any_specifiers_p = true;
28499
28500 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28501 (with, for example, in "typedef int wchar_t;") we remember that
28502 this is what happened. In system headers, we ignore these
28503 declarations so that G++ can work with system headers that are not
28504 C++-safe. */
28505 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28506 && !type_definition_p
28507 && (type_spec == boolean_type_node
28508 || type_spec == char16_type_node
28509 || type_spec == char32_type_node
28510 || type_spec == wchar_type_node)
28511 && (decl_specs->type
28512 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28513 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28514 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28515 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28516 {
28517 decl_specs->redefined_builtin_type = type_spec;
28518 set_and_check_decl_spec_loc (decl_specs,
28519 ds_redefined_builtin_type_spec,
28520 token);
28521 if (!decl_specs->type)
28522 {
28523 decl_specs->type = type_spec;
28524 decl_specs->type_definition_p = false;
28525 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28526 }
28527 }
28528 else if (decl_specs->type)
28529 decl_specs->multiple_types_p = true;
28530 else
28531 {
28532 decl_specs->type = type_spec;
28533 decl_specs->type_definition_p = type_definition_p;
28534 decl_specs->redefined_builtin_type = NULL_TREE;
28535 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28536 }
28537 }
28538
28539 /* True iff TOKEN is the GNU keyword __thread. */
28540
28541 static bool
28542 token_is__thread (cp_token *token)
28543 {
28544 gcc_assert (token->keyword == RID_THREAD);
28545 return id_equal (token->u.value, "__thread");
28546 }
28547
28548 /* Set the location for a declarator specifier and check if it is
28549 duplicated.
28550
28551 DECL_SPECS is the sequence of declarator specifiers onto which to
28552 set the location.
28553
28554 DS is the single declarator specifier to set which location is to
28555 be set onto the existing sequence of declarators.
28556
28557 LOCATION is the location for the declarator specifier to
28558 consider. */
28559
28560 static void
28561 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28562 cp_decl_spec ds, cp_token *token)
28563 {
28564 gcc_assert (ds < ds_last);
28565
28566 if (decl_specs == NULL)
28567 return;
28568
28569 location_t location = token->location;
28570
28571 if (decl_specs->locations[ds] == 0)
28572 {
28573 decl_specs->locations[ds] = location;
28574 if (ds == ds_thread)
28575 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28576 }
28577 else
28578 {
28579 if (ds == ds_long)
28580 {
28581 if (decl_specs->locations[ds_long_long] != 0)
28582 error_at (location,
28583 "%<long long long%> is too long for GCC");
28584 else
28585 {
28586 decl_specs->locations[ds_long_long] = location;
28587 pedwarn_cxx98 (location,
28588 OPT_Wlong_long,
28589 "ISO C++ 1998 does not support %<long long%>");
28590 }
28591 }
28592 else if (ds == ds_thread)
28593 {
28594 bool gnu = token_is__thread (token);
28595 gcc_rich_location richloc (location);
28596 if (gnu != decl_specs->gnu_thread_keyword_p)
28597 {
28598 richloc.add_range (decl_specs->locations[ds_thread]);
28599 error_at (&richloc,
28600 "both %<__thread%> and %<thread_local%> specified");
28601 }
28602 else
28603 {
28604 richloc.add_fixit_remove ();
28605 error_at (&richloc, "duplicate %qD", token->u.value);
28606 }
28607 }
28608 else
28609 {
28610 static const char *const decl_spec_names[] = {
28611 "signed",
28612 "unsigned",
28613 "short",
28614 "long",
28615 "const",
28616 "volatile",
28617 "restrict",
28618 "inline",
28619 "virtual",
28620 "explicit",
28621 "friend",
28622 "typedef",
28623 "using",
28624 "constexpr",
28625 "__complex"
28626 };
28627 gcc_rich_location richloc (location);
28628 richloc.add_fixit_remove ();
28629 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28630 }
28631 }
28632 }
28633
28634 /* Return true iff the declarator specifier DS is present in the
28635 sequence of declarator specifiers DECL_SPECS. */
28636
28637 bool
28638 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28639 cp_decl_spec ds)
28640 {
28641 gcc_assert (ds < ds_last);
28642
28643 if (decl_specs == NULL)
28644 return false;
28645
28646 return decl_specs->locations[ds] != 0;
28647 }
28648
28649 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28650 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28651
28652 static bool
28653 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28654 {
28655 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28656 }
28657
28658 /* Issue an error message indicating that TOKEN_DESC was expected.
28659 If KEYWORD is true, it indicated this function is called by
28660 cp_parser_require_keword and the required token can only be
28661 a indicated keyword.
28662
28663 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28664 within any error as the location of an "opening" token matching
28665 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28666 RT_CLOSE_PAREN). */
28667
28668 static void
28669 cp_parser_required_error (cp_parser *parser,
28670 required_token token_desc,
28671 bool keyword,
28672 location_t matching_location)
28673 {
28674 if (cp_parser_simulate_error (parser))
28675 return;
28676
28677 const char *gmsgid = NULL;
28678 switch (token_desc)
28679 {
28680 case RT_NEW:
28681 gmsgid = G_("expected %<new%>");
28682 break;
28683 case RT_DELETE:
28684 gmsgid = G_("expected %<delete%>");
28685 break;
28686 case RT_RETURN:
28687 gmsgid = G_("expected %<return%>");
28688 break;
28689 case RT_WHILE:
28690 gmsgid = G_("expected %<while%>");
28691 break;
28692 case RT_EXTERN:
28693 gmsgid = G_("expected %<extern%>");
28694 break;
28695 case RT_STATIC_ASSERT:
28696 gmsgid = G_("expected %<static_assert%>");
28697 break;
28698 case RT_DECLTYPE:
28699 gmsgid = G_("expected %<decltype%>");
28700 break;
28701 case RT_OPERATOR:
28702 gmsgid = G_("expected %<operator%>");
28703 break;
28704 case RT_CLASS:
28705 gmsgid = G_("expected %<class%>");
28706 break;
28707 case RT_TEMPLATE:
28708 gmsgid = G_("expected %<template%>");
28709 break;
28710 case RT_NAMESPACE:
28711 gmsgid = G_("expected %<namespace%>");
28712 break;
28713 case RT_USING:
28714 gmsgid = G_("expected %<using%>");
28715 break;
28716 case RT_ASM:
28717 gmsgid = G_("expected %<asm%>");
28718 break;
28719 case RT_TRY:
28720 gmsgid = G_("expected %<try%>");
28721 break;
28722 case RT_CATCH:
28723 gmsgid = G_("expected %<catch%>");
28724 break;
28725 case RT_THROW:
28726 gmsgid = G_("expected %<throw%>");
28727 break;
28728 case RT_LABEL:
28729 gmsgid = G_("expected %<__label__%>");
28730 break;
28731 case RT_AT_TRY:
28732 gmsgid = G_("expected %<@try%>");
28733 break;
28734 case RT_AT_SYNCHRONIZED:
28735 gmsgid = G_("expected %<@synchronized%>");
28736 break;
28737 case RT_AT_THROW:
28738 gmsgid = G_("expected %<@throw%>");
28739 break;
28740 case RT_TRANSACTION_ATOMIC:
28741 gmsgid = G_("expected %<__transaction_atomic%>");
28742 break;
28743 case RT_TRANSACTION_RELAXED:
28744 gmsgid = G_("expected %<__transaction_relaxed%>");
28745 break;
28746 default:
28747 break;
28748 }
28749
28750 if (!gmsgid && !keyword)
28751 {
28752 switch (token_desc)
28753 {
28754 case RT_SEMICOLON:
28755 gmsgid = G_("expected %<;%>");
28756 break;
28757 case RT_OPEN_PAREN:
28758 gmsgid = G_("expected %<(%>");
28759 break;
28760 case RT_CLOSE_BRACE:
28761 gmsgid = G_("expected %<}%>");
28762 break;
28763 case RT_OPEN_BRACE:
28764 gmsgid = G_("expected %<{%>");
28765 break;
28766 case RT_CLOSE_SQUARE:
28767 gmsgid = G_("expected %<]%>");
28768 break;
28769 case RT_OPEN_SQUARE:
28770 gmsgid = G_("expected %<[%>");
28771 break;
28772 case RT_COMMA:
28773 gmsgid = G_("expected %<,%>");
28774 break;
28775 case RT_SCOPE:
28776 gmsgid = G_("expected %<::%>");
28777 break;
28778 case RT_LESS:
28779 gmsgid = G_("expected %<<%>");
28780 break;
28781 case RT_GREATER:
28782 gmsgid = G_("expected %<>%>");
28783 break;
28784 case RT_EQ:
28785 gmsgid = G_("expected %<=%>");
28786 break;
28787 case RT_ELLIPSIS:
28788 gmsgid = G_("expected %<...%>");
28789 break;
28790 case RT_MULT:
28791 gmsgid = G_("expected %<*%>");
28792 break;
28793 case RT_COMPL:
28794 gmsgid = G_("expected %<~%>");
28795 break;
28796 case RT_COLON:
28797 gmsgid = G_("expected %<:%>");
28798 break;
28799 case RT_COLON_SCOPE:
28800 gmsgid = G_("expected %<:%> or %<::%>");
28801 break;
28802 case RT_CLOSE_PAREN:
28803 gmsgid = G_("expected %<)%>");
28804 break;
28805 case RT_COMMA_CLOSE_PAREN:
28806 gmsgid = G_("expected %<,%> or %<)%>");
28807 break;
28808 case RT_PRAGMA_EOL:
28809 gmsgid = G_("expected end of line");
28810 break;
28811 case RT_NAME:
28812 gmsgid = G_("expected identifier");
28813 break;
28814 case RT_SELECT:
28815 gmsgid = G_("expected selection-statement");
28816 break;
28817 case RT_ITERATION:
28818 gmsgid = G_("expected iteration-statement");
28819 break;
28820 case RT_JUMP:
28821 gmsgid = G_("expected jump-statement");
28822 break;
28823 case RT_CLASS_KEY:
28824 gmsgid = G_("expected class-key");
28825 break;
28826 case RT_CLASS_TYPENAME_TEMPLATE:
28827 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28828 break;
28829 default:
28830 gcc_unreachable ();
28831 }
28832 }
28833
28834 if (gmsgid)
28835 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28836 }
28837
28838
28839 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28840 issue an error message indicating that TOKEN_DESC was expected.
28841
28842 Returns the token consumed, if the token had the appropriate type.
28843 Otherwise, returns NULL.
28844
28845 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28846 within any error as the location of an "opening" token matching
28847 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28848 RT_CLOSE_PAREN). */
28849
28850 static cp_token *
28851 cp_parser_require (cp_parser* parser,
28852 enum cpp_ttype type,
28853 required_token token_desc,
28854 location_t matching_location)
28855 {
28856 if (cp_lexer_next_token_is (parser->lexer, type))
28857 return cp_lexer_consume_token (parser->lexer);
28858 else
28859 {
28860 /* Output the MESSAGE -- unless we're parsing tentatively. */
28861 if (!cp_parser_simulate_error (parser))
28862 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28863 matching_location);
28864 return NULL;
28865 }
28866 }
28867
28868 /* An error message is produced if the next token is not '>'.
28869 All further tokens are skipped until the desired token is
28870 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28871
28872 static void
28873 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28874 {
28875 /* Current level of '< ... >'. */
28876 unsigned level = 0;
28877 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28878 unsigned nesting_depth = 0;
28879
28880 /* Are we ready, yet? If not, issue error message. */
28881 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28882 return;
28883
28884 /* Skip tokens until the desired token is found. */
28885 while (true)
28886 {
28887 /* Peek at the next token. */
28888 switch (cp_lexer_peek_token (parser->lexer)->type)
28889 {
28890 case CPP_LESS:
28891 if (!nesting_depth)
28892 ++level;
28893 break;
28894
28895 case CPP_RSHIFT:
28896 if (cxx_dialect == cxx98)
28897 /* C++0x views the `>>' operator as two `>' tokens, but
28898 C++98 does not. */
28899 break;
28900 else if (!nesting_depth && level-- == 0)
28901 {
28902 /* We've hit a `>>' where the first `>' closes the
28903 template argument list, and the second `>' is
28904 spurious. Just consume the `>>' and stop; we've
28905 already produced at least one error. */
28906 cp_lexer_consume_token (parser->lexer);
28907 return;
28908 }
28909 /* Fall through for C++0x, so we handle the second `>' in
28910 the `>>'. */
28911 gcc_fallthrough ();
28912
28913 case CPP_GREATER:
28914 if (!nesting_depth && level-- == 0)
28915 {
28916 /* We've reached the token we want, consume it and stop. */
28917 cp_lexer_consume_token (parser->lexer);
28918 return;
28919 }
28920 break;
28921
28922 case CPP_OPEN_PAREN:
28923 case CPP_OPEN_SQUARE:
28924 ++nesting_depth;
28925 break;
28926
28927 case CPP_CLOSE_PAREN:
28928 case CPP_CLOSE_SQUARE:
28929 if (nesting_depth-- == 0)
28930 return;
28931 break;
28932
28933 case CPP_EOF:
28934 case CPP_PRAGMA_EOL:
28935 case CPP_SEMICOLON:
28936 case CPP_OPEN_BRACE:
28937 case CPP_CLOSE_BRACE:
28938 /* The '>' was probably forgotten, don't look further. */
28939 return;
28940
28941 default:
28942 break;
28943 }
28944
28945 /* Consume this token. */
28946 cp_lexer_consume_token (parser->lexer);
28947 }
28948 }
28949
28950 /* If the next token is the indicated keyword, consume it. Otherwise,
28951 issue an error message indicating that TOKEN_DESC was expected.
28952
28953 Returns the token consumed, if the token had the appropriate type.
28954 Otherwise, returns NULL. */
28955
28956 static cp_token *
28957 cp_parser_require_keyword (cp_parser* parser,
28958 enum rid keyword,
28959 required_token token_desc)
28960 {
28961 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28962
28963 if (token && token->keyword != keyword)
28964 {
28965 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28966 UNKNOWN_LOCATION);
28967 return NULL;
28968 }
28969
28970 return token;
28971 }
28972
28973 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28974 function-definition. */
28975
28976 static bool
28977 cp_parser_token_starts_function_definition_p (cp_token* token)
28978 {
28979 return (/* An ordinary function-body begins with an `{'. */
28980 token->type == CPP_OPEN_BRACE
28981 /* A ctor-initializer begins with a `:'. */
28982 || token->type == CPP_COLON
28983 /* A function-try-block begins with `try'. */
28984 || token->keyword == RID_TRY
28985 /* A function-transaction-block begins with `__transaction_atomic'
28986 or `__transaction_relaxed'. */
28987 || token->keyword == RID_TRANSACTION_ATOMIC
28988 || token->keyword == RID_TRANSACTION_RELAXED
28989 /* The named return value extension begins with `return'. */
28990 || token->keyword == RID_RETURN);
28991 }
28992
28993 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28994 definition. */
28995
28996 static bool
28997 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28998 {
28999 cp_token *token;
29000
29001 token = cp_lexer_peek_token (parser->lexer);
29002 return (token->type == CPP_OPEN_BRACE
29003 || (token->type == CPP_COLON
29004 && !parser->colon_doesnt_start_class_def_p));
29005 }
29006
29007 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29008 C++0x) ending a template-argument. */
29009
29010 static bool
29011 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29012 {
29013 cp_token *token;
29014
29015 token = cp_lexer_peek_token (parser->lexer);
29016 return (token->type == CPP_COMMA
29017 || token->type == CPP_GREATER
29018 || token->type == CPP_ELLIPSIS
29019 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29020 }
29021
29022 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29023 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29024
29025 static bool
29026 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29027 size_t n)
29028 {
29029 cp_token *token;
29030
29031 token = cp_lexer_peek_nth_token (parser->lexer, n);
29032 if (token->type == CPP_LESS)
29033 return true;
29034 /* Check for the sequence `<::' in the original code. It would be lexed as
29035 `[:', where `[' is a digraph, and there is no whitespace before
29036 `:'. */
29037 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29038 {
29039 cp_token *token2;
29040 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29041 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29042 return true;
29043 }
29044 return false;
29045 }
29046
29047 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29048 or none_type otherwise. */
29049
29050 static enum tag_types
29051 cp_parser_token_is_class_key (cp_token* token)
29052 {
29053 switch (token->keyword)
29054 {
29055 case RID_CLASS:
29056 return class_type;
29057 case RID_STRUCT:
29058 return record_type;
29059 case RID_UNION:
29060 return union_type;
29061
29062 default:
29063 return none_type;
29064 }
29065 }
29066
29067 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29068 or none_type otherwise or if the token is null. */
29069
29070 static enum tag_types
29071 cp_parser_token_is_type_parameter_key (cp_token* token)
29072 {
29073 if (!token)
29074 return none_type;
29075
29076 switch (token->keyword)
29077 {
29078 case RID_CLASS:
29079 return class_type;
29080 case RID_TYPENAME:
29081 return typename_type;
29082
29083 default:
29084 return none_type;
29085 }
29086 }
29087
29088 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29089
29090 static void
29091 cp_parser_check_class_key (enum tag_types class_key, tree type)
29092 {
29093 if (type == error_mark_node)
29094 return;
29095 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29096 {
29097 if (permerror (input_location, "%qs tag used in naming %q#T",
29098 class_key == union_type ? "union"
29099 : class_key == record_type ? "struct" : "class",
29100 type))
29101 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29102 "%q#T was previously declared here", type);
29103 }
29104 }
29105
29106 /* Issue an error message if DECL is redeclared with different
29107 access than its original declaration [class.access.spec/3].
29108 This applies to nested classes, nested class templates and
29109 enumerations [class.mem/1]. */
29110
29111 static void
29112 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29113 {
29114 if (!decl
29115 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29116 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29117 return;
29118
29119 if ((TREE_PRIVATE (decl)
29120 != (current_access_specifier == access_private_node))
29121 || (TREE_PROTECTED (decl)
29122 != (current_access_specifier == access_protected_node)))
29123 error_at (location, "%qD redeclared with different access", decl);
29124 }
29125
29126 /* Look for the `template' keyword, as a syntactic disambiguator.
29127 Return TRUE iff it is present, in which case it will be
29128 consumed. */
29129
29130 static bool
29131 cp_parser_optional_template_keyword (cp_parser *parser)
29132 {
29133 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29134 {
29135 /* In C++98 the `template' keyword can only be used within templates;
29136 outside templates the parser can always figure out what is a
29137 template and what is not. In C++11, per the resolution of DR 468,
29138 `template' is allowed in cases where it is not strictly necessary. */
29139 if (!processing_template_decl
29140 && pedantic && cxx_dialect == cxx98)
29141 {
29142 cp_token *token = cp_lexer_peek_token (parser->lexer);
29143 pedwarn (token->location, OPT_Wpedantic,
29144 "in C++98 %<template%> (as a disambiguator) is only "
29145 "allowed within templates");
29146 /* If this part of the token stream is rescanned, the same
29147 error message would be generated. So, we purge the token
29148 from the stream. */
29149 cp_lexer_purge_token (parser->lexer);
29150 return false;
29151 }
29152 else
29153 {
29154 /* Consume the `template' keyword. */
29155 cp_lexer_consume_token (parser->lexer);
29156 return true;
29157 }
29158 }
29159 return false;
29160 }
29161
29162 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29163 set PARSER->SCOPE, and perform other related actions. */
29164
29165 static void
29166 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29167 {
29168 struct tree_check *check_value;
29169
29170 /* Get the stored value. */
29171 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29172 /* Set the scope from the stored value. */
29173 parser->scope = saved_checks_value (check_value);
29174 parser->qualifying_scope = check_value->qualifying_scope;
29175 parser->object_scope = NULL_TREE;
29176 }
29177
29178 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29179 encounter the end of a block before what we were looking for. */
29180
29181 static bool
29182 cp_parser_cache_group (cp_parser *parser,
29183 enum cpp_ttype end,
29184 unsigned depth)
29185 {
29186 while (true)
29187 {
29188 cp_token *token = cp_lexer_peek_token (parser->lexer);
29189
29190 /* Abort a parenthesized expression if we encounter a semicolon. */
29191 if ((end == CPP_CLOSE_PAREN || depth == 0)
29192 && token->type == CPP_SEMICOLON)
29193 return true;
29194 /* If we've reached the end of the file, stop. */
29195 if (token->type == CPP_EOF
29196 || (end != CPP_PRAGMA_EOL
29197 && token->type == CPP_PRAGMA_EOL))
29198 return true;
29199 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29200 /* We've hit the end of an enclosing block, so there's been some
29201 kind of syntax error. */
29202 return true;
29203
29204 /* Consume the token. */
29205 cp_lexer_consume_token (parser->lexer);
29206 /* See if it starts a new group. */
29207 if (token->type == CPP_OPEN_BRACE)
29208 {
29209 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29210 /* In theory this should probably check end == '}', but
29211 cp_parser_save_member_function_body needs it to exit
29212 after either '}' or ')' when called with ')'. */
29213 if (depth == 0)
29214 return false;
29215 }
29216 else if (token->type == CPP_OPEN_PAREN)
29217 {
29218 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29219 if (depth == 0 && end == CPP_CLOSE_PAREN)
29220 return false;
29221 }
29222 else if (token->type == CPP_PRAGMA)
29223 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29224 else if (token->type == end)
29225 return false;
29226 }
29227 }
29228
29229 /* Like above, for caching a default argument or NSDMI. Both of these are
29230 terminated by a non-nested comma, but it can be unclear whether or not a
29231 comma is nested in a template argument list unless we do more parsing.
29232 In order to handle this ambiguity, when we encounter a ',' after a '<'
29233 we try to parse what follows as a parameter-declaration-list (in the
29234 case of a default argument) or a member-declarator (in the case of an
29235 NSDMI). If that succeeds, then we stop caching. */
29236
29237 static tree
29238 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29239 {
29240 unsigned depth = 0;
29241 int maybe_template_id = 0;
29242 cp_token *first_token;
29243 cp_token *token;
29244 tree default_argument;
29245
29246 /* Add tokens until we have processed the entire default
29247 argument. We add the range [first_token, token). */
29248 first_token = cp_lexer_peek_token (parser->lexer);
29249 if (first_token->type == CPP_OPEN_BRACE)
29250 {
29251 /* For list-initialization, this is straightforward. */
29252 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29253 token = cp_lexer_peek_token (parser->lexer);
29254 }
29255 else while (true)
29256 {
29257 bool done = false;
29258
29259 /* Peek at the next token. */
29260 token = cp_lexer_peek_token (parser->lexer);
29261 /* What we do depends on what token we have. */
29262 switch (token->type)
29263 {
29264 /* In valid code, a default argument must be
29265 immediately followed by a `,' `)', or `...'. */
29266 case CPP_COMMA:
29267 if (depth == 0 && maybe_template_id)
29268 {
29269 /* If we've seen a '<', we might be in a
29270 template-argument-list. Until Core issue 325 is
29271 resolved, we don't know how this situation ought
29272 to be handled, so try to DTRT. We check whether
29273 what comes after the comma is a valid parameter
29274 declaration list. If it is, then the comma ends
29275 the default argument; otherwise the default
29276 argument continues. */
29277 bool error = false;
29278 cp_token *peek;
29279
29280 /* Set ITALP so cp_parser_parameter_declaration_list
29281 doesn't decide to commit to this parse. */
29282 bool saved_italp = parser->in_template_argument_list_p;
29283 parser->in_template_argument_list_p = true;
29284
29285 cp_parser_parse_tentatively (parser);
29286
29287 if (nsdmi)
29288 {
29289 /* Parse declarators until we reach a non-comma or
29290 somthing that cannot be an initializer.
29291 Just checking whether we're looking at a single
29292 declarator is insufficient. Consider:
29293 int var = tuple<T,U>::x;
29294 The template parameter 'U' looks exactly like a
29295 declarator. */
29296 do
29297 {
29298 int ctor_dtor_or_conv_p;
29299 cp_lexer_consume_token (parser->lexer);
29300 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29301 &ctor_dtor_or_conv_p,
29302 /*parenthesized_p=*/NULL,
29303 /*member_p=*/true,
29304 /*friend_p=*/false);
29305 peek = cp_lexer_peek_token (parser->lexer);
29306 if (cp_parser_error_occurred (parser))
29307 break;
29308 }
29309 while (peek->type == CPP_COMMA);
29310 /* If we met an '=' or ';' then the original comma
29311 was the end of the NSDMI. Otherwise assume
29312 we're still in the NSDMI. */
29313 error = (peek->type != CPP_EQ
29314 && peek->type != CPP_SEMICOLON);
29315 }
29316 else
29317 {
29318 cp_lexer_consume_token (parser->lexer);
29319 begin_scope (sk_function_parms, NULL_TREE);
29320 if (cp_parser_parameter_declaration_list (parser)
29321 == error_mark_node)
29322 error = true;
29323 pop_bindings_and_leave_scope ();
29324 }
29325 if (!cp_parser_error_occurred (parser) && !error)
29326 done = true;
29327 cp_parser_abort_tentative_parse (parser);
29328
29329 parser->in_template_argument_list_p = saved_italp;
29330 break;
29331 }
29332 /* FALLTHRU */
29333 case CPP_CLOSE_PAREN:
29334 case CPP_ELLIPSIS:
29335 /* If we run into a non-nested `;', `}', or `]',
29336 then the code is invalid -- but the default
29337 argument is certainly over. */
29338 case CPP_SEMICOLON:
29339 case CPP_CLOSE_BRACE:
29340 case CPP_CLOSE_SQUARE:
29341 if (depth == 0
29342 /* Handle correctly int n = sizeof ... ( p ); */
29343 && token->type != CPP_ELLIPSIS)
29344 done = true;
29345 /* Update DEPTH, if necessary. */
29346 else if (token->type == CPP_CLOSE_PAREN
29347 || token->type == CPP_CLOSE_BRACE
29348 || token->type == CPP_CLOSE_SQUARE)
29349 --depth;
29350 break;
29351
29352 case CPP_OPEN_PAREN:
29353 case CPP_OPEN_SQUARE:
29354 case CPP_OPEN_BRACE:
29355 ++depth;
29356 break;
29357
29358 case CPP_LESS:
29359 if (depth == 0)
29360 /* This might be the comparison operator, or it might
29361 start a template argument list. */
29362 ++maybe_template_id;
29363 break;
29364
29365 case CPP_RSHIFT:
29366 if (cxx_dialect == cxx98)
29367 break;
29368 /* Fall through for C++0x, which treats the `>>'
29369 operator like two `>' tokens in certain
29370 cases. */
29371 gcc_fallthrough ();
29372
29373 case CPP_GREATER:
29374 if (depth == 0)
29375 {
29376 /* This might be an operator, or it might close a
29377 template argument list. But if a previous '<'
29378 started a template argument list, this will have
29379 closed it, so we can't be in one anymore. */
29380 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29381 if (maybe_template_id < 0)
29382 maybe_template_id = 0;
29383 }
29384 break;
29385
29386 /* If we run out of tokens, issue an error message. */
29387 case CPP_EOF:
29388 case CPP_PRAGMA_EOL:
29389 error_at (token->location, "file ends in default argument");
29390 return error_mark_node;
29391
29392 case CPP_NAME:
29393 case CPP_SCOPE:
29394 /* In these cases, we should look for template-ids.
29395 For example, if the default argument is
29396 `X<int, double>()', we need to do name lookup to
29397 figure out whether or not `X' is a template; if
29398 so, the `,' does not end the default argument.
29399
29400 That is not yet done. */
29401 break;
29402
29403 default:
29404 break;
29405 }
29406
29407 /* If we've reached the end, stop. */
29408 if (done)
29409 break;
29410
29411 /* Add the token to the token block. */
29412 token = cp_lexer_consume_token (parser->lexer);
29413 }
29414
29415 /* Create a DEFAULT_ARG to represent the unparsed default
29416 argument. */
29417 default_argument = make_node (DEFAULT_ARG);
29418 DEFARG_TOKENS (default_argument)
29419 = cp_token_cache_new (first_token, token);
29420 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29421
29422 return default_argument;
29423 }
29424
29425 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29426
29427 location_t
29428 defarg_location (tree default_argument)
29429 {
29430 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29431 location_t start = tokens->first->location;
29432 location_t end = tokens->last->location;
29433 return make_location (start, start, end);
29434 }
29435
29436 /* Begin parsing tentatively. We always save tokens while parsing
29437 tentatively so that if the tentative parsing fails we can restore the
29438 tokens. */
29439
29440 static void
29441 cp_parser_parse_tentatively (cp_parser* parser)
29442 {
29443 /* Enter a new parsing context. */
29444 parser->context = cp_parser_context_new (parser->context);
29445 /* Begin saving tokens. */
29446 cp_lexer_save_tokens (parser->lexer);
29447 /* In order to avoid repetitive access control error messages,
29448 access checks are queued up until we are no longer parsing
29449 tentatively. */
29450 push_deferring_access_checks (dk_deferred);
29451 }
29452
29453 /* Commit to the currently active tentative parse. */
29454
29455 static void
29456 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29457 {
29458 cp_parser_context *context;
29459 cp_lexer *lexer;
29460
29461 /* Mark all of the levels as committed. */
29462 lexer = parser->lexer;
29463 for (context = parser->context; context->next; context = context->next)
29464 {
29465 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29466 break;
29467 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29468 while (!cp_lexer_saving_tokens (lexer))
29469 lexer = lexer->next;
29470 cp_lexer_commit_tokens (lexer);
29471 }
29472 }
29473
29474 /* Commit to the topmost currently active tentative parse.
29475
29476 Note that this function shouldn't be called when there are
29477 irreversible side-effects while in a tentative state. For
29478 example, we shouldn't create a permanent entry in the symbol
29479 table, or issue an error message that might not apply if the
29480 tentative parse is aborted. */
29481
29482 static void
29483 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29484 {
29485 cp_parser_context *context = parser->context;
29486 cp_lexer *lexer = parser->lexer;
29487
29488 if (context)
29489 {
29490 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29491 return;
29492 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29493
29494 while (!cp_lexer_saving_tokens (lexer))
29495 lexer = lexer->next;
29496 cp_lexer_commit_tokens (lexer);
29497 }
29498 }
29499
29500 /* Abort the currently active tentative parse. All consumed tokens
29501 will be rolled back, and no diagnostics will be issued. */
29502
29503 static void
29504 cp_parser_abort_tentative_parse (cp_parser* parser)
29505 {
29506 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29507 || errorcount > 0);
29508 cp_parser_simulate_error (parser);
29509 /* Now, pretend that we want to see if the construct was
29510 successfully parsed. */
29511 cp_parser_parse_definitely (parser);
29512 }
29513
29514 /* Stop parsing tentatively. If a parse error has occurred, restore the
29515 token stream. Otherwise, commit to the tokens we have consumed.
29516 Returns true if no error occurred; false otherwise. */
29517
29518 static bool
29519 cp_parser_parse_definitely (cp_parser* parser)
29520 {
29521 bool error_occurred;
29522 cp_parser_context *context;
29523
29524 /* Remember whether or not an error occurred, since we are about to
29525 destroy that information. */
29526 error_occurred = cp_parser_error_occurred (parser);
29527 /* Remove the topmost context from the stack. */
29528 context = parser->context;
29529 parser->context = context->next;
29530 /* If no parse errors occurred, commit to the tentative parse. */
29531 if (!error_occurred)
29532 {
29533 /* Commit to the tokens read tentatively, unless that was
29534 already done. */
29535 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29536 cp_lexer_commit_tokens (parser->lexer);
29537
29538 pop_to_parent_deferring_access_checks ();
29539 }
29540 /* Otherwise, if errors occurred, roll back our state so that things
29541 are just as they were before we began the tentative parse. */
29542 else
29543 {
29544 cp_lexer_rollback_tokens (parser->lexer);
29545 pop_deferring_access_checks ();
29546 }
29547 /* Add the context to the front of the free list. */
29548 context->next = cp_parser_context_free_list;
29549 cp_parser_context_free_list = context;
29550
29551 return !error_occurred;
29552 }
29553
29554 /* Returns true if we are parsing tentatively and are not committed to
29555 this tentative parse. */
29556
29557 static bool
29558 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29559 {
29560 return (cp_parser_parsing_tentatively (parser)
29561 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29562 }
29563
29564 /* Returns nonzero iff an error has occurred during the most recent
29565 tentative parse. */
29566
29567 static bool
29568 cp_parser_error_occurred (cp_parser* parser)
29569 {
29570 return (cp_parser_parsing_tentatively (parser)
29571 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29572 }
29573
29574 /* Returns nonzero if GNU extensions are allowed. */
29575
29576 static bool
29577 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29578 {
29579 return parser->allow_gnu_extensions_p;
29580 }
29581 \f
29582 /* Objective-C++ Productions */
29583
29584
29585 /* Parse an Objective-C expression, which feeds into a primary-expression
29586 above.
29587
29588 objc-expression:
29589 objc-message-expression
29590 objc-string-literal
29591 objc-encode-expression
29592 objc-protocol-expression
29593 objc-selector-expression
29594
29595 Returns a tree representation of the expression. */
29596
29597 static cp_expr
29598 cp_parser_objc_expression (cp_parser* parser)
29599 {
29600 /* Try to figure out what kind of declaration is present. */
29601 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29602
29603 switch (kwd->type)
29604 {
29605 case CPP_OPEN_SQUARE:
29606 return cp_parser_objc_message_expression (parser);
29607
29608 case CPP_OBJC_STRING:
29609 kwd = cp_lexer_consume_token (parser->lexer);
29610 return objc_build_string_object (kwd->u.value);
29611
29612 case CPP_KEYWORD:
29613 switch (kwd->keyword)
29614 {
29615 case RID_AT_ENCODE:
29616 return cp_parser_objc_encode_expression (parser);
29617
29618 case RID_AT_PROTOCOL:
29619 return cp_parser_objc_protocol_expression (parser);
29620
29621 case RID_AT_SELECTOR:
29622 return cp_parser_objc_selector_expression (parser);
29623
29624 default:
29625 break;
29626 }
29627 /* FALLTHRU */
29628 default:
29629 error_at (kwd->location,
29630 "misplaced %<@%D%> Objective-C++ construct",
29631 kwd->u.value);
29632 cp_parser_skip_to_end_of_block_or_statement (parser);
29633 }
29634
29635 return error_mark_node;
29636 }
29637
29638 /* Parse an Objective-C message expression.
29639
29640 objc-message-expression:
29641 [ objc-message-receiver objc-message-args ]
29642
29643 Returns a representation of an Objective-C message. */
29644
29645 static tree
29646 cp_parser_objc_message_expression (cp_parser* parser)
29647 {
29648 tree receiver, messageargs;
29649
29650 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29651 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29652 receiver = cp_parser_objc_message_receiver (parser);
29653 messageargs = cp_parser_objc_message_args (parser);
29654 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29655 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29656
29657 tree result = objc_build_message_expr (receiver, messageargs);
29658
29659 /* Construct a location e.g.
29660 [self func1:5]
29661 ^~~~~~~~~~~~~~
29662 ranging from the '[' to the ']', with the caret at the start. */
29663 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29664 protected_set_expr_location (result, combined_loc);
29665
29666 return result;
29667 }
29668
29669 /* Parse an objc-message-receiver.
29670
29671 objc-message-receiver:
29672 expression
29673 simple-type-specifier
29674
29675 Returns a representation of the type or expression. */
29676
29677 static tree
29678 cp_parser_objc_message_receiver (cp_parser* parser)
29679 {
29680 tree rcv;
29681
29682 /* An Objective-C message receiver may be either (1) a type
29683 or (2) an expression. */
29684 cp_parser_parse_tentatively (parser);
29685 rcv = cp_parser_expression (parser);
29686
29687 /* If that worked out, fine. */
29688 if (cp_parser_parse_definitely (parser))
29689 return rcv;
29690
29691 cp_parser_parse_tentatively (parser);
29692 rcv = cp_parser_simple_type_specifier (parser,
29693 /*decl_specs=*/NULL,
29694 CP_PARSER_FLAGS_NONE);
29695
29696 if (cp_parser_parse_definitely (parser))
29697 return objc_get_class_reference (rcv);
29698
29699 cp_parser_error (parser, "objective-c++ message receiver expected");
29700 return error_mark_node;
29701 }
29702
29703 /* Parse the arguments and selectors comprising an Objective-C message.
29704
29705 objc-message-args:
29706 objc-selector
29707 objc-selector-args
29708 objc-selector-args , objc-comma-args
29709
29710 objc-selector-args:
29711 objc-selector [opt] : assignment-expression
29712 objc-selector-args objc-selector [opt] : assignment-expression
29713
29714 objc-comma-args:
29715 assignment-expression
29716 objc-comma-args , assignment-expression
29717
29718 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29719 selector arguments and TREE_VALUE containing a list of comma
29720 arguments. */
29721
29722 static tree
29723 cp_parser_objc_message_args (cp_parser* parser)
29724 {
29725 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29726 bool maybe_unary_selector_p = true;
29727 cp_token *token = cp_lexer_peek_token (parser->lexer);
29728
29729 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29730 {
29731 tree selector = NULL_TREE, arg;
29732
29733 if (token->type != CPP_COLON)
29734 selector = cp_parser_objc_selector (parser);
29735
29736 /* Detect if we have a unary selector. */
29737 if (maybe_unary_selector_p
29738 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29739 return build_tree_list (selector, NULL_TREE);
29740
29741 maybe_unary_selector_p = false;
29742 cp_parser_require (parser, CPP_COLON, RT_COLON);
29743 arg = cp_parser_assignment_expression (parser);
29744
29745 sel_args
29746 = chainon (sel_args,
29747 build_tree_list (selector, arg));
29748
29749 token = cp_lexer_peek_token (parser->lexer);
29750 }
29751
29752 /* Handle non-selector arguments, if any. */
29753 while (token->type == CPP_COMMA)
29754 {
29755 tree arg;
29756
29757 cp_lexer_consume_token (parser->lexer);
29758 arg = cp_parser_assignment_expression (parser);
29759
29760 addl_args
29761 = chainon (addl_args,
29762 build_tree_list (NULL_TREE, arg));
29763
29764 token = cp_lexer_peek_token (parser->lexer);
29765 }
29766
29767 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29768 {
29769 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29770 return build_tree_list (error_mark_node, error_mark_node);
29771 }
29772
29773 return build_tree_list (sel_args, addl_args);
29774 }
29775
29776 /* Parse an Objective-C encode expression.
29777
29778 objc-encode-expression:
29779 @encode objc-typename
29780
29781 Returns an encoded representation of the type argument. */
29782
29783 static cp_expr
29784 cp_parser_objc_encode_expression (cp_parser* parser)
29785 {
29786 tree type;
29787 cp_token *token;
29788 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29789
29790 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29791 matching_parens parens;
29792 parens.require_open (parser);
29793 token = cp_lexer_peek_token (parser->lexer);
29794 type = complete_type (cp_parser_type_id (parser));
29795 parens.require_close (parser);
29796
29797 if (!type)
29798 {
29799 error_at (token->location,
29800 "%<@encode%> must specify a type as an argument");
29801 return error_mark_node;
29802 }
29803
29804 /* This happens if we find @encode(T) (where T is a template
29805 typename or something dependent on a template typename) when
29806 parsing a template. In that case, we can't compile it
29807 immediately, but we rather create an AT_ENCODE_EXPR which will
29808 need to be instantiated when the template is used.
29809 */
29810 if (dependent_type_p (type))
29811 {
29812 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29813 TREE_READONLY (value) = 1;
29814 return value;
29815 }
29816
29817
29818 /* Build a location of the form:
29819 @encode(int)
29820 ^~~~~~~~~~~~
29821 with caret==start at the @ token, finishing at the close paren. */
29822 location_t combined_loc
29823 = make_location (start_loc, start_loc,
29824 cp_lexer_previous_token (parser->lexer)->location);
29825
29826 return cp_expr (objc_build_encode_expr (type), combined_loc);
29827 }
29828
29829 /* Parse an Objective-C @defs expression. */
29830
29831 static tree
29832 cp_parser_objc_defs_expression (cp_parser *parser)
29833 {
29834 tree name;
29835
29836 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29837 matching_parens parens;
29838 parens.require_open (parser);
29839 name = cp_parser_identifier (parser);
29840 parens.require_close (parser);
29841
29842 return objc_get_class_ivars (name);
29843 }
29844
29845 /* Parse an Objective-C protocol expression.
29846
29847 objc-protocol-expression:
29848 @protocol ( identifier )
29849
29850 Returns a representation of the protocol expression. */
29851
29852 static tree
29853 cp_parser_objc_protocol_expression (cp_parser* parser)
29854 {
29855 tree proto;
29856 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29857
29858 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29859 matching_parens parens;
29860 parens.require_open (parser);
29861 proto = cp_parser_identifier (parser);
29862 parens.require_close (parser);
29863
29864 /* Build a location of the form:
29865 @protocol(prot)
29866 ^~~~~~~~~~~~~~~
29867 with caret==start at the @ token, finishing at the close paren. */
29868 location_t combined_loc
29869 = make_location (start_loc, start_loc,
29870 cp_lexer_previous_token (parser->lexer)->location);
29871 tree result = objc_build_protocol_expr (proto);
29872 protected_set_expr_location (result, combined_loc);
29873 return result;
29874 }
29875
29876 /* Parse an Objective-C selector expression.
29877
29878 objc-selector-expression:
29879 @selector ( objc-method-signature )
29880
29881 objc-method-signature:
29882 objc-selector
29883 objc-selector-seq
29884
29885 objc-selector-seq:
29886 objc-selector :
29887 objc-selector-seq objc-selector :
29888
29889 Returns a representation of the method selector. */
29890
29891 static tree
29892 cp_parser_objc_selector_expression (cp_parser* parser)
29893 {
29894 tree sel_seq = NULL_TREE;
29895 bool maybe_unary_selector_p = true;
29896 cp_token *token;
29897 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29898
29899 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29900 matching_parens parens;
29901 parens.require_open (parser);
29902 token = cp_lexer_peek_token (parser->lexer);
29903
29904 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29905 || token->type == CPP_SCOPE)
29906 {
29907 tree selector = NULL_TREE;
29908
29909 if (token->type != CPP_COLON
29910 || token->type == CPP_SCOPE)
29911 selector = cp_parser_objc_selector (parser);
29912
29913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29914 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29915 {
29916 /* Detect if we have a unary selector. */
29917 if (maybe_unary_selector_p)
29918 {
29919 sel_seq = selector;
29920 goto finish_selector;
29921 }
29922 else
29923 {
29924 cp_parser_error (parser, "expected %<:%>");
29925 }
29926 }
29927 maybe_unary_selector_p = false;
29928 token = cp_lexer_consume_token (parser->lexer);
29929
29930 if (token->type == CPP_SCOPE)
29931 {
29932 sel_seq
29933 = chainon (sel_seq,
29934 build_tree_list (selector, NULL_TREE));
29935 sel_seq
29936 = chainon (sel_seq,
29937 build_tree_list (NULL_TREE, NULL_TREE));
29938 }
29939 else
29940 sel_seq
29941 = chainon (sel_seq,
29942 build_tree_list (selector, NULL_TREE));
29943
29944 token = cp_lexer_peek_token (parser->lexer);
29945 }
29946
29947 finish_selector:
29948 parens.require_close (parser);
29949
29950
29951 /* Build a location of the form:
29952 @selector(func)
29953 ^~~~~~~~~~~~~~~
29954 with caret==start at the @ token, finishing at the close paren. */
29955 location_t combined_loc
29956 = make_location (loc, loc,
29957 cp_lexer_previous_token (parser->lexer)->location);
29958 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29959 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29960 protected_set_expr_location (result, combined_loc);
29961 return result;
29962 }
29963
29964 /* Parse a list of identifiers.
29965
29966 objc-identifier-list:
29967 identifier
29968 objc-identifier-list , identifier
29969
29970 Returns a TREE_LIST of identifier nodes. */
29971
29972 static tree
29973 cp_parser_objc_identifier_list (cp_parser* parser)
29974 {
29975 tree identifier;
29976 tree list;
29977 cp_token *sep;
29978
29979 identifier = cp_parser_identifier (parser);
29980 if (identifier == error_mark_node)
29981 return error_mark_node;
29982
29983 list = build_tree_list (NULL_TREE, identifier);
29984 sep = cp_lexer_peek_token (parser->lexer);
29985
29986 while (sep->type == CPP_COMMA)
29987 {
29988 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29989 identifier = cp_parser_identifier (parser);
29990 if (identifier == error_mark_node)
29991 return list;
29992
29993 list = chainon (list, build_tree_list (NULL_TREE,
29994 identifier));
29995 sep = cp_lexer_peek_token (parser->lexer);
29996 }
29997
29998 return list;
29999 }
30000
30001 /* Parse an Objective-C alias declaration.
30002
30003 objc-alias-declaration:
30004 @compatibility_alias identifier identifier ;
30005
30006 This function registers the alias mapping with the Objective-C front end.
30007 It returns nothing. */
30008
30009 static void
30010 cp_parser_objc_alias_declaration (cp_parser* parser)
30011 {
30012 tree alias, orig;
30013
30014 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30015 alias = cp_parser_identifier (parser);
30016 orig = cp_parser_identifier (parser);
30017 objc_declare_alias (alias, orig);
30018 cp_parser_consume_semicolon_at_end_of_statement (parser);
30019 }
30020
30021 /* Parse an Objective-C class forward-declaration.
30022
30023 objc-class-declaration:
30024 @class objc-identifier-list ;
30025
30026 The function registers the forward declarations with the Objective-C
30027 front end. It returns nothing. */
30028
30029 static void
30030 cp_parser_objc_class_declaration (cp_parser* parser)
30031 {
30032 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30033 while (true)
30034 {
30035 tree id;
30036
30037 id = cp_parser_identifier (parser);
30038 if (id == error_mark_node)
30039 break;
30040
30041 objc_declare_class (id);
30042
30043 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30044 cp_lexer_consume_token (parser->lexer);
30045 else
30046 break;
30047 }
30048 cp_parser_consume_semicolon_at_end_of_statement (parser);
30049 }
30050
30051 /* Parse a list of Objective-C protocol references.
30052
30053 objc-protocol-refs-opt:
30054 objc-protocol-refs [opt]
30055
30056 objc-protocol-refs:
30057 < objc-identifier-list >
30058
30059 Returns a TREE_LIST of identifiers, if any. */
30060
30061 static tree
30062 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30063 {
30064 tree protorefs = NULL_TREE;
30065
30066 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30067 {
30068 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30069 protorefs = cp_parser_objc_identifier_list (parser);
30070 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30071 }
30072
30073 return protorefs;
30074 }
30075
30076 /* Parse a Objective-C visibility specification. */
30077
30078 static void
30079 cp_parser_objc_visibility_spec (cp_parser* parser)
30080 {
30081 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30082
30083 switch (vis->keyword)
30084 {
30085 case RID_AT_PRIVATE:
30086 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30087 break;
30088 case RID_AT_PROTECTED:
30089 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30090 break;
30091 case RID_AT_PUBLIC:
30092 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30093 break;
30094 case RID_AT_PACKAGE:
30095 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30096 break;
30097 default:
30098 return;
30099 }
30100
30101 /* Eat '@private'/'@protected'/'@public'. */
30102 cp_lexer_consume_token (parser->lexer);
30103 }
30104
30105 /* Parse an Objective-C method type. Return 'true' if it is a class
30106 (+) method, and 'false' if it is an instance (-) method. */
30107
30108 static inline bool
30109 cp_parser_objc_method_type (cp_parser* parser)
30110 {
30111 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30112 return true;
30113 else
30114 return false;
30115 }
30116
30117 /* Parse an Objective-C protocol qualifier. */
30118
30119 static tree
30120 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30121 {
30122 tree quals = NULL_TREE, node;
30123 cp_token *token = cp_lexer_peek_token (parser->lexer);
30124
30125 node = token->u.value;
30126
30127 while (node && identifier_p (node)
30128 && (node == ridpointers [(int) RID_IN]
30129 || node == ridpointers [(int) RID_OUT]
30130 || node == ridpointers [(int) RID_INOUT]
30131 || node == ridpointers [(int) RID_BYCOPY]
30132 || node == ridpointers [(int) RID_BYREF]
30133 || node == ridpointers [(int) RID_ONEWAY]))
30134 {
30135 quals = tree_cons (NULL_TREE, node, quals);
30136 cp_lexer_consume_token (parser->lexer);
30137 token = cp_lexer_peek_token (parser->lexer);
30138 node = token->u.value;
30139 }
30140
30141 return quals;
30142 }
30143
30144 /* Parse an Objective-C typename. */
30145
30146 static tree
30147 cp_parser_objc_typename (cp_parser* parser)
30148 {
30149 tree type_name = NULL_TREE;
30150
30151 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30152 {
30153 tree proto_quals, cp_type = NULL_TREE;
30154
30155 matching_parens parens;
30156 parens.consume_open (parser); /* Eat '('. */
30157 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30158
30159 /* An ObjC type name may consist of just protocol qualifiers, in which
30160 case the type shall default to 'id'. */
30161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30162 {
30163 cp_type = cp_parser_type_id (parser);
30164
30165 /* If the type could not be parsed, an error has already
30166 been produced. For error recovery, behave as if it had
30167 not been specified, which will use the default type
30168 'id'. */
30169 if (cp_type == error_mark_node)
30170 {
30171 cp_type = NULL_TREE;
30172 /* We need to skip to the closing parenthesis as
30173 cp_parser_type_id() does not seem to do it for
30174 us. */
30175 cp_parser_skip_to_closing_parenthesis (parser,
30176 /*recovering=*/true,
30177 /*or_comma=*/false,
30178 /*consume_paren=*/false);
30179 }
30180 }
30181
30182 parens.require_close (parser);
30183 type_name = build_tree_list (proto_quals, cp_type);
30184 }
30185
30186 return type_name;
30187 }
30188
30189 /* Check to see if TYPE refers to an Objective-C selector name. */
30190
30191 static bool
30192 cp_parser_objc_selector_p (enum cpp_ttype type)
30193 {
30194 return (type == CPP_NAME || type == CPP_KEYWORD
30195 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30196 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30197 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30198 || type == CPP_XOR || type == CPP_XOR_EQ);
30199 }
30200
30201 /* Parse an Objective-C selector. */
30202
30203 static tree
30204 cp_parser_objc_selector (cp_parser* parser)
30205 {
30206 cp_token *token = cp_lexer_consume_token (parser->lexer);
30207
30208 if (!cp_parser_objc_selector_p (token->type))
30209 {
30210 error_at (token->location, "invalid Objective-C++ selector name");
30211 return error_mark_node;
30212 }
30213
30214 /* C++ operator names are allowed to appear in ObjC selectors. */
30215 switch (token->type)
30216 {
30217 case CPP_AND_AND: return get_identifier ("and");
30218 case CPP_AND_EQ: return get_identifier ("and_eq");
30219 case CPP_AND: return get_identifier ("bitand");
30220 case CPP_OR: return get_identifier ("bitor");
30221 case CPP_COMPL: return get_identifier ("compl");
30222 case CPP_NOT: return get_identifier ("not");
30223 case CPP_NOT_EQ: return get_identifier ("not_eq");
30224 case CPP_OR_OR: return get_identifier ("or");
30225 case CPP_OR_EQ: return get_identifier ("or_eq");
30226 case CPP_XOR: return get_identifier ("xor");
30227 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30228 default: return token->u.value;
30229 }
30230 }
30231
30232 /* Parse an Objective-C params list. */
30233
30234 static tree
30235 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30236 {
30237 tree params = NULL_TREE;
30238 bool maybe_unary_selector_p = true;
30239 cp_token *token = cp_lexer_peek_token (parser->lexer);
30240
30241 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30242 {
30243 tree selector = NULL_TREE, type_name, identifier;
30244 tree parm_attr = NULL_TREE;
30245
30246 if (token->keyword == RID_ATTRIBUTE)
30247 break;
30248
30249 if (token->type != CPP_COLON)
30250 selector = cp_parser_objc_selector (parser);
30251
30252 /* Detect if we have a unary selector. */
30253 if (maybe_unary_selector_p
30254 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30255 {
30256 params = selector; /* Might be followed by attributes. */
30257 break;
30258 }
30259
30260 maybe_unary_selector_p = false;
30261 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30262 {
30263 /* Something went quite wrong. There should be a colon
30264 here, but there is not. Stop parsing parameters. */
30265 break;
30266 }
30267 type_name = cp_parser_objc_typename (parser);
30268 /* New ObjC allows attributes on parameters too. */
30269 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30270 parm_attr = cp_parser_attributes_opt (parser);
30271 identifier = cp_parser_identifier (parser);
30272
30273 params
30274 = chainon (params,
30275 objc_build_keyword_decl (selector,
30276 type_name,
30277 identifier,
30278 parm_attr));
30279
30280 token = cp_lexer_peek_token (parser->lexer);
30281 }
30282
30283 if (params == NULL_TREE)
30284 {
30285 cp_parser_error (parser, "objective-c++ method declaration is expected");
30286 return error_mark_node;
30287 }
30288
30289 /* We allow tail attributes for the method. */
30290 if (token->keyword == RID_ATTRIBUTE)
30291 {
30292 *attributes = cp_parser_attributes_opt (parser);
30293 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30294 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30295 return params;
30296 cp_parser_error (parser,
30297 "method attributes must be specified at the end");
30298 return error_mark_node;
30299 }
30300
30301 if (params == NULL_TREE)
30302 {
30303 cp_parser_error (parser, "objective-c++ method declaration is expected");
30304 return error_mark_node;
30305 }
30306 return params;
30307 }
30308
30309 /* Parse the non-keyword Objective-C params. */
30310
30311 static tree
30312 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30313 tree* attributes)
30314 {
30315 tree params = make_node (TREE_LIST);
30316 cp_token *token = cp_lexer_peek_token (parser->lexer);
30317 *ellipsisp = false; /* Initially, assume no ellipsis. */
30318
30319 while (token->type == CPP_COMMA)
30320 {
30321 cp_parameter_declarator *parmdecl;
30322 tree parm;
30323
30324 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30325 token = cp_lexer_peek_token (parser->lexer);
30326
30327 if (token->type == CPP_ELLIPSIS)
30328 {
30329 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30330 *ellipsisp = true;
30331 token = cp_lexer_peek_token (parser->lexer);
30332 break;
30333 }
30334
30335 /* TODO: parse attributes for tail parameters. */
30336 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30337 parm = grokdeclarator (parmdecl->declarator,
30338 &parmdecl->decl_specifiers,
30339 PARM, /*initialized=*/0,
30340 /*attrlist=*/NULL);
30341
30342 chainon (params, build_tree_list (NULL_TREE, parm));
30343 token = cp_lexer_peek_token (parser->lexer);
30344 }
30345
30346 /* We allow tail attributes for the method. */
30347 if (token->keyword == RID_ATTRIBUTE)
30348 {
30349 if (*attributes == NULL_TREE)
30350 {
30351 *attributes = cp_parser_attributes_opt (parser);
30352 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30353 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30354 return params;
30355 }
30356 else
30357 /* We have an error, but parse the attributes, so that we can
30358 carry on. */
30359 *attributes = cp_parser_attributes_opt (parser);
30360
30361 cp_parser_error (parser,
30362 "method attributes must be specified at the end");
30363 return error_mark_node;
30364 }
30365
30366 return params;
30367 }
30368
30369 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30370
30371 static void
30372 cp_parser_objc_interstitial_code (cp_parser* parser)
30373 {
30374 cp_token *token = cp_lexer_peek_token (parser->lexer);
30375
30376 /* If the next token is `extern' and the following token is a string
30377 literal, then we have a linkage specification. */
30378 if (token->keyword == RID_EXTERN
30379 && cp_parser_is_pure_string_literal
30380 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30381 cp_parser_linkage_specification (parser);
30382 /* Handle #pragma, if any. */
30383 else if (token->type == CPP_PRAGMA)
30384 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30385 /* Allow stray semicolons. */
30386 else if (token->type == CPP_SEMICOLON)
30387 cp_lexer_consume_token (parser->lexer);
30388 /* Mark methods as optional or required, when building protocols. */
30389 else if (token->keyword == RID_AT_OPTIONAL)
30390 {
30391 cp_lexer_consume_token (parser->lexer);
30392 objc_set_method_opt (true);
30393 }
30394 else if (token->keyword == RID_AT_REQUIRED)
30395 {
30396 cp_lexer_consume_token (parser->lexer);
30397 objc_set_method_opt (false);
30398 }
30399 else if (token->keyword == RID_NAMESPACE)
30400 cp_parser_namespace_definition (parser);
30401 /* Other stray characters must generate errors. */
30402 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30403 {
30404 cp_lexer_consume_token (parser->lexer);
30405 error ("stray %qs between Objective-C++ methods",
30406 token->type == CPP_OPEN_BRACE ? "{" : "}");
30407 }
30408 /* Finally, try to parse a block-declaration, or a function-definition. */
30409 else
30410 cp_parser_block_declaration (parser, /*statement_p=*/false);
30411 }
30412
30413 /* Parse a method signature. */
30414
30415 static tree
30416 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30417 {
30418 tree rettype, kwdparms, optparms;
30419 bool ellipsis = false;
30420 bool is_class_method;
30421
30422 is_class_method = cp_parser_objc_method_type (parser);
30423 rettype = cp_parser_objc_typename (parser);
30424 *attributes = NULL_TREE;
30425 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30426 if (kwdparms == error_mark_node)
30427 return error_mark_node;
30428 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30429 if (optparms == error_mark_node)
30430 return error_mark_node;
30431
30432 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30433 }
30434
30435 static bool
30436 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30437 {
30438 tree tattr;
30439 cp_lexer_save_tokens (parser->lexer);
30440 tattr = cp_parser_attributes_opt (parser);
30441 gcc_assert (tattr) ;
30442
30443 /* If the attributes are followed by a method introducer, this is not allowed.
30444 Dump the attributes and flag the situation. */
30445 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30446 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30447 return true;
30448
30449 /* Otherwise, the attributes introduce some interstitial code, possibly so
30450 rewind to allow that check. */
30451 cp_lexer_rollback_tokens (parser->lexer);
30452 return false;
30453 }
30454
30455 /* Parse an Objective-C method prototype list. */
30456
30457 static void
30458 cp_parser_objc_method_prototype_list (cp_parser* parser)
30459 {
30460 cp_token *token = cp_lexer_peek_token (parser->lexer);
30461
30462 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30463 {
30464 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30465 {
30466 tree attributes, sig;
30467 bool is_class_method;
30468 if (token->type == CPP_PLUS)
30469 is_class_method = true;
30470 else
30471 is_class_method = false;
30472 sig = cp_parser_objc_method_signature (parser, &attributes);
30473 if (sig == error_mark_node)
30474 {
30475 cp_parser_skip_to_end_of_block_or_statement (parser);
30476 token = cp_lexer_peek_token (parser->lexer);
30477 continue;
30478 }
30479 objc_add_method_declaration (is_class_method, sig, attributes);
30480 cp_parser_consume_semicolon_at_end_of_statement (parser);
30481 }
30482 else if (token->keyword == RID_AT_PROPERTY)
30483 cp_parser_objc_at_property_declaration (parser);
30484 else if (token->keyword == RID_ATTRIBUTE
30485 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30486 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30487 OPT_Wattributes,
30488 "prefix attributes are ignored for methods");
30489 else
30490 /* Allow for interspersed non-ObjC++ code. */
30491 cp_parser_objc_interstitial_code (parser);
30492
30493 token = cp_lexer_peek_token (parser->lexer);
30494 }
30495
30496 if (token->type != CPP_EOF)
30497 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30498 else
30499 cp_parser_error (parser, "expected %<@end%>");
30500
30501 objc_finish_interface ();
30502 }
30503
30504 /* Parse an Objective-C method definition list. */
30505
30506 static void
30507 cp_parser_objc_method_definition_list (cp_parser* parser)
30508 {
30509 cp_token *token = cp_lexer_peek_token (parser->lexer);
30510
30511 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30512 {
30513 tree meth;
30514
30515 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30516 {
30517 cp_token *ptk;
30518 tree sig, attribute;
30519 bool is_class_method;
30520 if (token->type == CPP_PLUS)
30521 is_class_method = true;
30522 else
30523 is_class_method = false;
30524 push_deferring_access_checks (dk_deferred);
30525 sig = cp_parser_objc_method_signature (parser, &attribute);
30526 if (sig == error_mark_node)
30527 {
30528 cp_parser_skip_to_end_of_block_or_statement (parser);
30529 token = cp_lexer_peek_token (parser->lexer);
30530 continue;
30531 }
30532 objc_start_method_definition (is_class_method, sig, attribute,
30533 NULL_TREE);
30534
30535 /* For historical reasons, we accept an optional semicolon. */
30536 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30537 cp_lexer_consume_token (parser->lexer);
30538
30539 ptk = cp_lexer_peek_token (parser->lexer);
30540 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30541 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30542 {
30543 perform_deferred_access_checks (tf_warning_or_error);
30544 stop_deferring_access_checks ();
30545 meth = cp_parser_function_definition_after_declarator (parser,
30546 false);
30547 pop_deferring_access_checks ();
30548 objc_finish_method_definition (meth);
30549 }
30550 }
30551 /* The following case will be removed once @synthesize is
30552 completely implemented. */
30553 else if (token->keyword == RID_AT_PROPERTY)
30554 cp_parser_objc_at_property_declaration (parser);
30555 else if (token->keyword == RID_AT_SYNTHESIZE)
30556 cp_parser_objc_at_synthesize_declaration (parser);
30557 else if (token->keyword == RID_AT_DYNAMIC)
30558 cp_parser_objc_at_dynamic_declaration (parser);
30559 else if (token->keyword == RID_ATTRIBUTE
30560 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30561 warning_at (token->location, OPT_Wattributes,
30562 "prefix attributes are ignored for methods");
30563 else
30564 /* Allow for interspersed non-ObjC++ code. */
30565 cp_parser_objc_interstitial_code (parser);
30566
30567 token = cp_lexer_peek_token (parser->lexer);
30568 }
30569
30570 if (token->type != CPP_EOF)
30571 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30572 else
30573 cp_parser_error (parser, "expected %<@end%>");
30574
30575 objc_finish_implementation ();
30576 }
30577
30578 /* Parse Objective-C ivars. */
30579
30580 static void
30581 cp_parser_objc_class_ivars (cp_parser* parser)
30582 {
30583 cp_token *token = cp_lexer_peek_token (parser->lexer);
30584
30585 if (token->type != CPP_OPEN_BRACE)
30586 return; /* No ivars specified. */
30587
30588 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30589 token = cp_lexer_peek_token (parser->lexer);
30590
30591 while (token->type != CPP_CLOSE_BRACE
30592 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30593 {
30594 cp_decl_specifier_seq declspecs;
30595 int decl_class_or_enum_p;
30596 tree prefix_attributes;
30597
30598 cp_parser_objc_visibility_spec (parser);
30599
30600 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30601 break;
30602
30603 cp_parser_decl_specifier_seq (parser,
30604 CP_PARSER_FLAGS_OPTIONAL,
30605 &declspecs,
30606 &decl_class_or_enum_p);
30607
30608 /* auto, register, static, extern, mutable. */
30609 if (declspecs.storage_class != sc_none)
30610 {
30611 cp_parser_error (parser, "invalid type for instance variable");
30612 declspecs.storage_class = sc_none;
30613 }
30614
30615 /* thread_local. */
30616 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30617 {
30618 cp_parser_error (parser, "invalid type for instance variable");
30619 declspecs.locations[ds_thread] = 0;
30620 }
30621
30622 /* typedef. */
30623 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30624 {
30625 cp_parser_error (parser, "invalid type for instance variable");
30626 declspecs.locations[ds_typedef] = 0;
30627 }
30628
30629 prefix_attributes = declspecs.attributes;
30630 declspecs.attributes = NULL_TREE;
30631
30632 /* Keep going until we hit the `;' at the end of the
30633 declaration. */
30634 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30635 {
30636 tree width = NULL_TREE, attributes, first_attribute, decl;
30637 cp_declarator *declarator = NULL;
30638 int ctor_dtor_or_conv_p;
30639
30640 /* Check for a (possibly unnamed) bitfield declaration. */
30641 token = cp_lexer_peek_token (parser->lexer);
30642 if (token->type == CPP_COLON)
30643 goto eat_colon;
30644
30645 if (token->type == CPP_NAME
30646 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30647 == CPP_COLON))
30648 {
30649 /* Get the name of the bitfield. */
30650 declarator = make_id_declarator (NULL_TREE,
30651 cp_parser_identifier (parser),
30652 sfk_none, token->location);
30653
30654 eat_colon:
30655 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30656 /* Get the width of the bitfield. */
30657 width
30658 = cp_parser_constant_expression (parser);
30659 }
30660 else
30661 {
30662 /* Parse the declarator. */
30663 declarator
30664 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30665 &ctor_dtor_or_conv_p,
30666 /*parenthesized_p=*/NULL,
30667 /*member_p=*/false,
30668 /*friend_p=*/false);
30669 }
30670
30671 /* Look for attributes that apply to the ivar. */
30672 attributes = cp_parser_attributes_opt (parser);
30673 /* Remember which attributes are prefix attributes and
30674 which are not. */
30675 first_attribute = attributes;
30676 /* Combine the attributes. */
30677 attributes = attr_chainon (prefix_attributes, attributes);
30678
30679 if (width)
30680 /* Create the bitfield declaration. */
30681 decl = grokbitfield (declarator, &declspecs,
30682 width, NULL_TREE, attributes);
30683 else
30684 decl = grokfield (declarator, &declspecs,
30685 NULL_TREE, /*init_const_expr_p=*/false,
30686 NULL_TREE, attributes);
30687
30688 /* Add the instance variable. */
30689 if (decl != error_mark_node && decl != NULL_TREE)
30690 objc_add_instance_variable (decl);
30691
30692 /* Reset PREFIX_ATTRIBUTES. */
30693 if (attributes != error_mark_node)
30694 {
30695 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30696 attributes = TREE_CHAIN (attributes);
30697 if (attributes)
30698 TREE_CHAIN (attributes) = NULL_TREE;
30699 }
30700
30701 token = cp_lexer_peek_token (parser->lexer);
30702
30703 if (token->type == CPP_COMMA)
30704 {
30705 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30706 continue;
30707 }
30708 break;
30709 }
30710
30711 cp_parser_consume_semicolon_at_end_of_statement (parser);
30712 token = cp_lexer_peek_token (parser->lexer);
30713 }
30714
30715 if (token->keyword == RID_AT_END)
30716 cp_parser_error (parser, "expected %<}%>");
30717
30718 /* Do not consume the RID_AT_END, so it will be read again as terminating
30719 the @interface of @implementation. */
30720 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30721 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30722
30723 /* For historical reasons, we accept an optional semicolon. */
30724 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30725 cp_lexer_consume_token (parser->lexer);
30726 }
30727
30728 /* Parse an Objective-C protocol declaration. */
30729
30730 static void
30731 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30732 {
30733 tree proto, protorefs;
30734 cp_token *tok;
30735
30736 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30738 {
30739 tok = cp_lexer_peek_token (parser->lexer);
30740 error_at (tok->location, "identifier expected after %<@protocol%>");
30741 cp_parser_consume_semicolon_at_end_of_statement (parser);
30742 return;
30743 }
30744
30745 /* See if we have a forward declaration or a definition. */
30746 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30747
30748 /* Try a forward declaration first. */
30749 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30750 {
30751 while (true)
30752 {
30753 tree id;
30754
30755 id = cp_parser_identifier (parser);
30756 if (id == error_mark_node)
30757 break;
30758
30759 objc_declare_protocol (id, attributes);
30760
30761 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30762 cp_lexer_consume_token (parser->lexer);
30763 else
30764 break;
30765 }
30766 cp_parser_consume_semicolon_at_end_of_statement (parser);
30767 }
30768
30769 /* Ok, we got a full-fledged definition (or at least should). */
30770 else
30771 {
30772 proto = cp_parser_identifier (parser);
30773 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30774 objc_start_protocol (proto, protorefs, attributes);
30775 cp_parser_objc_method_prototype_list (parser);
30776 }
30777 }
30778
30779 /* Parse an Objective-C superclass or category. */
30780
30781 static void
30782 cp_parser_objc_superclass_or_category (cp_parser *parser,
30783 bool iface_p,
30784 tree *super,
30785 tree *categ, bool *is_class_extension)
30786 {
30787 cp_token *next = cp_lexer_peek_token (parser->lexer);
30788
30789 *super = *categ = NULL_TREE;
30790 *is_class_extension = false;
30791 if (next->type == CPP_COLON)
30792 {
30793 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30794 *super = cp_parser_identifier (parser);
30795 }
30796 else if (next->type == CPP_OPEN_PAREN)
30797 {
30798 matching_parens parens;
30799 parens.consume_open (parser); /* Eat '('. */
30800
30801 /* If there is no category name, and this is an @interface, we
30802 have a class extension. */
30803 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30804 {
30805 *categ = NULL_TREE;
30806 *is_class_extension = true;
30807 }
30808 else
30809 *categ = cp_parser_identifier (parser);
30810
30811 parens.require_close (parser);
30812 }
30813 }
30814
30815 /* Parse an Objective-C class interface. */
30816
30817 static void
30818 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30819 {
30820 tree name, super, categ, protos;
30821 bool is_class_extension;
30822
30823 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30824 name = cp_parser_identifier (parser);
30825 if (name == error_mark_node)
30826 {
30827 /* It's hard to recover because even if valid @interface stuff
30828 is to follow, we can't compile it (or validate it) if we
30829 don't even know which class it refers to. Let's assume this
30830 was a stray '@interface' token in the stream and skip it.
30831 */
30832 return;
30833 }
30834 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30835 &is_class_extension);
30836 protos = cp_parser_objc_protocol_refs_opt (parser);
30837
30838 /* We have either a class or a category on our hands. */
30839 if (categ || is_class_extension)
30840 objc_start_category_interface (name, categ, protos, attributes);
30841 else
30842 {
30843 objc_start_class_interface (name, super, protos, attributes);
30844 /* Handle instance variable declarations, if any. */
30845 cp_parser_objc_class_ivars (parser);
30846 objc_continue_interface ();
30847 }
30848
30849 cp_parser_objc_method_prototype_list (parser);
30850 }
30851
30852 /* Parse an Objective-C class implementation. */
30853
30854 static void
30855 cp_parser_objc_class_implementation (cp_parser* parser)
30856 {
30857 tree name, super, categ;
30858 bool is_class_extension;
30859
30860 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30861 name = cp_parser_identifier (parser);
30862 if (name == error_mark_node)
30863 {
30864 /* It's hard to recover because even if valid @implementation
30865 stuff is to follow, we can't compile it (or validate it) if
30866 we don't even know which class it refers to. Let's assume
30867 this was a stray '@implementation' token in the stream and
30868 skip it.
30869 */
30870 return;
30871 }
30872 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30873 &is_class_extension);
30874
30875 /* We have either a class or a category on our hands. */
30876 if (categ)
30877 objc_start_category_implementation (name, categ);
30878 else
30879 {
30880 objc_start_class_implementation (name, super);
30881 /* Handle instance variable declarations, if any. */
30882 cp_parser_objc_class_ivars (parser);
30883 objc_continue_implementation ();
30884 }
30885
30886 cp_parser_objc_method_definition_list (parser);
30887 }
30888
30889 /* Consume the @end token and finish off the implementation. */
30890
30891 static void
30892 cp_parser_objc_end_implementation (cp_parser* parser)
30893 {
30894 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30895 objc_finish_implementation ();
30896 }
30897
30898 /* Parse an Objective-C declaration. */
30899
30900 static void
30901 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30902 {
30903 /* Try to figure out what kind of declaration is present. */
30904 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30905
30906 if (attributes)
30907 switch (kwd->keyword)
30908 {
30909 case RID_AT_ALIAS:
30910 case RID_AT_CLASS:
30911 case RID_AT_END:
30912 error_at (kwd->location, "attributes may not be specified before"
30913 " the %<@%D%> Objective-C++ keyword",
30914 kwd->u.value);
30915 attributes = NULL;
30916 break;
30917 case RID_AT_IMPLEMENTATION:
30918 warning_at (kwd->location, OPT_Wattributes,
30919 "prefix attributes are ignored before %<@%D%>",
30920 kwd->u.value);
30921 attributes = NULL;
30922 default:
30923 break;
30924 }
30925
30926 switch (kwd->keyword)
30927 {
30928 case RID_AT_ALIAS:
30929 cp_parser_objc_alias_declaration (parser);
30930 break;
30931 case RID_AT_CLASS:
30932 cp_parser_objc_class_declaration (parser);
30933 break;
30934 case RID_AT_PROTOCOL:
30935 cp_parser_objc_protocol_declaration (parser, attributes);
30936 break;
30937 case RID_AT_INTERFACE:
30938 cp_parser_objc_class_interface (parser, attributes);
30939 break;
30940 case RID_AT_IMPLEMENTATION:
30941 cp_parser_objc_class_implementation (parser);
30942 break;
30943 case RID_AT_END:
30944 cp_parser_objc_end_implementation (parser);
30945 break;
30946 default:
30947 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30948 kwd->u.value);
30949 cp_parser_skip_to_end_of_block_or_statement (parser);
30950 }
30951 }
30952
30953 /* Parse an Objective-C try-catch-finally statement.
30954
30955 objc-try-catch-finally-stmt:
30956 @try compound-statement objc-catch-clause-seq [opt]
30957 objc-finally-clause [opt]
30958
30959 objc-catch-clause-seq:
30960 objc-catch-clause objc-catch-clause-seq [opt]
30961
30962 objc-catch-clause:
30963 @catch ( objc-exception-declaration ) compound-statement
30964
30965 objc-finally-clause:
30966 @finally compound-statement
30967
30968 objc-exception-declaration:
30969 parameter-declaration
30970 '...'
30971
30972 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30973
30974 Returns NULL_TREE.
30975
30976 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30977 for C. Keep them in sync. */
30978
30979 static tree
30980 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30981 {
30982 location_t location;
30983 tree stmt;
30984
30985 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30986 location = cp_lexer_peek_token (parser->lexer)->location;
30987 objc_maybe_warn_exceptions (location);
30988 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30989 node, lest it get absorbed into the surrounding block. */
30990 stmt = push_stmt_list ();
30991 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30992 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30993
30994 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30995 {
30996 cp_parameter_declarator *parm;
30997 tree parameter_declaration = error_mark_node;
30998 bool seen_open_paren = false;
30999 matching_parens parens;
31000
31001 cp_lexer_consume_token (parser->lexer);
31002 if (parens.require_open (parser))
31003 seen_open_paren = true;
31004 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31005 {
31006 /* We have "@catch (...)" (where the '...' are literally
31007 what is in the code). Skip the '...'.
31008 parameter_declaration is set to NULL_TREE, and
31009 objc_being_catch_clauses() knows that that means
31010 '...'. */
31011 cp_lexer_consume_token (parser->lexer);
31012 parameter_declaration = NULL_TREE;
31013 }
31014 else
31015 {
31016 /* We have "@catch (NSException *exception)" or something
31017 like that. Parse the parameter declaration. */
31018 parm = cp_parser_parameter_declaration (parser, false, NULL);
31019 if (parm == NULL)
31020 parameter_declaration = error_mark_node;
31021 else
31022 parameter_declaration = grokdeclarator (parm->declarator,
31023 &parm->decl_specifiers,
31024 PARM, /*initialized=*/0,
31025 /*attrlist=*/NULL);
31026 }
31027 if (seen_open_paren)
31028 parens.require_close (parser);
31029 else
31030 {
31031 /* If there was no open parenthesis, we are recovering from
31032 an error, and we are trying to figure out what mistake
31033 the user has made. */
31034
31035 /* If there is an immediate closing parenthesis, the user
31036 probably forgot the opening one (ie, they typed "@catch
31037 NSException *e)". Parse the closing parenthesis and keep
31038 going. */
31039 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31040 cp_lexer_consume_token (parser->lexer);
31041
31042 /* If these is no immediate closing parenthesis, the user
31043 probably doesn't know that parenthesis are required at
31044 all (ie, they typed "@catch NSException *e"). So, just
31045 forget about the closing parenthesis and keep going. */
31046 }
31047 objc_begin_catch_clause (parameter_declaration);
31048 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31049 objc_finish_catch_clause ();
31050 }
31051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31052 {
31053 cp_lexer_consume_token (parser->lexer);
31054 location = cp_lexer_peek_token (parser->lexer)->location;
31055 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31056 node, lest it get absorbed into the surrounding block. */
31057 stmt = push_stmt_list ();
31058 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31059 objc_build_finally_clause (location, pop_stmt_list (stmt));
31060 }
31061
31062 return objc_finish_try_stmt ();
31063 }
31064
31065 /* Parse an Objective-C synchronized statement.
31066
31067 objc-synchronized-stmt:
31068 @synchronized ( expression ) compound-statement
31069
31070 Returns NULL_TREE. */
31071
31072 static tree
31073 cp_parser_objc_synchronized_statement (cp_parser *parser)
31074 {
31075 location_t location;
31076 tree lock, stmt;
31077
31078 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31079
31080 location = cp_lexer_peek_token (parser->lexer)->location;
31081 objc_maybe_warn_exceptions (location);
31082 matching_parens parens;
31083 parens.require_open (parser);
31084 lock = cp_parser_expression (parser);
31085 parens.require_close (parser);
31086
31087 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31088 node, lest it get absorbed into the surrounding block. */
31089 stmt = push_stmt_list ();
31090 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31091
31092 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31093 }
31094
31095 /* Parse an Objective-C throw statement.
31096
31097 objc-throw-stmt:
31098 @throw assignment-expression [opt] ;
31099
31100 Returns a constructed '@throw' statement. */
31101
31102 static tree
31103 cp_parser_objc_throw_statement (cp_parser *parser)
31104 {
31105 tree expr = NULL_TREE;
31106 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31107
31108 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31109
31110 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31111 expr = cp_parser_expression (parser);
31112
31113 cp_parser_consume_semicolon_at_end_of_statement (parser);
31114
31115 return objc_build_throw_stmt (loc, expr);
31116 }
31117
31118 /* Parse an Objective-C statement. */
31119
31120 static tree
31121 cp_parser_objc_statement (cp_parser * parser)
31122 {
31123 /* Try to figure out what kind of declaration is present. */
31124 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31125
31126 switch (kwd->keyword)
31127 {
31128 case RID_AT_TRY:
31129 return cp_parser_objc_try_catch_finally_statement (parser);
31130 case RID_AT_SYNCHRONIZED:
31131 return cp_parser_objc_synchronized_statement (parser);
31132 case RID_AT_THROW:
31133 return cp_parser_objc_throw_statement (parser);
31134 default:
31135 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31136 kwd->u.value);
31137 cp_parser_skip_to_end_of_block_or_statement (parser);
31138 }
31139
31140 return error_mark_node;
31141 }
31142
31143 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31144 look ahead to see if an objc keyword follows the attributes. This
31145 is to detect the use of prefix attributes on ObjC @interface and
31146 @protocol. */
31147
31148 static bool
31149 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31150 {
31151 cp_lexer_save_tokens (parser->lexer);
31152 *attrib = cp_parser_attributes_opt (parser);
31153 gcc_assert (*attrib);
31154 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31155 {
31156 cp_lexer_commit_tokens (parser->lexer);
31157 return true;
31158 }
31159 cp_lexer_rollback_tokens (parser->lexer);
31160 return false;
31161 }
31162
31163 /* This routine is a minimal replacement for
31164 c_parser_struct_declaration () used when parsing the list of
31165 types/names or ObjC++ properties. For example, when parsing the
31166 code
31167
31168 @property (readonly) int a, b, c;
31169
31170 this function is responsible for parsing "int a, int b, int c" and
31171 returning the declarations as CHAIN of DECLs.
31172
31173 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31174 similar parsing. */
31175 static tree
31176 cp_parser_objc_struct_declaration (cp_parser *parser)
31177 {
31178 tree decls = NULL_TREE;
31179 cp_decl_specifier_seq declspecs;
31180 int decl_class_or_enum_p;
31181 tree prefix_attributes;
31182
31183 cp_parser_decl_specifier_seq (parser,
31184 CP_PARSER_FLAGS_NONE,
31185 &declspecs,
31186 &decl_class_or_enum_p);
31187
31188 if (declspecs.type == error_mark_node)
31189 return error_mark_node;
31190
31191 /* auto, register, static, extern, mutable. */
31192 if (declspecs.storage_class != sc_none)
31193 {
31194 cp_parser_error (parser, "invalid type for property");
31195 declspecs.storage_class = sc_none;
31196 }
31197
31198 /* thread_local. */
31199 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31200 {
31201 cp_parser_error (parser, "invalid type for property");
31202 declspecs.locations[ds_thread] = 0;
31203 }
31204
31205 /* typedef. */
31206 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31207 {
31208 cp_parser_error (parser, "invalid type for property");
31209 declspecs.locations[ds_typedef] = 0;
31210 }
31211
31212 prefix_attributes = declspecs.attributes;
31213 declspecs.attributes = NULL_TREE;
31214
31215 /* Keep going until we hit the `;' at the end of the declaration. */
31216 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31217 {
31218 tree attributes, first_attribute, decl;
31219 cp_declarator *declarator;
31220 cp_token *token;
31221
31222 /* Parse the declarator. */
31223 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31224 NULL, NULL, false, false);
31225
31226 /* Look for attributes that apply to the ivar. */
31227 attributes = cp_parser_attributes_opt (parser);
31228 /* Remember which attributes are prefix attributes and
31229 which are not. */
31230 first_attribute = attributes;
31231 /* Combine the attributes. */
31232 attributes = attr_chainon (prefix_attributes, attributes);
31233
31234 decl = grokfield (declarator, &declspecs,
31235 NULL_TREE, /*init_const_expr_p=*/false,
31236 NULL_TREE, attributes);
31237
31238 if (decl == error_mark_node || decl == NULL_TREE)
31239 return error_mark_node;
31240
31241 /* Reset PREFIX_ATTRIBUTES. */
31242 if (attributes != error_mark_node)
31243 {
31244 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31245 attributes = TREE_CHAIN (attributes);
31246 if (attributes)
31247 TREE_CHAIN (attributes) = NULL_TREE;
31248 }
31249
31250 DECL_CHAIN (decl) = decls;
31251 decls = decl;
31252
31253 token = cp_lexer_peek_token (parser->lexer);
31254 if (token->type == CPP_COMMA)
31255 {
31256 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31257 continue;
31258 }
31259 else
31260 break;
31261 }
31262 return decls;
31263 }
31264
31265 /* Parse an Objective-C @property declaration. The syntax is:
31266
31267 objc-property-declaration:
31268 '@property' objc-property-attributes[opt] struct-declaration ;
31269
31270 objc-property-attributes:
31271 '(' objc-property-attribute-list ')'
31272
31273 objc-property-attribute-list:
31274 objc-property-attribute
31275 objc-property-attribute-list, objc-property-attribute
31276
31277 objc-property-attribute
31278 'getter' = identifier
31279 'setter' = identifier
31280 'readonly'
31281 'readwrite'
31282 'assign'
31283 'retain'
31284 'copy'
31285 'nonatomic'
31286
31287 For example:
31288 @property NSString *name;
31289 @property (readonly) id object;
31290 @property (retain, nonatomic, getter=getTheName) id name;
31291 @property int a, b, c;
31292
31293 PS: This function is identical to
31294 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31295 static void
31296 cp_parser_objc_at_property_declaration (cp_parser *parser)
31297 {
31298 /* The following variables hold the attributes of the properties as
31299 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31300 seen. When we see an attribute, we set them to 'true' (if they
31301 are boolean properties) or to the identifier (if they have an
31302 argument, ie, for getter and setter). Note that here we only
31303 parse the list of attributes, check the syntax and accumulate the
31304 attributes that we find. objc_add_property_declaration() will
31305 then process the information. */
31306 bool property_assign = false;
31307 bool property_copy = false;
31308 tree property_getter_ident = NULL_TREE;
31309 bool property_nonatomic = false;
31310 bool property_readonly = false;
31311 bool property_readwrite = false;
31312 bool property_retain = false;
31313 tree property_setter_ident = NULL_TREE;
31314
31315 /* 'properties' is the list of properties that we read. Usually a
31316 single one, but maybe more (eg, in "@property int a, b, c;" there
31317 are three). */
31318 tree properties;
31319 location_t loc;
31320
31321 loc = cp_lexer_peek_token (parser->lexer)->location;
31322
31323 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31324
31325 /* Parse the optional attribute list... */
31326 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31327 {
31328 /* Eat the '('. */
31329 matching_parens parens;
31330 parens.consume_open (parser);
31331
31332 while (true)
31333 {
31334 bool syntax_error = false;
31335 cp_token *token = cp_lexer_peek_token (parser->lexer);
31336 enum rid keyword;
31337
31338 if (token->type != CPP_NAME)
31339 {
31340 cp_parser_error (parser, "expected identifier");
31341 break;
31342 }
31343 keyword = C_RID_CODE (token->u.value);
31344 cp_lexer_consume_token (parser->lexer);
31345 switch (keyword)
31346 {
31347 case RID_ASSIGN: property_assign = true; break;
31348 case RID_COPY: property_copy = true; break;
31349 case RID_NONATOMIC: property_nonatomic = true; break;
31350 case RID_READONLY: property_readonly = true; break;
31351 case RID_READWRITE: property_readwrite = true; break;
31352 case RID_RETAIN: property_retain = true; break;
31353
31354 case RID_GETTER:
31355 case RID_SETTER:
31356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31357 {
31358 if (keyword == RID_GETTER)
31359 cp_parser_error (parser,
31360 "missing %<=%> (after %<getter%> attribute)");
31361 else
31362 cp_parser_error (parser,
31363 "missing %<=%> (after %<setter%> attribute)");
31364 syntax_error = true;
31365 break;
31366 }
31367 cp_lexer_consume_token (parser->lexer); /* eat the = */
31368 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31369 {
31370 cp_parser_error (parser, "expected identifier");
31371 syntax_error = true;
31372 break;
31373 }
31374 if (keyword == RID_SETTER)
31375 {
31376 if (property_setter_ident != NULL_TREE)
31377 {
31378 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31379 cp_lexer_consume_token (parser->lexer);
31380 }
31381 else
31382 property_setter_ident = cp_parser_objc_selector (parser);
31383 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31384 cp_parser_error (parser, "setter name must terminate with %<:%>");
31385 else
31386 cp_lexer_consume_token (parser->lexer);
31387 }
31388 else
31389 {
31390 if (property_getter_ident != NULL_TREE)
31391 {
31392 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31393 cp_lexer_consume_token (parser->lexer);
31394 }
31395 else
31396 property_getter_ident = cp_parser_objc_selector (parser);
31397 }
31398 break;
31399 default:
31400 cp_parser_error (parser, "unknown property attribute");
31401 syntax_error = true;
31402 break;
31403 }
31404
31405 if (syntax_error)
31406 break;
31407
31408 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31409 cp_lexer_consume_token (parser->lexer);
31410 else
31411 break;
31412 }
31413
31414 /* FIXME: "@property (setter, assign);" will generate a spurious
31415 "error: expected ‘)’ before ‘,’ token". This is because
31416 cp_parser_require, unlike the C counterpart, will produce an
31417 error even if we are in error recovery. */
31418 if (!parens.require_close (parser))
31419 {
31420 cp_parser_skip_to_closing_parenthesis (parser,
31421 /*recovering=*/true,
31422 /*or_comma=*/false,
31423 /*consume_paren=*/true);
31424 }
31425 }
31426
31427 /* ... and the property declaration(s). */
31428 properties = cp_parser_objc_struct_declaration (parser);
31429
31430 if (properties == error_mark_node)
31431 {
31432 cp_parser_skip_to_end_of_statement (parser);
31433 /* If the next token is now a `;', consume it. */
31434 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31435 cp_lexer_consume_token (parser->lexer);
31436 return;
31437 }
31438
31439 if (properties == NULL_TREE)
31440 cp_parser_error (parser, "expected identifier");
31441 else
31442 {
31443 /* Comma-separated properties are chained together in
31444 reverse order; add them one by one. */
31445 properties = nreverse (properties);
31446
31447 for (; properties; properties = TREE_CHAIN (properties))
31448 objc_add_property_declaration (loc, copy_node (properties),
31449 property_readonly, property_readwrite,
31450 property_assign, property_retain,
31451 property_copy, property_nonatomic,
31452 property_getter_ident, property_setter_ident);
31453 }
31454
31455 cp_parser_consume_semicolon_at_end_of_statement (parser);
31456 }
31457
31458 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31459
31460 objc-synthesize-declaration:
31461 @synthesize objc-synthesize-identifier-list ;
31462
31463 objc-synthesize-identifier-list:
31464 objc-synthesize-identifier
31465 objc-synthesize-identifier-list, objc-synthesize-identifier
31466
31467 objc-synthesize-identifier
31468 identifier
31469 identifier = identifier
31470
31471 For example:
31472 @synthesize MyProperty;
31473 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31474
31475 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31476 for C. Keep them in sync.
31477 */
31478 static void
31479 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31480 {
31481 tree list = NULL_TREE;
31482 location_t loc;
31483 loc = cp_lexer_peek_token (parser->lexer)->location;
31484
31485 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31486 while (true)
31487 {
31488 tree property, ivar;
31489 property = cp_parser_identifier (parser);
31490 if (property == error_mark_node)
31491 {
31492 cp_parser_consume_semicolon_at_end_of_statement (parser);
31493 return;
31494 }
31495 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31496 {
31497 cp_lexer_consume_token (parser->lexer);
31498 ivar = cp_parser_identifier (parser);
31499 if (ivar == error_mark_node)
31500 {
31501 cp_parser_consume_semicolon_at_end_of_statement (parser);
31502 return;
31503 }
31504 }
31505 else
31506 ivar = NULL_TREE;
31507 list = chainon (list, build_tree_list (ivar, property));
31508 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31509 cp_lexer_consume_token (parser->lexer);
31510 else
31511 break;
31512 }
31513 cp_parser_consume_semicolon_at_end_of_statement (parser);
31514 objc_add_synthesize_declaration (loc, list);
31515 }
31516
31517 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31518
31519 objc-dynamic-declaration:
31520 @dynamic identifier-list ;
31521
31522 For example:
31523 @dynamic MyProperty;
31524 @dynamic MyProperty, AnotherProperty;
31525
31526 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31527 for C. Keep them in sync.
31528 */
31529 static void
31530 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31531 {
31532 tree list = NULL_TREE;
31533 location_t loc;
31534 loc = cp_lexer_peek_token (parser->lexer)->location;
31535
31536 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31537 while (true)
31538 {
31539 tree property;
31540 property = cp_parser_identifier (parser);
31541 if (property == error_mark_node)
31542 {
31543 cp_parser_consume_semicolon_at_end_of_statement (parser);
31544 return;
31545 }
31546 list = chainon (list, build_tree_list (NULL, property));
31547 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31548 cp_lexer_consume_token (parser->lexer);
31549 else
31550 break;
31551 }
31552 cp_parser_consume_semicolon_at_end_of_statement (parser);
31553 objc_add_dynamic_declaration (loc, list);
31554 }
31555
31556 \f
31557 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31558
31559 /* Returns name of the next clause.
31560 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31561 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31562 returned and the token is consumed. */
31563
31564 static pragma_omp_clause
31565 cp_parser_omp_clause_name (cp_parser *parser)
31566 {
31567 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31568
31569 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31570 result = PRAGMA_OACC_CLAUSE_AUTO;
31571 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31572 result = PRAGMA_OMP_CLAUSE_IF;
31573 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31574 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31575 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31576 result = PRAGMA_OACC_CLAUSE_DELETE;
31577 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31578 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31579 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31580 result = PRAGMA_OMP_CLAUSE_FOR;
31581 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31582 {
31583 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31584 const char *p = IDENTIFIER_POINTER (id);
31585
31586 switch (p[0])
31587 {
31588 case 'a':
31589 if (!strcmp ("aligned", p))
31590 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31591 else if (!strcmp ("async", p))
31592 result = PRAGMA_OACC_CLAUSE_ASYNC;
31593 break;
31594 case 'c':
31595 if (!strcmp ("collapse", p))
31596 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31597 else if (!strcmp ("copy", p))
31598 result = PRAGMA_OACC_CLAUSE_COPY;
31599 else if (!strcmp ("copyin", p))
31600 result = PRAGMA_OMP_CLAUSE_COPYIN;
31601 else if (!strcmp ("copyout", p))
31602 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31603 else if (!strcmp ("copyprivate", p))
31604 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31605 else if (!strcmp ("create", p))
31606 result = PRAGMA_OACC_CLAUSE_CREATE;
31607 break;
31608 case 'd':
31609 if (!strcmp ("defaultmap", p))
31610 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31611 else if (!strcmp ("depend", p))
31612 result = PRAGMA_OMP_CLAUSE_DEPEND;
31613 else if (!strcmp ("device", p))
31614 result = PRAGMA_OMP_CLAUSE_DEVICE;
31615 else if (!strcmp ("deviceptr", p))
31616 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31617 else if (!strcmp ("device_resident", p))
31618 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31619 else if (!strcmp ("dist_schedule", p))
31620 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31621 break;
31622 case 'f':
31623 if (!strcmp ("final", p))
31624 result = PRAGMA_OMP_CLAUSE_FINAL;
31625 else if (!strcmp ("finalize", p))
31626 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31627 else if (!strcmp ("firstprivate", p))
31628 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31629 else if (!strcmp ("from", p))
31630 result = PRAGMA_OMP_CLAUSE_FROM;
31631 break;
31632 case 'g':
31633 if (!strcmp ("gang", p))
31634 result = PRAGMA_OACC_CLAUSE_GANG;
31635 else if (!strcmp ("grainsize", p))
31636 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31637 break;
31638 case 'h':
31639 if (!strcmp ("hint", p))
31640 result = PRAGMA_OMP_CLAUSE_HINT;
31641 else if (!strcmp ("host", p))
31642 result = PRAGMA_OACC_CLAUSE_HOST;
31643 break;
31644 case 'i':
31645 if (!strcmp ("if_present", p))
31646 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31647 else if (!strcmp ("in_reduction", p))
31648 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
31649 else if (!strcmp ("inbranch", p))
31650 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31651 else if (!strcmp ("independent", p))
31652 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31653 else if (!strcmp ("is_device_ptr", p))
31654 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31655 break;
31656 case 'l':
31657 if (!strcmp ("lastprivate", p))
31658 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31659 else if (!strcmp ("linear", p))
31660 result = PRAGMA_OMP_CLAUSE_LINEAR;
31661 else if (!strcmp ("link", p))
31662 result = PRAGMA_OMP_CLAUSE_LINK;
31663 break;
31664 case 'm':
31665 if (!strcmp ("map", p))
31666 result = PRAGMA_OMP_CLAUSE_MAP;
31667 else if (!strcmp ("mergeable", p))
31668 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31669 break;
31670 case 'n':
31671 if (!strcmp ("nogroup", p))
31672 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31673 else if (!strcmp ("nontemporal", p))
31674 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
31675 else if (!strcmp ("notinbranch", p))
31676 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31677 else if (!strcmp ("nowait", p))
31678 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31679 else if (!strcmp ("num_gangs", p))
31680 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31681 else if (!strcmp ("num_tasks", p))
31682 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31683 else if (!strcmp ("num_teams", p))
31684 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31685 else if (!strcmp ("num_threads", p))
31686 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31687 else if (!strcmp ("num_workers", p))
31688 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31689 break;
31690 case 'o':
31691 if (!strcmp ("ordered", p))
31692 result = PRAGMA_OMP_CLAUSE_ORDERED;
31693 break;
31694 case 'p':
31695 if (!strcmp ("parallel", p))
31696 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31697 else if (!strcmp ("present", p))
31698 result = PRAGMA_OACC_CLAUSE_PRESENT;
31699 else if (!strcmp ("present_or_copy", p)
31700 || !strcmp ("pcopy", p))
31701 result = PRAGMA_OACC_CLAUSE_COPY;
31702 else if (!strcmp ("present_or_copyin", p)
31703 || !strcmp ("pcopyin", p))
31704 result = PRAGMA_OACC_CLAUSE_COPYIN;
31705 else if (!strcmp ("present_or_copyout", p)
31706 || !strcmp ("pcopyout", p))
31707 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31708 else if (!strcmp ("present_or_create", p)
31709 || !strcmp ("pcreate", p))
31710 result = PRAGMA_OACC_CLAUSE_CREATE;
31711 else if (!strcmp ("priority", p))
31712 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31713 else if (!strcmp ("proc_bind", p))
31714 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31715 break;
31716 case 'r':
31717 if (!strcmp ("reduction", p))
31718 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31719 break;
31720 case 's':
31721 if (!strcmp ("safelen", p))
31722 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31723 else if (!strcmp ("schedule", p))
31724 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31725 else if (!strcmp ("sections", p))
31726 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31727 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31728 result = PRAGMA_OACC_CLAUSE_HOST;
31729 else if (!strcmp ("seq", p))
31730 result = PRAGMA_OACC_CLAUSE_SEQ;
31731 else if (!strcmp ("shared", p))
31732 result = PRAGMA_OMP_CLAUSE_SHARED;
31733 else if (!strcmp ("simd", p))
31734 result = PRAGMA_OMP_CLAUSE_SIMD;
31735 else if (!strcmp ("simdlen", p))
31736 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31737 break;
31738 case 't':
31739 if (!strcmp ("task_reduction", p))
31740 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
31741 else if (!strcmp ("taskgroup", p))
31742 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31743 else if (!strcmp ("thread_limit", p))
31744 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31745 else if (!strcmp ("threads", p))
31746 result = PRAGMA_OMP_CLAUSE_THREADS;
31747 else if (!strcmp ("tile", p))
31748 result = PRAGMA_OACC_CLAUSE_TILE;
31749 else if (!strcmp ("to", p))
31750 result = PRAGMA_OMP_CLAUSE_TO;
31751 break;
31752 case 'u':
31753 if (!strcmp ("uniform", p))
31754 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31755 else if (!strcmp ("untied", p))
31756 result = PRAGMA_OMP_CLAUSE_UNTIED;
31757 else if (!strcmp ("use_device", p))
31758 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31759 else if (!strcmp ("use_device_ptr", p))
31760 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31761 break;
31762 case 'v':
31763 if (!strcmp ("vector", p))
31764 result = PRAGMA_OACC_CLAUSE_VECTOR;
31765 else if (!strcmp ("vector_length", p))
31766 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31767 break;
31768 case 'w':
31769 if (!strcmp ("wait", p))
31770 result = PRAGMA_OACC_CLAUSE_WAIT;
31771 else if (!strcmp ("worker", p))
31772 result = PRAGMA_OACC_CLAUSE_WORKER;
31773 break;
31774 }
31775 }
31776
31777 if (result != PRAGMA_OMP_CLAUSE_NONE)
31778 cp_lexer_consume_token (parser->lexer);
31779
31780 return result;
31781 }
31782
31783 /* Validate that a clause of the given type does not already exist. */
31784
31785 static void
31786 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31787 const char *name, location_t location)
31788 {
31789 tree c;
31790
31791 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31792 if (OMP_CLAUSE_CODE (c) == code)
31793 {
31794 error_at (location, "too many %qs clauses", name);
31795 break;
31796 }
31797 }
31798
31799 /* OpenMP 2.5:
31800 variable-list:
31801 identifier
31802 variable-list , identifier
31803
31804 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31805 colon). An opening parenthesis will have been consumed by the caller.
31806
31807 If KIND is nonzero, create the appropriate node and install the decl
31808 in OMP_CLAUSE_DECL and add the node to the head of the list.
31809
31810 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31811 return the list created.
31812
31813 COLON can be NULL if only closing parenthesis should end the list,
31814 or pointer to bool which will receive false if the list is terminated
31815 by closing parenthesis or true if the list is terminated by colon. */
31816
31817 static tree
31818 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31819 tree list, bool *colon)
31820 {
31821 cp_token *token;
31822 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31823 if (colon)
31824 {
31825 parser->colon_corrects_to_scope_p = false;
31826 *colon = false;
31827 }
31828 while (1)
31829 {
31830 tree name, decl;
31831
31832 if (kind == OMP_CLAUSE_DEPEND)
31833 cp_parser_parse_tentatively (parser);
31834 token = cp_lexer_peek_token (parser->lexer);
31835 if (kind != 0
31836 && current_class_ptr
31837 && cp_parser_is_keyword (token, RID_THIS))
31838 {
31839 decl = finish_this_expr ();
31840 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31841 || CONVERT_EXPR_P (decl))
31842 decl = TREE_OPERAND (decl, 0);
31843 cp_lexer_consume_token (parser->lexer);
31844 }
31845 else
31846 {
31847 name = cp_parser_id_expression (parser, /*template_p=*/false,
31848 /*check_dependency_p=*/true,
31849 /*template_p=*/NULL,
31850 /*declarator_p=*/false,
31851 /*optional_p=*/false);
31852 if (name == error_mark_node)
31853 {
31854 if (kind == OMP_CLAUSE_DEPEND
31855 && cp_parser_simulate_error (parser))
31856 goto depend_lvalue;
31857 goto skip_comma;
31858 }
31859
31860 if (identifier_p (name))
31861 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31862 else
31863 decl = name;
31864 if (decl == error_mark_node)
31865 {
31866 if (kind == OMP_CLAUSE_DEPEND
31867 && cp_parser_simulate_error (parser))
31868 goto depend_lvalue;
31869 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31870 token->location);
31871 }
31872 }
31873 if (decl == error_mark_node)
31874 ;
31875 else if (kind != 0)
31876 {
31877 switch (kind)
31878 {
31879 case OMP_CLAUSE__CACHE_:
31880 /* The OpenACC cache directive explicitly only allows "array
31881 elements or subarrays". */
31882 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31883 {
31884 error_at (token->location, "expected %<[%>");
31885 decl = error_mark_node;
31886 break;
31887 }
31888 /* FALLTHROUGH. */
31889 case OMP_CLAUSE_MAP:
31890 case OMP_CLAUSE_FROM:
31891 case OMP_CLAUSE_TO:
31892 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31893 {
31894 location_t loc
31895 = cp_lexer_peek_token (parser->lexer)->location;
31896 cp_id_kind idk = CP_ID_KIND_NONE;
31897 cp_lexer_consume_token (parser->lexer);
31898 decl = convert_from_reference (decl);
31899 decl
31900 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31901 decl, false,
31902 &idk, loc);
31903 }
31904 /* FALLTHROUGH. */
31905 case OMP_CLAUSE_DEPEND:
31906 case OMP_CLAUSE_REDUCTION:
31907 case OMP_CLAUSE_IN_REDUCTION:
31908 case OMP_CLAUSE_TASK_REDUCTION:
31909 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31910 {
31911 tree low_bound = NULL_TREE, length = NULL_TREE;
31912
31913 parser->colon_corrects_to_scope_p = false;
31914 cp_lexer_consume_token (parser->lexer);
31915 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31916 low_bound = cp_parser_expression (parser);
31917 if (!colon)
31918 parser->colon_corrects_to_scope_p
31919 = saved_colon_corrects_to_scope_p;
31920 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31921 length = integer_one_node;
31922 else
31923 {
31924 /* Look for `:'. */
31925 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31926 {
31927 if (kind == OMP_CLAUSE_DEPEND
31928 && cp_parser_simulate_error (parser))
31929 goto depend_lvalue;
31930 goto skip_comma;
31931 }
31932 if (kind == OMP_CLAUSE_DEPEND)
31933 cp_parser_commit_to_tentative_parse (parser);
31934 if (!cp_lexer_next_token_is (parser->lexer,
31935 CPP_CLOSE_SQUARE))
31936 length = cp_parser_expression (parser);
31937 }
31938 /* Look for the closing `]'. */
31939 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31940 RT_CLOSE_SQUARE))
31941 {
31942 if (kind == OMP_CLAUSE_DEPEND
31943 && cp_parser_simulate_error (parser))
31944 goto depend_lvalue;
31945 goto skip_comma;
31946 }
31947
31948 decl = tree_cons (low_bound, length, decl);
31949 }
31950 break;
31951 default:
31952 break;
31953 }
31954
31955 if (kind == OMP_CLAUSE_DEPEND)
31956 {
31957 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
31958 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31959 && cp_parser_simulate_error (parser))
31960 {
31961 depend_lvalue:
31962 cp_parser_abort_tentative_parse (parser);
31963 decl = cp_parser_assignment_expression (parser, NULL,
31964 false, false);
31965 }
31966 else
31967 cp_parser_parse_definitely (parser);
31968 }
31969
31970 tree u = build_omp_clause (token->location, kind);
31971 OMP_CLAUSE_DECL (u) = decl;
31972 OMP_CLAUSE_CHAIN (u) = list;
31973 list = u;
31974 }
31975 else
31976 list = tree_cons (decl, NULL_TREE, list);
31977
31978 get_comma:
31979 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31980 break;
31981 cp_lexer_consume_token (parser->lexer);
31982 }
31983
31984 if (colon)
31985 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31986
31987 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31988 {
31989 *colon = true;
31990 cp_parser_require (parser, CPP_COLON, RT_COLON);
31991 return list;
31992 }
31993
31994 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31995 {
31996 int ending;
31997
31998 /* Try to resync to an unnested comma. Copied from
31999 cp_parser_parenthesized_expression_list. */
32000 skip_comma:
32001 if (colon)
32002 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32003 ending = cp_parser_skip_to_closing_parenthesis (parser,
32004 /*recovering=*/true,
32005 /*or_comma=*/true,
32006 /*consume_paren=*/true);
32007 if (ending < 0)
32008 goto get_comma;
32009 }
32010
32011 return list;
32012 }
32013
32014 /* Similarly, but expect leading and trailing parenthesis. This is a very
32015 common case for omp clauses. */
32016
32017 static tree
32018 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32019 {
32020 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32021 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32022 return list;
32023 }
32024
32025 /* OpenACC 2.0:
32026 copy ( variable-list )
32027 copyin ( variable-list )
32028 copyout ( variable-list )
32029 create ( variable-list )
32030 delete ( variable-list )
32031 present ( variable-list ) */
32032
32033 static tree
32034 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32035 tree list)
32036 {
32037 enum gomp_map_kind kind;
32038 switch (c_kind)
32039 {
32040 case PRAGMA_OACC_CLAUSE_COPY:
32041 kind = GOMP_MAP_TOFROM;
32042 break;
32043 case PRAGMA_OACC_CLAUSE_COPYIN:
32044 kind = GOMP_MAP_TO;
32045 break;
32046 case PRAGMA_OACC_CLAUSE_COPYOUT:
32047 kind = GOMP_MAP_FROM;
32048 break;
32049 case PRAGMA_OACC_CLAUSE_CREATE:
32050 kind = GOMP_MAP_ALLOC;
32051 break;
32052 case PRAGMA_OACC_CLAUSE_DELETE:
32053 kind = GOMP_MAP_RELEASE;
32054 break;
32055 case PRAGMA_OACC_CLAUSE_DEVICE:
32056 kind = GOMP_MAP_FORCE_TO;
32057 break;
32058 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32059 kind = GOMP_MAP_DEVICE_RESIDENT;
32060 break;
32061 case PRAGMA_OACC_CLAUSE_HOST:
32062 kind = GOMP_MAP_FORCE_FROM;
32063 break;
32064 case PRAGMA_OACC_CLAUSE_LINK:
32065 kind = GOMP_MAP_LINK;
32066 break;
32067 case PRAGMA_OACC_CLAUSE_PRESENT:
32068 kind = GOMP_MAP_FORCE_PRESENT;
32069 break;
32070 default:
32071 gcc_unreachable ();
32072 }
32073 tree nl, c;
32074 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32075
32076 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32077 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32078
32079 return nl;
32080 }
32081
32082 /* OpenACC 2.0:
32083 deviceptr ( variable-list ) */
32084
32085 static tree
32086 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32087 {
32088 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32089 tree vars, t;
32090
32091 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32092 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32093 variable-list must only allow for pointer variables. */
32094 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32095 for (t = vars; t; t = TREE_CHAIN (t))
32096 {
32097 tree v = TREE_PURPOSE (t);
32098 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32099 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32100 OMP_CLAUSE_DECL (u) = v;
32101 OMP_CLAUSE_CHAIN (u) = list;
32102 list = u;
32103 }
32104
32105 return list;
32106 }
32107
32108 /* OpenACC 2.5:
32109 auto
32110 finalize
32111 independent
32112 nohost
32113 seq */
32114
32115 static tree
32116 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
32117 enum omp_clause_code code,
32118 tree list, location_t location)
32119 {
32120 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32121 tree c = build_omp_clause (location, code);
32122 OMP_CLAUSE_CHAIN (c) = list;
32123 return c;
32124 }
32125
32126 /* OpenACC:
32127 num_gangs ( expression )
32128 num_workers ( expression )
32129 vector_length ( expression ) */
32130
32131 static tree
32132 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32133 const char *str, tree list)
32134 {
32135 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32136
32137 matching_parens parens;
32138 if (!parens.require_open (parser))
32139 return list;
32140
32141 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32142
32143 if (t == error_mark_node
32144 || !parens.require_close (parser))
32145 {
32146 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32147 /*or_comma=*/false,
32148 /*consume_paren=*/true);
32149 return list;
32150 }
32151
32152 check_no_duplicate_clause (list, code, str, loc);
32153
32154 tree c = build_omp_clause (loc, code);
32155 OMP_CLAUSE_OPERAND (c, 0) = t;
32156 OMP_CLAUSE_CHAIN (c) = list;
32157 return c;
32158 }
32159
32160 /* OpenACC:
32161
32162 gang [( gang-arg-list )]
32163 worker [( [num:] int-expr )]
32164 vector [( [length:] int-expr )]
32165
32166 where gang-arg is one of:
32167
32168 [num:] int-expr
32169 static: size-expr
32170
32171 and size-expr may be:
32172
32173 *
32174 int-expr
32175 */
32176
32177 static tree
32178 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
32179 const char *str, tree list)
32180 {
32181 const char *id = "num";
32182 cp_lexer *lexer = parser->lexer;
32183 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32184 location_t loc = cp_lexer_peek_token (lexer)->location;
32185
32186 if (kind == OMP_CLAUSE_VECTOR)
32187 id = "length";
32188
32189 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32190 {
32191 matching_parens parens;
32192 parens.consume_open (parser);
32193
32194 do
32195 {
32196 cp_token *next = cp_lexer_peek_token (lexer);
32197 int idx = 0;
32198
32199 /* Gang static argument. */
32200 if (kind == OMP_CLAUSE_GANG
32201 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32202 {
32203 cp_lexer_consume_token (lexer);
32204
32205 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32206 goto cleanup_error;
32207
32208 idx = 1;
32209 if (ops[idx] != NULL)
32210 {
32211 cp_parser_error (parser, "too many %<static%> arguments");
32212 goto cleanup_error;
32213 }
32214
32215 /* Check for the '*' argument. */
32216 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32217 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32218 || cp_lexer_nth_token_is (parser->lexer, 2,
32219 CPP_CLOSE_PAREN)))
32220 {
32221 cp_lexer_consume_token (lexer);
32222 ops[idx] = integer_minus_one_node;
32223
32224 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32225 {
32226 cp_lexer_consume_token (lexer);
32227 continue;
32228 }
32229 else break;
32230 }
32231 }
32232 /* Worker num: argument and vector length: arguments. */
32233 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32234 && id_equal (next->u.value, id)
32235 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32236 {
32237 cp_lexer_consume_token (lexer); /* id */
32238 cp_lexer_consume_token (lexer); /* ':' */
32239 }
32240
32241 /* Now collect the actual argument. */
32242 if (ops[idx] != NULL_TREE)
32243 {
32244 cp_parser_error (parser, "unexpected argument");
32245 goto cleanup_error;
32246 }
32247
32248 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32249 false);
32250 if (expr == error_mark_node)
32251 goto cleanup_error;
32252
32253 mark_exp_read (expr);
32254 ops[idx] = expr;
32255
32256 if (kind == OMP_CLAUSE_GANG
32257 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32258 {
32259 cp_lexer_consume_token (lexer);
32260 continue;
32261 }
32262 break;
32263 }
32264 while (1);
32265
32266 if (!parens.require_close (parser))
32267 goto cleanup_error;
32268 }
32269
32270 check_no_duplicate_clause (list, kind, str, loc);
32271
32272 c = build_omp_clause (loc, kind);
32273
32274 if (ops[1])
32275 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32276
32277 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32278 OMP_CLAUSE_CHAIN (c) = list;
32279
32280 return c;
32281
32282 cleanup_error:
32283 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32284 return list;
32285 }
32286
32287 /* OpenACC 2.0:
32288 tile ( size-expr-list ) */
32289
32290 static tree
32291 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32292 {
32293 tree c, expr = error_mark_node;
32294 tree tile = NULL_TREE;
32295
32296 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32297 so, but the spec authors never considered such a case and have
32298 differing opinions on what it might mean, including 'not
32299 allowed'.) */
32300 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32301 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32302 clause_loc);
32303
32304 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32305 return list;
32306
32307 do
32308 {
32309 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32310 return list;
32311
32312 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32313 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32314 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32315 {
32316 cp_lexer_consume_token (parser->lexer);
32317 expr = integer_zero_node;
32318 }
32319 else
32320 expr = cp_parser_constant_expression (parser);
32321
32322 tile = tree_cons (NULL_TREE, expr, tile);
32323 }
32324 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32325
32326 /* Consume the trailing ')'. */
32327 cp_lexer_consume_token (parser->lexer);
32328
32329 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32330 tile = nreverse (tile);
32331 OMP_CLAUSE_TILE_LIST (c) = tile;
32332 OMP_CLAUSE_CHAIN (c) = list;
32333 return c;
32334 }
32335
32336 /* OpenACC 2.0
32337 Parse wait clause or directive parameters. */
32338
32339 static tree
32340 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32341 {
32342 vec<tree, va_gc> *args;
32343 tree t, args_tree;
32344
32345 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32346 /*cast_p=*/false,
32347 /*allow_expansion_p=*/true,
32348 /*non_constant_p=*/NULL);
32349
32350 if (args == NULL || args->length () == 0)
32351 {
32352 cp_parser_error (parser, "expected integer expression before ')'");
32353 if (args != NULL)
32354 release_tree_vector (args);
32355 return list;
32356 }
32357
32358 args_tree = build_tree_list_vec (args);
32359
32360 release_tree_vector (args);
32361
32362 for (t = args_tree; t; t = TREE_CHAIN (t))
32363 {
32364 tree targ = TREE_VALUE (t);
32365
32366 if (targ != error_mark_node)
32367 {
32368 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32369 error ("%<wait%> expression must be integral");
32370 else
32371 {
32372 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32373
32374 targ = mark_rvalue_use (targ);
32375 OMP_CLAUSE_DECL (c) = targ;
32376 OMP_CLAUSE_CHAIN (c) = list;
32377 list = c;
32378 }
32379 }
32380 }
32381
32382 return list;
32383 }
32384
32385 /* OpenACC:
32386 wait ( int-expr-list ) */
32387
32388 static tree
32389 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32390 {
32391 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32392
32393 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32394 return list;
32395
32396 list = cp_parser_oacc_wait_list (parser, location, list);
32397
32398 return list;
32399 }
32400
32401 /* OpenMP 3.0:
32402 collapse ( constant-expression ) */
32403
32404 static tree
32405 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32406 {
32407 tree c, num;
32408 location_t loc;
32409 HOST_WIDE_INT n;
32410
32411 loc = cp_lexer_peek_token (parser->lexer)->location;
32412 matching_parens parens;
32413 if (!parens.require_open (parser))
32414 return list;
32415
32416 num = cp_parser_constant_expression (parser);
32417
32418 if (!parens.require_close (parser))
32419 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32420 /*or_comma=*/false,
32421 /*consume_paren=*/true);
32422
32423 if (num == error_mark_node)
32424 return list;
32425 num = fold_non_dependent_expr (num);
32426 if (!tree_fits_shwi_p (num)
32427 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32428 || (n = tree_to_shwi (num)) <= 0
32429 || (int) n != n)
32430 {
32431 error_at (loc, "collapse argument needs positive constant integer expression");
32432 return list;
32433 }
32434
32435 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32436 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32437 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32438 OMP_CLAUSE_CHAIN (c) = list;
32439 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32440
32441 return c;
32442 }
32443
32444 /* OpenMP 2.5:
32445 default ( none | shared )
32446
32447 OpenACC:
32448 default ( none | present ) */
32449
32450 static tree
32451 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32452 location_t location, bool is_oacc)
32453 {
32454 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32455 tree c;
32456
32457 matching_parens parens;
32458 if (!parens.require_open (parser))
32459 return list;
32460 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32461 {
32462 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32463 const char *p = IDENTIFIER_POINTER (id);
32464
32465 switch (p[0])
32466 {
32467 case 'n':
32468 if (strcmp ("none", p) != 0)
32469 goto invalid_kind;
32470 kind = OMP_CLAUSE_DEFAULT_NONE;
32471 break;
32472
32473 case 'p':
32474 if (strcmp ("present", p) != 0 || !is_oacc)
32475 goto invalid_kind;
32476 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32477 break;
32478
32479 case 's':
32480 if (strcmp ("shared", p) != 0 || is_oacc)
32481 goto invalid_kind;
32482 kind = OMP_CLAUSE_DEFAULT_SHARED;
32483 break;
32484
32485 default:
32486 goto invalid_kind;
32487 }
32488
32489 cp_lexer_consume_token (parser->lexer);
32490 }
32491 else
32492 {
32493 invalid_kind:
32494 if (is_oacc)
32495 cp_parser_error (parser, "expected %<none%> or %<present%>");
32496 else
32497 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32498 }
32499
32500 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32501 || !parens.require_close (parser))
32502 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32503 /*or_comma=*/false,
32504 /*consume_paren=*/true);
32505
32506 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32507 return list;
32508
32509 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32510 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32511 OMP_CLAUSE_CHAIN (c) = list;
32512 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32513
32514 return c;
32515 }
32516
32517 /* OpenMP 3.1:
32518 final ( expression ) */
32519
32520 static tree
32521 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32522 {
32523 tree t, c;
32524
32525 matching_parens parens;
32526 if (!parens.require_open (parser))
32527 return list;
32528
32529 t = cp_parser_assignment_expression (parser);
32530
32531 if (t == error_mark_node
32532 || !parens.require_close (parser))
32533 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32534 /*or_comma=*/false,
32535 /*consume_paren=*/true);
32536
32537 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32538
32539 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32540 OMP_CLAUSE_FINAL_EXPR (c) = t;
32541 OMP_CLAUSE_CHAIN (c) = list;
32542
32543 return c;
32544 }
32545
32546 /* OpenMP 2.5:
32547 if ( expression )
32548
32549 OpenMP 4.5:
32550 if ( directive-name-modifier : expression )
32551
32552 directive-name-modifier:
32553 parallel | task | taskloop | target data | target | target update
32554 | target enter data | target exit data
32555
32556 OpenMP 5.0:
32557 directive-name-modifier:
32558 ... | simd | cancel */
32559
32560 static tree
32561 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32562 bool is_omp)
32563 {
32564 tree t, c;
32565 enum tree_code if_modifier = ERROR_MARK;
32566
32567 matching_parens parens;
32568 if (!parens.require_open (parser))
32569 return list;
32570
32571 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32572 {
32573 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32574 const char *p = IDENTIFIER_POINTER (id);
32575 int n = 2;
32576
32577 if (strcmp ("cancel", p) == 0)
32578 if_modifier = VOID_CST;
32579 else if (strcmp ("parallel", p) == 0)
32580 if_modifier = OMP_PARALLEL;
32581 else if (strcmp ("simd", p) == 0)
32582 if_modifier = OMP_SIMD;
32583 else if (strcmp ("task", p) == 0)
32584 if_modifier = OMP_TASK;
32585 else if (strcmp ("taskloop", p) == 0)
32586 if_modifier = OMP_TASKLOOP;
32587 else if (strcmp ("target", p) == 0)
32588 {
32589 if_modifier = OMP_TARGET;
32590 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32591 {
32592 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32593 p = IDENTIFIER_POINTER (id);
32594 if (strcmp ("data", p) == 0)
32595 if_modifier = OMP_TARGET_DATA;
32596 else if (strcmp ("update", p) == 0)
32597 if_modifier = OMP_TARGET_UPDATE;
32598 else if (strcmp ("enter", p) == 0)
32599 if_modifier = OMP_TARGET_ENTER_DATA;
32600 else if (strcmp ("exit", p) == 0)
32601 if_modifier = OMP_TARGET_EXIT_DATA;
32602 if (if_modifier != OMP_TARGET)
32603 n = 3;
32604 else
32605 {
32606 location_t loc
32607 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32608 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32609 "or %<exit%>");
32610 if_modifier = ERROR_MARK;
32611 }
32612 if (if_modifier == OMP_TARGET_ENTER_DATA
32613 || if_modifier == OMP_TARGET_EXIT_DATA)
32614 {
32615 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32616 {
32617 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32618 p = IDENTIFIER_POINTER (id);
32619 if (strcmp ("data", p) == 0)
32620 n = 4;
32621 }
32622 if (n != 4)
32623 {
32624 location_t loc
32625 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32626 error_at (loc, "expected %<data%>");
32627 if_modifier = ERROR_MARK;
32628 }
32629 }
32630 }
32631 }
32632 if (if_modifier != ERROR_MARK)
32633 {
32634 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32635 {
32636 while (n-- > 0)
32637 cp_lexer_consume_token (parser->lexer);
32638 }
32639 else
32640 {
32641 if (n > 2)
32642 {
32643 location_t loc
32644 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32645 error_at (loc, "expected %<:%>");
32646 }
32647 if_modifier = ERROR_MARK;
32648 }
32649 }
32650 }
32651
32652 t = cp_parser_assignment_expression (parser);
32653
32654 if (t == error_mark_node
32655 || !parens.require_close (parser))
32656 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32657 /*or_comma=*/false,
32658 /*consume_paren=*/true);
32659
32660 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32661 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32662 {
32663 if (if_modifier != ERROR_MARK
32664 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32665 {
32666 const char *p = NULL;
32667 switch (if_modifier)
32668 {
32669 case VOID_CST: p = "cancel"; break;
32670 case OMP_PARALLEL: p = "parallel"; break;
32671 case OMP_SIMD: p = "simd"; break;
32672 case OMP_TASK: p = "task"; break;
32673 case OMP_TASKLOOP: p = "taskloop"; break;
32674 case OMP_TARGET_DATA: p = "target data"; break;
32675 case OMP_TARGET: p = "target"; break;
32676 case OMP_TARGET_UPDATE: p = "target update"; break;
32677 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32678 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32679 default: gcc_unreachable ();
32680 }
32681 error_at (location, "too many %<if%> clauses with %qs modifier",
32682 p);
32683 return list;
32684 }
32685 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32686 {
32687 if (!is_omp)
32688 error_at (location, "too many %<if%> clauses");
32689 else
32690 error_at (location, "too many %<if%> clauses without modifier");
32691 return list;
32692 }
32693 else if (if_modifier == ERROR_MARK
32694 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32695 {
32696 error_at (location, "if any %<if%> clause has modifier, then all "
32697 "%<if%> clauses have to use modifier");
32698 return list;
32699 }
32700 }
32701
32702 c = build_omp_clause (location, OMP_CLAUSE_IF);
32703 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32704 OMP_CLAUSE_IF_EXPR (c) = t;
32705 OMP_CLAUSE_CHAIN (c) = list;
32706
32707 return c;
32708 }
32709
32710 /* OpenMP 3.1:
32711 mergeable */
32712
32713 static tree
32714 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32715 tree list, location_t location)
32716 {
32717 tree c;
32718
32719 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32720 location);
32721
32722 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32723 OMP_CLAUSE_CHAIN (c) = list;
32724 return c;
32725 }
32726
32727 /* OpenMP 2.5:
32728 nowait */
32729
32730 static tree
32731 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32732 tree list, location_t location)
32733 {
32734 tree c;
32735
32736 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32737
32738 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32739 OMP_CLAUSE_CHAIN (c) = list;
32740 return c;
32741 }
32742
32743 /* OpenMP 2.5:
32744 num_threads ( expression ) */
32745
32746 static tree
32747 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32748 location_t location)
32749 {
32750 tree t, c;
32751
32752 matching_parens parens;
32753 if (!parens.require_open (parser))
32754 return list;
32755
32756 t = cp_parser_assignment_expression (parser);
32757
32758 if (t == error_mark_node
32759 || !parens.require_close (parser))
32760 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32761 /*or_comma=*/false,
32762 /*consume_paren=*/true);
32763
32764 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32765 "num_threads", location);
32766
32767 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32768 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32769 OMP_CLAUSE_CHAIN (c) = list;
32770
32771 return c;
32772 }
32773
32774 /* OpenMP 4.5:
32775 num_tasks ( expression ) */
32776
32777 static tree
32778 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32779 location_t location)
32780 {
32781 tree t, c;
32782
32783 matching_parens parens;
32784 if (!parens.require_open (parser))
32785 return list;
32786
32787 t = cp_parser_assignment_expression (parser);
32788
32789 if (t == error_mark_node
32790 || !parens.require_close (parser))
32791 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32792 /*or_comma=*/false,
32793 /*consume_paren=*/true);
32794
32795 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32796 "num_tasks", location);
32797
32798 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32799 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32800 OMP_CLAUSE_CHAIN (c) = list;
32801
32802 return c;
32803 }
32804
32805 /* OpenMP 4.5:
32806 grainsize ( expression ) */
32807
32808 static tree
32809 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32810 location_t location)
32811 {
32812 tree t, c;
32813
32814 matching_parens parens;
32815 if (!parens.require_open (parser))
32816 return list;
32817
32818 t = cp_parser_assignment_expression (parser);
32819
32820 if (t == error_mark_node
32821 || !parens.require_close (parser))
32822 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32823 /*or_comma=*/false,
32824 /*consume_paren=*/true);
32825
32826 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32827 "grainsize", location);
32828
32829 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32830 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32831 OMP_CLAUSE_CHAIN (c) = list;
32832
32833 return c;
32834 }
32835
32836 /* OpenMP 4.5:
32837 priority ( expression ) */
32838
32839 static tree
32840 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32841 location_t location)
32842 {
32843 tree t, c;
32844
32845 matching_parens parens;
32846 if (!parens.require_open (parser))
32847 return list;
32848
32849 t = cp_parser_assignment_expression (parser);
32850
32851 if (t == error_mark_node
32852 || !parens.require_close (parser))
32853 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32854 /*or_comma=*/false,
32855 /*consume_paren=*/true);
32856
32857 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32858 "priority", location);
32859
32860 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32861 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32862 OMP_CLAUSE_CHAIN (c) = list;
32863
32864 return c;
32865 }
32866
32867 /* OpenMP 4.5:
32868 hint ( expression ) */
32869
32870 static tree
32871 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
32872 {
32873 tree t, c;
32874
32875 matching_parens parens;
32876 if (!parens.require_open (parser))
32877 return list;
32878
32879 t = cp_parser_assignment_expression (parser);
32880
32881 if (t == error_mark_node
32882 || !parens.require_close (parser))
32883 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32884 /*or_comma=*/false,
32885 /*consume_paren=*/true);
32886
32887 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32888
32889 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32890 OMP_CLAUSE_HINT_EXPR (c) = t;
32891 OMP_CLAUSE_CHAIN (c) = list;
32892
32893 return c;
32894 }
32895
32896 /* OpenMP 4.5:
32897 defaultmap ( tofrom : scalar )
32898
32899 OpenMP 5.0:
32900 defaultmap ( implicit-behavior [ : variable-category ] ) */
32901
32902 static tree
32903 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32904 location_t location)
32905 {
32906 tree c, id;
32907 const char *p;
32908 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
32909 enum omp_clause_defaultmap_kind category
32910 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
32911
32912 matching_parens parens;
32913 if (!parens.require_open (parser))
32914 return list;
32915
32916 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32917 p = "default";
32918 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32919 {
32920 invalid_behavior:
32921 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
32922 "%<tofrom%>, %<firstprivate%>, %<none%> "
32923 "or %<default%>");
32924 goto out_err;
32925 }
32926 else
32927 {
32928 id = cp_lexer_peek_token (parser->lexer)->u.value;
32929 p = IDENTIFIER_POINTER (id);
32930 }
32931
32932 switch (p[0])
32933 {
32934 case 'a':
32935 if (strcmp ("alloc", p) == 0)
32936 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
32937 else
32938 goto invalid_behavior;
32939 break;
32940
32941 case 'd':
32942 if (strcmp ("default", p) == 0)
32943 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
32944 else
32945 goto invalid_behavior;
32946 break;
32947
32948 case 'f':
32949 if (strcmp ("firstprivate", p) == 0)
32950 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
32951 else if (strcmp ("from", p) == 0)
32952 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
32953 else
32954 goto invalid_behavior;
32955 break;
32956
32957 case 'n':
32958 if (strcmp ("none", p) == 0)
32959 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
32960 else
32961 goto invalid_behavior;
32962 break;
32963
32964 case 't':
32965 if (strcmp ("tofrom", p) == 0)
32966 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
32967 else if (strcmp ("to", p) == 0)
32968 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
32969 else
32970 goto invalid_behavior;
32971 break;
32972
32973 default:
32974 goto invalid_behavior;
32975 }
32976 cp_lexer_consume_token (parser->lexer);
32977
32978 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32979 {
32980 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32981 goto out_err;
32982
32983 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32984 {
32985 invalid_category:
32986 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
32987 "%<pointer%>");
32988 goto out_err;
32989 }
32990 id = cp_lexer_peek_token (parser->lexer)->u.value;
32991 p = IDENTIFIER_POINTER (id);
32992
32993 switch (p[0])
32994 {
32995 case 'a':
32996 if (strcmp ("aggregate", p) == 0)
32997 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
32998 else
32999 goto invalid_category;
33000 break;
33001
33002 case 'p':
33003 if (strcmp ("pointer", p) == 0)
33004 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33005 else
33006 goto invalid_category;
33007 break;
33008
33009 case 's':
33010 if (strcmp ("scalar", p) == 0)
33011 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33012 else
33013 goto invalid_category;
33014 break;
33015
33016 default:
33017 goto invalid_category;
33018 }
33019
33020 cp_lexer_consume_token (parser->lexer);
33021 }
33022 if (!parens.require_close (parser))
33023 goto out_err;
33024
33025 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33026 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33027 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33028 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33029 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33030 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33031 {
33032 enum omp_clause_defaultmap_kind cat = category;
33033 location_t loc = OMP_CLAUSE_LOCATION (c);
33034 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33035 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33036 p = NULL;
33037 switch (cat)
33038 {
33039 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33040 p = NULL;
33041 break;
33042 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33043 p = "aggregate";
33044 break;
33045 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33046 p = "pointer";
33047 break;
33048 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33049 p = "scalar";
33050 break;
33051 default:
33052 gcc_unreachable ();
33053 }
33054 if (p)
33055 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33056 p);
33057 else
33058 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33059 "category");
33060 break;
33061 }
33062
33063 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33064 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33065 OMP_CLAUSE_CHAIN (c) = list;
33066 return c;
33067
33068 out_err:
33069 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33070 /*or_comma=*/false,
33071 /*consume_paren=*/true);
33072 return list;
33073 }
33074
33075 /* OpenMP 2.5:
33076 ordered
33077
33078 OpenMP 4.5:
33079 ordered ( constant-expression ) */
33080
33081 static tree
33082 cp_parser_omp_clause_ordered (cp_parser *parser,
33083 tree list, location_t location)
33084 {
33085 tree c, num = NULL_TREE;
33086 HOST_WIDE_INT n;
33087
33088 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33089 "ordered", location);
33090
33091 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33092 {
33093 matching_parens parens;
33094 parens.consume_open (parser);
33095
33096 num = cp_parser_constant_expression (parser);
33097
33098 if (!parens.require_close (parser))
33099 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33100 /*or_comma=*/false,
33101 /*consume_paren=*/true);
33102
33103 if (num == error_mark_node)
33104 return list;
33105 num = fold_non_dependent_expr (num);
33106 if (!tree_fits_shwi_p (num)
33107 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33108 || (n = tree_to_shwi (num)) <= 0
33109 || (int) n != n)
33110 {
33111 error_at (location,
33112 "ordered argument needs positive constant integer "
33113 "expression");
33114 return list;
33115 }
33116 }
33117
33118 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33119 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33120 OMP_CLAUSE_CHAIN (c) = list;
33121 return c;
33122 }
33123
33124 /* OpenMP 2.5:
33125 reduction ( reduction-operator : variable-list )
33126
33127 reduction-operator:
33128 One of: + * - & ^ | && ||
33129
33130 OpenMP 3.1:
33131
33132 reduction-operator:
33133 One of: + * - & ^ | && || min max
33134
33135 OpenMP 4.0:
33136
33137 reduction-operator:
33138 One of: + * - & ^ | && ||
33139 id-expression
33140
33141 OpenMP 5.0:
33142 reduction ( reduction-modifier, reduction-operator : variable-list )
33143 in_reduction ( reduction-operator : variable-list )
33144 task_reduction ( reduction-operator : variable-list ) */
33145
33146 static tree
33147 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33148 bool is_omp, tree list)
33149 {
33150 enum tree_code code = ERROR_MARK;
33151 tree nlist, c, id = NULL_TREE;
33152 bool task = false;
33153 bool inscan = false;
33154
33155 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33156 return list;
33157
33158 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33159 {
33160 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33161 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33162 {
33163 cp_lexer_consume_token (parser->lexer);
33164 cp_lexer_consume_token (parser->lexer);
33165 }
33166 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33167 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33168 {
33169 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33170 const char *p = IDENTIFIER_POINTER (id);
33171 if (strcmp (p, "task") == 0)
33172 task = true;
33173 else if (strcmp (p, "inscan") == 0)
33174 {
33175 inscan = true;
33176 sorry ("%<inscan%> modifier on %<reduction%> clause "
33177 "not supported yet");
33178 }
33179 if (task || inscan)
33180 {
33181 cp_lexer_consume_token (parser->lexer);
33182 cp_lexer_consume_token (parser->lexer);
33183 }
33184 }
33185 }
33186
33187 switch (cp_lexer_peek_token (parser->lexer)->type)
33188 {
33189 case CPP_PLUS: code = PLUS_EXPR; break;
33190 case CPP_MULT: code = MULT_EXPR; break;
33191 case CPP_MINUS: code = MINUS_EXPR; break;
33192 case CPP_AND: code = BIT_AND_EXPR; break;
33193 case CPP_XOR: code = BIT_XOR_EXPR; break;
33194 case CPP_OR: code = BIT_IOR_EXPR; break;
33195 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33196 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33197 default: break;
33198 }
33199
33200 if (code != ERROR_MARK)
33201 cp_lexer_consume_token (parser->lexer);
33202 else
33203 {
33204 bool saved_colon_corrects_to_scope_p;
33205 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33206 parser->colon_corrects_to_scope_p = false;
33207 id = cp_parser_id_expression (parser, /*template_p=*/false,
33208 /*check_dependency_p=*/true,
33209 /*template_p=*/NULL,
33210 /*declarator_p=*/false,
33211 /*optional_p=*/false);
33212 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33213 if (identifier_p (id))
33214 {
33215 const char *p = IDENTIFIER_POINTER (id);
33216
33217 if (strcmp (p, "min") == 0)
33218 code = MIN_EXPR;
33219 else if (strcmp (p, "max") == 0)
33220 code = MAX_EXPR;
33221 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33222 code = PLUS_EXPR;
33223 else if (id == ovl_op_identifier (false, MULT_EXPR))
33224 code = MULT_EXPR;
33225 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33226 code = MINUS_EXPR;
33227 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33228 code = BIT_AND_EXPR;
33229 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33230 code = BIT_IOR_EXPR;
33231 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33232 code = BIT_XOR_EXPR;
33233 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33234 code = TRUTH_ANDIF_EXPR;
33235 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33236 code = TRUTH_ORIF_EXPR;
33237 id = omp_reduction_id (code, id, NULL_TREE);
33238 tree scope = parser->scope;
33239 if (scope)
33240 id = build_qualified_name (NULL_TREE, scope, id, false);
33241 parser->scope = NULL_TREE;
33242 parser->qualifying_scope = NULL_TREE;
33243 parser->object_scope = NULL_TREE;
33244 }
33245 else
33246 {
33247 error ("invalid reduction-identifier");
33248 resync_fail:
33249 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33250 /*or_comma=*/false,
33251 /*consume_paren=*/true);
33252 return list;
33253 }
33254 }
33255
33256 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33257 goto resync_fail;
33258
33259 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33260 NULL);
33261 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33262 {
33263 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33264 if (task)
33265 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33266 else if (inscan)
33267 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33268 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33269 }
33270
33271 return nlist;
33272 }
33273
33274 /* OpenMP 2.5:
33275 schedule ( schedule-kind )
33276 schedule ( schedule-kind , expression )
33277
33278 schedule-kind:
33279 static | dynamic | guided | runtime | auto
33280
33281 OpenMP 4.5:
33282 schedule ( schedule-modifier : schedule-kind )
33283 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33284
33285 schedule-modifier:
33286 simd
33287 monotonic
33288 nonmonotonic */
33289
33290 static tree
33291 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33292 {
33293 tree c, t;
33294 int modifiers = 0, nmodifiers = 0;
33295
33296 matching_parens parens;
33297 if (!parens.require_open (parser))
33298 return list;
33299
33300 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33301
33302 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33303 {
33304 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33305 const char *p = IDENTIFIER_POINTER (id);
33306 if (strcmp ("simd", p) == 0)
33307 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33308 else if (strcmp ("monotonic", p) == 0)
33309 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33310 else if (strcmp ("nonmonotonic", p) == 0)
33311 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33312 else
33313 break;
33314 cp_lexer_consume_token (parser->lexer);
33315 if (nmodifiers++ == 0
33316 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33317 cp_lexer_consume_token (parser->lexer);
33318 else
33319 {
33320 cp_parser_require (parser, CPP_COLON, RT_COLON);
33321 break;
33322 }
33323 }
33324
33325 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33326 {
33327 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33328 const char *p = IDENTIFIER_POINTER (id);
33329
33330 switch (p[0])
33331 {
33332 case 'd':
33333 if (strcmp ("dynamic", p) != 0)
33334 goto invalid_kind;
33335 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33336 break;
33337
33338 case 'g':
33339 if (strcmp ("guided", p) != 0)
33340 goto invalid_kind;
33341 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33342 break;
33343
33344 case 'r':
33345 if (strcmp ("runtime", p) != 0)
33346 goto invalid_kind;
33347 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33348 break;
33349
33350 default:
33351 goto invalid_kind;
33352 }
33353 }
33354 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33355 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33356 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33357 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33358 else
33359 goto invalid_kind;
33360 cp_lexer_consume_token (parser->lexer);
33361
33362 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33363 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33364 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33365 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33366 {
33367 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33368 "specified");
33369 modifiers = 0;
33370 }
33371
33372 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33373 {
33374 cp_token *token;
33375 cp_lexer_consume_token (parser->lexer);
33376
33377 token = cp_lexer_peek_token (parser->lexer);
33378 t = cp_parser_assignment_expression (parser);
33379
33380 if (t == error_mark_node)
33381 goto resync_fail;
33382 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33383 error_at (token->location, "schedule %<runtime%> does not take "
33384 "a %<chunk_size%> parameter");
33385 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33386 error_at (token->location, "schedule %<auto%> does not take "
33387 "a %<chunk_size%> parameter");
33388 else
33389 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33390
33391 if (!parens.require_close (parser))
33392 goto resync_fail;
33393 }
33394 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33395 goto resync_fail;
33396
33397 OMP_CLAUSE_SCHEDULE_KIND (c)
33398 = (enum omp_clause_schedule_kind)
33399 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33400
33401 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33402 OMP_CLAUSE_CHAIN (c) = list;
33403 return c;
33404
33405 invalid_kind:
33406 cp_parser_error (parser, "invalid schedule kind");
33407 resync_fail:
33408 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33409 /*or_comma=*/false,
33410 /*consume_paren=*/true);
33411 return list;
33412 }
33413
33414 /* OpenMP 3.0:
33415 untied */
33416
33417 static tree
33418 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33419 tree list, location_t location)
33420 {
33421 tree c;
33422
33423 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33424
33425 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33426 OMP_CLAUSE_CHAIN (c) = list;
33427 return c;
33428 }
33429
33430 /* OpenMP 4.0:
33431 inbranch
33432 notinbranch */
33433
33434 static tree
33435 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33436 tree list, location_t location)
33437 {
33438 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33439 tree c = build_omp_clause (location, code);
33440 OMP_CLAUSE_CHAIN (c) = list;
33441 return c;
33442 }
33443
33444 /* OpenMP 4.0:
33445 parallel
33446 for
33447 sections
33448 taskgroup */
33449
33450 static tree
33451 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33452 enum omp_clause_code code,
33453 tree list, location_t location)
33454 {
33455 tree c = build_omp_clause (location, code);
33456 OMP_CLAUSE_CHAIN (c) = list;
33457 return c;
33458 }
33459
33460 /* OpenMP 4.5:
33461 nogroup */
33462
33463 static tree
33464 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33465 tree list, location_t location)
33466 {
33467 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33468 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33469 OMP_CLAUSE_CHAIN (c) = list;
33470 return c;
33471 }
33472
33473 /* OpenMP 4.5:
33474 simd
33475 threads */
33476
33477 static tree
33478 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33479 enum omp_clause_code code,
33480 tree list, location_t location)
33481 {
33482 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33483 tree c = build_omp_clause (location, code);
33484 OMP_CLAUSE_CHAIN (c) = list;
33485 return c;
33486 }
33487
33488 /* OpenMP 4.0:
33489 num_teams ( expression ) */
33490
33491 static tree
33492 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33493 location_t location)
33494 {
33495 tree t, c;
33496
33497 matching_parens parens;
33498 if (!parens.require_open (parser))
33499 return list;
33500
33501 t = cp_parser_assignment_expression (parser);
33502
33503 if (t == error_mark_node
33504 || !parens.require_close (parser))
33505 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33506 /*or_comma=*/false,
33507 /*consume_paren=*/true);
33508
33509 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33510 "num_teams", location);
33511
33512 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33513 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33514 OMP_CLAUSE_CHAIN (c) = list;
33515
33516 return c;
33517 }
33518
33519 /* OpenMP 4.0:
33520 thread_limit ( expression ) */
33521
33522 static tree
33523 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33524 location_t location)
33525 {
33526 tree t, c;
33527
33528 matching_parens parens;
33529 if (!parens.require_open (parser))
33530 return list;
33531
33532 t = cp_parser_assignment_expression (parser);
33533
33534 if (t == error_mark_node
33535 || !parens.require_close (parser))
33536 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33537 /*or_comma=*/false,
33538 /*consume_paren=*/true);
33539
33540 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33541 "thread_limit", location);
33542
33543 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33544 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33545 OMP_CLAUSE_CHAIN (c) = list;
33546
33547 return c;
33548 }
33549
33550 /* OpenMP 4.0:
33551 aligned ( variable-list )
33552 aligned ( variable-list : constant-expression ) */
33553
33554 static tree
33555 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33556 {
33557 tree nlist, c, alignment = NULL_TREE;
33558 bool colon;
33559
33560 matching_parens parens;
33561 if (!parens.require_open (parser))
33562 return list;
33563
33564 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33565 &colon);
33566
33567 if (colon)
33568 {
33569 alignment = cp_parser_constant_expression (parser);
33570
33571 if (!parens.require_close (parser))
33572 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33573 /*or_comma=*/false,
33574 /*consume_paren=*/true);
33575
33576 if (alignment == error_mark_node)
33577 alignment = NULL_TREE;
33578 }
33579
33580 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33581 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33582
33583 return nlist;
33584 }
33585
33586 /* OpenMP 2.5:
33587 lastprivate ( variable-list )
33588
33589 OpenMP 5.0:
33590 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33591
33592 static tree
33593 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
33594 {
33595 bool conditional = false;
33596
33597 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33598 return list;
33599
33600 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33601 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
33602 {
33603 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33604 const char *p = IDENTIFIER_POINTER (id);
33605
33606 if (strcmp ("conditional", p) == 0)
33607 {
33608 conditional = true;
33609 cp_lexer_consume_token (parser->lexer);
33610 cp_lexer_consume_token (parser->lexer);
33611 }
33612 }
33613
33614 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
33615 list, NULL);
33616
33617 if (conditional)
33618 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33619 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
33620 return nlist;
33621 }
33622
33623 /* OpenMP 4.0:
33624 linear ( variable-list )
33625 linear ( variable-list : expression )
33626
33627 OpenMP 4.5:
33628 linear ( modifier ( variable-list ) )
33629 linear ( modifier ( variable-list ) : expression ) */
33630
33631 static tree
33632 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33633 bool declare_simd)
33634 {
33635 tree nlist, c, step = integer_one_node;
33636 bool colon;
33637 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33638
33639 matching_parens parens;
33640 if (!parens.require_open (parser))
33641 return list;
33642
33643 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33644 {
33645 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33646 const char *p = IDENTIFIER_POINTER (id);
33647
33648 if (strcmp ("ref", p) == 0)
33649 kind = OMP_CLAUSE_LINEAR_REF;
33650 else if (strcmp ("val", p) == 0)
33651 kind = OMP_CLAUSE_LINEAR_VAL;
33652 else if (strcmp ("uval", p) == 0)
33653 kind = OMP_CLAUSE_LINEAR_UVAL;
33654 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33655 cp_lexer_consume_token (parser->lexer);
33656 else
33657 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33658 }
33659
33660 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33661 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33662 &colon);
33663 else
33664 {
33665 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33666 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33667 if (colon)
33668 cp_parser_require (parser, CPP_COLON, RT_COLON);
33669 else if (!parens.require_close (parser))
33670 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33671 /*or_comma=*/false,
33672 /*consume_paren=*/true);
33673 }
33674
33675 if (colon)
33676 {
33677 step = NULL_TREE;
33678 if (declare_simd
33679 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33680 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33681 {
33682 cp_token *token = cp_lexer_peek_token (parser->lexer);
33683 cp_parser_parse_tentatively (parser);
33684 step = cp_parser_id_expression (parser, /*template_p=*/false,
33685 /*check_dependency_p=*/true,
33686 /*template_p=*/NULL,
33687 /*declarator_p=*/false,
33688 /*optional_p=*/false);
33689 if (step != error_mark_node)
33690 step = cp_parser_lookup_name_simple (parser, step, token->location);
33691 if (step == error_mark_node)
33692 {
33693 step = NULL_TREE;
33694 cp_parser_abort_tentative_parse (parser);
33695 }
33696 else if (!cp_parser_parse_definitely (parser))
33697 step = NULL_TREE;
33698 }
33699 if (!step)
33700 step = cp_parser_assignment_expression (parser);
33701
33702 if (!parens.require_close (parser))
33703 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33704 /*or_comma=*/false,
33705 /*consume_paren=*/true);
33706
33707 if (step == error_mark_node)
33708 return list;
33709 }
33710
33711 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33712 {
33713 OMP_CLAUSE_LINEAR_STEP (c) = step;
33714 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33715 }
33716
33717 return nlist;
33718 }
33719
33720 /* OpenMP 4.0:
33721 safelen ( constant-expression ) */
33722
33723 static tree
33724 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33725 location_t location)
33726 {
33727 tree t, c;
33728
33729 matching_parens parens;
33730 if (!parens.require_open (parser))
33731 return list;
33732
33733 t = cp_parser_constant_expression (parser);
33734
33735 if (t == error_mark_node
33736 || !parens.require_close (parser))
33737 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33738 /*or_comma=*/false,
33739 /*consume_paren=*/true);
33740
33741 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33742
33743 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33744 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33745 OMP_CLAUSE_CHAIN (c) = list;
33746
33747 return c;
33748 }
33749
33750 /* OpenMP 4.0:
33751 simdlen ( constant-expression ) */
33752
33753 static tree
33754 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33755 location_t location)
33756 {
33757 tree t, c;
33758
33759 matching_parens parens;
33760 if (!parens.require_open (parser))
33761 return list;
33762
33763 t = cp_parser_constant_expression (parser);
33764
33765 if (t == error_mark_node
33766 || !parens.require_close (parser))
33767 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33768 /*or_comma=*/false,
33769 /*consume_paren=*/true);
33770
33771 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33772
33773 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33774 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33775 OMP_CLAUSE_CHAIN (c) = list;
33776
33777 return c;
33778 }
33779
33780 /* OpenMP 4.5:
33781 vec:
33782 identifier [+/- integer]
33783 vec , identifier [+/- integer]
33784 */
33785
33786 static tree
33787 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33788 tree list)
33789 {
33790 tree vec = NULL;
33791
33792 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33793 {
33794 cp_parser_error (parser, "expected identifier");
33795 return list;
33796 }
33797
33798 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33799 {
33800 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33801 tree t, identifier = cp_parser_identifier (parser);
33802 tree addend = NULL;
33803
33804 if (identifier == error_mark_node)
33805 t = error_mark_node;
33806 else
33807 {
33808 t = cp_parser_lookup_name_simple
33809 (parser, identifier,
33810 cp_lexer_peek_token (parser->lexer)->location);
33811 if (t == error_mark_node)
33812 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33813 id_loc);
33814 }
33815
33816 bool neg = false;
33817 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33818 neg = true;
33819 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33820 {
33821 addend = integer_zero_node;
33822 goto add_to_vector;
33823 }
33824 cp_lexer_consume_token (parser->lexer);
33825
33826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33827 {
33828 cp_parser_error (parser, "expected integer");
33829 return list;
33830 }
33831
33832 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33833 if (TREE_CODE (addend) != INTEGER_CST)
33834 {
33835 cp_parser_error (parser, "expected integer");
33836 return list;
33837 }
33838 cp_lexer_consume_token (parser->lexer);
33839
33840 add_to_vector:
33841 if (t != error_mark_node)
33842 {
33843 vec = tree_cons (addend, t, vec);
33844 if (neg)
33845 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33846 }
33847
33848 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33849 break;
33850
33851 cp_lexer_consume_token (parser->lexer);
33852 }
33853
33854 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33855 {
33856 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33857 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33858 OMP_CLAUSE_DECL (u) = nreverse (vec);
33859 OMP_CLAUSE_CHAIN (u) = list;
33860 return u;
33861 }
33862 return list;
33863 }
33864
33865 /* OpenMP 5.0:
33866 iterators ( iterators-definition )
33867
33868 iterators-definition:
33869 iterator-specifier
33870 iterator-specifier , iterators-definition
33871
33872 iterator-specifier:
33873 identifier = range-specification
33874 iterator-type identifier = range-specification
33875
33876 range-specification:
33877 begin : end
33878 begin : end : step */
33879
33880 static tree
33881 cp_parser_omp_iterators (cp_parser *parser)
33882 {
33883 tree ret = NULL_TREE, *last = &ret;
33884 cp_lexer_consume_token (parser->lexer);
33885
33886 matching_parens parens;
33887 if (!parens.require_open (parser))
33888 return error_mark_node;
33889
33890 bool saved_colon_corrects_to_scope_p
33891 = parser->colon_corrects_to_scope_p;
33892 bool saved_colon_doesnt_start_class_def_p
33893 = parser->colon_doesnt_start_class_def_p;
33894
33895 do
33896 {
33897 tree iter_type;
33898 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33899 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
33900 iter_type = integer_type_node;
33901 else
33902 {
33903 const char *saved_message
33904 = parser->type_definition_forbidden_message;
33905 parser->type_definition_forbidden_message
33906 = G_("types may not be defined in iterator type");
33907
33908 iter_type = cp_parser_type_id (parser);
33909
33910 parser->type_definition_forbidden_message = saved_message;
33911 }
33912
33913 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33915 {
33916 cp_parser_error (parser, "expected identifier");
33917 break;
33918 }
33919
33920 tree id = cp_parser_identifier (parser);
33921 if (id == error_mark_node)
33922 break;
33923
33924 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33925 break;
33926
33927 parser->colon_corrects_to_scope_p = false;
33928 parser->colon_doesnt_start_class_def_p = true;
33929 tree begin = cp_parser_assignment_expression (parser);
33930
33931 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33932 break;
33933
33934 tree end = cp_parser_assignment_expression (parser);
33935
33936 tree step = integer_one_node;
33937 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
33938 {
33939 cp_lexer_consume_token (parser->lexer);
33940 step = cp_parser_assignment_expression (parser);
33941 }
33942
33943 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
33944 DECL_ARTIFICIAL (iter_var) = 1;
33945 DECL_CONTEXT (iter_var) = current_function_decl;
33946 pushdecl (iter_var);
33947
33948 *last = make_tree_vec (6);
33949 TREE_VEC_ELT (*last, 0) = iter_var;
33950 TREE_VEC_ELT (*last, 1) = begin;
33951 TREE_VEC_ELT (*last, 2) = end;
33952 TREE_VEC_ELT (*last, 3) = step;
33953 last = &TREE_CHAIN (*last);
33954
33955 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33956 {
33957 cp_lexer_consume_token (parser->lexer);
33958 continue;
33959 }
33960 break;
33961 }
33962 while (1);
33963
33964 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33965 parser->colon_doesnt_start_class_def_p
33966 = saved_colon_doesnt_start_class_def_p;
33967
33968 if (!parens.require_close (parser))
33969 cp_parser_skip_to_closing_parenthesis (parser,
33970 /*recovering=*/true,
33971 /*or_comma=*/false,
33972 /*consume_paren=*/true);
33973
33974 return ret ? ret : error_mark_node;
33975 }
33976
33977 /* OpenMP 4.0:
33978 depend ( depend-kind : variable-list )
33979
33980 depend-kind:
33981 in | out | inout
33982
33983 OpenMP 4.5:
33984 depend ( source )
33985
33986 depend ( sink : vec )
33987
33988 OpenMP 5.0:
33989 depend ( depend-modifier , depend-kind: variable-list )
33990
33991 depend-kind:
33992 in | out | inout | mutexinoutset | depobj
33993
33994 depend-modifier:
33995 iterator ( iterators-definition ) */
33996
33997 static tree
33998 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33999 {
34000 tree nlist, c, iterators = NULL_TREE;
34001 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34002
34003 matching_parens parens;
34004 if (!parens.require_open (parser))
34005 return list;
34006
34007 do
34008 {
34009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34010 goto invalid_kind;
34011
34012 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34013 const char *p = IDENTIFIER_POINTER (id);
34014
34015 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34016 {
34017 begin_scope (sk_omp, NULL);
34018 iterators = cp_parser_omp_iterators (parser);
34019 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34020 continue;
34021 }
34022 if (strcmp ("in", p) == 0)
34023 kind = OMP_CLAUSE_DEPEND_IN;
34024 else if (strcmp ("inout", p) == 0)
34025 kind = OMP_CLAUSE_DEPEND_INOUT;
34026 else if (strcmp ("mutexinoutset", p) == 0)
34027 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34028 else if (strcmp ("out", p) == 0)
34029 kind = OMP_CLAUSE_DEPEND_OUT;
34030 else if (strcmp ("depobj", p) == 0)
34031 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34032 else if (strcmp ("sink", p) == 0)
34033 kind = OMP_CLAUSE_DEPEND_SINK;
34034 else if (strcmp ("source", p) == 0)
34035 kind = OMP_CLAUSE_DEPEND_SOURCE;
34036 else
34037 goto invalid_kind;
34038 break;
34039 }
34040 while (1);
34041
34042 cp_lexer_consume_token (parser->lexer);
34043
34044 if (iterators
34045 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34046 {
34047 poplevel (0, 1, 0);
34048 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34049 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34050 iterators = NULL_TREE;
34051 }
34052
34053 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34054 {
34055 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34056 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34057 OMP_CLAUSE_DECL (c) = NULL_TREE;
34058 OMP_CLAUSE_CHAIN (c) = list;
34059 if (!parens.require_close (parser))
34060 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34061 /*or_comma=*/false,
34062 /*consume_paren=*/true);
34063 return c;
34064 }
34065
34066 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34067 goto resync_fail;
34068
34069 if (kind == OMP_CLAUSE_DEPEND_SINK)
34070 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34071 else
34072 {
34073 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34074 list, NULL);
34075
34076 if (iterators)
34077 {
34078 tree block = poplevel (1, 1, 0);
34079 if (iterators == error_mark_node)
34080 iterators = NULL_TREE;
34081 else
34082 TREE_VEC_ELT (iterators, 5) = block;
34083 }
34084
34085 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34086 {
34087 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34088 if (iterators)
34089 OMP_CLAUSE_DECL (c)
34090 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34091 }
34092 }
34093 return nlist;
34094
34095 invalid_kind:
34096 cp_parser_error (parser, "invalid depend kind");
34097 resync_fail:
34098 if (iterators)
34099 poplevel (0, 1, 0);
34100 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34101 /*or_comma=*/false,
34102 /*consume_paren=*/true);
34103 return list;
34104 }
34105
34106 /* OpenMP 4.0:
34107 map ( map-kind : variable-list )
34108 map ( variable-list )
34109
34110 map-kind:
34111 alloc | to | from | tofrom
34112
34113 OpenMP 4.5:
34114 map-kind:
34115 alloc | to | from | tofrom | release | delete
34116
34117 map ( always [,] map-kind: variable-list ) */
34118
34119 static tree
34120 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34121 {
34122 tree nlist, c;
34123 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34124 bool always = false;
34125
34126 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34127 return list;
34128
34129 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34130 {
34131 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34132 const char *p = IDENTIFIER_POINTER (id);
34133
34134 if (strcmp ("always", p) == 0)
34135 {
34136 int nth = 2;
34137 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34138 nth++;
34139 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34140 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34141 == RID_DELETE))
34142 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34143 == CPP_COLON))
34144 {
34145 always = true;
34146 cp_lexer_consume_token (parser->lexer);
34147 if (nth == 3)
34148 cp_lexer_consume_token (parser->lexer);
34149 }
34150 }
34151 }
34152
34153 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34154 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34155 {
34156 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34157 const char *p = IDENTIFIER_POINTER (id);
34158
34159 if (strcmp ("alloc", p) == 0)
34160 kind = GOMP_MAP_ALLOC;
34161 else if (strcmp ("to", p) == 0)
34162 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34163 else if (strcmp ("from", p) == 0)
34164 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34165 else if (strcmp ("tofrom", p) == 0)
34166 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34167 else if (strcmp ("release", p) == 0)
34168 kind = GOMP_MAP_RELEASE;
34169 else
34170 {
34171 cp_parser_error (parser, "invalid map kind");
34172 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34173 /*or_comma=*/false,
34174 /*consume_paren=*/true);
34175 return list;
34176 }
34177 cp_lexer_consume_token (parser->lexer);
34178 cp_lexer_consume_token (parser->lexer);
34179 }
34180 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34181 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34182 {
34183 kind = GOMP_MAP_DELETE;
34184 cp_lexer_consume_token (parser->lexer);
34185 cp_lexer_consume_token (parser->lexer);
34186 }
34187
34188 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34189 NULL);
34190
34191 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34192 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34193
34194 return nlist;
34195 }
34196
34197 /* OpenMP 4.0:
34198 device ( expression ) */
34199
34200 static tree
34201 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34202 location_t location)
34203 {
34204 tree t, c;
34205
34206 matching_parens parens;
34207 if (!parens.require_open (parser))
34208 return list;
34209
34210 t = cp_parser_assignment_expression (parser);
34211
34212 if (t == error_mark_node
34213 || !parens.require_close (parser))
34214 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34215 /*or_comma=*/false,
34216 /*consume_paren=*/true);
34217
34218 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34219 "device", location);
34220
34221 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34222 OMP_CLAUSE_DEVICE_ID (c) = t;
34223 OMP_CLAUSE_CHAIN (c) = list;
34224
34225 return c;
34226 }
34227
34228 /* OpenMP 4.0:
34229 dist_schedule ( static )
34230 dist_schedule ( static , expression ) */
34231
34232 static tree
34233 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34234 location_t location)
34235 {
34236 tree c, t;
34237
34238 matching_parens parens;
34239 if (!parens.require_open (parser))
34240 return list;
34241
34242 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34243
34244 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34245 goto invalid_kind;
34246 cp_lexer_consume_token (parser->lexer);
34247
34248 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34249 {
34250 cp_lexer_consume_token (parser->lexer);
34251
34252 t = cp_parser_assignment_expression (parser);
34253
34254 if (t == error_mark_node)
34255 goto resync_fail;
34256 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34257
34258 if (!parens.require_close (parser))
34259 goto resync_fail;
34260 }
34261 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34262 goto resync_fail;
34263
34264 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34265 location);
34266 OMP_CLAUSE_CHAIN (c) = list;
34267 return c;
34268
34269 invalid_kind:
34270 cp_parser_error (parser, "invalid dist_schedule kind");
34271 resync_fail:
34272 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34273 /*or_comma=*/false,
34274 /*consume_paren=*/true);
34275 return list;
34276 }
34277
34278 /* OpenMP 4.0:
34279 proc_bind ( proc-bind-kind )
34280
34281 proc-bind-kind:
34282 master | close | spread */
34283
34284 static tree
34285 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34286 location_t location)
34287 {
34288 tree c;
34289 enum omp_clause_proc_bind_kind kind;
34290
34291 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34292 return list;
34293
34294 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34295 {
34296 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34297 const char *p = IDENTIFIER_POINTER (id);
34298
34299 if (strcmp ("master", p) == 0)
34300 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34301 else if (strcmp ("close", p) == 0)
34302 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34303 else if (strcmp ("spread", p) == 0)
34304 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34305 else
34306 goto invalid_kind;
34307 }
34308 else
34309 goto invalid_kind;
34310
34311 cp_lexer_consume_token (parser->lexer);
34312 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34313 goto resync_fail;
34314
34315 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34316 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34317 location);
34318 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34319 OMP_CLAUSE_CHAIN (c) = list;
34320 return c;
34321
34322 invalid_kind:
34323 cp_parser_error (parser, "invalid depend kind");
34324 resync_fail:
34325 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34326 /*or_comma=*/false,
34327 /*consume_paren=*/true);
34328 return list;
34329 }
34330
34331 /* OpenACC:
34332 async [( int-expr )] */
34333
34334 static tree
34335 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34336 {
34337 tree c, t;
34338 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34339
34340 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34341
34342 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34343 {
34344 matching_parens parens;
34345 parens.consume_open (parser);
34346
34347 t = cp_parser_expression (parser);
34348 if (t == error_mark_node
34349 || !parens.require_close (parser))
34350 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34351 /*or_comma=*/false,
34352 /*consume_paren=*/true);
34353 }
34354
34355 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34356
34357 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34358 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34359 OMP_CLAUSE_CHAIN (c) = list;
34360 list = c;
34361
34362 return list;
34363 }
34364
34365 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34366 is a bitmask in MASK. Return the list of clauses found. */
34367
34368 static tree
34369 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34370 const char *where, cp_token *pragma_tok,
34371 bool finish_p = true)
34372 {
34373 tree clauses = NULL;
34374 bool first = true;
34375
34376 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34377 {
34378 location_t here;
34379 pragma_omp_clause c_kind;
34380 omp_clause_code code;
34381 const char *c_name;
34382 tree prev = clauses;
34383
34384 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34385 cp_lexer_consume_token (parser->lexer);
34386
34387 here = cp_lexer_peek_token (parser->lexer)->location;
34388 c_kind = cp_parser_omp_clause_name (parser);
34389
34390 switch (c_kind)
34391 {
34392 case PRAGMA_OACC_CLAUSE_ASYNC:
34393 clauses = cp_parser_oacc_clause_async (parser, clauses);
34394 c_name = "async";
34395 break;
34396 case PRAGMA_OACC_CLAUSE_AUTO:
34397 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
34398 clauses, here);
34399 c_name = "auto";
34400 break;
34401 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34402 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34403 c_name = "collapse";
34404 break;
34405 case PRAGMA_OACC_CLAUSE_COPY:
34406 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34407 c_name = "copy";
34408 break;
34409 case PRAGMA_OACC_CLAUSE_COPYIN:
34410 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34411 c_name = "copyin";
34412 break;
34413 case PRAGMA_OACC_CLAUSE_COPYOUT:
34414 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34415 c_name = "copyout";
34416 break;
34417 case PRAGMA_OACC_CLAUSE_CREATE:
34418 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34419 c_name = "create";
34420 break;
34421 case PRAGMA_OACC_CLAUSE_DELETE:
34422 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34423 c_name = "delete";
34424 break;
34425 case PRAGMA_OMP_CLAUSE_DEFAULT:
34426 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34427 c_name = "default";
34428 break;
34429 case PRAGMA_OACC_CLAUSE_DEVICE:
34430 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34431 c_name = "device";
34432 break;
34433 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34434 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34435 c_name = "deviceptr";
34436 break;
34437 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34438 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34439 c_name = "device_resident";
34440 break;
34441 case PRAGMA_OACC_CLAUSE_FINALIZE:
34442 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
34443 clauses, here);
34444 c_name = "finalize";
34445 break;
34446 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34447 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34448 clauses);
34449 c_name = "firstprivate";
34450 break;
34451 case PRAGMA_OACC_CLAUSE_GANG:
34452 c_name = "gang";
34453 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
34454 c_name, clauses);
34455 break;
34456 case PRAGMA_OACC_CLAUSE_HOST:
34457 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34458 c_name = "host";
34459 break;
34460 case PRAGMA_OACC_CLAUSE_IF:
34461 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34462 c_name = "if";
34463 break;
34464 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34465 clauses = cp_parser_oacc_simple_clause (parser,
34466 OMP_CLAUSE_IF_PRESENT,
34467 clauses, here);
34468 c_name = "if_present";
34469 break;
34470 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34471 clauses = cp_parser_oacc_simple_clause (parser,
34472 OMP_CLAUSE_INDEPENDENT,
34473 clauses, here);
34474 c_name = "independent";
34475 break;
34476 case PRAGMA_OACC_CLAUSE_LINK:
34477 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34478 c_name = "link";
34479 break;
34480 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34481 code = OMP_CLAUSE_NUM_GANGS;
34482 c_name = "num_gangs";
34483 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34484 clauses);
34485 break;
34486 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34487 c_name = "num_workers";
34488 code = OMP_CLAUSE_NUM_WORKERS;
34489 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34490 clauses);
34491 break;
34492 case PRAGMA_OACC_CLAUSE_PRESENT:
34493 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34494 c_name = "present";
34495 break;
34496 case PRAGMA_OACC_CLAUSE_PRIVATE:
34497 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34498 clauses);
34499 c_name = "private";
34500 break;
34501 case PRAGMA_OACC_CLAUSE_REDUCTION:
34502 clauses
34503 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34504 false, clauses);
34505 c_name = "reduction";
34506 break;
34507 case PRAGMA_OACC_CLAUSE_SEQ:
34508 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
34509 clauses, here);
34510 c_name = "seq";
34511 break;
34512 case PRAGMA_OACC_CLAUSE_TILE:
34513 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
34514 c_name = "tile";
34515 break;
34516 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
34517 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34518 clauses);
34519 c_name = "use_device";
34520 break;
34521 case PRAGMA_OACC_CLAUSE_VECTOR:
34522 c_name = "vector";
34523 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
34524 c_name, clauses);
34525 break;
34526 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
34527 c_name = "vector_length";
34528 code = OMP_CLAUSE_VECTOR_LENGTH;
34529 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34530 clauses);
34531 break;
34532 case PRAGMA_OACC_CLAUSE_WAIT:
34533 clauses = cp_parser_oacc_clause_wait (parser, clauses);
34534 c_name = "wait";
34535 break;
34536 case PRAGMA_OACC_CLAUSE_WORKER:
34537 c_name = "worker";
34538 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
34539 c_name, clauses);
34540 break;
34541 default:
34542 cp_parser_error (parser, "expected %<#pragma acc%> clause");
34543 goto saw_error;
34544 }
34545
34546 first = false;
34547
34548 if (((mask >> c_kind) & 1) == 0)
34549 {
34550 /* Remove the invalid clause(s) from the list to avoid
34551 confusing the rest of the compiler. */
34552 clauses = prev;
34553 error_at (here, "%qs is not valid for %qs", c_name, where);
34554 }
34555 }
34556
34557 saw_error:
34558 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34559
34560 if (finish_p)
34561 return finish_omp_clauses (clauses, C_ORT_ACC);
34562
34563 return clauses;
34564 }
34565
34566 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34567 is a bitmask in MASK. Return the list of clauses found; the result
34568 of clause default goes in *pdefault. */
34569
34570 static tree
34571 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
34572 const char *where, cp_token *pragma_tok,
34573 bool finish_p = true)
34574 {
34575 tree clauses = NULL;
34576 bool first = true;
34577 cp_token *token = NULL;
34578
34579 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34580 {
34581 pragma_omp_clause c_kind;
34582 const char *c_name;
34583 tree prev = clauses;
34584
34585 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34586 cp_lexer_consume_token (parser->lexer);
34587
34588 token = cp_lexer_peek_token (parser->lexer);
34589 c_kind = cp_parser_omp_clause_name (parser);
34590
34591 switch (c_kind)
34592 {
34593 case PRAGMA_OMP_CLAUSE_COLLAPSE:
34594 clauses = cp_parser_omp_clause_collapse (parser, clauses,
34595 token->location);
34596 c_name = "collapse";
34597 break;
34598 case PRAGMA_OMP_CLAUSE_COPYIN:
34599 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
34600 c_name = "copyin";
34601 break;
34602 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
34603 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
34604 clauses);
34605 c_name = "copyprivate";
34606 break;
34607 case PRAGMA_OMP_CLAUSE_DEFAULT:
34608 clauses = cp_parser_omp_clause_default (parser, clauses,
34609 token->location, false);
34610 c_name = "default";
34611 break;
34612 case PRAGMA_OMP_CLAUSE_FINAL:
34613 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
34614 c_name = "final";
34615 break;
34616 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
34617 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34618 clauses);
34619 c_name = "firstprivate";
34620 break;
34621 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34622 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34623 token->location);
34624 c_name = "grainsize";
34625 break;
34626 case PRAGMA_OMP_CLAUSE_HINT:
34627 clauses = cp_parser_omp_clause_hint (parser, clauses,
34628 token->location);
34629 c_name = "hint";
34630 break;
34631 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34632 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34633 token->location);
34634 c_name = "defaultmap";
34635 break;
34636 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34637 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34638 clauses);
34639 c_name = "use_device_ptr";
34640 break;
34641 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34642 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34643 clauses);
34644 c_name = "is_device_ptr";
34645 break;
34646 case PRAGMA_OMP_CLAUSE_IF:
34647 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34648 true);
34649 c_name = "if";
34650 break;
34651 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
34652 clauses
34653 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
34654 true, clauses);
34655 c_name = "in_reduction";
34656 break;
34657 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34658 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
34659 c_name = "lastprivate";
34660 break;
34661 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34662 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34663 token->location);
34664 c_name = "mergeable";
34665 break;
34666 case PRAGMA_OMP_CLAUSE_NOWAIT:
34667 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34668 c_name = "nowait";
34669 break;
34670 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34671 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34672 token->location);
34673 c_name = "num_tasks";
34674 break;
34675 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34676 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34677 token->location);
34678 c_name = "num_threads";
34679 break;
34680 case PRAGMA_OMP_CLAUSE_ORDERED:
34681 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34682 token->location);
34683 c_name = "ordered";
34684 break;
34685 case PRAGMA_OMP_CLAUSE_PRIORITY:
34686 clauses = cp_parser_omp_clause_priority (parser, clauses,
34687 token->location);
34688 c_name = "priority";
34689 break;
34690 case PRAGMA_OMP_CLAUSE_PRIVATE:
34691 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34692 clauses);
34693 c_name = "private";
34694 break;
34695 case PRAGMA_OMP_CLAUSE_REDUCTION:
34696 clauses
34697 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34698 true, clauses);
34699 c_name = "reduction";
34700 break;
34701 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34702 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34703 token->location);
34704 c_name = "schedule";
34705 break;
34706 case PRAGMA_OMP_CLAUSE_SHARED:
34707 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34708 clauses);
34709 c_name = "shared";
34710 break;
34711 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
34712 clauses
34713 = cp_parser_omp_clause_reduction (parser,
34714 OMP_CLAUSE_TASK_REDUCTION,
34715 true, clauses);
34716 c_name = "task_reduction";
34717 break;
34718 case PRAGMA_OMP_CLAUSE_UNTIED:
34719 clauses = cp_parser_omp_clause_untied (parser, clauses,
34720 token->location);
34721 c_name = "untied";
34722 break;
34723 case PRAGMA_OMP_CLAUSE_INBRANCH:
34724 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34725 clauses, token->location);
34726 c_name = "inbranch";
34727 break;
34728 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
34729 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
34730 clauses);
34731 c_name = "nontemporal";
34732 break;
34733 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34734 clauses = cp_parser_omp_clause_branch (parser,
34735 OMP_CLAUSE_NOTINBRANCH,
34736 clauses, token->location);
34737 c_name = "notinbranch";
34738 break;
34739 case PRAGMA_OMP_CLAUSE_PARALLEL:
34740 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34741 clauses, token->location);
34742 c_name = "parallel";
34743 if (!first)
34744 {
34745 clause_not_first:
34746 error_at (token->location, "%qs must be the first clause of %qs",
34747 c_name, where);
34748 clauses = prev;
34749 }
34750 break;
34751 case PRAGMA_OMP_CLAUSE_FOR:
34752 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34753 clauses, token->location);
34754 c_name = "for";
34755 if (!first)
34756 goto clause_not_first;
34757 break;
34758 case PRAGMA_OMP_CLAUSE_SECTIONS:
34759 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34760 clauses, token->location);
34761 c_name = "sections";
34762 if (!first)
34763 goto clause_not_first;
34764 break;
34765 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34766 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34767 clauses, token->location);
34768 c_name = "taskgroup";
34769 if (!first)
34770 goto clause_not_first;
34771 break;
34772 case PRAGMA_OMP_CLAUSE_LINK:
34773 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34774 c_name = "to";
34775 break;
34776 case PRAGMA_OMP_CLAUSE_TO:
34777 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34778 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34779 clauses);
34780 else
34781 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34782 c_name = "to";
34783 break;
34784 case PRAGMA_OMP_CLAUSE_FROM:
34785 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34786 c_name = "from";
34787 break;
34788 case PRAGMA_OMP_CLAUSE_UNIFORM:
34789 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34790 clauses);
34791 c_name = "uniform";
34792 break;
34793 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34794 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34795 token->location);
34796 c_name = "num_teams";
34797 break;
34798 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34799 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34800 token->location);
34801 c_name = "thread_limit";
34802 break;
34803 case PRAGMA_OMP_CLAUSE_ALIGNED:
34804 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34805 c_name = "aligned";
34806 break;
34807 case PRAGMA_OMP_CLAUSE_LINEAR:
34808 {
34809 bool declare_simd = false;
34810 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34811 declare_simd = true;
34812 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34813 }
34814 c_name = "linear";
34815 break;
34816 case PRAGMA_OMP_CLAUSE_DEPEND:
34817 clauses = cp_parser_omp_clause_depend (parser, clauses,
34818 token->location);
34819 c_name = "depend";
34820 break;
34821 case PRAGMA_OMP_CLAUSE_MAP:
34822 clauses = cp_parser_omp_clause_map (parser, clauses);
34823 c_name = "map";
34824 break;
34825 case PRAGMA_OMP_CLAUSE_DEVICE:
34826 clauses = cp_parser_omp_clause_device (parser, clauses,
34827 token->location);
34828 c_name = "device";
34829 break;
34830 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34831 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34832 token->location);
34833 c_name = "dist_schedule";
34834 break;
34835 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34836 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34837 token->location);
34838 c_name = "proc_bind";
34839 break;
34840 case PRAGMA_OMP_CLAUSE_SAFELEN:
34841 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34842 token->location);
34843 c_name = "safelen";
34844 break;
34845 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34846 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34847 token->location);
34848 c_name = "simdlen";
34849 break;
34850 case PRAGMA_OMP_CLAUSE_NOGROUP:
34851 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34852 token->location);
34853 c_name = "nogroup";
34854 break;
34855 case PRAGMA_OMP_CLAUSE_THREADS:
34856 clauses
34857 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34858 clauses, token->location);
34859 c_name = "threads";
34860 break;
34861 case PRAGMA_OMP_CLAUSE_SIMD:
34862 clauses
34863 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34864 clauses, token->location);
34865 c_name = "simd";
34866 break;
34867 default:
34868 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34869 goto saw_error;
34870 }
34871
34872 first = false;
34873
34874 if (((mask >> c_kind) & 1) == 0)
34875 {
34876 /* Remove the invalid clause(s) from the list to avoid
34877 confusing the rest of the compiler. */
34878 clauses = prev;
34879 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34880 }
34881 }
34882 saw_error:
34883 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34884 if (finish_p)
34885 {
34886 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34887 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34888 else
34889 return finish_omp_clauses (clauses, C_ORT_OMP);
34890 }
34891 return clauses;
34892 }
34893
34894 /* OpenMP 2.5:
34895 structured-block:
34896 statement
34897
34898 In practice, we're also interested in adding the statement to an
34899 outer node. So it is convenient if we work around the fact that
34900 cp_parser_statement calls add_stmt. */
34901
34902 static unsigned
34903 cp_parser_begin_omp_structured_block (cp_parser *parser)
34904 {
34905 unsigned save = parser->in_statement;
34906
34907 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34908 This preserves the "not within loop or switch" style error messages
34909 for nonsense cases like
34910 void foo() {
34911 #pragma omp single
34912 break;
34913 }
34914 */
34915 if (parser->in_statement)
34916 parser->in_statement = IN_OMP_BLOCK;
34917
34918 return save;
34919 }
34920
34921 static void
34922 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34923 {
34924 parser->in_statement = save;
34925 }
34926
34927 static tree
34928 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34929 {
34930 tree stmt = begin_omp_structured_block ();
34931 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34932
34933 cp_parser_statement (parser, NULL_TREE, false, if_p);
34934
34935 cp_parser_end_omp_structured_block (parser, save);
34936 return finish_omp_structured_block (stmt);
34937 }
34938
34939 /* OpenMP 2.5:
34940 # pragma omp atomic new-line
34941 expression-stmt
34942
34943 expression-stmt:
34944 x binop= expr | x++ | ++x | x-- | --x
34945 binop:
34946 +, *, -, /, &, ^, |, <<, >>
34947
34948 where x is an lvalue expression with scalar type.
34949
34950 OpenMP 3.1:
34951 # pragma omp atomic new-line
34952 update-stmt
34953
34954 # pragma omp atomic read new-line
34955 read-stmt
34956
34957 # pragma omp atomic write new-line
34958 write-stmt
34959
34960 # pragma omp atomic update new-line
34961 update-stmt
34962
34963 # pragma omp atomic capture new-line
34964 capture-stmt
34965
34966 # pragma omp atomic capture new-line
34967 capture-block
34968
34969 read-stmt:
34970 v = x
34971 write-stmt:
34972 x = expr
34973 update-stmt:
34974 expression-stmt | x = x binop expr
34975 capture-stmt:
34976 v = expression-stmt
34977 capture-block:
34978 { v = x; update-stmt; } | { update-stmt; v = x; }
34979
34980 OpenMP 4.0:
34981 update-stmt:
34982 expression-stmt | x = x binop expr | x = expr binop x
34983 capture-stmt:
34984 v = update-stmt
34985 capture-block:
34986 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34987
34988 where x and v are lvalue expressions with scalar type. */
34989
34990 static void
34991 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34992 {
34993 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34994 tree rhs1 = NULL_TREE, orig_lhs;
34995 location_t loc = pragma_tok->location;
34996 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
34997 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
34998 bool structured_block = false;
34999 bool first = true;
35000 tree clauses = NULL_TREE;
35001
35002 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35003 {
35004 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35005 cp_lexer_consume_token (parser->lexer);
35006
35007 first = false;
35008
35009 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35010 {
35011 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35012 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35013 const char *p = IDENTIFIER_POINTER (id);
35014 enum tree_code new_code = ERROR_MARK;
35015 enum omp_memory_order new_memory_order
35016 = OMP_MEMORY_ORDER_UNSPECIFIED;
35017
35018 if (!strcmp (p, "read"))
35019 new_code = OMP_ATOMIC_READ;
35020 else if (!strcmp (p, "write"))
35021 new_code = NOP_EXPR;
35022 else if (!strcmp (p, "update"))
35023 new_code = OMP_ATOMIC;
35024 else if (!strcmp (p, "capture"))
35025 new_code = OMP_ATOMIC_CAPTURE_NEW;
35026 else if (!strcmp (p, "seq_cst"))
35027 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35028 else if (!strcmp (p, "acq_rel"))
35029 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35030 else if (!strcmp (p, "release"))
35031 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35032 else if (!strcmp (p, "acquire"))
35033 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35034 else if (!strcmp (p, "relaxed"))
35035 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35036 else if (!strcmp (p, "hint"))
35037 {
35038 cp_lexer_consume_token (parser->lexer);
35039 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35040 continue;
35041 }
35042 else
35043 {
35044 p = NULL;
35045 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35046 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35047 "%<release%>, %<relaxed%> or %<hint%> clause");
35048 }
35049 if (p)
35050 {
35051 if (new_code != ERROR_MARK)
35052 {
35053 if (code != ERROR_MARK)
35054 error_at (cloc, "too many atomic clauses");
35055 else
35056 code = new_code;
35057 }
35058 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35059 {
35060 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35061 error_at (cloc, "too many memory order clauses");
35062 else
35063 memory_order = new_memory_order;
35064 }
35065 cp_lexer_consume_token (parser->lexer);
35066 continue;
35067 }
35068 }
35069 break;
35070 }
35071 cp_parser_require_pragma_eol (parser, pragma_tok);
35072
35073 if (code == ERROR_MARK)
35074 code = OMP_ATOMIC;
35075 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35076 {
35077 omp_requires_mask
35078 = (enum omp_requires) (omp_requires_mask
35079 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35080 switch ((enum omp_memory_order)
35081 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35082 {
35083 case OMP_MEMORY_ORDER_UNSPECIFIED:
35084 case OMP_MEMORY_ORDER_RELAXED:
35085 memory_order = OMP_MEMORY_ORDER_RELAXED;
35086 break;
35087 case OMP_MEMORY_ORDER_SEQ_CST:
35088 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35089 break;
35090 case OMP_MEMORY_ORDER_ACQ_REL:
35091 switch (code)
35092 {
35093 case OMP_ATOMIC_READ:
35094 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35095 break;
35096 case NOP_EXPR: /* atomic write */
35097 case OMP_ATOMIC:
35098 memory_order = OMP_MEMORY_ORDER_RELEASE;
35099 break;
35100 default:
35101 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35102 break;
35103 }
35104 break;
35105 default:
35106 gcc_unreachable ();
35107 }
35108 }
35109 else
35110 switch (code)
35111 {
35112 case OMP_ATOMIC_READ:
35113 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35114 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35115 {
35116 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35117 "%<acq_rel%> or %<release%> clauses");
35118 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35119 }
35120 break;
35121 case NOP_EXPR: /* atomic write */
35122 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35123 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35124 {
35125 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35126 "%<acq_rel%> or %<acquire%> clauses");
35127 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35128 }
35129 break;
35130 case OMP_ATOMIC:
35131 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35132 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35133 {
35134 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35135 "%<acq_rel%> or %<acquire%> clauses");
35136 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35137 }
35138 break;
35139 default:
35140 break;
35141 }
35142
35143 switch (code)
35144 {
35145 case OMP_ATOMIC_READ:
35146 case NOP_EXPR: /* atomic write */
35147 v = cp_parser_unary_expression (parser);
35148 if (v == error_mark_node)
35149 goto saw_error;
35150 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35151 goto saw_error;
35152 if (code == NOP_EXPR)
35153 lhs = cp_parser_expression (parser);
35154 else
35155 lhs = cp_parser_unary_expression (parser);
35156 if (lhs == error_mark_node)
35157 goto saw_error;
35158 if (code == NOP_EXPR)
35159 {
35160 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35161 opcode. */
35162 code = OMP_ATOMIC;
35163 rhs = lhs;
35164 lhs = v;
35165 v = NULL_TREE;
35166 }
35167 goto done;
35168 case OMP_ATOMIC_CAPTURE_NEW:
35169 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35170 {
35171 cp_lexer_consume_token (parser->lexer);
35172 structured_block = true;
35173 }
35174 else
35175 {
35176 v = cp_parser_unary_expression (parser);
35177 if (v == error_mark_node)
35178 goto saw_error;
35179 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35180 goto saw_error;
35181 }
35182 default:
35183 break;
35184 }
35185
35186 restart:
35187 lhs = cp_parser_unary_expression (parser);
35188 orig_lhs = lhs;
35189 switch (TREE_CODE (lhs))
35190 {
35191 case ERROR_MARK:
35192 goto saw_error;
35193
35194 case POSTINCREMENT_EXPR:
35195 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35196 code = OMP_ATOMIC_CAPTURE_OLD;
35197 /* FALLTHROUGH */
35198 case PREINCREMENT_EXPR:
35199 lhs = TREE_OPERAND (lhs, 0);
35200 opcode = PLUS_EXPR;
35201 rhs = integer_one_node;
35202 break;
35203
35204 case POSTDECREMENT_EXPR:
35205 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35206 code = OMP_ATOMIC_CAPTURE_OLD;
35207 /* FALLTHROUGH */
35208 case PREDECREMENT_EXPR:
35209 lhs = TREE_OPERAND (lhs, 0);
35210 opcode = MINUS_EXPR;
35211 rhs = integer_one_node;
35212 break;
35213
35214 case COMPOUND_EXPR:
35215 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35216 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35217 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35218 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35219 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35220 (TREE_OPERAND (lhs, 1), 0), 0)))
35221 == BOOLEAN_TYPE)
35222 /* Undo effects of boolean_increment for post {in,de}crement. */
35223 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35224 /* FALLTHRU */
35225 case MODIFY_EXPR:
35226 if (TREE_CODE (lhs) == MODIFY_EXPR
35227 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35228 {
35229 /* Undo effects of boolean_increment. */
35230 if (integer_onep (TREE_OPERAND (lhs, 1)))
35231 {
35232 /* This is pre or post increment. */
35233 rhs = TREE_OPERAND (lhs, 1);
35234 lhs = TREE_OPERAND (lhs, 0);
35235 opcode = NOP_EXPR;
35236 if (code == OMP_ATOMIC_CAPTURE_NEW
35237 && !structured_block
35238 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35239 code = OMP_ATOMIC_CAPTURE_OLD;
35240 break;
35241 }
35242 }
35243 /* FALLTHRU */
35244 default:
35245 switch (cp_lexer_peek_token (parser->lexer)->type)
35246 {
35247 case CPP_MULT_EQ:
35248 opcode = MULT_EXPR;
35249 break;
35250 case CPP_DIV_EQ:
35251 opcode = TRUNC_DIV_EXPR;
35252 break;
35253 case CPP_PLUS_EQ:
35254 opcode = PLUS_EXPR;
35255 break;
35256 case CPP_MINUS_EQ:
35257 opcode = MINUS_EXPR;
35258 break;
35259 case CPP_LSHIFT_EQ:
35260 opcode = LSHIFT_EXPR;
35261 break;
35262 case CPP_RSHIFT_EQ:
35263 opcode = RSHIFT_EXPR;
35264 break;
35265 case CPP_AND_EQ:
35266 opcode = BIT_AND_EXPR;
35267 break;
35268 case CPP_OR_EQ:
35269 opcode = BIT_IOR_EXPR;
35270 break;
35271 case CPP_XOR_EQ:
35272 opcode = BIT_XOR_EXPR;
35273 break;
35274 case CPP_EQ:
35275 enum cp_parser_prec oprec;
35276 cp_token *token;
35277 cp_lexer_consume_token (parser->lexer);
35278 cp_parser_parse_tentatively (parser);
35279 rhs1 = cp_parser_simple_cast_expression (parser);
35280 if (rhs1 == error_mark_node)
35281 {
35282 cp_parser_abort_tentative_parse (parser);
35283 cp_parser_simple_cast_expression (parser);
35284 goto saw_error;
35285 }
35286 token = cp_lexer_peek_token (parser->lexer);
35287 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35288 {
35289 cp_parser_abort_tentative_parse (parser);
35290 cp_parser_parse_tentatively (parser);
35291 rhs = cp_parser_binary_expression (parser, false, true,
35292 PREC_NOT_OPERATOR, NULL);
35293 if (rhs == error_mark_node)
35294 {
35295 cp_parser_abort_tentative_parse (parser);
35296 cp_parser_binary_expression (parser, false, true,
35297 PREC_NOT_OPERATOR, NULL);
35298 goto saw_error;
35299 }
35300 switch (TREE_CODE (rhs))
35301 {
35302 case MULT_EXPR:
35303 case TRUNC_DIV_EXPR:
35304 case RDIV_EXPR:
35305 case PLUS_EXPR:
35306 case MINUS_EXPR:
35307 case LSHIFT_EXPR:
35308 case RSHIFT_EXPR:
35309 case BIT_AND_EXPR:
35310 case BIT_IOR_EXPR:
35311 case BIT_XOR_EXPR:
35312 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35313 {
35314 if (cp_parser_parse_definitely (parser))
35315 {
35316 opcode = TREE_CODE (rhs);
35317 rhs1 = TREE_OPERAND (rhs, 0);
35318 rhs = TREE_OPERAND (rhs, 1);
35319 goto stmt_done;
35320 }
35321 else
35322 goto saw_error;
35323 }
35324 break;
35325 default:
35326 break;
35327 }
35328 cp_parser_abort_tentative_parse (parser);
35329 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35330 {
35331 rhs = cp_parser_expression (parser);
35332 if (rhs == error_mark_node)
35333 goto saw_error;
35334 opcode = NOP_EXPR;
35335 rhs1 = NULL_TREE;
35336 goto stmt_done;
35337 }
35338 cp_parser_error (parser,
35339 "invalid form of %<#pragma omp atomic%>");
35340 goto saw_error;
35341 }
35342 if (!cp_parser_parse_definitely (parser))
35343 goto saw_error;
35344 switch (token->type)
35345 {
35346 case CPP_SEMICOLON:
35347 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35348 {
35349 code = OMP_ATOMIC_CAPTURE_OLD;
35350 v = lhs;
35351 lhs = NULL_TREE;
35352 lhs1 = rhs1;
35353 rhs1 = NULL_TREE;
35354 cp_lexer_consume_token (parser->lexer);
35355 goto restart;
35356 }
35357 else if (structured_block)
35358 {
35359 opcode = NOP_EXPR;
35360 rhs = rhs1;
35361 rhs1 = NULL_TREE;
35362 goto stmt_done;
35363 }
35364 cp_parser_error (parser,
35365 "invalid form of %<#pragma omp atomic%>");
35366 goto saw_error;
35367 case CPP_MULT:
35368 opcode = MULT_EXPR;
35369 break;
35370 case CPP_DIV:
35371 opcode = TRUNC_DIV_EXPR;
35372 break;
35373 case CPP_PLUS:
35374 opcode = PLUS_EXPR;
35375 break;
35376 case CPP_MINUS:
35377 opcode = MINUS_EXPR;
35378 break;
35379 case CPP_LSHIFT:
35380 opcode = LSHIFT_EXPR;
35381 break;
35382 case CPP_RSHIFT:
35383 opcode = RSHIFT_EXPR;
35384 break;
35385 case CPP_AND:
35386 opcode = BIT_AND_EXPR;
35387 break;
35388 case CPP_OR:
35389 opcode = BIT_IOR_EXPR;
35390 break;
35391 case CPP_XOR:
35392 opcode = BIT_XOR_EXPR;
35393 break;
35394 default:
35395 cp_parser_error (parser,
35396 "invalid operator for %<#pragma omp atomic%>");
35397 goto saw_error;
35398 }
35399 oprec = TOKEN_PRECEDENCE (token);
35400 gcc_assert (oprec != PREC_NOT_OPERATOR);
35401 if (commutative_tree_code (opcode))
35402 oprec = (enum cp_parser_prec) (oprec - 1);
35403 cp_lexer_consume_token (parser->lexer);
35404 rhs = cp_parser_binary_expression (parser, false, false,
35405 oprec, NULL);
35406 if (rhs == error_mark_node)
35407 goto saw_error;
35408 goto stmt_done;
35409 /* FALLTHROUGH */
35410 default:
35411 cp_parser_error (parser,
35412 "invalid operator for %<#pragma omp atomic%>");
35413 goto saw_error;
35414 }
35415 cp_lexer_consume_token (parser->lexer);
35416
35417 rhs = cp_parser_expression (parser);
35418 if (rhs == error_mark_node)
35419 goto saw_error;
35420 break;
35421 }
35422 stmt_done:
35423 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35424 {
35425 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35426 goto saw_error;
35427 v = cp_parser_unary_expression (parser);
35428 if (v == error_mark_node)
35429 goto saw_error;
35430 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35431 goto saw_error;
35432 lhs1 = cp_parser_unary_expression (parser);
35433 if (lhs1 == error_mark_node)
35434 goto saw_error;
35435 }
35436 if (structured_block)
35437 {
35438 cp_parser_consume_semicolon_at_end_of_statement (parser);
35439 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35440 }
35441 done:
35442 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35443 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35444 rhs1, clauses, memory_order);
35445 if (!structured_block)
35446 cp_parser_consume_semicolon_at_end_of_statement (parser);
35447 return;
35448
35449 saw_error:
35450 cp_parser_skip_to_end_of_block_or_statement (parser);
35451 if (structured_block)
35452 {
35453 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35454 cp_lexer_consume_token (parser->lexer);
35455 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35456 {
35457 cp_parser_skip_to_end_of_block_or_statement (parser);
35458 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35459 cp_lexer_consume_token (parser->lexer);
35460 }
35461 }
35462 }
35463
35464
35465 /* OpenMP 2.5:
35466 # pragma omp barrier new-line */
35467
35468 static void
35469 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35470 {
35471 cp_parser_require_pragma_eol (parser, pragma_tok);
35472 finish_omp_barrier ();
35473 }
35474
35475 /* OpenMP 2.5:
35476 # pragma omp critical [(name)] new-line
35477 structured-block
35478
35479 OpenMP 4.5:
35480 # pragma omp critical [(name) [hint(expression)]] new-line
35481 structured-block */
35482
35483 #define OMP_CRITICAL_CLAUSE_MASK \
35484 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35485
35486 static tree
35487 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35488 {
35489 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
35490
35491 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35492 {
35493 matching_parens parens;
35494 parens.consume_open (parser);
35495
35496 name = cp_parser_identifier (parser);
35497
35498 if (name == error_mark_node
35499 || !parens.require_close (parser))
35500 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35501 /*or_comma=*/false,
35502 /*consume_paren=*/true);
35503 if (name == error_mark_node)
35504 name = NULL;
35505
35506 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
35507 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35508 cp_lexer_consume_token (parser->lexer);
35509
35510 clauses = cp_parser_omp_all_clauses (parser,
35511 OMP_CRITICAL_CLAUSE_MASK,
35512 "#pragma omp critical", pragma_tok);
35513 }
35514 else
35515 cp_parser_require_pragma_eol (parser, pragma_tok);
35516
35517 stmt = cp_parser_omp_structured_block (parser, if_p);
35518 return c_finish_omp_critical (input_location, stmt, name, clauses);
35519 }
35520
35521 /* OpenMP 5.0:
35522 # pragma omp depobj ( depobj ) depobj-clause new-line
35523
35524 depobj-clause:
35525 depend (dependence-type : locator)
35526 destroy
35527 update (dependence-type)
35528
35529 dependence-type:
35530 in
35531 out
35532 inout
35533 mutexinout */
35534
35535 static void
35536 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
35537 {
35538 location_t loc = pragma_tok->location;
35539 matching_parens parens;
35540 if (!parens.require_open (parser))
35541 {
35542 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35543 return;
35544 }
35545
35546 tree depobj = cp_parser_assignment_expression (parser);
35547
35548 if (!parens.require_close (parser))
35549 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35550 /*or_comma=*/false,
35551 /*consume_paren=*/true);
35552
35553 tree clause = NULL_TREE;
35554 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
35555 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
35556 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35557 {
35558 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35559 const char *p = IDENTIFIER_POINTER (id);
35560
35561 cp_lexer_consume_token (parser->lexer);
35562 if (!strcmp ("depend", p))
35563 {
35564 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
35565 if (clause)
35566 clause = finish_omp_clauses (clause, C_ORT_OMP);
35567 if (!clause)
35568 clause = error_mark_node;
35569 }
35570 else if (!strcmp ("destroy", p))
35571 kind = OMP_CLAUSE_DEPEND_LAST;
35572 else if (!strcmp ("update", p))
35573 {
35574 matching_parens c_parens;
35575 if (c_parens.require_open (parser))
35576 {
35577 location_t c2_loc
35578 = cp_lexer_peek_token (parser->lexer)->location;
35579 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35580 {
35581 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
35582 const char *p2 = IDENTIFIER_POINTER (id2);
35583
35584 cp_lexer_consume_token (parser->lexer);
35585 if (!strcmp ("in", p2))
35586 kind = OMP_CLAUSE_DEPEND_IN;
35587 else if (!strcmp ("out", p2))
35588 kind = OMP_CLAUSE_DEPEND_OUT;
35589 else if (!strcmp ("inout", p2))
35590 kind = OMP_CLAUSE_DEPEND_INOUT;
35591 else if (!strcmp ("mutexinoutset", p2))
35592 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
35593 }
35594 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
35595 {
35596 clause = error_mark_node;
35597 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
35598 "%<mutexinoutset%>");
35599 }
35600 if (!c_parens.require_close (parser))
35601 cp_parser_skip_to_closing_parenthesis (parser,
35602 /*recovering=*/true,
35603 /*or_comma=*/false,
35604 /*consume_paren=*/true);
35605 }
35606 else
35607 clause = error_mark_node;
35608 }
35609 }
35610 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
35611 {
35612 clause = error_mark_node;
35613 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
35614 }
35615 cp_parser_require_pragma_eol (parser, pragma_tok);
35616
35617 finish_omp_depobj (loc, depobj, kind, clause);
35618 }
35619
35620
35621 /* OpenMP 2.5:
35622 # pragma omp flush flush-vars[opt] new-line
35623
35624 flush-vars:
35625 ( variable-list )
35626
35627 OpenMP 5.0:
35628 # pragma omp flush memory-order-clause new-line */
35629
35630 static void
35631 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
35632 {
35633 enum memmodel mo = MEMMODEL_LAST;
35634 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35635 {
35636 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35637 const char *p = IDENTIFIER_POINTER (id);
35638 if (!strcmp (p, "acq_rel"))
35639 mo = MEMMODEL_ACQ_REL;
35640 else if (!strcmp (p, "release"))
35641 mo = MEMMODEL_RELEASE;
35642 else if (!strcmp (p, "acquire"))
35643 mo = MEMMODEL_ACQUIRE;
35644 else
35645 error_at (cp_lexer_peek_token (parser->lexer)->location,
35646 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35647 cp_lexer_consume_token (parser->lexer);
35648 }
35649 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35650 {
35651 if (mo != MEMMODEL_LAST)
35652 error_at (cp_lexer_peek_token (parser->lexer)->location,
35653 "%<flush%> list specified together with memory order "
35654 "clause");
35655 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35656 }
35657 cp_parser_require_pragma_eol (parser, pragma_tok);
35658
35659 finish_omp_flush (mo);
35660 }
35661
35662 /* Helper function, to parse omp for increment expression. */
35663
35664 static tree
35665 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
35666 {
35667 tree cond = cp_parser_binary_expression (parser, false, true,
35668 PREC_NOT_OPERATOR, NULL);
35669 if (cond == error_mark_node
35670 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35671 {
35672 cp_parser_skip_to_end_of_statement (parser);
35673 return error_mark_node;
35674 }
35675
35676 switch (TREE_CODE (cond))
35677 {
35678 case GT_EXPR:
35679 case GE_EXPR:
35680 case LT_EXPR:
35681 case LE_EXPR:
35682 break;
35683 case NE_EXPR:
35684 if (code != OACC_LOOP)
35685 break;
35686 gcc_fallthrough ();
35687 default:
35688 return error_mark_node;
35689 }
35690
35691 /* If decl is an iterator, preserve LHS and RHS of the relational
35692 expr until finish_omp_for. */
35693 if (decl
35694 && (type_dependent_expression_p (decl)
35695 || CLASS_TYPE_P (TREE_TYPE (decl))))
35696 return cond;
35697
35698 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
35699 TREE_CODE (cond),
35700 TREE_OPERAND (cond, 0), ERROR_MARK,
35701 TREE_OPERAND (cond, 1), ERROR_MARK,
35702 /*overload=*/NULL, tf_warning_or_error);
35703 }
35704
35705 /* Helper function, to parse omp for increment expression. */
35706
35707 static tree
35708 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
35709 {
35710 cp_token *token = cp_lexer_peek_token (parser->lexer);
35711 enum tree_code op;
35712 tree lhs, rhs;
35713 cp_id_kind idk;
35714 bool decl_first;
35715
35716 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35717 {
35718 op = (token->type == CPP_PLUS_PLUS
35719 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
35720 cp_lexer_consume_token (parser->lexer);
35721 lhs = cp_parser_simple_cast_expression (parser);
35722 if (lhs != decl
35723 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35724 return error_mark_node;
35725 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35726 }
35727
35728 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
35729 if (lhs != decl
35730 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35731 return error_mark_node;
35732
35733 token = cp_lexer_peek_token (parser->lexer);
35734 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35735 {
35736 op = (token->type == CPP_PLUS_PLUS
35737 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
35738 cp_lexer_consume_token (parser->lexer);
35739 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35740 }
35741
35742 op = cp_parser_assignment_operator_opt (parser);
35743 if (op == ERROR_MARK)
35744 return error_mark_node;
35745
35746 if (op != NOP_EXPR)
35747 {
35748 rhs = cp_parser_assignment_expression (parser);
35749 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
35750 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35751 }
35752
35753 lhs = cp_parser_binary_expression (parser, false, false,
35754 PREC_ADDITIVE_EXPRESSION, NULL);
35755 token = cp_lexer_peek_token (parser->lexer);
35756 decl_first = (lhs == decl
35757 || (processing_template_decl && cp_tree_equal (lhs, decl)));
35758 if (decl_first)
35759 lhs = NULL_TREE;
35760 if (token->type != CPP_PLUS
35761 && token->type != CPP_MINUS)
35762 return error_mark_node;
35763
35764 do
35765 {
35766 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
35767 cp_lexer_consume_token (parser->lexer);
35768 rhs = cp_parser_binary_expression (parser, false, false,
35769 PREC_ADDITIVE_EXPRESSION, NULL);
35770 token = cp_lexer_peek_token (parser->lexer);
35771 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
35772 {
35773 if (lhs == NULL_TREE)
35774 {
35775 if (op == PLUS_EXPR)
35776 lhs = rhs;
35777 else
35778 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
35779 tf_warning_or_error);
35780 }
35781 else
35782 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
35783 ERROR_MARK, NULL, tf_warning_or_error);
35784 }
35785 }
35786 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
35787
35788 if (!decl_first)
35789 {
35790 if ((rhs != decl
35791 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
35792 || op == MINUS_EXPR)
35793 return error_mark_node;
35794 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
35795 }
35796 else
35797 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
35798
35799 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35800 }
35801
35802 /* Parse the initialization statement of an OpenMP for loop.
35803
35804 Return true if the resulting construct should have an
35805 OMP_CLAUSE_PRIVATE added to it. */
35806
35807 static tree
35808 cp_parser_omp_for_loop_init (cp_parser *parser,
35809 tree &this_pre_body,
35810 vec<tree, va_gc> *&for_block,
35811 tree &init,
35812 tree &orig_init,
35813 tree &decl,
35814 tree &real_decl)
35815 {
35816 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35817 return NULL_TREE;
35818
35819 tree add_private_clause = NULL_TREE;
35820
35821 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35822
35823 init-expr:
35824 var = lb
35825 integer-type var = lb
35826 random-access-iterator-type var = lb
35827 pointer-type var = lb
35828 */
35829 cp_decl_specifier_seq type_specifiers;
35830
35831 /* First, try to parse as an initialized declaration. See
35832 cp_parser_condition, from whence the bulk of this is copied. */
35833
35834 cp_parser_parse_tentatively (parser);
35835 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
35836 /*is_trailing_return=*/false,
35837 &type_specifiers);
35838 if (cp_parser_parse_definitely (parser))
35839 {
35840 /* If parsing a type specifier seq succeeded, then this
35841 MUST be a initialized declaration. */
35842 tree asm_specification, attributes;
35843 cp_declarator *declarator;
35844
35845 declarator = cp_parser_declarator (parser,
35846 CP_PARSER_DECLARATOR_NAMED,
35847 /*ctor_dtor_or_conv_p=*/NULL,
35848 /*parenthesized_p=*/NULL,
35849 /*member_p=*/false,
35850 /*friend_p=*/false);
35851 attributes = cp_parser_attributes_opt (parser);
35852 asm_specification = cp_parser_asm_specification_opt (parser);
35853
35854 if (declarator == cp_error_declarator)
35855 cp_parser_skip_to_end_of_statement (parser);
35856
35857 else
35858 {
35859 tree pushed_scope, auto_node;
35860
35861 decl = start_decl (declarator, &type_specifiers,
35862 SD_INITIALIZED, attributes,
35863 /*prefix_attributes=*/NULL_TREE,
35864 &pushed_scope);
35865
35866 auto_node = type_uses_auto (TREE_TYPE (decl));
35867 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
35868 {
35869 if (cp_lexer_next_token_is (parser->lexer,
35870 CPP_OPEN_PAREN))
35871 error ("parenthesized initialization is not allowed in "
35872 "OpenMP %<for%> loop");
35873 else
35874 /* Trigger an error. */
35875 cp_parser_require (parser, CPP_EQ, RT_EQ);
35876
35877 init = error_mark_node;
35878 cp_parser_skip_to_end_of_statement (parser);
35879 }
35880 else if (CLASS_TYPE_P (TREE_TYPE (decl))
35881 || type_dependent_expression_p (decl)
35882 || auto_node)
35883 {
35884 bool is_direct_init, is_non_constant_init;
35885
35886 init = cp_parser_initializer (parser,
35887 &is_direct_init,
35888 &is_non_constant_init);
35889
35890 if (auto_node)
35891 {
35892 TREE_TYPE (decl)
35893 = do_auto_deduction (TREE_TYPE (decl), init,
35894 auto_node);
35895
35896 if (!CLASS_TYPE_P (TREE_TYPE (decl))
35897 && !type_dependent_expression_p (decl))
35898 goto non_class;
35899 }
35900
35901 cp_finish_decl (decl, init, !is_non_constant_init,
35902 asm_specification,
35903 LOOKUP_ONLYCONVERTING);
35904 orig_init = init;
35905 if (CLASS_TYPE_P (TREE_TYPE (decl)))
35906 {
35907 vec_safe_push (for_block, this_pre_body);
35908 init = NULL_TREE;
35909 }
35910 else
35911 {
35912 init = pop_stmt_list (this_pre_body);
35913 if (init && TREE_CODE (init) == STATEMENT_LIST)
35914 {
35915 tree_stmt_iterator i = tsi_start (init);
35916 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
35917 while (!tsi_end_p (i))
35918 {
35919 tree t = tsi_stmt (i);
35920 if (TREE_CODE (t) == DECL_EXPR
35921 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
35922 {
35923 tsi_delink (&i);
35924 vec_safe_push (for_block, t);
35925 continue;
35926 }
35927 break;
35928 }
35929 if (tsi_one_before_end_p (i))
35930 {
35931 tree t = tsi_stmt (i);
35932 tsi_delink (&i);
35933 free_stmt_list (init);
35934 init = t;
35935 }
35936 }
35937 }
35938 this_pre_body = NULL_TREE;
35939 }
35940 else
35941 {
35942 /* Consume '='. */
35943 cp_lexer_consume_token (parser->lexer);
35944 init = cp_parser_assignment_expression (parser);
35945
35946 non_class:
35947 if (TYPE_REF_P (TREE_TYPE (decl)))
35948 init = error_mark_node;
35949 else
35950 cp_finish_decl (decl, NULL_TREE,
35951 /*init_const_expr_p=*/false,
35952 asm_specification,
35953 LOOKUP_ONLYCONVERTING);
35954 }
35955
35956 if (pushed_scope)
35957 pop_scope (pushed_scope);
35958 }
35959 }
35960 else
35961 {
35962 cp_id_kind idk;
35963 /* If parsing a type specifier sequence failed, then
35964 this MUST be a simple expression. */
35965 cp_parser_parse_tentatively (parser);
35966 decl = cp_parser_primary_expression (parser, false, false,
35967 false, &idk);
35968 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
35969 if (!cp_parser_error_occurred (parser)
35970 && decl
35971 && (TREE_CODE (decl) == COMPONENT_REF
35972 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
35973 {
35974 cp_parser_abort_tentative_parse (parser);
35975 cp_parser_parse_tentatively (parser);
35976 cp_token *token = cp_lexer_peek_token (parser->lexer);
35977 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
35978 /*check_dependency_p=*/true,
35979 /*template_p=*/NULL,
35980 /*declarator_p=*/false,
35981 /*optional_p=*/false);
35982 if (name != error_mark_node
35983 && last_tok == cp_lexer_peek_token (parser->lexer))
35984 {
35985 decl = cp_parser_lookup_name_simple (parser, name,
35986 token->location);
35987 if (TREE_CODE (decl) == FIELD_DECL)
35988 add_private_clause = omp_privatize_field (decl, false);
35989 }
35990 cp_parser_abort_tentative_parse (parser);
35991 cp_parser_parse_tentatively (parser);
35992 decl = cp_parser_primary_expression (parser, false, false,
35993 false, &idk);
35994 }
35995 if (!cp_parser_error_occurred (parser)
35996 && decl
35997 && DECL_P (decl)
35998 && CLASS_TYPE_P (TREE_TYPE (decl)))
35999 {
36000 tree rhs;
36001
36002 cp_parser_parse_definitely (parser);
36003 cp_parser_require (parser, CPP_EQ, RT_EQ);
36004 rhs = cp_parser_assignment_expression (parser);
36005 orig_init = rhs;
36006 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36007 decl, NOP_EXPR,
36008 rhs,
36009 tf_warning_or_error));
36010 if (!add_private_clause)
36011 add_private_clause = decl;
36012 }
36013 else
36014 {
36015 decl = NULL;
36016 cp_parser_abort_tentative_parse (parser);
36017 init = cp_parser_expression (parser);
36018 if (init)
36019 {
36020 if (TREE_CODE (init) == MODIFY_EXPR
36021 || TREE_CODE (init) == MODOP_EXPR)
36022 real_decl = TREE_OPERAND (init, 0);
36023 }
36024 }
36025 }
36026 return add_private_clause;
36027 }
36028
36029 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36030
36031 void
36032 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36033 tree &decl, tree &orig_decl, tree &init,
36034 tree &orig_init, tree &cond, tree &incr)
36035 {
36036 tree begin, end, range_temp_decl = NULL_TREE;
36037 tree iter_type, begin_expr, end_expr;
36038
36039 if (processing_template_decl)
36040 {
36041 if (check_for_bare_parameter_packs (init))
36042 init = error_mark_node;
36043 if (!type_dependent_expression_p (init)
36044 /* do_auto_deduction doesn't mess with template init-lists. */
36045 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36046 {
36047 tree d = decl;
36048 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36049 {
36050 tree v = DECL_VALUE_EXPR (decl);
36051 if (TREE_CODE (v) == ARRAY_REF
36052 && VAR_P (TREE_OPERAND (v, 0))
36053 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36054 d = TREE_OPERAND (v, 0);
36055 }
36056 do_range_for_auto_deduction (d, init);
36057 }
36058 cond = global_namespace;
36059 incr = NULL_TREE;
36060 orig_init = init;
36061 if (this_pre_body)
36062 this_pre_body = pop_stmt_list (this_pre_body);
36063 return;
36064 }
36065
36066 init = mark_lvalue_use (init);
36067
36068 if (decl == error_mark_node || init == error_mark_node)
36069 /* If an error happened previously do nothing or else a lot of
36070 unhelpful errors would be issued. */
36071 begin_expr = end_expr = iter_type = error_mark_node;
36072 else
36073 {
36074 tree range_temp;
36075
36076 if (VAR_P (init)
36077 && array_of_runtime_bound_p (TREE_TYPE (init)))
36078 /* Can't bind a reference to an array of runtime bound. */
36079 range_temp = init;
36080 else
36081 {
36082 range_temp = build_range_temp (init);
36083 DECL_NAME (range_temp) = NULL_TREE;
36084 pushdecl (range_temp);
36085 cp_finish_decl (range_temp, init,
36086 /*is_constant_init*/false, NULL_TREE,
36087 LOOKUP_ONLYCONVERTING);
36088 range_temp_decl = range_temp;
36089 range_temp = convert_from_reference (range_temp);
36090 }
36091 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36092 &begin_expr, &end_expr);
36093 }
36094
36095 tree end_iter_type = iter_type;
36096 if (cxx_dialect >= cxx17)
36097 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36098 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36099 TREE_USED (end) = 1;
36100 DECL_ARTIFICIAL (end) = 1;
36101 pushdecl (end);
36102 cp_finish_decl (end, end_expr,
36103 /*is_constant_init*/false, NULL_TREE,
36104 LOOKUP_ONLYCONVERTING);
36105
36106 /* The new for initialization statement. */
36107 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36108 TREE_USED (begin) = 1;
36109 DECL_ARTIFICIAL (begin) = 1;
36110 pushdecl (begin);
36111 orig_init = init;
36112 if (CLASS_TYPE_P (iter_type))
36113 init = NULL_TREE;
36114 else
36115 {
36116 init = begin_expr;
36117 begin_expr = NULL_TREE;
36118 }
36119 cp_finish_decl (begin, begin_expr,
36120 /*is_constant_init*/false, NULL_TREE,
36121 LOOKUP_ONLYCONVERTING);
36122
36123 /* The new for condition. */
36124 if (CLASS_TYPE_P (iter_type))
36125 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36126 else
36127 cond = build_x_binary_op (input_location, NE_EXPR,
36128 begin, ERROR_MARK,
36129 end, ERROR_MARK,
36130 NULL, tf_warning_or_error);
36131
36132 /* The new increment expression. */
36133 if (CLASS_TYPE_P (iter_type))
36134 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36135 else
36136 incr = finish_unary_op_expr (input_location,
36137 PREINCREMENT_EXPR, begin,
36138 tf_warning_or_error);
36139
36140 orig_decl = decl;
36141 decl = begin;
36142 if (for_block)
36143 {
36144 vec_safe_push (for_block, this_pre_body);
36145 this_pre_body = NULL_TREE;
36146 }
36147
36148 tree decomp_first_name = NULL_TREE;
36149 unsigned decomp_cnt = 0;
36150 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36151 {
36152 tree v = DECL_VALUE_EXPR (orig_decl);
36153 if (TREE_CODE (v) == ARRAY_REF
36154 && VAR_P (TREE_OPERAND (v, 0))
36155 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36156 {
36157 tree d = orig_decl;
36158 orig_decl = TREE_OPERAND (v, 0);
36159 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36160 decomp_first_name = d;
36161 }
36162 }
36163
36164 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36165 if (auto_node)
36166 {
36167 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36168 tf_none);
36169 if (!error_operand_p (t))
36170 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36171 t, auto_node);
36172 }
36173
36174 tree v = make_tree_vec (decomp_cnt + 3);
36175 TREE_VEC_ELT (v, 0) = range_temp_decl;
36176 TREE_VEC_ELT (v, 1) = end;
36177 TREE_VEC_ELT (v, 2) = orig_decl;
36178 for (unsigned i = 0; i < decomp_cnt; i++)
36179 {
36180 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36181 decomp_first_name = DECL_CHAIN (decomp_first_name);
36182 }
36183 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36184 }
36185
36186 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36187 inside of the collapsed body. */
36188
36189 void
36190 cp_finish_omp_range_for (tree orig, tree begin)
36191 {
36192 gcc_assert (TREE_CODE (orig) == TREE_LIST
36193 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36194 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36195 tree decomp_first_name = NULL_TREE;
36196 unsigned int decomp_cnt = 0;
36197
36198 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36199 {
36200 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36201 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36202 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36203 }
36204
36205 /* The declaration is initialized with *__begin inside the loop body. */
36206 cp_finish_decl (decl,
36207 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36208 tf_warning_or_error),
36209 /*is_constant_init*/false, NULL_TREE,
36210 LOOKUP_ONLYCONVERTING);
36211 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36212 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36213 }
36214
36215 /* Parse the restricted form of the for statement allowed by OpenMP. */
36216
36217 static tree
36218 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36219 tree *cclauses, bool *if_p)
36220 {
36221 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36222 tree orig_decl;
36223 tree real_decl, initv, condv, incrv, declv, orig_declv;
36224 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36225 location_t loc_first;
36226 bool collapse_err = false;
36227 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36228 vec<tree, va_gc> *for_block = make_tree_vector ();
36229 auto_vec<tree, 4> orig_inits;
36230 bool tiling = false;
36231
36232 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36233 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36234 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36235 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36236 {
36237 tiling = true;
36238 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36239 }
36240 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36241 && OMP_CLAUSE_ORDERED_EXPR (cl))
36242 {
36243 ordered_cl = cl;
36244 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36245 }
36246
36247 if (ordered && ordered < collapse)
36248 {
36249 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36250 "%<ordered%> clause parameter is less than %<collapse%>");
36251 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36252 = build_int_cst (NULL_TREE, collapse);
36253 ordered = collapse;
36254 }
36255 if (ordered)
36256 {
36257 for (tree *pc = &clauses; *pc; )
36258 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36259 {
36260 error_at (OMP_CLAUSE_LOCATION (*pc),
36261 "%<linear%> clause may not be specified together "
36262 "with %<ordered%> clause with a parameter");
36263 *pc = OMP_CLAUSE_CHAIN (*pc);
36264 }
36265 else
36266 pc = &OMP_CLAUSE_CHAIN (*pc);
36267 }
36268
36269 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36270 count = ordered ? ordered : collapse;
36271
36272 declv = make_tree_vec (count);
36273 initv = make_tree_vec (count);
36274 condv = make_tree_vec (count);
36275 incrv = make_tree_vec (count);
36276 orig_declv = NULL_TREE;
36277
36278 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36279
36280 for (i = 0; i < count; i++)
36281 {
36282 int bracecount = 0;
36283 tree add_private_clause = NULL_TREE;
36284 location_t loc;
36285
36286 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36287 {
36288 if (!collapse_err)
36289 cp_parser_error (parser, "for statement expected");
36290 return NULL;
36291 }
36292 loc = cp_lexer_consume_token (parser->lexer)->location;
36293
36294 matching_parens parens;
36295 if (!parens.require_open (parser))
36296 return NULL;
36297
36298 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36299 this_pre_body = push_stmt_list ();
36300
36301 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36302 {
36303 /* Save tokens so that we can put them back. */
36304 cp_lexer_save_tokens (parser->lexer);
36305
36306 /* Look for ':' that is not nested in () or {}. */
36307 bool is_range_for
36308 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36309 /*recovering=*/false,
36310 CPP_COLON,
36311 /*consume_paren=*/
36312 false) == -1);
36313
36314 /* Roll back the tokens we skipped. */
36315 cp_lexer_rollback_tokens (parser->lexer);
36316
36317 if (is_range_for)
36318 {
36319 bool saved_colon_corrects_to_scope_p
36320 = parser->colon_corrects_to_scope_p;
36321
36322 /* A colon is used in range-based for. */
36323 parser->colon_corrects_to_scope_p = false;
36324
36325 /* Parse the declaration. */
36326 cp_parser_simple_declaration (parser,
36327 /*function_definition_allowed_p=*/
36328 false, &decl);
36329 parser->colon_corrects_to_scope_p
36330 = saved_colon_corrects_to_scope_p;
36331
36332 cp_parser_require (parser, CPP_COLON, RT_COLON);
36333
36334 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36335 false, 0, true);
36336
36337 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36338 orig_decl, init, orig_init,
36339 cond, incr);
36340 if (this_pre_body)
36341 {
36342 if (pre_body)
36343 {
36344 tree t = pre_body;
36345 pre_body = push_stmt_list ();
36346 add_stmt (t);
36347 add_stmt (this_pre_body);
36348 pre_body = pop_stmt_list (pre_body);
36349 }
36350 else
36351 pre_body = this_pre_body;
36352 }
36353
36354 if (ordered_cl)
36355 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36356 "%<ordered%> clause with parameter on "
36357 "range-based %<for%> loop");
36358
36359 goto parse_close_paren;
36360 }
36361 }
36362
36363 add_private_clause
36364 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36365 init, orig_init, decl, real_decl);
36366
36367 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36368 if (this_pre_body)
36369 {
36370 this_pre_body = pop_stmt_list (this_pre_body);
36371 if (pre_body)
36372 {
36373 tree t = pre_body;
36374 pre_body = push_stmt_list ();
36375 add_stmt (t);
36376 add_stmt (this_pre_body);
36377 pre_body = pop_stmt_list (pre_body);
36378 }
36379 else
36380 pre_body = this_pre_body;
36381 }
36382
36383 if (decl)
36384 real_decl = decl;
36385 if (cclauses != NULL
36386 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36387 && real_decl != NULL_TREE)
36388 {
36389 tree *c;
36390 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36391 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36392 && OMP_CLAUSE_DECL (*c) == real_decl)
36393 {
36394 error_at (loc, "iteration variable %qD"
36395 " should not be firstprivate", real_decl);
36396 *c = OMP_CLAUSE_CHAIN (*c);
36397 }
36398 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36399 && OMP_CLAUSE_DECL (*c) == real_decl)
36400 {
36401 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36402 tree l = *c;
36403 *c = OMP_CLAUSE_CHAIN (*c);
36404 if (code == OMP_SIMD)
36405 {
36406 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36407 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36408 }
36409 else
36410 {
36411 OMP_CLAUSE_CHAIN (l) = clauses;
36412 clauses = l;
36413 }
36414 add_private_clause = NULL_TREE;
36415 }
36416 else
36417 {
36418 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36419 && OMP_CLAUSE_DECL (*c) == real_decl)
36420 add_private_clause = NULL_TREE;
36421 c = &OMP_CLAUSE_CHAIN (*c);
36422 }
36423 }
36424
36425 if (add_private_clause)
36426 {
36427 tree c;
36428 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36429 {
36430 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36431 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36432 && OMP_CLAUSE_DECL (c) == decl)
36433 break;
36434 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36435 && OMP_CLAUSE_DECL (c) == decl)
36436 error_at (loc, "iteration variable %qD "
36437 "should not be firstprivate",
36438 decl);
36439 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36440 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36441 && OMP_CLAUSE_DECL (c) == decl)
36442 error_at (loc, "iteration variable %qD should not be reduction",
36443 decl);
36444 }
36445 if (c == NULL)
36446 {
36447 if (code != OMP_SIMD)
36448 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36449 else if (collapse == 1)
36450 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36451 else
36452 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36453 OMP_CLAUSE_DECL (c) = add_private_clause;
36454 c = finish_omp_clauses (c, C_ORT_OMP);
36455 if (c)
36456 {
36457 OMP_CLAUSE_CHAIN (c) = clauses;
36458 clauses = c;
36459 /* For linear, signal that we need to fill up
36460 the so far unknown linear step. */
36461 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36462 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36463 }
36464 }
36465 }
36466
36467 cond = NULL;
36468 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36469 cond = cp_parser_omp_for_cond (parser, decl, code);
36470 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36471
36472 incr = NULL;
36473 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36474 {
36475 /* If decl is an iterator, preserve the operator on decl
36476 until finish_omp_for. */
36477 if (real_decl
36478 && ((processing_template_decl
36479 && (TREE_TYPE (real_decl) == NULL_TREE
36480 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
36481 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
36482 incr = cp_parser_omp_for_incr (parser, real_decl);
36483 else
36484 incr = cp_parser_expression (parser);
36485 if (!EXPR_HAS_LOCATION (incr))
36486 protected_set_expr_location (incr, input_location);
36487 }
36488
36489 parse_close_paren:
36490 if (!parens.require_close (parser))
36491 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36492 /*or_comma=*/false,
36493 /*consume_paren=*/true);
36494
36495 TREE_VEC_ELT (declv, i) = decl;
36496 TREE_VEC_ELT (initv, i) = init;
36497 TREE_VEC_ELT (condv, i) = cond;
36498 TREE_VEC_ELT (incrv, i) = incr;
36499 if (orig_init)
36500 {
36501 orig_inits.safe_grow_cleared (i + 1);
36502 orig_inits[i] = orig_init;
36503 }
36504 if (orig_decl)
36505 {
36506 if (!orig_declv)
36507 orig_declv = copy_node (declv);
36508 TREE_VEC_ELT (orig_declv, i) = orig_decl;
36509 }
36510 else if (orig_declv)
36511 TREE_VEC_ELT (orig_declv, i) = decl;
36512
36513 if (i == count - 1)
36514 break;
36515
36516 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36517 in between the collapsed for loops to be still considered perfectly
36518 nested. Hopefully the final version clarifies this.
36519 For now handle (multiple) {'s and empty statements. */
36520 cp_parser_parse_tentatively (parser);
36521 for (;;)
36522 {
36523 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36524 break;
36525 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36526 {
36527 cp_lexer_consume_token (parser->lexer);
36528 bracecount++;
36529 }
36530 else if (bracecount
36531 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36532 cp_lexer_consume_token (parser->lexer);
36533 else
36534 {
36535 loc = cp_lexer_peek_token (parser->lexer)->location;
36536 error_at (loc, "not enough for loops to collapse");
36537 collapse_err = true;
36538 cp_parser_abort_tentative_parse (parser);
36539 declv = NULL_TREE;
36540 break;
36541 }
36542 }
36543
36544 if (declv)
36545 {
36546 cp_parser_parse_definitely (parser);
36547 nbraces += bracecount;
36548 }
36549 }
36550
36551 if (nbraces)
36552 if_p = NULL;
36553
36554 /* Note that we saved the original contents of this flag when we entered
36555 the structured block, and so we don't need to re-save it here. */
36556 parser->in_statement = IN_OMP_FOR;
36557
36558 /* Note that the grammar doesn't call for a structured block here,
36559 though the loop as a whole is a structured block. */
36560 if (orig_declv)
36561 {
36562 body = begin_omp_structured_block ();
36563 for (i = 0; i < count; i++)
36564 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
36565 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
36566 TREE_VEC_ELT (declv, i));
36567 }
36568 else
36569 body = push_stmt_list ();
36570 cp_parser_statement (parser, NULL_TREE, false, if_p);
36571 if (orig_declv)
36572 body = finish_omp_structured_block (body);
36573 else
36574 body = pop_stmt_list (body);
36575
36576 if (declv == NULL_TREE)
36577 ret = NULL_TREE;
36578 else
36579 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
36580 incrv, body, pre_body, &orig_inits, clauses);
36581
36582 while (nbraces)
36583 {
36584 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36585 {
36586 cp_lexer_consume_token (parser->lexer);
36587 nbraces--;
36588 }
36589 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36590 cp_lexer_consume_token (parser->lexer);
36591 else
36592 {
36593 if (!collapse_err)
36594 {
36595 error_at (cp_lexer_peek_token (parser->lexer)->location,
36596 "collapsed loops not perfectly nested");
36597 }
36598 collapse_err = true;
36599 cp_parser_statement_seq_opt (parser, NULL);
36600 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
36601 break;
36602 }
36603 }
36604
36605 while (!for_block->is_empty ())
36606 {
36607 tree t = for_block->pop ();
36608 if (TREE_CODE (t) == STATEMENT_LIST)
36609 add_stmt (pop_stmt_list (t));
36610 else
36611 add_stmt (t);
36612 }
36613 release_tree_vector (for_block);
36614
36615 return ret;
36616 }
36617
36618 /* Helper function for OpenMP parsing, split clauses and call
36619 finish_omp_clauses on each of the set of clauses afterwards. */
36620
36621 static void
36622 cp_omp_split_clauses (location_t loc, enum tree_code code,
36623 omp_clause_mask mask, tree clauses, tree *cclauses)
36624 {
36625 int i;
36626 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
36627 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
36628 if (cclauses[i])
36629 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
36630 }
36631
36632 /* OpenMP 4.0:
36633 #pragma omp simd simd-clause[optseq] new-line
36634 for-loop */
36635
36636 #define OMP_SIMD_CLAUSE_MASK \
36637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36647
36648 static tree
36649 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
36650 char *p_name, omp_clause_mask mask, tree *cclauses,
36651 bool *if_p)
36652 {
36653 tree clauses, sb, ret;
36654 unsigned int save;
36655 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36656
36657 strcat (p_name, " simd");
36658 mask |= OMP_SIMD_CLAUSE_MASK;
36659
36660 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36661 cclauses == NULL);
36662 if (cclauses)
36663 {
36664 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
36665 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
36666 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
36667 OMP_CLAUSE_ORDERED);
36668 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
36669 {
36670 error_at (OMP_CLAUSE_LOCATION (c),
36671 "%<ordered%> clause with parameter may not be specified "
36672 "on %qs construct", p_name);
36673 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
36674 }
36675 }
36676
36677 keep_next_level (true);
36678 sb = begin_omp_structured_block ();
36679 save = cp_parser_begin_omp_structured_block (parser);
36680
36681 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
36682
36683 cp_parser_end_omp_structured_block (parser, save);
36684 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36685
36686 return ret;
36687 }
36688
36689 /* OpenMP 2.5:
36690 #pragma omp for for-clause[optseq] new-line
36691 for-loop
36692
36693 OpenMP 4.0:
36694 #pragma omp for simd for-simd-clause[optseq] new-line
36695 for-loop */
36696
36697 #define OMP_FOR_CLAUSE_MASK \
36698 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
36704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
36705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36707
36708 static tree
36709 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
36710 char *p_name, omp_clause_mask mask, tree *cclauses,
36711 bool *if_p)
36712 {
36713 tree clauses, sb, ret;
36714 unsigned int save;
36715 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36716
36717 strcat (p_name, " for");
36718 mask |= OMP_FOR_CLAUSE_MASK;
36719 /* parallel for{, simd} disallows nowait clause, but for
36720 target {teams distribute ,}parallel for{, simd} it should be accepted. */
36721 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
36722 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36723 /* Composite distribute parallel for{, simd} disallows ordered clause. */
36724 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36725 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
36726
36727 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36728 {
36729 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36730 const char *p = IDENTIFIER_POINTER (id);
36731
36732 if (strcmp (p, "simd") == 0)
36733 {
36734 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36735 if (cclauses == NULL)
36736 cclauses = cclauses_buf;
36737
36738 cp_lexer_consume_token (parser->lexer);
36739 if (!flag_openmp) /* flag_openmp_simd */
36740 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36741 cclauses, if_p);
36742 sb = begin_omp_structured_block ();
36743 save = cp_parser_begin_omp_structured_block (parser);
36744 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36745 cclauses, if_p);
36746 cp_parser_end_omp_structured_block (parser, save);
36747 tree body = finish_omp_structured_block (sb);
36748 if (ret == NULL)
36749 return ret;
36750 ret = make_node (OMP_FOR);
36751 TREE_TYPE (ret) = void_type_node;
36752 OMP_FOR_BODY (ret) = body;
36753 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36754 SET_EXPR_LOCATION (ret, loc);
36755 add_stmt (ret);
36756 return ret;
36757 }
36758 }
36759 if (!flag_openmp) /* flag_openmp_simd */
36760 {
36761 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36762 return NULL_TREE;
36763 }
36764
36765 /* Composite distribute parallel for disallows linear clause. */
36766 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36767 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
36768
36769 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36770 cclauses == NULL);
36771 if (cclauses)
36772 {
36773 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
36774 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36775 }
36776
36777 keep_next_level (true);
36778 sb = begin_omp_structured_block ();
36779 save = cp_parser_begin_omp_structured_block (parser);
36780
36781 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
36782
36783 cp_parser_end_omp_structured_block (parser, save);
36784 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36785
36786 return ret;
36787 }
36788
36789 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
36790 omp_clause_mask, tree *, bool *);
36791
36792 /* OpenMP 2.5:
36793 # pragma omp master new-line
36794 structured-block */
36795
36796 static tree
36797 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
36798 char *p_name, omp_clause_mask mask, tree *cclauses,
36799 bool *if_p)
36800 {
36801 tree clauses, sb, ret;
36802 unsigned int save;
36803 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36804
36805 strcat (p_name, " master");
36806
36807 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36808 {
36809 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36810 const char *p = IDENTIFIER_POINTER (id);
36811
36812 if (strcmp (p, "taskloop") == 0)
36813 {
36814 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36815 if (cclauses == NULL)
36816 cclauses = cclauses_buf;
36817
36818 cp_lexer_consume_token (parser->lexer);
36819 if (!flag_openmp) /* flag_openmp_simd */
36820 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36821 cclauses, if_p);
36822 sb = begin_omp_structured_block ();
36823 save = cp_parser_begin_omp_structured_block (parser);
36824 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36825 cclauses, if_p);
36826 cp_parser_end_omp_structured_block (parser, save);
36827 tree body = finish_omp_structured_block (sb);
36828 if (ret == NULL)
36829 return ret;
36830 return c_finish_omp_master (loc, body);
36831 }
36832 }
36833 if (!flag_openmp) /* flag_openmp_simd */
36834 {
36835 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36836 return NULL_TREE;
36837 }
36838
36839 if (cclauses)
36840 {
36841 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36842 false);
36843 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
36844 }
36845 else
36846 cp_parser_require_pragma_eol (parser, pragma_tok);
36847
36848 return c_finish_omp_master (loc,
36849 cp_parser_omp_structured_block (parser, if_p));
36850 }
36851
36852 /* OpenMP 2.5:
36853 # pragma omp ordered new-line
36854 structured-block
36855
36856 OpenMP 4.5:
36857 # pragma omp ordered ordered-clauses new-line
36858 structured-block */
36859
36860 #define OMP_ORDERED_CLAUSE_MASK \
36861 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
36863
36864 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
36865 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
36866
36867 static bool
36868 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
36869 enum pragma_context context, bool *if_p)
36870 {
36871 location_t loc = pragma_tok->location;
36872
36873 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36874 {
36875 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36876 const char *p = IDENTIFIER_POINTER (id);
36877
36878 if (strcmp (p, "depend") == 0)
36879 {
36880 if (!flag_openmp) /* flag_openmp_simd */
36881 {
36882 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36883 return false;
36884 }
36885 if (context == pragma_stmt)
36886 {
36887 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
36888 "%<depend%> clause may only be used in compound "
36889 "statements");
36890 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36891 return false;
36892 }
36893 tree clauses
36894 = cp_parser_omp_all_clauses (parser,
36895 OMP_ORDERED_DEPEND_CLAUSE_MASK,
36896 "#pragma omp ordered", pragma_tok);
36897 c_finish_omp_ordered (loc, clauses, NULL_TREE);
36898 return false;
36899 }
36900 }
36901
36902 tree clauses
36903 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
36904 "#pragma omp ordered", pragma_tok);
36905
36906 if (!flag_openmp /* flag_openmp_simd */
36907 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
36908 return false;
36909
36910 c_finish_omp_ordered (loc, clauses,
36911 cp_parser_omp_structured_block (parser, if_p));
36912 return true;
36913 }
36914
36915 /* OpenMP 2.5:
36916
36917 section-scope:
36918 { section-sequence }
36919
36920 section-sequence:
36921 section-directive[opt] structured-block
36922 section-sequence section-directive structured-block */
36923
36924 static tree
36925 cp_parser_omp_sections_scope (cp_parser *parser)
36926 {
36927 tree stmt, substmt;
36928 bool error_suppress = false;
36929 cp_token *tok;
36930
36931 matching_braces braces;
36932 if (!braces.require_open (parser))
36933 return NULL_TREE;
36934
36935 stmt = push_stmt_list ();
36936
36937 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
36938 != PRAGMA_OMP_SECTION)
36939 {
36940 substmt = cp_parser_omp_structured_block (parser, NULL);
36941 substmt = build1 (OMP_SECTION, void_type_node, substmt);
36942 add_stmt (substmt);
36943 }
36944
36945 while (1)
36946 {
36947 tok = cp_lexer_peek_token (parser->lexer);
36948 if (tok->type == CPP_CLOSE_BRACE)
36949 break;
36950 if (tok->type == CPP_EOF)
36951 break;
36952
36953 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
36954 {
36955 cp_lexer_consume_token (parser->lexer);
36956 cp_parser_require_pragma_eol (parser, tok);
36957 error_suppress = false;
36958 }
36959 else if (!error_suppress)
36960 {
36961 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
36962 error_suppress = true;
36963 }
36964
36965 substmt = cp_parser_omp_structured_block (parser, NULL);
36966 substmt = build1 (OMP_SECTION, void_type_node, substmt);
36967 add_stmt (substmt);
36968 }
36969 braces.require_close (parser);
36970
36971 substmt = pop_stmt_list (stmt);
36972
36973 stmt = make_node (OMP_SECTIONS);
36974 TREE_TYPE (stmt) = void_type_node;
36975 OMP_SECTIONS_BODY (stmt) = substmt;
36976
36977 add_stmt (stmt);
36978 return stmt;
36979 }
36980
36981 /* OpenMP 2.5:
36982 # pragma omp sections sections-clause[optseq] newline
36983 sections-scope */
36984
36985 #define OMP_SECTIONS_CLAUSE_MASK \
36986 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36991
36992 static tree
36993 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
36994 char *p_name, omp_clause_mask mask, tree *cclauses)
36995 {
36996 tree clauses, ret;
36997 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36998
36999 strcat (p_name, " sections");
37000 mask |= OMP_SECTIONS_CLAUSE_MASK;
37001 if (cclauses)
37002 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37003
37004 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37005 cclauses == NULL);
37006 if (cclauses)
37007 {
37008 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37009 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37010 }
37011
37012 ret = cp_parser_omp_sections_scope (parser);
37013 if (ret)
37014 OMP_SECTIONS_CLAUSES (ret) = clauses;
37015
37016 return ret;
37017 }
37018
37019 /* OpenMP 2.5:
37020 # pragma omp parallel parallel-clause[optseq] new-line
37021 structured-block
37022 # pragma omp parallel for parallel-for-clause[optseq] new-line
37023 structured-block
37024 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37025 structured-block
37026
37027 OpenMP 4.0:
37028 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37029 structured-block */
37030
37031 #define OMP_PARALLEL_CLAUSE_MASK \
37032 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37038 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37039 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37041
37042 static tree
37043 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37044 char *p_name, omp_clause_mask mask, tree *cclauses,
37045 bool *if_p)
37046 {
37047 tree stmt, clauses, block;
37048 unsigned int save;
37049 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37050
37051 strcat (p_name, " parallel");
37052 mask |= OMP_PARALLEL_CLAUSE_MASK;
37053 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37054 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37055 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37056 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37057
37058 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37059 {
37060 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37061 if (cclauses == NULL)
37062 cclauses = cclauses_buf;
37063
37064 cp_lexer_consume_token (parser->lexer);
37065 if (!flag_openmp) /* flag_openmp_simd */
37066 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37067 if_p);
37068 block = begin_omp_parallel ();
37069 save = cp_parser_begin_omp_structured_block (parser);
37070 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37071 if_p);
37072 cp_parser_end_omp_structured_block (parser, save);
37073 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37074 block);
37075 if (ret == NULL_TREE)
37076 return ret;
37077 OMP_PARALLEL_COMBINED (stmt) = 1;
37078 return stmt;
37079 }
37080 /* When combined with distribute, parallel has to be followed by for.
37081 #pragma omp target parallel is allowed though. */
37082 else if (cclauses
37083 && (mask & (OMP_CLAUSE_MASK_1
37084 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37085 {
37086 error_at (loc, "expected %<for%> after %qs", p_name);
37087 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37088 return NULL_TREE;
37089 }
37090 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37091 {
37092 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37093 const char *p = IDENTIFIER_POINTER (id);
37094 if (strcmp (p, "master") == 0)
37095 {
37096 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37097 cclauses = cclauses_buf;
37098
37099 cp_lexer_consume_token (parser->lexer);
37100 block = begin_omp_parallel ();
37101 save = cp_parser_begin_omp_structured_block (parser);
37102 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37103 cclauses, if_p);
37104 cp_parser_end_omp_structured_block (parser, save);
37105 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37106 block);
37107 OMP_PARALLEL_COMBINED (stmt) = 1;
37108 if (ret == NULL_TREE)
37109 return ret;
37110 return stmt;
37111 }
37112 else if (!flag_openmp) /* flag_openmp_simd */
37113 {
37114 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37115 return NULL_TREE;
37116 }
37117 else if (strcmp (p, "sections") == 0)
37118 {
37119 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37120 cclauses = cclauses_buf;
37121
37122 cp_lexer_consume_token (parser->lexer);
37123 block = begin_omp_parallel ();
37124 save = cp_parser_begin_omp_structured_block (parser);
37125 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37126 cp_parser_end_omp_structured_block (parser, save);
37127 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37128 block);
37129 OMP_PARALLEL_COMBINED (stmt) = 1;
37130 return stmt;
37131 }
37132 }
37133 else if (!flag_openmp) /* flag_openmp_simd */
37134 {
37135 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37136 return NULL_TREE;
37137 }
37138
37139 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37140 cclauses == NULL);
37141 if (cclauses)
37142 {
37143 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37144 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37145 }
37146
37147 block = begin_omp_parallel ();
37148 save = cp_parser_begin_omp_structured_block (parser);
37149 cp_parser_statement (parser, NULL_TREE, false, if_p);
37150 cp_parser_end_omp_structured_block (parser, save);
37151 stmt = finish_omp_parallel (clauses, block);
37152 return stmt;
37153 }
37154
37155 /* OpenMP 2.5:
37156 # pragma omp single single-clause[optseq] new-line
37157 structured-block */
37158
37159 #define OMP_SINGLE_CLAUSE_MASK \
37160 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37164
37165 static tree
37166 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37167 {
37168 tree stmt = make_node (OMP_SINGLE);
37169 TREE_TYPE (stmt) = void_type_node;
37170 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37171
37172 OMP_SINGLE_CLAUSES (stmt)
37173 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37174 "#pragma omp single", pragma_tok);
37175 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37176
37177 return add_stmt (stmt);
37178 }
37179
37180 /* OpenMP 3.0:
37181 # pragma omp task task-clause[optseq] new-line
37182 structured-block */
37183
37184 #define OMP_TASK_CLAUSE_MASK \
37185 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37196
37197 static tree
37198 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37199 {
37200 tree clauses, block;
37201 unsigned int save;
37202
37203 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37204 "#pragma omp task", pragma_tok);
37205 block = begin_omp_task ();
37206 save = cp_parser_begin_omp_structured_block (parser);
37207 cp_parser_statement (parser, NULL_TREE, false, if_p);
37208 cp_parser_end_omp_structured_block (parser, save);
37209 return finish_omp_task (clauses, block);
37210 }
37211
37212 /* OpenMP 3.0:
37213 # pragma omp taskwait new-line
37214
37215 OpenMP 5.0:
37216 # pragma omp taskwait taskwait-clause[opt] new-line */
37217
37218 #define OMP_TASKWAIT_CLAUSE_MASK \
37219 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37220
37221 static void
37222 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37223 {
37224 tree clauses
37225 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37226 "#pragma omp taskwait", pragma_tok);
37227
37228 if (clauses)
37229 {
37230 tree stmt = make_node (OMP_TASK);
37231 TREE_TYPE (stmt) = void_node;
37232 OMP_TASK_CLAUSES (stmt) = clauses;
37233 OMP_TASK_BODY (stmt) = NULL_TREE;
37234 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37235 add_stmt (stmt);
37236 }
37237 else
37238 finish_omp_taskwait ();
37239 }
37240
37241 /* OpenMP 3.1:
37242 # pragma omp taskyield new-line */
37243
37244 static void
37245 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37246 {
37247 cp_parser_require_pragma_eol (parser, pragma_tok);
37248 finish_omp_taskyield ();
37249 }
37250
37251 /* OpenMP 4.0:
37252 # pragma omp taskgroup new-line
37253 structured-block
37254
37255 OpenMP 5.0:
37256 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37257
37258 #define OMP_TASKGROUP_CLAUSE_MASK \
37259 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37260
37261 static tree
37262 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37263 {
37264 tree clauses
37265 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37266 "#pragma omp taskgroup", pragma_tok);
37267 return c_finish_omp_taskgroup (input_location,
37268 cp_parser_omp_structured_block (parser,
37269 if_p),
37270 clauses);
37271 }
37272
37273
37274 /* OpenMP 2.5:
37275 # pragma omp threadprivate (variable-list) */
37276
37277 static void
37278 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37279 {
37280 tree vars;
37281
37282 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37283 cp_parser_require_pragma_eol (parser, pragma_tok);
37284
37285 finish_omp_threadprivate (vars);
37286 }
37287
37288 /* OpenMP 4.0:
37289 # pragma omp cancel cancel-clause[optseq] new-line */
37290
37291 #define OMP_CANCEL_CLAUSE_MASK \
37292 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37297
37298 static void
37299 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37300 {
37301 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37302 "#pragma omp cancel", pragma_tok);
37303 finish_omp_cancel (clauses);
37304 }
37305
37306 /* OpenMP 4.0:
37307 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37308
37309 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37310 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37314
37315 static void
37316 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37317 enum pragma_context context)
37318 {
37319 tree clauses;
37320 bool point_seen = false;
37321
37322 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37323 {
37324 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37325 const char *p = IDENTIFIER_POINTER (id);
37326
37327 if (strcmp (p, "point") == 0)
37328 {
37329 cp_lexer_consume_token (parser->lexer);
37330 point_seen = true;
37331 }
37332 }
37333 if (!point_seen)
37334 {
37335 cp_parser_error (parser, "expected %<point%>");
37336 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37337 return;
37338 }
37339
37340 if (context != pragma_compound)
37341 {
37342 if (context == pragma_stmt)
37343 error_at (pragma_tok->location,
37344 "%<#pragma %s%> may only be used in compound statements",
37345 "omp cancellation point");
37346 else
37347 cp_parser_error (parser, "expected declaration specifiers");
37348 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37349 return;
37350 }
37351
37352 clauses = cp_parser_omp_all_clauses (parser,
37353 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37354 "#pragma omp cancellation point",
37355 pragma_tok);
37356 finish_omp_cancellation_point (clauses);
37357 }
37358
37359 /* OpenMP 4.0:
37360 #pragma omp distribute distribute-clause[optseq] new-line
37361 for-loop */
37362
37363 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37364 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37369
37370 static tree
37371 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37372 char *p_name, omp_clause_mask mask, tree *cclauses,
37373 bool *if_p)
37374 {
37375 tree clauses, sb, ret;
37376 unsigned int save;
37377 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37378
37379 strcat (p_name, " distribute");
37380 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37381
37382 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37383 {
37384 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37385 const char *p = IDENTIFIER_POINTER (id);
37386 bool simd = false;
37387 bool parallel = false;
37388
37389 if (strcmp (p, "simd") == 0)
37390 simd = true;
37391 else
37392 parallel = strcmp (p, "parallel") == 0;
37393 if (parallel || simd)
37394 {
37395 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37396 if (cclauses == NULL)
37397 cclauses = cclauses_buf;
37398 cp_lexer_consume_token (parser->lexer);
37399 if (!flag_openmp) /* flag_openmp_simd */
37400 {
37401 if (simd)
37402 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37403 cclauses, if_p);
37404 else
37405 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37406 cclauses, if_p);
37407 }
37408 sb = begin_omp_structured_block ();
37409 save = cp_parser_begin_omp_structured_block (parser);
37410 if (simd)
37411 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37412 cclauses, if_p);
37413 else
37414 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37415 cclauses, if_p);
37416 cp_parser_end_omp_structured_block (parser, save);
37417 tree body = finish_omp_structured_block (sb);
37418 if (ret == NULL)
37419 return ret;
37420 ret = make_node (OMP_DISTRIBUTE);
37421 TREE_TYPE (ret) = void_type_node;
37422 OMP_FOR_BODY (ret) = body;
37423 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37424 SET_EXPR_LOCATION (ret, loc);
37425 add_stmt (ret);
37426 return ret;
37427 }
37428 }
37429 if (!flag_openmp) /* flag_openmp_simd */
37430 {
37431 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37432 return NULL_TREE;
37433 }
37434
37435 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37436 cclauses == NULL);
37437 if (cclauses)
37438 {
37439 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37440 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37441 }
37442
37443 keep_next_level (true);
37444 sb = begin_omp_structured_block ();
37445 save = cp_parser_begin_omp_structured_block (parser);
37446
37447 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37448
37449 cp_parser_end_omp_structured_block (parser, save);
37450 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37451
37452 return ret;
37453 }
37454
37455 /* OpenMP 4.0:
37456 # pragma omp teams teams-clause[optseq] new-line
37457 structured-block */
37458
37459 #define OMP_TEAMS_CLAUSE_MASK \
37460 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37467
37468 static tree
37469 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37470 char *p_name, omp_clause_mask mask, tree *cclauses,
37471 bool *if_p)
37472 {
37473 tree clauses, sb, ret;
37474 unsigned int save;
37475 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37476
37477 strcat (p_name, " teams");
37478 mask |= OMP_TEAMS_CLAUSE_MASK;
37479
37480 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37481 {
37482 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37483 const char *p = IDENTIFIER_POINTER (id);
37484 if (strcmp (p, "distribute") == 0)
37485 {
37486 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37487 if (cclauses == NULL)
37488 cclauses = cclauses_buf;
37489
37490 cp_lexer_consume_token (parser->lexer);
37491 if (!flag_openmp) /* flag_openmp_simd */
37492 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37493 cclauses, if_p);
37494 keep_next_level (true);
37495 sb = begin_omp_structured_block ();
37496 save = cp_parser_begin_omp_structured_block (parser);
37497 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37498 cclauses, if_p);
37499 cp_parser_end_omp_structured_block (parser, save);
37500 tree body = finish_omp_structured_block (sb);
37501 if (ret == NULL)
37502 return ret;
37503 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37504 ret = make_node (OMP_TEAMS);
37505 TREE_TYPE (ret) = void_type_node;
37506 OMP_TEAMS_CLAUSES (ret) = clauses;
37507 OMP_TEAMS_BODY (ret) = body;
37508 OMP_TEAMS_COMBINED (ret) = 1;
37509 SET_EXPR_LOCATION (ret, loc);
37510 return add_stmt (ret);
37511 }
37512 }
37513 if (!flag_openmp) /* flag_openmp_simd */
37514 {
37515 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37516 return NULL_TREE;
37517 }
37518
37519 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37520 cclauses == NULL);
37521 if (cclauses)
37522 {
37523 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
37524 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37525 }
37526
37527 tree stmt = make_node (OMP_TEAMS);
37528 TREE_TYPE (stmt) = void_type_node;
37529 OMP_TEAMS_CLAUSES (stmt) = clauses;
37530 keep_next_level (true);
37531 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37532 SET_EXPR_LOCATION (stmt, loc);
37533
37534 return add_stmt (stmt);
37535 }
37536
37537 /* OpenMP 4.0:
37538 # pragma omp target data target-data-clause[optseq] new-line
37539 structured-block */
37540
37541 #define OMP_TARGET_DATA_CLAUSE_MASK \
37542 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37546
37547 static tree
37548 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37549 {
37550 tree clauses
37551 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
37552 "#pragma omp target data", pragma_tok);
37553 int map_seen = 0;
37554 for (tree *pc = &clauses; *pc;)
37555 {
37556 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37557 switch (OMP_CLAUSE_MAP_KIND (*pc))
37558 {
37559 case GOMP_MAP_TO:
37560 case GOMP_MAP_ALWAYS_TO:
37561 case GOMP_MAP_FROM:
37562 case GOMP_MAP_ALWAYS_FROM:
37563 case GOMP_MAP_TOFROM:
37564 case GOMP_MAP_ALWAYS_TOFROM:
37565 case GOMP_MAP_ALLOC:
37566 map_seen = 3;
37567 break;
37568 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37569 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37570 case GOMP_MAP_ALWAYS_POINTER:
37571 break;
37572 default:
37573 map_seen |= 1;
37574 error_at (OMP_CLAUSE_LOCATION (*pc),
37575 "%<#pragma omp target data%> with map-type other "
37576 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37577 "on %<map%> clause");
37578 *pc = OMP_CLAUSE_CHAIN (*pc);
37579 continue;
37580 }
37581 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
37582 map_seen = 3;
37583 pc = &OMP_CLAUSE_CHAIN (*pc);
37584 }
37585
37586 if (map_seen != 3)
37587 {
37588 if (map_seen == 0)
37589 error_at (pragma_tok->location,
37590 "%<#pragma omp target data%> must contain at least "
37591 "one %<map%> or %<use_device_ptr%> clause");
37592 return NULL_TREE;
37593 }
37594
37595 tree stmt = make_node (OMP_TARGET_DATA);
37596 TREE_TYPE (stmt) = void_type_node;
37597 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
37598
37599 keep_next_level (true);
37600 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37601
37602 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37603 return add_stmt (stmt);
37604 }
37605
37606 /* OpenMP 4.5:
37607 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37608 structured-block */
37609
37610 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37611 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37616
37617 static tree
37618 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
37619 enum pragma_context context)
37620 {
37621 bool data_seen = false;
37622 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37623 {
37624 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37625 const char *p = IDENTIFIER_POINTER (id);
37626
37627 if (strcmp (p, "data") == 0)
37628 {
37629 cp_lexer_consume_token (parser->lexer);
37630 data_seen = true;
37631 }
37632 }
37633 if (!data_seen)
37634 {
37635 cp_parser_error (parser, "expected %<data%>");
37636 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37637 return NULL_TREE;
37638 }
37639
37640 if (context == pragma_stmt)
37641 {
37642 error_at (pragma_tok->location,
37643 "%<#pragma %s%> may only be used in compound statements",
37644 "omp target enter data");
37645 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37646 return NULL_TREE;
37647 }
37648
37649 tree clauses
37650 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
37651 "#pragma omp target enter data", pragma_tok);
37652 int map_seen = 0;
37653 for (tree *pc = &clauses; *pc;)
37654 {
37655 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37656 switch (OMP_CLAUSE_MAP_KIND (*pc))
37657 {
37658 case GOMP_MAP_TO:
37659 case GOMP_MAP_ALWAYS_TO:
37660 case GOMP_MAP_ALLOC:
37661 map_seen = 3;
37662 break;
37663 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37664 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37665 case GOMP_MAP_ALWAYS_POINTER:
37666 break;
37667 default:
37668 map_seen |= 1;
37669 error_at (OMP_CLAUSE_LOCATION (*pc),
37670 "%<#pragma omp target enter data%> with map-type other "
37671 "than %<to%> or %<alloc%> on %<map%> clause");
37672 *pc = OMP_CLAUSE_CHAIN (*pc);
37673 continue;
37674 }
37675 pc = &OMP_CLAUSE_CHAIN (*pc);
37676 }
37677
37678 if (map_seen != 3)
37679 {
37680 if (map_seen == 0)
37681 error_at (pragma_tok->location,
37682 "%<#pragma omp target enter data%> must contain at least "
37683 "one %<map%> clause");
37684 return NULL_TREE;
37685 }
37686
37687 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
37688 TREE_TYPE (stmt) = void_type_node;
37689 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
37690 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37691 return add_stmt (stmt);
37692 }
37693
37694 /* OpenMP 4.5:
37695 # pragma omp target exit data target-enter-data-clause[optseq] new-line
37696 structured-block */
37697
37698 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
37699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37704
37705 static tree
37706 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
37707 enum pragma_context context)
37708 {
37709 bool data_seen = false;
37710 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37711 {
37712 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37713 const char *p = IDENTIFIER_POINTER (id);
37714
37715 if (strcmp (p, "data") == 0)
37716 {
37717 cp_lexer_consume_token (parser->lexer);
37718 data_seen = true;
37719 }
37720 }
37721 if (!data_seen)
37722 {
37723 cp_parser_error (parser, "expected %<data%>");
37724 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37725 return NULL_TREE;
37726 }
37727
37728 if (context == pragma_stmt)
37729 {
37730 error_at (pragma_tok->location,
37731 "%<#pragma %s%> may only be used in compound statements",
37732 "omp target exit data");
37733 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37734 return NULL_TREE;
37735 }
37736
37737 tree clauses
37738 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
37739 "#pragma omp target exit data", pragma_tok);
37740 int map_seen = 0;
37741 for (tree *pc = &clauses; *pc;)
37742 {
37743 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37744 switch (OMP_CLAUSE_MAP_KIND (*pc))
37745 {
37746 case GOMP_MAP_FROM:
37747 case GOMP_MAP_ALWAYS_FROM:
37748 case GOMP_MAP_RELEASE:
37749 case GOMP_MAP_DELETE:
37750 map_seen = 3;
37751 break;
37752 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37753 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37754 case GOMP_MAP_ALWAYS_POINTER:
37755 break;
37756 default:
37757 map_seen |= 1;
37758 error_at (OMP_CLAUSE_LOCATION (*pc),
37759 "%<#pragma omp target exit data%> with map-type other "
37760 "than %<from%>, %<release%> or %<delete%> on %<map%>"
37761 " clause");
37762 *pc = OMP_CLAUSE_CHAIN (*pc);
37763 continue;
37764 }
37765 pc = &OMP_CLAUSE_CHAIN (*pc);
37766 }
37767
37768 if (map_seen != 3)
37769 {
37770 if (map_seen == 0)
37771 error_at (pragma_tok->location,
37772 "%<#pragma omp target exit data%> must contain at least "
37773 "one %<map%> clause");
37774 return NULL_TREE;
37775 }
37776
37777 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
37778 TREE_TYPE (stmt) = void_type_node;
37779 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
37780 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37781 return add_stmt (stmt);
37782 }
37783
37784 /* OpenMP 4.0:
37785 # pragma omp target update target-update-clause[optseq] new-line */
37786
37787 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
37788 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
37789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37794
37795 static bool
37796 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
37797 enum pragma_context context)
37798 {
37799 if (context == pragma_stmt)
37800 {
37801 error_at (pragma_tok->location,
37802 "%<#pragma %s%> may only be used in compound statements",
37803 "omp target update");
37804 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37805 return false;
37806 }
37807
37808 tree clauses
37809 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
37810 "#pragma omp target update", pragma_tok);
37811 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
37812 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
37813 {
37814 error_at (pragma_tok->location,
37815 "%<#pragma omp target update%> must contain at least one "
37816 "%<from%> or %<to%> clauses");
37817 return false;
37818 }
37819
37820 tree stmt = make_node (OMP_TARGET_UPDATE);
37821 TREE_TYPE (stmt) = void_type_node;
37822 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
37823 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37824 add_stmt (stmt);
37825 return false;
37826 }
37827
37828 /* OpenMP 4.0:
37829 # pragma omp target target-clause[optseq] new-line
37830 structured-block */
37831
37832 #define OMP_TARGET_CLAUSE_MASK \
37833 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
37841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
37842
37843 static bool
37844 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
37845 enum pragma_context context, bool *if_p)
37846 {
37847 tree *pc = NULL, stmt;
37848
37849 if (flag_openmp)
37850 omp_requires_mask
37851 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
37852
37853 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37854 {
37855 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37856 const char *p = IDENTIFIER_POINTER (id);
37857 enum tree_code ccode = ERROR_MARK;
37858
37859 if (strcmp (p, "teams") == 0)
37860 ccode = OMP_TEAMS;
37861 else if (strcmp (p, "parallel") == 0)
37862 ccode = OMP_PARALLEL;
37863 else if (strcmp (p, "simd") == 0)
37864 ccode = OMP_SIMD;
37865 if (ccode != ERROR_MARK)
37866 {
37867 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
37868 char p_name[sizeof ("#pragma omp target teams distribute "
37869 "parallel for simd")];
37870
37871 cp_lexer_consume_token (parser->lexer);
37872 strcpy (p_name, "#pragma omp target");
37873 if (!flag_openmp) /* flag_openmp_simd */
37874 {
37875 tree stmt;
37876 switch (ccode)
37877 {
37878 case OMP_TEAMS:
37879 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
37880 OMP_TARGET_CLAUSE_MASK,
37881 cclauses, if_p);
37882 break;
37883 case OMP_PARALLEL:
37884 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
37885 OMP_TARGET_CLAUSE_MASK,
37886 cclauses, if_p);
37887 break;
37888 case OMP_SIMD:
37889 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
37890 OMP_TARGET_CLAUSE_MASK,
37891 cclauses, if_p);
37892 break;
37893 default:
37894 gcc_unreachable ();
37895 }
37896 return stmt != NULL_TREE;
37897 }
37898 keep_next_level (true);
37899 tree sb = begin_omp_structured_block (), ret;
37900 unsigned save = cp_parser_begin_omp_structured_block (parser);
37901 switch (ccode)
37902 {
37903 case OMP_TEAMS:
37904 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
37905 OMP_TARGET_CLAUSE_MASK, cclauses,
37906 if_p);
37907 break;
37908 case OMP_PARALLEL:
37909 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
37910 OMP_TARGET_CLAUSE_MASK, cclauses,
37911 if_p);
37912 break;
37913 case OMP_SIMD:
37914 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
37915 OMP_TARGET_CLAUSE_MASK, cclauses,
37916 if_p);
37917 break;
37918 default:
37919 gcc_unreachable ();
37920 }
37921 cp_parser_end_omp_structured_block (parser, save);
37922 tree body = finish_omp_structured_block (sb);
37923 if (ret == NULL_TREE)
37924 return false;
37925 if (ccode == OMP_TEAMS && !processing_template_decl)
37926 {
37927 /* For combined target teams, ensure the num_teams and
37928 thread_limit clause expressions are evaluated on the host,
37929 before entering the target construct. */
37930 tree c;
37931 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37932 c; c = OMP_CLAUSE_CHAIN (c))
37933 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
37934 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
37935 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
37936 {
37937 tree expr = OMP_CLAUSE_OPERAND (c, 0);
37938 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
37939 if (expr == error_mark_node)
37940 continue;
37941 tree tmp = TARGET_EXPR_SLOT (expr);
37942 add_stmt (expr);
37943 OMP_CLAUSE_OPERAND (c, 0) = expr;
37944 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
37945 OMP_CLAUSE_FIRSTPRIVATE);
37946 OMP_CLAUSE_DECL (tc) = tmp;
37947 OMP_CLAUSE_CHAIN (tc)
37948 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
37949 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
37950 }
37951 }
37952 tree stmt = make_node (OMP_TARGET);
37953 TREE_TYPE (stmt) = void_type_node;
37954 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
37955 OMP_TARGET_BODY (stmt) = body;
37956 OMP_TARGET_COMBINED (stmt) = 1;
37957 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37958 add_stmt (stmt);
37959 pc = &OMP_TARGET_CLAUSES (stmt);
37960 goto check_clauses;
37961 }
37962 else if (!flag_openmp) /* flag_openmp_simd */
37963 {
37964 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37965 return false;
37966 }
37967 else if (strcmp (p, "data") == 0)
37968 {
37969 cp_lexer_consume_token (parser->lexer);
37970 cp_parser_omp_target_data (parser, pragma_tok, if_p);
37971 return true;
37972 }
37973 else if (strcmp (p, "enter") == 0)
37974 {
37975 cp_lexer_consume_token (parser->lexer);
37976 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
37977 return false;
37978 }
37979 else if (strcmp (p, "exit") == 0)
37980 {
37981 cp_lexer_consume_token (parser->lexer);
37982 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
37983 return false;
37984 }
37985 else if (strcmp (p, "update") == 0)
37986 {
37987 cp_lexer_consume_token (parser->lexer);
37988 return cp_parser_omp_target_update (parser, pragma_tok, context);
37989 }
37990 }
37991 if (!flag_openmp) /* flag_openmp_simd */
37992 {
37993 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37994 return false;
37995 }
37996
37997 stmt = make_node (OMP_TARGET);
37998 TREE_TYPE (stmt) = void_type_node;
37999
38000 OMP_TARGET_CLAUSES (stmt)
38001 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38002 "#pragma omp target", pragma_tok);
38003 pc = &OMP_TARGET_CLAUSES (stmt);
38004 keep_next_level (true);
38005 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38006
38007 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38008 add_stmt (stmt);
38009
38010 check_clauses:
38011 while (*pc)
38012 {
38013 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38014 switch (OMP_CLAUSE_MAP_KIND (*pc))
38015 {
38016 case GOMP_MAP_TO:
38017 case GOMP_MAP_ALWAYS_TO:
38018 case GOMP_MAP_FROM:
38019 case GOMP_MAP_ALWAYS_FROM:
38020 case GOMP_MAP_TOFROM:
38021 case GOMP_MAP_ALWAYS_TOFROM:
38022 case GOMP_MAP_ALLOC:
38023 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38024 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38025 case GOMP_MAP_ALWAYS_POINTER:
38026 break;
38027 default:
38028 error_at (OMP_CLAUSE_LOCATION (*pc),
38029 "%<#pragma omp target%> with map-type other "
38030 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38031 "on %<map%> clause");
38032 *pc = OMP_CLAUSE_CHAIN (*pc);
38033 continue;
38034 }
38035 pc = &OMP_CLAUSE_CHAIN (*pc);
38036 }
38037 return true;
38038 }
38039
38040 /* OpenACC 2.0:
38041 # pragma acc cache (variable-list) new-line
38042 */
38043
38044 static tree
38045 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38046 {
38047 tree stmt, clauses;
38048
38049 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38050 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38051
38052 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38053
38054 stmt = make_node (OACC_CACHE);
38055 TREE_TYPE (stmt) = void_type_node;
38056 OACC_CACHE_CLAUSES (stmt) = clauses;
38057 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38058 add_stmt (stmt);
38059
38060 return stmt;
38061 }
38062
38063 /* OpenACC 2.0:
38064 # pragma acc data oacc-data-clause[optseq] new-line
38065 structured-block */
38066
38067 #define OACC_DATA_CLAUSE_MASK \
38068 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38072 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38075
38076 static tree
38077 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38078 {
38079 tree stmt, clauses, block;
38080 unsigned int save;
38081
38082 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38083 "#pragma acc data", pragma_tok);
38084
38085 block = begin_omp_parallel ();
38086 save = cp_parser_begin_omp_structured_block (parser);
38087 cp_parser_statement (parser, NULL_TREE, false, if_p);
38088 cp_parser_end_omp_structured_block (parser, save);
38089 stmt = finish_oacc_data (clauses, block);
38090 return stmt;
38091 }
38092
38093 /* OpenACC 2.0:
38094 # pragma acc host_data <clauses> new-line
38095 structured-block */
38096
38097 #define OACC_HOST_DATA_CLAUSE_MASK \
38098 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38099
38100 static tree
38101 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38102 {
38103 tree stmt, clauses, block;
38104 unsigned int save;
38105
38106 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38107 "#pragma acc host_data", pragma_tok);
38108
38109 block = begin_omp_parallel ();
38110 save = cp_parser_begin_omp_structured_block (parser);
38111 cp_parser_statement (parser, NULL_TREE, false, if_p);
38112 cp_parser_end_omp_structured_block (parser, save);
38113 stmt = finish_oacc_host_data (clauses, block);
38114 return stmt;
38115 }
38116
38117 /* OpenACC 2.0:
38118 # pragma acc declare oacc-data-clause[optseq] new-line
38119 */
38120
38121 #define OACC_DECLARE_CLAUSE_MASK \
38122 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38130
38131 static tree
38132 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38133 {
38134 tree clauses, stmt;
38135 bool error = false;
38136
38137 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38138 "#pragma acc declare", pragma_tok, true);
38139
38140
38141 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38142 {
38143 error_at (pragma_tok->location,
38144 "no valid clauses specified in %<#pragma acc declare%>");
38145 return NULL_TREE;
38146 }
38147
38148 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38149 {
38150 location_t loc = OMP_CLAUSE_LOCATION (t);
38151 tree decl = OMP_CLAUSE_DECL (t);
38152 if (!DECL_P (decl))
38153 {
38154 error_at (loc, "array section in %<#pragma acc declare%>");
38155 error = true;
38156 continue;
38157 }
38158 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38159 switch (OMP_CLAUSE_MAP_KIND (t))
38160 {
38161 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38162 case GOMP_MAP_ALLOC:
38163 case GOMP_MAP_TO:
38164 case GOMP_MAP_FORCE_DEVICEPTR:
38165 case GOMP_MAP_DEVICE_RESIDENT:
38166 break;
38167
38168 case GOMP_MAP_LINK:
38169 if (!global_bindings_p ()
38170 && (TREE_STATIC (decl)
38171 || !DECL_EXTERNAL (decl)))
38172 {
38173 error_at (loc,
38174 "%qD must be a global variable in "
38175 "%<#pragma acc declare link%>",
38176 decl);
38177 error = true;
38178 continue;
38179 }
38180 break;
38181
38182 default:
38183 if (global_bindings_p ())
38184 {
38185 error_at (loc, "invalid OpenACC clause at file scope");
38186 error = true;
38187 continue;
38188 }
38189 if (DECL_EXTERNAL (decl))
38190 {
38191 error_at (loc,
38192 "invalid use of %<extern%> variable %qD "
38193 "in %<#pragma acc declare%>", decl);
38194 error = true;
38195 continue;
38196 }
38197 else if (TREE_PUBLIC (decl))
38198 {
38199 error_at (loc,
38200 "invalid use of %<global%> variable %qD "
38201 "in %<#pragma acc declare%>", decl);
38202 error = true;
38203 continue;
38204 }
38205 break;
38206 }
38207
38208 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38209 || lookup_attribute ("omp declare target link",
38210 DECL_ATTRIBUTES (decl)))
38211 {
38212 error_at (loc, "variable %qD used more than once with "
38213 "%<#pragma acc declare%>", decl);
38214 error = true;
38215 continue;
38216 }
38217
38218 if (!error)
38219 {
38220 tree id;
38221
38222 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38223 id = get_identifier ("omp declare target link");
38224 else
38225 id = get_identifier ("omp declare target");
38226
38227 DECL_ATTRIBUTES (decl)
38228 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38229 if (global_bindings_p ())
38230 {
38231 symtab_node *node = symtab_node::get (decl);
38232 if (node != NULL)
38233 {
38234 node->offloadable = 1;
38235 if (ENABLE_OFFLOADING)
38236 {
38237 g->have_offload = true;
38238 if (is_a <varpool_node *> (node))
38239 vec_safe_push (offload_vars, decl);
38240 }
38241 }
38242 }
38243 }
38244 }
38245
38246 if (error || global_bindings_p ())
38247 return NULL_TREE;
38248
38249 stmt = make_node (OACC_DECLARE);
38250 TREE_TYPE (stmt) = void_type_node;
38251 OACC_DECLARE_CLAUSES (stmt) = clauses;
38252 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38253
38254 add_stmt (stmt);
38255
38256 return NULL_TREE;
38257 }
38258
38259 /* OpenACC 2.0:
38260 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38261
38262 or
38263
38264 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38265
38266 LOC is the location of the #pragma token.
38267 */
38268
38269 #define OACC_ENTER_DATA_CLAUSE_MASK \
38270 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38275
38276 #define OACC_EXIT_DATA_CLAUSE_MASK \
38277 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38283
38284 static tree
38285 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38286 bool enter)
38287 {
38288 location_t loc = pragma_tok->location;
38289 tree stmt, clauses;
38290 const char *p = "";
38291
38292 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38293 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38294
38295 if (strcmp (p, "data") != 0)
38296 {
38297 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38298 enter ? "enter" : "exit");
38299 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38300 return NULL_TREE;
38301 }
38302
38303 cp_lexer_consume_token (parser->lexer);
38304
38305 if (enter)
38306 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38307 "#pragma acc enter data", pragma_tok);
38308 else
38309 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38310 "#pragma acc exit data", pragma_tok);
38311
38312 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38313 {
38314 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38315 enter ? "enter" : "exit");
38316 return NULL_TREE;
38317 }
38318
38319 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38320 TREE_TYPE (stmt) = void_type_node;
38321 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38322 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38323 add_stmt (stmt);
38324 return stmt;
38325 }
38326
38327 /* OpenACC 2.0:
38328 # pragma acc loop oacc-loop-clause[optseq] new-line
38329 structured-block */
38330
38331 #define OACC_LOOP_CLAUSE_MASK \
38332 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38342
38343 static tree
38344 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38345 omp_clause_mask mask, tree *cclauses, bool *if_p)
38346 {
38347 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38348
38349 strcat (p_name, " loop");
38350 mask |= OACC_LOOP_CLAUSE_MASK;
38351
38352 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38353 cclauses == NULL);
38354 if (cclauses)
38355 {
38356 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38357 if (*cclauses)
38358 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38359 if (clauses)
38360 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38361 }
38362
38363 tree block = begin_omp_structured_block ();
38364 int save = cp_parser_begin_omp_structured_block (parser);
38365 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38366 cp_parser_end_omp_structured_block (parser, save);
38367 add_stmt (finish_omp_structured_block (block));
38368
38369 return stmt;
38370 }
38371
38372 /* OpenACC 2.0:
38373 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38374 structured-block
38375
38376 or
38377
38378 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38379 structured-block
38380 */
38381
38382 #define OACC_KERNELS_CLAUSE_MASK \
38383 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38396
38397 #define OACC_PARALLEL_CLAUSE_MASK \
38398 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38414
38415 static tree
38416 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38417 char *p_name, bool *if_p)
38418 {
38419 omp_clause_mask mask;
38420 enum tree_code code;
38421 switch (cp_parser_pragma_kind (pragma_tok))
38422 {
38423 case PRAGMA_OACC_KERNELS:
38424 strcat (p_name, " kernels");
38425 mask = OACC_KERNELS_CLAUSE_MASK;
38426 code = OACC_KERNELS;
38427 break;
38428 case PRAGMA_OACC_PARALLEL:
38429 strcat (p_name, " parallel");
38430 mask = OACC_PARALLEL_CLAUSE_MASK;
38431 code = OACC_PARALLEL;
38432 break;
38433 default:
38434 gcc_unreachable ();
38435 }
38436
38437 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38438 {
38439 const char *p
38440 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38441 if (strcmp (p, "loop") == 0)
38442 {
38443 cp_lexer_consume_token (parser->lexer);
38444 tree block = begin_omp_parallel ();
38445 tree clauses;
38446 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
38447 if_p);
38448 return finish_omp_construct (code, block, clauses);
38449 }
38450 }
38451
38452 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38453
38454 tree block = begin_omp_parallel ();
38455 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38456 cp_parser_statement (parser, NULL_TREE, false, if_p);
38457 cp_parser_end_omp_structured_block (parser, save);
38458 return finish_omp_construct (code, block, clauses);
38459 }
38460
38461 /* OpenACC 2.0:
38462 # pragma acc update oacc-update-clause[optseq] new-line
38463 */
38464
38465 #define OACC_UPDATE_CLAUSE_MASK \
38466 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38472
38473 static tree
38474 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38475 {
38476 tree stmt, clauses;
38477
38478 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
38479 "#pragma acc update", pragma_tok);
38480
38481 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38482 {
38483 error_at (pragma_tok->location,
38484 "%<#pragma acc update%> must contain at least one "
38485 "%<device%> or %<host%> or %<self%> clause");
38486 return NULL_TREE;
38487 }
38488
38489 stmt = make_node (OACC_UPDATE);
38490 TREE_TYPE (stmt) = void_type_node;
38491 OACC_UPDATE_CLAUSES (stmt) = clauses;
38492 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38493 add_stmt (stmt);
38494 return stmt;
38495 }
38496
38497 /* OpenACC 2.0:
38498 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38499
38500 LOC is the location of the #pragma token.
38501 */
38502
38503 #define OACC_WAIT_CLAUSE_MASK \
38504 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38505
38506 static tree
38507 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
38508 {
38509 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
38510 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38511
38512 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38513 list = cp_parser_oacc_wait_list (parser, loc, list);
38514
38515 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
38516 "#pragma acc wait", pragma_tok);
38517
38518 stmt = c_finish_oacc_wait (loc, list, clauses);
38519 stmt = finish_expr_stmt (stmt);
38520
38521 return stmt;
38522 }
38523
38524 /* OpenMP 4.0:
38525 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38526
38527 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38528 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38534
38535 static void
38536 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
38537 enum pragma_context context)
38538 {
38539 bool first_p = parser->omp_declare_simd == NULL;
38540 cp_omp_declare_simd_data data;
38541 if (first_p)
38542 {
38543 data.error_seen = false;
38544 data.fndecl_seen = false;
38545 data.tokens = vNULL;
38546 data.clauses = NULL_TREE;
38547 /* It is safe to take the address of a local variable; it will only be
38548 used while this scope is live. */
38549 parser->omp_declare_simd = &data;
38550 }
38551
38552 /* Store away all pragma tokens. */
38553 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38554 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38555 cp_lexer_consume_token (parser->lexer);
38556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38557 parser->omp_declare_simd->error_seen = true;
38558 cp_parser_require_pragma_eol (parser, pragma_tok);
38559 struct cp_token_cache *cp
38560 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38561 parser->omp_declare_simd->tokens.safe_push (cp);
38562
38563 if (first_p)
38564 {
38565 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38566 cp_parser_pragma (parser, context, NULL);
38567 switch (context)
38568 {
38569 case pragma_external:
38570 cp_parser_declaration (parser);
38571 break;
38572 case pragma_member:
38573 cp_parser_member_declaration (parser);
38574 break;
38575 case pragma_objc_icode:
38576 cp_parser_block_declaration (parser, /*statement_p=*/false);
38577 break;
38578 default:
38579 cp_parser_declaration_statement (parser);
38580 break;
38581 }
38582 if (parser->omp_declare_simd
38583 && !parser->omp_declare_simd->error_seen
38584 && !parser->omp_declare_simd->fndecl_seen)
38585 error_at (pragma_tok->location,
38586 "%<#pragma omp declare simd%> not immediately followed by "
38587 "function declaration or definition");
38588 data.tokens.release ();
38589 parser->omp_declare_simd = NULL;
38590 }
38591 }
38592
38593 /* Finalize #pragma omp declare simd clauses after direct declarator has
38594 been parsed, and put that into "omp declare simd" attribute. */
38595
38596 static tree
38597 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
38598 {
38599 struct cp_token_cache *ce;
38600 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
38601 int i;
38602
38603 if (!data->error_seen && data->fndecl_seen)
38604 {
38605 error ("%<#pragma omp declare simd%> not immediately followed by "
38606 "a single function declaration or definition");
38607 data->error_seen = true;
38608 }
38609 if (data->error_seen)
38610 return attrs;
38611
38612 FOR_EACH_VEC_ELT (data->tokens, i, ce)
38613 {
38614 tree c, cl;
38615
38616 cp_parser_push_lexer_for_tokens (parser, ce);
38617 parser->lexer->in_pragma = true;
38618 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38619 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38620 cp_lexer_consume_token (parser->lexer);
38621 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
38622 "#pragma omp declare simd", pragma_tok);
38623 cp_parser_pop_lexer (parser);
38624 if (cl)
38625 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
38626 c = build_tree_list (get_identifier ("omp declare simd"), cl);
38627 TREE_CHAIN (c) = attrs;
38628 if (processing_template_decl)
38629 ATTR_IS_DEPENDENT (c) = 1;
38630 attrs = c;
38631 }
38632
38633 data->fndecl_seen = true;
38634 return attrs;
38635 }
38636
38637
38638 /* OpenMP 4.0:
38639 # pragma omp declare target new-line
38640 declarations and definitions
38641 # pragma omp end declare target new-line
38642
38643 OpenMP 4.5:
38644 # pragma omp declare target ( extended-list ) new-line
38645
38646 # pragma omp declare target declare-target-clauses[seq] new-line */
38647
38648 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38649 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38651
38652 static void
38653 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
38654 {
38655 tree clauses = NULL_TREE;
38656 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38657 clauses
38658 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
38659 "#pragma omp declare target", pragma_tok);
38660 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38661 {
38662 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
38663 clauses);
38664 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38665 cp_parser_require_pragma_eol (parser, pragma_tok);
38666 }
38667 else
38668 {
38669 cp_parser_require_pragma_eol (parser, pragma_tok);
38670 scope_chain->omp_declare_target_attribute++;
38671 return;
38672 }
38673 if (scope_chain->omp_declare_target_attribute)
38674 error_at (pragma_tok->location,
38675 "%<#pragma omp declare target%> with clauses in between "
38676 "%<#pragma omp declare target%> without clauses and "
38677 "%<#pragma omp end declare target%>");
38678 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
38679 {
38680 tree t = OMP_CLAUSE_DECL (c), id;
38681 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
38682 tree at2 = lookup_attribute ("omp declare target link",
38683 DECL_ATTRIBUTES (t));
38684 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
38685 {
38686 id = get_identifier ("omp declare target link");
38687 std::swap (at1, at2);
38688 }
38689 else
38690 id = get_identifier ("omp declare target");
38691 if (at2)
38692 {
38693 error_at (OMP_CLAUSE_LOCATION (c),
38694 "%qD specified both in declare target %<link%> and %<to%>"
38695 " clauses", t);
38696 continue;
38697 }
38698 if (!at1)
38699 {
38700 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
38701 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
38702 continue;
38703
38704 symtab_node *node = symtab_node::get (t);
38705 if (node != NULL)
38706 {
38707 node->offloadable = 1;
38708 if (ENABLE_OFFLOADING)
38709 {
38710 g->have_offload = true;
38711 if (is_a <varpool_node *> (node))
38712 vec_safe_push (offload_vars, t);
38713 }
38714 }
38715 }
38716 }
38717 }
38718
38719 static void
38720 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
38721 {
38722 const char *p = "";
38723 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38724 {
38725 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38726 p = IDENTIFIER_POINTER (id);
38727 }
38728 if (strcmp (p, "declare") == 0)
38729 {
38730 cp_lexer_consume_token (parser->lexer);
38731 p = "";
38732 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38733 {
38734 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38735 p = IDENTIFIER_POINTER (id);
38736 }
38737 if (strcmp (p, "target") == 0)
38738 cp_lexer_consume_token (parser->lexer);
38739 else
38740 {
38741 cp_parser_error (parser, "expected %<target%>");
38742 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38743 return;
38744 }
38745 }
38746 else
38747 {
38748 cp_parser_error (parser, "expected %<declare%>");
38749 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38750 return;
38751 }
38752 cp_parser_require_pragma_eol (parser, pragma_tok);
38753 if (!scope_chain->omp_declare_target_attribute)
38754 error_at (pragma_tok->location,
38755 "%<#pragma omp end declare target%> without corresponding "
38756 "%<#pragma omp declare target%>");
38757 else
38758 scope_chain->omp_declare_target_attribute--;
38759 }
38760
38761 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
38762 expression and optional initializer clause of
38763 #pragma omp declare reduction. We store the expression(s) as
38764 either 3, 6 or 7 special statements inside of the artificial function's
38765 body. The first two statements are DECL_EXPRs for the artificial
38766 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
38767 expression that uses those variables.
38768 If there was any INITIALIZER clause, this is followed by further statements,
38769 the fourth and fifth statements are DECL_EXPRs for the artificial
38770 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
38771 constructor variant (first token after open paren is not omp_priv),
38772 then the sixth statement is a statement with the function call expression
38773 that uses the OMP_PRIV and optionally OMP_ORIG variable.
38774 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
38775 to initialize the OMP_PRIV artificial variable and there is seventh
38776 statement, a DECL_EXPR of the OMP_PRIV statement again. */
38777
38778 static bool
38779 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
38780 {
38781 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
38782 gcc_assert (TYPE_REF_P (type));
38783 type = TREE_TYPE (type);
38784 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
38785 DECL_ARTIFICIAL (omp_out) = 1;
38786 pushdecl (omp_out);
38787 add_decl_expr (omp_out);
38788 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
38789 DECL_ARTIFICIAL (omp_in) = 1;
38790 pushdecl (omp_in);
38791 add_decl_expr (omp_in);
38792 tree combiner;
38793 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
38794
38795 keep_next_level (true);
38796 tree block = begin_omp_structured_block ();
38797 combiner = cp_parser_expression (parser);
38798 finish_expr_stmt (combiner);
38799 block = finish_omp_structured_block (block);
38800 add_stmt (block);
38801
38802 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38803 return false;
38804
38805 const char *p = "";
38806 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38807 {
38808 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38809 p = IDENTIFIER_POINTER (id);
38810 }
38811
38812 if (strcmp (p, "initializer") == 0)
38813 {
38814 cp_lexer_consume_token (parser->lexer);
38815 matching_parens parens;
38816 if (!parens.require_open (parser))
38817 return false;
38818
38819 p = "";
38820 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38821 {
38822 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38823 p = IDENTIFIER_POINTER (id);
38824 }
38825
38826 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
38827 DECL_ARTIFICIAL (omp_priv) = 1;
38828 pushdecl (omp_priv);
38829 add_decl_expr (omp_priv);
38830 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
38831 DECL_ARTIFICIAL (omp_orig) = 1;
38832 pushdecl (omp_orig);
38833 add_decl_expr (omp_orig);
38834
38835 keep_next_level (true);
38836 block = begin_omp_structured_block ();
38837
38838 bool ctor = false;
38839 if (strcmp (p, "omp_priv") == 0)
38840 {
38841 bool is_direct_init, is_non_constant_init;
38842 ctor = true;
38843 cp_lexer_consume_token (parser->lexer);
38844 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
38845 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
38846 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38847 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
38848 == CPP_CLOSE_PAREN
38849 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
38850 == CPP_CLOSE_PAREN))
38851 {
38852 finish_omp_structured_block (block);
38853 error ("invalid initializer clause");
38854 return false;
38855 }
38856 initializer = cp_parser_initializer (parser, &is_direct_init,
38857 &is_non_constant_init);
38858 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
38859 NULL_TREE, LOOKUP_ONLYCONVERTING);
38860 }
38861 else
38862 {
38863 cp_parser_parse_tentatively (parser);
38864 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
38865 /*check_dependency_p=*/true,
38866 /*template_p=*/NULL,
38867 /*declarator_p=*/false,
38868 /*optional_p=*/false);
38869 vec<tree, va_gc> *args;
38870 if (fn_name == error_mark_node
38871 || cp_parser_error_occurred (parser)
38872 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
38873 || ((args = cp_parser_parenthesized_expression_list
38874 (parser, non_attr, /*cast_p=*/false,
38875 /*allow_expansion_p=*/true,
38876 /*non_constant_p=*/NULL)),
38877 cp_parser_error_occurred (parser)))
38878 {
38879 finish_omp_structured_block (block);
38880 cp_parser_abort_tentative_parse (parser);
38881 cp_parser_error (parser, "expected id-expression (arguments)");
38882 return false;
38883 }
38884 unsigned int i;
38885 tree arg;
38886 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
38887 if (arg == omp_priv
38888 || (TREE_CODE (arg) == ADDR_EXPR
38889 && TREE_OPERAND (arg, 0) == omp_priv))
38890 break;
38891 cp_parser_abort_tentative_parse (parser);
38892 if (arg == NULL_TREE)
38893 error ("one of the initializer call arguments should be %<omp_priv%>"
38894 " or %<&omp_priv%>");
38895 initializer = cp_parser_postfix_expression (parser, false, false, false,
38896 false, NULL);
38897 finish_expr_stmt (initializer);
38898 }
38899
38900 block = finish_omp_structured_block (block);
38901 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
38902 add_stmt (block);
38903
38904 if (ctor)
38905 add_decl_expr (omp_orig);
38906
38907 if (!parens.require_close (parser))
38908 return false;
38909 }
38910
38911 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
38912 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
38913 UNKNOWN_LOCATION);
38914
38915 return true;
38916 }
38917
38918 /* OpenMP 4.0
38919 #pragma omp declare reduction (reduction-id : typename-list : expression) \
38920 initializer-clause[opt] new-line
38921
38922 initializer-clause:
38923 initializer (omp_priv initializer)
38924 initializer (function-name (argument-list)) */
38925
38926 static void
38927 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
38928 enum pragma_context)
38929 {
38930 auto_vec<tree> types;
38931 enum tree_code reduc_code = ERROR_MARK;
38932 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
38933 unsigned int i;
38934 cp_token *first_token;
38935 cp_token_cache *cp;
38936 int errs;
38937 void *p;
38938
38939 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
38940 p = obstack_alloc (&declarator_obstack, 0);
38941
38942 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38943 goto fail;
38944
38945 switch (cp_lexer_peek_token (parser->lexer)->type)
38946 {
38947 case CPP_PLUS:
38948 reduc_code = PLUS_EXPR;
38949 break;
38950 case CPP_MULT:
38951 reduc_code = MULT_EXPR;
38952 break;
38953 case CPP_MINUS:
38954 reduc_code = MINUS_EXPR;
38955 break;
38956 case CPP_AND:
38957 reduc_code = BIT_AND_EXPR;
38958 break;
38959 case CPP_XOR:
38960 reduc_code = BIT_XOR_EXPR;
38961 break;
38962 case CPP_OR:
38963 reduc_code = BIT_IOR_EXPR;
38964 break;
38965 case CPP_AND_AND:
38966 reduc_code = TRUTH_ANDIF_EXPR;
38967 break;
38968 case CPP_OR_OR:
38969 reduc_code = TRUTH_ORIF_EXPR;
38970 break;
38971 case CPP_NAME:
38972 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
38973 break;
38974 default:
38975 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
38976 "%<|%>, %<&&%>, %<||%> or identifier");
38977 goto fail;
38978 }
38979
38980 if (reduc_code != ERROR_MARK)
38981 cp_lexer_consume_token (parser->lexer);
38982
38983 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
38984 if (reduc_id == error_mark_node)
38985 goto fail;
38986
38987 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38988 goto fail;
38989
38990 /* Types may not be defined in declare reduction type list. */
38991 const char *saved_message;
38992 saved_message = parser->type_definition_forbidden_message;
38993 parser->type_definition_forbidden_message
38994 = G_("types may not be defined in declare reduction type list");
38995 bool saved_colon_corrects_to_scope_p;
38996 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38997 parser->colon_corrects_to_scope_p = false;
38998 bool saved_colon_doesnt_start_class_def_p;
38999 saved_colon_doesnt_start_class_def_p
39000 = parser->colon_doesnt_start_class_def_p;
39001 parser->colon_doesnt_start_class_def_p = true;
39002
39003 while (true)
39004 {
39005 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39006 type = cp_parser_type_id (parser);
39007 if (type == error_mark_node)
39008 ;
39009 else if (ARITHMETIC_TYPE_P (type)
39010 && (orig_reduc_id == NULL_TREE
39011 || (TREE_CODE (type) != COMPLEX_TYPE
39012 && (id_equal (orig_reduc_id, "min")
39013 || id_equal (orig_reduc_id, "max")))))
39014 error_at (loc, "predeclared arithmetic type %qT in "
39015 "%<#pragma omp declare reduction%>", type);
39016 else if (TREE_CODE (type) == FUNCTION_TYPE
39017 || TREE_CODE (type) == METHOD_TYPE
39018 || TREE_CODE (type) == ARRAY_TYPE)
39019 error_at (loc, "function or array type %qT in "
39020 "%<#pragma omp declare reduction%>", type);
39021 else if (TYPE_REF_P (type))
39022 error_at (loc, "reference type %qT in "
39023 "%<#pragma omp declare reduction%>", type);
39024 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39025 error_at (loc, "const, volatile or __restrict qualified type %qT in "
39026 "%<#pragma omp declare reduction%>", type);
39027 else
39028 types.safe_push (type);
39029
39030 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39031 cp_lexer_consume_token (parser->lexer);
39032 else
39033 break;
39034 }
39035
39036 /* Restore the saved message. */
39037 parser->type_definition_forbidden_message = saved_message;
39038 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39039 parser->colon_doesnt_start_class_def_p
39040 = saved_colon_doesnt_start_class_def_p;
39041
39042 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39043 || types.is_empty ())
39044 {
39045 fail:
39046 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39047 goto done;
39048 }
39049
39050 first_token = cp_lexer_peek_token (parser->lexer);
39051 cp = NULL;
39052 errs = errorcount;
39053 FOR_EACH_VEC_ELT (types, i, type)
39054 {
39055 tree fntype
39056 = build_function_type_list (void_type_node,
39057 cp_build_reference_type (type, false),
39058 NULL_TREE);
39059 tree this_reduc_id = reduc_id;
39060 if (!dependent_type_p (type))
39061 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39062 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39063 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39064 DECL_ARTIFICIAL (fndecl) = 1;
39065 DECL_EXTERNAL (fndecl) = 1;
39066 DECL_DECLARED_INLINE_P (fndecl) = 1;
39067 DECL_IGNORED_P (fndecl) = 1;
39068 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39069 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39070 DECL_ATTRIBUTES (fndecl)
39071 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39072 DECL_ATTRIBUTES (fndecl));
39073 if (processing_template_decl)
39074 fndecl = push_template_decl (fndecl);
39075 bool block_scope = false;
39076 tree block = NULL_TREE;
39077 if (current_function_decl)
39078 {
39079 block_scope = true;
39080 DECL_CONTEXT (fndecl) = global_namespace;
39081 if (!processing_template_decl)
39082 pushdecl (fndecl);
39083 }
39084 else if (current_class_type)
39085 {
39086 if (cp == NULL)
39087 {
39088 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39089 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39090 cp_lexer_consume_token (parser->lexer);
39091 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39092 goto fail;
39093 cp = cp_token_cache_new (first_token,
39094 cp_lexer_peek_nth_token (parser->lexer,
39095 2));
39096 }
39097 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39098 finish_member_declaration (fndecl);
39099 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39100 DECL_PENDING_INLINE_P (fndecl) = 1;
39101 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39102 continue;
39103 }
39104 else
39105 {
39106 DECL_CONTEXT (fndecl) = current_namespace;
39107 pushdecl (fndecl);
39108 }
39109 if (!block_scope)
39110 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39111 else
39112 block = begin_omp_structured_block ();
39113 if (cp)
39114 {
39115 cp_parser_push_lexer_for_tokens (parser, cp);
39116 parser->lexer->in_pragma = true;
39117 }
39118 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39119 {
39120 if (!block_scope)
39121 finish_function (/*inline_p=*/false);
39122 else
39123 DECL_CONTEXT (fndecl) = current_function_decl;
39124 if (cp)
39125 cp_parser_pop_lexer (parser);
39126 goto fail;
39127 }
39128 if (cp)
39129 cp_parser_pop_lexer (parser);
39130 if (!block_scope)
39131 finish_function (/*inline_p=*/false);
39132 else
39133 {
39134 DECL_CONTEXT (fndecl) = current_function_decl;
39135 block = finish_omp_structured_block (block);
39136 if (TREE_CODE (block) == BIND_EXPR)
39137 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39138 else if (TREE_CODE (block) == STATEMENT_LIST)
39139 DECL_SAVED_TREE (fndecl) = block;
39140 if (processing_template_decl)
39141 add_decl_expr (fndecl);
39142 }
39143 cp_check_omp_declare_reduction (fndecl);
39144 if (cp == NULL && types.length () > 1)
39145 cp = cp_token_cache_new (first_token,
39146 cp_lexer_peek_nth_token (parser->lexer, 2));
39147 if (errs != errorcount)
39148 break;
39149 }
39150
39151 cp_parser_require_pragma_eol (parser, pragma_tok);
39152
39153 done:
39154 /* Free any declarators allocated. */
39155 obstack_free (&declarator_obstack, p);
39156 }
39157
39158 /* OpenMP 4.0
39159 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39160 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39161 initializer-clause[opt] new-line
39162 #pragma omp declare target new-line */
39163
39164 static bool
39165 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39166 enum pragma_context context)
39167 {
39168 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39169 {
39170 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39171 const char *p = IDENTIFIER_POINTER (id);
39172
39173 if (strcmp (p, "simd") == 0)
39174 {
39175 cp_lexer_consume_token (parser->lexer);
39176 cp_parser_omp_declare_simd (parser, pragma_tok,
39177 context);
39178 return true;
39179 }
39180 cp_ensure_no_omp_declare_simd (parser);
39181 if (strcmp (p, "reduction") == 0)
39182 {
39183 cp_lexer_consume_token (parser->lexer);
39184 cp_parser_omp_declare_reduction (parser, pragma_tok,
39185 context);
39186 return false;
39187 }
39188 if (!flag_openmp) /* flag_openmp_simd */
39189 {
39190 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39191 return false;
39192 }
39193 if (strcmp (p, "target") == 0)
39194 {
39195 cp_lexer_consume_token (parser->lexer);
39196 cp_parser_omp_declare_target (parser, pragma_tok);
39197 return false;
39198 }
39199 }
39200 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39201 "or %<target%>");
39202 cp_parser_require_pragma_eol (parser, pragma_tok);
39203 return false;
39204 }
39205
39206 /* OpenMP 5.0
39207 #pragma omp requires clauses[optseq] new-line */
39208
39209 static bool
39210 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39211 {
39212 bool first = true;
39213 enum omp_requires new_req = (enum omp_requires) 0;
39214
39215 location_t loc = pragma_tok->location;
39216 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39217 {
39218 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39219 cp_lexer_consume_token (parser->lexer);
39220
39221 first = false;
39222
39223 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39224 {
39225 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39226 const char *p = IDENTIFIER_POINTER (id);
39227 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39228 enum omp_requires this_req = (enum omp_requires) 0;
39229
39230 if (!strcmp (p, "unified_address"))
39231 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39232 else if (!strcmp (p, "unified_shared_memory"))
39233 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39234 else if (!strcmp (p, "dynamic_allocators"))
39235 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39236 else if (!strcmp (p, "reverse_offload"))
39237 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39238 else if (!strcmp (p, "atomic_default_mem_order"))
39239 {
39240 cp_lexer_consume_token (parser->lexer);
39241
39242 matching_parens parens;
39243 if (parens.require_open (parser))
39244 {
39245 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39246 {
39247 id = cp_lexer_peek_token (parser->lexer)->u.value;
39248 p = IDENTIFIER_POINTER (id);
39249
39250 if (!strcmp (p, "seq_cst"))
39251 this_req
39252 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39253 else if (!strcmp (p, "relaxed"))
39254 this_req
39255 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39256 else if (!strcmp (p, "acq_rel"))
39257 this_req
39258 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39259 }
39260 if (this_req == 0)
39261 {
39262 error_at (cp_lexer_peek_token (parser->lexer)->location,
39263 "expected %<seq_cst%>, %<relaxed%> or "
39264 "%<acq_rel%>");
39265 if (cp_lexer_nth_token_is (parser->lexer, 2,
39266 CPP_CLOSE_PAREN))
39267 cp_lexer_consume_token (parser->lexer);
39268 }
39269 else
39270 cp_lexer_consume_token (parser->lexer);
39271
39272 if (!parens.require_close (parser))
39273 cp_parser_skip_to_closing_parenthesis (parser,
39274 /*recovering=*/true,
39275 /*or_comma=*/false,
39276 /*consume_paren=*/
39277 true);
39278
39279 if (this_req == 0)
39280 {
39281 cp_parser_require_pragma_eol (parser, pragma_tok);
39282 return false;
39283 }
39284 }
39285 p = NULL;
39286 }
39287 else
39288 {
39289 error_at (cloc, "expected %<unified_address%>, "
39290 "%<unified_shared_memory%>, "
39291 "%<dynamic_allocators%>, "
39292 "%<reverse_offload%> "
39293 "or %<atomic_default_mem_order%> clause");
39294 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39295 return false;
39296 }
39297 if (p)
39298 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39299 "supported yet", p);
39300 if (p)
39301 cp_lexer_consume_token (parser->lexer);
39302 if (this_req)
39303 {
39304 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39305 {
39306 if ((this_req & new_req) != 0)
39307 error_at (cloc, "too many %qs clauses", p);
39308 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39309 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39310 error_at (cloc, "%qs clause used lexically after first "
39311 "target construct or offloading API", p);
39312 }
39313 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39314 {
39315 error_at (cloc, "too many %qs clauses",
39316 "atomic_default_mem_order");
39317 this_req = (enum omp_requires) 0;
39318 }
39319 else if ((omp_requires_mask
39320 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39321 {
39322 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39323 " clause in a single compilation unit");
39324 this_req
39325 = (enum omp_requires)
39326 (omp_requires_mask
39327 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39328 }
39329 else if ((omp_requires_mask
39330 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39331 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39332 "lexically after first %<atomic%> construct "
39333 "without memory order clause");
39334 new_req = (enum omp_requires) (new_req | this_req);
39335 omp_requires_mask
39336 = (enum omp_requires) (omp_requires_mask | this_req);
39337 continue;
39338 }
39339 }
39340 break;
39341 }
39342 cp_parser_require_pragma_eol (parser, pragma_tok);
39343
39344 if (new_req == 0)
39345 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39346 return false;
39347 }
39348
39349
39350 /* OpenMP 4.5:
39351 #pragma omp taskloop taskloop-clause[optseq] new-line
39352 for-loop
39353
39354 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39355 for-loop */
39356
39357 #define OMP_TASKLOOP_CLAUSE_MASK \
39358 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39374
39375 static tree
39376 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39377 char *p_name, omp_clause_mask mask, tree *cclauses,
39378 bool *if_p)
39379 {
39380 tree clauses, sb, ret;
39381 unsigned int save;
39382 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39383
39384 strcat (p_name, " taskloop");
39385 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39386 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39387 clause. */
39388 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39389 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39390
39391 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39392 {
39393 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39394 const char *p = IDENTIFIER_POINTER (id);
39395
39396 if (strcmp (p, "simd") == 0)
39397 {
39398 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39399 if (cclauses == NULL)
39400 cclauses = cclauses_buf;
39401
39402 cp_lexer_consume_token (parser->lexer);
39403 if (!flag_openmp) /* flag_openmp_simd */
39404 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39405 cclauses, if_p);
39406 sb = begin_omp_structured_block ();
39407 save = cp_parser_begin_omp_structured_block (parser);
39408 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39409 cclauses, if_p);
39410 cp_parser_end_omp_structured_block (parser, save);
39411 tree body = finish_omp_structured_block (sb);
39412 if (ret == NULL)
39413 return ret;
39414 ret = make_node (OMP_TASKLOOP);
39415 TREE_TYPE (ret) = void_type_node;
39416 OMP_FOR_BODY (ret) = body;
39417 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39418 SET_EXPR_LOCATION (ret, loc);
39419 add_stmt (ret);
39420 return ret;
39421 }
39422 }
39423 if (!flag_openmp) /* flag_openmp_simd */
39424 {
39425 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39426 return NULL_TREE;
39427 }
39428
39429 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39430 cclauses == NULL);
39431 if (cclauses)
39432 {
39433 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39434 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39435 }
39436
39437 keep_next_level (true);
39438 sb = begin_omp_structured_block ();
39439 save = cp_parser_begin_omp_structured_block (parser);
39440
39441 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39442 if_p);
39443
39444 cp_parser_end_omp_structured_block (parser, save);
39445 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39446
39447 return ret;
39448 }
39449
39450
39451 /* OpenACC 2.0:
39452 # pragma acc routine oacc-routine-clause[optseq] new-line
39453 function-definition
39454
39455 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39456 */
39457
39458 #define OACC_ROUTINE_CLAUSE_MASK \
39459 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39463
39464
39465 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39466 component, which must resolve to a declared namespace-scope
39467 function. The clauses are either processed directly (for a named
39468 function), or defered until the immediatley following declaration
39469 is parsed. */
39470
39471 static void
39472 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39473 enum pragma_context context)
39474 {
39475 gcc_checking_assert (context == pragma_external);
39476 /* The checking for "another pragma following this one" in the "no optional
39477 '( name )'" case makes sure that we dont re-enter. */
39478 gcc_checking_assert (parser->oacc_routine == NULL);
39479
39480 cp_oacc_routine_data data;
39481 data.error_seen = false;
39482 data.fndecl_seen = false;
39483 data.tokens = vNULL;
39484 data.clauses = NULL_TREE;
39485 data.loc = pragma_tok->location;
39486 /* It is safe to take the address of a local variable; it will only be
39487 used while this scope is live. */
39488 parser->oacc_routine = &data;
39489
39490 /* Look for optional '( name )'. */
39491 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39492 {
39493 matching_parens parens;
39494 parens.consume_open (parser); /* '(' */
39495
39496 /* We parse the name as an id-expression. If it resolves to
39497 anything other than a non-overloaded function at namespace
39498 scope, it's an error. */
39499 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
39500 tree name = cp_parser_id_expression (parser,
39501 /*template_keyword_p=*/false,
39502 /*check_dependency_p=*/false,
39503 /*template_p=*/NULL,
39504 /*declarator_p=*/false,
39505 /*optional_p=*/false);
39506 tree decl = (identifier_p (name)
39507 ? cp_parser_lookup_name_simple (parser, name, name_loc)
39508 : name);
39509 if (name != error_mark_node && decl == error_mark_node)
39510 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
39511
39512 if (decl == error_mark_node
39513 || !parens.require_close (parser))
39514 {
39515 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39516 parser->oacc_routine = NULL;
39517 return;
39518 }
39519
39520 data.clauses
39521 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39522 "#pragma acc routine",
39523 cp_lexer_peek_token (parser->lexer));
39524
39525 if (decl && is_overloaded_fn (decl)
39526 && (TREE_CODE (decl) != FUNCTION_DECL
39527 || DECL_FUNCTION_TEMPLATE_P (decl)))
39528 {
39529 error_at (name_loc,
39530 "%<#pragma acc routine%> names a set of overloads");
39531 parser->oacc_routine = NULL;
39532 return;
39533 }
39534
39535 /* Perhaps we should use the same rule as declarations in different
39536 namespaces? */
39537 if (!DECL_NAMESPACE_SCOPE_P (decl))
39538 {
39539 error_at (name_loc,
39540 "%qD does not refer to a namespace scope function", decl);
39541 parser->oacc_routine = NULL;
39542 return;
39543 }
39544
39545 if (TREE_CODE (decl) != FUNCTION_DECL)
39546 {
39547 error_at (name_loc, "%qD does not refer to a function", decl);
39548 parser->oacc_routine = NULL;
39549 return;
39550 }
39551
39552 cp_finalize_oacc_routine (parser, decl, false);
39553 parser->oacc_routine = NULL;
39554 }
39555 else /* No optional '( name )'. */
39556 {
39557 /* Store away all pragma tokens. */
39558 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39559 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39560 cp_lexer_consume_token (parser->lexer);
39561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39562 parser->oacc_routine->error_seen = true;
39563 cp_parser_require_pragma_eol (parser, pragma_tok);
39564 struct cp_token_cache *cp
39565 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39566 parser->oacc_routine->tokens.safe_push (cp);
39567
39568 /* Emit a helpful diagnostic if there's another pragma following this
39569 one. */
39570 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39571 {
39572 cp_ensure_no_oacc_routine (parser);
39573 data.tokens.release ();
39574 /* ..., and then just keep going. */
39575 return;
39576 }
39577
39578 /* We only have to consider the pragma_external case here. */
39579 cp_parser_declaration (parser);
39580 if (parser->oacc_routine
39581 && !parser->oacc_routine->fndecl_seen)
39582 cp_ensure_no_oacc_routine (parser);
39583 else
39584 parser->oacc_routine = NULL;
39585 data.tokens.release ();
39586 }
39587 }
39588
39589 /* Finalize #pragma acc routine clauses after direct declarator has
39590 been parsed. */
39591
39592 static tree
39593 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
39594 {
39595 struct cp_token_cache *ce;
39596 cp_oacc_routine_data *data = parser->oacc_routine;
39597
39598 if (!data->error_seen && data->fndecl_seen)
39599 {
39600 error_at (data->loc,
39601 "%<#pragma acc routine%> not immediately followed by "
39602 "a single function declaration or definition");
39603 data->error_seen = true;
39604 }
39605 if (data->error_seen)
39606 return attrs;
39607
39608 gcc_checking_assert (data->tokens.length () == 1);
39609 ce = data->tokens[0];
39610
39611 cp_parser_push_lexer_for_tokens (parser, ce);
39612 parser->lexer->in_pragma = true;
39613 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39614
39615 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39616 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
39617 parser->oacc_routine->clauses
39618 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39619 "#pragma acc routine", pragma_tok);
39620 cp_parser_pop_lexer (parser);
39621 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39622 fndecl_seen. */
39623
39624 return attrs;
39625 }
39626
39627 /* Apply any saved OpenACC routine clauses to a just-parsed
39628 declaration. */
39629
39630 static void
39631 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
39632 {
39633 if (__builtin_expect (parser->oacc_routine != NULL, 0))
39634 {
39635 /* Keep going if we're in error reporting mode. */
39636 if (parser->oacc_routine->error_seen
39637 || fndecl == error_mark_node)
39638 return;
39639
39640 if (parser->oacc_routine->fndecl_seen)
39641 {
39642 error_at (parser->oacc_routine->loc,
39643 "%<#pragma acc routine%> not immediately followed by"
39644 " a single function declaration or definition");
39645 parser->oacc_routine = NULL;
39646 return;
39647 }
39648 if (TREE_CODE (fndecl) != FUNCTION_DECL)
39649 {
39650 cp_ensure_no_oacc_routine (parser);
39651 return;
39652 }
39653
39654 if (oacc_get_fn_attrib (fndecl))
39655 {
39656 error_at (parser->oacc_routine->loc,
39657 "%<#pragma acc routine%> already applied to %qD", fndecl);
39658 parser->oacc_routine = NULL;
39659 return;
39660 }
39661
39662 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
39663 {
39664 error_at (parser->oacc_routine->loc,
39665 TREE_USED (fndecl)
39666 ? G_("%<#pragma acc routine%> must be applied before use")
39667 : G_("%<#pragma acc routine%> must be applied before "
39668 "definition"));
39669 parser->oacc_routine = NULL;
39670 return;
39671 }
39672
39673 /* Process the routine's dimension clauses. */
39674 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
39675 oacc_replace_fn_attrib (fndecl, dims);
39676
39677 /* Add an "omp declare target" attribute. */
39678 DECL_ATTRIBUTES (fndecl)
39679 = tree_cons (get_identifier ("omp declare target"),
39680 NULL_TREE, DECL_ATTRIBUTES (fndecl));
39681
39682 /* Don't unset parser->oacc_routine here: we may still need it to
39683 diagnose wrong usage. But, remember that we've used this "#pragma acc
39684 routine". */
39685 parser->oacc_routine->fndecl_seen = true;
39686 }
39687 }
39688
39689 /* Main entry point to OpenMP statement pragmas. */
39690
39691 static void
39692 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39693 {
39694 tree stmt;
39695 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
39696 omp_clause_mask mask (0);
39697
39698 switch (cp_parser_pragma_kind (pragma_tok))
39699 {
39700 case PRAGMA_OACC_ATOMIC:
39701 cp_parser_omp_atomic (parser, pragma_tok);
39702 return;
39703 case PRAGMA_OACC_CACHE:
39704 stmt = cp_parser_oacc_cache (parser, pragma_tok);
39705 break;
39706 case PRAGMA_OACC_DATA:
39707 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
39708 break;
39709 case PRAGMA_OACC_ENTER_DATA:
39710 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
39711 break;
39712 case PRAGMA_OACC_EXIT_DATA:
39713 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
39714 break;
39715 case PRAGMA_OACC_HOST_DATA:
39716 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
39717 break;
39718 case PRAGMA_OACC_KERNELS:
39719 case PRAGMA_OACC_PARALLEL:
39720 strcpy (p_name, "#pragma acc");
39721 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
39722 if_p);
39723 break;
39724 case PRAGMA_OACC_LOOP:
39725 strcpy (p_name, "#pragma acc");
39726 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
39727 if_p);
39728 break;
39729 case PRAGMA_OACC_UPDATE:
39730 stmt = cp_parser_oacc_update (parser, pragma_tok);
39731 break;
39732 case PRAGMA_OACC_WAIT:
39733 stmt = cp_parser_oacc_wait (parser, pragma_tok);
39734 break;
39735 case PRAGMA_OMP_ATOMIC:
39736 cp_parser_omp_atomic (parser, pragma_tok);
39737 return;
39738 case PRAGMA_OMP_CRITICAL:
39739 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
39740 break;
39741 case PRAGMA_OMP_DISTRIBUTE:
39742 strcpy (p_name, "#pragma omp");
39743 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
39744 if_p);
39745 break;
39746 case PRAGMA_OMP_FOR:
39747 strcpy (p_name, "#pragma omp");
39748 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
39749 if_p);
39750 break;
39751 case PRAGMA_OMP_MASTER:
39752 strcpy (p_name, "#pragma omp");
39753 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
39754 if_p);
39755 break;
39756 case PRAGMA_OMP_PARALLEL:
39757 strcpy (p_name, "#pragma omp");
39758 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
39759 if_p);
39760 break;
39761 case PRAGMA_OMP_SECTIONS:
39762 strcpy (p_name, "#pragma omp");
39763 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
39764 break;
39765 case PRAGMA_OMP_SIMD:
39766 strcpy (p_name, "#pragma omp");
39767 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
39768 if_p);
39769 break;
39770 case PRAGMA_OMP_SINGLE:
39771 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
39772 break;
39773 case PRAGMA_OMP_TASK:
39774 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
39775 break;
39776 case PRAGMA_OMP_TASKGROUP:
39777 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
39778 break;
39779 case PRAGMA_OMP_TASKLOOP:
39780 strcpy (p_name, "#pragma omp");
39781 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
39782 if_p);
39783 break;
39784 case PRAGMA_OMP_TEAMS:
39785 strcpy (p_name, "#pragma omp");
39786 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
39787 if_p);
39788 break;
39789 default:
39790 gcc_unreachable ();
39791 }
39792
39793 protected_set_expr_location (stmt, pragma_tok->location);
39794 }
39795 \f
39796 /* Transactional Memory parsing routines. */
39797
39798 /* Parse a transaction attribute.
39799
39800 txn-attribute:
39801 attribute
39802 [ [ identifier ] ]
39803
39804 We use this instead of cp_parser_attributes_opt for transactions to avoid
39805 the pedwarn in C++98 mode. */
39806
39807 static tree
39808 cp_parser_txn_attribute_opt (cp_parser *parser)
39809 {
39810 cp_token *token;
39811 tree attr_name, attr = NULL;
39812
39813 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
39814 return cp_parser_attributes_opt (parser);
39815
39816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
39817 return NULL_TREE;
39818 cp_lexer_consume_token (parser->lexer);
39819 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
39820 goto error1;
39821
39822 token = cp_lexer_peek_token (parser->lexer);
39823 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
39824 {
39825 token = cp_lexer_consume_token (parser->lexer);
39826
39827 attr_name = (token->type == CPP_KEYWORD
39828 /* For keywords, use the canonical spelling,
39829 not the parsed identifier. */
39830 ? ridpointers[(int) token->keyword]
39831 : token->u.value);
39832 attr = build_tree_list (attr_name, NULL_TREE);
39833 }
39834 else
39835 cp_parser_error (parser, "expected identifier");
39836
39837 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39838 error1:
39839 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39840 return attr;
39841 }
39842
39843 /* Parse a __transaction_atomic or __transaction_relaxed statement.
39844
39845 transaction-statement:
39846 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
39847 compound-statement
39848 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
39849 */
39850
39851 static tree
39852 cp_parser_transaction (cp_parser *parser, cp_token *token)
39853 {
39854 unsigned char old_in = parser->in_transaction;
39855 unsigned char this_in = 1, new_in;
39856 enum rid keyword = token->keyword;
39857 tree stmt, attrs, noex;
39858
39859 cp_lexer_consume_token (parser->lexer);
39860
39861 if (keyword == RID_TRANSACTION_RELAXED
39862 || keyword == RID_SYNCHRONIZED)
39863 this_in |= TM_STMT_ATTR_RELAXED;
39864 else
39865 {
39866 attrs = cp_parser_txn_attribute_opt (parser);
39867 if (attrs)
39868 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
39869 }
39870
39871 /* Parse a noexcept specification. */
39872 if (keyword == RID_ATOMIC_NOEXCEPT)
39873 noex = boolean_true_node;
39874 else if (keyword == RID_ATOMIC_CANCEL)
39875 {
39876 /* cancel-and-throw is unimplemented. */
39877 sorry ("atomic_cancel");
39878 noex = NULL_TREE;
39879 }
39880 else
39881 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
39882
39883 /* Keep track if we're in the lexical scope of an outer transaction. */
39884 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
39885
39886 stmt = begin_transaction_stmt (token->location, NULL, this_in);
39887
39888 parser->in_transaction = new_in;
39889 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
39890 parser->in_transaction = old_in;
39891
39892 finish_transaction_stmt (stmt, NULL, this_in, noex);
39893
39894 return stmt;
39895 }
39896
39897 /* Parse a __transaction_atomic or __transaction_relaxed expression.
39898
39899 transaction-expression:
39900 __transaction_atomic txn-noexcept-spec[opt] ( expression )
39901 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
39902 */
39903
39904 static tree
39905 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
39906 {
39907 unsigned char old_in = parser->in_transaction;
39908 unsigned char this_in = 1;
39909 cp_token *token;
39910 tree expr, noex;
39911 bool noex_expr;
39912 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39913
39914 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
39915 || keyword == RID_TRANSACTION_RELAXED);
39916
39917 if (!flag_tm)
39918 error_at (loc,
39919 keyword == RID_TRANSACTION_RELAXED
39920 ? G_("%<__transaction_relaxed%> without transactional memory "
39921 "support enabled")
39922 : G_("%<__transaction_atomic%> without transactional memory "
39923 "support enabled"));
39924
39925 token = cp_parser_require_keyword (parser, keyword,
39926 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
39927 : RT_TRANSACTION_RELAXED));
39928 gcc_assert (token != NULL);
39929
39930 if (keyword == RID_TRANSACTION_RELAXED)
39931 this_in |= TM_STMT_ATTR_RELAXED;
39932
39933 /* Set this early. This might mean that we allow transaction_cancel in
39934 an expression that we find out later actually has to be a constexpr.
39935 However, we expect that cxx_constant_value will be able to deal with
39936 this; also, if the noexcept has no constexpr, then what we parse next
39937 really is a transaction's body. */
39938 parser->in_transaction = this_in;
39939
39940 /* Parse a noexcept specification. */
39941 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
39942 true);
39943
39944 if (!noex || !noex_expr
39945 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39946 {
39947 matching_parens parens;
39948 parens.require_open (parser);
39949
39950 expr = cp_parser_expression (parser);
39951 expr = finish_parenthesized_expr (expr);
39952
39953 parens.require_close (parser);
39954 }
39955 else
39956 {
39957 /* The only expression that is available got parsed for the noexcept
39958 already. noexcept is true then. */
39959 expr = noex;
39960 noex = boolean_true_node;
39961 }
39962
39963 expr = build_transaction_expr (token->location, expr, this_in, noex);
39964 parser->in_transaction = old_in;
39965
39966 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
39967 return error_mark_node;
39968
39969 return (flag_tm ? expr : error_mark_node);
39970 }
39971
39972 /* Parse a function-transaction-block.
39973
39974 function-transaction-block:
39975 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
39976 function-body
39977 __transaction_atomic txn-attribute[opt] function-try-block
39978 __transaction_relaxed ctor-initializer[opt] function-body
39979 __transaction_relaxed function-try-block
39980 */
39981
39982 static void
39983 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
39984 {
39985 unsigned char old_in = parser->in_transaction;
39986 unsigned char new_in = 1;
39987 tree compound_stmt, stmt, attrs;
39988 cp_token *token;
39989
39990 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
39991 || keyword == RID_TRANSACTION_RELAXED);
39992 token = cp_parser_require_keyword (parser, keyword,
39993 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
39994 : RT_TRANSACTION_RELAXED));
39995 gcc_assert (token != NULL);
39996
39997 if (keyword == RID_TRANSACTION_RELAXED)
39998 new_in |= TM_STMT_ATTR_RELAXED;
39999 else
40000 {
40001 attrs = cp_parser_txn_attribute_opt (parser);
40002 if (attrs)
40003 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40004 }
40005
40006 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40007
40008 parser->in_transaction = new_in;
40009
40010 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40011 cp_parser_function_try_block (parser);
40012 else
40013 cp_parser_ctor_initializer_opt_and_function_body
40014 (parser, /*in_function_try_block=*/false);
40015
40016 parser->in_transaction = old_in;
40017
40018 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40019 }
40020
40021 /* Parse a __transaction_cancel statement.
40022
40023 cancel-statement:
40024 __transaction_cancel txn-attribute[opt] ;
40025 __transaction_cancel txn-attribute[opt] throw-expression ;
40026
40027 ??? Cancel and throw is not yet implemented. */
40028
40029 static tree
40030 cp_parser_transaction_cancel (cp_parser *parser)
40031 {
40032 cp_token *token;
40033 bool is_outer = false;
40034 tree stmt, attrs;
40035
40036 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40037 RT_TRANSACTION_CANCEL);
40038 gcc_assert (token != NULL);
40039
40040 attrs = cp_parser_txn_attribute_opt (parser);
40041 if (attrs)
40042 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40043
40044 /* ??? Parse cancel-and-throw here. */
40045
40046 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40047
40048 if (!flag_tm)
40049 {
40050 error_at (token->location, "%<__transaction_cancel%> without "
40051 "transactional memory support enabled");
40052 return error_mark_node;
40053 }
40054 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40055 {
40056 error_at (token->location, "%<__transaction_cancel%> within a "
40057 "%<__transaction_relaxed%>");
40058 return error_mark_node;
40059 }
40060 else if (is_outer)
40061 {
40062 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40063 && !is_tm_may_cancel_outer (current_function_decl))
40064 {
40065 error_at (token->location, "outer %<__transaction_cancel%> not "
40066 "within outer %<__transaction_atomic%>");
40067 error_at (token->location,
40068 " or a %<transaction_may_cancel_outer%> function");
40069 return error_mark_node;
40070 }
40071 }
40072 else if (parser->in_transaction == 0)
40073 {
40074 error_at (token->location, "%<__transaction_cancel%> not within "
40075 "%<__transaction_atomic%>");
40076 return error_mark_node;
40077 }
40078
40079 stmt = build_tm_abort_call (token->location, is_outer);
40080 add_stmt (stmt);
40081
40082 return stmt;
40083 }
40084 \f
40085 /* The parser. */
40086
40087 static GTY (()) cp_parser *the_parser;
40088
40089 \f
40090 /* Special handling for the first token or line in the file. The first
40091 thing in the file might be #pragma GCC pch_preprocess, which loads a
40092 PCH file, which is a GC collection point. So we need to handle this
40093 first pragma without benefit of an existing lexer structure.
40094
40095 Always returns one token to the caller in *FIRST_TOKEN. This is
40096 either the true first token of the file, or the first token after
40097 the initial pragma. */
40098
40099 static void
40100 cp_parser_initial_pragma (cp_token *first_token)
40101 {
40102 tree name = NULL;
40103
40104 cp_lexer_get_preprocessor_token (NULL, first_token);
40105 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40106 return;
40107
40108 cp_lexer_get_preprocessor_token (NULL, first_token);
40109 if (first_token->type == CPP_STRING)
40110 {
40111 name = first_token->u.value;
40112
40113 cp_lexer_get_preprocessor_token (NULL, first_token);
40114 if (first_token->type != CPP_PRAGMA_EOL)
40115 error_at (first_token->location,
40116 "junk at end of %<#pragma GCC pch_preprocess%>");
40117 }
40118 else
40119 error_at (first_token->location, "expected string literal");
40120
40121 /* Skip to the end of the pragma. */
40122 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40123 cp_lexer_get_preprocessor_token (NULL, first_token);
40124
40125 /* Now actually load the PCH file. */
40126 if (name)
40127 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40128
40129 /* Read one more token to return to our caller. We have to do this
40130 after reading the PCH file in, since its pointers have to be
40131 live. */
40132 cp_lexer_get_preprocessor_token (NULL, first_token);
40133 }
40134
40135 /* Parse a pragma GCC ivdep. */
40136
40137 static bool
40138 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40139 {
40140 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40141 return true;
40142 }
40143
40144 /* Parse a pragma GCC unroll. */
40145
40146 static unsigned short
40147 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40148 {
40149 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40150 tree expr = cp_parser_constant_expression (parser);
40151 unsigned short unroll;
40152 expr = maybe_constant_value (expr);
40153 HOST_WIDE_INT lunroll = 0;
40154 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40155 || TREE_CODE (expr) != INTEGER_CST
40156 || (lunroll = tree_to_shwi (expr)) < 0
40157 || lunroll >= USHRT_MAX)
40158 {
40159 error_at (location, "%<#pragma GCC unroll%> requires an"
40160 " assignment-expression that evaluates to a non-negative"
40161 " integral constant less than %u", USHRT_MAX);
40162 unroll = 0;
40163 }
40164 else
40165 {
40166 unroll = (unsigned short)lunroll;
40167 if (unroll == 0)
40168 unroll = 1;
40169 }
40170 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40171 return unroll;
40172 }
40173
40174 /* Normal parsing of a pragma token. Here we can (and must) use the
40175 regular lexer. */
40176
40177 static bool
40178 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40179 {
40180 cp_token *pragma_tok;
40181 unsigned int id;
40182 tree stmt;
40183 bool ret;
40184
40185 pragma_tok = cp_lexer_consume_token (parser->lexer);
40186 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40187 parser->lexer->in_pragma = true;
40188
40189 id = cp_parser_pragma_kind (pragma_tok);
40190 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40191 cp_ensure_no_omp_declare_simd (parser);
40192 switch (id)
40193 {
40194 case PRAGMA_GCC_PCH_PREPROCESS:
40195 error_at (pragma_tok->location,
40196 "%<#pragma GCC pch_preprocess%> must be first");
40197 break;
40198
40199 case PRAGMA_OMP_BARRIER:
40200 switch (context)
40201 {
40202 case pragma_compound:
40203 cp_parser_omp_barrier (parser, pragma_tok);
40204 return false;
40205 case pragma_stmt:
40206 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40207 "used in compound statements", "omp barrier");
40208 break;
40209 default:
40210 goto bad_stmt;
40211 }
40212 break;
40213
40214 case PRAGMA_OMP_DEPOBJ:
40215 switch (context)
40216 {
40217 case pragma_compound:
40218 cp_parser_omp_depobj (parser, pragma_tok);
40219 return false;
40220 case pragma_stmt:
40221 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40222 "used in compound statements", "omp depobj");
40223 break;
40224 default:
40225 goto bad_stmt;
40226 }
40227 break;
40228
40229 case PRAGMA_OMP_FLUSH:
40230 switch (context)
40231 {
40232 case pragma_compound:
40233 cp_parser_omp_flush (parser, pragma_tok);
40234 return false;
40235 case pragma_stmt:
40236 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40237 "used in compound statements", "omp flush");
40238 break;
40239 default:
40240 goto bad_stmt;
40241 }
40242 break;
40243
40244 case PRAGMA_OMP_TASKWAIT:
40245 switch (context)
40246 {
40247 case pragma_compound:
40248 cp_parser_omp_taskwait (parser, pragma_tok);
40249 return false;
40250 case pragma_stmt:
40251 error_at (pragma_tok->location,
40252 "%<#pragma %s%> may only be used in compound statements",
40253 "omp taskwait");
40254 break;
40255 default:
40256 goto bad_stmt;
40257 }
40258 break;
40259
40260 case PRAGMA_OMP_TASKYIELD:
40261 switch (context)
40262 {
40263 case pragma_compound:
40264 cp_parser_omp_taskyield (parser, pragma_tok);
40265 return false;
40266 case pragma_stmt:
40267 error_at (pragma_tok->location,
40268 "%<#pragma %s%> may only be used in compound statements",
40269 "omp taskyield");
40270 break;
40271 default:
40272 goto bad_stmt;
40273 }
40274 break;
40275
40276 case PRAGMA_OMP_CANCEL:
40277 switch (context)
40278 {
40279 case pragma_compound:
40280 cp_parser_omp_cancel (parser, pragma_tok);
40281 return false;
40282 case pragma_stmt:
40283 error_at (pragma_tok->location,
40284 "%<#pragma %s%> may only be used in compound statements",
40285 "omp cancel");
40286 break;
40287 default:
40288 goto bad_stmt;
40289 }
40290 break;
40291
40292 case PRAGMA_OMP_CANCELLATION_POINT:
40293 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40294 return false;
40295
40296 case PRAGMA_OMP_THREADPRIVATE:
40297 cp_parser_omp_threadprivate (parser, pragma_tok);
40298 return false;
40299
40300 case PRAGMA_OMP_DECLARE:
40301 return cp_parser_omp_declare (parser, pragma_tok, context);
40302
40303 case PRAGMA_OACC_DECLARE:
40304 cp_parser_oacc_declare (parser, pragma_tok);
40305 return false;
40306
40307 case PRAGMA_OACC_ENTER_DATA:
40308 if (context == pragma_stmt)
40309 {
40310 error_at (pragma_tok->location,
40311 "%<#pragma %s%> may only be used in compound statements",
40312 "acc enter data");
40313 break;
40314 }
40315 else if (context != pragma_compound)
40316 goto bad_stmt;
40317 cp_parser_omp_construct (parser, pragma_tok, if_p);
40318 return true;
40319
40320 case PRAGMA_OACC_EXIT_DATA:
40321 if (context == pragma_stmt)
40322 {
40323 error_at (pragma_tok->location,
40324 "%<#pragma %s%> may only be used in compound statements",
40325 "acc exit data");
40326 break;
40327 }
40328 else if (context != pragma_compound)
40329 goto bad_stmt;
40330 cp_parser_omp_construct (parser, pragma_tok, if_p);
40331 return true;
40332
40333 case PRAGMA_OACC_ROUTINE:
40334 if (context != pragma_external)
40335 {
40336 error_at (pragma_tok->location,
40337 "%<#pragma acc routine%> must be at file scope");
40338 break;
40339 }
40340 cp_parser_oacc_routine (parser, pragma_tok, context);
40341 return false;
40342
40343 case PRAGMA_OACC_UPDATE:
40344 if (context == pragma_stmt)
40345 {
40346 error_at (pragma_tok->location,
40347 "%<#pragma %s%> may only be used in compound statements",
40348 "acc update");
40349 break;
40350 }
40351 else if (context != pragma_compound)
40352 goto bad_stmt;
40353 cp_parser_omp_construct (parser, pragma_tok, if_p);
40354 return true;
40355
40356 case PRAGMA_OACC_WAIT:
40357 if (context == pragma_stmt)
40358 {
40359 error_at (pragma_tok->location,
40360 "%<#pragma %s%> may only be used in compound statements",
40361 "acc wait");
40362 break;
40363 }
40364 else if (context != pragma_compound)
40365 goto bad_stmt;
40366 cp_parser_omp_construct (parser, pragma_tok, if_p);
40367 return true;
40368
40369 case PRAGMA_OACC_ATOMIC:
40370 case PRAGMA_OACC_CACHE:
40371 case PRAGMA_OACC_DATA:
40372 case PRAGMA_OACC_HOST_DATA:
40373 case PRAGMA_OACC_KERNELS:
40374 case PRAGMA_OACC_PARALLEL:
40375 case PRAGMA_OACC_LOOP:
40376 case PRAGMA_OMP_ATOMIC:
40377 case PRAGMA_OMP_CRITICAL:
40378 case PRAGMA_OMP_DISTRIBUTE:
40379 case PRAGMA_OMP_FOR:
40380 case PRAGMA_OMP_MASTER:
40381 case PRAGMA_OMP_PARALLEL:
40382 case PRAGMA_OMP_SECTIONS:
40383 case PRAGMA_OMP_SIMD:
40384 case PRAGMA_OMP_SINGLE:
40385 case PRAGMA_OMP_TASK:
40386 case PRAGMA_OMP_TASKGROUP:
40387 case PRAGMA_OMP_TASKLOOP:
40388 case PRAGMA_OMP_TEAMS:
40389 if (context != pragma_stmt && context != pragma_compound)
40390 goto bad_stmt;
40391 stmt = push_omp_privatization_clauses (false);
40392 cp_parser_omp_construct (parser, pragma_tok, if_p);
40393 pop_omp_privatization_clauses (stmt);
40394 return true;
40395
40396 case PRAGMA_OMP_REQUIRES:
40397 return cp_parser_omp_requires (parser, pragma_tok);
40398
40399 case PRAGMA_OMP_ORDERED:
40400 if (context != pragma_stmt && context != pragma_compound)
40401 goto bad_stmt;
40402 stmt = push_omp_privatization_clauses (false);
40403 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40404 pop_omp_privatization_clauses (stmt);
40405 return ret;
40406
40407 case PRAGMA_OMP_TARGET:
40408 if (context != pragma_stmt && context != pragma_compound)
40409 goto bad_stmt;
40410 stmt = push_omp_privatization_clauses (false);
40411 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40412 pop_omp_privatization_clauses (stmt);
40413 return ret;
40414
40415 case PRAGMA_OMP_END_DECLARE_TARGET:
40416 cp_parser_omp_end_declare_target (parser, pragma_tok);
40417 return false;
40418
40419 case PRAGMA_OMP_SECTION:
40420 error_at (pragma_tok->location,
40421 "%<#pragma omp section%> may only be used in "
40422 "%<#pragma omp sections%> construct");
40423 break;
40424
40425 case PRAGMA_IVDEP:
40426 {
40427 if (context == pragma_external)
40428 {
40429 error_at (pragma_tok->location,
40430 "%<#pragma GCC ivdep%> must be inside a function");
40431 break;
40432 }
40433 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40434 unsigned short unroll;
40435 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40436 if (tok->type == CPP_PRAGMA
40437 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40438 {
40439 tok = cp_lexer_consume_token (parser->lexer);
40440 unroll = cp_parser_pragma_unroll (parser, tok);
40441 tok = cp_lexer_peek_token (the_parser->lexer);
40442 }
40443 else
40444 unroll = 0;
40445 if (tok->type != CPP_KEYWORD
40446 || (tok->keyword != RID_FOR
40447 && tok->keyword != RID_WHILE
40448 && tok->keyword != RID_DO))
40449 {
40450 cp_parser_error (parser, "for, while or do statement expected");
40451 return false;
40452 }
40453 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40454 return true;
40455 }
40456
40457 case PRAGMA_UNROLL:
40458 {
40459 if (context == pragma_external)
40460 {
40461 error_at (pragma_tok->location,
40462 "%<#pragma GCC unroll%> must be inside a function");
40463 break;
40464 }
40465 const unsigned short unroll
40466 = cp_parser_pragma_unroll (parser, pragma_tok);
40467 bool ivdep;
40468 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40469 if (tok->type == CPP_PRAGMA
40470 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40471 {
40472 tok = cp_lexer_consume_token (parser->lexer);
40473 ivdep = cp_parser_pragma_ivdep (parser, tok);
40474 tok = cp_lexer_peek_token (the_parser->lexer);
40475 }
40476 else
40477 ivdep = false;
40478 if (tok->type != CPP_KEYWORD
40479 || (tok->keyword != RID_FOR
40480 && tok->keyword != RID_WHILE
40481 && tok->keyword != RID_DO))
40482 {
40483 cp_parser_error (parser, "for, while or do statement expected");
40484 return false;
40485 }
40486 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40487 return true;
40488 }
40489
40490 default:
40491 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
40492 c_invoke_pragma_handler (id);
40493 break;
40494
40495 bad_stmt:
40496 cp_parser_error (parser, "expected declaration specifiers");
40497 break;
40498 }
40499
40500 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40501 return false;
40502 }
40503
40504 /* The interface the pragma parsers have to the lexer. */
40505
40506 enum cpp_ttype
40507 pragma_lex (tree *value, location_t *loc)
40508 {
40509 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40510 enum cpp_ttype ret = tok->type;
40511
40512 *value = tok->u.value;
40513 if (loc)
40514 *loc = tok->location;
40515
40516 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
40517 ret = CPP_EOF;
40518 else if (ret == CPP_STRING)
40519 *value = cp_parser_string_literal (the_parser, false, false);
40520 else
40521 {
40522 if (ret == CPP_KEYWORD)
40523 ret = CPP_NAME;
40524 cp_lexer_consume_token (the_parser->lexer);
40525 }
40526
40527 return ret;
40528 }
40529
40530 \f
40531 /* External interface. */
40532
40533 /* Parse one entire translation unit. */
40534
40535 void
40536 c_parse_file (void)
40537 {
40538 static bool already_called = false;
40539
40540 if (already_called)
40541 fatal_error (input_location,
40542 "inter-module optimizations not implemented for C++");
40543 already_called = true;
40544
40545 the_parser = cp_parser_new ();
40546 push_deferring_access_checks (flag_access_control
40547 ? dk_no_deferred : dk_no_check);
40548 cp_parser_translation_unit (the_parser);
40549 the_parser = NULL;
40550
40551 finish_translation_unit ();
40552 }
40553
40554 /* Create an identifier for a generic parameter type (a synthesized
40555 template parameter implied by `auto' or a concept identifier). */
40556
40557 static GTY(()) int generic_parm_count;
40558 static tree
40559 make_generic_type_name ()
40560 {
40561 char buf[32];
40562 sprintf (buf, "auto:%d", ++generic_parm_count);
40563 return get_identifier (buf);
40564 }
40565
40566 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40567 (creating a new template parameter list if necessary). Returns the newly
40568 created template type parm. */
40569
40570 static tree
40571 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
40572 {
40573 gcc_assert (current_binding_level->kind == sk_function_parms);
40574
40575 /* Before committing to modifying any scope, if we're in an
40576 implicit template scope, and we're trying to synthesize a
40577 constrained parameter, try to find a previous parameter with
40578 the same name. This is the same-type rule for abbreviated
40579 function templates.
40580
40581 NOTE: We can generate implicit parameters when tentatively
40582 parsing a nested name specifier, only to reject that parse
40583 later. However, matching the same template-id as part of a
40584 direct-declarator should generate an identical template
40585 parameter, so this rule will merge them. */
40586 if (parser->implicit_template_scope && constr)
40587 {
40588 tree t = parser->implicit_template_parms;
40589 while (t)
40590 {
40591 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
40592 {
40593 tree d = TREE_VALUE (t);
40594 if (TREE_CODE (d) == PARM_DECL)
40595 /* Return the TEMPLATE_PARM_INDEX. */
40596 d = DECL_INITIAL (d);
40597 return d;
40598 }
40599 t = TREE_CHAIN (t);
40600 }
40601 }
40602
40603 /* We are either continuing a function template that already contains implicit
40604 template parameters, creating a new fully-implicit function template, or
40605 extending an existing explicit function template with implicit template
40606 parameters. */
40607
40608 cp_binding_level *const entry_scope = current_binding_level;
40609
40610 bool become_template = false;
40611 cp_binding_level *parent_scope = 0;
40612
40613 if (parser->implicit_template_scope)
40614 {
40615 gcc_assert (parser->implicit_template_parms);
40616
40617 current_binding_level = parser->implicit_template_scope;
40618 }
40619 else
40620 {
40621 /* Roll back to the existing template parameter scope (in the case of
40622 extending an explicit function template) or introduce a new template
40623 parameter scope ahead of the function parameter scope (or class scope
40624 in the case of out-of-line member definitions). The function scope is
40625 added back after template parameter synthesis below. */
40626
40627 cp_binding_level *scope = entry_scope;
40628
40629 while (scope->kind == sk_function_parms)
40630 {
40631 parent_scope = scope;
40632 scope = scope->level_chain;
40633 }
40634 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
40635 {
40636 /* If not defining a class, then any class scope is a scope level in
40637 an out-of-line member definition. In this case simply wind back
40638 beyond the first such scope to inject the template parameter list.
40639 Otherwise wind back to the class being defined. The latter can
40640 occur in class member friend declarations such as:
40641
40642 class A {
40643 void foo (auto);
40644 };
40645 class B {
40646 friend void A::foo (auto);
40647 };
40648
40649 The template parameter list synthesized for the friend declaration
40650 must be injected in the scope of 'B'. This can also occur in
40651 erroneous cases such as:
40652
40653 struct A {
40654 struct B {
40655 void foo (auto);
40656 };
40657 void B::foo (auto) {}
40658 };
40659
40660 Here the attempted definition of 'B::foo' within 'A' is ill-formed
40661 but, nevertheless, the template parameter list synthesized for the
40662 declarator should be injected into the scope of 'A' as if the
40663 ill-formed template was specified explicitly. */
40664
40665 while (scope->kind == sk_class && !scope->defining_class_p)
40666 {
40667 parent_scope = scope;
40668 scope = scope->level_chain;
40669 }
40670 }
40671
40672 current_binding_level = scope;
40673
40674 if (scope->kind != sk_template_parms
40675 || !function_being_declared_is_template_p (parser))
40676 {
40677 /* Introduce a new template parameter list for implicit template
40678 parameters. */
40679
40680 become_template = true;
40681
40682 parser->implicit_template_scope
40683 = begin_scope (sk_template_parms, NULL);
40684
40685 ++processing_template_decl;
40686
40687 parser->fully_implicit_function_template_p = true;
40688 ++parser->num_template_parameter_lists;
40689 }
40690 else
40691 {
40692 /* Synthesize implicit template parameters at the end of the explicit
40693 template parameter list. */
40694
40695 gcc_assert (current_template_parms);
40696
40697 parser->implicit_template_scope = scope;
40698
40699 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40700 parser->implicit_template_parms
40701 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
40702 }
40703 }
40704
40705 /* Synthesize a new template parameter and track the current template
40706 parameter chain with implicit_template_parms. */
40707
40708 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
40709 tree synth_id = make_generic_type_name ();
40710 tree synth_tmpl_parm;
40711 bool non_type = false;
40712
40713 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
40714 synth_tmpl_parm
40715 = finish_template_type_parm (class_type_node, synth_id);
40716 else if (TREE_CODE (proto) == TEMPLATE_DECL)
40717 synth_tmpl_parm
40718 = finish_constrained_template_template_parm (proto, synth_id);
40719 else
40720 {
40721 synth_tmpl_parm = copy_decl (proto);
40722 DECL_NAME (synth_tmpl_parm) = synth_id;
40723 non_type = true;
40724 }
40725
40726 // Attach the constraint to the parm before processing.
40727 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
40728 TREE_TYPE (node) = constr;
40729 tree new_parm
40730 = process_template_parm (parser->implicit_template_parms,
40731 input_location,
40732 node,
40733 /*non_type=*/non_type,
40734 /*param_pack=*/false);
40735
40736 // Chain the new parameter to the list of implicit parameters.
40737 if (parser->implicit_template_parms)
40738 parser->implicit_template_parms
40739 = TREE_CHAIN (parser->implicit_template_parms);
40740 else
40741 parser->implicit_template_parms = new_parm;
40742
40743 tree new_decl = get_local_decls ();
40744 if (non_type)
40745 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
40746 new_decl = DECL_INITIAL (new_decl);
40747
40748 /* If creating a fully implicit function template, start the new implicit
40749 template parameter list with this synthesized type, otherwise grow the
40750 current template parameter list. */
40751
40752 if (become_template)
40753 {
40754 parent_scope->level_chain = current_binding_level;
40755
40756 tree new_parms = make_tree_vec (1);
40757 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
40758 current_template_parms = tree_cons (size_int (processing_template_decl),
40759 new_parms, current_template_parms);
40760 }
40761 else
40762 {
40763 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40764 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
40765 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
40766 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
40767 }
40768
40769 // If the new parameter was constrained, we need to add that to the
40770 // constraints in the template parameter list.
40771 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
40772 {
40773 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
40774 reqs = conjoin_constraints (reqs, req);
40775 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
40776 }
40777
40778 current_binding_level = entry_scope;
40779
40780 return new_decl;
40781 }
40782
40783 /* Finish the declaration of a fully implicit function template. Such a
40784 template has no explicit template parameter list so has not been through the
40785 normal template head and tail processing. synthesize_implicit_template_parm
40786 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
40787 provided if the declaration is a class member such that its template
40788 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
40789 form is returned. Otherwise NULL_TREE is returned. */
40790
40791 static tree
40792 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
40793 {
40794 gcc_assert (parser->fully_implicit_function_template_p);
40795
40796 if (member_decl_opt && member_decl_opt != error_mark_node
40797 && DECL_VIRTUAL_P (member_decl_opt))
40798 {
40799 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
40800 "implicit templates may not be %<virtual%>");
40801 DECL_VIRTUAL_P (member_decl_opt) = false;
40802 }
40803
40804 if (member_decl_opt)
40805 member_decl_opt = finish_member_template_decl (member_decl_opt);
40806 end_template_decl ();
40807
40808 parser->fully_implicit_function_template_p = false;
40809 parser->implicit_template_parms = 0;
40810 parser->implicit_template_scope = 0;
40811 --parser->num_template_parameter_lists;
40812
40813 return member_decl_opt;
40814 }
40815
40816 /* Like finish_fully_implicit_template, but to be used in error
40817 recovery, rearranging scopes so that we restore the state we had
40818 before synthesize_implicit_template_parm inserted the implement
40819 template parms scope. */
40820
40821 static void
40822 abort_fully_implicit_template (cp_parser *parser)
40823 {
40824 cp_binding_level *return_to_scope = current_binding_level;
40825
40826 if (parser->implicit_template_scope
40827 && return_to_scope != parser->implicit_template_scope)
40828 {
40829 cp_binding_level *child = return_to_scope;
40830 for (cp_binding_level *scope = child->level_chain;
40831 scope != parser->implicit_template_scope;
40832 scope = child->level_chain)
40833 child = scope;
40834 child->level_chain = parser->implicit_template_scope->level_chain;
40835 parser->implicit_template_scope->level_chain = return_to_scope;
40836 current_binding_level = parser->implicit_template_scope;
40837 }
40838 else
40839 return_to_scope = return_to_scope->level_chain;
40840
40841 finish_fully_implicit_template (parser, NULL);
40842
40843 gcc_assert (current_binding_level == return_to_scope);
40844 }
40845
40846 /* Helper function for diagnostics that have complained about things
40847 being used with 'extern "C"' linkage.
40848
40849 Attempt to issue a note showing where the 'extern "C"' linkage began. */
40850
40851 void
40852 maybe_show_extern_c_location (void)
40853 {
40854 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
40855 inform (the_parser->innermost_linkage_specification_location,
40856 "%<extern \"C\"%> linkage started here");
40857 }
40858
40859 #include "gt-cp-parser.h"