]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
[C++] Use existing local variable in cp_parser_oacc_enter_exit_data
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2018 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48
49 \f
50 /* The lexer. */
51
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
54
55 static cp_token eof_token =
56 {
57 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 };
59
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62 NIC_NONE,
63 /* floating-point literal */
64 NIC_FLOAT,
65 /* %<this%> */
66 NIC_THIS,
67 /* %<__FUNCTION__%> */
68 NIC_FUNC_NAME,
69 /* %<__PRETTY_FUNCTION__%> */
70 NIC_PRETTY_FUNC,
71 /* %<__func__%> */
72 NIC_C99_FUNC,
73 /* "%<va_arg%> */
74 NIC_VA_ARG,
75 /* a cast */
76 NIC_CAST,
77 /* %<typeid%> operator */
78 NIC_TYPEID,
79 /* non-constant compound literals */
80 NIC_NCC,
81 /* a function call */
82 NIC_FUNC_CALL,
83 /* an increment */
84 NIC_INC,
85 /* an decrement */
86 NIC_DEC,
87 /* an array reference */
88 NIC_ARRAY_REF,
89 /* %<->%> */
90 NIC_ARROW,
91 /* %<.%> */
92 NIC_POINT,
93 /* the address of a label */
94 NIC_ADDR_LABEL,
95 /* %<*%> */
96 NIC_STAR,
97 /* %<&%> */
98 NIC_ADDR,
99 /* %<++%> */
100 NIC_PREINCREMENT,
101 /* %<--%> */
102 NIC_PREDECREMENT,
103 /* %<new%> */
104 NIC_NEW,
105 /* %<delete%> */
106 NIC_DEL,
107 /* calls to overloaded operators */
108 NIC_OVERLOADED,
109 /* an assignment */
110 NIC_ASSIGNMENT,
111 /* a comma operator */
112 NIC_COMMA,
113 /* a call to a constructor */
114 NIC_CONSTRUCTOR,
115 /* a transaction expression */
116 NIC_TRANSACTION
117 };
118
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121 /* NULL */
122 NLE_NULL,
123 /* is not a type */
124 NLE_TYPE,
125 /* is not a class or namespace */
126 NLE_CXX98,
127 /* is not a class, namespace, or enumeration */
128 NLE_NOT_CXX98
129 };
130
131 /* The various kinds of required token */
132 enum required_token {
133 RT_NONE,
134 RT_SEMICOLON, /* ';' */
135 RT_OPEN_PAREN, /* '(' */
136 RT_CLOSE_BRACE, /* '}' */
137 RT_OPEN_BRACE, /* '{' */
138 RT_CLOSE_SQUARE, /* ']' */
139 RT_OPEN_SQUARE, /* '[' */
140 RT_COMMA, /* ',' */
141 RT_SCOPE, /* '::' */
142 RT_LESS, /* '<' */
143 RT_GREATER, /* '>' */
144 RT_EQ, /* '=' */
145 RT_ELLIPSIS, /* '...' */
146 RT_MULT, /* '*' */
147 RT_COMPL, /* '~' */
148 RT_COLON, /* ':' */
149 RT_COLON_SCOPE, /* ':' or '::' */
150 RT_CLOSE_PAREN, /* ')' */
151 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152 RT_PRAGMA_EOL, /* end of line */
153 RT_NAME, /* identifier */
154
155 /* The type is CPP_KEYWORD */
156 RT_NEW, /* new */
157 RT_DELETE, /* delete */
158 RT_RETURN, /* return */
159 RT_WHILE, /* while */
160 RT_EXTERN, /* extern */
161 RT_STATIC_ASSERT, /* static_assert */
162 RT_DECLTYPE, /* decltype */
163 RT_OPERATOR, /* operator */
164 RT_CLASS, /* class */
165 RT_TEMPLATE, /* template */
166 RT_NAMESPACE, /* namespace */
167 RT_USING, /* using */
168 RT_ASM, /* asm */
169 RT_TRY, /* try */
170 RT_CATCH, /* catch */
171 RT_THROW, /* throw */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
176
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 };
186
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
189
190 class type_id_in_expr_sentinel
191 {
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
201 };
202
203 /* Prototypes. */
204
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
247
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
250
251 static void cp_parser_initial_pragma
252 (cp_token *);
253
254 static bool cp_parser_omp_declare_reduction_exprs
255 (tree, cp_parser *);
256 static void cp_finalize_oacc_routine
257 (cp_parser *, tree, bool);
258
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
262
263 /* Variables. */
264
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream;
267
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand;
271
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
277
278 static void
279 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
280 cp_token *start_token, unsigned num,
281 cp_token *curr_token)
282 {
283 unsigned i, nprinted;
284 cp_token *token;
285 bool do_print;
286
287 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288
289 if (buffer == NULL)
290 return;
291
292 if (num == 0)
293 num = buffer->length ();
294
295 if (start_token == NULL)
296 start_token = buffer->address ();
297
298 if (start_token > buffer->address ())
299 {
300 cp_lexer_print_token (file, &(*buffer)[0]);
301 fprintf (file, " ... ");
302 }
303
304 do_print = false;
305 nprinted = 0;
306 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 {
308 if (token == start_token)
309 do_print = true;
310
311 if (!do_print)
312 continue;
313
314 nprinted++;
315 if (token == curr_token)
316 fprintf (file, "[[");
317
318 cp_lexer_print_token (file, token);
319
320 if (token == curr_token)
321 fprintf (file, "]]");
322
323 switch (token->type)
324 {
325 case CPP_SEMICOLON:
326 case CPP_OPEN_BRACE:
327 case CPP_CLOSE_BRACE:
328 case CPP_EOF:
329 fputc ('\n', file);
330 break;
331
332 default:
333 fputc (' ', file);
334 }
335 }
336
337 if (i == num && i < buffer->length ())
338 {
339 fprintf (file, " ... ");
340 cp_lexer_print_token (file, &buffer->last ());
341 }
342
343 fprintf (file, "\n");
344 }
345
346
347 /* Dump all tokens in BUFFER to stderr. */
348
349 void
350 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 {
352 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
353 }
354
355 DEBUG_FUNCTION void
356 debug (vec<cp_token, va_gc> &ref)
357 {
358 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
359 }
360
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> *ptr)
363 {
364 if (ptr)
365 debug (*ptr);
366 else
367 fprintf (stderr, "<nil>\n");
368 }
369
370
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
373
374 static void
375 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 {
377 if (t)
378 {
379 fprintf (file, "%s: ", desc);
380 print_node_brief (file, "", t, 0);
381 }
382 }
383
384
385 /* Dump parser context C to FILE. */
386
387 static void
388 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 {
390 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
392 print_node_brief (file, "", c->object_type, 0);
393 fprintf (file, "}\n");
394 }
395
396
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398
399 static void
400 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 {
402 unsigned i;
403 cp_parser_context *c;
404
405 fprintf (file, "Parsing context stack:\n");
406 for (i = 0, c = first; c; c = c->next, i++)
407 {
408 fprintf (file, "\t#%u: ", i);
409 cp_debug_print_context (file, c);
410 }
411 }
412
413
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415
416 static void
417 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 {
419 if (flag)
420 fprintf (file, "%s: true\n", desc);
421 }
422
423
424 /* Print an unparsed function entry UF to FILE. */
425
426 static void
427 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 {
429 unsigned i;
430 cp_default_arg_entry *default_arg_fn;
431 tree fn;
432
433 fprintf (file, "\tFunctions with default args:\n");
434 for (i = 0;
435 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
436 i++)
437 {
438 fprintf (file, "\t\tClass type: ");
439 print_node_brief (file, "", default_arg_fn->class_type, 0);
440 fprintf (file, "\t\tDeclaration: ");
441 print_node_brief (file, "", default_arg_fn->decl, 0);
442 fprintf (file, "\n");
443 }
444
445 fprintf (file, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 {
449 print_node_brief (file, "", fn, 0);
450 fprintf (file, " ");
451 }
452 fprintf (file, "\n");
453
454 fprintf (file, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 {
458 print_node_brief (file, "", fn, 0);
459 fprintf (file, " ");
460 }
461 fprintf (file, "\n");
462 }
463
464
465 /* Print the stack of unparsed member functions S to FILE. */
466
467 static void
468 cp_debug_print_unparsed_queues (FILE *file,
469 vec<cp_unparsed_functions_entry, va_gc> *s)
470 {
471 unsigned i;
472 cp_unparsed_functions_entry *uf;
473
474 fprintf (file, "Unparsed functions\n");
475 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 {
477 fprintf (file, "#%u:\n", i);
478 cp_debug_print_unparsed_function (file, uf);
479 }
480 }
481
482
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485
486 static void
487 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 {
489 cp_token *next_token, *first_token, *start_token;
490
491 if (file == NULL)
492 file = stderr;
493
494 next_token = parser->lexer->next_token;
495 first_token = parser->lexer->buffer->address ();
496 start_token = (next_token > first_token + window_size / 2)
497 ? next_token - window_size / 2
498 : first_token;
499 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
500 next_token);
501 }
502
503
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
506
507 void
508 cp_debug_parser (FILE *file, cp_parser *parser)
509 {
510 const size_t window_size = 20;
511 cp_token *token;
512 expanded_location eloc;
513
514 if (file == NULL)
515 file = stderr;
516
517 fprintf (file, "Parser state\n\n");
518 fprintf (file, "Number of tokens: %u\n",
519 vec_safe_length (parser->lexer->buffer));
520 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
521 cp_debug_print_tree_if_set (file, "Object scope",
522 parser->object_scope);
523 cp_debug_print_tree_if_set (file, "Qualifying scope",
524 parser->qualifying_scope);
525 cp_debug_print_context_stack (file, parser->context);
526 cp_debug_print_flag (file, "Allow GNU extensions",
527 parser->allow_gnu_extensions_p);
528 cp_debug_print_flag (file, "'>' token is greater-than",
529 parser->greater_than_is_operator_p);
530 cp_debug_print_flag (file, "Default args allowed in current "
531 "parameter list", parser->default_arg_ok_p);
532 cp_debug_print_flag (file, "Parsing integral constant-expression",
533 parser->integral_constant_expression_p);
534 cp_debug_print_flag (file, "Allow non-constant expression in current "
535 "constant-expression",
536 parser->allow_non_integral_constant_expression_p);
537 cp_debug_print_flag (file, "Seen non-constant expression",
538 parser->non_integral_constant_expression_p);
539 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
540 "current context",
541 parser->local_variables_forbidden_p);
542 cp_debug_print_flag (file, "In unbraced linkage specification",
543 parser->in_unbraced_linkage_specification_p);
544 cp_debug_print_flag (file, "Parsing a declarator",
545 parser->in_declarator_p);
546 cp_debug_print_flag (file, "In template argument list",
547 parser->in_template_argument_list_p);
548 cp_debug_print_flag (file, "Parsing an iteration statement",
549 parser->in_statement & IN_ITERATION_STMT);
550 cp_debug_print_flag (file, "Parsing a switch statement",
551 parser->in_statement & IN_SWITCH_STMT);
552 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
553 parser->in_statement & IN_OMP_BLOCK);
554 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
555 parser->in_statement & IN_OMP_FOR);
556 cp_debug_print_flag (file, "Parsing an if statement",
557 parser->in_statement & IN_IF_STMT);
558 cp_debug_print_flag (file, "Parsing a type-id in an expression "
559 "context", parser->in_type_id_in_expr_p);
560 cp_debug_print_flag (file, "String expressions should be translated "
561 "to execution character set",
562 parser->translate_strings_p);
563 cp_debug_print_flag (file, "Parsing function body outside of a "
564 "local class", parser->in_function_body);
565 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
566 parser->colon_corrects_to_scope_p);
567 cp_debug_print_flag (file, "Colon doesn't start a class definition",
568 parser->colon_doesnt_start_class_def_p);
569 if (parser->type_definition_forbidden_message)
570 fprintf (file, "Error message for forbidden type definitions: %s\n",
571 parser->type_definition_forbidden_message);
572 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
573 fprintf (file, "Number of class definitions in progress: %u\n",
574 parser->num_classes_being_defined);
575 fprintf (file, "Number of template parameter lists for the current "
576 "declaration: %u\n", parser->num_template_parameter_lists);
577 cp_debug_parser_tokens (file, parser, window_size);
578 token = parser->lexer->next_token;
579 fprintf (file, "Next token to parse:\n");
580 fprintf (file, "\tToken: ");
581 cp_lexer_print_token (file, token);
582 eloc = expand_location (token->location);
583 fprintf (file, "\n\tFile: %s\n", eloc.file);
584 fprintf (file, "\tLine: %d\n", eloc.line);
585 fprintf (file, "\tColumn: %d\n", eloc.column);
586 }
587
588 DEBUG_FUNCTION void
589 debug (cp_parser &ref)
590 {
591 cp_debug_parser (stderr, &ref);
592 }
593
594 DEBUG_FUNCTION void
595 debug (cp_parser *ptr)
596 {
597 if (ptr)
598 debug (*ptr);
599 else
600 fprintf (stderr, "<nil>\n");
601 }
602
603 /* Allocate memory for a new lexer object and return it. */
604
605 static cp_lexer *
606 cp_lexer_alloc (void)
607 {
608 cp_lexer *lexer;
609
610 c_common_no_more_pch ();
611
612 /* Allocate the memory. */
613 lexer = ggc_cleared_alloc<cp_lexer> ();
614
615 /* Initially we are not debugging. */
616 lexer->debugging_p = false;
617
618 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
619
620 /* Create the buffer. */
621 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
622
623 return lexer;
624 }
625
626
627 /* Create a new main C++ lexer, the lexer that gets tokens from the
628 preprocessor. */
629
630 static cp_lexer *
631 cp_lexer_new_main (void)
632 {
633 cp_lexer *lexer;
634 cp_token token;
635
636 /* It's possible that parsing the first pragma will load a PCH file,
637 which is a GC collection point. So we have to do that before
638 allocating any memory. */
639 cp_parser_initial_pragma (&token);
640
641 lexer = cp_lexer_alloc ();
642
643 /* Put the first token in the buffer. */
644 lexer->buffer->quick_push (token);
645
646 /* Get the remaining tokens from the preprocessor. */
647 while (token.type != CPP_EOF)
648 {
649 cp_lexer_get_preprocessor_token (lexer, &token);
650 vec_safe_push (lexer->buffer, token);
651 }
652
653 lexer->last_token = lexer->buffer->address ()
654 + lexer->buffer->length ()
655 - 1;
656 lexer->next_token = lexer->buffer->length ()
657 ? lexer->buffer->address ()
658 : &eof_token;
659
660 /* Subsequent preprocessor diagnostics should use compiler
661 diagnostic functions to get the compiler source location. */
662 done_lexing = true;
663
664 gcc_assert (!lexer->next_token->purged_p);
665 return lexer;
666 }
667
668 /* Create a new lexer whose token stream is primed with the tokens in
669 CACHE. When these tokens are exhausted, no new tokens will be read. */
670
671 static cp_lexer *
672 cp_lexer_new_from_tokens (cp_token_cache *cache)
673 {
674 cp_token *first = cache->first;
675 cp_token *last = cache->last;
676 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
677
678 /* We do not own the buffer. */
679 lexer->buffer = NULL;
680 lexer->next_token = first == last ? &eof_token : first;
681 lexer->last_token = last;
682
683 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
684
685 /* Initially we are not debugging. */
686 lexer->debugging_p = false;
687
688 gcc_assert (!lexer->next_token->purged_p);
689 return lexer;
690 }
691
692 /* Frees all resources associated with LEXER. */
693
694 static void
695 cp_lexer_destroy (cp_lexer *lexer)
696 {
697 vec_free (lexer->buffer);
698 lexer->saved_tokens.release ();
699 ggc_free (lexer);
700 }
701
702 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
703 be used. The point of this flag is to help the compiler to fold away calls
704 to cp_lexer_debugging_p within this source file at compile time, when the
705 lexer is not being debugged. */
706
707 #define LEXER_DEBUGGING_ENABLED_P false
708
709 /* Returns nonzero if debugging information should be output. */
710
711 static inline bool
712 cp_lexer_debugging_p (cp_lexer *lexer)
713 {
714 if (!LEXER_DEBUGGING_ENABLED_P)
715 return false;
716
717 return lexer->debugging_p;
718 }
719
720
721 static inline cp_token_position
722 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
723 {
724 gcc_assert (!previous_p || lexer->next_token != &eof_token);
725
726 return lexer->next_token - previous_p;
727 }
728
729 static inline cp_token *
730 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
731 {
732 return pos;
733 }
734
735 static inline void
736 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
737 {
738 lexer->next_token = cp_lexer_token_at (lexer, pos);
739 }
740
741 static inline cp_token_position
742 cp_lexer_previous_token_position (cp_lexer *lexer)
743 {
744 if (lexer->next_token == &eof_token)
745 return lexer->last_token - 1;
746 else
747 return cp_lexer_token_position (lexer, true);
748 }
749
750 static inline cp_token *
751 cp_lexer_previous_token (cp_lexer *lexer)
752 {
753 cp_token_position tp = cp_lexer_previous_token_position (lexer);
754
755 /* Skip past purged tokens. */
756 while (tp->purged_p)
757 {
758 gcc_assert (tp != vec_safe_address (lexer->buffer));
759 tp--;
760 }
761
762 return cp_lexer_token_at (lexer, tp);
763 }
764
765 /* nonzero if we are presently saving tokens. */
766
767 static inline int
768 cp_lexer_saving_tokens (const cp_lexer* lexer)
769 {
770 return lexer->saved_tokens.length () != 0;
771 }
772
773 /* Store the next token from the preprocessor in *TOKEN. Return true
774 if we reach EOF. If LEXER is NULL, assume we are handling an
775 initial #pragma pch_preprocess, and thus want the lexer to return
776 processed strings. */
777
778 static void
779 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
780 {
781 static int is_extern_c = 0;
782
783 /* Get a new token from the preprocessor. */
784 token->type
785 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
786 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
787 token->keyword = RID_MAX;
788 token->purged_p = false;
789 token->error_reported = false;
790
791 /* On some systems, some header files are surrounded by an
792 implicit extern "C" block. Set a flag in the token if it
793 comes from such a header. */
794 is_extern_c += pending_lang_change;
795 pending_lang_change = 0;
796 token->implicit_extern_c = is_extern_c > 0;
797
798 /* Check to see if this token is a keyword. */
799 if (token->type == CPP_NAME)
800 {
801 if (IDENTIFIER_KEYWORD_P (token->u.value))
802 {
803 /* Mark this token as a keyword. */
804 token->type = CPP_KEYWORD;
805 /* Record which keyword. */
806 token->keyword = C_RID_CODE (token->u.value);
807 }
808 else
809 {
810 if (warn_cxx11_compat
811 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
812 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
813 {
814 /* Warn about the C++0x keyword (but still treat it as
815 an identifier). */
816 warning (OPT_Wc__11_compat,
817 "identifier %qE is a keyword in C++11",
818 token->u.value);
819
820 /* Clear out the C_RID_CODE so we don't warn about this
821 particular identifier-turned-keyword again. */
822 C_SET_RID_CODE (token->u.value, RID_MAX);
823 }
824
825 token->keyword = RID_MAX;
826 }
827 }
828 else if (token->type == CPP_AT_NAME)
829 {
830 /* This only happens in Objective-C++; it must be a keyword. */
831 token->type = CPP_KEYWORD;
832 switch (C_RID_CODE (token->u.value))
833 {
834 /* Replace 'class' with '@class', 'private' with '@private',
835 etc. This prevents confusion with the C++ keyword
836 'class', and makes the tokens consistent with other
837 Objective-C 'AT' keywords. For example '@class' is
838 reported as RID_AT_CLASS which is consistent with
839 '@synchronized', which is reported as
840 RID_AT_SYNCHRONIZED.
841 */
842 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
843 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
844 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
845 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
846 case RID_THROW: token->keyword = RID_AT_THROW; break;
847 case RID_TRY: token->keyword = RID_AT_TRY; break;
848 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
849 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
850 default: token->keyword = C_RID_CODE (token->u.value);
851 }
852 }
853 }
854
855 /* Update the globals input_location and the input file stack from TOKEN. */
856 static inline void
857 cp_lexer_set_source_position_from_token (cp_token *token)
858 {
859 if (token->type != CPP_EOF)
860 {
861 input_location = token->location;
862 }
863 }
864
865 /* Update the globals input_location and the input file stack from LEXER. */
866 static inline void
867 cp_lexer_set_source_position (cp_lexer *lexer)
868 {
869 cp_token *token = cp_lexer_peek_token (lexer);
870 cp_lexer_set_source_position_from_token (token);
871 }
872
873 /* Return a pointer to the next token in the token stream, but do not
874 consume it. */
875
876 static inline cp_token *
877 cp_lexer_peek_token (cp_lexer *lexer)
878 {
879 if (cp_lexer_debugging_p (lexer))
880 {
881 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
882 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
883 putc ('\n', cp_lexer_debug_stream);
884 }
885 return lexer->next_token;
886 }
887
888 /* Return true if the next token has the indicated TYPE. */
889
890 static inline bool
891 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
892 {
893 return cp_lexer_peek_token (lexer)->type == type;
894 }
895
896 /* Return true if the next token does not have the indicated TYPE. */
897
898 static inline bool
899 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
900 {
901 return !cp_lexer_next_token_is (lexer, type);
902 }
903
904 /* Return true if the next token is the indicated KEYWORD. */
905
906 static inline bool
907 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
908 {
909 return cp_lexer_peek_token (lexer)->keyword == keyword;
910 }
911
912 static inline bool
913 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
914 {
915 return cp_lexer_peek_nth_token (lexer, n)->type == type;
916 }
917
918 static inline bool
919 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
920 {
921 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
922 }
923
924 /* Return true if KEYWORD can start a decl-specifier. */
925
926 bool
927 cp_keyword_starts_decl_specifier_p (enum rid keyword)
928 {
929 switch (keyword)
930 {
931 /* auto specifier: storage-class-specifier in C++,
932 simple-type-specifier in C++0x. */
933 case RID_AUTO:
934 /* Storage classes. */
935 case RID_REGISTER:
936 case RID_STATIC:
937 case RID_EXTERN:
938 case RID_MUTABLE:
939 case RID_THREAD:
940 /* Elaborated type specifiers. */
941 case RID_ENUM:
942 case RID_CLASS:
943 case RID_STRUCT:
944 case RID_UNION:
945 case RID_TYPENAME:
946 /* Simple type specifiers. */
947 case RID_CHAR:
948 case RID_CHAR16:
949 case RID_CHAR32:
950 case RID_WCHAR:
951 case RID_BOOL:
952 case RID_SHORT:
953 case RID_INT:
954 case RID_LONG:
955 case RID_SIGNED:
956 case RID_UNSIGNED:
957 case RID_FLOAT:
958 case RID_DOUBLE:
959 case RID_VOID:
960 /* GNU extensions. */
961 case RID_ATTRIBUTE:
962 case RID_TYPEOF:
963 /* C++0x extensions. */
964 case RID_DECLTYPE:
965 case RID_UNDERLYING_TYPE:
966 case RID_CONSTEXPR:
967 return true;
968
969 default:
970 if (keyword >= RID_FIRST_INT_N
971 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
972 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
973 return true;
974 return false;
975 }
976 }
977
978 /* Return true if the next token is a keyword for a decl-specifier. */
979
980 static bool
981 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
982 {
983 cp_token *token;
984
985 token = cp_lexer_peek_token (lexer);
986 return cp_keyword_starts_decl_specifier_p (token->keyword);
987 }
988
989 /* Returns TRUE iff the token T begins a decltype type. */
990
991 static bool
992 token_is_decltype (cp_token *t)
993 {
994 return (t->keyword == RID_DECLTYPE
995 || t->type == CPP_DECLTYPE);
996 }
997
998 /* Returns TRUE iff the next token begins a decltype type. */
999
1000 static bool
1001 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1002 {
1003 cp_token *t = cp_lexer_peek_token (lexer);
1004 return token_is_decltype (t);
1005 }
1006
1007 /* Called when processing a token with tree_check_value; perform or defer the
1008 associated checks and return the value. */
1009
1010 static tree
1011 saved_checks_value (struct tree_check *check_value)
1012 {
1013 /* Perform any access checks that were deferred. */
1014 vec<deferred_access_check, va_gc> *checks;
1015 deferred_access_check *chk;
1016 checks = check_value->checks;
1017 if (checks)
1018 {
1019 int i;
1020 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1021 perform_or_defer_access_check (chk->binfo,
1022 chk->decl,
1023 chk->diag_decl, tf_warning_or_error);
1024 }
1025 /* Return the stored value. */
1026 return check_value->value;
1027 }
1028
1029 /* Return a pointer to the Nth token in the token stream. If N is 1,
1030 then this is precisely equivalent to cp_lexer_peek_token (except
1031 that it is not inline). One would like to disallow that case, but
1032 there is one case (cp_parser_nth_token_starts_template_id) where
1033 the caller passes a variable for N and it might be 1. */
1034
1035 static cp_token *
1036 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1037 {
1038 cp_token *token;
1039
1040 /* N is 1-based, not zero-based. */
1041 gcc_assert (n > 0);
1042
1043 if (cp_lexer_debugging_p (lexer))
1044 fprintf (cp_lexer_debug_stream,
1045 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1046
1047 --n;
1048 token = lexer->next_token;
1049 gcc_assert (!n || token != &eof_token);
1050 while (n != 0)
1051 {
1052 ++token;
1053 if (token == lexer->last_token)
1054 {
1055 token = &eof_token;
1056 break;
1057 }
1058
1059 if (!token->purged_p)
1060 --n;
1061 }
1062
1063 if (cp_lexer_debugging_p (lexer))
1064 {
1065 cp_lexer_print_token (cp_lexer_debug_stream, token);
1066 putc ('\n', cp_lexer_debug_stream);
1067 }
1068
1069 return token;
1070 }
1071
1072 /* Return the next token, and advance the lexer's next_token pointer
1073 to point to the next non-purged token. */
1074
1075 static cp_token *
1076 cp_lexer_consume_token (cp_lexer* lexer)
1077 {
1078 cp_token *token = lexer->next_token;
1079
1080 gcc_assert (token != &eof_token);
1081 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1082
1083 do
1084 {
1085 lexer->next_token++;
1086 if (lexer->next_token == lexer->last_token)
1087 {
1088 lexer->next_token = &eof_token;
1089 break;
1090 }
1091
1092 }
1093 while (lexer->next_token->purged_p);
1094
1095 cp_lexer_set_source_position_from_token (token);
1096
1097 /* Provide debugging output. */
1098 if (cp_lexer_debugging_p (lexer))
1099 {
1100 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1101 cp_lexer_print_token (cp_lexer_debug_stream, token);
1102 putc ('\n', cp_lexer_debug_stream);
1103 }
1104
1105 return token;
1106 }
1107
1108 /* Permanently remove the next token from the token stream, and
1109 advance the next_token pointer to refer to the next non-purged
1110 token. */
1111
1112 static void
1113 cp_lexer_purge_token (cp_lexer *lexer)
1114 {
1115 cp_token *tok = lexer->next_token;
1116
1117 gcc_assert (tok != &eof_token);
1118 tok->purged_p = true;
1119 tok->location = UNKNOWN_LOCATION;
1120 tok->u.value = NULL_TREE;
1121 tok->keyword = RID_MAX;
1122
1123 do
1124 {
1125 tok++;
1126 if (tok == lexer->last_token)
1127 {
1128 tok = &eof_token;
1129 break;
1130 }
1131 }
1132 while (tok->purged_p);
1133 lexer->next_token = tok;
1134 }
1135
1136 /* Permanently remove all tokens after TOK, up to, but not
1137 including, the token that will be returned next by
1138 cp_lexer_peek_token. */
1139
1140 static void
1141 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1142 {
1143 cp_token *peek = lexer->next_token;
1144
1145 if (peek == &eof_token)
1146 peek = lexer->last_token;
1147
1148 gcc_assert (tok < peek);
1149
1150 for ( tok += 1; tok != peek; tok += 1)
1151 {
1152 tok->purged_p = true;
1153 tok->location = UNKNOWN_LOCATION;
1154 tok->u.value = NULL_TREE;
1155 tok->keyword = RID_MAX;
1156 }
1157 }
1158
1159 /* Begin saving tokens. All tokens consumed after this point will be
1160 preserved. */
1161
1162 static void
1163 cp_lexer_save_tokens (cp_lexer* lexer)
1164 {
1165 /* Provide debugging output. */
1166 if (cp_lexer_debugging_p (lexer))
1167 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1168
1169 lexer->saved_tokens.safe_push (lexer->next_token);
1170 }
1171
1172 /* Commit to the portion of the token stream most recently saved. */
1173
1174 static void
1175 cp_lexer_commit_tokens (cp_lexer* lexer)
1176 {
1177 /* Provide debugging output. */
1178 if (cp_lexer_debugging_p (lexer))
1179 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1180
1181 lexer->saved_tokens.pop ();
1182 }
1183
1184 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1185 to the token stream. Stop saving tokens. */
1186
1187 static void
1188 cp_lexer_rollback_tokens (cp_lexer* lexer)
1189 {
1190 /* Provide debugging output. */
1191 if (cp_lexer_debugging_p (lexer))
1192 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1193
1194 lexer->next_token = lexer->saved_tokens.pop ();
1195 }
1196
1197 /* RAII wrapper around the above functions, with sanity checking. Creating
1198 a variable saves tokens, which are committed when the variable is
1199 destroyed unless they are explicitly rolled back by calling the rollback
1200 member function. */
1201
1202 struct saved_token_sentinel
1203 {
1204 cp_lexer *lexer;
1205 unsigned len;
1206 bool commit;
1207 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1208 {
1209 len = lexer->saved_tokens.length ();
1210 cp_lexer_save_tokens (lexer);
1211 }
1212 void rollback ()
1213 {
1214 cp_lexer_rollback_tokens (lexer);
1215 commit = false;
1216 }
1217 ~saved_token_sentinel()
1218 {
1219 if (commit)
1220 cp_lexer_commit_tokens (lexer);
1221 gcc_assert (lexer->saved_tokens.length () == len);
1222 }
1223 };
1224
1225 /* Print a representation of the TOKEN on the STREAM. */
1226
1227 static void
1228 cp_lexer_print_token (FILE * stream, cp_token *token)
1229 {
1230 /* We don't use cpp_type2name here because the parser defines
1231 a few tokens of its own. */
1232 static const char *const token_names[] = {
1233 /* cpplib-defined token types */
1234 #define OP(e, s) #e,
1235 #define TK(e, s) #e,
1236 TTYPE_TABLE
1237 #undef OP
1238 #undef TK
1239 /* C++ parser token types - see "Manifest constants", above. */
1240 "KEYWORD",
1241 "TEMPLATE_ID",
1242 "NESTED_NAME_SPECIFIER",
1243 };
1244
1245 /* For some tokens, print the associated data. */
1246 switch (token->type)
1247 {
1248 case CPP_KEYWORD:
1249 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1250 For example, `struct' is mapped to an INTEGER_CST. */
1251 if (!identifier_p (token->u.value))
1252 break;
1253 /* fall through */
1254 case CPP_NAME:
1255 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1256 break;
1257
1258 case CPP_STRING:
1259 case CPP_STRING16:
1260 case CPP_STRING32:
1261 case CPP_WSTRING:
1262 case CPP_UTF8STRING:
1263 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1264 break;
1265
1266 case CPP_NUMBER:
1267 print_generic_expr (stream, token->u.value);
1268 break;
1269
1270 default:
1271 /* If we have a name for the token, print it out. Otherwise, we
1272 simply give the numeric code. */
1273 if (token->type < ARRAY_SIZE(token_names))
1274 fputs (token_names[token->type], stream);
1275 else
1276 fprintf (stream, "[%d]", token->type);
1277 break;
1278 }
1279 }
1280
1281 DEBUG_FUNCTION void
1282 debug (cp_token &ref)
1283 {
1284 cp_lexer_print_token (stderr, &ref);
1285 fprintf (stderr, "\n");
1286 }
1287
1288 DEBUG_FUNCTION void
1289 debug (cp_token *ptr)
1290 {
1291 if (ptr)
1292 debug (*ptr);
1293 else
1294 fprintf (stderr, "<nil>\n");
1295 }
1296
1297
1298 /* Start emitting debugging information. */
1299
1300 static void
1301 cp_lexer_start_debugging (cp_lexer* lexer)
1302 {
1303 if (!LEXER_DEBUGGING_ENABLED_P)
1304 fatal_error (input_location,
1305 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1306
1307 lexer->debugging_p = true;
1308 cp_lexer_debug_stream = stderr;
1309 }
1310
1311 /* Stop emitting debugging information. */
1312
1313 static void
1314 cp_lexer_stop_debugging (cp_lexer* lexer)
1315 {
1316 if (!LEXER_DEBUGGING_ENABLED_P)
1317 fatal_error (input_location,
1318 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1319
1320 lexer->debugging_p = false;
1321 cp_lexer_debug_stream = NULL;
1322 }
1323
1324 /* Create a new cp_token_cache, representing a range of tokens. */
1325
1326 static cp_token_cache *
1327 cp_token_cache_new (cp_token *first, cp_token *last)
1328 {
1329 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1330 cache->first = first;
1331 cache->last = last;
1332 return cache;
1333 }
1334
1335 /* Diagnose if #pragma omp declare simd isn't followed immediately
1336 by function declaration or definition. */
1337
1338 static inline void
1339 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1340 {
1341 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1342 {
1343 error ("%<#pragma omp declare simd%> not immediately followed by "
1344 "function declaration or definition");
1345 parser->omp_declare_simd = NULL;
1346 }
1347 }
1348
1349 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1350 and put that into "omp declare simd" attribute. */
1351
1352 static inline void
1353 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1354 {
1355 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1356 {
1357 if (fndecl == error_mark_node)
1358 {
1359 parser->omp_declare_simd = NULL;
1360 return;
1361 }
1362 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1363 {
1364 cp_ensure_no_omp_declare_simd (parser);
1365 return;
1366 }
1367 }
1368 }
1369
1370 /* Diagnose if #pragma acc routine isn't followed immediately by function
1371 declaration or definition. */
1372
1373 static inline void
1374 cp_ensure_no_oacc_routine (cp_parser *parser)
1375 {
1376 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1377 {
1378 error_at (parser->oacc_routine->loc,
1379 "%<#pragma acc routine%> not immediately followed by "
1380 "function declaration or definition");
1381 parser->oacc_routine = NULL;
1382 }
1383 }
1384 \f
1385 /* Decl-specifiers. */
1386
1387 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1388
1389 static void
1390 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1391 {
1392 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1393 }
1394
1395 /* Declarators. */
1396
1397 /* Nothing other than the parser should be creating declarators;
1398 declarators are a semi-syntactic representation of C++ entities.
1399 Other parts of the front end that need to create entities (like
1400 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1401
1402 static cp_declarator *make_call_declarator
1403 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1404 static cp_declarator *make_array_declarator
1405 (cp_declarator *, tree);
1406 static cp_declarator *make_pointer_declarator
1407 (cp_cv_quals, cp_declarator *, tree);
1408 static cp_declarator *make_reference_declarator
1409 (cp_cv_quals, cp_declarator *, bool, tree);
1410 static cp_declarator *make_ptrmem_declarator
1411 (cp_cv_quals, tree, cp_declarator *, tree);
1412
1413 /* An erroneous declarator. */
1414 static cp_declarator *cp_error_declarator;
1415
1416 /* The obstack on which declarators and related data structures are
1417 allocated. */
1418 static struct obstack declarator_obstack;
1419
1420 /* Alloc BYTES from the declarator memory pool. */
1421
1422 static inline void *
1423 alloc_declarator (size_t bytes)
1424 {
1425 return obstack_alloc (&declarator_obstack, bytes);
1426 }
1427
1428 /* Allocate a declarator of the indicated KIND. Clear fields that are
1429 common to all declarators. */
1430
1431 static cp_declarator *
1432 make_declarator (cp_declarator_kind kind)
1433 {
1434 cp_declarator *declarator;
1435
1436 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1437 declarator->kind = kind;
1438 declarator->parenthesized = UNKNOWN_LOCATION;
1439 declarator->attributes = NULL_TREE;
1440 declarator->std_attributes = NULL_TREE;
1441 declarator->declarator = NULL;
1442 declarator->parameter_pack_p = false;
1443 declarator->id_loc = UNKNOWN_LOCATION;
1444
1445 return declarator;
1446 }
1447
1448 /* Make a declarator for a generalized identifier. If
1449 QUALIFYING_SCOPE is non-NULL, the identifier is
1450 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1451 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1452 is, if any. */
1453
1454 static cp_declarator *
1455 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1456 special_function_kind sfk, 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_has_attribute_expression
2052 (cp_parser *);
2053 static tree cp_parser_new_expression
2054 (cp_parser *);
2055 static vec<tree, va_gc> *cp_parser_new_placement
2056 (cp_parser *);
2057 static tree cp_parser_new_type_id
2058 (cp_parser *, tree *);
2059 static cp_declarator *cp_parser_new_declarator_opt
2060 (cp_parser *);
2061 static cp_declarator *cp_parser_direct_new_declarator
2062 (cp_parser *);
2063 static vec<tree, va_gc> *cp_parser_new_initializer
2064 (cp_parser *);
2065 static tree cp_parser_delete_expression
2066 (cp_parser *);
2067 static cp_expr cp_parser_cast_expression
2068 (cp_parser *, bool, bool, bool, cp_id_kind *);
2069 static cp_expr cp_parser_binary_expression
2070 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2071 static tree cp_parser_question_colon_clause
2072 (cp_parser *, cp_expr);
2073 static cp_expr cp_parser_assignment_expression
2074 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2075 static enum tree_code cp_parser_assignment_operator_opt
2076 (cp_parser *);
2077 static cp_expr cp_parser_expression
2078 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2079 static cp_expr cp_parser_constant_expression
2080 (cp_parser *, bool = false, bool * = NULL, bool = false);
2081 static cp_expr cp_parser_builtin_offsetof
2082 (cp_parser *);
2083 static cp_expr cp_parser_lambda_expression
2084 (cp_parser *);
2085 static void cp_parser_lambda_introducer
2086 (cp_parser *, tree);
2087 static bool cp_parser_lambda_declarator_opt
2088 (cp_parser *, tree);
2089 static void cp_parser_lambda_body
2090 (cp_parser *, tree);
2091
2092 /* Statements [gram.stmt.stmt] */
2093
2094 static void cp_parser_statement
2095 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2096 static void cp_parser_label_for_labeled_statement
2097 (cp_parser *, tree);
2098 static tree cp_parser_expression_statement
2099 (cp_parser *, tree);
2100 static tree cp_parser_compound_statement
2101 (cp_parser *, tree, int, bool);
2102 static void cp_parser_statement_seq_opt
2103 (cp_parser *, tree);
2104 static tree cp_parser_selection_statement
2105 (cp_parser *, bool *, vec<tree> *);
2106 static tree cp_parser_condition
2107 (cp_parser *);
2108 static tree cp_parser_iteration_statement
2109 (cp_parser *, bool *, bool, unsigned short);
2110 static bool cp_parser_init_statement
2111 (cp_parser *, tree *decl);
2112 static tree cp_parser_for
2113 (cp_parser *, bool, unsigned short);
2114 static tree cp_parser_c_for
2115 (cp_parser *, tree, tree, bool, unsigned short);
2116 static tree cp_parser_range_for
2117 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2118 static void do_range_for_auto_deduction
2119 (tree, tree);
2120 static tree cp_parser_perform_range_for_lookup
2121 (tree, tree *, tree *);
2122 static tree cp_parser_range_for_member_function
2123 (tree, tree);
2124 static tree cp_parser_jump_statement
2125 (cp_parser *);
2126 static void cp_parser_declaration_statement
2127 (cp_parser *);
2128
2129 static tree cp_parser_implicitly_scoped_statement
2130 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2131 static void cp_parser_already_scoped_statement
2132 (cp_parser *, bool *, const token_indent_info &);
2133
2134 /* Declarations [gram.dcl.dcl] */
2135
2136 static void cp_parser_declaration_seq_opt
2137 (cp_parser *);
2138 static void cp_parser_declaration
2139 (cp_parser *);
2140 static void cp_parser_toplevel_declaration
2141 (cp_parser *);
2142 static void cp_parser_block_declaration
2143 (cp_parser *, bool);
2144 static void cp_parser_simple_declaration
2145 (cp_parser *, bool, tree *);
2146 static void cp_parser_decl_specifier_seq
2147 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2148 static tree cp_parser_storage_class_specifier_opt
2149 (cp_parser *);
2150 static tree cp_parser_function_specifier_opt
2151 (cp_parser *, cp_decl_specifier_seq *);
2152 static tree cp_parser_type_specifier
2153 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2154 int *, bool *);
2155 static tree cp_parser_simple_type_specifier
2156 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2157 static tree cp_parser_type_name
2158 (cp_parser *, bool);
2159 static tree cp_parser_type_name
2160 (cp_parser *);
2161 static tree cp_parser_nonclass_name
2162 (cp_parser* parser);
2163 static tree cp_parser_elaborated_type_specifier
2164 (cp_parser *, bool, bool);
2165 static tree cp_parser_enum_specifier
2166 (cp_parser *);
2167 static void cp_parser_enumerator_list
2168 (cp_parser *, tree);
2169 static void cp_parser_enumerator_definition
2170 (cp_parser *, tree);
2171 static tree cp_parser_namespace_name
2172 (cp_parser *);
2173 static void cp_parser_namespace_definition
2174 (cp_parser *);
2175 static void cp_parser_namespace_body
2176 (cp_parser *);
2177 static tree cp_parser_qualified_namespace_specifier
2178 (cp_parser *);
2179 static void cp_parser_namespace_alias_definition
2180 (cp_parser *);
2181 static bool cp_parser_using_declaration
2182 (cp_parser *, bool);
2183 static void cp_parser_using_directive
2184 (cp_parser *);
2185 static tree cp_parser_alias_declaration
2186 (cp_parser *);
2187 static void cp_parser_asm_definition
2188 (cp_parser *);
2189 static void cp_parser_linkage_specification
2190 (cp_parser *);
2191 static void cp_parser_static_assert
2192 (cp_parser *, bool);
2193 static tree cp_parser_decltype
2194 (cp_parser *);
2195 static tree cp_parser_decomposition_declaration
2196 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2197
2198 /* Declarators [gram.dcl.decl] */
2199
2200 static tree cp_parser_init_declarator
2201 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2202 bool, bool, int, bool *, tree *, location_t *, tree *);
2203 static cp_declarator *cp_parser_declarator
2204 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2205 static cp_declarator *cp_parser_direct_declarator
2206 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2207 static enum tree_code cp_parser_ptr_operator
2208 (cp_parser *, tree *, cp_cv_quals *, tree *);
2209 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2210 (cp_parser *);
2211 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2212 (cp_parser *);
2213 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2214 (cp_parser *);
2215 static tree cp_parser_tx_qualifier_opt
2216 (cp_parser *);
2217 static tree cp_parser_late_return_type_opt
2218 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2219 static tree cp_parser_declarator_id
2220 (cp_parser *, bool);
2221 static tree cp_parser_type_id
2222 (cp_parser *, location_t * = NULL);
2223 static tree cp_parser_template_type_arg
2224 (cp_parser *);
2225 static tree cp_parser_trailing_type_id (cp_parser *);
2226 static tree cp_parser_type_id_1
2227 (cp_parser *, bool, bool, location_t *);
2228 static void cp_parser_type_specifier_seq
2229 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2230 static tree cp_parser_parameter_declaration_clause
2231 (cp_parser *);
2232 static tree cp_parser_parameter_declaration_list
2233 (cp_parser *);
2234 static cp_parameter_declarator *cp_parser_parameter_declaration
2235 (cp_parser *, bool, bool *);
2236 static tree cp_parser_default_argument
2237 (cp_parser *, bool);
2238 static void cp_parser_function_body
2239 (cp_parser *, bool);
2240 static tree cp_parser_initializer
2241 (cp_parser *, bool *, bool *, bool = false);
2242 static cp_expr cp_parser_initializer_clause
2243 (cp_parser *, bool *);
2244 static cp_expr cp_parser_braced_list
2245 (cp_parser*, bool*);
2246 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2247 (cp_parser *, bool *);
2248
2249 static void cp_parser_ctor_initializer_opt_and_function_body
2250 (cp_parser *, bool);
2251
2252 static tree cp_parser_late_parsing_omp_declare_simd
2253 (cp_parser *, tree);
2254
2255 static tree cp_parser_late_parsing_oacc_routine
2256 (cp_parser *, tree);
2257
2258 static tree synthesize_implicit_template_parm
2259 (cp_parser *, tree);
2260 static tree finish_fully_implicit_template
2261 (cp_parser *, tree);
2262 static void abort_fully_implicit_template
2263 (cp_parser *);
2264
2265 /* Classes [gram.class] */
2266
2267 static tree cp_parser_class_name
2268 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2269 static tree cp_parser_class_specifier
2270 (cp_parser *);
2271 static tree cp_parser_class_head
2272 (cp_parser *, bool *);
2273 static enum tag_types cp_parser_class_key
2274 (cp_parser *);
2275 static void cp_parser_type_parameter_key
2276 (cp_parser* parser);
2277 static void cp_parser_member_specification_opt
2278 (cp_parser *);
2279 static void cp_parser_member_declaration
2280 (cp_parser *);
2281 static tree cp_parser_pure_specifier
2282 (cp_parser *);
2283 static tree cp_parser_constant_initializer
2284 (cp_parser *);
2285
2286 /* Derived classes [gram.class.derived] */
2287
2288 static tree cp_parser_base_clause
2289 (cp_parser *);
2290 static tree cp_parser_base_specifier
2291 (cp_parser *);
2292
2293 /* Special member functions [gram.special] */
2294
2295 static tree cp_parser_conversion_function_id
2296 (cp_parser *);
2297 static tree cp_parser_conversion_type_id
2298 (cp_parser *);
2299 static cp_declarator *cp_parser_conversion_declarator_opt
2300 (cp_parser *);
2301 static void cp_parser_ctor_initializer_opt
2302 (cp_parser *);
2303 static void cp_parser_mem_initializer_list
2304 (cp_parser *);
2305 static tree cp_parser_mem_initializer
2306 (cp_parser *);
2307 static tree cp_parser_mem_initializer_id
2308 (cp_parser *);
2309
2310 /* Overloading [gram.over] */
2311
2312 static cp_expr cp_parser_operator_function_id
2313 (cp_parser *);
2314 static cp_expr cp_parser_operator
2315 (cp_parser *, location_t);
2316
2317 /* Templates [gram.temp] */
2318
2319 static void cp_parser_template_declaration
2320 (cp_parser *, bool);
2321 static tree cp_parser_template_parameter_list
2322 (cp_parser *);
2323 static tree cp_parser_template_parameter
2324 (cp_parser *, bool *, bool *);
2325 static tree cp_parser_type_parameter
2326 (cp_parser *, bool *);
2327 static tree cp_parser_template_id
2328 (cp_parser *, bool, bool, enum tag_types, bool);
2329 static tree cp_parser_template_name
2330 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2331 static tree cp_parser_template_argument_list
2332 (cp_parser *);
2333 static tree cp_parser_template_argument
2334 (cp_parser *);
2335 static void cp_parser_explicit_instantiation
2336 (cp_parser *);
2337 static void cp_parser_explicit_specialization
2338 (cp_parser *);
2339
2340 /* Exception handling [gram.exception] */
2341
2342 static tree cp_parser_try_block
2343 (cp_parser *);
2344 static void cp_parser_function_try_block
2345 (cp_parser *);
2346 static void cp_parser_handler_seq
2347 (cp_parser *);
2348 static void cp_parser_handler
2349 (cp_parser *);
2350 static tree cp_parser_exception_declaration
2351 (cp_parser *);
2352 static tree cp_parser_throw_expression
2353 (cp_parser *);
2354 static tree cp_parser_exception_specification_opt
2355 (cp_parser *);
2356 static tree cp_parser_type_id_list
2357 (cp_parser *);
2358
2359 /* GNU Extensions */
2360
2361 static tree cp_parser_asm_specification_opt
2362 (cp_parser *);
2363 static tree cp_parser_asm_operand_list
2364 (cp_parser *);
2365 static tree cp_parser_asm_clobber_list
2366 (cp_parser *);
2367 static tree cp_parser_asm_label_list
2368 (cp_parser *);
2369 static bool cp_next_tokens_can_be_attribute_p
2370 (cp_parser *);
2371 static bool cp_next_tokens_can_be_gnu_attribute_p
2372 (cp_parser *);
2373 static bool cp_next_tokens_can_be_std_attribute_p
2374 (cp_parser *);
2375 static bool cp_nth_tokens_can_be_std_attribute_p
2376 (cp_parser *, size_t);
2377 static bool cp_nth_tokens_can_be_gnu_attribute_p
2378 (cp_parser *, size_t);
2379 static bool cp_nth_tokens_can_be_attribute_p
2380 (cp_parser *, size_t);
2381 static tree cp_parser_attributes_opt
2382 (cp_parser *);
2383 static tree cp_parser_gnu_attributes_opt
2384 (cp_parser *);
2385 static tree cp_parser_gnu_attribute_list
2386 (cp_parser *, bool = false);
2387 static tree cp_parser_std_attribute
2388 (cp_parser *, tree);
2389 static tree cp_parser_std_attribute_spec
2390 (cp_parser *);
2391 static tree cp_parser_std_attribute_spec_seq
2392 (cp_parser *);
2393 static size_t cp_parser_skip_attributes_opt
2394 (cp_parser *, size_t);
2395 static bool cp_parser_extension_opt
2396 (cp_parser *, int *);
2397 static void cp_parser_label_declaration
2398 (cp_parser *);
2399
2400 /* Concept Extensions */
2401
2402 static tree cp_parser_requires_clause
2403 (cp_parser *);
2404 static tree cp_parser_requires_clause_opt
2405 (cp_parser *);
2406 static tree cp_parser_requires_expression
2407 (cp_parser *);
2408 static tree cp_parser_requirement_parameter_list
2409 (cp_parser *);
2410 static tree cp_parser_requirement_body
2411 (cp_parser *);
2412 static tree cp_parser_requirement_list
2413 (cp_parser *);
2414 static tree cp_parser_requirement
2415 (cp_parser *);
2416 static tree cp_parser_simple_requirement
2417 (cp_parser *);
2418 static tree cp_parser_compound_requirement
2419 (cp_parser *);
2420 static tree cp_parser_type_requirement
2421 (cp_parser *);
2422 static tree cp_parser_nested_requirement
2423 (cp_parser *);
2424
2425 /* Transactional Memory Extensions */
2426
2427 static tree cp_parser_transaction
2428 (cp_parser *, cp_token *);
2429 static tree cp_parser_transaction_expression
2430 (cp_parser *, enum rid);
2431 static void cp_parser_function_transaction
2432 (cp_parser *, enum rid);
2433 static tree cp_parser_transaction_cancel
2434 (cp_parser *);
2435
2436 enum pragma_context {
2437 pragma_external,
2438 pragma_member,
2439 pragma_objc_icode,
2440 pragma_stmt,
2441 pragma_compound
2442 };
2443 static bool cp_parser_pragma
2444 (cp_parser *, enum pragma_context, bool *);
2445
2446 /* Objective-C++ Productions */
2447
2448 static tree cp_parser_objc_message_receiver
2449 (cp_parser *);
2450 static tree cp_parser_objc_message_args
2451 (cp_parser *);
2452 static tree cp_parser_objc_message_expression
2453 (cp_parser *);
2454 static cp_expr cp_parser_objc_encode_expression
2455 (cp_parser *);
2456 static tree cp_parser_objc_defs_expression
2457 (cp_parser *);
2458 static tree cp_parser_objc_protocol_expression
2459 (cp_parser *);
2460 static tree cp_parser_objc_selector_expression
2461 (cp_parser *);
2462 static cp_expr cp_parser_objc_expression
2463 (cp_parser *);
2464 static bool cp_parser_objc_selector_p
2465 (enum cpp_ttype);
2466 static tree cp_parser_objc_selector
2467 (cp_parser *);
2468 static tree cp_parser_objc_protocol_refs_opt
2469 (cp_parser *);
2470 static void cp_parser_objc_declaration
2471 (cp_parser *, tree);
2472 static tree cp_parser_objc_statement
2473 (cp_parser *);
2474 static bool cp_parser_objc_valid_prefix_attributes
2475 (cp_parser *, tree *);
2476 static void cp_parser_objc_at_property_declaration
2477 (cp_parser *) ;
2478 static void cp_parser_objc_at_synthesize_declaration
2479 (cp_parser *) ;
2480 static void cp_parser_objc_at_dynamic_declaration
2481 (cp_parser *) ;
2482 static tree cp_parser_objc_struct_declaration
2483 (cp_parser *) ;
2484
2485 /* Utility Routines */
2486
2487 static cp_expr cp_parser_lookup_name
2488 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2489 static tree cp_parser_lookup_name_simple
2490 (cp_parser *, tree, location_t);
2491 static tree cp_parser_maybe_treat_template_as_class
2492 (tree, bool);
2493 static bool cp_parser_check_declarator_template_parameters
2494 (cp_parser *, cp_declarator *, location_t);
2495 static bool cp_parser_check_template_parameters
2496 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2497 static cp_expr cp_parser_simple_cast_expression
2498 (cp_parser *);
2499 static tree cp_parser_global_scope_opt
2500 (cp_parser *, bool);
2501 static bool cp_parser_constructor_declarator_p
2502 (cp_parser *, bool);
2503 static tree cp_parser_function_definition_from_specifiers_and_declarator
2504 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2505 static tree cp_parser_function_definition_after_declarator
2506 (cp_parser *, bool);
2507 static bool cp_parser_template_declaration_after_export
2508 (cp_parser *, bool);
2509 static void cp_parser_perform_template_parameter_access_checks
2510 (vec<deferred_access_check, va_gc> *);
2511 static tree cp_parser_single_declaration
2512 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2513 static cp_expr cp_parser_functional_cast
2514 (cp_parser *, tree);
2515 static tree cp_parser_save_member_function_body
2516 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2517 static tree cp_parser_save_nsdmi
2518 (cp_parser *);
2519 static tree cp_parser_enclosed_template_argument_list
2520 (cp_parser *);
2521 static void cp_parser_save_default_args
2522 (cp_parser *, tree);
2523 static void cp_parser_late_parsing_for_member
2524 (cp_parser *, tree);
2525 static tree cp_parser_late_parse_one_default_arg
2526 (cp_parser *, tree, tree, tree);
2527 static void cp_parser_late_parsing_nsdmi
2528 (cp_parser *, tree);
2529 static void cp_parser_late_parsing_default_args
2530 (cp_parser *, tree);
2531 static tree cp_parser_sizeof_operand
2532 (cp_parser *, enum rid);
2533 static cp_expr cp_parser_trait_expr
2534 (cp_parser *, enum rid);
2535 static bool cp_parser_declares_only_class_p
2536 (cp_parser *);
2537 static void cp_parser_set_storage_class
2538 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2539 static void cp_parser_set_decl_spec_type
2540 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2541 static void set_and_check_decl_spec_loc
2542 (cp_decl_specifier_seq *decl_specs,
2543 cp_decl_spec ds, cp_token *);
2544 static bool cp_parser_friend_p
2545 (const cp_decl_specifier_seq *);
2546 static void cp_parser_required_error
2547 (cp_parser *, required_token, bool, location_t);
2548 static cp_token *cp_parser_require
2549 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2550 static cp_token *cp_parser_require_keyword
2551 (cp_parser *, enum rid, required_token);
2552 static bool cp_parser_token_starts_function_definition_p
2553 (cp_token *);
2554 static bool cp_parser_next_token_starts_class_definition_p
2555 (cp_parser *);
2556 static bool cp_parser_next_token_ends_template_argument_p
2557 (cp_parser *);
2558 static bool cp_parser_nth_token_starts_template_argument_list_p
2559 (cp_parser *, size_t);
2560 static enum tag_types cp_parser_token_is_class_key
2561 (cp_token *);
2562 static enum tag_types cp_parser_token_is_type_parameter_key
2563 (cp_token *);
2564 static void cp_parser_check_class_key
2565 (enum tag_types, tree type);
2566 static void cp_parser_check_access_in_redeclaration
2567 (tree type, location_t location);
2568 static bool cp_parser_optional_template_keyword
2569 (cp_parser *);
2570 static void cp_parser_pre_parsed_nested_name_specifier
2571 (cp_parser *);
2572 static bool cp_parser_cache_group
2573 (cp_parser *, enum cpp_ttype, unsigned);
2574 static tree cp_parser_cache_defarg
2575 (cp_parser *parser, bool nsdmi);
2576 static void cp_parser_parse_tentatively
2577 (cp_parser *);
2578 static void cp_parser_commit_to_tentative_parse
2579 (cp_parser *);
2580 static void cp_parser_commit_to_topmost_tentative_parse
2581 (cp_parser *);
2582 static void cp_parser_abort_tentative_parse
2583 (cp_parser *);
2584 static bool cp_parser_parse_definitely
2585 (cp_parser *);
2586 static inline bool cp_parser_parsing_tentatively
2587 (cp_parser *);
2588 static bool cp_parser_uncommitted_to_tentative_parse_p
2589 (cp_parser *);
2590 static void cp_parser_error
2591 (cp_parser *, const char *);
2592 static void cp_parser_name_lookup_error
2593 (cp_parser *, tree, tree, name_lookup_error, location_t);
2594 static bool cp_parser_simulate_error
2595 (cp_parser *);
2596 static bool cp_parser_check_type_definition
2597 (cp_parser *);
2598 static void cp_parser_check_for_definition_in_return_type
2599 (cp_declarator *, tree, location_t type_location);
2600 static void cp_parser_check_for_invalid_template_id
2601 (cp_parser *, tree, enum tag_types, location_t location);
2602 static bool cp_parser_non_integral_constant_expression
2603 (cp_parser *, non_integral_constant);
2604 static void cp_parser_diagnose_invalid_type_name
2605 (cp_parser *, tree, location_t);
2606 static bool cp_parser_parse_and_diagnose_invalid_type_name
2607 (cp_parser *);
2608 static int cp_parser_skip_to_closing_parenthesis
2609 (cp_parser *, bool, bool, bool);
2610 static void cp_parser_skip_to_end_of_statement
2611 (cp_parser *);
2612 static void cp_parser_consume_semicolon_at_end_of_statement
2613 (cp_parser *);
2614 static void cp_parser_skip_to_end_of_block_or_statement
2615 (cp_parser *);
2616 static bool cp_parser_skip_to_closing_brace
2617 (cp_parser *);
2618 static void cp_parser_skip_to_end_of_template_parameter_list
2619 (cp_parser *);
2620 static void cp_parser_skip_to_pragma_eol
2621 (cp_parser*, cp_token *);
2622 static bool cp_parser_error_occurred
2623 (cp_parser *);
2624 static bool cp_parser_allow_gnu_extensions_p
2625 (cp_parser *);
2626 static bool cp_parser_is_pure_string_literal
2627 (cp_token *);
2628 static bool cp_parser_is_string_literal
2629 (cp_token *);
2630 static bool cp_parser_is_keyword
2631 (cp_token *, enum rid);
2632 static tree cp_parser_make_typename_type
2633 (cp_parser *, tree, location_t location);
2634 static cp_declarator * cp_parser_make_indirect_declarator
2635 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2636 static bool cp_parser_compound_literal_p
2637 (cp_parser *);
2638 static bool cp_parser_array_designator_p
2639 (cp_parser *);
2640 static bool cp_parser_init_statement_p
2641 (cp_parser *);
2642 static bool cp_parser_skip_to_closing_square_bracket
2643 (cp_parser *);
2644
2645 /* Concept-related syntactic transformations */
2646
2647 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2648 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2649
2650 // -------------------------------------------------------------------------- //
2651 // Unevaluated Operand Guard
2652 //
2653 // Implementation of an RAII helper for unevaluated operand parsing.
2654 cp_unevaluated::cp_unevaluated ()
2655 {
2656 ++cp_unevaluated_operand;
2657 ++c_inhibit_evaluation_warnings;
2658 }
2659
2660 cp_unevaluated::~cp_unevaluated ()
2661 {
2662 --c_inhibit_evaluation_warnings;
2663 --cp_unevaluated_operand;
2664 }
2665
2666 // -------------------------------------------------------------------------- //
2667 // Tentative Parsing
2668
2669 /* Returns nonzero if we are parsing tentatively. */
2670
2671 static inline bool
2672 cp_parser_parsing_tentatively (cp_parser* parser)
2673 {
2674 return parser->context->next != NULL;
2675 }
2676
2677 /* Returns nonzero if TOKEN is a string literal. */
2678
2679 static bool
2680 cp_parser_is_pure_string_literal (cp_token* token)
2681 {
2682 return (token->type == CPP_STRING ||
2683 token->type == CPP_STRING16 ||
2684 token->type == CPP_STRING32 ||
2685 token->type == CPP_WSTRING ||
2686 token->type == CPP_UTF8STRING);
2687 }
2688
2689 /* Returns nonzero if TOKEN is a string literal
2690 of a user-defined string literal. */
2691
2692 static bool
2693 cp_parser_is_string_literal (cp_token* token)
2694 {
2695 return (cp_parser_is_pure_string_literal (token) ||
2696 token->type == CPP_STRING_USERDEF ||
2697 token->type == CPP_STRING16_USERDEF ||
2698 token->type == CPP_STRING32_USERDEF ||
2699 token->type == CPP_WSTRING_USERDEF ||
2700 token->type == CPP_UTF8STRING_USERDEF);
2701 }
2702
2703 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2704
2705 static bool
2706 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2707 {
2708 return token->keyword == keyword;
2709 }
2710
2711 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2712 PRAGMA_NONE. */
2713
2714 static enum pragma_kind
2715 cp_parser_pragma_kind (cp_token *token)
2716 {
2717 if (token->type != CPP_PRAGMA)
2718 return PRAGMA_NONE;
2719 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2720 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2721 }
2722
2723 /* Helper function for cp_parser_error.
2724 Having peeked a token of kind TOK1_KIND that might signify
2725 a conflict marker, peek successor tokens to determine
2726 if we actually do have a conflict marker.
2727 Specifically, we consider a run of 7 '<', '=' or '>' characters
2728 at the start of a line as a conflict marker.
2729 These come through the lexer as three pairs and a single,
2730 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2731 If it returns true, *OUT_LOC is written to with the location/range
2732 of the marker. */
2733
2734 static bool
2735 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2736 location_t *out_loc)
2737 {
2738 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2739 if (token2->type != tok1_kind)
2740 return false;
2741 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2742 if (token3->type != tok1_kind)
2743 return false;
2744 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2745 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2746 return false;
2747
2748 /* It must be at the start of the line. */
2749 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2750 if (LOCATION_COLUMN (start_loc) != 1)
2751 return false;
2752
2753 /* We have a conflict marker. Construct a location of the form:
2754 <<<<<<<
2755 ^~~~~~~
2756 with start == caret, finishing at the end of the marker. */
2757 location_t finish_loc = get_finish (token4->location);
2758 *out_loc = make_location (start_loc, start_loc, finish_loc);
2759
2760 return true;
2761 }
2762
2763 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2764 RT_CLOSE_PAREN. */
2765
2766 static const char *
2767 get_matching_symbol (required_token token_desc)
2768 {
2769 switch (token_desc)
2770 {
2771 default:
2772 gcc_unreachable ();
2773 return "";
2774 case RT_CLOSE_BRACE:
2775 return "{";
2776 case RT_CLOSE_PAREN:
2777 return "(";
2778 }
2779 }
2780
2781 /* Attempt to convert TOKEN_DESC from a required_token to an
2782 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2783
2784 static enum cpp_ttype
2785 get_required_cpp_ttype (required_token token_desc)
2786 {
2787 switch (token_desc)
2788 {
2789 case RT_SEMICOLON:
2790 return CPP_SEMICOLON;
2791 case RT_OPEN_PAREN:
2792 return CPP_OPEN_PAREN;
2793 case RT_CLOSE_BRACE:
2794 return CPP_CLOSE_BRACE;
2795 case RT_OPEN_BRACE:
2796 return CPP_OPEN_BRACE;
2797 case RT_CLOSE_SQUARE:
2798 return CPP_CLOSE_SQUARE;
2799 case RT_OPEN_SQUARE:
2800 return CPP_OPEN_SQUARE;
2801 case RT_COMMA:
2802 return CPP_COMMA;
2803 case RT_COLON:
2804 return CPP_COLON;
2805 case RT_CLOSE_PAREN:
2806 return CPP_CLOSE_PAREN;
2807
2808 default:
2809 /* Use CPP_EOF as a "no completions possible" code. */
2810 return CPP_EOF;
2811 }
2812 }
2813
2814
2815 /* Subroutine of cp_parser_error and cp_parser_required_error.
2816
2817 Issue a diagnostic of the form
2818 FILE:LINE: MESSAGE before TOKEN
2819 where TOKEN is the next token in the input stream. MESSAGE
2820 (specified by the caller) is usually of the form "expected
2821 OTHER-TOKEN".
2822
2823 This bypasses the check for tentative passing, and potentially
2824 adds material needed by cp_parser_required_error.
2825
2826 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2827 suggesting insertion of the missing token.
2828
2829 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2830 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2831 location. */
2832
2833 static void
2834 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2835 required_token missing_token_desc,
2836 location_t matching_location)
2837 {
2838 cp_token *token = cp_lexer_peek_token (parser->lexer);
2839 /* This diagnostic makes more sense if it is tagged to the line
2840 of the token we just peeked at. */
2841 cp_lexer_set_source_position_from_token (token);
2842
2843 if (token->type == CPP_PRAGMA)
2844 {
2845 error_at (token->location,
2846 "%<#pragma%> is not allowed here");
2847 cp_parser_skip_to_pragma_eol (parser, token);
2848 return;
2849 }
2850
2851 /* If this is actually a conflict marker, report it as such. */
2852 if (token->type == CPP_LSHIFT
2853 || token->type == CPP_RSHIFT
2854 || token->type == CPP_EQ_EQ)
2855 {
2856 location_t loc;
2857 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2858 {
2859 error_at (loc, "version control conflict marker in file");
2860 expanded_location token_exploc = expand_location (token->location);
2861 /* Consume tokens until the end of the source line. */
2862 while (1)
2863 {
2864 cp_lexer_consume_token (parser->lexer);
2865 cp_token *next = cp_lexer_peek_token (parser->lexer);
2866 if (next == NULL)
2867 break;
2868 expanded_location next_exploc = expand_location (next->location);
2869 if (next_exploc.file != token_exploc.file)
2870 break;
2871 if (next_exploc.line != token_exploc.line)
2872 break;
2873 }
2874 return;
2875 }
2876 }
2877
2878 gcc_rich_location richloc (input_location);
2879
2880 bool added_matching_location = false;
2881
2882 if (missing_token_desc != RT_NONE)
2883 {
2884 /* Potentially supply a fix-it hint, suggesting to add the
2885 missing token immediately after the *previous* token.
2886 This may move the primary location within richloc. */
2887 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2888 location_t prev_token_loc
2889 = cp_lexer_previous_token (parser->lexer)->location;
2890 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2891
2892 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2893 Attempt to consolidate diagnostics by printing it as a
2894 secondary range within the main diagnostic. */
2895 if (matching_location != UNKNOWN_LOCATION)
2896 added_matching_location
2897 = richloc.add_location_if_nearby (matching_location);
2898 }
2899
2900 /* Actually emit the error. */
2901 c_parse_error (gmsgid,
2902 /* Because c_parser_error does not understand
2903 CPP_KEYWORD, keywords are treated like
2904 identifiers. */
2905 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2906 token->u.value, token->flags, &richloc);
2907
2908 if (missing_token_desc != RT_NONE)
2909 {
2910 /* If we weren't able to consolidate matching_location, then
2911 print it as a secondary diagnostic. */
2912 if (matching_location != UNKNOWN_LOCATION
2913 && !added_matching_location)
2914 inform (matching_location, "to match this %qs",
2915 get_matching_symbol (missing_token_desc));
2916 }
2917 }
2918
2919 /* If not parsing tentatively, issue a diagnostic of the form
2920 FILE:LINE: MESSAGE before TOKEN
2921 where TOKEN is the next token in the input stream. MESSAGE
2922 (specified by the caller) is usually of the form "expected
2923 OTHER-TOKEN". */
2924
2925 static void
2926 cp_parser_error (cp_parser* parser, const char* gmsgid)
2927 {
2928 if (!cp_parser_simulate_error (parser))
2929 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2930 }
2931
2932 /* Issue an error about name-lookup failing. NAME is the
2933 IDENTIFIER_NODE DECL is the result of
2934 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2935 the thing that we hoped to find. */
2936
2937 static void
2938 cp_parser_name_lookup_error (cp_parser* parser,
2939 tree name,
2940 tree decl,
2941 name_lookup_error desired,
2942 location_t location)
2943 {
2944 /* If name lookup completely failed, tell the user that NAME was not
2945 declared. */
2946 if (decl == error_mark_node)
2947 {
2948 if (parser->scope && parser->scope != global_namespace)
2949 error_at (location, "%<%E::%E%> has not been declared",
2950 parser->scope, name);
2951 else if (parser->scope == global_namespace)
2952 error_at (location, "%<::%E%> has not been declared", name);
2953 else if (parser->object_scope
2954 && !CLASS_TYPE_P (parser->object_scope))
2955 error_at (location, "request for member %qE in non-class type %qT",
2956 name, parser->object_scope);
2957 else if (parser->object_scope)
2958 error_at (location, "%<%T::%E%> has not been declared",
2959 parser->object_scope, name);
2960 else
2961 error_at (location, "%qE has not been declared", name);
2962 }
2963 else if (parser->scope && parser->scope != global_namespace)
2964 {
2965 switch (desired)
2966 {
2967 case NLE_TYPE:
2968 error_at (location, "%<%E::%E%> is not a type",
2969 parser->scope, name);
2970 break;
2971 case NLE_CXX98:
2972 error_at (location, "%<%E::%E%> is not a class or namespace",
2973 parser->scope, name);
2974 break;
2975 case NLE_NOT_CXX98:
2976 error_at (location,
2977 "%<%E::%E%> is not a class, namespace, or enumeration",
2978 parser->scope, name);
2979 break;
2980 default:
2981 gcc_unreachable ();
2982
2983 }
2984 }
2985 else if (parser->scope == global_namespace)
2986 {
2987 switch (desired)
2988 {
2989 case NLE_TYPE:
2990 error_at (location, "%<::%E%> is not a type", name);
2991 break;
2992 case NLE_CXX98:
2993 error_at (location, "%<::%E%> is not a class or namespace", name);
2994 break;
2995 case NLE_NOT_CXX98:
2996 error_at (location,
2997 "%<::%E%> is not a class, namespace, or enumeration",
2998 name);
2999 break;
3000 default:
3001 gcc_unreachable ();
3002 }
3003 }
3004 else
3005 {
3006 switch (desired)
3007 {
3008 case NLE_TYPE:
3009 error_at (location, "%qE is not a type", name);
3010 break;
3011 case NLE_CXX98:
3012 error_at (location, "%qE is not a class or namespace", name);
3013 break;
3014 case NLE_NOT_CXX98:
3015 error_at (location,
3016 "%qE is not a class, namespace, or enumeration", name);
3017 break;
3018 default:
3019 gcc_unreachable ();
3020 }
3021 }
3022 }
3023
3024 /* If we are parsing tentatively, remember that an error has occurred
3025 during this tentative parse. Returns true if the error was
3026 simulated; false if a message should be issued by the caller. */
3027
3028 static bool
3029 cp_parser_simulate_error (cp_parser* parser)
3030 {
3031 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3032 {
3033 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3034 return true;
3035 }
3036 return false;
3037 }
3038
3039 /* This function is called when a type is defined. If type
3040 definitions are forbidden at this point, an error message is
3041 issued. */
3042
3043 static bool
3044 cp_parser_check_type_definition (cp_parser* parser)
3045 {
3046 /* If types are forbidden here, issue a message. */
3047 if (parser->type_definition_forbidden_message)
3048 {
3049 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3050 in the message need to be interpreted. */
3051 error (parser->type_definition_forbidden_message);
3052 return false;
3053 }
3054 return true;
3055 }
3056
3057 /* This function is called when the DECLARATOR is processed. The TYPE
3058 was a type defined in the decl-specifiers. If it is invalid to
3059 define a type in the decl-specifiers for DECLARATOR, an error is
3060 issued. TYPE_LOCATION is the location of TYPE and is used
3061 for error reporting. */
3062
3063 static void
3064 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3065 tree type, location_t type_location)
3066 {
3067 /* [dcl.fct] forbids type definitions in return types.
3068 Unfortunately, it's not easy to know whether or not we are
3069 processing a return type until after the fact. */
3070 while (declarator
3071 && (declarator->kind == cdk_pointer
3072 || declarator->kind == cdk_reference
3073 || declarator->kind == cdk_ptrmem))
3074 declarator = declarator->declarator;
3075 if (declarator
3076 && declarator->kind == cdk_function)
3077 {
3078 error_at (type_location,
3079 "new types may not be defined in a return type");
3080 inform (type_location,
3081 "(perhaps a semicolon is missing after the definition of %qT)",
3082 type);
3083 }
3084 }
3085
3086 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3087 "<" in any valid C++ program. If the next token is indeed "<",
3088 issue a message warning the user about what appears to be an
3089 invalid attempt to form a template-id. LOCATION is the location
3090 of the type-specifier (TYPE) */
3091
3092 static void
3093 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3094 tree type,
3095 enum tag_types tag_type,
3096 location_t location)
3097 {
3098 cp_token_position start = 0;
3099
3100 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3101 {
3102 if (TREE_CODE (type) == TYPE_DECL)
3103 type = TREE_TYPE (type);
3104 if (TYPE_P (type) && !template_placeholder_p (type))
3105 error_at (location, "%qT is not a template", type);
3106 else if (identifier_p (type))
3107 {
3108 if (tag_type != none_type)
3109 error_at (location, "%qE is not a class template", type);
3110 else
3111 error_at (location, "%qE is not a template", type);
3112 }
3113 else
3114 error_at (location, "invalid template-id");
3115 /* Remember the location of the invalid "<". */
3116 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3117 start = cp_lexer_token_position (parser->lexer, true);
3118 /* Consume the "<". */
3119 cp_lexer_consume_token (parser->lexer);
3120 /* Parse the template arguments. */
3121 cp_parser_enclosed_template_argument_list (parser);
3122 /* Permanently remove the invalid template arguments so that
3123 this error message is not issued again. */
3124 if (start)
3125 cp_lexer_purge_tokens_after (parser->lexer, start);
3126 }
3127 }
3128
3129 /* If parsing an integral constant-expression, issue an error message
3130 about the fact that THING appeared and return true. Otherwise,
3131 return false. In either case, set
3132 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3133
3134 static bool
3135 cp_parser_non_integral_constant_expression (cp_parser *parser,
3136 non_integral_constant thing)
3137 {
3138 parser->non_integral_constant_expression_p = true;
3139 if (parser->integral_constant_expression_p)
3140 {
3141 if (!parser->allow_non_integral_constant_expression_p)
3142 {
3143 const char *msg = NULL;
3144 switch (thing)
3145 {
3146 case NIC_FLOAT:
3147 pedwarn (input_location, OPT_Wpedantic,
3148 "ISO C++ forbids using a floating-point literal "
3149 "in a constant-expression");
3150 return true;
3151 case NIC_CAST:
3152 error ("a cast to a type other than an integral or "
3153 "enumeration type cannot appear in a "
3154 "constant-expression");
3155 return true;
3156 case NIC_TYPEID:
3157 error ("%<typeid%> operator "
3158 "cannot appear in a constant-expression");
3159 return true;
3160 case NIC_NCC:
3161 error ("non-constant compound literals "
3162 "cannot appear in a constant-expression");
3163 return true;
3164 case NIC_FUNC_CALL:
3165 error ("a function call "
3166 "cannot appear in a constant-expression");
3167 return true;
3168 case NIC_INC:
3169 error ("an increment "
3170 "cannot appear in a constant-expression");
3171 return true;
3172 case NIC_DEC:
3173 error ("an decrement "
3174 "cannot appear in a constant-expression");
3175 return true;
3176 case NIC_ARRAY_REF:
3177 error ("an array reference "
3178 "cannot appear in a constant-expression");
3179 return true;
3180 case NIC_ADDR_LABEL:
3181 error ("the address of a label "
3182 "cannot appear in a constant-expression");
3183 return true;
3184 case NIC_OVERLOADED:
3185 error ("calls to overloaded operators "
3186 "cannot appear in a constant-expression");
3187 return true;
3188 case NIC_ASSIGNMENT:
3189 error ("an assignment cannot appear in a constant-expression");
3190 return true;
3191 case NIC_COMMA:
3192 error ("a comma operator "
3193 "cannot appear in a constant-expression");
3194 return true;
3195 case NIC_CONSTRUCTOR:
3196 error ("a call to a constructor "
3197 "cannot appear in a constant-expression");
3198 return true;
3199 case NIC_TRANSACTION:
3200 error ("a transaction expression "
3201 "cannot appear in a constant-expression");
3202 return true;
3203 case NIC_THIS:
3204 msg = "this";
3205 break;
3206 case NIC_FUNC_NAME:
3207 msg = "__FUNCTION__";
3208 break;
3209 case NIC_PRETTY_FUNC:
3210 msg = "__PRETTY_FUNCTION__";
3211 break;
3212 case NIC_C99_FUNC:
3213 msg = "__func__";
3214 break;
3215 case NIC_VA_ARG:
3216 msg = "va_arg";
3217 break;
3218 case NIC_ARROW:
3219 msg = "->";
3220 break;
3221 case NIC_POINT:
3222 msg = ".";
3223 break;
3224 case NIC_STAR:
3225 msg = "*";
3226 break;
3227 case NIC_ADDR:
3228 msg = "&";
3229 break;
3230 case NIC_PREINCREMENT:
3231 msg = "++";
3232 break;
3233 case NIC_PREDECREMENT:
3234 msg = "--";
3235 break;
3236 case NIC_NEW:
3237 msg = "new";
3238 break;
3239 case NIC_DEL:
3240 msg = "delete";
3241 break;
3242 default:
3243 gcc_unreachable ();
3244 }
3245 if (msg)
3246 error ("%qs cannot appear in a constant-expression", msg);
3247 return true;
3248 }
3249 }
3250 return false;
3251 }
3252
3253 /* Emit a diagnostic for an invalid type name. This function commits
3254 to the current active tentative parse, if any. (Otherwise, the
3255 problematic construct might be encountered again later, resulting
3256 in duplicate error messages.) LOCATION is the location of ID. */
3257
3258 static void
3259 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3260 location_t location)
3261 {
3262 tree decl, ambiguous_decls;
3263 cp_parser_commit_to_tentative_parse (parser);
3264 /* Try to lookup the identifier. */
3265 decl = cp_parser_lookup_name (parser, id, none_type,
3266 /*is_template=*/false,
3267 /*is_namespace=*/false,
3268 /*check_dependency=*/true,
3269 &ambiguous_decls, location);
3270 if (ambiguous_decls)
3271 /* If the lookup was ambiguous, an error will already have
3272 been issued. */
3273 return;
3274 /* If the lookup found a template-name, it means that the user forgot
3275 to specify an argument list. Emit a useful error message. */
3276 if (DECL_TYPE_TEMPLATE_P (decl))
3277 {
3278 auto_diagnostic_group d;
3279 error_at (location,
3280 "invalid use of template-name %qE without an argument list",
3281 decl);
3282 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3283 inform (location, "class template argument deduction is only available "
3284 "with -std=c++17 or -std=gnu++17");
3285 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3286 }
3287 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3288 error_at (location, "invalid use of destructor %qD as a type", id);
3289 else if (TREE_CODE (decl) == TYPE_DECL)
3290 /* Something like 'unsigned A a;' */
3291 error_at (location, "invalid combination of multiple type-specifiers");
3292 else if (!parser->scope)
3293 {
3294 /* Issue an error message. */
3295 auto_diagnostic_group d;
3296 name_hint hint;
3297 if (TREE_CODE (id) == IDENTIFIER_NODE)
3298 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3299 if (const char *suggestion = hint.suggestion ())
3300 {
3301 gcc_rich_location richloc (location);
3302 richloc.add_fixit_replace (suggestion);
3303 error_at (&richloc,
3304 "%qE does not name a type; did you mean %qs?",
3305 id, suggestion);
3306 }
3307 else
3308 error_at (location, "%qE does not name a type", id);
3309 /* If we're in a template class, it's possible that the user was
3310 referring to a type from a base class. For example:
3311
3312 template <typename T> struct A { typedef T X; };
3313 template <typename T> struct B : public A<T> { X x; };
3314
3315 The user should have said "typename A<T>::X". */
3316 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3317 inform (location, "C++11 %<constexpr%> only available with "
3318 "-std=c++11 or -std=gnu++11");
3319 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3320 inform (location, "C++11 %<noexcept%> only available with "
3321 "-std=c++11 or -std=gnu++11");
3322 else if (cxx_dialect < cxx11
3323 && TREE_CODE (id) == IDENTIFIER_NODE
3324 && id_equal (id, "thread_local"))
3325 inform (location, "C++11 %<thread_local%> only available with "
3326 "-std=c++11 or -std=gnu++11");
3327 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3328 inform (location, "%<concept%> only available with -fconcepts");
3329 else if (processing_template_decl && current_class_type
3330 && TYPE_BINFO (current_class_type))
3331 {
3332 tree b;
3333
3334 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3335 b;
3336 b = TREE_CHAIN (b))
3337 {
3338 tree base_type = BINFO_TYPE (b);
3339 if (CLASS_TYPE_P (base_type)
3340 && dependent_type_p (base_type))
3341 {
3342 tree field;
3343 /* Go from a particular instantiation of the
3344 template (which will have an empty TYPE_FIELDs),
3345 to the main version. */
3346 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3347 for (field = TYPE_FIELDS (base_type);
3348 field;
3349 field = DECL_CHAIN (field))
3350 if (TREE_CODE (field) == TYPE_DECL
3351 && DECL_NAME (field) == id)
3352 {
3353 inform (location,
3354 "(perhaps %<typename %T::%E%> was intended)",
3355 BINFO_TYPE (b), id);
3356 break;
3357 }
3358 if (field)
3359 break;
3360 }
3361 }
3362 }
3363 }
3364 /* Here we diagnose qualified-ids where the scope is actually correct,
3365 but the identifier does not resolve to a valid type name. */
3366 else if (parser->scope != error_mark_node)
3367 {
3368 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3369 {
3370 auto_diagnostic_group d;
3371 name_hint hint;
3372 if (decl == error_mark_node)
3373 hint = suggest_alternative_in_explicit_scope (location, id,
3374 parser->scope);
3375 const char *suggestion = hint.suggestion ();
3376 gcc_rich_location richloc (location_of (id));
3377 if (suggestion)
3378 richloc.add_fixit_replace (suggestion);
3379 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3380 {
3381 if (suggestion)
3382 error_at (&richloc,
3383 "%qE in namespace %qE does not name a template"
3384 " type; did you mean %qs?",
3385 id, parser->scope, suggestion);
3386 else
3387 error_at (&richloc,
3388 "%qE in namespace %qE does not name a template type",
3389 id, parser->scope);
3390 }
3391 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3392 {
3393 if (suggestion)
3394 error_at (&richloc,
3395 "%qE in namespace %qE does not name a template"
3396 " type; did you mean %qs?",
3397 TREE_OPERAND (id, 0), parser->scope, suggestion);
3398 else
3399 error_at (&richloc,
3400 "%qE in namespace %qE does not name a template"
3401 " type",
3402 TREE_OPERAND (id, 0), parser->scope);
3403 }
3404 else
3405 {
3406 if (suggestion)
3407 error_at (&richloc,
3408 "%qE in namespace %qE does not name a type"
3409 "; did you mean %qs?",
3410 id, parser->scope, suggestion);
3411 else
3412 error_at (&richloc,
3413 "%qE in namespace %qE does not name a type",
3414 id, parser->scope);
3415 }
3416 if (DECL_P (decl))
3417 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3418 }
3419 else if (CLASS_TYPE_P (parser->scope)
3420 && constructor_name_p (id, parser->scope))
3421 {
3422 /* A<T>::A<T>() */
3423 auto_diagnostic_group d;
3424 error_at (location, "%<%T::%E%> names the constructor, not"
3425 " the type", parser->scope, id);
3426 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3427 error_at (location, "and %qT has no template constructors",
3428 parser->scope);
3429 }
3430 else if (TYPE_P (parser->scope)
3431 && dependent_scope_p (parser->scope))
3432 {
3433 gcc_rich_location richloc (location);
3434 richloc.add_fixit_insert_before ("typename ");
3435 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3436 error_at (&richloc,
3437 "need %<typename%> before %<%T::%D::%E%> because "
3438 "%<%T::%D%> is a dependent scope",
3439 TYPE_CONTEXT (parser->scope),
3440 TYPENAME_TYPE_FULLNAME (parser->scope),
3441 id,
3442 TYPE_CONTEXT (parser->scope),
3443 TYPENAME_TYPE_FULLNAME (parser->scope));
3444 else
3445 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3446 "%qT is a dependent scope",
3447 parser->scope, id, parser->scope);
3448 }
3449 else if (TYPE_P (parser->scope))
3450 {
3451 auto_diagnostic_group d;
3452 if (!COMPLETE_TYPE_P (parser->scope))
3453 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3454 parser->scope);
3455 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3456 error_at (location_of (id),
3457 "%qE in %q#T does not name a template type",
3458 id, parser->scope);
3459 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3460 error_at (location_of (id),
3461 "%qE in %q#T does not name a template type",
3462 TREE_OPERAND (id, 0), parser->scope);
3463 else
3464 error_at (location_of (id),
3465 "%qE in %q#T does not name a type",
3466 id, parser->scope);
3467 if (DECL_P (decl))
3468 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3469 }
3470 else
3471 gcc_unreachable ();
3472 }
3473 }
3474
3475 /* Check for a common situation where a type-name should be present,
3476 but is not, and issue a sensible error message. Returns true if an
3477 invalid type-name was detected.
3478
3479 The situation handled by this function are variable declarations of the
3480 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3481 Usually, `ID' should name a type, but if we got here it means that it
3482 does not. We try to emit the best possible error message depending on
3483 how exactly the id-expression looks like. */
3484
3485 static bool
3486 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3487 {
3488 tree id;
3489 cp_token *token = cp_lexer_peek_token (parser->lexer);
3490
3491 /* Avoid duplicate error about ambiguous lookup. */
3492 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3493 {
3494 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3495 if (next->type == CPP_NAME && next->error_reported)
3496 goto out;
3497 }
3498
3499 cp_parser_parse_tentatively (parser);
3500 id = cp_parser_id_expression (parser,
3501 /*template_keyword_p=*/false,
3502 /*check_dependency_p=*/true,
3503 /*template_p=*/NULL,
3504 /*declarator_p=*/false,
3505 /*optional_p=*/false);
3506 /* If the next token is a (, this is a function with no explicit return
3507 type, i.e. constructor, destructor or conversion op. */
3508 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3509 || TREE_CODE (id) == TYPE_DECL)
3510 {
3511 cp_parser_abort_tentative_parse (parser);
3512 return false;
3513 }
3514 if (!cp_parser_parse_definitely (parser))
3515 return false;
3516
3517 /* Emit a diagnostic for the invalid type. */
3518 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3519 out:
3520 /* If we aren't in the middle of a declarator (i.e. in a
3521 parameter-declaration-clause), skip to the end of the declaration;
3522 there's no point in trying to process it. */
3523 if (!parser->in_declarator_p)
3524 cp_parser_skip_to_end_of_block_or_statement (parser);
3525 return true;
3526 }
3527
3528 /* Consume tokens up to, and including, the next non-nested closing `)'.
3529 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3530 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3531 found an unnested token of that type. */
3532
3533 static int
3534 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3535 bool recovering,
3536 cpp_ttype or_ttype,
3537 bool consume_paren)
3538 {
3539 unsigned paren_depth = 0;
3540 unsigned brace_depth = 0;
3541 unsigned square_depth = 0;
3542 unsigned condop_depth = 0;
3543
3544 if (recovering && or_ttype == CPP_EOF
3545 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3546 return 0;
3547
3548 while (true)
3549 {
3550 cp_token * token = cp_lexer_peek_token (parser->lexer);
3551
3552 /* Have we found what we're looking for before the closing paren? */
3553 if (token->type == or_ttype && or_ttype != CPP_EOF
3554 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3555 return -1;
3556
3557 switch (token->type)
3558 {
3559 case CPP_EOF:
3560 case CPP_PRAGMA_EOL:
3561 /* If we've run out of tokens, then there is no closing `)'. */
3562 return 0;
3563
3564 /* This is good for lambda expression capture-lists. */
3565 case CPP_OPEN_SQUARE:
3566 ++square_depth;
3567 break;
3568 case CPP_CLOSE_SQUARE:
3569 if (!square_depth--)
3570 return 0;
3571 break;
3572
3573 case CPP_SEMICOLON:
3574 /* This matches the processing in skip_to_end_of_statement. */
3575 if (!brace_depth)
3576 return 0;
3577 break;
3578
3579 case CPP_OPEN_BRACE:
3580 ++brace_depth;
3581 break;
3582 case CPP_CLOSE_BRACE:
3583 if (!brace_depth--)
3584 return 0;
3585 break;
3586
3587 case CPP_OPEN_PAREN:
3588 if (!brace_depth)
3589 ++paren_depth;
3590 break;
3591
3592 case CPP_CLOSE_PAREN:
3593 if (!brace_depth && !paren_depth--)
3594 {
3595 if (consume_paren)
3596 cp_lexer_consume_token (parser->lexer);
3597 return 1;
3598 }
3599 break;
3600
3601 case CPP_QUERY:
3602 if (!brace_depth && !paren_depth && !square_depth)
3603 ++condop_depth;
3604 break;
3605
3606 case CPP_COLON:
3607 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3608 condop_depth--;
3609 break;
3610
3611 default:
3612 break;
3613 }
3614
3615 /* Consume the token. */
3616 cp_lexer_consume_token (parser->lexer);
3617 }
3618 }
3619
3620 /* Consume tokens up to, and including, the next non-nested closing `)'.
3621 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3622 are doing error recovery. Returns -1 if OR_COMMA is true and we
3623 found an unnested token of that type. */
3624
3625 static int
3626 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3627 bool recovering,
3628 bool or_comma,
3629 bool consume_paren)
3630 {
3631 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3632 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3633 ttype, consume_paren);
3634 }
3635
3636 /* Consume tokens until we reach the end of the current statement.
3637 Normally, that will be just before consuming a `;'. However, if a
3638 non-nested `}' comes first, then we stop before consuming that. */
3639
3640 static void
3641 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3642 {
3643 unsigned nesting_depth = 0;
3644
3645 /* Unwind generic function template scope if necessary. */
3646 if (parser->fully_implicit_function_template_p)
3647 abort_fully_implicit_template (parser);
3648
3649 while (true)
3650 {
3651 cp_token *token = cp_lexer_peek_token (parser->lexer);
3652
3653 switch (token->type)
3654 {
3655 case CPP_EOF:
3656 case CPP_PRAGMA_EOL:
3657 /* If we've run out of tokens, stop. */
3658 return;
3659
3660 case CPP_SEMICOLON:
3661 /* If the next token is a `;', we have reached the end of the
3662 statement. */
3663 if (!nesting_depth)
3664 return;
3665 break;
3666
3667 case CPP_CLOSE_BRACE:
3668 /* If this is a non-nested '}', stop before consuming it.
3669 That way, when confronted with something like:
3670
3671 { 3 + }
3672
3673 we stop before consuming the closing '}', even though we
3674 have not yet reached a `;'. */
3675 if (nesting_depth == 0)
3676 return;
3677
3678 /* If it is the closing '}' for a block that we have
3679 scanned, stop -- but only after consuming the token.
3680 That way given:
3681
3682 void f g () { ... }
3683 typedef int I;
3684
3685 we will stop after the body of the erroneously declared
3686 function, but before consuming the following `typedef'
3687 declaration. */
3688 if (--nesting_depth == 0)
3689 {
3690 cp_lexer_consume_token (parser->lexer);
3691 return;
3692 }
3693 break;
3694
3695 case CPP_OPEN_BRACE:
3696 ++nesting_depth;
3697 break;
3698
3699 default:
3700 break;
3701 }
3702
3703 /* Consume the token. */
3704 cp_lexer_consume_token (parser->lexer);
3705 }
3706 }
3707
3708 /* This function is called at the end of a statement or declaration.
3709 If the next token is a semicolon, it is consumed; otherwise, error
3710 recovery is attempted. */
3711
3712 static void
3713 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3714 {
3715 /* Look for the trailing `;'. */
3716 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3717 {
3718 /* If there is additional (erroneous) input, skip to the end of
3719 the statement. */
3720 cp_parser_skip_to_end_of_statement (parser);
3721 /* If the next token is now a `;', consume it. */
3722 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3723 cp_lexer_consume_token (parser->lexer);
3724 }
3725 }
3726
3727 /* Skip tokens until we have consumed an entire block, or until we
3728 have consumed a non-nested `;'. */
3729
3730 static void
3731 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3732 {
3733 int nesting_depth = 0;
3734
3735 /* Unwind generic function template scope if necessary. */
3736 if (parser->fully_implicit_function_template_p)
3737 abort_fully_implicit_template (parser);
3738
3739 while (nesting_depth >= 0)
3740 {
3741 cp_token *token = cp_lexer_peek_token (parser->lexer);
3742
3743 switch (token->type)
3744 {
3745 case CPP_EOF:
3746 case CPP_PRAGMA_EOL:
3747 /* If we've run out of tokens, stop. */
3748 return;
3749
3750 case CPP_SEMICOLON:
3751 /* Stop if this is an unnested ';'. */
3752 if (!nesting_depth)
3753 nesting_depth = -1;
3754 break;
3755
3756 case CPP_CLOSE_BRACE:
3757 /* Stop if this is an unnested '}', or closes the outermost
3758 nesting level. */
3759 nesting_depth--;
3760 if (nesting_depth < 0)
3761 return;
3762 if (!nesting_depth)
3763 nesting_depth = -1;
3764 break;
3765
3766 case CPP_OPEN_BRACE:
3767 /* Nest. */
3768 nesting_depth++;
3769 break;
3770
3771 default:
3772 break;
3773 }
3774
3775 /* Consume the token. */
3776 cp_lexer_consume_token (parser->lexer);
3777 }
3778 }
3779
3780 /* Skip tokens until a non-nested closing curly brace is the next
3781 token, or there are no more tokens. Return true in the first case,
3782 false otherwise. */
3783
3784 static bool
3785 cp_parser_skip_to_closing_brace (cp_parser *parser)
3786 {
3787 unsigned nesting_depth = 0;
3788
3789 while (true)
3790 {
3791 cp_token *token = cp_lexer_peek_token (parser->lexer);
3792
3793 switch (token->type)
3794 {
3795 case CPP_EOF:
3796 case CPP_PRAGMA_EOL:
3797 /* If we've run out of tokens, stop. */
3798 return false;
3799
3800 case CPP_CLOSE_BRACE:
3801 /* If the next token is a non-nested `}', then we have reached
3802 the end of the current block. */
3803 if (nesting_depth-- == 0)
3804 return true;
3805 break;
3806
3807 case CPP_OPEN_BRACE:
3808 /* If it the next token is a `{', then we are entering a new
3809 block. Consume the entire block. */
3810 ++nesting_depth;
3811 break;
3812
3813 default:
3814 break;
3815 }
3816
3817 /* Consume the token. */
3818 cp_lexer_consume_token (parser->lexer);
3819 }
3820 }
3821
3822 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3823 parameter is the PRAGMA token, allowing us to purge the entire pragma
3824 sequence. */
3825
3826 static void
3827 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3828 {
3829 cp_token *token;
3830
3831 parser->lexer->in_pragma = false;
3832
3833 do
3834 token = cp_lexer_consume_token (parser->lexer);
3835 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3836
3837 /* Ensure that the pragma is not parsed again. */
3838 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3839 }
3840
3841 /* Require pragma end of line, resyncing with it as necessary. The
3842 arguments are as for cp_parser_skip_to_pragma_eol. */
3843
3844 static void
3845 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3846 {
3847 parser->lexer->in_pragma = false;
3848 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3849 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3850 }
3851
3852 /* This is a simple wrapper around make_typename_type. When the id is
3853 an unresolved identifier node, we can provide a superior diagnostic
3854 using cp_parser_diagnose_invalid_type_name. */
3855
3856 static tree
3857 cp_parser_make_typename_type (cp_parser *parser, tree id,
3858 location_t id_location)
3859 {
3860 tree result;
3861 if (identifier_p (id))
3862 {
3863 result = make_typename_type (parser->scope, id, typename_type,
3864 /*complain=*/tf_none);
3865 if (result == error_mark_node)
3866 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3867 return result;
3868 }
3869 return make_typename_type (parser->scope, id, typename_type, tf_error);
3870 }
3871
3872 /* This is a wrapper around the
3873 make_{pointer,ptrmem,reference}_declarator functions that decides
3874 which one to call based on the CODE and CLASS_TYPE arguments. The
3875 CODE argument should be one of the values returned by
3876 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3877 appertain to the pointer or reference. */
3878
3879 static cp_declarator *
3880 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3881 cp_cv_quals cv_qualifiers,
3882 cp_declarator *target,
3883 tree attributes)
3884 {
3885 if (code == ERROR_MARK || target == cp_error_declarator)
3886 return cp_error_declarator;
3887
3888 if (code == INDIRECT_REF)
3889 if (class_type == NULL_TREE)
3890 return make_pointer_declarator (cv_qualifiers, target, attributes);
3891 else
3892 return make_ptrmem_declarator (cv_qualifiers, class_type,
3893 target, attributes);
3894 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3895 return make_reference_declarator (cv_qualifiers, target,
3896 false, attributes);
3897 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3898 return make_reference_declarator (cv_qualifiers, target,
3899 true, attributes);
3900 gcc_unreachable ();
3901 }
3902
3903 /* Create a new C++ parser. */
3904
3905 static cp_parser *
3906 cp_parser_new (void)
3907 {
3908 cp_parser *parser;
3909 cp_lexer *lexer;
3910 unsigned i;
3911
3912 /* cp_lexer_new_main is called before doing GC allocation because
3913 cp_lexer_new_main might load a PCH file. */
3914 lexer = cp_lexer_new_main ();
3915
3916 /* Initialize the binops_by_token so that we can get the tree
3917 directly from the token. */
3918 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3919 binops_by_token[binops[i].token_type] = binops[i];
3920
3921 parser = ggc_cleared_alloc<cp_parser> ();
3922 parser->lexer = lexer;
3923 parser->context = cp_parser_context_new (NULL);
3924
3925 /* For now, we always accept GNU extensions. */
3926 parser->allow_gnu_extensions_p = 1;
3927
3928 /* The `>' token is a greater-than operator, not the end of a
3929 template-id. */
3930 parser->greater_than_is_operator_p = true;
3931
3932 parser->default_arg_ok_p = true;
3933
3934 /* We are not parsing a constant-expression. */
3935 parser->integral_constant_expression_p = false;
3936 parser->allow_non_integral_constant_expression_p = false;
3937 parser->non_integral_constant_expression_p = false;
3938
3939 /* Local variable names are not forbidden. */
3940 parser->local_variables_forbidden_p = false;
3941
3942 /* We are not processing an `extern "C"' declaration. */
3943 parser->in_unbraced_linkage_specification_p = false;
3944
3945 /* We are not processing a declarator. */
3946 parser->in_declarator_p = false;
3947
3948 /* We are not processing a template-argument-list. */
3949 parser->in_template_argument_list_p = false;
3950
3951 /* We are not in an iteration statement. */
3952 parser->in_statement = 0;
3953
3954 /* We are not in a switch statement. */
3955 parser->in_switch_statement_p = false;
3956
3957 /* We are not parsing a type-id inside an expression. */
3958 parser->in_type_id_in_expr_p = false;
3959
3960 /* String literals should be translated to the execution character set. */
3961 parser->translate_strings_p = true;
3962
3963 /* We are not parsing a function body. */
3964 parser->in_function_body = false;
3965
3966 /* We can correct until told otherwise. */
3967 parser->colon_corrects_to_scope_p = true;
3968
3969 /* The unparsed function queue is empty. */
3970 push_unparsed_function_queues (parser);
3971
3972 /* There are no classes being defined. */
3973 parser->num_classes_being_defined = 0;
3974
3975 /* No template parameters apply. */
3976 parser->num_template_parameter_lists = 0;
3977
3978 /* Special parsing data structures. */
3979 parser->omp_declare_simd = NULL;
3980 parser->oacc_routine = NULL;
3981
3982 /* Not declaring an implicit function template. */
3983 parser->auto_is_implicit_function_template_parm_p = false;
3984 parser->fully_implicit_function_template_p = false;
3985 parser->implicit_template_parms = 0;
3986 parser->implicit_template_scope = 0;
3987
3988 /* Allow constrained-type-specifiers. */
3989 parser->prevent_constrained_type_specifiers = 0;
3990
3991 /* We haven't yet seen an 'extern "C"'. */
3992 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3993
3994 return parser;
3995 }
3996
3997 /* Create a cp_lexer structure which will emit the tokens in CACHE
3998 and push it onto the parser's lexer stack. This is used for delayed
3999 parsing of in-class method bodies and default arguments, and should
4000 not be confused with tentative parsing. */
4001 static void
4002 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4003 {
4004 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4005 lexer->next = parser->lexer;
4006 parser->lexer = lexer;
4007
4008 /* Move the current source position to that of the first token in the
4009 new lexer. */
4010 cp_lexer_set_source_position_from_token (lexer->next_token);
4011 }
4012
4013 /* Pop the top lexer off the parser stack. This is never used for the
4014 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4015 static void
4016 cp_parser_pop_lexer (cp_parser *parser)
4017 {
4018 cp_lexer *lexer = parser->lexer;
4019 parser->lexer = lexer->next;
4020 cp_lexer_destroy (lexer);
4021
4022 /* Put the current source position back where it was before this
4023 lexer was pushed. */
4024 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4025 }
4026
4027 /* Lexical conventions [gram.lex] */
4028
4029 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4030 identifier. */
4031
4032 static cp_expr
4033 cp_parser_identifier (cp_parser* parser)
4034 {
4035 cp_token *token;
4036
4037 /* Look for the identifier. */
4038 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4039 /* Return the value. */
4040 if (token)
4041 return cp_expr (token->u.value, token->location);
4042 else
4043 return error_mark_node;
4044 }
4045
4046 /* Parse a sequence of adjacent string constants. Returns a
4047 TREE_STRING representing the combined, nul-terminated string
4048 constant. If TRANSLATE is true, translate the string to the
4049 execution character set. If WIDE_OK is true, a wide string is
4050 invalid here.
4051
4052 C++98 [lex.string] says that if a narrow string literal token is
4053 adjacent to a wide string literal token, the behavior is undefined.
4054 However, C99 6.4.5p4 says that this results in a wide string literal.
4055 We follow C99 here, for consistency with the C front end.
4056
4057 This code is largely lifted from lex_string() in c-lex.c.
4058
4059 FUTURE: ObjC++ will need to handle @-strings here. */
4060 static cp_expr
4061 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4062 bool lookup_udlit = true)
4063 {
4064 tree value;
4065 size_t count;
4066 struct obstack str_ob;
4067 struct obstack loc_ob;
4068 cpp_string str, istr, *strs;
4069 cp_token *tok;
4070 enum cpp_ttype type, curr_type;
4071 int have_suffix_p = 0;
4072 tree string_tree;
4073 tree suffix_id = NULL_TREE;
4074 bool curr_tok_is_userdef_p = false;
4075
4076 tok = cp_lexer_peek_token (parser->lexer);
4077 if (!cp_parser_is_string_literal (tok))
4078 {
4079 cp_parser_error (parser, "expected string-literal");
4080 return error_mark_node;
4081 }
4082
4083 location_t loc = tok->location;
4084
4085 if (cpp_userdef_string_p (tok->type))
4086 {
4087 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4088 curr_type = cpp_userdef_string_remove_type (tok->type);
4089 curr_tok_is_userdef_p = true;
4090 }
4091 else
4092 {
4093 string_tree = tok->u.value;
4094 curr_type = tok->type;
4095 }
4096 type = curr_type;
4097
4098 /* Try to avoid the overhead of creating and destroying an obstack
4099 for the common case of just one string. */
4100 if (!cp_parser_is_string_literal
4101 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4102 {
4103 cp_lexer_consume_token (parser->lexer);
4104
4105 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4106 str.len = TREE_STRING_LENGTH (string_tree);
4107 count = 1;
4108
4109 if (curr_tok_is_userdef_p)
4110 {
4111 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4112 have_suffix_p = 1;
4113 curr_type = cpp_userdef_string_remove_type (tok->type);
4114 }
4115 else
4116 curr_type = tok->type;
4117
4118 strs = &str;
4119 }
4120 else
4121 {
4122 location_t last_tok_loc = tok->location;
4123 gcc_obstack_init (&str_ob);
4124 gcc_obstack_init (&loc_ob);
4125 count = 0;
4126
4127 do
4128 {
4129 cp_lexer_consume_token (parser->lexer);
4130 count++;
4131 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4132 str.len = TREE_STRING_LENGTH (string_tree);
4133
4134 if (curr_tok_is_userdef_p)
4135 {
4136 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4137 if (have_suffix_p == 0)
4138 {
4139 suffix_id = curr_suffix_id;
4140 have_suffix_p = 1;
4141 }
4142 else if (have_suffix_p == 1
4143 && curr_suffix_id != suffix_id)
4144 {
4145 error ("inconsistent user-defined literal suffixes"
4146 " %qD and %qD in string literal",
4147 suffix_id, curr_suffix_id);
4148 have_suffix_p = -1;
4149 }
4150 curr_type = cpp_userdef_string_remove_type (tok->type);
4151 }
4152 else
4153 curr_type = tok->type;
4154
4155 if (type != curr_type)
4156 {
4157 if (type == CPP_STRING)
4158 type = curr_type;
4159 else if (curr_type != CPP_STRING)
4160 {
4161 rich_location rich_loc (line_table, tok->location);
4162 rich_loc.add_range (last_tok_loc);
4163 error_at (&rich_loc,
4164 "unsupported non-standard concatenation "
4165 "of string literals");
4166 }
4167 }
4168
4169 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4170 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4171
4172 last_tok_loc = tok->location;
4173
4174 tok = cp_lexer_peek_token (parser->lexer);
4175 if (cpp_userdef_string_p (tok->type))
4176 {
4177 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4178 curr_type = cpp_userdef_string_remove_type (tok->type);
4179 curr_tok_is_userdef_p = true;
4180 }
4181 else
4182 {
4183 string_tree = tok->u.value;
4184 curr_type = tok->type;
4185 curr_tok_is_userdef_p = false;
4186 }
4187 }
4188 while (cp_parser_is_string_literal (tok));
4189
4190 /* A string literal built by concatenation has its caret=start at
4191 the start of the initial string, and its finish at the finish of
4192 the final string literal. */
4193 loc = make_location (loc, loc, get_finish (last_tok_loc));
4194
4195 strs = (cpp_string *) obstack_finish (&str_ob);
4196 }
4197
4198 if (type != CPP_STRING && !wide_ok)
4199 {
4200 cp_parser_error (parser, "a wide string is invalid in this context");
4201 type = CPP_STRING;
4202 }
4203
4204 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4205 (parse_in, strs, count, &istr, type))
4206 {
4207 value = build_string (istr.len, (const char *)istr.text);
4208 free (CONST_CAST (unsigned char *, istr.text));
4209 if (count > 1)
4210 {
4211 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4212 gcc_assert (g_string_concat_db);
4213 g_string_concat_db->record_string_concatenation (count, locs);
4214 }
4215
4216 switch (type)
4217 {
4218 default:
4219 case CPP_STRING:
4220 case CPP_UTF8STRING:
4221 TREE_TYPE (value) = char_array_type_node;
4222 break;
4223 case CPP_STRING16:
4224 TREE_TYPE (value) = char16_array_type_node;
4225 break;
4226 case CPP_STRING32:
4227 TREE_TYPE (value) = char32_array_type_node;
4228 break;
4229 case CPP_WSTRING:
4230 TREE_TYPE (value) = wchar_array_type_node;
4231 break;
4232 }
4233
4234 value = fix_string_type (value);
4235
4236 if (have_suffix_p)
4237 {
4238 tree literal = build_userdef_literal (suffix_id, value,
4239 OT_NONE, NULL_TREE);
4240 if (lookup_udlit)
4241 value = cp_parser_userdef_string_literal (literal);
4242 else
4243 value = literal;
4244 }
4245 }
4246 else
4247 /* cpp_interpret_string has issued an error. */
4248 value = error_mark_node;
4249
4250 if (count > 1)
4251 {
4252 obstack_free (&str_ob, 0);
4253 obstack_free (&loc_ob, 0);
4254 }
4255
4256 return cp_expr (value, loc);
4257 }
4258
4259 /* Look up a literal operator with the name and the exact arguments. */
4260
4261 static tree
4262 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4263 {
4264 tree decl = lookup_name (name);
4265 if (!decl || !is_overloaded_fn (decl))
4266 return error_mark_node;
4267
4268 for (lkp_iterator iter (decl); iter; ++iter)
4269 {
4270 tree fn = *iter;
4271
4272 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4273 {
4274 unsigned int ix;
4275 bool found = true;
4276
4277 for (ix = 0;
4278 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4279 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4280 {
4281 tree tparm = TREE_VALUE (parmtypes);
4282 tree targ = TREE_TYPE ((*args)[ix]);
4283 bool ptr = TYPE_PTR_P (tparm);
4284 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4285 if ((ptr || arr || !same_type_p (tparm, targ))
4286 && (!ptr || !arr
4287 || !same_type_p (TREE_TYPE (tparm),
4288 TREE_TYPE (targ))))
4289 found = false;
4290 }
4291
4292 if (found
4293 && ix == vec_safe_length (args)
4294 /* May be this should be sufficient_parms_p instead,
4295 depending on how exactly should user-defined literals
4296 work in presence of default arguments on the literal
4297 operator parameters. */
4298 && parmtypes == void_list_node)
4299 return decl;
4300 }
4301 }
4302
4303 return error_mark_node;
4304 }
4305
4306 /* Parse a user-defined char constant. Returns a call to a user-defined
4307 literal operator taking the character as an argument. */
4308
4309 static cp_expr
4310 cp_parser_userdef_char_literal (cp_parser *parser)
4311 {
4312 cp_token *token = cp_lexer_consume_token (parser->lexer);
4313 tree literal = token->u.value;
4314 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4315 tree value = USERDEF_LITERAL_VALUE (literal);
4316 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4317 tree decl, result;
4318
4319 /* Build up a call to the user-defined operator */
4320 /* Lookup the name we got back from the id-expression. */
4321 vec<tree, va_gc> *args = make_tree_vector ();
4322 vec_safe_push (args, value);
4323 decl = lookup_literal_operator (name, args);
4324 if (!decl || decl == error_mark_node)
4325 {
4326 error ("unable to find character literal operator %qD with %qT argument",
4327 name, TREE_TYPE (value));
4328 release_tree_vector (args);
4329 return error_mark_node;
4330 }
4331 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4332 release_tree_vector (args);
4333 return result;
4334 }
4335
4336 /* A subroutine of cp_parser_userdef_numeric_literal to
4337 create a char... template parameter pack from a string node. */
4338
4339 static tree
4340 make_char_string_pack (tree value)
4341 {
4342 tree charvec;
4343 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4344 const char *str = TREE_STRING_POINTER (value);
4345 int i, len = TREE_STRING_LENGTH (value) - 1;
4346 tree argvec = make_tree_vec (1);
4347
4348 /* Fill in CHARVEC with all of the parameters. */
4349 charvec = make_tree_vec (len);
4350 for (i = 0; i < len; ++i)
4351 {
4352 unsigned char s[3] = { '\'', str[i], '\'' };
4353 cpp_string in = { 3, s };
4354 cpp_string out = { 0, 0 };
4355 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4356 return NULL_TREE;
4357 gcc_assert (out.len == 2);
4358 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4359 out.text[0]);
4360 }
4361
4362 /* Build the argument packs. */
4363 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4364
4365 TREE_VEC_ELT (argvec, 0) = argpack;
4366
4367 return argvec;
4368 }
4369
4370 /* A subroutine of cp_parser_userdef_numeric_literal to
4371 create a char... template parameter pack from a string node. */
4372
4373 static tree
4374 make_string_pack (tree value)
4375 {
4376 tree charvec;
4377 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4378 const unsigned char *str
4379 = (const unsigned char *) TREE_STRING_POINTER (value);
4380 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4381 int len = TREE_STRING_LENGTH (value) / sz - 1;
4382 tree argvec = make_tree_vec (2);
4383
4384 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4385 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4386
4387 /* First template parm is character type. */
4388 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4389
4390 /* Fill in CHARVEC with all of the parameters. */
4391 charvec = make_tree_vec (len);
4392 for (int i = 0; i < len; ++i)
4393 TREE_VEC_ELT (charvec, i)
4394 = double_int_to_tree (str_char_type_node,
4395 double_int::from_buffer (str + i * sz, sz));
4396
4397 /* Build the argument packs. */
4398 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4399
4400 TREE_VEC_ELT (argvec, 1) = argpack;
4401
4402 return argvec;
4403 }
4404
4405 /* Parse a user-defined numeric constant. returns a call to a user-defined
4406 literal operator. */
4407
4408 static cp_expr
4409 cp_parser_userdef_numeric_literal (cp_parser *parser)
4410 {
4411 cp_token *token = cp_lexer_consume_token (parser->lexer);
4412 tree literal = token->u.value;
4413 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4414 tree value = USERDEF_LITERAL_VALUE (literal);
4415 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4416 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4417 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4418 tree decl, result;
4419 vec<tree, va_gc> *args;
4420
4421 /* Look for a literal operator taking the exact type of numeric argument
4422 as the literal value. */
4423 args = make_tree_vector ();
4424 vec_safe_push (args, value);
4425 decl = lookup_literal_operator (name, args);
4426 if (decl && decl != error_mark_node)
4427 {
4428 result = finish_call_expr (decl, &args, false, true,
4429 tf_warning_or_error);
4430
4431 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4432 {
4433 warning_at (token->location, OPT_Woverflow,
4434 "integer literal exceeds range of %qT type",
4435 long_long_unsigned_type_node);
4436 }
4437 else
4438 {
4439 if (overflow > 0)
4440 warning_at (token->location, OPT_Woverflow,
4441 "floating literal exceeds range of %qT type",
4442 long_double_type_node);
4443 else if (overflow < 0)
4444 warning_at (token->location, OPT_Woverflow,
4445 "floating literal truncated to zero");
4446 }
4447
4448 release_tree_vector (args);
4449 return result;
4450 }
4451 release_tree_vector (args);
4452
4453 /* If the numeric argument didn't work, look for a raw literal
4454 operator taking a const char* argument consisting of the number
4455 in string format. */
4456 args = make_tree_vector ();
4457 vec_safe_push (args, num_string);
4458 decl = lookup_literal_operator (name, args);
4459 if (decl && decl != error_mark_node)
4460 {
4461 result = finish_call_expr (decl, &args, false, true,
4462 tf_warning_or_error);
4463 release_tree_vector (args);
4464 return result;
4465 }
4466 release_tree_vector (args);
4467
4468 /* If the raw literal didn't work, look for a non-type template
4469 function with parameter pack char.... Call the function with
4470 template parameter characters representing the number. */
4471 args = make_tree_vector ();
4472 decl = lookup_literal_operator (name, args);
4473 if (decl && decl != error_mark_node)
4474 {
4475 tree tmpl_args = make_char_string_pack (num_string);
4476 if (tmpl_args == NULL_TREE)
4477 {
4478 error ("failed to translate literal to execution character set %qT",
4479 num_string);
4480 return error_mark_node;
4481 }
4482 decl = lookup_template_function (decl, tmpl_args);
4483 result = finish_call_expr (decl, &args, false, true,
4484 tf_warning_or_error);
4485 release_tree_vector (args);
4486 return result;
4487 }
4488
4489 release_tree_vector (args);
4490
4491 /* In C++14 the standard library defines complex number suffixes that
4492 conflict with GNU extensions. Prefer them if <complex> is #included. */
4493 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4494 bool i14 = (cxx_dialect > cxx11
4495 && (id_equal (suffix_id, "i")
4496 || id_equal (suffix_id, "if")
4497 || id_equal (suffix_id, "il")));
4498 diagnostic_t kind = DK_ERROR;
4499 int opt = 0;
4500
4501 if (i14 && ext)
4502 {
4503 tree cxlit = lookup_qualified_name (std_node,
4504 get_identifier ("complex_literals"),
4505 0, false, false);
4506 if (cxlit == error_mark_node)
4507 {
4508 /* No <complex>, so pedwarn and use GNU semantics. */
4509 kind = DK_PEDWARN;
4510 opt = OPT_Wpedantic;
4511 }
4512 }
4513
4514 bool complained
4515 = emit_diagnostic (kind, input_location, opt,
4516 "unable to find numeric literal operator %qD", name);
4517
4518 if (!complained)
4519 /* Don't inform either. */;
4520 else if (i14)
4521 {
4522 inform (token->location, "add %<using namespace std::complex_literals%> "
4523 "(from <complex>) to enable the C++14 user-defined literal "
4524 "suffixes");
4525 if (ext)
4526 inform (token->location, "or use %<j%> instead of %<i%> for the "
4527 "GNU built-in suffix");
4528 }
4529 else if (!ext)
4530 inform (token->location, "use -fext-numeric-literals "
4531 "to enable more built-in suffixes");
4532
4533 if (kind == DK_ERROR)
4534 value = error_mark_node;
4535 else
4536 {
4537 /* Use the built-in semantics. */
4538 tree type;
4539 if (id_equal (suffix_id, "i"))
4540 {
4541 if (TREE_CODE (value) == INTEGER_CST)
4542 type = integer_type_node;
4543 else
4544 type = double_type_node;
4545 }
4546 else if (id_equal (suffix_id, "if"))
4547 type = float_type_node;
4548 else /* if (id_equal (suffix_id, "il")) */
4549 type = long_double_type_node;
4550
4551 value = build_complex (build_complex_type (type),
4552 fold_convert (type, integer_zero_node),
4553 fold_convert (type, value));
4554 }
4555
4556 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4557 /* Avoid repeated diagnostics. */
4558 token->u.value = value;
4559 return value;
4560 }
4561
4562 /* Parse a user-defined string constant. Returns a call to a user-defined
4563 literal operator taking a character pointer and the length of the string
4564 as arguments. */
4565
4566 static tree
4567 cp_parser_userdef_string_literal (tree literal)
4568 {
4569 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4570 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4571 tree value = USERDEF_LITERAL_VALUE (literal);
4572 int len = TREE_STRING_LENGTH (value)
4573 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4574 tree decl;
4575
4576 /* Build up a call to the user-defined operator. */
4577 /* Lookup the name we got back from the id-expression. */
4578 releasing_vec rargs;
4579 vec<tree, va_gc> *&args = rargs.get_ref();
4580 vec_safe_push (args, value);
4581 vec_safe_push (args, build_int_cst (size_type_node, len));
4582 decl = lookup_literal_operator (name, args);
4583
4584 if (decl && decl != error_mark_node)
4585 return finish_call_expr (decl, &args, false, true,
4586 tf_warning_or_error);
4587
4588 /* Look for a suitable template function, either (C++20) with a single
4589 parameter of class type, or (N3599) with typename parameter CharT and
4590 parameter pack CharT... */
4591 args->truncate (0);
4592 decl = lookup_literal_operator (name, args);
4593 if (decl && decl != error_mark_node)
4594 {
4595 /* Use resolve_nondeduced_context to try to choose one form of template
4596 or the other. */
4597 tree tmpl_args = make_tree_vec (1);
4598 TREE_VEC_ELT (tmpl_args, 0) = value;
4599 decl = lookup_template_function (decl, tmpl_args);
4600 tree res = resolve_nondeduced_context (decl, tf_none);
4601 if (DECL_P (res))
4602 decl = res;
4603 else
4604 {
4605 TREE_OPERAND (decl, 1) = make_string_pack (value);
4606 res = resolve_nondeduced_context (decl, tf_none);
4607 if (DECL_P (res))
4608 decl = res;
4609 }
4610 if (!DECL_P (decl) && cxx_dialect > cxx17)
4611 TREE_OPERAND (decl, 1) = tmpl_args;
4612 return finish_call_expr (decl, &args, false, true,
4613 tf_warning_or_error);
4614 }
4615
4616 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4617 name, TREE_TYPE (value), size_type_node);
4618 return error_mark_node;
4619 }
4620
4621
4622 /* Basic concepts [gram.basic] */
4623
4624 /* Parse a translation-unit.
4625
4626 translation-unit:
4627 declaration-seq [opt] */
4628
4629 static void
4630 cp_parser_translation_unit (cp_parser* parser)
4631 {
4632 gcc_checking_assert (!cp_error_declarator);
4633
4634 /* Create the declarator obstack. */
4635 gcc_obstack_init (&declarator_obstack);
4636 /* Create the error declarator. */
4637 cp_error_declarator = make_declarator (cdk_error);
4638 /* Create the empty parameter list. */
4639 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4640 UNKNOWN_LOCATION);
4641 /* Remember where the base of the declarator obstack lies. */
4642 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4643
4644 bool implicit_extern_c = false;
4645
4646 for (;;)
4647 {
4648 cp_token *token = cp_lexer_peek_token (parser->lexer);
4649
4650 /* If we're entering or exiting a region that's implicitly
4651 extern "C", modify the lang context appropriately. */
4652 if (implicit_extern_c
4653 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4654 {
4655 implicit_extern_c = !implicit_extern_c;
4656 if (implicit_extern_c)
4657 push_lang_context (lang_name_c);
4658 else
4659 pop_lang_context ();
4660 }
4661
4662 if (token->type == CPP_EOF)
4663 break;
4664
4665 if (token->type == CPP_CLOSE_BRACE)
4666 {
4667 cp_parser_error (parser, "expected declaration");
4668 cp_lexer_consume_token (parser->lexer);
4669 /* If the next token is now a `;', consume it. */
4670 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4671 cp_lexer_consume_token (parser->lexer);
4672 }
4673 else
4674 cp_parser_toplevel_declaration (parser);
4675 }
4676
4677 /* Get rid of the token array; we don't need it any more. */
4678 cp_lexer_destroy (parser->lexer);
4679 parser->lexer = NULL;
4680
4681 /* The EOF should have reset this. */
4682 gcc_checking_assert (!implicit_extern_c);
4683
4684 /* Make sure the declarator obstack was fully cleaned up. */
4685 gcc_assert (obstack_next_free (&declarator_obstack)
4686 == declarator_obstack_base);
4687 }
4688
4689 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4690 decltype context. */
4691
4692 static inline tsubst_flags_t
4693 complain_flags (bool decltype_p)
4694 {
4695 tsubst_flags_t complain = tf_warning_or_error;
4696 if (decltype_p)
4697 complain |= tf_decltype;
4698 return complain;
4699 }
4700
4701 /* We're about to parse a collection of statements. If we're currently
4702 parsing tentatively, set up a firewall so that any nested
4703 cp_parser_commit_to_tentative_parse won't affect the current context. */
4704
4705 static cp_token_position
4706 cp_parser_start_tentative_firewall (cp_parser *parser)
4707 {
4708 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4709 return 0;
4710
4711 cp_parser_parse_tentatively (parser);
4712 cp_parser_commit_to_topmost_tentative_parse (parser);
4713 return cp_lexer_token_position (parser->lexer, false);
4714 }
4715
4716 /* We've finished parsing the collection of statements. Wrap up the
4717 firewall and replace the relevant tokens with the parsed form. */
4718
4719 static void
4720 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4721 tree expr)
4722 {
4723 if (!start)
4724 return;
4725
4726 /* Finish the firewall level. */
4727 cp_parser_parse_definitely (parser);
4728 /* And remember the result of the parse for when we try again. */
4729 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4730 token->type = CPP_PREPARSED_EXPR;
4731 token->u.value = expr;
4732 token->keyword = RID_MAX;
4733 cp_lexer_purge_tokens_after (parser->lexer, start);
4734 }
4735
4736 /* Like the above functions, but let the user modify the tokens. Used by
4737 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4738 later parses, so it makes sense to localize the effects of
4739 cp_parser_commit_to_tentative_parse. */
4740
4741 struct tentative_firewall
4742 {
4743 cp_parser *parser;
4744 bool set;
4745
4746 tentative_firewall (cp_parser *p): parser(p)
4747 {
4748 /* If we're currently parsing tentatively, start a committed level as a
4749 firewall and then an inner tentative parse. */
4750 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4751 {
4752 cp_parser_parse_tentatively (parser);
4753 cp_parser_commit_to_topmost_tentative_parse (parser);
4754 cp_parser_parse_tentatively (parser);
4755 }
4756 }
4757
4758 ~tentative_firewall()
4759 {
4760 if (set)
4761 {
4762 /* Finish the inner tentative parse and the firewall, propagating any
4763 uncommitted error state to the outer tentative parse. */
4764 bool err = cp_parser_error_occurred (parser);
4765 cp_parser_parse_definitely (parser);
4766 cp_parser_parse_definitely (parser);
4767 if (err)
4768 cp_parser_simulate_error (parser);
4769 }
4770 }
4771 };
4772
4773 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4774 This class is for tracking such a matching pair of symbols.
4775 In particular, it tracks the location of the first token,
4776 so that if the second token is missing, we can highlight the
4777 location of the first token when notifying the user about the
4778 problem. */
4779
4780 template <typename traits_t>
4781 class token_pair
4782 {
4783 public:
4784 /* token_pair's ctor. */
4785 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4786
4787 /* If the next token is the opening symbol for this pair, consume it and
4788 return true.
4789 Otherwise, issue an error and return false.
4790 In either case, record the location of the opening token. */
4791
4792 bool require_open (cp_parser *parser)
4793 {
4794 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4795 return cp_parser_require (parser, traits_t::open_token_type,
4796 traits_t::required_token_open);
4797 }
4798
4799 /* Consume the next token from PARSER, recording its location as
4800 that of the opening token within the pair. */
4801
4802 cp_token * consume_open (cp_parser *parser)
4803 {
4804 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4805 gcc_assert (tok->type == traits_t::open_token_type);
4806 m_open_loc = tok->location;
4807 return tok;
4808 }
4809
4810 /* If the next token is the closing symbol for this pair, consume it
4811 and return it.
4812 Otherwise, issue an error, highlighting the location of the
4813 corresponding opening token, and return NULL. */
4814
4815 cp_token *require_close (cp_parser *parser) const
4816 {
4817 return cp_parser_require (parser, traits_t::close_token_type,
4818 traits_t::required_token_close,
4819 m_open_loc);
4820 }
4821
4822 private:
4823 location_t m_open_loc;
4824 };
4825
4826 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4827
4828 struct matching_paren_traits
4829 {
4830 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4831 static const enum required_token required_token_open = RT_OPEN_PAREN;
4832 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4833 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4834 };
4835
4836 /* "matching_parens" is a token_pair<T> class for tracking matching
4837 pairs of parentheses. */
4838
4839 typedef token_pair<matching_paren_traits> matching_parens;
4840
4841 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4842
4843 struct matching_brace_traits
4844 {
4845 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4846 static const enum required_token required_token_open = RT_OPEN_BRACE;
4847 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4848 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4849 };
4850
4851 /* "matching_braces" is a token_pair<T> class for tracking matching
4852 pairs of braces. */
4853
4854 typedef token_pair<matching_brace_traits> matching_braces;
4855
4856
4857 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4858 enclosing parentheses. */
4859
4860 static cp_expr
4861 cp_parser_statement_expr (cp_parser *parser)
4862 {
4863 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4864
4865 /* Consume the '('. */
4866 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4867 matching_parens parens;
4868 parens.consume_open (parser);
4869 /* Start the statement-expression. */
4870 tree expr = begin_stmt_expr ();
4871 /* Parse the compound-statement. */
4872 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4873 /* Finish up. */
4874 expr = finish_stmt_expr (expr, false);
4875 /* Consume the ')'. */
4876 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4877 if (!parens.require_close (parser))
4878 cp_parser_skip_to_end_of_statement (parser);
4879
4880 cp_parser_end_tentative_firewall (parser, start, expr);
4881 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4882 return cp_expr (expr, combined_loc);
4883 }
4884
4885 /* Expressions [gram.expr] */
4886
4887 /* Parse a fold-operator.
4888
4889 fold-operator:
4890 - * / % ^ & | = < > << >>
4891 = -= *= /= %= ^= &= |= <<= >>=
4892 == != <= >= && || , .* ->*
4893
4894 This returns the tree code corresponding to the matched operator
4895 as an int. When the current token matches a compound assignment
4896 opertor, the resulting tree code is the negative value of the
4897 non-assignment operator. */
4898
4899 static int
4900 cp_parser_fold_operator (cp_token *token)
4901 {
4902 switch (token->type)
4903 {
4904 case CPP_PLUS: return PLUS_EXPR;
4905 case CPP_MINUS: return MINUS_EXPR;
4906 case CPP_MULT: return MULT_EXPR;
4907 case CPP_DIV: return TRUNC_DIV_EXPR;
4908 case CPP_MOD: return TRUNC_MOD_EXPR;
4909 case CPP_XOR: return BIT_XOR_EXPR;
4910 case CPP_AND: return BIT_AND_EXPR;
4911 case CPP_OR: return BIT_IOR_EXPR;
4912 case CPP_LSHIFT: return LSHIFT_EXPR;
4913 case CPP_RSHIFT: return RSHIFT_EXPR;
4914
4915 case CPP_EQ: return -NOP_EXPR;
4916 case CPP_PLUS_EQ: return -PLUS_EXPR;
4917 case CPP_MINUS_EQ: return -MINUS_EXPR;
4918 case CPP_MULT_EQ: return -MULT_EXPR;
4919 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4920 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4921 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4922 case CPP_AND_EQ: return -BIT_AND_EXPR;
4923 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4924 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4925 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4926
4927 case CPP_EQ_EQ: return EQ_EXPR;
4928 case CPP_NOT_EQ: return NE_EXPR;
4929 case CPP_LESS: return LT_EXPR;
4930 case CPP_GREATER: return GT_EXPR;
4931 case CPP_LESS_EQ: return LE_EXPR;
4932 case CPP_GREATER_EQ: return GE_EXPR;
4933
4934 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4935 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4936
4937 case CPP_COMMA: return COMPOUND_EXPR;
4938
4939 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4940 case CPP_DEREF_STAR: return MEMBER_REF;
4941
4942 default: return ERROR_MARK;
4943 }
4944 }
4945
4946 /* Returns true if CODE indicates a binary expression, which is not allowed in
4947 the LHS of a fold-expression. More codes will need to be added to use this
4948 function in other contexts. */
4949
4950 static bool
4951 is_binary_op (tree_code code)
4952 {
4953 switch (code)
4954 {
4955 case PLUS_EXPR:
4956 case POINTER_PLUS_EXPR:
4957 case MINUS_EXPR:
4958 case MULT_EXPR:
4959 case TRUNC_DIV_EXPR:
4960 case TRUNC_MOD_EXPR:
4961 case BIT_XOR_EXPR:
4962 case BIT_AND_EXPR:
4963 case BIT_IOR_EXPR:
4964 case LSHIFT_EXPR:
4965 case RSHIFT_EXPR:
4966
4967 case MODOP_EXPR:
4968
4969 case EQ_EXPR:
4970 case NE_EXPR:
4971 case LE_EXPR:
4972 case GE_EXPR:
4973 case LT_EXPR:
4974 case GT_EXPR:
4975
4976 case TRUTH_ANDIF_EXPR:
4977 case TRUTH_ORIF_EXPR:
4978
4979 case COMPOUND_EXPR:
4980
4981 case DOTSTAR_EXPR:
4982 case MEMBER_REF:
4983 return true;
4984
4985 default:
4986 return false;
4987 }
4988 }
4989
4990 /* If the next token is a suitable fold operator, consume it and return as
4991 the function above. */
4992
4993 static int
4994 cp_parser_fold_operator (cp_parser *parser)
4995 {
4996 cp_token* token = cp_lexer_peek_token (parser->lexer);
4997 int code = cp_parser_fold_operator (token);
4998 if (code != ERROR_MARK)
4999 cp_lexer_consume_token (parser->lexer);
5000 return code;
5001 }
5002
5003 /* Parse a fold-expression.
5004
5005 fold-expression:
5006 ( ... folding-operator cast-expression)
5007 ( cast-expression folding-operator ... )
5008 ( cast-expression folding operator ... folding-operator cast-expression)
5009
5010 Note that the '(' and ')' are matched in primary expression. */
5011
5012 static cp_expr
5013 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5014 {
5015 cp_id_kind pidk;
5016
5017 // Left fold.
5018 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5019 {
5020 cp_lexer_consume_token (parser->lexer);
5021 int op = cp_parser_fold_operator (parser);
5022 if (op == ERROR_MARK)
5023 {
5024 cp_parser_error (parser, "expected binary operator");
5025 return error_mark_node;
5026 }
5027
5028 tree expr = cp_parser_cast_expression (parser, false, false,
5029 false, &pidk);
5030 if (expr == error_mark_node)
5031 return error_mark_node;
5032 return finish_left_unary_fold_expr (expr, op);
5033 }
5034
5035 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5036 int op = cp_parser_fold_operator (parser);
5037 if (op == ERROR_MARK)
5038 {
5039 cp_parser_error (parser, "expected binary operator");
5040 return error_mark_node;
5041 }
5042
5043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5044 {
5045 cp_parser_error (parser, "expected ...");
5046 return error_mark_node;
5047 }
5048 cp_lexer_consume_token (parser->lexer);
5049
5050 /* The operands of a fold-expression are cast-expressions, so binary or
5051 conditional expressions are not allowed. We check this here to avoid
5052 tentative parsing. */
5053 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5054 /* OK, the expression was parenthesized. */;
5055 else if (is_binary_op (TREE_CODE (expr1)))
5056 error_at (location_of (expr1),
5057 "binary expression in operand of fold-expression");
5058 else if (TREE_CODE (expr1) == COND_EXPR
5059 || (REFERENCE_REF_P (expr1)
5060 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5061 error_at (location_of (expr1),
5062 "conditional expression in operand of fold-expression");
5063
5064 // Right fold.
5065 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5066 return finish_right_unary_fold_expr (expr1, op);
5067
5068 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5069 {
5070 cp_parser_error (parser, "mismatched operator in fold-expression");
5071 return error_mark_node;
5072 }
5073 cp_lexer_consume_token (parser->lexer);
5074
5075 // Binary left or right fold.
5076 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5077 if (expr2 == error_mark_node)
5078 return error_mark_node;
5079 return finish_binary_fold_expr (expr1, expr2, op);
5080 }
5081
5082 /* Parse a primary-expression.
5083
5084 primary-expression:
5085 literal
5086 this
5087 ( expression )
5088 id-expression
5089 lambda-expression (C++11)
5090
5091 GNU Extensions:
5092
5093 primary-expression:
5094 ( compound-statement )
5095 __builtin_va_arg ( assignment-expression , type-id )
5096 __builtin_offsetof ( type-id , offsetof-expression )
5097
5098 C++ Extensions:
5099 __has_nothrow_assign ( type-id )
5100 __has_nothrow_constructor ( type-id )
5101 __has_nothrow_copy ( type-id )
5102 __has_trivial_assign ( type-id )
5103 __has_trivial_constructor ( type-id )
5104 __has_trivial_copy ( type-id )
5105 __has_trivial_destructor ( type-id )
5106 __has_virtual_destructor ( type-id )
5107 __is_abstract ( type-id )
5108 __is_base_of ( type-id , type-id )
5109 __is_class ( type-id )
5110 __is_empty ( type-id )
5111 __is_enum ( type-id )
5112 __is_final ( type-id )
5113 __is_literal_type ( type-id )
5114 __is_pod ( type-id )
5115 __is_polymorphic ( type-id )
5116 __is_std_layout ( type-id )
5117 __is_trivial ( type-id )
5118 __is_union ( type-id )
5119
5120 Objective-C++ Extension:
5121
5122 primary-expression:
5123 objc-expression
5124
5125 literal:
5126 __null
5127
5128 ADDRESS_P is true iff this expression was immediately preceded by
5129 "&" and therefore might denote a pointer-to-member. CAST_P is true
5130 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5131 true iff this expression is a template argument.
5132
5133 Returns a representation of the expression. Upon return, *IDK
5134 indicates what kind of id-expression (if any) was present. */
5135
5136 static cp_expr
5137 cp_parser_primary_expression (cp_parser *parser,
5138 bool address_p,
5139 bool cast_p,
5140 bool template_arg_p,
5141 bool decltype_p,
5142 cp_id_kind *idk)
5143 {
5144 cp_token *token = NULL;
5145
5146 /* Assume the primary expression is not an id-expression. */
5147 *idk = CP_ID_KIND_NONE;
5148
5149 /* Peek at the next token. */
5150 token = cp_lexer_peek_token (parser->lexer);
5151 switch ((int) token->type)
5152 {
5153 /* literal:
5154 integer-literal
5155 character-literal
5156 floating-literal
5157 string-literal
5158 boolean-literal
5159 pointer-literal
5160 user-defined-literal */
5161 case CPP_CHAR:
5162 case CPP_CHAR16:
5163 case CPP_CHAR32:
5164 case CPP_WCHAR:
5165 case CPP_UTF8CHAR:
5166 case CPP_NUMBER:
5167 case CPP_PREPARSED_EXPR:
5168 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5169 return cp_parser_userdef_numeric_literal (parser);
5170 token = cp_lexer_consume_token (parser->lexer);
5171 if (TREE_CODE (token->u.value) == FIXED_CST)
5172 {
5173 error_at (token->location,
5174 "fixed-point types not supported in C++");
5175 return error_mark_node;
5176 }
5177 /* Floating-point literals are only allowed in an integral
5178 constant expression if they are cast to an integral or
5179 enumeration type. */
5180 if (TREE_CODE (token->u.value) == REAL_CST
5181 && parser->integral_constant_expression_p
5182 && pedantic)
5183 {
5184 /* CAST_P will be set even in invalid code like "int(2.7 +
5185 ...)". Therefore, we have to check that the next token
5186 is sure to end the cast. */
5187 if (cast_p)
5188 {
5189 cp_token *next_token;
5190
5191 next_token = cp_lexer_peek_token (parser->lexer);
5192 if (/* The comma at the end of an
5193 enumerator-definition. */
5194 next_token->type != CPP_COMMA
5195 /* The curly brace at the end of an enum-specifier. */
5196 && next_token->type != CPP_CLOSE_BRACE
5197 /* The end of a statement. */
5198 && next_token->type != CPP_SEMICOLON
5199 /* The end of the cast-expression. */
5200 && next_token->type != CPP_CLOSE_PAREN
5201 /* The end of an array bound. */
5202 && next_token->type != CPP_CLOSE_SQUARE
5203 /* The closing ">" in a template-argument-list. */
5204 && (next_token->type != CPP_GREATER
5205 || parser->greater_than_is_operator_p)
5206 /* C++0x only: A ">>" treated like two ">" tokens,
5207 in a template-argument-list. */
5208 && (next_token->type != CPP_RSHIFT
5209 || (cxx_dialect == cxx98)
5210 || parser->greater_than_is_operator_p))
5211 cast_p = false;
5212 }
5213
5214 /* If we are within a cast, then the constraint that the
5215 cast is to an integral or enumeration type will be
5216 checked at that point. If we are not within a cast, then
5217 this code is invalid. */
5218 if (!cast_p)
5219 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5220 }
5221 return cp_expr (token->u.value, token->location);
5222
5223 case CPP_CHAR_USERDEF:
5224 case CPP_CHAR16_USERDEF:
5225 case CPP_CHAR32_USERDEF:
5226 case CPP_WCHAR_USERDEF:
5227 case CPP_UTF8CHAR_USERDEF:
5228 return cp_parser_userdef_char_literal (parser);
5229
5230 case CPP_STRING:
5231 case CPP_STRING16:
5232 case CPP_STRING32:
5233 case CPP_WSTRING:
5234 case CPP_UTF8STRING:
5235 case CPP_STRING_USERDEF:
5236 case CPP_STRING16_USERDEF:
5237 case CPP_STRING32_USERDEF:
5238 case CPP_WSTRING_USERDEF:
5239 case CPP_UTF8STRING_USERDEF:
5240 /* ??? Should wide strings be allowed when parser->translate_strings_p
5241 is false (i.e. in attributes)? If not, we can kill the third
5242 argument to cp_parser_string_literal. */
5243 return cp_parser_string_literal (parser,
5244 parser->translate_strings_p,
5245 true);
5246
5247 case CPP_OPEN_PAREN:
5248 /* If we see `( { ' then we are looking at the beginning of
5249 a GNU statement-expression. */
5250 if (cp_parser_allow_gnu_extensions_p (parser)
5251 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5252 {
5253 /* Statement-expressions are not allowed by the standard. */
5254 pedwarn (token->location, OPT_Wpedantic,
5255 "ISO C++ forbids braced-groups within expressions");
5256
5257 /* And they're not allowed outside of a function-body; you
5258 cannot, for example, write:
5259
5260 int i = ({ int j = 3; j + 1; });
5261
5262 at class or namespace scope. */
5263 if (!parser->in_function_body
5264 || parser->in_template_argument_list_p)
5265 {
5266 error_at (token->location,
5267 "statement-expressions are not allowed outside "
5268 "functions nor in template-argument lists");
5269 cp_parser_skip_to_end_of_block_or_statement (parser);
5270 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5271 cp_lexer_consume_token (parser->lexer);
5272 return error_mark_node;
5273 }
5274 else
5275 return cp_parser_statement_expr (parser);
5276 }
5277 /* Otherwise it's a normal parenthesized expression. */
5278 {
5279 cp_expr expr;
5280 bool saved_greater_than_is_operator_p;
5281
5282 location_t open_paren_loc = token->location;
5283
5284 /* Consume the `('. */
5285 matching_parens parens;
5286 parens.consume_open (parser);
5287 /* Within a parenthesized expression, a `>' token is always
5288 the greater-than operator. */
5289 saved_greater_than_is_operator_p
5290 = parser->greater_than_is_operator_p;
5291 parser->greater_than_is_operator_p = true;
5292
5293 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5294 /* Left fold expression. */
5295 expr = NULL_TREE;
5296 else
5297 /* Parse the parenthesized expression. */
5298 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5299
5300 token = cp_lexer_peek_token (parser->lexer);
5301 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5302 {
5303 expr = cp_parser_fold_expression (parser, expr);
5304 if (expr != error_mark_node
5305 && cxx_dialect < cxx17
5306 && !in_system_header_at (input_location))
5307 pedwarn (input_location, 0, "fold-expressions only available "
5308 "with -std=c++17 or -std=gnu++17");
5309 }
5310 else
5311 /* Let the front end know that this expression was
5312 enclosed in parentheses. This matters in case, for
5313 example, the expression is of the form `A::B', since
5314 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5315 not. */
5316 expr = finish_parenthesized_expr (expr);
5317
5318 /* DR 705: Wrapping an unqualified name in parentheses
5319 suppresses arg-dependent lookup. We want to pass back
5320 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5321 (c++/37862), but none of the others. */
5322 if (*idk != CP_ID_KIND_QUALIFIED)
5323 *idk = CP_ID_KIND_NONE;
5324
5325 /* The `>' token might be the end of a template-id or
5326 template-parameter-list now. */
5327 parser->greater_than_is_operator_p
5328 = saved_greater_than_is_operator_p;
5329
5330 /* Consume the `)'. */
5331 token = cp_lexer_peek_token (parser->lexer);
5332 location_t close_paren_loc = token->location;
5333 expr.set_range (open_paren_loc, close_paren_loc);
5334 if (!parens.require_close (parser)
5335 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5336 cp_parser_skip_to_end_of_statement (parser);
5337
5338 return expr;
5339 }
5340
5341 case CPP_OPEN_SQUARE:
5342 {
5343 if (c_dialect_objc ())
5344 {
5345 /* We might have an Objective-C++ message. */
5346 cp_parser_parse_tentatively (parser);
5347 tree msg = cp_parser_objc_message_expression (parser);
5348 /* If that works out, we're done ... */
5349 if (cp_parser_parse_definitely (parser))
5350 return msg;
5351 /* ... else, fall though to see if it's a lambda. */
5352 }
5353 cp_expr lam = cp_parser_lambda_expression (parser);
5354 /* Don't warn about a failed tentative parse. */
5355 if (cp_parser_error_occurred (parser))
5356 return error_mark_node;
5357 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5358 return lam;
5359 }
5360
5361 case CPP_OBJC_STRING:
5362 if (c_dialect_objc ())
5363 /* We have an Objective-C++ string literal. */
5364 return cp_parser_objc_expression (parser);
5365 cp_parser_error (parser, "expected primary-expression");
5366 return error_mark_node;
5367
5368 case CPP_KEYWORD:
5369 switch (token->keyword)
5370 {
5371 /* These two are the boolean literals. */
5372 case RID_TRUE:
5373 cp_lexer_consume_token (parser->lexer);
5374 return cp_expr (boolean_true_node, token->location);
5375 case RID_FALSE:
5376 cp_lexer_consume_token (parser->lexer);
5377 return cp_expr (boolean_false_node, token->location);
5378
5379 /* The `__null' literal. */
5380 case RID_NULL:
5381 cp_lexer_consume_token (parser->lexer);
5382 return cp_expr (null_node, token->location);
5383
5384 /* The `nullptr' literal. */
5385 case RID_NULLPTR:
5386 cp_lexer_consume_token (parser->lexer);
5387 return cp_expr (nullptr_node, token->location);
5388
5389 /* Recognize the `this' keyword. */
5390 case RID_THIS:
5391 cp_lexer_consume_token (parser->lexer);
5392 if (parser->local_variables_forbidden_p)
5393 {
5394 error_at (token->location,
5395 "%<this%> may not be used in this context");
5396 return error_mark_node;
5397 }
5398 /* Pointers cannot appear in constant-expressions. */
5399 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5400 return error_mark_node;
5401 return cp_expr (finish_this_expr (), token->location);
5402
5403 /* The `operator' keyword can be the beginning of an
5404 id-expression. */
5405 case RID_OPERATOR:
5406 goto id_expression;
5407
5408 case RID_FUNCTION_NAME:
5409 case RID_PRETTY_FUNCTION_NAME:
5410 case RID_C99_FUNCTION_NAME:
5411 {
5412 non_integral_constant name;
5413
5414 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5415 __func__ are the names of variables -- but they are
5416 treated specially. Therefore, they are handled here,
5417 rather than relying on the generic id-expression logic
5418 below. Grammatically, these names are id-expressions.
5419
5420 Consume the token. */
5421 token = cp_lexer_consume_token (parser->lexer);
5422
5423 switch (token->keyword)
5424 {
5425 case RID_FUNCTION_NAME:
5426 name = NIC_FUNC_NAME;
5427 break;
5428 case RID_PRETTY_FUNCTION_NAME:
5429 name = NIC_PRETTY_FUNC;
5430 break;
5431 case RID_C99_FUNCTION_NAME:
5432 name = NIC_C99_FUNC;
5433 break;
5434 default:
5435 gcc_unreachable ();
5436 }
5437
5438 if (cp_parser_non_integral_constant_expression (parser, name))
5439 return error_mark_node;
5440
5441 /* Look up the name. */
5442 return finish_fname (token->u.value);
5443 }
5444
5445 case RID_VA_ARG:
5446 {
5447 tree expression;
5448 tree type;
5449 location_t type_location;
5450 location_t start_loc
5451 = cp_lexer_peek_token (parser->lexer)->location;
5452 /* The `__builtin_va_arg' construct is used to handle
5453 `va_arg'. Consume the `__builtin_va_arg' token. */
5454 cp_lexer_consume_token (parser->lexer);
5455 /* Look for the opening `('. */
5456 matching_parens parens;
5457 parens.require_open (parser);
5458 /* Now, parse the assignment-expression. */
5459 expression = cp_parser_assignment_expression (parser);
5460 /* Look for the `,'. */
5461 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5462 type_location = cp_lexer_peek_token (parser->lexer)->location;
5463 /* Parse the type-id. */
5464 {
5465 type_id_in_expr_sentinel s (parser);
5466 type = cp_parser_type_id (parser);
5467 }
5468 /* Look for the closing `)'. */
5469 location_t finish_loc
5470 = cp_lexer_peek_token (parser->lexer)->location;
5471 parens.require_close (parser);
5472 /* Using `va_arg' in a constant-expression is not
5473 allowed. */
5474 if (cp_parser_non_integral_constant_expression (parser,
5475 NIC_VA_ARG))
5476 return error_mark_node;
5477 /* Construct a location of the form:
5478 __builtin_va_arg (v, int)
5479 ~~~~~~~~~~~~~~~~~~~~~^~~~
5480 with the caret at the type, ranging from the start of the
5481 "__builtin_va_arg" token to the close paren. */
5482 location_t combined_loc
5483 = make_location (type_location, start_loc, finish_loc);
5484 return build_x_va_arg (combined_loc, expression, type);
5485 }
5486
5487 case RID_OFFSETOF:
5488 return cp_parser_builtin_offsetof (parser);
5489
5490 case RID_HAS_NOTHROW_ASSIGN:
5491 case RID_HAS_NOTHROW_CONSTRUCTOR:
5492 case RID_HAS_NOTHROW_COPY:
5493 case RID_HAS_TRIVIAL_ASSIGN:
5494 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5495 case RID_HAS_TRIVIAL_COPY:
5496 case RID_HAS_TRIVIAL_DESTRUCTOR:
5497 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5498 case RID_HAS_VIRTUAL_DESTRUCTOR:
5499 case RID_IS_ABSTRACT:
5500 case RID_IS_AGGREGATE:
5501 case RID_IS_BASE_OF:
5502 case RID_IS_CLASS:
5503 case RID_IS_EMPTY:
5504 case RID_IS_ENUM:
5505 case RID_IS_FINAL:
5506 case RID_IS_LITERAL_TYPE:
5507 case RID_IS_POD:
5508 case RID_IS_POLYMORPHIC:
5509 case RID_IS_SAME_AS:
5510 case RID_IS_STD_LAYOUT:
5511 case RID_IS_TRIVIAL:
5512 case RID_IS_TRIVIALLY_ASSIGNABLE:
5513 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5514 case RID_IS_TRIVIALLY_COPYABLE:
5515 case RID_IS_UNION:
5516 case RID_IS_ASSIGNABLE:
5517 case RID_IS_CONSTRUCTIBLE:
5518 return cp_parser_trait_expr (parser, token->keyword);
5519
5520 // C++ concepts
5521 case RID_REQUIRES:
5522 return cp_parser_requires_expression (parser);
5523
5524 /* Objective-C++ expressions. */
5525 case RID_AT_ENCODE:
5526 case RID_AT_PROTOCOL:
5527 case RID_AT_SELECTOR:
5528 return cp_parser_objc_expression (parser);
5529
5530 case RID_TEMPLATE:
5531 if (parser->in_function_body
5532 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5533 == CPP_LESS))
5534 {
5535 error_at (token->location,
5536 "a template declaration cannot appear at block scope");
5537 cp_parser_skip_to_end_of_block_or_statement (parser);
5538 return error_mark_node;
5539 }
5540 /* FALLTHRU */
5541 default:
5542 cp_parser_error (parser, "expected primary-expression");
5543 return error_mark_node;
5544 }
5545
5546 /* An id-expression can start with either an identifier, a
5547 `::' as the beginning of a qualified-id, or the "operator"
5548 keyword. */
5549 case CPP_NAME:
5550 case CPP_SCOPE:
5551 case CPP_TEMPLATE_ID:
5552 case CPP_NESTED_NAME_SPECIFIER:
5553 {
5554 id_expression:
5555 cp_expr id_expression;
5556 cp_expr decl;
5557 const char *error_msg;
5558 bool template_p;
5559 bool done;
5560 cp_token *id_expr_token;
5561
5562 /* Parse the id-expression. */
5563 id_expression
5564 = cp_parser_id_expression (parser,
5565 /*template_keyword_p=*/false,
5566 /*check_dependency_p=*/true,
5567 &template_p,
5568 /*declarator_p=*/false,
5569 /*optional_p=*/false);
5570 if (id_expression == error_mark_node)
5571 return error_mark_node;
5572 id_expr_token = token;
5573 token = cp_lexer_peek_token (parser->lexer);
5574 done = (token->type != CPP_OPEN_SQUARE
5575 && token->type != CPP_OPEN_PAREN
5576 && token->type != CPP_DOT
5577 && token->type != CPP_DEREF
5578 && token->type != CPP_PLUS_PLUS
5579 && token->type != CPP_MINUS_MINUS);
5580 /* If we have a template-id, then no further lookup is
5581 required. If the template-id was for a template-class, we
5582 will sometimes have a TYPE_DECL at this point. */
5583 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5584 || TREE_CODE (id_expression) == TYPE_DECL)
5585 decl = id_expression;
5586 /* Look up the name. */
5587 else
5588 {
5589 tree ambiguous_decls;
5590
5591 /* If we already know that this lookup is ambiguous, then
5592 we've already issued an error message; there's no reason
5593 to check again. */
5594 if (id_expr_token->type == CPP_NAME
5595 && id_expr_token->error_reported)
5596 {
5597 cp_parser_simulate_error (parser);
5598 return error_mark_node;
5599 }
5600
5601 decl = cp_parser_lookup_name (parser, id_expression,
5602 none_type,
5603 template_p,
5604 /*is_namespace=*/false,
5605 /*check_dependency=*/true,
5606 &ambiguous_decls,
5607 id_expression.get_location ());
5608 /* If the lookup was ambiguous, an error will already have
5609 been issued. */
5610 if (ambiguous_decls)
5611 return error_mark_node;
5612
5613 /* In Objective-C++, we may have an Objective-C 2.0
5614 dot-syntax for classes here. */
5615 if (c_dialect_objc ()
5616 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5617 && TREE_CODE (decl) == TYPE_DECL
5618 && objc_is_class_name (decl))
5619 {
5620 tree component;
5621 cp_lexer_consume_token (parser->lexer);
5622 component = cp_parser_identifier (parser);
5623 if (component == error_mark_node)
5624 return error_mark_node;
5625
5626 tree result = objc_build_class_component_ref (id_expression,
5627 component);
5628 /* Build a location of the form:
5629 expr.component
5630 ~~~~~^~~~~~~~~
5631 with caret at the start of the component name (at
5632 input_location), ranging from the start of the id_expression
5633 to the end of the component name. */
5634 location_t combined_loc
5635 = make_location (input_location, id_expression.get_start (),
5636 get_finish (input_location));
5637 protected_set_expr_location (result, combined_loc);
5638 return result;
5639 }
5640
5641 /* In Objective-C++, an instance variable (ivar) may be preferred
5642 to whatever cp_parser_lookup_name() found.
5643 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5644 rest of c-family, we have to do a little extra work to preserve
5645 any location information in cp_expr "decl". Given that
5646 objc_lookup_ivar is implemented in "c-family" and "objc", we
5647 have a trip through the pure "tree" type, rather than cp_expr.
5648 Naively copying it back to "decl" would implicitly give the
5649 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5650 store an EXPR_LOCATION. Hence we only update "decl" (and
5651 hence its location_t) if we get back a different tree node. */
5652 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5653 id_expression);
5654 if (decl_tree != decl.get_value ())
5655 decl = cp_expr (decl_tree);
5656
5657 /* If name lookup gives us a SCOPE_REF, then the
5658 qualifying scope was dependent. */
5659 if (TREE_CODE (decl) == SCOPE_REF)
5660 {
5661 /* At this point, we do not know if DECL is a valid
5662 integral constant expression. We assume that it is
5663 in fact such an expression, so that code like:
5664
5665 template <int N> struct A {
5666 int a[B<N>::i];
5667 };
5668
5669 is accepted. At template-instantiation time, we
5670 will check that B<N>::i is actually a constant. */
5671 return decl;
5672 }
5673 /* Check to see if DECL is a local variable in a context
5674 where that is forbidden. */
5675 if (parser->local_variables_forbidden_p
5676 && local_variable_p (decl))
5677 {
5678 error_at (id_expression.get_location (),
5679 "local variable %qD may not appear in this context",
5680 decl.get_value ());
5681 return error_mark_node;
5682 }
5683 }
5684
5685 decl = (finish_id_expression
5686 (id_expression, decl, parser->scope,
5687 idk,
5688 parser->integral_constant_expression_p,
5689 parser->allow_non_integral_constant_expression_p,
5690 &parser->non_integral_constant_expression_p,
5691 template_p, done, address_p,
5692 template_arg_p,
5693 &error_msg,
5694 id_expression.get_location ()));
5695 if (error_msg)
5696 cp_parser_error (parser, error_msg);
5697 decl.set_location (id_expression.get_location ());
5698 decl.set_range (id_expr_token->location, id_expression.get_finish ());
5699 return decl;
5700 }
5701
5702 /* Anything else is an error. */
5703 default:
5704 cp_parser_error (parser, "expected primary-expression");
5705 return error_mark_node;
5706 }
5707 }
5708
5709 static inline cp_expr
5710 cp_parser_primary_expression (cp_parser *parser,
5711 bool address_p,
5712 bool cast_p,
5713 bool template_arg_p,
5714 cp_id_kind *idk)
5715 {
5716 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5717 /*decltype*/false, idk);
5718 }
5719
5720 /* Parse an id-expression.
5721
5722 id-expression:
5723 unqualified-id
5724 qualified-id
5725
5726 qualified-id:
5727 :: [opt] nested-name-specifier template [opt] unqualified-id
5728 :: identifier
5729 :: operator-function-id
5730 :: template-id
5731
5732 Return a representation of the unqualified portion of the
5733 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5734 a `::' or nested-name-specifier.
5735
5736 Often, if the id-expression was a qualified-id, the caller will
5737 want to make a SCOPE_REF to represent the qualified-id. This
5738 function does not do this in order to avoid wastefully creating
5739 SCOPE_REFs when they are not required.
5740
5741 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5742 `template' keyword.
5743
5744 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5745 uninstantiated templates.
5746
5747 If *TEMPLATE_P is non-NULL, it is set to true iff the
5748 `template' keyword is used to explicitly indicate that the entity
5749 named is a template.
5750
5751 If DECLARATOR_P is true, the id-expression is appearing as part of
5752 a declarator, rather than as part of an expression. */
5753
5754 static cp_expr
5755 cp_parser_id_expression (cp_parser *parser,
5756 bool template_keyword_p,
5757 bool check_dependency_p,
5758 bool *template_p,
5759 bool declarator_p,
5760 bool optional_p)
5761 {
5762 bool global_scope_p;
5763 bool nested_name_specifier_p;
5764
5765 /* Assume the `template' keyword was not used. */
5766 if (template_p)
5767 *template_p = template_keyword_p;
5768
5769 /* Look for the optional `::' operator. */
5770 global_scope_p
5771 = (!template_keyword_p
5772 && (cp_parser_global_scope_opt (parser,
5773 /*current_scope_valid_p=*/false)
5774 != NULL_TREE));
5775
5776 /* Look for the optional nested-name-specifier. */
5777 nested_name_specifier_p
5778 = (cp_parser_nested_name_specifier_opt (parser,
5779 /*typename_keyword_p=*/false,
5780 check_dependency_p,
5781 /*type_p=*/false,
5782 declarator_p,
5783 template_keyword_p)
5784 != NULL_TREE);
5785
5786 /* If there is a nested-name-specifier, then we are looking at
5787 the first qualified-id production. */
5788 if (nested_name_specifier_p)
5789 {
5790 tree saved_scope;
5791 tree saved_object_scope;
5792 tree saved_qualifying_scope;
5793 cp_expr unqualified_id;
5794 bool is_template;
5795
5796 /* See if the next token is the `template' keyword. */
5797 if (!template_p)
5798 template_p = &is_template;
5799 *template_p = cp_parser_optional_template_keyword (parser);
5800 /* Name lookup we do during the processing of the
5801 unqualified-id might obliterate SCOPE. */
5802 saved_scope = parser->scope;
5803 saved_object_scope = parser->object_scope;
5804 saved_qualifying_scope = parser->qualifying_scope;
5805 /* Process the final unqualified-id. */
5806 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5807 check_dependency_p,
5808 declarator_p,
5809 /*optional_p=*/false);
5810 /* Restore the SAVED_SCOPE for our caller. */
5811 parser->scope = saved_scope;
5812 parser->object_scope = saved_object_scope;
5813 parser->qualifying_scope = saved_qualifying_scope;
5814
5815 return unqualified_id;
5816 }
5817 /* Otherwise, if we are in global scope, then we are looking at one
5818 of the other qualified-id productions. */
5819 else if (global_scope_p)
5820 {
5821 cp_token *token;
5822 tree id;
5823
5824 /* Peek at the next token. */
5825 token = cp_lexer_peek_token (parser->lexer);
5826
5827 /* If it's an identifier, and the next token is not a "<", then
5828 we can avoid the template-id case. This is an optimization
5829 for this common case. */
5830 if (token->type == CPP_NAME
5831 && !cp_parser_nth_token_starts_template_argument_list_p
5832 (parser, 2))
5833 return cp_parser_identifier (parser);
5834
5835 cp_parser_parse_tentatively (parser);
5836 /* Try a template-id. */
5837 id = cp_parser_template_id (parser,
5838 /*template_keyword_p=*/false,
5839 /*check_dependency_p=*/true,
5840 none_type,
5841 declarator_p);
5842 /* If that worked, we're done. */
5843 if (cp_parser_parse_definitely (parser))
5844 return id;
5845
5846 /* Peek at the next token. (Changes in the token buffer may
5847 have invalidated the pointer obtained above.) */
5848 token = cp_lexer_peek_token (parser->lexer);
5849
5850 switch (token->type)
5851 {
5852 case CPP_NAME:
5853 return cp_parser_identifier (parser);
5854
5855 case CPP_KEYWORD:
5856 if (token->keyword == RID_OPERATOR)
5857 return cp_parser_operator_function_id (parser);
5858 /* Fall through. */
5859
5860 default:
5861 cp_parser_error (parser, "expected id-expression");
5862 return error_mark_node;
5863 }
5864 }
5865 else
5866 return cp_parser_unqualified_id (parser, template_keyword_p,
5867 /*check_dependency_p=*/true,
5868 declarator_p,
5869 optional_p);
5870 }
5871
5872 /* Parse an unqualified-id.
5873
5874 unqualified-id:
5875 identifier
5876 operator-function-id
5877 conversion-function-id
5878 ~ class-name
5879 template-id
5880
5881 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5882 keyword, in a construct like `A::template ...'.
5883
5884 Returns a representation of unqualified-id. For the `identifier'
5885 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5886 production a BIT_NOT_EXPR is returned; the operand of the
5887 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5888 other productions, see the documentation accompanying the
5889 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5890 names are looked up in uninstantiated templates. If DECLARATOR_P
5891 is true, the unqualified-id is appearing as part of a declarator,
5892 rather than as part of an expression. */
5893
5894 static cp_expr
5895 cp_parser_unqualified_id (cp_parser* parser,
5896 bool template_keyword_p,
5897 bool check_dependency_p,
5898 bool declarator_p,
5899 bool optional_p)
5900 {
5901 cp_token *token;
5902
5903 /* Peek at the next token. */
5904 token = cp_lexer_peek_token (parser->lexer);
5905
5906 switch ((int) token->type)
5907 {
5908 case CPP_NAME:
5909 {
5910 tree id;
5911
5912 /* We don't know yet whether or not this will be a
5913 template-id. */
5914 cp_parser_parse_tentatively (parser);
5915 /* Try a template-id. */
5916 id = cp_parser_template_id (parser, template_keyword_p,
5917 check_dependency_p,
5918 none_type,
5919 declarator_p);
5920 /* If it worked, we're done. */
5921 if (cp_parser_parse_definitely (parser))
5922 return id;
5923 /* Otherwise, it's an ordinary identifier. */
5924 return cp_parser_identifier (parser);
5925 }
5926
5927 case CPP_TEMPLATE_ID:
5928 return cp_parser_template_id (parser, template_keyword_p,
5929 check_dependency_p,
5930 none_type,
5931 declarator_p);
5932
5933 case CPP_COMPL:
5934 {
5935 tree type_decl;
5936 tree qualifying_scope;
5937 tree object_scope;
5938 tree scope;
5939 bool done;
5940
5941 /* Consume the `~' token. */
5942 cp_lexer_consume_token (parser->lexer);
5943 /* Parse the class-name. The standard, as written, seems to
5944 say that:
5945
5946 template <typename T> struct S { ~S (); };
5947 template <typename T> S<T>::~S() {}
5948
5949 is invalid, since `~' must be followed by a class-name, but
5950 `S<T>' is dependent, and so not known to be a class.
5951 That's not right; we need to look in uninstantiated
5952 templates. A further complication arises from:
5953
5954 template <typename T> void f(T t) {
5955 t.T::~T();
5956 }
5957
5958 Here, it is not possible to look up `T' in the scope of `T'
5959 itself. We must look in both the current scope, and the
5960 scope of the containing complete expression.
5961
5962 Yet another issue is:
5963
5964 struct S {
5965 int S;
5966 ~S();
5967 };
5968
5969 S::~S() {}
5970
5971 The standard does not seem to say that the `S' in `~S'
5972 should refer to the type `S' and not the data member
5973 `S::S'. */
5974
5975 /* DR 244 says that we look up the name after the "~" in the
5976 same scope as we looked up the qualifying name. That idea
5977 isn't fully worked out; it's more complicated than that. */
5978 scope = parser->scope;
5979 object_scope = parser->object_scope;
5980 qualifying_scope = parser->qualifying_scope;
5981
5982 /* Check for invalid scopes. */
5983 if (scope == error_mark_node)
5984 {
5985 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5986 cp_lexer_consume_token (parser->lexer);
5987 return error_mark_node;
5988 }
5989 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5990 {
5991 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5992 error_at (token->location,
5993 "scope %qT before %<~%> is not a class-name",
5994 scope);
5995 cp_parser_simulate_error (parser);
5996 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5997 cp_lexer_consume_token (parser->lexer);
5998 return error_mark_node;
5999 }
6000 gcc_assert (!scope || TYPE_P (scope));
6001
6002 /* If the name is of the form "X::~X" it's OK even if X is a
6003 typedef. */
6004 token = cp_lexer_peek_token (parser->lexer);
6005 if (scope
6006 && token->type == CPP_NAME
6007 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6008 != CPP_LESS)
6009 && (token->u.value == TYPE_IDENTIFIER (scope)
6010 || (CLASS_TYPE_P (scope)
6011 && constructor_name_p (token->u.value, scope))))
6012 {
6013 cp_lexer_consume_token (parser->lexer);
6014 return build_nt (BIT_NOT_EXPR, scope);
6015 }
6016
6017 /* ~auto means the destructor of whatever the object is. */
6018 if (cp_parser_is_keyword (token, RID_AUTO))
6019 {
6020 if (cxx_dialect < cxx14)
6021 pedwarn (input_location, 0,
6022 "%<~auto%> only available with "
6023 "-std=c++14 or -std=gnu++14");
6024 cp_lexer_consume_token (parser->lexer);
6025 return build_nt (BIT_NOT_EXPR, make_auto ());
6026 }
6027
6028 /* If there was an explicit qualification (S::~T), first look
6029 in the scope given by the qualification (i.e., S).
6030
6031 Note: in the calls to cp_parser_class_name below we pass
6032 typename_type so that lookup finds the injected-class-name
6033 rather than the constructor. */
6034 done = false;
6035 type_decl = NULL_TREE;
6036 if (scope)
6037 {
6038 cp_parser_parse_tentatively (parser);
6039 type_decl = cp_parser_class_name (parser,
6040 /*typename_keyword_p=*/false,
6041 /*template_keyword_p=*/false,
6042 typename_type,
6043 /*check_dependency=*/false,
6044 /*class_head_p=*/false,
6045 declarator_p);
6046 if (cp_parser_parse_definitely (parser))
6047 done = true;
6048 }
6049 /* In "N::S::~S", look in "N" as well. */
6050 if (!done && scope && qualifying_scope)
6051 {
6052 cp_parser_parse_tentatively (parser);
6053 parser->scope = qualifying_scope;
6054 parser->object_scope = NULL_TREE;
6055 parser->qualifying_scope = NULL_TREE;
6056 type_decl
6057 = cp_parser_class_name (parser,
6058 /*typename_keyword_p=*/false,
6059 /*template_keyword_p=*/false,
6060 typename_type,
6061 /*check_dependency=*/false,
6062 /*class_head_p=*/false,
6063 declarator_p);
6064 if (cp_parser_parse_definitely (parser))
6065 done = true;
6066 }
6067 /* In "p->S::~T", look in the scope given by "*p" as well. */
6068 else if (!done && object_scope)
6069 {
6070 cp_parser_parse_tentatively (parser);
6071 parser->scope = object_scope;
6072 parser->object_scope = NULL_TREE;
6073 parser->qualifying_scope = NULL_TREE;
6074 type_decl
6075 = cp_parser_class_name (parser,
6076 /*typename_keyword_p=*/false,
6077 /*template_keyword_p=*/false,
6078 typename_type,
6079 /*check_dependency=*/false,
6080 /*class_head_p=*/false,
6081 declarator_p);
6082 if (cp_parser_parse_definitely (parser))
6083 done = true;
6084 }
6085 /* Look in the surrounding context. */
6086 if (!done)
6087 {
6088 parser->scope = NULL_TREE;
6089 parser->object_scope = NULL_TREE;
6090 parser->qualifying_scope = NULL_TREE;
6091 if (processing_template_decl)
6092 cp_parser_parse_tentatively (parser);
6093 type_decl
6094 = cp_parser_class_name (parser,
6095 /*typename_keyword_p=*/false,
6096 /*template_keyword_p=*/false,
6097 typename_type,
6098 /*check_dependency=*/false,
6099 /*class_head_p=*/false,
6100 declarator_p);
6101 if (processing_template_decl
6102 && ! cp_parser_parse_definitely (parser))
6103 {
6104 /* We couldn't find a type with this name. If we're parsing
6105 tentatively, fail and try something else. */
6106 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6107 {
6108 cp_parser_simulate_error (parser);
6109 return error_mark_node;
6110 }
6111 /* Otherwise, accept it and check for a match at instantiation
6112 time. */
6113 type_decl = cp_parser_identifier (parser);
6114 if (type_decl != error_mark_node)
6115 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6116 return type_decl;
6117 }
6118 }
6119 /* If an error occurred, assume that the name of the
6120 destructor is the same as the name of the qualifying
6121 class. That allows us to keep parsing after running
6122 into ill-formed destructor names. */
6123 if (type_decl == error_mark_node && scope)
6124 return build_nt (BIT_NOT_EXPR, scope);
6125 else if (type_decl == error_mark_node)
6126 return error_mark_node;
6127
6128 /* Check that destructor name and scope match. */
6129 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6130 {
6131 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6132 error_at (token->location,
6133 "declaration of %<~%T%> as member of %qT",
6134 type_decl, scope);
6135 cp_parser_simulate_error (parser);
6136 return error_mark_node;
6137 }
6138
6139 /* [class.dtor]
6140
6141 A typedef-name that names a class shall not be used as the
6142 identifier in the declarator for a destructor declaration. */
6143 if (declarator_p
6144 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6145 && !DECL_SELF_REFERENCE_P (type_decl)
6146 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6147 error_at (token->location,
6148 "typedef-name %qD used as destructor declarator",
6149 type_decl);
6150
6151 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6152 }
6153
6154 case CPP_KEYWORD:
6155 if (token->keyword == RID_OPERATOR)
6156 {
6157 cp_expr id;
6158
6159 /* This could be a template-id, so we try that first. */
6160 cp_parser_parse_tentatively (parser);
6161 /* Try a template-id. */
6162 id = cp_parser_template_id (parser, template_keyword_p,
6163 /*check_dependency_p=*/true,
6164 none_type,
6165 declarator_p);
6166 /* If that worked, we're done. */
6167 if (cp_parser_parse_definitely (parser))
6168 return id;
6169 /* We still don't know whether we're looking at an
6170 operator-function-id or a conversion-function-id. */
6171 cp_parser_parse_tentatively (parser);
6172 /* Try an operator-function-id. */
6173 id = cp_parser_operator_function_id (parser);
6174 /* If that didn't work, try a conversion-function-id. */
6175 if (!cp_parser_parse_definitely (parser))
6176 id = cp_parser_conversion_function_id (parser);
6177
6178 return id;
6179 }
6180 /* Fall through. */
6181
6182 default:
6183 if (optional_p)
6184 return NULL_TREE;
6185 cp_parser_error (parser, "expected unqualified-id");
6186 return error_mark_node;
6187 }
6188 }
6189
6190 /* Parse an (optional) nested-name-specifier.
6191
6192 nested-name-specifier: [C++98]
6193 class-or-namespace-name :: nested-name-specifier [opt]
6194 class-or-namespace-name :: template nested-name-specifier [opt]
6195
6196 nested-name-specifier: [C++0x]
6197 type-name ::
6198 namespace-name ::
6199 nested-name-specifier identifier ::
6200 nested-name-specifier template [opt] simple-template-id ::
6201
6202 PARSER->SCOPE should be set appropriately before this function is
6203 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6204 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6205 in name lookups.
6206
6207 Sets PARSER->SCOPE to the class (TYPE) or namespace
6208 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6209 it unchanged if there is no nested-name-specifier. Returns the new
6210 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6211
6212 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6213 part of a declaration and/or decl-specifier. */
6214
6215 static tree
6216 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6217 bool typename_keyword_p,
6218 bool check_dependency_p,
6219 bool type_p,
6220 bool is_declaration,
6221 bool template_keyword_p /* = false */)
6222 {
6223 bool success = false;
6224 cp_token_position start = 0;
6225 cp_token *token;
6226
6227 /* Remember where the nested-name-specifier starts. */
6228 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6229 {
6230 start = cp_lexer_token_position (parser->lexer, false);
6231 push_deferring_access_checks (dk_deferred);
6232 }
6233
6234 while (true)
6235 {
6236 tree new_scope;
6237 tree old_scope;
6238 tree saved_qualifying_scope;
6239
6240 /* Spot cases that cannot be the beginning of a
6241 nested-name-specifier. */
6242 token = cp_lexer_peek_token (parser->lexer);
6243
6244 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6245 the already parsed nested-name-specifier. */
6246 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6247 {
6248 /* Grab the nested-name-specifier and continue the loop. */
6249 cp_parser_pre_parsed_nested_name_specifier (parser);
6250 /* If we originally encountered this nested-name-specifier
6251 with IS_DECLARATION set to false, we will not have
6252 resolved TYPENAME_TYPEs, so we must do so here. */
6253 if (is_declaration
6254 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6255 {
6256 new_scope = resolve_typename_type (parser->scope,
6257 /*only_current_p=*/false);
6258 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6259 parser->scope = new_scope;
6260 }
6261 success = true;
6262 continue;
6263 }
6264
6265 /* Spot cases that cannot be the beginning of a
6266 nested-name-specifier. On the second and subsequent times
6267 through the loop, we look for the `template' keyword. */
6268 if (success && token->keyword == RID_TEMPLATE)
6269 ;
6270 /* A template-id can start a nested-name-specifier. */
6271 else if (token->type == CPP_TEMPLATE_ID)
6272 ;
6273 /* DR 743: decltype can be used in a nested-name-specifier. */
6274 else if (token_is_decltype (token))
6275 ;
6276 else
6277 {
6278 /* If the next token is not an identifier, then it is
6279 definitely not a type-name or namespace-name. */
6280 if (token->type != CPP_NAME)
6281 break;
6282 /* If the following token is neither a `<' (to begin a
6283 template-id), nor a `::', then we are not looking at a
6284 nested-name-specifier. */
6285 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6286
6287 if (token->type == CPP_COLON
6288 && parser->colon_corrects_to_scope_p
6289 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6290 {
6291 gcc_rich_location richloc (token->location);
6292 richloc.add_fixit_replace ("::");
6293 error_at (&richloc,
6294 "found %<:%> in nested-name-specifier, "
6295 "expected %<::%>");
6296 token->type = CPP_SCOPE;
6297 }
6298
6299 if (token->type != CPP_SCOPE
6300 && !cp_parser_nth_token_starts_template_argument_list_p
6301 (parser, 2))
6302 break;
6303 }
6304
6305 /* The nested-name-specifier is optional, so we parse
6306 tentatively. */
6307 cp_parser_parse_tentatively (parser);
6308
6309 /* Look for the optional `template' keyword, if this isn't the
6310 first time through the loop. */
6311 if (success)
6312 template_keyword_p = cp_parser_optional_template_keyword (parser);
6313
6314 /* Save the old scope since the name lookup we are about to do
6315 might destroy it. */
6316 old_scope = parser->scope;
6317 saved_qualifying_scope = parser->qualifying_scope;
6318 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6319 look up names in "X<T>::I" in order to determine that "Y" is
6320 a template. So, if we have a typename at this point, we make
6321 an effort to look through it. */
6322 if (is_declaration
6323 && !typename_keyword_p
6324 && parser->scope
6325 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6326 parser->scope = resolve_typename_type (parser->scope,
6327 /*only_current_p=*/false);
6328 /* Parse the qualifying entity. */
6329 new_scope
6330 = cp_parser_qualifying_entity (parser,
6331 typename_keyword_p,
6332 template_keyword_p,
6333 check_dependency_p,
6334 type_p,
6335 is_declaration);
6336 /* Look for the `::' token. */
6337 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6338
6339 /* If we found what we wanted, we keep going; otherwise, we're
6340 done. */
6341 if (!cp_parser_parse_definitely (parser))
6342 {
6343 bool error_p = false;
6344
6345 /* Restore the OLD_SCOPE since it was valid before the
6346 failed attempt at finding the last
6347 class-or-namespace-name. */
6348 parser->scope = old_scope;
6349 parser->qualifying_scope = saved_qualifying_scope;
6350
6351 /* If the next token is a decltype, and the one after that is a
6352 `::', then the decltype has failed to resolve to a class or
6353 enumeration type. Give this error even when parsing
6354 tentatively since it can't possibly be valid--and we're going
6355 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6356 won't get another chance.*/
6357 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6358 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6359 == CPP_SCOPE))
6360 {
6361 token = cp_lexer_consume_token (parser->lexer);
6362 error_at (token->location, "decltype evaluates to %qT, "
6363 "which is not a class or enumeration type",
6364 token->u.tree_check_value->value);
6365 parser->scope = error_mark_node;
6366 error_p = true;
6367 /* As below. */
6368 success = true;
6369 cp_lexer_consume_token (parser->lexer);
6370 }
6371
6372 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6373 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6374 {
6375 /* If we have a non-type template-id followed by ::, it can't
6376 possibly be valid. */
6377 token = cp_lexer_peek_token (parser->lexer);
6378 tree tid = token->u.tree_check_value->value;
6379 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6380 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6381 {
6382 tree tmpl = NULL_TREE;
6383 if (is_overloaded_fn (tid))
6384 {
6385 tree fns = get_fns (tid);
6386 if (OVL_SINGLE_P (fns))
6387 tmpl = OVL_FIRST (fns);
6388 error_at (token->location, "function template-id %qD "
6389 "in nested-name-specifier", tid);
6390 }
6391 else
6392 {
6393 /* Variable template. */
6394 tmpl = TREE_OPERAND (tid, 0);
6395 gcc_assert (variable_template_p (tmpl));
6396 error_at (token->location, "variable template-id %qD "
6397 "in nested-name-specifier", tid);
6398 }
6399 if (tmpl)
6400 inform (DECL_SOURCE_LOCATION (tmpl),
6401 "%qD declared here", tmpl);
6402
6403 parser->scope = error_mark_node;
6404 error_p = true;
6405 /* As below. */
6406 success = true;
6407 cp_lexer_consume_token (parser->lexer);
6408 cp_lexer_consume_token (parser->lexer);
6409 }
6410 }
6411
6412 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6413 break;
6414 /* If the next token is an identifier, and the one after
6415 that is a `::', then any valid interpretation would have
6416 found a class-or-namespace-name. */
6417 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6418 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6419 == CPP_SCOPE)
6420 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6421 != CPP_COMPL))
6422 {
6423 token = cp_lexer_consume_token (parser->lexer);
6424 if (!error_p)
6425 {
6426 if (!token->error_reported)
6427 {
6428 tree decl;
6429 tree ambiguous_decls;
6430
6431 decl = cp_parser_lookup_name (parser, token->u.value,
6432 none_type,
6433 /*is_template=*/false,
6434 /*is_namespace=*/false,
6435 /*check_dependency=*/true,
6436 &ambiguous_decls,
6437 token->location);
6438 if (TREE_CODE (decl) == TEMPLATE_DECL)
6439 error_at (token->location,
6440 "%qD used without template arguments",
6441 decl);
6442 else if (ambiguous_decls)
6443 {
6444 // cp_parser_lookup_name has the same diagnostic,
6445 // thus make sure to emit it at most once.
6446 if (cp_parser_uncommitted_to_tentative_parse_p
6447 (parser))
6448 {
6449 error_at (token->location,
6450 "reference to %qD is ambiguous",
6451 token->u.value);
6452 print_candidates (ambiguous_decls);
6453 }
6454 decl = error_mark_node;
6455 }
6456 else
6457 {
6458 if (cxx_dialect != cxx98)
6459 cp_parser_name_lookup_error
6460 (parser, token->u.value, decl, NLE_NOT_CXX98,
6461 token->location);
6462 else
6463 cp_parser_name_lookup_error
6464 (parser, token->u.value, decl, NLE_CXX98,
6465 token->location);
6466 }
6467 }
6468 parser->scope = error_mark_node;
6469 error_p = true;
6470 /* Treat this as a successful nested-name-specifier
6471 due to:
6472
6473 [basic.lookup.qual]
6474
6475 If the name found is not a class-name (clause
6476 _class_) or namespace-name (_namespace.def_), the
6477 program is ill-formed. */
6478 success = true;
6479 }
6480 cp_lexer_consume_token (parser->lexer);
6481 }
6482 break;
6483 }
6484 /* We've found one valid nested-name-specifier. */
6485 success = true;
6486 /* Name lookup always gives us a DECL. */
6487 if (TREE_CODE (new_scope) == TYPE_DECL)
6488 new_scope = TREE_TYPE (new_scope);
6489 /* Uses of "template" must be followed by actual templates. */
6490 if (template_keyword_p
6491 && !(CLASS_TYPE_P (new_scope)
6492 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6493 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6494 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6495 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6496 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6497 == TEMPLATE_ID_EXPR)))
6498 permerror (input_location, TYPE_P (new_scope)
6499 ? G_("%qT is not a template")
6500 : G_("%qD is not a template"),
6501 new_scope);
6502 /* If it is a class scope, try to complete it; we are about to
6503 be looking up names inside the class. */
6504 if (TYPE_P (new_scope)
6505 /* Since checking types for dependency can be expensive,
6506 avoid doing it if the type is already complete. */
6507 && !COMPLETE_TYPE_P (new_scope)
6508 /* Do not try to complete dependent types. */
6509 && !dependent_type_p (new_scope))
6510 {
6511 new_scope = complete_type (new_scope);
6512 /* If it is a typedef to current class, use the current
6513 class instead, as the typedef won't have any names inside
6514 it yet. */
6515 if (!COMPLETE_TYPE_P (new_scope)
6516 && currently_open_class (new_scope))
6517 new_scope = TYPE_MAIN_VARIANT (new_scope);
6518 }
6519 /* Make sure we look in the right scope the next time through
6520 the loop. */
6521 parser->scope = new_scope;
6522 }
6523
6524 /* If parsing tentatively, replace the sequence of tokens that makes
6525 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6526 token. That way, should we re-parse the token stream, we will
6527 not have to repeat the effort required to do the parse, nor will
6528 we issue duplicate error messages. */
6529 if (success && start)
6530 {
6531 cp_token *token;
6532
6533 token = cp_lexer_token_at (parser->lexer, start);
6534 /* Reset the contents of the START token. */
6535 token->type = CPP_NESTED_NAME_SPECIFIER;
6536 /* Retrieve any deferred checks. Do not pop this access checks yet
6537 so the memory will not be reclaimed during token replacing below. */
6538 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6539 token->u.tree_check_value->value = parser->scope;
6540 token->u.tree_check_value->checks = get_deferred_access_checks ();
6541 token->u.tree_check_value->qualifying_scope =
6542 parser->qualifying_scope;
6543 token->keyword = RID_MAX;
6544
6545 /* Purge all subsequent tokens. */
6546 cp_lexer_purge_tokens_after (parser->lexer, start);
6547 }
6548
6549 if (start)
6550 pop_to_parent_deferring_access_checks ();
6551
6552 return success ? parser->scope : NULL_TREE;
6553 }
6554
6555 /* Parse a nested-name-specifier. See
6556 cp_parser_nested_name_specifier_opt for details. This function
6557 behaves identically, except that it will an issue an error if no
6558 nested-name-specifier is present. */
6559
6560 static tree
6561 cp_parser_nested_name_specifier (cp_parser *parser,
6562 bool typename_keyword_p,
6563 bool check_dependency_p,
6564 bool type_p,
6565 bool is_declaration)
6566 {
6567 tree scope;
6568
6569 /* Look for the nested-name-specifier. */
6570 scope = cp_parser_nested_name_specifier_opt (parser,
6571 typename_keyword_p,
6572 check_dependency_p,
6573 type_p,
6574 is_declaration);
6575 /* If it was not present, issue an error message. */
6576 if (!scope)
6577 {
6578 cp_parser_error (parser, "expected nested-name-specifier");
6579 parser->scope = NULL_TREE;
6580 }
6581
6582 return scope;
6583 }
6584
6585 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6586 this is either a class-name or a namespace-name (which corresponds
6587 to the class-or-namespace-name production in the grammar). For
6588 C++0x, it can also be a type-name that refers to an enumeration
6589 type or a simple-template-id.
6590
6591 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6592 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6593 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6594 TYPE_P is TRUE iff the next name should be taken as a class-name,
6595 even the same name is declared to be another entity in the same
6596 scope.
6597
6598 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6599 specified by the class-or-namespace-name. If neither is found the
6600 ERROR_MARK_NODE is returned. */
6601
6602 static tree
6603 cp_parser_qualifying_entity (cp_parser *parser,
6604 bool typename_keyword_p,
6605 bool template_keyword_p,
6606 bool check_dependency_p,
6607 bool type_p,
6608 bool is_declaration)
6609 {
6610 tree saved_scope;
6611 tree saved_qualifying_scope;
6612 tree saved_object_scope;
6613 tree scope;
6614 bool only_class_p;
6615 bool successful_parse_p;
6616
6617 /* DR 743: decltype can appear in a nested-name-specifier. */
6618 if (cp_lexer_next_token_is_decltype (parser->lexer))
6619 {
6620 scope = cp_parser_decltype (parser);
6621 if (TREE_CODE (scope) != ENUMERAL_TYPE
6622 && !MAYBE_CLASS_TYPE_P (scope))
6623 {
6624 cp_parser_simulate_error (parser);
6625 return error_mark_node;
6626 }
6627 if (TYPE_NAME (scope))
6628 scope = TYPE_NAME (scope);
6629 return scope;
6630 }
6631
6632 /* Before we try to parse the class-name, we must save away the
6633 current PARSER->SCOPE since cp_parser_class_name will destroy
6634 it. */
6635 saved_scope = parser->scope;
6636 saved_qualifying_scope = parser->qualifying_scope;
6637 saved_object_scope = parser->object_scope;
6638 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6639 there is no need to look for a namespace-name. */
6640 only_class_p = template_keyword_p
6641 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6642 if (!only_class_p)
6643 cp_parser_parse_tentatively (parser);
6644 scope = cp_parser_class_name (parser,
6645 typename_keyword_p,
6646 template_keyword_p,
6647 type_p ? class_type : none_type,
6648 check_dependency_p,
6649 /*class_head_p=*/false,
6650 is_declaration,
6651 /*enum_ok=*/cxx_dialect > cxx98);
6652 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6653 /* If that didn't work, try for a namespace-name. */
6654 if (!only_class_p && !successful_parse_p)
6655 {
6656 /* Restore the saved scope. */
6657 parser->scope = saved_scope;
6658 parser->qualifying_scope = saved_qualifying_scope;
6659 parser->object_scope = saved_object_scope;
6660 /* If we are not looking at an identifier followed by the scope
6661 resolution operator, then this is not part of a
6662 nested-name-specifier. (Note that this function is only used
6663 to parse the components of a nested-name-specifier.) */
6664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6665 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6666 return error_mark_node;
6667 scope = cp_parser_namespace_name (parser);
6668 }
6669
6670 return scope;
6671 }
6672
6673 /* Return true if we are looking at a compound-literal, false otherwise. */
6674
6675 static bool
6676 cp_parser_compound_literal_p (cp_parser *parser)
6677 {
6678 cp_lexer_save_tokens (parser->lexer);
6679
6680 /* Skip tokens until the next token is a closing parenthesis.
6681 If we find the closing `)', and the next token is a `{', then
6682 we are looking at a compound-literal. */
6683 bool compound_literal_p
6684 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6685 /*consume_paren=*/true)
6686 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6687
6688 /* Roll back the tokens we skipped. */
6689 cp_lexer_rollback_tokens (parser->lexer);
6690
6691 return compound_literal_p;
6692 }
6693
6694 /* Return true if EXPR is the integer constant zero or a complex constant
6695 of zero, without any folding, but ignoring location wrappers. */
6696
6697 bool
6698 literal_integer_zerop (const_tree expr)
6699 {
6700 return (location_wrapper_p (expr)
6701 && integer_zerop (TREE_OPERAND (expr, 0)));
6702 }
6703
6704 /* Parse a postfix-expression.
6705
6706 postfix-expression:
6707 primary-expression
6708 postfix-expression [ expression ]
6709 postfix-expression ( expression-list [opt] )
6710 simple-type-specifier ( expression-list [opt] )
6711 typename :: [opt] nested-name-specifier identifier
6712 ( expression-list [opt] )
6713 typename :: [opt] nested-name-specifier template [opt] template-id
6714 ( expression-list [opt] )
6715 postfix-expression . template [opt] id-expression
6716 postfix-expression -> template [opt] id-expression
6717 postfix-expression . pseudo-destructor-name
6718 postfix-expression -> pseudo-destructor-name
6719 postfix-expression ++
6720 postfix-expression --
6721 dynamic_cast < type-id > ( expression )
6722 static_cast < type-id > ( expression )
6723 reinterpret_cast < type-id > ( expression )
6724 const_cast < type-id > ( expression )
6725 typeid ( expression )
6726 typeid ( type-id )
6727
6728 GNU Extension:
6729
6730 postfix-expression:
6731 ( type-id ) { initializer-list , [opt] }
6732
6733 This extension is a GNU version of the C99 compound-literal
6734 construct. (The C99 grammar uses `type-name' instead of `type-id',
6735 but they are essentially the same concept.)
6736
6737 If ADDRESS_P is true, the postfix expression is the operand of the
6738 `&' operator. CAST_P is true if this expression is the target of a
6739 cast.
6740
6741 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6742 class member access expressions [expr.ref].
6743
6744 Returns a representation of the expression. */
6745
6746 static cp_expr
6747 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6748 bool member_access_only_p, bool decltype_p,
6749 cp_id_kind * pidk_return)
6750 {
6751 cp_token *token;
6752 location_t loc;
6753 enum rid keyword;
6754 cp_id_kind idk = CP_ID_KIND_NONE;
6755 cp_expr postfix_expression = NULL_TREE;
6756 bool is_member_access = false;
6757
6758 /* Peek at the next token. */
6759 token = cp_lexer_peek_token (parser->lexer);
6760 loc = token->location;
6761 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6762
6763 /* Some of the productions are determined by keywords. */
6764 keyword = token->keyword;
6765 switch (keyword)
6766 {
6767 case RID_DYNCAST:
6768 case RID_STATCAST:
6769 case RID_REINTCAST:
6770 case RID_CONSTCAST:
6771 {
6772 tree type;
6773 cp_expr expression;
6774 const char *saved_message;
6775 bool saved_in_type_id_in_expr_p;
6776
6777 /* All of these can be handled in the same way from the point
6778 of view of parsing. Begin by consuming the token
6779 identifying the cast. */
6780 cp_lexer_consume_token (parser->lexer);
6781
6782 /* New types cannot be defined in the cast. */
6783 saved_message = parser->type_definition_forbidden_message;
6784 parser->type_definition_forbidden_message
6785 = G_("types may not be defined in casts");
6786
6787 /* Look for the opening `<'. */
6788 cp_parser_require (parser, CPP_LESS, RT_LESS);
6789 /* Parse the type to which we are casting. */
6790 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6791 parser->in_type_id_in_expr_p = true;
6792 type = cp_parser_type_id (parser);
6793 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6794 /* Look for the closing `>'. */
6795 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6796 /* Restore the old message. */
6797 parser->type_definition_forbidden_message = saved_message;
6798
6799 bool saved_greater_than_is_operator_p
6800 = parser->greater_than_is_operator_p;
6801 parser->greater_than_is_operator_p = true;
6802
6803 /* And the expression which is being cast. */
6804 matching_parens parens;
6805 parens.require_open (parser);
6806 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6807 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6808 RT_CLOSE_PAREN);
6809 location_t end_loc = close_paren ?
6810 close_paren->location : UNKNOWN_LOCATION;
6811
6812 parser->greater_than_is_operator_p
6813 = saved_greater_than_is_operator_p;
6814
6815 /* Only type conversions to integral or enumeration types
6816 can be used in constant-expressions. */
6817 if (!cast_valid_in_integral_constant_expression_p (type)
6818 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6819 {
6820 postfix_expression = error_mark_node;
6821 break;
6822 }
6823
6824 switch (keyword)
6825 {
6826 case RID_DYNCAST:
6827 postfix_expression
6828 = build_dynamic_cast (type, expression, tf_warning_or_error);
6829 break;
6830 case RID_STATCAST:
6831 postfix_expression
6832 = build_static_cast (type, expression, tf_warning_or_error);
6833 break;
6834 case RID_REINTCAST:
6835 postfix_expression
6836 = build_reinterpret_cast (type, expression,
6837 tf_warning_or_error);
6838 break;
6839 case RID_CONSTCAST:
6840 postfix_expression
6841 = build_const_cast (type, expression, tf_warning_or_error);
6842 break;
6843 default:
6844 gcc_unreachable ();
6845 }
6846
6847 /* Construct a location e.g. :
6848 reinterpret_cast <int *> (expr)
6849 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6850 ranging from the start of the "*_cast" token to the final closing
6851 paren, with the caret at the start. */
6852 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6853 postfix_expression.set_location (cp_cast_loc);
6854 }
6855 break;
6856
6857 case RID_TYPEID:
6858 {
6859 tree type;
6860 const char *saved_message;
6861 bool saved_in_type_id_in_expr_p;
6862
6863 /* Consume the `typeid' token. */
6864 cp_lexer_consume_token (parser->lexer);
6865 /* Look for the `(' token. */
6866 matching_parens parens;
6867 parens.require_open (parser);
6868 /* Types cannot be defined in a `typeid' expression. */
6869 saved_message = parser->type_definition_forbidden_message;
6870 parser->type_definition_forbidden_message
6871 = G_("types may not be defined in a %<typeid%> expression");
6872 /* We can't be sure yet whether we're looking at a type-id or an
6873 expression. */
6874 cp_parser_parse_tentatively (parser);
6875 /* Try a type-id first. */
6876 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6877 parser->in_type_id_in_expr_p = true;
6878 type = cp_parser_type_id (parser);
6879 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6880 /* Look for the `)' token. Otherwise, we can't be sure that
6881 we're not looking at an expression: consider `typeid (int
6882 (3))', for example. */
6883 cp_token *close_paren = parens.require_close (parser);
6884 /* If all went well, simply lookup the type-id. */
6885 if (cp_parser_parse_definitely (parser))
6886 postfix_expression = get_typeid (type, tf_warning_or_error);
6887 /* Otherwise, fall back to the expression variant. */
6888 else
6889 {
6890 tree expression;
6891
6892 /* Look for an expression. */
6893 expression = cp_parser_expression (parser, & idk);
6894 /* Compute its typeid. */
6895 postfix_expression = build_typeid (expression, tf_warning_or_error);
6896 /* Look for the `)' token. */
6897 close_paren = parens.require_close (parser);
6898 }
6899 /* Restore the saved message. */
6900 parser->type_definition_forbidden_message = saved_message;
6901 /* `typeid' may not appear in an integral constant expression. */
6902 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6903 postfix_expression = error_mark_node;
6904
6905 /* Construct a location e.g. :
6906 typeid (expr)
6907 ^~~~~~~~~~~~~
6908 ranging from the start of the "typeid" token to the final closing
6909 paren, with the caret at the start. */
6910 if (close_paren)
6911 {
6912 location_t typeid_loc
6913 = make_location (start_loc, start_loc, close_paren->location);
6914 postfix_expression.set_location (typeid_loc);
6915 postfix_expression.maybe_add_location_wrapper ();
6916 }
6917 }
6918 break;
6919
6920 case RID_TYPENAME:
6921 {
6922 tree type;
6923 /* The syntax permitted here is the same permitted for an
6924 elaborated-type-specifier. */
6925 ++parser->prevent_constrained_type_specifiers;
6926 type = cp_parser_elaborated_type_specifier (parser,
6927 /*is_friend=*/false,
6928 /*is_declaration=*/false);
6929 --parser->prevent_constrained_type_specifiers;
6930 postfix_expression = cp_parser_functional_cast (parser, type);
6931 }
6932 break;
6933
6934 case RID_ADDRESSOF:
6935 case RID_BUILTIN_SHUFFLE:
6936 case RID_BUILTIN_LAUNDER:
6937 {
6938 vec<tree, va_gc> *vec;
6939 unsigned int i;
6940 tree p;
6941
6942 cp_lexer_consume_token (parser->lexer);
6943 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6944 /*cast_p=*/false, /*allow_expansion_p=*/true,
6945 /*non_constant_p=*/NULL);
6946 if (vec == NULL)
6947 {
6948 postfix_expression = error_mark_node;
6949 break;
6950 }
6951
6952 FOR_EACH_VEC_ELT (*vec, i, p)
6953 mark_exp_read (p);
6954
6955 switch (keyword)
6956 {
6957 case RID_ADDRESSOF:
6958 if (vec->length () == 1)
6959 postfix_expression
6960 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6961 else
6962 {
6963 error_at (loc, "wrong number of arguments to "
6964 "%<__builtin_addressof%>");
6965 postfix_expression = error_mark_node;
6966 }
6967 break;
6968
6969 case RID_BUILTIN_LAUNDER:
6970 if (vec->length () == 1)
6971 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6972 tf_warning_or_error);
6973 else
6974 {
6975 error_at (loc, "wrong number of arguments to "
6976 "%<__builtin_launder%>");
6977 postfix_expression = error_mark_node;
6978 }
6979 break;
6980
6981 case RID_BUILTIN_SHUFFLE:
6982 if (vec->length () == 2)
6983 postfix_expression
6984 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6985 (*vec)[1], tf_warning_or_error);
6986 else if (vec->length () == 3)
6987 postfix_expression
6988 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6989 (*vec)[2], tf_warning_or_error);
6990 else
6991 {
6992 error_at (loc, "wrong number of arguments to "
6993 "%<__builtin_shuffle%>");
6994 postfix_expression = error_mark_node;
6995 }
6996 break;
6997
6998 default:
6999 gcc_unreachable ();
7000 }
7001 break;
7002 }
7003
7004 default:
7005 {
7006 tree type;
7007
7008 /* If the next thing is a simple-type-specifier, we may be
7009 looking at a functional cast. We could also be looking at
7010 an id-expression. So, we try the functional cast, and if
7011 that doesn't work we fall back to the primary-expression. */
7012 cp_parser_parse_tentatively (parser);
7013 /* Look for the simple-type-specifier. */
7014 ++parser->prevent_constrained_type_specifiers;
7015 type = cp_parser_simple_type_specifier (parser,
7016 /*decl_specs=*/NULL,
7017 CP_PARSER_FLAGS_NONE);
7018 --parser->prevent_constrained_type_specifiers;
7019 /* Parse the cast itself. */
7020 if (!cp_parser_error_occurred (parser))
7021 postfix_expression
7022 = cp_parser_functional_cast (parser, type);
7023 /* If that worked, we're done. */
7024 if (cp_parser_parse_definitely (parser))
7025 break;
7026
7027 /* If the functional-cast didn't work out, try a
7028 compound-literal. */
7029 if (cp_parser_allow_gnu_extensions_p (parser)
7030 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7031 {
7032 cp_expr initializer = NULL_TREE;
7033
7034 cp_parser_parse_tentatively (parser);
7035
7036 matching_parens parens;
7037 parens.consume_open (parser);
7038
7039 /* Avoid calling cp_parser_type_id pointlessly, see comment
7040 in cp_parser_cast_expression about c++/29234. */
7041 if (!cp_parser_compound_literal_p (parser))
7042 cp_parser_simulate_error (parser);
7043 else
7044 {
7045 /* Parse the type. */
7046 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7047 parser->in_type_id_in_expr_p = true;
7048 type = cp_parser_type_id (parser);
7049 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7050 parens.require_close (parser);
7051 }
7052
7053 /* If things aren't going well, there's no need to
7054 keep going. */
7055 if (!cp_parser_error_occurred (parser))
7056 {
7057 bool non_constant_p;
7058 /* Parse the brace-enclosed initializer list. */
7059 initializer = cp_parser_braced_list (parser,
7060 &non_constant_p);
7061 }
7062 /* If that worked, we're definitely looking at a
7063 compound-literal expression. */
7064 if (cp_parser_parse_definitely (parser))
7065 {
7066 /* Warn the user that a compound literal is not
7067 allowed in standard C++. */
7068 pedwarn (input_location, OPT_Wpedantic,
7069 "ISO C++ forbids compound-literals");
7070 /* For simplicity, we disallow compound literals in
7071 constant-expressions. We could
7072 allow compound literals of integer type, whose
7073 initializer was a constant, in constant
7074 expressions. Permitting that usage, as a further
7075 extension, would not change the meaning of any
7076 currently accepted programs. (Of course, as
7077 compound literals are not part of ISO C++, the
7078 standard has nothing to say.) */
7079 if (cp_parser_non_integral_constant_expression (parser,
7080 NIC_NCC))
7081 {
7082 postfix_expression = error_mark_node;
7083 break;
7084 }
7085 /* Form the representation of the compound-literal. */
7086 postfix_expression
7087 = finish_compound_literal (type, initializer,
7088 tf_warning_or_error, fcl_c99);
7089 postfix_expression.set_location (initializer.get_location ());
7090 break;
7091 }
7092 }
7093
7094 /* It must be a primary-expression. */
7095 postfix_expression
7096 = cp_parser_primary_expression (parser, address_p, cast_p,
7097 /*template_arg_p=*/false,
7098 decltype_p,
7099 &idk);
7100 }
7101 break;
7102 }
7103
7104 /* Note that we don't need to worry about calling build_cplus_new on a
7105 class-valued CALL_EXPR in decltype when it isn't the end of the
7106 postfix-expression; unary_complex_lvalue will take care of that for
7107 all these cases. */
7108
7109 /* Keep looping until the postfix-expression is complete. */
7110 while (true)
7111 {
7112 if (idk == CP_ID_KIND_UNQUALIFIED
7113 && identifier_p (postfix_expression)
7114 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7115 /* It is not a Koenig lookup function call. */
7116 postfix_expression
7117 = unqualified_name_lookup_error (postfix_expression);
7118
7119 /* Peek at the next token. */
7120 token = cp_lexer_peek_token (parser->lexer);
7121
7122 switch (token->type)
7123 {
7124 case CPP_OPEN_SQUARE:
7125 if (cp_next_tokens_can_be_std_attribute_p (parser))
7126 {
7127 cp_parser_error (parser,
7128 "two consecutive %<[%> shall "
7129 "only introduce an attribute");
7130 return error_mark_node;
7131 }
7132 postfix_expression
7133 = cp_parser_postfix_open_square_expression (parser,
7134 postfix_expression,
7135 false,
7136 decltype_p);
7137 postfix_expression.set_range (start_loc,
7138 postfix_expression.get_location ());
7139
7140 idk = CP_ID_KIND_NONE;
7141 is_member_access = false;
7142 break;
7143
7144 case CPP_OPEN_PAREN:
7145 /* postfix-expression ( expression-list [opt] ) */
7146 {
7147 bool koenig_p;
7148 bool is_builtin_constant_p;
7149 bool saved_integral_constant_expression_p = false;
7150 bool saved_non_integral_constant_expression_p = false;
7151 tsubst_flags_t complain = complain_flags (decltype_p);
7152 vec<tree, va_gc> *args;
7153 location_t close_paren_loc = UNKNOWN_LOCATION;
7154
7155 is_member_access = false;
7156
7157 is_builtin_constant_p
7158 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7159 if (is_builtin_constant_p)
7160 {
7161 /* The whole point of __builtin_constant_p is to allow
7162 non-constant expressions to appear as arguments. */
7163 saved_integral_constant_expression_p
7164 = parser->integral_constant_expression_p;
7165 saved_non_integral_constant_expression_p
7166 = parser->non_integral_constant_expression_p;
7167 parser->integral_constant_expression_p = false;
7168 }
7169 args = (cp_parser_parenthesized_expression_list
7170 (parser, non_attr,
7171 /*cast_p=*/false, /*allow_expansion_p=*/true,
7172 /*non_constant_p=*/NULL,
7173 /*close_paren_loc=*/&close_paren_loc,
7174 /*wrap_locations_p=*/true));
7175 if (is_builtin_constant_p)
7176 {
7177 parser->integral_constant_expression_p
7178 = saved_integral_constant_expression_p;
7179 parser->non_integral_constant_expression_p
7180 = saved_non_integral_constant_expression_p;
7181 }
7182
7183 if (args == NULL)
7184 {
7185 postfix_expression = error_mark_node;
7186 break;
7187 }
7188
7189 /* Function calls are not permitted in
7190 constant-expressions. */
7191 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7192 && cp_parser_non_integral_constant_expression (parser,
7193 NIC_FUNC_CALL))
7194 {
7195 postfix_expression = error_mark_node;
7196 release_tree_vector (args);
7197 break;
7198 }
7199
7200 koenig_p = false;
7201 if (idk == CP_ID_KIND_UNQUALIFIED
7202 || idk == CP_ID_KIND_TEMPLATE_ID)
7203 {
7204 if (identifier_p (postfix_expression)
7205 /* In C++2A, we may need to perform ADL for a template
7206 name. */
7207 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7208 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7209 {
7210 if (!args->is_empty ())
7211 {
7212 koenig_p = true;
7213 if (!any_type_dependent_arguments_p (args))
7214 postfix_expression
7215 = perform_koenig_lookup (postfix_expression, args,
7216 complain);
7217 }
7218 else
7219 postfix_expression
7220 = unqualified_fn_lookup_error (postfix_expression);
7221 }
7222 /* We do not perform argument-dependent lookup if
7223 normal lookup finds a non-function, in accordance
7224 with the expected resolution of DR 218. */
7225 else if (!args->is_empty ()
7226 && is_overloaded_fn (postfix_expression))
7227 {
7228 tree fn = get_first_fn (postfix_expression);
7229 fn = STRIP_TEMPLATE (fn);
7230
7231 /* Do not do argument dependent lookup if regular
7232 lookup finds a member function or a block-scope
7233 function declaration. [basic.lookup.argdep]/3 */
7234 if (!DECL_FUNCTION_MEMBER_P (fn)
7235 && !DECL_LOCAL_FUNCTION_P (fn))
7236 {
7237 koenig_p = true;
7238 if (!any_type_dependent_arguments_p (args))
7239 postfix_expression
7240 = perform_koenig_lookup (postfix_expression, args,
7241 complain);
7242 }
7243 }
7244 }
7245
7246 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7247 {
7248 tree instance = TREE_OPERAND (postfix_expression, 0);
7249 tree fn = TREE_OPERAND (postfix_expression, 1);
7250
7251 if (processing_template_decl
7252 && (type_dependent_object_expression_p (instance)
7253 || (!BASELINK_P (fn)
7254 && TREE_CODE (fn) != FIELD_DECL)
7255 || type_dependent_expression_p (fn)
7256 || any_type_dependent_arguments_p (args)))
7257 {
7258 maybe_generic_this_capture (instance, fn);
7259 postfix_expression
7260 = build_min_nt_call_vec (postfix_expression, args);
7261 release_tree_vector (args);
7262 break;
7263 }
7264
7265 if (BASELINK_P (fn))
7266 {
7267 postfix_expression
7268 = (build_new_method_call
7269 (instance, fn, &args, NULL_TREE,
7270 (idk == CP_ID_KIND_QUALIFIED
7271 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7272 : LOOKUP_NORMAL),
7273 /*fn_p=*/NULL,
7274 complain));
7275 }
7276 else
7277 postfix_expression
7278 = finish_call_expr (postfix_expression, &args,
7279 /*disallow_virtual=*/false,
7280 /*koenig_p=*/false,
7281 complain);
7282 }
7283 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7284 || TREE_CODE (postfix_expression) == MEMBER_REF
7285 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7286 postfix_expression = (build_offset_ref_call_from_tree
7287 (postfix_expression, &args,
7288 complain));
7289 else if (idk == CP_ID_KIND_QUALIFIED)
7290 /* A call to a static class member, or a namespace-scope
7291 function. */
7292 postfix_expression
7293 = finish_call_expr (postfix_expression, &args,
7294 /*disallow_virtual=*/true,
7295 koenig_p,
7296 complain);
7297 else
7298 /* All other function calls. */
7299 postfix_expression
7300 = finish_call_expr (postfix_expression, &args,
7301 /*disallow_virtual=*/false,
7302 koenig_p,
7303 complain);
7304
7305 if (close_paren_loc != UNKNOWN_LOCATION)
7306 {
7307 location_t combined_loc = make_location (token->location,
7308 start_loc,
7309 close_paren_loc);
7310 postfix_expression.set_location (combined_loc);
7311 }
7312
7313 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7314 idk = CP_ID_KIND_NONE;
7315
7316 release_tree_vector (args);
7317 }
7318 break;
7319
7320 case CPP_DOT:
7321 case CPP_DEREF:
7322 /* postfix-expression . template [opt] id-expression
7323 postfix-expression . pseudo-destructor-name
7324 postfix-expression -> template [opt] id-expression
7325 postfix-expression -> pseudo-destructor-name */
7326
7327 /* Consume the `.' or `->' operator. */
7328 cp_lexer_consume_token (parser->lexer);
7329
7330 postfix_expression
7331 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7332 postfix_expression,
7333 false, &idk, loc);
7334
7335 is_member_access = true;
7336 break;
7337
7338 case CPP_PLUS_PLUS:
7339 /* postfix-expression ++ */
7340 /* Consume the `++' token. */
7341 cp_lexer_consume_token (parser->lexer);
7342 /* Generate a representation for the complete expression. */
7343 postfix_expression
7344 = finish_increment_expr (postfix_expression,
7345 POSTINCREMENT_EXPR);
7346 /* Increments may not appear in constant-expressions. */
7347 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7348 postfix_expression = error_mark_node;
7349 idk = CP_ID_KIND_NONE;
7350 is_member_access = false;
7351 break;
7352
7353 case CPP_MINUS_MINUS:
7354 /* postfix-expression -- */
7355 /* Consume the `--' token. */
7356 cp_lexer_consume_token (parser->lexer);
7357 /* Generate a representation for the complete expression. */
7358 postfix_expression
7359 = finish_increment_expr (postfix_expression,
7360 POSTDECREMENT_EXPR);
7361 /* Decrements may not appear in constant-expressions. */
7362 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7363 postfix_expression = error_mark_node;
7364 idk = CP_ID_KIND_NONE;
7365 is_member_access = false;
7366 break;
7367
7368 default:
7369 if (pidk_return != NULL)
7370 * pidk_return = idk;
7371 if (member_access_only_p)
7372 return is_member_access
7373 ? postfix_expression
7374 : cp_expr (error_mark_node);
7375 else
7376 return postfix_expression;
7377 }
7378 }
7379
7380 /* We should never get here. */
7381 gcc_unreachable ();
7382 return error_mark_node;
7383 }
7384
7385 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7386 by cp_parser_builtin_offsetof. We're looking for
7387
7388 postfix-expression [ expression ]
7389 postfix-expression [ braced-init-list ] (C++11)
7390
7391 FOR_OFFSETOF is set if we're being called in that context, which
7392 changes how we deal with integer constant expressions. */
7393
7394 static tree
7395 cp_parser_postfix_open_square_expression (cp_parser *parser,
7396 tree postfix_expression,
7397 bool for_offsetof,
7398 bool decltype_p)
7399 {
7400 tree index = NULL_TREE;
7401 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7402 bool saved_greater_than_is_operator_p;
7403
7404 /* Consume the `[' token. */
7405 cp_lexer_consume_token (parser->lexer);
7406
7407 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7408 parser->greater_than_is_operator_p = true;
7409
7410 /* Parse the index expression. */
7411 /* ??? For offsetof, there is a question of what to allow here. If
7412 offsetof is not being used in an integral constant expression context,
7413 then we *could* get the right answer by computing the value at runtime.
7414 If we are in an integral constant expression context, then we might
7415 could accept any constant expression; hard to say without analysis.
7416 Rather than open the barn door too wide right away, allow only integer
7417 constant expressions here. */
7418 if (for_offsetof)
7419 index = cp_parser_constant_expression (parser);
7420 else
7421 {
7422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7423 {
7424 bool expr_nonconst_p;
7425 cp_lexer_set_source_position (parser->lexer);
7426 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7427 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7428 }
7429 else
7430 index = cp_parser_expression (parser);
7431 }
7432
7433 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7434
7435 /* Look for the closing `]'. */
7436 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7437
7438 /* Build the ARRAY_REF. */
7439 postfix_expression = grok_array_decl (loc, postfix_expression,
7440 index, decltype_p);
7441
7442 /* When not doing offsetof, array references are not permitted in
7443 constant-expressions. */
7444 if (!for_offsetof
7445 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7446 postfix_expression = error_mark_node;
7447
7448 return postfix_expression;
7449 }
7450
7451 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7452 dereference of incomplete type, returns true if error_mark_node should
7453 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7454 and *DEPENDENT_P. */
7455
7456 bool
7457 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7458 bool *dependent_p)
7459 {
7460 /* In a template, be permissive by treating an object expression
7461 of incomplete type as dependent (after a pedwarn). */
7462 diagnostic_t kind = (processing_template_decl
7463 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7464
7465 switch (TREE_CODE (*postfix_expression))
7466 {
7467 case CAST_EXPR:
7468 case REINTERPRET_CAST_EXPR:
7469 case CONST_CAST_EXPR:
7470 case STATIC_CAST_EXPR:
7471 case DYNAMIC_CAST_EXPR:
7472 case IMPLICIT_CONV_EXPR:
7473 case VIEW_CONVERT_EXPR:
7474 case NON_LVALUE_EXPR:
7475 kind = DK_ERROR;
7476 break;
7477 case OVERLOAD:
7478 /* Don't emit any diagnostic for OVERLOADs. */
7479 kind = DK_IGNORED;
7480 break;
7481 default:
7482 /* Avoid clobbering e.g. DECLs. */
7483 if (!EXPR_P (*postfix_expression))
7484 kind = DK_ERROR;
7485 break;
7486 }
7487
7488 if (kind == DK_IGNORED)
7489 return false;
7490
7491 location_t exploc = location_of (*postfix_expression);
7492 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7493 if (!MAYBE_CLASS_TYPE_P (*scope))
7494 return true;
7495 if (kind == DK_ERROR)
7496 *scope = *postfix_expression = error_mark_node;
7497 else if (processing_template_decl)
7498 {
7499 *dependent_p = true;
7500 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7501 }
7502 return false;
7503 }
7504
7505 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7506 by cp_parser_builtin_offsetof. We're looking for
7507
7508 postfix-expression . template [opt] id-expression
7509 postfix-expression . pseudo-destructor-name
7510 postfix-expression -> template [opt] id-expression
7511 postfix-expression -> pseudo-destructor-name
7512
7513 FOR_OFFSETOF is set if we're being called in that context. That sorta
7514 limits what of the above we'll actually accept, but nevermind.
7515 TOKEN_TYPE is the "." or "->" token, which will already have been
7516 removed from the stream. */
7517
7518 static tree
7519 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7520 enum cpp_ttype token_type,
7521 cp_expr postfix_expression,
7522 bool for_offsetof, cp_id_kind *idk,
7523 location_t location)
7524 {
7525 tree name;
7526 bool dependent_p;
7527 bool pseudo_destructor_p;
7528 tree scope = NULL_TREE;
7529 location_t start_loc = postfix_expression.get_start ();
7530
7531 /* If this is a `->' operator, dereference the pointer. */
7532 if (token_type == CPP_DEREF)
7533 postfix_expression = build_x_arrow (location, postfix_expression,
7534 tf_warning_or_error);
7535 /* Check to see whether or not the expression is type-dependent and
7536 not the current instantiation. */
7537 dependent_p = type_dependent_object_expression_p (postfix_expression);
7538 /* The identifier following the `->' or `.' is not qualified. */
7539 parser->scope = NULL_TREE;
7540 parser->qualifying_scope = NULL_TREE;
7541 parser->object_scope = NULL_TREE;
7542 *idk = CP_ID_KIND_NONE;
7543
7544 /* Enter the scope corresponding to the type of the object
7545 given by the POSTFIX_EXPRESSION. */
7546 if (!dependent_p)
7547 {
7548 scope = TREE_TYPE (postfix_expression);
7549 /* According to the standard, no expression should ever have
7550 reference type. Unfortunately, we do not currently match
7551 the standard in this respect in that our internal representation
7552 of an expression may have reference type even when the standard
7553 says it does not. Therefore, we have to manually obtain the
7554 underlying type here. */
7555 scope = non_reference (scope);
7556 /* The type of the POSTFIX_EXPRESSION must be complete. */
7557 /* Unlike the object expression in other contexts, *this is not
7558 required to be of complete type for purposes of class member
7559 access (5.2.5) outside the member function body. */
7560 if (postfix_expression != current_class_ref
7561 && scope != error_mark_node
7562 && !currently_open_class (scope))
7563 {
7564 scope = complete_type (scope);
7565 if (!COMPLETE_TYPE_P (scope)
7566 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7567 &dependent_p))
7568 return error_mark_node;
7569 }
7570
7571 if (!dependent_p)
7572 {
7573 /* Let the name lookup machinery know that we are processing a
7574 class member access expression. */
7575 parser->context->object_type = scope;
7576 /* If something went wrong, we want to be able to discern that case,
7577 as opposed to the case where there was no SCOPE due to the type
7578 of expression being dependent. */
7579 if (!scope)
7580 scope = error_mark_node;
7581 /* If the SCOPE was erroneous, make the various semantic analysis
7582 functions exit quickly -- and without issuing additional error
7583 messages. */
7584 if (scope == error_mark_node)
7585 postfix_expression = error_mark_node;
7586 }
7587 }
7588
7589 if (dependent_p)
7590 /* Tell cp_parser_lookup_name that there was an object, even though it's
7591 type-dependent. */
7592 parser->context->object_type = unknown_type_node;
7593
7594 /* Assume this expression is not a pseudo-destructor access. */
7595 pseudo_destructor_p = false;
7596
7597 /* If the SCOPE is a scalar type, then, if this is a valid program,
7598 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7599 is type dependent, it can be pseudo-destructor-name or something else.
7600 Try to parse it as pseudo-destructor-name first. */
7601 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7602 {
7603 tree s;
7604 tree type;
7605
7606 cp_parser_parse_tentatively (parser);
7607 /* Parse the pseudo-destructor-name. */
7608 s = NULL_TREE;
7609 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7610 &s, &type);
7611 if (dependent_p
7612 && (cp_parser_error_occurred (parser)
7613 || !SCALAR_TYPE_P (type)))
7614 cp_parser_abort_tentative_parse (parser);
7615 else if (cp_parser_parse_definitely (parser))
7616 {
7617 pseudo_destructor_p = true;
7618 postfix_expression
7619 = finish_pseudo_destructor_expr (postfix_expression,
7620 s, type, location);
7621 }
7622 }
7623
7624 if (!pseudo_destructor_p)
7625 {
7626 /* If the SCOPE is not a scalar type, we are looking at an
7627 ordinary class member access expression, rather than a
7628 pseudo-destructor-name. */
7629 bool template_p;
7630 cp_token *token = cp_lexer_peek_token (parser->lexer);
7631 /* Parse the id-expression. */
7632 name = (cp_parser_id_expression
7633 (parser,
7634 cp_parser_optional_template_keyword (parser),
7635 /*check_dependency_p=*/true,
7636 &template_p,
7637 /*declarator_p=*/false,
7638 /*optional_p=*/false));
7639 /* In general, build a SCOPE_REF if the member name is qualified.
7640 However, if the name was not dependent and has already been
7641 resolved; there is no need to build the SCOPE_REF. For example;
7642
7643 struct X { void f(); };
7644 template <typename T> void f(T* t) { t->X::f(); }
7645
7646 Even though "t" is dependent, "X::f" is not and has been resolved
7647 to a BASELINK; there is no need to include scope information. */
7648
7649 /* But we do need to remember that there was an explicit scope for
7650 virtual function calls. */
7651 if (parser->scope)
7652 *idk = CP_ID_KIND_QUALIFIED;
7653
7654 /* If the name is a template-id that names a type, we will get a
7655 TYPE_DECL here. That is invalid code. */
7656 if (TREE_CODE (name) == TYPE_DECL)
7657 {
7658 error_at (token->location, "invalid use of %qD", name);
7659 postfix_expression = error_mark_node;
7660 }
7661 else
7662 {
7663 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7664 {
7665 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7666 {
7667 error_at (token->location, "%<%D::%D%> is not a class member",
7668 parser->scope, name);
7669 postfix_expression = error_mark_node;
7670 }
7671 else
7672 name = build_qualified_name (/*type=*/NULL_TREE,
7673 parser->scope,
7674 name,
7675 template_p);
7676 parser->scope = NULL_TREE;
7677 parser->qualifying_scope = NULL_TREE;
7678 parser->object_scope = NULL_TREE;
7679 }
7680 if (parser->scope && name && BASELINK_P (name))
7681 adjust_result_of_qualified_name_lookup
7682 (name, parser->scope, scope);
7683 postfix_expression
7684 = finish_class_member_access_expr (postfix_expression, name,
7685 template_p,
7686 tf_warning_or_error);
7687 /* Build a location e.g.:
7688 ptr->access_expr
7689 ~~~^~~~~~~~~~~~~
7690 where the caret is at the deref token, ranging from
7691 the start of postfix_expression to the end of the access expr. */
7692 location_t end_loc
7693 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7694 location_t combined_loc
7695 = make_location (input_location, start_loc, end_loc);
7696 protected_set_expr_location (postfix_expression, combined_loc);
7697 }
7698 }
7699
7700 /* We no longer need to look up names in the scope of the object on
7701 the left-hand side of the `.' or `->' operator. */
7702 parser->context->object_type = NULL_TREE;
7703
7704 /* Outside of offsetof, these operators may not appear in
7705 constant-expressions. */
7706 if (!for_offsetof
7707 && (cp_parser_non_integral_constant_expression
7708 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7709 postfix_expression = error_mark_node;
7710
7711 return postfix_expression;
7712 }
7713
7714 /* Parse a parenthesized expression-list.
7715
7716 expression-list:
7717 assignment-expression
7718 expression-list, assignment-expression
7719
7720 attribute-list:
7721 expression-list
7722 identifier
7723 identifier, expression-list
7724
7725 CAST_P is true if this expression is the target of a cast.
7726
7727 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7728 argument pack.
7729
7730 WRAP_LOCATIONS_P is true if expressions within this list for which
7731 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7732 their source locations.
7733
7734 Returns a vector of trees. Each element is a representation of an
7735 assignment-expression. NULL is returned if the ( and or ) are
7736 missing. An empty, but allocated, vector is returned on no
7737 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7738 if we are parsing an attribute list for an attribute that wants a
7739 plain identifier argument, normal_attr for an attribute that wants
7740 an expression, or non_attr if we aren't parsing an attribute list. If
7741 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7742 not all of the expressions in the list were constant.
7743 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7744 will be written to with the location of the closing parenthesis. If
7745 an error occurs, it may or may not be written to. */
7746
7747 static vec<tree, va_gc> *
7748 cp_parser_parenthesized_expression_list (cp_parser* parser,
7749 int is_attribute_list,
7750 bool cast_p,
7751 bool allow_expansion_p,
7752 bool *non_constant_p,
7753 location_t *close_paren_loc,
7754 bool wrap_locations_p)
7755 {
7756 vec<tree, va_gc> *expression_list;
7757 bool fold_expr_p = is_attribute_list != non_attr;
7758 tree identifier = NULL_TREE;
7759 bool saved_greater_than_is_operator_p;
7760
7761 /* Assume all the expressions will be constant. */
7762 if (non_constant_p)
7763 *non_constant_p = false;
7764
7765 matching_parens parens;
7766 if (!parens.require_open (parser))
7767 return NULL;
7768
7769 expression_list = make_tree_vector ();
7770
7771 /* Within a parenthesized expression, a `>' token is always
7772 the greater-than operator. */
7773 saved_greater_than_is_operator_p
7774 = parser->greater_than_is_operator_p;
7775 parser->greater_than_is_operator_p = true;
7776
7777 cp_expr expr (NULL_TREE);
7778
7779 /* Consume expressions until there are no more. */
7780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7781 while (true)
7782 {
7783 /* At the beginning of attribute lists, check to see if the
7784 next token is an identifier. */
7785 if (is_attribute_list == id_attr
7786 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7787 {
7788 cp_token *token;
7789
7790 /* Consume the identifier. */
7791 token = cp_lexer_consume_token (parser->lexer);
7792 /* Save the identifier. */
7793 identifier = token->u.value;
7794 }
7795 else
7796 {
7797 bool expr_non_constant_p;
7798
7799 /* Parse the next assignment-expression. */
7800 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7801 {
7802 /* A braced-init-list. */
7803 cp_lexer_set_source_position (parser->lexer);
7804 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7805 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7806 if (non_constant_p && expr_non_constant_p)
7807 *non_constant_p = true;
7808 }
7809 else if (non_constant_p)
7810 {
7811 expr = (cp_parser_constant_expression
7812 (parser, /*allow_non_constant_p=*/true,
7813 &expr_non_constant_p));
7814 if (expr_non_constant_p)
7815 *non_constant_p = true;
7816 }
7817 else
7818 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7819 cast_p);
7820
7821 if (fold_expr_p)
7822 expr = instantiate_non_dependent_expr (expr);
7823
7824 /* If we have an ellipsis, then this is an expression
7825 expansion. */
7826 if (allow_expansion_p
7827 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7828 {
7829 /* Consume the `...'. */
7830 cp_lexer_consume_token (parser->lexer);
7831
7832 /* Build the argument pack. */
7833 expr = make_pack_expansion (expr);
7834 }
7835
7836 if (wrap_locations_p)
7837 expr.maybe_add_location_wrapper ();
7838
7839 /* Add it to the list. We add error_mark_node
7840 expressions to the list, so that we can still tell if
7841 the correct form for a parenthesized expression-list
7842 is found. That gives better errors. */
7843 vec_safe_push (expression_list, expr.get_value ());
7844
7845 if (expr == error_mark_node)
7846 goto skip_comma;
7847 }
7848
7849 /* After the first item, attribute lists look the same as
7850 expression lists. */
7851 is_attribute_list = non_attr;
7852
7853 get_comma:;
7854 /* If the next token isn't a `,', then we are done. */
7855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7856 break;
7857
7858 /* Otherwise, consume the `,' and keep going. */
7859 cp_lexer_consume_token (parser->lexer);
7860 }
7861
7862 if (close_paren_loc)
7863 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7864
7865 if (!parens.require_close (parser))
7866 {
7867 int ending;
7868
7869 skip_comma:;
7870 /* We try and resync to an unnested comma, as that will give the
7871 user better diagnostics. */
7872 ending = cp_parser_skip_to_closing_parenthesis (parser,
7873 /*recovering=*/true,
7874 /*or_comma=*/true,
7875 /*consume_paren=*/true);
7876 if (ending < 0)
7877 goto get_comma;
7878 if (!ending)
7879 {
7880 parser->greater_than_is_operator_p
7881 = saved_greater_than_is_operator_p;
7882 return NULL;
7883 }
7884 }
7885
7886 parser->greater_than_is_operator_p
7887 = saved_greater_than_is_operator_p;
7888
7889 if (identifier)
7890 vec_safe_insert (expression_list, 0, identifier);
7891
7892 return expression_list;
7893 }
7894
7895 /* Parse a pseudo-destructor-name.
7896
7897 pseudo-destructor-name:
7898 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7899 :: [opt] nested-name-specifier template template-id :: ~ type-name
7900 :: [opt] nested-name-specifier [opt] ~ type-name
7901
7902 If either of the first two productions is used, sets *SCOPE to the
7903 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7904 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7905 or ERROR_MARK_NODE if the parse fails. */
7906
7907 static void
7908 cp_parser_pseudo_destructor_name (cp_parser* parser,
7909 tree object,
7910 tree* scope,
7911 tree* type)
7912 {
7913 bool nested_name_specifier_p;
7914
7915 /* Handle ~auto. */
7916 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7917 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7918 && !type_dependent_expression_p (object))
7919 {
7920 if (cxx_dialect < cxx14)
7921 pedwarn (input_location, 0,
7922 "%<~auto%> only available with "
7923 "-std=c++14 or -std=gnu++14");
7924 cp_lexer_consume_token (parser->lexer);
7925 cp_lexer_consume_token (parser->lexer);
7926 *scope = NULL_TREE;
7927 *type = TREE_TYPE (object);
7928 return;
7929 }
7930
7931 /* Assume that things will not work out. */
7932 *type = error_mark_node;
7933
7934 /* Look for the optional `::' operator. */
7935 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7936 /* Look for the optional nested-name-specifier. */
7937 nested_name_specifier_p
7938 = (cp_parser_nested_name_specifier_opt (parser,
7939 /*typename_keyword_p=*/false,
7940 /*check_dependency_p=*/true,
7941 /*type_p=*/false,
7942 /*is_declaration=*/false)
7943 != NULL_TREE);
7944 /* Now, if we saw a nested-name-specifier, we might be doing the
7945 second production. */
7946 if (nested_name_specifier_p
7947 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7948 {
7949 /* Consume the `template' keyword. */
7950 cp_lexer_consume_token (parser->lexer);
7951 /* Parse the template-id. */
7952 cp_parser_template_id (parser,
7953 /*template_keyword_p=*/true,
7954 /*check_dependency_p=*/false,
7955 class_type,
7956 /*is_declaration=*/true);
7957 /* Look for the `::' token. */
7958 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7959 }
7960 /* If the next token is not a `~', then there might be some
7961 additional qualification. */
7962 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7963 {
7964 /* At this point, we're looking for "type-name :: ~". The type-name
7965 must not be a class-name, since this is a pseudo-destructor. So,
7966 it must be either an enum-name, or a typedef-name -- both of which
7967 are just identifiers. So, we peek ahead to check that the "::"
7968 and "~" tokens are present; if they are not, then we can avoid
7969 calling type_name. */
7970 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7971 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7972 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7973 {
7974 cp_parser_error (parser, "non-scalar type");
7975 return;
7976 }
7977
7978 /* Look for the type-name. */
7979 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7980 if (*scope == error_mark_node)
7981 return;
7982
7983 /* Look for the `::' token. */
7984 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7985 }
7986 else
7987 *scope = NULL_TREE;
7988
7989 /* Look for the `~'. */
7990 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7991
7992 /* Once we see the ~, this has to be a pseudo-destructor. */
7993 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7994 cp_parser_commit_to_topmost_tentative_parse (parser);
7995
7996 /* Look for the type-name again. We are not responsible for
7997 checking that it matches the first type-name. */
7998 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7999 }
8000
8001 /* Parse a unary-expression.
8002
8003 unary-expression:
8004 postfix-expression
8005 ++ cast-expression
8006 -- cast-expression
8007 unary-operator cast-expression
8008 sizeof unary-expression
8009 sizeof ( type-id )
8010 alignof ( type-id ) [C++0x]
8011 new-expression
8012 delete-expression
8013
8014 GNU Extensions:
8015
8016 unary-expression:
8017 __extension__ cast-expression
8018 __alignof__ unary-expression
8019 __alignof__ ( type-id )
8020 alignof unary-expression [C++0x]
8021 __real__ cast-expression
8022 __imag__ cast-expression
8023 && identifier
8024 sizeof ( type-id ) { initializer-list , [opt] }
8025 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8026 __alignof__ ( type-id ) { initializer-list , [opt] }
8027
8028 ADDRESS_P is true iff the unary-expression is appearing as the
8029 operand of the `&' operator. CAST_P is true if this expression is
8030 the target of a cast.
8031
8032 Returns a representation of the expression. */
8033
8034 static cp_expr
8035 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8036 bool address_p, bool cast_p, bool decltype_p)
8037 {
8038 cp_token *token;
8039 enum tree_code unary_operator;
8040
8041 /* Peek at the next token. */
8042 token = cp_lexer_peek_token (parser->lexer);
8043 /* Some keywords give away the kind of expression. */
8044 if (token->type == CPP_KEYWORD)
8045 {
8046 enum rid keyword = token->keyword;
8047
8048 switch (keyword)
8049 {
8050 case RID_ALIGNOF:
8051 case RID_SIZEOF:
8052 {
8053 tree operand, ret;
8054 enum tree_code op;
8055 location_t start_loc = token->location;
8056
8057 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8058 bool std_alignof = id_equal (token->u.value, "alignof");
8059
8060 /* Consume the token. */
8061 cp_lexer_consume_token (parser->lexer);
8062 /* Parse the operand. */
8063 operand = cp_parser_sizeof_operand (parser, keyword);
8064
8065 if (TYPE_P (operand))
8066 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8067 true);
8068 else
8069 {
8070 /* ISO C++ defines alignof only with types, not with
8071 expressions. So pedwarn if alignof is used with a non-
8072 type expression. However, __alignof__ is ok. */
8073 if (std_alignof)
8074 pedwarn (token->location, OPT_Wpedantic,
8075 "ISO C++ does not allow %<alignof%> "
8076 "with a non-type");
8077
8078 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8079 }
8080 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8081 SIZEOF_EXPR with the original operand. */
8082 if (op == SIZEOF_EXPR && ret != error_mark_node)
8083 {
8084 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8085 {
8086 if (!processing_template_decl && TYPE_P (operand))
8087 {
8088 ret = build_min (SIZEOF_EXPR, size_type_node,
8089 build1 (NOP_EXPR, operand,
8090 error_mark_node));
8091 SIZEOF_EXPR_TYPE_P (ret) = 1;
8092 }
8093 else
8094 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8095 TREE_SIDE_EFFECTS (ret) = 0;
8096 TREE_READONLY (ret) = 1;
8097 }
8098 }
8099
8100 /* Construct a location e.g. :
8101 alignof (expr)
8102 ^~~~~~~~~~~~~~
8103 with start == caret at the start of the "alignof"/"sizeof"
8104 token, with the endpoint at the final closing paren. */
8105 location_t finish_loc
8106 = cp_lexer_previous_token (parser->lexer)->location;
8107 location_t compound_loc
8108 = make_location (start_loc, start_loc, finish_loc);
8109
8110 cp_expr ret_expr (ret);
8111 ret_expr.set_location (compound_loc);
8112 ret_expr = ret_expr.maybe_add_location_wrapper ();
8113 return ret_expr;
8114 }
8115
8116 case RID_BUILTIN_HAS_ATTRIBUTE:
8117 return cp_parser_has_attribute_expression (parser);
8118
8119 case RID_NEW:
8120 return cp_parser_new_expression (parser);
8121
8122 case RID_DELETE:
8123 return cp_parser_delete_expression (parser);
8124
8125 case RID_EXTENSION:
8126 {
8127 /* The saved value of the PEDANTIC flag. */
8128 int saved_pedantic;
8129 tree expr;
8130
8131 /* Save away the PEDANTIC flag. */
8132 cp_parser_extension_opt (parser, &saved_pedantic);
8133 /* Parse the cast-expression. */
8134 expr = cp_parser_simple_cast_expression (parser);
8135 /* Restore the PEDANTIC flag. */
8136 pedantic = saved_pedantic;
8137
8138 return expr;
8139 }
8140
8141 case RID_REALPART:
8142 case RID_IMAGPART:
8143 {
8144 tree expression;
8145
8146 /* Consume the `__real__' or `__imag__' token. */
8147 cp_lexer_consume_token (parser->lexer);
8148 /* Parse the cast-expression. */
8149 expression = cp_parser_simple_cast_expression (parser);
8150 /* Create the complete representation. */
8151 return build_x_unary_op (token->location,
8152 (keyword == RID_REALPART
8153 ? REALPART_EXPR : IMAGPART_EXPR),
8154 expression,
8155 tf_warning_or_error);
8156 }
8157 break;
8158
8159 case RID_TRANSACTION_ATOMIC:
8160 case RID_TRANSACTION_RELAXED:
8161 return cp_parser_transaction_expression (parser, keyword);
8162
8163 case RID_NOEXCEPT:
8164 {
8165 tree expr;
8166 const char *saved_message;
8167 bool saved_integral_constant_expression_p;
8168 bool saved_non_integral_constant_expression_p;
8169 bool saved_greater_than_is_operator_p;
8170
8171 location_t start_loc = token->location;
8172
8173 cp_lexer_consume_token (parser->lexer);
8174 matching_parens parens;
8175 parens.require_open (parser);
8176
8177 saved_message = parser->type_definition_forbidden_message;
8178 parser->type_definition_forbidden_message
8179 = G_("types may not be defined in %<noexcept%> expressions");
8180
8181 saved_integral_constant_expression_p
8182 = parser->integral_constant_expression_p;
8183 saved_non_integral_constant_expression_p
8184 = parser->non_integral_constant_expression_p;
8185 parser->integral_constant_expression_p = false;
8186
8187 saved_greater_than_is_operator_p
8188 = parser->greater_than_is_operator_p;
8189 parser->greater_than_is_operator_p = true;
8190
8191 ++cp_unevaluated_operand;
8192 ++c_inhibit_evaluation_warnings;
8193 ++cp_noexcept_operand;
8194 expr = cp_parser_expression (parser);
8195 --cp_noexcept_operand;
8196 --c_inhibit_evaluation_warnings;
8197 --cp_unevaluated_operand;
8198
8199 parser->greater_than_is_operator_p
8200 = saved_greater_than_is_operator_p;
8201
8202 parser->integral_constant_expression_p
8203 = saved_integral_constant_expression_p;
8204 parser->non_integral_constant_expression_p
8205 = saved_non_integral_constant_expression_p;
8206
8207 parser->type_definition_forbidden_message = saved_message;
8208
8209 location_t finish_loc
8210 = cp_lexer_peek_token (parser->lexer)->location;
8211 parens.require_close (parser);
8212
8213 /* Construct a location of the form:
8214 noexcept (expr)
8215 ^~~~~~~~~~~~~~~
8216 with start == caret, finishing at the close-paren. */
8217 location_t noexcept_loc
8218 = make_location (start_loc, start_loc, finish_loc);
8219
8220 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8221 noexcept_loc);
8222 }
8223
8224 default:
8225 break;
8226 }
8227 }
8228
8229 /* Look for the `:: new' and `:: delete', which also signal the
8230 beginning of a new-expression, or delete-expression,
8231 respectively. If the next token is `::', then it might be one of
8232 these. */
8233 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8234 {
8235 enum rid keyword;
8236
8237 /* See if the token after the `::' is one of the keywords in
8238 which we're interested. */
8239 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8240 /* If it's `new', we have a new-expression. */
8241 if (keyword == RID_NEW)
8242 return cp_parser_new_expression (parser);
8243 /* Similarly, for `delete'. */
8244 else if (keyword == RID_DELETE)
8245 return cp_parser_delete_expression (parser);
8246 }
8247
8248 /* Look for a unary operator. */
8249 unary_operator = cp_parser_unary_operator (token);
8250 /* The `++' and `--' operators can be handled similarly, even though
8251 they are not technically unary-operators in the grammar. */
8252 if (unary_operator == ERROR_MARK)
8253 {
8254 if (token->type == CPP_PLUS_PLUS)
8255 unary_operator = PREINCREMENT_EXPR;
8256 else if (token->type == CPP_MINUS_MINUS)
8257 unary_operator = PREDECREMENT_EXPR;
8258 /* Handle the GNU address-of-label extension. */
8259 else if (cp_parser_allow_gnu_extensions_p (parser)
8260 && token->type == CPP_AND_AND)
8261 {
8262 tree identifier;
8263 tree expression;
8264 location_t start_loc = token->location;
8265
8266 /* Consume the '&&' token. */
8267 cp_lexer_consume_token (parser->lexer);
8268 /* Look for the identifier. */
8269 location_t finish_loc
8270 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8271 identifier = cp_parser_identifier (parser);
8272 /* Construct a location of the form:
8273 &&label
8274 ^~~~~~~
8275 with caret==start at the "&&", finish at the end of the label. */
8276 location_t combined_loc
8277 = make_location (start_loc, start_loc, finish_loc);
8278 /* Create an expression representing the address. */
8279 expression = finish_label_address_expr (identifier, combined_loc);
8280 if (cp_parser_non_integral_constant_expression (parser,
8281 NIC_ADDR_LABEL))
8282 expression = error_mark_node;
8283 return expression;
8284 }
8285 }
8286 if (unary_operator != ERROR_MARK)
8287 {
8288 cp_expr cast_expression;
8289 cp_expr expression = error_mark_node;
8290 non_integral_constant non_constant_p = NIC_NONE;
8291 location_t loc = token->location;
8292 tsubst_flags_t complain = complain_flags (decltype_p);
8293
8294 /* Consume the operator token. */
8295 token = cp_lexer_consume_token (parser->lexer);
8296 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8297
8298 /* Parse the cast-expression. */
8299 cast_expression
8300 = cp_parser_cast_expression (parser,
8301 unary_operator == ADDR_EXPR,
8302 /*cast_p=*/false,
8303 /*decltype*/false,
8304 pidk);
8305
8306 /* Make a location:
8307 OP_TOKEN CAST_EXPRESSION
8308 ^~~~~~~~~~~~~~~~~~~~~~~~~
8309 with start==caret at the operator token, and
8310 extending to the end of the cast_expression. */
8311 loc = make_location (loc, loc, cast_expression.get_finish ());
8312
8313 /* Now, build an appropriate representation. */
8314 switch (unary_operator)
8315 {
8316 case INDIRECT_REF:
8317 non_constant_p = NIC_STAR;
8318 expression = build_x_indirect_ref (loc, cast_expression,
8319 RO_UNARY_STAR,
8320 complain);
8321 /* TODO: build_x_indirect_ref does not always honor the
8322 location, so ensure it is set. */
8323 expression.set_location (loc);
8324 break;
8325
8326 case ADDR_EXPR:
8327 non_constant_p = NIC_ADDR;
8328 /* Fall through. */
8329 case BIT_NOT_EXPR:
8330 expression = build_x_unary_op (loc, unary_operator,
8331 cast_expression,
8332 complain);
8333 /* TODO: build_x_unary_op does not always honor the location,
8334 so ensure it is set. */
8335 expression.set_location (loc);
8336 break;
8337
8338 case PREINCREMENT_EXPR:
8339 case PREDECREMENT_EXPR:
8340 non_constant_p = unary_operator == PREINCREMENT_EXPR
8341 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8342 /* Fall through. */
8343 case NEGATE_EXPR:
8344 /* Immediately fold negation of a constant, unless the constant is 0
8345 (since -0 == 0) or it would overflow. */
8346 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8347 && CONSTANT_CLASS_P (cast_expression)
8348 && !integer_zerop (cast_expression)
8349 && !TREE_OVERFLOW (cast_expression))
8350 {
8351 tree folded = fold_build1 (unary_operator,
8352 TREE_TYPE (cast_expression),
8353 cast_expression);
8354 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8355 {
8356 expression = cp_expr (folded, loc);
8357 break;
8358 }
8359 }
8360 /* Fall through. */
8361 case UNARY_PLUS_EXPR:
8362 case TRUTH_NOT_EXPR:
8363 expression = finish_unary_op_expr (loc, unary_operator,
8364 cast_expression, complain);
8365 break;
8366
8367 default:
8368 gcc_unreachable ();
8369 }
8370
8371 if (non_constant_p != NIC_NONE
8372 && cp_parser_non_integral_constant_expression (parser,
8373 non_constant_p))
8374 expression = error_mark_node;
8375
8376 return expression;
8377 }
8378
8379 return cp_parser_postfix_expression (parser, address_p, cast_p,
8380 /*member_access_only_p=*/false,
8381 decltype_p,
8382 pidk);
8383 }
8384
8385 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8386 unary-operator, the corresponding tree code is returned. */
8387
8388 static enum tree_code
8389 cp_parser_unary_operator (cp_token* token)
8390 {
8391 switch (token->type)
8392 {
8393 case CPP_MULT:
8394 return INDIRECT_REF;
8395
8396 case CPP_AND:
8397 return ADDR_EXPR;
8398
8399 case CPP_PLUS:
8400 return UNARY_PLUS_EXPR;
8401
8402 case CPP_MINUS:
8403 return NEGATE_EXPR;
8404
8405 case CPP_NOT:
8406 return TRUTH_NOT_EXPR;
8407
8408 case CPP_COMPL:
8409 return BIT_NOT_EXPR;
8410
8411 default:
8412 return ERROR_MARK;
8413 }
8414 }
8415
8416 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8417 Returns a representation of the expression. */
8418
8419 static tree
8420 cp_parser_has_attribute_expression (cp_parser *parser)
8421 {
8422 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8423
8424 /* Consume the __builtin_has_attribute token. */
8425 cp_lexer_consume_token (parser->lexer);
8426
8427 matching_parens parens;
8428 if (!parens.require_open (parser))
8429 return error_mark_node;
8430
8431 /* Types cannot be defined in a `sizeof' expression. Save away the
8432 old message. */
8433 const char *saved_message = parser->type_definition_forbidden_message;
8434 /* And create the new one. */
8435 const int kwd = RID_BUILTIN_HAS_ATTRIBUTE;
8436 char *tmp = concat ("types may not be defined in %<",
8437 IDENTIFIER_POINTER (ridpointers[kwd]),
8438 "%> expressions", NULL);
8439 parser->type_definition_forbidden_message = tmp;
8440
8441 /* The restrictions on constant-expressions do not apply inside
8442 sizeof expressions. */
8443 bool saved_integral_constant_expression_p
8444 = parser->integral_constant_expression_p;
8445 bool saved_non_integral_constant_expression_p
8446 = parser->non_integral_constant_expression_p;
8447 parser->integral_constant_expression_p = false;
8448
8449 /* Do not actually evaluate the expression. */
8450 ++cp_unevaluated_operand;
8451 ++c_inhibit_evaluation_warnings;
8452
8453 tree oper = NULL_TREE;
8454
8455 /* We can't be sure yet whether we're looking at a type-id or an
8456 expression. */
8457 cp_parser_parse_tentatively (parser);
8458
8459 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8460 parser->in_type_id_in_expr_p = true;
8461 /* Look for the type-id. */
8462 oper = cp_parser_type_id (parser);
8463 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8464
8465 cp_parser_parse_definitely (parser);
8466
8467 /* If the type-id production did not work out, then we must be
8468 looking at the unary-expression production. */
8469 if (!oper || oper == error_mark_node)
8470 oper = cp_parser_unary_expression (parser);
8471
8472 /* Go back to evaluating expressions. */
8473 --cp_unevaluated_operand;
8474 --c_inhibit_evaluation_warnings;
8475
8476 /* Free the message we created. */
8477 free (tmp);
8478 /* And restore the old one. */
8479 parser->type_definition_forbidden_message = saved_message;
8480 parser->integral_constant_expression_p
8481 = saved_integral_constant_expression_p;
8482 parser->non_integral_constant_expression_p
8483 = saved_non_integral_constant_expression_p;
8484
8485 /* Consume the comma if it's there. */
8486 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8487 {
8488 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8489 /*consume_paren=*/true);
8490 return error_mark_node;
8491 }
8492
8493 /* Parse the attribute specification. */
8494 bool ret = false;
8495 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8496 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8497 {
8498 if (oper != error_mark_node)
8499 {
8500 /* Fold constant expressions used in attributes first. */
8501 cp_check_const_attributes (attr);
8502
8503 /* Finally, see if OPER has been declared with ATTR. */
8504 ret = has_attribute (atloc, oper, attr, default_conversion);
8505 }
8506
8507 parens.require_close (parser);
8508 }
8509 else
8510 {
8511 error_at (atloc, "expected identifier");
8512 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8513 }
8514
8515 /* Construct a location e.g. :
8516 __builtin_has_attribute (oper, attr)
8517 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8518 with start == caret at the start of the built-in token,
8519 and with the endpoint at the final closing paren. */
8520 location_t finish_loc
8521 = cp_lexer_previous_token (parser->lexer)->location;
8522 location_t compound_loc
8523 = make_location (start_loc, start_loc, finish_loc);
8524
8525 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8526 ret_expr.set_location (compound_loc);
8527 ret_expr = ret_expr.maybe_add_location_wrapper ();
8528 return ret_expr;
8529 }
8530
8531 /* Parse a new-expression.
8532
8533 new-expression:
8534 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8535 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8536
8537 Returns a representation of the expression. */
8538
8539 static tree
8540 cp_parser_new_expression (cp_parser* parser)
8541 {
8542 bool global_scope_p;
8543 vec<tree, va_gc> *placement;
8544 tree type;
8545 vec<tree, va_gc> *initializer;
8546 tree nelts = NULL_TREE;
8547 tree ret;
8548
8549 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8550
8551 /* Look for the optional `::' operator. */
8552 global_scope_p
8553 = (cp_parser_global_scope_opt (parser,
8554 /*current_scope_valid_p=*/false)
8555 != NULL_TREE);
8556 /* Look for the `new' operator. */
8557 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8558 /* There's no easy way to tell a new-placement from the
8559 `( type-id )' construct. */
8560 cp_parser_parse_tentatively (parser);
8561 /* Look for a new-placement. */
8562 placement = cp_parser_new_placement (parser);
8563 /* If that didn't work out, there's no new-placement. */
8564 if (!cp_parser_parse_definitely (parser))
8565 {
8566 if (placement != NULL)
8567 release_tree_vector (placement);
8568 placement = NULL;
8569 }
8570
8571 /* If the next token is a `(', then we have a parenthesized
8572 type-id. */
8573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8574 {
8575 cp_token *token;
8576 const char *saved_message = parser->type_definition_forbidden_message;
8577
8578 /* Consume the `('. */
8579 matching_parens parens;
8580 parens.consume_open (parser);
8581
8582 /* Parse the type-id. */
8583 parser->type_definition_forbidden_message
8584 = G_("types may not be defined in a new-expression");
8585 {
8586 type_id_in_expr_sentinel s (parser);
8587 type = cp_parser_type_id (parser);
8588 }
8589 parser->type_definition_forbidden_message = saved_message;
8590
8591 /* Look for the closing `)'. */
8592 parens.require_close (parser);
8593 token = cp_lexer_peek_token (parser->lexer);
8594 /* There should not be a direct-new-declarator in this production,
8595 but GCC used to allowed this, so we check and emit a sensible error
8596 message for this case. */
8597 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8598 {
8599 error_at (token->location,
8600 "array bound forbidden after parenthesized type-id");
8601 inform (token->location,
8602 "try removing the parentheses around the type-id");
8603 cp_parser_direct_new_declarator (parser);
8604 }
8605 }
8606 /* Otherwise, there must be a new-type-id. */
8607 else
8608 type = cp_parser_new_type_id (parser, &nelts);
8609
8610 /* If the next token is a `(' or '{', then we have a new-initializer. */
8611 cp_token *token = cp_lexer_peek_token (parser->lexer);
8612 if (token->type == CPP_OPEN_PAREN
8613 || token->type == CPP_OPEN_BRACE)
8614 initializer = cp_parser_new_initializer (parser);
8615 else
8616 initializer = NULL;
8617
8618 /* A new-expression may not appear in an integral constant
8619 expression. */
8620 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8621 ret = error_mark_node;
8622 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8623 of a new-type-id or type-id of a new-expression, the new-expression shall
8624 contain a new-initializer of the form ( assignment-expression )".
8625 Additionally, consistently with the spirit of DR 1467, we want to accept
8626 'new auto { 2 }' too. */
8627 else if ((ret = type_uses_auto (type))
8628 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8629 && (vec_safe_length (initializer) != 1
8630 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8631 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8632 {
8633 error_at (token->location,
8634 "initialization of new-expression for type %<auto%> "
8635 "requires exactly one element");
8636 ret = error_mark_node;
8637 }
8638 else
8639 {
8640 /* Construct a location e.g.:
8641 ptr = new int[100]
8642 ^~~~~~~~~~~~
8643 with caret == start at the start of the "new" token, and the end
8644 at the end of the final token we consumed. */
8645 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8646 location_t end_loc = get_finish (end_tok->location);
8647 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8648
8649 /* Create a representation of the new-expression. */
8650 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8651 tf_warning_or_error);
8652 protected_set_expr_location (ret, combined_loc);
8653 }
8654
8655 if (placement != NULL)
8656 release_tree_vector (placement);
8657 if (initializer != NULL)
8658 release_tree_vector (initializer);
8659
8660 return ret;
8661 }
8662
8663 /* Parse a new-placement.
8664
8665 new-placement:
8666 ( expression-list )
8667
8668 Returns the same representation as for an expression-list. */
8669
8670 static vec<tree, va_gc> *
8671 cp_parser_new_placement (cp_parser* parser)
8672 {
8673 vec<tree, va_gc> *expression_list;
8674
8675 /* Parse the expression-list. */
8676 expression_list = (cp_parser_parenthesized_expression_list
8677 (parser, non_attr, /*cast_p=*/false,
8678 /*allow_expansion_p=*/true,
8679 /*non_constant_p=*/NULL));
8680
8681 if (expression_list && expression_list->is_empty ())
8682 error ("expected expression-list or type-id");
8683
8684 return expression_list;
8685 }
8686
8687 /* Parse a new-type-id.
8688
8689 new-type-id:
8690 type-specifier-seq new-declarator [opt]
8691
8692 Returns the TYPE allocated. If the new-type-id indicates an array
8693 type, *NELTS is set to the number of elements in the last array
8694 bound; the TYPE will not include the last array bound. */
8695
8696 static tree
8697 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8698 {
8699 cp_decl_specifier_seq type_specifier_seq;
8700 cp_declarator *new_declarator;
8701 cp_declarator *declarator;
8702 cp_declarator *outer_declarator;
8703 const char *saved_message;
8704
8705 /* The type-specifier sequence must not contain type definitions.
8706 (It cannot contain declarations of new types either, but if they
8707 are not definitions we will catch that because they are not
8708 complete.) */
8709 saved_message = parser->type_definition_forbidden_message;
8710 parser->type_definition_forbidden_message
8711 = G_("types may not be defined in a new-type-id");
8712 /* Parse the type-specifier-seq. */
8713 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8714 /*is_trailing_return=*/false,
8715 &type_specifier_seq);
8716 /* Restore the old message. */
8717 parser->type_definition_forbidden_message = saved_message;
8718
8719 if (type_specifier_seq.type == error_mark_node)
8720 return error_mark_node;
8721
8722 /* Parse the new-declarator. */
8723 new_declarator = cp_parser_new_declarator_opt (parser);
8724
8725 /* Determine the number of elements in the last array dimension, if
8726 any. */
8727 *nelts = NULL_TREE;
8728 /* Skip down to the last array dimension. */
8729 declarator = new_declarator;
8730 outer_declarator = NULL;
8731 while (declarator && (declarator->kind == cdk_pointer
8732 || declarator->kind == cdk_ptrmem))
8733 {
8734 outer_declarator = declarator;
8735 declarator = declarator->declarator;
8736 }
8737 while (declarator
8738 && declarator->kind == cdk_array
8739 && declarator->declarator
8740 && declarator->declarator->kind == cdk_array)
8741 {
8742 outer_declarator = declarator;
8743 declarator = declarator->declarator;
8744 }
8745
8746 if (declarator && declarator->kind == cdk_array)
8747 {
8748 *nelts = declarator->u.array.bounds;
8749 if (*nelts == error_mark_node)
8750 *nelts = integer_one_node;
8751
8752 if (outer_declarator)
8753 outer_declarator->declarator = declarator->declarator;
8754 else
8755 new_declarator = NULL;
8756 }
8757
8758 return groktypename (&type_specifier_seq, new_declarator, false);
8759 }
8760
8761 /* Parse an (optional) new-declarator.
8762
8763 new-declarator:
8764 ptr-operator new-declarator [opt]
8765 direct-new-declarator
8766
8767 Returns the declarator. */
8768
8769 static cp_declarator *
8770 cp_parser_new_declarator_opt (cp_parser* parser)
8771 {
8772 enum tree_code code;
8773 tree type, std_attributes = NULL_TREE;
8774 cp_cv_quals cv_quals;
8775
8776 /* We don't know if there's a ptr-operator next, or not. */
8777 cp_parser_parse_tentatively (parser);
8778 /* Look for a ptr-operator. */
8779 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8780 /* If that worked, look for more new-declarators. */
8781 if (cp_parser_parse_definitely (parser))
8782 {
8783 cp_declarator *declarator;
8784
8785 /* Parse another optional declarator. */
8786 declarator = cp_parser_new_declarator_opt (parser);
8787
8788 declarator = cp_parser_make_indirect_declarator
8789 (code, type, cv_quals, declarator, std_attributes);
8790
8791 return declarator;
8792 }
8793
8794 /* If the next token is a `[', there is a direct-new-declarator. */
8795 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8796 return cp_parser_direct_new_declarator (parser);
8797
8798 return NULL;
8799 }
8800
8801 /* Parse a direct-new-declarator.
8802
8803 direct-new-declarator:
8804 [ expression ]
8805 direct-new-declarator [constant-expression]
8806
8807 */
8808
8809 static cp_declarator *
8810 cp_parser_direct_new_declarator (cp_parser* parser)
8811 {
8812 cp_declarator *declarator = NULL;
8813
8814 while (true)
8815 {
8816 tree expression;
8817 cp_token *token;
8818
8819 /* Look for the opening `['. */
8820 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8821
8822 token = cp_lexer_peek_token (parser->lexer);
8823 expression = cp_parser_expression (parser);
8824 /* The standard requires that the expression have integral
8825 type. DR 74 adds enumeration types. We believe that the
8826 real intent is that these expressions be handled like the
8827 expression in a `switch' condition, which also allows
8828 classes with a single conversion to integral or
8829 enumeration type. */
8830 if (!processing_template_decl)
8831 {
8832 expression
8833 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8834 expression,
8835 /*complain=*/true);
8836 if (!expression)
8837 {
8838 error_at (token->location,
8839 "expression in new-declarator must have integral "
8840 "or enumeration type");
8841 expression = error_mark_node;
8842 }
8843 }
8844
8845 /* Look for the closing `]'. */
8846 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8847
8848 /* Add this bound to the declarator. */
8849 declarator = make_array_declarator (declarator, expression);
8850
8851 /* If the next token is not a `[', then there are no more
8852 bounds. */
8853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8854 break;
8855 }
8856
8857 return declarator;
8858 }
8859
8860 /* Parse a new-initializer.
8861
8862 new-initializer:
8863 ( expression-list [opt] )
8864 braced-init-list
8865
8866 Returns a representation of the expression-list. */
8867
8868 static vec<tree, va_gc> *
8869 cp_parser_new_initializer (cp_parser* parser)
8870 {
8871 vec<tree, va_gc> *expression_list;
8872
8873 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8874 {
8875 tree t;
8876 bool expr_non_constant_p;
8877 cp_lexer_set_source_position (parser->lexer);
8878 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8879 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8880 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8881 expression_list = make_tree_vector_single (t);
8882 }
8883 else
8884 expression_list = (cp_parser_parenthesized_expression_list
8885 (parser, non_attr, /*cast_p=*/false,
8886 /*allow_expansion_p=*/true,
8887 /*non_constant_p=*/NULL));
8888
8889 return expression_list;
8890 }
8891
8892 /* Parse a delete-expression.
8893
8894 delete-expression:
8895 :: [opt] delete cast-expression
8896 :: [opt] delete [ ] cast-expression
8897
8898 Returns a representation of the expression. */
8899
8900 static tree
8901 cp_parser_delete_expression (cp_parser* parser)
8902 {
8903 bool global_scope_p;
8904 bool array_p;
8905 tree expression;
8906
8907 /* Look for the optional `::' operator. */
8908 global_scope_p
8909 = (cp_parser_global_scope_opt (parser,
8910 /*current_scope_valid_p=*/false)
8911 != NULL_TREE);
8912 /* Look for the `delete' keyword. */
8913 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8914 /* See if the array syntax is in use. */
8915 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8916 {
8917 /* Consume the `[' token. */
8918 cp_lexer_consume_token (parser->lexer);
8919 /* Look for the `]' token. */
8920 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8921 /* Remember that this is the `[]' construct. */
8922 array_p = true;
8923 }
8924 else
8925 array_p = false;
8926
8927 /* Parse the cast-expression. */
8928 expression = cp_parser_simple_cast_expression (parser);
8929
8930 /* A delete-expression may not appear in an integral constant
8931 expression. */
8932 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8933 return error_mark_node;
8934
8935 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8936 tf_warning_or_error);
8937 }
8938
8939 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8940 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8941 0 otherwise. */
8942
8943 static int
8944 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8945 {
8946 cp_token *token = cp_lexer_peek_token (parser->lexer);
8947 switch (token->type)
8948 {
8949 case CPP_COMMA:
8950 case CPP_SEMICOLON:
8951 case CPP_QUERY:
8952 case CPP_COLON:
8953 case CPP_CLOSE_SQUARE:
8954 case CPP_CLOSE_PAREN:
8955 case CPP_CLOSE_BRACE:
8956 case CPP_OPEN_BRACE:
8957 case CPP_DOT:
8958 case CPP_DOT_STAR:
8959 case CPP_DEREF:
8960 case CPP_DEREF_STAR:
8961 case CPP_DIV:
8962 case CPP_MOD:
8963 case CPP_LSHIFT:
8964 case CPP_RSHIFT:
8965 case CPP_LESS:
8966 case CPP_GREATER:
8967 case CPP_LESS_EQ:
8968 case CPP_GREATER_EQ:
8969 case CPP_EQ_EQ:
8970 case CPP_NOT_EQ:
8971 case CPP_EQ:
8972 case CPP_MULT_EQ:
8973 case CPP_DIV_EQ:
8974 case CPP_MOD_EQ:
8975 case CPP_PLUS_EQ:
8976 case CPP_MINUS_EQ:
8977 case CPP_RSHIFT_EQ:
8978 case CPP_LSHIFT_EQ:
8979 case CPP_AND_EQ:
8980 case CPP_XOR_EQ:
8981 case CPP_OR_EQ:
8982 case CPP_XOR:
8983 case CPP_OR:
8984 case CPP_OR_OR:
8985 case CPP_EOF:
8986 case CPP_ELLIPSIS:
8987 return 0;
8988
8989 case CPP_OPEN_PAREN:
8990 /* In ((type ()) () the last () isn't a valid cast-expression,
8991 so the whole must be parsed as postfix-expression. */
8992 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8993 != CPP_CLOSE_PAREN;
8994
8995 case CPP_OPEN_SQUARE:
8996 /* '[' may start a primary-expression in obj-c++ and in C++11,
8997 as a lambda-expression, eg, '(void)[]{}'. */
8998 if (cxx_dialect >= cxx11)
8999 return -1;
9000 return c_dialect_objc ();
9001
9002 case CPP_PLUS_PLUS:
9003 case CPP_MINUS_MINUS:
9004 /* '++' and '--' may or may not start a cast-expression:
9005
9006 struct T { void operator++(int); };
9007 void f() { (T())++; }
9008
9009 vs
9010
9011 int a;
9012 (int)++a; */
9013 return -1;
9014
9015 default:
9016 return 1;
9017 }
9018 }
9019
9020 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9021 in the order: const_cast, static_cast, reinterpret_cast.
9022
9023 Don't suggest dynamic_cast.
9024
9025 Return the first legal cast kind found, or NULL otherwise. */
9026
9027 static const char *
9028 get_cast_suggestion (tree dst_type, tree orig_expr)
9029 {
9030 tree trial;
9031
9032 /* Reuse the parser logic by attempting to build the various kinds of
9033 cast, with "complain" disabled.
9034 Identify the first such cast that is valid. */
9035
9036 /* Don't attempt to run such logic within template processing. */
9037 if (processing_template_decl)
9038 return NULL;
9039
9040 /* First try const_cast. */
9041 trial = build_const_cast (dst_type, orig_expr, tf_none);
9042 if (trial != error_mark_node)
9043 return "const_cast";
9044
9045 /* If that fails, try static_cast. */
9046 trial = build_static_cast (dst_type, orig_expr, tf_none);
9047 if (trial != error_mark_node)
9048 return "static_cast";
9049
9050 /* Finally, try reinterpret_cast. */
9051 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9052 if (trial != error_mark_node)
9053 return "reinterpret_cast";
9054
9055 /* No such cast possible. */
9056 return NULL;
9057 }
9058
9059 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9060 suggesting how to convert a C-style cast of the form:
9061
9062 (DST_TYPE)ORIG_EXPR
9063
9064 to a C++-style cast.
9065
9066 The primary range of RICHLOC is asssumed to be that of the original
9067 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9068 of the parens in the C-style cast. */
9069
9070 static void
9071 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9072 location_t close_paren_loc, tree orig_expr,
9073 tree dst_type)
9074 {
9075 /* This function is non-trivial, so bail out now if the warning isn't
9076 going to be emitted. */
9077 if (!warn_old_style_cast)
9078 return;
9079
9080 /* Try to find a legal C++ cast, trying them in order:
9081 const_cast, static_cast, reinterpret_cast. */
9082 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9083 if (!cast_suggestion)
9084 return;
9085
9086 /* Replace the open paren with "CAST_SUGGESTION<". */
9087 pretty_printer pp;
9088 pp_printf (&pp, "%s<", cast_suggestion);
9089 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9090
9091 /* Replace the close paren with "> (". */
9092 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9093
9094 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9095 rich_loc->add_fixit_insert_after (")");
9096 }
9097
9098
9099 /* Parse a cast-expression.
9100
9101 cast-expression:
9102 unary-expression
9103 ( type-id ) cast-expression
9104
9105 ADDRESS_P is true iff the unary-expression is appearing as the
9106 operand of the `&' operator. CAST_P is true if this expression is
9107 the target of a cast.
9108
9109 Returns a representation of the expression. */
9110
9111 static cp_expr
9112 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9113 bool decltype_p, cp_id_kind * pidk)
9114 {
9115 /* If it's a `(', then we might be looking at a cast. */
9116 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9117 {
9118 tree type = NULL_TREE;
9119 cp_expr expr (NULL_TREE);
9120 int cast_expression = 0;
9121 const char *saved_message;
9122
9123 /* There's no way to know yet whether or not this is a cast.
9124 For example, `(int (3))' is a unary-expression, while `(int)
9125 3' is a cast. So, we resort to parsing tentatively. */
9126 cp_parser_parse_tentatively (parser);
9127 /* Types may not be defined in a cast. */
9128 saved_message = parser->type_definition_forbidden_message;
9129 parser->type_definition_forbidden_message
9130 = G_("types may not be defined in casts");
9131 /* Consume the `('. */
9132 matching_parens parens;
9133 cp_token *open_paren = parens.consume_open (parser);
9134 location_t open_paren_loc = open_paren->location;
9135 location_t close_paren_loc = UNKNOWN_LOCATION;
9136
9137 /* A very tricky bit is that `(struct S) { 3 }' is a
9138 compound-literal (which we permit in C++ as an extension).
9139 But, that construct is not a cast-expression -- it is a
9140 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9141 is legal; if the compound-literal were a cast-expression,
9142 you'd need an extra set of parentheses.) But, if we parse
9143 the type-id, and it happens to be a class-specifier, then we
9144 will commit to the parse at that point, because we cannot
9145 undo the action that is done when creating a new class. So,
9146 then we cannot back up and do a postfix-expression.
9147
9148 Another tricky case is the following (c++/29234):
9149
9150 struct S { void operator () (); };
9151
9152 void foo ()
9153 {
9154 ( S()() );
9155 }
9156
9157 As a type-id we parse the parenthesized S()() as a function
9158 returning a function, groktypename complains and we cannot
9159 back up in this case either.
9160
9161 Therefore, we scan ahead to the closing `)', and check to see
9162 if the tokens after the `)' can start a cast-expression. Otherwise
9163 we are dealing with an unary-expression, a postfix-expression
9164 or something else.
9165
9166 Yet another tricky case, in C++11, is the following (c++/54891):
9167
9168 (void)[]{};
9169
9170 The issue is that usually, besides the case of lambda-expressions,
9171 the parenthesized type-id cannot be followed by '[', and, eg, we
9172 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9173 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9174 we don't commit, we try a cast-expression, then an unary-expression.
9175
9176 Save tokens so that we can put them back. */
9177 cp_lexer_save_tokens (parser->lexer);
9178
9179 /* We may be looking at a cast-expression. */
9180 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9181 /*consume_paren=*/true))
9182 cast_expression
9183 = cp_parser_tokens_start_cast_expression (parser);
9184
9185 /* Roll back the tokens we skipped. */
9186 cp_lexer_rollback_tokens (parser->lexer);
9187 /* If we aren't looking at a cast-expression, simulate an error so
9188 that the call to cp_parser_error_occurred below returns true. */
9189 if (!cast_expression)
9190 cp_parser_simulate_error (parser);
9191 else
9192 {
9193 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9194 parser->in_type_id_in_expr_p = true;
9195 /* Look for the type-id. */
9196 type = cp_parser_type_id (parser);
9197 /* Look for the closing `)'. */
9198 cp_token *close_paren = parens.require_close (parser);
9199 if (close_paren)
9200 close_paren_loc = close_paren->location;
9201 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9202 }
9203
9204 /* Restore the saved message. */
9205 parser->type_definition_forbidden_message = saved_message;
9206
9207 /* At this point this can only be either a cast or a
9208 parenthesized ctor such as `(T ())' that looks like a cast to
9209 function returning T. */
9210 if (!cp_parser_error_occurred (parser))
9211 {
9212 /* Only commit if the cast-expression doesn't start with
9213 '++', '--', or '[' in C++11. */
9214 if (cast_expression > 0)
9215 cp_parser_commit_to_topmost_tentative_parse (parser);
9216
9217 expr = cp_parser_cast_expression (parser,
9218 /*address_p=*/false,
9219 /*cast_p=*/true,
9220 /*decltype_p=*/false,
9221 pidk);
9222
9223 if (cp_parser_parse_definitely (parser))
9224 {
9225 /* Warn about old-style casts, if so requested. */
9226 if (warn_old_style_cast
9227 && !in_system_header_at (input_location)
9228 && !VOID_TYPE_P (type)
9229 && current_lang_name != lang_name_c)
9230 {
9231 gcc_rich_location rich_loc (input_location);
9232 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9233 expr, type);
9234 warning_at (&rich_loc, OPT_Wold_style_cast,
9235 "use of old-style cast to %q#T", type);
9236 }
9237
9238 /* Only type conversions to integral or enumeration types
9239 can be used in constant-expressions. */
9240 if (!cast_valid_in_integral_constant_expression_p (type)
9241 && cp_parser_non_integral_constant_expression (parser,
9242 NIC_CAST))
9243 return error_mark_node;
9244
9245 /* Perform the cast. */
9246 /* Make a location:
9247 (TYPE) EXPR
9248 ^~~~~~~~~~~
9249 with start==caret at the open paren, extending to the
9250 end of "expr". */
9251 location_t cast_loc = make_location (open_paren_loc,
9252 open_paren_loc,
9253 expr.get_finish ());
9254 expr = build_c_cast (cast_loc, type, expr);
9255 return expr;
9256 }
9257 }
9258 else
9259 cp_parser_abort_tentative_parse (parser);
9260 }
9261
9262 /* If we get here, then it's not a cast, so it must be a
9263 unary-expression. */
9264 return cp_parser_unary_expression (parser, pidk, address_p,
9265 cast_p, decltype_p);
9266 }
9267
9268 /* Parse a binary expression of the general form:
9269
9270 pm-expression:
9271 cast-expression
9272 pm-expression .* cast-expression
9273 pm-expression ->* cast-expression
9274
9275 multiplicative-expression:
9276 pm-expression
9277 multiplicative-expression * pm-expression
9278 multiplicative-expression / pm-expression
9279 multiplicative-expression % pm-expression
9280
9281 additive-expression:
9282 multiplicative-expression
9283 additive-expression + multiplicative-expression
9284 additive-expression - multiplicative-expression
9285
9286 shift-expression:
9287 additive-expression
9288 shift-expression << additive-expression
9289 shift-expression >> additive-expression
9290
9291 relational-expression:
9292 shift-expression
9293 relational-expression < shift-expression
9294 relational-expression > shift-expression
9295 relational-expression <= shift-expression
9296 relational-expression >= shift-expression
9297
9298 GNU Extension:
9299
9300 relational-expression:
9301 relational-expression <? shift-expression
9302 relational-expression >? shift-expression
9303
9304 equality-expression:
9305 relational-expression
9306 equality-expression == relational-expression
9307 equality-expression != relational-expression
9308
9309 and-expression:
9310 equality-expression
9311 and-expression & equality-expression
9312
9313 exclusive-or-expression:
9314 and-expression
9315 exclusive-or-expression ^ and-expression
9316
9317 inclusive-or-expression:
9318 exclusive-or-expression
9319 inclusive-or-expression | exclusive-or-expression
9320
9321 logical-and-expression:
9322 inclusive-or-expression
9323 logical-and-expression && inclusive-or-expression
9324
9325 logical-or-expression:
9326 logical-and-expression
9327 logical-or-expression || logical-and-expression
9328
9329 All these are implemented with a single function like:
9330
9331 binary-expression:
9332 simple-cast-expression
9333 binary-expression <token> binary-expression
9334
9335 CAST_P is true if this expression is the target of a cast.
9336
9337 The binops_by_token map is used to get the tree codes for each <token> type.
9338 binary-expressions are associated according to a precedence table. */
9339
9340 #define TOKEN_PRECEDENCE(token) \
9341 (((token->type == CPP_GREATER \
9342 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9343 && !parser->greater_than_is_operator_p) \
9344 ? PREC_NOT_OPERATOR \
9345 : binops_by_token[token->type].prec)
9346
9347 static cp_expr
9348 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9349 bool no_toplevel_fold_p,
9350 bool decltype_p,
9351 enum cp_parser_prec prec,
9352 cp_id_kind * pidk)
9353 {
9354 cp_parser_expression_stack stack;
9355 cp_parser_expression_stack_entry *sp = &stack[0];
9356 cp_parser_expression_stack_entry current;
9357 cp_expr rhs;
9358 cp_token *token;
9359 enum tree_code rhs_type;
9360 enum cp_parser_prec new_prec, lookahead_prec;
9361 tree overload;
9362
9363 /* Parse the first expression. */
9364 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9365 ? TRUTH_NOT_EXPR : ERROR_MARK);
9366 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9367 cast_p, decltype_p, pidk);
9368 current.prec = prec;
9369
9370 if (cp_parser_error_occurred (parser))
9371 return error_mark_node;
9372
9373 for (;;)
9374 {
9375 /* Get an operator token. */
9376 token = cp_lexer_peek_token (parser->lexer);
9377
9378 if (warn_cxx11_compat
9379 && token->type == CPP_RSHIFT
9380 && !parser->greater_than_is_operator_p)
9381 {
9382 if (warning_at (token->location, OPT_Wc__11_compat,
9383 "%<>>%> operator is treated"
9384 " as two right angle brackets in C++11"))
9385 inform (token->location,
9386 "suggest parentheses around %<>>%> expression");
9387 }
9388
9389 new_prec = TOKEN_PRECEDENCE (token);
9390 if (new_prec != PREC_NOT_OPERATOR
9391 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9392 /* This is a fold-expression; handle it later. */
9393 new_prec = PREC_NOT_OPERATOR;
9394
9395 /* Popping an entry off the stack means we completed a subexpression:
9396 - either we found a token which is not an operator (`>' where it is not
9397 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9398 will happen repeatedly;
9399 - or, we found an operator which has lower priority. This is the case
9400 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9401 parsing `3 * 4'. */
9402 if (new_prec <= current.prec)
9403 {
9404 if (sp == stack)
9405 break;
9406 else
9407 goto pop;
9408 }
9409
9410 get_rhs:
9411 current.tree_type = binops_by_token[token->type].tree_type;
9412 current.loc = token->location;
9413
9414 /* We used the operator token. */
9415 cp_lexer_consume_token (parser->lexer);
9416
9417 /* For "false && x" or "true || x", x will never be executed;
9418 disable warnings while evaluating it. */
9419 if (current.tree_type == TRUTH_ANDIF_EXPR)
9420 c_inhibit_evaluation_warnings +=
9421 cp_fully_fold (current.lhs) == truthvalue_false_node;
9422 else if (current.tree_type == TRUTH_ORIF_EXPR)
9423 c_inhibit_evaluation_warnings +=
9424 cp_fully_fold (current.lhs) == truthvalue_true_node;
9425
9426 /* Extract another operand. It may be the RHS of this expression
9427 or the LHS of a new, higher priority expression. */
9428 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9429 ? TRUTH_NOT_EXPR : ERROR_MARK);
9430 rhs = cp_parser_simple_cast_expression (parser);
9431
9432 /* Get another operator token. Look up its precedence to avoid
9433 building a useless (immediately popped) stack entry for common
9434 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9435 token = cp_lexer_peek_token (parser->lexer);
9436 lookahead_prec = TOKEN_PRECEDENCE (token);
9437 if (lookahead_prec != PREC_NOT_OPERATOR
9438 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9439 lookahead_prec = PREC_NOT_OPERATOR;
9440 if (lookahead_prec > new_prec)
9441 {
9442 /* ... and prepare to parse the RHS of the new, higher priority
9443 expression. Since precedence levels on the stack are
9444 monotonically increasing, we do not have to care about
9445 stack overflows. */
9446 *sp = current;
9447 ++sp;
9448 current.lhs = rhs;
9449 current.lhs_type = rhs_type;
9450 current.prec = new_prec;
9451 new_prec = lookahead_prec;
9452 goto get_rhs;
9453
9454 pop:
9455 lookahead_prec = new_prec;
9456 /* If the stack is not empty, we have parsed into LHS the right side
9457 (`4' in the example above) of an expression we had suspended.
9458 We can use the information on the stack to recover the LHS (`3')
9459 from the stack together with the tree code (`MULT_EXPR'), and
9460 the precedence of the higher level subexpression
9461 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9462 which will be used to actually build the additive expression. */
9463 rhs = current.lhs;
9464 rhs_type = current.lhs_type;
9465 --sp;
9466 current = *sp;
9467 }
9468
9469 /* Undo the disabling of warnings done above. */
9470 if (current.tree_type == TRUTH_ANDIF_EXPR)
9471 c_inhibit_evaluation_warnings -=
9472 cp_fully_fold (current.lhs) == truthvalue_false_node;
9473 else if (current.tree_type == TRUTH_ORIF_EXPR)
9474 c_inhibit_evaluation_warnings -=
9475 cp_fully_fold (current.lhs) == truthvalue_true_node;
9476
9477 if (warn_logical_not_paren
9478 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9479 && current.lhs_type == TRUTH_NOT_EXPR
9480 /* Avoid warning for !!x == y. */
9481 && (TREE_CODE (current.lhs) != NE_EXPR
9482 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9483 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9484 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9485 /* Avoid warning for !b == y where b is boolean. */
9486 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9487 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9488 != BOOLEAN_TYPE))))
9489 /* Avoid warning for !!b == y where b is boolean. */
9490 && (!DECL_P (current.lhs)
9491 || TREE_TYPE (current.lhs) == NULL_TREE
9492 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9493 warn_logical_not_parentheses (current.loc, current.tree_type,
9494 current.lhs, maybe_constant_value (rhs));
9495
9496 overload = NULL;
9497
9498 location_t combined_loc = make_location (current.loc,
9499 current.lhs.get_start (),
9500 rhs.get_finish ());
9501
9502 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9503 ERROR_MARK for everything that is not a binary expression.
9504 This makes warn_about_parentheses miss some warnings that
9505 involve unary operators. For unary expressions we should
9506 pass the correct tree_code unless the unary expression was
9507 surrounded by parentheses.
9508 */
9509 if (no_toplevel_fold_p
9510 && lookahead_prec <= current.prec
9511 && sp == stack)
9512 {
9513 if (current.lhs == error_mark_node || rhs == error_mark_node)
9514 current.lhs = error_mark_node;
9515 else
9516 {
9517 current.lhs
9518 = build_min (current.tree_type,
9519 TREE_CODE_CLASS (current.tree_type)
9520 == tcc_comparison
9521 ? boolean_type_node : TREE_TYPE (current.lhs),
9522 current.lhs.get_value (), rhs.get_value ());
9523 SET_EXPR_LOCATION (current.lhs, combined_loc);
9524 }
9525 }
9526 else
9527 {
9528 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9529 current.lhs, current.lhs_type,
9530 rhs, rhs_type, &overload,
9531 complain_flags (decltype_p));
9532 /* TODO: build_x_binary_op doesn't always honor the location. */
9533 current.lhs.set_location (combined_loc);
9534 }
9535 current.lhs_type = current.tree_type;
9536
9537 /* If the binary operator required the use of an overloaded operator,
9538 then this expression cannot be an integral constant-expression.
9539 An overloaded operator can be used even if both operands are
9540 otherwise permissible in an integral constant-expression if at
9541 least one of the operands is of enumeration type. */
9542
9543 if (overload
9544 && cp_parser_non_integral_constant_expression (parser,
9545 NIC_OVERLOADED))
9546 return error_mark_node;
9547 }
9548
9549 return current.lhs;
9550 }
9551
9552 static cp_expr
9553 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9554 bool no_toplevel_fold_p,
9555 enum cp_parser_prec prec,
9556 cp_id_kind * pidk)
9557 {
9558 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9559 /*decltype*/false, prec, pidk);
9560 }
9561
9562 /* Parse the `? expression : assignment-expression' part of a
9563 conditional-expression. The LOGICAL_OR_EXPR is the
9564 logical-or-expression that started the conditional-expression.
9565 Returns a representation of the entire conditional-expression.
9566
9567 This routine is used by cp_parser_assignment_expression.
9568
9569 ? expression : assignment-expression
9570
9571 GNU Extensions:
9572
9573 ? : assignment-expression */
9574
9575 static tree
9576 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9577 {
9578 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9579 cp_expr assignment_expr;
9580 struct cp_token *token;
9581 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9582
9583 /* Consume the `?' token. */
9584 cp_lexer_consume_token (parser->lexer);
9585 token = cp_lexer_peek_token (parser->lexer);
9586 if (cp_parser_allow_gnu_extensions_p (parser)
9587 && token->type == CPP_COLON)
9588 {
9589 pedwarn (token->location, OPT_Wpedantic,
9590 "ISO C++ does not allow ?: with omitted middle operand");
9591 /* Implicit true clause. */
9592 expr = NULL_TREE;
9593 c_inhibit_evaluation_warnings +=
9594 folded_logical_or_expr == truthvalue_true_node;
9595 warn_for_omitted_condop (token->location, logical_or_expr);
9596 }
9597 else
9598 {
9599 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9600 parser->colon_corrects_to_scope_p = false;
9601 /* Parse the expression. */
9602 c_inhibit_evaluation_warnings +=
9603 folded_logical_or_expr == truthvalue_false_node;
9604 expr = cp_parser_expression (parser);
9605 c_inhibit_evaluation_warnings +=
9606 ((folded_logical_or_expr == truthvalue_true_node)
9607 - (folded_logical_or_expr == truthvalue_false_node));
9608 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9609 }
9610
9611 /* The next token should be a `:'. */
9612 cp_parser_require (parser, CPP_COLON, RT_COLON);
9613 /* Parse the assignment-expression. */
9614 assignment_expr = cp_parser_assignment_expression (parser);
9615 c_inhibit_evaluation_warnings -=
9616 folded_logical_or_expr == truthvalue_true_node;
9617
9618 /* Make a location:
9619 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9620 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9621 with the caret at the "?", ranging from the start of
9622 the logical_or_expr to the end of the assignment_expr. */
9623 loc = make_location (loc,
9624 logical_or_expr.get_start (),
9625 assignment_expr.get_finish ());
9626
9627 /* Build the conditional-expression. */
9628 return build_x_conditional_expr (loc, logical_or_expr,
9629 expr,
9630 assignment_expr,
9631 tf_warning_or_error);
9632 }
9633
9634 /* Parse an assignment-expression.
9635
9636 assignment-expression:
9637 conditional-expression
9638 logical-or-expression assignment-operator assignment_expression
9639 throw-expression
9640
9641 CAST_P is true if this expression is the target of a cast.
9642 DECLTYPE_P is true if this expression is the operand of decltype.
9643
9644 Returns a representation for the expression. */
9645
9646 static cp_expr
9647 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9648 bool cast_p, bool decltype_p)
9649 {
9650 cp_expr expr;
9651
9652 /* If the next token is the `throw' keyword, then we're looking at
9653 a throw-expression. */
9654 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9655 expr = cp_parser_throw_expression (parser);
9656 /* Otherwise, it must be that we are looking at a
9657 logical-or-expression. */
9658 else
9659 {
9660 /* Parse the binary expressions (logical-or-expression). */
9661 expr = cp_parser_binary_expression (parser, cast_p, false,
9662 decltype_p,
9663 PREC_NOT_OPERATOR, pidk);
9664 /* If the next token is a `?' then we're actually looking at a
9665 conditional-expression. */
9666 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9667 return cp_parser_question_colon_clause (parser, expr);
9668 else
9669 {
9670 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9671
9672 /* If it's an assignment-operator, we're using the second
9673 production. */
9674 enum tree_code assignment_operator
9675 = cp_parser_assignment_operator_opt (parser);
9676 if (assignment_operator != ERROR_MARK)
9677 {
9678 bool non_constant_p;
9679
9680 /* Parse the right-hand side of the assignment. */
9681 cp_expr rhs = cp_parser_initializer_clause (parser,
9682 &non_constant_p);
9683
9684 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9685 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9686
9687 /* An assignment may not appear in a
9688 constant-expression. */
9689 if (cp_parser_non_integral_constant_expression (parser,
9690 NIC_ASSIGNMENT))
9691 return error_mark_node;
9692 /* Build the assignment expression. Its default
9693 location:
9694 LHS = RHS
9695 ~~~~^~~~~
9696 is the location of the '=' token as the
9697 caret, ranging from the start of the lhs to the
9698 end of the rhs. */
9699 loc = make_location (loc,
9700 expr.get_start (),
9701 rhs.get_finish ());
9702 expr = build_x_modify_expr (loc, expr,
9703 assignment_operator,
9704 rhs,
9705 complain_flags (decltype_p));
9706 /* TODO: build_x_modify_expr doesn't honor the location,
9707 so we must set it here. */
9708 expr.set_location (loc);
9709 }
9710 }
9711 }
9712
9713 return expr;
9714 }
9715
9716 /* Parse an (optional) assignment-operator.
9717
9718 assignment-operator: one of
9719 = *= /= %= += -= >>= <<= &= ^= |=
9720
9721 GNU Extension:
9722
9723 assignment-operator: one of
9724 <?= >?=
9725
9726 If the next token is an assignment operator, the corresponding tree
9727 code is returned, and the token is consumed. For example, for
9728 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9729 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9730 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9731 operator, ERROR_MARK is returned. */
9732
9733 static enum tree_code
9734 cp_parser_assignment_operator_opt (cp_parser* parser)
9735 {
9736 enum tree_code op;
9737 cp_token *token;
9738
9739 /* Peek at the next token. */
9740 token = cp_lexer_peek_token (parser->lexer);
9741
9742 switch (token->type)
9743 {
9744 case CPP_EQ:
9745 op = NOP_EXPR;
9746 break;
9747
9748 case CPP_MULT_EQ:
9749 op = MULT_EXPR;
9750 break;
9751
9752 case CPP_DIV_EQ:
9753 op = TRUNC_DIV_EXPR;
9754 break;
9755
9756 case CPP_MOD_EQ:
9757 op = TRUNC_MOD_EXPR;
9758 break;
9759
9760 case CPP_PLUS_EQ:
9761 op = PLUS_EXPR;
9762 break;
9763
9764 case CPP_MINUS_EQ:
9765 op = MINUS_EXPR;
9766 break;
9767
9768 case CPP_RSHIFT_EQ:
9769 op = RSHIFT_EXPR;
9770 break;
9771
9772 case CPP_LSHIFT_EQ:
9773 op = LSHIFT_EXPR;
9774 break;
9775
9776 case CPP_AND_EQ:
9777 op = BIT_AND_EXPR;
9778 break;
9779
9780 case CPP_XOR_EQ:
9781 op = BIT_XOR_EXPR;
9782 break;
9783
9784 case CPP_OR_EQ:
9785 op = BIT_IOR_EXPR;
9786 break;
9787
9788 default:
9789 /* Nothing else is an assignment operator. */
9790 op = ERROR_MARK;
9791 }
9792
9793 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9794 if (op != ERROR_MARK
9795 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9796 op = ERROR_MARK;
9797
9798 /* If it was an assignment operator, consume it. */
9799 if (op != ERROR_MARK)
9800 cp_lexer_consume_token (parser->lexer);
9801
9802 return op;
9803 }
9804
9805 /* Parse an expression.
9806
9807 expression:
9808 assignment-expression
9809 expression , assignment-expression
9810
9811 CAST_P is true if this expression is the target of a cast.
9812 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9813 except possibly parenthesized or on the RHS of a comma (N3276).
9814
9815 Returns a representation of the expression. */
9816
9817 static cp_expr
9818 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9819 bool cast_p, bool decltype_p)
9820 {
9821 cp_expr expression = NULL_TREE;
9822 location_t loc = UNKNOWN_LOCATION;
9823
9824 while (true)
9825 {
9826 cp_expr assignment_expression;
9827
9828 /* Parse the next assignment-expression. */
9829 assignment_expression
9830 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9831
9832 /* We don't create a temporary for a call that is the immediate operand
9833 of decltype or on the RHS of a comma. But when we see a comma, we
9834 need to create a temporary for a call on the LHS. */
9835 if (decltype_p && !processing_template_decl
9836 && TREE_CODE (assignment_expression) == CALL_EXPR
9837 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9838 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9839 assignment_expression
9840 = build_cplus_new (TREE_TYPE (assignment_expression),
9841 assignment_expression, tf_warning_or_error);
9842
9843 /* If this is the first assignment-expression, we can just
9844 save it away. */
9845 if (!expression)
9846 expression = assignment_expression;
9847 else
9848 {
9849 /* Create a location with caret at the comma, ranging
9850 from the start of the LHS to the end of the RHS. */
9851 loc = make_location (loc,
9852 expression.get_start (),
9853 assignment_expression.get_finish ());
9854 expression = build_x_compound_expr (loc, expression,
9855 assignment_expression,
9856 complain_flags (decltype_p));
9857 expression.set_location (loc);
9858 }
9859 /* If the next token is not a comma, or we're in a fold-expression, then
9860 we are done with the expression. */
9861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9862 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9863 break;
9864 /* Consume the `,'. */
9865 loc = cp_lexer_peek_token (parser->lexer)->location;
9866 cp_lexer_consume_token (parser->lexer);
9867 /* A comma operator cannot appear in a constant-expression. */
9868 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9869 expression = error_mark_node;
9870 }
9871
9872 return expression;
9873 }
9874
9875 /* Parse a constant-expression.
9876
9877 constant-expression:
9878 conditional-expression
9879
9880 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9881 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9882 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9883 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9884 only parse a conditional-expression, otherwise parse an
9885 assignment-expression. See below for rationale. */
9886
9887 static cp_expr
9888 cp_parser_constant_expression (cp_parser* parser,
9889 bool allow_non_constant_p,
9890 bool *non_constant_p,
9891 bool strict_p)
9892 {
9893 bool saved_integral_constant_expression_p;
9894 bool saved_allow_non_integral_constant_expression_p;
9895 bool saved_non_integral_constant_expression_p;
9896 cp_expr expression;
9897
9898 /* It might seem that we could simply parse the
9899 conditional-expression, and then check to see if it were
9900 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9901 one that the compiler can figure out is constant, possibly after
9902 doing some simplifications or optimizations. The standard has a
9903 precise definition of constant-expression, and we must honor
9904 that, even though it is somewhat more restrictive.
9905
9906 For example:
9907
9908 int i[(2, 3)];
9909
9910 is not a legal declaration, because `(2, 3)' is not a
9911 constant-expression. The `,' operator is forbidden in a
9912 constant-expression. However, GCC's constant-folding machinery
9913 will fold this operation to an INTEGER_CST for `3'. */
9914
9915 /* Save the old settings. */
9916 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9917 saved_allow_non_integral_constant_expression_p
9918 = parser->allow_non_integral_constant_expression_p;
9919 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9920 /* We are now parsing a constant-expression. */
9921 parser->integral_constant_expression_p = true;
9922 parser->allow_non_integral_constant_expression_p
9923 = (allow_non_constant_p || cxx_dialect >= cxx11);
9924 parser->non_integral_constant_expression_p = false;
9925 /* Although the grammar says "conditional-expression", when not STRICT_P,
9926 we parse an "assignment-expression", which also permits
9927 "throw-expression" and the use of assignment operators. In the case
9928 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9929 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9930 actually essential that we look for an assignment-expression.
9931 For example, cp_parser_initializer_clauses uses this function to
9932 determine whether a particular assignment-expression is in fact
9933 constant. */
9934 if (strict_p)
9935 {
9936 /* Parse the binary expressions (logical-or-expression). */
9937 expression = cp_parser_binary_expression (parser, false, false, false,
9938 PREC_NOT_OPERATOR, NULL);
9939 /* If the next token is a `?' then we're actually looking at
9940 a conditional-expression; otherwise we're done. */
9941 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9942 expression = cp_parser_question_colon_clause (parser, expression);
9943 }
9944 else
9945 expression = cp_parser_assignment_expression (parser);
9946 /* Restore the old settings. */
9947 parser->integral_constant_expression_p
9948 = saved_integral_constant_expression_p;
9949 parser->allow_non_integral_constant_expression_p
9950 = saved_allow_non_integral_constant_expression_p;
9951 if (cxx_dialect >= cxx11)
9952 {
9953 /* Require an rvalue constant expression here; that's what our
9954 callers expect. Reference constant expressions are handled
9955 separately in e.g. cp_parser_template_argument. */
9956 tree decay = expression;
9957 if (TREE_TYPE (expression)
9958 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9959 decay = build_address (expression);
9960 bool is_const = potential_rvalue_constant_expression (decay);
9961 parser->non_integral_constant_expression_p = !is_const;
9962 if (!is_const && !allow_non_constant_p)
9963 require_potential_rvalue_constant_expression (decay);
9964 }
9965 if (allow_non_constant_p)
9966 *non_constant_p = parser->non_integral_constant_expression_p;
9967 parser->non_integral_constant_expression_p
9968 = saved_non_integral_constant_expression_p;
9969
9970 return expression;
9971 }
9972
9973 /* Parse __builtin_offsetof.
9974
9975 offsetof-expression:
9976 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9977
9978 offsetof-member-designator:
9979 id-expression
9980 | offsetof-member-designator "." id-expression
9981 | offsetof-member-designator "[" expression "]"
9982 | offsetof-member-designator "->" id-expression */
9983
9984 static cp_expr
9985 cp_parser_builtin_offsetof (cp_parser *parser)
9986 {
9987 int save_ice_p, save_non_ice_p;
9988 tree type;
9989 cp_expr expr;
9990 cp_id_kind dummy;
9991 cp_token *token;
9992 location_t finish_loc;
9993
9994 /* We're about to accept non-integral-constant things, but will
9995 definitely yield an integral constant expression. Save and
9996 restore these values around our local parsing. */
9997 save_ice_p = parser->integral_constant_expression_p;
9998 save_non_ice_p = parser->non_integral_constant_expression_p;
9999
10000 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10001
10002 /* Consume the "__builtin_offsetof" token. */
10003 cp_lexer_consume_token (parser->lexer);
10004 /* Consume the opening `('. */
10005 matching_parens parens;
10006 parens.require_open (parser);
10007 /* Parse the type-id. */
10008 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10009 {
10010 const char *saved_message = parser->type_definition_forbidden_message;
10011 parser->type_definition_forbidden_message
10012 = G_("types may not be defined within __builtin_offsetof");
10013 type = cp_parser_type_id (parser);
10014 parser->type_definition_forbidden_message = saved_message;
10015 }
10016 /* Look for the `,'. */
10017 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10018 token = cp_lexer_peek_token (parser->lexer);
10019
10020 /* Build the (type *)null that begins the traditional offsetof macro. */
10021 tree object_ptr
10022 = build_static_cast (build_pointer_type (type), null_pointer_node,
10023 tf_warning_or_error);
10024
10025 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10026 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10027 true, &dummy, token->location);
10028 while (true)
10029 {
10030 token = cp_lexer_peek_token (parser->lexer);
10031 switch (token->type)
10032 {
10033 case CPP_OPEN_SQUARE:
10034 /* offsetof-member-designator "[" expression "]" */
10035 expr = cp_parser_postfix_open_square_expression (parser, expr,
10036 true, false);
10037 break;
10038
10039 case CPP_DEREF:
10040 /* offsetof-member-designator "->" identifier */
10041 expr = grok_array_decl (token->location, expr,
10042 integer_zero_node, false);
10043 /* FALLTHRU */
10044
10045 case CPP_DOT:
10046 /* offsetof-member-designator "." identifier */
10047 cp_lexer_consume_token (parser->lexer);
10048 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10049 expr, true, &dummy,
10050 token->location);
10051 break;
10052
10053 case CPP_CLOSE_PAREN:
10054 /* Consume the ")" token. */
10055 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10056 cp_lexer_consume_token (parser->lexer);
10057 goto success;
10058
10059 default:
10060 /* Error. We know the following require will fail, but
10061 that gives the proper error message. */
10062 parens.require_close (parser);
10063 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10064 expr = error_mark_node;
10065 goto failure;
10066 }
10067 }
10068
10069 success:
10070 /* Make a location of the form:
10071 __builtin_offsetof (struct s, f)
10072 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10073 with caret at the type-id, ranging from the start of the
10074 "_builtin_offsetof" token to the close paren. */
10075 loc = make_location (loc, start_loc, finish_loc);
10076 /* The result will be an INTEGER_CST, so we need to explicitly
10077 preserve the location. */
10078 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10079
10080 failure:
10081 parser->integral_constant_expression_p = save_ice_p;
10082 parser->non_integral_constant_expression_p = save_non_ice_p;
10083
10084 expr = expr.maybe_add_location_wrapper ();
10085 return expr;
10086 }
10087
10088 /* Parse a trait expression.
10089
10090 Returns a representation of the expression, the underlying type
10091 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10092
10093 static cp_expr
10094 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10095 {
10096 cp_trait_kind kind;
10097 tree type1, type2 = NULL_TREE;
10098 bool binary = false;
10099 bool variadic = false;
10100
10101 switch (keyword)
10102 {
10103 case RID_HAS_NOTHROW_ASSIGN:
10104 kind = CPTK_HAS_NOTHROW_ASSIGN;
10105 break;
10106 case RID_HAS_NOTHROW_CONSTRUCTOR:
10107 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10108 break;
10109 case RID_HAS_NOTHROW_COPY:
10110 kind = CPTK_HAS_NOTHROW_COPY;
10111 break;
10112 case RID_HAS_TRIVIAL_ASSIGN:
10113 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10114 break;
10115 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10116 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10117 break;
10118 case RID_HAS_TRIVIAL_COPY:
10119 kind = CPTK_HAS_TRIVIAL_COPY;
10120 break;
10121 case RID_HAS_TRIVIAL_DESTRUCTOR:
10122 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10123 break;
10124 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10125 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10126 break;
10127 case RID_HAS_VIRTUAL_DESTRUCTOR:
10128 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10129 break;
10130 case RID_IS_ABSTRACT:
10131 kind = CPTK_IS_ABSTRACT;
10132 break;
10133 case RID_IS_AGGREGATE:
10134 kind = CPTK_IS_AGGREGATE;
10135 break;
10136 case RID_IS_BASE_OF:
10137 kind = CPTK_IS_BASE_OF;
10138 binary = true;
10139 break;
10140 case RID_IS_CLASS:
10141 kind = CPTK_IS_CLASS;
10142 break;
10143 case RID_IS_EMPTY:
10144 kind = CPTK_IS_EMPTY;
10145 break;
10146 case RID_IS_ENUM:
10147 kind = CPTK_IS_ENUM;
10148 break;
10149 case RID_IS_FINAL:
10150 kind = CPTK_IS_FINAL;
10151 break;
10152 case RID_IS_LITERAL_TYPE:
10153 kind = CPTK_IS_LITERAL_TYPE;
10154 break;
10155 case RID_IS_POD:
10156 kind = CPTK_IS_POD;
10157 break;
10158 case RID_IS_POLYMORPHIC:
10159 kind = CPTK_IS_POLYMORPHIC;
10160 break;
10161 case RID_IS_SAME_AS:
10162 kind = CPTK_IS_SAME_AS;
10163 binary = true;
10164 break;
10165 case RID_IS_STD_LAYOUT:
10166 kind = CPTK_IS_STD_LAYOUT;
10167 break;
10168 case RID_IS_TRIVIAL:
10169 kind = CPTK_IS_TRIVIAL;
10170 break;
10171 case RID_IS_TRIVIALLY_ASSIGNABLE:
10172 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10173 binary = true;
10174 break;
10175 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10176 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10177 variadic = true;
10178 break;
10179 case RID_IS_TRIVIALLY_COPYABLE:
10180 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10181 break;
10182 case RID_IS_UNION:
10183 kind = CPTK_IS_UNION;
10184 break;
10185 case RID_UNDERLYING_TYPE:
10186 kind = CPTK_UNDERLYING_TYPE;
10187 break;
10188 case RID_BASES:
10189 kind = CPTK_BASES;
10190 break;
10191 case RID_DIRECT_BASES:
10192 kind = CPTK_DIRECT_BASES;
10193 break;
10194 case RID_IS_ASSIGNABLE:
10195 kind = CPTK_IS_ASSIGNABLE;
10196 binary = true;
10197 break;
10198 case RID_IS_CONSTRUCTIBLE:
10199 kind = CPTK_IS_CONSTRUCTIBLE;
10200 variadic = true;
10201 break;
10202 default:
10203 gcc_unreachable ();
10204 }
10205
10206 /* Get location of initial token. */
10207 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10208
10209 /* Consume the token. */
10210 cp_lexer_consume_token (parser->lexer);
10211
10212 matching_parens parens;
10213 parens.require_open (parser);
10214
10215 {
10216 type_id_in_expr_sentinel s (parser);
10217 type1 = cp_parser_type_id (parser);
10218 }
10219
10220 if (type1 == error_mark_node)
10221 return error_mark_node;
10222
10223 if (binary)
10224 {
10225 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10226
10227 {
10228 type_id_in_expr_sentinel s (parser);
10229 type2 = cp_parser_type_id (parser);
10230 }
10231
10232 if (type2 == error_mark_node)
10233 return error_mark_node;
10234 }
10235 else if (variadic)
10236 {
10237 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10238 {
10239 cp_lexer_consume_token (parser->lexer);
10240 tree elt = cp_parser_type_id (parser);
10241 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10242 {
10243 cp_lexer_consume_token (parser->lexer);
10244 elt = make_pack_expansion (elt);
10245 }
10246 if (elt == error_mark_node)
10247 return error_mark_node;
10248 type2 = tree_cons (NULL_TREE, elt, type2);
10249 }
10250 }
10251
10252 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10253 parens.require_close (parser);
10254
10255 /* Construct a location of the form:
10256 __is_trivially_copyable(_Tp)
10257 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10258 with start == caret, finishing at the close-paren. */
10259 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10260
10261 /* Complete the trait expression, which may mean either processing
10262 the trait expr now or saving it for template instantiation. */
10263 switch (kind)
10264 {
10265 case CPTK_UNDERLYING_TYPE:
10266 return cp_expr (finish_underlying_type (type1), trait_loc);
10267 case CPTK_BASES:
10268 return cp_expr (finish_bases (type1, false), trait_loc);
10269 case CPTK_DIRECT_BASES:
10270 return cp_expr (finish_bases (type1, true), trait_loc);
10271 default:
10272 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10273 }
10274 }
10275
10276 /* Parse a lambda expression.
10277
10278 lambda-expression:
10279 lambda-introducer lambda-declarator [opt] compound-statement
10280
10281 Returns a representation of the expression. */
10282
10283 static cp_expr
10284 cp_parser_lambda_expression (cp_parser* parser)
10285 {
10286 tree lambda_expr = build_lambda_expr ();
10287 tree type;
10288 bool ok = true;
10289 cp_token *token = cp_lexer_peek_token (parser->lexer);
10290 cp_token_position start = 0;
10291
10292 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10293
10294 if (cxx_dialect >= cxx2a)
10295 /* C++20 allows lambdas in unevaluated context. */;
10296 else if (cp_unevaluated_operand)
10297 {
10298 if (!token->error_reported)
10299 {
10300 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10301 "lambda-expression in unevaluated context"
10302 " only available with -std=c++2a or -std=gnu++2a");
10303 token->error_reported = true;
10304 }
10305 ok = false;
10306 }
10307 else if (parser->in_template_argument_list_p)
10308 {
10309 if (!token->error_reported)
10310 {
10311 error_at (token->location, "lambda-expression in template-argument"
10312 " only available with -std=c++2a or -std=gnu++2a");
10313 token->error_reported = true;
10314 }
10315 ok = false;
10316 }
10317
10318 /* We may be in the middle of deferred access check. Disable
10319 it now. */
10320 push_deferring_access_checks (dk_no_deferred);
10321
10322 cp_parser_lambda_introducer (parser, lambda_expr);
10323 if (cp_parser_error_occurred (parser))
10324 return error_mark_node;
10325
10326 type = begin_lambda_type (lambda_expr);
10327 if (type == error_mark_node)
10328 return error_mark_node;
10329
10330 record_lambda_scope (lambda_expr);
10331
10332 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10333 determine_visibility (TYPE_NAME (type));
10334
10335 /* Now that we've started the type, add the capture fields for any
10336 explicit captures. */
10337 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10338
10339 {
10340 /* Inside the class, surrounding template-parameter-lists do not apply. */
10341 unsigned int saved_num_template_parameter_lists
10342 = parser->num_template_parameter_lists;
10343 unsigned char in_statement = parser->in_statement;
10344 bool in_switch_statement_p = parser->in_switch_statement_p;
10345 bool fully_implicit_function_template_p
10346 = parser->fully_implicit_function_template_p;
10347 tree implicit_template_parms = parser->implicit_template_parms;
10348 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10349 bool auto_is_implicit_function_template_parm_p
10350 = parser->auto_is_implicit_function_template_parm_p;
10351
10352 parser->num_template_parameter_lists = 0;
10353 parser->in_statement = 0;
10354 parser->in_switch_statement_p = false;
10355 parser->fully_implicit_function_template_p = false;
10356 parser->implicit_template_parms = 0;
10357 parser->implicit_template_scope = 0;
10358 parser->auto_is_implicit_function_template_parm_p = false;
10359
10360 /* By virtue of defining a local class, a lambda expression has access to
10361 the private variables of enclosing classes. */
10362
10363 if (cp_parser_start_tentative_firewall (parser))
10364 start = token;
10365
10366 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10367
10368 if (ok && cp_parser_error_occurred (parser))
10369 ok = false;
10370
10371 if (ok)
10372 {
10373 cp_parser_lambda_body (parser, lambda_expr);
10374 }
10375 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10376 {
10377 if (cp_parser_skip_to_closing_brace (parser))
10378 cp_lexer_consume_token (parser->lexer);
10379 }
10380
10381 /* The capture list was built up in reverse order; fix that now. */
10382 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10383 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10384
10385 if (ok)
10386 maybe_add_lambda_conv_op (type);
10387
10388 type = finish_struct (type, /*attributes=*/NULL_TREE);
10389
10390 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10391 parser->in_statement = in_statement;
10392 parser->in_switch_statement_p = in_switch_statement_p;
10393 parser->fully_implicit_function_template_p
10394 = fully_implicit_function_template_p;
10395 parser->implicit_template_parms = implicit_template_parms;
10396 parser->implicit_template_scope = implicit_template_scope;
10397 parser->auto_is_implicit_function_template_parm_p
10398 = auto_is_implicit_function_template_parm_p;
10399 }
10400
10401 /* This field is only used during parsing of the lambda. */
10402 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10403
10404 /* This lambda shouldn't have any proxies left at this point. */
10405 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10406 /* And now that we're done, push proxies for an enclosing lambda. */
10407 insert_pending_capture_proxies ();
10408
10409 /* Update the lambda expression to a range. */
10410 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10411 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10412 token->location,
10413 end_tok->location);
10414
10415 if (ok)
10416 lambda_expr = build_lambda_object (lambda_expr);
10417 else
10418 lambda_expr = error_mark_node;
10419
10420 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10421
10422 pop_deferring_access_checks ();
10423
10424 return lambda_expr;
10425 }
10426
10427 /* Parse the beginning of a lambda expression.
10428
10429 lambda-introducer:
10430 [ lambda-capture [opt] ]
10431
10432 LAMBDA_EXPR is the current representation of the lambda expression. */
10433
10434 static void
10435 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10436 {
10437 /* Need commas after the first capture. */
10438 bool first = true;
10439
10440 /* Eat the leading `['. */
10441 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10442
10443 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10444 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10445 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10446 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10447 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10448 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10449
10450 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10451 {
10452 cp_lexer_consume_token (parser->lexer);
10453 first = false;
10454
10455 if (!(at_function_scope_p () || parsing_nsdmi ()))
10456 error ("non-local lambda expression cannot have a capture-default");
10457 }
10458
10459 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10460 {
10461 cp_token* capture_token;
10462 tree capture_id;
10463 tree capture_init_expr;
10464 cp_id_kind idk = CP_ID_KIND_NONE;
10465 bool explicit_init_p = false;
10466
10467 enum capture_kind_type
10468 {
10469 BY_COPY,
10470 BY_REFERENCE
10471 };
10472 enum capture_kind_type capture_kind = BY_COPY;
10473
10474 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10475 {
10476 error ("expected end of capture-list");
10477 return;
10478 }
10479
10480 if (first)
10481 first = false;
10482 else
10483 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10484
10485 /* Possibly capture `this'. */
10486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10487 {
10488 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10489 if (cxx_dialect < cxx2a
10490 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10491 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10492 "with by-copy capture default");
10493 cp_lexer_consume_token (parser->lexer);
10494 add_capture (lambda_expr,
10495 /*id=*/this_identifier,
10496 /*initializer=*/finish_this_expr (),
10497 /*by_reference_p=*/true,
10498 explicit_init_p);
10499 continue;
10500 }
10501
10502 /* Possibly capture `*this'. */
10503 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10504 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10505 {
10506 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10507 if (cxx_dialect < cxx17)
10508 pedwarn (loc, 0, "%<*this%> capture only available with "
10509 "-std=c++17 or -std=gnu++17");
10510 cp_lexer_consume_token (parser->lexer);
10511 cp_lexer_consume_token (parser->lexer);
10512 add_capture (lambda_expr,
10513 /*id=*/this_identifier,
10514 /*initializer=*/finish_this_expr (),
10515 /*by_reference_p=*/false,
10516 explicit_init_p);
10517 continue;
10518 }
10519
10520 bool init_pack_expansion = false;
10521 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10522 {
10523 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10524 if (cxx_dialect < cxx2a)
10525 pedwarn (loc, 0, "pack init-capture only available with "
10526 "-std=c++2a or -std=gnu++2a");
10527 cp_lexer_consume_token (parser->lexer);
10528 init_pack_expansion = true;
10529 }
10530
10531 /* Remember whether we want to capture as a reference or not. */
10532 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10533 {
10534 capture_kind = BY_REFERENCE;
10535 cp_lexer_consume_token (parser->lexer);
10536 }
10537
10538 /* Get the identifier. */
10539 capture_token = cp_lexer_peek_token (parser->lexer);
10540 capture_id = cp_parser_identifier (parser);
10541
10542 if (capture_id == error_mark_node)
10543 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10544 delimiters, but I modified this to stop on unnested ']' as well. It
10545 was already changed to stop on unnested '}', so the
10546 "closing_parenthesis" name is no more misleading with my change. */
10547 {
10548 cp_parser_skip_to_closing_parenthesis (parser,
10549 /*recovering=*/true,
10550 /*or_comma=*/true,
10551 /*consume_paren=*/true);
10552 break;
10553 }
10554
10555 /* Find the initializer for this capture. */
10556 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10557 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10558 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10559 {
10560 bool direct, non_constant;
10561 /* An explicit initializer exists. */
10562 if (cxx_dialect < cxx14)
10563 pedwarn (input_location, 0,
10564 "lambda capture initializers "
10565 "only available with -std=c++14 or -std=gnu++14");
10566 capture_init_expr = cp_parser_initializer (parser, &direct,
10567 &non_constant, true);
10568 explicit_init_p = true;
10569 if (capture_init_expr == NULL_TREE)
10570 {
10571 error ("empty initializer for lambda init-capture");
10572 capture_init_expr = error_mark_node;
10573 }
10574 if (init_pack_expansion)
10575 capture_init_expr = make_pack_expansion (capture_init_expr);
10576 }
10577 else
10578 {
10579 const char* error_msg;
10580
10581 /* Turn the identifier into an id-expression. */
10582 capture_init_expr
10583 = cp_parser_lookup_name_simple (parser, capture_id,
10584 capture_token->location);
10585
10586 if (capture_init_expr == error_mark_node)
10587 {
10588 unqualified_name_lookup_error (capture_id);
10589 continue;
10590 }
10591 else if (!VAR_P (capture_init_expr)
10592 && TREE_CODE (capture_init_expr) != PARM_DECL)
10593 {
10594 error_at (capture_token->location,
10595 "capture of non-variable %qE",
10596 capture_init_expr);
10597 if (DECL_P (capture_init_expr))
10598 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10599 "%q#D declared here", capture_init_expr);
10600 continue;
10601 }
10602 if (VAR_P (capture_init_expr)
10603 && decl_storage_duration (capture_init_expr) != dk_auto)
10604 {
10605 if (pedwarn (capture_token->location, 0, "capture of variable "
10606 "%qD with non-automatic storage duration",
10607 capture_init_expr))
10608 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10609 "%q#D declared here", capture_init_expr);
10610 continue;
10611 }
10612
10613 capture_init_expr
10614 = finish_id_expression
10615 (capture_id,
10616 capture_init_expr,
10617 parser->scope,
10618 &idk,
10619 /*integral_constant_expression_p=*/false,
10620 /*allow_non_integral_constant_expression_p=*/false,
10621 /*non_integral_constant_expression_p=*/NULL,
10622 /*template_p=*/false,
10623 /*done=*/true,
10624 /*address_p=*/false,
10625 /*template_arg_p=*/false,
10626 &error_msg,
10627 capture_token->location);
10628
10629 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10630 {
10631 cp_lexer_consume_token (parser->lexer);
10632 capture_init_expr = make_pack_expansion (capture_init_expr);
10633 }
10634 }
10635
10636 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10637 && !explicit_init_p)
10638 {
10639 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10640 && capture_kind == BY_COPY)
10641 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10642 "of %qD redundant with by-copy capture default",
10643 capture_id);
10644 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10645 && capture_kind == BY_REFERENCE)
10646 pedwarn (capture_token->location, 0, "explicit by-reference "
10647 "capture of %qD redundant with by-reference capture "
10648 "default", capture_id);
10649 }
10650
10651 add_capture (lambda_expr,
10652 capture_id,
10653 capture_init_expr,
10654 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10655 explicit_init_p);
10656
10657 /* If there is any qualification still in effect, clear it
10658 now; we will be starting fresh with the next capture. */
10659 parser->scope = NULL_TREE;
10660 parser->qualifying_scope = NULL_TREE;
10661 parser->object_scope = NULL_TREE;
10662 }
10663
10664 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10665 }
10666
10667 /* Parse the (optional) middle of a lambda expression.
10668
10669 lambda-declarator:
10670 < template-parameter-list [opt] >
10671 ( parameter-declaration-clause [opt] )
10672 attribute-specifier [opt]
10673 decl-specifier-seq [opt]
10674 exception-specification [opt]
10675 lambda-return-type-clause [opt]
10676
10677 LAMBDA_EXPR is the current representation of the lambda expression. */
10678
10679 static bool
10680 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10681 {
10682 /* 5.1.1.4 of the standard says:
10683 If a lambda-expression does not include a lambda-declarator, it is as if
10684 the lambda-declarator were ().
10685 This means an empty parameter list, no attributes, and no exception
10686 specification. */
10687 tree param_list = void_list_node;
10688 tree attributes = NULL_TREE;
10689 tree exception_spec = NULL_TREE;
10690 tree template_param_list = NULL_TREE;
10691 tree tx_qual = NULL_TREE;
10692 tree return_type = NULL_TREE;
10693 cp_decl_specifier_seq lambda_specs;
10694 clear_decl_specs (&lambda_specs);
10695
10696 /* The template-parameter-list is optional, but must begin with
10697 an opening angle if present. */
10698 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10699 {
10700 if (cxx_dialect < cxx14)
10701 pedwarn (parser->lexer->next_token->location, 0,
10702 "lambda templates are only available with "
10703 "-std=c++14 or -std=gnu++14");
10704 else if (cxx_dialect < cxx2a)
10705 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10706 "lambda templates are only available with "
10707 "-std=c++2a or -std=gnu++2a");
10708
10709 cp_lexer_consume_token (parser->lexer);
10710
10711 template_param_list = cp_parser_template_parameter_list (parser);
10712
10713 cp_parser_skip_to_end_of_template_parameter_list (parser);
10714
10715 /* We just processed one more parameter list. */
10716 ++parser->num_template_parameter_lists;
10717 }
10718
10719 /* The parameter-declaration-clause is optional (unless
10720 template-parameter-list was given), but must begin with an
10721 opening parenthesis if present. */
10722 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10723 {
10724 matching_parens parens;
10725 parens.consume_open (parser);
10726
10727 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10728
10729 /* Parse parameters. */
10730 param_list = cp_parser_parameter_declaration_clause (parser);
10731
10732 /* Default arguments shall not be specified in the
10733 parameter-declaration-clause of a lambda-declarator. */
10734 if (cxx_dialect < cxx14)
10735 for (tree t = param_list; t; t = TREE_CHAIN (t))
10736 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10737 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10738 "default argument specified for lambda parameter");
10739
10740 parens.require_close (parser);
10741
10742 /* In the decl-specifier-seq of the lambda-declarator, each
10743 decl-specifier shall either be mutable or constexpr. */
10744 int declares_class_or_enum;
10745 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10746 cp_parser_decl_specifier_seq (parser,
10747 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10748 &lambda_specs, &declares_class_or_enum);
10749 if (lambda_specs.storage_class == sc_mutable)
10750 {
10751 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10752 if (lambda_specs.conflicting_specifiers_p)
10753 error_at (lambda_specs.locations[ds_storage_class],
10754 "duplicate %<mutable%>");
10755 }
10756
10757 tx_qual = cp_parser_tx_qualifier_opt (parser);
10758
10759 /* Parse optional exception specification. */
10760 exception_spec = cp_parser_exception_specification_opt (parser);
10761
10762 attributes = cp_parser_std_attribute_spec_seq (parser);
10763
10764 /* Parse optional trailing return type. */
10765 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10766 {
10767 cp_lexer_consume_token (parser->lexer);
10768 return_type = cp_parser_trailing_type_id (parser);
10769 }
10770
10771 /* The function parameters must be in scope all the way until after the
10772 trailing-return-type in case of decltype. */
10773 pop_bindings_and_leave_scope ();
10774 }
10775 else if (template_param_list != NULL_TREE) // generate diagnostic
10776 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10777
10778 /* Create the function call operator.
10779
10780 Messing with declarators like this is no uglier than building up the
10781 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10782 other code. */
10783 {
10784 cp_decl_specifier_seq return_type_specs;
10785 cp_declarator* declarator;
10786 tree fco;
10787 int quals;
10788 void *p;
10789
10790 clear_decl_specs (&return_type_specs);
10791 return_type_specs.type = make_auto ();
10792
10793 if (lambda_specs.locations[ds_constexpr])
10794 {
10795 if (cxx_dialect >= cxx17)
10796 return_type_specs.locations[ds_constexpr]
10797 = lambda_specs.locations[ds_constexpr];
10798 else
10799 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10800 "lambda only available with -std=c++17 or -std=gnu++17");
10801 }
10802
10803 p = obstack_alloc (&declarator_obstack, 0);
10804
10805 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10806 LAMBDA_EXPR_LOCATION (lambda_expr));
10807
10808 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10809 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10810 declarator = make_call_declarator (declarator, param_list, quals,
10811 VIRT_SPEC_UNSPECIFIED,
10812 REF_QUAL_NONE,
10813 tx_qual,
10814 exception_spec,
10815 return_type,
10816 /*requires_clause*/NULL_TREE);
10817 declarator->std_attributes = attributes;
10818
10819 fco = grokmethod (&return_type_specs,
10820 declarator,
10821 NULL_TREE);
10822 if (fco != error_mark_node)
10823 {
10824 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10825 DECL_ARTIFICIAL (fco) = 1;
10826 /* Give the object parameter a different name. */
10827 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10828 DECL_LAMBDA_FUNCTION (fco) = 1;
10829 }
10830 if (template_param_list)
10831 {
10832 fco = finish_member_template_decl (fco);
10833 finish_template_decl (template_param_list);
10834 --parser->num_template_parameter_lists;
10835 }
10836 else if (parser->fully_implicit_function_template_p)
10837 fco = finish_fully_implicit_template (parser, fco);
10838
10839 finish_member_declaration (fco);
10840
10841 obstack_free (&declarator_obstack, p);
10842
10843 return (fco != error_mark_node);
10844 }
10845 }
10846
10847 /* Parse the body of a lambda expression, which is simply
10848
10849 compound-statement
10850
10851 but which requires special handling.
10852 LAMBDA_EXPR is the current representation of the lambda expression. */
10853
10854 static void
10855 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10856 {
10857 bool nested = (current_function_decl != NULL_TREE);
10858 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10859 bool in_function_body = parser->in_function_body;
10860
10861 /* The body of a lambda-expression is not a subexpression of the enclosing
10862 expression. */
10863 cp_evaluated ev;
10864
10865 if (nested)
10866 push_function_context ();
10867 else
10868 /* Still increment function_depth so that we don't GC in the
10869 middle of an expression. */
10870 ++function_depth;
10871
10872 vec<tree> omp_privatization_save;
10873 save_omp_privatization_clauses (omp_privatization_save);
10874 /* Clear this in case we're in the middle of a default argument. */
10875 parser->local_variables_forbidden_p = false;
10876 parser->in_function_body = true;
10877
10878 {
10879 local_specialization_stack s (lss_copy);
10880 tree fco = lambda_function (lambda_expr);
10881 tree body = start_lambda_function (fco, lambda_expr);
10882 matching_braces braces;
10883
10884 if (braces.require_open (parser))
10885 {
10886 tree compound_stmt = begin_compound_stmt (0);
10887
10888 /* Originally C++11 required us to peek for 'return expr'; and
10889 process it specially here to deduce the return type. N3638
10890 removed the need for that. */
10891
10892 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10893 cp_parser_label_declaration (parser);
10894 cp_parser_statement_seq_opt (parser, NULL_TREE);
10895 braces.require_close (parser);
10896
10897 finish_compound_stmt (compound_stmt);
10898 }
10899
10900 finish_lambda_function (body);
10901 }
10902
10903 restore_omp_privatization_clauses (omp_privatization_save);
10904 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10905 parser->in_function_body = in_function_body;
10906 if (nested)
10907 pop_function_context();
10908 else
10909 --function_depth;
10910 }
10911
10912 /* Statements [gram.stmt.stmt] */
10913
10914 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10915
10916 static void
10917 add_debug_begin_stmt (location_t loc)
10918 {
10919 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10920 return;
10921 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10922 /* A concept is never expanded normally. */
10923 return;
10924
10925 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10926 SET_EXPR_LOCATION (stmt, loc);
10927 add_stmt (stmt);
10928 }
10929
10930 /* Parse a statement.
10931
10932 statement:
10933 labeled-statement
10934 expression-statement
10935 compound-statement
10936 selection-statement
10937 iteration-statement
10938 jump-statement
10939 declaration-statement
10940 try-block
10941
10942 C++11:
10943
10944 statement:
10945 labeled-statement
10946 attribute-specifier-seq (opt) expression-statement
10947 attribute-specifier-seq (opt) compound-statement
10948 attribute-specifier-seq (opt) selection-statement
10949 attribute-specifier-seq (opt) iteration-statement
10950 attribute-specifier-seq (opt) jump-statement
10951 declaration-statement
10952 attribute-specifier-seq (opt) try-block
10953
10954 init-statement:
10955 expression-statement
10956 simple-declaration
10957
10958 TM Extension:
10959
10960 statement:
10961 atomic-statement
10962
10963 IN_COMPOUND is true when the statement is nested inside a
10964 cp_parser_compound_statement; this matters for certain pragmas.
10965
10966 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10967 is a (possibly labeled) if statement which is not enclosed in braces
10968 and has an else clause. This is used to implement -Wparentheses.
10969
10970 CHAIN is a vector of if-else-if conditions. */
10971
10972 static void
10973 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10974 bool in_compound, bool *if_p, vec<tree> *chain,
10975 location_t *loc_after_labels)
10976 {
10977 tree statement, std_attrs = NULL_TREE;
10978 cp_token *token;
10979 location_t statement_location, attrs_location;
10980
10981 restart:
10982 if (if_p != NULL)
10983 *if_p = false;
10984 /* There is no statement yet. */
10985 statement = NULL_TREE;
10986
10987 saved_token_sentinel saved_tokens (parser->lexer);
10988 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10989 if (c_dialect_objc ())
10990 /* In obj-c++, seeing '[[' might be the either the beginning of
10991 c++11 attributes, or a nested objc-message-expression. So
10992 let's parse the c++11 attributes tentatively. */
10993 cp_parser_parse_tentatively (parser);
10994 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10995 if (c_dialect_objc ())
10996 {
10997 if (!cp_parser_parse_definitely (parser))
10998 std_attrs = NULL_TREE;
10999 }
11000
11001 /* Peek at the next token. */
11002 token = cp_lexer_peek_token (parser->lexer);
11003 /* Remember the location of the first token in the statement. */
11004 cp_token *statement_token = token;
11005 statement_location = token->location;
11006 add_debug_begin_stmt (statement_location);
11007 /* If this is a keyword, then that will often determine what kind of
11008 statement we have. */
11009 if (token->type == CPP_KEYWORD)
11010 {
11011 enum rid keyword = token->keyword;
11012
11013 switch (keyword)
11014 {
11015 case RID_CASE:
11016 case RID_DEFAULT:
11017 /* Looks like a labeled-statement with a case label.
11018 Parse the label, and then use tail recursion to parse
11019 the statement. */
11020 cp_parser_label_for_labeled_statement (parser, std_attrs);
11021 in_compound = false;
11022 goto restart;
11023
11024 case RID_IF:
11025 case RID_SWITCH:
11026 std_attrs = process_stmt_hotness_attribute (std_attrs);
11027 statement = cp_parser_selection_statement (parser, if_p, chain);
11028 break;
11029
11030 case RID_WHILE:
11031 case RID_DO:
11032 case RID_FOR:
11033 std_attrs = process_stmt_hotness_attribute (std_attrs);
11034 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11035 break;
11036
11037 case RID_BREAK:
11038 case RID_CONTINUE:
11039 case RID_RETURN:
11040 case RID_GOTO:
11041 std_attrs = process_stmt_hotness_attribute (std_attrs);
11042 statement = cp_parser_jump_statement (parser);
11043 break;
11044
11045 /* Objective-C++ exception-handling constructs. */
11046 case RID_AT_TRY:
11047 case RID_AT_CATCH:
11048 case RID_AT_FINALLY:
11049 case RID_AT_SYNCHRONIZED:
11050 case RID_AT_THROW:
11051 std_attrs = process_stmt_hotness_attribute (std_attrs);
11052 statement = cp_parser_objc_statement (parser);
11053 break;
11054
11055 case RID_TRY:
11056 std_attrs = process_stmt_hotness_attribute (std_attrs);
11057 statement = cp_parser_try_block (parser);
11058 break;
11059
11060 case RID_NAMESPACE:
11061 /* This must be a namespace alias definition. */
11062 if (std_attrs != NULL_TREE)
11063 {
11064 /* Attributes should be parsed as part of the the
11065 declaration, so let's un-parse them. */
11066 saved_tokens.rollback();
11067 std_attrs = NULL_TREE;
11068 }
11069 cp_parser_declaration_statement (parser);
11070 return;
11071
11072 case RID_TRANSACTION_ATOMIC:
11073 case RID_TRANSACTION_RELAXED:
11074 case RID_SYNCHRONIZED:
11075 case RID_ATOMIC_NOEXCEPT:
11076 case RID_ATOMIC_CANCEL:
11077 std_attrs = process_stmt_hotness_attribute (std_attrs);
11078 statement = cp_parser_transaction (parser, token);
11079 break;
11080 case RID_TRANSACTION_CANCEL:
11081 std_attrs = process_stmt_hotness_attribute (std_attrs);
11082 statement = cp_parser_transaction_cancel (parser);
11083 break;
11084
11085 default:
11086 /* It might be a keyword like `int' that can start a
11087 declaration-statement. */
11088 break;
11089 }
11090 }
11091 else if (token->type == CPP_NAME)
11092 {
11093 /* If the next token is a `:', then we are looking at a
11094 labeled-statement. */
11095 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11096 if (token->type == CPP_COLON)
11097 {
11098 /* Looks like a labeled-statement with an ordinary label.
11099 Parse the label, and then use tail recursion to parse
11100 the statement. */
11101
11102 cp_parser_label_for_labeled_statement (parser, std_attrs);
11103 in_compound = false;
11104 goto restart;
11105 }
11106 }
11107 /* Anything that starts with a `{' must be a compound-statement. */
11108 else if (token->type == CPP_OPEN_BRACE)
11109 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11110 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11111 a statement all its own. */
11112 else if (token->type == CPP_PRAGMA)
11113 {
11114 /* Only certain OpenMP pragmas are attached to statements, and thus
11115 are considered statements themselves. All others are not. In
11116 the context of a compound, accept the pragma as a "statement" and
11117 return so that we can check for a close brace. Otherwise we
11118 require a real statement and must go back and read one. */
11119 if (in_compound)
11120 cp_parser_pragma (parser, pragma_compound, if_p);
11121 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11122 goto restart;
11123 return;
11124 }
11125 else if (token->type == CPP_EOF)
11126 {
11127 cp_parser_error (parser, "expected statement");
11128 return;
11129 }
11130
11131 /* Everything else must be a declaration-statement or an
11132 expression-statement. Try for the declaration-statement
11133 first, unless we are looking at a `;', in which case we know that
11134 we have an expression-statement. */
11135 if (!statement)
11136 {
11137 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11138 {
11139 if (std_attrs != NULL_TREE)
11140 /* Attributes should be parsed as part of the declaration,
11141 so let's un-parse them. */
11142 saved_tokens.rollback();
11143
11144 cp_parser_parse_tentatively (parser);
11145 /* Try to parse the declaration-statement. */
11146 cp_parser_declaration_statement (parser);
11147 /* If that worked, we're done. */
11148 if (cp_parser_parse_definitely (parser))
11149 return;
11150 /* It didn't work, restore the post-attribute position. */
11151 if (std_attrs)
11152 cp_lexer_set_token_position (parser->lexer, statement_token);
11153 }
11154 /* All preceding labels have been parsed at this point. */
11155 if (loc_after_labels != NULL)
11156 *loc_after_labels = statement_location;
11157
11158 std_attrs = process_stmt_hotness_attribute (std_attrs);
11159
11160 /* Look for an expression-statement instead. */
11161 statement = cp_parser_expression_statement (parser, in_statement_expr);
11162
11163 /* Handle [[fallthrough]];. */
11164 if (attribute_fallthrough_p (std_attrs))
11165 {
11166 /* The next token after the fallthrough attribute is ';'. */
11167 if (statement == NULL_TREE)
11168 {
11169 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11170 statement = build_call_expr_internal_loc (statement_location,
11171 IFN_FALLTHROUGH,
11172 void_type_node, 0);
11173 finish_expr_stmt (statement);
11174 }
11175 else
11176 warning_at (statement_location, OPT_Wattributes,
11177 "%<fallthrough%> attribute not followed by %<;%>");
11178 std_attrs = NULL_TREE;
11179 }
11180 }
11181
11182 /* Set the line number for the statement. */
11183 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11184 SET_EXPR_LOCATION (statement, statement_location);
11185
11186 /* Allow "[[fallthrough]];", but warn otherwise. */
11187 if (std_attrs != NULL_TREE)
11188 warning_at (attrs_location,
11189 OPT_Wattributes,
11190 "attributes at the beginning of statement are ignored");
11191 }
11192
11193 /* Append ATTR to attribute list ATTRS. */
11194
11195 static tree
11196 attr_chainon (tree attrs, tree attr)
11197 {
11198 if (attrs == error_mark_node)
11199 return error_mark_node;
11200 if (attr == error_mark_node)
11201 return error_mark_node;
11202 return chainon (attrs, attr);
11203 }
11204
11205 /* Parse the label for a labeled-statement, i.e.
11206
11207 identifier :
11208 case constant-expression :
11209 default :
11210
11211 GNU Extension:
11212 case constant-expression ... constant-expression : statement
11213
11214 When a label is parsed without errors, the label is added to the
11215 parse tree by the finish_* functions, so this function doesn't
11216 have to return the label. */
11217
11218 static void
11219 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11220 {
11221 cp_token *token;
11222 tree label = NULL_TREE;
11223 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11224
11225 /* The next token should be an identifier. */
11226 token = cp_lexer_peek_token (parser->lexer);
11227 if (token->type != CPP_NAME
11228 && token->type != CPP_KEYWORD)
11229 {
11230 cp_parser_error (parser, "expected labeled-statement");
11231 return;
11232 }
11233
11234 /* Remember whether this case or a user-defined label is allowed to fall
11235 through to. */
11236 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11237
11238 parser->colon_corrects_to_scope_p = false;
11239 switch (token->keyword)
11240 {
11241 case RID_CASE:
11242 {
11243 tree expr, expr_hi;
11244 cp_token *ellipsis;
11245
11246 /* Consume the `case' token. */
11247 cp_lexer_consume_token (parser->lexer);
11248 /* Parse the constant-expression. */
11249 expr = cp_parser_constant_expression (parser);
11250 if (check_for_bare_parameter_packs (expr))
11251 expr = error_mark_node;
11252
11253 ellipsis = cp_lexer_peek_token (parser->lexer);
11254 if (ellipsis->type == CPP_ELLIPSIS)
11255 {
11256 /* Consume the `...' token. */
11257 cp_lexer_consume_token (parser->lexer);
11258 expr_hi = cp_parser_constant_expression (parser);
11259 if (check_for_bare_parameter_packs (expr_hi))
11260 expr_hi = error_mark_node;
11261
11262 /* We don't need to emit warnings here, as the common code
11263 will do this for us. */
11264 }
11265 else
11266 expr_hi = NULL_TREE;
11267
11268 if (parser->in_switch_statement_p)
11269 {
11270 tree l = finish_case_label (token->location, expr, expr_hi);
11271 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11272 {
11273 label = CASE_LABEL (l);
11274 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11275 }
11276 }
11277 else
11278 error_at (token->location,
11279 "case label %qE not within a switch statement",
11280 expr);
11281 }
11282 break;
11283
11284 case RID_DEFAULT:
11285 /* Consume the `default' token. */
11286 cp_lexer_consume_token (parser->lexer);
11287
11288 if (parser->in_switch_statement_p)
11289 {
11290 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11291 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11292 {
11293 label = CASE_LABEL (l);
11294 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11295 }
11296 }
11297 else
11298 error_at (token->location, "case label not within a switch statement");
11299 break;
11300
11301 default:
11302 /* Anything else must be an ordinary label. */
11303 label = finish_label_stmt (cp_parser_identifier (parser));
11304 if (label && TREE_CODE (label) == LABEL_DECL)
11305 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11306 break;
11307 }
11308
11309 /* Require the `:' token. */
11310 cp_parser_require (parser, CPP_COLON, RT_COLON);
11311
11312 /* An ordinary label may optionally be followed by attributes.
11313 However, this is only permitted if the attributes are then
11314 followed by a semicolon. This is because, for backward
11315 compatibility, when parsing
11316 lab: __attribute__ ((unused)) int i;
11317 we want the attribute to attach to "i", not "lab". */
11318 if (label != NULL_TREE
11319 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11320 {
11321 tree attrs;
11322 cp_parser_parse_tentatively (parser);
11323 attrs = cp_parser_gnu_attributes_opt (parser);
11324 if (attrs == NULL_TREE
11325 /* And fallthrough always binds to the expression-statement. */
11326 || attribute_fallthrough_p (attrs)
11327 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11328 cp_parser_abort_tentative_parse (parser);
11329 else if (!cp_parser_parse_definitely (parser))
11330 ;
11331 else
11332 attributes = attr_chainon (attributes, attrs);
11333 }
11334
11335 if (attributes != NULL_TREE)
11336 cplus_decl_attributes (&label, attributes, 0);
11337
11338 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11339 }
11340
11341 /* Parse an expression-statement.
11342
11343 expression-statement:
11344 expression [opt] ;
11345
11346 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11347 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11348 indicates whether this expression-statement is part of an
11349 expression statement. */
11350
11351 static tree
11352 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11353 {
11354 tree statement = NULL_TREE;
11355 cp_token *token = cp_lexer_peek_token (parser->lexer);
11356 location_t loc = token->location;
11357
11358 /* There might be attribute fallthrough. */
11359 tree attr = cp_parser_gnu_attributes_opt (parser);
11360
11361 /* If the next token is a ';', then there is no expression
11362 statement. */
11363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11364 {
11365 statement = cp_parser_expression (parser);
11366 if (statement == error_mark_node
11367 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11368 {
11369 cp_parser_skip_to_end_of_block_or_statement (parser);
11370 return error_mark_node;
11371 }
11372 }
11373
11374 /* Handle [[fallthrough]];. */
11375 if (attribute_fallthrough_p (attr))
11376 {
11377 /* The next token after the fallthrough attribute is ';'. */
11378 if (statement == NULL_TREE)
11379 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11380 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11381 void_type_node, 0);
11382 else
11383 warning_at (loc, OPT_Wattributes,
11384 "%<fallthrough%> attribute not followed by %<;%>");
11385 attr = NULL_TREE;
11386 }
11387
11388 /* Allow "[[fallthrough]];", but warn otherwise. */
11389 if (attr != NULL_TREE)
11390 warning_at (loc, OPT_Wattributes,
11391 "attributes at the beginning of statement are ignored");
11392
11393 /* Give a helpful message for "A<T>::type t;" and the like. */
11394 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11395 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11396 {
11397 if (TREE_CODE (statement) == SCOPE_REF)
11398 error_at (token->location, "need %<typename%> before %qE because "
11399 "%qT is a dependent scope",
11400 statement, TREE_OPERAND (statement, 0));
11401 else if (is_overloaded_fn (statement)
11402 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11403 {
11404 /* A::A a; */
11405 tree fn = get_first_fn (statement);
11406 error_at (token->location,
11407 "%<%T::%D%> names the constructor, not the type",
11408 DECL_CONTEXT (fn), DECL_NAME (fn));
11409 }
11410 }
11411
11412 /* Consume the final `;'. */
11413 cp_parser_consume_semicolon_at_end_of_statement (parser);
11414
11415 if (in_statement_expr
11416 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11417 /* This is the final expression statement of a statement
11418 expression. */
11419 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11420 else if (statement)
11421 statement = finish_expr_stmt (statement);
11422
11423 return statement;
11424 }
11425
11426 /* Parse a compound-statement.
11427
11428 compound-statement:
11429 { statement-seq [opt] }
11430
11431 GNU extension:
11432
11433 compound-statement:
11434 { label-declaration-seq [opt] statement-seq [opt] }
11435
11436 label-declaration-seq:
11437 label-declaration
11438 label-declaration-seq label-declaration
11439
11440 Returns a tree representing the statement. */
11441
11442 static tree
11443 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11444 int bcs_flags, bool function_body)
11445 {
11446 tree compound_stmt;
11447 matching_braces braces;
11448
11449 /* Consume the `{'. */
11450 if (!braces.require_open (parser))
11451 return error_mark_node;
11452 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11453 && !function_body && cxx_dialect < cxx14)
11454 pedwarn (input_location, OPT_Wpedantic,
11455 "compound-statement in %<constexpr%> function");
11456 /* Begin the compound-statement. */
11457 compound_stmt = begin_compound_stmt (bcs_flags);
11458 /* If the next keyword is `__label__' we have a label declaration. */
11459 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11460 cp_parser_label_declaration (parser);
11461 /* Parse an (optional) statement-seq. */
11462 cp_parser_statement_seq_opt (parser, in_statement_expr);
11463 /* Finish the compound-statement. */
11464 finish_compound_stmt (compound_stmt);
11465 /* Consume the `}'. */
11466 braces.require_close (parser);
11467
11468 return compound_stmt;
11469 }
11470
11471 /* Parse an (optional) statement-seq.
11472
11473 statement-seq:
11474 statement
11475 statement-seq [opt] statement */
11476
11477 static void
11478 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11479 {
11480 /* Scan statements until there aren't any more. */
11481 while (true)
11482 {
11483 cp_token *token = cp_lexer_peek_token (parser->lexer);
11484
11485 /* If we are looking at a `}', then we have run out of
11486 statements; the same is true if we have reached the end
11487 of file, or have stumbled upon a stray '@end'. */
11488 if (token->type == CPP_CLOSE_BRACE
11489 || token->type == CPP_EOF
11490 || token->type == CPP_PRAGMA_EOL
11491 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11492 break;
11493
11494 /* If we are in a compound statement and find 'else' then
11495 something went wrong. */
11496 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11497 {
11498 if (parser->in_statement & IN_IF_STMT)
11499 break;
11500 else
11501 {
11502 token = cp_lexer_consume_token (parser->lexer);
11503 error_at (token->location, "%<else%> without a previous %<if%>");
11504 }
11505 }
11506
11507 /* Parse the statement. */
11508 cp_parser_statement (parser, in_statement_expr, true, NULL);
11509 }
11510 }
11511
11512 /* Return true if this is the C++20 version of range-based-for with
11513 init-statement. */
11514
11515 static bool
11516 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11517 {
11518 bool r = false;
11519
11520 /* Save tokens so that we can put them back. */
11521 cp_lexer_save_tokens (parser->lexer);
11522
11523 /* There has to be an unnested ; followed by an unnested :. */
11524 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11525 /*recovering=*/false,
11526 CPP_SEMICOLON,
11527 /*consume_paren=*/false) != -1)
11528 goto out;
11529
11530 /* We found the semicolon, eat it now. */
11531 cp_lexer_consume_token (parser->lexer);
11532
11533 /* Now look for ':' that is not nested in () or {}. */
11534 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11535 /*recovering=*/false,
11536 CPP_COLON,
11537 /*consume_paren=*/false) == -1);
11538
11539 out:
11540 /* Roll back the tokens we skipped. */
11541 cp_lexer_rollback_tokens (parser->lexer);
11542
11543 return r;
11544 }
11545
11546 /* Return true if we're looking at (init; cond), false otherwise. */
11547
11548 static bool
11549 cp_parser_init_statement_p (cp_parser *parser)
11550 {
11551 /* Save tokens so that we can put them back. */
11552 cp_lexer_save_tokens (parser->lexer);
11553
11554 /* Look for ';' that is not nested in () or {}. */
11555 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11556 /*recovering=*/false,
11557 CPP_SEMICOLON,
11558 /*consume_paren=*/false);
11559
11560 /* Roll back the tokens we skipped. */
11561 cp_lexer_rollback_tokens (parser->lexer);
11562
11563 return ret == -1;
11564 }
11565
11566 /* Parse a selection-statement.
11567
11568 selection-statement:
11569 if ( init-statement [opt] condition ) statement
11570 if ( init-statement [opt] condition ) statement else statement
11571 switch ( init-statement [opt] condition ) statement
11572
11573 Returns the new IF_STMT or SWITCH_STMT.
11574
11575 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11576 is a (possibly labeled) if statement which is not enclosed in
11577 braces and has an else clause. This is used to implement
11578 -Wparentheses.
11579
11580 CHAIN is a vector of if-else-if conditions. This is used to implement
11581 -Wduplicated-cond. */
11582
11583 static tree
11584 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11585 vec<tree> *chain)
11586 {
11587 cp_token *token;
11588 enum rid keyword;
11589 token_indent_info guard_tinfo;
11590
11591 if (if_p != NULL)
11592 *if_p = false;
11593
11594 /* Peek at the next token. */
11595 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11596 guard_tinfo = get_token_indent_info (token);
11597
11598 /* See what kind of keyword it is. */
11599 keyword = token->keyword;
11600 switch (keyword)
11601 {
11602 case RID_IF:
11603 case RID_SWITCH:
11604 {
11605 tree statement;
11606 tree condition;
11607
11608 bool cx = false;
11609 if (keyword == RID_IF
11610 && cp_lexer_next_token_is_keyword (parser->lexer,
11611 RID_CONSTEXPR))
11612 {
11613 cx = true;
11614 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11615 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11616 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11617 "with -std=c++17 or -std=gnu++17");
11618 }
11619
11620 /* Look for the `('. */
11621 matching_parens parens;
11622 if (!parens.require_open (parser))
11623 {
11624 cp_parser_skip_to_end_of_statement (parser);
11625 return error_mark_node;
11626 }
11627
11628 /* Begin the selection-statement. */
11629 if (keyword == RID_IF)
11630 {
11631 statement = begin_if_stmt ();
11632 IF_STMT_CONSTEXPR_P (statement) = cx;
11633 }
11634 else
11635 statement = begin_switch_stmt ();
11636
11637 /* Parse the optional init-statement. */
11638 if (cp_parser_init_statement_p (parser))
11639 {
11640 tree decl;
11641 if (cxx_dialect < cxx17)
11642 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11643 "init-statement in selection statements only available "
11644 "with -std=c++17 or -std=gnu++17");
11645 cp_parser_init_statement (parser, &decl);
11646 }
11647
11648 /* Parse the condition. */
11649 condition = cp_parser_condition (parser);
11650 /* Look for the `)'. */
11651 if (!parens.require_close (parser))
11652 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11653 /*consume_paren=*/true);
11654
11655 if (keyword == RID_IF)
11656 {
11657 bool nested_if;
11658 unsigned char in_statement;
11659
11660 /* Add the condition. */
11661 condition = finish_if_stmt_cond (condition, statement);
11662
11663 if (warn_duplicated_cond)
11664 warn_duplicated_cond_add_or_warn (token->location, condition,
11665 &chain);
11666
11667 /* Parse the then-clause. */
11668 in_statement = parser->in_statement;
11669 parser->in_statement |= IN_IF_STMT;
11670
11671 /* Outside a template, the non-selected branch of a constexpr
11672 if is a 'discarded statement', i.e. unevaluated. */
11673 bool was_discarded = in_discarded_stmt;
11674 bool discard_then = (cx && !processing_template_decl
11675 && integer_zerop (condition));
11676 if (discard_then)
11677 {
11678 in_discarded_stmt = true;
11679 ++c_inhibit_evaluation_warnings;
11680 }
11681
11682 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11683 guard_tinfo);
11684
11685 parser->in_statement = in_statement;
11686
11687 finish_then_clause (statement);
11688
11689 if (discard_then)
11690 {
11691 THEN_CLAUSE (statement) = NULL_TREE;
11692 in_discarded_stmt = was_discarded;
11693 --c_inhibit_evaluation_warnings;
11694 }
11695
11696 /* If the next token is `else', parse the else-clause. */
11697 if (cp_lexer_next_token_is_keyword (parser->lexer,
11698 RID_ELSE))
11699 {
11700 bool discard_else = (cx && !processing_template_decl
11701 && integer_nonzerop (condition));
11702 if (discard_else)
11703 {
11704 in_discarded_stmt = true;
11705 ++c_inhibit_evaluation_warnings;
11706 }
11707
11708 guard_tinfo
11709 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11710 /* Consume the `else' keyword. */
11711 cp_lexer_consume_token (parser->lexer);
11712 if (warn_duplicated_cond)
11713 {
11714 if (cp_lexer_next_token_is_keyword (parser->lexer,
11715 RID_IF)
11716 && chain == NULL)
11717 {
11718 /* We've got "if (COND) else if (COND2)". Start
11719 the condition chain and add COND as the first
11720 element. */
11721 chain = new vec<tree> ();
11722 if (!CONSTANT_CLASS_P (condition)
11723 && !TREE_SIDE_EFFECTS (condition))
11724 {
11725 /* Wrap it in a NOP_EXPR so that we can set the
11726 location of the condition. */
11727 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11728 condition);
11729 SET_EXPR_LOCATION (e, token->location);
11730 chain->safe_push (e);
11731 }
11732 }
11733 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11734 RID_IF))
11735 {
11736 /* This is if-else without subsequent if. Zap the
11737 condition chain; we would have already warned at
11738 this point. */
11739 delete chain;
11740 chain = NULL;
11741 }
11742 }
11743 begin_else_clause (statement);
11744 /* Parse the else-clause. */
11745 cp_parser_implicitly_scoped_statement (parser, NULL,
11746 guard_tinfo, chain);
11747
11748 finish_else_clause (statement);
11749
11750 /* If we are currently parsing a then-clause, then
11751 IF_P will not be NULL. We set it to true to
11752 indicate that this if statement has an else clause.
11753 This may trigger the Wparentheses warning below
11754 when we get back up to the parent if statement. */
11755 if (if_p != NULL)
11756 *if_p = true;
11757
11758 if (discard_else)
11759 {
11760 ELSE_CLAUSE (statement) = NULL_TREE;
11761 in_discarded_stmt = was_discarded;
11762 --c_inhibit_evaluation_warnings;
11763 }
11764 }
11765 else
11766 {
11767 /* This if statement does not have an else clause. If
11768 NESTED_IF is true, then the then-clause has an if
11769 statement which does have an else clause. We warn
11770 about the potential ambiguity. */
11771 if (nested_if)
11772 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11773 "suggest explicit braces to avoid ambiguous"
11774 " %<else%>");
11775 if (warn_duplicated_cond)
11776 {
11777 /* We don't need the condition chain anymore. */
11778 delete chain;
11779 chain = NULL;
11780 }
11781 }
11782
11783 /* Now we're all done with the if-statement. */
11784 finish_if_stmt (statement);
11785 }
11786 else
11787 {
11788 bool in_switch_statement_p;
11789 unsigned char in_statement;
11790
11791 /* Add the condition. */
11792 finish_switch_cond (condition, statement);
11793
11794 /* Parse the body of the switch-statement. */
11795 in_switch_statement_p = parser->in_switch_statement_p;
11796 in_statement = parser->in_statement;
11797 parser->in_switch_statement_p = true;
11798 parser->in_statement |= IN_SWITCH_STMT;
11799 cp_parser_implicitly_scoped_statement (parser, if_p,
11800 guard_tinfo);
11801 parser->in_switch_statement_p = in_switch_statement_p;
11802 parser->in_statement = in_statement;
11803
11804 /* Now we're all done with the switch-statement. */
11805 finish_switch_stmt (statement);
11806 }
11807
11808 return statement;
11809 }
11810 break;
11811
11812 default:
11813 cp_parser_error (parser, "expected selection-statement");
11814 return error_mark_node;
11815 }
11816 }
11817
11818 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11819 If we have seen at least one decl-specifier, and the next token
11820 is not a parenthesis, then we must be looking at a declaration.
11821 (After "int (" we might be looking at a functional cast.) */
11822
11823 static void
11824 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11825 bool any_specifiers_p)
11826 {
11827 if (any_specifiers_p
11828 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11829 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11830 && !cp_parser_error_occurred (parser))
11831 cp_parser_commit_to_tentative_parse (parser);
11832 }
11833
11834 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11835 The declarator shall not specify a function or an array. Returns
11836 TRUE if the declarator is valid, FALSE otherwise. */
11837
11838 static bool
11839 cp_parser_check_condition_declarator (cp_parser* parser,
11840 cp_declarator *declarator,
11841 location_t loc)
11842 {
11843 if (declarator == cp_error_declarator
11844 || function_declarator_p (declarator)
11845 || declarator->kind == cdk_array)
11846 {
11847 if (declarator == cp_error_declarator)
11848 /* Already complained. */;
11849 else if (declarator->kind == cdk_array)
11850 error_at (loc, "condition declares an array");
11851 else
11852 error_at (loc, "condition declares a function");
11853 if (parser->fully_implicit_function_template_p)
11854 abort_fully_implicit_template (parser);
11855 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11856 /*or_comma=*/false,
11857 /*consume_paren=*/false);
11858 return false;
11859 }
11860 else
11861 return true;
11862 }
11863
11864 /* Parse a condition.
11865
11866 condition:
11867 expression
11868 type-specifier-seq declarator = initializer-clause
11869 type-specifier-seq declarator braced-init-list
11870
11871 GNU Extension:
11872
11873 condition:
11874 type-specifier-seq declarator asm-specification [opt]
11875 attributes [opt] = assignment-expression
11876
11877 Returns the expression that should be tested. */
11878
11879 static tree
11880 cp_parser_condition (cp_parser* parser)
11881 {
11882 cp_decl_specifier_seq type_specifiers;
11883 const char *saved_message;
11884 int declares_class_or_enum;
11885
11886 /* Try the declaration first. */
11887 cp_parser_parse_tentatively (parser);
11888 /* New types are not allowed in the type-specifier-seq for a
11889 condition. */
11890 saved_message = parser->type_definition_forbidden_message;
11891 parser->type_definition_forbidden_message
11892 = G_("types may not be defined in conditions");
11893 /* Parse the type-specifier-seq. */
11894 cp_parser_decl_specifier_seq (parser,
11895 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11896 &type_specifiers,
11897 &declares_class_or_enum);
11898 /* Restore the saved message. */
11899 parser->type_definition_forbidden_message = saved_message;
11900
11901 cp_parser_maybe_commit_to_declaration (parser,
11902 type_specifiers.any_specifiers_p);
11903
11904 /* If all is well, we might be looking at a declaration. */
11905 if (!cp_parser_error_occurred (parser))
11906 {
11907 tree decl;
11908 tree asm_specification;
11909 tree attributes;
11910 cp_declarator *declarator;
11911 tree initializer = NULL_TREE;
11912 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11913
11914 /* Parse the declarator. */
11915 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11916 /*ctor_dtor_or_conv_p=*/NULL,
11917 /*parenthesized_p=*/NULL,
11918 /*member_p=*/false,
11919 /*friend_p=*/false);
11920 /* Parse the attributes. */
11921 attributes = cp_parser_attributes_opt (parser);
11922 /* Parse the asm-specification. */
11923 asm_specification = cp_parser_asm_specification_opt (parser);
11924 /* If the next token is not an `=' or '{', then we might still be
11925 looking at an expression. For example:
11926
11927 if (A(a).x)
11928
11929 looks like a decl-specifier-seq and a declarator -- but then
11930 there is no `=', so this is an expression. */
11931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11932 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11933 cp_parser_simulate_error (parser);
11934
11935 /* If we did see an `=' or '{', then we are looking at a declaration
11936 for sure. */
11937 if (cp_parser_parse_definitely (parser))
11938 {
11939 tree pushed_scope;
11940 bool non_constant_p = false;
11941 int flags = LOOKUP_ONLYCONVERTING;
11942
11943 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11944 return error_mark_node;
11945
11946 /* Create the declaration. */
11947 decl = start_decl (declarator, &type_specifiers,
11948 /*initialized_p=*/true,
11949 attributes, /*prefix_attributes=*/NULL_TREE,
11950 &pushed_scope);
11951
11952 /* Parse the initializer. */
11953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11954 {
11955 initializer = cp_parser_braced_list (parser, &non_constant_p);
11956 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11957 flags = 0;
11958 }
11959 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11960 {
11961 /* Consume the `='. */
11962 cp_lexer_consume_token (parser->lexer);
11963 initializer = cp_parser_initializer_clause (parser,
11964 &non_constant_p);
11965 }
11966 else
11967 {
11968 cp_parser_error (parser, "expected initializer");
11969 initializer = error_mark_node;
11970 }
11971 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11972 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11973
11974 /* Process the initializer. */
11975 cp_finish_decl (decl,
11976 initializer, !non_constant_p,
11977 asm_specification,
11978 flags);
11979
11980 if (pushed_scope)
11981 pop_scope (pushed_scope);
11982
11983 return convert_from_reference (decl);
11984 }
11985 }
11986 /* If we didn't even get past the declarator successfully, we are
11987 definitely not looking at a declaration. */
11988 else
11989 cp_parser_abort_tentative_parse (parser);
11990
11991 /* Otherwise, we are looking at an expression. */
11992 return cp_parser_expression (parser);
11993 }
11994
11995 /* Parses a for-statement or range-for-statement until the closing ')',
11996 not included. */
11997
11998 static tree
11999 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12000 {
12001 tree init, scope, decl;
12002 bool is_range_for;
12003
12004 /* Begin the for-statement. */
12005 scope = begin_for_scope (&init);
12006
12007 /* Parse the initialization. */
12008 is_range_for = cp_parser_init_statement (parser, &decl);
12009
12010 if (is_range_for)
12011 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12012 false);
12013 else
12014 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12015 }
12016
12017 static tree
12018 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12019 unsigned short unroll)
12020 {
12021 /* Normal for loop */
12022 tree condition = NULL_TREE;
12023 tree expression = NULL_TREE;
12024 tree stmt;
12025
12026 stmt = begin_for_stmt (scope, init);
12027 /* The init-statement has already been parsed in
12028 cp_parser_init_statement, so no work is needed here. */
12029 finish_init_stmt (stmt);
12030
12031 /* If there's a condition, process it. */
12032 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12033 condition = cp_parser_condition (parser);
12034 else if (ivdep)
12035 {
12036 cp_parser_error (parser, "missing loop condition in loop with "
12037 "%<GCC ivdep%> pragma");
12038 condition = error_mark_node;
12039 }
12040 else if (unroll)
12041 {
12042 cp_parser_error (parser, "missing loop condition in loop with "
12043 "%<GCC unroll%> pragma");
12044 condition = error_mark_node;
12045 }
12046 finish_for_cond (condition, stmt, ivdep, unroll);
12047 /* Look for the `;'. */
12048 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12049
12050 /* If there's an expression, process it. */
12051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12052 expression = cp_parser_expression (parser);
12053 finish_for_expr (expression, stmt);
12054
12055 return stmt;
12056 }
12057
12058 /* Tries to parse a range-based for-statement:
12059
12060 range-based-for:
12061 decl-specifier-seq declarator : expression
12062
12063 The decl-specifier-seq declarator and the `:' are already parsed by
12064 cp_parser_init_statement. If processing_template_decl it returns a
12065 newly created RANGE_FOR_STMT; if not, it is converted to a
12066 regular FOR_STMT. */
12067
12068 static tree
12069 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12070 bool ivdep, unsigned short unroll, bool is_omp)
12071 {
12072 tree stmt, range_expr;
12073 auto_vec <cxx_binding *, 16> bindings;
12074 auto_vec <tree, 16> names;
12075 tree decomp_first_name = NULL_TREE;
12076 unsigned int decomp_cnt = 0;
12077
12078 /* Get the range declaration momentarily out of the way so that
12079 the range expression doesn't clash with it. */
12080 if (range_decl != error_mark_node)
12081 {
12082 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12083 {
12084 tree v = DECL_VALUE_EXPR (range_decl);
12085 /* For decomposition declaration get all of the corresponding
12086 declarations out of the way. */
12087 if (TREE_CODE (v) == ARRAY_REF
12088 && VAR_P (TREE_OPERAND (v, 0))
12089 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12090 {
12091 tree d = range_decl;
12092 range_decl = TREE_OPERAND (v, 0);
12093 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12094 decomp_first_name = d;
12095 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12096 {
12097 tree name = DECL_NAME (d);
12098 names.safe_push (name);
12099 bindings.safe_push (IDENTIFIER_BINDING (name));
12100 IDENTIFIER_BINDING (name)
12101 = IDENTIFIER_BINDING (name)->previous;
12102 }
12103 }
12104 }
12105 if (names.is_empty ())
12106 {
12107 tree name = DECL_NAME (range_decl);
12108 names.safe_push (name);
12109 bindings.safe_push (IDENTIFIER_BINDING (name));
12110 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12111 }
12112 }
12113
12114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12115 {
12116 bool expr_non_constant_p;
12117 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12118 }
12119 else
12120 range_expr = cp_parser_expression (parser);
12121
12122 /* Put the range declaration(s) back into scope. */
12123 for (unsigned int i = 0; i < names.length (); i++)
12124 {
12125 cxx_binding *binding = bindings[i];
12126 binding->previous = IDENTIFIER_BINDING (names[i]);
12127 IDENTIFIER_BINDING (names[i]) = binding;
12128 }
12129
12130 /* finish_omp_for has its own code for the following, so just
12131 return the range_expr instead. */
12132 if (is_omp)
12133 return range_expr;
12134
12135 /* If in template, STMT is converted to a normal for-statement
12136 at instantiation. If not, it is done just ahead. */
12137 if (processing_template_decl)
12138 {
12139 if (check_for_bare_parameter_packs (range_expr))
12140 range_expr = error_mark_node;
12141 stmt = begin_range_for_stmt (scope, init);
12142 if (ivdep)
12143 RANGE_FOR_IVDEP (stmt) = 1;
12144 if (unroll)
12145 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12146 finish_range_for_decl (stmt, range_decl, range_expr);
12147 if (!type_dependent_expression_p (range_expr)
12148 /* do_auto_deduction doesn't mess with template init-lists. */
12149 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12150 do_range_for_auto_deduction (range_decl, range_expr);
12151 }
12152 else
12153 {
12154 stmt = begin_for_stmt (scope, init);
12155 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12156 decomp_first_name, decomp_cnt, ivdep,
12157 unroll);
12158 }
12159 return stmt;
12160 }
12161
12162 /* Subroutine of cp_convert_range_for: given the initializer expression,
12163 builds up the range temporary. */
12164
12165 static tree
12166 build_range_temp (tree range_expr)
12167 {
12168 tree range_type, range_temp;
12169
12170 /* Find out the type deduced by the declaration
12171 `auto &&__range = range_expr'. */
12172 range_type = cp_build_reference_type (make_auto (), true);
12173 range_type = do_auto_deduction (range_type, range_expr,
12174 type_uses_auto (range_type));
12175
12176 /* Create the __range variable. */
12177 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12178 range_type);
12179 TREE_USED (range_temp) = 1;
12180 DECL_ARTIFICIAL (range_temp) = 1;
12181
12182 return range_temp;
12183 }
12184
12185 /* Used by cp_parser_range_for in template context: we aren't going to
12186 do a full conversion yet, but we still need to resolve auto in the
12187 type of the for-range-declaration if present. This is basically
12188 a shortcut version of cp_convert_range_for. */
12189
12190 static void
12191 do_range_for_auto_deduction (tree decl, tree range_expr)
12192 {
12193 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12194 if (auto_node)
12195 {
12196 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12197 range_temp = convert_from_reference (build_range_temp (range_expr));
12198 iter_type = (cp_parser_perform_range_for_lookup
12199 (range_temp, &begin_dummy, &end_dummy));
12200 if (iter_type)
12201 {
12202 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12203 iter_type);
12204 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12205 RO_UNARY_STAR,
12206 tf_warning_or_error);
12207 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12208 iter_decl, auto_node);
12209 }
12210 }
12211 }
12212
12213 /* Converts a range-based for-statement into a normal
12214 for-statement, as per the definition.
12215
12216 for (RANGE_DECL : RANGE_EXPR)
12217 BLOCK
12218
12219 should be equivalent to:
12220
12221 {
12222 auto &&__range = RANGE_EXPR;
12223 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12224 __begin != __end;
12225 ++__begin)
12226 {
12227 RANGE_DECL = *__begin;
12228 BLOCK
12229 }
12230 }
12231
12232 If RANGE_EXPR is an array:
12233 BEGIN_EXPR = __range
12234 END_EXPR = __range + ARRAY_SIZE(__range)
12235 Else if RANGE_EXPR has a member 'begin' or 'end':
12236 BEGIN_EXPR = __range.begin()
12237 END_EXPR = __range.end()
12238 Else:
12239 BEGIN_EXPR = begin(__range)
12240 END_EXPR = end(__range);
12241
12242 If __range has a member 'begin' but not 'end', or vice versa, we must
12243 still use the second alternative (it will surely fail, however).
12244 When calling begin()/end() in the third alternative we must use
12245 argument dependent lookup, but always considering 'std' as an associated
12246 namespace. */
12247
12248 tree
12249 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12250 tree decomp_first_name, unsigned int decomp_cnt,
12251 bool ivdep, unsigned short unroll)
12252 {
12253 tree begin, end;
12254 tree iter_type, begin_expr, end_expr;
12255 tree condition, expression;
12256
12257 range_expr = mark_lvalue_use (range_expr);
12258
12259 if (range_decl == error_mark_node || range_expr == error_mark_node)
12260 /* If an error happened previously do nothing or else a lot of
12261 unhelpful errors would be issued. */
12262 begin_expr = end_expr = iter_type = error_mark_node;
12263 else
12264 {
12265 tree range_temp;
12266
12267 if (VAR_P (range_expr)
12268 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12269 /* Can't bind a reference to an array of runtime bound. */
12270 range_temp = range_expr;
12271 else
12272 {
12273 range_temp = build_range_temp (range_expr);
12274 pushdecl (range_temp);
12275 cp_finish_decl (range_temp, range_expr,
12276 /*is_constant_init*/false, NULL_TREE,
12277 LOOKUP_ONLYCONVERTING);
12278 range_temp = convert_from_reference (range_temp);
12279 }
12280 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12281 &begin_expr, &end_expr);
12282 }
12283
12284 /* The new for initialization statement. */
12285 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12286 iter_type);
12287 TREE_USED (begin) = 1;
12288 DECL_ARTIFICIAL (begin) = 1;
12289 pushdecl (begin);
12290 cp_finish_decl (begin, begin_expr,
12291 /*is_constant_init*/false, NULL_TREE,
12292 LOOKUP_ONLYCONVERTING);
12293
12294 if (cxx_dialect >= cxx17)
12295 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12296 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12297 TREE_USED (end) = 1;
12298 DECL_ARTIFICIAL (end) = 1;
12299 pushdecl (end);
12300 cp_finish_decl (end, end_expr,
12301 /*is_constant_init*/false, NULL_TREE,
12302 LOOKUP_ONLYCONVERTING);
12303
12304 finish_init_stmt (statement);
12305
12306 /* The new for condition. */
12307 condition = build_x_binary_op (input_location, NE_EXPR,
12308 begin, ERROR_MARK,
12309 end, ERROR_MARK,
12310 NULL, tf_warning_or_error);
12311 finish_for_cond (condition, statement, ivdep, unroll);
12312
12313 /* The new increment expression. */
12314 expression = finish_unary_op_expr (input_location,
12315 PREINCREMENT_EXPR, begin,
12316 tf_warning_or_error);
12317 finish_for_expr (expression, statement);
12318
12319 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12320 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12321
12322 /* The declaration is initialized with *__begin inside the loop body. */
12323 cp_finish_decl (range_decl,
12324 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12325 tf_warning_or_error),
12326 /*is_constant_init*/false, NULL_TREE,
12327 LOOKUP_ONLYCONVERTING);
12328 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12329 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12330
12331 return statement;
12332 }
12333
12334 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12335 We need to solve both at the same time because the method used
12336 depends on the existence of members begin or end.
12337 Returns the type deduced for the iterator expression. */
12338
12339 static tree
12340 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12341 {
12342 if (error_operand_p (range))
12343 {
12344 *begin = *end = error_mark_node;
12345 return error_mark_node;
12346 }
12347
12348 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12349 {
12350 error ("range-based %<for%> expression of type %qT "
12351 "has incomplete type", TREE_TYPE (range));
12352 *begin = *end = error_mark_node;
12353 return error_mark_node;
12354 }
12355 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12356 {
12357 /* If RANGE is an array, we will use pointer arithmetic. */
12358 *begin = decay_conversion (range, tf_warning_or_error);
12359 *end = build_binary_op (input_location, PLUS_EXPR,
12360 range,
12361 array_type_nelts_top (TREE_TYPE (range)),
12362 false);
12363 return TREE_TYPE (*begin);
12364 }
12365 else
12366 {
12367 /* If it is not an array, we must do a bit of magic. */
12368 tree id_begin, id_end;
12369 tree member_begin, member_end;
12370
12371 *begin = *end = error_mark_node;
12372
12373 id_begin = get_identifier ("begin");
12374 id_end = get_identifier ("end");
12375 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12376 /*protect=*/2, /*want_type=*/false,
12377 tf_warning_or_error);
12378 member_end = lookup_member (TREE_TYPE (range), id_end,
12379 /*protect=*/2, /*want_type=*/false,
12380 tf_warning_or_error);
12381
12382 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12383 {
12384 /* Use the member functions. */
12385 *begin = cp_parser_range_for_member_function (range, id_begin);
12386 *end = cp_parser_range_for_member_function (range, id_end);
12387 }
12388 else
12389 {
12390 /* Use global functions with ADL. */
12391 vec<tree, va_gc> *vec;
12392 vec = make_tree_vector ();
12393
12394 vec_safe_push (vec, range);
12395
12396 member_begin = perform_koenig_lookup (id_begin, vec,
12397 tf_warning_or_error);
12398 *begin = finish_call_expr (member_begin, &vec, false, true,
12399 tf_warning_or_error);
12400 member_end = perform_koenig_lookup (id_end, vec,
12401 tf_warning_or_error);
12402 *end = finish_call_expr (member_end, &vec, false, true,
12403 tf_warning_or_error);
12404
12405 release_tree_vector (vec);
12406 }
12407
12408 /* Last common checks. */
12409 if (*begin == error_mark_node || *end == error_mark_node)
12410 {
12411 /* If one of the expressions is an error do no more checks. */
12412 *begin = *end = error_mark_node;
12413 return error_mark_node;
12414 }
12415 else if (type_dependent_expression_p (*begin)
12416 || type_dependent_expression_p (*end))
12417 /* Can happen, when, eg, in a template context, Koenig lookup
12418 can't resolve begin/end (c++/58503). */
12419 return NULL_TREE;
12420 else
12421 {
12422 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12423 /* The unqualified type of the __begin and __end temporaries should
12424 be the same, as required by the multiple auto declaration. */
12425 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12426 {
12427 if (cxx_dialect >= cxx17
12428 && (build_x_binary_op (input_location, NE_EXPR,
12429 *begin, ERROR_MARK,
12430 *end, ERROR_MARK,
12431 NULL, tf_none)
12432 != error_mark_node))
12433 /* P0184R0 allows __begin and __end to have different types,
12434 but make sure they are comparable so we can give a better
12435 diagnostic. */;
12436 else
12437 error ("inconsistent begin/end types in range-based %<for%> "
12438 "statement: %qT and %qT",
12439 TREE_TYPE (*begin), TREE_TYPE (*end));
12440 }
12441 return iter_type;
12442 }
12443 }
12444 }
12445
12446 /* Helper function for cp_parser_perform_range_for_lookup.
12447 Builds a tree for RANGE.IDENTIFIER(). */
12448
12449 static tree
12450 cp_parser_range_for_member_function (tree range, tree identifier)
12451 {
12452 tree member, res;
12453 vec<tree, va_gc> *vec;
12454
12455 member = finish_class_member_access_expr (range, identifier,
12456 false, tf_warning_or_error);
12457 if (member == error_mark_node)
12458 return error_mark_node;
12459
12460 vec = make_tree_vector ();
12461 res = finish_call_expr (member, &vec,
12462 /*disallow_virtual=*/false,
12463 /*koenig_p=*/false,
12464 tf_warning_or_error);
12465 release_tree_vector (vec);
12466 return res;
12467 }
12468
12469 /* Parse an iteration-statement.
12470
12471 iteration-statement:
12472 while ( condition ) statement
12473 do statement while ( expression ) ;
12474 for ( init-statement condition [opt] ; expression [opt] )
12475 statement
12476
12477 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12478
12479 static tree
12480 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12481 unsigned short unroll)
12482 {
12483 cp_token *token;
12484 enum rid keyword;
12485 tree statement;
12486 unsigned char in_statement;
12487 token_indent_info guard_tinfo;
12488
12489 /* Peek at the next token. */
12490 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12491 if (!token)
12492 return error_mark_node;
12493
12494 guard_tinfo = get_token_indent_info (token);
12495
12496 /* Remember whether or not we are already within an iteration
12497 statement. */
12498 in_statement = parser->in_statement;
12499
12500 /* See what kind of keyword it is. */
12501 keyword = token->keyword;
12502 switch (keyword)
12503 {
12504 case RID_WHILE:
12505 {
12506 tree condition;
12507
12508 /* Begin the while-statement. */
12509 statement = begin_while_stmt ();
12510 /* Look for the `('. */
12511 matching_parens parens;
12512 parens.require_open (parser);
12513 /* Parse the condition. */
12514 condition = cp_parser_condition (parser);
12515 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12516 /* Look for the `)'. */
12517 parens.require_close (parser);
12518 /* Parse the dependent statement. */
12519 parser->in_statement = IN_ITERATION_STMT;
12520 bool prev = note_iteration_stmt_body_start ();
12521 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12522 note_iteration_stmt_body_end (prev);
12523 parser->in_statement = in_statement;
12524 /* We're done with the while-statement. */
12525 finish_while_stmt (statement);
12526 }
12527 break;
12528
12529 case RID_DO:
12530 {
12531 tree expression;
12532
12533 /* Begin the do-statement. */
12534 statement = begin_do_stmt ();
12535 /* Parse the body of the do-statement. */
12536 parser->in_statement = IN_ITERATION_STMT;
12537 bool prev = note_iteration_stmt_body_start ();
12538 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12539 note_iteration_stmt_body_end (prev);
12540 parser->in_statement = in_statement;
12541 finish_do_body (statement);
12542 /* Look for the `while' keyword. */
12543 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12544 /* Look for the `('. */
12545 matching_parens parens;
12546 parens.require_open (parser);
12547 /* Parse the expression. */
12548 expression = cp_parser_expression (parser);
12549 /* We're done with the do-statement. */
12550 finish_do_stmt (expression, statement, ivdep, unroll);
12551 /* Look for the `)'. */
12552 parens.require_close (parser);
12553 /* Look for the `;'. */
12554 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12555 }
12556 break;
12557
12558 case RID_FOR:
12559 {
12560 /* Look for the `('. */
12561 matching_parens parens;
12562 parens.require_open (parser);
12563
12564 statement = cp_parser_for (parser, ivdep, unroll);
12565
12566 /* Look for the `)'. */
12567 parens.require_close (parser);
12568
12569 /* Parse the body of the for-statement. */
12570 parser->in_statement = IN_ITERATION_STMT;
12571 bool prev = note_iteration_stmt_body_start ();
12572 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12573 note_iteration_stmt_body_end (prev);
12574 parser->in_statement = in_statement;
12575
12576 /* We're done with the for-statement. */
12577 finish_for_stmt (statement);
12578 }
12579 break;
12580
12581 default:
12582 cp_parser_error (parser, "expected iteration-statement");
12583 statement = error_mark_node;
12584 break;
12585 }
12586
12587 return statement;
12588 }
12589
12590 /* Parse a init-statement or the declarator of a range-based-for.
12591 Returns true if a range-based-for declaration is seen.
12592
12593 init-statement:
12594 expression-statement
12595 simple-declaration */
12596
12597 static bool
12598 cp_parser_init_statement (cp_parser *parser, tree *decl)
12599 {
12600 /* If the next token is a `;', then we have an empty
12601 expression-statement. Grammatically, this is also a
12602 simple-declaration, but an invalid one, because it does not
12603 declare anything. Therefore, if we did not handle this case
12604 specially, we would issue an error message about an invalid
12605 declaration. */
12606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12607 {
12608 bool is_range_for = false;
12609 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12610
12611 /* Try to parse the init-statement. */
12612 if (cp_parser_range_based_for_with_init_p (parser))
12613 {
12614 tree dummy;
12615 cp_parser_parse_tentatively (parser);
12616 /* Parse the declaration. */
12617 cp_parser_simple_declaration (parser,
12618 /*function_definition_allowed_p=*/false,
12619 &dummy);
12620 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12621 if (!cp_parser_parse_definitely (parser))
12622 /* That didn't work, try to parse it as an expression-statement. */
12623 cp_parser_expression_statement (parser, NULL_TREE);
12624
12625 if (cxx_dialect < cxx2a)
12626 {
12627 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12628 "range-based %<for%> loops with initializer only "
12629 "available with -std=c++2a or -std=gnu++2a");
12630 *decl = error_mark_node;
12631 }
12632 }
12633
12634 /* A colon is used in range-based for. */
12635 parser->colon_corrects_to_scope_p = false;
12636
12637 /* We're going to speculatively look for a declaration, falling back
12638 to an expression, if necessary. */
12639 cp_parser_parse_tentatively (parser);
12640 /* Parse the declaration. */
12641 cp_parser_simple_declaration (parser,
12642 /*function_definition_allowed_p=*/false,
12643 decl);
12644 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12645 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12646 {
12647 /* It is a range-for, consume the ':'. */
12648 cp_lexer_consume_token (parser->lexer);
12649 is_range_for = true;
12650 if (cxx_dialect < cxx11)
12651 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12652 "range-based %<for%> loops only available with "
12653 "-std=c++11 or -std=gnu++11");
12654 }
12655 else
12656 /* The ';' is not consumed yet because we told
12657 cp_parser_simple_declaration not to. */
12658 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12659
12660 if (cp_parser_parse_definitely (parser))
12661 return is_range_for;
12662 /* If the tentative parse failed, then we shall need to look for an
12663 expression-statement. */
12664 }
12665 /* If we are here, it is an expression-statement. */
12666 cp_parser_expression_statement (parser, NULL_TREE);
12667 return false;
12668 }
12669
12670 /* Parse a jump-statement.
12671
12672 jump-statement:
12673 break ;
12674 continue ;
12675 return expression [opt] ;
12676 return braced-init-list ;
12677 goto identifier ;
12678
12679 GNU extension:
12680
12681 jump-statement:
12682 goto * expression ;
12683
12684 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12685
12686 static tree
12687 cp_parser_jump_statement (cp_parser* parser)
12688 {
12689 tree statement = error_mark_node;
12690 cp_token *token;
12691 enum rid keyword;
12692 unsigned char in_statement;
12693
12694 /* Peek at the next token. */
12695 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12696 if (!token)
12697 return error_mark_node;
12698
12699 /* See what kind of keyword it is. */
12700 keyword = token->keyword;
12701 switch (keyword)
12702 {
12703 case RID_BREAK:
12704 in_statement = parser->in_statement & ~IN_IF_STMT;
12705 switch (in_statement)
12706 {
12707 case 0:
12708 error_at (token->location, "break statement not within loop or switch");
12709 break;
12710 default:
12711 gcc_assert ((in_statement & IN_SWITCH_STMT)
12712 || in_statement == IN_ITERATION_STMT);
12713 statement = finish_break_stmt ();
12714 if (in_statement == IN_ITERATION_STMT)
12715 break_maybe_infinite_loop ();
12716 break;
12717 case IN_OMP_BLOCK:
12718 error_at (token->location, "invalid exit from OpenMP structured block");
12719 break;
12720 case IN_OMP_FOR:
12721 error_at (token->location, "break statement used with OpenMP for loop");
12722 break;
12723 }
12724 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12725 break;
12726
12727 case RID_CONTINUE:
12728 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12729 {
12730 case 0:
12731 error_at (token->location, "continue statement not within a loop");
12732 break;
12733 /* Fall through. */
12734 case IN_ITERATION_STMT:
12735 case IN_OMP_FOR:
12736 statement = finish_continue_stmt ();
12737 break;
12738 case IN_OMP_BLOCK:
12739 error_at (token->location, "invalid exit from OpenMP structured block");
12740 break;
12741 default:
12742 gcc_unreachable ();
12743 }
12744 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12745 break;
12746
12747 case RID_RETURN:
12748 {
12749 tree expr;
12750 bool expr_non_constant_p;
12751
12752 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12753 {
12754 cp_lexer_set_source_position (parser->lexer);
12755 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12756 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12757 }
12758 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12759 expr = cp_parser_expression (parser);
12760 else
12761 /* If the next token is a `;', then there is no
12762 expression. */
12763 expr = NULL_TREE;
12764 /* Build the return-statement. */
12765 if (current_function_auto_return_pattern && in_discarded_stmt)
12766 /* Don't deduce from a discarded return statement. */;
12767 else
12768 statement = finish_return_stmt (expr);
12769 /* Look for the final `;'. */
12770 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12771 }
12772 break;
12773
12774 case RID_GOTO:
12775 if (parser->in_function_body
12776 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12777 {
12778 error ("%<goto%> in %<constexpr%> function");
12779 cp_function_chain->invalid_constexpr = true;
12780 }
12781
12782 /* Create the goto-statement. */
12783 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12784 {
12785 /* Issue a warning about this use of a GNU extension. */
12786 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12787 /* Consume the '*' token. */
12788 cp_lexer_consume_token (parser->lexer);
12789 /* Parse the dependent expression. */
12790 finish_goto_stmt (cp_parser_expression (parser));
12791 }
12792 else
12793 finish_goto_stmt (cp_parser_identifier (parser));
12794 /* Look for the final `;'. */
12795 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12796 break;
12797
12798 default:
12799 cp_parser_error (parser, "expected jump-statement");
12800 break;
12801 }
12802
12803 return statement;
12804 }
12805
12806 /* Parse a declaration-statement.
12807
12808 declaration-statement:
12809 block-declaration */
12810
12811 static void
12812 cp_parser_declaration_statement (cp_parser* parser)
12813 {
12814 void *p;
12815
12816 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12817 p = obstack_alloc (&declarator_obstack, 0);
12818
12819 /* Parse the block-declaration. */
12820 cp_parser_block_declaration (parser, /*statement_p=*/true);
12821
12822 /* Free any declarators allocated. */
12823 obstack_free (&declarator_obstack, p);
12824 }
12825
12826 /* Some dependent statements (like `if (cond) statement'), are
12827 implicitly in their own scope. In other words, if the statement is
12828 a single statement (as opposed to a compound-statement), it is
12829 none-the-less treated as if it were enclosed in braces. Any
12830 declarations appearing in the dependent statement are out of scope
12831 after control passes that point. This function parses a statement,
12832 but ensures that is in its own scope, even if it is not a
12833 compound-statement.
12834
12835 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12836 is a (possibly labeled) if statement which is not enclosed in
12837 braces and has an else clause. This is used to implement
12838 -Wparentheses.
12839
12840 CHAIN is a vector of if-else-if conditions. This is used to implement
12841 -Wduplicated-cond.
12842
12843 Returns the new statement. */
12844
12845 static tree
12846 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12847 const token_indent_info &guard_tinfo,
12848 vec<tree> *chain)
12849 {
12850 tree statement;
12851 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12852 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12853 token_indent_info body_tinfo
12854 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12855
12856 if (if_p != NULL)
12857 *if_p = false;
12858
12859 /* Mark if () ; with a special NOP_EXPR. */
12860 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12861 {
12862 cp_lexer_consume_token (parser->lexer);
12863 statement = add_stmt (build_empty_stmt (body_loc));
12864
12865 if (guard_tinfo.keyword == RID_IF
12866 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12867 warning_at (body_loc, OPT_Wempty_body,
12868 "suggest braces around empty body in an %<if%> statement");
12869 else if (guard_tinfo.keyword == RID_ELSE)
12870 warning_at (body_loc, OPT_Wempty_body,
12871 "suggest braces around empty body in an %<else%> statement");
12872 }
12873 /* if a compound is opened, we simply parse the statement directly. */
12874 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12875 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12876 /* If the token is not a `{', then we must take special action. */
12877 else
12878 {
12879 /* Create a compound-statement. */
12880 statement = begin_compound_stmt (0);
12881 /* Parse the dependent-statement. */
12882 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12883 &body_loc_after_labels);
12884 /* Finish the dummy compound-statement. */
12885 finish_compound_stmt (statement);
12886 }
12887
12888 token_indent_info next_tinfo
12889 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12890 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12891
12892 if (body_loc_after_labels != UNKNOWN_LOCATION
12893 && next_tinfo.type != CPP_SEMICOLON)
12894 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12895 guard_tinfo.location, guard_tinfo.keyword);
12896
12897 /* Return the statement. */
12898 return statement;
12899 }
12900
12901 /* For some dependent statements (like `while (cond) statement'), we
12902 have already created a scope. Therefore, even if the dependent
12903 statement is a compound-statement, we do not want to create another
12904 scope. */
12905
12906 static void
12907 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12908 const token_indent_info &guard_tinfo)
12909 {
12910 /* If the token is a `{', then we must take special action. */
12911 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12912 {
12913 token_indent_info body_tinfo
12914 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12915 location_t loc_after_labels = UNKNOWN_LOCATION;
12916
12917 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12918 &loc_after_labels);
12919 token_indent_info next_tinfo
12920 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12921 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12922
12923 if (loc_after_labels != UNKNOWN_LOCATION
12924 && next_tinfo.type != CPP_SEMICOLON)
12925 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12926 guard_tinfo.location,
12927 guard_tinfo.keyword);
12928 }
12929 else
12930 {
12931 /* Avoid calling cp_parser_compound_statement, so that we
12932 don't create a new scope. Do everything else by hand. */
12933 matching_braces braces;
12934 braces.require_open (parser);
12935 /* If the next keyword is `__label__' we have a label declaration. */
12936 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12937 cp_parser_label_declaration (parser);
12938 /* Parse an (optional) statement-seq. */
12939 cp_parser_statement_seq_opt (parser, NULL_TREE);
12940 braces.require_close (parser);
12941 }
12942 }
12943
12944 /* Declarations [gram.dcl.dcl] */
12945
12946 /* Parse an optional declaration-sequence.
12947
12948 declaration-seq:
12949 declaration
12950 declaration-seq declaration */
12951
12952 static void
12953 cp_parser_declaration_seq_opt (cp_parser* parser)
12954 {
12955 while (true)
12956 {
12957 cp_token *token = cp_lexer_peek_token (parser->lexer);
12958
12959 if (token->type == CPP_CLOSE_BRACE
12960 || token->type == CPP_EOF)
12961 break;
12962 else
12963 cp_parser_toplevel_declaration (parser);
12964 }
12965 }
12966
12967 /* Parse a declaration.
12968
12969 declaration:
12970 block-declaration
12971 function-definition
12972 template-declaration
12973 explicit-instantiation
12974 explicit-specialization
12975 linkage-specification
12976 namespace-definition
12977
12978 C++17:
12979 deduction-guide
12980
12981 GNU extension:
12982
12983 declaration:
12984 __extension__ declaration */
12985
12986 static void
12987 cp_parser_declaration (cp_parser* parser)
12988 {
12989 cp_token token1;
12990 cp_token token2;
12991 int saved_pedantic;
12992 void *p;
12993 tree attributes = NULL_TREE;
12994
12995 /* Check for the `__extension__' keyword. */
12996 if (cp_parser_extension_opt (parser, &saved_pedantic))
12997 {
12998 /* Parse the qualified declaration. */
12999 cp_parser_declaration (parser);
13000 /* Restore the PEDANTIC flag. */
13001 pedantic = saved_pedantic;
13002
13003 return;
13004 }
13005
13006 /* Try to figure out what kind of declaration is present. */
13007 token1 = *cp_lexer_peek_token (parser->lexer);
13008
13009 if (token1.type != CPP_EOF)
13010 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13011 else
13012 {
13013 token2.type = CPP_EOF;
13014 token2.keyword = RID_MAX;
13015 }
13016
13017 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13018 p = obstack_alloc (&declarator_obstack, 0);
13019
13020 /* If the next token is `extern' and the following token is a string
13021 literal, then we have a linkage specification. */
13022 if (token1.keyword == RID_EXTERN
13023 && cp_parser_is_pure_string_literal (&token2))
13024 cp_parser_linkage_specification (parser);
13025 /* If the next token is `template', then we have either a template
13026 declaration, an explicit instantiation, or an explicit
13027 specialization. */
13028 else if (token1.keyword == RID_TEMPLATE)
13029 {
13030 /* `template <>' indicates a template specialization. */
13031 if (token2.type == CPP_LESS
13032 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13033 cp_parser_explicit_specialization (parser);
13034 /* `template <' indicates a template declaration. */
13035 else if (token2.type == CPP_LESS)
13036 cp_parser_template_declaration (parser, /*member_p=*/false);
13037 /* Anything else must be an explicit instantiation. */
13038 else
13039 cp_parser_explicit_instantiation (parser);
13040 }
13041 /* If the next token is `export', then we have a template
13042 declaration. */
13043 else if (token1.keyword == RID_EXPORT)
13044 cp_parser_template_declaration (parser, /*member_p=*/false);
13045 /* If the next token is `extern', 'static' or 'inline' and the one
13046 after that is `template', we have a GNU extended explicit
13047 instantiation directive. */
13048 else if (cp_parser_allow_gnu_extensions_p (parser)
13049 && (token1.keyword == RID_EXTERN
13050 || token1.keyword == RID_STATIC
13051 || token1.keyword == RID_INLINE)
13052 && token2.keyword == RID_TEMPLATE)
13053 cp_parser_explicit_instantiation (parser);
13054 /* If the next token is `namespace', check for a named or unnamed
13055 namespace definition. */
13056 else if (token1.keyword == RID_NAMESPACE
13057 && (/* A named namespace definition. */
13058 (token2.type == CPP_NAME
13059 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13060 != CPP_EQ))
13061 || (token2.type == CPP_OPEN_SQUARE
13062 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13063 == CPP_OPEN_SQUARE)
13064 /* An unnamed namespace definition. */
13065 || token2.type == CPP_OPEN_BRACE
13066 || token2.keyword == RID_ATTRIBUTE))
13067 cp_parser_namespace_definition (parser);
13068 /* An inline (associated) namespace definition. */
13069 else if (token1.keyword == RID_INLINE
13070 && token2.keyword == RID_NAMESPACE)
13071 cp_parser_namespace_definition (parser);
13072 /* Objective-C++ declaration/definition. */
13073 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13074 cp_parser_objc_declaration (parser, NULL_TREE);
13075 else if (c_dialect_objc ()
13076 && token1.keyword == RID_ATTRIBUTE
13077 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13078 cp_parser_objc_declaration (parser, attributes);
13079 /* At this point we may have a template declared by a concept
13080 introduction. */
13081 else if (flag_concepts
13082 && cp_parser_template_declaration_after_export (parser,
13083 /*member_p=*/false))
13084 /* We did. */;
13085 else
13086 /* Try to parse a block-declaration, or a function-definition. */
13087 cp_parser_block_declaration (parser, /*statement_p=*/false);
13088
13089 /* Free any declarators allocated. */
13090 obstack_free (&declarator_obstack, p);
13091 }
13092
13093 /* Parse a namespace-scope declaration. */
13094
13095 static void
13096 cp_parser_toplevel_declaration (cp_parser* parser)
13097 {
13098 cp_token *token = cp_lexer_peek_token (parser->lexer);
13099
13100 if (token->type == CPP_PRAGMA)
13101 /* A top-level declaration can consist solely of a #pragma. A
13102 nested declaration cannot, so this is done here and not in
13103 cp_parser_declaration. (A #pragma at block scope is
13104 handled in cp_parser_statement.) */
13105 cp_parser_pragma (parser, pragma_external, NULL);
13106 else if (token->type == CPP_SEMICOLON)
13107 {
13108 /* A declaration consisting of a single semicolon is
13109 invalid. Allow it unless we're being pedantic. */
13110 cp_lexer_consume_token (parser->lexer);
13111 if (!in_system_header_at (input_location))
13112 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13113 }
13114 else
13115 /* Parse the declaration itself. */
13116 cp_parser_declaration (parser);
13117 }
13118
13119 /* Parse a block-declaration.
13120
13121 block-declaration:
13122 simple-declaration
13123 asm-definition
13124 namespace-alias-definition
13125 using-declaration
13126 using-directive
13127
13128 GNU Extension:
13129
13130 block-declaration:
13131 __extension__ block-declaration
13132
13133 C++0x Extension:
13134
13135 block-declaration:
13136 static_assert-declaration
13137
13138 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13139 part of a declaration-statement. */
13140
13141 static void
13142 cp_parser_block_declaration (cp_parser *parser,
13143 bool statement_p)
13144 {
13145 cp_token *token1;
13146 int saved_pedantic;
13147
13148 /* Check for the `__extension__' keyword. */
13149 if (cp_parser_extension_opt (parser, &saved_pedantic))
13150 {
13151 /* Parse the qualified declaration. */
13152 cp_parser_block_declaration (parser, statement_p);
13153 /* Restore the PEDANTIC flag. */
13154 pedantic = saved_pedantic;
13155
13156 return;
13157 }
13158
13159 /* Peek at the next token to figure out which kind of declaration is
13160 present. */
13161 token1 = cp_lexer_peek_token (parser->lexer);
13162
13163 /* If the next keyword is `asm', we have an asm-definition. */
13164 if (token1->keyword == RID_ASM)
13165 {
13166 if (statement_p)
13167 cp_parser_commit_to_tentative_parse (parser);
13168 cp_parser_asm_definition (parser);
13169 }
13170 /* If the next keyword is `namespace', we have a
13171 namespace-alias-definition. */
13172 else if (token1->keyword == RID_NAMESPACE)
13173 cp_parser_namespace_alias_definition (parser);
13174 /* If the next keyword is `using', we have a
13175 using-declaration, a using-directive, or an alias-declaration. */
13176 else if (token1->keyword == RID_USING)
13177 {
13178 cp_token *token2;
13179
13180 if (statement_p)
13181 cp_parser_commit_to_tentative_parse (parser);
13182 /* If the token after `using' is `namespace', then we have a
13183 using-directive. */
13184 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13185 if (token2->keyword == RID_NAMESPACE)
13186 cp_parser_using_directive (parser);
13187 /* If the second token after 'using' is '=', then we have an
13188 alias-declaration. */
13189 else if (cxx_dialect >= cxx11
13190 && token2->type == CPP_NAME
13191 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13192 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13193 cp_parser_alias_declaration (parser);
13194 /* Otherwise, it's a using-declaration. */
13195 else
13196 cp_parser_using_declaration (parser,
13197 /*access_declaration_p=*/false);
13198 }
13199 /* If the next keyword is `__label__' we have a misplaced label
13200 declaration. */
13201 else if (token1->keyword == RID_LABEL)
13202 {
13203 cp_lexer_consume_token (parser->lexer);
13204 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13205 cp_parser_skip_to_end_of_statement (parser);
13206 /* If the next token is now a `;', consume it. */
13207 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13208 cp_lexer_consume_token (parser->lexer);
13209 }
13210 /* If the next token is `static_assert' we have a static assertion. */
13211 else if (token1->keyword == RID_STATIC_ASSERT)
13212 cp_parser_static_assert (parser, /*member_p=*/false);
13213 /* Anything else must be a simple-declaration. */
13214 else
13215 cp_parser_simple_declaration (parser, !statement_p,
13216 /*maybe_range_for_decl*/NULL);
13217 }
13218
13219 /* Parse a simple-declaration.
13220
13221 simple-declaration:
13222 decl-specifier-seq [opt] init-declarator-list [opt] ;
13223 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13224 brace-or-equal-initializer ;
13225
13226 init-declarator-list:
13227 init-declarator
13228 init-declarator-list , init-declarator
13229
13230 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13231 function-definition as a simple-declaration.
13232
13233 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13234 parsed declaration if it is an uninitialized single declarator not followed
13235 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13236 if present, will not be consumed. */
13237
13238 static void
13239 cp_parser_simple_declaration (cp_parser* parser,
13240 bool function_definition_allowed_p,
13241 tree *maybe_range_for_decl)
13242 {
13243 cp_decl_specifier_seq decl_specifiers;
13244 int declares_class_or_enum;
13245 bool saw_declarator;
13246 location_t comma_loc = UNKNOWN_LOCATION;
13247 location_t init_loc = UNKNOWN_LOCATION;
13248
13249 if (maybe_range_for_decl)
13250 *maybe_range_for_decl = NULL_TREE;
13251
13252 /* Defer access checks until we know what is being declared; the
13253 checks for names appearing in the decl-specifier-seq should be
13254 done as if we were in the scope of the thing being declared. */
13255 push_deferring_access_checks (dk_deferred);
13256
13257 /* Parse the decl-specifier-seq. We have to keep track of whether
13258 or not the decl-specifier-seq declares a named class or
13259 enumeration type, since that is the only case in which the
13260 init-declarator-list is allowed to be empty.
13261
13262 [dcl.dcl]
13263
13264 In a simple-declaration, the optional init-declarator-list can be
13265 omitted only when declaring a class or enumeration, that is when
13266 the decl-specifier-seq contains either a class-specifier, an
13267 elaborated-type-specifier, or an enum-specifier. */
13268 cp_parser_decl_specifier_seq (parser,
13269 CP_PARSER_FLAGS_OPTIONAL,
13270 &decl_specifiers,
13271 &declares_class_or_enum);
13272 /* We no longer need to defer access checks. */
13273 stop_deferring_access_checks ();
13274
13275 /* In a block scope, a valid declaration must always have a
13276 decl-specifier-seq. By not trying to parse declarators, we can
13277 resolve the declaration/expression ambiguity more quickly. */
13278 if (!function_definition_allowed_p
13279 && !decl_specifiers.any_specifiers_p)
13280 {
13281 cp_parser_error (parser, "expected declaration");
13282 goto done;
13283 }
13284
13285 /* If the next two tokens are both identifiers, the code is
13286 erroneous. The usual cause of this situation is code like:
13287
13288 T t;
13289
13290 where "T" should name a type -- but does not. */
13291 if (!decl_specifiers.any_type_specifiers_p
13292 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13293 {
13294 /* If parsing tentatively, we should commit; we really are
13295 looking at a declaration. */
13296 cp_parser_commit_to_tentative_parse (parser);
13297 /* Give up. */
13298 goto done;
13299 }
13300
13301 cp_parser_maybe_commit_to_declaration (parser,
13302 decl_specifiers.any_specifiers_p);
13303
13304 /* Look for C++17 decomposition declaration. */
13305 for (size_t n = 1; ; n++)
13306 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13307 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13308 continue;
13309 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13310 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13311 && decl_specifiers.any_specifiers_p)
13312 {
13313 tree decl
13314 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13315 maybe_range_for_decl,
13316 &init_loc);
13317
13318 /* The next token should be either a `,' or a `;'. */
13319 cp_token *token = cp_lexer_peek_token (parser->lexer);
13320 /* If it's a `;', we are done. */
13321 if (token->type == CPP_SEMICOLON)
13322 goto finish;
13323 else if (maybe_range_for_decl)
13324 {
13325 if (*maybe_range_for_decl == NULL_TREE)
13326 *maybe_range_for_decl = error_mark_node;
13327 goto finish;
13328 }
13329 /* Anything else is an error. */
13330 else
13331 {
13332 /* If we have already issued an error message we don't need
13333 to issue another one. */
13334 if ((decl != error_mark_node
13335 && DECL_INITIAL (decl) != error_mark_node)
13336 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13337 cp_parser_error (parser, "expected %<,%> or %<;%>");
13338 /* Skip tokens until we reach the end of the statement. */
13339 cp_parser_skip_to_end_of_statement (parser);
13340 /* If the next token is now a `;', consume it. */
13341 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13342 cp_lexer_consume_token (parser->lexer);
13343 goto done;
13344 }
13345 }
13346 else
13347 break;
13348
13349 tree last_type;
13350 bool auto_specifier_p;
13351 /* NULL_TREE if both variable and function declaration are allowed,
13352 error_mark_node if function declaration are not allowed and
13353 a FUNCTION_DECL that should be diagnosed if it is followed by
13354 variable declarations. */
13355 tree auto_function_declaration;
13356
13357 last_type = NULL_TREE;
13358 auto_specifier_p
13359 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13360 auto_function_declaration = NULL_TREE;
13361
13362 /* Keep going until we hit the `;' at the end of the simple
13363 declaration. */
13364 saw_declarator = false;
13365 while (cp_lexer_next_token_is_not (parser->lexer,
13366 CPP_SEMICOLON))
13367 {
13368 cp_token *token;
13369 bool function_definition_p;
13370 tree decl;
13371 tree auto_result = NULL_TREE;
13372
13373 if (saw_declarator)
13374 {
13375 /* If we are processing next declarator, comma is expected */
13376 token = cp_lexer_peek_token (parser->lexer);
13377 gcc_assert (token->type == CPP_COMMA);
13378 cp_lexer_consume_token (parser->lexer);
13379 if (maybe_range_for_decl)
13380 {
13381 *maybe_range_for_decl = error_mark_node;
13382 if (comma_loc == UNKNOWN_LOCATION)
13383 comma_loc = token->location;
13384 }
13385 }
13386 else
13387 saw_declarator = true;
13388
13389 /* Parse the init-declarator. */
13390 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13391 /*checks=*/NULL,
13392 function_definition_allowed_p,
13393 /*member_p=*/false,
13394 declares_class_or_enum,
13395 &function_definition_p,
13396 maybe_range_for_decl,
13397 &init_loc,
13398 &auto_result);
13399 /* If an error occurred while parsing tentatively, exit quickly.
13400 (That usually happens when in the body of a function; each
13401 statement is treated as a declaration-statement until proven
13402 otherwise.) */
13403 if (cp_parser_error_occurred (parser))
13404 goto done;
13405
13406 if (auto_specifier_p && cxx_dialect >= cxx14)
13407 {
13408 /* If the init-declarator-list contains more than one
13409 init-declarator, they shall all form declarations of
13410 variables. */
13411 if (auto_function_declaration == NULL_TREE)
13412 auto_function_declaration
13413 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13414 else if (TREE_CODE (decl) == FUNCTION_DECL
13415 || auto_function_declaration != error_mark_node)
13416 {
13417 error_at (decl_specifiers.locations[ds_type_spec],
13418 "non-variable %qD in declaration with more than one "
13419 "declarator with placeholder type",
13420 TREE_CODE (decl) == FUNCTION_DECL
13421 ? decl : auto_function_declaration);
13422 auto_function_declaration = error_mark_node;
13423 }
13424 }
13425
13426 if (auto_result
13427 && (!processing_template_decl || !type_uses_auto (auto_result)))
13428 {
13429 if (last_type
13430 && last_type != error_mark_node
13431 && !same_type_p (auto_result, last_type))
13432 {
13433 /* If the list of declarators contains more than one declarator,
13434 the type of each declared variable is determined as described
13435 above. If the type deduced for the template parameter U is not
13436 the same in each deduction, the program is ill-formed. */
13437 error_at (decl_specifiers.locations[ds_type_spec],
13438 "inconsistent deduction for %qT: %qT and then %qT",
13439 decl_specifiers.type, last_type, auto_result);
13440 last_type = error_mark_node;
13441 }
13442 else
13443 last_type = auto_result;
13444 }
13445
13446 /* Handle function definitions specially. */
13447 if (function_definition_p)
13448 {
13449 /* If the next token is a `,', then we are probably
13450 processing something like:
13451
13452 void f() {}, *p;
13453
13454 which is erroneous. */
13455 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13456 {
13457 cp_token *token = cp_lexer_peek_token (parser->lexer);
13458 error_at (token->location,
13459 "mixing"
13460 " declarations and function-definitions is forbidden");
13461 }
13462 /* Otherwise, we're done with the list of declarators. */
13463 else
13464 {
13465 pop_deferring_access_checks ();
13466 return;
13467 }
13468 }
13469 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13470 *maybe_range_for_decl = decl;
13471 /* The next token should be either a `,' or a `;'. */
13472 token = cp_lexer_peek_token (parser->lexer);
13473 /* If it's a `,', there are more declarators to come. */
13474 if (token->type == CPP_COMMA)
13475 /* will be consumed next time around */;
13476 /* If it's a `;', we are done. */
13477 else if (token->type == CPP_SEMICOLON)
13478 break;
13479 else if (maybe_range_for_decl)
13480 {
13481 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13482 permerror (decl_specifiers.locations[ds_type_spec],
13483 "types may not be defined in a for-range-declaration");
13484 break;
13485 }
13486 /* Anything else is an error. */
13487 else
13488 {
13489 /* If we have already issued an error message we don't need
13490 to issue another one. */
13491 if ((decl != error_mark_node
13492 && DECL_INITIAL (decl) != error_mark_node)
13493 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13494 cp_parser_error (parser, "expected %<,%> or %<;%>");
13495 /* Skip tokens until we reach the end of the statement. */
13496 cp_parser_skip_to_end_of_statement (parser);
13497 /* If the next token is now a `;', consume it. */
13498 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13499 cp_lexer_consume_token (parser->lexer);
13500 goto done;
13501 }
13502 /* After the first time around, a function-definition is not
13503 allowed -- even if it was OK at first. For example:
13504
13505 int i, f() {}
13506
13507 is not valid. */
13508 function_definition_allowed_p = false;
13509 }
13510
13511 /* Issue an error message if no declarators are present, and the
13512 decl-specifier-seq does not itself declare a class or
13513 enumeration: [dcl.dcl]/3. */
13514 if (!saw_declarator)
13515 {
13516 if (cp_parser_declares_only_class_p (parser))
13517 {
13518 if (!declares_class_or_enum
13519 && decl_specifiers.type
13520 && OVERLOAD_TYPE_P (decl_specifiers.type))
13521 /* Ensure an error is issued anyway when finish_decltype_type,
13522 called via cp_parser_decl_specifier_seq, returns a class or
13523 an enumeration (c++/51786). */
13524 decl_specifiers.type = NULL_TREE;
13525 shadow_tag (&decl_specifiers);
13526 }
13527 /* Perform any deferred access checks. */
13528 perform_deferred_access_checks (tf_warning_or_error);
13529 }
13530
13531 /* Consume the `;'. */
13532 finish:
13533 if (!maybe_range_for_decl)
13534 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13535 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13536 {
13537 if (init_loc != UNKNOWN_LOCATION)
13538 error_at (init_loc, "initializer in range-based %<for%> loop");
13539 if (comma_loc != UNKNOWN_LOCATION)
13540 error_at (comma_loc,
13541 "multiple declarations in range-based %<for%> loop");
13542 }
13543
13544 done:
13545 pop_deferring_access_checks ();
13546 }
13547
13548 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13549 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13550 initializer ; */
13551
13552 static tree
13553 cp_parser_decomposition_declaration (cp_parser *parser,
13554 cp_decl_specifier_seq *decl_specifiers,
13555 tree *maybe_range_for_decl,
13556 location_t *init_loc)
13557 {
13558 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13559 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13560 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13561
13562 /* Parse the identifier-list. */
13563 auto_vec<cp_expr, 10> v;
13564 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13565 while (true)
13566 {
13567 cp_expr e = cp_parser_identifier (parser);
13568 if (e.get_value () == error_mark_node)
13569 break;
13570 v.safe_push (e);
13571 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13572 break;
13573 cp_lexer_consume_token (parser->lexer);
13574 }
13575
13576 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13577 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13578 {
13579 end_loc = UNKNOWN_LOCATION;
13580 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13581 false);
13582 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13583 cp_lexer_consume_token (parser->lexer);
13584 else
13585 {
13586 cp_parser_skip_to_end_of_statement (parser);
13587 return error_mark_node;
13588 }
13589 }
13590
13591 if (cxx_dialect < cxx17)
13592 pedwarn (loc, 0, "structured bindings only available with "
13593 "-std=c++17 or -std=gnu++17");
13594
13595 tree pushed_scope;
13596 cp_declarator *declarator = make_declarator (cdk_decomp);
13597 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13598 declarator->id_loc = loc;
13599 if (ref_qual != REF_QUAL_NONE)
13600 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13601 ref_qual == REF_QUAL_RVALUE,
13602 NULL_TREE);
13603 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13604 NULL_TREE, decl_specifiers->attributes,
13605 &pushed_scope);
13606 tree orig_decl = decl;
13607
13608 unsigned int i;
13609 cp_expr e;
13610 cp_decl_specifier_seq decl_specs;
13611 clear_decl_specs (&decl_specs);
13612 decl_specs.type = make_auto ();
13613 tree prev = decl;
13614 FOR_EACH_VEC_ELT (v, i, e)
13615 {
13616 if (i == 0)
13617 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13618 sfk_none, e.get_location ());
13619 else
13620 {
13621 declarator->u.id.unqualified_name = e.get_value ();
13622 declarator->id_loc = e.get_location ();
13623 }
13624 tree elt_pushed_scope;
13625 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13626 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13627 if (decl2 == error_mark_node)
13628 decl = error_mark_node;
13629 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13630 {
13631 /* Ensure we've diagnosed redeclaration if we aren't creating
13632 a new VAR_DECL. */
13633 gcc_assert (errorcount);
13634 decl = error_mark_node;
13635 }
13636 else
13637 prev = decl2;
13638 if (elt_pushed_scope)
13639 pop_scope (elt_pushed_scope);
13640 }
13641
13642 if (v.is_empty ())
13643 {
13644 error_at (loc, "empty structured binding declaration");
13645 decl = error_mark_node;
13646 }
13647
13648 if (maybe_range_for_decl == NULL
13649 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13650 {
13651 bool non_constant_p = false, is_direct_init = false;
13652 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13653 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13654 &non_constant_p);
13655 if (initializer == NULL_TREE
13656 || (TREE_CODE (initializer) == TREE_LIST
13657 && TREE_CHAIN (initializer))
13658 || (is_direct_init
13659 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13660 && CONSTRUCTOR_NELTS (initializer) != 1))
13661 {
13662 error_at (loc, "invalid initializer for structured binding "
13663 "declaration");
13664 initializer = error_mark_node;
13665 }
13666
13667 if (decl != error_mark_node)
13668 {
13669 cp_maybe_mangle_decomp (decl, prev, v.length ());
13670 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13671 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13672 cp_finish_decomp (decl, prev, v.length ());
13673 }
13674 }
13675 else if (decl != error_mark_node)
13676 {
13677 *maybe_range_for_decl = prev;
13678 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13679 the underlying DECL. */
13680 cp_finish_decomp (decl, prev, v.length ());
13681 }
13682
13683 if (pushed_scope)
13684 pop_scope (pushed_scope);
13685
13686 if (decl == error_mark_node && DECL_P (orig_decl))
13687 {
13688 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13689 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13690 }
13691
13692 return decl;
13693 }
13694
13695 /* Parse a decl-specifier-seq.
13696
13697 decl-specifier-seq:
13698 decl-specifier-seq [opt] decl-specifier
13699 decl-specifier attribute-specifier-seq [opt] (C++11)
13700
13701 decl-specifier:
13702 storage-class-specifier
13703 type-specifier
13704 function-specifier
13705 friend
13706 typedef
13707
13708 GNU Extension:
13709
13710 decl-specifier:
13711 attributes
13712
13713 Concepts Extension:
13714
13715 decl-specifier:
13716 concept
13717
13718 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13719
13720 The parser flags FLAGS is used to control type-specifier parsing.
13721
13722 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13723 flags:
13724
13725 1: one of the decl-specifiers is an elaborated-type-specifier
13726 (i.e., a type declaration)
13727 2: one of the decl-specifiers is an enum-specifier or a
13728 class-specifier (i.e., a type definition)
13729
13730 */
13731
13732 static void
13733 cp_parser_decl_specifier_seq (cp_parser* parser,
13734 cp_parser_flags flags,
13735 cp_decl_specifier_seq *decl_specs,
13736 int* declares_class_or_enum)
13737 {
13738 bool constructor_possible_p = !parser->in_declarator_p;
13739 bool found_decl_spec = false;
13740 cp_token *start_token = NULL;
13741 cp_decl_spec ds;
13742
13743 /* Clear DECL_SPECS. */
13744 clear_decl_specs (decl_specs);
13745
13746 /* Assume no class or enumeration type is declared. */
13747 *declares_class_or_enum = 0;
13748
13749 /* Keep reading specifiers until there are no more to read. */
13750 while (true)
13751 {
13752 bool constructor_p;
13753 cp_token *token;
13754 ds = ds_last;
13755
13756 /* Peek at the next token. */
13757 token = cp_lexer_peek_token (parser->lexer);
13758
13759 /* Save the first token of the decl spec list for error
13760 reporting. */
13761 if (!start_token)
13762 start_token = token;
13763 /* Handle attributes. */
13764 if (cp_next_tokens_can_be_attribute_p (parser))
13765 {
13766 /* Parse the attributes. */
13767 tree attrs = cp_parser_attributes_opt (parser);
13768
13769 /* In a sequence of declaration specifiers, c++11 attributes
13770 appertain to the type that precede them. In that case
13771 [dcl.spec]/1 says:
13772
13773 The attribute-specifier-seq affects the type only for
13774 the declaration it appears in, not other declarations
13775 involving the same type.
13776
13777 But for now let's force the user to position the
13778 attribute either at the beginning of the declaration or
13779 after the declarator-id, which would clearly mean that it
13780 applies to the declarator. */
13781 if (cxx11_attribute_p (attrs))
13782 {
13783 if (!found_decl_spec)
13784 /* The c++11 attribute is at the beginning of the
13785 declaration. It appertains to the entity being
13786 declared. */;
13787 else
13788 {
13789 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13790 {
13791 /* This is an attribute following a
13792 class-specifier. */
13793 if (decl_specs->type_definition_p)
13794 warn_misplaced_attr_for_class_type (token->location,
13795 decl_specs->type);
13796 attrs = NULL_TREE;
13797 }
13798 else
13799 {
13800 decl_specs->std_attributes
13801 = attr_chainon (decl_specs->std_attributes, attrs);
13802 if (decl_specs->locations[ds_std_attribute] == 0)
13803 decl_specs->locations[ds_std_attribute] = token->location;
13804 }
13805 continue;
13806 }
13807 }
13808
13809 decl_specs->attributes
13810 = attr_chainon (decl_specs->attributes, attrs);
13811 if (decl_specs->locations[ds_attribute] == 0)
13812 decl_specs->locations[ds_attribute] = token->location;
13813 continue;
13814 }
13815 /* Assume we will find a decl-specifier keyword. */
13816 found_decl_spec = true;
13817 /* If the next token is an appropriate keyword, we can simply
13818 add it to the list. */
13819 switch (token->keyword)
13820 {
13821 /* decl-specifier:
13822 friend
13823 constexpr */
13824 case RID_FRIEND:
13825 if (!at_class_scope_p ())
13826 {
13827 gcc_rich_location richloc (token->location);
13828 richloc.add_fixit_remove ();
13829 error_at (&richloc, "%<friend%> used outside of class");
13830 cp_lexer_purge_token (parser->lexer);
13831 }
13832 else
13833 {
13834 ds = ds_friend;
13835 /* Consume the token. */
13836 cp_lexer_consume_token (parser->lexer);
13837 }
13838 break;
13839
13840 case RID_CONSTEXPR:
13841 ds = ds_constexpr;
13842 cp_lexer_consume_token (parser->lexer);
13843 break;
13844
13845 case RID_CONCEPT:
13846 ds = ds_concept;
13847 cp_lexer_consume_token (parser->lexer);
13848 break;
13849
13850 /* function-specifier:
13851 inline
13852 virtual
13853 explicit */
13854 case RID_INLINE:
13855 case RID_VIRTUAL:
13856 case RID_EXPLICIT:
13857 cp_parser_function_specifier_opt (parser, decl_specs);
13858 break;
13859
13860 /* decl-specifier:
13861 typedef */
13862 case RID_TYPEDEF:
13863 ds = ds_typedef;
13864 /* Consume the token. */
13865 cp_lexer_consume_token (parser->lexer);
13866 /* A constructor declarator cannot appear in a typedef. */
13867 constructor_possible_p = false;
13868 /* The "typedef" keyword can only occur in a declaration; we
13869 may as well commit at this point. */
13870 cp_parser_commit_to_tentative_parse (parser);
13871
13872 if (decl_specs->storage_class != sc_none)
13873 decl_specs->conflicting_specifiers_p = true;
13874 break;
13875
13876 /* storage-class-specifier:
13877 auto
13878 register
13879 static
13880 extern
13881 mutable
13882
13883 GNU Extension:
13884 thread */
13885 case RID_AUTO:
13886 if (cxx_dialect == cxx98)
13887 {
13888 /* Consume the token. */
13889 cp_lexer_consume_token (parser->lexer);
13890
13891 /* Complain about `auto' as a storage specifier, if
13892 we're complaining about C++0x compatibility. */
13893 gcc_rich_location richloc (token->location);
13894 richloc.add_fixit_remove ();
13895 warning_at (&richloc, OPT_Wc__11_compat,
13896 "%<auto%> changes meaning in C++11; "
13897 "please remove it");
13898
13899 /* Set the storage class anyway. */
13900 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13901 token);
13902 }
13903 else
13904 /* C++0x auto type-specifier. */
13905 found_decl_spec = false;
13906 break;
13907
13908 case RID_REGISTER:
13909 case RID_STATIC:
13910 case RID_EXTERN:
13911 case RID_MUTABLE:
13912 /* Consume the token. */
13913 cp_lexer_consume_token (parser->lexer);
13914 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13915 token);
13916 break;
13917 case RID_THREAD:
13918 /* Consume the token. */
13919 ds = ds_thread;
13920 cp_lexer_consume_token (parser->lexer);
13921 break;
13922
13923 default:
13924 /* We did not yet find a decl-specifier yet. */
13925 found_decl_spec = false;
13926 break;
13927 }
13928
13929 if (found_decl_spec
13930 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13931 && token->keyword != RID_CONSTEXPR)
13932 error ("decl-specifier invalid in condition");
13933
13934 if (found_decl_spec
13935 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13936 && token->keyword != RID_MUTABLE
13937 && token->keyword != RID_CONSTEXPR)
13938 error_at (token->location, "%qD invalid in lambda",
13939 ridpointers[token->keyword]);
13940
13941 if (ds != ds_last)
13942 set_and_check_decl_spec_loc (decl_specs, ds, token);
13943
13944 /* Constructors are a special case. The `S' in `S()' is not a
13945 decl-specifier; it is the beginning of the declarator. */
13946 constructor_p
13947 = (!found_decl_spec
13948 && constructor_possible_p
13949 && (cp_parser_constructor_declarator_p
13950 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13951
13952 /* If we don't have a DECL_SPEC yet, then we must be looking at
13953 a type-specifier. */
13954 if (!found_decl_spec && !constructor_p)
13955 {
13956 int decl_spec_declares_class_or_enum;
13957 bool is_cv_qualifier;
13958 tree type_spec;
13959
13960 type_spec
13961 = cp_parser_type_specifier (parser, flags,
13962 decl_specs,
13963 /*is_declaration=*/true,
13964 &decl_spec_declares_class_or_enum,
13965 &is_cv_qualifier);
13966 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13967
13968 /* If this type-specifier referenced a user-defined type
13969 (a typedef, class-name, etc.), then we can't allow any
13970 more such type-specifiers henceforth.
13971
13972 [dcl.spec]
13973
13974 The longest sequence of decl-specifiers that could
13975 possibly be a type name is taken as the
13976 decl-specifier-seq of a declaration. The sequence shall
13977 be self-consistent as described below.
13978
13979 [dcl.type]
13980
13981 As a general rule, at most one type-specifier is allowed
13982 in the complete decl-specifier-seq of a declaration. The
13983 only exceptions are the following:
13984
13985 -- const or volatile can be combined with any other
13986 type-specifier.
13987
13988 -- signed or unsigned can be combined with char, long,
13989 short, or int.
13990
13991 -- ..
13992
13993 Example:
13994
13995 typedef char* Pc;
13996 void g (const int Pc);
13997
13998 Here, Pc is *not* part of the decl-specifier seq; it's
13999 the declarator. Therefore, once we see a type-specifier
14000 (other than a cv-qualifier), we forbid any additional
14001 user-defined types. We *do* still allow things like `int
14002 int' to be considered a decl-specifier-seq, and issue the
14003 error message later. */
14004 if (type_spec && !is_cv_qualifier)
14005 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14006 /* A constructor declarator cannot follow a type-specifier. */
14007 if (type_spec)
14008 {
14009 constructor_possible_p = false;
14010 found_decl_spec = true;
14011 if (!is_cv_qualifier)
14012 decl_specs->any_type_specifiers_p = true;
14013
14014 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14015 error_at (token->location, "type-specifier invalid in lambda");
14016 }
14017 }
14018
14019 /* If we still do not have a DECL_SPEC, then there are no more
14020 decl-specifiers. */
14021 if (!found_decl_spec)
14022 break;
14023
14024 decl_specs->any_specifiers_p = true;
14025 /* After we see one decl-specifier, further decl-specifiers are
14026 always optional. */
14027 flags |= CP_PARSER_FLAGS_OPTIONAL;
14028 }
14029
14030 /* Don't allow a friend specifier with a class definition. */
14031 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14032 && (*declares_class_or_enum & 2))
14033 error_at (decl_specs->locations[ds_friend],
14034 "class definition may not be declared a friend");
14035 }
14036
14037 /* Parse an (optional) storage-class-specifier.
14038
14039 storage-class-specifier:
14040 auto
14041 register
14042 static
14043 extern
14044 mutable
14045
14046 GNU Extension:
14047
14048 storage-class-specifier:
14049 thread
14050
14051 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14052
14053 static tree
14054 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14055 {
14056 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14057 {
14058 case RID_AUTO:
14059 if (cxx_dialect != cxx98)
14060 return NULL_TREE;
14061 /* Fall through for C++98. */
14062 gcc_fallthrough ();
14063
14064 case RID_REGISTER:
14065 case RID_STATIC:
14066 case RID_EXTERN:
14067 case RID_MUTABLE:
14068 case RID_THREAD:
14069 /* Consume the token. */
14070 return cp_lexer_consume_token (parser->lexer)->u.value;
14071
14072 default:
14073 return NULL_TREE;
14074 }
14075 }
14076
14077 /* Parse an (optional) function-specifier.
14078
14079 function-specifier:
14080 inline
14081 virtual
14082 explicit
14083
14084 C++2A Extension:
14085 explicit(constant-expression)
14086
14087 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14088 Updates DECL_SPECS, if it is non-NULL. */
14089
14090 static tree
14091 cp_parser_function_specifier_opt (cp_parser* parser,
14092 cp_decl_specifier_seq *decl_specs)
14093 {
14094 cp_token *token = cp_lexer_peek_token (parser->lexer);
14095 switch (token->keyword)
14096 {
14097 case RID_INLINE:
14098 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14099 break;
14100
14101 case RID_VIRTUAL:
14102 /* 14.5.2.3 [temp.mem]
14103
14104 A member function template shall not be virtual. */
14105 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14106 && current_class_type)
14107 error_at (token->location, "templates may not be %<virtual%>");
14108 else
14109 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14110 break;
14111
14112 case RID_EXPLICIT:
14113 {
14114 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14115 /* If we see '(', it's C++20 explicit(bool). */
14116 tree expr;
14117 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14118 {
14119 matching_parens parens;
14120 parens.consume_open (parser);
14121
14122 /* New types are not allowed in an explicit-specifier. */
14123 const char *saved_message
14124 = parser->type_definition_forbidden_message;
14125 parser->type_definition_forbidden_message
14126 = G_("types may not be defined in explicit-specifier");
14127
14128 if (cxx_dialect < cxx2a)
14129 pedwarn (token->location, 0,
14130 "%<explicit(bool)%> only available with -std=c++2a "
14131 "or -std=gnu++2a");
14132
14133 /* Parse the constant-expression. */
14134 expr = cp_parser_constant_expression (parser);
14135
14136 /* Restore the saved message. */
14137 parser->type_definition_forbidden_message = saved_message;
14138 parens.require_close (parser);
14139 }
14140 else
14141 /* The explicit-specifier explicit without a constant-expression is
14142 equivalent to the explicit-specifier explicit(true). */
14143 expr = boolean_true_node;
14144
14145 /* [dcl.fct.spec]
14146 "the constant-expression, if supplied, shall be a contextually
14147 converted constant expression of type bool." */
14148 expr = build_explicit_specifier (expr, tf_warning_or_error);
14149 /* We could evaluate it -- mark the decl as appropriate. */
14150 if (expr == boolean_true_node)
14151 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14152 else if (expr == boolean_false_node)
14153 /* Don't mark the decl as explicit. */;
14154 else if (decl_specs)
14155 /* The expression was value-dependent. Remember it so that we can
14156 substitute it later. */
14157 decl_specs->explicit_specifier = expr;
14158 return id;
14159 }
14160
14161 default:
14162 return NULL_TREE;
14163 }
14164
14165 /* Consume the token. */
14166 return cp_lexer_consume_token (parser->lexer)->u.value;
14167 }
14168
14169 /* Parse a linkage-specification.
14170
14171 linkage-specification:
14172 extern string-literal { declaration-seq [opt] }
14173 extern string-literal declaration */
14174
14175 static void
14176 cp_parser_linkage_specification (cp_parser* parser)
14177 {
14178 tree linkage;
14179
14180 /* Look for the `extern' keyword. */
14181 cp_token *extern_token
14182 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14183
14184 /* Look for the string-literal. */
14185 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14186 linkage = cp_parser_string_literal (parser, false, false);
14187
14188 /* Transform the literal into an identifier. If the literal is a
14189 wide-character string, or contains embedded NULs, then we can't
14190 handle it as the user wants. */
14191 if (strlen (TREE_STRING_POINTER (linkage))
14192 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14193 {
14194 cp_parser_error (parser, "invalid linkage-specification");
14195 /* Assume C++ linkage. */
14196 linkage = lang_name_cplusplus;
14197 }
14198 else
14199 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14200
14201 /* We're now using the new linkage. */
14202 push_lang_context (linkage);
14203
14204 /* Preserve the location of the the innermost linkage specification,
14205 tracking the locations of nested specifications via a local. */
14206 location_t saved_location
14207 = parser->innermost_linkage_specification_location;
14208 /* Construct a location ranging from the start of the "extern" to
14209 the end of the string-literal, with the caret at the start, e.g.:
14210 extern "C" {
14211 ^~~~~~~~~~
14212 */
14213 parser->innermost_linkage_specification_location
14214 = make_location (extern_token->location,
14215 extern_token->location,
14216 get_finish (string_token->location));
14217
14218 /* If the next token is a `{', then we're using the first
14219 production. */
14220 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14221 {
14222 cp_ensure_no_omp_declare_simd (parser);
14223 cp_ensure_no_oacc_routine (parser);
14224
14225 /* Consume the `{' token. */
14226 matching_braces braces;
14227 braces.consume_open (parser);
14228 /* Parse the declarations. */
14229 cp_parser_declaration_seq_opt (parser);
14230 /* Look for the closing `}'. */
14231 braces.require_close (parser);
14232 }
14233 /* Otherwise, there's just one declaration. */
14234 else
14235 {
14236 bool saved_in_unbraced_linkage_specification_p;
14237
14238 saved_in_unbraced_linkage_specification_p
14239 = parser->in_unbraced_linkage_specification_p;
14240 parser->in_unbraced_linkage_specification_p = true;
14241 cp_parser_declaration (parser);
14242 parser->in_unbraced_linkage_specification_p
14243 = saved_in_unbraced_linkage_specification_p;
14244 }
14245
14246 /* We're done with the linkage-specification. */
14247 pop_lang_context ();
14248
14249 /* Restore location of parent linkage specification, if any. */
14250 parser->innermost_linkage_specification_location = saved_location;
14251 }
14252
14253 /* Parse a static_assert-declaration.
14254
14255 static_assert-declaration:
14256 static_assert ( constant-expression , string-literal ) ;
14257 static_assert ( constant-expression ) ; (C++17)
14258
14259 If MEMBER_P, this static_assert is a class member. */
14260
14261 static void
14262 cp_parser_static_assert(cp_parser *parser, bool member_p)
14263 {
14264 cp_expr condition;
14265 location_t token_loc;
14266 tree message;
14267 bool dummy;
14268
14269 /* Peek at the `static_assert' token so we can keep track of exactly
14270 where the static assertion started. */
14271 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14272
14273 /* Look for the `static_assert' keyword. */
14274 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14275 RT_STATIC_ASSERT))
14276 return;
14277
14278 /* We know we are in a static assertion; commit to any tentative
14279 parse. */
14280 if (cp_parser_parsing_tentatively (parser))
14281 cp_parser_commit_to_tentative_parse (parser);
14282
14283 /* Parse the `(' starting the static assertion condition. */
14284 matching_parens parens;
14285 parens.require_open (parser);
14286
14287 /* Parse the constant-expression. Allow a non-constant expression
14288 here in order to give better diagnostics in finish_static_assert. */
14289 condition =
14290 cp_parser_constant_expression (parser,
14291 /*allow_non_constant_p=*/true,
14292 /*non_constant_p=*/&dummy);
14293
14294 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14295 {
14296 if (cxx_dialect < cxx17)
14297 pedwarn (input_location, OPT_Wpedantic,
14298 "static_assert without a message "
14299 "only available with -std=c++17 or -std=gnu++17");
14300 /* Eat the ')' */
14301 cp_lexer_consume_token (parser->lexer);
14302 message = build_string (1, "");
14303 TREE_TYPE (message) = char_array_type_node;
14304 fix_string_type (message);
14305 }
14306 else
14307 {
14308 /* Parse the separating `,'. */
14309 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14310
14311 /* Parse the string-literal message. */
14312 message = cp_parser_string_literal (parser,
14313 /*translate=*/false,
14314 /*wide_ok=*/true);
14315
14316 /* A `)' completes the static assertion. */
14317 if (!parens.require_close (parser))
14318 cp_parser_skip_to_closing_parenthesis (parser,
14319 /*recovering=*/true,
14320 /*or_comma=*/false,
14321 /*consume_paren=*/true);
14322 }
14323
14324 /* A semicolon terminates the declaration. */
14325 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14326
14327 /* Get the location for the static assertion. Use that of the
14328 condition if available, otherwise, use that of the "static_assert"
14329 token. */
14330 location_t assert_loc = condition.get_location ();
14331 if (assert_loc == UNKNOWN_LOCATION)
14332 assert_loc = token_loc;
14333
14334 /* Complete the static assertion, which may mean either processing
14335 the static assert now or saving it for template instantiation. */
14336 finish_static_assert (condition, message, assert_loc, member_p);
14337 }
14338
14339 /* Parse the expression in decltype ( expression ). */
14340
14341 static tree
14342 cp_parser_decltype_expr (cp_parser *parser,
14343 bool &id_expression_or_member_access_p)
14344 {
14345 cp_token *id_expr_start_token;
14346 tree expr;
14347
14348 /* Since we're going to preserve any side-effects from this parse, set up a
14349 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14350 in the expression. */
14351 tentative_firewall firewall (parser);
14352
14353 /* First, try parsing an id-expression. */
14354 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14355 cp_parser_parse_tentatively (parser);
14356 expr = cp_parser_id_expression (parser,
14357 /*template_keyword_p=*/false,
14358 /*check_dependency_p=*/true,
14359 /*template_p=*/NULL,
14360 /*declarator_p=*/false,
14361 /*optional_p=*/false);
14362
14363 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14364 {
14365 bool non_integral_constant_expression_p = false;
14366 tree id_expression = expr;
14367 cp_id_kind idk;
14368 const char *error_msg;
14369
14370 if (identifier_p (expr))
14371 /* Lookup the name we got back from the id-expression. */
14372 expr = cp_parser_lookup_name_simple (parser, expr,
14373 id_expr_start_token->location);
14374
14375 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14376 /* A template without args is not a complete id-expression. */
14377 expr = error_mark_node;
14378
14379 if (expr
14380 && expr != error_mark_node
14381 && TREE_CODE (expr) != TYPE_DECL
14382 && (TREE_CODE (expr) != BIT_NOT_EXPR
14383 || !TYPE_P (TREE_OPERAND (expr, 0)))
14384 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14385 {
14386 /* Complete lookup of the id-expression. */
14387 expr = (finish_id_expression
14388 (id_expression, expr, parser->scope, &idk,
14389 /*integral_constant_expression_p=*/false,
14390 /*allow_non_integral_constant_expression_p=*/true,
14391 &non_integral_constant_expression_p,
14392 /*template_p=*/false,
14393 /*done=*/true,
14394 /*address_p=*/false,
14395 /*template_arg_p=*/false,
14396 &error_msg,
14397 id_expr_start_token->location));
14398
14399 if (expr == error_mark_node)
14400 /* We found an id-expression, but it was something that we
14401 should not have found. This is an error, not something
14402 we can recover from, so note that we found an
14403 id-expression and we'll recover as gracefully as
14404 possible. */
14405 id_expression_or_member_access_p = true;
14406 }
14407
14408 if (expr
14409 && expr != error_mark_node
14410 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14411 /* We have an id-expression. */
14412 id_expression_or_member_access_p = true;
14413 }
14414
14415 if (!id_expression_or_member_access_p)
14416 {
14417 /* Abort the id-expression parse. */
14418 cp_parser_abort_tentative_parse (parser);
14419
14420 /* Parsing tentatively, again. */
14421 cp_parser_parse_tentatively (parser);
14422
14423 /* Parse a class member access. */
14424 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14425 /*cast_p=*/false, /*decltype*/true,
14426 /*member_access_only_p=*/true, NULL);
14427
14428 if (expr
14429 && expr != error_mark_node
14430 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14431 /* We have an id-expression. */
14432 id_expression_or_member_access_p = true;
14433 }
14434
14435 if (id_expression_or_member_access_p)
14436 /* We have parsed the complete id-expression or member access. */
14437 cp_parser_parse_definitely (parser);
14438 else
14439 {
14440 /* Abort our attempt to parse an id-expression or member access
14441 expression. */
14442 cp_parser_abort_tentative_parse (parser);
14443
14444 /* Commit to the tentative_firewall so we get syntax errors. */
14445 cp_parser_commit_to_tentative_parse (parser);
14446
14447 /* Parse a full expression. */
14448 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14449 /*decltype_p=*/true);
14450 }
14451
14452 return expr;
14453 }
14454
14455 /* Parse a `decltype' type. Returns the type.
14456
14457 simple-type-specifier:
14458 decltype ( expression )
14459 C++14 proposal:
14460 decltype ( auto ) */
14461
14462 static tree
14463 cp_parser_decltype (cp_parser *parser)
14464 {
14465 bool id_expression_or_member_access_p = false;
14466 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14467
14468 if (start_token->type == CPP_DECLTYPE)
14469 {
14470 /* Already parsed. */
14471 cp_lexer_consume_token (parser->lexer);
14472 return saved_checks_value (start_token->u.tree_check_value);
14473 }
14474
14475 /* Look for the `decltype' token. */
14476 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14477 return error_mark_node;
14478
14479 /* Parse the opening `('. */
14480 matching_parens parens;
14481 if (!parens.require_open (parser))
14482 return error_mark_node;
14483
14484 push_deferring_access_checks (dk_deferred);
14485
14486 tree expr = NULL_TREE;
14487
14488 if (cxx_dialect >= cxx14
14489 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14490 /* decltype (auto) */
14491 cp_lexer_consume_token (parser->lexer);
14492 else
14493 {
14494 /* decltype (expression) */
14495
14496 /* Types cannot be defined in a `decltype' expression. Save away the
14497 old message and set the new one. */
14498 const char *saved_message = parser->type_definition_forbidden_message;
14499 parser->type_definition_forbidden_message
14500 = G_("types may not be defined in %<decltype%> expressions");
14501
14502 /* The restrictions on constant-expressions do not apply inside
14503 decltype expressions. */
14504 bool saved_integral_constant_expression_p
14505 = parser->integral_constant_expression_p;
14506 bool saved_non_integral_constant_expression_p
14507 = parser->non_integral_constant_expression_p;
14508 parser->integral_constant_expression_p = false;
14509
14510 /* Within a parenthesized expression, a `>' token is always
14511 the greater-than operator. */
14512 bool saved_greater_than_is_operator_p
14513 = parser->greater_than_is_operator_p;
14514 parser->greater_than_is_operator_p = true;
14515
14516 /* Do not actually evaluate the expression. */
14517 ++cp_unevaluated_operand;
14518
14519 /* Do not warn about problems with the expression. */
14520 ++c_inhibit_evaluation_warnings;
14521
14522 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14523
14524 /* Go back to evaluating expressions. */
14525 --cp_unevaluated_operand;
14526 --c_inhibit_evaluation_warnings;
14527
14528 /* The `>' token might be the end of a template-id or
14529 template-parameter-list now. */
14530 parser->greater_than_is_operator_p
14531 = saved_greater_than_is_operator_p;
14532
14533 /* Restore the old message and the integral constant expression
14534 flags. */
14535 parser->type_definition_forbidden_message = saved_message;
14536 parser->integral_constant_expression_p
14537 = saved_integral_constant_expression_p;
14538 parser->non_integral_constant_expression_p
14539 = saved_non_integral_constant_expression_p;
14540 }
14541
14542 /* Parse to the closing `)'. */
14543 if (!parens.require_close (parser))
14544 {
14545 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14546 /*consume_paren=*/true);
14547 pop_deferring_access_checks ();
14548 return error_mark_node;
14549 }
14550
14551 if (!expr)
14552 {
14553 /* Build auto. */
14554 expr = make_decltype_auto ();
14555 AUTO_IS_DECLTYPE (expr) = true;
14556 }
14557 else
14558 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14559 tf_warning_or_error);
14560
14561 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14562 it again. */
14563 start_token->type = CPP_DECLTYPE;
14564 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14565 start_token->u.tree_check_value->value = expr;
14566 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14567 start_token->keyword = RID_MAX;
14568 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14569
14570 pop_to_parent_deferring_access_checks ();
14571
14572 return expr;
14573 }
14574
14575 /* Special member functions [gram.special] */
14576
14577 /* Parse a conversion-function-id.
14578
14579 conversion-function-id:
14580 operator conversion-type-id
14581
14582 Returns an IDENTIFIER_NODE representing the operator. */
14583
14584 static tree
14585 cp_parser_conversion_function_id (cp_parser* parser)
14586 {
14587 tree type;
14588 tree saved_scope;
14589 tree saved_qualifying_scope;
14590 tree saved_object_scope;
14591 tree pushed_scope = NULL_TREE;
14592
14593 /* Look for the `operator' token. */
14594 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14595 return error_mark_node;
14596 /* When we parse the conversion-type-id, the current scope will be
14597 reset. However, we need that information in able to look up the
14598 conversion function later, so we save it here. */
14599 saved_scope = parser->scope;
14600 saved_qualifying_scope = parser->qualifying_scope;
14601 saved_object_scope = parser->object_scope;
14602 /* We must enter the scope of the class so that the names of
14603 entities declared within the class are available in the
14604 conversion-type-id. For example, consider:
14605
14606 struct S {
14607 typedef int I;
14608 operator I();
14609 };
14610
14611 S::operator I() { ... }
14612
14613 In order to see that `I' is a type-name in the definition, we
14614 must be in the scope of `S'. */
14615 if (saved_scope)
14616 pushed_scope = push_scope (saved_scope);
14617 /* Parse the conversion-type-id. */
14618 type = cp_parser_conversion_type_id (parser);
14619 /* Leave the scope of the class, if any. */
14620 if (pushed_scope)
14621 pop_scope (pushed_scope);
14622 /* Restore the saved scope. */
14623 parser->scope = saved_scope;
14624 parser->qualifying_scope = saved_qualifying_scope;
14625 parser->object_scope = saved_object_scope;
14626 /* If the TYPE is invalid, indicate failure. */
14627 if (type == error_mark_node)
14628 return error_mark_node;
14629 return make_conv_op_name (type);
14630 }
14631
14632 /* Parse a conversion-type-id:
14633
14634 conversion-type-id:
14635 type-specifier-seq conversion-declarator [opt]
14636
14637 Returns the TYPE specified. */
14638
14639 static tree
14640 cp_parser_conversion_type_id (cp_parser* parser)
14641 {
14642 tree attributes;
14643 cp_decl_specifier_seq type_specifiers;
14644 cp_declarator *declarator;
14645 tree type_specified;
14646 const char *saved_message;
14647
14648 /* Parse the attributes. */
14649 attributes = cp_parser_attributes_opt (parser);
14650
14651 saved_message = parser->type_definition_forbidden_message;
14652 parser->type_definition_forbidden_message
14653 = G_("types may not be defined in a conversion-type-id");
14654
14655 /* Parse the type-specifiers. */
14656 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14657 /*is_trailing_return=*/false,
14658 &type_specifiers);
14659
14660 parser->type_definition_forbidden_message = saved_message;
14661
14662 /* If that didn't work, stop. */
14663 if (type_specifiers.type == error_mark_node)
14664 return error_mark_node;
14665 /* Parse the conversion-declarator. */
14666 declarator = cp_parser_conversion_declarator_opt (parser);
14667
14668 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14669 /*initialized=*/0, &attributes);
14670 if (attributes)
14671 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14672
14673 /* Don't give this error when parsing tentatively. This happens to
14674 work because we always parse this definitively once. */
14675 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14676 && type_uses_auto (type_specified))
14677 {
14678 if (cxx_dialect < cxx14)
14679 {
14680 error ("invalid use of %<auto%> in conversion operator");
14681 return error_mark_node;
14682 }
14683 else if (template_parm_scope_p ())
14684 warning (0, "use of %<auto%> in member template "
14685 "conversion operator can never be deduced");
14686 }
14687
14688 return type_specified;
14689 }
14690
14691 /* Parse an (optional) conversion-declarator.
14692
14693 conversion-declarator:
14694 ptr-operator conversion-declarator [opt]
14695
14696 */
14697
14698 static cp_declarator *
14699 cp_parser_conversion_declarator_opt (cp_parser* parser)
14700 {
14701 enum tree_code code;
14702 tree class_type, std_attributes = NULL_TREE;
14703 cp_cv_quals cv_quals;
14704
14705 /* We don't know if there's a ptr-operator next, or not. */
14706 cp_parser_parse_tentatively (parser);
14707 /* Try the ptr-operator. */
14708 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14709 &std_attributes);
14710 /* If it worked, look for more conversion-declarators. */
14711 if (cp_parser_parse_definitely (parser))
14712 {
14713 cp_declarator *declarator;
14714
14715 /* Parse another optional declarator. */
14716 declarator = cp_parser_conversion_declarator_opt (parser);
14717
14718 declarator = cp_parser_make_indirect_declarator
14719 (code, class_type, cv_quals, declarator, std_attributes);
14720
14721 return declarator;
14722 }
14723
14724 return NULL;
14725 }
14726
14727 /* Parse an (optional) ctor-initializer.
14728
14729 ctor-initializer:
14730 : mem-initializer-list */
14731
14732 static void
14733 cp_parser_ctor_initializer_opt (cp_parser* parser)
14734 {
14735 /* If the next token is not a `:', then there is no
14736 ctor-initializer. */
14737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14738 {
14739 /* Do default initialization of any bases and members. */
14740 if (DECL_CONSTRUCTOR_P (current_function_decl))
14741 finish_mem_initializers (NULL_TREE);
14742 return;
14743 }
14744
14745 /* Consume the `:' token. */
14746 cp_lexer_consume_token (parser->lexer);
14747 /* And the mem-initializer-list. */
14748 cp_parser_mem_initializer_list (parser);
14749 }
14750
14751 /* Parse a mem-initializer-list.
14752
14753 mem-initializer-list:
14754 mem-initializer ... [opt]
14755 mem-initializer ... [opt] , mem-initializer-list */
14756
14757 static void
14758 cp_parser_mem_initializer_list (cp_parser* parser)
14759 {
14760 tree mem_initializer_list = NULL_TREE;
14761 tree target_ctor = error_mark_node;
14762 cp_token *token = cp_lexer_peek_token (parser->lexer);
14763
14764 /* Let the semantic analysis code know that we are starting the
14765 mem-initializer-list. */
14766 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14767 error_at (token->location,
14768 "only constructors take member initializers");
14769
14770 /* Loop through the list. */
14771 while (true)
14772 {
14773 tree mem_initializer;
14774
14775 token = cp_lexer_peek_token (parser->lexer);
14776 /* Parse the mem-initializer. */
14777 mem_initializer = cp_parser_mem_initializer (parser);
14778 /* If the next token is a `...', we're expanding member initializers. */
14779 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14780 if (ellipsis
14781 || (mem_initializer != error_mark_node
14782 && check_for_bare_parameter_packs (TREE_PURPOSE
14783 (mem_initializer))))
14784 {
14785 /* Consume the `...'. */
14786 if (ellipsis)
14787 cp_lexer_consume_token (parser->lexer);
14788
14789 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14790 can be expanded but members cannot. */
14791 if (mem_initializer != error_mark_node
14792 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14793 {
14794 error_at (token->location,
14795 "cannot expand initializer for member %qD",
14796 TREE_PURPOSE (mem_initializer));
14797 mem_initializer = error_mark_node;
14798 }
14799
14800 /* Construct the pack expansion type. */
14801 if (mem_initializer != error_mark_node)
14802 mem_initializer = make_pack_expansion (mem_initializer);
14803 }
14804 if (target_ctor != error_mark_node
14805 && mem_initializer != error_mark_node)
14806 {
14807 error ("mem-initializer for %qD follows constructor delegation",
14808 TREE_PURPOSE (mem_initializer));
14809 mem_initializer = error_mark_node;
14810 }
14811 /* Look for a target constructor. */
14812 if (mem_initializer != error_mark_node
14813 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14814 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14815 {
14816 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14817 if (mem_initializer_list)
14818 {
14819 error ("constructor delegation follows mem-initializer for %qD",
14820 TREE_PURPOSE (mem_initializer_list));
14821 mem_initializer = error_mark_node;
14822 }
14823 target_ctor = mem_initializer;
14824 }
14825 /* Add it to the list, unless it was erroneous. */
14826 if (mem_initializer != error_mark_node)
14827 {
14828 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14829 mem_initializer_list = mem_initializer;
14830 }
14831 /* If the next token is not a `,', we're done. */
14832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14833 break;
14834 /* Consume the `,' token. */
14835 cp_lexer_consume_token (parser->lexer);
14836 }
14837
14838 /* Perform semantic analysis. */
14839 if (DECL_CONSTRUCTOR_P (current_function_decl))
14840 finish_mem_initializers (mem_initializer_list);
14841 }
14842
14843 /* Parse a mem-initializer.
14844
14845 mem-initializer:
14846 mem-initializer-id ( expression-list [opt] )
14847 mem-initializer-id braced-init-list
14848
14849 GNU extension:
14850
14851 mem-initializer:
14852 ( expression-list [opt] )
14853
14854 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14855 class) or FIELD_DECL (for a non-static data member) to initialize;
14856 the TREE_VALUE is the expression-list. An empty initialization
14857 list is represented by void_list_node. */
14858
14859 static tree
14860 cp_parser_mem_initializer (cp_parser* parser)
14861 {
14862 tree mem_initializer_id;
14863 tree expression_list;
14864 tree member;
14865 cp_token *token = cp_lexer_peek_token (parser->lexer);
14866
14867 /* Find out what is being initialized. */
14868 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14869 {
14870 permerror (token->location,
14871 "anachronistic old-style base class initializer");
14872 mem_initializer_id = NULL_TREE;
14873 }
14874 else
14875 {
14876 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14877 if (mem_initializer_id == error_mark_node)
14878 return mem_initializer_id;
14879 }
14880 member = expand_member_init (mem_initializer_id);
14881 if (member && !DECL_P (member))
14882 in_base_initializer = 1;
14883
14884 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14885 {
14886 bool expr_non_constant_p;
14887 cp_lexer_set_source_position (parser->lexer);
14888 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14889 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14890 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14891 expression_list = build_tree_list (NULL_TREE, expression_list);
14892 }
14893 else
14894 {
14895 vec<tree, va_gc> *vec;
14896 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14897 /*cast_p=*/false,
14898 /*allow_expansion_p=*/true,
14899 /*non_constant_p=*/NULL);
14900 if (vec == NULL)
14901 return error_mark_node;
14902 expression_list = build_tree_list_vec (vec);
14903 release_tree_vector (vec);
14904 }
14905
14906 if (expression_list == error_mark_node)
14907 return error_mark_node;
14908 if (!expression_list)
14909 expression_list = void_type_node;
14910
14911 in_base_initializer = 0;
14912
14913 return member ? build_tree_list (member, expression_list) : error_mark_node;
14914 }
14915
14916 /* Parse a mem-initializer-id.
14917
14918 mem-initializer-id:
14919 :: [opt] nested-name-specifier [opt] class-name
14920 decltype-specifier (C++11)
14921 identifier
14922
14923 Returns a TYPE indicating the class to be initialized for the first
14924 production (and the second in C++11). Returns an IDENTIFIER_NODE
14925 indicating the data member to be initialized for the last production. */
14926
14927 static tree
14928 cp_parser_mem_initializer_id (cp_parser* parser)
14929 {
14930 bool global_scope_p;
14931 bool nested_name_specifier_p;
14932 bool template_p = false;
14933 tree id;
14934
14935 cp_token *token = cp_lexer_peek_token (parser->lexer);
14936
14937 /* `typename' is not allowed in this context ([temp.res]). */
14938 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14939 {
14940 error_at (token->location,
14941 "keyword %<typename%> not allowed in this context (a qualified "
14942 "member initializer is implicitly a type)");
14943 cp_lexer_consume_token (parser->lexer);
14944 }
14945 /* Look for the optional `::' operator. */
14946 global_scope_p
14947 = (cp_parser_global_scope_opt (parser,
14948 /*current_scope_valid_p=*/false)
14949 != NULL_TREE);
14950 /* Look for the optional nested-name-specifier. The simplest way to
14951 implement:
14952
14953 [temp.res]
14954
14955 The keyword `typename' is not permitted in a base-specifier or
14956 mem-initializer; in these contexts a qualified name that
14957 depends on a template-parameter is implicitly assumed to be a
14958 type name.
14959
14960 is to assume that we have seen the `typename' keyword at this
14961 point. */
14962 nested_name_specifier_p
14963 = (cp_parser_nested_name_specifier_opt (parser,
14964 /*typename_keyword_p=*/true,
14965 /*check_dependency_p=*/true,
14966 /*type_p=*/true,
14967 /*is_declaration=*/true)
14968 != NULL_TREE);
14969 if (nested_name_specifier_p)
14970 template_p = cp_parser_optional_template_keyword (parser);
14971 /* If there is a `::' operator or a nested-name-specifier, then we
14972 are definitely looking for a class-name. */
14973 if (global_scope_p || nested_name_specifier_p)
14974 return cp_parser_class_name (parser,
14975 /*typename_keyword_p=*/true,
14976 /*template_keyword_p=*/template_p,
14977 typename_type,
14978 /*check_dependency_p=*/true,
14979 /*class_head_p=*/false,
14980 /*is_declaration=*/true);
14981 /* Otherwise, we could also be looking for an ordinary identifier. */
14982 cp_parser_parse_tentatively (parser);
14983 if (cp_lexer_next_token_is_decltype (parser->lexer))
14984 /* Try a decltype-specifier. */
14985 id = cp_parser_decltype (parser);
14986 else
14987 /* Otherwise, try a class-name. */
14988 id = cp_parser_class_name (parser,
14989 /*typename_keyword_p=*/true,
14990 /*template_keyword_p=*/false,
14991 none_type,
14992 /*check_dependency_p=*/true,
14993 /*class_head_p=*/false,
14994 /*is_declaration=*/true);
14995 /* If we found one, we're done. */
14996 if (cp_parser_parse_definitely (parser))
14997 return id;
14998 /* Otherwise, look for an ordinary identifier. */
14999 return cp_parser_identifier (parser);
15000 }
15001
15002 /* Overloading [gram.over] */
15003
15004 /* Parse an operator-function-id.
15005
15006 operator-function-id:
15007 operator operator
15008
15009 Returns an IDENTIFIER_NODE for the operator which is a
15010 human-readable spelling of the identifier, e.g., `operator +'. */
15011
15012 static cp_expr
15013 cp_parser_operator_function_id (cp_parser* parser)
15014 {
15015 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15016 /* Look for the `operator' keyword. */
15017 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15018 return error_mark_node;
15019 /* And then the name of the operator itself. */
15020 return cp_parser_operator (parser, start_loc);
15021 }
15022
15023 /* Return an identifier node for a user-defined literal operator.
15024 The suffix identifier is chained to the operator name identifier. */
15025
15026 tree
15027 cp_literal_operator_id (const char* name)
15028 {
15029 tree identifier;
15030 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15031 + strlen (name) + 10);
15032 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15033 identifier = get_identifier (buffer);
15034
15035 return identifier;
15036 }
15037
15038 /* Parse an operator.
15039
15040 operator:
15041 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15042 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15043 || ++ -- , ->* -> () []
15044
15045 GNU Extensions:
15046
15047 operator:
15048 <? >? <?= >?=
15049
15050 Returns an IDENTIFIER_NODE for the operator which is a
15051 human-readable spelling of the identifier, e.g., `operator +'. */
15052
15053 static cp_expr
15054 cp_parser_operator (cp_parser* parser, location_t start_loc)
15055 {
15056 tree id = NULL_TREE;
15057 cp_token *token;
15058 bool utf8 = false;
15059
15060 /* Peek at the next token. */
15061 token = cp_lexer_peek_token (parser->lexer);
15062
15063 location_t end_loc = token->location;
15064
15065 /* Figure out which operator we have. */
15066 enum tree_code op = ERROR_MARK;
15067 bool assop = false;
15068 bool consumed = false;
15069 switch (token->type)
15070 {
15071 case CPP_KEYWORD:
15072 {
15073 /* The keyword should be either `new' or `delete'. */
15074 if (token->keyword == RID_NEW)
15075 op = NEW_EXPR;
15076 else if (token->keyword == RID_DELETE)
15077 op = DELETE_EXPR;
15078 else
15079 break;
15080
15081 /* Consume the `new' or `delete' token. */
15082 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15083
15084 /* Peek at the next token. */
15085 token = cp_lexer_peek_token (parser->lexer);
15086 /* If it's a `[' token then this is the array variant of the
15087 operator. */
15088 if (token->type == CPP_OPEN_SQUARE)
15089 {
15090 /* Consume the `[' token. */
15091 cp_lexer_consume_token (parser->lexer);
15092 /* Look for the `]' token. */
15093 if (cp_token *close_token
15094 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15095 end_loc = close_token->location;
15096 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15097 }
15098 consumed = true;
15099 break;
15100 }
15101
15102 case CPP_PLUS:
15103 op = PLUS_EXPR;
15104 break;
15105
15106 case CPP_MINUS:
15107 op = MINUS_EXPR;
15108 break;
15109
15110 case CPP_MULT:
15111 op = MULT_EXPR;
15112 break;
15113
15114 case CPP_DIV:
15115 op = TRUNC_DIV_EXPR;
15116 break;
15117
15118 case CPP_MOD:
15119 op = TRUNC_MOD_EXPR;
15120 break;
15121
15122 case CPP_XOR:
15123 op = BIT_XOR_EXPR;
15124 break;
15125
15126 case CPP_AND:
15127 op = BIT_AND_EXPR;
15128 break;
15129
15130 case CPP_OR:
15131 op = BIT_IOR_EXPR;
15132 break;
15133
15134 case CPP_COMPL:
15135 op = BIT_NOT_EXPR;
15136 break;
15137
15138 case CPP_NOT:
15139 op = TRUTH_NOT_EXPR;
15140 break;
15141
15142 case CPP_EQ:
15143 assop = true;
15144 op = NOP_EXPR;
15145 break;
15146
15147 case CPP_LESS:
15148 op = LT_EXPR;
15149 break;
15150
15151 case CPP_GREATER:
15152 op = GT_EXPR;
15153 break;
15154
15155 case CPP_PLUS_EQ:
15156 assop = true;
15157 op = PLUS_EXPR;
15158 break;
15159
15160 case CPP_MINUS_EQ:
15161 assop = true;
15162 op = MINUS_EXPR;
15163 break;
15164
15165 case CPP_MULT_EQ:
15166 assop = true;
15167 op = MULT_EXPR;
15168 break;
15169
15170 case CPP_DIV_EQ:
15171 assop = true;
15172 op = TRUNC_DIV_EXPR;
15173 break;
15174
15175 case CPP_MOD_EQ:
15176 assop = true;
15177 op = TRUNC_MOD_EXPR;
15178 break;
15179
15180 case CPP_XOR_EQ:
15181 assop = true;
15182 op = BIT_XOR_EXPR;
15183 break;
15184
15185 case CPP_AND_EQ:
15186 assop = true;
15187 op = BIT_AND_EXPR;
15188 break;
15189
15190 case CPP_OR_EQ:
15191 assop = true;
15192 op = BIT_IOR_EXPR;
15193 break;
15194
15195 case CPP_LSHIFT:
15196 op = LSHIFT_EXPR;
15197 break;
15198
15199 case CPP_RSHIFT:
15200 op = RSHIFT_EXPR;
15201 break;
15202
15203 case CPP_LSHIFT_EQ:
15204 assop = true;
15205 op = LSHIFT_EXPR;
15206 break;
15207
15208 case CPP_RSHIFT_EQ:
15209 assop = true;
15210 op = RSHIFT_EXPR;
15211 break;
15212
15213 case CPP_EQ_EQ:
15214 op = EQ_EXPR;
15215 break;
15216
15217 case CPP_NOT_EQ:
15218 op = NE_EXPR;
15219 break;
15220
15221 case CPP_LESS_EQ:
15222 op = LE_EXPR;
15223 break;
15224
15225 case CPP_GREATER_EQ:
15226 op = GE_EXPR;
15227 break;
15228
15229 case CPP_AND_AND:
15230 op = TRUTH_ANDIF_EXPR;
15231 break;
15232
15233 case CPP_OR_OR:
15234 op = TRUTH_ORIF_EXPR;
15235 break;
15236
15237 case CPP_PLUS_PLUS:
15238 op = POSTINCREMENT_EXPR;
15239 break;
15240
15241 case CPP_MINUS_MINUS:
15242 op = PREDECREMENT_EXPR;
15243 break;
15244
15245 case CPP_COMMA:
15246 op = COMPOUND_EXPR;
15247 break;
15248
15249 case CPP_DEREF_STAR:
15250 op = MEMBER_REF;
15251 break;
15252
15253 case CPP_DEREF:
15254 op = COMPONENT_REF;
15255 break;
15256
15257 case CPP_OPEN_PAREN:
15258 {
15259 /* Consume the `('. */
15260 matching_parens parens;
15261 parens.consume_open (parser);
15262 /* Look for the matching `)'. */
15263 token = parens.require_close (parser);
15264 if (token)
15265 end_loc = token->location;
15266 op = CALL_EXPR;
15267 consumed = true;
15268 break;
15269 }
15270
15271 case CPP_OPEN_SQUARE:
15272 /* Consume the `['. */
15273 cp_lexer_consume_token (parser->lexer);
15274 /* Look for the matching `]'. */
15275 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15276 if (token)
15277 end_loc = token->location;
15278 op = ARRAY_REF;
15279 consumed = true;
15280 break;
15281
15282 case CPP_UTF8STRING:
15283 case CPP_UTF8STRING_USERDEF:
15284 utf8 = true;
15285 /* FALLTHRU */
15286 case CPP_STRING:
15287 case CPP_WSTRING:
15288 case CPP_STRING16:
15289 case CPP_STRING32:
15290 case CPP_STRING_USERDEF:
15291 case CPP_WSTRING_USERDEF:
15292 case CPP_STRING16_USERDEF:
15293 case CPP_STRING32_USERDEF:
15294 {
15295 cp_expr str;
15296 tree string_tree;
15297 int sz, len;
15298
15299 if (cxx_dialect == cxx98)
15300 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15301
15302 /* Consume the string. */
15303 str = cp_parser_string_literal (parser, /*translate=*/true,
15304 /*wide_ok=*/true, /*lookup_udlit=*/false);
15305 if (str == error_mark_node)
15306 return error_mark_node;
15307 else if (TREE_CODE (str) == USERDEF_LITERAL)
15308 {
15309 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15310 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15311 end_loc = str.get_location ();
15312 }
15313 else
15314 {
15315 string_tree = str;
15316 /* Look for the suffix identifier. */
15317 token = cp_lexer_peek_token (parser->lexer);
15318 if (token->type == CPP_NAME)
15319 {
15320 id = cp_parser_identifier (parser);
15321 end_loc = token->location;
15322 }
15323 else if (token->type == CPP_KEYWORD)
15324 {
15325 error ("unexpected keyword;"
15326 " remove space between quotes and suffix identifier");
15327 return error_mark_node;
15328 }
15329 else
15330 {
15331 error ("expected suffix identifier");
15332 return error_mark_node;
15333 }
15334 }
15335 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15336 (TREE_TYPE (TREE_TYPE (string_tree))));
15337 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15338 if (len != 0)
15339 {
15340 error ("expected empty string after %<operator%> keyword");
15341 return error_mark_node;
15342 }
15343 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15344 != char_type_node)
15345 {
15346 error ("invalid encoding prefix in literal operator");
15347 return error_mark_node;
15348 }
15349 if (id != error_mark_node)
15350 {
15351 const char *name = IDENTIFIER_POINTER (id);
15352 id = cp_literal_operator_id (name);
15353 }
15354 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15355 return cp_expr (id, start_loc);
15356 }
15357
15358 default:
15359 /* Anything else is an error. */
15360 break;
15361 }
15362
15363 /* If we have selected an identifier, we need to consume the
15364 operator token. */
15365 if (op != ERROR_MARK)
15366 {
15367 id = ovl_op_identifier (assop, op);
15368 if (!consumed)
15369 cp_lexer_consume_token (parser->lexer);
15370 }
15371 /* Otherwise, no valid operator name was present. */
15372 else
15373 {
15374 cp_parser_error (parser, "expected operator");
15375 id = error_mark_node;
15376 }
15377
15378 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15379 return cp_expr (id, start_loc);
15380 }
15381
15382 /* Parse a template-declaration.
15383
15384 template-declaration:
15385 export [opt] template < template-parameter-list > declaration
15386
15387 If MEMBER_P is TRUE, this template-declaration occurs within a
15388 class-specifier.
15389
15390 The grammar rule given by the standard isn't correct. What
15391 is really meant is:
15392
15393 template-declaration:
15394 export [opt] template-parameter-list-seq
15395 decl-specifier-seq [opt] init-declarator [opt] ;
15396 export [opt] template-parameter-list-seq
15397 function-definition
15398
15399 template-parameter-list-seq:
15400 template-parameter-list-seq [opt]
15401 template < template-parameter-list >
15402
15403 Concept Extensions:
15404
15405 template-parameter-list-seq:
15406 template < template-parameter-list > requires-clause [opt]
15407
15408 requires-clause:
15409 requires logical-or-expression */
15410
15411 static void
15412 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15413 {
15414 /* Check for `export'. */
15415 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15416 {
15417 /* Consume the `export' token. */
15418 cp_lexer_consume_token (parser->lexer);
15419 /* Warn that we do not support `export'. */
15420 warning (0, "keyword %<export%> not implemented, and will be ignored");
15421 }
15422
15423 cp_parser_template_declaration_after_export (parser, member_p);
15424 }
15425
15426 /* Parse a template-parameter-list.
15427
15428 template-parameter-list:
15429 template-parameter
15430 template-parameter-list , template-parameter
15431
15432 Returns a TREE_LIST. Each node represents a template parameter.
15433 The nodes are connected via their TREE_CHAINs. */
15434
15435 static tree
15436 cp_parser_template_parameter_list (cp_parser* parser)
15437 {
15438 tree parameter_list = NULL_TREE;
15439
15440 begin_template_parm_list ();
15441
15442 /* The loop below parses the template parms. We first need to know
15443 the total number of template parms to be able to compute proper
15444 canonical types of each dependent type. So after the loop, when
15445 we know the total number of template parms,
15446 end_template_parm_list computes the proper canonical types and
15447 fixes up the dependent types accordingly. */
15448 while (true)
15449 {
15450 tree parameter;
15451 bool is_non_type;
15452 bool is_parameter_pack;
15453 location_t parm_loc;
15454
15455 /* Parse the template-parameter. */
15456 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15457 parameter = cp_parser_template_parameter (parser,
15458 &is_non_type,
15459 &is_parameter_pack);
15460 /* Add it to the list. */
15461 if (parameter != error_mark_node)
15462 parameter_list = process_template_parm (parameter_list,
15463 parm_loc,
15464 parameter,
15465 is_non_type,
15466 is_parameter_pack);
15467 else
15468 {
15469 tree err_parm = build_tree_list (parameter, parameter);
15470 parameter_list = chainon (parameter_list, err_parm);
15471 }
15472
15473 /* If the next token is not a `,', we're done. */
15474 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15475 break;
15476 /* Otherwise, consume the `,' token. */
15477 cp_lexer_consume_token (parser->lexer);
15478 }
15479
15480 return end_template_parm_list (parameter_list);
15481 }
15482
15483 /* Parse a introduction-list.
15484
15485 introduction-list:
15486 introduced-parameter
15487 introduction-list , introduced-parameter
15488
15489 introduced-parameter:
15490 ...[opt] identifier
15491
15492 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15493 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15494 WILDCARD_DECL will also have DECL_NAME set and token location in
15495 DECL_SOURCE_LOCATION. */
15496
15497 static tree
15498 cp_parser_introduction_list (cp_parser *parser)
15499 {
15500 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15501
15502 while (true)
15503 {
15504 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15505 if (is_pack)
15506 cp_lexer_consume_token (parser->lexer);
15507
15508 tree identifier = cp_parser_identifier (parser);
15509 if (identifier == error_mark_node)
15510 break;
15511
15512 /* Build placeholder. */
15513 tree parm = build_nt (WILDCARD_DECL);
15514 DECL_SOURCE_LOCATION (parm)
15515 = cp_lexer_peek_token (parser->lexer)->location;
15516 DECL_NAME (parm) = identifier;
15517 WILDCARD_PACK_P (parm) = is_pack;
15518 vec_safe_push (introduction_vec, parm);
15519
15520 /* If the next token is not a `,', we're done. */
15521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15522 break;
15523 /* Otherwise, consume the `,' token. */
15524 cp_lexer_consume_token (parser->lexer);
15525 }
15526
15527 /* Convert the vec into a TREE_VEC. */
15528 tree introduction_list = make_tree_vec (introduction_vec->length ());
15529 unsigned int n;
15530 tree parm;
15531 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15532 TREE_VEC_ELT (introduction_list, n) = parm;
15533
15534 release_tree_vector (introduction_vec);
15535 return introduction_list;
15536 }
15537
15538 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15539 is an abstract declarator. */
15540
15541 static inline cp_declarator*
15542 get_id_declarator (cp_declarator *declarator)
15543 {
15544 cp_declarator *d = declarator;
15545 while (d && d->kind != cdk_id)
15546 d = d->declarator;
15547 return d;
15548 }
15549
15550 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15551 is an abstract declarator. */
15552
15553 static inline tree
15554 get_unqualified_id (cp_declarator *declarator)
15555 {
15556 declarator = get_id_declarator (declarator);
15557 if (declarator)
15558 return declarator->u.id.unqualified_name;
15559 else
15560 return NULL_TREE;
15561 }
15562
15563 /* Returns true if DECL represents a constrained-parameter. */
15564
15565 static inline bool
15566 is_constrained_parameter (tree decl)
15567 {
15568 return (decl
15569 && TREE_CODE (decl) == TYPE_DECL
15570 && CONSTRAINED_PARM_CONCEPT (decl)
15571 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15572 }
15573
15574 /* Returns true if PARM declares a constrained-parameter. */
15575
15576 static inline bool
15577 is_constrained_parameter (cp_parameter_declarator *parm)
15578 {
15579 return is_constrained_parameter (parm->decl_specifiers.type);
15580 }
15581
15582 /* Check that the type parameter is only a declarator-id, and that its
15583 type is not cv-qualified. */
15584
15585 bool
15586 cp_parser_check_constrained_type_parm (cp_parser *parser,
15587 cp_parameter_declarator *parm)
15588 {
15589 if (!parm->declarator)
15590 return true;
15591
15592 if (parm->declarator->kind != cdk_id)
15593 {
15594 cp_parser_error (parser, "invalid constrained type parameter");
15595 return false;
15596 }
15597
15598 /* Don't allow cv-qualified type parameters. */
15599 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15600 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15601 {
15602 cp_parser_error (parser, "cv-qualified type parameter");
15603 return false;
15604 }
15605
15606 return true;
15607 }
15608
15609 /* Finish parsing/processing a template type parameter and checking
15610 various restrictions. */
15611
15612 static inline tree
15613 cp_parser_constrained_type_template_parm (cp_parser *parser,
15614 tree id,
15615 cp_parameter_declarator* parmdecl)
15616 {
15617 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15618 return finish_template_type_parm (class_type_node, id);
15619 else
15620 return error_mark_node;
15621 }
15622
15623 static tree
15624 finish_constrained_template_template_parm (tree proto, tree id)
15625 {
15626 /* FIXME: This should probably be copied, and we may need to adjust
15627 the template parameter depths. */
15628 tree saved_parms = current_template_parms;
15629 begin_template_parm_list ();
15630 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15631 end_template_parm_list ();
15632
15633 tree parm = finish_template_template_parm (class_type_node, id);
15634 current_template_parms = saved_parms;
15635
15636 return parm;
15637 }
15638
15639 /* Finish parsing/processing a template template parameter by borrowing
15640 the template parameter list from the prototype parameter. */
15641
15642 static tree
15643 cp_parser_constrained_template_template_parm (cp_parser *parser,
15644 tree proto,
15645 tree id,
15646 cp_parameter_declarator *parmdecl)
15647 {
15648 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15649 return error_mark_node;
15650 return finish_constrained_template_template_parm (proto, id);
15651 }
15652
15653 /* Create a new non-type template parameter from the given PARM
15654 declarator. */
15655
15656 static tree
15657 constrained_non_type_template_parm (bool *is_non_type,
15658 cp_parameter_declarator *parm)
15659 {
15660 *is_non_type = true;
15661 cp_declarator *decl = parm->declarator;
15662 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15663 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15664 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15665 }
15666
15667 /* Build a constrained template parameter based on the PARMDECL
15668 declarator. The type of PARMDECL is the constrained type, which
15669 refers to the prototype template parameter that ultimately
15670 specifies the type of the declared parameter. */
15671
15672 static tree
15673 finish_constrained_parameter (cp_parser *parser,
15674 cp_parameter_declarator *parmdecl,
15675 bool *is_non_type,
15676 bool *is_parameter_pack)
15677 {
15678 tree decl = parmdecl->decl_specifiers.type;
15679 tree id = get_unqualified_id (parmdecl->declarator);
15680 tree def = parmdecl->default_argument;
15681 tree proto = DECL_INITIAL (decl);
15682
15683 /* A template parameter constrained by a variadic concept shall also
15684 be declared as a template parameter pack. */
15685 bool is_variadic = template_parameter_pack_p (proto);
15686 if (is_variadic && !*is_parameter_pack)
15687 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15688
15689 /* Build the parameter. Return an error if the declarator was invalid. */
15690 tree parm;
15691 if (TREE_CODE (proto) == TYPE_DECL)
15692 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15693 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15694 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15695 parmdecl);
15696 else
15697 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15698 if (parm == error_mark_node)
15699 return error_mark_node;
15700
15701 /* Finish the parameter decl and create a node attaching the
15702 default argument and constraint. */
15703 parm = build_tree_list (def, parm);
15704 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15705
15706 return parm;
15707 }
15708
15709 /* Returns true if the parsed type actually represents the declaration
15710 of a type template-parameter. */
15711
15712 static inline bool
15713 declares_constrained_type_template_parameter (tree type)
15714 {
15715 return (is_constrained_parameter (type)
15716 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15717 }
15718
15719
15720 /* Returns true if the parsed type actually represents the declaration of
15721 a template template-parameter. */
15722
15723 static bool
15724 declares_constrained_template_template_parameter (tree type)
15725 {
15726 return (is_constrained_parameter (type)
15727 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15728 }
15729
15730 /* Parse a default argument for a type template-parameter.
15731 Note that diagnostics are handled in cp_parser_template_parameter. */
15732
15733 static tree
15734 cp_parser_default_type_template_argument (cp_parser *parser)
15735 {
15736 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15737
15738 /* Consume the `=' token. */
15739 cp_lexer_consume_token (parser->lexer);
15740
15741 cp_token *token = cp_lexer_peek_token (parser->lexer);
15742
15743 /* Parse the default-argument. */
15744 push_deferring_access_checks (dk_no_deferred);
15745 tree default_argument = cp_parser_type_id (parser);
15746 pop_deferring_access_checks ();
15747
15748 if (flag_concepts && type_uses_auto (default_argument))
15749 {
15750 error_at (token->location,
15751 "invalid use of %<auto%> in default template argument");
15752 return error_mark_node;
15753 }
15754
15755 return default_argument;
15756 }
15757
15758 /* Parse a default argument for a template template-parameter. */
15759
15760 static tree
15761 cp_parser_default_template_template_argument (cp_parser *parser)
15762 {
15763 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15764
15765 bool is_template;
15766
15767 /* Consume the `='. */
15768 cp_lexer_consume_token (parser->lexer);
15769 /* Parse the id-expression. */
15770 push_deferring_access_checks (dk_no_deferred);
15771 /* save token before parsing the id-expression, for error
15772 reporting */
15773 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15774 tree default_argument
15775 = cp_parser_id_expression (parser,
15776 /*template_keyword_p=*/false,
15777 /*check_dependency_p=*/true,
15778 /*template_p=*/&is_template,
15779 /*declarator_p=*/false,
15780 /*optional_p=*/false);
15781 if (TREE_CODE (default_argument) == TYPE_DECL)
15782 /* If the id-expression was a template-id that refers to
15783 a template-class, we already have the declaration here,
15784 so no further lookup is needed. */
15785 ;
15786 else
15787 /* Look up the name. */
15788 default_argument
15789 = cp_parser_lookup_name (parser, default_argument,
15790 none_type,
15791 /*is_template=*/is_template,
15792 /*is_namespace=*/false,
15793 /*check_dependency=*/true,
15794 /*ambiguous_decls=*/NULL,
15795 token->location);
15796 /* See if the default argument is valid. */
15797 default_argument = check_template_template_default_arg (default_argument);
15798 pop_deferring_access_checks ();
15799 return default_argument;
15800 }
15801
15802 /* Parse a template-parameter.
15803
15804 template-parameter:
15805 type-parameter
15806 parameter-declaration
15807
15808 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15809 the parameter. The TREE_PURPOSE is the default value, if any.
15810 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15811 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15812 set to true iff this parameter is a parameter pack. */
15813
15814 static tree
15815 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15816 bool *is_parameter_pack)
15817 {
15818 cp_token *token;
15819 cp_parameter_declarator *parameter_declarator;
15820 tree parm;
15821
15822 /* Assume it is a type parameter or a template parameter. */
15823 *is_non_type = false;
15824 /* Assume it not a parameter pack. */
15825 *is_parameter_pack = false;
15826 /* Peek at the next token. */
15827 token = cp_lexer_peek_token (parser->lexer);
15828 /* If it is `template', we have a type-parameter. */
15829 if (token->keyword == RID_TEMPLATE)
15830 return cp_parser_type_parameter (parser, is_parameter_pack);
15831 /* If it is `class' or `typename' we do not know yet whether it is a
15832 type parameter or a non-type parameter. Consider:
15833
15834 template <typename T, typename T::X X> ...
15835
15836 or:
15837
15838 template <class C, class D*> ...
15839
15840 Here, the first parameter is a type parameter, and the second is
15841 a non-type parameter. We can tell by looking at the token after
15842 the identifier -- if it is a `,', `=', or `>' then we have a type
15843 parameter. */
15844 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15845 {
15846 /* Peek at the token after `class' or `typename'. */
15847 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15848 /* If it's an ellipsis, we have a template type parameter
15849 pack. */
15850 if (token->type == CPP_ELLIPSIS)
15851 return cp_parser_type_parameter (parser, is_parameter_pack);
15852 /* If it's an identifier, skip it. */
15853 if (token->type == CPP_NAME)
15854 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15855 /* Now, see if the token looks like the end of a template
15856 parameter. */
15857 if (token->type == CPP_COMMA
15858 || token->type == CPP_EQ
15859 || token->type == CPP_GREATER)
15860 return cp_parser_type_parameter (parser, is_parameter_pack);
15861 }
15862
15863 /* Otherwise, it is a non-type parameter or a constrained parameter.
15864
15865 [temp.param]
15866
15867 When parsing a default template-argument for a non-type
15868 template-parameter, the first non-nested `>' is taken as the end
15869 of the template parameter-list rather than a greater-than
15870 operator. */
15871 parameter_declarator
15872 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15873 /*parenthesized_p=*/NULL);
15874
15875 if (!parameter_declarator)
15876 return error_mark_node;
15877
15878 /* If the parameter declaration is marked as a parameter pack, set
15879 *IS_PARAMETER_PACK to notify the caller. */
15880 if (parameter_declarator->template_parameter_pack_p)
15881 *is_parameter_pack = true;
15882
15883 if (parameter_declarator->default_argument)
15884 {
15885 /* Can happen in some cases of erroneous input (c++/34892). */
15886 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15887 /* Consume the `...' for better error recovery. */
15888 cp_lexer_consume_token (parser->lexer);
15889 }
15890
15891 // The parameter may have been constrained.
15892 if (is_constrained_parameter (parameter_declarator))
15893 return finish_constrained_parameter (parser,
15894 parameter_declarator,
15895 is_non_type,
15896 is_parameter_pack);
15897
15898 // Now we're sure that the parameter is a non-type parameter.
15899 *is_non_type = true;
15900
15901 parm = grokdeclarator (parameter_declarator->declarator,
15902 &parameter_declarator->decl_specifiers,
15903 TPARM, /*initialized=*/0,
15904 /*attrlist=*/NULL);
15905 if (parm == error_mark_node)
15906 return error_mark_node;
15907
15908 return build_tree_list (parameter_declarator->default_argument, parm);
15909 }
15910
15911 /* Parse a type-parameter.
15912
15913 type-parameter:
15914 class identifier [opt]
15915 class identifier [opt] = type-id
15916 typename identifier [opt]
15917 typename identifier [opt] = type-id
15918 template < template-parameter-list > class identifier [opt]
15919 template < template-parameter-list > class identifier [opt]
15920 = id-expression
15921
15922 GNU Extension (variadic templates):
15923
15924 type-parameter:
15925 class ... identifier [opt]
15926 typename ... identifier [opt]
15927
15928 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15929 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15930 the declaration of the parameter.
15931
15932 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15933
15934 static tree
15935 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15936 {
15937 cp_token *token;
15938 tree parameter;
15939
15940 /* Look for a keyword to tell us what kind of parameter this is. */
15941 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15942 if (!token)
15943 return error_mark_node;
15944
15945 switch (token->keyword)
15946 {
15947 case RID_CLASS:
15948 case RID_TYPENAME:
15949 {
15950 tree identifier;
15951 tree default_argument;
15952
15953 /* If the next token is an ellipsis, we have a template
15954 argument pack. */
15955 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15956 {
15957 /* Consume the `...' token. */
15958 cp_lexer_consume_token (parser->lexer);
15959 maybe_warn_variadic_templates ();
15960
15961 *is_parameter_pack = true;
15962 }
15963
15964 /* If the next token is an identifier, then it names the
15965 parameter. */
15966 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15967 identifier = cp_parser_identifier (parser);
15968 else
15969 identifier = NULL_TREE;
15970
15971 /* Create the parameter. */
15972 parameter = finish_template_type_parm (class_type_node, identifier);
15973
15974 /* If the next token is an `=', we have a default argument. */
15975 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15976 {
15977 default_argument
15978 = cp_parser_default_type_template_argument (parser);
15979
15980 /* Template parameter packs cannot have default
15981 arguments. */
15982 if (*is_parameter_pack)
15983 {
15984 if (identifier)
15985 error_at (token->location,
15986 "template parameter pack %qD cannot have a "
15987 "default argument", identifier);
15988 else
15989 error_at (token->location,
15990 "template parameter packs cannot have "
15991 "default arguments");
15992 default_argument = NULL_TREE;
15993 }
15994 else if (check_for_bare_parameter_packs (default_argument))
15995 default_argument = error_mark_node;
15996 }
15997 else
15998 default_argument = NULL_TREE;
15999
16000 /* Create the combined representation of the parameter and the
16001 default argument. */
16002 parameter = build_tree_list (default_argument, parameter);
16003 }
16004 break;
16005
16006 case RID_TEMPLATE:
16007 {
16008 tree identifier;
16009 tree default_argument;
16010
16011 /* Look for the `<'. */
16012 cp_parser_require (parser, CPP_LESS, RT_LESS);
16013 /* Parse the template-parameter-list. */
16014 cp_parser_template_parameter_list (parser);
16015 /* Look for the `>'. */
16016 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16017
16018 // If template requirements are present, parse them.
16019 if (flag_concepts)
16020 {
16021 tree reqs = get_shorthand_constraints (current_template_parms);
16022 if (tree r = cp_parser_requires_clause_opt (parser))
16023 reqs = conjoin_constraints (reqs, normalize_expression (r));
16024 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16025 }
16026
16027 /* Look for the `class' or 'typename' keywords. */
16028 cp_parser_type_parameter_key (parser);
16029 /* If the next token is an ellipsis, we have a template
16030 argument pack. */
16031 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16032 {
16033 /* Consume the `...' token. */
16034 cp_lexer_consume_token (parser->lexer);
16035 maybe_warn_variadic_templates ();
16036
16037 *is_parameter_pack = true;
16038 }
16039 /* If the next token is an `=', then there is a
16040 default-argument. If the next token is a `>', we are at
16041 the end of the parameter-list. If the next token is a `,',
16042 then we are at the end of this parameter. */
16043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16044 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16045 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16046 {
16047 identifier = cp_parser_identifier (parser);
16048 /* Treat invalid names as if the parameter were nameless. */
16049 if (identifier == error_mark_node)
16050 identifier = NULL_TREE;
16051 }
16052 else
16053 identifier = NULL_TREE;
16054
16055 /* Create the template parameter. */
16056 parameter = finish_template_template_parm (class_type_node,
16057 identifier);
16058
16059 /* If the next token is an `=', then there is a
16060 default-argument. */
16061 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16062 {
16063 default_argument
16064 = cp_parser_default_template_template_argument (parser);
16065
16066 /* Template parameter packs cannot have default
16067 arguments. */
16068 if (*is_parameter_pack)
16069 {
16070 if (identifier)
16071 error_at (token->location,
16072 "template parameter pack %qD cannot "
16073 "have a default argument",
16074 identifier);
16075 else
16076 error_at (token->location, "template parameter packs cannot "
16077 "have default arguments");
16078 default_argument = NULL_TREE;
16079 }
16080 }
16081 else
16082 default_argument = NULL_TREE;
16083
16084 /* Create the combined representation of the parameter and the
16085 default argument. */
16086 parameter = build_tree_list (default_argument, parameter);
16087 }
16088 break;
16089
16090 default:
16091 gcc_unreachable ();
16092 break;
16093 }
16094
16095 return parameter;
16096 }
16097
16098 /* Parse a template-id.
16099
16100 template-id:
16101 template-name < template-argument-list [opt] >
16102
16103 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16104 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16105 returned. Otherwise, if the template-name names a function, or set
16106 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16107 names a class, returns a TYPE_DECL for the specialization.
16108
16109 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16110 uninstantiated templates. */
16111
16112 static tree
16113 cp_parser_template_id (cp_parser *parser,
16114 bool template_keyword_p,
16115 bool check_dependency_p,
16116 enum tag_types tag_type,
16117 bool is_declaration)
16118 {
16119 tree templ;
16120 tree arguments;
16121 tree template_id;
16122 cp_token_position start_of_id = 0;
16123 cp_token *next_token = NULL, *next_token_2 = NULL;
16124 bool is_identifier;
16125
16126 /* If the next token corresponds to a template-id, there is no need
16127 to reparse it. */
16128 cp_token *token = cp_lexer_peek_token (parser->lexer);
16129 if (token->type == CPP_TEMPLATE_ID)
16130 {
16131 cp_lexer_consume_token (parser->lexer);
16132 return saved_checks_value (token->u.tree_check_value);
16133 }
16134
16135 /* Avoid performing name lookup if there is no possibility of
16136 finding a template-id. */
16137 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16138 || (token->type == CPP_NAME
16139 && !cp_parser_nth_token_starts_template_argument_list_p
16140 (parser, 2)))
16141 {
16142 cp_parser_error (parser, "expected template-id");
16143 return error_mark_node;
16144 }
16145
16146 /* Remember where the template-id starts. */
16147 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16148 start_of_id = cp_lexer_token_position (parser->lexer, false);
16149
16150 push_deferring_access_checks (dk_deferred);
16151
16152 /* Parse the template-name. */
16153 is_identifier = false;
16154 templ = cp_parser_template_name (parser, template_keyword_p,
16155 check_dependency_p,
16156 is_declaration,
16157 tag_type,
16158 &is_identifier);
16159 if (templ == error_mark_node || is_identifier)
16160 {
16161 pop_deferring_access_checks ();
16162 return templ;
16163 }
16164
16165 /* Since we're going to preserve any side-effects from this parse, set up a
16166 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16167 in the template arguments. */
16168 tentative_firewall firewall (parser);
16169
16170 /* If we find the sequence `[:' after a template-name, it's probably
16171 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16172 parse correctly the argument list. */
16173 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16174 == CPP_OPEN_SQUARE)
16175 && next_token->flags & DIGRAPH
16176 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16177 == CPP_COLON)
16178 && !(next_token_2->flags & PREV_WHITE))
16179 {
16180 cp_parser_parse_tentatively (parser);
16181 /* Change `:' into `::'. */
16182 next_token_2->type = CPP_SCOPE;
16183 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16184 CPP_LESS. */
16185 cp_lexer_consume_token (parser->lexer);
16186
16187 /* Parse the arguments. */
16188 arguments = cp_parser_enclosed_template_argument_list (parser);
16189 if (!cp_parser_parse_definitely (parser))
16190 {
16191 /* If we couldn't parse an argument list, then we revert our changes
16192 and return simply an error. Maybe this is not a template-id
16193 after all. */
16194 next_token_2->type = CPP_COLON;
16195 cp_parser_error (parser, "expected %<<%>");
16196 pop_deferring_access_checks ();
16197 return error_mark_node;
16198 }
16199 /* Otherwise, emit an error about the invalid digraph, but continue
16200 parsing because we got our argument list. */
16201 if (permerror (next_token->location,
16202 "%<<::%> cannot begin a template-argument list"))
16203 {
16204 static bool hint = false;
16205 inform (next_token->location,
16206 "%<<:%> is an alternate spelling for %<[%>."
16207 " Insert whitespace between %<<%> and %<::%>");
16208 if (!hint && !flag_permissive)
16209 {
16210 inform (next_token->location, "(if you use %<-fpermissive%> "
16211 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16212 "accept your code)");
16213 hint = true;
16214 }
16215 }
16216 }
16217 else
16218 {
16219 /* Look for the `<' that starts the template-argument-list. */
16220 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16221 {
16222 pop_deferring_access_checks ();
16223 return error_mark_node;
16224 }
16225 /* Parse the arguments. */
16226 arguments = cp_parser_enclosed_template_argument_list (parser);
16227
16228 if ((cxx_dialect > cxx17)
16229 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16230 && !template_keyword_p
16231 && (cp_parser_error_occurred (parser)
16232 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16233 {
16234 /* This didn't go well. */
16235 if (TREE_CODE (templ) == FUNCTION_DECL)
16236 {
16237 /* C++2A says that "function-name < a;" is now ill-formed. */
16238 if (cp_parser_error_occurred (parser))
16239 {
16240 error_at (token->location, "invalid template-argument-list");
16241 inform (token->location, "function name as the left hand "
16242 "operand of %<<%> is ill-formed in C++2a; wrap the "
16243 "function name in %<()%>");
16244 }
16245 else
16246 /* We expect "f<targs>" to be followed by "(args)". */
16247 error_at (cp_lexer_peek_token (parser->lexer)->location,
16248 "expected %<(%> after template-argument-list");
16249 if (start_of_id)
16250 /* Purge all subsequent tokens. */
16251 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16252 }
16253 else
16254 cp_parser_simulate_error (parser);
16255 pop_deferring_access_checks ();
16256 return error_mark_node;
16257 }
16258 }
16259
16260 /* Set the location to be of the form:
16261 template-name < template-argument-list [opt] >
16262 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16263 with caret == start at the start of the template-name,
16264 ranging until the closing '>'. */
16265 location_t finish_loc
16266 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16267 location_t combined_loc
16268 = make_location (token->location, token->location, finish_loc);
16269
16270 /* Check for concepts autos where they don't belong. We could
16271 identify types in some cases of idnetifier TEMPL, looking ahead
16272 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16273 types. We reject them in functions, but if what we have is an
16274 identifier, even with none_type we can't conclude it's NOT a
16275 type, we have to wait for template substitution. */
16276 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16277 template_id = error_mark_node;
16278 /* Build a representation of the specialization. */
16279 else if (identifier_p (templ))
16280 template_id = build_min_nt_loc (combined_loc,
16281 TEMPLATE_ID_EXPR,
16282 templ, arguments);
16283 else if (DECL_TYPE_TEMPLATE_P (templ)
16284 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16285 {
16286 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16287 template (rather than some instantiation thereof) only if
16288 is not nested within some other construct. For example, in
16289 "template <typename T> void f(T) { A<T>::", A<T> is just an
16290 instantiation of A. */
16291 bool entering_scope
16292 = (template_parm_scope_p ()
16293 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16294 template_id
16295 = finish_template_type (templ, arguments, entering_scope);
16296 }
16297 /* A template-like identifier may be a partial concept id. */
16298 else if (flag_concepts
16299 && (template_id = (cp_parser_maybe_partial_concept_id
16300 (parser, templ, arguments))))
16301 return template_id;
16302 else if (variable_template_p (templ))
16303 {
16304 template_id = lookup_template_variable (templ, arguments);
16305 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16306 SET_EXPR_LOCATION (template_id, combined_loc);
16307 }
16308 else
16309 {
16310 /* If it's not a class-template or a template-template, it should be
16311 a function-template. */
16312 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16313 || TREE_CODE (templ) == OVERLOAD
16314 || TREE_CODE (templ) == FUNCTION_DECL
16315 || BASELINK_P (templ)));
16316
16317 template_id = lookup_template_function (templ, arguments);
16318 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16319 SET_EXPR_LOCATION (template_id, combined_loc);
16320 }
16321
16322 /* If parsing tentatively, replace the sequence of tokens that makes
16323 up the template-id with a CPP_TEMPLATE_ID token. That way,
16324 should we re-parse the token stream, we will not have to repeat
16325 the effort required to do the parse, nor will we issue duplicate
16326 error messages about problems during instantiation of the
16327 template. */
16328 if (start_of_id
16329 /* Don't do this if we had a parse error in a declarator; re-parsing
16330 might succeed if a name changes meaning (60361). */
16331 && !(cp_parser_error_occurred (parser)
16332 && cp_parser_parsing_tentatively (parser)
16333 && parser->in_declarator_p))
16334 {
16335 /* Reset the contents of the START_OF_ID token. */
16336 token->type = CPP_TEMPLATE_ID;
16337 token->location = combined_loc;
16338
16339 /* Retrieve any deferred checks. Do not pop this access checks yet
16340 so the memory will not be reclaimed during token replacing below. */
16341 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16342 token->u.tree_check_value->value = template_id;
16343 token->u.tree_check_value->checks = get_deferred_access_checks ();
16344 token->keyword = RID_MAX;
16345
16346 /* Purge all subsequent tokens. */
16347 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16348
16349 /* ??? Can we actually assume that, if template_id ==
16350 error_mark_node, we will have issued a diagnostic to the
16351 user, as opposed to simply marking the tentative parse as
16352 failed? */
16353 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16354 error_at (token->location, "parse error in template argument list");
16355 }
16356
16357 pop_to_parent_deferring_access_checks ();
16358 return template_id;
16359 }
16360
16361 /* Parse a template-name.
16362
16363 template-name:
16364 identifier
16365
16366 The standard should actually say:
16367
16368 template-name:
16369 identifier
16370 operator-function-id
16371
16372 A defect report has been filed about this issue.
16373
16374 A conversion-function-id cannot be a template name because they cannot
16375 be part of a template-id. In fact, looking at this code:
16376
16377 a.operator K<int>()
16378
16379 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16380 It is impossible to call a templated conversion-function-id with an
16381 explicit argument list, since the only allowed template parameter is
16382 the type to which it is converting.
16383
16384 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16385 `template' keyword, in a construction like:
16386
16387 T::template f<3>()
16388
16389 In that case `f' is taken to be a template-name, even though there
16390 is no way of knowing for sure.
16391
16392 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16393 name refers to a set of overloaded functions, at least one of which
16394 is a template, or an IDENTIFIER_NODE with the name of the template,
16395 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16396 names are looked up inside uninstantiated templates. */
16397
16398 static tree
16399 cp_parser_template_name (cp_parser* parser,
16400 bool template_keyword_p,
16401 bool check_dependency_p,
16402 bool is_declaration,
16403 enum tag_types tag_type,
16404 bool *is_identifier)
16405 {
16406 tree identifier;
16407 tree decl;
16408 cp_token *token = cp_lexer_peek_token (parser->lexer);
16409
16410 /* If the next token is `operator', then we have either an
16411 operator-function-id or a conversion-function-id. */
16412 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16413 {
16414 /* We don't know whether we're looking at an
16415 operator-function-id or a conversion-function-id. */
16416 cp_parser_parse_tentatively (parser);
16417 /* Try an operator-function-id. */
16418 identifier = cp_parser_operator_function_id (parser);
16419 /* If that didn't work, try a conversion-function-id. */
16420 if (!cp_parser_parse_definitely (parser))
16421 {
16422 cp_parser_error (parser, "expected template-name");
16423 return error_mark_node;
16424 }
16425 }
16426 /* Look for the identifier. */
16427 else
16428 identifier = cp_parser_identifier (parser);
16429
16430 /* If we didn't find an identifier, we don't have a template-id. */
16431 if (identifier == error_mark_node)
16432 return error_mark_node;
16433
16434 /* If the name immediately followed the `template' keyword, then it
16435 is a template-name. However, if the next token is not `<', then
16436 we do not treat it as a template-name, since it is not being used
16437 as part of a template-id. This enables us to handle constructs
16438 like:
16439
16440 template <typename T> struct S { S(); };
16441 template <typename T> S<T>::S();
16442
16443 correctly. We would treat `S' as a template -- if it were `S<T>'
16444 -- but we do not if there is no `<'. */
16445
16446 if (processing_template_decl
16447 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16448 {
16449 /* In a declaration, in a dependent context, we pretend that the
16450 "template" keyword was present in order to improve error
16451 recovery. For example, given:
16452
16453 template <typename T> void f(T::X<int>);
16454
16455 we want to treat "X<int>" as a template-id. */
16456 if (is_declaration
16457 && !template_keyword_p
16458 && parser->scope && TYPE_P (parser->scope)
16459 && check_dependency_p
16460 && dependent_scope_p (parser->scope)
16461 /* Do not do this for dtors (or ctors), since they never
16462 need the template keyword before their name. */
16463 && !constructor_name_p (identifier, parser->scope))
16464 {
16465 cp_token_position start = 0;
16466
16467 /* Explain what went wrong. */
16468 error_at (token->location, "non-template %qD used as template",
16469 identifier);
16470 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16471 parser->scope, identifier);
16472 /* If parsing tentatively, find the location of the "<" token. */
16473 if (cp_parser_simulate_error (parser))
16474 start = cp_lexer_token_position (parser->lexer, true);
16475 /* Parse the template arguments so that we can issue error
16476 messages about them. */
16477 cp_lexer_consume_token (parser->lexer);
16478 cp_parser_enclosed_template_argument_list (parser);
16479 /* Skip tokens until we find a good place from which to
16480 continue parsing. */
16481 cp_parser_skip_to_closing_parenthesis (parser,
16482 /*recovering=*/true,
16483 /*or_comma=*/true,
16484 /*consume_paren=*/false);
16485 /* If parsing tentatively, permanently remove the
16486 template argument list. That will prevent duplicate
16487 error messages from being issued about the missing
16488 "template" keyword. */
16489 if (start)
16490 cp_lexer_purge_tokens_after (parser->lexer, start);
16491 if (is_identifier)
16492 *is_identifier = true;
16493 parser->context->object_type = NULL_TREE;
16494 return identifier;
16495 }
16496
16497 /* If the "template" keyword is present, then there is generally
16498 no point in doing name-lookup, so we just return IDENTIFIER.
16499 But, if the qualifying scope is non-dependent then we can
16500 (and must) do name-lookup normally. */
16501 if (template_keyword_p)
16502 {
16503 tree scope = (parser->scope ? parser->scope
16504 : parser->context->object_type);
16505 if (scope && TYPE_P (scope)
16506 && (!CLASS_TYPE_P (scope)
16507 || (check_dependency_p && dependent_type_p (scope))))
16508 {
16509 /* We're optimizing away the call to cp_parser_lookup_name, but
16510 we still need to do this. */
16511 parser->context->object_type = NULL_TREE;
16512 return identifier;
16513 }
16514 }
16515 }
16516
16517 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16518 const bool scoped_p = ((parser->scope ? parser->scope
16519 : parser->context->object_type) != NULL_TREE);
16520
16521 /* Look up the name. */
16522 decl = cp_parser_lookup_name (parser, identifier,
16523 tag_type,
16524 /*is_template=*/true,
16525 /*is_namespace=*/false,
16526 check_dependency_p,
16527 /*ambiguous_decls=*/NULL,
16528 token->location);
16529
16530 decl = strip_using_decl (decl);
16531
16532 /* If DECL is a template, then the name was a template-name. */
16533 if (TREE_CODE (decl) == TEMPLATE_DECL)
16534 {
16535 if (TREE_DEPRECATED (decl)
16536 && deprecated_state != DEPRECATED_SUPPRESS)
16537 warn_deprecated_use (decl, NULL_TREE);
16538 }
16539 else
16540 {
16541 /* The standard does not explicitly indicate whether a name that
16542 names a set of overloaded declarations, some of which are
16543 templates, is a template-name. However, such a name should
16544 be a template-name; otherwise, there is no way to form a
16545 template-id for the overloaded templates. */
16546 bool found = false;
16547
16548 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16549 !found && iter; ++iter)
16550 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16551 found = true;
16552
16553 if (!found
16554 && (cxx_dialect > cxx17)
16555 && !scoped_p
16556 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16557 {
16558 /* [temp.names] says "A name is also considered to refer to a template
16559 if it is an unqualified-id followed by a < and name lookup finds
16560 either one or more functions or finds nothing." */
16561
16562 /* The "more functions" case. Just use the OVERLOAD as normally.
16563 We don't use is_overloaded_fn here to avoid considering
16564 BASELINKs. */
16565 if (TREE_CODE (decl) == OVERLOAD
16566 /* Name lookup found one function. */
16567 || TREE_CODE (decl) == FUNCTION_DECL)
16568 found = true;
16569 /* Name lookup found nothing. */
16570 else if (decl == error_mark_node)
16571 return identifier;
16572 }
16573
16574 if (!found)
16575 {
16576 /* The name does not name a template. */
16577 cp_parser_error (parser, "expected template-name");
16578 return error_mark_node;
16579 }
16580 }
16581
16582 return decl;
16583 }
16584
16585 /* Parse a template-argument-list.
16586
16587 template-argument-list:
16588 template-argument ... [opt]
16589 template-argument-list , template-argument ... [opt]
16590
16591 Returns a TREE_VEC containing the arguments. */
16592
16593 static tree
16594 cp_parser_template_argument_list (cp_parser* parser)
16595 {
16596 tree fixed_args[10];
16597 unsigned n_args = 0;
16598 unsigned alloced = 10;
16599 tree *arg_ary = fixed_args;
16600 tree vec;
16601 bool saved_in_template_argument_list_p;
16602 bool saved_ice_p;
16603 bool saved_non_ice_p;
16604
16605 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16606 parser->in_template_argument_list_p = true;
16607 /* Even if the template-id appears in an integral
16608 constant-expression, the contents of the argument list do
16609 not. */
16610 saved_ice_p = parser->integral_constant_expression_p;
16611 parser->integral_constant_expression_p = false;
16612 saved_non_ice_p = parser->non_integral_constant_expression_p;
16613 parser->non_integral_constant_expression_p = false;
16614
16615 /* Parse the arguments. */
16616 do
16617 {
16618 tree argument;
16619
16620 if (n_args)
16621 /* Consume the comma. */
16622 cp_lexer_consume_token (parser->lexer);
16623
16624 /* Parse the template-argument. */
16625 argument = cp_parser_template_argument (parser);
16626
16627 /* If the next token is an ellipsis, we're expanding a template
16628 argument pack. */
16629 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16630 {
16631 if (argument == error_mark_node)
16632 {
16633 cp_token *token = cp_lexer_peek_token (parser->lexer);
16634 error_at (token->location,
16635 "expected parameter pack before %<...%>");
16636 }
16637 /* Consume the `...' token. */
16638 cp_lexer_consume_token (parser->lexer);
16639
16640 /* Make the argument into a TYPE_PACK_EXPANSION or
16641 EXPR_PACK_EXPANSION. */
16642 argument = make_pack_expansion (argument);
16643 }
16644
16645 if (n_args == alloced)
16646 {
16647 alloced *= 2;
16648
16649 if (arg_ary == fixed_args)
16650 {
16651 arg_ary = XNEWVEC (tree, alloced);
16652 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16653 }
16654 else
16655 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16656 }
16657 arg_ary[n_args++] = argument;
16658 }
16659 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16660
16661 vec = make_tree_vec (n_args);
16662
16663 while (n_args--)
16664 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16665
16666 if (arg_ary != fixed_args)
16667 free (arg_ary);
16668 parser->non_integral_constant_expression_p = saved_non_ice_p;
16669 parser->integral_constant_expression_p = saved_ice_p;
16670 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16671 if (CHECKING_P)
16672 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16673 return vec;
16674 }
16675
16676 /* Parse a template-argument.
16677
16678 template-argument:
16679 assignment-expression
16680 type-id
16681 id-expression
16682
16683 The representation is that of an assignment-expression, type-id, or
16684 id-expression -- except that the qualified id-expression is
16685 evaluated, so that the value returned is either a DECL or an
16686 OVERLOAD.
16687
16688 Although the standard says "assignment-expression", it forbids
16689 throw-expressions or assignments in the template argument.
16690 Therefore, we use "conditional-expression" instead. */
16691
16692 static tree
16693 cp_parser_template_argument (cp_parser* parser)
16694 {
16695 tree argument;
16696 bool template_p;
16697 bool address_p;
16698 bool maybe_type_id = false;
16699 cp_token *token = NULL, *argument_start_token = NULL;
16700 location_t loc = 0;
16701 cp_id_kind idk;
16702
16703 /* There's really no way to know what we're looking at, so we just
16704 try each alternative in order.
16705
16706 [temp.arg]
16707
16708 In a template-argument, an ambiguity between a type-id and an
16709 expression is resolved to a type-id, regardless of the form of
16710 the corresponding template-parameter.
16711
16712 Therefore, we try a type-id first. */
16713 cp_parser_parse_tentatively (parser);
16714 argument = cp_parser_template_type_arg (parser);
16715 /* If there was no error parsing the type-id but the next token is a
16716 '>>', our behavior depends on which dialect of C++ we're
16717 parsing. In C++98, we probably found a typo for '> >'. But there
16718 are type-id which are also valid expressions. For instance:
16719
16720 struct X { int operator >> (int); };
16721 template <int V> struct Foo {};
16722 Foo<X () >> 5> r;
16723
16724 Here 'X()' is a valid type-id of a function type, but the user just
16725 wanted to write the expression "X() >> 5". Thus, we remember that we
16726 found a valid type-id, but we still try to parse the argument as an
16727 expression to see what happens.
16728
16729 In C++0x, the '>>' will be considered two separate '>'
16730 tokens. */
16731 if (!cp_parser_error_occurred (parser)
16732 && cxx_dialect == cxx98
16733 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16734 {
16735 maybe_type_id = true;
16736 cp_parser_abort_tentative_parse (parser);
16737 }
16738 else
16739 {
16740 /* If the next token isn't a `,' or a `>', then this argument wasn't
16741 really finished. This means that the argument is not a valid
16742 type-id. */
16743 if (!cp_parser_next_token_ends_template_argument_p (parser))
16744 cp_parser_error (parser, "expected template-argument");
16745 /* If that worked, we're done. */
16746 if (cp_parser_parse_definitely (parser))
16747 return argument;
16748 }
16749 /* We're still not sure what the argument will be. */
16750 cp_parser_parse_tentatively (parser);
16751 /* Try a template. */
16752 argument_start_token = cp_lexer_peek_token (parser->lexer);
16753 argument = cp_parser_id_expression (parser,
16754 /*template_keyword_p=*/false,
16755 /*check_dependency_p=*/true,
16756 &template_p,
16757 /*declarator_p=*/false,
16758 /*optional_p=*/false);
16759 /* If the next token isn't a `,' or a `>', then this argument wasn't
16760 really finished. */
16761 if (!cp_parser_next_token_ends_template_argument_p (parser))
16762 cp_parser_error (parser, "expected template-argument");
16763 if (!cp_parser_error_occurred (parser))
16764 {
16765 /* Figure out what is being referred to. If the id-expression
16766 was for a class template specialization, then we will have a
16767 TYPE_DECL at this point. There is no need to do name lookup
16768 at this point in that case. */
16769 if (TREE_CODE (argument) != TYPE_DECL)
16770 argument = cp_parser_lookup_name (parser, argument,
16771 none_type,
16772 /*is_template=*/template_p,
16773 /*is_namespace=*/false,
16774 /*check_dependency=*/true,
16775 /*ambiguous_decls=*/NULL,
16776 argument_start_token->location);
16777 /* Handle a constrained-type-specifier for a non-type template
16778 parameter. */
16779 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16780 argument = decl;
16781 else if (TREE_CODE (argument) != TEMPLATE_DECL
16782 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16783 cp_parser_error (parser, "expected template-name");
16784 }
16785 if (cp_parser_parse_definitely (parser))
16786 {
16787 if (TREE_DEPRECATED (argument))
16788 warn_deprecated_use (argument, NULL_TREE);
16789 return argument;
16790 }
16791 /* It must be a non-type argument. In C++17 any constant-expression is
16792 allowed. */
16793 if (cxx_dialect > cxx14)
16794 goto general_expr;
16795
16796 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16797
16798 -- an integral constant-expression of integral or enumeration
16799 type; or
16800
16801 -- the name of a non-type template-parameter; or
16802
16803 -- the name of an object or function with external linkage...
16804
16805 -- the address of an object or function with external linkage...
16806
16807 -- a pointer to member... */
16808 /* Look for a non-type template parameter. */
16809 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16810 {
16811 cp_parser_parse_tentatively (parser);
16812 argument = cp_parser_primary_expression (parser,
16813 /*address_p=*/false,
16814 /*cast_p=*/false,
16815 /*template_arg_p=*/true,
16816 &idk);
16817 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16818 || !cp_parser_next_token_ends_template_argument_p (parser))
16819 cp_parser_simulate_error (parser);
16820 if (cp_parser_parse_definitely (parser))
16821 return argument;
16822 }
16823
16824 /* If the next token is "&", the argument must be the address of an
16825 object or function with external linkage. */
16826 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16827 if (address_p)
16828 {
16829 loc = cp_lexer_peek_token (parser->lexer)->location;
16830 cp_lexer_consume_token (parser->lexer);
16831 }
16832 /* See if we might have an id-expression. */
16833 token = cp_lexer_peek_token (parser->lexer);
16834 if (token->type == CPP_NAME
16835 || token->keyword == RID_OPERATOR
16836 || token->type == CPP_SCOPE
16837 || token->type == CPP_TEMPLATE_ID
16838 || token->type == CPP_NESTED_NAME_SPECIFIER)
16839 {
16840 cp_parser_parse_tentatively (parser);
16841 argument = cp_parser_primary_expression (parser,
16842 address_p,
16843 /*cast_p=*/false,
16844 /*template_arg_p=*/true,
16845 &idk);
16846 if (cp_parser_error_occurred (parser)
16847 || !cp_parser_next_token_ends_template_argument_p (parser))
16848 cp_parser_abort_tentative_parse (parser);
16849 else
16850 {
16851 tree probe;
16852
16853 if (INDIRECT_REF_P (argument))
16854 {
16855 /* Strip the dereference temporarily. */
16856 gcc_assert (REFERENCE_REF_P (argument));
16857 argument = TREE_OPERAND (argument, 0);
16858 }
16859
16860 /* If we're in a template, we represent a qualified-id referring
16861 to a static data member as a SCOPE_REF even if the scope isn't
16862 dependent so that we can check access control later. */
16863 probe = argument;
16864 if (TREE_CODE (probe) == SCOPE_REF)
16865 probe = TREE_OPERAND (probe, 1);
16866 if (VAR_P (probe))
16867 {
16868 /* A variable without external linkage might still be a
16869 valid constant-expression, so no error is issued here
16870 if the external-linkage check fails. */
16871 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16872 cp_parser_simulate_error (parser);
16873 }
16874 else if (is_overloaded_fn (argument))
16875 /* All overloaded functions are allowed; if the external
16876 linkage test does not pass, an error will be issued
16877 later. */
16878 ;
16879 else if (address_p
16880 && (TREE_CODE (argument) == OFFSET_REF
16881 || TREE_CODE (argument) == SCOPE_REF))
16882 /* A pointer-to-member. */
16883 ;
16884 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16885 ;
16886 else
16887 cp_parser_simulate_error (parser);
16888
16889 if (cp_parser_parse_definitely (parser))
16890 {
16891 if (address_p)
16892 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16893 tf_warning_or_error);
16894 else
16895 argument = convert_from_reference (argument);
16896 return argument;
16897 }
16898 }
16899 }
16900 /* If the argument started with "&", there are no other valid
16901 alternatives at this point. */
16902 if (address_p)
16903 {
16904 cp_parser_error (parser, "invalid non-type template argument");
16905 return error_mark_node;
16906 }
16907
16908 general_expr:
16909 /* If the argument wasn't successfully parsed as a type-id followed
16910 by '>>', the argument can only be a constant expression now.
16911 Otherwise, we try parsing the constant-expression tentatively,
16912 because the argument could really be a type-id. */
16913 if (maybe_type_id)
16914 cp_parser_parse_tentatively (parser);
16915
16916 if (cxx_dialect <= cxx14)
16917 argument = cp_parser_constant_expression (parser);
16918 else
16919 {
16920 /* With C++17 generalized non-type template arguments we need to handle
16921 lvalue constant expressions, too. */
16922 argument = cp_parser_assignment_expression (parser);
16923 require_potential_constant_expression (argument);
16924 }
16925
16926 if (!maybe_type_id)
16927 return argument;
16928 if (!cp_parser_next_token_ends_template_argument_p (parser))
16929 cp_parser_error (parser, "expected template-argument");
16930 if (cp_parser_parse_definitely (parser))
16931 return argument;
16932 /* We did our best to parse the argument as a non type-id, but that
16933 was the only alternative that matched (albeit with a '>' after
16934 it). We can assume it's just a typo from the user, and a
16935 diagnostic will then be issued. */
16936 return cp_parser_template_type_arg (parser);
16937 }
16938
16939 /* Parse an explicit-instantiation.
16940
16941 explicit-instantiation:
16942 template declaration
16943
16944 Although the standard says `declaration', what it really means is:
16945
16946 explicit-instantiation:
16947 template decl-specifier-seq [opt] declarator [opt] ;
16948
16949 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16950 supposed to be allowed. A defect report has been filed about this
16951 issue.
16952
16953 GNU Extension:
16954
16955 explicit-instantiation:
16956 storage-class-specifier template
16957 decl-specifier-seq [opt] declarator [opt] ;
16958 function-specifier template
16959 decl-specifier-seq [opt] declarator [opt] ; */
16960
16961 static void
16962 cp_parser_explicit_instantiation (cp_parser* parser)
16963 {
16964 int declares_class_or_enum;
16965 cp_decl_specifier_seq decl_specifiers;
16966 tree extension_specifier = NULL_TREE;
16967
16968 timevar_push (TV_TEMPLATE_INST);
16969
16970 /* Look for an (optional) storage-class-specifier or
16971 function-specifier. */
16972 if (cp_parser_allow_gnu_extensions_p (parser))
16973 {
16974 extension_specifier
16975 = cp_parser_storage_class_specifier_opt (parser);
16976 if (!extension_specifier)
16977 extension_specifier
16978 = cp_parser_function_specifier_opt (parser,
16979 /*decl_specs=*/NULL);
16980 }
16981
16982 /* Look for the `template' keyword. */
16983 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16984 /* Let the front end know that we are processing an explicit
16985 instantiation. */
16986 begin_explicit_instantiation ();
16987 /* [temp.explicit] says that we are supposed to ignore access
16988 control while processing explicit instantiation directives. */
16989 push_deferring_access_checks (dk_no_check);
16990 /* Parse a decl-specifier-seq. */
16991 cp_parser_decl_specifier_seq (parser,
16992 CP_PARSER_FLAGS_OPTIONAL,
16993 &decl_specifiers,
16994 &declares_class_or_enum);
16995 /* If there was exactly one decl-specifier, and it declared a class,
16996 and there's no declarator, then we have an explicit type
16997 instantiation. */
16998 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16999 {
17000 tree type;
17001
17002 type = check_tag_decl (&decl_specifiers,
17003 /*explicit_type_instantiation_p=*/true);
17004 /* Turn access control back on for names used during
17005 template instantiation. */
17006 pop_deferring_access_checks ();
17007 if (type)
17008 do_type_instantiation (type, extension_specifier,
17009 /*complain=*/tf_error);
17010 }
17011 else
17012 {
17013 cp_declarator *declarator;
17014 tree decl;
17015
17016 /* Parse the declarator. */
17017 declarator
17018 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17019 /*ctor_dtor_or_conv_p=*/NULL,
17020 /*parenthesized_p=*/NULL,
17021 /*member_p=*/false,
17022 /*friend_p=*/false);
17023 if (declares_class_or_enum & 2)
17024 cp_parser_check_for_definition_in_return_type (declarator,
17025 decl_specifiers.type,
17026 decl_specifiers.locations[ds_type_spec]);
17027 if (declarator != cp_error_declarator)
17028 {
17029 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17030 permerror (decl_specifiers.locations[ds_inline],
17031 "explicit instantiation shall not use"
17032 " %<inline%> specifier");
17033 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17034 permerror (decl_specifiers.locations[ds_constexpr],
17035 "explicit instantiation shall not use"
17036 " %<constexpr%> specifier");
17037
17038 decl = grokdeclarator (declarator, &decl_specifiers,
17039 NORMAL, 0, &decl_specifiers.attributes);
17040 /* Turn access control back on for names used during
17041 template instantiation. */
17042 pop_deferring_access_checks ();
17043 /* Do the explicit instantiation. */
17044 do_decl_instantiation (decl, extension_specifier);
17045 }
17046 else
17047 {
17048 pop_deferring_access_checks ();
17049 /* Skip the body of the explicit instantiation. */
17050 cp_parser_skip_to_end_of_statement (parser);
17051 }
17052 }
17053 /* We're done with the instantiation. */
17054 end_explicit_instantiation ();
17055
17056 cp_parser_consume_semicolon_at_end_of_statement (parser);
17057
17058 timevar_pop (TV_TEMPLATE_INST);
17059 }
17060
17061 /* Parse an explicit-specialization.
17062
17063 explicit-specialization:
17064 template < > declaration
17065
17066 Although the standard says `declaration', what it really means is:
17067
17068 explicit-specialization:
17069 template <> decl-specifier [opt] init-declarator [opt] ;
17070 template <> function-definition
17071 template <> explicit-specialization
17072 template <> template-declaration */
17073
17074 static void
17075 cp_parser_explicit_specialization (cp_parser* parser)
17076 {
17077 bool need_lang_pop;
17078 cp_token *token = cp_lexer_peek_token (parser->lexer);
17079
17080 /* Look for the `template' keyword. */
17081 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17082 /* Look for the `<'. */
17083 cp_parser_require (parser, CPP_LESS, RT_LESS);
17084 /* Look for the `>'. */
17085 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17086 /* We have processed another parameter list. */
17087 ++parser->num_template_parameter_lists;
17088 /* [temp]
17089
17090 A template ... explicit specialization ... shall not have C
17091 linkage. */
17092 if (current_lang_name == lang_name_c)
17093 {
17094 error_at (token->location, "template specialization with C linkage");
17095 maybe_show_extern_c_location ();
17096 /* Give it C++ linkage to avoid confusing other parts of the
17097 front end. */
17098 push_lang_context (lang_name_cplusplus);
17099 need_lang_pop = true;
17100 }
17101 else
17102 need_lang_pop = false;
17103 /* Let the front end know that we are beginning a specialization. */
17104 if (!begin_specialization ())
17105 {
17106 end_specialization ();
17107 return;
17108 }
17109
17110 /* If the next keyword is `template', we need to figure out whether
17111 or not we're looking a template-declaration. */
17112 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17113 {
17114 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17115 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17116 cp_parser_template_declaration_after_export (parser,
17117 /*member_p=*/false);
17118 else
17119 cp_parser_explicit_specialization (parser);
17120 }
17121 else
17122 /* Parse the dependent declaration. */
17123 cp_parser_single_declaration (parser,
17124 /*checks=*/NULL,
17125 /*member_p=*/false,
17126 /*explicit_specialization_p=*/true,
17127 /*friend_p=*/NULL);
17128 /* We're done with the specialization. */
17129 end_specialization ();
17130 /* For the erroneous case of a template with C linkage, we pushed an
17131 implicit C++ linkage scope; exit that scope now. */
17132 if (need_lang_pop)
17133 pop_lang_context ();
17134 /* We're done with this parameter list. */
17135 --parser->num_template_parameter_lists;
17136 }
17137
17138 /* Parse a type-specifier.
17139
17140 type-specifier:
17141 simple-type-specifier
17142 class-specifier
17143 enum-specifier
17144 elaborated-type-specifier
17145 cv-qualifier
17146
17147 GNU Extension:
17148
17149 type-specifier:
17150 __complex__
17151
17152 Returns a representation of the type-specifier. For a
17153 class-specifier, enum-specifier, or elaborated-type-specifier, a
17154 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17155
17156 The parser flags FLAGS is used to control type-specifier parsing.
17157
17158 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17159 in a decl-specifier-seq.
17160
17161 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17162 class-specifier, enum-specifier, or elaborated-type-specifier, then
17163 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17164 if a type is declared; 2 if it is defined. Otherwise, it is set to
17165 zero.
17166
17167 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17168 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17169 is set to FALSE. */
17170
17171 static tree
17172 cp_parser_type_specifier (cp_parser* parser,
17173 cp_parser_flags flags,
17174 cp_decl_specifier_seq *decl_specs,
17175 bool is_declaration,
17176 int* declares_class_or_enum,
17177 bool* is_cv_qualifier)
17178 {
17179 tree type_spec = NULL_TREE;
17180 cp_token *token;
17181 enum rid keyword;
17182 cp_decl_spec ds = ds_last;
17183
17184 /* Assume this type-specifier does not declare a new type. */
17185 if (declares_class_or_enum)
17186 *declares_class_or_enum = 0;
17187 /* And that it does not specify a cv-qualifier. */
17188 if (is_cv_qualifier)
17189 *is_cv_qualifier = false;
17190 /* Peek at the next token. */
17191 token = cp_lexer_peek_token (parser->lexer);
17192
17193 /* If we're looking at a keyword, we can use that to guide the
17194 production we choose. */
17195 keyword = token->keyword;
17196 switch (keyword)
17197 {
17198 case RID_ENUM:
17199 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17200 goto elaborated_type_specifier;
17201
17202 /* Look for the enum-specifier. */
17203 type_spec = cp_parser_enum_specifier (parser);
17204 /* If that worked, we're done. */
17205 if (type_spec)
17206 {
17207 if (declares_class_or_enum)
17208 *declares_class_or_enum = 2;
17209 if (decl_specs)
17210 cp_parser_set_decl_spec_type (decl_specs,
17211 type_spec,
17212 token,
17213 /*type_definition_p=*/true);
17214 return type_spec;
17215 }
17216 else
17217 goto elaborated_type_specifier;
17218
17219 /* Any of these indicate either a class-specifier, or an
17220 elaborated-type-specifier. */
17221 case RID_CLASS:
17222 case RID_STRUCT:
17223 case RID_UNION:
17224 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17225 goto elaborated_type_specifier;
17226
17227 /* Parse tentatively so that we can back up if we don't find a
17228 class-specifier. */
17229 cp_parser_parse_tentatively (parser);
17230 /* Look for the class-specifier. */
17231 type_spec = cp_parser_class_specifier (parser);
17232 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17233 /* If that worked, we're done. */
17234 if (cp_parser_parse_definitely (parser))
17235 {
17236 if (declares_class_or_enum)
17237 *declares_class_or_enum = 2;
17238 if (decl_specs)
17239 cp_parser_set_decl_spec_type (decl_specs,
17240 type_spec,
17241 token,
17242 /*type_definition_p=*/true);
17243 return type_spec;
17244 }
17245
17246 /* Fall through. */
17247 elaborated_type_specifier:
17248 /* We're declaring (not defining) a class or enum. */
17249 if (declares_class_or_enum)
17250 *declares_class_or_enum = 1;
17251
17252 /* Fall through. */
17253 case RID_TYPENAME:
17254 /* Look for an elaborated-type-specifier. */
17255 type_spec
17256 = (cp_parser_elaborated_type_specifier
17257 (parser,
17258 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17259 is_declaration));
17260 if (decl_specs)
17261 cp_parser_set_decl_spec_type (decl_specs,
17262 type_spec,
17263 token,
17264 /*type_definition_p=*/false);
17265 return type_spec;
17266
17267 case RID_CONST:
17268 ds = ds_const;
17269 if (is_cv_qualifier)
17270 *is_cv_qualifier = true;
17271 break;
17272
17273 case RID_VOLATILE:
17274 ds = ds_volatile;
17275 if (is_cv_qualifier)
17276 *is_cv_qualifier = true;
17277 break;
17278
17279 case RID_RESTRICT:
17280 ds = ds_restrict;
17281 if (is_cv_qualifier)
17282 *is_cv_qualifier = true;
17283 break;
17284
17285 case RID_COMPLEX:
17286 /* The `__complex__' keyword is a GNU extension. */
17287 ds = ds_complex;
17288 break;
17289
17290 default:
17291 break;
17292 }
17293
17294 /* Handle simple keywords. */
17295 if (ds != ds_last)
17296 {
17297 if (decl_specs)
17298 {
17299 set_and_check_decl_spec_loc (decl_specs, ds, token);
17300 decl_specs->any_specifiers_p = true;
17301 }
17302 return cp_lexer_consume_token (parser->lexer)->u.value;
17303 }
17304
17305 /* If we do not already have a type-specifier, assume we are looking
17306 at a simple-type-specifier. */
17307 type_spec = cp_parser_simple_type_specifier (parser,
17308 decl_specs,
17309 flags);
17310
17311 /* If we didn't find a type-specifier, and a type-specifier was not
17312 optional in this context, issue an error message. */
17313 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17314 {
17315 cp_parser_error (parser, "expected type specifier");
17316 return error_mark_node;
17317 }
17318
17319 return type_spec;
17320 }
17321
17322 /* Parse a simple-type-specifier.
17323
17324 simple-type-specifier:
17325 :: [opt] nested-name-specifier [opt] type-name
17326 :: [opt] nested-name-specifier template template-id
17327 char
17328 wchar_t
17329 bool
17330 short
17331 int
17332 long
17333 signed
17334 unsigned
17335 float
17336 double
17337 void
17338
17339 C++11 Extension:
17340
17341 simple-type-specifier:
17342 auto
17343 decltype ( expression )
17344 char16_t
17345 char32_t
17346 __underlying_type ( type-id )
17347
17348 C++17 extension:
17349
17350 nested-name-specifier(opt) template-name
17351
17352 GNU Extension:
17353
17354 simple-type-specifier:
17355 __int128
17356 __typeof__ unary-expression
17357 __typeof__ ( type-id )
17358 __typeof__ ( type-id ) { initializer-list , [opt] }
17359
17360 Concepts Extension:
17361
17362 simple-type-specifier:
17363 constrained-type-specifier
17364
17365 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17366 appropriately updated. */
17367
17368 static tree
17369 cp_parser_simple_type_specifier (cp_parser* parser,
17370 cp_decl_specifier_seq *decl_specs,
17371 cp_parser_flags flags)
17372 {
17373 tree type = NULL_TREE;
17374 cp_token *token;
17375 int idx;
17376
17377 /* Peek at the next token. */
17378 token = cp_lexer_peek_token (parser->lexer);
17379
17380 /* If we're looking at a keyword, things are easy. */
17381 switch (token->keyword)
17382 {
17383 case RID_CHAR:
17384 if (decl_specs)
17385 decl_specs->explicit_char_p = true;
17386 type = char_type_node;
17387 break;
17388 case RID_CHAR16:
17389 type = char16_type_node;
17390 break;
17391 case RID_CHAR32:
17392 type = char32_type_node;
17393 break;
17394 case RID_WCHAR:
17395 type = wchar_type_node;
17396 break;
17397 case RID_BOOL:
17398 type = boolean_type_node;
17399 break;
17400 case RID_SHORT:
17401 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17402 type = short_integer_type_node;
17403 break;
17404 case RID_INT:
17405 if (decl_specs)
17406 decl_specs->explicit_int_p = true;
17407 type = integer_type_node;
17408 break;
17409 case RID_INT_N_0:
17410 case RID_INT_N_1:
17411 case RID_INT_N_2:
17412 case RID_INT_N_3:
17413 idx = token->keyword - RID_INT_N_0;
17414 if (! int_n_enabled_p [idx])
17415 break;
17416 if (decl_specs)
17417 {
17418 decl_specs->explicit_intN_p = true;
17419 decl_specs->int_n_idx = idx;
17420 }
17421 type = int_n_trees [idx].signed_type;
17422 break;
17423 case RID_LONG:
17424 if (decl_specs)
17425 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17426 type = long_integer_type_node;
17427 break;
17428 case RID_SIGNED:
17429 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17430 type = integer_type_node;
17431 break;
17432 case RID_UNSIGNED:
17433 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17434 type = unsigned_type_node;
17435 break;
17436 case RID_FLOAT:
17437 type = float_type_node;
17438 break;
17439 case RID_DOUBLE:
17440 type = double_type_node;
17441 break;
17442 case RID_VOID:
17443 type = void_type_node;
17444 break;
17445
17446 case RID_AUTO:
17447 maybe_warn_cpp0x (CPP0X_AUTO);
17448 if (parser->auto_is_implicit_function_template_parm_p)
17449 {
17450 /* The 'auto' might be the placeholder return type for a function decl
17451 with trailing return type. */
17452 bool have_trailing_return_fn_decl = false;
17453
17454 cp_parser_parse_tentatively (parser);
17455 cp_lexer_consume_token (parser->lexer);
17456 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17457 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17458 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17459 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17460 {
17461 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17462 {
17463 cp_lexer_consume_token (parser->lexer);
17464 cp_parser_skip_to_closing_parenthesis (parser,
17465 /*recovering*/false,
17466 /*or_comma*/false,
17467 /*consume_paren*/true);
17468 continue;
17469 }
17470
17471 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17472 {
17473 have_trailing_return_fn_decl = true;
17474 break;
17475 }
17476
17477 cp_lexer_consume_token (parser->lexer);
17478 }
17479 cp_parser_abort_tentative_parse (parser);
17480
17481 if (have_trailing_return_fn_decl)
17482 {
17483 type = make_auto ();
17484 break;
17485 }
17486
17487 if (cxx_dialect >= cxx14)
17488 {
17489 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17490 type = TREE_TYPE (type);
17491 }
17492 else
17493 type = error_mark_node;
17494
17495 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17496 {
17497 if (cxx_dialect < cxx14)
17498 error_at (token->location,
17499 "use of %<auto%> in lambda parameter declaration "
17500 "only available with "
17501 "-std=c++14 or -std=gnu++14");
17502 }
17503 else if (cxx_dialect < cxx14)
17504 error_at (token->location,
17505 "use of %<auto%> in parameter declaration "
17506 "only available with "
17507 "-std=c++14 or -std=gnu++14");
17508 else if (!flag_concepts)
17509 pedwarn (token->location, 0,
17510 "use of %<auto%> in parameter declaration "
17511 "only available with -fconcepts");
17512 }
17513 else
17514 type = make_auto ();
17515 break;
17516
17517 case RID_DECLTYPE:
17518 /* Since DR 743, decltype can either be a simple-type-specifier by
17519 itself or begin a nested-name-specifier. Parsing it will replace
17520 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17521 handling below decide what to do. */
17522 cp_parser_decltype (parser);
17523 cp_lexer_set_token_position (parser->lexer, token);
17524 break;
17525
17526 case RID_TYPEOF:
17527 /* Consume the `typeof' token. */
17528 cp_lexer_consume_token (parser->lexer);
17529 /* Parse the operand to `typeof'. */
17530 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17531 /* If it is not already a TYPE, take its type. */
17532 if (!TYPE_P (type))
17533 type = finish_typeof (type);
17534
17535 if (decl_specs)
17536 cp_parser_set_decl_spec_type (decl_specs, type,
17537 token,
17538 /*type_definition_p=*/false);
17539
17540 return type;
17541
17542 case RID_UNDERLYING_TYPE:
17543 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17544 if (decl_specs)
17545 cp_parser_set_decl_spec_type (decl_specs, type,
17546 token,
17547 /*type_definition_p=*/false);
17548
17549 return type;
17550
17551 case RID_BASES:
17552 case RID_DIRECT_BASES:
17553 type = cp_parser_trait_expr (parser, token->keyword);
17554 if (decl_specs)
17555 cp_parser_set_decl_spec_type (decl_specs, type,
17556 token,
17557 /*type_definition_p=*/false);
17558 return type;
17559 default:
17560 break;
17561 }
17562
17563 /* If token is an already-parsed decltype not followed by ::,
17564 it's a simple-type-specifier. */
17565 if (token->type == CPP_DECLTYPE
17566 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17567 {
17568 type = saved_checks_value (token->u.tree_check_value);
17569 if (decl_specs)
17570 {
17571 cp_parser_set_decl_spec_type (decl_specs, type,
17572 token,
17573 /*type_definition_p=*/false);
17574 /* Remember that we are handling a decltype in order to
17575 implement the resolution of DR 1510 when the argument
17576 isn't instantiation dependent. */
17577 decl_specs->decltype_p = true;
17578 }
17579 cp_lexer_consume_token (parser->lexer);
17580 return type;
17581 }
17582
17583 /* If the type-specifier was for a built-in type, we're done. */
17584 if (type)
17585 {
17586 /* Record the type. */
17587 if (decl_specs
17588 && (token->keyword != RID_SIGNED
17589 && token->keyword != RID_UNSIGNED
17590 && token->keyword != RID_SHORT
17591 && token->keyword != RID_LONG))
17592 cp_parser_set_decl_spec_type (decl_specs,
17593 type,
17594 token,
17595 /*type_definition_p=*/false);
17596 if (decl_specs)
17597 decl_specs->any_specifiers_p = true;
17598
17599 /* Consume the token. */
17600 cp_lexer_consume_token (parser->lexer);
17601
17602 if (type == error_mark_node)
17603 return error_mark_node;
17604
17605 /* There is no valid C++ program where a non-template type is
17606 followed by a "<". That usually indicates that the user thought
17607 that the type was a template. */
17608 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17609 token->location);
17610
17611 return TYPE_NAME (type);
17612 }
17613
17614 /* The type-specifier must be a user-defined type. */
17615 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17616 {
17617 bool qualified_p;
17618 bool global_p;
17619
17620 /* Don't gobble tokens or issue error messages if this is an
17621 optional type-specifier. */
17622 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17623 cp_parser_parse_tentatively (parser);
17624
17625 token = cp_lexer_peek_token (parser->lexer);
17626
17627 /* Look for the optional `::' operator. */
17628 global_p
17629 = (cp_parser_global_scope_opt (parser,
17630 /*current_scope_valid_p=*/false)
17631 != NULL_TREE);
17632 /* Look for the nested-name specifier. */
17633 qualified_p
17634 = (cp_parser_nested_name_specifier_opt (parser,
17635 /*typename_keyword_p=*/false,
17636 /*check_dependency_p=*/true,
17637 /*type_p=*/false,
17638 /*is_declaration=*/false)
17639 != NULL_TREE);
17640 /* If we have seen a nested-name-specifier, and the next token
17641 is `template', then we are using the template-id production. */
17642 if (parser->scope
17643 && cp_parser_optional_template_keyword (parser))
17644 {
17645 /* Look for the template-id. */
17646 type = cp_parser_template_id (parser,
17647 /*template_keyword_p=*/true,
17648 /*check_dependency_p=*/true,
17649 none_type,
17650 /*is_declaration=*/false);
17651 /* If the template-id did not name a type, we are out of
17652 luck. */
17653 if (TREE_CODE (type) != TYPE_DECL)
17654 {
17655 cp_parser_error (parser, "expected template-id for type");
17656 type = NULL_TREE;
17657 }
17658 }
17659 /* Otherwise, look for a type-name. */
17660 else
17661 type = cp_parser_type_name (parser);
17662 /* Keep track of all name-lookups performed in class scopes. */
17663 if (type
17664 && !global_p
17665 && !qualified_p
17666 && TREE_CODE (type) == TYPE_DECL
17667 && identifier_p (DECL_NAME (type)))
17668 maybe_note_name_used_in_class (DECL_NAME (type), type);
17669 /* If it didn't work out, we don't have a TYPE. */
17670 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17671 && !cp_parser_parse_definitely (parser))
17672 type = NULL_TREE;
17673 if (!type && cxx_dialect >= cxx17)
17674 {
17675 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17676 cp_parser_parse_tentatively (parser);
17677
17678 cp_parser_global_scope_opt (parser,
17679 /*current_scope_valid_p=*/false);
17680 cp_parser_nested_name_specifier_opt (parser,
17681 /*typename_keyword_p=*/false,
17682 /*check_dependency_p=*/true,
17683 /*type_p=*/false,
17684 /*is_declaration=*/false);
17685 tree name = cp_parser_identifier (parser);
17686 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17687 && parser->scope != error_mark_node)
17688 {
17689 tree tmpl = cp_parser_lookup_name (parser, name,
17690 none_type,
17691 /*is_template=*/false,
17692 /*is_namespace=*/false,
17693 /*check_dependency=*/true,
17694 /*ambiguous_decls=*/NULL,
17695 token->location);
17696 if (tmpl && tmpl != error_mark_node
17697 && (DECL_CLASS_TEMPLATE_P (tmpl)
17698 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17699 type = make_template_placeholder (tmpl);
17700 else
17701 {
17702 type = error_mark_node;
17703 if (!cp_parser_simulate_error (parser))
17704 cp_parser_name_lookup_error (parser, name, tmpl,
17705 NLE_TYPE, token->location);
17706 }
17707 }
17708 else
17709 type = error_mark_node;
17710
17711 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17712 && !cp_parser_parse_definitely (parser))
17713 type = NULL_TREE;
17714 }
17715 if (type && decl_specs)
17716 cp_parser_set_decl_spec_type (decl_specs, type,
17717 token,
17718 /*type_definition_p=*/false);
17719 }
17720
17721 /* If we didn't get a type-name, issue an error message. */
17722 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17723 {
17724 cp_parser_error (parser, "expected type-name");
17725 return error_mark_node;
17726 }
17727
17728 if (type && type != error_mark_node)
17729 {
17730 /* See if TYPE is an Objective-C type, and if so, parse and
17731 accept any protocol references following it. Do this before
17732 the cp_parser_check_for_invalid_template_id() call, because
17733 Objective-C types can be followed by '<...>' which would
17734 enclose protocol names rather than template arguments, and so
17735 everything is fine. */
17736 if (c_dialect_objc () && !parser->scope
17737 && (objc_is_id (type) || objc_is_class_name (type)))
17738 {
17739 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17740 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17741
17742 /* Clobber the "unqualified" type previously entered into
17743 DECL_SPECS with the new, improved protocol-qualified version. */
17744 if (decl_specs)
17745 decl_specs->type = qual_type;
17746
17747 return qual_type;
17748 }
17749
17750 /* There is no valid C++ program where a non-template type is
17751 followed by a "<". That usually indicates that the user
17752 thought that the type was a template. */
17753 cp_parser_check_for_invalid_template_id (parser, type,
17754 none_type,
17755 token->location);
17756 }
17757
17758 return type;
17759 }
17760
17761 /* Parse a type-name.
17762
17763 type-name:
17764 class-name
17765 enum-name
17766 typedef-name
17767 simple-template-id [in c++0x]
17768
17769 enum-name:
17770 identifier
17771
17772 typedef-name:
17773 identifier
17774
17775 Concepts:
17776
17777 type-name:
17778 concept-name
17779 partial-concept-id
17780
17781 concept-name:
17782 identifier
17783
17784 Returns a TYPE_DECL for the type. */
17785
17786 static tree
17787 cp_parser_type_name (cp_parser* parser)
17788 {
17789 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17790 }
17791
17792 /* See above. */
17793 static tree
17794 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17795 {
17796 tree type_decl;
17797
17798 /* We can't know yet whether it is a class-name or not. */
17799 cp_parser_parse_tentatively (parser);
17800 /* Try a class-name. */
17801 type_decl = cp_parser_class_name (parser,
17802 typename_keyword_p,
17803 /*template_keyword_p=*/false,
17804 none_type,
17805 /*check_dependency_p=*/true,
17806 /*class_head_p=*/false,
17807 /*is_declaration=*/false);
17808 /* If it's not a class-name, keep looking. */
17809 if (!cp_parser_parse_definitely (parser))
17810 {
17811 if (cxx_dialect < cxx11)
17812 /* It must be a typedef-name or an enum-name. */
17813 return cp_parser_nonclass_name (parser);
17814
17815 cp_parser_parse_tentatively (parser);
17816 /* It is either a simple-template-id representing an
17817 instantiation of an alias template... */
17818 type_decl = cp_parser_template_id (parser,
17819 /*template_keyword_p=*/false,
17820 /*check_dependency_p=*/true,
17821 none_type,
17822 /*is_declaration=*/false);
17823 /* Note that this must be an instantiation of an alias template
17824 because [temp.names]/6 says:
17825
17826 A template-id that names an alias template specialization
17827 is a type-name.
17828
17829 Whereas [temp.names]/7 says:
17830
17831 A simple-template-id that names a class template
17832 specialization is a class-name.
17833
17834 With concepts, this could also be a partial-concept-id that
17835 declares a non-type template parameter. */
17836 if (type_decl != NULL_TREE
17837 && TREE_CODE (type_decl) == TYPE_DECL
17838 && TYPE_DECL_ALIAS_P (type_decl))
17839 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17840 else if (is_constrained_parameter (type_decl))
17841 /* Don't do anything. */ ;
17842 else
17843 cp_parser_simulate_error (parser);
17844
17845 if (!cp_parser_parse_definitely (parser))
17846 /* ... Or a typedef-name or an enum-name. */
17847 return cp_parser_nonclass_name (parser);
17848 }
17849
17850 return type_decl;
17851 }
17852
17853 /* Check if DECL and ARGS can form a constrained-type-specifier.
17854 If ARGS is non-null, we try to form a concept check of the
17855 form DECL<?, ARGS> where ? is a wildcard that matches any
17856 kind of template argument. If ARGS is NULL, then we try to
17857 form a concept check of the form DECL<?>. */
17858
17859 static tree
17860 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17861 tree decl, tree args)
17862 {
17863 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17864
17865 /* If we a constrained-type-specifier cannot be deduced. */
17866 if (parser->prevent_constrained_type_specifiers)
17867 return NULL_TREE;
17868
17869 /* A constrained type specifier can only be found in an
17870 overload set or as a reference to a template declaration.
17871
17872 FIXME: This might be masking a bug. It's possible that
17873 that the deduction below is causing template specializations
17874 to be formed with the wildcard as an argument. */
17875 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17876 return NULL_TREE;
17877
17878 /* Try to build a call expression that evaluates the
17879 concept. This can fail if the overload set refers
17880 only to non-templates. */
17881 tree placeholder = build_nt (WILDCARD_DECL);
17882 tree check = build_concept_check (decl, placeholder, args);
17883 if (check == error_mark_node)
17884 return NULL_TREE;
17885
17886 /* Deduce the checked constraint and the prototype parameter.
17887
17888 FIXME: In certain cases, failure to deduce should be a
17889 diagnosable error. */
17890 tree conc;
17891 tree proto;
17892 if (!deduce_constrained_parameter (check, conc, proto))
17893 return NULL_TREE;
17894
17895 /* In template parameter scope, this results in a constrained
17896 parameter. Return a descriptor of that parm. */
17897 if (processing_template_parmlist)
17898 return build_constrained_parameter (conc, proto, args);
17899
17900 /* In a parameter-declaration-clause, constrained-type
17901 specifiers result in invented template parameters. */
17902 if (parser->auto_is_implicit_function_template_parm_p)
17903 {
17904 tree x = build_constrained_parameter (conc, proto, args);
17905 return synthesize_implicit_template_parm (parser, x);
17906 }
17907 else
17908 {
17909 /* Otherwise, we're in a context where the constrained
17910 type name is deduced and the constraint applies
17911 after deduction. */
17912 return make_constrained_auto (conc, args);
17913 }
17914
17915 return NULL_TREE;
17916 }
17917
17918 /* If DECL refers to a concept, return a TYPE_DECL representing
17919 the result of using the constrained type specifier in the
17920 current context. DECL refers to a concept if
17921
17922 - it is an overload set containing a function concept taking a single
17923 type argument, or
17924
17925 - it is a variable concept taking a single type argument. */
17926
17927 static tree
17928 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17929 {
17930 if (flag_concepts
17931 && (TREE_CODE (decl) == OVERLOAD
17932 || BASELINK_P (decl)
17933 || variable_concept_p (decl)))
17934 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17935 else
17936 return NULL_TREE;
17937 }
17938
17939 /* Check if DECL and ARGS form a partial-concept-id. If so,
17940 assign ID to the resulting constrained placeholder.
17941
17942 Returns true if the partial-concept-id designates a placeholder
17943 and false otherwise. Note that *id is set to NULL_TREE in
17944 this case. */
17945
17946 static tree
17947 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17948 {
17949 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17950 }
17951
17952 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17953 or a concept-name.
17954
17955 enum-name:
17956 identifier
17957
17958 typedef-name:
17959 identifier
17960
17961 concept-name:
17962 identifier
17963
17964 Returns a TYPE_DECL for the type. */
17965
17966 static tree
17967 cp_parser_nonclass_name (cp_parser* parser)
17968 {
17969 tree type_decl;
17970 tree identifier;
17971
17972 cp_token *token = cp_lexer_peek_token (parser->lexer);
17973 identifier = cp_parser_identifier (parser);
17974 if (identifier == error_mark_node)
17975 return error_mark_node;
17976
17977 /* Look up the type-name. */
17978 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17979
17980 type_decl = strip_using_decl (type_decl);
17981
17982 /* If we found an overload set, then it may refer to a concept-name. */
17983 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17984 type_decl = decl;
17985
17986 if (TREE_CODE (type_decl) != TYPE_DECL
17987 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17988 {
17989 /* See if this is an Objective-C type. */
17990 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17991 tree type = objc_get_protocol_qualified_type (identifier, protos);
17992 if (type)
17993 type_decl = TYPE_NAME (type);
17994 }
17995
17996 /* Issue an error if we did not find a type-name. */
17997 if (TREE_CODE (type_decl) != TYPE_DECL
17998 /* In Objective-C, we have the complication that class names are
17999 normally type names and start declarations (eg, the
18000 "NSObject" in "NSObject *object;"), but can be used in an
18001 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18002 is an expression. So, a classname followed by a dot is not a
18003 valid type-name. */
18004 || (objc_is_class_name (TREE_TYPE (type_decl))
18005 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18006 {
18007 if (!cp_parser_simulate_error (parser))
18008 cp_parser_name_lookup_error (parser, identifier, type_decl,
18009 NLE_TYPE, token->location);
18010 return error_mark_node;
18011 }
18012 /* Remember that the name was used in the definition of the
18013 current class so that we can check later to see if the
18014 meaning would have been different after the class was
18015 entirely defined. */
18016 else if (type_decl != error_mark_node
18017 && !parser->scope)
18018 maybe_note_name_used_in_class (identifier, type_decl);
18019
18020 return type_decl;
18021 }
18022
18023 /* Parse an elaborated-type-specifier. Note that the grammar given
18024 here incorporates the resolution to DR68.
18025
18026 elaborated-type-specifier:
18027 class-key :: [opt] nested-name-specifier [opt] identifier
18028 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18029 enum-key :: [opt] nested-name-specifier [opt] identifier
18030 typename :: [opt] nested-name-specifier identifier
18031 typename :: [opt] nested-name-specifier template [opt]
18032 template-id
18033
18034 GNU extension:
18035
18036 elaborated-type-specifier:
18037 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18038 class-key attributes :: [opt] nested-name-specifier [opt]
18039 template [opt] template-id
18040 enum attributes :: [opt] nested-name-specifier [opt] identifier
18041
18042 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18043 declared `friend'. If IS_DECLARATION is TRUE, then this
18044 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18045 something is being declared.
18046
18047 Returns the TYPE specified. */
18048
18049 static tree
18050 cp_parser_elaborated_type_specifier (cp_parser* parser,
18051 bool is_friend,
18052 bool is_declaration)
18053 {
18054 enum tag_types tag_type;
18055 tree identifier;
18056 tree type = NULL_TREE;
18057 tree attributes = NULL_TREE;
18058 tree globalscope;
18059 cp_token *token = NULL;
18060
18061 /* See if we're looking at the `enum' keyword. */
18062 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18063 {
18064 /* Consume the `enum' token. */
18065 cp_lexer_consume_token (parser->lexer);
18066 /* Remember that it's an enumeration type. */
18067 tag_type = enum_type;
18068 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18069 enums) is used here. */
18070 cp_token *token = cp_lexer_peek_token (parser->lexer);
18071 if (cp_parser_is_keyword (token, RID_CLASS)
18072 || cp_parser_is_keyword (token, RID_STRUCT))
18073 {
18074 gcc_rich_location richloc (token->location);
18075 richloc.add_range (input_location);
18076 richloc.add_fixit_remove ();
18077 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18078 "a scoped enum must not use the %qD keyword",
18079 token->u.value);
18080 /* Consume the `struct' or `class' and parse it anyway. */
18081 cp_lexer_consume_token (parser->lexer);
18082 }
18083 /* Parse the attributes. */
18084 attributes = cp_parser_attributes_opt (parser);
18085 }
18086 /* Or, it might be `typename'. */
18087 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18088 RID_TYPENAME))
18089 {
18090 /* Consume the `typename' token. */
18091 cp_lexer_consume_token (parser->lexer);
18092 /* Remember that it's a `typename' type. */
18093 tag_type = typename_type;
18094 }
18095 /* Otherwise it must be a class-key. */
18096 else
18097 {
18098 tag_type = cp_parser_class_key (parser);
18099 if (tag_type == none_type)
18100 return error_mark_node;
18101 /* Parse the attributes. */
18102 attributes = cp_parser_attributes_opt (parser);
18103 }
18104
18105 /* Look for the `::' operator. */
18106 globalscope = cp_parser_global_scope_opt (parser,
18107 /*current_scope_valid_p=*/false);
18108 /* Look for the nested-name-specifier. */
18109 tree nested_name_specifier;
18110 if (tag_type == typename_type && !globalscope)
18111 {
18112 nested_name_specifier
18113 = cp_parser_nested_name_specifier (parser,
18114 /*typename_keyword_p=*/true,
18115 /*check_dependency_p=*/true,
18116 /*type_p=*/true,
18117 is_declaration);
18118 if (!nested_name_specifier)
18119 return error_mark_node;
18120 }
18121 else
18122 /* Even though `typename' is not present, the proposed resolution
18123 to Core Issue 180 says that in `class A<T>::B', `B' should be
18124 considered a type-name, even if `A<T>' is dependent. */
18125 nested_name_specifier
18126 = cp_parser_nested_name_specifier_opt (parser,
18127 /*typename_keyword_p=*/true,
18128 /*check_dependency_p=*/true,
18129 /*type_p=*/true,
18130 is_declaration);
18131 /* For everything but enumeration types, consider a template-id.
18132 For an enumeration type, consider only a plain identifier. */
18133 if (tag_type != enum_type)
18134 {
18135 bool template_p = false;
18136 tree decl;
18137
18138 /* Allow the `template' keyword. */
18139 template_p = cp_parser_optional_template_keyword (parser);
18140 /* If we didn't see `template', we don't know if there's a
18141 template-id or not. */
18142 if (!template_p)
18143 cp_parser_parse_tentatively (parser);
18144 /* The `template' keyword must follow a nested-name-specifier. */
18145 else if (!nested_name_specifier)
18146 {
18147 cp_parser_error (parser, "%<template%> must follow a nested-"
18148 "name-specifier");
18149 return error_mark_node;
18150 }
18151
18152 /* Parse the template-id. */
18153 token = cp_lexer_peek_token (parser->lexer);
18154 decl = cp_parser_template_id (parser, template_p,
18155 /*check_dependency_p=*/true,
18156 tag_type,
18157 is_declaration);
18158 /* If we didn't find a template-id, look for an ordinary
18159 identifier. */
18160 if (!template_p && !cp_parser_parse_definitely (parser))
18161 ;
18162 /* We can get here when cp_parser_template_id, called by
18163 cp_parser_class_name with tag_type == none_type, succeeds
18164 and caches a BASELINK. Then, when called again here,
18165 instead of failing and returning an error_mark_node
18166 returns it (see template/typename17.C in C++11).
18167 ??? Could we diagnose this earlier? */
18168 else if (tag_type == typename_type && BASELINK_P (decl))
18169 {
18170 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18171 type = error_mark_node;
18172 }
18173 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18174 in effect, then we must assume that, upon instantiation, the
18175 template will correspond to a class. */
18176 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18177 && tag_type == typename_type)
18178 type = make_typename_type (parser->scope, decl,
18179 typename_type,
18180 /*complain=*/tf_error);
18181 /* If the `typename' keyword is in effect and DECL is not a type
18182 decl, then type is non existent. */
18183 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18184 ;
18185 else if (TREE_CODE (decl) == TYPE_DECL)
18186 {
18187 type = check_elaborated_type_specifier (tag_type, decl,
18188 /*allow_template_p=*/true);
18189
18190 /* If the next token is a semicolon, this must be a specialization,
18191 instantiation, or friend declaration. Check the scope while we
18192 still know whether or not we had a nested-name-specifier. */
18193 if (type != error_mark_node
18194 && !nested_name_specifier && !is_friend
18195 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18196 check_unqualified_spec_or_inst (type, token->location);
18197 }
18198 else if (decl == error_mark_node)
18199 type = error_mark_node;
18200 }
18201
18202 if (!type)
18203 {
18204 token = cp_lexer_peek_token (parser->lexer);
18205 identifier = cp_parser_identifier (parser);
18206
18207 if (identifier == error_mark_node)
18208 {
18209 parser->scope = NULL_TREE;
18210 return error_mark_node;
18211 }
18212
18213 /* For a `typename', we needn't call xref_tag. */
18214 if (tag_type == typename_type
18215 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18216 return cp_parser_make_typename_type (parser, identifier,
18217 token->location);
18218
18219 /* Template parameter lists apply only if we are not within a
18220 function parameter list. */
18221 bool template_parm_lists_apply
18222 = parser->num_template_parameter_lists;
18223 if (template_parm_lists_apply)
18224 for (cp_binding_level *s = current_binding_level;
18225 s && s->kind != sk_template_parms;
18226 s = s->level_chain)
18227 if (s->kind == sk_function_parms)
18228 template_parm_lists_apply = false;
18229
18230 /* Look up a qualified name in the usual way. */
18231 if (parser->scope)
18232 {
18233 tree decl;
18234 tree ambiguous_decls;
18235
18236 decl = cp_parser_lookup_name (parser, identifier,
18237 tag_type,
18238 /*is_template=*/false,
18239 /*is_namespace=*/false,
18240 /*check_dependency=*/true,
18241 &ambiguous_decls,
18242 token->location);
18243
18244 /* If the lookup was ambiguous, an error will already have been
18245 issued. */
18246 if (ambiguous_decls)
18247 return error_mark_node;
18248
18249 /* If we are parsing friend declaration, DECL may be a
18250 TEMPLATE_DECL tree node here. However, we need to check
18251 whether this TEMPLATE_DECL results in valid code. Consider
18252 the following example:
18253
18254 namespace N {
18255 template <class T> class C {};
18256 }
18257 class X {
18258 template <class T> friend class N::C; // #1, valid code
18259 };
18260 template <class T> class Y {
18261 friend class N::C; // #2, invalid code
18262 };
18263
18264 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18265 name lookup of `N::C'. We see that friend declaration must
18266 be template for the code to be valid. Note that
18267 processing_template_decl does not work here since it is
18268 always 1 for the above two cases. */
18269
18270 decl = (cp_parser_maybe_treat_template_as_class
18271 (decl, /*tag_name_p=*/is_friend
18272 && template_parm_lists_apply));
18273
18274 if (TREE_CODE (decl) != TYPE_DECL)
18275 {
18276 cp_parser_diagnose_invalid_type_name (parser,
18277 identifier,
18278 token->location);
18279 return error_mark_node;
18280 }
18281
18282 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18283 {
18284 bool allow_template = (template_parm_lists_apply
18285 || DECL_SELF_REFERENCE_P (decl));
18286 type = check_elaborated_type_specifier (tag_type, decl,
18287 allow_template);
18288
18289 if (type == error_mark_node)
18290 return error_mark_node;
18291 }
18292
18293 /* Forward declarations of nested types, such as
18294
18295 class C1::C2;
18296 class C1::C2::C3;
18297
18298 are invalid unless all components preceding the final '::'
18299 are complete. If all enclosing types are complete, these
18300 declarations become merely pointless.
18301
18302 Invalid forward declarations of nested types are errors
18303 caught elsewhere in parsing. Those that are pointless arrive
18304 here. */
18305
18306 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18307 && !is_friend && !processing_explicit_instantiation)
18308 warning (0, "declaration %qD does not declare anything", decl);
18309
18310 type = TREE_TYPE (decl);
18311 }
18312 else
18313 {
18314 /* An elaborated-type-specifier sometimes introduces a new type and
18315 sometimes names an existing type. Normally, the rule is that it
18316 introduces a new type only if there is not an existing type of
18317 the same name already in scope. For example, given:
18318
18319 struct S {};
18320 void f() { struct S s; }
18321
18322 the `struct S' in the body of `f' is the same `struct S' as in
18323 the global scope; the existing definition is used. However, if
18324 there were no global declaration, this would introduce a new
18325 local class named `S'.
18326
18327 An exception to this rule applies to the following code:
18328
18329 namespace N { struct S; }
18330
18331 Here, the elaborated-type-specifier names a new type
18332 unconditionally; even if there is already an `S' in the
18333 containing scope this declaration names a new type.
18334 This exception only applies if the elaborated-type-specifier
18335 forms the complete declaration:
18336
18337 [class.name]
18338
18339 A declaration consisting solely of `class-key identifier ;' is
18340 either a redeclaration of the name in the current scope or a
18341 forward declaration of the identifier as a class name. It
18342 introduces the name into the current scope.
18343
18344 We are in this situation precisely when the next token is a `;'.
18345
18346 An exception to the exception is that a `friend' declaration does
18347 *not* name a new type; i.e., given:
18348
18349 struct S { friend struct T; };
18350
18351 `T' is not a new type in the scope of `S'.
18352
18353 Also, `new struct S' or `sizeof (struct S)' never results in the
18354 definition of a new type; a new type can only be declared in a
18355 declaration context. */
18356
18357 tag_scope ts;
18358 bool template_p;
18359
18360 if (is_friend)
18361 /* Friends have special name lookup rules. */
18362 ts = ts_within_enclosing_non_class;
18363 else if (is_declaration
18364 && cp_lexer_next_token_is (parser->lexer,
18365 CPP_SEMICOLON))
18366 /* This is a `class-key identifier ;' */
18367 ts = ts_current;
18368 else
18369 ts = ts_global;
18370
18371 template_p =
18372 (template_parm_lists_apply
18373 && (cp_parser_next_token_starts_class_definition_p (parser)
18374 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18375 /* An unqualified name was used to reference this type, so
18376 there were no qualifying templates. */
18377 if (template_parm_lists_apply
18378 && !cp_parser_check_template_parameters (parser,
18379 /*num_templates=*/0,
18380 /*template_id*/false,
18381 token->location,
18382 /*declarator=*/NULL))
18383 return error_mark_node;
18384 type = xref_tag (tag_type, identifier, ts, template_p);
18385 }
18386 }
18387
18388 if (type == error_mark_node)
18389 return error_mark_node;
18390
18391 /* Allow attributes on forward declarations of classes. */
18392 if (attributes)
18393 {
18394 if (TREE_CODE (type) == TYPENAME_TYPE)
18395 warning (OPT_Wattributes,
18396 "attributes ignored on uninstantiated type");
18397 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18398 && ! processing_explicit_instantiation)
18399 warning (OPT_Wattributes,
18400 "attributes ignored on template instantiation");
18401 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18402 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18403 else
18404 warning (OPT_Wattributes,
18405 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18406 }
18407
18408 if (tag_type != enum_type)
18409 {
18410 /* Indicate whether this class was declared as a `class' or as a
18411 `struct'. */
18412 if (CLASS_TYPE_P (type))
18413 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18414 cp_parser_check_class_key (tag_type, type);
18415 }
18416
18417 /* A "<" cannot follow an elaborated type specifier. If that
18418 happens, the user was probably trying to form a template-id. */
18419 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18420 token->location);
18421
18422 return type;
18423 }
18424
18425 /* Parse an enum-specifier.
18426
18427 enum-specifier:
18428 enum-head { enumerator-list [opt] }
18429 enum-head { enumerator-list , } [C++0x]
18430
18431 enum-head:
18432 enum-key identifier [opt] enum-base [opt]
18433 enum-key nested-name-specifier identifier enum-base [opt]
18434
18435 enum-key:
18436 enum
18437 enum class [C++0x]
18438 enum struct [C++0x]
18439
18440 enum-base: [C++0x]
18441 : type-specifier-seq
18442
18443 opaque-enum-specifier:
18444 enum-key identifier enum-base [opt] ;
18445
18446 GNU Extensions:
18447 enum-key attributes[opt] identifier [opt] enum-base [opt]
18448 { enumerator-list [opt] }attributes[opt]
18449 enum-key attributes[opt] identifier [opt] enum-base [opt]
18450 { enumerator-list, }attributes[opt] [C++0x]
18451
18452 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18453 if the token stream isn't an enum-specifier after all. */
18454
18455 static tree
18456 cp_parser_enum_specifier (cp_parser* parser)
18457 {
18458 tree identifier;
18459 tree type = NULL_TREE;
18460 tree prev_scope;
18461 tree nested_name_specifier = NULL_TREE;
18462 tree attributes;
18463 bool scoped_enum_p = false;
18464 bool has_underlying_type = false;
18465 bool nested_being_defined = false;
18466 bool new_value_list = false;
18467 bool is_new_type = false;
18468 bool is_unnamed = false;
18469 tree underlying_type = NULL_TREE;
18470 cp_token *type_start_token = NULL;
18471 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18472
18473 parser->colon_corrects_to_scope_p = false;
18474
18475 /* Parse tentatively so that we can back up if we don't find a
18476 enum-specifier. */
18477 cp_parser_parse_tentatively (parser);
18478
18479 /* Caller guarantees that the current token is 'enum', an identifier
18480 possibly follows, and the token after that is an opening brace.
18481 If we don't have an identifier, fabricate an anonymous name for
18482 the enumeration being defined. */
18483 cp_lexer_consume_token (parser->lexer);
18484
18485 /* Parse the "class" or "struct", which indicates a scoped
18486 enumeration type in C++0x. */
18487 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18488 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18489 {
18490 if (cxx_dialect < cxx11)
18491 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18492
18493 /* Consume the `struct' or `class' token. */
18494 cp_lexer_consume_token (parser->lexer);
18495
18496 scoped_enum_p = true;
18497 }
18498
18499 attributes = cp_parser_attributes_opt (parser);
18500
18501 /* Clear the qualification. */
18502 parser->scope = NULL_TREE;
18503 parser->qualifying_scope = NULL_TREE;
18504 parser->object_scope = NULL_TREE;
18505
18506 /* Figure out in what scope the declaration is being placed. */
18507 prev_scope = current_scope ();
18508
18509 type_start_token = cp_lexer_peek_token (parser->lexer);
18510
18511 push_deferring_access_checks (dk_no_check);
18512 nested_name_specifier
18513 = cp_parser_nested_name_specifier_opt (parser,
18514 /*typename_keyword_p=*/true,
18515 /*check_dependency_p=*/false,
18516 /*type_p=*/false,
18517 /*is_declaration=*/false);
18518
18519 if (nested_name_specifier)
18520 {
18521 tree name;
18522
18523 identifier = cp_parser_identifier (parser);
18524 name = cp_parser_lookup_name (parser, identifier,
18525 enum_type,
18526 /*is_template=*/false,
18527 /*is_namespace=*/false,
18528 /*check_dependency=*/true,
18529 /*ambiguous_decls=*/NULL,
18530 input_location);
18531 if (name && name != error_mark_node)
18532 {
18533 type = TREE_TYPE (name);
18534 if (TREE_CODE (type) == TYPENAME_TYPE)
18535 {
18536 /* Are template enums allowed in ISO? */
18537 if (template_parm_scope_p ())
18538 pedwarn (type_start_token->location, OPT_Wpedantic,
18539 "%qD is an enumeration template", name);
18540 /* ignore a typename reference, for it will be solved by name
18541 in start_enum. */
18542 type = NULL_TREE;
18543 }
18544 }
18545 else if (nested_name_specifier == error_mark_node)
18546 /* We already issued an error. */;
18547 else
18548 {
18549 error_at (type_start_token->location,
18550 "%qD does not name an enumeration in %qT",
18551 identifier, nested_name_specifier);
18552 nested_name_specifier = error_mark_node;
18553 }
18554 }
18555 else
18556 {
18557 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18558 identifier = cp_parser_identifier (parser);
18559 else
18560 {
18561 identifier = make_anon_name ();
18562 is_unnamed = true;
18563 if (scoped_enum_p)
18564 error_at (type_start_token->location,
18565 "unnamed scoped enum is not allowed");
18566 }
18567 }
18568 pop_deferring_access_checks ();
18569
18570 /* Check for the `:' that denotes a specified underlying type in C++0x.
18571 Note that a ':' could also indicate a bitfield width, however. */
18572 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18573 {
18574 cp_decl_specifier_seq type_specifiers;
18575
18576 /* Consume the `:'. */
18577 cp_lexer_consume_token (parser->lexer);
18578
18579 /* Parse the type-specifier-seq. */
18580 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18581 /*is_trailing_return=*/false,
18582 &type_specifiers);
18583
18584 /* At this point this is surely not elaborated type specifier. */
18585 if (!cp_parser_parse_definitely (parser))
18586 return NULL_TREE;
18587
18588 if (cxx_dialect < cxx11)
18589 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18590
18591 has_underlying_type = true;
18592
18593 /* If that didn't work, stop. */
18594 if (type_specifiers.type != error_mark_node)
18595 {
18596 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18597 /*initialized=*/0, NULL);
18598 if (underlying_type == error_mark_node
18599 || check_for_bare_parameter_packs (underlying_type))
18600 underlying_type = NULL_TREE;
18601 }
18602 }
18603
18604 /* Look for the `{' but don't consume it yet. */
18605 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18606 {
18607 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18608 {
18609 cp_parser_error (parser, "expected %<{%>");
18610 if (has_underlying_type)
18611 {
18612 type = NULL_TREE;
18613 goto out;
18614 }
18615 }
18616 /* An opaque-enum-specifier must have a ';' here. */
18617 if ((scoped_enum_p || underlying_type)
18618 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18619 {
18620 cp_parser_error (parser, "expected %<;%> or %<{%>");
18621 if (has_underlying_type)
18622 {
18623 type = NULL_TREE;
18624 goto out;
18625 }
18626 }
18627 }
18628
18629 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18630 return NULL_TREE;
18631
18632 if (nested_name_specifier)
18633 {
18634 if (CLASS_TYPE_P (nested_name_specifier))
18635 {
18636 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18637 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18638 push_scope (nested_name_specifier);
18639 }
18640 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18641 {
18642 push_nested_namespace (nested_name_specifier);
18643 }
18644 }
18645
18646 /* Issue an error message if type-definitions are forbidden here. */
18647 if (!cp_parser_check_type_definition (parser))
18648 type = error_mark_node;
18649 else
18650 /* Create the new type. We do this before consuming the opening
18651 brace so the enum will be recorded as being on the line of its
18652 tag (or the 'enum' keyword, if there is no tag). */
18653 type = start_enum (identifier, type, underlying_type,
18654 attributes, scoped_enum_p, &is_new_type);
18655
18656 /* If the next token is not '{' it is an opaque-enum-specifier or an
18657 elaborated-type-specifier. */
18658 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18659 {
18660 timevar_push (TV_PARSE_ENUM);
18661 if (nested_name_specifier
18662 && nested_name_specifier != error_mark_node)
18663 {
18664 /* The following catches invalid code such as:
18665 enum class S<int>::E { A, B, C }; */
18666 if (!processing_specialization
18667 && CLASS_TYPE_P (nested_name_specifier)
18668 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18669 error_at (type_start_token->location, "cannot add an enumerator "
18670 "list to a template instantiation");
18671
18672 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18673 {
18674 error_at (type_start_token->location,
18675 "%<%T::%E%> has not been declared",
18676 TYPE_CONTEXT (nested_name_specifier),
18677 nested_name_specifier);
18678 type = error_mark_node;
18679 }
18680 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18681 && !CLASS_TYPE_P (nested_name_specifier))
18682 {
18683 error_at (type_start_token->location, "nested name specifier "
18684 "%qT for enum declaration does not name a class "
18685 "or namespace", nested_name_specifier);
18686 type = error_mark_node;
18687 }
18688 /* If that scope does not contain the scope in which the
18689 class was originally declared, the program is invalid. */
18690 else if (prev_scope && !is_ancestor (prev_scope,
18691 nested_name_specifier))
18692 {
18693 if (at_namespace_scope_p ())
18694 error_at (type_start_token->location,
18695 "declaration of %qD in namespace %qD which does not "
18696 "enclose %qD",
18697 type, prev_scope, nested_name_specifier);
18698 else
18699 error_at (type_start_token->location,
18700 "declaration of %qD in %qD which does not "
18701 "enclose %qD",
18702 type, prev_scope, nested_name_specifier);
18703 type = error_mark_node;
18704 }
18705 /* If that scope is the scope where the declaration is being placed
18706 the program is invalid. */
18707 else if (CLASS_TYPE_P (nested_name_specifier)
18708 && CLASS_TYPE_P (prev_scope)
18709 && same_type_p (nested_name_specifier, prev_scope))
18710 {
18711 permerror (type_start_token->location,
18712 "extra qualification not allowed");
18713 nested_name_specifier = NULL_TREE;
18714 }
18715 }
18716
18717 if (scoped_enum_p)
18718 begin_scope (sk_scoped_enum, type);
18719
18720 /* Consume the opening brace. */
18721 matching_braces braces;
18722 braces.consume_open (parser);
18723
18724 if (type == error_mark_node)
18725 ; /* Nothing to add */
18726 else if (OPAQUE_ENUM_P (type)
18727 || (cxx_dialect > cxx98 && processing_specialization))
18728 {
18729 new_value_list = true;
18730 SET_OPAQUE_ENUM_P (type, false);
18731 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18732 }
18733 else
18734 {
18735 error_at (type_start_token->location,
18736 "multiple definition of %q#T", type);
18737 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18738 "previous definition here");
18739 type = error_mark_node;
18740 }
18741
18742 if (type == error_mark_node)
18743 cp_parser_skip_to_end_of_block_or_statement (parser);
18744 /* If the next token is not '}', then there are some enumerators. */
18745 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18746 {
18747 if (is_unnamed && !scoped_enum_p)
18748 pedwarn (type_start_token->location, OPT_Wpedantic,
18749 "ISO C++ forbids empty unnamed enum");
18750 }
18751 else
18752 cp_parser_enumerator_list (parser, type);
18753
18754 /* Consume the final '}'. */
18755 braces.require_close (parser);
18756
18757 if (scoped_enum_p)
18758 finish_scope ();
18759 timevar_pop (TV_PARSE_ENUM);
18760 }
18761 else
18762 {
18763 /* If a ';' follows, then it is an opaque-enum-specifier
18764 and additional restrictions apply. */
18765 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18766 {
18767 if (is_unnamed)
18768 error_at (type_start_token->location,
18769 "opaque-enum-specifier without name");
18770 else if (nested_name_specifier)
18771 error_at (type_start_token->location,
18772 "opaque-enum-specifier must use a simple identifier");
18773 }
18774 }
18775
18776 /* Look for trailing attributes to apply to this enumeration, and
18777 apply them if appropriate. */
18778 if (cp_parser_allow_gnu_extensions_p (parser))
18779 {
18780 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18781 cplus_decl_attributes (&type,
18782 trailing_attr,
18783 (int) ATTR_FLAG_TYPE_IN_PLACE);
18784 }
18785
18786 /* Finish up the enumeration. */
18787 if (type != error_mark_node)
18788 {
18789 if (new_value_list)
18790 finish_enum_value_list (type);
18791 if (is_new_type)
18792 finish_enum (type);
18793 }
18794
18795 if (nested_name_specifier)
18796 {
18797 if (CLASS_TYPE_P (nested_name_specifier))
18798 {
18799 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18800 pop_scope (nested_name_specifier);
18801 }
18802 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18803 {
18804 pop_nested_namespace (nested_name_specifier);
18805 }
18806 }
18807 out:
18808 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18809 return type;
18810 }
18811
18812 /* Parse an enumerator-list. The enumerators all have the indicated
18813 TYPE.
18814
18815 enumerator-list:
18816 enumerator-definition
18817 enumerator-list , enumerator-definition */
18818
18819 static void
18820 cp_parser_enumerator_list (cp_parser* parser, tree type)
18821 {
18822 while (true)
18823 {
18824 /* Parse an enumerator-definition. */
18825 cp_parser_enumerator_definition (parser, type);
18826
18827 /* If the next token is not a ',', we've reached the end of
18828 the list. */
18829 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18830 break;
18831 /* Otherwise, consume the `,' and keep going. */
18832 cp_lexer_consume_token (parser->lexer);
18833 /* If the next token is a `}', there is a trailing comma. */
18834 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18835 {
18836 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18837 pedwarn (input_location, OPT_Wpedantic,
18838 "comma at end of enumerator list");
18839 break;
18840 }
18841 }
18842 }
18843
18844 /* Parse an enumerator-definition. The enumerator has the indicated
18845 TYPE.
18846
18847 enumerator-definition:
18848 enumerator
18849 enumerator = constant-expression
18850
18851 enumerator:
18852 identifier
18853
18854 GNU Extensions:
18855
18856 enumerator-definition:
18857 enumerator attributes [opt]
18858 enumerator attributes [opt] = constant-expression */
18859
18860 static void
18861 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18862 {
18863 tree identifier;
18864 tree value;
18865 location_t loc;
18866
18867 /* Save the input location because we are interested in the location
18868 of the identifier and not the location of the explicit value. */
18869 loc = cp_lexer_peek_token (parser->lexer)->location;
18870
18871 /* Look for the identifier. */
18872 identifier = cp_parser_identifier (parser);
18873 if (identifier == error_mark_node)
18874 return;
18875
18876 /* Parse any specified attributes. */
18877 tree attrs = cp_parser_attributes_opt (parser);
18878
18879 /* If the next token is an '=', then there is an explicit value. */
18880 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18881 {
18882 /* Consume the `=' token. */
18883 cp_lexer_consume_token (parser->lexer);
18884 /* Parse the value. */
18885 value = cp_parser_constant_expression (parser);
18886 }
18887 else
18888 value = NULL_TREE;
18889
18890 /* If we are processing a template, make sure the initializer of the
18891 enumerator doesn't contain any bare template parameter pack. */
18892 if (check_for_bare_parameter_packs (value))
18893 value = error_mark_node;
18894
18895 /* Create the enumerator. */
18896 build_enumerator (identifier, value, type, attrs, loc);
18897 }
18898
18899 /* Parse a namespace-name.
18900
18901 namespace-name:
18902 original-namespace-name
18903 namespace-alias
18904
18905 Returns the NAMESPACE_DECL for the namespace. */
18906
18907 static tree
18908 cp_parser_namespace_name (cp_parser* parser)
18909 {
18910 tree identifier;
18911 tree namespace_decl;
18912
18913 cp_token *token = cp_lexer_peek_token (parser->lexer);
18914
18915 /* Get the name of the namespace. */
18916 identifier = cp_parser_identifier (parser);
18917 if (identifier == error_mark_node)
18918 return error_mark_node;
18919
18920 /* Look up the identifier in the currently active scope. Look only
18921 for namespaces, due to:
18922
18923 [basic.lookup.udir]
18924
18925 When looking up a namespace-name in a using-directive or alias
18926 definition, only namespace names are considered.
18927
18928 And:
18929
18930 [basic.lookup.qual]
18931
18932 During the lookup of a name preceding the :: scope resolution
18933 operator, object, function, and enumerator names are ignored.
18934
18935 (Note that cp_parser_qualifying_entity only calls this
18936 function if the token after the name is the scope resolution
18937 operator.) */
18938 namespace_decl = cp_parser_lookup_name (parser, identifier,
18939 none_type,
18940 /*is_template=*/false,
18941 /*is_namespace=*/true,
18942 /*check_dependency=*/true,
18943 /*ambiguous_decls=*/NULL,
18944 token->location);
18945 /* If it's not a namespace, issue an error. */
18946 if (namespace_decl == error_mark_node
18947 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18948 {
18949 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18950 {
18951 auto_diagnostic_group d;
18952 name_hint hint;
18953 if (namespace_decl == error_mark_node
18954 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
18955 hint = suggest_alternative_in_explicit_scope (token->location,
18956 identifier,
18957 parser->scope);
18958 if (const char *suggestion = hint.suggestion ())
18959 {
18960 gcc_rich_location richloc (token->location);
18961 richloc.add_fixit_replace (suggestion);
18962 error_at (&richloc,
18963 "%qD is not a namespace-name; did you mean %qs?",
18964 identifier, suggestion);
18965 }
18966 else
18967 error_at (token->location, "%qD is not a namespace-name",
18968 identifier);
18969 }
18970 else
18971 cp_parser_error (parser, "expected namespace-name");
18972 namespace_decl = error_mark_node;
18973 }
18974
18975 return namespace_decl;
18976 }
18977
18978 /* Parse a namespace-definition.
18979
18980 namespace-definition:
18981 named-namespace-definition
18982 unnamed-namespace-definition
18983
18984 named-namespace-definition:
18985 original-namespace-definition
18986 extension-namespace-definition
18987
18988 original-namespace-definition:
18989 namespace identifier { namespace-body }
18990
18991 extension-namespace-definition:
18992 namespace original-namespace-name { namespace-body }
18993
18994 unnamed-namespace-definition:
18995 namespace { namespace-body } */
18996
18997 static void
18998 cp_parser_namespace_definition (cp_parser* parser)
18999 {
19000 tree identifier;
19001 int nested_definition_count = 0;
19002
19003 cp_ensure_no_omp_declare_simd (parser);
19004 cp_ensure_no_oacc_routine (parser);
19005
19006 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19007 const bool topmost_inline_p = is_inline;
19008
19009 if (is_inline)
19010 {
19011 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19012 cp_lexer_consume_token (parser->lexer);
19013 }
19014
19015 /* Look for the `namespace' keyword. */
19016 cp_token* token
19017 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19018
19019 /* Parse any specified attributes before the identifier. */
19020 tree attribs = cp_parser_attributes_opt (parser);
19021
19022 for (;;)
19023 {
19024 identifier = NULL_TREE;
19025
19026 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19027 RID_INLINE);
19028 if (nested_inline_p && nested_definition_count != 0)
19029 {
19030 if (cxx_dialect < cxx2a)
19031 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19032 OPT_Wpedantic, "nested inline namespace definitions only "
19033 "available with -std=c++2a or -std=gnu++2a");
19034 cp_lexer_consume_token (parser->lexer);
19035 }
19036
19037 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19038 {
19039 identifier = cp_parser_identifier (parser);
19040
19041 if (cp_next_tokens_can_be_std_attribute_p (parser))
19042 pedwarn (input_location, OPT_Wpedantic,
19043 "standard attributes on namespaces must precede "
19044 "the namespace name");
19045
19046 /* Parse any attributes specified after the identifier. */
19047 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19048 }
19049
19050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19051 {
19052 /* Don't forget that the innermost namespace might have been
19053 marked as inline. Use |= because we cannot overwrite
19054 IS_INLINE in case the outermost namespace is inline, but
19055 there are no nested inlines. */
19056 is_inline |= nested_inline_p;
19057 break;
19058 }
19059
19060 if (!nested_definition_count && cxx_dialect < cxx17)
19061 pedwarn (input_location, OPT_Wpedantic,
19062 "nested namespace definitions only available with "
19063 "-std=c++17 or -std=gnu++17");
19064
19065 /* Nested namespace names can create new namespaces (unlike
19066 other qualified-ids). */
19067 if (int count = (identifier
19068 ? push_namespace (identifier, nested_inline_p)
19069 : 0))
19070 nested_definition_count += count;
19071 else
19072 cp_parser_error (parser, "nested namespace name required");
19073 cp_lexer_consume_token (parser->lexer);
19074 }
19075
19076 if (nested_definition_count && !identifier)
19077 cp_parser_error (parser, "namespace name required");
19078
19079 if (nested_definition_count && attribs)
19080 error_at (token->location,
19081 "a nested namespace definition cannot have attributes");
19082 if (nested_definition_count && topmost_inline_p)
19083 error_at (token->location,
19084 "a nested namespace definition cannot be inline");
19085
19086 /* Start the namespace. */
19087 nested_definition_count += push_namespace (identifier, is_inline);
19088
19089 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19090
19091 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19092
19093 /* Look for the `{' to validate starting the namespace. */
19094 matching_braces braces;
19095 if (braces.require_open (parser))
19096 {
19097 /* Parse the body of the namespace. */
19098 cp_parser_namespace_body (parser);
19099
19100 /* Look for the final `}'. */
19101 braces.require_close (parser);
19102 }
19103
19104 if (has_visibility)
19105 pop_visibility (1);
19106
19107 /* Pop the nested namespace definitions. */
19108 while (nested_definition_count--)
19109 pop_namespace ();
19110 }
19111
19112 /* Parse a namespace-body.
19113
19114 namespace-body:
19115 declaration-seq [opt] */
19116
19117 static void
19118 cp_parser_namespace_body (cp_parser* parser)
19119 {
19120 cp_parser_declaration_seq_opt (parser);
19121 }
19122
19123 /* Parse a namespace-alias-definition.
19124
19125 namespace-alias-definition:
19126 namespace identifier = qualified-namespace-specifier ; */
19127
19128 static void
19129 cp_parser_namespace_alias_definition (cp_parser* parser)
19130 {
19131 tree identifier;
19132 tree namespace_specifier;
19133
19134 cp_token *token = cp_lexer_peek_token (parser->lexer);
19135
19136 /* Look for the `namespace' keyword. */
19137 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19138 /* Look for the identifier. */
19139 identifier = cp_parser_identifier (parser);
19140 if (identifier == error_mark_node)
19141 return;
19142 /* Look for the `=' token. */
19143 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19144 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19145 {
19146 error_at (token->location, "%<namespace%> definition is not allowed here");
19147 /* Skip the definition. */
19148 cp_lexer_consume_token (parser->lexer);
19149 if (cp_parser_skip_to_closing_brace (parser))
19150 cp_lexer_consume_token (parser->lexer);
19151 return;
19152 }
19153 cp_parser_require (parser, CPP_EQ, RT_EQ);
19154 /* Look for the qualified-namespace-specifier. */
19155 namespace_specifier
19156 = cp_parser_qualified_namespace_specifier (parser);
19157 /* Look for the `;' token. */
19158 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19159
19160 /* Register the alias in the symbol table. */
19161 do_namespace_alias (identifier, namespace_specifier);
19162 }
19163
19164 /* Parse a qualified-namespace-specifier.
19165
19166 qualified-namespace-specifier:
19167 :: [opt] nested-name-specifier [opt] namespace-name
19168
19169 Returns a NAMESPACE_DECL corresponding to the specified
19170 namespace. */
19171
19172 static tree
19173 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19174 {
19175 /* Look for the optional `::'. */
19176 cp_parser_global_scope_opt (parser,
19177 /*current_scope_valid_p=*/false);
19178
19179 /* Look for the optional nested-name-specifier. */
19180 cp_parser_nested_name_specifier_opt (parser,
19181 /*typename_keyword_p=*/false,
19182 /*check_dependency_p=*/true,
19183 /*type_p=*/false,
19184 /*is_declaration=*/true);
19185
19186 return cp_parser_namespace_name (parser);
19187 }
19188
19189 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19190 access declaration.
19191
19192 using-declaration:
19193 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19194 using :: unqualified-id ;
19195
19196 access-declaration:
19197 qualified-id ;
19198
19199 */
19200
19201 static bool
19202 cp_parser_using_declaration (cp_parser* parser,
19203 bool access_declaration_p)
19204 {
19205 cp_token *token;
19206 bool typename_p = false;
19207 bool global_scope_p;
19208 tree decl;
19209 tree identifier;
19210 tree qscope;
19211 int oldcount = errorcount;
19212 cp_token *diag_token = NULL;
19213
19214 if (access_declaration_p)
19215 {
19216 diag_token = cp_lexer_peek_token (parser->lexer);
19217 cp_parser_parse_tentatively (parser);
19218 }
19219 else
19220 {
19221 /* Look for the `using' keyword. */
19222 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19223
19224 again:
19225 /* Peek at the next token. */
19226 token = cp_lexer_peek_token (parser->lexer);
19227 /* See if it's `typename'. */
19228 if (token->keyword == RID_TYPENAME)
19229 {
19230 /* Remember that we've seen it. */
19231 typename_p = true;
19232 /* Consume the `typename' token. */
19233 cp_lexer_consume_token (parser->lexer);
19234 }
19235 }
19236
19237 /* Look for the optional global scope qualification. */
19238 global_scope_p
19239 = (cp_parser_global_scope_opt (parser,
19240 /*current_scope_valid_p=*/false)
19241 != NULL_TREE);
19242
19243 /* If we saw `typename', or didn't see `::', then there must be a
19244 nested-name-specifier present. */
19245 if (typename_p || !global_scope_p)
19246 {
19247 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19248 /*check_dependency_p=*/true,
19249 /*type_p=*/false,
19250 /*is_declaration=*/true);
19251 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19252 {
19253 cp_parser_skip_to_end_of_block_or_statement (parser);
19254 return false;
19255 }
19256 }
19257 /* Otherwise, we could be in either of the two productions. In that
19258 case, treat the nested-name-specifier as optional. */
19259 else
19260 qscope = cp_parser_nested_name_specifier_opt (parser,
19261 /*typename_keyword_p=*/false,
19262 /*check_dependency_p=*/true,
19263 /*type_p=*/false,
19264 /*is_declaration=*/true);
19265 if (!qscope)
19266 qscope = global_namespace;
19267 else if (UNSCOPED_ENUM_P (qscope))
19268 qscope = CP_TYPE_CONTEXT (qscope);
19269
19270 if (access_declaration_p && cp_parser_error_occurred (parser))
19271 /* Something has already gone wrong; there's no need to parse
19272 further. Since an error has occurred, the return value of
19273 cp_parser_parse_definitely will be false, as required. */
19274 return cp_parser_parse_definitely (parser);
19275
19276 token = cp_lexer_peek_token (parser->lexer);
19277 /* Parse the unqualified-id. */
19278 identifier = cp_parser_unqualified_id (parser,
19279 /*template_keyword_p=*/false,
19280 /*check_dependency_p=*/true,
19281 /*declarator_p=*/true,
19282 /*optional_p=*/false);
19283
19284 if (access_declaration_p)
19285 {
19286 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19287 cp_parser_simulate_error (parser);
19288 if (!cp_parser_parse_definitely (parser))
19289 return false;
19290 }
19291 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19292 {
19293 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19294 if (cxx_dialect < cxx17
19295 && !in_system_header_at (ell->location))
19296 pedwarn (ell->location, 0,
19297 "pack expansion in using-declaration only available "
19298 "with -std=c++17 or -std=gnu++17");
19299 qscope = make_pack_expansion (qscope);
19300 }
19301
19302 /* The function we call to handle a using-declaration is different
19303 depending on what scope we are in. */
19304 if (qscope == error_mark_node || identifier == error_mark_node)
19305 ;
19306 else if (!identifier_p (identifier)
19307 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19308 /* [namespace.udecl]
19309
19310 A using declaration shall not name a template-id. */
19311 error_at (token->location,
19312 "a template-id may not appear in a using-declaration");
19313 else
19314 {
19315 if (at_class_scope_p ())
19316 {
19317 /* Create the USING_DECL. */
19318 decl = do_class_using_decl (qscope, identifier);
19319
19320 if (decl && typename_p)
19321 USING_DECL_TYPENAME_P (decl) = 1;
19322
19323 if (check_for_bare_parameter_packs (decl))
19324 {
19325 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19326 return false;
19327 }
19328 else
19329 /* Add it to the list of members in this class. */
19330 finish_member_declaration (decl);
19331 }
19332 else
19333 {
19334 decl = cp_parser_lookup_name_simple (parser,
19335 identifier,
19336 token->location);
19337 if (decl == error_mark_node)
19338 cp_parser_name_lookup_error (parser, identifier,
19339 decl, NLE_NULL,
19340 token->location);
19341 else if (check_for_bare_parameter_packs (decl))
19342 {
19343 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19344 return false;
19345 }
19346 else if (!at_namespace_scope_p ())
19347 finish_local_using_decl (decl, qscope, identifier);
19348 else
19349 finish_namespace_using_decl (decl, qscope, identifier);
19350 }
19351 }
19352
19353 if (!access_declaration_p
19354 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19355 {
19356 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19357 if (cxx_dialect < cxx17)
19358 pedwarn (comma->location, 0,
19359 "comma-separated list in using-declaration only available "
19360 "with -std=c++17 or -std=gnu++17");
19361 goto again;
19362 }
19363
19364 /* Look for the final `;'. */
19365 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19366
19367 if (access_declaration_p && errorcount == oldcount)
19368 warning_at (diag_token->location, OPT_Wdeprecated,
19369 "access declarations are deprecated "
19370 "in favour of using-declarations; "
19371 "suggestion: add the %<using%> keyword");
19372
19373 return true;
19374 }
19375
19376 /* Parse an alias-declaration.
19377
19378 alias-declaration:
19379 using identifier attribute-specifier-seq [opt] = type-id */
19380
19381 static tree
19382 cp_parser_alias_declaration (cp_parser* parser)
19383 {
19384 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19385 location_t id_location, type_location;
19386 cp_declarator *declarator;
19387 cp_decl_specifier_seq decl_specs;
19388 bool member_p;
19389 const char *saved_message = NULL;
19390
19391 /* Look for the `using' keyword. */
19392 cp_token *using_token
19393 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19394 if (using_token == NULL)
19395 return error_mark_node;
19396
19397 id_location = cp_lexer_peek_token (parser->lexer)->location;
19398 id = cp_parser_identifier (parser);
19399 if (id == error_mark_node)
19400 return error_mark_node;
19401
19402 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19403 attributes = cp_parser_attributes_opt (parser);
19404 if (attributes == error_mark_node)
19405 return error_mark_node;
19406
19407 cp_parser_require (parser, CPP_EQ, RT_EQ);
19408
19409 if (cp_parser_error_occurred (parser))
19410 return error_mark_node;
19411
19412 cp_parser_commit_to_tentative_parse (parser);
19413
19414 /* Now we are going to parse the type-id of the declaration. */
19415
19416 /*
19417 [dcl.type]/3 says:
19418
19419 "A type-specifier-seq shall not define a class or enumeration
19420 unless it appears in the type-id of an alias-declaration (7.1.3) that
19421 is not the declaration of a template-declaration."
19422
19423 In other words, if we currently are in an alias template, the
19424 type-id should not define a type.
19425
19426 So let's set parser->type_definition_forbidden_message in that
19427 case; cp_parser_check_type_definition (called by
19428 cp_parser_class_specifier) will then emit an error if a type is
19429 defined in the type-id. */
19430 if (parser->num_template_parameter_lists)
19431 {
19432 saved_message = parser->type_definition_forbidden_message;
19433 parser->type_definition_forbidden_message =
19434 G_("types may not be defined in alias template declarations");
19435 }
19436
19437 type = cp_parser_type_id (parser, &type_location);
19438
19439 /* Restore the error message if need be. */
19440 if (parser->num_template_parameter_lists)
19441 parser->type_definition_forbidden_message = saved_message;
19442
19443 if (type == error_mark_node
19444 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19445 {
19446 cp_parser_skip_to_end_of_block_or_statement (parser);
19447 return error_mark_node;
19448 }
19449
19450 /* A typedef-name can also be introduced by an alias-declaration. The
19451 identifier following the using keyword becomes a typedef-name. It has
19452 the same semantics as if it were introduced by the typedef
19453 specifier. In particular, it does not define a new type and it shall
19454 not appear in the type-id. */
19455
19456 clear_decl_specs (&decl_specs);
19457 decl_specs.type = type;
19458 if (attributes != NULL_TREE)
19459 {
19460 decl_specs.attributes = attributes;
19461 set_and_check_decl_spec_loc (&decl_specs,
19462 ds_attribute,
19463 attrs_token);
19464 }
19465 set_and_check_decl_spec_loc (&decl_specs,
19466 ds_typedef,
19467 using_token);
19468 set_and_check_decl_spec_loc (&decl_specs,
19469 ds_alias,
19470 using_token);
19471 decl_specs.locations[ds_type_spec] = type_location;
19472
19473 if (parser->num_template_parameter_lists
19474 && !cp_parser_check_template_parameters (parser,
19475 /*num_templates=*/0,
19476 /*template_id*/false,
19477 id_location,
19478 /*declarator=*/NULL))
19479 return error_mark_node;
19480
19481 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19482
19483 member_p = at_class_scope_p ();
19484 if (member_p)
19485 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19486 NULL_TREE, attributes);
19487 else
19488 decl = start_decl (declarator, &decl_specs, 0,
19489 attributes, NULL_TREE, &pushed_scope);
19490 if (decl == error_mark_node)
19491 return decl;
19492
19493 // Attach constraints to the alias declaration.
19494 if (flag_concepts && current_template_parms)
19495 {
19496 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19497 tree constr = build_constraints (reqs, NULL_TREE);
19498 set_constraints (decl, constr);
19499 }
19500
19501 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19502
19503 if (pushed_scope)
19504 pop_scope (pushed_scope);
19505
19506 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19507 added into the symbol table; otherwise, return the TYPE_DECL. */
19508 if (DECL_LANG_SPECIFIC (decl)
19509 && DECL_TEMPLATE_INFO (decl)
19510 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19511 {
19512 decl = DECL_TI_TEMPLATE (decl);
19513 if (member_p)
19514 check_member_template (decl);
19515 }
19516
19517 return decl;
19518 }
19519
19520 /* Parse a using-directive.
19521
19522 using-directive:
19523 using namespace :: [opt] nested-name-specifier [opt]
19524 namespace-name ; */
19525
19526 static void
19527 cp_parser_using_directive (cp_parser* parser)
19528 {
19529 tree namespace_decl;
19530 tree attribs;
19531
19532 /* Look for the `using' keyword. */
19533 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19534 /* And the `namespace' keyword. */
19535 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19536 /* Look for the optional `::' operator. */
19537 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19538 /* And the optional nested-name-specifier. */
19539 cp_parser_nested_name_specifier_opt (parser,
19540 /*typename_keyword_p=*/false,
19541 /*check_dependency_p=*/true,
19542 /*type_p=*/false,
19543 /*is_declaration=*/true);
19544 /* Get the namespace being used. */
19545 namespace_decl = cp_parser_namespace_name (parser);
19546 /* And any specified attributes. */
19547 attribs = cp_parser_attributes_opt (parser);
19548
19549 /* Update the symbol table. */
19550 if (namespace_bindings_p ())
19551 finish_namespace_using_directive (namespace_decl, attribs);
19552 else
19553 finish_local_using_directive (namespace_decl, attribs);
19554
19555 /* Look for the final `;'. */
19556 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19557 }
19558
19559 /* Parse an asm-definition.
19560
19561 asm-definition:
19562 asm ( string-literal ) ;
19563
19564 GNU Extension:
19565
19566 asm-definition:
19567 asm volatile [opt] ( string-literal ) ;
19568 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19569 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19570 : asm-operand-list [opt] ) ;
19571 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19572 : asm-operand-list [opt]
19573 : asm-clobber-list [opt] ) ;
19574 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19575 : asm-clobber-list [opt]
19576 : asm-goto-list ) ; */
19577
19578 static void
19579 cp_parser_asm_definition (cp_parser* parser)
19580 {
19581 tree string;
19582 tree outputs = NULL_TREE;
19583 tree inputs = NULL_TREE;
19584 tree clobbers = NULL_TREE;
19585 tree labels = NULL_TREE;
19586 tree asm_stmt;
19587 bool volatile_p = false;
19588 bool extended_p = false;
19589 bool invalid_inputs_p = false;
19590 bool invalid_outputs_p = false;
19591 bool goto_p = false;
19592 required_token missing = RT_NONE;
19593
19594 /* Look for the `asm' keyword. */
19595 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19596
19597 if (parser->in_function_body
19598 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19599 {
19600 error ("%<asm%> in %<constexpr%> function");
19601 cp_function_chain->invalid_constexpr = true;
19602 }
19603
19604 /* See if the next token is `volatile'. */
19605 if (cp_parser_allow_gnu_extensions_p (parser)
19606 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19607 {
19608 /* Remember that we saw the `volatile' keyword. */
19609 volatile_p = true;
19610 /* Consume the token. */
19611 cp_lexer_consume_token (parser->lexer);
19612 }
19613 if (cp_parser_allow_gnu_extensions_p (parser)
19614 && parser->in_function_body
19615 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19616 {
19617 /* Remember that we saw the `goto' keyword. */
19618 goto_p = true;
19619 /* Consume the token. */
19620 cp_lexer_consume_token (parser->lexer);
19621 }
19622 /* Look for the opening `('. */
19623 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19624 return;
19625 /* Look for the string. */
19626 string = cp_parser_string_literal (parser, false, false);
19627 if (string == error_mark_node)
19628 {
19629 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19630 /*consume_paren=*/true);
19631 return;
19632 }
19633
19634 /* If we're allowing GNU extensions, check for the extended assembly
19635 syntax. Unfortunately, the `:' tokens need not be separated by
19636 a space in C, and so, for compatibility, we tolerate that here
19637 too. Doing that means that we have to treat the `::' operator as
19638 two `:' tokens. */
19639 if (cp_parser_allow_gnu_extensions_p (parser)
19640 && parser->in_function_body
19641 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19642 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19643 {
19644 bool inputs_p = false;
19645 bool clobbers_p = false;
19646 bool labels_p = false;
19647
19648 /* The extended syntax was used. */
19649 extended_p = true;
19650
19651 /* Look for outputs. */
19652 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19653 {
19654 /* Consume the `:'. */
19655 cp_lexer_consume_token (parser->lexer);
19656 /* Parse the output-operands. */
19657 if (cp_lexer_next_token_is_not (parser->lexer,
19658 CPP_COLON)
19659 && cp_lexer_next_token_is_not (parser->lexer,
19660 CPP_SCOPE)
19661 && cp_lexer_next_token_is_not (parser->lexer,
19662 CPP_CLOSE_PAREN)
19663 && !goto_p)
19664 {
19665 outputs = cp_parser_asm_operand_list (parser);
19666 if (outputs == error_mark_node)
19667 invalid_outputs_p = true;
19668 }
19669 }
19670 /* If the next token is `::', there are no outputs, and the
19671 next token is the beginning of the inputs. */
19672 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19673 /* The inputs are coming next. */
19674 inputs_p = true;
19675
19676 /* Look for inputs. */
19677 if (inputs_p
19678 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19679 {
19680 /* Consume the `:' or `::'. */
19681 cp_lexer_consume_token (parser->lexer);
19682 /* Parse the output-operands. */
19683 if (cp_lexer_next_token_is_not (parser->lexer,
19684 CPP_COLON)
19685 && cp_lexer_next_token_is_not (parser->lexer,
19686 CPP_SCOPE)
19687 && cp_lexer_next_token_is_not (parser->lexer,
19688 CPP_CLOSE_PAREN))
19689 {
19690 inputs = cp_parser_asm_operand_list (parser);
19691 if (inputs == error_mark_node)
19692 invalid_inputs_p = true;
19693 }
19694 }
19695 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19696 /* The clobbers are coming next. */
19697 clobbers_p = true;
19698
19699 /* Look for clobbers. */
19700 if (clobbers_p
19701 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19702 {
19703 clobbers_p = true;
19704 /* Consume the `:' or `::'. */
19705 cp_lexer_consume_token (parser->lexer);
19706 /* Parse the clobbers. */
19707 if (cp_lexer_next_token_is_not (parser->lexer,
19708 CPP_COLON)
19709 && cp_lexer_next_token_is_not (parser->lexer,
19710 CPP_CLOSE_PAREN))
19711 clobbers = cp_parser_asm_clobber_list (parser);
19712 }
19713 else if (goto_p
19714 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19715 /* The labels are coming next. */
19716 labels_p = true;
19717
19718 /* Look for labels. */
19719 if (labels_p
19720 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19721 {
19722 labels_p = true;
19723 /* Consume the `:' or `::'. */
19724 cp_lexer_consume_token (parser->lexer);
19725 /* Parse the labels. */
19726 labels = cp_parser_asm_label_list (parser);
19727 }
19728
19729 if (goto_p && !labels_p)
19730 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19731 }
19732 else if (goto_p)
19733 missing = RT_COLON_SCOPE;
19734
19735 /* Look for the closing `)'. */
19736 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19737 missing ? missing : RT_CLOSE_PAREN))
19738 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19739 /*consume_paren=*/true);
19740 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19741
19742 if (!invalid_inputs_p && !invalid_outputs_p)
19743 {
19744 /* Create the ASM_EXPR. */
19745 if (parser->in_function_body)
19746 {
19747 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19748 inputs, clobbers, labels);
19749 /* If the extended syntax was not used, mark the ASM_EXPR. */
19750 if (!extended_p)
19751 {
19752 tree temp = asm_stmt;
19753 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19754 temp = TREE_OPERAND (temp, 0);
19755
19756 ASM_INPUT_P (temp) = 1;
19757 }
19758 }
19759 else
19760 symtab->finalize_toplevel_asm (string);
19761 }
19762 }
19763
19764 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19765 type that comes from the decl-specifier-seq. */
19766
19767 static tree
19768 strip_declarator_types (tree type, cp_declarator *declarator)
19769 {
19770 for (cp_declarator *d = declarator; d;)
19771 switch (d->kind)
19772 {
19773 case cdk_id:
19774 case cdk_decomp:
19775 case cdk_error:
19776 d = NULL;
19777 break;
19778
19779 default:
19780 if (TYPE_PTRMEMFUNC_P (type))
19781 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19782 type = TREE_TYPE (type);
19783 d = d->declarator;
19784 break;
19785 }
19786
19787 return type;
19788 }
19789
19790 /* Declarators [gram.dcl.decl] */
19791
19792 /* Parse an init-declarator.
19793
19794 init-declarator:
19795 declarator initializer [opt]
19796
19797 GNU Extension:
19798
19799 init-declarator:
19800 declarator asm-specification [opt] attributes [opt] initializer [opt]
19801
19802 function-definition:
19803 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19804 function-body
19805 decl-specifier-seq [opt] declarator function-try-block
19806
19807 GNU Extension:
19808
19809 function-definition:
19810 __extension__ function-definition
19811
19812 TM Extension:
19813
19814 function-definition:
19815 decl-specifier-seq [opt] declarator function-transaction-block
19816
19817 The DECL_SPECIFIERS apply to this declarator. Returns a
19818 representation of the entity declared. If MEMBER_P is TRUE, then
19819 this declarator appears in a class scope. The new DECL created by
19820 this declarator is returned.
19821
19822 The CHECKS are access checks that should be performed once we know
19823 what entity is being declared (and, therefore, what classes have
19824 befriended it).
19825
19826 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19827 for a function-definition here as well. If the declarator is a
19828 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19829 be TRUE upon return. By that point, the function-definition will
19830 have been completely parsed.
19831
19832 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19833 is FALSE.
19834
19835 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19836 parsed declaration if it is an uninitialized single declarator not followed
19837 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19838 if present, will not be consumed. If returned, this declarator will be
19839 created with SD_INITIALIZED but will not call cp_finish_decl.
19840
19841 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19842 and there is an initializer, the pointed location_t is set to the
19843 location of the '=' or `(', or '{' in C++11 token introducing the
19844 initializer. */
19845
19846 static tree
19847 cp_parser_init_declarator (cp_parser* parser,
19848 cp_decl_specifier_seq *decl_specifiers,
19849 vec<deferred_access_check, va_gc> *checks,
19850 bool function_definition_allowed_p,
19851 bool member_p,
19852 int declares_class_or_enum,
19853 bool* function_definition_p,
19854 tree* maybe_range_for_decl,
19855 location_t* init_loc,
19856 tree* auto_result)
19857 {
19858 cp_token *token = NULL, *asm_spec_start_token = NULL,
19859 *attributes_start_token = NULL;
19860 cp_declarator *declarator;
19861 tree prefix_attributes;
19862 tree attributes = NULL;
19863 tree asm_specification;
19864 tree initializer;
19865 tree decl = NULL_TREE;
19866 tree scope;
19867 int is_initialized;
19868 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19869 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19870 "(...)". */
19871 enum cpp_ttype initialization_kind;
19872 bool is_direct_init = false;
19873 bool is_non_constant_init;
19874 int ctor_dtor_or_conv_p;
19875 bool friend_p = cp_parser_friend_p (decl_specifiers);
19876 tree pushed_scope = NULL_TREE;
19877 bool range_for_decl_p = false;
19878 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19879 location_t tmp_init_loc = UNKNOWN_LOCATION;
19880
19881 /* Gather the attributes that were provided with the
19882 decl-specifiers. */
19883 prefix_attributes = decl_specifiers->attributes;
19884
19885 /* Assume that this is not the declarator for a function
19886 definition. */
19887 if (function_definition_p)
19888 *function_definition_p = false;
19889
19890 /* Default arguments are only permitted for function parameters. */
19891 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19892 parser->default_arg_ok_p = false;
19893
19894 /* Defer access checks while parsing the declarator; we cannot know
19895 what names are accessible until we know what is being
19896 declared. */
19897 resume_deferring_access_checks ();
19898
19899 token = cp_lexer_peek_token (parser->lexer);
19900
19901 /* Parse the declarator. */
19902 declarator
19903 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19904 &ctor_dtor_or_conv_p,
19905 /*parenthesized_p=*/NULL,
19906 member_p, friend_p);
19907 /* Gather up the deferred checks. */
19908 stop_deferring_access_checks ();
19909
19910 parser->default_arg_ok_p = saved_default_arg_ok_p;
19911
19912 /* If the DECLARATOR was erroneous, there's no need to go
19913 further. */
19914 if (declarator == cp_error_declarator)
19915 return error_mark_node;
19916
19917 /* Check that the number of template-parameter-lists is OK. */
19918 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19919 token->location))
19920 return error_mark_node;
19921
19922 if (declares_class_or_enum & 2)
19923 cp_parser_check_for_definition_in_return_type (declarator,
19924 decl_specifiers->type,
19925 decl_specifiers->locations[ds_type_spec]);
19926
19927 /* Figure out what scope the entity declared by the DECLARATOR is
19928 located in. `grokdeclarator' sometimes changes the scope, so
19929 we compute it now. */
19930 scope = get_scope_of_declarator (declarator);
19931
19932 /* Perform any lookups in the declared type which were thought to be
19933 dependent, but are not in the scope of the declarator. */
19934 decl_specifiers->type
19935 = maybe_update_decl_type (decl_specifiers->type, scope);
19936
19937 /* If we're allowing GNU extensions, look for an
19938 asm-specification. */
19939 if (cp_parser_allow_gnu_extensions_p (parser))
19940 {
19941 /* Look for an asm-specification. */
19942 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19943 asm_specification = cp_parser_asm_specification_opt (parser);
19944 }
19945 else
19946 asm_specification = NULL_TREE;
19947
19948 /* Look for attributes. */
19949 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19950 attributes = cp_parser_attributes_opt (parser);
19951
19952 /* Peek at the next token. */
19953 token = cp_lexer_peek_token (parser->lexer);
19954
19955 bool bogus_implicit_tmpl = false;
19956
19957 if (function_declarator_p (declarator))
19958 {
19959 /* Handle C++17 deduction guides. */
19960 if (!decl_specifiers->type
19961 && ctor_dtor_or_conv_p <= 0
19962 && cxx_dialect >= cxx17)
19963 {
19964 cp_declarator *id = get_id_declarator (declarator);
19965 tree name = id->u.id.unqualified_name;
19966 parser->scope = id->u.id.qualifying_scope;
19967 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19968 if (tmpl
19969 && (DECL_CLASS_TEMPLATE_P (tmpl)
19970 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19971 {
19972 id->u.id.unqualified_name = dguide_name (tmpl);
19973 id->u.id.sfk = sfk_deduction_guide;
19974 ctor_dtor_or_conv_p = 1;
19975 }
19976 }
19977
19978 /* Check to see if the token indicates the start of a
19979 function-definition. */
19980 if (cp_parser_token_starts_function_definition_p (token))
19981 {
19982 if (!function_definition_allowed_p)
19983 {
19984 /* If a function-definition should not appear here, issue an
19985 error message. */
19986 cp_parser_error (parser,
19987 "a function-definition is not allowed here");
19988 return error_mark_node;
19989 }
19990
19991 location_t func_brace_location
19992 = cp_lexer_peek_token (parser->lexer)->location;
19993
19994 /* Neither attributes nor an asm-specification are allowed
19995 on a function-definition. */
19996 if (asm_specification)
19997 error_at (asm_spec_start_token->location,
19998 "an asm-specification is not allowed "
19999 "on a function-definition");
20000 if (attributes)
20001 error_at (attributes_start_token->location,
20002 "attributes are not allowed "
20003 "on a function-definition");
20004 /* This is a function-definition. */
20005 *function_definition_p = true;
20006
20007 /* Parse the function definition. */
20008 if (member_p)
20009 decl = cp_parser_save_member_function_body (parser,
20010 decl_specifiers,
20011 declarator,
20012 prefix_attributes);
20013 else
20014 decl =
20015 (cp_parser_function_definition_from_specifiers_and_declarator
20016 (parser, decl_specifiers, prefix_attributes, declarator));
20017
20018 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20019 {
20020 /* This is where the prologue starts... */
20021 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20022 = func_brace_location;
20023 }
20024
20025 return decl;
20026 }
20027 }
20028 else if (parser->fully_implicit_function_template_p)
20029 {
20030 /* A non-template declaration involving a function parameter list
20031 containing an implicit template parameter will be made into a
20032 template. If the resulting declaration is not going to be an
20033 actual function then finish the template scope here to prevent it.
20034 An error message will be issued once we have a decl to talk about.
20035
20036 FIXME probably we should do type deduction rather than create an
20037 implicit template, but the standard currently doesn't allow it. */
20038 bogus_implicit_tmpl = true;
20039 finish_fully_implicit_template (parser, NULL_TREE);
20040 }
20041
20042 /* [dcl.dcl]
20043
20044 Only in function declarations for constructors, destructors, type
20045 conversions, and deduction guides can the decl-specifier-seq be omitted.
20046
20047 We explicitly postpone this check past the point where we handle
20048 function-definitions because we tolerate function-definitions
20049 that are missing their return types in some modes. */
20050 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20051 {
20052 cp_parser_error (parser,
20053 "expected constructor, destructor, or type conversion");
20054 return error_mark_node;
20055 }
20056
20057 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20058 if (token->type == CPP_EQ
20059 || token->type == CPP_OPEN_PAREN
20060 || token->type == CPP_OPEN_BRACE)
20061 {
20062 is_initialized = SD_INITIALIZED;
20063 initialization_kind = token->type;
20064 if (maybe_range_for_decl)
20065 *maybe_range_for_decl = error_mark_node;
20066 tmp_init_loc = token->location;
20067 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20068 *init_loc = tmp_init_loc;
20069
20070 if (token->type == CPP_EQ
20071 && function_declarator_p (declarator))
20072 {
20073 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20074 if (t2->keyword == RID_DEFAULT)
20075 is_initialized = SD_DEFAULTED;
20076 else if (t2->keyword == RID_DELETE)
20077 is_initialized = SD_DELETED;
20078 }
20079 }
20080 else
20081 {
20082 /* If the init-declarator isn't initialized and isn't followed by a
20083 `,' or `;', it's not a valid init-declarator. */
20084 if (token->type != CPP_COMMA
20085 && token->type != CPP_SEMICOLON)
20086 {
20087 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20088 range_for_decl_p = true;
20089 else
20090 {
20091 if (!maybe_range_for_decl)
20092 cp_parser_error (parser, "expected initializer");
20093 return error_mark_node;
20094 }
20095 }
20096 is_initialized = SD_UNINITIALIZED;
20097 initialization_kind = CPP_EOF;
20098 }
20099
20100 /* Because start_decl has side-effects, we should only call it if we
20101 know we're going ahead. By this point, we know that we cannot
20102 possibly be looking at any other construct. */
20103 cp_parser_commit_to_tentative_parse (parser);
20104
20105 /* Enter the newly declared entry in the symbol table. If we're
20106 processing a declaration in a class-specifier, we wait until
20107 after processing the initializer. */
20108 if (!member_p)
20109 {
20110 if (parser->in_unbraced_linkage_specification_p)
20111 decl_specifiers->storage_class = sc_extern;
20112 decl = start_decl (declarator, decl_specifiers,
20113 range_for_decl_p? SD_INITIALIZED : is_initialized,
20114 attributes, prefix_attributes, &pushed_scope);
20115 cp_finalize_omp_declare_simd (parser, decl);
20116 cp_finalize_oacc_routine (parser, decl, false);
20117 /* Adjust location of decl if declarator->id_loc is more appropriate:
20118 set, and decl wasn't merged with another decl, in which case its
20119 location would be different from input_location, and more accurate. */
20120 if (DECL_P (decl)
20121 && declarator->id_loc != UNKNOWN_LOCATION
20122 && DECL_SOURCE_LOCATION (decl) == input_location)
20123 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20124 }
20125 else if (scope)
20126 /* Enter the SCOPE. That way unqualified names appearing in the
20127 initializer will be looked up in SCOPE. */
20128 pushed_scope = push_scope (scope);
20129
20130 /* Perform deferred access control checks, now that we know in which
20131 SCOPE the declared entity resides. */
20132 if (!member_p && decl)
20133 {
20134 tree saved_current_function_decl = NULL_TREE;
20135
20136 /* If the entity being declared is a function, pretend that we
20137 are in its scope. If it is a `friend', it may have access to
20138 things that would not otherwise be accessible. */
20139 if (TREE_CODE (decl) == FUNCTION_DECL)
20140 {
20141 saved_current_function_decl = current_function_decl;
20142 current_function_decl = decl;
20143 }
20144
20145 /* Perform access checks for template parameters. */
20146 cp_parser_perform_template_parameter_access_checks (checks);
20147
20148 /* Perform the access control checks for the declarator and the
20149 decl-specifiers. */
20150 perform_deferred_access_checks (tf_warning_or_error);
20151
20152 /* Restore the saved value. */
20153 if (TREE_CODE (decl) == FUNCTION_DECL)
20154 current_function_decl = saved_current_function_decl;
20155 }
20156
20157 /* Parse the initializer. */
20158 initializer = NULL_TREE;
20159 is_direct_init = false;
20160 is_non_constant_init = true;
20161 if (is_initialized)
20162 {
20163 if (function_declarator_p (declarator))
20164 {
20165 if (initialization_kind == CPP_EQ)
20166 initializer = cp_parser_pure_specifier (parser);
20167 else
20168 {
20169 /* If the declaration was erroneous, we don't really
20170 know what the user intended, so just silently
20171 consume the initializer. */
20172 if (decl != error_mark_node)
20173 error_at (tmp_init_loc, "initializer provided for function");
20174 cp_parser_skip_to_closing_parenthesis (parser,
20175 /*recovering=*/true,
20176 /*or_comma=*/false,
20177 /*consume_paren=*/true);
20178 }
20179 }
20180 else
20181 {
20182 /* We want to record the extra mangling scope for in-class
20183 initializers of class members and initializers of static data
20184 member templates. The former involves deferring
20185 parsing of the initializer until end of class as with default
20186 arguments. So right here we only handle the latter. */
20187 if (!member_p && processing_template_decl && decl != error_mark_node)
20188 start_lambda_scope (decl);
20189 initializer = cp_parser_initializer (parser,
20190 &is_direct_init,
20191 &is_non_constant_init);
20192 if (!member_p && processing_template_decl && decl != error_mark_node)
20193 finish_lambda_scope ();
20194 if (initializer == error_mark_node)
20195 cp_parser_skip_to_end_of_statement (parser);
20196 }
20197 }
20198
20199 /* The old parser allows attributes to appear after a parenthesized
20200 initializer. Mark Mitchell proposed removing this functionality
20201 on the GCC mailing lists on 2002-08-13. This parser accepts the
20202 attributes -- but ignores them. Made a permerror in GCC 8. */
20203 if (cp_parser_allow_gnu_extensions_p (parser)
20204 && initialization_kind == CPP_OPEN_PAREN
20205 && cp_parser_attributes_opt (parser)
20206 && permerror (input_location,
20207 "attributes after parenthesized initializer ignored"))
20208 {
20209 static bool hint;
20210 if (flag_permissive && !hint)
20211 {
20212 hint = true;
20213 inform (input_location,
20214 "this flexibility is deprecated and will be removed");
20215 }
20216 }
20217
20218 /* And now complain about a non-function implicit template. */
20219 if (bogus_implicit_tmpl && decl != error_mark_node)
20220 error_at (DECL_SOURCE_LOCATION (decl),
20221 "non-function %qD declared as implicit template", decl);
20222
20223 /* For an in-class declaration, use `grokfield' to create the
20224 declaration. */
20225 if (member_p)
20226 {
20227 if (pushed_scope)
20228 {
20229 pop_scope (pushed_scope);
20230 pushed_scope = NULL_TREE;
20231 }
20232 decl = grokfield (declarator, decl_specifiers,
20233 initializer, !is_non_constant_init,
20234 /*asmspec=*/NULL_TREE,
20235 attr_chainon (attributes, prefix_attributes));
20236 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20237 cp_parser_save_default_args (parser, decl);
20238 cp_finalize_omp_declare_simd (parser, decl);
20239 cp_finalize_oacc_routine (parser, decl, false);
20240 }
20241
20242 /* Finish processing the declaration. But, skip member
20243 declarations. */
20244 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20245 {
20246 cp_finish_decl (decl,
20247 initializer, !is_non_constant_init,
20248 asm_specification,
20249 /* If the initializer is in parentheses, then this is
20250 a direct-initialization, which means that an
20251 `explicit' constructor is OK. Otherwise, an
20252 `explicit' constructor cannot be used. */
20253 ((is_direct_init || !is_initialized)
20254 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20255 }
20256 else if ((cxx_dialect != cxx98) && friend_p
20257 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20258 /* Core issue #226 (C++0x only): A default template-argument
20259 shall not be specified in a friend class template
20260 declaration. */
20261 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20262 /*is_partial=*/false, /*is_friend_decl=*/1);
20263
20264 if (!friend_p && pushed_scope)
20265 pop_scope (pushed_scope);
20266
20267 if (function_declarator_p (declarator)
20268 && parser->fully_implicit_function_template_p)
20269 {
20270 if (member_p)
20271 decl = finish_fully_implicit_template (parser, decl);
20272 else
20273 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20274 }
20275
20276 if (auto_result && is_initialized && decl_specifiers->type
20277 && type_uses_auto (decl_specifiers->type))
20278 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20279
20280 return decl;
20281 }
20282
20283 /* Parse a declarator.
20284
20285 declarator:
20286 direct-declarator
20287 ptr-operator declarator
20288
20289 abstract-declarator:
20290 ptr-operator abstract-declarator [opt]
20291 direct-abstract-declarator
20292
20293 GNU Extensions:
20294
20295 declarator:
20296 attributes [opt] direct-declarator
20297 attributes [opt] ptr-operator declarator
20298
20299 abstract-declarator:
20300 attributes [opt] ptr-operator abstract-declarator [opt]
20301 attributes [opt] direct-abstract-declarator
20302
20303 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20304 detect constructors, destructors, deduction guides, or conversion operators.
20305 It is set to -1 if the declarator is a name, and +1 if it is a
20306 function. Otherwise it is set to zero. Usually you just want to
20307 test for >0, but internally the negative value is used.
20308
20309 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20310 a decl-specifier-seq unless it declares a constructor, destructor,
20311 or conversion. It might seem that we could check this condition in
20312 semantic analysis, rather than parsing, but that makes it difficult
20313 to handle something like `f()'. We want to notice that there are
20314 no decl-specifiers, and therefore realize that this is an
20315 expression, not a declaration.)
20316
20317 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20318 the declarator is a direct-declarator of the form "(...)".
20319
20320 MEMBER_P is true iff this declarator is a member-declarator.
20321
20322 FRIEND_P is true iff this declarator is a friend. */
20323
20324 static cp_declarator *
20325 cp_parser_declarator (cp_parser* parser,
20326 cp_parser_declarator_kind dcl_kind,
20327 int* ctor_dtor_or_conv_p,
20328 bool* parenthesized_p,
20329 bool member_p, bool friend_p)
20330 {
20331 cp_declarator *declarator;
20332 enum tree_code code;
20333 cp_cv_quals cv_quals;
20334 tree class_type;
20335 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20336
20337 /* Assume this is not a constructor, destructor, or type-conversion
20338 operator. */
20339 if (ctor_dtor_or_conv_p)
20340 *ctor_dtor_or_conv_p = 0;
20341
20342 if (cp_parser_allow_gnu_extensions_p (parser))
20343 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20344
20345 /* Check for the ptr-operator production. */
20346 cp_parser_parse_tentatively (parser);
20347 /* Parse the ptr-operator. */
20348 code = cp_parser_ptr_operator (parser,
20349 &class_type,
20350 &cv_quals,
20351 &std_attributes);
20352
20353 /* If that worked, then we have a ptr-operator. */
20354 if (cp_parser_parse_definitely (parser))
20355 {
20356 /* If a ptr-operator was found, then this declarator was not
20357 parenthesized. */
20358 if (parenthesized_p)
20359 *parenthesized_p = true;
20360 /* The dependent declarator is optional if we are parsing an
20361 abstract-declarator. */
20362 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20363 cp_parser_parse_tentatively (parser);
20364
20365 /* Parse the dependent declarator. */
20366 declarator = cp_parser_declarator (parser, dcl_kind,
20367 /*ctor_dtor_or_conv_p=*/NULL,
20368 /*parenthesized_p=*/NULL,
20369 /*member_p=*/false,
20370 friend_p);
20371
20372 /* If we are parsing an abstract-declarator, we must handle the
20373 case where the dependent declarator is absent. */
20374 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20375 && !cp_parser_parse_definitely (parser))
20376 declarator = NULL;
20377
20378 declarator = cp_parser_make_indirect_declarator
20379 (code, class_type, cv_quals, declarator, std_attributes);
20380 }
20381 /* Everything else is a direct-declarator. */
20382 else
20383 {
20384 if (parenthesized_p)
20385 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20386 CPP_OPEN_PAREN);
20387 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20388 ctor_dtor_or_conv_p,
20389 member_p, friend_p);
20390 }
20391
20392 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20393 declarator->attributes = gnu_attributes;
20394 return declarator;
20395 }
20396
20397 /* Parse a direct-declarator or direct-abstract-declarator.
20398
20399 direct-declarator:
20400 declarator-id
20401 direct-declarator ( parameter-declaration-clause )
20402 cv-qualifier-seq [opt]
20403 ref-qualifier [opt]
20404 exception-specification [opt]
20405 direct-declarator [ constant-expression [opt] ]
20406 ( declarator )
20407
20408 direct-abstract-declarator:
20409 direct-abstract-declarator [opt]
20410 ( parameter-declaration-clause )
20411 cv-qualifier-seq [opt]
20412 ref-qualifier [opt]
20413 exception-specification [opt]
20414 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20415 ( abstract-declarator )
20416
20417 Returns a representation of the declarator. DCL_KIND is
20418 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20419 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20420 we are parsing a direct-declarator. It is
20421 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20422 of ambiguity we prefer an abstract declarator, as per
20423 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20424 as for cp_parser_declarator. */
20425
20426 static cp_declarator *
20427 cp_parser_direct_declarator (cp_parser* parser,
20428 cp_parser_declarator_kind dcl_kind,
20429 int* ctor_dtor_or_conv_p,
20430 bool member_p, bool friend_p)
20431 {
20432 cp_token *token;
20433 cp_declarator *declarator = NULL;
20434 tree scope = NULL_TREE;
20435 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20436 bool saved_in_declarator_p = parser->in_declarator_p;
20437 bool first = true;
20438 tree pushed_scope = NULL_TREE;
20439 cp_token *open_paren = NULL, *close_paren = NULL;
20440
20441 while (true)
20442 {
20443 /* Peek at the next token. */
20444 token = cp_lexer_peek_token (parser->lexer);
20445 if (token->type == CPP_OPEN_PAREN)
20446 {
20447 /* This is either a parameter-declaration-clause, or a
20448 parenthesized declarator. When we know we are parsing a
20449 named declarator, it must be a parenthesized declarator
20450 if FIRST is true. For instance, `(int)' is a
20451 parameter-declaration-clause, with an omitted
20452 direct-abstract-declarator. But `((*))', is a
20453 parenthesized abstract declarator. Finally, when T is a
20454 template parameter `(T)' is a
20455 parameter-declaration-clause, and not a parenthesized
20456 named declarator.
20457
20458 We first try and parse a parameter-declaration-clause,
20459 and then try a nested declarator (if FIRST is true).
20460
20461 It is not an error for it not to be a
20462 parameter-declaration-clause, even when FIRST is
20463 false. Consider,
20464
20465 int i (int);
20466 int i (3);
20467
20468 The first is the declaration of a function while the
20469 second is the definition of a variable, including its
20470 initializer.
20471
20472 Having seen only the parenthesis, we cannot know which of
20473 these two alternatives should be selected. Even more
20474 complex are examples like:
20475
20476 int i (int (a));
20477 int i (int (3));
20478
20479 The former is a function-declaration; the latter is a
20480 variable initialization.
20481
20482 Thus again, we try a parameter-declaration-clause, and if
20483 that fails, we back out and return. */
20484
20485 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20486 {
20487 tree params;
20488 bool is_declarator = false;
20489
20490 open_paren = NULL;
20491
20492 /* In a member-declarator, the only valid interpretation
20493 of a parenthesis is the start of a
20494 parameter-declaration-clause. (It is invalid to
20495 initialize a static data member with a parenthesized
20496 initializer; only the "=" form of initialization is
20497 permitted.) */
20498 if (!member_p)
20499 cp_parser_parse_tentatively (parser);
20500
20501 /* Consume the `('. */
20502 matching_parens parens;
20503 parens.consume_open (parser);
20504 if (first)
20505 {
20506 /* If this is going to be an abstract declarator, we're
20507 in a declarator and we can't have default args. */
20508 parser->default_arg_ok_p = false;
20509 parser->in_declarator_p = true;
20510 }
20511
20512 begin_scope (sk_function_parms, NULL_TREE);
20513
20514 /* Parse the parameter-declaration-clause. */
20515 params = cp_parser_parameter_declaration_clause (parser);
20516
20517 /* Consume the `)'. */
20518 parens.require_close (parser);
20519
20520 /* If all went well, parse the cv-qualifier-seq,
20521 ref-qualifier and the exception-specification. */
20522 if (member_p || cp_parser_parse_definitely (parser))
20523 {
20524 cp_cv_quals cv_quals;
20525 cp_virt_specifiers virt_specifiers;
20526 cp_ref_qualifier ref_qual;
20527 tree exception_specification;
20528 tree late_return;
20529 tree attrs;
20530 bool memfn = (member_p || (pushed_scope
20531 && CLASS_TYPE_P (pushed_scope)));
20532
20533 is_declarator = true;
20534
20535 if (ctor_dtor_or_conv_p)
20536 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20537 first = false;
20538
20539 /* Parse the cv-qualifier-seq. */
20540 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20541 /* Parse the ref-qualifier. */
20542 ref_qual = cp_parser_ref_qualifier_opt (parser);
20543 /* Parse the tx-qualifier. */
20544 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20545 /* And the exception-specification. */
20546 exception_specification
20547 = cp_parser_exception_specification_opt (parser);
20548
20549 attrs = cp_parser_std_attribute_spec_seq (parser);
20550
20551 /* In here, we handle cases where attribute is used after
20552 the function declaration. For example:
20553 void func (int x) __attribute__((vector(..))); */
20554 tree gnu_attrs = NULL_TREE;
20555 tree requires_clause = NULL_TREE;
20556 late_return = (cp_parser_late_return_type_opt
20557 (parser, declarator, requires_clause,
20558 memfn ? cv_quals : -1));
20559
20560 /* Parse the virt-specifier-seq. */
20561 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20562
20563 /* Create the function-declarator. */
20564 declarator = make_call_declarator (declarator,
20565 params,
20566 cv_quals,
20567 virt_specifiers,
20568 ref_qual,
20569 tx_qual,
20570 exception_specification,
20571 late_return,
20572 requires_clause);
20573 declarator->std_attributes = attrs;
20574 declarator->attributes = gnu_attrs;
20575 /* Any subsequent parameter lists are to do with
20576 return type, so are not those of the declared
20577 function. */
20578 parser->default_arg_ok_p = false;
20579 }
20580
20581 /* Remove the function parms from scope. */
20582 pop_bindings_and_leave_scope ();
20583
20584 if (is_declarator)
20585 /* Repeat the main loop. */
20586 continue;
20587 }
20588
20589 /* If this is the first, we can try a parenthesized
20590 declarator. */
20591 if (first)
20592 {
20593 bool saved_in_type_id_in_expr_p;
20594
20595 parser->default_arg_ok_p = saved_default_arg_ok_p;
20596 parser->in_declarator_p = saved_in_declarator_p;
20597
20598 open_paren = token;
20599 /* Consume the `('. */
20600 matching_parens parens;
20601 parens.consume_open (parser);
20602 /* Parse the nested declarator. */
20603 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20604 parser->in_type_id_in_expr_p = true;
20605 declarator
20606 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20607 /*parenthesized_p=*/NULL,
20608 member_p, friend_p);
20609 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20610 first = false;
20611 /* Expect a `)'. */
20612 close_paren = cp_lexer_peek_token (parser->lexer);
20613 if (!parens.require_close (parser))
20614 declarator = cp_error_declarator;
20615 if (declarator == cp_error_declarator)
20616 break;
20617
20618 goto handle_declarator;
20619 }
20620 /* Otherwise, we must be done. */
20621 else
20622 break;
20623 }
20624 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20625 && token->type == CPP_OPEN_SQUARE
20626 && !cp_next_tokens_can_be_attribute_p (parser))
20627 {
20628 /* Parse an array-declarator. */
20629 tree bounds, attrs;
20630
20631 if (ctor_dtor_or_conv_p)
20632 *ctor_dtor_or_conv_p = 0;
20633
20634 open_paren = NULL;
20635 first = false;
20636 parser->default_arg_ok_p = false;
20637 parser->in_declarator_p = true;
20638 /* Consume the `['. */
20639 cp_lexer_consume_token (parser->lexer);
20640 /* Peek at the next token. */
20641 token = cp_lexer_peek_token (parser->lexer);
20642 /* If the next token is `]', then there is no
20643 constant-expression. */
20644 if (token->type != CPP_CLOSE_SQUARE)
20645 {
20646 bool non_constant_p;
20647 bounds
20648 = cp_parser_constant_expression (parser,
20649 /*allow_non_constant=*/true,
20650 &non_constant_p);
20651 if (!non_constant_p)
20652 /* OK */;
20653 else if (error_operand_p (bounds))
20654 /* Already gave an error. */;
20655 else if (!parser->in_function_body
20656 || current_binding_level->kind == sk_function_parms)
20657 {
20658 /* Normally, the array bound must be an integral constant
20659 expression. However, as an extension, we allow VLAs
20660 in function scopes as long as they aren't part of a
20661 parameter declaration. */
20662 cp_parser_error (parser,
20663 "array bound is not an integer constant");
20664 bounds = error_mark_node;
20665 }
20666 else if (processing_template_decl
20667 && !type_dependent_expression_p (bounds))
20668 {
20669 /* Remember this wasn't a constant-expression. */
20670 bounds = build_nop (TREE_TYPE (bounds), bounds);
20671 TREE_SIDE_EFFECTS (bounds) = 1;
20672 }
20673 }
20674 else
20675 bounds = NULL_TREE;
20676 /* Look for the closing `]'. */
20677 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20678 {
20679 declarator = cp_error_declarator;
20680 break;
20681 }
20682
20683 attrs = cp_parser_std_attribute_spec_seq (parser);
20684 declarator = make_array_declarator (declarator, bounds);
20685 declarator->std_attributes = attrs;
20686 }
20687 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20688 {
20689 {
20690 tree qualifying_scope;
20691 tree unqualified_name;
20692 tree attrs;
20693 special_function_kind sfk;
20694 bool abstract_ok;
20695 bool pack_expansion_p = false;
20696 cp_token *declarator_id_start_token;
20697
20698 /* Parse a declarator-id */
20699 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20700 if (abstract_ok)
20701 {
20702 cp_parser_parse_tentatively (parser);
20703
20704 /* If we see an ellipsis, we should be looking at a
20705 parameter pack. */
20706 if (token->type == CPP_ELLIPSIS)
20707 {
20708 /* Consume the `...' */
20709 cp_lexer_consume_token (parser->lexer);
20710
20711 pack_expansion_p = true;
20712 }
20713 }
20714
20715 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20716 unqualified_name
20717 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20718 qualifying_scope = parser->scope;
20719 if (abstract_ok)
20720 {
20721 bool okay = false;
20722
20723 if (!unqualified_name && pack_expansion_p)
20724 {
20725 /* Check whether an error occurred. */
20726 okay = !cp_parser_error_occurred (parser);
20727
20728 /* We already consumed the ellipsis to mark a
20729 parameter pack, but we have no way to report it,
20730 so abort the tentative parse. We will be exiting
20731 immediately anyway. */
20732 cp_parser_abort_tentative_parse (parser);
20733 }
20734 else
20735 okay = cp_parser_parse_definitely (parser);
20736
20737 if (!okay)
20738 unqualified_name = error_mark_node;
20739 else if (unqualified_name
20740 && (qualifying_scope
20741 || (!identifier_p (unqualified_name))))
20742 {
20743 cp_parser_error (parser, "expected unqualified-id");
20744 unqualified_name = error_mark_node;
20745 }
20746 }
20747
20748 if (!unqualified_name)
20749 return NULL;
20750 if (unqualified_name == error_mark_node)
20751 {
20752 declarator = cp_error_declarator;
20753 pack_expansion_p = false;
20754 declarator->parameter_pack_p = false;
20755 break;
20756 }
20757
20758 attrs = cp_parser_std_attribute_spec_seq (parser);
20759
20760 if (qualifying_scope && at_namespace_scope_p ()
20761 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20762 {
20763 /* In the declaration of a member of a template class
20764 outside of the class itself, the SCOPE will sometimes
20765 be a TYPENAME_TYPE. For example, given:
20766
20767 template <typename T>
20768 int S<T>::R::i = 3;
20769
20770 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20771 this context, we must resolve S<T>::R to an ordinary
20772 type, rather than a typename type.
20773
20774 The reason we normally avoid resolving TYPENAME_TYPEs
20775 is that a specialization of `S' might render
20776 `S<T>::R' not a type. However, if `S' is
20777 specialized, then this `i' will not be used, so there
20778 is no harm in resolving the types here. */
20779 tree type;
20780
20781 /* Resolve the TYPENAME_TYPE. */
20782 type = resolve_typename_type (qualifying_scope,
20783 /*only_current_p=*/false);
20784 /* If that failed, the declarator is invalid. */
20785 if (TREE_CODE (type) == TYPENAME_TYPE)
20786 {
20787 if (typedef_variant_p (type))
20788 error_at (declarator_id_start_token->location,
20789 "cannot define member of dependent typedef "
20790 "%qT", type);
20791 else
20792 error_at (declarator_id_start_token->location,
20793 "%<%T::%E%> is not a type",
20794 TYPE_CONTEXT (qualifying_scope),
20795 TYPE_IDENTIFIER (qualifying_scope));
20796 }
20797 qualifying_scope = type;
20798 }
20799
20800 sfk = sfk_none;
20801
20802 if (unqualified_name)
20803 {
20804 tree class_type;
20805
20806 if (qualifying_scope
20807 && CLASS_TYPE_P (qualifying_scope))
20808 class_type = qualifying_scope;
20809 else
20810 class_type = current_class_type;
20811
20812 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20813 {
20814 tree name_type = TREE_TYPE (unqualified_name);
20815
20816 if (!class_type || !same_type_p (name_type, class_type))
20817 {
20818 /* We do not attempt to print the declarator
20819 here because we do not have enough
20820 information about its original syntactic
20821 form. */
20822 cp_parser_error (parser, "invalid declarator");
20823 declarator = cp_error_declarator;
20824 break;
20825 }
20826 else if (qualifying_scope
20827 && CLASSTYPE_USE_TEMPLATE (name_type))
20828 {
20829 error_at (declarator_id_start_token->location,
20830 "invalid use of constructor as a template");
20831 inform (declarator_id_start_token->location,
20832 "use %<%T::%D%> instead of %<%T::%D%> to "
20833 "name the constructor in a qualified name",
20834 class_type,
20835 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20836 class_type, name_type);
20837 declarator = cp_error_declarator;
20838 break;
20839 }
20840 unqualified_name = constructor_name (class_type);
20841 }
20842
20843 if (class_type)
20844 {
20845 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20846 sfk = sfk_destructor;
20847 else if (identifier_p (unqualified_name)
20848 && IDENTIFIER_CONV_OP_P (unqualified_name))
20849 sfk = sfk_conversion;
20850 else if (/* There's no way to declare a constructor
20851 for an unnamed type, even if the type
20852 got a name for linkage purposes. */
20853 !TYPE_WAS_UNNAMED (class_type)
20854 /* Handle correctly (c++/19200):
20855
20856 struct S {
20857 struct T{};
20858 friend void S(T);
20859 };
20860
20861 and also:
20862
20863 namespace N {
20864 void S();
20865 }
20866
20867 struct S {
20868 friend void N::S();
20869 }; */
20870 && (!friend_p || class_type == qualifying_scope)
20871 && constructor_name_p (unqualified_name,
20872 class_type))
20873 sfk = sfk_constructor;
20874 else if (is_overloaded_fn (unqualified_name)
20875 && DECL_CONSTRUCTOR_P (get_first_fn
20876 (unqualified_name)))
20877 sfk = sfk_constructor;
20878
20879 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20880 *ctor_dtor_or_conv_p = -1;
20881 }
20882 }
20883 declarator = make_id_declarator (qualifying_scope,
20884 unqualified_name,
20885 sfk, token->location);
20886 declarator->std_attributes = attrs;
20887 declarator->parameter_pack_p = pack_expansion_p;
20888
20889 if (pack_expansion_p)
20890 maybe_warn_variadic_templates ();
20891 }
20892
20893 handle_declarator:;
20894 scope = get_scope_of_declarator (declarator);
20895 if (scope)
20896 {
20897 /* Any names that appear after the declarator-id for a
20898 member are looked up in the containing scope. */
20899 if (at_function_scope_p ())
20900 {
20901 /* But declarations with qualified-ids can't appear in a
20902 function. */
20903 cp_parser_error (parser, "qualified-id in declaration");
20904 declarator = cp_error_declarator;
20905 break;
20906 }
20907 pushed_scope = push_scope (scope);
20908 }
20909 parser->in_declarator_p = true;
20910 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20911 || (declarator && declarator->kind == cdk_id))
20912 /* Default args are only allowed on function
20913 declarations. */
20914 parser->default_arg_ok_p = saved_default_arg_ok_p;
20915 else
20916 parser->default_arg_ok_p = false;
20917
20918 first = false;
20919 }
20920 /* We're done. */
20921 else
20922 break;
20923 }
20924
20925 /* For an abstract declarator, we might wind up with nothing at this
20926 point. That's an error; the declarator is not optional. */
20927 if (!declarator)
20928 cp_parser_error (parser, "expected declarator");
20929 else if (open_paren)
20930 {
20931 /* Record overly parenthesized declarator so we can give a
20932 diagnostic about confusing decl/expr disambiguation. */
20933 if (declarator->kind == cdk_array)
20934 {
20935 /* If the open and close parens are on different lines, this
20936 is probably a formatting thing, so ignore. */
20937 expanded_location open = expand_location (open_paren->location);
20938 expanded_location close = expand_location (close_paren->location);
20939 if (open.line != close.line || open.file != close.file)
20940 open_paren = NULL;
20941 }
20942 if (open_paren)
20943 declarator->parenthesized = open_paren->location;
20944 }
20945
20946 /* If we entered a scope, we must exit it now. */
20947 if (pushed_scope)
20948 pop_scope (pushed_scope);
20949
20950 parser->default_arg_ok_p = saved_default_arg_ok_p;
20951 parser->in_declarator_p = saved_in_declarator_p;
20952
20953 return declarator;
20954 }
20955
20956 /* Parse a ptr-operator.
20957
20958 ptr-operator:
20959 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20960 * cv-qualifier-seq [opt]
20961 &
20962 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20963 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20964
20965 GNU Extension:
20966
20967 ptr-operator:
20968 & cv-qualifier-seq [opt]
20969
20970 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20971 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20972 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20973 filled in with the TYPE containing the member. *CV_QUALS is
20974 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20975 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20976 Note that the tree codes returned by this function have nothing
20977 to do with the types of trees that will be eventually be created
20978 to represent the pointer or reference type being parsed. They are
20979 just constants with suggestive names. */
20980 static enum tree_code
20981 cp_parser_ptr_operator (cp_parser* parser,
20982 tree* type,
20983 cp_cv_quals *cv_quals,
20984 tree *attributes)
20985 {
20986 enum tree_code code = ERROR_MARK;
20987 cp_token *token;
20988 tree attrs = NULL_TREE;
20989
20990 /* Assume that it's not a pointer-to-member. */
20991 *type = NULL_TREE;
20992 /* And that there are no cv-qualifiers. */
20993 *cv_quals = TYPE_UNQUALIFIED;
20994
20995 /* Peek at the next token. */
20996 token = cp_lexer_peek_token (parser->lexer);
20997
20998 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20999 if (token->type == CPP_MULT)
21000 code = INDIRECT_REF;
21001 else if (token->type == CPP_AND)
21002 code = ADDR_EXPR;
21003 else if ((cxx_dialect != cxx98) &&
21004 token->type == CPP_AND_AND) /* C++0x only */
21005 code = NON_LVALUE_EXPR;
21006
21007 if (code != ERROR_MARK)
21008 {
21009 /* Consume the `*', `&' or `&&'. */
21010 cp_lexer_consume_token (parser->lexer);
21011
21012 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21013 `&', if we are allowing GNU extensions. (The only qualifier
21014 that can legally appear after `&' is `restrict', but that is
21015 enforced during semantic analysis. */
21016 if (code == INDIRECT_REF
21017 || cp_parser_allow_gnu_extensions_p (parser))
21018 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21019
21020 attrs = cp_parser_std_attribute_spec_seq (parser);
21021 if (attributes != NULL)
21022 *attributes = attrs;
21023 }
21024 else
21025 {
21026 /* Try the pointer-to-member case. */
21027 cp_parser_parse_tentatively (parser);
21028 /* Look for the optional `::' operator. */
21029 cp_parser_global_scope_opt (parser,
21030 /*current_scope_valid_p=*/false);
21031 /* Look for the nested-name specifier. */
21032 token = cp_lexer_peek_token (parser->lexer);
21033 cp_parser_nested_name_specifier (parser,
21034 /*typename_keyword_p=*/false,
21035 /*check_dependency_p=*/true,
21036 /*type_p=*/false,
21037 /*is_declaration=*/false);
21038 /* If we found it, and the next token is a `*', then we are
21039 indeed looking at a pointer-to-member operator. */
21040 if (!cp_parser_error_occurred (parser)
21041 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21042 {
21043 /* Indicate that the `*' operator was used. */
21044 code = INDIRECT_REF;
21045
21046 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21047 error_at (token->location, "%qD is a namespace", parser->scope);
21048 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21049 error_at (token->location, "cannot form pointer to member of "
21050 "non-class %q#T", parser->scope);
21051 else
21052 {
21053 /* The type of which the member is a member is given by the
21054 current SCOPE. */
21055 *type = parser->scope;
21056 /* The next name will not be qualified. */
21057 parser->scope = NULL_TREE;
21058 parser->qualifying_scope = NULL_TREE;
21059 parser->object_scope = NULL_TREE;
21060 /* Look for optional c++11 attributes. */
21061 attrs = cp_parser_std_attribute_spec_seq (parser);
21062 if (attributes != NULL)
21063 *attributes = attrs;
21064 /* Look for the optional cv-qualifier-seq. */
21065 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21066 }
21067 }
21068 /* If that didn't work we don't have a ptr-operator. */
21069 if (!cp_parser_parse_definitely (parser))
21070 cp_parser_error (parser, "expected ptr-operator");
21071 }
21072
21073 return code;
21074 }
21075
21076 /* Parse an (optional) cv-qualifier-seq.
21077
21078 cv-qualifier-seq:
21079 cv-qualifier cv-qualifier-seq [opt]
21080
21081 cv-qualifier:
21082 const
21083 volatile
21084
21085 GNU Extension:
21086
21087 cv-qualifier:
21088 __restrict__
21089
21090 Returns a bitmask representing the cv-qualifiers. */
21091
21092 static cp_cv_quals
21093 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21094 {
21095 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21096
21097 while (true)
21098 {
21099 cp_token *token;
21100 cp_cv_quals cv_qualifier;
21101
21102 /* Peek at the next token. */
21103 token = cp_lexer_peek_token (parser->lexer);
21104 /* See if it's a cv-qualifier. */
21105 switch (token->keyword)
21106 {
21107 case RID_CONST:
21108 cv_qualifier = TYPE_QUAL_CONST;
21109 break;
21110
21111 case RID_VOLATILE:
21112 cv_qualifier = TYPE_QUAL_VOLATILE;
21113 break;
21114
21115 case RID_RESTRICT:
21116 cv_qualifier = TYPE_QUAL_RESTRICT;
21117 break;
21118
21119 default:
21120 cv_qualifier = TYPE_UNQUALIFIED;
21121 break;
21122 }
21123
21124 if (!cv_qualifier)
21125 break;
21126
21127 if (cv_quals & cv_qualifier)
21128 {
21129 gcc_rich_location richloc (token->location);
21130 richloc.add_fixit_remove ();
21131 error_at (&richloc, "duplicate cv-qualifier");
21132 cp_lexer_purge_token (parser->lexer);
21133 }
21134 else
21135 {
21136 cp_lexer_consume_token (parser->lexer);
21137 cv_quals |= cv_qualifier;
21138 }
21139 }
21140
21141 return cv_quals;
21142 }
21143
21144 /* Parse an (optional) ref-qualifier
21145
21146 ref-qualifier:
21147 &
21148 &&
21149
21150 Returns cp_ref_qualifier representing ref-qualifier. */
21151
21152 static cp_ref_qualifier
21153 cp_parser_ref_qualifier_opt (cp_parser* parser)
21154 {
21155 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21156
21157 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21158 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21159 return ref_qual;
21160
21161 while (true)
21162 {
21163 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21164 cp_token *token = cp_lexer_peek_token (parser->lexer);
21165
21166 switch (token->type)
21167 {
21168 case CPP_AND:
21169 curr_ref_qual = REF_QUAL_LVALUE;
21170 break;
21171
21172 case CPP_AND_AND:
21173 curr_ref_qual = REF_QUAL_RVALUE;
21174 break;
21175
21176 default:
21177 curr_ref_qual = REF_QUAL_NONE;
21178 break;
21179 }
21180
21181 if (!curr_ref_qual)
21182 break;
21183 else if (ref_qual)
21184 {
21185 error_at (token->location, "multiple ref-qualifiers");
21186 cp_lexer_purge_token (parser->lexer);
21187 }
21188 else
21189 {
21190 ref_qual = curr_ref_qual;
21191 cp_lexer_consume_token (parser->lexer);
21192 }
21193 }
21194
21195 return ref_qual;
21196 }
21197
21198 /* Parse an optional tx-qualifier.
21199
21200 tx-qualifier:
21201 transaction_safe
21202 transaction_safe_dynamic */
21203
21204 static tree
21205 cp_parser_tx_qualifier_opt (cp_parser *parser)
21206 {
21207 cp_token *token = cp_lexer_peek_token (parser->lexer);
21208 if (token->type == CPP_NAME)
21209 {
21210 tree name = token->u.value;
21211 const char *p = IDENTIFIER_POINTER (name);
21212 const int len = strlen ("transaction_safe");
21213 if (!strncmp (p, "transaction_safe", len))
21214 {
21215 p += len;
21216 if (*p == '\0'
21217 || !strcmp (p, "_dynamic"))
21218 {
21219 cp_lexer_consume_token (parser->lexer);
21220 if (!flag_tm)
21221 {
21222 error ("%qE requires %<-fgnu-tm%>", name);
21223 return NULL_TREE;
21224 }
21225 else
21226 return name;
21227 }
21228 }
21229 }
21230 return NULL_TREE;
21231 }
21232
21233 /* Parse an (optional) virt-specifier-seq.
21234
21235 virt-specifier-seq:
21236 virt-specifier virt-specifier-seq [opt]
21237
21238 virt-specifier:
21239 override
21240 final
21241
21242 Returns a bitmask representing the virt-specifiers. */
21243
21244 static cp_virt_specifiers
21245 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21246 {
21247 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21248
21249 while (true)
21250 {
21251 cp_token *token;
21252 cp_virt_specifiers virt_specifier;
21253
21254 /* Peek at the next token. */
21255 token = cp_lexer_peek_token (parser->lexer);
21256 /* See if it's a virt-specifier-qualifier. */
21257 if (token->type != CPP_NAME)
21258 break;
21259 if (id_equal (token->u.value, "override"))
21260 {
21261 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21262 virt_specifier = VIRT_SPEC_OVERRIDE;
21263 }
21264 else if (id_equal (token->u.value, "final"))
21265 {
21266 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21267 virt_specifier = VIRT_SPEC_FINAL;
21268 }
21269 else if (id_equal (token->u.value, "__final"))
21270 {
21271 virt_specifier = VIRT_SPEC_FINAL;
21272 }
21273 else
21274 break;
21275
21276 if (virt_specifiers & virt_specifier)
21277 {
21278 gcc_rich_location richloc (token->location);
21279 richloc.add_fixit_remove ();
21280 error_at (&richloc, "duplicate virt-specifier");
21281 cp_lexer_purge_token (parser->lexer);
21282 }
21283 else
21284 {
21285 cp_lexer_consume_token (parser->lexer);
21286 virt_specifiers |= virt_specifier;
21287 }
21288 }
21289 return virt_specifiers;
21290 }
21291
21292 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21293 is in scope even though it isn't real. */
21294
21295 void
21296 inject_this_parameter (tree ctype, cp_cv_quals quals)
21297 {
21298 tree this_parm;
21299
21300 if (current_class_ptr)
21301 {
21302 /* We don't clear this between NSDMIs. Is it already what we want? */
21303 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21304 if (DECL_P (current_class_ptr)
21305 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21306 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21307 && cp_type_quals (type) == quals)
21308 return;
21309 }
21310
21311 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21312 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21313 current_class_ptr = NULL_TREE;
21314 current_class_ref
21315 = cp_build_fold_indirect_ref (this_parm);
21316 current_class_ptr = this_parm;
21317 }
21318
21319 /* Return true iff our current scope is a non-static data member
21320 initializer. */
21321
21322 bool
21323 parsing_nsdmi (void)
21324 {
21325 /* We recognize NSDMI context by the context-less 'this' pointer set up
21326 by the function above. */
21327 if (current_class_ptr
21328 && TREE_CODE (current_class_ptr) == PARM_DECL
21329 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21330 return true;
21331 return false;
21332 }
21333
21334 /* Parse a late-specified return type, if any. This is not a separate
21335 non-terminal, but part of a function declarator, which looks like
21336
21337 -> trailing-type-specifier-seq abstract-declarator(opt)
21338
21339 Returns the type indicated by the type-id.
21340
21341 In addition to this, parse any queued up #pragma omp declare simd
21342 clauses, and #pragma acc routine clauses.
21343
21344 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21345 function. */
21346
21347 static tree
21348 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21349 tree& requires_clause, cp_cv_quals quals)
21350 {
21351 cp_token *token;
21352 tree type = NULL_TREE;
21353 bool declare_simd_p = (parser->omp_declare_simd
21354 && declarator
21355 && declarator->kind == cdk_id);
21356
21357 bool oacc_routine_p = (parser->oacc_routine
21358 && declarator
21359 && declarator->kind == cdk_id);
21360
21361 /* Peek at the next token. */
21362 token = cp_lexer_peek_token (parser->lexer);
21363 /* A late-specified return type is indicated by an initial '->'. */
21364 if (token->type != CPP_DEREF
21365 && token->keyword != RID_REQUIRES
21366 && !(token->type == CPP_NAME
21367 && token->u.value == ridpointers[RID_REQUIRES])
21368 && !(declare_simd_p || oacc_routine_p))
21369 return NULL_TREE;
21370
21371 tree save_ccp = current_class_ptr;
21372 tree save_ccr = current_class_ref;
21373 if (quals >= 0)
21374 {
21375 /* DR 1207: 'this' is in scope in the trailing return type. */
21376 inject_this_parameter (current_class_type, quals);
21377 }
21378
21379 if (token->type == CPP_DEREF)
21380 {
21381 /* Consume the ->. */
21382 cp_lexer_consume_token (parser->lexer);
21383
21384 type = cp_parser_trailing_type_id (parser);
21385 }
21386
21387 /* Function declarations may be followed by a trailing
21388 requires-clause. */
21389 requires_clause = cp_parser_requires_clause_opt (parser);
21390
21391 if (declare_simd_p)
21392 declarator->attributes
21393 = cp_parser_late_parsing_omp_declare_simd (parser,
21394 declarator->attributes);
21395 if (oacc_routine_p)
21396 declarator->attributes
21397 = cp_parser_late_parsing_oacc_routine (parser,
21398 declarator->attributes);
21399
21400 if (quals >= 0)
21401 {
21402 current_class_ptr = save_ccp;
21403 current_class_ref = save_ccr;
21404 }
21405
21406 return type;
21407 }
21408
21409 /* Parse a declarator-id.
21410
21411 declarator-id:
21412 id-expression
21413 :: [opt] nested-name-specifier [opt] type-name
21414
21415 In the `id-expression' case, the value returned is as for
21416 cp_parser_id_expression if the id-expression was an unqualified-id.
21417 If the id-expression was a qualified-id, then a SCOPE_REF is
21418 returned. The first operand is the scope (either a NAMESPACE_DECL
21419 or TREE_TYPE), but the second is still just a representation of an
21420 unqualified-id. */
21421
21422 static tree
21423 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21424 {
21425 tree id;
21426 /* The expression must be an id-expression. Assume that qualified
21427 names are the names of types so that:
21428
21429 template <class T>
21430 int S<T>::R::i = 3;
21431
21432 will work; we must treat `S<T>::R' as the name of a type.
21433 Similarly, assume that qualified names are templates, where
21434 required, so that:
21435
21436 template <class T>
21437 int S<T>::R<T>::i = 3;
21438
21439 will work, too. */
21440 id = cp_parser_id_expression (parser,
21441 /*template_keyword_p=*/false,
21442 /*check_dependency_p=*/false,
21443 /*template_p=*/NULL,
21444 /*declarator_p=*/true,
21445 optional_p);
21446 if (id && BASELINK_P (id))
21447 id = BASELINK_FUNCTIONS (id);
21448 return id;
21449 }
21450
21451 /* Parse a type-id.
21452
21453 type-id:
21454 type-specifier-seq abstract-declarator [opt]
21455
21456 Returns the TYPE specified. */
21457
21458 static tree
21459 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
21460 bool is_trailing_return, location_t * type_location)
21461 {
21462 cp_decl_specifier_seq type_specifier_seq;
21463 cp_declarator *abstract_declarator;
21464
21465 /* Parse the type-specifier-seq. */
21466 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
21467 is_trailing_return,
21468 &type_specifier_seq);
21469 if (type_location)
21470 *type_location = type_specifier_seq.locations[ds_type_spec];
21471
21472 if (is_template_arg && type_specifier_seq.type
21473 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21474 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21475 /* A bare template name as a template argument is a template template
21476 argument, not a placeholder, so fail parsing it as a type argument. */
21477 {
21478 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21479 cp_parser_simulate_error (parser);
21480 return error_mark_node;
21481 }
21482 if (type_specifier_seq.type == error_mark_node)
21483 return error_mark_node;
21484
21485 /* There might or might not be an abstract declarator. */
21486 cp_parser_parse_tentatively (parser);
21487 /* Look for the declarator. */
21488 abstract_declarator
21489 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21490 /*parenthesized_p=*/NULL,
21491 /*member_p=*/false,
21492 /*friend_p=*/false);
21493 /* Check to see if there really was a declarator. */
21494 if (!cp_parser_parse_definitely (parser))
21495 abstract_declarator = NULL;
21496
21497 if (type_specifier_seq.type
21498 /* The concepts TS allows 'auto' as a type-id. */
21499 && (!flag_concepts || parser->in_type_id_in_expr_p)
21500 /* None of the valid uses of 'auto' in C++14 involve the type-id
21501 nonterminal, but it is valid in a trailing-return-type. */
21502 && !(cxx_dialect >= cxx14 && is_trailing_return))
21503 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21504 {
21505 /* A type-id with type 'auto' is only ok if the abstract declarator
21506 is a function declarator with a late-specified return type.
21507
21508 A type-id with 'auto' is also valid in a trailing-return-type
21509 in a compound-requirement. */
21510 if (abstract_declarator
21511 && abstract_declarator->kind == cdk_function
21512 && abstract_declarator->u.function.late_return_type)
21513 /* OK */;
21514 else if (parser->in_result_type_constraint_p)
21515 /* OK */;
21516 else
21517 {
21518 location_t loc = type_specifier_seq.locations[ds_type_spec];
21519 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21520 {
21521 error_at (loc, "missing template arguments after %qT",
21522 auto_node);
21523 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21524 tmpl);
21525 }
21526 else
21527 error_at (loc, "invalid use of %qT", auto_node);
21528 return error_mark_node;
21529 }
21530 }
21531
21532 return groktypename (&type_specifier_seq, abstract_declarator,
21533 is_template_arg);
21534 }
21535
21536 static tree
21537 cp_parser_type_id (cp_parser *parser, location_t * type_location)
21538 {
21539 return cp_parser_type_id_1 (parser, false, false, type_location);
21540 }
21541
21542 static tree
21543 cp_parser_template_type_arg (cp_parser *parser)
21544 {
21545 tree r;
21546 const char *saved_message = parser->type_definition_forbidden_message;
21547 parser->type_definition_forbidden_message
21548 = G_("types may not be defined in template arguments");
21549 r = cp_parser_type_id_1 (parser, true, false, NULL);
21550 parser->type_definition_forbidden_message = saved_message;
21551 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21552 {
21553 error ("invalid use of %<auto%> in template argument");
21554 r = error_mark_node;
21555 }
21556 return r;
21557 }
21558
21559 static tree
21560 cp_parser_trailing_type_id (cp_parser *parser)
21561 {
21562 return cp_parser_type_id_1 (parser, false, true, NULL);
21563 }
21564
21565 /* Parse a type-specifier-seq.
21566
21567 type-specifier-seq:
21568 type-specifier type-specifier-seq [opt]
21569
21570 GNU extension:
21571
21572 type-specifier-seq:
21573 attributes type-specifier-seq [opt]
21574
21575 If IS_DECLARATION is true, we are at the start of a "condition" or
21576 exception-declaration, so we might be followed by a declarator-id.
21577
21578 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21579 i.e. we've just seen "->".
21580
21581 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21582
21583 static void
21584 cp_parser_type_specifier_seq (cp_parser* parser,
21585 bool is_declaration,
21586 bool is_trailing_return,
21587 cp_decl_specifier_seq *type_specifier_seq)
21588 {
21589 bool seen_type_specifier = false;
21590 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21591 cp_token *start_token = NULL;
21592
21593 /* Clear the TYPE_SPECIFIER_SEQ. */
21594 clear_decl_specs (type_specifier_seq);
21595
21596 /* In the context of a trailing return type, enum E { } is an
21597 elaborated-type-specifier followed by a function-body, not an
21598 enum-specifier. */
21599 if (is_trailing_return)
21600 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21601
21602 /* Parse the type-specifiers and attributes. */
21603 while (true)
21604 {
21605 tree type_specifier;
21606 bool is_cv_qualifier;
21607
21608 /* Check for attributes first. */
21609 if (cp_next_tokens_can_be_attribute_p (parser))
21610 {
21611 type_specifier_seq->attributes
21612 = attr_chainon (type_specifier_seq->attributes,
21613 cp_parser_attributes_opt (parser));
21614 continue;
21615 }
21616
21617 /* record the token of the beginning of the type specifier seq,
21618 for error reporting purposes*/
21619 if (!start_token)
21620 start_token = cp_lexer_peek_token (parser->lexer);
21621
21622 /* Look for the type-specifier. */
21623 type_specifier = cp_parser_type_specifier (parser,
21624 flags,
21625 type_specifier_seq,
21626 /*is_declaration=*/false,
21627 NULL,
21628 &is_cv_qualifier);
21629 if (!type_specifier)
21630 {
21631 /* If the first type-specifier could not be found, this is not a
21632 type-specifier-seq at all. */
21633 if (!seen_type_specifier)
21634 {
21635 /* Set in_declarator_p to avoid skipping to the semicolon. */
21636 int in_decl = parser->in_declarator_p;
21637 parser->in_declarator_p = true;
21638
21639 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21640 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21641 cp_parser_error (parser, "expected type-specifier");
21642
21643 parser->in_declarator_p = in_decl;
21644
21645 type_specifier_seq->type = error_mark_node;
21646 return;
21647 }
21648 /* If subsequent type-specifiers could not be found, the
21649 type-specifier-seq is complete. */
21650 break;
21651 }
21652
21653 seen_type_specifier = true;
21654 /* The standard says that a condition can be:
21655
21656 type-specifier-seq declarator = assignment-expression
21657
21658 However, given:
21659
21660 struct S {};
21661 if (int S = ...)
21662
21663 we should treat the "S" as a declarator, not as a
21664 type-specifier. The standard doesn't say that explicitly for
21665 type-specifier-seq, but it does say that for
21666 decl-specifier-seq in an ordinary declaration. Perhaps it
21667 would be clearer just to allow a decl-specifier-seq here, and
21668 then add a semantic restriction that if any decl-specifiers
21669 that are not type-specifiers appear, the program is invalid. */
21670 if (is_declaration && !is_cv_qualifier)
21671 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21672 }
21673 }
21674
21675 /* Return whether the function currently being declared has an associated
21676 template parameter list. */
21677
21678 static bool
21679 function_being_declared_is_template_p (cp_parser* parser)
21680 {
21681 if (!current_template_parms || processing_template_parmlist)
21682 return false;
21683
21684 if (parser->implicit_template_scope)
21685 return true;
21686
21687 if (at_class_scope_p ()
21688 && TYPE_BEING_DEFINED (current_class_type))
21689 return parser->num_template_parameter_lists != 0;
21690
21691 return ((int) parser->num_template_parameter_lists > template_class_depth
21692 (current_class_type));
21693 }
21694
21695 /* Parse a parameter-declaration-clause.
21696
21697 parameter-declaration-clause:
21698 parameter-declaration-list [opt] ... [opt]
21699 parameter-declaration-list , ...
21700
21701 Returns a representation for the parameter declarations. A return
21702 value of NULL indicates a parameter-declaration-clause consisting
21703 only of an ellipsis. */
21704
21705 static tree
21706 cp_parser_parameter_declaration_clause (cp_parser* parser)
21707 {
21708 tree parameters;
21709 cp_token *token;
21710 bool ellipsis_p;
21711
21712 temp_override<bool> cleanup
21713 (parser->auto_is_implicit_function_template_parm_p);
21714
21715 if (!processing_specialization
21716 && !processing_template_parmlist
21717 && !processing_explicit_instantiation
21718 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21719 actual function or a random abstract declarator. */
21720 && parser->default_arg_ok_p)
21721 if (!current_function_decl
21722 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21723 parser->auto_is_implicit_function_template_parm_p = true;
21724
21725 /* Peek at the next token. */
21726 token = cp_lexer_peek_token (parser->lexer);
21727 /* Check for trivial parameter-declaration-clauses. */
21728 if (token->type == CPP_ELLIPSIS)
21729 {
21730 /* Consume the `...' token. */
21731 cp_lexer_consume_token (parser->lexer);
21732 return NULL_TREE;
21733 }
21734 else if (token->type == CPP_CLOSE_PAREN)
21735 /* There are no parameters. */
21736 return void_list_node;
21737 /* Check for `(void)', too, which is a special case. */
21738 else if (token->keyword == RID_VOID
21739 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21740 == CPP_CLOSE_PAREN))
21741 {
21742 /* Consume the `void' token. */
21743 cp_lexer_consume_token (parser->lexer);
21744 /* There are no parameters. */
21745 return void_list_node;
21746 }
21747
21748 /* Parse the parameter-declaration-list. */
21749 parameters = cp_parser_parameter_declaration_list (parser);
21750 /* If a parse error occurred while parsing the
21751 parameter-declaration-list, then the entire
21752 parameter-declaration-clause is erroneous. */
21753 if (parameters == error_mark_node)
21754 return NULL_TREE;
21755
21756 /* Peek at the next token. */
21757 token = cp_lexer_peek_token (parser->lexer);
21758 /* If it's a `,', the clause should terminate with an ellipsis. */
21759 if (token->type == CPP_COMMA)
21760 {
21761 /* Consume the `,'. */
21762 cp_lexer_consume_token (parser->lexer);
21763 /* Expect an ellipsis. */
21764 ellipsis_p
21765 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21766 }
21767 /* It might also be `...' if the optional trailing `,' was
21768 omitted. */
21769 else if (token->type == CPP_ELLIPSIS)
21770 {
21771 /* Consume the `...' token. */
21772 cp_lexer_consume_token (parser->lexer);
21773 /* And remember that we saw it. */
21774 ellipsis_p = true;
21775 }
21776 else
21777 ellipsis_p = false;
21778
21779 /* Finish the parameter list. */
21780 if (!ellipsis_p)
21781 parameters = chainon (parameters, void_list_node);
21782
21783 return parameters;
21784 }
21785
21786 /* Parse a parameter-declaration-list.
21787
21788 parameter-declaration-list:
21789 parameter-declaration
21790 parameter-declaration-list , parameter-declaration
21791
21792 Returns a representation of the parameter-declaration-list, as for
21793 cp_parser_parameter_declaration_clause. However, the
21794 `void_list_node' is never appended to the list. */
21795
21796 static tree
21797 cp_parser_parameter_declaration_list (cp_parser* parser)
21798 {
21799 tree parameters = NULL_TREE;
21800 tree *tail = &parameters;
21801 bool saved_in_unbraced_linkage_specification_p;
21802 int index = 0;
21803
21804 /* The special considerations that apply to a function within an
21805 unbraced linkage specifications do not apply to the parameters
21806 to the function. */
21807 saved_in_unbraced_linkage_specification_p
21808 = parser->in_unbraced_linkage_specification_p;
21809 parser->in_unbraced_linkage_specification_p = false;
21810
21811 /* Look for more parameters. */
21812 while (true)
21813 {
21814 cp_parameter_declarator *parameter;
21815 tree decl = error_mark_node;
21816 bool parenthesized_p = false;
21817
21818 /* Parse the parameter. */
21819 parameter
21820 = cp_parser_parameter_declaration (parser,
21821 /*template_parm_p=*/false,
21822 &parenthesized_p);
21823
21824 /* We don't know yet if the enclosing context is deprecated, so wait
21825 and warn in grokparms if appropriate. */
21826 deprecated_state = DEPRECATED_SUPPRESS;
21827
21828 if (parameter)
21829 {
21830 decl = grokdeclarator (parameter->declarator,
21831 &parameter->decl_specifiers,
21832 PARM,
21833 parameter->default_argument != NULL_TREE,
21834 &parameter->decl_specifiers.attributes);
21835 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21836 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21837 }
21838
21839 deprecated_state = DEPRECATED_NORMAL;
21840
21841 /* If a parse error occurred parsing the parameter declaration,
21842 then the entire parameter-declaration-list is erroneous. */
21843 if (decl == error_mark_node)
21844 {
21845 parameters = error_mark_node;
21846 break;
21847 }
21848
21849 if (parameter->decl_specifiers.attributes)
21850 cplus_decl_attributes (&decl,
21851 parameter->decl_specifiers.attributes,
21852 0);
21853 if (DECL_NAME (decl))
21854 decl = pushdecl (decl);
21855
21856 if (decl != error_mark_node)
21857 {
21858 retrofit_lang_decl (decl);
21859 DECL_PARM_INDEX (decl) = ++index;
21860 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21861 }
21862
21863 /* Add the new parameter to the list. */
21864 *tail = build_tree_list (parameter->default_argument, decl);
21865 tail = &TREE_CHAIN (*tail);
21866
21867 /* Peek at the next token. */
21868 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21869 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21870 /* These are for Objective-C++ */
21871 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21872 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21873 /* The parameter-declaration-list is complete. */
21874 break;
21875 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21876 {
21877 cp_token *token;
21878
21879 /* Peek at the next token. */
21880 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21881 /* If it's an ellipsis, then the list is complete. */
21882 if (token->type == CPP_ELLIPSIS)
21883 break;
21884 /* Otherwise, there must be more parameters. Consume the
21885 `,'. */
21886 cp_lexer_consume_token (parser->lexer);
21887 /* When parsing something like:
21888
21889 int i(float f, double d)
21890
21891 we can tell after seeing the declaration for "f" that we
21892 are not looking at an initialization of a variable "i",
21893 but rather at the declaration of a function "i".
21894
21895 Due to the fact that the parsing of template arguments
21896 (as specified to a template-id) requires backtracking we
21897 cannot use this technique when inside a template argument
21898 list. */
21899 if (!parser->in_template_argument_list_p
21900 && !parser->in_type_id_in_expr_p
21901 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21902 /* However, a parameter-declaration of the form
21903 "float(f)" (which is a valid declaration of a
21904 parameter "f") can also be interpreted as an
21905 expression (the conversion of "f" to "float"). */
21906 && !parenthesized_p)
21907 cp_parser_commit_to_tentative_parse (parser);
21908 }
21909 else
21910 {
21911 cp_parser_error (parser, "expected %<,%> or %<...%>");
21912 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21913 cp_parser_skip_to_closing_parenthesis (parser,
21914 /*recovering=*/true,
21915 /*or_comma=*/false,
21916 /*consume_paren=*/false);
21917 break;
21918 }
21919 }
21920
21921 parser->in_unbraced_linkage_specification_p
21922 = saved_in_unbraced_linkage_specification_p;
21923
21924 /* Reset implicit_template_scope if we are about to leave the function
21925 parameter list that introduced it. Note that for out-of-line member
21926 definitions, there will be one or more class scopes before we get to
21927 the template parameter scope. */
21928
21929 if (cp_binding_level *its = parser->implicit_template_scope)
21930 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21931 {
21932 while (maybe_its->kind == sk_class)
21933 maybe_its = maybe_its->level_chain;
21934 if (maybe_its == its)
21935 {
21936 parser->implicit_template_parms = 0;
21937 parser->implicit_template_scope = 0;
21938 }
21939 }
21940
21941 return parameters;
21942 }
21943
21944 /* Parse a parameter declaration.
21945
21946 parameter-declaration:
21947 decl-specifier-seq ... [opt] declarator
21948 decl-specifier-seq declarator = assignment-expression
21949 decl-specifier-seq ... [opt] abstract-declarator [opt]
21950 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21951
21952 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21953 declares a template parameter. (In that case, a non-nested `>'
21954 token encountered during the parsing of the assignment-expression
21955 is not interpreted as a greater-than operator.)
21956
21957 Returns a representation of the parameter, or NULL if an error
21958 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21959 true iff the declarator is of the form "(p)". */
21960
21961 static cp_parameter_declarator *
21962 cp_parser_parameter_declaration (cp_parser *parser,
21963 bool template_parm_p,
21964 bool *parenthesized_p)
21965 {
21966 int declares_class_or_enum;
21967 cp_decl_specifier_seq decl_specifiers;
21968 cp_declarator *declarator;
21969 tree default_argument;
21970 cp_token *token = NULL, *declarator_token_start = NULL;
21971 const char *saved_message;
21972 bool template_parameter_pack_p = false;
21973
21974 /* In a template parameter, `>' is not an operator.
21975
21976 [temp.param]
21977
21978 When parsing a default template-argument for a non-type
21979 template-parameter, the first non-nested `>' is taken as the end
21980 of the template parameter-list rather than a greater-than
21981 operator. */
21982
21983 /* Type definitions may not appear in parameter types. */
21984 saved_message = parser->type_definition_forbidden_message;
21985 parser->type_definition_forbidden_message
21986 = G_("types may not be defined in parameter types");
21987
21988 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
21989 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21990 (current_template_parms)) : 0);
21991
21992 /* Parse the declaration-specifiers. */
21993 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21994 cp_parser_decl_specifier_seq (parser,
21995 CP_PARSER_FLAGS_NONE,
21996 &decl_specifiers,
21997 &declares_class_or_enum);
21998
21999 /* Complain about missing 'typename' or other invalid type names. */
22000 if (!decl_specifiers.any_type_specifiers_p
22001 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22002 decl_specifiers.type = error_mark_node;
22003
22004 /* If an error occurred, there's no reason to attempt to parse the
22005 rest of the declaration. */
22006 if (cp_parser_error_occurred (parser))
22007 {
22008 parser->type_definition_forbidden_message = saved_message;
22009 return NULL;
22010 }
22011
22012 /* Peek at the next token. */
22013 token = cp_lexer_peek_token (parser->lexer);
22014
22015 /* If the next token is a `)', `,', `=', `>', or `...', then there
22016 is no declarator. However, when variadic templates are enabled,
22017 there may be a declarator following `...'. */
22018 if (token->type == CPP_CLOSE_PAREN
22019 || token->type == CPP_COMMA
22020 || token->type == CPP_EQ
22021 || token->type == CPP_GREATER)
22022 {
22023 declarator = NULL;
22024 if (parenthesized_p)
22025 *parenthesized_p = false;
22026 }
22027 /* Otherwise, there should be a declarator. */
22028 else
22029 {
22030 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22031 parser->default_arg_ok_p = false;
22032
22033 /* After seeing a decl-specifier-seq, if the next token is not a
22034 "(", there is no possibility that the code is a valid
22035 expression. Therefore, if parsing tentatively, we commit at
22036 this point. */
22037 if (!parser->in_template_argument_list_p
22038 /* In an expression context, having seen:
22039
22040 (int((char ...
22041
22042 we cannot be sure whether we are looking at a
22043 function-type (taking a "char" as a parameter) or a cast
22044 of some object of type "char" to "int". */
22045 && !parser->in_type_id_in_expr_p
22046 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22047 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22048 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22049 cp_parser_commit_to_tentative_parse (parser);
22050 /* Parse the declarator. */
22051 declarator_token_start = token;
22052 declarator = cp_parser_declarator (parser,
22053 CP_PARSER_DECLARATOR_EITHER,
22054 /*ctor_dtor_or_conv_p=*/NULL,
22055 parenthesized_p,
22056 /*member_p=*/false,
22057 /*friend_p=*/false);
22058 parser->default_arg_ok_p = saved_default_arg_ok_p;
22059 /* After the declarator, allow more attributes. */
22060 decl_specifiers.attributes
22061 = attr_chainon (decl_specifiers.attributes,
22062 cp_parser_attributes_opt (parser));
22063
22064 /* If the declarator is a template parameter pack, remember that and
22065 clear the flag in the declarator itself so we don't get errors
22066 from grokdeclarator. */
22067 if (template_parm_p && declarator && declarator->parameter_pack_p)
22068 {
22069 declarator->parameter_pack_p = false;
22070 template_parameter_pack_p = true;
22071 }
22072 }
22073
22074 /* If the next token is an ellipsis, and we have not seen a declarator
22075 name, and if either the type of the declarator contains parameter
22076 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22077 for, eg, abbreviated integral type names), then we actually have a
22078 parameter pack expansion expression. Otherwise, leave the ellipsis
22079 for a C-style variadic function. */
22080 token = cp_lexer_peek_token (parser->lexer);
22081
22082 /* If a function parameter pack was specified and an implicit template
22083 parameter was introduced during cp_parser_parameter_declaration,
22084 change any implicit parameters introduced into packs. */
22085 if (parser->implicit_template_parms
22086 && ((token->type == CPP_ELLIPSIS
22087 && declarator_can_be_parameter_pack (declarator))
22088 || (declarator && declarator->parameter_pack_p)))
22089 {
22090 int latest_template_parm_idx = TREE_VEC_LENGTH
22091 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22092
22093 if (latest_template_parm_idx != template_parm_idx)
22094 decl_specifiers.type = convert_generic_types_to_packs
22095 (decl_specifiers.type,
22096 template_parm_idx, latest_template_parm_idx);
22097 }
22098
22099 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22100 {
22101 tree type = decl_specifiers.type;
22102
22103 if (type && DECL_P (type))
22104 type = TREE_TYPE (type);
22105
22106 if (((type
22107 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22108 && (template_parm_p || uses_parameter_packs (type)))
22109 || (!type && template_parm_p))
22110 && declarator_can_be_parameter_pack (declarator))
22111 {
22112 /* Consume the `...'. */
22113 cp_lexer_consume_token (parser->lexer);
22114 maybe_warn_variadic_templates ();
22115
22116 /* Build a pack expansion type */
22117 if (template_parm_p)
22118 template_parameter_pack_p = true;
22119 else if (declarator)
22120 declarator->parameter_pack_p = true;
22121 else
22122 decl_specifiers.type = make_pack_expansion (type);
22123 }
22124 }
22125
22126 /* The restriction on defining new types applies only to the type
22127 of the parameter, not to the default argument. */
22128 parser->type_definition_forbidden_message = saved_message;
22129
22130 /* If the next token is `=', then process a default argument. */
22131 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22132 {
22133 tree type = decl_specifiers.type;
22134 token = cp_lexer_peek_token (parser->lexer);
22135 /* If we are defining a class, then the tokens that make up the
22136 default argument must be saved and processed later. */
22137 if (!template_parm_p && at_class_scope_p ()
22138 && TYPE_BEING_DEFINED (current_class_type)
22139 && !LAMBDA_TYPE_P (current_class_type))
22140 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22141
22142 // A constrained-type-specifier may declare a type template-parameter.
22143 else if (declares_constrained_type_template_parameter (type))
22144 default_argument
22145 = cp_parser_default_type_template_argument (parser);
22146
22147 // A constrained-type-specifier may declare a template-template-parameter.
22148 else if (declares_constrained_template_template_parameter (type))
22149 default_argument
22150 = cp_parser_default_template_template_argument (parser);
22151
22152 /* Outside of a class definition, we can just parse the
22153 assignment-expression. */
22154 else
22155 default_argument
22156 = cp_parser_default_argument (parser, template_parm_p);
22157
22158 if (!parser->default_arg_ok_p)
22159 {
22160 permerror (token->location,
22161 "default arguments are only "
22162 "permitted for function parameters");
22163 }
22164 else if ((declarator && declarator->parameter_pack_p)
22165 || template_parameter_pack_p
22166 || (decl_specifiers.type
22167 && PACK_EXPANSION_P (decl_specifiers.type)))
22168 {
22169 /* Find the name of the parameter pack. */
22170 cp_declarator *id_declarator = declarator;
22171 while (id_declarator && id_declarator->kind != cdk_id)
22172 id_declarator = id_declarator->declarator;
22173
22174 if (id_declarator && id_declarator->kind == cdk_id)
22175 error_at (declarator_token_start->location,
22176 template_parm_p
22177 ? G_("template parameter pack %qD "
22178 "cannot have a default argument")
22179 : G_("parameter pack %qD cannot have "
22180 "a default argument"),
22181 id_declarator->u.id.unqualified_name);
22182 else
22183 error_at (declarator_token_start->location,
22184 template_parm_p
22185 ? G_("template parameter pack cannot have "
22186 "a default argument")
22187 : G_("parameter pack cannot have a "
22188 "default argument"));
22189
22190 default_argument = NULL_TREE;
22191 }
22192 }
22193 else
22194 default_argument = NULL_TREE;
22195
22196 /* Generate a location for the parameter, ranging from the start of the
22197 initial token to the end of the final token (using input_location for
22198 the latter, set up by cp_lexer_set_source_position_from_token when
22199 consuming tokens).
22200
22201 If we have a identifier, then use it for the caret location, e.g.
22202
22203 extern int callee (int one, int (*two)(int, int), float three);
22204 ~~~~~~^~~~~~~~~~~~~~
22205
22206 otherwise, reuse the start location for the caret location e.g.:
22207
22208 extern int callee (int one, int (*)(int, int), float three);
22209 ^~~~~~~~~~~~~~~~~
22210
22211 */
22212 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22213 ? declarator->id_loc
22214 : decl_spec_token_start->location);
22215 location_t param_loc = make_location (caret_loc,
22216 decl_spec_token_start->location,
22217 input_location);
22218
22219 return make_parameter_declarator (&decl_specifiers,
22220 declarator,
22221 default_argument,
22222 param_loc,
22223 template_parameter_pack_p);
22224 }
22225
22226 /* Parse a default argument and return it.
22227
22228 TEMPLATE_PARM_P is true if this is a default argument for a
22229 non-type template parameter. */
22230 static tree
22231 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22232 {
22233 tree default_argument = NULL_TREE;
22234 bool saved_greater_than_is_operator_p;
22235 bool saved_local_variables_forbidden_p;
22236 bool non_constant_p, is_direct_init;
22237
22238 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22239 set correctly. */
22240 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22241 parser->greater_than_is_operator_p = !template_parm_p;
22242 /* Local variable names (and the `this' keyword) may not
22243 appear in a default argument. */
22244 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22245 parser->local_variables_forbidden_p = true;
22246 /* Parse the assignment-expression. */
22247 if (template_parm_p)
22248 push_deferring_access_checks (dk_no_deferred);
22249 tree saved_class_ptr = NULL_TREE;
22250 tree saved_class_ref = NULL_TREE;
22251 /* The "this" pointer is not valid in a default argument. */
22252 if (cfun)
22253 {
22254 saved_class_ptr = current_class_ptr;
22255 cp_function_chain->x_current_class_ptr = NULL_TREE;
22256 saved_class_ref = current_class_ref;
22257 cp_function_chain->x_current_class_ref = NULL_TREE;
22258 }
22259 default_argument
22260 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22261 /* Restore the "this" pointer. */
22262 if (cfun)
22263 {
22264 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22265 cp_function_chain->x_current_class_ref = saved_class_ref;
22266 }
22267 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22268 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22269 if (template_parm_p)
22270 pop_deferring_access_checks ();
22271 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22272 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22273
22274 return default_argument;
22275 }
22276
22277 /* Parse a function-body.
22278
22279 function-body:
22280 compound_statement */
22281
22282 static void
22283 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22284 {
22285 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22286 ? BCS_TRY_BLOCK : BCS_NORMAL),
22287 true);
22288 }
22289
22290 /* Parse a ctor-initializer-opt followed by a function-body. Return
22291 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22292 is true we are parsing a function-try-block. */
22293
22294 static void
22295 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22296 bool in_function_try_block)
22297 {
22298 tree body, list;
22299 const bool check_body_p =
22300 DECL_CONSTRUCTOR_P (current_function_decl)
22301 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22302 tree last = NULL;
22303
22304 /* Begin the function body. */
22305 body = begin_function_body ();
22306 /* Parse the optional ctor-initializer. */
22307 cp_parser_ctor_initializer_opt (parser);
22308
22309 /* If we're parsing a constexpr constructor definition, we need
22310 to check that the constructor body is indeed empty. However,
22311 before we get to cp_parser_function_body lot of junk has been
22312 generated, so we can't just check that we have an empty block.
22313 Rather we take a snapshot of the outermost block, and check whether
22314 cp_parser_function_body changed its state. */
22315 if (check_body_p)
22316 {
22317 list = cur_stmt_list;
22318 if (STATEMENT_LIST_TAIL (list))
22319 last = STATEMENT_LIST_TAIL (list)->stmt;
22320 }
22321 /* Parse the function-body. */
22322 cp_parser_function_body (parser, in_function_try_block);
22323 if (check_body_p)
22324 check_constexpr_ctor_body (last, list, /*complain=*/true);
22325 /* Finish the function body. */
22326 finish_function_body (body);
22327 }
22328
22329 /* Parse an initializer.
22330
22331 initializer:
22332 = initializer-clause
22333 ( expression-list )
22334
22335 Returns an expression representing the initializer. If no
22336 initializer is present, NULL_TREE is returned.
22337
22338 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22339 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22340 set to TRUE if there is no initializer present. If there is an
22341 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22342 is set to true; otherwise it is set to false. */
22343
22344 static tree
22345 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22346 bool* non_constant_p, bool subexpression_p)
22347 {
22348 cp_token *token;
22349 tree init;
22350
22351 /* Peek at the next token. */
22352 token = cp_lexer_peek_token (parser->lexer);
22353
22354 /* Let our caller know whether or not this initializer was
22355 parenthesized. */
22356 *is_direct_init = (token->type != CPP_EQ);
22357 /* Assume that the initializer is constant. */
22358 *non_constant_p = false;
22359
22360 if (token->type == CPP_EQ)
22361 {
22362 /* Consume the `='. */
22363 cp_lexer_consume_token (parser->lexer);
22364 /* Parse the initializer-clause. */
22365 init = cp_parser_initializer_clause (parser, non_constant_p);
22366 }
22367 else if (token->type == CPP_OPEN_PAREN)
22368 {
22369 vec<tree, va_gc> *vec;
22370 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22371 /*cast_p=*/false,
22372 /*allow_expansion_p=*/true,
22373 non_constant_p);
22374 if (vec == NULL)
22375 return error_mark_node;
22376 init = build_tree_list_vec (vec);
22377 release_tree_vector (vec);
22378 }
22379 else if (token->type == CPP_OPEN_BRACE)
22380 {
22381 cp_lexer_set_source_position (parser->lexer);
22382 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22383 init = cp_parser_braced_list (parser, non_constant_p);
22384 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22385 }
22386 else
22387 {
22388 /* Anything else is an error. */
22389 cp_parser_error (parser, "expected initializer");
22390 init = error_mark_node;
22391 }
22392
22393 if (!subexpression_p && check_for_bare_parameter_packs (init))
22394 init = error_mark_node;
22395
22396 return init;
22397 }
22398
22399 /* Parse an initializer-clause.
22400
22401 initializer-clause:
22402 assignment-expression
22403 braced-init-list
22404
22405 Returns an expression representing the initializer.
22406
22407 If the `assignment-expression' production is used the value
22408 returned is simply a representation for the expression.
22409
22410 Otherwise, calls cp_parser_braced_list. */
22411
22412 static cp_expr
22413 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22414 {
22415 cp_expr initializer;
22416
22417 /* Assume the expression is constant. */
22418 *non_constant_p = false;
22419
22420 /* If it is not a `{', then we are looking at an
22421 assignment-expression. */
22422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22423 {
22424 initializer
22425 = cp_parser_constant_expression (parser,
22426 /*allow_non_constant_p=*/true,
22427 non_constant_p);
22428 }
22429 else
22430 initializer = cp_parser_braced_list (parser, non_constant_p);
22431
22432 return initializer;
22433 }
22434
22435 /* Parse a brace-enclosed initializer list.
22436
22437 braced-init-list:
22438 { initializer-list , [opt] }
22439 { designated-initializer-list , [opt] }
22440 { }
22441
22442 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22443 the elements of the initializer-list (or NULL, if the last
22444 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22445 NULL_TREE. There is no way to detect whether or not the optional
22446 trailing `,' was provided. NON_CONSTANT_P is as for
22447 cp_parser_initializer. */
22448
22449 static cp_expr
22450 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22451 {
22452 tree initializer;
22453 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22454
22455 /* Consume the `{' token. */
22456 matching_braces braces;
22457 braces.require_open (parser);
22458 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22459 initializer = make_node (CONSTRUCTOR);
22460 /* If it's not a `}', then there is a non-trivial initializer. */
22461 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22462 {
22463 /* Parse the initializer list. */
22464 CONSTRUCTOR_ELTS (initializer)
22465 = cp_parser_initializer_list (parser, non_constant_p);
22466 /* A trailing `,' token is allowed. */
22467 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22468 cp_lexer_consume_token (parser->lexer);
22469 }
22470 else
22471 *non_constant_p = false;
22472 /* Now, there should be a trailing `}'. */
22473 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22474 braces.require_close (parser);
22475 TREE_TYPE (initializer) = init_list_type_node;
22476
22477 cp_expr result (initializer);
22478 /* Build a location of the form:
22479 { ... }
22480 ^~~~~~~
22481 with caret==start at the open brace, finish at the close brace. */
22482 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22483 result.set_location (combined_loc);
22484 return result;
22485 }
22486
22487 /* Consume tokens up to, and including, the next non-nested closing `]'.
22488 Returns true iff we found a closing `]'. */
22489
22490 static bool
22491 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22492 {
22493 unsigned square_depth = 0;
22494
22495 while (true)
22496 {
22497 cp_token * token = cp_lexer_peek_token (parser->lexer);
22498
22499 switch (token->type)
22500 {
22501 case CPP_EOF:
22502 case CPP_PRAGMA_EOL:
22503 /* If we've run out of tokens, then there is no closing `]'. */
22504 return false;
22505
22506 case CPP_OPEN_SQUARE:
22507 ++square_depth;
22508 break;
22509
22510 case CPP_CLOSE_SQUARE:
22511 if (!square_depth--)
22512 {
22513 cp_lexer_consume_token (parser->lexer);
22514 return true;
22515 }
22516 break;
22517
22518 default:
22519 break;
22520 }
22521
22522 /* Consume the token. */
22523 cp_lexer_consume_token (parser->lexer);
22524 }
22525 }
22526
22527 /* Return true if we are looking at an array-designator, false otherwise. */
22528
22529 static bool
22530 cp_parser_array_designator_p (cp_parser *parser)
22531 {
22532 /* Consume the `['. */
22533 cp_lexer_consume_token (parser->lexer);
22534
22535 cp_lexer_save_tokens (parser->lexer);
22536
22537 /* Skip tokens until the next token is a closing square bracket.
22538 If we find the closing `]', and the next token is a `=', then
22539 we are looking at an array designator. */
22540 bool array_designator_p
22541 = (cp_parser_skip_to_closing_square_bracket (parser)
22542 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22543
22544 /* Roll back the tokens we skipped. */
22545 cp_lexer_rollback_tokens (parser->lexer);
22546
22547 return array_designator_p;
22548 }
22549
22550 /* Parse an initializer-list.
22551
22552 initializer-list:
22553 initializer-clause ... [opt]
22554 initializer-list , initializer-clause ... [opt]
22555
22556 C++2A Extension:
22557
22558 designated-initializer-list:
22559 designated-initializer-clause
22560 designated-initializer-list , designated-initializer-clause
22561
22562 designated-initializer-clause:
22563 designator brace-or-equal-initializer
22564
22565 designator:
22566 . identifier
22567
22568 GNU Extension:
22569
22570 initializer-list:
22571 designation initializer-clause ...[opt]
22572 initializer-list , designation initializer-clause ...[opt]
22573
22574 designation:
22575 . identifier =
22576 identifier :
22577 [ constant-expression ] =
22578
22579 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22580 for the initializer. If the INDEX of the elt is non-NULL, it is the
22581 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22582 as for cp_parser_initializer. */
22583
22584 static vec<constructor_elt, va_gc> *
22585 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22586 {
22587 vec<constructor_elt, va_gc> *v = NULL;
22588 bool first_p = true;
22589 tree first_designator = NULL_TREE;
22590
22591 /* Assume all of the expressions are constant. */
22592 *non_constant_p = false;
22593
22594 /* Parse the rest of the list. */
22595 while (true)
22596 {
22597 cp_token *token;
22598 tree designator;
22599 tree initializer;
22600 bool clause_non_constant_p;
22601 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22602
22603 /* Handle the C++2A syntax, '. id ='. */
22604 if ((cxx_dialect >= cxx2a
22605 || cp_parser_allow_gnu_extensions_p (parser))
22606 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22607 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22608 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22609 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22610 == CPP_OPEN_BRACE)))
22611 {
22612 if (cxx_dialect < cxx2a)
22613 pedwarn (loc, OPT_Wpedantic,
22614 "C++ designated initializers only available with "
22615 "-std=c++2a or -std=gnu++2a");
22616 /* Consume the `.'. */
22617 cp_lexer_consume_token (parser->lexer);
22618 /* Consume the identifier. */
22619 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22620 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22621 /* Consume the `='. */
22622 cp_lexer_consume_token (parser->lexer);
22623 }
22624 /* Also, if the next token is an identifier and the following one is a
22625 colon, we are looking at the GNU designated-initializer
22626 syntax. */
22627 else if (cp_parser_allow_gnu_extensions_p (parser)
22628 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22629 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22630 == CPP_COLON))
22631 {
22632 /* Warn the user that they are using an extension. */
22633 pedwarn (loc, OPT_Wpedantic,
22634 "ISO C++ does not allow GNU designated initializers");
22635 /* Consume the identifier. */
22636 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22637 /* Consume the `:'. */
22638 cp_lexer_consume_token (parser->lexer);
22639 }
22640 /* Also handle C99 array designators, '[ const ] ='. */
22641 else if (cp_parser_allow_gnu_extensions_p (parser)
22642 && !c_dialect_objc ()
22643 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22644 {
22645 /* In C++11, [ could start a lambda-introducer. */
22646 bool non_const = false;
22647
22648 cp_parser_parse_tentatively (parser);
22649
22650 if (!cp_parser_array_designator_p (parser))
22651 {
22652 cp_parser_simulate_error (parser);
22653 designator = NULL_TREE;
22654 }
22655 else
22656 {
22657 designator = cp_parser_constant_expression (parser, true,
22658 &non_const);
22659 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22660 cp_parser_require (parser, CPP_EQ, RT_EQ);
22661 }
22662
22663 if (!cp_parser_parse_definitely (parser))
22664 designator = NULL_TREE;
22665 else if (non_const
22666 && (!require_potential_rvalue_constant_expression
22667 (designator)))
22668 designator = NULL_TREE;
22669 if (designator)
22670 /* Warn the user that they are using an extension. */
22671 pedwarn (loc, OPT_Wpedantic,
22672 "ISO C++ does not allow C99 designated initializers");
22673 }
22674 else
22675 designator = NULL_TREE;
22676
22677 if (first_p)
22678 {
22679 first_designator = designator;
22680 first_p = false;
22681 }
22682 else if (cxx_dialect >= cxx2a
22683 && first_designator != error_mark_node
22684 && (!first_designator != !designator))
22685 {
22686 error_at (loc, "either all initializer clauses should be designated "
22687 "or none of them should be");
22688 first_designator = error_mark_node;
22689 }
22690 else if (cxx_dialect < cxx2a && !first_designator)
22691 first_designator = designator;
22692
22693 /* Parse the initializer. */
22694 initializer = cp_parser_initializer_clause (parser,
22695 &clause_non_constant_p);
22696 /* If any clause is non-constant, so is the entire initializer. */
22697 if (clause_non_constant_p)
22698 *non_constant_p = true;
22699
22700 /* If we have an ellipsis, this is an initializer pack
22701 expansion. */
22702 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22703 {
22704 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22705
22706 /* Consume the `...'. */
22707 cp_lexer_consume_token (parser->lexer);
22708
22709 if (designator && cxx_dialect >= cxx2a)
22710 error_at (loc,
22711 "%<...%> not allowed in designated initializer list");
22712
22713 /* Turn the initializer into an initializer expansion. */
22714 initializer = make_pack_expansion (initializer);
22715 }
22716
22717 /* Add it to the vector. */
22718 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22719
22720 /* If the next token is not a comma, we have reached the end of
22721 the list. */
22722 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22723 break;
22724
22725 /* Peek at the next token. */
22726 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22727 /* If the next token is a `}', then we're still done. An
22728 initializer-clause can have a trailing `,' after the
22729 initializer-list and before the closing `}'. */
22730 if (token->type == CPP_CLOSE_BRACE)
22731 break;
22732
22733 /* Consume the `,' token. */
22734 cp_lexer_consume_token (parser->lexer);
22735 }
22736
22737 /* The same identifier shall not appear in multiple designators
22738 of a designated-initializer-list. */
22739 if (first_designator)
22740 {
22741 unsigned int i;
22742 tree designator, val;
22743 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22744 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22745 {
22746 if (IDENTIFIER_MARKED (designator))
22747 {
22748 error_at (cp_expr_loc_or_loc (val, input_location),
22749 "%<.%s%> designator used multiple times in "
22750 "the same initializer list",
22751 IDENTIFIER_POINTER (designator));
22752 (*v)[i].index = error_mark_node;
22753 }
22754 else
22755 IDENTIFIER_MARKED (designator) = 1;
22756 }
22757 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22758 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22759 IDENTIFIER_MARKED (designator) = 0;
22760 }
22761
22762 return v;
22763 }
22764
22765 /* Classes [gram.class] */
22766
22767 /* Parse a class-name.
22768
22769 class-name:
22770 identifier
22771 template-id
22772
22773 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22774 to indicate that names looked up in dependent types should be
22775 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22776 keyword has been used to indicate that the name that appears next
22777 is a template. TAG_TYPE indicates the explicit tag given before
22778 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22779 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22780 is the class being defined in a class-head. If ENUM_OK is TRUE,
22781 enum-names are also accepted.
22782
22783 Returns the TYPE_DECL representing the class. */
22784
22785 static tree
22786 cp_parser_class_name (cp_parser *parser,
22787 bool typename_keyword_p,
22788 bool template_keyword_p,
22789 enum tag_types tag_type,
22790 bool check_dependency_p,
22791 bool class_head_p,
22792 bool is_declaration,
22793 bool enum_ok)
22794 {
22795 tree decl;
22796 tree scope;
22797 bool typename_p;
22798 cp_token *token;
22799 tree identifier = NULL_TREE;
22800
22801 /* All class-names start with an identifier. */
22802 token = cp_lexer_peek_token (parser->lexer);
22803 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22804 {
22805 cp_parser_error (parser, "expected class-name");
22806 return error_mark_node;
22807 }
22808
22809 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22810 to a template-id, so we save it here. */
22811 scope = parser->scope;
22812 if (scope == error_mark_node)
22813 return error_mark_node;
22814
22815 /* Any name names a type if we're following the `typename' keyword
22816 in a qualified name where the enclosing scope is type-dependent. */
22817 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22818 && dependent_type_p (scope));
22819 /* Handle the common case (an identifier, but not a template-id)
22820 efficiently. */
22821 if (token->type == CPP_NAME
22822 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22823 {
22824 cp_token *identifier_token;
22825 bool ambiguous_p;
22826
22827 /* Look for the identifier. */
22828 identifier_token = cp_lexer_peek_token (parser->lexer);
22829 ambiguous_p = identifier_token->error_reported;
22830 identifier = cp_parser_identifier (parser);
22831 /* If the next token isn't an identifier, we are certainly not
22832 looking at a class-name. */
22833 if (identifier == error_mark_node)
22834 decl = error_mark_node;
22835 /* If we know this is a type-name, there's no need to look it
22836 up. */
22837 else if (typename_p)
22838 decl = identifier;
22839 else
22840 {
22841 tree ambiguous_decls;
22842 /* If we already know that this lookup is ambiguous, then
22843 we've already issued an error message; there's no reason
22844 to check again. */
22845 if (ambiguous_p)
22846 {
22847 cp_parser_simulate_error (parser);
22848 return error_mark_node;
22849 }
22850 /* If the next token is a `::', then the name must be a type
22851 name.
22852
22853 [basic.lookup.qual]
22854
22855 During the lookup for a name preceding the :: scope
22856 resolution operator, object, function, and enumerator
22857 names are ignored. */
22858 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22859 tag_type = scope_type;
22860 /* Look up the name. */
22861 decl = cp_parser_lookup_name (parser, identifier,
22862 tag_type,
22863 /*is_template=*/false,
22864 /*is_namespace=*/false,
22865 check_dependency_p,
22866 &ambiguous_decls,
22867 identifier_token->location);
22868 if (ambiguous_decls)
22869 {
22870 if (cp_parser_parsing_tentatively (parser))
22871 cp_parser_simulate_error (parser);
22872 return error_mark_node;
22873 }
22874 }
22875 }
22876 else
22877 {
22878 /* Try a template-id. */
22879 decl = cp_parser_template_id (parser, template_keyword_p,
22880 check_dependency_p,
22881 tag_type,
22882 is_declaration);
22883 if (decl == error_mark_node)
22884 return error_mark_node;
22885 }
22886
22887 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22888
22889 /* If this is a typename, create a TYPENAME_TYPE. */
22890 if (typename_p && decl != error_mark_node)
22891 {
22892 decl = make_typename_type (scope, decl, typename_type,
22893 /*complain=*/tf_error);
22894 if (decl != error_mark_node)
22895 decl = TYPE_NAME (decl);
22896 }
22897
22898 decl = strip_using_decl (decl);
22899
22900 /* Check to see that it is really the name of a class. */
22901 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22902 && identifier_p (TREE_OPERAND (decl, 0))
22903 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22904 /* Situations like this:
22905
22906 template <typename T> struct A {
22907 typename T::template X<int>::I i;
22908 };
22909
22910 are problematic. Is `T::template X<int>' a class-name? The
22911 standard does not seem to be definitive, but there is no other
22912 valid interpretation of the following `::'. Therefore, those
22913 names are considered class-names. */
22914 {
22915 decl = make_typename_type (scope, decl, tag_type, tf_error);
22916 if (decl != error_mark_node)
22917 decl = TYPE_NAME (decl);
22918 }
22919 else if (TREE_CODE (decl) != TYPE_DECL
22920 || TREE_TYPE (decl) == error_mark_node
22921 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22922 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22923 /* In Objective-C 2.0, a classname followed by '.' starts a
22924 dot-syntax expression, and it's not a type-name. */
22925 || (c_dialect_objc ()
22926 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22927 && objc_is_class_name (decl)))
22928 decl = error_mark_node;
22929
22930 if (decl == error_mark_node)
22931 cp_parser_error (parser, "expected class-name");
22932 else if (identifier && !parser->scope)
22933 maybe_note_name_used_in_class (identifier, decl);
22934
22935 return decl;
22936 }
22937
22938 /* Parse a class-specifier.
22939
22940 class-specifier:
22941 class-head { member-specification [opt] }
22942
22943 Returns the TREE_TYPE representing the class. */
22944
22945 static tree
22946 cp_parser_class_specifier_1 (cp_parser* parser)
22947 {
22948 tree type;
22949 tree attributes = NULL_TREE;
22950 bool nested_name_specifier_p;
22951 unsigned saved_num_template_parameter_lists;
22952 bool saved_in_function_body;
22953 unsigned char in_statement;
22954 bool in_switch_statement_p;
22955 bool saved_in_unbraced_linkage_specification_p;
22956 tree old_scope = NULL_TREE;
22957 tree scope = NULL_TREE;
22958 cp_token *closing_brace;
22959
22960 push_deferring_access_checks (dk_no_deferred);
22961
22962 /* Parse the class-head. */
22963 type = cp_parser_class_head (parser,
22964 &nested_name_specifier_p);
22965 /* If the class-head was a semantic disaster, skip the entire body
22966 of the class. */
22967 if (!type)
22968 {
22969 cp_parser_skip_to_end_of_block_or_statement (parser);
22970 pop_deferring_access_checks ();
22971 return error_mark_node;
22972 }
22973
22974 /* Look for the `{'. */
22975 matching_braces braces;
22976 if (!braces.require_open (parser))
22977 {
22978 pop_deferring_access_checks ();
22979 return error_mark_node;
22980 }
22981
22982 cp_ensure_no_omp_declare_simd (parser);
22983 cp_ensure_no_oacc_routine (parser);
22984
22985 /* Issue an error message if type-definitions are forbidden here. */
22986 cp_parser_check_type_definition (parser);
22987 /* Remember that we are defining one more class. */
22988 ++parser->num_classes_being_defined;
22989 /* Inside the class, surrounding template-parameter-lists do not
22990 apply. */
22991 saved_num_template_parameter_lists
22992 = parser->num_template_parameter_lists;
22993 parser->num_template_parameter_lists = 0;
22994 /* We are not in a function body. */
22995 saved_in_function_body = parser->in_function_body;
22996 parser->in_function_body = false;
22997 /* Or in a loop. */
22998 in_statement = parser->in_statement;
22999 parser->in_statement = 0;
23000 /* Or in a switch. */
23001 in_switch_statement_p = parser->in_switch_statement_p;
23002 parser->in_switch_statement_p = false;
23003 /* We are not immediately inside an extern "lang" block. */
23004 saved_in_unbraced_linkage_specification_p
23005 = parser->in_unbraced_linkage_specification_p;
23006 parser->in_unbraced_linkage_specification_p = false;
23007
23008 // Associate constraints with the type.
23009 if (flag_concepts)
23010 type = associate_classtype_constraints (type);
23011
23012 /* Start the class. */
23013 if (nested_name_specifier_p)
23014 {
23015 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23016 old_scope = push_inner_scope (scope);
23017 }
23018 type = begin_class_definition (type);
23019
23020 if (type == error_mark_node)
23021 /* If the type is erroneous, skip the entire body of the class. */
23022 cp_parser_skip_to_closing_brace (parser);
23023 else
23024 /* Parse the member-specification. */
23025 cp_parser_member_specification_opt (parser);
23026
23027 /* Look for the trailing `}'. */
23028 closing_brace = braces.require_close (parser);
23029 /* Look for trailing attributes to apply to this class. */
23030 if (cp_parser_allow_gnu_extensions_p (parser))
23031 attributes = cp_parser_gnu_attributes_opt (parser);
23032 if (type != error_mark_node)
23033 type = finish_struct (type, attributes);
23034 if (nested_name_specifier_p)
23035 pop_inner_scope (old_scope, scope);
23036
23037 /* We've finished a type definition. Check for the common syntax
23038 error of forgetting a semicolon after the definition. We need to
23039 be careful, as we can't just check for not-a-semicolon and be done
23040 with it; the user might have typed:
23041
23042 class X { } c = ...;
23043 class X { } *p = ...;
23044
23045 and so forth. Instead, enumerate all the possible tokens that
23046 might follow this production; if we don't see one of them, then
23047 complain and silently insert the semicolon. */
23048 {
23049 cp_token *token = cp_lexer_peek_token (parser->lexer);
23050 bool want_semicolon = true;
23051
23052 if (cp_next_tokens_can_be_std_attribute_p (parser))
23053 /* Don't try to parse c++11 attributes here. As per the
23054 grammar, that should be a task for
23055 cp_parser_decl_specifier_seq. */
23056 want_semicolon = false;
23057
23058 switch (token->type)
23059 {
23060 case CPP_NAME:
23061 case CPP_SEMICOLON:
23062 case CPP_MULT:
23063 case CPP_AND:
23064 case CPP_OPEN_PAREN:
23065 case CPP_CLOSE_PAREN:
23066 case CPP_COMMA:
23067 want_semicolon = false;
23068 break;
23069
23070 /* While it's legal for type qualifiers and storage class
23071 specifiers to follow type definitions in the grammar, only
23072 compiler testsuites contain code like that. Assume that if
23073 we see such code, then what we're really seeing is a case
23074 like:
23075
23076 class X { }
23077 const <type> var = ...;
23078
23079 or
23080
23081 class Y { }
23082 static <type> func (...) ...
23083
23084 i.e. the qualifier or specifier applies to the next
23085 declaration. To do so, however, we need to look ahead one
23086 more token to see if *that* token is a type specifier.
23087
23088 This code could be improved to handle:
23089
23090 class Z { }
23091 static const <type> var = ...; */
23092 case CPP_KEYWORD:
23093 if (keyword_is_decl_specifier (token->keyword))
23094 {
23095 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23096
23097 /* Handling user-defined types here would be nice, but very
23098 tricky. */
23099 want_semicolon
23100 = (lookahead->type == CPP_KEYWORD
23101 && keyword_begins_type_specifier (lookahead->keyword));
23102 }
23103 break;
23104 default:
23105 break;
23106 }
23107
23108 /* If we don't have a type, then something is very wrong and we
23109 shouldn't try to do anything clever. Likewise for not seeing the
23110 closing brace. */
23111 if (closing_brace && TYPE_P (type) && want_semicolon)
23112 {
23113 /* Locate the closing brace. */
23114 cp_token_position prev
23115 = cp_lexer_previous_token_position (parser->lexer);
23116 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23117 location_t loc = prev_token->location;
23118
23119 /* We want to suggest insertion of a ';' immediately *after* the
23120 closing brace, so, if we can, offset the location by 1 column. */
23121 location_t next_loc = loc;
23122 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23123 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23124
23125 rich_location richloc (line_table, next_loc);
23126
23127 /* If we successfully offset the location, suggest the fix-it. */
23128 if (next_loc != loc)
23129 richloc.add_fixit_insert_before (next_loc, ";");
23130
23131 if (CLASSTYPE_DECLARED_CLASS (type))
23132 error_at (&richloc,
23133 "expected %<;%> after class definition");
23134 else if (TREE_CODE (type) == RECORD_TYPE)
23135 error_at (&richloc,
23136 "expected %<;%> after struct definition");
23137 else if (TREE_CODE (type) == UNION_TYPE)
23138 error_at (&richloc,
23139 "expected %<;%> after union definition");
23140 else
23141 gcc_unreachable ();
23142
23143 /* Unget one token and smash it to look as though we encountered
23144 a semicolon in the input stream. */
23145 cp_lexer_set_token_position (parser->lexer, prev);
23146 token = cp_lexer_peek_token (parser->lexer);
23147 token->type = CPP_SEMICOLON;
23148 token->keyword = RID_MAX;
23149 }
23150 }
23151
23152 /* If this class is not itself within the scope of another class,
23153 then we need to parse the bodies of all of the queued function
23154 definitions. Note that the queued functions defined in a class
23155 are not always processed immediately following the
23156 class-specifier for that class. Consider:
23157
23158 struct A {
23159 struct B { void f() { sizeof (A); } };
23160 };
23161
23162 If `f' were processed before the processing of `A' were
23163 completed, there would be no way to compute the size of `A'.
23164 Note that the nesting we are interested in here is lexical --
23165 not the semantic nesting given by TYPE_CONTEXT. In particular,
23166 for:
23167
23168 struct A { struct B; };
23169 struct A::B { void f() { } };
23170
23171 there is no need to delay the parsing of `A::B::f'. */
23172 if (--parser->num_classes_being_defined == 0)
23173 {
23174 tree decl;
23175 tree class_type = NULL_TREE;
23176 tree pushed_scope = NULL_TREE;
23177 unsigned ix;
23178 cp_default_arg_entry *e;
23179 tree save_ccp, save_ccr;
23180
23181 if (any_erroneous_template_args_p (type))
23182 {
23183 /* Skip default arguments, NSDMIs, etc, in order to improve
23184 error recovery (c++/71169, c++/71832). */
23185 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23186 vec_safe_truncate (unparsed_nsdmis, 0);
23187 vec_safe_truncate (unparsed_classes, 0);
23188 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23189 }
23190
23191 /* In a first pass, parse default arguments to the functions.
23192 Then, in a second pass, parse the bodies of the functions.
23193 This two-phased approach handles cases like:
23194
23195 struct S {
23196 void f() { g(); }
23197 void g(int i = 3);
23198 };
23199
23200 */
23201 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23202 {
23203 decl = e->decl;
23204 /* If there are default arguments that have not yet been processed,
23205 take care of them now. */
23206 if (class_type != e->class_type)
23207 {
23208 if (pushed_scope)
23209 pop_scope (pushed_scope);
23210 class_type = e->class_type;
23211 pushed_scope = push_scope (class_type);
23212 }
23213 /* Make sure that any template parameters are in scope. */
23214 maybe_begin_member_template_processing (decl);
23215 /* Parse the default argument expressions. */
23216 cp_parser_late_parsing_default_args (parser, decl);
23217 /* Remove any template parameters from the symbol table. */
23218 maybe_end_member_template_processing ();
23219 }
23220 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23221 /* Now parse any NSDMIs. */
23222 save_ccp = current_class_ptr;
23223 save_ccr = current_class_ref;
23224 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23225 {
23226 if (class_type != DECL_CONTEXT (decl))
23227 {
23228 if (pushed_scope)
23229 pop_scope (pushed_scope);
23230 class_type = DECL_CONTEXT (decl);
23231 pushed_scope = push_scope (class_type);
23232 }
23233 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23234 cp_parser_late_parsing_nsdmi (parser, decl);
23235 }
23236 vec_safe_truncate (unparsed_nsdmis, 0);
23237 current_class_ptr = save_ccp;
23238 current_class_ref = save_ccr;
23239 if (pushed_scope)
23240 pop_scope (pushed_scope);
23241
23242 /* Now do some post-NSDMI bookkeeping. */
23243 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23244 after_nsdmi_defaulted_late_checks (class_type);
23245 vec_safe_truncate (unparsed_classes, 0);
23246 after_nsdmi_defaulted_late_checks (type);
23247
23248 /* Now parse the body of the functions. */
23249 if (flag_openmp)
23250 {
23251 /* OpenMP UDRs need to be parsed before all other functions. */
23252 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23253 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23254 cp_parser_late_parsing_for_member (parser, decl);
23255 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23256 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23257 cp_parser_late_parsing_for_member (parser, decl);
23258 }
23259 else
23260 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23261 cp_parser_late_parsing_for_member (parser, decl);
23262 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23263 }
23264 else
23265 vec_safe_push (unparsed_classes, type);
23266
23267 /* Put back any saved access checks. */
23268 pop_deferring_access_checks ();
23269
23270 /* Restore saved state. */
23271 parser->in_switch_statement_p = in_switch_statement_p;
23272 parser->in_statement = in_statement;
23273 parser->in_function_body = saved_in_function_body;
23274 parser->num_template_parameter_lists
23275 = saved_num_template_parameter_lists;
23276 parser->in_unbraced_linkage_specification_p
23277 = saved_in_unbraced_linkage_specification_p;
23278
23279 return type;
23280 }
23281
23282 static tree
23283 cp_parser_class_specifier (cp_parser* parser)
23284 {
23285 tree ret;
23286 timevar_push (TV_PARSE_STRUCT);
23287 ret = cp_parser_class_specifier_1 (parser);
23288 timevar_pop (TV_PARSE_STRUCT);
23289 return ret;
23290 }
23291
23292 /* Parse a class-head.
23293
23294 class-head:
23295 class-key identifier [opt] base-clause [opt]
23296 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23297 class-key nested-name-specifier [opt] template-id
23298 base-clause [opt]
23299
23300 class-virt-specifier:
23301 final
23302
23303 GNU Extensions:
23304 class-key attributes identifier [opt] base-clause [opt]
23305 class-key attributes nested-name-specifier identifier base-clause [opt]
23306 class-key attributes nested-name-specifier [opt] template-id
23307 base-clause [opt]
23308
23309 Upon return BASES is initialized to the list of base classes (or
23310 NULL, if there are none) in the same form returned by
23311 cp_parser_base_clause.
23312
23313 Returns the TYPE of the indicated class. Sets
23314 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23315 involving a nested-name-specifier was used, and FALSE otherwise.
23316
23317 Returns error_mark_node if this is not a class-head.
23318
23319 Returns NULL_TREE if the class-head is syntactically valid, but
23320 semantically invalid in a way that means we should skip the entire
23321 body of the class. */
23322
23323 static tree
23324 cp_parser_class_head (cp_parser* parser,
23325 bool* nested_name_specifier_p)
23326 {
23327 tree nested_name_specifier;
23328 enum tag_types class_key;
23329 tree id = NULL_TREE;
23330 tree type = NULL_TREE;
23331 tree attributes;
23332 tree bases;
23333 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23334 bool template_id_p = false;
23335 bool qualified_p = false;
23336 bool invalid_nested_name_p = false;
23337 bool invalid_explicit_specialization_p = false;
23338 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23339 tree pushed_scope = NULL_TREE;
23340 unsigned num_templates;
23341 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23342 /* Assume no nested-name-specifier will be present. */
23343 *nested_name_specifier_p = false;
23344 /* Assume no template parameter lists will be used in defining the
23345 type. */
23346 num_templates = 0;
23347 parser->colon_corrects_to_scope_p = false;
23348
23349 /* Look for the class-key. */
23350 class_key = cp_parser_class_key (parser);
23351 if (class_key == none_type)
23352 return error_mark_node;
23353
23354 location_t class_head_start_location = input_location;
23355
23356 /* Parse the attributes. */
23357 attributes = cp_parser_attributes_opt (parser);
23358
23359 /* If the next token is `::', that is invalid -- but sometimes
23360 people do try to write:
23361
23362 struct ::S {};
23363
23364 Handle this gracefully by accepting the extra qualifier, and then
23365 issuing an error about it later if this really is a
23366 class-head. If it turns out just to be an elaborated type
23367 specifier, remain silent. */
23368 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23369 qualified_p = true;
23370
23371 push_deferring_access_checks (dk_no_check);
23372
23373 /* Determine the name of the class. Begin by looking for an
23374 optional nested-name-specifier. */
23375 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23376 nested_name_specifier
23377 = cp_parser_nested_name_specifier_opt (parser,
23378 /*typename_keyword_p=*/false,
23379 /*check_dependency_p=*/false,
23380 /*type_p=*/true,
23381 /*is_declaration=*/false);
23382 /* If there was a nested-name-specifier, then there *must* be an
23383 identifier. */
23384
23385 cp_token *bad_template_keyword = NULL;
23386
23387 if (nested_name_specifier)
23388 {
23389 type_start_token = cp_lexer_peek_token (parser->lexer);
23390 /* Although the grammar says `identifier', it really means
23391 `class-name' or `template-name'. You are only allowed to
23392 define a class that has already been declared with this
23393 syntax.
23394
23395 The proposed resolution for Core Issue 180 says that wherever
23396 you see `class T::X' you should treat `X' as a type-name.
23397
23398 It is OK to define an inaccessible class; for example:
23399
23400 class A { class B; };
23401 class A::B {};
23402
23403 We do not know if we will see a class-name, or a
23404 template-name. We look for a class-name first, in case the
23405 class-name is a template-id; if we looked for the
23406 template-name first we would stop after the template-name. */
23407 cp_parser_parse_tentatively (parser);
23408 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23409 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23410 type = cp_parser_class_name (parser,
23411 /*typename_keyword_p=*/false,
23412 /*template_keyword_p=*/false,
23413 class_type,
23414 /*check_dependency_p=*/false,
23415 /*class_head_p=*/true,
23416 /*is_declaration=*/false);
23417 /* If that didn't work, ignore the nested-name-specifier. */
23418 if (!cp_parser_parse_definitely (parser))
23419 {
23420 invalid_nested_name_p = true;
23421 type_start_token = cp_lexer_peek_token (parser->lexer);
23422 id = cp_parser_identifier (parser);
23423 if (id == error_mark_node)
23424 id = NULL_TREE;
23425 }
23426 /* If we could not find a corresponding TYPE, treat this
23427 declaration like an unqualified declaration. */
23428 if (type == error_mark_node)
23429 nested_name_specifier = NULL_TREE;
23430 /* Otherwise, count the number of templates used in TYPE and its
23431 containing scopes. */
23432 else
23433 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23434 }
23435 /* Otherwise, the identifier is optional. */
23436 else
23437 {
23438 /* We don't know whether what comes next is a template-id,
23439 an identifier, or nothing at all. */
23440 cp_parser_parse_tentatively (parser);
23441 /* Check for a template-id. */
23442 type_start_token = cp_lexer_peek_token (parser->lexer);
23443 id = cp_parser_template_id (parser,
23444 /*template_keyword_p=*/false,
23445 /*check_dependency_p=*/true,
23446 class_key,
23447 /*is_declaration=*/true);
23448 /* If that didn't work, it could still be an identifier. */
23449 if (!cp_parser_parse_definitely (parser))
23450 {
23451 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23452 {
23453 type_start_token = cp_lexer_peek_token (parser->lexer);
23454 id = cp_parser_identifier (parser);
23455 }
23456 else
23457 id = NULL_TREE;
23458 }
23459 else
23460 {
23461 template_id_p = true;
23462 ++num_templates;
23463 }
23464 }
23465
23466 pop_deferring_access_checks ();
23467
23468 if (id)
23469 {
23470 cp_parser_check_for_invalid_template_id (parser, id,
23471 class_key,
23472 type_start_token->location);
23473 }
23474 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23475
23476 /* If it's not a `:' or a `{' then we can't really be looking at a
23477 class-head, since a class-head only appears as part of a
23478 class-specifier. We have to detect this situation before calling
23479 xref_tag, since that has irreversible side-effects. */
23480 if (!cp_parser_next_token_starts_class_definition_p (parser))
23481 {
23482 cp_parser_error (parser, "expected %<{%> or %<:%>");
23483 type = error_mark_node;
23484 goto out;
23485 }
23486
23487 /* At this point, we're going ahead with the class-specifier, even
23488 if some other problem occurs. */
23489 cp_parser_commit_to_tentative_parse (parser);
23490 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23491 {
23492 cp_parser_error (parser,
23493 "cannot specify %<override%> for a class");
23494 type = error_mark_node;
23495 goto out;
23496 }
23497 /* Issue the error about the overly-qualified name now. */
23498 if (qualified_p)
23499 {
23500 cp_parser_error (parser,
23501 "global qualification of class name is invalid");
23502 type = error_mark_node;
23503 goto out;
23504 }
23505 else if (invalid_nested_name_p)
23506 {
23507 cp_parser_error (parser,
23508 "qualified name does not name a class");
23509 type = error_mark_node;
23510 goto out;
23511 }
23512 else if (nested_name_specifier)
23513 {
23514 tree scope;
23515
23516 if (bad_template_keyword)
23517 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23518 keyword template shall not appear at the top level. */
23519 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23520 "keyword %<template%> not allowed in class-head-name");
23521
23522 /* Reject typedef-names in class heads. */
23523 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23524 {
23525 error_at (type_start_token->location,
23526 "invalid class name in declaration of %qD",
23527 type);
23528 type = NULL_TREE;
23529 goto done;
23530 }
23531
23532 /* Figure out in what scope the declaration is being placed. */
23533 scope = current_scope ();
23534 /* If that scope does not contain the scope in which the
23535 class was originally declared, the program is invalid. */
23536 if (scope && !is_ancestor (scope, nested_name_specifier))
23537 {
23538 if (at_namespace_scope_p ())
23539 error_at (type_start_token->location,
23540 "declaration of %qD in namespace %qD which does not "
23541 "enclose %qD",
23542 type, scope, nested_name_specifier);
23543 else
23544 error_at (type_start_token->location,
23545 "declaration of %qD in %qD which does not enclose %qD",
23546 type, scope, nested_name_specifier);
23547 type = NULL_TREE;
23548 goto done;
23549 }
23550 /* [dcl.meaning]
23551
23552 A declarator-id shall not be qualified except for the
23553 definition of a ... nested class outside of its class
23554 ... [or] the definition or explicit instantiation of a
23555 class member of a namespace outside of its namespace. */
23556 if (scope == nested_name_specifier)
23557 {
23558 permerror (nested_name_specifier_token_start->location,
23559 "extra qualification not allowed");
23560 nested_name_specifier = NULL_TREE;
23561 num_templates = 0;
23562 }
23563 }
23564 /* An explicit-specialization must be preceded by "template <>". If
23565 it is not, try to recover gracefully. */
23566 if (at_namespace_scope_p ()
23567 && parser->num_template_parameter_lists == 0
23568 && !processing_template_parmlist
23569 && template_id_p)
23570 {
23571 /* Build a location of this form:
23572 struct typename <ARGS>
23573 ^~~~~~~~~~~~~~~~~~~~~~
23574 with caret==start at the start token, and
23575 finishing at the end of the type. */
23576 location_t reported_loc
23577 = make_location (class_head_start_location,
23578 class_head_start_location,
23579 get_finish (type_start_token->location));
23580 rich_location richloc (line_table, reported_loc);
23581 richloc.add_fixit_insert_before (class_head_start_location,
23582 "template <> ");
23583 error_at (&richloc,
23584 "an explicit specialization must be preceded by"
23585 " %<template <>%>");
23586 invalid_explicit_specialization_p = true;
23587 /* Take the same action that would have been taken by
23588 cp_parser_explicit_specialization. */
23589 ++parser->num_template_parameter_lists;
23590 begin_specialization ();
23591 }
23592 /* There must be no "return" statements between this point and the
23593 end of this function; set "type "to the correct return value and
23594 use "goto done;" to return. */
23595 /* Make sure that the right number of template parameters were
23596 present. */
23597 if (!cp_parser_check_template_parameters (parser, num_templates,
23598 template_id_p,
23599 type_start_token->location,
23600 /*declarator=*/NULL))
23601 {
23602 /* If something went wrong, there is no point in even trying to
23603 process the class-definition. */
23604 type = NULL_TREE;
23605 goto done;
23606 }
23607
23608 /* Look up the type. */
23609 if (template_id_p)
23610 {
23611 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23612 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23613 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23614 {
23615 error_at (type_start_token->location,
23616 "function template %qD redeclared as a class template", id);
23617 type = error_mark_node;
23618 }
23619 else
23620 {
23621 type = TREE_TYPE (id);
23622 type = maybe_process_partial_specialization (type);
23623
23624 /* Check the scope while we still know whether or not we had a
23625 nested-name-specifier. */
23626 if (type != error_mark_node)
23627 check_unqualified_spec_or_inst (type, type_start_token->location);
23628 }
23629 if (nested_name_specifier)
23630 pushed_scope = push_scope (nested_name_specifier);
23631 }
23632 else if (nested_name_specifier)
23633 {
23634 tree class_type;
23635
23636 /* Given:
23637
23638 template <typename T> struct S { struct T };
23639 template <typename T> struct S<T>::T { };
23640
23641 we will get a TYPENAME_TYPE when processing the definition of
23642 `S::T'. We need to resolve it to the actual type before we
23643 try to define it. */
23644 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23645 {
23646 class_type = resolve_typename_type (TREE_TYPE (type),
23647 /*only_current_p=*/false);
23648 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23649 type = TYPE_NAME (class_type);
23650 else
23651 {
23652 cp_parser_error (parser, "could not resolve typename type");
23653 type = error_mark_node;
23654 }
23655 }
23656
23657 if (maybe_process_partial_specialization (TREE_TYPE (type))
23658 == error_mark_node)
23659 {
23660 type = NULL_TREE;
23661 goto done;
23662 }
23663
23664 class_type = current_class_type;
23665 /* Enter the scope indicated by the nested-name-specifier. */
23666 pushed_scope = push_scope (nested_name_specifier);
23667 /* Get the canonical version of this type. */
23668 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23669 /* Call push_template_decl if it seems like we should be defining a
23670 template either from the template headers or the type we're
23671 defining, so that we diagnose both extra and missing headers. */
23672 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23673 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23674 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23675 {
23676 type = push_template_decl (type);
23677 if (type == error_mark_node)
23678 {
23679 type = NULL_TREE;
23680 goto done;
23681 }
23682 }
23683
23684 type = TREE_TYPE (type);
23685 *nested_name_specifier_p = true;
23686 }
23687 else /* The name is not a nested name. */
23688 {
23689 /* If the class was unnamed, create a dummy name. */
23690 if (!id)
23691 id = make_anon_name ();
23692 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23693 ? ts_within_enclosing_non_class
23694 : ts_current);
23695 type = xref_tag (class_key, id, tag_scope,
23696 parser->num_template_parameter_lists);
23697 }
23698
23699 /* Indicate whether this class was declared as a `class' or as a
23700 `struct'. */
23701 if (TREE_CODE (type) == RECORD_TYPE)
23702 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23703 cp_parser_check_class_key (class_key, type);
23704
23705 /* If this type was already complete, and we see another definition,
23706 that's an error. */
23707 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23708 {
23709 error_at (type_start_token->location, "redefinition of %q#T",
23710 type);
23711 inform (location_of (type), "previous definition of %q#T",
23712 type);
23713 type = NULL_TREE;
23714 goto done;
23715 }
23716 else if (type == error_mark_node)
23717 type = NULL_TREE;
23718
23719 if (type)
23720 {
23721 /* Apply attributes now, before any use of the class as a template
23722 argument in its base list. */
23723 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23724 fixup_attribute_variants (type);
23725 }
23726
23727 /* We will have entered the scope containing the class; the names of
23728 base classes should be looked up in that context. For example:
23729
23730 struct A { struct B {}; struct C; };
23731 struct A::C : B {};
23732
23733 is valid. */
23734
23735 /* Get the list of base-classes, if there is one. */
23736 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23737 {
23738 /* PR59482: enter the class scope so that base-specifiers are looked
23739 up correctly. */
23740 if (type)
23741 pushclass (type);
23742 bases = cp_parser_base_clause (parser);
23743 /* PR59482: get out of the previously pushed class scope so that the
23744 subsequent pops pop the right thing. */
23745 if (type)
23746 popclass ();
23747 }
23748 else
23749 bases = NULL_TREE;
23750
23751 /* If we're really defining a class, process the base classes.
23752 If they're invalid, fail. */
23753 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23754 xref_basetypes (type, bases);
23755
23756 done:
23757 /* Leave the scope given by the nested-name-specifier. We will
23758 enter the class scope itself while processing the members. */
23759 if (pushed_scope)
23760 pop_scope (pushed_scope);
23761
23762 if (invalid_explicit_specialization_p)
23763 {
23764 end_specialization ();
23765 --parser->num_template_parameter_lists;
23766 }
23767
23768 if (type)
23769 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23770 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23771 CLASSTYPE_FINAL (type) = 1;
23772 out:
23773 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23774 return type;
23775 }
23776
23777 /* Parse a class-key.
23778
23779 class-key:
23780 class
23781 struct
23782 union
23783
23784 Returns the kind of class-key specified, or none_type to indicate
23785 error. */
23786
23787 static enum tag_types
23788 cp_parser_class_key (cp_parser* parser)
23789 {
23790 cp_token *token;
23791 enum tag_types tag_type;
23792
23793 /* Look for the class-key. */
23794 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23795 if (!token)
23796 return none_type;
23797
23798 /* Check to see if the TOKEN is a class-key. */
23799 tag_type = cp_parser_token_is_class_key (token);
23800 if (!tag_type)
23801 cp_parser_error (parser, "expected class-key");
23802 return tag_type;
23803 }
23804
23805 /* Parse a type-parameter-key.
23806
23807 type-parameter-key:
23808 class
23809 typename
23810 */
23811
23812 static void
23813 cp_parser_type_parameter_key (cp_parser* parser)
23814 {
23815 /* Look for the type-parameter-key. */
23816 enum tag_types tag_type = none_type;
23817 cp_token *token = cp_lexer_peek_token (parser->lexer);
23818 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23819 {
23820 cp_lexer_consume_token (parser->lexer);
23821 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23822 /* typename is not allowed in a template template parameter
23823 by the standard until C++17. */
23824 pedwarn (token->location, OPT_Wpedantic,
23825 "ISO C++ forbids typename key in template template parameter;"
23826 " use -std=c++17 or -std=gnu++17");
23827 }
23828 else
23829 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23830
23831 return;
23832 }
23833
23834 /* Parse an (optional) member-specification.
23835
23836 member-specification:
23837 member-declaration member-specification [opt]
23838 access-specifier : member-specification [opt] */
23839
23840 static void
23841 cp_parser_member_specification_opt (cp_parser* parser)
23842 {
23843 while (true)
23844 {
23845 cp_token *token;
23846 enum rid keyword;
23847
23848 /* Peek at the next token. */
23849 token = cp_lexer_peek_token (parser->lexer);
23850 /* If it's a `}', or EOF then we've seen all the members. */
23851 if (token->type == CPP_CLOSE_BRACE
23852 || token->type == CPP_EOF
23853 || token->type == CPP_PRAGMA_EOL)
23854 break;
23855
23856 /* See if this token is a keyword. */
23857 keyword = token->keyword;
23858 switch (keyword)
23859 {
23860 case RID_PUBLIC:
23861 case RID_PROTECTED:
23862 case RID_PRIVATE:
23863 /* Consume the access-specifier. */
23864 cp_lexer_consume_token (parser->lexer);
23865 /* Remember which access-specifier is active. */
23866 current_access_specifier = token->u.value;
23867 /* Look for the `:'. */
23868 cp_parser_require (parser, CPP_COLON, RT_COLON);
23869 break;
23870
23871 default:
23872 /* Accept #pragmas at class scope. */
23873 if (token->type == CPP_PRAGMA)
23874 {
23875 cp_parser_pragma (parser, pragma_member, NULL);
23876 break;
23877 }
23878
23879 /* Otherwise, the next construction must be a
23880 member-declaration. */
23881 cp_parser_member_declaration (parser);
23882 }
23883 }
23884 }
23885
23886 /* Parse a member-declaration.
23887
23888 member-declaration:
23889 decl-specifier-seq [opt] member-declarator-list [opt] ;
23890 function-definition ; [opt]
23891 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23892 using-declaration
23893 template-declaration
23894 alias-declaration
23895
23896 member-declarator-list:
23897 member-declarator
23898 member-declarator-list , member-declarator
23899
23900 member-declarator:
23901 declarator pure-specifier [opt]
23902 declarator constant-initializer [opt]
23903 identifier [opt] : constant-expression
23904
23905 GNU Extensions:
23906
23907 member-declaration:
23908 __extension__ member-declaration
23909
23910 member-declarator:
23911 declarator attributes [opt] pure-specifier [opt]
23912 declarator attributes [opt] constant-initializer [opt]
23913 identifier [opt] attributes [opt] : constant-expression
23914
23915 C++0x Extensions:
23916
23917 member-declaration:
23918 static_assert-declaration */
23919
23920 static void
23921 cp_parser_member_declaration (cp_parser* parser)
23922 {
23923 cp_decl_specifier_seq decl_specifiers;
23924 tree prefix_attributes;
23925 tree decl;
23926 int declares_class_or_enum;
23927 bool friend_p;
23928 cp_token *token = NULL;
23929 cp_token *decl_spec_token_start = NULL;
23930 cp_token *initializer_token_start = NULL;
23931 int saved_pedantic;
23932 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23933
23934 /* Check for the `__extension__' keyword. */
23935 if (cp_parser_extension_opt (parser, &saved_pedantic))
23936 {
23937 /* Recurse. */
23938 cp_parser_member_declaration (parser);
23939 /* Restore the old value of the PEDANTIC flag. */
23940 pedantic = saved_pedantic;
23941
23942 return;
23943 }
23944
23945 /* Check for a template-declaration. */
23946 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23947 {
23948 /* An explicit specialization here is an error condition, and we
23949 expect the specialization handler to detect and report this. */
23950 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23951 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23952 cp_parser_explicit_specialization (parser);
23953 else
23954 cp_parser_template_declaration (parser, /*member_p=*/true);
23955
23956 return;
23957 }
23958 /* Check for a template introduction. */
23959 else if (cp_parser_template_declaration_after_export (parser, true))
23960 return;
23961
23962 /* Check for a using-declaration. */
23963 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23964 {
23965 if (cxx_dialect < cxx11)
23966 {
23967 /* Parse the using-declaration. */
23968 cp_parser_using_declaration (parser,
23969 /*access_declaration_p=*/false);
23970 return;
23971 }
23972 else
23973 {
23974 tree decl;
23975 bool alias_decl_expected;
23976 cp_parser_parse_tentatively (parser);
23977 decl = cp_parser_alias_declaration (parser);
23978 /* Note that if we actually see the '=' token after the
23979 identifier, cp_parser_alias_declaration commits the
23980 tentative parse. In that case, we really expect an
23981 alias-declaration. Otherwise, we expect a using
23982 declaration. */
23983 alias_decl_expected =
23984 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23985 cp_parser_parse_definitely (parser);
23986
23987 if (alias_decl_expected)
23988 finish_member_declaration (decl);
23989 else
23990 cp_parser_using_declaration (parser,
23991 /*access_declaration_p=*/false);
23992 return;
23993 }
23994 }
23995
23996 /* Check for @defs. */
23997 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23998 {
23999 tree ivar, member;
24000 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24001 ivar = ivar_chains;
24002 while (ivar)
24003 {
24004 member = ivar;
24005 ivar = TREE_CHAIN (member);
24006 TREE_CHAIN (member) = NULL_TREE;
24007 finish_member_declaration (member);
24008 }
24009 return;
24010 }
24011
24012 /* If the next token is `static_assert' we have a static assertion. */
24013 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24014 {
24015 cp_parser_static_assert (parser, /*member_p=*/true);
24016 return;
24017 }
24018
24019 parser->colon_corrects_to_scope_p = false;
24020
24021 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24022 goto out;
24023
24024 /* Parse the decl-specifier-seq. */
24025 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24026 cp_parser_decl_specifier_seq (parser,
24027 CP_PARSER_FLAGS_OPTIONAL,
24028 &decl_specifiers,
24029 &declares_class_or_enum);
24030 /* Check for an invalid type-name. */
24031 if (!decl_specifiers.any_type_specifiers_p
24032 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24033 goto out;
24034 /* If there is no declarator, then the decl-specifier-seq should
24035 specify a type. */
24036 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24037 {
24038 /* If there was no decl-specifier-seq, and the next token is a
24039 `;', then we have something like:
24040
24041 struct S { ; };
24042
24043 [class.mem]
24044
24045 Each member-declaration shall declare at least one member
24046 name of the class. */
24047 if (!decl_specifiers.any_specifiers_p)
24048 {
24049 cp_token *token = cp_lexer_peek_token (parser->lexer);
24050 if (!in_system_header_at (token->location))
24051 {
24052 gcc_rich_location richloc (token->location);
24053 richloc.add_fixit_remove ();
24054 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24055 }
24056 }
24057 else
24058 {
24059 tree type;
24060
24061 /* See if this declaration is a friend. */
24062 friend_p = cp_parser_friend_p (&decl_specifiers);
24063 /* If there were decl-specifiers, check to see if there was
24064 a class-declaration. */
24065 type = check_tag_decl (&decl_specifiers,
24066 /*explicit_type_instantiation_p=*/false);
24067 /* Nested classes have already been added to the class, but
24068 a `friend' needs to be explicitly registered. */
24069 if (friend_p)
24070 {
24071 /* If the `friend' keyword was present, the friend must
24072 be introduced with a class-key. */
24073 if (!declares_class_or_enum && cxx_dialect < cxx11)
24074 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24075 "in C++03 a class-key must be used "
24076 "when declaring a friend");
24077 /* In this case:
24078
24079 template <typename T> struct A {
24080 friend struct A<T>::B;
24081 };
24082
24083 A<T>::B will be represented by a TYPENAME_TYPE, and
24084 therefore not recognized by check_tag_decl. */
24085 if (!type)
24086 {
24087 type = decl_specifiers.type;
24088 if (type && TREE_CODE (type) == TYPE_DECL)
24089 type = TREE_TYPE (type);
24090 }
24091 if (!type || !TYPE_P (type))
24092 error_at (decl_spec_token_start->location,
24093 "friend declaration does not name a class or "
24094 "function");
24095 else
24096 make_friend_class (current_class_type, type,
24097 /*complain=*/true);
24098 }
24099 /* If there is no TYPE, an error message will already have
24100 been issued. */
24101 else if (!type || type == error_mark_node)
24102 ;
24103 /* An anonymous aggregate has to be handled specially; such
24104 a declaration really declares a data member (with a
24105 particular type), as opposed to a nested class. */
24106 else if (ANON_AGGR_TYPE_P (type))
24107 {
24108 /* C++11 9.5/6. */
24109 if (decl_specifiers.storage_class != sc_none)
24110 error_at (decl_spec_token_start->location,
24111 "a storage class on an anonymous aggregate "
24112 "in class scope is not allowed");
24113
24114 /* Remove constructors and such from TYPE, now that we
24115 know it is an anonymous aggregate. */
24116 fixup_anonymous_aggr (type);
24117 /* And make the corresponding data member. */
24118 decl = build_decl (decl_spec_token_start->location,
24119 FIELD_DECL, NULL_TREE, type);
24120 /* Add it to the class. */
24121 finish_member_declaration (decl);
24122 }
24123 else
24124 cp_parser_check_access_in_redeclaration
24125 (TYPE_NAME (type),
24126 decl_spec_token_start->location);
24127 }
24128 }
24129 else
24130 {
24131 bool assume_semicolon = false;
24132
24133 /* Clear attributes from the decl_specifiers but keep them
24134 around as prefix attributes that apply them to the entity
24135 being declared. */
24136 prefix_attributes = decl_specifiers.attributes;
24137 decl_specifiers.attributes = NULL_TREE;
24138
24139 /* See if these declarations will be friends. */
24140 friend_p = cp_parser_friend_p (&decl_specifiers);
24141
24142 /* Keep going until we hit the `;' at the end of the
24143 declaration. */
24144 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24145 {
24146 tree attributes = NULL_TREE;
24147 tree first_attribute;
24148 tree initializer;
24149 bool named_bitfld = false;
24150
24151 /* Peek at the next token. */
24152 token = cp_lexer_peek_token (parser->lexer);
24153
24154 /* The following code wants to know early if it is a bit-field
24155 or some other declaration. Attributes can appear before
24156 the `:' token. Skip over them without consuming any tokens
24157 to peek if they are followed by `:'. */
24158 if (cp_next_tokens_can_be_attribute_p (parser)
24159 || (token->type == CPP_NAME
24160 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24161 && (named_bitfld = true)))
24162 {
24163 size_t n
24164 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24165 token = cp_lexer_peek_nth_token (parser->lexer, n);
24166 }
24167
24168 /* Check for a bitfield declaration. */
24169 if (token->type == CPP_COLON
24170 || (token->type == CPP_NAME
24171 && token == cp_lexer_peek_token (parser->lexer)
24172 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24173 && (named_bitfld = true)))
24174 {
24175 tree identifier;
24176 tree width;
24177 tree late_attributes = NULL_TREE;
24178 location_t id_location
24179 = cp_lexer_peek_token (parser->lexer)->location;
24180
24181 if (named_bitfld)
24182 identifier = cp_parser_identifier (parser);
24183 else
24184 identifier = NULL_TREE;
24185
24186 /* Look for attributes that apply to the bitfield. */
24187 attributes = cp_parser_attributes_opt (parser);
24188
24189 /* Consume the `:' token. */
24190 cp_lexer_consume_token (parser->lexer);
24191
24192 /* Get the width of the bitfield. */
24193 width = cp_parser_constant_expression (parser, false, NULL,
24194 cxx_dialect >= cxx11);
24195
24196 /* In C++2A and as extension for C++11 and above we allow
24197 default member initializers for bit-fields. */
24198 initializer = NULL_TREE;
24199 if (cxx_dialect >= cxx11
24200 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24201 || cp_lexer_next_token_is (parser->lexer,
24202 CPP_OPEN_BRACE)))
24203 {
24204 location_t loc
24205 = cp_lexer_peek_token (parser->lexer)->location;
24206 if (cxx_dialect < cxx2a
24207 && !in_system_header_at (loc)
24208 && identifier != NULL_TREE)
24209 pedwarn (loc, 0,
24210 "default member initializers for bit-fields "
24211 "only available with -std=c++2a or "
24212 "-std=gnu++2a");
24213
24214 initializer = cp_parser_save_nsdmi (parser);
24215 if (identifier == NULL_TREE)
24216 {
24217 error_at (loc, "default member initializer for "
24218 "unnamed bit-field");
24219 initializer = NULL_TREE;
24220 }
24221 }
24222 else
24223 {
24224 /* Look for attributes that apply to the bitfield after
24225 the `:' token and width. This is where GCC used to
24226 parse attributes in the past, pedwarn if there is
24227 a std attribute. */
24228 if (cp_next_tokens_can_be_std_attribute_p (parser))
24229 pedwarn (input_location, OPT_Wpedantic,
24230 "ISO C++ allows bit-field attributes only "
24231 "before the %<:%> token");
24232
24233 late_attributes = cp_parser_attributes_opt (parser);
24234 }
24235
24236 attributes = attr_chainon (attributes, late_attributes);
24237
24238 /* Remember which attributes are prefix attributes and
24239 which are not. */
24240 first_attribute = attributes;
24241 /* Combine the attributes. */
24242 attributes = attr_chainon (prefix_attributes, attributes);
24243
24244 /* Create the bitfield declaration. */
24245 decl = grokbitfield (identifier
24246 ? make_id_declarator (NULL_TREE,
24247 identifier,
24248 sfk_none,
24249 id_location)
24250 : NULL,
24251 &decl_specifiers,
24252 width, initializer,
24253 attributes);
24254 }
24255 else
24256 {
24257 cp_declarator *declarator;
24258 tree asm_specification;
24259 int ctor_dtor_or_conv_p;
24260
24261 /* Parse the declarator. */
24262 declarator
24263 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24264 &ctor_dtor_or_conv_p,
24265 /*parenthesized_p=*/NULL,
24266 /*member_p=*/true,
24267 friend_p);
24268
24269 /* If something went wrong parsing the declarator, make sure
24270 that we at least consume some tokens. */
24271 if (declarator == cp_error_declarator)
24272 {
24273 /* Skip to the end of the statement. */
24274 cp_parser_skip_to_end_of_statement (parser);
24275 /* If the next token is not a semicolon, that is
24276 probably because we just skipped over the body of
24277 a function. So, we consume a semicolon if
24278 present, but do not issue an error message if it
24279 is not present. */
24280 if (cp_lexer_next_token_is (parser->lexer,
24281 CPP_SEMICOLON))
24282 cp_lexer_consume_token (parser->lexer);
24283 goto out;
24284 }
24285
24286 if (declares_class_or_enum & 2)
24287 cp_parser_check_for_definition_in_return_type
24288 (declarator, decl_specifiers.type,
24289 decl_specifiers.locations[ds_type_spec]);
24290
24291 /* Look for an asm-specification. */
24292 asm_specification = cp_parser_asm_specification_opt (parser);
24293 /* Look for attributes that apply to the declaration. */
24294 attributes = cp_parser_attributes_opt (parser);
24295 /* Remember which attributes are prefix attributes and
24296 which are not. */
24297 first_attribute = attributes;
24298 /* Combine the attributes. */
24299 attributes = attr_chainon (prefix_attributes, attributes);
24300
24301 /* If it's an `=', then we have a constant-initializer or a
24302 pure-specifier. It is not correct to parse the
24303 initializer before registering the member declaration
24304 since the member declaration should be in scope while
24305 its initializer is processed. However, the rest of the
24306 front end does not yet provide an interface that allows
24307 us to handle this correctly. */
24308 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24309 {
24310 /* In [class.mem]:
24311
24312 A pure-specifier shall be used only in the declaration of
24313 a virtual function.
24314
24315 A member-declarator can contain a constant-initializer
24316 only if it declares a static member of integral or
24317 enumeration type.
24318
24319 Therefore, if the DECLARATOR is for a function, we look
24320 for a pure-specifier; otherwise, we look for a
24321 constant-initializer. When we call `grokfield', it will
24322 perform more stringent semantics checks. */
24323 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24324 if (function_declarator_p (declarator)
24325 || (decl_specifiers.type
24326 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24327 && declarator->kind == cdk_id
24328 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24329 == FUNCTION_TYPE)))
24330 initializer = cp_parser_pure_specifier (parser);
24331 else if (decl_specifiers.storage_class != sc_static)
24332 initializer = cp_parser_save_nsdmi (parser);
24333 else if (cxx_dialect >= cxx11)
24334 {
24335 bool nonconst;
24336 /* Don't require a constant rvalue in C++11, since we
24337 might want a reference constant. We'll enforce
24338 constancy later. */
24339 cp_lexer_consume_token (parser->lexer);
24340 /* Parse the initializer. */
24341 initializer = cp_parser_initializer_clause (parser,
24342 &nonconst);
24343 }
24344 else
24345 /* Parse the initializer. */
24346 initializer = cp_parser_constant_initializer (parser);
24347 }
24348 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24349 && !function_declarator_p (declarator))
24350 {
24351 bool x;
24352 if (decl_specifiers.storage_class != sc_static)
24353 initializer = cp_parser_save_nsdmi (parser);
24354 else
24355 initializer = cp_parser_initializer (parser, &x, &x);
24356 }
24357 /* Otherwise, there is no initializer. */
24358 else
24359 initializer = NULL_TREE;
24360
24361 /* See if we are probably looking at a function
24362 definition. We are certainly not looking at a
24363 member-declarator. Calling `grokfield' has
24364 side-effects, so we must not do it unless we are sure
24365 that we are looking at a member-declarator. */
24366 if (cp_parser_token_starts_function_definition_p
24367 (cp_lexer_peek_token (parser->lexer)))
24368 {
24369 /* The grammar does not allow a pure-specifier to be
24370 used when a member function is defined. (It is
24371 possible that this fact is an oversight in the
24372 standard, since a pure function may be defined
24373 outside of the class-specifier. */
24374 if (initializer && initializer_token_start)
24375 error_at (initializer_token_start->location,
24376 "pure-specifier on function-definition");
24377 decl = cp_parser_save_member_function_body (parser,
24378 &decl_specifiers,
24379 declarator,
24380 attributes);
24381 if (parser->fully_implicit_function_template_p)
24382 decl = finish_fully_implicit_template (parser, decl);
24383 /* If the member was not a friend, declare it here. */
24384 if (!friend_p)
24385 finish_member_declaration (decl);
24386 /* Peek at the next token. */
24387 token = cp_lexer_peek_token (parser->lexer);
24388 /* If the next token is a semicolon, consume it. */
24389 if (token->type == CPP_SEMICOLON)
24390 {
24391 location_t semicolon_loc
24392 = cp_lexer_consume_token (parser->lexer)->location;
24393 gcc_rich_location richloc (semicolon_loc);
24394 richloc.add_fixit_remove ();
24395 warning_at (&richloc, OPT_Wextra_semi,
24396 "extra %<;%> after in-class "
24397 "function definition");
24398 }
24399 goto out;
24400 }
24401 else
24402 if (declarator->kind == cdk_function)
24403 declarator->id_loc = token->location;
24404 /* Create the declaration. */
24405 decl = grokfield (declarator, &decl_specifiers,
24406 initializer, /*init_const_expr_p=*/true,
24407 asm_specification, attributes);
24408 if (parser->fully_implicit_function_template_p)
24409 {
24410 if (friend_p)
24411 finish_fully_implicit_template (parser, 0);
24412 else
24413 decl = finish_fully_implicit_template (parser, decl);
24414 }
24415 }
24416
24417 cp_finalize_omp_declare_simd (parser, decl);
24418 cp_finalize_oacc_routine (parser, decl, false);
24419
24420 /* Reset PREFIX_ATTRIBUTES. */
24421 if (attributes != error_mark_node)
24422 {
24423 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24424 attributes = TREE_CHAIN (attributes);
24425 if (attributes)
24426 TREE_CHAIN (attributes) = NULL_TREE;
24427 }
24428
24429 /* If there is any qualification still in effect, clear it
24430 now; we will be starting fresh with the next declarator. */
24431 parser->scope = NULL_TREE;
24432 parser->qualifying_scope = NULL_TREE;
24433 parser->object_scope = NULL_TREE;
24434 /* If it's a `,', then there are more declarators. */
24435 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24436 {
24437 cp_lexer_consume_token (parser->lexer);
24438 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24439 {
24440 cp_token *token = cp_lexer_previous_token (parser->lexer);
24441 gcc_rich_location richloc (token->location);
24442 richloc.add_fixit_remove ();
24443 error_at (&richloc, "stray %<,%> at end of "
24444 "member declaration");
24445 }
24446 }
24447 /* If the next token isn't a `;', then we have a parse error. */
24448 else if (cp_lexer_next_token_is_not (parser->lexer,
24449 CPP_SEMICOLON))
24450 {
24451 /* The next token might be a ways away from where the
24452 actual semicolon is missing. Find the previous token
24453 and use that for our error position. */
24454 cp_token *token = cp_lexer_previous_token (parser->lexer);
24455 gcc_rich_location richloc (token->location);
24456 richloc.add_fixit_insert_after (";");
24457 error_at (&richloc, "expected %<;%> at end of "
24458 "member declaration");
24459
24460 /* Assume that the user meant to provide a semicolon. If
24461 we were to cp_parser_skip_to_end_of_statement, we might
24462 skip to a semicolon inside a member function definition
24463 and issue nonsensical error messages. */
24464 assume_semicolon = true;
24465 }
24466
24467 if (decl)
24468 {
24469 /* Add DECL to the list of members. */
24470 if (!friend_p
24471 /* Explicitly include, eg, NSDMIs, for better error
24472 recovery (c++/58650). */
24473 || !DECL_DECLARES_FUNCTION_P (decl))
24474 finish_member_declaration (decl);
24475
24476 if (TREE_CODE (decl) == FUNCTION_DECL)
24477 cp_parser_save_default_args (parser, decl);
24478 else if (TREE_CODE (decl) == FIELD_DECL
24479 && DECL_INITIAL (decl))
24480 /* Add DECL to the queue of NSDMI to be parsed later. */
24481 vec_safe_push (unparsed_nsdmis, decl);
24482 }
24483
24484 if (assume_semicolon)
24485 goto out;
24486 }
24487 }
24488
24489 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24490 out:
24491 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24492 }
24493
24494 /* Parse a pure-specifier.
24495
24496 pure-specifier:
24497 = 0
24498
24499 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24500 Otherwise, ERROR_MARK_NODE is returned. */
24501
24502 static tree
24503 cp_parser_pure_specifier (cp_parser* parser)
24504 {
24505 cp_token *token;
24506
24507 /* Look for the `=' token. */
24508 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24509 return error_mark_node;
24510 /* Look for the `0' token. */
24511 token = cp_lexer_peek_token (parser->lexer);
24512
24513 if (token->type == CPP_EOF
24514 || token->type == CPP_PRAGMA_EOL)
24515 return error_mark_node;
24516
24517 cp_lexer_consume_token (parser->lexer);
24518
24519 /* Accept = default or = delete in c++0x mode. */
24520 if (token->keyword == RID_DEFAULT
24521 || token->keyword == RID_DELETE)
24522 {
24523 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24524 return token->u.value;
24525 }
24526
24527 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24528 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24529 {
24530 cp_parser_error (parser,
24531 "invalid pure specifier (only %<= 0%> is allowed)");
24532 cp_parser_skip_to_end_of_statement (parser);
24533 return error_mark_node;
24534 }
24535 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24536 {
24537 error_at (token->location, "templates may not be %<virtual%>");
24538 return error_mark_node;
24539 }
24540
24541 return integer_zero_node;
24542 }
24543
24544 /* Parse a constant-initializer.
24545
24546 constant-initializer:
24547 = constant-expression
24548
24549 Returns a representation of the constant-expression. */
24550
24551 static tree
24552 cp_parser_constant_initializer (cp_parser* parser)
24553 {
24554 /* Look for the `=' token. */
24555 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24556 return error_mark_node;
24557
24558 /* It is invalid to write:
24559
24560 struct S { static const int i = { 7 }; };
24561
24562 */
24563 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24564 {
24565 cp_parser_error (parser,
24566 "a brace-enclosed initializer is not allowed here");
24567 /* Consume the opening brace. */
24568 matching_braces braces;
24569 braces.consume_open (parser);
24570 /* Skip the initializer. */
24571 cp_parser_skip_to_closing_brace (parser);
24572 /* Look for the trailing `}'. */
24573 braces.require_close (parser);
24574
24575 return error_mark_node;
24576 }
24577
24578 return cp_parser_constant_expression (parser);
24579 }
24580
24581 /* Derived classes [gram.class.derived] */
24582
24583 /* Parse a base-clause.
24584
24585 base-clause:
24586 : base-specifier-list
24587
24588 base-specifier-list:
24589 base-specifier ... [opt]
24590 base-specifier-list , base-specifier ... [opt]
24591
24592 Returns a TREE_LIST representing the base-classes, in the order in
24593 which they were declared. The representation of each node is as
24594 described by cp_parser_base_specifier.
24595
24596 In the case that no bases are specified, this function will return
24597 NULL_TREE, not ERROR_MARK_NODE. */
24598
24599 static tree
24600 cp_parser_base_clause (cp_parser* parser)
24601 {
24602 tree bases = NULL_TREE;
24603
24604 /* Look for the `:' that begins the list. */
24605 cp_parser_require (parser, CPP_COLON, RT_COLON);
24606
24607 /* Scan the base-specifier-list. */
24608 while (true)
24609 {
24610 cp_token *token;
24611 tree base;
24612 bool pack_expansion_p = false;
24613
24614 /* Look for the base-specifier. */
24615 base = cp_parser_base_specifier (parser);
24616 /* Look for the (optional) ellipsis. */
24617 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24618 {
24619 /* Consume the `...'. */
24620 cp_lexer_consume_token (parser->lexer);
24621
24622 pack_expansion_p = true;
24623 }
24624
24625 /* Add BASE to the front of the list. */
24626 if (base && base != error_mark_node)
24627 {
24628 if (pack_expansion_p)
24629 /* Make this a pack expansion type. */
24630 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24631
24632 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24633 {
24634 TREE_CHAIN (base) = bases;
24635 bases = base;
24636 }
24637 }
24638 /* Peek at the next token. */
24639 token = cp_lexer_peek_token (parser->lexer);
24640 /* If it's not a comma, then the list is complete. */
24641 if (token->type != CPP_COMMA)
24642 break;
24643 /* Consume the `,'. */
24644 cp_lexer_consume_token (parser->lexer);
24645 }
24646
24647 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24648 base class had a qualified name. However, the next name that
24649 appears is certainly not qualified. */
24650 parser->scope = NULL_TREE;
24651 parser->qualifying_scope = NULL_TREE;
24652 parser->object_scope = NULL_TREE;
24653
24654 return nreverse (bases);
24655 }
24656
24657 /* Parse a base-specifier.
24658
24659 base-specifier:
24660 :: [opt] nested-name-specifier [opt] class-name
24661 virtual access-specifier [opt] :: [opt] nested-name-specifier
24662 [opt] class-name
24663 access-specifier virtual [opt] :: [opt] nested-name-specifier
24664 [opt] class-name
24665
24666 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24667 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24668 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24669 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24670
24671 static tree
24672 cp_parser_base_specifier (cp_parser* parser)
24673 {
24674 cp_token *token;
24675 bool done = false;
24676 bool virtual_p = false;
24677 bool duplicate_virtual_error_issued_p = false;
24678 bool duplicate_access_error_issued_p = false;
24679 bool class_scope_p, template_p;
24680 tree access = access_default_node;
24681 tree type;
24682
24683 /* Process the optional `virtual' and `access-specifier'. */
24684 while (!done)
24685 {
24686 /* Peek at the next token. */
24687 token = cp_lexer_peek_token (parser->lexer);
24688 /* Process `virtual'. */
24689 switch (token->keyword)
24690 {
24691 case RID_VIRTUAL:
24692 /* If `virtual' appears more than once, issue an error. */
24693 if (virtual_p && !duplicate_virtual_error_issued_p)
24694 {
24695 cp_parser_error (parser,
24696 "%<virtual%> specified more than once in base-specifier");
24697 duplicate_virtual_error_issued_p = true;
24698 }
24699
24700 virtual_p = true;
24701
24702 /* Consume the `virtual' token. */
24703 cp_lexer_consume_token (parser->lexer);
24704
24705 break;
24706
24707 case RID_PUBLIC:
24708 case RID_PROTECTED:
24709 case RID_PRIVATE:
24710 /* If more than one access specifier appears, issue an
24711 error. */
24712 if (access != access_default_node
24713 && !duplicate_access_error_issued_p)
24714 {
24715 cp_parser_error (parser,
24716 "more than one access specifier in base-specifier");
24717 duplicate_access_error_issued_p = true;
24718 }
24719
24720 access = ridpointers[(int) token->keyword];
24721
24722 /* Consume the access-specifier. */
24723 cp_lexer_consume_token (parser->lexer);
24724
24725 break;
24726
24727 default:
24728 done = true;
24729 break;
24730 }
24731 }
24732 /* It is not uncommon to see programs mechanically, erroneously, use
24733 the 'typename' keyword to denote (dependent) qualified types
24734 as base classes. */
24735 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24736 {
24737 token = cp_lexer_peek_token (parser->lexer);
24738 if (!processing_template_decl)
24739 error_at (token->location,
24740 "keyword %<typename%> not allowed outside of templates");
24741 else
24742 error_at (token->location,
24743 "keyword %<typename%> not allowed in this context "
24744 "(the base class is implicitly a type)");
24745 cp_lexer_consume_token (parser->lexer);
24746 }
24747
24748 /* Look for the optional `::' operator. */
24749 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24750 /* Look for the nested-name-specifier. The simplest way to
24751 implement:
24752
24753 [temp.res]
24754
24755 The keyword `typename' is not permitted in a base-specifier or
24756 mem-initializer; in these contexts a qualified name that
24757 depends on a template-parameter is implicitly assumed to be a
24758 type name.
24759
24760 is to pretend that we have seen the `typename' keyword at this
24761 point. */
24762 cp_parser_nested_name_specifier_opt (parser,
24763 /*typename_keyword_p=*/true,
24764 /*check_dependency_p=*/true,
24765 /*type_p=*/true,
24766 /*is_declaration=*/true);
24767 /* If the base class is given by a qualified name, assume that names
24768 we see are type names or templates, as appropriate. */
24769 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24770 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24771
24772 if (!parser->scope
24773 && cp_lexer_next_token_is_decltype (parser->lexer))
24774 /* DR 950 allows decltype as a base-specifier. */
24775 type = cp_parser_decltype (parser);
24776 else
24777 {
24778 /* Otherwise, look for the class-name. */
24779 type = cp_parser_class_name (parser,
24780 class_scope_p,
24781 template_p,
24782 typename_type,
24783 /*check_dependency_p=*/true,
24784 /*class_head_p=*/false,
24785 /*is_declaration=*/true);
24786 type = TREE_TYPE (type);
24787 }
24788
24789 if (type == error_mark_node)
24790 return error_mark_node;
24791
24792 return finish_base_specifier (type, access, virtual_p);
24793 }
24794
24795 /* Exception handling [gram.exception] */
24796
24797 /* Parse an (optional) noexcept-specification.
24798
24799 noexcept-specification:
24800 noexcept ( constant-expression ) [opt]
24801
24802 If no noexcept-specification is present, returns NULL_TREE.
24803 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24804 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24805 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24806 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24807 in which case a boolean condition is returned instead. */
24808
24809 static tree
24810 cp_parser_noexcept_specification_opt (cp_parser* parser,
24811 bool require_constexpr,
24812 bool* consumed_expr,
24813 bool return_cond)
24814 {
24815 cp_token *token;
24816 const char *saved_message;
24817
24818 /* Peek at the next token. */
24819 token = cp_lexer_peek_token (parser->lexer);
24820
24821 /* Is it a noexcept-specification? */
24822 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24823 {
24824 tree expr;
24825 cp_lexer_consume_token (parser->lexer);
24826
24827 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24828 {
24829 matching_parens parens;
24830 parens.consume_open (parser);
24831
24832 tree save_ccp = current_class_ptr;
24833 tree save_ccr = current_class_ref;
24834
24835 if (current_class_type)
24836 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
24837
24838 if (require_constexpr)
24839 {
24840 /* Types may not be defined in an exception-specification. */
24841 saved_message = parser->type_definition_forbidden_message;
24842 parser->type_definition_forbidden_message
24843 = G_("types may not be defined in an exception-specification");
24844
24845 expr = cp_parser_constant_expression (parser);
24846
24847 /* Restore the saved message. */
24848 parser->type_definition_forbidden_message = saved_message;
24849 }
24850 else
24851 {
24852 expr = cp_parser_expression (parser);
24853 *consumed_expr = true;
24854 }
24855
24856 parens.require_close (parser);
24857
24858 current_class_ptr = save_ccp;
24859 current_class_ref = save_ccr;
24860 }
24861 else
24862 {
24863 expr = boolean_true_node;
24864 if (!require_constexpr)
24865 *consumed_expr = false;
24866 }
24867
24868 /* We cannot build a noexcept-spec right away because this will check
24869 that expr is a constexpr. */
24870 if (!return_cond)
24871 return build_noexcept_spec (expr, tf_warning_or_error);
24872 else
24873 return expr;
24874 }
24875 else
24876 return NULL_TREE;
24877 }
24878
24879 /* Parse an (optional) exception-specification.
24880
24881 exception-specification:
24882 throw ( type-id-list [opt] )
24883
24884 Returns a TREE_LIST representing the exception-specification. The
24885 TREE_VALUE of each node is a type. */
24886
24887 static tree
24888 cp_parser_exception_specification_opt (cp_parser* parser)
24889 {
24890 cp_token *token;
24891 tree type_id_list;
24892 const char *saved_message;
24893
24894 /* Peek at the next token. */
24895 token = cp_lexer_peek_token (parser->lexer);
24896
24897 /* Is it a noexcept-specification? */
24898 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24899 false);
24900 if (type_id_list != NULL_TREE)
24901 return type_id_list;
24902
24903 /* If it's not `throw', then there's no exception-specification. */
24904 if (!cp_parser_is_keyword (token, RID_THROW))
24905 return NULL_TREE;
24906
24907 location_t loc = token->location;
24908
24909 /* Consume the `throw'. */
24910 cp_lexer_consume_token (parser->lexer);
24911
24912 /* Look for the `('. */
24913 matching_parens parens;
24914 parens.require_open (parser);
24915
24916 /* Peek at the next token. */
24917 token = cp_lexer_peek_token (parser->lexer);
24918 /* If it's not a `)', then there is a type-id-list. */
24919 if (token->type != CPP_CLOSE_PAREN)
24920 {
24921 /* Types may not be defined in an exception-specification. */
24922 saved_message = parser->type_definition_forbidden_message;
24923 parser->type_definition_forbidden_message
24924 = G_("types may not be defined in an exception-specification");
24925 /* Parse the type-id-list. */
24926 type_id_list = cp_parser_type_id_list (parser);
24927 /* Restore the saved message. */
24928 parser->type_definition_forbidden_message = saved_message;
24929
24930 if (cxx_dialect >= cxx17)
24931 {
24932 error_at (loc, "ISO C++17 does not allow dynamic exception "
24933 "specifications");
24934 type_id_list = NULL_TREE;
24935 }
24936 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24937 warning_at (loc, OPT_Wdeprecated,
24938 "dynamic exception specifications are deprecated in "
24939 "C++11");
24940 }
24941 /* In C++17, throw() is equivalent to noexcept (true). throw()
24942 is deprecated in C++11 and above as well, but is still widely used,
24943 so don't warn about it yet. */
24944 else if (cxx_dialect >= cxx17)
24945 type_id_list = noexcept_true_spec;
24946 else
24947 type_id_list = empty_except_spec;
24948
24949 /* Look for the `)'. */
24950 parens.require_close (parser);
24951
24952 return type_id_list;
24953 }
24954
24955 /* Parse an (optional) type-id-list.
24956
24957 type-id-list:
24958 type-id ... [opt]
24959 type-id-list , type-id ... [opt]
24960
24961 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24962 in the order that the types were presented. */
24963
24964 static tree
24965 cp_parser_type_id_list (cp_parser* parser)
24966 {
24967 tree types = NULL_TREE;
24968
24969 while (true)
24970 {
24971 cp_token *token;
24972 tree type;
24973
24974 token = cp_lexer_peek_token (parser->lexer);
24975
24976 /* Get the next type-id. */
24977 type = cp_parser_type_id (parser);
24978 /* Check for invalid 'auto'. */
24979 if (flag_concepts && type_uses_auto (type))
24980 {
24981 error_at (token->location,
24982 "invalid use of %<auto%> in exception-specification");
24983 type = error_mark_node;
24984 }
24985 /* Parse the optional ellipsis. */
24986 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24987 {
24988 /* Consume the `...'. */
24989 cp_lexer_consume_token (parser->lexer);
24990
24991 /* Turn the type into a pack expansion expression. */
24992 type = make_pack_expansion (type);
24993 }
24994 /* Add it to the list. */
24995 types = add_exception_specifier (types, type, /*complain=*/1);
24996 /* Peek at the next token. */
24997 token = cp_lexer_peek_token (parser->lexer);
24998 /* If it is not a `,', we are done. */
24999 if (token->type != CPP_COMMA)
25000 break;
25001 /* Consume the `,'. */
25002 cp_lexer_consume_token (parser->lexer);
25003 }
25004
25005 return nreverse (types);
25006 }
25007
25008 /* Parse a try-block.
25009
25010 try-block:
25011 try compound-statement handler-seq */
25012
25013 static tree
25014 cp_parser_try_block (cp_parser* parser)
25015 {
25016 tree try_block;
25017
25018 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25019 if (parser->in_function_body
25020 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
25021 error ("%<try%> in %<constexpr%> function");
25022
25023 try_block = begin_try_block ();
25024 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25025 finish_try_block (try_block);
25026 cp_parser_handler_seq (parser);
25027 finish_handler_sequence (try_block);
25028
25029 return try_block;
25030 }
25031
25032 /* Parse a function-try-block.
25033
25034 function-try-block:
25035 try ctor-initializer [opt] function-body handler-seq */
25036
25037 static void
25038 cp_parser_function_try_block (cp_parser* parser)
25039 {
25040 tree compound_stmt;
25041 tree try_block;
25042
25043 /* Look for the `try' keyword. */
25044 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25045 return;
25046 /* Let the rest of the front end know where we are. */
25047 try_block = begin_function_try_block (&compound_stmt);
25048 /* Parse the function-body. */
25049 cp_parser_ctor_initializer_opt_and_function_body
25050 (parser, /*in_function_try_block=*/true);
25051 /* We're done with the `try' part. */
25052 finish_function_try_block (try_block);
25053 /* Parse the handlers. */
25054 cp_parser_handler_seq (parser);
25055 /* We're done with the handlers. */
25056 finish_function_handler_sequence (try_block, compound_stmt);
25057 }
25058
25059 /* Parse a handler-seq.
25060
25061 handler-seq:
25062 handler handler-seq [opt] */
25063
25064 static void
25065 cp_parser_handler_seq (cp_parser* parser)
25066 {
25067 while (true)
25068 {
25069 cp_token *token;
25070
25071 /* Parse the handler. */
25072 cp_parser_handler (parser);
25073 /* Peek at the next token. */
25074 token = cp_lexer_peek_token (parser->lexer);
25075 /* If it's not `catch' then there are no more handlers. */
25076 if (!cp_parser_is_keyword (token, RID_CATCH))
25077 break;
25078 }
25079 }
25080
25081 /* Parse a handler.
25082
25083 handler:
25084 catch ( exception-declaration ) compound-statement */
25085
25086 static void
25087 cp_parser_handler (cp_parser* parser)
25088 {
25089 tree handler;
25090 tree declaration;
25091
25092 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25093 handler = begin_handler ();
25094 matching_parens parens;
25095 parens.require_open (parser);
25096 declaration = cp_parser_exception_declaration (parser);
25097 finish_handler_parms (declaration, handler);
25098 parens.require_close (parser);
25099 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25100 finish_handler (handler);
25101 }
25102
25103 /* Parse an exception-declaration.
25104
25105 exception-declaration:
25106 type-specifier-seq declarator
25107 type-specifier-seq abstract-declarator
25108 type-specifier-seq
25109 ...
25110
25111 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25112 ellipsis variant is used. */
25113
25114 static tree
25115 cp_parser_exception_declaration (cp_parser* parser)
25116 {
25117 cp_decl_specifier_seq type_specifiers;
25118 cp_declarator *declarator;
25119 const char *saved_message;
25120
25121 /* If it's an ellipsis, it's easy to handle. */
25122 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25123 {
25124 /* Consume the `...' token. */
25125 cp_lexer_consume_token (parser->lexer);
25126 return NULL_TREE;
25127 }
25128
25129 /* Types may not be defined in exception-declarations. */
25130 saved_message = parser->type_definition_forbidden_message;
25131 parser->type_definition_forbidden_message
25132 = G_("types may not be defined in exception-declarations");
25133
25134 /* Parse the type-specifier-seq. */
25135 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
25136 /*is_trailing_return=*/false,
25137 &type_specifiers);
25138 /* If it's a `)', then there is no declarator. */
25139 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25140 declarator = NULL;
25141 else
25142 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25143 /*ctor_dtor_or_conv_p=*/NULL,
25144 /*parenthesized_p=*/NULL,
25145 /*member_p=*/false,
25146 /*friend_p=*/false);
25147
25148 /* Restore the saved message. */
25149 parser->type_definition_forbidden_message = saved_message;
25150
25151 if (!type_specifiers.any_specifiers_p)
25152 return error_mark_node;
25153
25154 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25155 }
25156
25157 /* Parse a throw-expression.
25158
25159 throw-expression:
25160 throw assignment-expression [opt]
25161
25162 Returns a THROW_EXPR representing the throw-expression. */
25163
25164 static tree
25165 cp_parser_throw_expression (cp_parser* parser)
25166 {
25167 tree expression;
25168 cp_token* token;
25169
25170 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25171 token = cp_lexer_peek_token (parser->lexer);
25172 /* Figure out whether or not there is an assignment-expression
25173 following the "throw" keyword. */
25174 if (token->type == CPP_COMMA
25175 || token->type == CPP_SEMICOLON
25176 || token->type == CPP_CLOSE_PAREN
25177 || token->type == CPP_CLOSE_SQUARE
25178 || token->type == CPP_CLOSE_BRACE
25179 || token->type == CPP_COLON)
25180 expression = NULL_TREE;
25181 else
25182 expression = cp_parser_assignment_expression (parser);
25183
25184 return build_throw (expression);
25185 }
25186
25187 /* GNU Extensions */
25188
25189 /* Parse an (optional) asm-specification.
25190
25191 asm-specification:
25192 asm ( string-literal )
25193
25194 If the asm-specification is present, returns a STRING_CST
25195 corresponding to the string-literal. Otherwise, returns
25196 NULL_TREE. */
25197
25198 static tree
25199 cp_parser_asm_specification_opt (cp_parser* parser)
25200 {
25201 cp_token *token;
25202 tree asm_specification;
25203
25204 /* Peek at the next token. */
25205 token = cp_lexer_peek_token (parser->lexer);
25206 /* If the next token isn't the `asm' keyword, then there's no
25207 asm-specification. */
25208 if (!cp_parser_is_keyword (token, RID_ASM))
25209 return NULL_TREE;
25210
25211 /* Consume the `asm' token. */
25212 cp_lexer_consume_token (parser->lexer);
25213 /* Look for the `('. */
25214 matching_parens parens;
25215 parens.require_open (parser);
25216
25217 /* Look for the string-literal. */
25218 asm_specification = cp_parser_string_literal (parser, false, false);
25219
25220 /* Look for the `)'. */
25221 parens.require_close (parser);
25222
25223 return asm_specification;
25224 }
25225
25226 /* Parse an asm-operand-list.
25227
25228 asm-operand-list:
25229 asm-operand
25230 asm-operand-list , asm-operand
25231
25232 asm-operand:
25233 string-literal ( expression )
25234 [ string-literal ] string-literal ( expression )
25235
25236 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25237 each node is the expression. The TREE_PURPOSE is itself a
25238 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25239 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25240 is a STRING_CST for the string literal before the parenthesis. Returns
25241 ERROR_MARK_NODE if any of the operands are invalid. */
25242
25243 static tree
25244 cp_parser_asm_operand_list (cp_parser* parser)
25245 {
25246 tree asm_operands = NULL_TREE;
25247 bool invalid_operands = false;
25248
25249 while (true)
25250 {
25251 tree string_literal;
25252 tree expression;
25253 tree name;
25254
25255 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25256 {
25257 /* Consume the `[' token. */
25258 cp_lexer_consume_token (parser->lexer);
25259 /* Read the operand name. */
25260 name = cp_parser_identifier (parser);
25261 if (name != error_mark_node)
25262 name = build_string (IDENTIFIER_LENGTH (name),
25263 IDENTIFIER_POINTER (name));
25264 /* Look for the closing `]'. */
25265 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25266 }
25267 else
25268 name = NULL_TREE;
25269 /* Look for the string-literal. */
25270 string_literal = cp_parser_string_literal (parser, false, false);
25271
25272 /* Look for the `('. */
25273 matching_parens parens;
25274 parens.require_open (parser);
25275 /* Parse the expression. */
25276 expression = cp_parser_expression (parser);
25277 /* Look for the `)'. */
25278 parens.require_close (parser);
25279
25280 if (name == error_mark_node
25281 || string_literal == error_mark_node
25282 || expression == error_mark_node)
25283 invalid_operands = true;
25284
25285 /* Add this operand to the list. */
25286 asm_operands = tree_cons (build_tree_list (name, string_literal),
25287 expression,
25288 asm_operands);
25289 /* If the next token is not a `,', there are no more
25290 operands. */
25291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25292 break;
25293 /* Consume the `,'. */
25294 cp_lexer_consume_token (parser->lexer);
25295 }
25296
25297 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25298 }
25299
25300 /* Parse an asm-clobber-list.
25301
25302 asm-clobber-list:
25303 string-literal
25304 asm-clobber-list , string-literal
25305
25306 Returns a TREE_LIST, indicating the clobbers in the order that they
25307 appeared. The TREE_VALUE of each node is a STRING_CST. */
25308
25309 static tree
25310 cp_parser_asm_clobber_list (cp_parser* parser)
25311 {
25312 tree clobbers = NULL_TREE;
25313
25314 while (true)
25315 {
25316 tree string_literal;
25317
25318 /* Look for the string literal. */
25319 string_literal = cp_parser_string_literal (parser, false, false);
25320 /* Add it to the list. */
25321 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25322 /* If the next token is not a `,', then the list is
25323 complete. */
25324 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25325 break;
25326 /* Consume the `,' token. */
25327 cp_lexer_consume_token (parser->lexer);
25328 }
25329
25330 return clobbers;
25331 }
25332
25333 /* Parse an asm-label-list.
25334
25335 asm-label-list:
25336 identifier
25337 asm-label-list , identifier
25338
25339 Returns a TREE_LIST, indicating the labels in the order that they
25340 appeared. The TREE_VALUE of each node is a label. */
25341
25342 static tree
25343 cp_parser_asm_label_list (cp_parser* parser)
25344 {
25345 tree labels = NULL_TREE;
25346
25347 while (true)
25348 {
25349 tree identifier, label, name;
25350
25351 /* Look for the identifier. */
25352 identifier = cp_parser_identifier (parser);
25353 if (!error_operand_p (identifier))
25354 {
25355 label = lookup_label (identifier);
25356 if (TREE_CODE (label) == LABEL_DECL)
25357 {
25358 TREE_USED (label) = 1;
25359 check_goto (label);
25360 name = build_string (IDENTIFIER_LENGTH (identifier),
25361 IDENTIFIER_POINTER (identifier));
25362 labels = tree_cons (name, label, labels);
25363 }
25364 }
25365 /* If the next token is not a `,', then the list is
25366 complete. */
25367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25368 break;
25369 /* Consume the `,' token. */
25370 cp_lexer_consume_token (parser->lexer);
25371 }
25372
25373 return nreverse (labels);
25374 }
25375
25376 /* Return TRUE iff the next tokens in the stream are possibly the
25377 beginning of a GNU extension attribute. */
25378
25379 static bool
25380 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25381 {
25382 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25383 }
25384
25385 /* Return TRUE iff the next tokens in the stream are possibly the
25386 beginning of a standard C++-11 attribute specifier. */
25387
25388 static bool
25389 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25390 {
25391 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25392 }
25393
25394 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25395 beginning of a standard C++-11 attribute specifier. */
25396
25397 static bool
25398 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25399 {
25400 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25401
25402 return (cxx_dialect >= cxx11
25403 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25404 || (token->type == CPP_OPEN_SQUARE
25405 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25406 && token->type == CPP_OPEN_SQUARE)));
25407 }
25408
25409 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25410 beginning of a GNU extension attribute. */
25411
25412 static bool
25413 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25414 {
25415 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25416
25417 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25418 }
25419
25420 /* Return true iff the next tokens can be the beginning of either a
25421 GNU attribute list, or a standard C++11 attribute sequence. */
25422
25423 static bool
25424 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25425 {
25426 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25427 || cp_next_tokens_can_be_std_attribute_p (parser));
25428 }
25429
25430 /* Return true iff the next Nth tokens can be the beginning of either
25431 a GNU attribute list, or a standard C++11 attribute sequence. */
25432
25433 static bool
25434 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25435 {
25436 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25437 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25438 }
25439
25440 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25441 of GNU attributes, or return NULL. */
25442
25443 static tree
25444 cp_parser_attributes_opt (cp_parser *parser)
25445 {
25446 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25447 return cp_parser_gnu_attributes_opt (parser);
25448 return cp_parser_std_attribute_spec_seq (parser);
25449 }
25450
25451 /* Parse an (optional) series of attributes.
25452
25453 attributes:
25454 attributes attribute
25455
25456 attribute:
25457 __attribute__ (( attribute-list [opt] ))
25458
25459 The return value is as for cp_parser_gnu_attribute_list. */
25460
25461 static tree
25462 cp_parser_gnu_attributes_opt (cp_parser* parser)
25463 {
25464 tree attributes = NULL_TREE;
25465
25466 temp_override<bool> cleanup
25467 (parser->auto_is_implicit_function_template_parm_p, false);
25468
25469 while (true)
25470 {
25471 cp_token *token;
25472 tree attribute_list;
25473 bool ok = true;
25474
25475 /* Peek at the next token. */
25476 token = cp_lexer_peek_token (parser->lexer);
25477 /* If it's not `__attribute__', then we're done. */
25478 if (token->keyword != RID_ATTRIBUTE)
25479 break;
25480
25481 /* Consume the `__attribute__' keyword. */
25482 cp_lexer_consume_token (parser->lexer);
25483 /* Look for the two `(' tokens. */
25484 matching_parens outer_parens;
25485 outer_parens.require_open (parser);
25486 matching_parens inner_parens;
25487 inner_parens.require_open (parser);
25488
25489 /* Peek at the next token. */
25490 token = cp_lexer_peek_token (parser->lexer);
25491 if (token->type != CPP_CLOSE_PAREN)
25492 /* Parse the attribute-list. */
25493 attribute_list = cp_parser_gnu_attribute_list (parser);
25494 else
25495 /* If the next token is a `)', then there is no attribute
25496 list. */
25497 attribute_list = NULL;
25498
25499 /* Look for the two `)' tokens. */
25500 if (!inner_parens.require_close (parser))
25501 ok = false;
25502 if (!outer_parens.require_close (parser))
25503 ok = false;
25504 if (!ok)
25505 cp_parser_skip_to_end_of_statement (parser);
25506
25507 /* Add these new attributes to the list. */
25508 attributes = attr_chainon (attributes, attribute_list);
25509 }
25510
25511 return attributes;
25512 }
25513
25514 /* Parse a GNU attribute-list.
25515
25516 attribute-list:
25517 attribute
25518 attribute-list , attribute
25519
25520 attribute:
25521 identifier
25522 identifier ( identifier )
25523 identifier ( identifier , expression-list )
25524 identifier ( expression-list )
25525
25526 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25527 to an attribute. The TREE_PURPOSE of each node is the identifier
25528 indicating which attribute is in use. The TREE_VALUE represents
25529 the arguments, if any. */
25530
25531 static tree
25532 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
25533 {
25534 tree attribute_list = NULL_TREE;
25535 bool save_translate_strings_p = parser->translate_strings_p;
25536
25537 parser->translate_strings_p = false;
25538 while (true)
25539 {
25540 cp_token *token;
25541 tree identifier;
25542 tree attribute;
25543
25544 /* Look for the identifier. We also allow keywords here; for
25545 example `__attribute__ ((const))' is legal. */
25546 token = cp_lexer_peek_token (parser->lexer);
25547 if (token->type == CPP_NAME
25548 || token->type == CPP_KEYWORD)
25549 {
25550 tree arguments = NULL_TREE;
25551
25552 /* Consume the token, but save it since we need it for the
25553 SIMD enabled function parsing. */
25554 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25555
25556 /* Save away the identifier that indicates which attribute
25557 this is. */
25558 identifier = (token->type == CPP_KEYWORD)
25559 /* For keywords, use the canonical spelling, not the
25560 parsed identifier. */
25561 ? ridpointers[(int) token->keyword]
25562 : id_token->u.value;
25563
25564 identifier = canonicalize_attr_name (identifier);
25565 attribute = build_tree_list (identifier, NULL_TREE);
25566
25567 /* Peek at the next token. */
25568 token = cp_lexer_peek_token (parser->lexer);
25569 /* If it's an `(', then parse the attribute arguments. */
25570 if (token->type == CPP_OPEN_PAREN)
25571 {
25572 vec<tree, va_gc> *vec;
25573 int attr_flag = (attribute_takes_identifier_p (identifier)
25574 ? id_attr : normal_attr);
25575 vec = cp_parser_parenthesized_expression_list
25576 (parser, attr_flag, /*cast_p=*/false,
25577 /*allow_expansion_p=*/false,
25578 /*non_constant_p=*/NULL);
25579 if (vec == NULL)
25580 arguments = error_mark_node;
25581 else
25582 {
25583 arguments = build_tree_list_vec (vec);
25584 release_tree_vector (vec);
25585 }
25586 /* Save the arguments away. */
25587 TREE_VALUE (attribute) = arguments;
25588 }
25589
25590 if (arguments != error_mark_node)
25591 {
25592 /* Add this attribute to the list. */
25593 TREE_CHAIN (attribute) = attribute_list;
25594 attribute_list = attribute;
25595 }
25596
25597 token = cp_lexer_peek_token (parser->lexer);
25598 }
25599 /* Unless EXACTLY_ONE is set look for more attributes.
25600 If the next token isn't a `,', we're done. */
25601 if (exactly_one || token->type != CPP_COMMA)
25602 break;
25603
25604 /* Consume the comma and keep going. */
25605 cp_lexer_consume_token (parser->lexer);
25606 }
25607 parser->translate_strings_p = save_translate_strings_p;
25608
25609 /* We built up the list in reverse order. */
25610 return nreverse (attribute_list);
25611 }
25612
25613 /* Parse a standard C++11 attribute.
25614
25615 The returned representation is a TREE_LIST which TREE_PURPOSE is
25616 the scoped name of the attribute, and the TREE_VALUE is its
25617 arguments list.
25618
25619 Note that the scoped name of the attribute is itself a TREE_LIST
25620 which TREE_PURPOSE is the namespace of the attribute, and
25621 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25622 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25623 and which TREE_PURPOSE is directly the attribute name.
25624
25625 Clients of the attribute code should use get_attribute_namespace
25626 and get_attribute_name to get the actual namespace and name of
25627 attributes, regardless of their being GNU or C++11 attributes.
25628
25629 attribute:
25630 attribute-token attribute-argument-clause [opt]
25631
25632 attribute-token:
25633 identifier
25634 attribute-scoped-token
25635
25636 attribute-scoped-token:
25637 attribute-namespace :: identifier
25638
25639 attribute-namespace:
25640 identifier
25641
25642 attribute-argument-clause:
25643 ( balanced-token-seq )
25644
25645 balanced-token-seq:
25646 balanced-token [opt]
25647 balanced-token-seq balanced-token
25648
25649 balanced-token:
25650 ( balanced-token-seq )
25651 [ balanced-token-seq ]
25652 { balanced-token-seq }. */
25653
25654 static tree
25655 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25656 {
25657 tree attribute, attr_id = NULL_TREE, arguments;
25658 cp_token *token;
25659
25660 temp_override<bool> cleanup
25661 (parser->auto_is_implicit_function_template_parm_p, false);
25662
25663 /* First, parse name of the attribute, a.k.a attribute-token. */
25664
25665 token = cp_lexer_peek_token (parser->lexer);
25666 if (token->type == CPP_NAME)
25667 attr_id = token->u.value;
25668 else if (token->type == CPP_KEYWORD)
25669 attr_id = ridpointers[(int) token->keyword];
25670 else if (token->flags & NAMED_OP)
25671 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25672
25673 if (attr_id == NULL_TREE)
25674 return NULL_TREE;
25675
25676 cp_lexer_consume_token (parser->lexer);
25677
25678 token = cp_lexer_peek_token (parser->lexer);
25679 if (token->type == CPP_SCOPE)
25680 {
25681 /* We are seeing a scoped attribute token. */
25682
25683 cp_lexer_consume_token (parser->lexer);
25684 if (attr_ns)
25685 error_at (token->location, "attribute using prefix used together "
25686 "with scoped attribute token");
25687 attr_ns = attr_id;
25688
25689 token = cp_lexer_consume_token (parser->lexer);
25690 if (token->type == CPP_NAME)
25691 attr_id = token->u.value;
25692 else if (token->type == CPP_KEYWORD)
25693 attr_id = ridpointers[(int) token->keyword];
25694 else if (token->flags & NAMED_OP)
25695 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25696 else
25697 {
25698 error_at (token->location,
25699 "expected an identifier for the attribute name");
25700 return error_mark_node;
25701 }
25702
25703 attr_ns = canonicalize_attr_name (attr_ns);
25704 attr_id = canonicalize_attr_name (attr_id);
25705 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25706 NULL_TREE);
25707 token = cp_lexer_peek_token (parser->lexer);
25708 }
25709 else if (attr_ns)
25710 {
25711 attr_ns = canonicalize_attr_name (attr_ns);
25712 attr_id = canonicalize_attr_name (attr_id);
25713 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25714 NULL_TREE);
25715 }
25716 else
25717 {
25718 attr_id = canonicalize_attr_name (attr_id);
25719 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25720 NULL_TREE);
25721 /* C++11 noreturn attribute is equivalent to GNU's. */
25722 if (is_attribute_p ("noreturn", attr_id))
25723 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25724 /* C++14 deprecated attribute is equivalent to GNU's. */
25725 else if (is_attribute_p ("deprecated", attr_id))
25726 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25727 /* C++17 fallthrough attribute is equivalent to GNU's. */
25728 else if (is_attribute_p ("fallthrough", attr_id))
25729 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25730 /* Transactional Memory TS optimize_for_synchronized attribute is
25731 equivalent to GNU transaction_callable. */
25732 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25733 TREE_PURPOSE (attribute)
25734 = get_identifier ("transaction_callable");
25735 /* Transactional Memory attributes are GNU attributes. */
25736 else if (tm_attr_to_mask (attr_id))
25737 TREE_PURPOSE (attribute) = attr_id;
25738 }
25739
25740 /* Now parse the optional argument clause of the attribute. */
25741
25742 if (token->type != CPP_OPEN_PAREN)
25743 return attribute;
25744
25745 {
25746 vec<tree, va_gc> *vec;
25747 int attr_flag = normal_attr;
25748
25749 if (attr_ns == gnu_identifier
25750 && attribute_takes_identifier_p (attr_id))
25751 /* A GNU attribute that takes an identifier in parameter. */
25752 attr_flag = id_attr;
25753
25754 vec = cp_parser_parenthesized_expression_list
25755 (parser, attr_flag, /*cast_p=*/false,
25756 /*allow_expansion_p=*/true,
25757 /*non_constant_p=*/NULL);
25758 if (vec == NULL)
25759 arguments = error_mark_node;
25760 else
25761 {
25762 arguments = build_tree_list_vec (vec);
25763 release_tree_vector (vec);
25764 }
25765
25766 if (arguments == error_mark_node)
25767 attribute = error_mark_node;
25768 else
25769 TREE_VALUE (attribute) = arguments;
25770 }
25771
25772 return attribute;
25773 }
25774
25775 /* Check that the attribute ATTRIBUTE appears at most once in the
25776 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25777 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25778 isn't implemented yet in GCC. */
25779
25780 static void
25781 cp_parser_check_std_attribute (tree attributes, tree attribute)
25782 {
25783 if (attributes)
25784 {
25785 tree name = get_attribute_name (attribute);
25786 if (is_attribute_p ("noreturn", name)
25787 && lookup_attribute ("noreturn", attributes))
25788 error ("attribute %<noreturn%> can appear at most once "
25789 "in an attribute-list");
25790 else if (is_attribute_p ("deprecated", name)
25791 && lookup_attribute ("deprecated", attributes))
25792 error ("attribute %<deprecated%> can appear at most once "
25793 "in an attribute-list");
25794 }
25795 }
25796
25797 /* Parse a list of standard C++-11 attributes.
25798
25799 attribute-list:
25800 attribute [opt]
25801 attribute-list , attribute[opt]
25802 attribute ...
25803 attribute-list , attribute ...
25804 */
25805
25806 static tree
25807 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25808 {
25809 tree attributes = NULL_TREE, attribute = NULL_TREE;
25810 cp_token *token = NULL;
25811
25812 while (true)
25813 {
25814 attribute = cp_parser_std_attribute (parser, attr_ns);
25815 if (attribute == error_mark_node)
25816 break;
25817 if (attribute != NULL_TREE)
25818 {
25819 cp_parser_check_std_attribute (attributes, attribute);
25820 TREE_CHAIN (attribute) = attributes;
25821 attributes = attribute;
25822 }
25823 token = cp_lexer_peek_token (parser->lexer);
25824 if (token->type == CPP_ELLIPSIS)
25825 {
25826 cp_lexer_consume_token (parser->lexer);
25827 if (attribute == NULL_TREE)
25828 error_at (token->location,
25829 "expected attribute before %<...%>");
25830 else
25831 {
25832 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25833 if (pack == error_mark_node)
25834 return error_mark_node;
25835 TREE_VALUE (attribute) = pack;
25836 }
25837 token = cp_lexer_peek_token (parser->lexer);
25838 }
25839 if (token->type != CPP_COMMA)
25840 break;
25841 cp_lexer_consume_token (parser->lexer);
25842 }
25843 attributes = nreverse (attributes);
25844 return attributes;
25845 }
25846
25847 /* Parse a standard C++-11 attribute specifier.
25848
25849 attribute-specifier:
25850 [ [ attribute-using-prefix [opt] attribute-list ] ]
25851 alignment-specifier
25852
25853 attribute-using-prefix:
25854 using attribute-namespace :
25855
25856 alignment-specifier:
25857 alignas ( type-id ... [opt] )
25858 alignas ( alignment-expression ... [opt] ). */
25859
25860 static tree
25861 cp_parser_std_attribute_spec (cp_parser *parser)
25862 {
25863 tree attributes = NULL_TREE;
25864 cp_token *token = cp_lexer_peek_token (parser->lexer);
25865
25866 if (token->type == CPP_OPEN_SQUARE
25867 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25868 {
25869 tree attr_ns = NULL_TREE;
25870
25871 cp_lexer_consume_token (parser->lexer);
25872 cp_lexer_consume_token (parser->lexer);
25873
25874 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25875 {
25876 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25877 if (token->type == CPP_NAME)
25878 attr_ns = token->u.value;
25879 else if (token->type == CPP_KEYWORD)
25880 attr_ns = ridpointers[(int) token->keyword];
25881 else if (token->flags & NAMED_OP)
25882 attr_ns = get_identifier (cpp_type2name (token->type,
25883 token->flags));
25884 if (attr_ns
25885 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25886 {
25887 if (cxx_dialect < cxx17
25888 && !in_system_header_at (input_location))
25889 pedwarn (input_location, 0,
25890 "attribute using prefix only available "
25891 "with -std=c++17 or -std=gnu++17");
25892
25893 cp_lexer_consume_token (parser->lexer);
25894 cp_lexer_consume_token (parser->lexer);
25895 cp_lexer_consume_token (parser->lexer);
25896 }
25897 else
25898 attr_ns = NULL_TREE;
25899 }
25900
25901 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25902
25903 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25904 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25905 cp_parser_skip_to_end_of_statement (parser);
25906 else
25907 /* Warn about parsing c++11 attribute in non-c++11 mode, only
25908 when we are sure that we have actually parsed them. */
25909 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25910 }
25911 else
25912 {
25913 tree alignas_expr;
25914
25915 /* Look for an alignment-specifier. */
25916
25917 token = cp_lexer_peek_token (parser->lexer);
25918
25919 if (token->type != CPP_KEYWORD
25920 || token->keyword != RID_ALIGNAS)
25921 return NULL_TREE;
25922
25923 cp_lexer_consume_token (parser->lexer);
25924 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25925
25926 matching_parens parens;
25927 if (!parens.require_open (parser))
25928 return error_mark_node;
25929
25930 cp_parser_parse_tentatively (parser);
25931 alignas_expr = cp_parser_type_id (parser);
25932
25933 if (!cp_parser_parse_definitely (parser))
25934 {
25935 alignas_expr = cp_parser_assignment_expression (parser);
25936 if (alignas_expr == error_mark_node)
25937 cp_parser_skip_to_end_of_statement (parser);
25938 if (alignas_expr == NULL_TREE
25939 || alignas_expr == error_mark_node)
25940 return alignas_expr;
25941 }
25942
25943 alignas_expr = cxx_alignas_expr (alignas_expr);
25944 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25945
25946 /* Handle alignas (pack...). */
25947 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25948 {
25949 cp_lexer_consume_token (parser->lexer);
25950 alignas_expr = make_pack_expansion (alignas_expr);
25951 }
25952
25953 /* Something went wrong, so don't build the attribute. */
25954 if (alignas_expr == error_mark_node)
25955 return error_mark_node;
25956
25957 if (!parens.require_close (parser))
25958 return error_mark_node;
25959
25960 /* Build the C++-11 representation of an 'aligned'
25961 attribute. */
25962 attributes
25963 = build_tree_list (build_tree_list (gnu_identifier,
25964 aligned_identifier), alignas_expr);
25965 }
25966
25967 return attributes;
25968 }
25969
25970 /* Parse a standard C++-11 attribute-specifier-seq.
25971
25972 attribute-specifier-seq:
25973 attribute-specifier-seq [opt] attribute-specifier
25974 */
25975
25976 static tree
25977 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25978 {
25979 tree attr_specs = NULL_TREE;
25980 tree attr_last = NULL_TREE;
25981
25982 while (true)
25983 {
25984 tree attr_spec = cp_parser_std_attribute_spec (parser);
25985 if (attr_spec == NULL_TREE)
25986 break;
25987 if (attr_spec == error_mark_node)
25988 return error_mark_node;
25989
25990 if (attr_last)
25991 TREE_CHAIN (attr_last) = attr_spec;
25992 else
25993 attr_specs = attr_last = attr_spec;
25994 attr_last = tree_last (attr_last);
25995 }
25996
25997 return attr_specs;
25998 }
25999
26000 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26001 return index of the first token after balanced-token, or N on failure. */
26002
26003 static size_t
26004 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26005 {
26006 size_t orig_n = n;
26007 int nparens = 0, nbraces = 0, nsquares = 0;
26008 do
26009 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26010 {
26011 case CPP_EOF:
26012 case CPP_PRAGMA_EOL:
26013 /* Ran out of tokens. */
26014 return orig_n;
26015 case CPP_OPEN_PAREN:
26016 ++nparens;
26017 break;
26018 case CPP_OPEN_BRACE:
26019 ++nbraces;
26020 break;
26021 case CPP_OPEN_SQUARE:
26022 ++nsquares;
26023 break;
26024 case CPP_CLOSE_PAREN:
26025 --nparens;
26026 break;
26027 case CPP_CLOSE_BRACE:
26028 --nbraces;
26029 break;
26030 case CPP_CLOSE_SQUARE:
26031 --nsquares;
26032 break;
26033 default:
26034 break;
26035 }
26036 while (nparens || nbraces || nsquares);
26037 return n;
26038 }
26039
26040 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26041 return index of the first token after the GNU attribute tokens, or N on
26042 failure. */
26043
26044 static size_t
26045 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26046 {
26047 while (true)
26048 {
26049 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26050 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26051 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26052 break;
26053
26054 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26055 if (n2 == n + 2)
26056 break;
26057 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26058 break;
26059 n = n2 + 1;
26060 }
26061 return n;
26062 }
26063
26064 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26065 next token), return index of the first token after the standard C++11
26066 attribute tokens, or N on failure. */
26067
26068 static size_t
26069 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26070 {
26071 while (true)
26072 {
26073 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26074 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26075 {
26076 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26077 if (n2 == n + 1)
26078 break;
26079 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26080 break;
26081 n = n2 + 1;
26082 }
26083 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26084 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26085 {
26086 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26087 if (n2 == n + 1)
26088 break;
26089 n = n2;
26090 }
26091 else
26092 break;
26093 }
26094 return n;
26095 }
26096
26097 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26098 as the next token), return index of the first token after the attribute
26099 tokens, or N on failure. */
26100
26101 static size_t
26102 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26103 {
26104 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26105 return cp_parser_skip_gnu_attributes_opt (parser, n);
26106 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26107 }
26108
26109 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26110 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26111 current value of the PEDANTIC flag, regardless of whether or not
26112 the `__extension__' keyword is present. The caller is responsible
26113 for restoring the value of the PEDANTIC flag. */
26114
26115 static bool
26116 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26117 {
26118 /* Save the old value of the PEDANTIC flag. */
26119 *saved_pedantic = pedantic;
26120
26121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26122 {
26123 /* Consume the `__extension__' token. */
26124 cp_lexer_consume_token (parser->lexer);
26125 /* We're not being pedantic while the `__extension__' keyword is
26126 in effect. */
26127 pedantic = 0;
26128
26129 return true;
26130 }
26131
26132 return false;
26133 }
26134
26135 /* Parse a label declaration.
26136
26137 label-declaration:
26138 __label__ label-declarator-seq ;
26139
26140 label-declarator-seq:
26141 identifier , label-declarator-seq
26142 identifier */
26143
26144 static void
26145 cp_parser_label_declaration (cp_parser* parser)
26146 {
26147 /* Look for the `__label__' keyword. */
26148 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26149
26150 while (true)
26151 {
26152 tree identifier;
26153
26154 /* Look for an identifier. */
26155 identifier = cp_parser_identifier (parser);
26156 /* If we failed, stop. */
26157 if (identifier == error_mark_node)
26158 break;
26159 /* Declare it as a label. */
26160 finish_label_decl (identifier);
26161 /* If the next token is a `;', stop. */
26162 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26163 break;
26164 /* Look for the `,' separating the label declarations. */
26165 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26166 }
26167
26168 /* Look for the final `;'. */
26169 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26170 }
26171
26172 // -------------------------------------------------------------------------- //
26173 // Requires Clause
26174
26175 // Parse a requires clause.
26176 //
26177 // requires-clause:
26178 // 'requires' logical-or-expression
26179 //
26180 // The required logical-or-expression must be a constant expression. Note
26181 // that we don't check that the expression is constepxr here. We defer until
26182 // we analyze constraints and then, we only check atomic constraints.
26183 static tree
26184 cp_parser_requires_clause (cp_parser *parser)
26185 {
26186 // Parse the requires clause so that it is not automatically folded.
26187 ++processing_template_decl;
26188 tree expr = cp_parser_binary_expression (parser, false, false,
26189 PREC_NOT_OPERATOR, NULL);
26190 if (check_for_bare_parameter_packs (expr))
26191 expr = error_mark_node;
26192 --processing_template_decl;
26193 return expr;
26194 }
26195
26196 // Optionally parse a requires clause:
26197 static tree
26198 cp_parser_requires_clause_opt (cp_parser *parser)
26199 {
26200 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26201 if (tok->keyword != RID_REQUIRES)
26202 {
26203 if (!flag_concepts && tok->type == CPP_NAME
26204 && tok->u.value == ridpointers[RID_REQUIRES])
26205 {
26206 error_at (cp_lexer_peek_token (parser->lexer)->location,
26207 "%<requires%> only available with -fconcepts");
26208 /* Parse and discard the requires-clause. */
26209 cp_lexer_consume_token (parser->lexer);
26210 cp_parser_requires_clause (parser);
26211 }
26212 return NULL_TREE;
26213 }
26214 cp_lexer_consume_token (parser->lexer);
26215 return cp_parser_requires_clause (parser);
26216 }
26217
26218
26219 /*---------------------------------------------------------------------------
26220 Requires expressions
26221 ---------------------------------------------------------------------------*/
26222
26223 /* Parse a requires expression
26224
26225 requirement-expression:
26226 'requires' requirement-parameter-list [opt] requirement-body */
26227 static tree
26228 cp_parser_requires_expression (cp_parser *parser)
26229 {
26230 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26231 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26232
26233 /* A requires-expression shall appear only within a concept
26234 definition or a requires-clause.
26235
26236 TODO: Implement this diagnostic correctly. */
26237 if (!processing_template_decl)
26238 {
26239 error_at (loc, "a requires expression cannot appear outside a template");
26240 cp_parser_skip_to_end_of_statement (parser);
26241 return error_mark_node;
26242 }
26243
26244 tree parms, reqs;
26245 {
26246 /* Local parameters are delared as variables within the scope
26247 of the expression. They are not visible past the end of
26248 the expression. Expressions within the requires-expression
26249 are unevaluated. */
26250 struct scope_sentinel
26251 {
26252 scope_sentinel ()
26253 {
26254 ++cp_unevaluated_operand;
26255 begin_scope (sk_block, NULL_TREE);
26256 }
26257
26258 ~scope_sentinel ()
26259 {
26260 pop_bindings_and_leave_scope ();
26261 --cp_unevaluated_operand;
26262 }
26263 } s;
26264
26265 /* Parse the optional parameter list. */
26266 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26267 {
26268 parms = cp_parser_requirement_parameter_list (parser);
26269 if (parms == error_mark_node)
26270 return error_mark_node;
26271 }
26272 else
26273 parms = NULL_TREE;
26274
26275 /* Parse the requirement body. */
26276 reqs = cp_parser_requirement_body (parser);
26277 if (reqs == error_mark_node)
26278 return error_mark_node;
26279 }
26280
26281 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26282 the parm chain. */
26283 grokparms (parms, &parms);
26284 return finish_requires_expr (parms, reqs);
26285 }
26286
26287 /* Parse a parameterized requirement.
26288
26289 requirement-parameter-list:
26290 '(' parameter-declaration-clause ')' */
26291 static tree
26292 cp_parser_requirement_parameter_list (cp_parser *parser)
26293 {
26294 matching_parens parens;
26295 if (!parens.require_open (parser))
26296 return error_mark_node;
26297
26298 tree parms = cp_parser_parameter_declaration_clause (parser);
26299
26300 if (!parens.require_close (parser))
26301 return error_mark_node;
26302
26303 return parms;
26304 }
26305
26306 /* Parse the body of a requirement.
26307
26308 requirement-body:
26309 '{' requirement-list '}' */
26310 static tree
26311 cp_parser_requirement_body (cp_parser *parser)
26312 {
26313 matching_braces braces;
26314 if (!braces.require_open (parser))
26315 return error_mark_node;
26316
26317 tree reqs = cp_parser_requirement_list (parser);
26318
26319 if (!braces.require_close (parser))
26320 return error_mark_node;
26321
26322 return reqs;
26323 }
26324
26325 /* Parse a list of requirements.
26326
26327 requirement-list:
26328 requirement
26329 requirement-list ';' requirement[opt] */
26330 static tree
26331 cp_parser_requirement_list (cp_parser *parser)
26332 {
26333 tree result = NULL_TREE;
26334 while (true)
26335 {
26336 tree req = cp_parser_requirement (parser);
26337 if (req == error_mark_node)
26338 return error_mark_node;
26339
26340 result = tree_cons (NULL_TREE, req, result);
26341
26342 /* If we see a semi-colon, consume it. */
26343 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26344 cp_lexer_consume_token (parser->lexer);
26345
26346 /* Stop processing at the end of the list. */
26347 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26348 break;
26349 }
26350
26351 /* Reverse the order of requirements so they are analyzed in
26352 declaration order. */
26353 return nreverse (result);
26354 }
26355
26356 /* Parse a syntactic requirement or type requirement.
26357
26358 requirement:
26359 simple-requirement
26360 compound-requirement
26361 type-requirement
26362 nested-requirement */
26363 static tree
26364 cp_parser_requirement (cp_parser *parser)
26365 {
26366 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26367 return cp_parser_compound_requirement (parser);
26368 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26369 return cp_parser_type_requirement (parser);
26370 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26371 return cp_parser_nested_requirement (parser);
26372 else
26373 return cp_parser_simple_requirement (parser);
26374 }
26375
26376 /* Parse a simple requirement.
26377
26378 simple-requirement:
26379 expression ';' */
26380 static tree
26381 cp_parser_simple_requirement (cp_parser *parser)
26382 {
26383 tree expr = cp_parser_expression (parser, NULL, false, false);
26384 if (!expr || expr == error_mark_node)
26385 return error_mark_node;
26386
26387 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26388 return error_mark_node;
26389
26390 return finish_simple_requirement (expr);
26391 }
26392
26393 /* Parse a type requirement
26394
26395 type-requirement
26396 nested-name-specifier [opt] required-type-name ';'
26397
26398 required-type-name:
26399 type-name
26400 'template' [opt] simple-template-id */
26401 static tree
26402 cp_parser_type_requirement (cp_parser *parser)
26403 {
26404 cp_lexer_consume_token (parser->lexer);
26405
26406 // Save the scope before parsing name specifiers.
26407 tree saved_scope = parser->scope;
26408 tree saved_object_scope = parser->object_scope;
26409 tree saved_qualifying_scope = parser->qualifying_scope;
26410 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26411 cp_parser_nested_name_specifier_opt (parser,
26412 /*typename_keyword_p=*/true,
26413 /*check_dependency_p=*/false,
26414 /*type_p=*/true,
26415 /*is_declaration=*/false);
26416
26417 tree type;
26418 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26419 {
26420 cp_lexer_consume_token (parser->lexer);
26421 type = cp_parser_template_id (parser,
26422 /*template_keyword_p=*/true,
26423 /*check_dependency=*/false,
26424 /*tag_type=*/none_type,
26425 /*is_declaration=*/false);
26426 type = make_typename_type (parser->scope, type, typename_type,
26427 /*complain=*/tf_error);
26428 }
26429 else
26430 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26431
26432 if (TREE_CODE (type) == TYPE_DECL)
26433 type = TREE_TYPE (type);
26434
26435 parser->scope = saved_scope;
26436 parser->object_scope = saved_object_scope;
26437 parser->qualifying_scope = saved_qualifying_scope;
26438
26439 if (type == error_mark_node)
26440 cp_parser_skip_to_end_of_statement (parser);
26441
26442 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26443 return error_mark_node;
26444 if (type == error_mark_node)
26445 return error_mark_node;
26446
26447 return finish_type_requirement (type);
26448 }
26449
26450 /* Parse a compound requirement
26451
26452 compound-requirement:
26453 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26454 static tree
26455 cp_parser_compound_requirement (cp_parser *parser)
26456 {
26457 /* Parse an expression enclosed in '{ }'s. */
26458 matching_braces braces;
26459 if (!braces.require_open (parser))
26460 return error_mark_node;
26461
26462 tree expr = cp_parser_expression (parser, NULL, false, false);
26463 if (!expr || expr == error_mark_node)
26464 return error_mark_node;
26465
26466 if (!braces.require_close (parser))
26467 return error_mark_node;
26468
26469 /* Parse the optional noexcept. */
26470 bool noexcept_p = false;
26471 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26472 {
26473 cp_lexer_consume_token (parser->lexer);
26474 noexcept_p = true;
26475 }
26476
26477 /* Parse the optional trailing return type. */
26478 tree type = NULL_TREE;
26479 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26480 {
26481 cp_lexer_consume_token (parser->lexer);
26482 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26483 parser->in_result_type_constraint_p = true;
26484 type = cp_parser_trailing_type_id (parser);
26485 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26486 if (type == error_mark_node)
26487 return error_mark_node;
26488 }
26489
26490 return finish_compound_requirement (expr, type, noexcept_p);
26491 }
26492
26493 /* Parse a nested requirement. This is the same as a requires clause.
26494
26495 nested-requirement:
26496 requires-clause */
26497 static tree
26498 cp_parser_nested_requirement (cp_parser *parser)
26499 {
26500 cp_lexer_consume_token (parser->lexer);
26501 tree req = cp_parser_requires_clause (parser);
26502 if (req == error_mark_node)
26503 return error_mark_node;
26504 return finish_nested_requirement (req);
26505 }
26506
26507 /* Support Functions */
26508
26509 /* Return the appropriate prefer_type argument for lookup_name_real based on
26510 tag_type and template_mem_access. */
26511
26512 static inline int
26513 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26514 {
26515 /* DR 141: When looking in the current enclosing context for a template-name
26516 after -> or ., only consider class templates. */
26517 if (template_mem_access)
26518 return 2;
26519 switch (tag_type)
26520 {
26521 case none_type: return 0; // No preference.
26522 case scope_type: return 1; // Type or namespace.
26523 default: return 2; // Type only.
26524 }
26525 }
26526
26527 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26528 NAME should have one of the representations used for an
26529 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26530 is returned. If PARSER->SCOPE is a dependent type, then a
26531 SCOPE_REF is returned.
26532
26533 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26534 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26535 was formed. Abstractly, such entities should not be passed to this
26536 function, because they do not need to be looked up, but it is
26537 simpler to check for this special case here, rather than at the
26538 call-sites.
26539
26540 In cases not explicitly covered above, this function returns a
26541 DECL, OVERLOAD, or baselink representing the result of the lookup.
26542 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26543 is returned.
26544
26545 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26546 (e.g., "struct") that was used. In that case bindings that do not
26547 refer to types are ignored.
26548
26549 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26550 ignored.
26551
26552 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26553 are ignored.
26554
26555 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26556 types.
26557
26558 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26559 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26560 NULL_TREE otherwise. */
26561
26562 static cp_expr
26563 cp_parser_lookup_name (cp_parser *parser, tree name,
26564 enum tag_types tag_type,
26565 bool is_template,
26566 bool is_namespace,
26567 bool check_dependency,
26568 tree *ambiguous_decls,
26569 location_t name_location)
26570 {
26571 tree decl;
26572 tree object_type = parser->context->object_type;
26573
26574 /* Assume that the lookup will be unambiguous. */
26575 if (ambiguous_decls)
26576 *ambiguous_decls = NULL_TREE;
26577
26578 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26579 no longer valid. Note that if we are parsing tentatively, and
26580 the parse fails, OBJECT_TYPE will be automatically restored. */
26581 parser->context->object_type = NULL_TREE;
26582
26583 if (name == error_mark_node)
26584 return error_mark_node;
26585
26586 /* A template-id has already been resolved; there is no lookup to
26587 do. */
26588 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26589 return name;
26590 if (BASELINK_P (name))
26591 {
26592 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26593 == TEMPLATE_ID_EXPR);
26594 return name;
26595 }
26596
26597 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26598 it should already have been checked to make sure that the name
26599 used matches the type being destroyed. */
26600 if (TREE_CODE (name) == BIT_NOT_EXPR)
26601 {
26602 tree type;
26603
26604 /* Figure out to which type this destructor applies. */
26605 if (parser->scope)
26606 type = parser->scope;
26607 else if (object_type)
26608 type = object_type;
26609 else
26610 type = current_class_type;
26611 /* If that's not a class type, there is no destructor. */
26612 if (!type || !CLASS_TYPE_P (type))
26613 return error_mark_node;
26614
26615 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26616 lazily_declare_fn (sfk_destructor, type);
26617
26618 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26619 return dtor;
26620
26621 return error_mark_node;
26622 }
26623
26624 /* By this point, the NAME should be an ordinary identifier. If
26625 the id-expression was a qualified name, the qualifying scope is
26626 stored in PARSER->SCOPE at this point. */
26627 gcc_assert (identifier_p (name));
26628
26629 /* Perform the lookup. */
26630 if (parser->scope)
26631 {
26632 bool dependent_p;
26633
26634 if (parser->scope == error_mark_node)
26635 return error_mark_node;
26636
26637 /* If the SCOPE is dependent, the lookup must be deferred until
26638 the template is instantiated -- unless we are explicitly
26639 looking up names in uninstantiated templates. Even then, we
26640 cannot look up the name if the scope is not a class type; it
26641 might, for example, be a template type parameter. */
26642 dependent_p = (TYPE_P (parser->scope)
26643 && dependent_scope_p (parser->scope));
26644 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26645 && dependent_p)
26646 /* Defer lookup. */
26647 decl = error_mark_node;
26648 else
26649 {
26650 tree pushed_scope = NULL_TREE;
26651
26652 /* If PARSER->SCOPE is a dependent type, then it must be a
26653 class type, and we must not be checking dependencies;
26654 otherwise, we would have processed this lookup above. So
26655 that PARSER->SCOPE is not considered a dependent base by
26656 lookup_member, we must enter the scope here. */
26657 if (dependent_p)
26658 pushed_scope = push_scope (parser->scope);
26659
26660 /* If the PARSER->SCOPE is a template specialization, it
26661 may be instantiated during name lookup. In that case,
26662 errors may be issued. Even if we rollback the current
26663 tentative parse, those errors are valid. */
26664 decl = lookup_qualified_name (parser->scope, name,
26665 prefer_type_arg (tag_type),
26666 /*complain=*/true);
26667
26668 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26669 lookup result and the nested-name-specifier nominates a class C:
26670 * if the name specified after the nested-name-specifier, when
26671 looked up in C, is the injected-class-name of C (Clause 9), or
26672 * if the name specified after the nested-name-specifier is the
26673 same as the identifier or the simple-template-id's template-
26674 name in the last component of the nested-name-specifier,
26675 the name is instead considered to name the constructor of
26676 class C. [ Note: for example, the constructor is not an
26677 acceptable lookup result in an elaborated-type-specifier so
26678 the constructor would not be used in place of the
26679 injected-class-name. --end note ] Such a constructor name
26680 shall be used only in the declarator-id of a declaration that
26681 names a constructor or in a using-declaration. */
26682 if (tag_type == none_type
26683 && DECL_SELF_REFERENCE_P (decl)
26684 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26685 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26686 prefer_type_arg (tag_type),
26687 /*complain=*/true);
26688
26689 /* If we have a single function from a using decl, pull it out. */
26690 if (TREE_CODE (decl) == OVERLOAD
26691 && !really_overloaded_fn (decl))
26692 decl = OVL_FUNCTION (decl);
26693
26694 if (pushed_scope)
26695 pop_scope (pushed_scope);
26696 }
26697
26698 /* If the scope is a dependent type and either we deferred lookup or
26699 we did lookup but didn't find the name, rememeber the name. */
26700 if (decl == error_mark_node && TYPE_P (parser->scope)
26701 && dependent_type_p (parser->scope))
26702 {
26703 if (tag_type)
26704 {
26705 tree type;
26706
26707 /* The resolution to Core Issue 180 says that `struct
26708 A::B' should be considered a type-name, even if `A'
26709 is dependent. */
26710 type = make_typename_type (parser->scope, name, tag_type,
26711 /*complain=*/tf_error);
26712 if (type != error_mark_node)
26713 decl = TYPE_NAME (type);
26714 }
26715 else if (is_template
26716 && (cp_parser_next_token_ends_template_argument_p (parser)
26717 || cp_lexer_next_token_is (parser->lexer,
26718 CPP_CLOSE_PAREN)))
26719 decl = make_unbound_class_template (parser->scope,
26720 name, NULL_TREE,
26721 /*complain=*/tf_error);
26722 else
26723 decl = build_qualified_name (/*type=*/NULL_TREE,
26724 parser->scope, name,
26725 is_template);
26726 }
26727 parser->qualifying_scope = parser->scope;
26728 parser->object_scope = NULL_TREE;
26729 }
26730 else if (object_type)
26731 {
26732 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26733 OBJECT_TYPE is not a class. */
26734 if (CLASS_TYPE_P (object_type))
26735 /* If the OBJECT_TYPE is a template specialization, it may
26736 be instantiated during name lookup. In that case, errors
26737 may be issued. Even if we rollback the current tentative
26738 parse, those errors are valid. */
26739 decl = lookup_member (object_type,
26740 name,
26741 /*protect=*/0,
26742 prefer_type_arg (tag_type),
26743 tf_warning_or_error);
26744 else
26745 decl = NULL_TREE;
26746
26747 if (!decl)
26748 /* Look it up in the enclosing context. DR 141: When looking for a
26749 template-name after -> or ., only consider class templates. */
26750 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26751 /*nonclass=*/0,
26752 /*block_p=*/true, is_namespace, 0);
26753 if (object_type == unknown_type_node)
26754 /* The object is type-dependent, so we can't look anything up; we used
26755 this to get the DR 141 behavior. */
26756 object_type = NULL_TREE;
26757 parser->object_scope = object_type;
26758 parser->qualifying_scope = NULL_TREE;
26759 }
26760 else
26761 {
26762 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26763 /*nonclass=*/0,
26764 /*block_p=*/true, is_namespace, 0);
26765 parser->qualifying_scope = NULL_TREE;
26766 parser->object_scope = NULL_TREE;
26767 }
26768
26769 /* If the lookup failed, let our caller know. */
26770 if (!decl || decl == error_mark_node)
26771 return error_mark_node;
26772
26773 /* Pull out the template from an injected-class-name (or multiple). */
26774 if (is_template)
26775 decl = maybe_get_template_decl_from_type_decl (decl);
26776
26777 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26778 if (TREE_CODE (decl) == TREE_LIST)
26779 {
26780 if (ambiguous_decls)
26781 *ambiguous_decls = decl;
26782 /* The error message we have to print is too complicated for
26783 cp_parser_error, so we incorporate its actions directly. */
26784 if (!cp_parser_simulate_error (parser))
26785 {
26786 error_at (name_location, "reference to %qD is ambiguous",
26787 name);
26788 print_candidates (decl);
26789 }
26790 return error_mark_node;
26791 }
26792
26793 gcc_assert (DECL_P (decl)
26794 || TREE_CODE (decl) == OVERLOAD
26795 || TREE_CODE (decl) == SCOPE_REF
26796 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26797 || BASELINK_P (decl));
26798
26799 /* If we have resolved the name of a member declaration, check to
26800 see if the declaration is accessible. When the name resolves to
26801 set of overloaded functions, accessibility is checked when
26802 overload resolution is done.
26803
26804 During an explicit instantiation, access is not checked at all,
26805 as per [temp.explicit]. */
26806 if (DECL_P (decl))
26807 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26808
26809 maybe_record_typedef_use (decl);
26810
26811 return cp_expr (decl, name_location);
26812 }
26813
26814 /* Like cp_parser_lookup_name, but for use in the typical case where
26815 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26816 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26817
26818 static tree
26819 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26820 {
26821 return cp_parser_lookup_name (parser, name,
26822 none_type,
26823 /*is_template=*/false,
26824 /*is_namespace=*/false,
26825 /*check_dependency=*/true,
26826 /*ambiguous_decls=*/NULL,
26827 location);
26828 }
26829
26830 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26831 the current context, return the TYPE_DECL. If TAG_NAME_P is
26832 true, the DECL indicates the class being defined in a class-head,
26833 or declared in an elaborated-type-specifier.
26834
26835 Otherwise, return DECL. */
26836
26837 static tree
26838 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26839 {
26840 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26841 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26842
26843 struct A {
26844 template <typename T> struct B;
26845 };
26846
26847 template <typename T> struct A::B {};
26848
26849 Similarly, in an elaborated-type-specifier:
26850
26851 namespace N { struct X{}; }
26852
26853 struct A {
26854 template <typename T> friend struct N::X;
26855 };
26856
26857 However, if the DECL refers to a class type, and we are in
26858 the scope of the class, then the name lookup automatically
26859 finds the TYPE_DECL created by build_self_reference rather
26860 than a TEMPLATE_DECL. For example, in:
26861
26862 template <class T> struct S {
26863 S s;
26864 };
26865
26866 there is no need to handle such case. */
26867
26868 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26869 return DECL_TEMPLATE_RESULT (decl);
26870
26871 return decl;
26872 }
26873
26874 /* If too many, or too few, template-parameter lists apply to the
26875 declarator, issue an error message. Returns TRUE if all went well,
26876 and FALSE otherwise. */
26877
26878 static bool
26879 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26880 cp_declarator *declarator,
26881 location_t declarator_location)
26882 {
26883 switch (declarator->kind)
26884 {
26885 case cdk_id:
26886 {
26887 unsigned num_templates = 0;
26888 tree scope = declarator->u.id.qualifying_scope;
26889 bool template_id_p = false;
26890
26891 if (scope)
26892 num_templates = num_template_headers_for_class (scope);
26893 else if (TREE_CODE (declarator->u.id.unqualified_name)
26894 == TEMPLATE_ID_EXPR)
26895 {
26896 /* If the DECLARATOR has the form `X<y>' then it uses one
26897 additional level of template parameters. */
26898 ++num_templates;
26899 template_id_p = true;
26900 }
26901
26902 return cp_parser_check_template_parameters
26903 (parser, num_templates, template_id_p, declarator_location,
26904 declarator);
26905 }
26906
26907 case cdk_function:
26908 case cdk_array:
26909 case cdk_pointer:
26910 case cdk_reference:
26911 case cdk_ptrmem:
26912 return (cp_parser_check_declarator_template_parameters
26913 (parser, declarator->declarator, declarator_location));
26914
26915 case cdk_decomp:
26916 case cdk_error:
26917 return true;
26918
26919 default:
26920 gcc_unreachable ();
26921 }
26922 return false;
26923 }
26924
26925 /* NUM_TEMPLATES were used in the current declaration. If that is
26926 invalid, return FALSE and issue an error messages. Otherwise,
26927 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26928 declarator and we can print more accurate diagnostics. */
26929
26930 static bool
26931 cp_parser_check_template_parameters (cp_parser* parser,
26932 unsigned num_templates,
26933 bool template_id_p,
26934 location_t location,
26935 cp_declarator *declarator)
26936 {
26937 /* If there are the same number of template classes and parameter
26938 lists, that's OK. */
26939 if (parser->num_template_parameter_lists == num_templates)
26940 return true;
26941 /* If there are more, but only one more, and the name ends in an identifier,
26942 then we are declaring a primary template. That's OK too. */
26943 if (!template_id_p
26944 && parser->num_template_parameter_lists == num_templates + 1)
26945 return true;
26946 /* If there are more template classes than parameter lists, we have
26947 something like:
26948
26949 template <class T> void S<T>::R<T>::f (); */
26950 if (parser->num_template_parameter_lists < num_templates)
26951 {
26952 if (declarator && !current_function_decl)
26953 error_at (location, "specializing member %<%T::%E%> "
26954 "requires %<template<>%> syntax",
26955 declarator->u.id.qualifying_scope,
26956 declarator->u.id.unqualified_name);
26957 else if (declarator)
26958 error_at (location, "invalid declaration of %<%T::%E%>",
26959 declarator->u.id.qualifying_scope,
26960 declarator->u.id.unqualified_name);
26961 else
26962 error_at (location, "too few template-parameter-lists");
26963 return false;
26964 }
26965 /* Otherwise, there are too many template parameter lists. We have
26966 something like:
26967
26968 template <class T> template <class U> void S::f(); */
26969 error_at (location, "too many template-parameter-lists");
26970 return false;
26971 }
26972
26973 /* Parse an optional `::' token indicating that the following name is
26974 from the global namespace. If so, PARSER->SCOPE is set to the
26975 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26976 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26977 Returns the new value of PARSER->SCOPE, if the `::' token is
26978 present, and NULL_TREE otherwise. */
26979
26980 static tree
26981 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26982 {
26983 cp_token *token;
26984
26985 /* Peek at the next token. */
26986 token = cp_lexer_peek_token (parser->lexer);
26987 /* If we're looking at a `::' token then we're starting from the
26988 global namespace, not our current location. */
26989 if (token->type == CPP_SCOPE)
26990 {
26991 /* Consume the `::' token. */
26992 cp_lexer_consume_token (parser->lexer);
26993 /* Set the SCOPE so that we know where to start the lookup. */
26994 parser->scope = global_namespace;
26995 parser->qualifying_scope = global_namespace;
26996 parser->object_scope = NULL_TREE;
26997
26998 return parser->scope;
26999 }
27000 else if (!current_scope_valid_p)
27001 {
27002 parser->scope = NULL_TREE;
27003 parser->qualifying_scope = NULL_TREE;
27004 parser->object_scope = NULL_TREE;
27005 }
27006
27007 return NULL_TREE;
27008 }
27009
27010 /* Returns TRUE if the upcoming token sequence is the start of a
27011 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27012 declarator is preceded by the `friend' specifier. */
27013
27014 static bool
27015 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
27016 {
27017 bool constructor_p;
27018 bool outside_class_specifier_p;
27019 tree nested_name_specifier;
27020 cp_token *next_token;
27021
27022 /* The common case is that this is not a constructor declarator, so
27023 try to avoid doing lots of work if at all possible. It's not
27024 valid declare a constructor at function scope. */
27025 if (parser->in_function_body)
27026 return false;
27027 /* And only certain tokens can begin a constructor declarator. */
27028 next_token = cp_lexer_peek_token (parser->lexer);
27029 if (next_token->type != CPP_NAME
27030 && next_token->type != CPP_SCOPE
27031 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27032 && next_token->type != CPP_TEMPLATE_ID)
27033 return false;
27034
27035 /* Parse tentatively; we are going to roll back all of the tokens
27036 consumed here. */
27037 cp_parser_parse_tentatively (parser);
27038 /* Assume that we are looking at a constructor declarator. */
27039 constructor_p = true;
27040
27041 /* Look for the optional `::' operator. */
27042 cp_parser_global_scope_opt (parser,
27043 /*current_scope_valid_p=*/false);
27044 /* Look for the nested-name-specifier. */
27045 nested_name_specifier
27046 = (cp_parser_nested_name_specifier_opt (parser,
27047 /*typename_keyword_p=*/false,
27048 /*check_dependency_p=*/false,
27049 /*type_p=*/false,
27050 /*is_declaration=*/false));
27051
27052 outside_class_specifier_p = (!at_class_scope_p ()
27053 || !TYPE_BEING_DEFINED (current_class_type)
27054 || friend_p);
27055
27056 /* Outside of a class-specifier, there must be a
27057 nested-name-specifier. Except in C++17 mode, where we
27058 might be declaring a guiding declaration. */
27059 if (!nested_name_specifier && outside_class_specifier_p
27060 && cxx_dialect < cxx17)
27061 constructor_p = false;
27062 else if (nested_name_specifier == error_mark_node)
27063 constructor_p = false;
27064
27065 /* If we have a class scope, this is easy; DR 147 says that S::S always
27066 names the constructor, and no other qualified name could. */
27067 if (constructor_p && nested_name_specifier
27068 && CLASS_TYPE_P (nested_name_specifier))
27069 {
27070 tree id = cp_parser_unqualified_id (parser,
27071 /*template_keyword_p=*/false,
27072 /*check_dependency_p=*/false,
27073 /*declarator_p=*/true,
27074 /*optional_p=*/false);
27075 if (is_overloaded_fn (id))
27076 id = DECL_NAME (get_first_fn (id));
27077 if (!constructor_name_p (id, nested_name_specifier))
27078 constructor_p = false;
27079 }
27080 /* If we still think that this might be a constructor-declarator,
27081 look for a class-name. */
27082 else if (constructor_p)
27083 {
27084 /* If we have:
27085
27086 template <typename T> struct S {
27087 S();
27088 };
27089
27090 we must recognize that the nested `S' names a class. */
27091 if (cxx_dialect >= cxx17)
27092 cp_parser_parse_tentatively (parser);
27093
27094 tree type_decl;
27095 type_decl = cp_parser_class_name (parser,
27096 /*typename_keyword_p=*/false,
27097 /*template_keyword_p=*/false,
27098 none_type,
27099 /*check_dependency_p=*/false,
27100 /*class_head_p=*/false,
27101 /*is_declaration=*/false);
27102
27103 if (cxx_dialect >= cxx17
27104 && !cp_parser_parse_definitely (parser))
27105 {
27106 type_decl = NULL_TREE;
27107 tree tmpl = cp_parser_template_name (parser,
27108 /*template_keyword*/false,
27109 /*check_dependency_p*/false,
27110 /*is_declaration*/false,
27111 none_type,
27112 /*is_identifier*/NULL);
27113 if (DECL_CLASS_TEMPLATE_P (tmpl)
27114 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27115 /* It's a deduction guide, return true. */;
27116 else
27117 cp_parser_simulate_error (parser);
27118 }
27119
27120 /* If there was no class-name, then this is not a constructor.
27121 Otherwise, if we are in a class-specifier and we aren't
27122 handling a friend declaration, check that its type matches
27123 current_class_type (c++/38313). Note: error_mark_node
27124 is left alone for error recovery purposes. */
27125 constructor_p = (!cp_parser_error_occurred (parser)
27126 && (outside_class_specifier_p
27127 || type_decl == NULL_TREE
27128 || type_decl == error_mark_node
27129 || same_type_p (current_class_type,
27130 TREE_TYPE (type_decl))));
27131
27132 /* If we're still considering a constructor, we have to see a `(',
27133 to begin the parameter-declaration-clause, followed by either a
27134 `)', an `...', or a decl-specifier. We need to check for a
27135 type-specifier to avoid being fooled into thinking that:
27136
27137 S (f) (int);
27138
27139 is a constructor. (It is actually a function named `f' that
27140 takes one parameter (of type `int') and returns a value of type
27141 `S'. */
27142 if (constructor_p
27143 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27144 constructor_p = false;
27145
27146 if (constructor_p
27147 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27148 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27149 /* A parameter declaration begins with a decl-specifier,
27150 which is either the "attribute" keyword, a storage class
27151 specifier, or (usually) a type-specifier. */
27152 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27153 {
27154 tree type;
27155 tree pushed_scope = NULL_TREE;
27156 unsigned saved_num_template_parameter_lists;
27157
27158 /* Names appearing in the type-specifier should be looked up
27159 in the scope of the class. */
27160 if (current_class_type)
27161 type = NULL_TREE;
27162 else if (type_decl)
27163 {
27164 type = TREE_TYPE (type_decl);
27165 if (TREE_CODE (type) == TYPENAME_TYPE)
27166 {
27167 type = resolve_typename_type (type,
27168 /*only_current_p=*/false);
27169 if (TREE_CODE (type) == TYPENAME_TYPE)
27170 {
27171 cp_parser_abort_tentative_parse (parser);
27172 return false;
27173 }
27174 }
27175 pushed_scope = push_scope (type);
27176 }
27177
27178 /* Inside the constructor parameter list, surrounding
27179 template-parameter-lists do not apply. */
27180 saved_num_template_parameter_lists
27181 = parser->num_template_parameter_lists;
27182 parser->num_template_parameter_lists = 0;
27183
27184 /* Look for the type-specifier. */
27185 cp_parser_type_specifier (parser,
27186 CP_PARSER_FLAGS_NONE,
27187 /*decl_specs=*/NULL,
27188 /*is_declarator=*/true,
27189 /*declares_class_or_enum=*/NULL,
27190 /*is_cv_qualifier=*/NULL);
27191
27192 parser->num_template_parameter_lists
27193 = saved_num_template_parameter_lists;
27194
27195 /* Leave the scope of the class. */
27196 if (pushed_scope)
27197 pop_scope (pushed_scope);
27198
27199 constructor_p = !cp_parser_error_occurred (parser);
27200 }
27201 }
27202
27203 /* We did not really want to consume any tokens. */
27204 cp_parser_abort_tentative_parse (parser);
27205
27206 return constructor_p;
27207 }
27208
27209 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27210 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27211 they must be performed once we are in the scope of the function.
27212
27213 Returns the function defined. */
27214
27215 static tree
27216 cp_parser_function_definition_from_specifiers_and_declarator
27217 (cp_parser* parser,
27218 cp_decl_specifier_seq *decl_specifiers,
27219 tree attributes,
27220 const cp_declarator *declarator)
27221 {
27222 tree fn;
27223 bool success_p;
27224
27225 /* Begin the function-definition. */
27226 success_p = start_function (decl_specifiers, declarator, attributes);
27227
27228 /* The things we're about to see are not directly qualified by any
27229 template headers we've seen thus far. */
27230 reset_specialization ();
27231
27232 /* If there were names looked up in the decl-specifier-seq that we
27233 did not check, check them now. We must wait until we are in the
27234 scope of the function to perform the checks, since the function
27235 might be a friend. */
27236 perform_deferred_access_checks (tf_warning_or_error);
27237
27238 if (success_p)
27239 {
27240 cp_finalize_omp_declare_simd (parser, current_function_decl);
27241 parser->omp_declare_simd = NULL;
27242 cp_finalize_oacc_routine (parser, current_function_decl, true);
27243 parser->oacc_routine = NULL;
27244 }
27245
27246 if (!success_p)
27247 {
27248 /* Skip the entire function. */
27249 cp_parser_skip_to_end_of_block_or_statement (parser);
27250 fn = error_mark_node;
27251 }
27252 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27253 {
27254 /* Seen already, skip it. An error message has already been output. */
27255 cp_parser_skip_to_end_of_block_or_statement (parser);
27256 fn = current_function_decl;
27257 current_function_decl = NULL_TREE;
27258 /* If this is a function from a class, pop the nested class. */
27259 if (current_class_name)
27260 pop_nested_class ();
27261 }
27262 else
27263 {
27264 timevar_id_t tv;
27265 if (DECL_DECLARED_INLINE_P (current_function_decl))
27266 tv = TV_PARSE_INLINE;
27267 else
27268 tv = TV_PARSE_FUNC;
27269 timevar_push (tv);
27270 fn = cp_parser_function_definition_after_declarator (parser,
27271 /*inline_p=*/false);
27272 timevar_pop (tv);
27273 }
27274
27275 return fn;
27276 }
27277
27278 /* Parse the part of a function-definition that follows the
27279 declarator. INLINE_P is TRUE iff this function is an inline
27280 function defined within a class-specifier.
27281
27282 Returns the function defined. */
27283
27284 static tree
27285 cp_parser_function_definition_after_declarator (cp_parser* parser,
27286 bool inline_p)
27287 {
27288 tree fn;
27289 bool saved_in_unbraced_linkage_specification_p;
27290 bool saved_in_function_body;
27291 unsigned saved_num_template_parameter_lists;
27292 cp_token *token;
27293 bool fully_implicit_function_template_p
27294 = parser->fully_implicit_function_template_p;
27295 parser->fully_implicit_function_template_p = false;
27296 tree implicit_template_parms
27297 = parser->implicit_template_parms;
27298 parser->implicit_template_parms = 0;
27299 cp_binding_level* implicit_template_scope
27300 = parser->implicit_template_scope;
27301 parser->implicit_template_scope = 0;
27302
27303 saved_in_function_body = parser->in_function_body;
27304 parser->in_function_body = true;
27305 /* If the next token is `return', then the code may be trying to
27306 make use of the "named return value" extension that G++ used to
27307 support. */
27308 token = cp_lexer_peek_token (parser->lexer);
27309 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27310 {
27311 /* Consume the `return' keyword. */
27312 cp_lexer_consume_token (parser->lexer);
27313 /* Look for the identifier that indicates what value is to be
27314 returned. */
27315 cp_parser_identifier (parser);
27316 /* Issue an error message. */
27317 error_at (token->location,
27318 "named return values are no longer supported");
27319 /* Skip tokens until we reach the start of the function body. */
27320 while (true)
27321 {
27322 cp_token *token = cp_lexer_peek_token (parser->lexer);
27323 if (token->type == CPP_OPEN_BRACE
27324 || token->type == CPP_EOF
27325 || token->type == CPP_PRAGMA_EOL)
27326 break;
27327 cp_lexer_consume_token (parser->lexer);
27328 }
27329 }
27330 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27331 anything declared inside `f'. */
27332 saved_in_unbraced_linkage_specification_p
27333 = parser->in_unbraced_linkage_specification_p;
27334 parser->in_unbraced_linkage_specification_p = false;
27335 /* Inside the function, surrounding template-parameter-lists do not
27336 apply. */
27337 saved_num_template_parameter_lists
27338 = parser->num_template_parameter_lists;
27339 parser->num_template_parameter_lists = 0;
27340
27341 /* If the next token is `try', `__transaction_atomic', or
27342 `__transaction_relaxed`, then we are looking at either function-try-block
27343 or function-transaction-block. Note that all of these include the
27344 function-body. */
27345 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27346 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27347 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27348 RID_TRANSACTION_RELAXED))
27349 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27350 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27351 cp_parser_function_try_block (parser);
27352 else
27353 cp_parser_ctor_initializer_opt_and_function_body
27354 (parser, /*in_function_try_block=*/false);
27355
27356 /* Finish the function. */
27357 fn = finish_function (inline_p);
27358 /* Generate code for it, if necessary. */
27359 expand_or_defer_fn (fn);
27360 /* Restore the saved values. */
27361 parser->in_unbraced_linkage_specification_p
27362 = saved_in_unbraced_linkage_specification_p;
27363 parser->num_template_parameter_lists
27364 = saved_num_template_parameter_lists;
27365 parser->in_function_body = saved_in_function_body;
27366
27367 parser->fully_implicit_function_template_p
27368 = fully_implicit_function_template_p;
27369 parser->implicit_template_parms
27370 = implicit_template_parms;
27371 parser->implicit_template_scope
27372 = implicit_template_scope;
27373
27374 if (parser->fully_implicit_function_template_p)
27375 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27376
27377 return fn;
27378 }
27379
27380 /* Parse a template-declaration body (following argument list). */
27381
27382 static void
27383 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27384 tree parameter_list,
27385 bool member_p)
27386 {
27387 tree decl = NULL_TREE;
27388 bool friend_p = false;
27389
27390 /* We just processed one more parameter list. */
27391 ++parser->num_template_parameter_lists;
27392
27393 /* Get the deferred access checks from the parameter list. These
27394 will be checked once we know what is being declared, as for a
27395 member template the checks must be performed in the scope of the
27396 class containing the member. */
27397 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27398
27399 /* Tentatively parse for a new template parameter list, which can either be
27400 the template keyword or a template introduction. */
27401 if (cp_parser_template_declaration_after_export (parser, member_p))
27402 /* OK */;
27403 else if (cxx_dialect >= cxx11
27404 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27405 decl = cp_parser_alias_declaration (parser);
27406 else
27407 {
27408 /* There are no access checks when parsing a template, as we do not
27409 know if a specialization will be a friend. */
27410 push_deferring_access_checks (dk_no_check);
27411 cp_token *token = cp_lexer_peek_token (parser->lexer);
27412 decl = cp_parser_single_declaration (parser,
27413 checks,
27414 member_p,
27415 /*explicit_specialization_p=*/false,
27416 &friend_p);
27417 pop_deferring_access_checks ();
27418
27419 /* If this is a member template declaration, let the front
27420 end know. */
27421 if (member_p && !friend_p && decl)
27422 {
27423 if (TREE_CODE (decl) == TYPE_DECL)
27424 cp_parser_check_access_in_redeclaration (decl, token->location);
27425
27426 decl = finish_member_template_decl (decl);
27427 }
27428 else if (friend_p && decl
27429 && DECL_DECLARES_TYPE_P (decl))
27430 make_friend_class (current_class_type, TREE_TYPE (decl),
27431 /*complain=*/true);
27432 }
27433 /* We are done with the current parameter list. */
27434 --parser->num_template_parameter_lists;
27435
27436 pop_deferring_access_checks ();
27437
27438 /* Finish up. */
27439 finish_template_decl (parameter_list);
27440
27441 /* Check the template arguments for a literal operator template. */
27442 if (decl
27443 && DECL_DECLARES_FUNCTION_P (decl)
27444 && UDLIT_OPER_P (DECL_NAME (decl)))
27445 {
27446 bool ok = true;
27447 if (parameter_list == NULL_TREE)
27448 ok = false;
27449 else
27450 {
27451 int num_parms = TREE_VEC_LENGTH (parameter_list);
27452 if (num_parms == 1)
27453 {
27454 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27455 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27456 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27457 /* OK, C++20 string literal operator template. We don't need
27458 to warn in lower dialects here because we will have already
27459 warned about the template parameter. */;
27460 else if (TREE_TYPE (parm) != char_type_node
27461 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27462 ok = false;
27463 }
27464 else if (num_parms == 2 && cxx_dialect >= cxx14)
27465 {
27466 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27467 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27468 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27469 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27470 if (parm == error_mark_node
27471 || TREE_TYPE (parm) != TREE_TYPE (type)
27472 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27473 ok = false;
27474 else
27475 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27476 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27477 "ISO C++ did not adopt string literal operator templa"
27478 "tes taking an argument pack of characters");
27479 }
27480 else
27481 ok = false;
27482 }
27483 if (!ok)
27484 {
27485 if (cxx_dialect > cxx17)
27486 error ("literal operator template %qD has invalid parameter list;"
27487 " Expected non-type template parameter pack <char...> "
27488 " or single non-type parameter of class type",
27489 decl);
27490 else
27491 error ("literal operator template %qD has invalid parameter list."
27492 " Expected non-type template parameter pack <char...>",
27493 decl);
27494 }
27495 }
27496
27497 /* Register member declarations. */
27498 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27499 finish_member_declaration (decl);
27500 /* If DECL is a function template, we must return to parse it later.
27501 (Even though there is no definition, there might be default
27502 arguments that need handling.) */
27503 if (member_p && decl
27504 && DECL_DECLARES_FUNCTION_P (decl))
27505 vec_safe_push (unparsed_funs_with_definitions, decl);
27506 }
27507
27508 /* Parse a template introduction header for a template-declaration. Returns
27509 false if tentative parse fails. */
27510
27511 static bool
27512 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27513 {
27514 cp_parser_parse_tentatively (parser);
27515
27516 tree saved_scope = parser->scope;
27517 tree saved_object_scope = parser->object_scope;
27518 tree saved_qualifying_scope = parser->qualifying_scope;
27519
27520 /* Look for the optional `::' operator. */
27521 cp_parser_global_scope_opt (parser,
27522 /*current_scope_valid_p=*/false);
27523 /* Look for the nested-name-specifier. */
27524 cp_parser_nested_name_specifier_opt (parser,
27525 /*typename_keyword_p=*/false,
27526 /*check_dependency_p=*/true,
27527 /*type_p=*/false,
27528 /*is_declaration=*/false);
27529
27530 cp_token *token = cp_lexer_peek_token (parser->lexer);
27531 tree concept_name = cp_parser_identifier (parser);
27532
27533 /* Look up the concept for which we will be matching
27534 template parameters. */
27535 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27536 token->location);
27537 parser->scope = saved_scope;
27538 parser->object_scope = saved_object_scope;
27539 parser->qualifying_scope = saved_qualifying_scope;
27540
27541 if (concept_name == error_mark_node)
27542 cp_parser_simulate_error (parser);
27543
27544 /* Look for opening brace for introduction. */
27545 matching_braces braces;
27546 braces.require_open (parser);
27547
27548 if (!cp_parser_parse_definitely (parser))
27549 return false;
27550
27551 push_deferring_access_checks (dk_deferred);
27552
27553 /* Build vector of placeholder parameters and grab
27554 matching identifiers. */
27555 tree introduction_list = cp_parser_introduction_list (parser);
27556
27557 /* Look for closing brace for introduction. */
27558 if (!braces.require_close (parser))
27559 return true;
27560
27561 /* The introduction-list shall not be empty. */
27562 int nargs = TREE_VEC_LENGTH (introduction_list);
27563 if (nargs == 0)
27564 {
27565 /* In cp_parser_introduction_list we have already issued an error. */
27566 return true;
27567 }
27568
27569 if (tmpl_decl == error_mark_node)
27570 {
27571 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27572 token->location);
27573 return true;
27574 }
27575
27576 /* Build and associate the constraint. */
27577 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27578 if (parms && parms != error_mark_node)
27579 {
27580 cp_parser_template_declaration_after_parameters (parser, parms,
27581 member_p);
27582 return true;
27583 }
27584
27585 error_at (token->location, "no matching concept for template-introduction");
27586 return true;
27587 }
27588
27589 /* Parse a normal template-declaration following the template keyword. */
27590
27591 static void
27592 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27593 {
27594 tree parameter_list;
27595 bool need_lang_pop;
27596 location_t location = input_location;
27597
27598 /* Look for the `<' token. */
27599 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27600 return;
27601 if (at_class_scope_p () && current_function_decl)
27602 {
27603 /* 14.5.2.2 [temp.mem]
27604
27605 A local class shall not have member templates. */
27606 error_at (location,
27607 "invalid declaration of member template in local class");
27608 cp_parser_skip_to_end_of_block_or_statement (parser);
27609 return;
27610 }
27611 /* [temp]
27612
27613 A template ... shall not have C linkage. */
27614 if (current_lang_name == lang_name_c)
27615 {
27616 error_at (location, "template with C linkage");
27617 maybe_show_extern_c_location ();
27618 /* Give it C++ linkage to avoid confusing other parts of the
27619 front end. */
27620 push_lang_context (lang_name_cplusplus);
27621 need_lang_pop = true;
27622 }
27623 else
27624 need_lang_pop = false;
27625
27626 /* We cannot perform access checks on the template parameter
27627 declarations until we know what is being declared, just as we
27628 cannot check the decl-specifier list. */
27629 push_deferring_access_checks (dk_deferred);
27630
27631 /* If the next token is `>', then we have an invalid
27632 specialization. Rather than complain about an invalid template
27633 parameter, issue an error message here. */
27634 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27635 {
27636 cp_parser_error (parser, "invalid explicit specialization");
27637 begin_specialization ();
27638 parameter_list = NULL_TREE;
27639 }
27640 else
27641 {
27642 /* Parse the template parameters. */
27643 parameter_list = cp_parser_template_parameter_list (parser);
27644 }
27645
27646 /* Look for the `>'. */
27647 cp_parser_skip_to_end_of_template_parameter_list (parser);
27648
27649 /* Manage template requirements */
27650 if (flag_concepts)
27651 {
27652 tree reqs = get_shorthand_constraints (current_template_parms);
27653 if (tree r = cp_parser_requires_clause_opt (parser))
27654 reqs = conjoin_constraints (reqs, normalize_expression (r));
27655 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27656 }
27657
27658 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27659 member_p);
27660
27661 /* For the erroneous case of a template with C linkage, we pushed an
27662 implicit C++ linkage scope; exit that scope now. */
27663 if (need_lang_pop)
27664 pop_lang_context ();
27665 }
27666
27667 /* Parse a template-declaration, assuming that the `export' (and
27668 `extern') keywords, if present, has already been scanned. MEMBER_P
27669 is as for cp_parser_template_declaration. */
27670
27671 static bool
27672 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27673 {
27674 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27675 {
27676 cp_lexer_consume_token (parser->lexer);
27677 cp_parser_explicit_template_declaration (parser, member_p);
27678 return true;
27679 }
27680 else if (flag_concepts)
27681 return cp_parser_template_introduction (parser, member_p);
27682
27683 return false;
27684 }
27685
27686 /* Perform the deferred access checks from a template-parameter-list.
27687 CHECKS is a TREE_LIST of access checks, as returned by
27688 get_deferred_access_checks. */
27689
27690 static void
27691 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27692 {
27693 ++processing_template_parmlist;
27694 perform_access_checks (checks, tf_warning_or_error);
27695 --processing_template_parmlist;
27696 }
27697
27698 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27699 `function-definition' sequence that follows a template header.
27700 If MEMBER_P is true, this declaration appears in a class scope.
27701
27702 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27703 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27704
27705 static tree
27706 cp_parser_single_declaration (cp_parser* parser,
27707 vec<deferred_access_check, va_gc> *checks,
27708 bool member_p,
27709 bool explicit_specialization_p,
27710 bool* friend_p)
27711 {
27712 int declares_class_or_enum;
27713 tree decl = NULL_TREE;
27714 cp_decl_specifier_seq decl_specifiers;
27715 bool function_definition_p = false;
27716 cp_token *decl_spec_token_start;
27717
27718 /* This function is only used when processing a template
27719 declaration. */
27720 gcc_assert (innermost_scope_kind () == sk_template_parms
27721 || innermost_scope_kind () == sk_template_spec);
27722
27723 /* Defer access checks until we know what is being declared. */
27724 push_deferring_access_checks (dk_deferred);
27725
27726 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27727 alternative. */
27728 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27729 cp_parser_decl_specifier_seq (parser,
27730 CP_PARSER_FLAGS_OPTIONAL,
27731 &decl_specifiers,
27732 &declares_class_or_enum);
27733 if (friend_p)
27734 *friend_p = cp_parser_friend_p (&decl_specifiers);
27735
27736 /* There are no template typedefs. */
27737 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27738 {
27739 error_at (decl_spec_token_start->location,
27740 "template declaration of %<typedef%>");
27741 decl = error_mark_node;
27742 }
27743
27744 /* Gather up the access checks that occurred the
27745 decl-specifier-seq. */
27746 stop_deferring_access_checks ();
27747
27748 /* Check for the declaration of a template class. */
27749 if (declares_class_or_enum)
27750 {
27751 if (cp_parser_declares_only_class_p (parser)
27752 || (declares_class_or_enum & 2))
27753 {
27754 // If this is a declaration, but not a definition, associate
27755 // any constraints with the type declaration. Constraints
27756 // are associated with definitions in cp_parser_class_specifier.
27757 if (declares_class_or_enum == 1)
27758 associate_classtype_constraints (decl_specifiers.type);
27759
27760 decl = shadow_tag (&decl_specifiers);
27761
27762 /* In this case:
27763
27764 struct C {
27765 friend template <typename T> struct A<T>::B;
27766 };
27767
27768 A<T>::B will be represented by a TYPENAME_TYPE, and
27769 therefore not recognized by shadow_tag. */
27770 if (friend_p && *friend_p
27771 && !decl
27772 && decl_specifiers.type
27773 && TYPE_P (decl_specifiers.type))
27774 decl = decl_specifiers.type;
27775
27776 if (decl && decl != error_mark_node)
27777 decl = TYPE_NAME (decl);
27778 else
27779 decl = error_mark_node;
27780
27781 /* Perform access checks for template parameters. */
27782 cp_parser_perform_template_parameter_access_checks (checks);
27783
27784 /* Give a helpful diagnostic for
27785 template <class T> struct A { } a;
27786 if we aren't already recovering from an error. */
27787 if (!cp_parser_declares_only_class_p (parser)
27788 && !seen_error ())
27789 {
27790 error_at (cp_lexer_peek_token (parser->lexer)->location,
27791 "a class template declaration must not declare "
27792 "anything else");
27793 cp_parser_skip_to_end_of_block_or_statement (parser);
27794 goto out;
27795 }
27796 }
27797 }
27798
27799 /* Complain about missing 'typename' or other invalid type names. */
27800 if (!decl_specifiers.any_type_specifiers_p
27801 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27802 {
27803 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27804 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27805 the rest of this declaration. */
27806 decl = error_mark_node;
27807 goto out;
27808 }
27809
27810 /* If it's not a template class, try for a template function. If
27811 the next token is a `;', then this declaration does not declare
27812 anything. But, if there were errors in the decl-specifiers, then
27813 the error might well have come from an attempted class-specifier.
27814 In that case, there's no need to warn about a missing declarator. */
27815 if (!decl
27816 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27817 || decl_specifiers.type != error_mark_node))
27818 {
27819 decl = cp_parser_init_declarator (parser,
27820 &decl_specifiers,
27821 checks,
27822 /*function_definition_allowed_p=*/true,
27823 member_p,
27824 declares_class_or_enum,
27825 &function_definition_p,
27826 NULL, NULL, NULL);
27827
27828 /* 7.1.1-1 [dcl.stc]
27829
27830 A storage-class-specifier shall not be specified in an explicit
27831 specialization... */
27832 if (decl
27833 && explicit_specialization_p
27834 && decl_specifiers.storage_class != sc_none)
27835 {
27836 error_at (decl_spec_token_start->location,
27837 "explicit template specialization cannot have a storage class");
27838 decl = error_mark_node;
27839 }
27840
27841 if (decl && VAR_P (decl))
27842 check_template_variable (decl);
27843 }
27844
27845 /* Look for a trailing `;' after the declaration. */
27846 if (!function_definition_p
27847 && (decl == error_mark_node
27848 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27849 cp_parser_skip_to_end_of_block_or_statement (parser);
27850
27851 out:
27852 pop_deferring_access_checks ();
27853
27854 /* Clear any current qualification; whatever comes next is the start
27855 of something new. */
27856 parser->scope = NULL_TREE;
27857 parser->qualifying_scope = NULL_TREE;
27858 parser->object_scope = NULL_TREE;
27859
27860 return decl;
27861 }
27862
27863 /* Parse a cast-expression that is not the operand of a unary "&". */
27864
27865 static cp_expr
27866 cp_parser_simple_cast_expression (cp_parser *parser)
27867 {
27868 return cp_parser_cast_expression (parser, /*address_p=*/false,
27869 /*cast_p=*/false, /*decltype*/false, NULL);
27870 }
27871
27872 /* Parse a functional cast to TYPE. Returns an expression
27873 representing the cast. */
27874
27875 static cp_expr
27876 cp_parser_functional_cast (cp_parser* parser, tree type)
27877 {
27878 vec<tree, va_gc> *vec;
27879 tree expression_list;
27880 cp_expr cast;
27881 bool nonconst_p;
27882
27883 location_t start_loc = input_location;
27884
27885 if (!type)
27886 type = error_mark_node;
27887
27888 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27889 {
27890 cp_lexer_set_source_position (parser->lexer);
27891 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27892 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27893 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27894 if (TREE_CODE (type) == TYPE_DECL)
27895 type = TREE_TYPE (type);
27896
27897 cast = finish_compound_literal (type, expression_list,
27898 tf_warning_or_error, fcl_functional);
27899 /* Create a location of the form:
27900 type_name{i, f}
27901 ^~~~~~~~~~~~~~~
27902 with caret == start at the start of the type name,
27903 finishing at the closing brace. */
27904 location_t finish_loc
27905 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27906 location_t combined_loc = make_location (start_loc, start_loc,
27907 finish_loc);
27908 cast.set_location (combined_loc);
27909 return cast;
27910 }
27911
27912
27913 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27914 /*cast_p=*/true,
27915 /*allow_expansion_p=*/true,
27916 /*non_constant_p=*/NULL);
27917 if (vec == NULL)
27918 expression_list = error_mark_node;
27919 else
27920 {
27921 expression_list = build_tree_list_vec (vec);
27922 release_tree_vector (vec);
27923 }
27924
27925 cast = build_functional_cast (type, expression_list,
27926 tf_warning_or_error);
27927 /* [expr.const]/1: In an integral constant expression "only type
27928 conversions to integral or enumeration type can be used". */
27929 if (TREE_CODE (type) == TYPE_DECL)
27930 type = TREE_TYPE (type);
27931 if (cast != error_mark_node
27932 && !cast_valid_in_integral_constant_expression_p (type)
27933 && cp_parser_non_integral_constant_expression (parser,
27934 NIC_CONSTRUCTOR))
27935 return error_mark_node;
27936
27937 /* Create a location of the form:
27938 float(i)
27939 ^~~~~~~~
27940 with caret == start at the start of the type name,
27941 finishing at the closing paren. */
27942 location_t finish_loc
27943 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27944 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27945 cast.set_location (combined_loc);
27946 return cast;
27947 }
27948
27949 /* Save the tokens that make up the body of a member function defined
27950 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27951 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27952 specifiers applied to the declaration. Returns the FUNCTION_DECL
27953 for the member function. */
27954
27955 static tree
27956 cp_parser_save_member_function_body (cp_parser* parser,
27957 cp_decl_specifier_seq *decl_specifiers,
27958 cp_declarator *declarator,
27959 tree attributes)
27960 {
27961 cp_token *first;
27962 cp_token *last;
27963 tree fn;
27964 bool function_try_block = false;
27965
27966 /* Create the FUNCTION_DECL. */
27967 fn = grokmethod (decl_specifiers, declarator, attributes);
27968 cp_finalize_omp_declare_simd (parser, fn);
27969 cp_finalize_oacc_routine (parser, fn, true);
27970 /* If something went badly wrong, bail out now. */
27971 if (fn == error_mark_node)
27972 {
27973 /* If there's a function-body, skip it. */
27974 if (cp_parser_token_starts_function_definition_p
27975 (cp_lexer_peek_token (parser->lexer)))
27976 cp_parser_skip_to_end_of_block_or_statement (parser);
27977 return error_mark_node;
27978 }
27979
27980 /* Remember it, if there default args to post process. */
27981 cp_parser_save_default_args (parser, fn);
27982
27983 /* Save away the tokens that make up the body of the
27984 function. */
27985 first = parser->lexer->next_token;
27986
27987 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27988 cp_lexer_consume_token (parser->lexer);
27989 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27990 RID_TRANSACTION_ATOMIC))
27991 {
27992 cp_lexer_consume_token (parser->lexer);
27993 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27995 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27996 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27997 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27998 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27999 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28000 {
28001 cp_lexer_consume_token (parser->lexer);
28002 cp_lexer_consume_token (parser->lexer);
28003 cp_lexer_consume_token (parser->lexer);
28004 cp_lexer_consume_token (parser->lexer);
28005 cp_lexer_consume_token (parser->lexer);
28006 }
28007 else
28008 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28009 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28010 {
28011 cp_lexer_consume_token (parser->lexer);
28012 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28013 break;
28014 }
28015 }
28016
28017 /* Handle function try blocks. */
28018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28019 {
28020 cp_lexer_consume_token (parser->lexer);
28021 function_try_block = true;
28022 }
28023 /* We can have braced-init-list mem-initializers before the fn body. */
28024 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28025 {
28026 cp_lexer_consume_token (parser->lexer);
28027 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28028 {
28029 /* cache_group will stop after an un-nested { } pair, too. */
28030 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28031 break;
28032
28033 /* variadic mem-inits have ... after the ')'. */
28034 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28035 cp_lexer_consume_token (parser->lexer);
28036 }
28037 }
28038 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28039 /* Handle function try blocks. */
28040 if (function_try_block)
28041 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28042 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28043 last = parser->lexer->next_token;
28044
28045 /* Save away the inline definition; we will process it when the
28046 class is complete. */
28047 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28048 DECL_PENDING_INLINE_P (fn) = 1;
28049
28050 /* We need to know that this was defined in the class, so that
28051 friend templates are handled correctly. */
28052 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28053
28054 /* Add FN to the queue of functions to be parsed later. */
28055 vec_safe_push (unparsed_funs_with_definitions, fn);
28056
28057 return fn;
28058 }
28059
28060 /* Save the tokens that make up the in-class initializer for a non-static
28061 data member. Returns a DEFAULT_ARG. */
28062
28063 static tree
28064 cp_parser_save_nsdmi (cp_parser* parser)
28065 {
28066 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28067 }
28068
28069 /* Parse a template-argument-list, as well as the trailing ">" (but
28070 not the opening "<"). See cp_parser_template_argument_list for the
28071 return value. */
28072
28073 static tree
28074 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28075 {
28076 tree arguments;
28077 tree saved_scope;
28078 tree saved_qualifying_scope;
28079 tree saved_object_scope;
28080 bool saved_greater_than_is_operator_p;
28081
28082 /* [temp.names]
28083
28084 When parsing a template-id, the first non-nested `>' is taken as
28085 the end of the template-argument-list rather than a greater-than
28086 operator. */
28087 saved_greater_than_is_operator_p
28088 = parser->greater_than_is_operator_p;
28089 parser->greater_than_is_operator_p = false;
28090 /* Parsing the argument list may modify SCOPE, so we save it
28091 here. */
28092 saved_scope = parser->scope;
28093 saved_qualifying_scope = parser->qualifying_scope;
28094 saved_object_scope = parser->object_scope;
28095 /* We need to evaluate the template arguments, even though this
28096 template-id may be nested within a "sizeof". */
28097 cp_evaluated ev;
28098 /* Parse the template-argument-list itself. */
28099 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28100 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28101 arguments = NULL_TREE;
28102 else
28103 arguments = cp_parser_template_argument_list (parser);
28104 /* Look for the `>' that ends the template-argument-list. If we find
28105 a '>>' instead, it's probably just a typo. */
28106 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28107 {
28108 if (cxx_dialect != cxx98)
28109 {
28110 /* In C++0x, a `>>' in a template argument list or cast
28111 expression is considered to be two separate `>'
28112 tokens. So, change the current token to a `>', but don't
28113 consume it: it will be consumed later when the outer
28114 template argument list (or cast expression) is parsed.
28115 Note that this replacement of `>' for `>>' is necessary
28116 even if we are parsing tentatively: in the tentative
28117 case, after calling
28118 cp_parser_enclosed_template_argument_list we will always
28119 throw away all of the template arguments and the first
28120 closing `>', either because the template argument list
28121 was erroneous or because we are replacing those tokens
28122 with a CPP_TEMPLATE_ID token. The second `>' (which will
28123 not have been thrown away) is needed either to close an
28124 outer template argument list or to complete a new-style
28125 cast. */
28126 cp_token *token = cp_lexer_peek_token (parser->lexer);
28127 token->type = CPP_GREATER;
28128 }
28129 else if (!saved_greater_than_is_operator_p)
28130 {
28131 /* If we're in a nested template argument list, the '>>' has
28132 to be a typo for '> >'. We emit the error message, but we
28133 continue parsing and we push a '>' as next token, so that
28134 the argument list will be parsed correctly. Note that the
28135 global source location is still on the token before the
28136 '>>', so we need to say explicitly where we want it. */
28137 cp_token *token = cp_lexer_peek_token (parser->lexer);
28138 gcc_rich_location richloc (token->location);
28139 richloc.add_fixit_replace ("> >");
28140 error_at (&richloc, "%<>>%> should be %<> >%> "
28141 "within a nested template argument list");
28142
28143 token->type = CPP_GREATER;
28144 }
28145 else
28146 {
28147 /* If this is not a nested template argument list, the '>>'
28148 is a typo for '>'. Emit an error message and continue.
28149 Same deal about the token location, but here we can get it
28150 right by consuming the '>>' before issuing the diagnostic. */
28151 cp_token *token = cp_lexer_consume_token (parser->lexer);
28152 error_at (token->location,
28153 "spurious %<>>%>, use %<>%> to terminate "
28154 "a template argument list");
28155 }
28156 }
28157 else
28158 cp_parser_skip_to_end_of_template_parameter_list (parser);
28159 /* The `>' token might be a greater-than operator again now. */
28160 parser->greater_than_is_operator_p
28161 = saved_greater_than_is_operator_p;
28162 /* Restore the SAVED_SCOPE. */
28163 parser->scope = saved_scope;
28164 parser->qualifying_scope = saved_qualifying_scope;
28165 parser->object_scope = saved_object_scope;
28166
28167 return arguments;
28168 }
28169
28170 /* MEMBER_FUNCTION is a member function, or a friend. If default
28171 arguments, or the body of the function have not yet been parsed,
28172 parse them now. */
28173
28174 static void
28175 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28176 {
28177 timevar_push (TV_PARSE_INMETH);
28178 /* If this member is a template, get the underlying
28179 FUNCTION_DECL. */
28180 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28181 member_function = DECL_TEMPLATE_RESULT (member_function);
28182
28183 /* There should not be any class definitions in progress at this
28184 point; the bodies of members are only parsed outside of all class
28185 definitions. */
28186 gcc_assert (parser->num_classes_being_defined == 0);
28187 /* While we're parsing the member functions we might encounter more
28188 classes. We want to handle them right away, but we don't want
28189 them getting mixed up with functions that are currently in the
28190 queue. */
28191 push_unparsed_function_queues (parser);
28192
28193 /* Make sure that any template parameters are in scope. */
28194 maybe_begin_member_template_processing (member_function);
28195
28196 /* If the body of the function has not yet been parsed, parse it
28197 now. */
28198 if (DECL_PENDING_INLINE_P (member_function))
28199 {
28200 tree function_scope;
28201 cp_token_cache *tokens;
28202
28203 /* The function is no longer pending; we are processing it. */
28204 tokens = DECL_PENDING_INLINE_INFO (member_function);
28205 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28206 DECL_PENDING_INLINE_P (member_function) = 0;
28207
28208 /* If this is a local class, enter the scope of the containing
28209 function. */
28210 function_scope = current_function_decl;
28211 if (function_scope)
28212 push_function_context ();
28213
28214 /* Push the body of the function onto the lexer stack. */
28215 cp_parser_push_lexer_for_tokens (parser, tokens);
28216
28217 /* Let the front end know that we going to be defining this
28218 function. */
28219 start_preparsed_function (member_function, NULL_TREE,
28220 SF_PRE_PARSED | SF_INCLASS_INLINE);
28221
28222 /* Don't do access checking if it is a templated function. */
28223 if (processing_template_decl)
28224 push_deferring_access_checks (dk_no_check);
28225
28226 /* #pragma omp declare reduction needs special parsing. */
28227 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28228 {
28229 parser->lexer->in_pragma = true;
28230 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28231 finish_function (/*inline_p=*/true);
28232 cp_check_omp_declare_reduction (member_function);
28233 }
28234 else
28235 /* Now, parse the body of the function. */
28236 cp_parser_function_definition_after_declarator (parser,
28237 /*inline_p=*/true);
28238
28239 if (processing_template_decl)
28240 pop_deferring_access_checks ();
28241
28242 /* Leave the scope of the containing function. */
28243 if (function_scope)
28244 pop_function_context ();
28245 cp_parser_pop_lexer (parser);
28246 }
28247
28248 /* Remove any template parameters from the symbol table. */
28249 maybe_end_member_template_processing ();
28250
28251 /* Restore the queue. */
28252 pop_unparsed_function_queues (parser);
28253 timevar_pop (TV_PARSE_INMETH);
28254 }
28255
28256 /* If DECL contains any default args, remember it on the unparsed
28257 functions queue. */
28258
28259 static void
28260 cp_parser_save_default_args (cp_parser* parser, tree decl)
28261 {
28262 tree probe;
28263
28264 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28265 probe;
28266 probe = TREE_CHAIN (probe))
28267 if (TREE_PURPOSE (probe))
28268 {
28269 cp_default_arg_entry entry = {current_class_type, decl};
28270 vec_safe_push (unparsed_funs_with_default_args, entry);
28271 break;
28272 }
28273 }
28274
28275 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28276 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28277 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28278 from the parameter-type-list. */
28279
28280 static tree
28281 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28282 tree default_arg, tree parmtype)
28283 {
28284 cp_token_cache *tokens;
28285 tree parsed_arg;
28286 bool dummy;
28287
28288 if (default_arg == error_mark_node)
28289 return error_mark_node;
28290
28291 /* Push the saved tokens for the default argument onto the parser's
28292 lexer stack. */
28293 tokens = DEFARG_TOKENS (default_arg);
28294 cp_parser_push_lexer_for_tokens (parser, tokens);
28295
28296 start_lambda_scope (decl);
28297
28298 /* Parse the default argument. */
28299 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28300 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28301 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28302
28303 finish_lambda_scope ();
28304
28305 if (parsed_arg == error_mark_node)
28306 cp_parser_skip_to_end_of_statement (parser);
28307
28308 if (!processing_template_decl)
28309 {
28310 /* In a non-template class, check conversions now. In a template,
28311 we'll wait and instantiate these as needed. */
28312 if (TREE_CODE (decl) == PARM_DECL)
28313 parsed_arg = check_default_argument (parmtype, parsed_arg,
28314 tf_warning_or_error);
28315 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28316 parsed_arg = error_mark_node;
28317 else
28318 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28319 }
28320
28321 /* If the token stream has not been completely used up, then
28322 there was extra junk after the end of the default
28323 argument. */
28324 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28325 {
28326 if (TREE_CODE (decl) == PARM_DECL)
28327 cp_parser_error (parser, "expected %<,%>");
28328 else
28329 cp_parser_error (parser, "expected %<;%>");
28330 }
28331
28332 /* Revert to the main lexer. */
28333 cp_parser_pop_lexer (parser);
28334
28335 return parsed_arg;
28336 }
28337
28338 /* FIELD is a non-static data member with an initializer which we saved for
28339 later; parse it now. */
28340
28341 static void
28342 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28343 {
28344 tree def;
28345
28346 maybe_begin_member_template_processing (field);
28347
28348 push_unparsed_function_queues (parser);
28349 def = cp_parser_late_parse_one_default_arg (parser, field,
28350 DECL_INITIAL (field),
28351 NULL_TREE);
28352 pop_unparsed_function_queues (parser);
28353
28354 maybe_end_member_template_processing ();
28355
28356 DECL_INITIAL (field) = def;
28357 }
28358
28359 /* FN is a FUNCTION_DECL which may contains a parameter with an
28360 unparsed DEFAULT_ARG. Parse the default args now. This function
28361 assumes that the current scope is the scope in which the default
28362 argument should be processed. */
28363
28364 static void
28365 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28366 {
28367 bool saved_local_variables_forbidden_p;
28368 tree parm, parmdecl;
28369
28370 /* While we're parsing the default args, we might (due to the
28371 statement expression extension) encounter more classes. We want
28372 to handle them right away, but we don't want them getting mixed
28373 up with default args that are currently in the queue. */
28374 push_unparsed_function_queues (parser);
28375
28376 /* Local variable names (and the `this' keyword) may not appear
28377 in a default argument. */
28378 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28379 parser->local_variables_forbidden_p = true;
28380
28381 push_defarg_context (fn);
28382
28383 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28384 parmdecl = DECL_ARGUMENTS (fn);
28385 parm && parm != void_list_node;
28386 parm = TREE_CHAIN (parm),
28387 parmdecl = DECL_CHAIN (parmdecl))
28388 {
28389 tree default_arg = TREE_PURPOSE (parm);
28390 tree parsed_arg;
28391 vec<tree, va_gc> *insts;
28392 tree copy;
28393 unsigned ix;
28394
28395 if (!default_arg)
28396 continue;
28397
28398 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28399 /* This can happen for a friend declaration for a function
28400 already declared with default arguments. */
28401 continue;
28402
28403 parsed_arg
28404 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28405 default_arg,
28406 TREE_VALUE (parm));
28407 TREE_PURPOSE (parm) = parsed_arg;
28408
28409 /* Update any instantiations we've already created. */
28410 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28411 vec_safe_iterate (insts, ix, &copy); ix++)
28412 TREE_PURPOSE (copy) = parsed_arg;
28413 }
28414
28415 pop_defarg_context ();
28416
28417 /* Make sure no default arg is missing. */
28418 check_default_args (fn);
28419
28420 /* Restore the state of local_variables_forbidden_p. */
28421 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28422
28423 /* Restore the queue. */
28424 pop_unparsed_function_queues (parser);
28425 }
28426
28427 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28428
28429 sizeof ... ( identifier )
28430
28431 where the 'sizeof' token has already been consumed. */
28432
28433 static tree
28434 cp_parser_sizeof_pack (cp_parser *parser)
28435 {
28436 /* Consume the `...'. */
28437 cp_lexer_consume_token (parser->lexer);
28438 maybe_warn_variadic_templates ();
28439
28440 matching_parens parens;
28441 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28442 if (paren)
28443 parens.consume_open (parser);
28444 else
28445 permerror (cp_lexer_peek_token (parser->lexer)->location,
28446 "%<sizeof...%> argument must be surrounded by parentheses");
28447
28448 cp_token *token = cp_lexer_peek_token (parser->lexer);
28449 tree name = cp_parser_identifier (parser);
28450 if (name == error_mark_node)
28451 return error_mark_node;
28452 /* The name is not qualified. */
28453 parser->scope = NULL_TREE;
28454 parser->qualifying_scope = NULL_TREE;
28455 parser->object_scope = NULL_TREE;
28456 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28457 if (expr == error_mark_node)
28458 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28459 token->location);
28460 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28461 expr = TREE_TYPE (expr);
28462 else if (TREE_CODE (expr) == CONST_DECL)
28463 expr = DECL_INITIAL (expr);
28464 expr = make_pack_expansion (expr);
28465 PACK_EXPANSION_SIZEOF_P (expr) = true;
28466
28467 if (paren)
28468 parens.require_close (parser);
28469
28470 return expr;
28471 }
28472
28473 /* Parse the operand of `sizeof' (or a similar operator). Returns
28474 either a TYPE or an expression, depending on the form of the
28475 input. The KEYWORD indicates which kind of expression we have
28476 encountered. */
28477
28478 static tree
28479 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28480 {
28481 tree expr = NULL_TREE;
28482 const char *saved_message;
28483 char *tmp;
28484 bool saved_integral_constant_expression_p;
28485 bool saved_non_integral_constant_expression_p;
28486
28487 /* If it's a `...', then we are computing the length of a parameter
28488 pack. */
28489 if (keyword == RID_SIZEOF
28490 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28491 return cp_parser_sizeof_pack (parser);
28492
28493 /* Types cannot be defined in a `sizeof' expression. Save away the
28494 old message. */
28495 saved_message = parser->type_definition_forbidden_message;
28496 /* And create the new one. */
28497 tmp = concat ("types may not be defined in %<",
28498 IDENTIFIER_POINTER (ridpointers[keyword]),
28499 "%> expressions", NULL);
28500 parser->type_definition_forbidden_message = tmp;
28501
28502 /* The restrictions on constant-expressions do not apply inside
28503 sizeof expressions. */
28504 saved_integral_constant_expression_p
28505 = parser->integral_constant_expression_p;
28506 saved_non_integral_constant_expression_p
28507 = parser->non_integral_constant_expression_p;
28508 parser->integral_constant_expression_p = false;
28509
28510 /* Do not actually evaluate the expression. */
28511 ++cp_unevaluated_operand;
28512 ++c_inhibit_evaluation_warnings;
28513 /* If it's a `(', then we might be looking at the type-id
28514 construction. */
28515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28516 {
28517 tree type = NULL_TREE;
28518
28519 /* We can't be sure yet whether we're looking at a type-id or an
28520 expression. */
28521 cp_parser_parse_tentatively (parser);
28522
28523 matching_parens parens;
28524 parens.consume_open (parser);
28525
28526 /* Note: as a GNU Extension, compound literals are considered
28527 postfix-expressions as they are in C99, so they are valid
28528 arguments to sizeof. See comment in cp_parser_cast_expression
28529 for details. */
28530 if (cp_parser_compound_literal_p (parser))
28531 cp_parser_simulate_error (parser);
28532 else
28533 {
28534 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28535 parser->in_type_id_in_expr_p = true;
28536 /* Look for the type-id. */
28537 type = cp_parser_type_id (parser);
28538 /* Look for the closing `)'. */
28539 parens.require_close (parser);
28540 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28541 }
28542
28543 /* If all went well, then we're done. */
28544 if (cp_parser_parse_definitely (parser))
28545 expr = type;
28546 }
28547
28548 /* If the type-id production did not work out, then we must be
28549 looking at the unary-expression production. */
28550 if (!expr)
28551 expr = cp_parser_unary_expression (parser);
28552
28553 /* Go back to evaluating expressions. */
28554 --cp_unevaluated_operand;
28555 --c_inhibit_evaluation_warnings;
28556
28557 /* Free the message we created. */
28558 free (tmp);
28559 /* And restore the old one. */
28560 parser->type_definition_forbidden_message = saved_message;
28561 parser->integral_constant_expression_p
28562 = saved_integral_constant_expression_p;
28563 parser->non_integral_constant_expression_p
28564 = saved_non_integral_constant_expression_p;
28565
28566 return expr;
28567 }
28568
28569 /* If the current declaration has no declarator, return true. */
28570
28571 static bool
28572 cp_parser_declares_only_class_p (cp_parser *parser)
28573 {
28574 /* If the next token is a `;' or a `,' then there is no
28575 declarator. */
28576 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28577 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28578 }
28579
28580 /* Update the DECL_SPECS to reflect the storage class indicated by
28581 KEYWORD. */
28582
28583 static void
28584 cp_parser_set_storage_class (cp_parser *parser,
28585 cp_decl_specifier_seq *decl_specs,
28586 enum rid keyword,
28587 cp_token *token)
28588 {
28589 cp_storage_class storage_class;
28590
28591 if (parser->in_unbraced_linkage_specification_p)
28592 {
28593 error_at (token->location, "invalid use of %qD in linkage specification",
28594 ridpointers[keyword]);
28595 return;
28596 }
28597 else if (decl_specs->storage_class != sc_none)
28598 {
28599 decl_specs->conflicting_specifiers_p = true;
28600 return;
28601 }
28602
28603 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28604 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28605 && decl_specs->gnu_thread_keyword_p)
28606 {
28607 pedwarn (decl_specs->locations[ds_thread], 0,
28608 "%<__thread%> before %qD", ridpointers[keyword]);
28609 }
28610
28611 switch (keyword)
28612 {
28613 case RID_AUTO:
28614 storage_class = sc_auto;
28615 break;
28616 case RID_REGISTER:
28617 storage_class = sc_register;
28618 break;
28619 case RID_STATIC:
28620 storage_class = sc_static;
28621 break;
28622 case RID_EXTERN:
28623 storage_class = sc_extern;
28624 break;
28625 case RID_MUTABLE:
28626 storage_class = sc_mutable;
28627 break;
28628 default:
28629 gcc_unreachable ();
28630 }
28631 decl_specs->storage_class = storage_class;
28632 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28633
28634 /* A storage class specifier cannot be applied alongside a typedef
28635 specifier. If there is a typedef specifier present then set
28636 conflicting_specifiers_p which will trigger an error later
28637 on in grokdeclarator. */
28638 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28639 decl_specs->conflicting_specifiers_p = true;
28640 }
28641
28642 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28643 is true, the type is a class or enum definition. */
28644
28645 static void
28646 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28647 tree type_spec,
28648 cp_token *token,
28649 bool type_definition_p)
28650 {
28651 decl_specs->any_specifiers_p = true;
28652
28653 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28654 (with, for example, in "typedef int wchar_t;") we remember that
28655 this is what happened. In system headers, we ignore these
28656 declarations so that G++ can work with system headers that are not
28657 C++-safe. */
28658 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28659 && !type_definition_p
28660 && (type_spec == boolean_type_node
28661 || type_spec == char16_type_node
28662 || type_spec == char32_type_node
28663 || type_spec == wchar_type_node)
28664 && (decl_specs->type
28665 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28666 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28667 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28668 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28669 {
28670 decl_specs->redefined_builtin_type = type_spec;
28671 set_and_check_decl_spec_loc (decl_specs,
28672 ds_redefined_builtin_type_spec,
28673 token);
28674 if (!decl_specs->type)
28675 {
28676 decl_specs->type = type_spec;
28677 decl_specs->type_definition_p = false;
28678 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28679 }
28680 }
28681 else if (decl_specs->type)
28682 decl_specs->multiple_types_p = true;
28683 else
28684 {
28685 decl_specs->type = type_spec;
28686 decl_specs->type_definition_p = type_definition_p;
28687 decl_specs->redefined_builtin_type = NULL_TREE;
28688 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28689 }
28690 }
28691
28692 /* True iff TOKEN is the GNU keyword __thread. */
28693
28694 static bool
28695 token_is__thread (cp_token *token)
28696 {
28697 gcc_assert (token->keyword == RID_THREAD);
28698 return id_equal (token->u.value, "__thread");
28699 }
28700
28701 /* Set the location for a declarator specifier and check if it is
28702 duplicated.
28703
28704 DECL_SPECS is the sequence of declarator specifiers onto which to
28705 set the location.
28706
28707 DS is the single declarator specifier to set which location is to
28708 be set onto the existing sequence of declarators.
28709
28710 LOCATION is the location for the declarator specifier to
28711 consider. */
28712
28713 static void
28714 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28715 cp_decl_spec ds, cp_token *token)
28716 {
28717 gcc_assert (ds < ds_last);
28718
28719 if (decl_specs == NULL)
28720 return;
28721
28722 location_t location = token->location;
28723
28724 if (decl_specs->locations[ds] == 0)
28725 {
28726 decl_specs->locations[ds] = location;
28727 if (ds == ds_thread)
28728 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28729 }
28730 else
28731 {
28732 if (ds == ds_long)
28733 {
28734 if (decl_specs->locations[ds_long_long] != 0)
28735 error_at (location,
28736 "%<long long long%> is too long for GCC");
28737 else
28738 {
28739 decl_specs->locations[ds_long_long] = location;
28740 pedwarn_cxx98 (location,
28741 OPT_Wlong_long,
28742 "ISO C++ 1998 does not support %<long long%>");
28743 }
28744 }
28745 else if (ds == ds_thread)
28746 {
28747 bool gnu = token_is__thread (token);
28748 gcc_rich_location richloc (location);
28749 if (gnu != decl_specs->gnu_thread_keyword_p)
28750 {
28751 richloc.add_range (decl_specs->locations[ds_thread]);
28752 error_at (&richloc,
28753 "both %<__thread%> and %<thread_local%> specified");
28754 }
28755 else
28756 {
28757 richloc.add_fixit_remove ();
28758 error_at (&richloc, "duplicate %qD", token->u.value);
28759 }
28760 }
28761 else
28762 {
28763 static const char *const decl_spec_names[] = {
28764 "signed",
28765 "unsigned",
28766 "short",
28767 "long",
28768 "const",
28769 "volatile",
28770 "restrict",
28771 "inline",
28772 "virtual",
28773 "explicit",
28774 "friend",
28775 "typedef",
28776 "using",
28777 "constexpr",
28778 "__complex"
28779 };
28780 gcc_rich_location richloc (location);
28781 richloc.add_fixit_remove ();
28782 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28783 }
28784 }
28785 }
28786
28787 /* Return true iff the declarator specifier DS is present in the
28788 sequence of declarator specifiers DECL_SPECS. */
28789
28790 bool
28791 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28792 cp_decl_spec ds)
28793 {
28794 gcc_assert (ds < ds_last);
28795
28796 if (decl_specs == NULL)
28797 return false;
28798
28799 return decl_specs->locations[ds] != 0;
28800 }
28801
28802 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28803 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28804
28805 static bool
28806 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28807 {
28808 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28809 }
28810
28811 /* Issue an error message indicating that TOKEN_DESC was expected.
28812 If KEYWORD is true, it indicated this function is called by
28813 cp_parser_require_keword and the required token can only be
28814 a indicated keyword.
28815
28816 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28817 within any error as the location of an "opening" token matching
28818 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28819 RT_CLOSE_PAREN). */
28820
28821 static void
28822 cp_parser_required_error (cp_parser *parser,
28823 required_token token_desc,
28824 bool keyword,
28825 location_t matching_location)
28826 {
28827 if (cp_parser_simulate_error (parser))
28828 return;
28829
28830 const char *gmsgid = NULL;
28831 switch (token_desc)
28832 {
28833 case RT_NEW:
28834 gmsgid = G_("expected %<new%>");
28835 break;
28836 case RT_DELETE:
28837 gmsgid = G_("expected %<delete%>");
28838 break;
28839 case RT_RETURN:
28840 gmsgid = G_("expected %<return%>");
28841 break;
28842 case RT_WHILE:
28843 gmsgid = G_("expected %<while%>");
28844 break;
28845 case RT_EXTERN:
28846 gmsgid = G_("expected %<extern%>");
28847 break;
28848 case RT_STATIC_ASSERT:
28849 gmsgid = G_("expected %<static_assert%>");
28850 break;
28851 case RT_DECLTYPE:
28852 gmsgid = G_("expected %<decltype%>");
28853 break;
28854 case RT_OPERATOR:
28855 gmsgid = G_("expected %<operator%>");
28856 break;
28857 case RT_CLASS:
28858 gmsgid = G_("expected %<class%>");
28859 break;
28860 case RT_TEMPLATE:
28861 gmsgid = G_("expected %<template%>");
28862 break;
28863 case RT_NAMESPACE:
28864 gmsgid = G_("expected %<namespace%>");
28865 break;
28866 case RT_USING:
28867 gmsgid = G_("expected %<using%>");
28868 break;
28869 case RT_ASM:
28870 gmsgid = G_("expected %<asm%>");
28871 break;
28872 case RT_TRY:
28873 gmsgid = G_("expected %<try%>");
28874 break;
28875 case RT_CATCH:
28876 gmsgid = G_("expected %<catch%>");
28877 break;
28878 case RT_THROW:
28879 gmsgid = G_("expected %<throw%>");
28880 break;
28881 case RT_LABEL:
28882 gmsgid = G_("expected %<__label__%>");
28883 break;
28884 case RT_AT_TRY:
28885 gmsgid = G_("expected %<@try%>");
28886 break;
28887 case RT_AT_SYNCHRONIZED:
28888 gmsgid = G_("expected %<@synchronized%>");
28889 break;
28890 case RT_AT_THROW:
28891 gmsgid = G_("expected %<@throw%>");
28892 break;
28893 case RT_TRANSACTION_ATOMIC:
28894 gmsgid = G_("expected %<__transaction_atomic%>");
28895 break;
28896 case RT_TRANSACTION_RELAXED:
28897 gmsgid = G_("expected %<__transaction_relaxed%>");
28898 break;
28899 default:
28900 break;
28901 }
28902
28903 if (!gmsgid && !keyword)
28904 {
28905 switch (token_desc)
28906 {
28907 case RT_SEMICOLON:
28908 gmsgid = G_("expected %<;%>");
28909 break;
28910 case RT_OPEN_PAREN:
28911 gmsgid = G_("expected %<(%>");
28912 break;
28913 case RT_CLOSE_BRACE:
28914 gmsgid = G_("expected %<}%>");
28915 break;
28916 case RT_OPEN_BRACE:
28917 gmsgid = G_("expected %<{%>");
28918 break;
28919 case RT_CLOSE_SQUARE:
28920 gmsgid = G_("expected %<]%>");
28921 break;
28922 case RT_OPEN_SQUARE:
28923 gmsgid = G_("expected %<[%>");
28924 break;
28925 case RT_COMMA:
28926 gmsgid = G_("expected %<,%>");
28927 break;
28928 case RT_SCOPE:
28929 gmsgid = G_("expected %<::%>");
28930 break;
28931 case RT_LESS:
28932 gmsgid = G_("expected %<<%>");
28933 break;
28934 case RT_GREATER:
28935 gmsgid = G_("expected %<>%>");
28936 break;
28937 case RT_EQ:
28938 gmsgid = G_("expected %<=%>");
28939 break;
28940 case RT_ELLIPSIS:
28941 gmsgid = G_("expected %<...%>");
28942 break;
28943 case RT_MULT:
28944 gmsgid = G_("expected %<*%>");
28945 break;
28946 case RT_COMPL:
28947 gmsgid = G_("expected %<~%>");
28948 break;
28949 case RT_COLON:
28950 gmsgid = G_("expected %<:%>");
28951 break;
28952 case RT_COLON_SCOPE:
28953 gmsgid = G_("expected %<:%> or %<::%>");
28954 break;
28955 case RT_CLOSE_PAREN:
28956 gmsgid = G_("expected %<)%>");
28957 break;
28958 case RT_COMMA_CLOSE_PAREN:
28959 gmsgid = G_("expected %<,%> or %<)%>");
28960 break;
28961 case RT_PRAGMA_EOL:
28962 gmsgid = G_("expected end of line");
28963 break;
28964 case RT_NAME:
28965 gmsgid = G_("expected identifier");
28966 break;
28967 case RT_SELECT:
28968 gmsgid = G_("expected selection-statement");
28969 break;
28970 case RT_ITERATION:
28971 gmsgid = G_("expected iteration-statement");
28972 break;
28973 case RT_JUMP:
28974 gmsgid = G_("expected jump-statement");
28975 break;
28976 case RT_CLASS_KEY:
28977 gmsgid = G_("expected class-key");
28978 break;
28979 case RT_CLASS_TYPENAME_TEMPLATE:
28980 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28981 break;
28982 default:
28983 gcc_unreachable ();
28984 }
28985 }
28986
28987 if (gmsgid)
28988 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28989 }
28990
28991
28992 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28993 issue an error message indicating that TOKEN_DESC was expected.
28994
28995 Returns the token consumed, if the token had the appropriate type.
28996 Otherwise, returns NULL.
28997
28998 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28999 within any error as the location of an "opening" token matching
29000 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29001 RT_CLOSE_PAREN). */
29002
29003 static cp_token *
29004 cp_parser_require (cp_parser* parser,
29005 enum cpp_ttype type,
29006 required_token token_desc,
29007 location_t matching_location)
29008 {
29009 if (cp_lexer_next_token_is (parser->lexer, type))
29010 return cp_lexer_consume_token (parser->lexer);
29011 else
29012 {
29013 /* Output the MESSAGE -- unless we're parsing tentatively. */
29014 if (!cp_parser_simulate_error (parser))
29015 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29016 matching_location);
29017 return NULL;
29018 }
29019 }
29020
29021 /* An error message is produced if the next token is not '>'.
29022 All further tokens are skipped until the desired token is
29023 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29024
29025 static void
29026 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29027 {
29028 /* Current level of '< ... >'. */
29029 unsigned level = 0;
29030 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29031 unsigned nesting_depth = 0;
29032
29033 /* Are we ready, yet? If not, issue error message. */
29034 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29035 return;
29036
29037 /* Skip tokens until the desired token is found. */
29038 while (true)
29039 {
29040 /* Peek at the next token. */
29041 switch (cp_lexer_peek_token (parser->lexer)->type)
29042 {
29043 case CPP_LESS:
29044 if (!nesting_depth)
29045 ++level;
29046 break;
29047
29048 case CPP_RSHIFT:
29049 if (cxx_dialect == cxx98)
29050 /* C++0x views the `>>' operator as two `>' tokens, but
29051 C++98 does not. */
29052 break;
29053 else if (!nesting_depth && level-- == 0)
29054 {
29055 /* We've hit a `>>' where the first `>' closes the
29056 template argument list, and the second `>' is
29057 spurious. Just consume the `>>' and stop; we've
29058 already produced at least one error. */
29059 cp_lexer_consume_token (parser->lexer);
29060 return;
29061 }
29062 /* Fall through for C++0x, so we handle the second `>' in
29063 the `>>'. */
29064 gcc_fallthrough ();
29065
29066 case CPP_GREATER:
29067 if (!nesting_depth && level-- == 0)
29068 {
29069 /* We've reached the token we want, consume it and stop. */
29070 cp_lexer_consume_token (parser->lexer);
29071 return;
29072 }
29073 break;
29074
29075 case CPP_OPEN_PAREN:
29076 case CPP_OPEN_SQUARE:
29077 ++nesting_depth;
29078 break;
29079
29080 case CPP_CLOSE_PAREN:
29081 case CPP_CLOSE_SQUARE:
29082 if (nesting_depth-- == 0)
29083 return;
29084 break;
29085
29086 case CPP_EOF:
29087 case CPP_PRAGMA_EOL:
29088 case CPP_SEMICOLON:
29089 case CPP_OPEN_BRACE:
29090 case CPP_CLOSE_BRACE:
29091 /* The '>' was probably forgotten, don't look further. */
29092 return;
29093
29094 default:
29095 break;
29096 }
29097
29098 /* Consume this token. */
29099 cp_lexer_consume_token (parser->lexer);
29100 }
29101 }
29102
29103 /* If the next token is the indicated keyword, consume it. Otherwise,
29104 issue an error message indicating that TOKEN_DESC was expected.
29105
29106 Returns the token consumed, if the token had the appropriate type.
29107 Otherwise, returns NULL. */
29108
29109 static cp_token *
29110 cp_parser_require_keyword (cp_parser* parser,
29111 enum rid keyword,
29112 required_token token_desc)
29113 {
29114 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29115
29116 if (token && token->keyword != keyword)
29117 {
29118 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29119 UNKNOWN_LOCATION);
29120 return NULL;
29121 }
29122
29123 return token;
29124 }
29125
29126 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29127 function-definition. */
29128
29129 static bool
29130 cp_parser_token_starts_function_definition_p (cp_token* token)
29131 {
29132 return (/* An ordinary function-body begins with an `{'. */
29133 token->type == CPP_OPEN_BRACE
29134 /* A ctor-initializer begins with a `:'. */
29135 || token->type == CPP_COLON
29136 /* A function-try-block begins with `try'. */
29137 || token->keyword == RID_TRY
29138 /* A function-transaction-block begins with `__transaction_atomic'
29139 or `__transaction_relaxed'. */
29140 || token->keyword == RID_TRANSACTION_ATOMIC
29141 || token->keyword == RID_TRANSACTION_RELAXED
29142 /* The named return value extension begins with `return'. */
29143 || token->keyword == RID_RETURN);
29144 }
29145
29146 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29147 definition. */
29148
29149 static bool
29150 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29151 {
29152 cp_token *token;
29153
29154 token = cp_lexer_peek_token (parser->lexer);
29155 return (token->type == CPP_OPEN_BRACE
29156 || (token->type == CPP_COLON
29157 && !parser->colon_doesnt_start_class_def_p));
29158 }
29159
29160 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29161 C++0x) ending a template-argument. */
29162
29163 static bool
29164 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29165 {
29166 cp_token *token;
29167
29168 token = cp_lexer_peek_token (parser->lexer);
29169 return (token->type == CPP_COMMA
29170 || token->type == CPP_GREATER
29171 || token->type == CPP_ELLIPSIS
29172 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29173 }
29174
29175 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29176 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29177
29178 static bool
29179 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29180 size_t n)
29181 {
29182 cp_token *token;
29183
29184 token = cp_lexer_peek_nth_token (parser->lexer, n);
29185 if (token->type == CPP_LESS)
29186 return true;
29187 /* Check for the sequence `<::' in the original code. It would be lexed as
29188 `[:', where `[' is a digraph, and there is no whitespace before
29189 `:'. */
29190 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29191 {
29192 cp_token *token2;
29193 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29194 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29195 return true;
29196 }
29197 return false;
29198 }
29199
29200 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29201 or none_type otherwise. */
29202
29203 static enum tag_types
29204 cp_parser_token_is_class_key (cp_token* token)
29205 {
29206 switch (token->keyword)
29207 {
29208 case RID_CLASS:
29209 return class_type;
29210 case RID_STRUCT:
29211 return record_type;
29212 case RID_UNION:
29213 return union_type;
29214
29215 default:
29216 return none_type;
29217 }
29218 }
29219
29220 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29221 or none_type otherwise or if the token is null. */
29222
29223 static enum tag_types
29224 cp_parser_token_is_type_parameter_key (cp_token* token)
29225 {
29226 if (!token)
29227 return none_type;
29228
29229 switch (token->keyword)
29230 {
29231 case RID_CLASS:
29232 return class_type;
29233 case RID_TYPENAME:
29234 return typename_type;
29235
29236 default:
29237 return none_type;
29238 }
29239 }
29240
29241 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29242
29243 static void
29244 cp_parser_check_class_key (enum tag_types class_key, tree type)
29245 {
29246 if (type == error_mark_node)
29247 return;
29248 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29249 {
29250 if (permerror (input_location, "%qs tag used in naming %q#T",
29251 class_key == union_type ? "union"
29252 : class_key == record_type ? "struct" : "class",
29253 type))
29254 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29255 "%q#T was previously declared here", type);
29256 }
29257 }
29258
29259 /* Issue an error message if DECL is redeclared with different
29260 access than its original declaration [class.access.spec/3].
29261 This applies to nested classes, nested class templates and
29262 enumerations [class.mem/1]. */
29263
29264 static void
29265 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29266 {
29267 if (!decl
29268 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29269 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29270 return;
29271
29272 if ((TREE_PRIVATE (decl)
29273 != (current_access_specifier == access_private_node))
29274 || (TREE_PROTECTED (decl)
29275 != (current_access_specifier == access_protected_node)))
29276 error_at (location, "%qD redeclared with different access", decl);
29277 }
29278
29279 /* Look for the `template' keyword, as a syntactic disambiguator.
29280 Return TRUE iff it is present, in which case it will be
29281 consumed. */
29282
29283 static bool
29284 cp_parser_optional_template_keyword (cp_parser *parser)
29285 {
29286 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29287 {
29288 /* In C++98 the `template' keyword can only be used within templates;
29289 outside templates the parser can always figure out what is a
29290 template and what is not. In C++11, per the resolution of DR 468,
29291 `template' is allowed in cases where it is not strictly necessary. */
29292 if (!processing_template_decl
29293 && pedantic && cxx_dialect == cxx98)
29294 {
29295 cp_token *token = cp_lexer_peek_token (parser->lexer);
29296 pedwarn (token->location, OPT_Wpedantic,
29297 "in C++98 %<template%> (as a disambiguator) is only "
29298 "allowed within templates");
29299 /* If this part of the token stream is rescanned, the same
29300 error message would be generated. So, we purge the token
29301 from the stream. */
29302 cp_lexer_purge_token (parser->lexer);
29303 return false;
29304 }
29305 else
29306 {
29307 /* Consume the `template' keyword. */
29308 cp_lexer_consume_token (parser->lexer);
29309 return true;
29310 }
29311 }
29312 return false;
29313 }
29314
29315 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29316 set PARSER->SCOPE, and perform other related actions. */
29317
29318 static void
29319 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29320 {
29321 struct tree_check *check_value;
29322
29323 /* Get the stored value. */
29324 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29325 /* Set the scope from the stored value. */
29326 parser->scope = saved_checks_value (check_value);
29327 parser->qualifying_scope = check_value->qualifying_scope;
29328 parser->object_scope = NULL_TREE;
29329 }
29330
29331 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29332 encounter the end of a block before what we were looking for. */
29333
29334 static bool
29335 cp_parser_cache_group (cp_parser *parser,
29336 enum cpp_ttype end,
29337 unsigned depth)
29338 {
29339 while (true)
29340 {
29341 cp_token *token = cp_lexer_peek_token (parser->lexer);
29342
29343 /* Abort a parenthesized expression if we encounter a semicolon. */
29344 if ((end == CPP_CLOSE_PAREN || depth == 0)
29345 && token->type == CPP_SEMICOLON)
29346 return true;
29347 /* If we've reached the end of the file, stop. */
29348 if (token->type == CPP_EOF
29349 || (end != CPP_PRAGMA_EOL
29350 && token->type == CPP_PRAGMA_EOL))
29351 return true;
29352 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29353 /* We've hit the end of an enclosing block, so there's been some
29354 kind of syntax error. */
29355 return true;
29356
29357 /* Consume the token. */
29358 cp_lexer_consume_token (parser->lexer);
29359 /* See if it starts a new group. */
29360 if (token->type == CPP_OPEN_BRACE)
29361 {
29362 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29363 /* In theory this should probably check end == '}', but
29364 cp_parser_save_member_function_body needs it to exit
29365 after either '}' or ')' when called with ')'. */
29366 if (depth == 0)
29367 return false;
29368 }
29369 else if (token->type == CPP_OPEN_PAREN)
29370 {
29371 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29372 if (depth == 0 && end == CPP_CLOSE_PAREN)
29373 return false;
29374 }
29375 else if (token->type == CPP_PRAGMA)
29376 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29377 else if (token->type == end)
29378 return false;
29379 }
29380 }
29381
29382 /* Like above, for caching a default argument or NSDMI. Both of these are
29383 terminated by a non-nested comma, but it can be unclear whether or not a
29384 comma is nested in a template argument list unless we do more parsing.
29385 In order to handle this ambiguity, when we encounter a ',' after a '<'
29386 we try to parse what follows as a parameter-declaration-list (in the
29387 case of a default argument) or a member-declarator (in the case of an
29388 NSDMI). If that succeeds, then we stop caching. */
29389
29390 static tree
29391 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29392 {
29393 unsigned depth = 0;
29394 int maybe_template_id = 0;
29395 cp_token *first_token;
29396 cp_token *token;
29397 tree default_argument;
29398
29399 /* Add tokens until we have processed the entire default
29400 argument. We add the range [first_token, token). */
29401 first_token = cp_lexer_peek_token (parser->lexer);
29402 if (first_token->type == CPP_OPEN_BRACE)
29403 {
29404 /* For list-initialization, this is straightforward. */
29405 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29406 token = cp_lexer_peek_token (parser->lexer);
29407 }
29408 else while (true)
29409 {
29410 bool done = false;
29411
29412 /* Peek at the next token. */
29413 token = cp_lexer_peek_token (parser->lexer);
29414 /* What we do depends on what token we have. */
29415 switch (token->type)
29416 {
29417 /* In valid code, a default argument must be
29418 immediately followed by a `,' `)', or `...'. */
29419 case CPP_COMMA:
29420 if (depth == 0 && maybe_template_id)
29421 {
29422 /* If we've seen a '<', we might be in a
29423 template-argument-list. Until Core issue 325 is
29424 resolved, we don't know how this situation ought
29425 to be handled, so try to DTRT. We check whether
29426 what comes after the comma is a valid parameter
29427 declaration list. If it is, then the comma ends
29428 the default argument; otherwise the default
29429 argument continues. */
29430 bool error = false;
29431 cp_token *peek;
29432
29433 /* Set ITALP so cp_parser_parameter_declaration_list
29434 doesn't decide to commit to this parse. */
29435 bool saved_italp = parser->in_template_argument_list_p;
29436 parser->in_template_argument_list_p = true;
29437
29438 cp_parser_parse_tentatively (parser);
29439
29440 if (nsdmi)
29441 {
29442 /* Parse declarators until we reach a non-comma or
29443 somthing that cannot be an initializer.
29444 Just checking whether we're looking at a single
29445 declarator is insufficient. Consider:
29446 int var = tuple<T,U>::x;
29447 The template parameter 'U' looks exactly like a
29448 declarator. */
29449 do
29450 {
29451 int ctor_dtor_or_conv_p;
29452 cp_lexer_consume_token (parser->lexer);
29453 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29454 &ctor_dtor_or_conv_p,
29455 /*parenthesized_p=*/NULL,
29456 /*member_p=*/true,
29457 /*friend_p=*/false);
29458 peek = cp_lexer_peek_token (parser->lexer);
29459 if (cp_parser_error_occurred (parser))
29460 break;
29461 }
29462 while (peek->type == CPP_COMMA);
29463 /* If we met an '=' or ';' then the original comma
29464 was the end of the NSDMI. Otherwise assume
29465 we're still in the NSDMI. */
29466 error = (peek->type != CPP_EQ
29467 && peek->type != CPP_SEMICOLON);
29468 }
29469 else
29470 {
29471 cp_lexer_consume_token (parser->lexer);
29472 begin_scope (sk_function_parms, NULL_TREE);
29473 if (cp_parser_parameter_declaration_list (parser)
29474 == error_mark_node)
29475 error = true;
29476 pop_bindings_and_leave_scope ();
29477 }
29478 if (!cp_parser_error_occurred (parser) && !error)
29479 done = true;
29480 cp_parser_abort_tentative_parse (parser);
29481
29482 parser->in_template_argument_list_p = saved_italp;
29483 break;
29484 }
29485 /* FALLTHRU */
29486 case CPP_CLOSE_PAREN:
29487 case CPP_ELLIPSIS:
29488 /* If we run into a non-nested `;', `}', or `]',
29489 then the code is invalid -- but the default
29490 argument is certainly over. */
29491 case CPP_SEMICOLON:
29492 case CPP_CLOSE_BRACE:
29493 case CPP_CLOSE_SQUARE:
29494 if (depth == 0
29495 /* Handle correctly int n = sizeof ... ( p ); */
29496 && token->type != CPP_ELLIPSIS)
29497 done = true;
29498 /* Update DEPTH, if necessary. */
29499 else if (token->type == CPP_CLOSE_PAREN
29500 || token->type == CPP_CLOSE_BRACE
29501 || token->type == CPP_CLOSE_SQUARE)
29502 --depth;
29503 break;
29504
29505 case CPP_OPEN_PAREN:
29506 case CPP_OPEN_SQUARE:
29507 case CPP_OPEN_BRACE:
29508 ++depth;
29509 break;
29510
29511 case CPP_LESS:
29512 if (depth == 0)
29513 /* This might be the comparison operator, or it might
29514 start a template argument list. */
29515 ++maybe_template_id;
29516 break;
29517
29518 case CPP_RSHIFT:
29519 if (cxx_dialect == cxx98)
29520 break;
29521 /* Fall through for C++0x, which treats the `>>'
29522 operator like two `>' tokens in certain
29523 cases. */
29524 gcc_fallthrough ();
29525
29526 case CPP_GREATER:
29527 if (depth == 0)
29528 {
29529 /* This might be an operator, or it might close a
29530 template argument list. But if a previous '<'
29531 started a template argument list, this will have
29532 closed it, so we can't be in one anymore. */
29533 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29534 if (maybe_template_id < 0)
29535 maybe_template_id = 0;
29536 }
29537 break;
29538
29539 /* If we run out of tokens, issue an error message. */
29540 case CPP_EOF:
29541 case CPP_PRAGMA_EOL:
29542 error_at (token->location, "file ends in default argument");
29543 return error_mark_node;
29544
29545 case CPP_NAME:
29546 case CPP_SCOPE:
29547 /* In these cases, we should look for template-ids.
29548 For example, if the default argument is
29549 `X<int, double>()', we need to do name lookup to
29550 figure out whether or not `X' is a template; if
29551 so, the `,' does not end the default argument.
29552
29553 That is not yet done. */
29554 break;
29555
29556 default:
29557 break;
29558 }
29559
29560 /* If we've reached the end, stop. */
29561 if (done)
29562 break;
29563
29564 /* Add the token to the token block. */
29565 token = cp_lexer_consume_token (parser->lexer);
29566 }
29567
29568 /* Create a DEFAULT_ARG to represent the unparsed default
29569 argument. */
29570 default_argument = make_node (DEFAULT_ARG);
29571 DEFARG_TOKENS (default_argument)
29572 = cp_token_cache_new (first_token, token);
29573 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29574
29575 return default_argument;
29576 }
29577
29578 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29579
29580 location_t
29581 defarg_location (tree default_argument)
29582 {
29583 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29584 location_t start = tokens->first->location;
29585 location_t end = tokens->last->location;
29586 return make_location (start, start, end);
29587 }
29588
29589 /* Begin parsing tentatively. We always save tokens while parsing
29590 tentatively so that if the tentative parsing fails we can restore the
29591 tokens. */
29592
29593 static void
29594 cp_parser_parse_tentatively (cp_parser* parser)
29595 {
29596 /* Enter a new parsing context. */
29597 parser->context = cp_parser_context_new (parser->context);
29598 /* Begin saving tokens. */
29599 cp_lexer_save_tokens (parser->lexer);
29600 /* In order to avoid repetitive access control error messages,
29601 access checks are queued up until we are no longer parsing
29602 tentatively. */
29603 push_deferring_access_checks (dk_deferred);
29604 }
29605
29606 /* Commit to the currently active tentative parse. */
29607
29608 static void
29609 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29610 {
29611 cp_parser_context *context;
29612 cp_lexer *lexer;
29613
29614 /* Mark all of the levels as committed. */
29615 lexer = parser->lexer;
29616 for (context = parser->context; context->next; context = context->next)
29617 {
29618 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29619 break;
29620 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29621 while (!cp_lexer_saving_tokens (lexer))
29622 lexer = lexer->next;
29623 cp_lexer_commit_tokens (lexer);
29624 }
29625 }
29626
29627 /* Commit to the topmost currently active tentative parse.
29628
29629 Note that this function shouldn't be called when there are
29630 irreversible side-effects while in a tentative state. For
29631 example, we shouldn't create a permanent entry in the symbol
29632 table, or issue an error message that might not apply if the
29633 tentative parse is aborted. */
29634
29635 static void
29636 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29637 {
29638 cp_parser_context *context = parser->context;
29639 cp_lexer *lexer = parser->lexer;
29640
29641 if (context)
29642 {
29643 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29644 return;
29645 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29646
29647 while (!cp_lexer_saving_tokens (lexer))
29648 lexer = lexer->next;
29649 cp_lexer_commit_tokens (lexer);
29650 }
29651 }
29652
29653 /* Abort the currently active tentative parse. All consumed tokens
29654 will be rolled back, and no diagnostics will be issued. */
29655
29656 static void
29657 cp_parser_abort_tentative_parse (cp_parser* parser)
29658 {
29659 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29660 || errorcount > 0);
29661 cp_parser_simulate_error (parser);
29662 /* Now, pretend that we want to see if the construct was
29663 successfully parsed. */
29664 cp_parser_parse_definitely (parser);
29665 }
29666
29667 /* Stop parsing tentatively. If a parse error has occurred, restore the
29668 token stream. Otherwise, commit to the tokens we have consumed.
29669 Returns true if no error occurred; false otherwise. */
29670
29671 static bool
29672 cp_parser_parse_definitely (cp_parser* parser)
29673 {
29674 bool error_occurred;
29675 cp_parser_context *context;
29676
29677 /* Remember whether or not an error occurred, since we are about to
29678 destroy that information. */
29679 error_occurred = cp_parser_error_occurred (parser);
29680 /* Remove the topmost context from the stack. */
29681 context = parser->context;
29682 parser->context = context->next;
29683 /* If no parse errors occurred, commit to the tentative parse. */
29684 if (!error_occurred)
29685 {
29686 /* Commit to the tokens read tentatively, unless that was
29687 already done. */
29688 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29689 cp_lexer_commit_tokens (parser->lexer);
29690
29691 pop_to_parent_deferring_access_checks ();
29692 }
29693 /* Otherwise, if errors occurred, roll back our state so that things
29694 are just as they were before we began the tentative parse. */
29695 else
29696 {
29697 cp_lexer_rollback_tokens (parser->lexer);
29698 pop_deferring_access_checks ();
29699 }
29700 /* Add the context to the front of the free list. */
29701 context->next = cp_parser_context_free_list;
29702 cp_parser_context_free_list = context;
29703
29704 return !error_occurred;
29705 }
29706
29707 /* Returns true if we are parsing tentatively and are not committed to
29708 this tentative parse. */
29709
29710 static bool
29711 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29712 {
29713 return (cp_parser_parsing_tentatively (parser)
29714 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29715 }
29716
29717 /* Returns nonzero iff an error has occurred during the most recent
29718 tentative parse. */
29719
29720 static bool
29721 cp_parser_error_occurred (cp_parser* parser)
29722 {
29723 return (cp_parser_parsing_tentatively (parser)
29724 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29725 }
29726
29727 /* Returns nonzero if GNU extensions are allowed. */
29728
29729 static bool
29730 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29731 {
29732 return parser->allow_gnu_extensions_p;
29733 }
29734 \f
29735 /* Objective-C++ Productions */
29736
29737
29738 /* Parse an Objective-C expression, which feeds into a primary-expression
29739 above.
29740
29741 objc-expression:
29742 objc-message-expression
29743 objc-string-literal
29744 objc-encode-expression
29745 objc-protocol-expression
29746 objc-selector-expression
29747
29748 Returns a tree representation of the expression. */
29749
29750 static cp_expr
29751 cp_parser_objc_expression (cp_parser* parser)
29752 {
29753 /* Try to figure out what kind of declaration is present. */
29754 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29755
29756 switch (kwd->type)
29757 {
29758 case CPP_OPEN_SQUARE:
29759 return cp_parser_objc_message_expression (parser);
29760
29761 case CPP_OBJC_STRING:
29762 kwd = cp_lexer_consume_token (parser->lexer);
29763 return objc_build_string_object (kwd->u.value);
29764
29765 case CPP_KEYWORD:
29766 switch (kwd->keyword)
29767 {
29768 case RID_AT_ENCODE:
29769 return cp_parser_objc_encode_expression (parser);
29770
29771 case RID_AT_PROTOCOL:
29772 return cp_parser_objc_protocol_expression (parser);
29773
29774 case RID_AT_SELECTOR:
29775 return cp_parser_objc_selector_expression (parser);
29776
29777 default:
29778 break;
29779 }
29780 /* FALLTHRU */
29781 default:
29782 error_at (kwd->location,
29783 "misplaced %<@%D%> Objective-C++ construct",
29784 kwd->u.value);
29785 cp_parser_skip_to_end_of_block_or_statement (parser);
29786 }
29787
29788 return error_mark_node;
29789 }
29790
29791 /* Parse an Objective-C message expression.
29792
29793 objc-message-expression:
29794 [ objc-message-receiver objc-message-args ]
29795
29796 Returns a representation of an Objective-C message. */
29797
29798 static tree
29799 cp_parser_objc_message_expression (cp_parser* parser)
29800 {
29801 tree receiver, messageargs;
29802
29803 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29804 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29805 receiver = cp_parser_objc_message_receiver (parser);
29806 messageargs = cp_parser_objc_message_args (parser);
29807 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29808 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29809
29810 tree result = objc_build_message_expr (receiver, messageargs);
29811
29812 /* Construct a location e.g.
29813 [self func1:5]
29814 ^~~~~~~~~~~~~~
29815 ranging from the '[' to the ']', with the caret at the start. */
29816 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29817 protected_set_expr_location (result, combined_loc);
29818
29819 return result;
29820 }
29821
29822 /* Parse an objc-message-receiver.
29823
29824 objc-message-receiver:
29825 expression
29826 simple-type-specifier
29827
29828 Returns a representation of the type or expression. */
29829
29830 static tree
29831 cp_parser_objc_message_receiver (cp_parser* parser)
29832 {
29833 tree rcv;
29834
29835 /* An Objective-C message receiver may be either (1) a type
29836 or (2) an expression. */
29837 cp_parser_parse_tentatively (parser);
29838 rcv = cp_parser_expression (parser);
29839
29840 /* If that worked out, fine. */
29841 if (cp_parser_parse_definitely (parser))
29842 return rcv;
29843
29844 cp_parser_parse_tentatively (parser);
29845 rcv = cp_parser_simple_type_specifier (parser,
29846 /*decl_specs=*/NULL,
29847 CP_PARSER_FLAGS_NONE);
29848
29849 if (cp_parser_parse_definitely (parser))
29850 return objc_get_class_reference (rcv);
29851
29852 cp_parser_error (parser, "objective-c++ message receiver expected");
29853 return error_mark_node;
29854 }
29855
29856 /* Parse the arguments and selectors comprising an Objective-C message.
29857
29858 objc-message-args:
29859 objc-selector
29860 objc-selector-args
29861 objc-selector-args , objc-comma-args
29862
29863 objc-selector-args:
29864 objc-selector [opt] : assignment-expression
29865 objc-selector-args objc-selector [opt] : assignment-expression
29866
29867 objc-comma-args:
29868 assignment-expression
29869 objc-comma-args , assignment-expression
29870
29871 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29872 selector arguments and TREE_VALUE containing a list of comma
29873 arguments. */
29874
29875 static tree
29876 cp_parser_objc_message_args (cp_parser* parser)
29877 {
29878 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29879 bool maybe_unary_selector_p = true;
29880 cp_token *token = cp_lexer_peek_token (parser->lexer);
29881
29882 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29883 {
29884 tree selector = NULL_TREE, arg;
29885
29886 if (token->type != CPP_COLON)
29887 selector = cp_parser_objc_selector (parser);
29888
29889 /* Detect if we have a unary selector. */
29890 if (maybe_unary_selector_p
29891 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29892 return build_tree_list (selector, NULL_TREE);
29893
29894 maybe_unary_selector_p = false;
29895 cp_parser_require (parser, CPP_COLON, RT_COLON);
29896 arg = cp_parser_assignment_expression (parser);
29897
29898 sel_args
29899 = chainon (sel_args,
29900 build_tree_list (selector, arg));
29901
29902 token = cp_lexer_peek_token (parser->lexer);
29903 }
29904
29905 /* Handle non-selector arguments, if any. */
29906 while (token->type == CPP_COMMA)
29907 {
29908 tree arg;
29909
29910 cp_lexer_consume_token (parser->lexer);
29911 arg = cp_parser_assignment_expression (parser);
29912
29913 addl_args
29914 = chainon (addl_args,
29915 build_tree_list (NULL_TREE, arg));
29916
29917 token = cp_lexer_peek_token (parser->lexer);
29918 }
29919
29920 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29921 {
29922 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29923 return build_tree_list (error_mark_node, error_mark_node);
29924 }
29925
29926 return build_tree_list (sel_args, addl_args);
29927 }
29928
29929 /* Parse an Objective-C encode expression.
29930
29931 objc-encode-expression:
29932 @encode objc-typename
29933
29934 Returns an encoded representation of the type argument. */
29935
29936 static cp_expr
29937 cp_parser_objc_encode_expression (cp_parser* parser)
29938 {
29939 tree type;
29940 cp_token *token;
29941 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29942
29943 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29944 matching_parens parens;
29945 parens.require_open (parser);
29946 token = cp_lexer_peek_token (parser->lexer);
29947 type = complete_type (cp_parser_type_id (parser));
29948 parens.require_close (parser);
29949
29950 if (!type)
29951 {
29952 error_at (token->location,
29953 "%<@encode%> must specify a type as an argument");
29954 return error_mark_node;
29955 }
29956
29957 /* This happens if we find @encode(T) (where T is a template
29958 typename or something dependent on a template typename) when
29959 parsing a template. In that case, we can't compile it
29960 immediately, but we rather create an AT_ENCODE_EXPR which will
29961 need to be instantiated when the template is used.
29962 */
29963 if (dependent_type_p (type))
29964 {
29965 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29966 TREE_READONLY (value) = 1;
29967 return value;
29968 }
29969
29970
29971 /* Build a location of the form:
29972 @encode(int)
29973 ^~~~~~~~~~~~
29974 with caret==start at the @ token, finishing at the close paren. */
29975 location_t combined_loc
29976 = make_location (start_loc, start_loc,
29977 cp_lexer_previous_token (parser->lexer)->location);
29978
29979 return cp_expr (objc_build_encode_expr (type), combined_loc);
29980 }
29981
29982 /* Parse an Objective-C @defs expression. */
29983
29984 static tree
29985 cp_parser_objc_defs_expression (cp_parser *parser)
29986 {
29987 tree name;
29988
29989 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29990 matching_parens parens;
29991 parens.require_open (parser);
29992 name = cp_parser_identifier (parser);
29993 parens.require_close (parser);
29994
29995 return objc_get_class_ivars (name);
29996 }
29997
29998 /* Parse an Objective-C protocol expression.
29999
30000 objc-protocol-expression:
30001 @protocol ( identifier )
30002
30003 Returns a representation of the protocol expression. */
30004
30005 static tree
30006 cp_parser_objc_protocol_expression (cp_parser* parser)
30007 {
30008 tree proto;
30009 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30010
30011 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30012 matching_parens parens;
30013 parens.require_open (parser);
30014 proto = cp_parser_identifier (parser);
30015 parens.require_close (parser);
30016
30017 /* Build a location of the form:
30018 @protocol(prot)
30019 ^~~~~~~~~~~~~~~
30020 with caret==start at the @ token, finishing at the close paren. */
30021 location_t combined_loc
30022 = make_location (start_loc, start_loc,
30023 cp_lexer_previous_token (parser->lexer)->location);
30024 tree result = objc_build_protocol_expr (proto);
30025 protected_set_expr_location (result, combined_loc);
30026 return result;
30027 }
30028
30029 /* Parse an Objective-C selector expression.
30030
30031 objc-selector-expression:
30032 @selector ( objc-method-signature )
30033
30034 objc-method-signature:
30035 objc-selector
30036 objc-selector-seq
30037
30038 objc-selector-seq:
30039 objc-selector :
30040 objc-selector-seq objc-selector :
30041
30042 Returns a representation of the method selector. */
30043
30044 static tree
30045 cp_parser_objc_selector_expression (cp_parser* parser)
30046 {
30047 tree sel_seq = NULL_TREE;
30048 bool maybe_unary_selector_p = true;
30049 cp_token *token;
30050 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30051
30052 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30053 matching_parens parens;
30054 parens.require_open (parser);
30055 token = cp_lexer_peek_token (parser->lexer);
30056
30057 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30058 || token->type == CPP_SCOPE)
30059 {
30060 tree selector = NULL_TREE;
30061
30062 if (token->type != CPP_COLON
30063 || token->type == CPP_SCOPE)
30064 selector = cp_parser_objc_selector (parser);
30065
30066 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30067 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30068 {
30069 /* Detect if we have a unary selector. */
30070 if (maybe_unary_selector_p)
30071 {
30072 sel_seq = selector;
30073 goto finish_selector;
30074 }
30075 else
30076 {
30077 cp_parser_error (parser, "expected %<:%>");
30078 }
30079 }
30080 maybe_unary_selector_p = false;
30081 token = cp_lexer_consume_token (parser->lexer);
30082
30083 if (token->type == CPP_SCOPE)
30084 {
30085 sel_seq
30086 = chainon (sel_seq,
30087 build_tree_list (selector, NULL_TREE));
30088 sel_seq
30089 = chainon (sel_seq,
30090 build_tree_list (NULL_TREE, NULL_TREE));
30091 }
30092 else
30093 sel_seq
30094 = chainon (sel_seq,
30095 build_tree_list (selector, NULL_TREE));
30096
30097 token = cp_lexer_peek_token (parser->lexer);
30098 }
30099
30100 finish_selector:
30101 parens.require_close (parser);
30102
30103
30104 /* Build a location of the form:
30105 @selector(func)
30106 ^~~~~~~~~~~~~~~
30107 with caret==start at the @ token, finishing at the close paren. */
30108 location_t combined_loc
30109 = make_location (loc, loc,
30110 cp_lexer_previous_token (parser->lexer)->location);
30111 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30112 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30113 protected_set_expr_location (result, combined_loc);
30114 return result;
30115 }
30116
30117 /* Parse a list of identifiers.
30118
30119 objc-identifier-list:
30120 identifier
30121 objc-identifier-list , identifier
30122
30123 Returns a TREE_LIST of identifier nodes. */
30124
30125 static tree
30126 cp_parser_objc_identifier_list (cp_parser* parser)
30127 {
30128 tree identifier;
30129 tree list;
30130 cp_token *sep;
30131
30132 identifier = cp_parser_identifier (parser);
30133 if (identifier == error_mark_node)
30134 return error_mark_node;
30135
30136 list = build_tree_list (NULL_TREE, identifier);
30137 sep = cp_lexer_peek_token (parser->lexer);
30138
30139 while (sep->type == CPP_COMMA)
30140 {
30141 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30142 identifier = cp_parser_identifier (parser);
30143 if (identifier == error_mark_node)
30144 return list;
30145
30146 list = chainon (list, build_tree_list (NULL_TREE,
30147 identifier));
30148 sep = cp_lexer_peek_token (parser->lexer);
30149 }
30150
30151 return list;
30152 }
30153
30154 /* Parse an Objective-C alias declaration.
30155
30156 objc-alias-declaration:
30157 @compatibility_alias identifier identifier ;
30158
30159 This function registers the alias mapping with the Objective-C front end.
30160 It returns nothing. */
30161
30162 static void
30163 cp_parser_objc_alias_declaration (cp_parser* parser)
30164 {
30165 tree alias, orig;
30166
30167 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30168 alias = cp_parser_identifier (parser);
30169 orig = cp_parser_identifier (parser);
30170 objc_declare_alias (alias, orig);
30171 cp_parser_consume_semicolon_at_end_of_statement (parser);
30172 }
30173
30174 /* Parse an Objective-C class forward-declaration.
30175
30176 objc-class-declaration:
30177 @class objc-identifier-list ;
30178
30179 The function registers the forward declarations with the Objective-C
30180 front end. It returns nothing. */
30181
30182 static void
30183 cp_parser_objc_class_declaration (cp_parser* parser)
30184 {
30185 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30186 while (true)
30187 {
30188 tree id;
30189
30190 id = cp_parser_identifier (parser);
30191 if (id == error_mark_node)
30192 break;
30193
30194 objc_declare_class (id);
30195
30196 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30197 cp_lexer_consume_token (parser->lexer);
30198 else
30199 break;
30200 }
30201 cp_parser_consume_semicolon_at_end_of_statement (parser);
30202 }
30203
30204 /* Parse a list of Objective-C protocol references.
30205
30206 objc-protocol-refs-opt:
30207 objc-protocol-refs [opt]
30208
30209 objc-protocol-refs:
30210 < objc-identifier-list >
30211
30212 Returns a TREE_LIST of identifiers, if any. */
30213
30214 static tree
30215 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30216 {
30217 tree protorefs = NULL_TREE;
30218
30219 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30220 {
30221 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30222 protorefs = cp_parser_objc_identifier_list (parser);
30223 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30224 }
30225
30226 return protorefs;
30227 }
30228
30229 /* Parse a Objective-C visibility specification. */
30230
30231 static void
30232 cp_parser_objc_visibility_spec (cp_parser* parser)
30233 {
30234 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30235
30236 switch (vis->keyword)
30237 {
30238 case RID_AT_PRIVATE:
30239 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30240 break;
30241 case RID_AT_PROTECTED:
30242 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30243 break;
30244 case RID_AT_PUBLIC:
30245 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30246 break;
30247 case RID_AT_PACKAGE:
30248 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30249 break;
30250 default:
30251 return;
30252 }
30253
30254 /* Eat '@private'/'@protected'/'@public'. */
30255 cp_lexer_consume_token (parser->lexer);
30256 }
30257
30258 /* Parse an Objective-C method type. Return 'true' if it is a class
30259 (+) method, and 'false' if it is an instance (-) method. */
30260
30261 static inline bool
30262 cp_parser_objc_method_type (cp_parser* parser)
30263 {
30264 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30265 return true;
30266 else
30267 return false;
30268 }
30269
30270 /* Parse an Objective-C protocol qualifier. */
30271
30272 static tree
30273 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30274 {
30275 tree quals = NULL_TREE, node;
30276 cp_token *token = cp_lexer_peek_token (parser->lexer);
30277
30278 node = token->u.value;
30279
30280 while (node && identifier_p (node)
30281 && (node == ridpointers [(int) RID_IN]
30282 || node == ridpointers [(int) RID_OUT]
30283 || node == ridpointers [(int) RID_INOUT]
30284 || node == ridpointers [(int) RID_BYCOPY]
30285 || node == ridpointers [(int) RID_BYREF]
30286 || node == ridpointers [(int) RID_ONEWAY]))
30287 {
30288 quals = tree_cons (NULL_TREE, node, quals);
30289 cp_lexer_consume_token (parser->lexer);
30290 token = cp_lexer_peek_token (parser->lexer);
30291 node = token->u.value;
30292 }
30293
30294 return quals;
30295 }
30296
30297 /* Parse an Objective-C typename. */
30298
30299 static tree
30300 cp_parser_objc_typename (cp_parser* parser)
30301 {
30302 tree type_name = NULL_TREE;
30303
30304 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30305 {
30306 tree proto_quals, cp_type = NULL_TREE;
30307
30308 matching_parens parens;
30309 parens.consume_open (parser); /* Eat '('. */
30310 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30311
30312 /* An ObjC type name may consist of just protocol qualifiers, in which
30313 case the type shall default to 'id'. */
30314 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30315 {
30316 cp_type = cp_parser_type_id (parser);
30317
30318 /* If the type could not be parsed, an error has already
30319 been produced. For error recovery, behave as if it had
30320 not been specified, which will use the default type
30321 'id'. */
30322 if (cp_type == error_mark_node)
30323 {
30324 cp_type = NULL_TREE;
30325 /* We need to skip to the closing parenthesis as
30326 cp_parser_type_id() does not seem to do it for
30327 us. */
30328 cp_parser_skip_to_closing_parenthesis (parser,
30329 /*recovering=*/true,
30330 /*or_comma=*/false,
30331 /*consume_paren=*/false);
30332 }
30333 }
30334
30335 parens.require_close (parser);
30336 type_name = build_tree_list (proto_quals, cp_type);
30337 }
30338
30339 return type_name;
30340 }
30341
30342 /* Check to see if TYPE refers to an Objective-C selector name. */
30343
30344 static bool
30345 cp_parser_objc_selector_p (enum cpp_ttype type)
30346 {
30347 return (type == CPP_NAME || type == CPP_KEYWORD
30348 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30349 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30350 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30351 || type == CPP_XOR || type == CPP_XOR_EQ);
30352 }
30353
30354 /* Parse an Objective-C selector. */
30355
30356 static tree
30357 cp_parser_objc_selector (cp_parser* parser)
30358 {
30359 cp_token *token = cp_lexer_consume_token (parser->lexer);
30360
30361 if (!cp_parser_objc_selector_p (token->type))
30362 {
30363 error_at (token->location, "invalid Objective-C++ selector name");
30364 return error_mark_node;
30365 }
30366
30367 /* C++ operator names are allowed to appear in ObjC selectors. */
30368 switch (token->type)
30369 {
30370 case CPP_AND_AND: return get_identifier ("and");
30371 case CPP_AND_EQ: return get_identifier ("and_eq");
30372 case CPP_AND: return get_identifier ("bitand");
30373 case CPP_OR: return get_identifier ("bitor");
30374 case CPP_COMPL: return get_identifier ("compl");
30375 case CPP_NOT: return get_identifier ("not");
30376 case CPP_NOT_EQ: return get_identifier ("not_eq");
30377 case CPP_OR_OR: return get_identifier ("or");
30378 case CPP_OR_EQ: return get_identifier ("or_eq");
30379 case CPP_XOR: return get_identifier ("xor");
30380 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30381 default: return token->u.value;
30382 }
30383 }
30384
30385 /* Parse an Objective-C params list. */
30386
30387 static tree
30388 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30389 {
30390 tree params = NULL_TREE;
30391 bool maybe_unary_selector_p = true;
30392 cp_token *token = cp_lexer_peek_token (parser->lexer);
30393
30394 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30395 {
30396 tree selector = NULL_TREE, type_name, identifier;
30397 tree parm_attr = NULL_TREE;
30398
30399 if (token->keyword == RID_ATTRIBUTE)
30400 break;
30401
30402 if (token->type != CPP_COLON)
30403 selector = cp_parser_objc_selector (parser);
30404
30405 /* Detect if we have a unary selector. */
30406 if (maybe_unary_selector_p
30407 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30408 {
30409 params = selector; /* Might be followed by attributes. */
30410 break;
30411 }
30412
30413 maybe_unary_selector_p = false;
30414 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30415 {
30416 /* Something went quite wrong. There should be a colon
30417 here, but there is not. Stop parsing parameters. */
30418 break;
30419 }
30420 type_name = cp_parser_objc_typename (parser);
30421 /* New ObjC allows attributes on parameters too. */
30422 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30423 parm_attr = cp_parser_attributes_opt (parser);
30424 identifier = cp_parser_identifier (parser);
30425
30426 params
30427 = chainon (params,
30428 objc_build_keyword_decl (selector,
30429 type_name,
30430 identifier,
30431 parm_attr));
30432
30433 token = cp_lexer_peek_token (parser->lexer);
30434 }
30435
30436 if (params == NULL_TREE)
30437 {
30438 cp_parser_error (parser, "objective-c++ method declaration is expected");
30439 return error_mark_node;
30440 }
30441
30442 /* We allow tail attributes for the method. */
30443 if (token->keyword == RID_ATTRIBUTE)
30444 {
30445 *attributes = cp_parser_attributes_opt (parser);
30446 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30447 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30448 return params;
30449 cp_parser_error (parser,
30450 "method attributes must be specified at the end");
30451 return error_mark_node;
30452 }
30453
30454 if (params == NULL_TREE)
30455 {
30456 cp_parser_error (parser, "objective-c++ method declaration is expected");
30457 return error_mark_node;
30458 }
30459 return params;
30460 }
30461
30462 /* Parse the non-keyword Objective-C params. */
30463
30464 static tree
30465 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30466 tree* attributes)
30467 {
30468 tree params = make_node (TREE_LIST);
30469 cp_token *token = cp_lexer_peek_token (parser->lexer);
30470 *ellipsisp = false; /* Initially, assume no ellipsis. */
30471
30472 while (token->type == CPP_COMMA)
30473 {
30474 cp_parameter_declarator *parmdecl;
30475 tree parm;
30476
30477 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30478 token = cp_lexer_peek_token (parser->lexer);
30479
30480 if (token->type == CPP_ELLIPSIS)
30481 {
30482 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30483 *ellipsisp = true;
30484 token = cp_lexer_peek_token (parser->lexer);
30485 break;
30486 }
30487
30488 /* TODO: parse attributes for tail parameters. */
30489 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
30490 parm = grokdeclarator (parmdecl->declarator,
30491 &parmdecl->decl_specifiers,
30492 PARM, /*initialized=*/0,
30493 /*attrlist=*/NULL);
30494
30495 chainon (params, build_tree_list (NULL_TREE, parm));
30496 token = cp_lexer_peek_token (parser->lexer);
30497 }
30498
30499 /* We allow tail attributes for the method. */
30500 if (token->keyword == RID_ATTRIBUTE)
30501 {
30502 if (*attributes == NULL_TREE)
30503 {
30504 *attributes = cp_parser_attributes_opt (parser);
30505 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30506 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30507 return params;
30508 }
30509 else
30510 /* We have an error, but parse the attributes, so that we can
30511 carry on. */
30512 *attributes = cp_parser_attributes_opt (parser);
30513
30514 cp_parser_error (parser,
30515 "method attributes must be specified at the end");
30516 return error_mark_node;
30517 }
30518
30519 return params;
30520 }
30521
30522 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30523
30524 static void
30525 cp_parser_objc_interstitial_code (cp_parser* parser)
30526 {
30527 cp_token *token = cp_lexer_peek_token (parser->lexer);
30528
30529 /* If the next token is `extern' and the following token is a string
30530 literal, then we have a linkage specification. */
30531 if (token->keyword == RID_EXTERN
30532 && cp_parser_is_pure_string_literal
30533 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30534 cp_parser_linkage_specification (parser);
30535 /* Handle #pragma, if any. */
30536 else if (token->type == CPP_PRAGMA)
30537 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30538 /* Allow stray semicolons. */
30539 else if (token->type == CPP_SEMICOLON)
30540 cp_lexer_consume_token (parser->lexer);
30541 /* Mark methods as optional or required, when building protocols. */
30542 else if (token->keyword == RID_AT_OPTIONAL)
30543 {
30544 cp_lexer_consume_token (parser->lexer);
30545 objc_set_method_opt (true);
30546 }
30547 else if (token->keyword == RID_AT_REQUIRED)
30548 {
30549 cp_lexer_consume_token (parser->lexer);
30550 objc_set_method_opt (false);
30551 }
30552 else if (token->keyword == RID_NAMESPACE)
30553 cp_parser_namespace_definition (parser);
30554 /* Other stray characters must generate errors. */
30555 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30556 {
30557 cp_lexer_consume_token (parser->lexer);
30558 error ("stray %qs between Objective-C++ methods",
30559 token->type == CPP_OPEN_BRACE ? "{" : "}");
30560 }
30561 /* Finally, try to parse a block-declaration, or a function-definition. */
30562 else
30563 cp_parser_block_declaration (parser, /*statement_p=*/false);
30564 }
30565
30566 /* Parse a method signature. */
30567
30568 static tree
30569 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30570 {
30571 tree rettype, kwdparms, optparms;
30572 bool ellipsis = false;
30573 bool is_class_method;
30574
30575 is_class_method = cp_parser_objc_method_type (parser);
30576 rettype = cp_parser_objc_typename (parser);
30577 *attributes = NULL_TREE;
30578 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30579 if (kwdparms == error_mark_node)
30580 return error_mark_node;
30581 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30582 if (optparms == error_mark_node)
30583 return error_mark_node;
30584
30585 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30586 }
30587
30588 static bool
30589 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30590 {
30591 tree tattr;
30592 cp_lexer_save_tokens (parser->lexer);
30593 tattr = cp_parser_attributes_opt (parser);
30594 gcc_assert (tattr) ;
30595
30596 /* If the attributes are followed by a method introducer, this is not allowed.
30597 Dump the attributes and flag the situation. */
30598 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30599 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30600 return true;
30601
30602 /* Otherwise, the attributes introduce some interstitial code, possibly so
30603 rewind to allow that check. */
30604 cp_lexer_rollback_tokens (parser->lexer);
30605 return false;
30606 }
30607
30608 /* Parse an Objective-C method prototype list. */
30609
30610 static void
30611 cp_parser_objc_method_prototype_list (cp_parser* parser)
30612 {
30613 cp_token *token = cp_lexer_peek_token (parser->lexer);
30614
30615 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30616 {
30617 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30618 {
30619 tree attributes, sig;
30620 bool is_class_method;
30621 if (token->type == CPP_PLUS)
30622 is_class_method = true;
30623 else
30624 is_class_method = false;
30625 sig = cp_parser_objc_method_signature (parser, &attributes);
30626 if (sig == error_mark_node)
30627 {
30628 cp_parser_skip_to_end_of_block_or_statement (parser);
30629 token = cp_lexer_peek_token (parser->lexer);
30630 continue;
30631 }
30632 objc_add_method_declaration (is_class_method, sig, attributes);
30633 cp_parser_consume_semicolon_at_end_of_statement (parser);
30634 }
30635 else if (token->keyword == RID_AT_PROPERTY)
30636 cp_parser_objc_at_property_declaration (parser);
30637 else if (token->keyword == RID_ATTRIBUTE
30638 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30639 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30640 OPT_Wattributes,
30641 "prefix attributes are ignored for methods");
30642 else
30643 /* Allow for interspersed non-ObjC++ code. */
30644 cp_parser_objc_interstitial_code (parser);
30645
30646 token = cp_lexer_peek_token (parser->lexer);
30647 }
30648
30649 if (token->type != CPP_EOF)
30650 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30651 else
30652 cp_parser_error (parser, "expected %<@end%>");
30653
30654 objc_finish_interface ();
30655 }
30656
30657 /* Parse an Objective-C method definition list. */
30658
30659 static void
30660 cp_parser_objc_method_definition_list (cp_parser* parser)
30661 {
30662 cp_token *token = cp_lexer_peek_token (parser->lexer);
30663
30664 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30665 {
30666 tree meth;
30667
30668 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30669 {
30670 cp_token *ptk;
30671 tree sig, attribute;
30672 bool is_class_method;
30673 if (token->type == CPP_PLUS)
30674 is_class_method = true;
30675 else
30676 is_class_method = false;
30677 push_deferring_access_checks (dk_deferred);
30678 sig = cp_parser_objc_method_signature (parser, &attribute);
30679 if (sig == error_mark_node)
30680 {
30681 cp_parser_skip_to_end_of_block_or_statement (parser);
30682 token = cp_lexer_peek_token (parser->lexer);
30683 continue;
30684 }
30685 objc_start_method_definition (is_class_method, sig, attribute,
30686 NULL_TREE);
30687
30688 /* For historical reasons, we accept an optional semicolon. */
30689 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30690 cp_lexer_consume_token (parser->lexer);
30691
30692 ptk = cp_lexer_peek_token (parser->lexer);
30693 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30694 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30695 {
30696 perform_deferred_access_checks (tf_warning_or_error);
30697 stop_deferring_access_checks ();
30698 meth = cp_parser_function_definition_after_declarator (parser,
30699 false);
30700 pop_deferring_access_checks ();
30701 objc_finish_method_definition (meth);
30702 }
30703 }
30704 /* The following case will be removed once @synthesize is
30705 completely implemented. */
30706 else if (token->keyword == RID_AT_PROPERTY)
30707 cp_parser_objc_at_property_declaration (parser);
30708 else if (token->keyword == RID_AT_SYNTHESIZE)
30709 cp_parser_objc_at_synthesize_declaration (parser);
30710 else if (token->keyword == RID_AT_DYNAMIC)
30711 cp_parser_objc_at_dynamic_declaration (parser);
30712 else if (token->keyword == RID_ATTRIBUTE
30713 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30714 warning_at (token->location, OPT_Wattributes,
30715 "prefix attributes are ignored for methods");
30716 else
30717 /* Allow for interspersed non-ObjC++ code. */
30718 cp_parser_objc_interstitial_code (parser);
30719
30720 token = cp_lexer_peek_token (parser->lexer);
30721 }
30722
30723 if (token->type != CPP_EOF)
30724 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30725 else
30726 cp_parser_error (parser, "expected %<@end%>");
30727
30728 objc_finish_implementation ();
30729 }
30730
30731 /* Parse Objective-C ivars. */
30732
30733 static void
30734 cp_parser_objc_class_ivars (cp_parser* parser)
30735 {
30736 cp_token *token = cp_lexer_peek_token (parser->lexer);
30737
30738 if (token->type != CPP_OPEN_BRACE)
30739 return; /* No ivars specified. */
30740
30741 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30742 token = cp_lexer_peek_token (parser->lexer);
30743
30744 while (token->type != CPP_CLOSE_BRACE
30745 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30746 {
30747 cp_decl_specifier_seq declspecs;
30748 int decl_class_or_enum_p;
30749 tree prefix_attributes;
30750
30751 cp_parser_objc_visibility_spec (parser);
30752
30753 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30754 break;
30755
30756 cp_parser_decl_specifier_seq (parser,
30757 CP_PARSER_FLAGS_OPTIONAL,
30758 &declspecs,
30759 &decl_class_or_enum_p);
30760
30761 /* auto, register, static, extern, mutable. */
30762 if (declspecs.storage_class != sc_none)
30763 {
30764 cp_parser_error (parser, "invalid type for instance variable");
30765 declspecs.storage_class = sc_none;
30766 }
30767
30768 /* thread_local. */
30769 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30770 {
30771 cp_parser_error (parser, "invalid type for instance variable");
30772 declspecs.locations[ds_thread] = 0;
30773 }
30774
30775 /* typedef. */
30776 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30777 {
30778 cp_parser_error (parser, "invalid type for instance variable");
30779 declspecs.locations[ds_typedef] = 0;
30780 }
30781
30782 prefix_attributes = declspecs.attributes;
30783 declspecs.attributes = NULL_TREE;
30784
30785 /* Keep going until we hit the `;' at the end of the
30786 declaration. */
30787 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30788 {
30789 tree width = NULL_TREE, attributes, first_attribute, decl;
30790 cp_declarator *declarator = NULL;
30791 int ctor_dtor_or_conv_p;
30792
30793 /* Check for a (possibly unnamed) bitfield declaration. */
30794 token = cp_lexer_peek_token (parser->lexer);
30795 if (token->type == CPP_COLON)
30796 goto eat_colon;
30797
30798 if (token->type == CPP_NAME
30799 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30800 == CPP_COLON))
30801 {
30802 /* Get the name of the bitfield. */
30803 declarator = make_id_declarator (NULL_TREE,
30804 cp_parser_identifier (parser),
30805 sfk_none, token->location);
30806
30807 eat_colon:
30808 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30809 /* Get the width of the bitfield. */
30810 width
30811 = cp_parser_constant_expression (parser);
30812 }
30813 else
30814 {
30815 /* Parse the declarator. */
30816 declarator
30817 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30818 &ctor_dtor_or_conv_p,
30819 /*parenthesized_p=*/NULL,
30820 /*member_p=*/false,
30821 /*friend_p=*/false);
30822 }
30823
30824 /* Look for attributes that apply to the ivar. */
30825 attributes = cp_parser_attributes_opt (parser);
30826 /* Remember which attributes are prefix attributes and
30827 which are not. */
30828 first_attribute = attributes;
30829 /* Combine the attributes. */
30830 attributes = attr_chainon (prefix_attributes, attributes);
30831
30832 if (width)
30833 /* Create the bitfield declaration. */
30834 decl = grokbitfield (declarator, &declspecs,
30835 width, NULL_TREE, attributes);
30836 else
30837 decl = grokfield (declarator, &declspecs,
30838 NULL_TREE, /*init_const_expr_p=*/false,
30839 NULL_TREE, attributes);
30840
30841 /* Add the instance variable. */
30842 if (decl != error_mark_node && decl != NULL_TREE)
30843 objc_add_instance_variable (decl);
30844
30845 /* Reset PREFIX_ATTRIBUTES. */
30846 if (attributes != error_mark_node)
30847 {
30848 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30849 attributes = TREE_CHAIN (attributes);
30850 if (attributes)
30851 TREE_CHAIN (attributes) = NULL_TREE;
30852 }
30853
30854 token = cp_lexer_peek_token (parser->lexer);
30855
30856 if (token->type == CPP_COMMA)
30857 {
30858 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30859 continue;
30860 }
30861 break;
30862 }
30863
30864 cp_parser_consume_semicolon_at_end_of_statement (parser);
30865 token = cp_lexer_peek_token (parser->lexer);
30866 }
30867
30868 if (token->keyword == RID_AT_END)
30869 cp_parser_error (parser, "expected %<}%>");
30870
30871 /* Do not consume the RID_AT_END, so it will be read again as terminating
30872 the @interface of @implementation. */
30873 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30874 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30875
30876 /* For historical reasons, we accept an optional semicolon. */
30877 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30878 cp_lexer_consume_token (parser->lexer);
30879 }
30880
30881 /* Parse an Objective-C protocol declaration. */
30882
30883 static void
30884 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30885 {
30886 tree proto, protorefs;
30887 cp_token *tok;
30888
30889 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30890 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30891 {
30892 tok = cp_lexer_peek_token (parser->lexer);
30893 error_at (tok->location, "identifier expected after %<@protocol%>");
30894 cp_parser_consume_semicolon_at_end_of_statement (parser);
30895 return;
30896 }
30897
30898 /* See if we have a forward declaration or a definition. */
30899 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30900
30901 /* Try a forward declaration first. */
30902 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30903 {
30904 while (true)
30905 {
30906 tree id;
30907
30908 id = cp_parser_identifier (parser);
30909 if (id == error_mark_node)
30910 break;
30911
30912 objc_declare_protocol (id, attributes);
30913
30914 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30915 cp_lexer_consume_token (parser->lexer);
30916 else
30917 break;
30918 }
30919 cp_parser_consume_semicolon_at_end_of_statement (parser);
30920 }
30921
30922 /* Ok, we got a full-fledged definition (or at least should). */
30923 else
30924 {
30925 proto = cp_parser_identifier (parser);
30926 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30927 objc_start_protocol (proto, protorefs, attributes);
30928 cp_parser_objc_method_prototype_list (parser);
30929 }
30930 }
30931
30932 /* Parse an Objective-C superclass or category. */
30933
30934 static void
30935 cp_parser_objc_superclass_or_category (cp_parser *parser,
30936 bool iface_p,
30937 tree *super,
30938 tree *categ, bool *is_class_extension)
30939 {
30940 cp_token *next = cp_lexer_peek_token (parser->lexer);
30941
30942 *super = *categ = NULL_TREE;
30943 *is_class_extension = false;
30944 if (next->type == CPP_COLON)
30945 {
30946 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30947 *super = cp_parser_identifier (parser);
30948 }
30949 else if (next->type == CPP_OPEN_PAREN)
30950 {
30951 matching_parens parens;
30952 parens.consume_open (parser); /* Eat '('. */
30953
30954 /* If there is no category name, and this is an @interface, we
30955 have a class extension. */
30956 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30957 {
30958 *categ = NULL_TREE;
30959 *is_class_extension = true;
30960 }
30961 else
30962 *categ = cp_parser_identifier (parser);
30963
30964 parens.require_close (parser);
30965 }
30966 }
30967
30968 /* Parse an Objective-C class interface. */
30969
30970 static void
30971 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30972 {
30973 tree name, super, categ, protos;
30974 bool is_class_extension;
30975
30976 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30977 name = cp_parser_identifier (parser);
30978 if (name == error_mark_node)
30979 {
30980 /* It's hard to recover because even if valid @interface stuff
30981 is to follow, we can't compile it (or validate it) if we
30982 don't even know which class it refers to. Let's assume this
30983 was a stray '@interface' token in the stream and skip it.
30984 */
30985 return;
30986 }
30987 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30988 &is_class_extension);
30989 protos = cp_parser_objc_protocol_refs_opt (parser);
30990
30991 /* We have either a class or a category on our hands. */
30992 if (categ || is_class_extension)
30993 objc_start_category_interface (name, categ, protos, attributes);
30994 else
30995 {
30996 objc_start_class_interface (name, super, protos, attributes);
30997 /* Handle instance variable declarations, if any. */
30998 cp_parser_objc_class_ivars (parser);
30999 objc_continue_interface ();
31000 }
31001
31002 cp_parser_objc_method_prototype_list (parser);
31003 }
31004
31005 /* Parse an Objective-C class implementation. */
31006
31007 static void
31008 cp_parser_objc_class_implementation (cp_parser* parser)
31009 {
31010 tree name, super, categ;
31011 bool is_class_extension;
31012
31013 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
31014 name = cp_parser_identifier (parser);
31015 if (name == error_mark_node)
31016 {
31017 /* It's hard to recover because even if valid @implementation
31018 stuff is to follow, we can't compile it (or validate it) if
31019 we don't even know which class it refers to. Let's assume
31020 this was a stray '@implementation' token in the stream and
31021 skip it.
31022 */
31023 return;
31024 }
31025 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31026 &is_class_extension);
31027
31028 /* We have either a class or a category on our hands. */
31029 if (categ)
31030 objc_start_category_implementation (name, categ);
31031 else
31032 {
31033 objc_start_class_implementation (name, super);
31034 /* Handle instance variable declarations, if any. */
31035 cp_parser_objc_class_ivars (parser);
31036 objc_continue_implementation ();
31037 }
31038
31039 cp_parser_objc_method_definition_list (parser);
31040 }
31041
31042 /* Consume the @end token and finish off the implementation. */
31043
31044 static void
31045 cp_parser_objc_end_implementation (cp_parser* parser)
31046 {
31047 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31048 objc_finish_implementation ();
31049 }
31050
31051 /* Parse an Objective-C declaration. */
31052
31053 static void
31054 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31055 {
31056 /* Try to figure out what kind of declaration is present. */
31057 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31058
31059 if (attributes)
31060 switch (kwd->keyword)
31061 {
31062 case RID_AT_ALIAS:
31063 case RID_AT_CLASS:
31064 case RID_AT_END:
31065 error_at (kwd->location, "attributes may not be specified before"
31066 " the %<@%D%> Objective-C++ keyword",
31067 kwd->u.value);
31068 attributes = NULL;
31069 break;
31070 case RID_AT_IMPLEMENTATION:
31071 warning_at (kwd->location, OPT_Wattributes,
31072 "prefix attributes are ignored before %<@%D%>",
31073 kwd->u.value);
31074 attributes = NULL;
31075 default:
31076 break;
31077 }
31078
31079 switch (kwd->keyword)
31080 {
31081 case RID_AT_ALIAS:
31082 cp_parser_objc_alias_declaration (parser);
31083 break;
31084 case RID_AT_CLASS:
31085 cp_parser_objc_class_declaration (parser);
31086 break;
31087 case RID_AT_PROTOCOL:
31088 cp_parser_objc_protocol_declaration (parser, attributes);
31089 break;
31090 case RID_AT_INTERFACE:
31091 cp_parser_objc_class_interface (parser, attributes);
31092 break;
31093 case RID_AT_IMPLEMENTATION:
31094 cp_parser_objc_class_implementation (parser);
31095 break;
31096 case RID_AT_END:
31097 cp_parser_objc_end_implementation (parser);
31098 break;
31099 default:
31100 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31101 kwd->u.value);
31102 cp_parser_skip_to_end_of_block_or_statement (parser);
31103 }
31104 }
31105
31106 /* Parse an Objective-C try-catch-finally statement.
31107
31108 objc-try-catch-finally-stmt:
31109 @try compound-statement objc-catch-clause-seq [opt]
31110 objc-finally-clause [opt]
31111
31112 objc-catch-clause-seq:
31113 objc-catch-clause objc-catch-clause-seq [opt]
31114
31115 objc-catch-clause:
31116 @catch ( objc-exception-declaration ) compound-statement
31117
31118 objc-finally-clause:
31119 @finally compound-statement
31120
31121 objc-exception-declaration:
31122 parameter-declaration
31123 '...'
31124
31125 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31126
31127 Returns NULL_TREE.
31128
31129 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31130 for C. Keep them in sync. */
31131
31132 static tree
31133 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31134 {
31135 location_t location;
31136 tree stmt;
31137
31138 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31139 location = cp_lexer_peek_token (parser->lexer)->location;
31140 objc_maybe_warn_exceptions (location);
31141 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31142 node, lest it get absorbed into the surrounding block. */
31143 stmt = push_stmt_list ();
31144 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31145 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31146
31147 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31148 {
31149 cp_parameter_declarator *parm;
31150 tree parameter_declaration = error_mark_node;
31151 bool seen_open_paren = false;
31152 matching_parens parens;
31153
31154 cp_lexer_consume_token (parser->lexer);
31155 if (parens.require_open (parser))
31156 seen_open_paren = true;
31157 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31158 {
31159 /* We have "@catch (...)" (where the '...' are literally
31160 what is in the code). Skip the '...'.
31161 parameter_declaration is set to NULL_TREE, and
31162 objc_being_catch_clauses() knows that that means
31163 '...'. */
31164 cp_lexer_consume_token (parser->lexer);
31165 parameter_declaration = NULL_TREE;
31166 }
31167 else
31168 {
31169 /* We have "@catch (NSException *exception)" or something
31170 like that. Parse the parameter declaration. */
31171 parm = cp_parser_parameter_declaration (parser, false, NULL);
31172 if (parm == NULL)
31173 parameter_declaration = error_mark_node;
31174 else
31175 parameter_declaration = grokdeclarator (parm->declarator,
31176 &parm->decl_specifiers,
31177 PARM, /*initialized=*/0,
31178 /*attrlist=*/NULL);
31179 }
31180 if (seen_open_paren)
31181 parens.require_close (parser);
31182 else
31183 {
31184 /* If there was no open parenthesis, we are recovering from
31185 an error, and we are trying to figure out what mistake
31186 the user has made. */
31187
31188 /* If there is an immediate closing parenthesis, the user
31189 probably forgot the opening one (ie, they typed "@catch
31190 NSException *e)". Parse the closing parenthesis and keep
31191 going. */
31192 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31193 cp_lexer_consume_token (parser->lexer);
31194
31195 /* If these is no immediate closing parenthesis, the user
31196 probably doesn't know that parenthesis are required at
31197 all (ie, they typed "@catch NSException *e"). So, just
31198 forget about the closing parenthesis and keep going. */
31199 }
31200 objc_begin_catch_clause (parameter_declaration);
31201 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31202 objc_finish_catch_clause ();
31203 }
31204 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31205 {
31206 cp_lexer_consume_token (parser->lexer);
31207 location = cp_lexer_peek_token (parser->lexer)->location;
31208 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31209 node, lest it get absorbed into the surrounding block. */
31210 stmt = push_stmt_list ();
31211 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31212 objc_build_finally_clause (location, pop_stmt_list (stmt));
31213 }
31214
31215 return objc_finish_try_stmt ();
31216 }
31217
31218 /* Parse an Objective-C synchronized statement.
31219
31220 objc-synchronized-stmt:
31221 @synchronized ( expression ) compound-statement
31222
31223 Returns NULL_TREE. */
31224
31225 static tree
31226 cp_parser_objc_synchronized_statement (cp_parser *parser)
31227 {
31228 location_t location;
31229 tree lock, stmt;
31230
31231 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31232
31233 location = cp_lexer_peek_token (parser->lexer)->location;
31234 objc_maybe_warn_exceptions (location);
31235 matching_parens parens;
31236 parens.require_open (parser);
31237 lock = cp_parser_expression (parser);
31238 parens.require_close (parser);
31239
31240 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31241 node, lest it get absorbed into the surrounding block. */
31242 stmt = push_stmt_list ();
31243 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31244
31245 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31246 }
31247
31248 /* Parse an Objective-C throw statement.
31249
31250 objc-throw-stmt:
31251 @throw assignment-expression [opt] ;
31252
31253 Returns a constructed '@throw' statement. */
31254
31255 static tree
31256 cp_parser_objc_throw_statement (cp_parser *parser)
31257 {
31258 tree expr = NULL_TREE;
31259 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31260
31261 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31262
31263 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31264 expr = cp_parser_expression (parser);
31265
31266 cp_parser_consume_semicolon_at_end_of_statement (parser);
31267
31268 return objc_build_throw_stmt (loc, expr);
31269 }
31270
31271 /* Parse an Objective-C statement. */
31272
31273 static tree
31274 cp_parser_objc_statement (cp_parser * parser)
31275 {
31276 /* Try to figure out what kind of declaration is present. */
31277 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31278
31279 switch (kwd->keyword)
31280 {
31281 case RID_AT_TRY:
31282 return cp_parser_objc_try_catch_finally_statement (parser);
31283 case RID_AT_SYNCHRONIZED:
31284 return cp_parser_objc_synchronized_statement (parser);
31285 case RID_AT_THROW:
31286 return cp_parser_objc_throw_statement (parser);
31287 default:
31288 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31289 kwd->u.value);
31290 cp_parser_skip_to_end_of_block_or_statement (parser);
31291 }
31292
31293 return error_mark_node;
31294 }
31295
31296 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31297 look ahead to see if an objc keyword follows the attributes. This
31298 is to detect the use of prefix attributes on ObjC @interface and
31299 @protocol. */
31300
31301 static bool
31302 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31303 {
31304 cp_lexer_save_tokens (parser->lexer);
31305 *attrib = cp_parser_attributes_opt (parser);
31306 gcc_assert (*attrib);
31307 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31308 {
31309 cp_lexer_commit_tokens (parser->lexer);
31310 return true;
31311 }
31312 cp_lexer_rollback_tokens (parser->lexer);
31313 return false;
31314 }
31315
31316 /* This routine is a minimal replacement for
31317 c_parser_struct_declaration () used when parsing the list of
31318 types/names or ObjC++ properties. For example, when parsing the
31319 code
31320
31321 @property (readonly) int a, b, c;
31322
31323 this function is responsible for parsing "int a, int b, int c" and
31324 returning the declarations as CHAIN of DECLs.
31325
31326 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31327 similar parsing. */
31328 static tree
31329 cp_parser_objc_struct_declaration (cp_parser *parser)
31330 {
31331 tree decls = NULL_TREE;
31332 cp_decl_specifier_seq declspecs;
31333 int decl_class_or_enum_p;
31334 tree prefix_attributes;
31335
31336 cp_parser_decl_specifier_seq (parser,
31337 CP_PARSER_FLAGS_NONE,
31338 &declspecs,
31339 &decl_class_or_enum_p);
31340
31341 if (declspecs.type == error_mark_node)
31342 return error_mark_node;
31343
31344 /* auto, register, static, extern, mutable. */
31345 if (declspecs.storage_class != sc_none)
31346 {
31347 cp_parser_error (parser, "invalid type for property");
31348 declspecs.storage_class = sc_none;
31349 }
31350
31351 /* thread_local. */
31352 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31353 {
31354 cp_parser_error (parser, "invalid type for property");
31355 declspecs.locations[ds_thread] = 0;
31356 }
31357
31358 /* typedef. */
31359 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31360 {
31361 cp_parser_error (parser, "invalid type for property");
31362 declspecs.locations[ds_typedef] = 0;
31363 }
31364
31365 prefix_attributes = declspecs.attributes;
31366 declspecs.attributes = NULL_TREE;
31367
31368 /* Keep going until we hit the `;' at the end of the declaration. */
31369 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31370 {
31371 tree attributes, first_attribute, decl;
31372 cp_declarator *declarator;
31373 cp_token *token;
31374
31375 /* Parse the declarator. */
31376 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31377 NULL, NULL, false, false);
31378
31379 /* Look for attributes that apply to the ivar. */
31380 attributes = cp_parser_attributes_opt (parser);
31381 /* Remember which attributes are prefix attributes and
31382 which are not. */
31383 first_attribute = attributes;
31384 /* Combine the attributes. */
31385 attributes = attr_chainon (prefix_attributes, attributes);
31386
31387 decl = grokfield (declarator, &declspecs,
31388 NULL_TREE, /*init_const_expr_p=*/false,
31389 NULL_TREE, attributes);
31390
31391 if (decl == error_mark_node || decl == NULL_TREE)
31392 return error_mark_node;
31393
31394 /* Reset PREFIX_ATTRIBUTES. */
31395 if (attributes != error_mark_node)
31396 {
31397 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31398 attributes = TREE_CHAIN (attributes);
31399 if (attributes)
31400 TREE_CHAIN (attributes) = NULL_TREE;
31401 }
31402
31403 DECL_CHAIN (decl) = decls;
31404 decls = decl;
31405
31406 token = cp_lexer_peek_token (parser->lexer);
31407 if (token->type == CPP_COMMA)
31408 {
31409 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31410 continue;
31411 }
31412 else
31413 break;
31414 }
31415 return decls;
31416 }
31417
31418 /* Parse an Objective-C @property declaration. The syntax is:
31419
31420 objc-property-declaration:
31421 '@property' objc-property-attributes[opt] struct-declaration ;
31422
31423 objc-property-attributes:
31424 '(' objc-property-attribute-list ')'
31425
31426 objc-property-attribute-list:
31427 objc-property-attribute
31428 objc-property-attribute-list, objc-property-attribute
31429
31430 objc-property-attribute
31431 'getter' = identifier
31432 'setter' = identifier
31433 'readonly'
31434 'readwrite'
31435 'assign'
31436 'retain'
31437 'copy'
31438 'nonatomic'
31439
31440 For example:
31441 @property NSString *name;
31442 @property (readonly) id object;
31443 @property (retain, nonatomic, getter=getTheName) id name;
31444 @property int a, b, c;
31445
31446 PS: This function is identical to
31447 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31448 static void
31449 cp_parser_objc_at_property_declaration (cp_parser *parser)
31450 {
31451 /* The following variables hold the attributes of the properties as
31452 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31453 seen. When we see an attribute, we set them to 'true' (if they
31454 are boolean properties) or to the identifier (if they have an
31455 argument, ie, for getter and setter). Note that here we only
31456 parse the list of attributes, check the syntax and accumulate the
31457 attributes that we find. objc_add_property_declaration() will
31458 then process the information. */
31459 bool property_assign = false;
31460 bool property_copy = false;
31461 tree property_getter_ident = NULL_TREE;
31462 bool property_nonatomic = false;
31463 bool property_readonly = false;
31464 bool property_readwrite = false;
31465 bool property_retain = false;
31466 tree property_setter_ident = NULL_TREE;
31467
31468 /* 'properties' is the list of properties that we read. Usually a
31469 single one, but maybe more (eg, in "@property int a, b, c;" there
31470 are three). */
31471 tree properties;
31472 location_t loc;
31473
31474 loc = cp_lexer_peek_token (parser->lexer)->location;
31475
31476 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31477
31478 /* Parse the optional attribute list... */
31479 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31480 {
31481 /* Eat the '('. */
31482 matching_parens parens;
31483 parens.consume_open (parser);
31484
31485 while (true)
31486 {
31487 bool syntax_error = false;
31488 cp_token *token = cp_lexer_peek_token (parser->lexer);
31489 enum rid keyword;
31490
31491 if (token->type != CPP_NAME)
31492 {
31493 cp_parser_error (parser, "expected identifier");
31494 break;
31495 }
31496 keyword = C_RID_CODE (token->u.value);
31497 cp_lexer_consume_token (parser->lexer);
31498 switch (keyword)
31499 {
31500 case RID_ASSIGN: property_assign = true; break;
31501 case RID_COPY: property_copy = true; break;
31502 case RID_NONATOMIC: property_nonatomic = true; break;
31503 case RID_READONLY: property_readonly = true; break;
31504 case RID_READWRITE: property_readwrite = true; break;
31505 case RID_RETAIN: property_retain = true; break;
31506
31507 case RID_GETTER:
31508 case RID_SETTER:
31509 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31510 {
31511 if (keyword == RID_GETTER)
31512 cp_parser_error (parser,
31513 "missing %<=%> (after %<getter%> attribute)");
31514 else
31515 cp_parser_error (parser,
31516 "missing %<=%> (after %<setter%> attribute)");
31517 syntax_error = true;
31518 break;
31519 }
31520 cp_lexer_consume_token (parser->lexer); /* eat the = */
31521 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31522 {
31523 cp_parser_error (parser, "expected identifier");
31524 syntax_error = true;
31525 break;
31526 }
31527 if (keyword == RID_SETTER)
31528 {
31529 if (property_setter_ident != NULL_TREE)
31530 {
31531 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31532 cp_lexer_consume_token (parser->lexer);
31533 }
31534 else
31535 property_setter_ident = cp_parser_objc_selector (parser);
31536 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31537 cp_parser_error (parser, "setter name must terminate with %<:%>");
31538 else
31539 cp_lexer_consume_token (parser->lexer);
31540 }
31541 else
31542 {
31543 if (property_getter_ident != NULL_TREE)
31544 {
31545 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31546 cp_lexer_consume_token (parser->lexer);
31547 }
31548 else
31549 property_getter_ident = cp_parser_objc_selector (parser);
31550 }
31551 break;
31552 default:
31553 cp_parser_error (parser, "unknown property attribute");
31554 syntax_error = true;
31555 break;
31556 }
31557
31558 if (syntax_error)
31559 break;
31560
31561 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31562 cp_lexer_consume_token (parser->lexer);
31563 else
31564 break;
31565 }
31566
31567 /* FIXME: "@property (setter, assign);" will generate a spurious
31568 "error: expected ‘)’ before ‘,’ token". This is because
31569 cp_parser_require, unlike the C counterpart, will produce an
31570 error even if we are in error recovery. */
31571 if (!parens.require_close (parser))
31572 {
31573 cp_parser_skip_to_closing_parenthesis (parser,
31574 /*recovering=*/true,
31575 /*or_comma=*/false,
31576 /*consume_paren=*/true);
31577 }
31578 }
31579
31580 /* ... and the property declaration(s). */
31581 properties = cp_parser_objc_struct_declaration (parser);
31582
31583 if (properties == error_mark_node)
31584 {
31585 cp_parser_skip_to_end_of_statement (parser);
31586 /* If the next token is now a `;', consume it. */
31587 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31588 cp_lexer_consume_token (parser->lexer);
31589 return;
31590 }
31591
31592 if (properties == NULL_TREE)
31593 cp_parser_error (parser, "expected identifier");
31594 else
31595 {
31596 /* Comma-separated properties are chained together in
31597 reverse order; add them one by one. */
31598 properties = nreverse (properties);
31599
31600 for (; properties; properties = TREE_CHAIN (properties))
31601 objc_add_property_declaration (loc, copy_node (properties),
31602 property_readonly, property_readwrite,
31603 property_assign, property_retain,
31604 property_copy, property_nonatomic,
31605 property_getter_ident, property_setter_ident);
31606 }
31607
31608 cp_parser_consume_semicolon_at_end_of_statement (parser);
31609 }
31610
31611 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31612
31613 objc-synthesize-declaration:
31614 @synthesize objc-synthesize-identifier-list ;
31615
31616 objc-synthesize-identifier-list:
31617 objc-synthesize-identifier
31618 objc-synthesize-identifier-list, objc-synthesize-identifier
31619
31620 objc-synthesize-identifier
31621 identifier
31622 identifier = identifier
31623
31624 For example:
31625 @synthesize MyProperty;
31626 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31627
31628 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31629 for C. Keep them in sync.
31630 */
31631 static void
31632 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31633 {
31634 tree list = NULL_TREE;
31635 location_t loc;
31636 loc = cp_lexer_peek_token (parser->lexer)->location;
31637
31638 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31639 while (true)
31640 {
31641 tree property, ivar;
31642 property = cp_parser_identifier (parser);
31643 if (property == error_mark_node)
31644 {
31645 cp_parser_consume_semicolon_at_end_of_statement (parser);
31646 return;
31647 }
31648 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31649 {
31650 cp_lexer_consume_token (parser->lexer);
31651 ivar = cp_parser_identifier (parser);
31652 if (ivar == error_mark_node)
31653 {
31654 cp_parser_consume_semicolon_at_end_of_statement (parser);
31655 return;
31656 }
31657 }
31658 else
31659 ivar = NULL_TREE;
31660 list = chainon (list, build_tree_list (ivar, property));
31661 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31662 cp_lexer_consume_token (parser->lexer);
31663 else
31664 break;
31665 }
31666 cp_parser_consume_semicolon_at_end_of_statement (parser);
31667 objc_add_synthesize_declaration (loc, list);
31668 }
31669
31670 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31671
31672 objc-dynamic-declaration:
31673 @dynamic identifier-list ;
31674
31675 For example:
31676 @dynamic MyProperty;
31677 @dynamic MyProperty, AnotherProperty;
31678
31679 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31680 for C. Keep them in sync.
31681 */
31682 static void
31683 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31684 {
31685 tree list = NULL_TREE;
31686 location_t loc;
31687 loc = cp_lexer_peek_token (parser->lexer)->location;
31688
31689 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31690 while (true)
31691 {
31692 tree property;
31693 property = cp_parser_identifier (parser);
31694 if (property == error_mark_node)
31695 {
31696 cp_parser_consume_semicolon_at_end_of_statement (parser);
31697 return;
31698 }
31699 list = chainon (list, build_tree_list (NULL, property));
31700 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31701 cp_lexer_consume_token (parser->lexer);
31702 else
31703 break;
31704 }
31705 cp_parser_consume_semicolon_at_end_of_statement (parser);
31706 objc_add_dynamic_declaration (loc, list);
31707 }
31708
31709 \f
31710 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31711
31712 /* Returns name of the next clause.
31713 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31714 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31715 returned and the token is consumed. */
31716
31717 static pragma_omp_clause
31718 cp_parser_omp_clause_name (cp_parser *parser)
31719 {
31720 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31721
31722 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31723 result = PRAGMA_OACC_CLAUSE_AUTO;
31724 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31725 result = PRAGMA_OMP_CLAUSE_IF;
31726 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31727 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31728 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31729 result = PRAGMA_OACC_CLAUSE_DELETE;
31730 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31731 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31732 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31733 result = PRAGMA_OMP_CLAUSE_FOR;
31734 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31735 {
31736 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31737 const char *p = IDENTIFIER_POINTER (id);
31738
31739 switch (p[0])
31740 {
31741 case 'a':
31742 if (!strcmp ("aligned", p))
31743 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31744 else if (!strcmp ("async", p))
31745 result = PRAGMA_OACC_CLAUSE_ASYNC;
31746 break;
31747 case 'c':
31748 if (!strcmp ("collapse", p))
31749 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31750 else if (!strcmp ("copy", p))
31751 result = PRAGMA_OACC_CLAUSE_COPY;
31752 else if (!strcmp ("copyin", p))
31753 result = PRAGMA_OMP_CLAUSE_COPYIN;
31754 else if (!strcmp ("copyout", p))
31755 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31756 else if (!strcmp ("copyprivate", p))
31757 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31758 else if (!strcmp ("create", p))
31759 result = PRAGMA_OACC_CLAUSE_CREATE;
31760 break;
31761 case 'd':
31762 if (!strcmp ("defaultmap", p))
31763 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31764 else if (!strcmp ("depend", p))
31765 result = PRAGMA_OMP_CLAUSE_DEPEND;
31766 else if (!strcmp ("device", p))
31767 result = PRAGMA_OMP_CLAUSE_DEVICE;
31768 else if (!strcmp ("deviceptr", p))
31769 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31770 else if (!strcmp ("device_resident", p))
31771 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31772 else if (!strcmp ("dist_schedule", p))
31773 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31774 break;
31775 case 'f':
31776 if (!strcmp ("final", p))
31777 result = PRAGMA_OMP_CLAUSE_FINAL;
31778 else if (!strcmp ("finalize", p))
31779 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31780 else if (!strcmp ("firstprivate", p))
31781 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31782 else if (!strcmp ("from", p))
31783 result = PRAGMA_OMP_CLAUSE_FROM;
31784 break;
31785 case 'g':
31786 if (!strcmp ("gang", p))
31787 result = PRAGMA_OACC_CLAUSE_GANG;
31788 else if (!strcmp ("grainsize", p))
31789 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31790 break;
31791 case 'h':
31792 if (!strcmp ("hint", p))
31793 result = PRAGMA_OMP_CLAUSE_HINT;
31794 else if (!strcmp ("host", p))
31795 result = PRAGMA_OACC_CLAUSE_HOST;
31796 break;
31797 case 'i':
31798 if (!strcmp ("if_present", p))
31799 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31800 else if (!strcmp ("in_reduction", p))
31801 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
31802 else if (!strcmp ("inbranch", p))
31803 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31804 else if (!strcmp ("independent", p))
31805 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31806 else if (!strcmp ("is_device_ptr", p))
31807 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31808 break;
31809 case 'l':
31810 if (!strcmp ("lastprivate", p))
31811 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31812 else if (!strcmp ("linear", p))
31813 result = PRAGMA_OMP_CLAUSE_LINEAR;
31814 else if (!strcmp ("link", p))
31815 result = PRAGMA_OMP_CLAUSE_LINK;
31816 break;
31817 case 'm':
31818 if (!strcmp ("map", p))
31819 result = PRAGMA_OMP_CLAUSE_MAP;
31820 else if (!strcmp ("mergeable", p))
31821 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31822 break;
31823 case 'n':
31824 if (!strcmp ("nogroup", p))
31825 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31826 else if (!strcmp ("nontemporal", p))
31827 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
31828 else if (!strcmp ("notinbranch", p))
31829 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31830 else if (!strcmp ("nowait", p))
31831 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31832 else if (!strcmp ("num_gangs", p))
31833 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31834 else if (!strcmp ("num_tasks", p))
31835 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31836 else if (!strcmp ("num_teams", p))
31837 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31838 else if (!strcmp ("num_threads", p))
31839 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31840 else if (!strcmp ("num_workers", p))
31841 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31842 break;
31843 case 'o':
31844 if (!strcmp ("ordered", p))
31845 result = PRAGMA_OMP_CLAUSE_ORDERED;
31846 break;
31847 case 'p':
31848 if (!strcmp ("parallel", p))
31849 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31850 else if (!strcmp ("present", p))
31851 result = PRAGMA_OACC_CLAUSE_PRESENT;
31852 else if (!strcmp ("present_or_copy", p)
31853 || !strcmp ("pcopy", p))
31854 result = PRAGMA_OACC_CLAUSE_COPY;
31855 else if (!strcmp ("present_or_copyin", p)
31856 || !strcmp ("pcopyin", p))
31857 result = PRAGMA_OACC_CLAUSE_COPYIN;
31858 else if (!strcmp ("present_or_copyout", p)
31859 || !strcmp ("pcopyout", p))
31860 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31861 else if (!strcmp ("present_or_create", p)
31862 || !strcmp ("pcreate", p))
31863 result = PRAGMA_OACC_CLAUSE_CREATE;
31864 else if (!strcmp ("priority", p))
31865 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31866 else if (!strcmp ("proc_bind", p))
31867 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31868 break;
31869 case 'r':
31870 if (!strcmp ("reduction", p))
31871 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31872 break;
31873 case 's':
31874 if (!strcmp ("safelen", p))
31875 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31876 else if (!strcmp ("schedule", p))
31877 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31878 else if (!strcmp ("sections", p))
31879 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31880 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
31881 result = PRAGMA_OACC_CLAUSE_HOST;
31882 else if (!strcmp ("seq", p))
31883 result = PRAGMA_OACC_CLAUSE_SEQ;
31884 else if (!strcmp ("shared", p))
31885 result = PRAGMA_OMP_CLAUSE_SHARED;
31886 else if (!strcmp ("simd", p))
31887 result = PRAGMA_OMP_CLAUSE_SIMD;
31888 else if (!strcmp ("simdlen", p))
31889 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31890 break;
31891 case 't':
31892 if (!strcmp ("task_reduction", p))
31893 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
31894 else if (!strcmp ("taskgroup", p))
31895 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31896 else if (!strcmp ("thread_limit", p))
31897 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31898 else if (!strcmp ("threads", p))
31899 result = PRAGMA_OMP_CLAUSE_THREADS;
31900 else if (!strcmp ("tile", p))
31901 result = PRAGMA_OACC_CLAUSE_TILE;
31902 else if (!strcmp ("to", p))
31903 result = PRAGMA_OMP_CLAUSE_TO;
31904 break;
31905 case 'u':
31906 if (!strcmp ("uniform", p))
31907 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31908 else if (!strcmp ("untied", p))
31909 result = PRAGMA_OMP_CLAUSE_UNTIED;
31910 else if (!strcmp ("use_device", p))
31911 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31912 else if (!strcmp ("use_device_ptr", p))
31913 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31914 break;
31915 case 'v':
31916 if (!strcmp ("vector", p))
31917 result = PRAGMA_OACC_CLAUSE_VECTOR;
31918 else if (!strcmp ("vector_length", p))
31919 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31920 break;
31921 case 'w':
31922 if (!strcmp ("wait", p))
31923 result = PRAGMA_OACC_CLAUSE_WAIT;
31924 else if (!strcmp ("worker", p))
31925 result = PRAGMA_OACC_CLAUSE_WORKER;
31926 break;
31927 }
31928 }
31929
31930 if (result != PRAGMA_OMP_CLAUSE_NONE)
31931 cp_lexer_consume_token (parser->lexer);
31932
31933 return result;
31934 }
31935
31936 /* Validate that a clause of the given type does not already exist. */
31937
31938 static void
31939 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31940 const char *name, location_t location)
31941 {
31942 tree c;
31943
31944 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31945 if (OMP_CLAUSE_CODE (c) == code)
31946 {
31947 error_at (location, "too many %qs clauses", name);
31948 break;
31949 }
31950 }
31951
31952 /* OpenMP 2.5:
31953 variable-list:
31954 identifier
31955 variable-list , identifier
31956
31957 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31958 colon). An opening parenthesis will have been consumed by the caller.
31959
31960 If KIND is nonzero, create the appropriate node and install the decl
31961 in OMP_CLAUSE_DECL and add the node to the head of the list.
31962
31963 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31964 return the list created.
31965
31966 COLON can be NULL if only closing parenthesis should end the list,
31967 or pointer to bool which will receive false if the list is terminated
31968 by closing parenthesis or true if the list is terminated by colon. */
31969
31970 static tree
31971 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31972 tree list, bool *colon)
31973 {
31974 cp_token *token;
31975 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31976 if (colon)
31977 {
31978 parser->colon_corrects_to_scope_p = false;
31979 *colon = false;
31980 }
31981 while (1)
31982 {
31983 tree name, decl;
31984
31985 if (kind == OMP_CLAUSE_DEPEND)
31986 cp_parser_parse_tentatively (parser);
31987 token = cp_lexer_peek_token (parser->lexer);
31988 if (kind != 0
31989 && current_class_ptr
31990 && cp_parser_is_keyword (token, RID_THIS))
31991 {
31992 decl = finish_this_expr ();
31993 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31994 || CONVERT_EXPR_P (decl))
31995 decl = TREE_OPERAND (decl, 0);
31996 cp_lexer_consume_token (parser->lexer);
31997 }
31998 else
31999 {
32000 name = cp_parser_id_expression (parser, /*template_p=*/false,
32001 /*check_dependency_p=*/true,
32002 /*template_p=*/NULL,
32003 /*declarator_p=*/false,
32004 /*optional_p=*/false);
32005 if (name == error_mark_node)
32006 {
32007 if (kind == OMP_CLAUSE_DEPEND
32008 && cp_parser_simulate_error (parser))
32009 goto depend_lvalue;
32010 goto skip_comma;
32011 }
32012
32013 if (identifier_p (name))
32014 decl = cp_parser_lookup_name_simple (parser, name, token->location);
32015 else
32016 decl = name;
32017 if (decl == error_mark_node)
32018 {
32019 if (kind == OMP_CLAUSE_DEPEND
32020 && cp_parser_simulate_error (parser))
32021 goto depend_lvalue;
32022 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32023 token->location);
32024 }
32025 }
32026 if (decl == error_mark_node)
32027 ;
32028 else if (kind != 0)
32029 {
32030 switch (kind)
32031 {
32032 case OMP_CLAUSE__CACHE_:
32033 /* The OpenACC cache directive explicitly only allows "array
32034 elements or subarrays". */
32035 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32036 {
32037 error_at (token->location, "expected %<[%>");
32038 decl = error_mark_node;
32039 break;
32040 }
32041 /* FALLTHROUGH. */
32042 case OMP_CLAUSE_MAP:
32043 case OMP_CLAUSE_FROM:
32044 case OMP_CLAUSE_TO:
32045 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32046 {
32047 location_t loc
32048 = cp_lexer_peek_token (parser->lexer)->location;
32049 cp_id_kind idk = CP_ID_KIND_NONE;
32050 cp_lexer_consume_token (parser->lexer);
32051 decl = convert_from_reference (decl);
32052 decl
32053 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32054 decl, false,
32055 &idk, loc);
32056 }
32057 /* FALLTHROUGH. */
32058 case OMP_CLAUSE_DEPEND:
32059 case OMP_CLAUSE_REDUCTION:
32060 case OMP_CLAUSE_IN_REDUCTION:
32061 case OMP_CLAUSE_TASK_REDUCTION:
32062 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32063 {
32064 tree low_bound = NULL_TREE, length = NULL_TREE;
32065
32066 parser->colon_corrects_to_scope_p = false;
32067 cp_lexer_consume_token (parser->lexer);
32068 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32069 low_bound = cp_parser_expression (parser);
32070 if (!colon)
32071 parser->colon_corrects_to_scope_p
32072 = saved_colon_corrects_to_scope_p;
32073 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32074 length = integer_one_node;
32075 else
32076 {
32077 /* Look for `:'. */
32078 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32079 {
32080 if (kind == OMP_CLAUSE_DEPEND
32081 && cp_parser_simulate_error (parser))
32082 goto depend_lvalue;
32083 goto skip_comma;
32084 }
32085 if (kind == OMP_CLAUSE_DEPEND)
32086 cp_parser_commit_to_tentative_parse (parser);
32087 if (!cp_lexer_next_token_is (parser->lexer,
32088 CPP_CLOSE_SQUARE))
32089 length = cp_parser_expression (parser);
32090 }
32091 /* Look for the closing `]'. */
32092 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32093 RT_CLOSE_SQUARE))
32094 {
32095 if (kind == OMP_CLAUSE_DEPEND
32096 && cp_parser_simulate_error (parser))
32097 goto depend_lvalue;
32098 goto skip_comma;
32099 }
32100
32101 decl = tree_cons (low_bound, length, decl);
32102 }
32103 break;
32104 default:
32105 break;
32106 }
32107
32108 if (kind == OMP_CLAUSE_DEPEND)
32109 {
32110 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32111 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32112 && cp_parser_simulate_error (parser))
32113 {
32114 depend_lvalue:
32115 cp_parser_abort_tentative_parse (parser);
32116 decl = cp_parser_assignment_expression (parser, NULL,
32117 false, false);
32118 }
32119 else
32120 cp_parser_parse_definitely (parser);
32121 }
32122
32123 tree u = build_omp_clause (token->location, kind);
32124 OMP_CLAUSE_DECL (u) = decl;
32125 OMP_CLAUSE_CHAIN (u) = list;
32126 list = u;
32127 }
32128 else
32129 list = tree_cons (decl, NULL_TREE, list);
32130
32131 get_comma:
32132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32133 break;
32134 cp_lexer_consume_token (parser->lexer);
32135 }
32136
32137 if (colon)
32138 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32139
32140 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32141 {
32142 *colon = true;
32143 cp_parser_require (parser, CPP_COLON, RT_COLON);
32144 return list;
32145 }
32146
32147 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32148 {
32149 int ending;
32150
32151 /* Try to resync to an unnested comma. Copied from
32152 cp_parser_parenthesized_expression_list. */
32153 skip_comma:
32154 if (colon)
32155 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32156 ending = cp_parser_skip_to_closing_parenthesis (parser,
32157 /*recovering=*/true,
32158 /*or_comma=*/true,
32159 /*consume_paren=*/true);
32160 if (ending < 0)
32161 goto get_comma;
32162 }
32163
32164 return list;
32165 }
32166
32167 /* Similarly, but expect leading and trailing parenthesis. This is a very
32168 common case for omp clauses. */
32169
32170 static tree
32171 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32172 {
32173 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32174 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32175 return list;
32176 }
32177
32178 /* OpenACC 2.0:
32179 copy ( variable-list )
32180 copyin ( variable-list )
32181 copyout ( variable-list )
32182 create ( variable-list )
32183 delete ( variable-list )
32184 present ( variable-list ) */
32185
32186 static tree
32187 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32188 tree list)
32189 {
32190 enum gomp_map_kind kind;
32191 switch (c_kind)
32192 {
32193 case PRAGMA_OACC_CLAUSE_COPY:
32194 kind = GOMP_MAP_TOFROM;
32195 break;
32196 case PRAGMA_OACC_CLAUSE_COPYIN:
32197 kind = GOMP_MAP_TO;
32198 break;
32199 case PRAGMA_OACC_CLAUSE_COPYOUT:
32200 kind = GOMP_MAP_FROM;
32201 break;
32202 case PRAGMA_OACC_CLAUSE_CREATE:
32203 kind = GOMP_MAP_ALLOC;
32204 break;
32205 case PRAGMA_OACC_CLAUSE_DELETE:
32206 kind = GOMP_MAP_RELEASE;
32207 break;
32208 case PRAGMA_OACC_CLAUSE_DEVICE:
32209 kind = GOMP_MAP_FORCE_TO;
32210 break;
32211 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32212 kind = GOMP_MAP_DEVICE_RESIDENT;
32213 break;
32214 case PRAGMA_OACC_CLAUSE_HOST:
32215 kind = GOMP_MAP_FORCE_FROM;
32216 break;
32217 case PRAGMA_OACC_CLAUSE_LINK:
32218 kind = GOMP_MAP_LINK;
32219 break;
32220 case PRAGMA_OACC_CLAUSE_PRESENT:
32221 kind = GOMP_MAP_FORCE_PRESENT;
32222 break;
32223 default:
32224 gcc_unreachable ();
32225 }
32226 tree nl, c;
32227 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32228
32229 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32230 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32231
32232 return nl;
32233 }
32234
32235 /* OpenACC 2.0:
32236 deviceptr ( variable-list ) */
32237
32238 static tree
32239 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32240 {
32241 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32242 tree vars, t;
32243
32244 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32245 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32246 variable-list must only allow for pointer variables. */
32247 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32248 for (t = vars; t; t = TREE_CHAIN (t))
32249 {
32250 tree v = TREE_PURPOSE (t);
32251 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32252 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32253 OMP_CLAUSE_DECL (u) = v;
32254 OMP_CLAUSE_CHAIN (u) = list;
32255 list = u;
32256 }
32257
32258 return list;
32259 }
32260
32261 /* OpenACC 2.5:
32262 auto
32263 finalize
32264 independent
32265 nohost
32266 seq */
32267
32268 static tree
32269 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
32270 enum omp_clause_code code,
32271 tree list, location_t location)
32272 {
32273 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32274 tree c = build_omp_clause (location, code);
32275 OMP_CLAUSE_CHAIN (c) = list;
32276 return c;
32277 }
32278
32279 /* OpenACC:
32280 num_gangs ( expression )
32281 num_workers ( expression )
32282 vector_length ( expression ) */
32283
32284 static tree
32285 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32286 const char *str, tree list)
32287 {
32288 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32289
32290 matching_parens parens;
32291 if (!parens.require_open (parser))
32292 return list;
32293
32294 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32295
32296 if (t == error_mark_node
32297 || !parens.require_close (parser))
32298 {
32299 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32300 /*or_comma=*/false,
32301 /*consume_paren=*/true);
32302 return list;
32303 }
32304
32305 check_no_duplicate_clause (list, code, str, loc);
32306
32307 tree c = build_omp_clause (loc, code);
32308 OMP_CLAUSE_OPERAND (c, 0) = t;
32309 OMP_CLAUSE_CHAIN (c) = list;
32310 return c;
32311 }
32312
32313 /* OpenACC:
32314
32315 gang [( gang-arg-list )]
32316 worker [( [num:] int-expr )]
32317 vector [( [length:] int-expr )]
32318
32319 where gang-arg is one of:
32320
32321 [num:] int-expr
32322 static: size-expr
32323
32324 and size-expr may be:
32325
32326 *
32327 int-expr
32328 */
32329
32330 static tree
32331 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
32332 const char *str, tree list)
32333 {
32334 const char *id = "num";
32335 cp_lexer *lexer = parser->lexer;
32336 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32337 location_t loc = cp_lexer_peek_token (lexer)->location;
32338
32339 if (kind == OMP_CLAUSE_VECTOR)
32340 id = "length";
32341
32342 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32343 {
32344 matching_parens parens;
32345 parens.consume_open (parser);
32346
32347 do
32348 {
32349 cp_token *next = cp_lexer_peek_token (lexer);
32350 int idx = 0;
32351
32352 /* Gang static argument. */
32353 if (kind == OMP_CLAUSE_GANG
32354 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32355 {
32356 cp_lexer_consume_token (lexer);
32357
32358 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32359 goto cleanup_error;
32360
32361 idx = 1;
32362 if (ops[idx] != NULL)
32363 {
32364 cp_parser_error (parser, "too many %<static%> arguments");
32365 goto cleanup_error;
32366 }
32367
32368 /* Check for the '*' argument. */
32369 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32370 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32371 || cp_lexer_nth_token_is (parser->lexer, 2,
32372 CPP_CLOSE_PAREN)))
32373 {
32374 cp_lexer_consume_token (lexer);
32375 ops[idx] = integer_minus_one_node;
32376
32377 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32378 {
32379 cp_lexer_consume_token (lexer);
32380 continue;
32381 }
32382 else break;
32383 }
32384 }
32385 /* Worker num: argument and vector length: arguments. */
32386 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32387 && id_equal (next->u.value, id)
32388 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32389 {
32390 cp_lexer_consume_token (lexer); /* id */
32391 cp_lexer_consume_token (lexer); /* ':' */
32392 }
32393
32394 /* Now collect the actual argument. */
32395 if (ops[idx] != NULL_TREE)
32396 {
32397 cp_parser_error (parser, "unexpected argument");
32398 goto cleanup_error;
32399 }
32400
32401 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32402 false);
32403 if (expr == error_mark_node)
32404 goto cleanup_error;
32405
32406 mark_exp_read (expr);
32407 ops[idx] = expr;
32408
32409 if (kind == OMP_CLAUSE_GANG
32410 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32411 {
32412 cp_lexer_consume_token (lexer);
32413 continue;
32414 }
32415 break;
32416 }
32417 while (1);
32418
32419 if (!parens.require_close (parser))
32420 goto cleanup_error;
32421 }
32422
32423 check_no_duplicate_clause (list, kind, str, loc);
32424
32425 c = build_omp_clause (loc, kind);
32426
32427 if (ops[1])
32428 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32429
32430 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32431 OMP_CLAUSE_CHAIN (c) = list;
32432
32433 return c;
32434
32435 cleanup_error:
32436 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32437 return list;
32438 }
32439
32440 /* OpenACC 2.0:
32441 tile ( size-expr-list ) */
32442
32443 static tree
32444 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32445 {
32446 tree c, expr = error_mark_node;
32447 tree tile = NULL_TREE;
32448
32449 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32450 so, but the spec authors never considered such a case and have
32451 differing opinions on what it might mean, including 'not
32452 allowed'.) */
32453 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32454 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32455 clause_loc);
32456
32457 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32458 return list;
32459
32460 do
32461 {
32462 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32463 return list;
32464
32465 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32466 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32467 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32468 {
32469 cp_lexer_consume_token (parser->lexer);
32470 expr = integer_zero_node;
32471 }
32472 else
32473 expr = cp_parser_constant_expression (parser);
32474
32475 tile = tree_cons (NULL_TREE, expr, tile);
32476 }
32477 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32478
32479 /* Consume the trailing ')'. */
32480 cp_lexer_consume_token (parser->lexer);
32481
32482 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32483 tile = nreverse (tile);
32484 OMP_CLAUSE_TILE_LIST (c) = tile;
32485 OMP_CLAUSE_CHAIN (c) = list;
32486 return c;
32487 }
32488
32489 /* OpenACC 2.0
32490 Parse wait clause or directive parameters. */
32491
32492 static tree
32493 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32494 {
32495 vec<tree, va_gc> *args;
32496 tree t, args_tree;
32497
32498 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32499 /*cast_p=*/false,
32500 /*allow_expansion_p=*/true,
32501 /*non_constant_p=*/NULL);
32502
32503 if (args == NULL || args->length () == 0)
32504 {
32505 cp_parser_error (parser, "expected integer expression before ')'");
32506 if (args != NULL)
32507 release_tree_vector (args);
32508 return list;
32509 }
32510
32511 args_tree = build_tree_list_vec (args);
32512
32513 release_tree_vector (args);
32514
32515 for (t = args_tree; t; t = TREE_CHAIN (t))
32516 {
32517 tree targ = TREE_VALUE (t);
32518
32519 if (targ != error_mark_node)
32520 {
32521 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32522 error ("%<wait%> expression must be integral");
32523 else
32524 {
32525 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32526
32527 targ = mark_rvalue_use (targ);
32528 OMP_CLAUSE_DECL (c) = targ;
32529 OMP_CLAUSE_CHAIN (c) = list;
32530 list = c;
32531 }
32532 }
32533 }
32534
32535 return list;
32536 }
32537
32538 /* OpenACC:
32539 wait ( int-expr-list ) */
32540
32541 static tree
32542 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32543 {
32544 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32545
32546 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32547 return list;
32548
32549 list = cp_parser_oacc_wait_list (parser, location, list);
32550
32551 return list;
32552 }
32553
32554 /* OpenMP 3.0:
32555 collapse ( constant-expression ) */
32556
32557 static tree
32558 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32559 {
32560 tree c, num;
32561 location_t loc;
32562 HOST_WIDE_INT n;
32563
32564 loc = cp_lexer_peek_token (parser->lexer)->location;
32565 matching_parens parens;
32566 if (!parens.require_open (parser))
32567 return list;
32568
32569 num = cp_parser_constant_expression (parser);
32570
32571 if (!parens.require_close (parser))
32572 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32573 /*or_comma=*/false,
32574 /*consume_paren=*/true);
32575
32576 if (num == error_mark_node)
32577 return list;
32578 num = fold_non_dependent_expr (num);
32579 if (!tree_fits_shwi_p (num)
32580 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32581 || (n = tree_to_shwi (num)) <= 0
32582 || (int) n != n)
32583 {
32584 error_at (loc, "collapse argument needs positive constant integer expression");
32585 return list;
32586 }
32587
32588 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32589 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32590 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32591 OMP_CLAUSE_CHAIN (c) = list;
32592 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32593
32594 return c;
32595 }
32596
32597 /* OpenMP 2.5:
32598 default ( none | shared )
32599
32600 OpenACC:
32601 default ( none | present ) */
32602
32603 static tree
32604 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32605 location_t location, bool is_oacc)
32606 {
32607 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32608 tree c;
32609
32610 matching_parens parens;
32611 if (!parens.require_open (parser))
32612 return list;
32613 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32614 {
32615 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32616 const char *p = IDENTIFIER_POINTER (id);
32617
32618 switch (p[0])
32619 {
32620 case 'n':
32621 if (strcmp ("none", p) != 0)
32622 goto invalid_kind;
32623 kind = OMP_CLAUSE_DEFAULT_NONE;
32624 break;
32625
32626 case 'p':
32627 if (strcmp ("present", p) != 0 || !is_oacc)
32628 goto invalid_kind;
32629 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32630 break;
32631
32632 case 's':
32633 if (strcmp ("shared", p) != 0 || is_oacc)
32634 goto invalid_kind;
32635 kind = OMP_CLAUSE_DEFAULT_SHARED;
32636 break;
32637
32638 default:
32639 goto invalid_kind;
32640 }
32641
32642 cp_lexer_consume_token (parser->lexer);
32643 }
32644 else
32645 {
32646 invalid_kind:
32647 if (is_oacc)
32648 cp_parser_error (parser, "expected %<none%> or %<present%>");
32649 else
32650 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32651 }
32652
32653 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32654 || !parens.require_close (parser))
32655 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32656 /*or_comma=*/false,
32657 /*consume_paren=*/true);
32658
32659 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32660 return list;
32661
32662 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32663 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32664 OMP_CLAUSE_CHAIN (c) = list;
32665 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32666
32667 return c;
32668 }
32669
32670 /* OpenMP 3.1:
32671 final ( expression ) */
32672
32673 static tree
32674 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32675 {
32676 tree t, c;
32677
32678 matching_parens parens;
32679 if (!parens.require_open (parser))
32680 return list;
32681
32682 t = cp_parser_assignment_expression (parser);
32683
32684 if (t == error_mark_node
32685 || !parens.require_close (parser))
32686 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32687 /*or_comma=*/false,
32688 /*consume_paren=*/true);
32689
32690 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32691
32692 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32693 OMP_CLAUSE_FINAL_EXPR (c) = t;
32694 OMP_CLAUSE_CHAIN (c) = list;
32695
32696 return c;
32697 }
32698
32699 /* OpenMP 2.5:
32700 if ( expression )
32701
32702 OpenMP 4.5:
32703 if ( directive-name-modifier : expression )
32704
32705 directive-name-modifier:
32706 parallel | task | taskloop | target data | target | target update
32707 | target enter data | target exit data
32708
32709 OpenMP 5.0:
32710 directive-name-modifier:
32711 ... | simd | cancel */
32712
32713 static tree
32714 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32715 bool is_omp)
32716 {
32717 tree t, c;
32718 enum tree_code if_modifier = ERROR_MARK;
32719
32720 matching_parens parens;
32721 if (!parens.require_open (parser))
32722 return list;
32723
32724 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32725 {
32726 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32727 const char *p = IDENTIFIER_POINTER (id);
32728 int n = 2;
32729
32730 if (strcmp ("cancel", p) == 0)
32731 if_modifier = VOID_CST;
32732 else if (strcmp ("parallel", p) == 0)
32733 if_modifier = OMP_PARALLEL;
32734 else if (strcmp ("simd", p) == 0)
32735 if_modifier = OMP_SIMD;
32736 else if (strcmp ("task", p) == 0)
32737 if_modifier = OMP_TASK;
32738 else if (strcmp ("taskloop", p) == 0)
32739 if_modifier = OMP_TASKLOOP;
32740 else if (strcmp ("target", p) == 0)
32741 {
32742 if_modifier = OMP_TARGET;
32743 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32744 {
32745 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32746 p = IDENTIFIER_POINTER (id);
32747 if (strcmp ("data", p) == 0)
32748 if_modifier = OMP_TARGET_DATA;
32749 else if (strcmp ("update", p) == 0)
32750 if_modifier = OMP_TARGET_UPDATE;
32751 else if (strcmp ("enter", p) == 0)
32752 if_modifier = OMP_TARGET_ENTER_DATA;
32753 else if (strcmp ("exit", p) == 0)
32754 if_modifier = OMP_TARGET_EXIT_DATA;
32755 if (if_modifier != OMP_TARGET)
32756 n = 3;
32757 else
32758 {
32759 location_t loc
32760 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32761 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32762 "or %<exit%>");
32763 if_modifier = ERROR_MARK;
32764 }
32765 if (if_modifier == OMP_TARGET_ENTER_DATA
32766 || if_modifier == OMP_TARGET_EXIT_DATA)
32767 {
32768 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32769 {
32770 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32771 p = IDENTIFIER_POINTER (id);
32772 if (strcmp ("data", p) == 0)
32773 n = 4;
32774 }
32775 if (n != 4)
32776 {
32777 location_t loc
32778 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32779 error_at (loc, "expected %<data%>");
32780 if_modifier = ERROR_MARK;
32781 }
32782 }
32783 }
32784 }
32785 if (if_modifier != ERROR_MARK)
32786 {
32787 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32788 {
32789 while (n-- > 0)
32790 cp_lexer_consume_token (parser->lexer);
32791 }
32792 else
32793 {
32794 if (n > 2)
32795 {
32796 location_t loc
32797 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32798 error_at (loc, "expected %<:%>");
32799 }
32800 if_modifier = ERROR_MARK;
32801 }
32802 }
32803 }
32804
32805 t = cp_parser_assignment_expression (parser);
32806
32807 if (t == error_mark_node
32808 || !parens.require_close (parser))
32809 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32810 /*or_comma=*/false,
32811 /*consume_paren=*/true);
32812
32813 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32814 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32815 {
32816 if (if_modifier != ERROR_MARK
32817 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32818 {
32819 const char *p = NULL;
32820 switch (if_modifier)
32821 {
32822 case VOID_CST: p = "cancel"; break;
32823 case OMP_PARALLEL: p = "parallel"; break;
32824 case OMP_SIMD: p = "simd"; break;
32825 case OMP_TASK: p = "task"; break;
32826 case OMP_TASKLOOP: p = "taskloop"; break;
32827 case OMP_TARGET_DATA: p = "target data"; break;
32828 case OMP_TARGET: p = "target"; break;
32829 case OMP_TARGET_UPDATE: p = "target update"; break;
32830 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32831 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32832 default: gcc_unreachable ();
32833 }
32834 error_at (location, "too many %<if%> clauses with %qs modifier",
32835 p);
32836 return list;
32837 }
32838 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32839 {
32840 if (!is_omp)
32841 error_at (location, "too many %<if%> clauses");
32842 else
32843 error_at (location, "too many %<if%> clauses without modifier");
32844 return list;
32845 }
32846 else if (if_modifier == ERROR_MARK
32847 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32848 {
32849 error_at (location, "if any %<if%> clause has modifier, then all "
32850 "%<if%> clauses have to use modifier");
32851 return list;
32852 }
32853 }
32854
32855 c = build_omp_clause (location, OMP_CLAUSE_IF);
32856 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32857 OMP_CLAUSE_IF_EXPR (c) = t;
32858 OMP_CLAUSE_CHAIN (c) = list;
32859
32860 return c;
32861 }
32862
32863 /* OpenMP 3.1:
32864 mergeable */
32865
32866 static tree
32867 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32868 tree list, location_t location)
32869 {
32870 tree c;
32871
32872 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32873 location);
32874
32875 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32876 OMP_CLAUSE_CHAIN (c) = list;
32877 return c;
32878 }
32879
32880 /* OpenMP 2.5:
32881 nowait */
32882
32883 static tree
32884 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32885 tree list, location_t location)
32886 {
32887 tree c;
32888
32889 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32890
32891 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32892 OMP_CLAUSE_CHAIN (c) = list;
32893 return c;
32894 }
32895
32896 /* OpenMP 2.5:
32897 num_threads ( expression ) */
32898
32899 static tree
32900 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32901 location_t location)
32902 {
32903 tree t, c;
32904
32905 matching_parens parens;
32906 if (!parens.require_open (parser))
32907 return list;
32908
32909 t = cp_parser_assignment_expression (parser);
32910
32911 if (t == error_mark_node
32912 || !parens.require_close (parser))
32913 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32914 /*or_comma=*/false,
32915 /*consume_paren=*/true);
32916
32917 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32918 "num_threads", location);
32919
32920 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32921 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32922 OMP_CLAUSE_CHAIN (c) = list;
32923
32924 return c;
32925 }
32926
32927 /* OpenMP 4.5:
32928 num_tasks ( expression ) */
32929
32930 static tree
32931 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32932 location_t location)
32933 {
32934 tree t, c;
32935
32936 matching_parens parens;
32937 if (!parens.require_open (parser))
32938 return list;
32939
32940 t = cp_parser_assignment_expression (parser);
32941
32942 if (t == error_mark_node
32943 || !parens.require_close (parser))
32944 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32945 /*or_comma=*/false,
32946 /*consume_paren=*/true);
32947
32948 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32949 "num_tasks", location);
32950
32951 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32952 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32953 OMP_CLAUSE_CHAIN (c) = list;
32954
32955 return c;
32956 }
32957
32958 /* OpenMP 4.5:
32959 grainsize ( expression ) */
32960
32961 static tree
32962 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32963 location_t location)
32964 {
32965 tree t, c;
32966
32967 matching_parens parens;
32968 if (!parens.require_open (parser))
32969 return list;
32970
32971 t = cp_parser_assignment_expression (parser);
32972
32973 if (t == error_mark_node
32974 || !parens.require_close (parser))
32975 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32976 /*or_comma=*/false,
32977 /*consume_paren=*/true);
32978
32979 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32980 "grainsize", location);
32981
32982 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32983 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32984 OMP_CLAUSE_CHAIN (c) = list;
32985
32986 return c;
32987 }
32988
32989 /* OpenMP 4.5:
32990 priority ( expression ) */
32991
32992 static tree
32993 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32994 location_t location)
32995 {
32996 tree t, c;
32997
32998 matching_parens parens;
32999 if (!parens.require_open (parser))
33000 return list;
33001
33002 t = cp_parser_assignment_expression (parser);
33003
33004 if (t == error_mark_node
33005 || !parens.require_close (parser))
33006 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33007 /*or_comma=*/false,
33008 /*consume_paren=*/true);
33009
33010 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33011 "priority", location);
33012
33013 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33014 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33015 OMP_CLAUSE_CHAIN (c) = list;
33016
33017 return c;
33018 }
33019
33020 /* OpenMP 4.5:
33021 hint ( expression ) */
33022
33023 static tree
33024 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33025 {
33026 tree t, c;
33027
33028 matching_parens parens;
33029 if (!parens.require_open (parser))
33030 return list;
33031
33032 t = cp_parser_assignment_expression (parser);
33033
33034 if (t == error_mark_node
33035 || !parens.require_close (parser))
33036 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33037 /*or_comma=*/false,
33038 /*consume_paren=*/true);
33039
33040 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33041
33042 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33043 OMP_CLAUSE_HINT_EXPR (c) = t;
33044 OMP_CLAUSE_CHAIN (c) = list;
33045
33046 return c;
33047 }
33048
33049 /* OpenMP 4.5:
33050 defaultmap ( tofrom : scalar )
33051
33052 OpenMP 5.0:
33053 defaultmap ( implicit-behavior [ : variable-category ] ) */
33054
33055 static tree
33056 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33057 location_t location)
33058 {
33059 tree c, id;
33060 const char *p;
33061 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33062 enum omp_clause_defaultmap_kind category
33063 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33064
33065 matching_parens parens;
33066 if (!parens.require_open (parser))
33067 return list;
33068
33069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33070 p = "default";
33071 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33072 {
33073 invalid_behavior:
33074 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33075 "%<tofrom%>, %<firstprivate%>, %<none%> "
33076 "or %<default%>");
33077 goto out_err;
33078 }
33079 else
33080 {
33081 id = cp_lexer_peek_token (parser->lexer)->u.value;
33082 p = IDENTIFIER_POINTER (id);
33083 }
33084
33085 switch (p[0])
33086 {
33087 case 'a':
33088 if (strcmp ("alloc", p) == 0)
33089 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33090 else
33091 goto invalid_behavior;
33092 break;
33093
33094 case 'd':
33095 if (strcmp ("default", p) == 0)
33096 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33097 else
33098 goto invalid_behavior;
33099 break;
33100
33101 case 'f':
33102 if (strcmp ("firstprivate", p) == 0)
33103 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33104 else if (strcmp ("from", p) == 0)
33105 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33106 else
33107 goto invalid_behavior;
33108 break;
33109
33110 case 'n':
33111 if (strcmp ("none", p) == 0)
33112 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33113 else
33114 goto invalid_behavior;
33115 break;
33116
33117 case 't':
33118 if (strcmp ("tofrom", p) == 0)
33119 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33120 else if (strcmp ("to", p) == 0)
33121 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33122 else
33123 goto invalid_behavior;
33124 break;
33125
33126 default:
33127 goto invalid_behavior;
33128 }
33129 cp_lexer_consume_token (parser->lexer);
33130
33131 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33132 {
33133 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33134 goto out_err;
33135
33136 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33137 {
33138 invalid_category:
33139 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33140 "%<pointer%>");
33141 goto out_err;
33142 }
33143 id = cp_lexer_peek_token (parser->lexer)->u.value;
33144 p = IDENTIFIER_POINTER (id);
33145
33146 switch (p[0])
33147 {
33148 case 'a':
33149 if (strcmp ("aggregate", p) == 0)
33150 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33151 else
33152 goto invalid_category;
33153 break;
33154
33155 case 'p':
33156 if (strcmp ("pointer", p) == 0)
33157 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33158 else
33159 goto invalid_category;
33160 break;
33161
33162 case 's':
33163 if (strcmp ("scalar", p) == 0)
33164 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33165 else
33166 goto invalid_category;
33167 break;
33168
33169 default:
33170 goto invalid_category;
33171 }
33172
33173 cp_lexer_consume_token (parser->lexer);
33174 }
33175 if (!parens.require_close (parser))
33176 goto out_err;
33177
33178 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33179 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33180 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33181 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33182 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33183 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33184 {
33185 enum omp_clause_defaultmap_kind cat = category;
33186 location_t loc = OMP_CLAUSE_LOCATION (c);
33187 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33188 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33189 p = NULL;
33190 switch (cat)
33191 {
33192 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33193 p = NULL;
33194 break;
33195 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33196 p = "aggregate";
33197 break;
33198 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33199 p = "pointer";
33200 break;
33201 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33202 p = "scalar";
33203 break;
33204 default:
33205 gcc_unreachable ();
33206 }
33207 if (p)
33208 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33209 p);
33210 else
33211 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33212 "category");
33213 break;
33214 }
33215
33216 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33217 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33218 OMP_CLAUSE_CHAIN (c) = list;
33219 return c;
33220
33221 out_err:
33222 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33223 /*or_comma=*/false,
33224 /*consume_paren=*/true);
33225 return list;
33226 }
33227
33228 /* OpenMP 2.5:
33229 ordered
33230
33231 OpenMP 4.5:
33232 ordered ( constant-expression ) */
33233
33234 static tree
33235 cp_parser_omp_clause_ordered (cp_parser *parser,
33236 tree list, location_t location)
33237 {
33238 tree c, num = NULL_TREE;
33239 HOST_WIDE_INT n;
33240
33241 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33242 "ordered", location);
33243
33244 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33245 {
33246 matching_parens parens;
33247 parens.consume_open (parser);
33248
33249 num = cp_parser_constant_expression (parser);
33250
33251 if (!parens.require_close (parser))
33252 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33253 /*or_comma=*/false,
33254 /*consume_paren=*/true);
33255
33256 if (num == error_mark_node)
33257 return list;
33258 num = fold_non_dependent_expr (num);
33259 if (!tree_fits_shwi_p (num)
33260 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33261 || (n = tree_to_shwi (num)) <= 0
33262 || (int) n != n)
33263 {
33264 error_at (location,
33265 "ordered argument needs positive constant integer "
33266 "expression");
33267 return list;
33268 }
33269 }
33270
33271 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33272 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33273 OMP_CLAUSE_CHAIN (c) = list;
33274 return c;
33275 }
33276
33277 /* OpenMP 2.5:
33278 reduction ( reduction-operator : variable-list )
33279
33280 reduction-operator:
33281 One of: + * - & ^ | && ||
33282
33283 OpenMP 3.1:
33284
33285 reduction-operator:
33286 One of: + * - & ^ | && || min max
33287
33288 OpenMP 4.0:
33289
33290 reduction-operator:
33291 One of: + * - & ^ | && ||
33292 id-expression
33293
33294 OpenMP 5.0:
33295 reduction ( reduction-modifier, reduction-operator : variable-list )
33296 in_reduction ( reduction-operator : variable-list )
33297 task_reduction ( reduction-operator : variable-list ) */
33298
33299 static tree
33300 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33301 bool is_omp, tree list)
33302 {
33303 enum tree_code code = ERROR_MARK;
33304 tree nlist, c, id = NULL_TREE;
33305 bool task = false;
33306 bool inscan = false;
33307
33308 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33309 return list;
33310
33311 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33312 {
33313 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33314 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33315 {
33316 cp_lexer_consume_token (parser->lexer);
33317 cp_lexer_consume_token (parser->lexer);
33318 }
33319 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33320 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33321 {
33322 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33323 const char *p = IDENTIFIER_POINTER (id);
33324 if (strcmp (p, "task") == 0)
33325 task = true;
33326 else if (strcmp (p, "inscan") == 0)
33327 {
33328 inscan = true;
33329 sorry ("%<inscan%> modifier on %<reduction%> clause "
33330 "not supported yet");
33331 }
33332 if (task || inscan)
33333 {
33334 cp_lexer_consume_token (parser->lexer);
33335 cp_lexer_consume_token (parser->lexer);
33336 }
33337 }
33338 }
33339
33340 switch (cp_lexer_peek_token (parser->lexer)->type)
33341 {
33342 case CPP_PLUS: code = PLUS_EXPR; break;
33343 case CPP_MULT: code = MULT_EXPR; break;
33344 case CPP_MINUS: code = MINUS_EXPR; break;
33345 case CPP_AND: code = BIT_AND_EXPR; break;
33346 case CPP_XOR: code = BIT_XOR_EXPR; break;
33347 case CPP_OR: code = BIT_IOR_EXPR; break;
33348 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33349 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33350 default: break;
33351 }
33352
33353 if (code != ERROR_MARK)
33354 cp_lexer_consume_token (parser->lexer);
33355 else
33356 {
33357 bool saved_colon_corrects_to_scope_p;
33358 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33359 parser->colon_corrects_to_scope_p = false;
33360 id = cp_parser_id_expression (parser, /*template_p=*/false,
33361 /*check_dependency_p=*/true,
33362 /*template_p=*/NULL,
33363 /*declarator_p=*/false,
33364 /*optional_p=*/false);
33365 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33366 if (identifier_p (id))
33367 {
33368 const char *p = IDENTIFIER_POINTER (id);
33369
33370 if (strcmp (p, "min") == 0)
33371 code = MIN_EXPR;
33372 else if (strcmp (p, "max") == 0)
33373 code = MAX_EXPR;
33374 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33375 code = PLUS_EXPR;
33376 else if (id == ovl_op_identifier (false, MULT_EXPR))
33377 code = MULT_EXPR;
33378 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33379 code = MINUS_EXPR;
33380 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33381 code = BIT_AND_EXPR;
33382 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33383 code = BIT_IOR_EXPR;
33384 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33385 code = BIT_XOR_EXPR;
33386 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33387 code = TRUTH_ANDIF_EXPR;
33388 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33389 code = TRUTH_ORIF_EXPR;
33390 id = omp_reduction_id (code, id, NULL_TREE);
33391 tree scope = parser->scope;
33392 if (scope)
33393 id = build_qualified_name (NULL_TREE, scope, id, false);
33394 parser->scope = NULL_TREE;
33395 parser->qualifying_scope = NULL_TREE;
33396 parser->object_scope = NULL_TREE;
33397 }
33398 else
33399 {
33400 error ("invalid reduction-identifier");
33401 resync_fail:
33402 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33403 /*or_comma=*/false,
33404 /*consume_paren=*/true);
33405 return list;
33406 }
33407 }
33408
33409 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33410 goto resync_fail;
33411
33412 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33413 NULL);
33414 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33415 {
33416 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33417 if (task)
33418 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33419 else if (inscan)
33420 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33421 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33422 }
33423
33424 return nlist;
33425 }
33426
33427 /* OpenMP 2.5:
33428 schedule ( schedule-kind )
33429 schedule ( schedule-kind , expression )
33430
33431 schedule-kind:
33432 static | dynamic | guided | runtime | auto
33433
33434 OpenMP 4.5:
33435 schedule ( schedule-modifier : schedule-kind )
33436 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33437
33438 schedule-modifier:
33439 simd
33440 monotonic
33441 nonmonotonic */
33442
33443 static tree
33444 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33445 {
33446 tree c, t;
33447 int modifiers = 0, nmodifiers = 0;
33448
33449 matching_parens parens;
33450 if (!parens.require_open (parser))
33451 return list;
33452
33453 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33454
33455 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33456 {
33457 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33458 const char *p = IDENTIFIER_POINTER (id);
33459 if (strcmp ("simd", p) == 0)
33460 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33461 else if (strcmp ("monotonic", p) == 0)
33462 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33463 else if (strcmp ("nonmonotonic", p) == 0)
33464 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33465 else
33466 break;
33467 cp_lexer_consume_token (parser->lexer);
33468 if (nmodifiers++ == 0
33469 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33470 cp_lexer_consume_token (parser->lexer);
33471 else
33472 {
33473 cp_parser_require (parser, CPP_COLON, RT_COLON);
33474 break;
33475 }
33476 }
33477
33478 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33479 {
33480 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33481 const char *p = IDENTIFIER_POINTER (id);
33482
33483 switch (p[0])
33484 {
33485 case 'd':
33486 if (strcmp ("dynamic", p) != 0)
33487 goto invalid_kind;
33488 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33489 break;
33490
33491 case 'g':
33492 if (strcmp ("guided", p) != 0)
33493 goto invalid_kind;
33494 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33495 break;
33496
33497 case 'r':
33498 if (strcmp ("runtime", p) != 0)
33499 goto invalid_kind;
33500 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33501 break;
33502
33503 default:
33504 goto invalid_kind;
33505 }
33506 }
33507 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33508 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33509 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33510 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33511 else
33512 goto invalid_kind;
33513 cp_lexer_consume_token (parser->lexer);
33514
33515 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33516 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33517 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33518 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33519 {
33520 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33521 "specified");
33522 modifiers = 0;
33523 }
33524
33525 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33526 {
33527 cp_token *token;
33528 cp_lexer_consume_token (parser->lexer);
33529
33530 token = cp_lexer_peek_token (parser->lexer);
33531 t = cp_parser_assignment_expression (parser);
33532
33533 if (t == error_mark_node)
33534 goto resync_fail;
33535 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33536 error_at (token->location, "schedule %<runtime%> does not take "
33537 "a %<chunk_size%> parameter");
33538 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33539 error_at (token->location, "schedule %<auto%> does not take "
33540 "a %<chunk_size%> parameter");
33541 else
33542 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33543
33544 if (!parens.require_close (parser))
33545 goto resync_fail;
33546 }
33547 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33548 goto resync_fail;
33549
33550 OMP_CLAUSE_SCHEDULE_KIND (c)
33551 = (enum omp_clause_schedule_kind)
33552 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33553
33554 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33555 OMP_CLAUSE_CHAIN (c) = list;
33556 return c;
33557
33558 invalid_kind:
33559 cp_parser_error (parser, "invalid schedule kind");
33560 resync_fail:
33561 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33562 /*or_comma=*/false,
33563 /*consume_paren=*/true);
33564 return list;
33565 }
33566
33567 /* OpenMP 3.0:
33568 untied */
33569
33570 static tree
33571 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33572 tree list, location_t location)
33573 {
33574 tree c;
33575
33576 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33577
33578 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33579 OMP_CLAUSE_CHAIN (c) = list;
33580 return c;
33581 }
33582
33583 /* OpenMP 4.0:
33584 inbranch
33585 notinbranch */
33586
33587 static tree
33588 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33589 tree list, location_t location)
33590 {
33591 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33592 tree c = build_omp_clause (location, code);
33593 OMP_CLAUSE_CHAIN (c) = list;
33594 return c;
33595 }
33596
33597 /* OpenMP 4.0:
33598 parallel
33599 for
33600 sections
33601 taskgroup */
33602
33603 static tree
33604 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33605 enum omp_clause_code code,
33606 tree list, location_t location)
33607 {
33608 tree c = build_omp_clause (location, code);
33609 OMP_CLAUSE_CHAIN (c) = list;
33610 return c;
33611 }
33612
33613 /* OpenMP 4.5:
33614 nogroup */
33615
33616 static tree
33617 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33618 tree list, location_t location)
33619 {
33620 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33621 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33622 OMP_CLAUSE_CHAIN (c) = list;
33623 return c;
33624 }
33625
33626 /* OpenMP 4.5:
33627 simd
33628 threads */
33629
33630 static tree
33631 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33632 enum omp_clause_code code,
33633 tree list, location_t location)
33634 {
33635 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33636 tree c = build_omp_clause (location, code);
33637 OMP_CLAUSE_CHAIN (c) = list;
33638 return c;
33639 }
33640
33641 /* OpenMP 4.0:
33642 num_teams ( expression ) */
33643
33644 static tree
33645 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33646 location_t location)
33647 {
33648 tree t, c;
33649
33650 matching_parens parens;
33651 if (!parens.require_open (parser))
33652 return list;
33653
33654 t = cp_parser_assignment_expression (parser);
33655
33656 if (t == error_mark_node
33657 || !parens.require_close (parser))
33658 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33659 /*or_comma=*/false,
33660 /*consume_paren=*/true);
33661
33662 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33663 "num_teams", location);
33664
33665 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33666 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33667 OMP_CLAUSE_CHAIN (c) = list;
33668
33669 return c;
33670 }
33671
33672 /* OpenMP 4.0:
33673 thread_limit ( expression ) */
33674
33675 static tree
33676 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33677 location_t location)
33678 {
33679 tree t, c;
33680
33681 matching_parens parens;
33682 if (!parens.require_open (parser))
33683 return list;
33684
33685 t = cp_parser_assignment_expression (parser);
33686
33687 if (t == error_mark_node
33688 || !parens.require_close (parser))
33689 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33690 /*or_comma=*/false,
33691 /*consume_paren=*/true);
33692
33693 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33694 "thread_limit", location);
33695
33696 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33697 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33698 OMP_CLAUSE_CHAIN (c) = list;
33699
33700 return c;
33701 }
33702
33703 /* OpenMP 4.0:
33704 aligned ( variable-list )
33705 aligned ( variable-list : constant-expression ) */
33706
33707 static tree
33708 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33709 {
33710 tree nlist, c, alignment = NULL_TREE;
33711 bool colon;
33712
33713 matching_parens parens;
33714 if (!parens.require_open (parser))
33715 return list;
33716
33717 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33718 &colon);
33719
33720 if (colon)
33721 {
33722 alignment = cp_parser_constant_expression (parser);
33723
33724 if (!parens.require_close (parser))
33725 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33726 /*or_comma=*/false,
33727 /*consume_paren=*/true);
33728
33729 if (alignment == error_mark_node)
33730 alignment = NULL_TREE;
33731 }
33732
33733 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33734 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33735
33736 return nlist;
33737 }
33738
33739 /* OpenMP 2.5:
33740 lastprivate ( variable-list )
33741
33742 OpenMP 5.0:
33743 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33744
33745 static tree
33746 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
33747 {
33748 bool conditional = false;
33749
33750 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33751 return list;
33752
33753 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33754 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
33755 {
33756 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33757 const char *p = IDENTIFIER_POINTER (id);
33758
33759 if (strcmp ("conditional", p) == 0)
33760 {
33761 conditional = true;
33762 cp_lexer_consume_token (parser->lexer);
33763 cp_lexer_consume_token (parser->lexer);
33764 }
33765 }
33766
33767 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
33768 list, NULL);
33769
33770 if (conditional)
33771 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33772 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
33773 return nlist;
33774 }
33775
33776 /* OpenMP 4.0:
33777 linear ( variable-list )
33778 linear ( variable-list : expression )
33779
33780 OpenMP 4.5:
33781 linear ( modifier ( variable-list ) )
33782 linear ( modifier ( variable-list ) : expression ) */
33783
33784 static tree
33785 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33786 bool declare_simd)
33787 {
33788 tree nlist, c, step = integer_one_node;
33789 bool colon;
33790 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33791
33792 matching_parens parens;
33793 if (!parens.require_open (parser))
33794 return list;
33795
33796 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33797 {
33798 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33799 const char *p = IDENTIFIER_POINTER (id);
33800
33801 if (strcmp ("ref", p) == 0)
33802 kind = OMP_CLAUSE_LINEAR_REF;
33803 else if (strcmp ("val", p) == 0)
33804 kind = OMP_CLAUSE_LINEAR_VAL;
33805 else if (strcmp ("uval", p) == 0)
33806 kind = OMP_CLAUSE_LINEAR_UVAL;
33807 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33808 cp_lexer_consume_token (parser->lexer);
33809 else
33810 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33811 }
33812
33813 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33814 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33815 &colon);
33816 else
33817 {
33818 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
33819 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
33820 if (colon)
33821 cp_parser_require (parser, CPP_COLON, RT_COLON);
33822 else if (!parens.require_close (parser))
33823 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33824 /*or_comma=*/false,
33825 /*consume_paren=*/true);
33826 }
33827
33828 if (colon)
33829 {
33830 step = NULL_TREE;
33831 if (declare_simd
33832 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33833 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
33834 {
33835 cp_token *token = cp_lexer_peek_token (parser->lexer);
33836 cp_parser_parse_tentatively (parser);
33837 step = cp_parser_id_expression (parser, /*template_p=*/false,
33838 /*check_dependency_p=*/true,
33839 /*template_p=*/NULL,
33840 /*declarator_p=*/false,
33841 /*optional_p=*/false);
33842 if (step != error_mark_node)
33843 step = cp_parser_lookup_name_simple (parser, step, token->location);
33844 if (step == error_mark_node)
33845 {
33846 step = NULL_TREE;
33847 cp_parser_abort_tentative_parse (parser);
33848 }
33849 else if (!cp_parser_parse_definitely (parser))
33850 step = NULL_TREE;
33851 }
33852 if (!step)
33853 step = cp_parser_assignment_expression (parser);
33854
33855 if (!parens.require_close (parser))
33856 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33857 /*or_comma=*/false,
33858 /*consume_paren=*/true);
33859
33860 if (step == error_mark_node)
33861 return list;
33862 }
33863
33864 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33865 {
33866 OMP_CLAUSE_LINEAR_STEP (c) = step;
33867 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33868 }
33869
33870 return nlist;
33871 }
33872
33873 /* OpenMP 4.0:
33874 safelen ( constant-expression ) */
33875
33876 static tree
33877 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33878 location_t location)
33879 {
33880 tree t, c;
33881
33882 matching_parens parens;
33883 if (!parens.require_open (parser))
33884 return list;
33885
33886 t = cp_parser_constant_expression (parser);
33887
33888 if (t == error_mark_node
33889 || !parens.require_close (parser))
33890 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33891 /*or_comma=*/false,
33892 /*consume_paren=*/true);
33893
33894 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33895
33896 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33897 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33898 OMP_CLAUSE_CHAIN (c) = list;
33899
33900 return c;
33901 }
33902
33903 /* OpenMP 4.0:
33904 simdlen ( constant-expression ) */
33905
33906 static tree
33907 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33908 location_t location)
33909 {
33910 tree t, c;
33911
33912 matching_parens parens;
33913 if (!parens.require_open (parser))
33914 return list;
33915
33916 t = cp_parser_constant_expression (parser);
33917
33918 if (t == error_mark_node
33919 || !parens.require_close (parser))
33920 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33921 /*or_comma=*/false,
33922 /*consume_paren=*/true);
33923
33924 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33925
33926 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33927 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33928 OMP_CLAUSE_CHAIN (c) = list;
33929
33930 return c;
33931 }
33932
33933 /* OpenMP 4.5:
33934 vec:
33935 identifier [+/- integer]
33936 vec , identifier [+/- integer]
33937 */
33938
33939 static tree
33940 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33941 tree list)
33942 {
33943 tree vec = NULL;
33944
33945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33946 {
33947 cp_parser_error (parser, "expected identifier");
33948 return list;
33949 }
33950
33951 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33952 {
33953 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33954 tree t, identifier = cp_parser_identifier (parser);
33955 tree addend = NULL;
33956
33957 if (identifier == error_mark_node)
33958 t = error_mark_node;
33959 else
33960 {
33961 t = cp_parser_lookup_name_simple
33962 (parser, identifier,
33963 cp_lexer_peek_token (parser->lexer)->location);
33964 if (t == error_mark_node)
33965 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33966 id_loc);
33967 }
33968
33969 bool neg = false;
33970 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33971 neg = true;
33972 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33973 {
33974 addend = integer_zero_node;
33975 goto add_to_vector;
33976 }
33977 cp_lexer_consume_token (parser->lexer);
33978
33979 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33980 {
33981 cp_parser_error (parser, "expected integer");
33982 return list;
33983 }
33984
33985 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33986 if (TREE_CODE (addend) != INTEGER_CST)
33987 {
33988 cp_parser_error (parser, "expected integer");
33989 return list;
33990 }
33991 cp_lexer_consume_token (parser->lexer);
33992
33993 add_to_vector:
33994 if (t != error_mark_node)
33995 {
33996 vec = tree_cons (addend, t, vec);
33997 if (neg)
33998 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33999 }
34000
34001 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34002 break;
34003
34004 cp_lexer_consume_token (parser->lexer);
34005 }
34006
34007 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34008 {
34009 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34010 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34011 OMP_CLAUSE_DECL (u) = nreverse (vec);
34012 OMP_CLAUSE_CHAIN (u) = list;
34013 return u;
34014 }
34015 return list;
34016 }
34017
34018 /* OpenMP 5.0:
34019 iterators ( iterators-definition )
34020
34021 iterators-definition:
34022 iterator-specifier
34023 iterator-specifier , iterators-definition
34024
34025 iterator-specifier:
34026 identifier = range-specification
34027 iterator-type identifier = range-specification
34028
34029 range-specification:
34030 begin : end
34031 begin : end : step */
34032
34033 static tree
34034 cp_parser_omp_iterators (cp_parser *parser)
34035 {
34036 tree ret = NULL_TREE, *last = &ret;
34037 cp_lexer_consume_token (parser->lexer);
34038
34039 matching_parens parens;
34040 if (!parens.require_open (parser))
34041 return error_mark_node;
34042
34043 bool saved_colon_corrects_to_scope_p
34044 = parser->colon_corrects_to_scope_p;
34045 bool saved_colon_doesnt_start_class_def_p
34046 = parser->colon_doesnt_start_class_def_p;
34047
34048 do
34049 {
34050 tree iter_type;
34051 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34052 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34053 iter_type = integer_type_node;
34054 else
34055 {
34056 const char *saved_message
34057 = parser->type_definition_forbidden_message;
34058 parser->type_definition_forbidden_message
34059 = G_("types may not be defined in iterator type");
34060
34061 iter_type = cp_parser_type_id (parser);
34062
34063 parser->type_definition_forbidden_message = saved_message;
34064 }
34065
34066 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34068 {
34069 cp_parser_error (parser, "expected identifier");
34070 break;
34071 }
34072
34073 tree id = cp_parser_identifier (parser);
34074 if (id == error_mark_node)
34075 break;
34076
34077 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34078 break;
34079
34080 parser->colon_corrects_to_scope_p = false;
34081 parser->colon_doesnt_start_class_def_p = true;
34082 tree begin = cp_parser_assignment_expression (parser);
34083
34084 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34085 break;
34086
34087 tree end = cp_parser_assignment_expression (parser);
34088
34089 tree step = integer_one_node;
34090 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34091 {
34092 cp_lexer_consume_token (parser->lexer);
34093 step = cp_parser_assignment_expression (parser);
34094 }
34095
34096 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34097 DECL_ARTIFICIAL (iter_var) = 1;
34098 DECL_CONTEXT (iter_var) = current_function_decl;
34099 pushdecl (iter_var);
34100
34101 *last = make_tree_vec (6);
34102 TREE_VEC_ELT (*last, 0) = iter_var;
34103 TREE_VEC_ELT (*last, 1) = begin;
34104 TREE_VEC_ELT (*last, 2) = end;
34105 TREE_VEC_ELT (*last, 3) = step;
34106 last = &TREE_CHAIN (*last);
34107
34108 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34109 {
34110 cp_lexer_consume_token (parser->lexer);
34111 continue;
34112 }
34113 break;
34114 }
34115 while (1);
34116
34117 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34118 parser->colon_doesnt_start_class_def_p
34119 = saved_colon_doesnt_start_class_def_p;
34120
34121 if (!parens.require_close (parser))
34122 cp_parser_skip_to_closing_parenthesis (parser,
34123 /*recovering=*/true,
34124 /*or_comma=*/false,
34125 /*consume_paren=*/true);
34126
34127 return ret ? ret : error_mark_node;
34128 }
34129
34130 /* OpenMP 4.0:
34131 depend ( depend-kind : variable-list )
34132
34133 depend-kind:
34134 in | out | inout
34135
34136 OpenMP 4.5:
34137 depend ( source )
34138
34139 depend ( sink : vec )
34140
34141 OpenMP 5.0:
34142 depend ( depend-modifier , depend-kind: variable-list )
34143
34144 depend-kind:
34145 in | out | inout | mutexinoutset | depobj
34146
34147 depend-modifier:
34148 iterator ( iterators-definition ) */
34149
34150 static tree
34151 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34152 {
34153 tree nlist, c, iterators = NULL_TREE;
34154 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34155
34156 matching_parens parens;
34157 if (!parens.require_open (parser))
34158 return list;
34159
34160 do
34161 {
34162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34163 goto invalid_kind;
34164
34165 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34166 const char *p = IDENTIFIER_POINTER (id);
34167
34168 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34169 {
34170 begin_scope (sk_omp, NULL);
34171 iterators = cp_parser_omp_iterators (parser);
34172 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34173 continue;
34174 }
34175 if (strcmp ("in", p) == 0)
34176 kind = OMP_CLAUSE_DEPEND_IN;
34177 else if (strcmp ("inout", p) == 0)
34178 kind = OMP_CLAUSE_DEPEND_INOUT;
34179 else if (strcmp ("mutexinoutset", p) == 0)
34180 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34181 else if (strcmp ("out", p) == 0)
34182 kind = OMP_CLAUSE_DEPEND_OUT;
34183 else if (strcmp ("depobj", p) == 0)
34184 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34185 else if (strcmp ("sink", p) == 0)
34186 kind = OMP_CLAUSE_DEPEND_SINK;
34187 else if (strcmp ("source", p) == 0)
34188 kind = OMP_CLAUSE_DEPEND_SOURCE;
34189 else
34190 goto invalid_kind;
34191 break;
34192 }
34193 while (1);
34194
34195 cp_lexer_consume_token (parser->lexer);
34196
34197 if (iterators
34198 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34199 {
34200 poplevel (0, 1, 0);
34201 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34202 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34203 iterators = NULL_TREE;
34204 }
34205
34206 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34207 {
34208 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34209 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34210 OMP_CLAUSE_DECL (c) = NULL_TREE;
34211 OMP_CLAUSE_CHAIN (c) = list;
34212 if (!parens.require_close (parser))
34213 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34214 /*or_comma=*/false,
34215 /*consume_paren=*/true);
34216 return c;
34217 }
34218
34219 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34220 goto resync_fail;
34221
34222 if (kind == OMP_CLAUSE_DEPEND_SINK)
34223 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34224 else
34225 {
34226 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34227 list, NULL);
34228
34229 if (iterators)
34230 {
34231 tree block = poplevel (1, 1, 0);
34232 if (iterators == error_mark_node)
34233 iterators = NULL_TREE;
34234 else
34235 TREE_VEC_ELT (iterators, 5) = block;
34236 }
34237
34238 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34239 {
34240 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34241 if (iterators)
34242 OMP_CLAUSE_DECL (c)
34243 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34244 }
34245 }
34246 return nlist;
34247
34248 invalid_kind:
34249 cp_parser_error (parser, "invalid depend kind");
34250 resync_fail:
34251 if (iterators)
34252 poplevel (0, 1, 0);
34253 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34254 /*or_comma=*/false,
34255 /*consume_paren=*/true);
34256 return list;
34257 }
34258
34259 /* OpenMP 4.0:
34260 map ( map-kind : variable-list )
34261 map ( variable-list )
34262
34263 map-kind:
34264 alloc | to | from | tofrom
34265
34266 OpenMP 4.5:
34267 map-kind:
34268 alloc | to | from | tofrom | release | delete
34269
34270 map ( always [,] map-kind: variable-list ) */
34271
34272 static tree
34273 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34274 {
34275 tree nlist, c;
34276 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34277 bool always = false;
34278
34279 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34280 return list;
34281
34282 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34283 {
34284 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34285 const char *p = IDENTIFIER_POINTER (id);
34286
34287 if (strcmp ("always", p) == 0)
34288 {
34289 int nth = 2;
34290 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34291 nth++;
34292 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34293 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34294 == RID_DELETE))
34295 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34296 == CPP_COLON))
34297 {
34298 always = true;
34299 cp_lexer_consume_token (parser->lexer);
34300 if (nth == 3)
34301 cp_lexer_consume_token (parser->lexer);
34302 }
34303 }
34304 }
34305
34306 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34307 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34308 {
34309 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34310 const char *p = IDENTIFIER_POINTER (id);
34311
34312 if (strcmp ("alloc", p) == 0)
34313 kind = GOMP_MAP_ALLOC;
34314 else if (strcmp ("to", p) == 0)
34315 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34316 else if (strcmp ("from", p) == 0)
34317 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34318 else if (strcmp ("tofrom", p) == 0)
34319 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34320 else if (strcmp ("release", p) == 0)
34321 kind = GOMP_MAP_RELEASE;
34322 else
34323 {
34324 cp_parser_error (parser, "invalid map kind");
34325 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34326 /*or_comma=*/false,
34327 /*consume_paren=*/true);
34328 return list;
34329 }
34330 cp_lexer_consume_token (parser->lexer);
34331 cp_lexer_consume_token (parser->lexer);
34332 }
34333 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34334 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34335 {
34336 kind = GOMP_MAP_DELETE;
34337 cp_lexer_consume_token (parser->lexer);
34338 cp_lexer_consume_token (parser->lexer);
34339 }
34340
34341 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34342 NULL);
34343
34344 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34345 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34346
34347 return nlist;
34348 }
34349
34350 /* OpenMP 4.0:
34351 device ( expression ) */
34352
34353 static tree
34354 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34355 location_t location)
34356 {
34357 tree t, c;
34358
34359 matching_parens parens;
34360 if (!parens.require_open (parser))
34361 return list;
34362
34363 t = cp_parser_assignment_expression (parser);
34364
34365 if (t == error_mark_node
34366 || !parens.require_close (parser))
34367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34368 /*or_comma=*/false,
34369 /*consume_paren=*/true);
34370
34371 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34372 "device", location);
34373
34374 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34375 OMP_CLAUSE_DEVICE_ID (c) = t;
34376 OMP_CLAUSE_CHAIN (c) = list;
34377
34378 return c;
34379 }
34380
34381 /* OpenMP 4.0:
34382 dist_schedule ( static )
34383 dist_schedule ( static , expression ) */
34384
34385 static tree
34386 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34387 location_t location)
34388 {
34389 tree c, t;
34390
34391 matching_parens parens;
34392 if (!parens.require_open (parser))
34393 return list;
34394
34395 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34396
34397 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34398 goto invalid_kind;
34399 cp_lexer_consume_token (parser->lexer);
34400
34401 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34402 {
34403 cp_lexer_consume_token (parser->lexer);
34404
34405 t = cp_parser_assignment_expression (parser);
34406
34407 if (t == error_mark_node)
34408 goto resync_fail;
34409 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34410
34411 if (!parens.require_close (parser))
34412 goto resync_fail;
34413 }
34414 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34415 goto resync_fail;
34416
34417 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34418 location);
34419 OMP_CLAUSE_CHAIN (c) = list;
34420 return c;
34421
34422 invalid_kind:
34423 cp_parser_error (parser, "invalid dist_schedule kind");
34424 resync_fail:
34425 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34426 /*or_comma=*/false,
34427 /*consume_paren=*/true);
34428 return list;
34429 }
34430
34431 /* OpenMP 4.0:
34432 proc_bind ( proc-bind-kind )
34433
34434 proc-bind-kind:
34435 master | close | spread */
34436
34437 static tree
34438 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34439 location_t location)
34440 {
34441 tree c;
34442 enum omp_clause_proc_bind_kind kind;
34443
34444 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34445 return list;
34446
34447 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34448 {
34449 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34450 const char *p = IDENTIFIER_POINTER (id);
34451
34452 if (strcmp ("master", p) == 0)
34453 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34454 else if (strcmp ("close", p) == 0)
34455 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34456 else if (strcmp ("spread", p) == 0)
34457 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34458 else
34459 goto invalid_kind;
34460 }
34461 else
34462 goto invalid_kind;
34463
34464 cp_lexer_consume_token (parser->lexer);
34465 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34466 goto resync_fail;
34467
34468 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34469 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34470 location);
34471 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34472 OMP_CLAUSE_CHAIN (c) = list;
34473 return c;
34474
34475 invalid_kind:
34476 cp_parser_error (parser, "invalid depend kind");
34477 resync_fail:
34478 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34479 /*or_comma=*/false,
34480 /*consume_paren=*/true);
34481 return list;
34482 }
34483
34484 /* OpenACC:
34485 async [( int-expr )] */
34486
34487 static tree
34488 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34489 {
34490 tree c, t;
34491 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34492
34493 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34494
34495 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34496 {
34497 matching_parens parens;
34498 parens.consume_open (parser);
34499
34500 t = cp_parser_expression (parser);
34501 if (t == error_mark_node
34502 || !parens.require_close (parser))
34503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34504 /*or_comma=*/false,
34505 /*consume_paren=*/true);
34506 }
34507
34508 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34509
34510 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34511 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34512 OMP_CLAUSE_CHAIN (c) = list;
34513 list = c;
34514
34515 return list;
34516 }
34517
34518 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34519 is a bitmask in MASK. Return the list of clauses found. */
34520
34521 static tree
34522 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34523 const char *where, cp_token *pragma_tok,
34524 bool finish_p = true)
34525 {
34526 tree clauses = NULL;
34527 bool first = true;
34528
34529 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34530 {
34531 location_t here;
34532 pragma_omp_clause c_kind;
34533 omp_clause_code code;
34534 const char *c_name;
34535 tree prev = clauses;
34536
34537 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34538 cp_lexer_consume_token (parser->lexer);
34539
34540 here = cp_lexer_peek_token (parser->lexer)->location;
34541 c_kind = cp_parser_omp_clause_name (parser);
34542
34543 switch (c_kind)
34544 {
34545 case PRAGMA_OACC_CLAUSE_ASYNC:
34546 clauses = cp_parser_oacc_clause_async (parser, clauses);
34547 c_name = "async";
34548 break;
34549 case PRAGMA_OACC_CLAUSE_AUTO:
34550 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
34551 clauses, here);
34552 c_name = "auto";
34553 break;
34554 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34555 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34556 c_name = "collapse";
34557 break;
34558 case PRAGMA_OACC_CLAUSE_COPY:
34559 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34560 c_name = "copy";
34561 break;
34562 case PRAGMA_OACC_CLAUSE_COPYIN:
34563 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34564 c_name = "copyin";
34565 break;
34566 case PRAGMA_OACC_CLAUSE_COPYOUT:
34567 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34568 c_name = "copyout";
34569 break;
34570 case PRAGMA_OACC_CLAUSE_CREATE:
34571 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34572 c_name = "create";
34573 break;
34574 case PRAGMA_OACC_CLAUSE_DELETE:
34575 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34576 c_name = "delete";
34577 break;
34578 case PRAGMA_OMP_CLAUSE_DEFAULT:
34579 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34580 c_name = "default";
34581 break;
34582 case PRAGMA_OACC_CLAUSE_DEVICE:
34583 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34584 c_name = "device";
34585 break;
34586 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34587 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34588 c_name = "deviceptr";
34589 break;
34590 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34591 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34592 c_name = "device_resident";
34593 break;
34594 case PRAGMA_OACC_CLAUSE_FINALIZE:
34595 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
34596 clauses, here);
34597 c_name = "finalize";
34598 break;
34599 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34600 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34601 clauses);
34602 c_name = "firstprivate";
34603 break;
34604 case PRAGMA_OACC_CLAUSE_GANG:
34605 c_name = "gang";
34606 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
34607 c_name, clauses);
34608 break;
34609 case PRAGMA_OACC_CLAUSE_HOST:
34610 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34611 c_name = "host";
34612 break;
34613 case PRAGMA_OACC_CLAUSE_IF:
34614 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34615 c_name = "if";
34616 break;
34617 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34618 clauses = cp_parser_oacc_simple_clause (parser,
34619 OMP_CLAUSE_IF_PRESENT,
34620 clauses, here);
34621 c_name = "if_present";
34622 break;
34623 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34624 clauses = cp_parser_oacc_simple_clause (parser,
34625 OMP_CLAUSE_INDEPENDENT,
34626 clauses, here);
34627 c_name = "independent";
34628 break;
34629 case PRAGMA_OACC_CLAUSE_LINK:
34630 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34631 c_name = "link";
34632 break;
34633 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34634 code = OMP_CLAUSE_NUM_GANGS;
34635 c_name = "num_gangs";
34636 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34637 clauses);
34638 break;
34639 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34640 c_name = "num_workers";
34641 code = OMP_CLAUSE_NUM_WORKERS;
34642 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34643 clauses);
34644 break;
34645 case PRAGMA_OACC_CLAUSE_PRESENT:
34646 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34647 c_name = "present";
34648 break;
34649 case PRAGMA_OACC_CLAUSE_PRIVATE:
34650 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34651 clauses);
34652 c_name = "private";
34653 break;
34654 case PRAGMA_OACC_CLAUSE_REDUCTION:
34655 clauses
34656 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34657 false, clauses);
34658 c_name = "reduction";
34659 break;
34660 case PRAGMA_OACC_CLAUSE_SEQ:
34661 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
34662 clauses, here);
34663 c_name = "seq";
34664 break;
34665 case PRAGMA_OACC_CLAUSE_TILE:
34666 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
34667 c_name = "tile";
34668 break;
34669 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
34670 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34671 clauses);
34672 c_name = "use_device";
34673 break;
34674 case PRAGMA_OACC_CLAUSE_VECTOR:
34675 c_name = "vector";
34676 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
34677 c_name, clauses);
34678 break;
34679 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
34680 c_name = "vector_length";
34681 code = OMP_CLAUSE_VECTOR_LENGTH;
34682 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34683 clauses);
34684 break;
34685 case PRAGMA_OACC_CLAUSE_WAIT:
34686 clauses = cp_parser_oacc_clause_wait (parser, clauses);
34687 c_name = "wait";
34688 break;
34689 case PRAGMA_OACC_CLAUSE_WORKER:
34690 c_name = "worker";
34691 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
34692 c_name, clauses);
34693 break;
34694 default:
34695 cp_parser_error (parser, "expected %<#pragma acc%> clause");
34696 goto saw_error;
34697 }
34698
34699 first = false;
34700
34701 if (((mask >> c_kind) & 1) == 0)
34702 {
34703 /* Remove the invalid clause(s) from the list to avoid
34704 confusing the rest of the compiler. */
34705 clauses = prev;
34706 error_at (here, "%qs is not valid for %qs", c_name, where);
34707 }
34708 }
34709
34710 saw_error:
34711 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34712
34713 if (finish_p)
34714 return finish_omp_clauses (clauses, C_ORT_ACC);
34715
34716 return clauses;
34717 }
34718
34719 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34720 is a bitmask in MASK. Return the list of clauses found; the result
34721 of clause default goes in *pdefault. */
34722
34723 static tree
34724 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
34725 const char *where, cp_token *pragma_tok,
34726 bool finish_p = true)
34727 {
34728 tree clauses = NULL;
34729 bool first = true;
34730 cp_token *token = NULL;
34731
34732 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34733 {
34734 pragma_omp_clause c_kind;
34735 const char *c_name;
34736 tree prev = clauses;
34737
34738 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34739 cp_lexer_consume_token (parser->lexer);
34740
34741 token = cp_lexer_peek_token (parser->lexer);
34742 c_kind = cp_parser_omp_clause_name (parser);
34743
34744 switch (c_kind)
34745 {
34746 case PRAGMA_OMP_CLAUSE_COLLAPSE:
34747 clauses = cp_parser_omp_clause_collapse (parser, clauses,
34748 token->location);
34749 c_name = "collapse";
34750 break;
34751 case PRAGMA_OMP_CLAUSE_COPYIN:
34752 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
34753 c_name = "copyin";
34754 break;
34755 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
34756 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
34757 clauses);
34758 c_name = "copyprivate";
34759 break;
34760 case PRAGMA_OMP_CLAUSE_DEFAULT:
34761 clauses = cp_parser_omp_clause_default (parser, clauses,
34762 token->location, false);
34763 c_name = "default";
34764 break;
34765 case PRAGMA_OMP_CLAUSE_FINAL:
34766 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
34767 c_name = "final";
34768 break;
34769 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
34770 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34771 clauses);
34772 c_name = "firstprivate";
34773 break;
34774 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34775 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34776 token->location);
34777 c_name = "grainsize";
34778 break;
34779 case PRAGMA_OMP_CLAUSE_HINT:
34780 clauses = cp_parser_omp_clause_hint (parser, clauses,
34781 token->location);
34782 c_name = "hint";
34783 break;
34784 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34785 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34786 token->location);
34787 c_name = "defaultmap";
34788 break;
34789 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34790 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34791 clauses);
34792 c_name = "use_device_ptr";
34793 break;
34794 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34795 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34796 clauses);
34797 c_name = "is_device_ptr";
34798 break;
34799 case PRAGMA_OMP_CLAUSE_IF:
34800 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34801 true);
34802 c_name = "if";
34803 break;
34804 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
34805 clauses
34806 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
34807 true, clauses);
34808 c_name = "in_reduction";
34809 break;
34810 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34811 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
34812 c_name = "lastprivate";
34813 break;
34814 case PRAGMA_OMP_CLAUSE_MERGEABLE:
34815 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
34816 token->location);
34817 c_name = "mergeable";
34818 break;
34819 case PRAGMA_OMP_CLAUSE_NOWAIT:
34820 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
34821 c_name = "nowait";
34822 break;
34823 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
34824 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
34825 token->location);
34826 c_name = "num_tasks";
34827 break;
34828 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
34829 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
34830 token->location);
34831 c_name = "num_threads";
34832 break;
34833 case PRAGMA_OMP_CLAUSE_ORDERED:
34834 clauses = cp_parser_omp_clause_ordered (parser, clauses,
34835 token->location);
34836 c_name = "ordered";
34837 break;
34838 case PRAGMA_OMP_CLAUSE_PRIORITY:
34839 clauses = cp_parser_omp_clause_priority (parser, clauses,
34840 token->location);
34841 c_name = "priority";
34842 break;
34843 case PRAGMA_OMP_CLAUSE_PRIVATE:
34844 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34845 clauses);
34846 c_name = "private";
34847 break;
34848 case PRAGMA_OMP_CLAUSE_REDUCTION:
34849 clauses
34850 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34851 true, clauses);
34852 c_name = "reduction";
34853 break;
34854 case PRAGMA_OMP_CLAUSE_SCHEDULE:
34855 clauses = cp_parser_omp_clause_schedule (parser, clauses,
34856 token->location);
34857 c_name = "schedule";
34858 break;
34859 case PRAGMA_OMP_CLAUSE_SHARED:
34860 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
34861 clauses);
34862 c_name = "shared";
34863 break;
34864 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
34865 clauses
34866 = cp_parser_omp_clause_reduction (parser,
34867 OMP_CLAUSE_TASK_REDUCTION,
34868 true, clauses);
34869 c_name = "task_reduction";
34870 break;
34871 case PRAGMA_OMP_CLAUSE_UNTIED:
34872 clauses = cp_parser_omp_clause_untied (parser, clauses,
34873 token->location);
34874 c_name = "untied";
34875 break;
34876 case PRAGMA_OMP_CLAUSE_INBRANCH:
34877 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
34878 clauses, token->location);
34879 c_name = "inbranch";
34880 break;
34881 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
34882 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
34883 clauses);
34884 c_name = "nontemporal";
34885 break;
34886 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
34887 clauses = cp_parser_omp_clause_branch (parser,
34888 OMP_CLAUSE_NOTINBRANCH,
34889 clauses, token->location);
34890 c_name = "notinbranch";
34891 break;
34892 case PRAGMA_OMP_CLAUSE_PARALLEL:
34893 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
34894 clauses, token->location);
34895 c_name = "parallel";
34896 if (!first)
34897 {
34898 clause_not_first:
34899 error_at (token->location, "%qs must be the first clause of %qs",
34900 c_name, where);
34901 clauses = prev;
34902 }
34903 break;
34904 case PRAGMA_OMP_CLAUSE_FOR:
34905 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
34906 clauses, token->location);
34907 c_name = "for";
34908 if (!first)
34909 goto clause_not_first;
34910 break;
34911 case PRAGMA_OMP_CLAUSE_SECTIONS:
34912 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
34913 clauses, token->location);
34914 c_name = "sections";
34915 if (!first)
34916 goto clause_not_first;
34917 break;
34918 case PRAGMA_OMP_CLAUSE_TASKGROUP:
34919 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
34920 clauses, token->location);
34921 c_name = "taskgroup";
34922 if (!first)
34923 goto clause_not_first;
34924 break;
34925 case PRAGMA_OMP_CLAUSE_LINK:
34926 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
34927 c_name = "to";
34928 break;
34929 case PRAGMA_OMP_CLAUSE_TO:
34930 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
34931 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
34932 clauses);
34933 else
34934 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
34935 c_name = "to";
34936 break;
34937 case PRAGMA_OMP_CLAUSE_FROM:
34938 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
34939 c_name = "from";
34940 break;
34941 case PRAGMA_OMP_CLAUSE_UNIFORM:
34942 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
34943 clauses);
34944 c_name = "uniform";
34945 break;
34946 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
34947 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
34948 token->location);
34949 c_name = "num_teams";
34950 break;
34951 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
34952 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
34953 token->location);
34954 c_name = "thread_limit";
34955 break;
34956 case PRAGMA_OMP_CLAUSE_ALIGNED:
34957 clauses = cp_parser_omp_clause_aligned (parser, clauses);
34958 c_name = "aligned";
34959 break;
34960 case PRAGMA_OMP_CLAUSE_LINEAR:
34961 {
34962 bool declare_simd = false;
34963 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
34964 declare_simd = true;
34965 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
34966 }
34967 c_name = "linear";
34968 break;
34969 case PRAGMA_OMP_CLAUSE_DEPEND:
34970 clauses = cp_parser_omp_clause_depend (parser, clauses,
34971 token->location);
34972 c_name = "depend";
34973 break;
34974 case PRAGMA_OMP_CLAUSE_MAP:
34975 clauses = cp_parser_omp_clause_map (parser, clauses);
34976 c_name = "map";
34977 break;
34978 case PRAGMA_OMP_CLAUSE_DEVICE:
34979 clauses = cp_parser_omp_clause_device (parser, clauses,
34980 token->location);
34981 c_name = "device";
34982 break;
34983 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
34984 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
34985 token->location);
34986 c_name = "dist_schedule";
34987 break;
34988 case PRAGMA_OMP_CLAUSE_PROC_BIND:
34989 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
34990 token->location);
34991 c_name = "proc_bind";
34992 break;
34993 case PRAGMA_OMP_CLAUSE_SAFELEN:
34994 clauses = cp_parser_omp_clause_safelen (parser, clauses,
34995 token->location);
34996 c_name = "safelen";
34997 break;
34998 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34999 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35000 token->location);
35001 c_name = "simdlen";
35002 break;
35003 case PRAGMA_OMP_CLAUSE_NOGROUP:
35004 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35005 token->location);
35006 c_name = "nogroup";
35007 break;
35008 case PRAGMA_OMP_CLAUSE_THREADS:
35009 clauses
35010 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35011 clauses, token->location);
35012 c_name = "threads";
35013 break;
35014 case PRAGMA_OMP_CLAUSE_SIMD:
35015 clauses
35016 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35017 clauses, token->location);
35018 c_name = "simd";
35019 break;
35020 default:
35021 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35022 goto saw_error;
35023 }
35024
35025 first = false;
35026
35027 if (((mask >> c_kind) & 1) == 0)
35028 {
35029 /* Remove the invalid clause(s) from the list to avoid
35030 confusing the rest of the compiler. */
35031 clauses = prev;
35032 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35033 }
35034 }
35035 saw_error:
35036 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35037 if (finish_p)
35038 {
35039 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35040 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35041 else
35042 return finish_omp_clauses (clauses, C_ORT_OMP);
35043 }
35044 return clauses;
35045 }
35046
35047 /* OpenMP 2.5:
35048 structured-block:
35049 statement
35050
35051 In practice, we're also interested in adding the statement to an
35052 outer node. So it is convenient if we work around the fact that
35053 cp_parser_statement calls add_stmt. */
35054
35055 static unsigned
35056 cp_parser_begin_omp_structured_block (cp_parser *parser)
35057 {
35058 unsigned save = parser->in_statement;
35059
35060 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35061 This preserves the "not within loop or switch" style error messages
35062 for nonsense cases like
35063 void foo() {
35064 #pragma omp single
35065 break;
35066 }
35067 */
35068 if (parser->in_statement)
35069 parser->in_statement = IN_OMP_BLOCK;
35070
35071 return save;
35072 }
35073
35074 static void
35075 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35076 {
35077 parser->in_statement = save;
35078 }
35079
35080 static tree
35081 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35082 {
35083 tree stmt = begin_omp_structured_block ();
35084 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35085
35086 cp_parser_statement (parser, NULL_TREE, false, if_p);
35087
35088 cp_parser_end_omp_structured_block (parser, save);
35089 return finish_omp_structured_block (stmt);
35090 }
35091
35092 /* OpenMP 2.5:
35093 # pragma omp atomic new-line
35094 expression-stmt
35095
35096 expression-stmt:
35097 x binop= expr | x++ | ++x | x-- | --x
35098 binop:
35099 +, *, -, /, &, ^, |, <<, >>
35100
35101 where x is an lvalue expression with scalar type.
35102
35103 OpenMP 3.1:
35104 # pragma omp atomic new-line
35105 update-stmt
35106
35107 # pragma omp atomic read new-line
35108 read-stmt
35109
35110 # pragma omp atomic write new-line
35111 write-stmt
35112
35113 # pragma omp atomic update new-line
35114 update-stmt
35115
35116 # pragma omp atomic capture new-line
35117 capture-stmt
35118
35119 # pragma omp atomic capture new-line
35120 capture-block
35121
35122 read-stmt:
35123 v = x
35124 write-stmt:
35125 x = expr
35126 update-stmt:
35127 expression-stmt | x = x binop expr
35128 capture-stmt:
35129 v = expression-stmt
35130 capture-block:
35131 { v = x; update-stmt; } | { update-stmt; v = x; }
35132
35133 OpenMP 4.0:
35134 update-stmt:
35135 expression-stmt | x = x binop expr | x = expr binop x
35136 capture-stmt:
35137 v = update-stmt
35138 capture-block:
35139 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35140
35141 where x and v are lvalue expressions with scalar type. */
35142
35143 static void
35144 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35145 {
35146 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35147 tree rhs1 = NULL_TREE, orig_lhs;
35148 location_t loc = pragma_tok->location;
35149 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35150 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35151 bool structured_block = false;
35152 bool first = true;
35153 tree clauses = NULL_TREE;
35154
35155 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35156 {
35157 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35158 cp_lexer_consume_token (parser->lexer);
35159
35160 first = false;
35161
35162 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35163 {
35164 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35165 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35166 const char *p = IDENTIFIER_POINTER (id);
35167 enum tree_code new_code = ERROR_MARK;
35168 enum omp_memory_order new_memory_order
35169 = OMP_MEMORY_ORDER_UNSPECIFIED;
35170
35171 if (!strcmp (p, "read"))
35172 new_code = OMP_ATOMIC_READ;
35173 else if (!strcmp (p, "write"))
35174 new_code = NOP_EXPR;
35175 else if (!strcmp (p, "update"))
35176 new_code = OMP_ATOMIC;
35177 else if (!strcmp (p, "capture"))
35178 new_code = OMP_ATOMIC_CAPTURE_NEW;
35179 else if (!strcmp (p, "seq_cst"))
35180 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35181 else if (!strcmp (p, "acq_rel"))
35182 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35183 else if (!strcmp (p, "release"))
35184 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35185 else if (!strcmp (p, "acquire"))
35186 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35187 else if (!strcmp (p, "relaxed"))
35188 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35189 else if (!strcmp (p, "hint"))
35190 {
35191 cp_lexer_consume_token (parser->lexer);
35192 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35193 continue;
35194 }
35195 else
35196 {
35197 p = NULL;
35198 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35199 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35200 "%<release%>, %<relaxed%> or %<hint%> clause");
35201 }
35202 if (p)
35203 {
35204 if (new_code != ERROR_MARK)
35205 {
35206 if (code != ERROR_MARK)
35207 error_at (cloc, "too many atomic clauses");
35208 else
35209 code = new_code;
35210 }
35211 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35212 {
35213 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35214 error_at (cloc, "too many memory order clauses");
35215 else
35216 memory_order = new_memory_order;
35217 }
35218 cp_lexer_consume_token (parser->lexer);
35219 continue;
35220 }
35221 }
35222 break;
35223 }
35224 cp_parser_require_pragma_eol (parser, pragma_tok);
35225
35226 if (code == ERROR_MARK)
35227 code = OMP_ATOMIC;
35228 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35229 {
35230 omp_requires_mask
35231 = (enum omp_requires) (omp_requires_mask
35232 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35233 switch ((enum omp_memory_order)
35234 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35235 {
35236 case OMP_MEMORY_ORDER_UNSPECIFIED:
35237 case OMP_MEMORY_ORDER_RELAXED:
35238 memory_order = OMP_MEMORY_ORDER_RELAXED;
35239 break;
35240 case OMP_MEMORY_ORDER_SEQ_CST:
35241 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35242 break;
35243 case OMP_MEMORY_ORDER_ACQ_REL:
35244 switch (code)
35245 {
35246 case OMP_ATOMIC_READ:
35247 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35248 break;
35249 case NOP_EXPR: /* atomic write */
35250 case OMP_ATOMIC:
35251 memory_order = OMP_MEMORY_ORDER_RELEASE;
35252 break;
35253 default:
35254 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35255 break;
35256 }
35257 break;
35258 default:
35259 gcc_unreachable ();
35260 }
35261 }
35262 else
35263 switch (code)
35264 {
35265 case OMP_ATOMIC_READ:
35266 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35267 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35268 {
35269 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35270 "%<acq_rel%> or %<release%> clauses");
35271 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35272 }
35273 break;
35274 case NOP_EXPR: /* atomic write */
35275 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35276 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35277 {
35278 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35279 "%<acq_rel%> or %<acquire%> clauses");
35280 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35281 }
35282 break;
35283 case OMP_ATOMIC:
35284 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35285 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35286 {
35287 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35288 "%<acq_rel%> or %<acquire%> clauses");
35289 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35290 }
35291 break;
35292 default:
35293 break;
35294 }
35295
35296 switch (code)
35297 {
35298 case OMP_ATOMIC_READ:
35299 case NOP_EXPR: /* atomic write */
35300 v = cp_parser_unary_expression (parser);
35301 if (v == error_mark_node)
35302 goto saw_error;
35303 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35304 goto saw_error;
35305 if (code == NOP_EXPR)
35306 lhs = cp_parser_expression (parser);
35307 else
35308 lhs = cp_parser_unary_expression (parser);
35309 if (lhs == error_mark_node)
35310 goto saw_error;
35311 if (code == NOP_EXPR)
35312 {
35313 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35314 opcode. */
35315 code = OMP_ATOMIC;
35316 rhs = lhs;
35317 lhs = v;
35318 v = NULL_TREE;
35319 }
35320 goto done;
35321 case OMP_ATOMIC_CAPTURE_NEW:
35322 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35323 {
35324 cp_lexer_consume_token (parser->lexer);
35325 structured_block = true;
35326 }
35327 else
35328 {
35329 v = cp_parser_unary_expression (parser);
35330 if (v == error_mark_node)
35331 goto saw_error;
35332 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35333 goto saw_error;
35334 }
35335 default:
35336 break;
35337 }
35338
35339 restart:
35340 lhs = cp_parser_unary_expression (parser);
35341 orig_lhs = lhs;
35342 switch (TREE_CODE (lhs))
35343 {
35344 case ERROR_MARK:
35345 goto saw_error;
35346
35347 case POSTINCREMENT_EXPR:
35348 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35349 code = OMP_ATOMIC_CAPTURE_OLD;
35350 /* FALLTHROUGH */
35351 case PREINCREMENT_EXPR:
35352 lhs = TREE_OPERAND (lhs, 0);
35353 opcode = PLUS_EXPR;
35354 rhs = integer_one_node;
35355 break;
35356
35357 case POSTDECREMENT_EXPR:
35358 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35359 code = OMP_ATOMIC_CAPTURE_OLD;
35360 /* FALLTHROUGH */
35361 case PREDECREMENT_EXPR:
35362 lhs = TREE_OPERAND (lhs, 0);
35363 opcode = MINUS_EXPR;
35364 rhs = integer_one_node;
35365 break;
35366
35367 case COMPOUND_EXPR:
35368 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35369 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35370 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35371 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35372 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35373 (TREE_OPERAND (lhs, 1), 0), 0)))
35374 == BOOLEAN_TYPE)
35375 /* Undo effects of boolean_increment for post {in,de}crement. */
35376 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35377 /* FALLTHRU */
35378 case MODIFY_EXPR:
35379 if (TREE_CODE (lhs) == MODIFY_EXPR
35380 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35381 {
35382 /* Undo effects of boolean_increment. */
35383 if (integer_onep (TREE_OPERAND (lhs, 1)))
35384 {
35385 /* This is pre or post increment. */
35386 rhs = TREE_OPERAND (lhs, 1);
35387 lhs = TREE_OPERAND (lhs, 0);
35388 opcode = NOP_EXPR;
35389 if (code == OMP_ATOMIC_CAPTURE_NEW
35390 && !structured_block
35391 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35392 code = OMP_ATOMIC_CAPTURE_OLD;
35393 break;
35394 }
35395 }
35396 /* FALLTHRU */
35397 default:
35398 switch (cp_lexer_peek_token (parser->lexer)->type)
35399 {
35400 case CPP_MULT_EQ:
35401 opcode = MULT_EXPR;
35402 break;
35403 case CPP_DIV_EQ:
35404 opcode = TRUNC_DIV_EXPR;
35405 break;
35406 case CPP_PLUS_EQ:
35407 opcode = PLUS_EXPR;
35408 break;
35409 case CPP_MINUS_EQ:
35410 opcode = MINUS_EXPR;
35411 break;
35412 case CPP_LSHIFT_EQ:
35413 opcode = LSHIFT_EXPR;
35414 break;
35415 case CPP_RSHIFT_EQ:
35416 opcode = RSHIFT_EXPR;
35417 break;
35418 case CPP_AND_EQ:
35419 opcode = BIT_AND_EXPR;
35420 break;
35421 case CPP_OR_EQ:
35422 opcode = BIT_IOR_EXPR;
35423 break;
35424 case CPP_XOR_EQ:
35425 opcode = BIT_XOR_EXPR;
35426 break;
35427 case CPP_EQ:
35428 enum cp_parser_prec oprec;
35429 cp_token *token;
35430 cp_lexer_consume_token (parser->lexer);
35431 cp_parser_parse_tentatively (parser);
35432 rhs1 = cp_parser_simple_cast_expression (parser);
35433 if (rhs1 == error_mark_node)
35434 {
35435 cp_parser_abort_tentative_parse (parser);
35436 cp_parser_simple_cast_expression (parser);
35437 goto saw_error;
35438 }
35439 token = cp_lexer_peek_token (parser->lexer);
35440 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35441 {
35442 cp_parser_abort_tentative_parse (parser);
35443 cp_parser_parse_tentatively (parser);
35444 rhs = cp_parser_binary_expression (parser, false, true,
35445 PREC_NOT_OPERATOR, NULL);
35446 if (rhs == error_mark_node)
35447 {
35448 cp_parser_abort_tentative_parse (parser);
35449 cp_parser_binary_expression (parser, false, true,
35450 PREC_NOT_OPERATOR, NULL);
35451 goto saw_error;
35452 }
35453 switch (TREE_CODE (rhs))
35454 {
35455 case MULT_EXPR:
35456 case TRUNC_DIV_EXPR:
35457 case RDIV_EXPR:
35458 case PLUS_EXPR:
35459 case MINUS_EXPR:
35460 case LSHIFT_EXPR:
35461 case RSHIFT_EXPR:
35462 case BIT_AND_EXPR:
35463 case BIT_IOR_EXPR:
35464 case BIT_XOR_EXPR:
35465 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35466 {
35467 if (cp_parser_parse_definitely (parser))
35468 {
35469 opcode = TREE_CODE (rhs);
35470 rhs1 = TREE_OPERAND (rhs, 0);
35471 rhs = TREE_OPERAND (rhs, 1);
35472 goto stmt_done;
35473 }
35474 else
35475 goto saw_error;
35476 }
35477 break;
35478 default:
35479 break;
35480 }
35481 cp_parser_abort_tentative_parse (parser);
35482 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35483 {
35484 rhs = cp_parser_expression (parser);
35485 if (rhs == error_mark_node)
35486 goto saw_error;
35487 opcode = NOP_EXPR;
35488 rhs1 = NULL_TREE;
35489 goto stmt_done;
35490 }
35491 cp_parser_error (parser,
35492 "invalid form of %<#pragma omp atomic%>");
35493 goto saw_error;
35494 }
35495 if (!cp_parser_parse_definitely (parser))
35496 goto saw_error;
35497 switch (token->type)
35498 {
35499 case CPP_SEMICOLON:
35500 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35501 {
35502 code = OMP_ATOMIC_CAPTURE_OLD;
35503 v = lhs;
35504 lhs = NULL_TREE;
35505 lhs1 = rhs1;
35506 rhs1 = NULL_TREE;
35507 cp_lexer_consume_token (parser->lexer);
35508 goto restart;
35509 }
35510 else if (structured_block)
35511 {
35512 opcode = NOP_EXPR;
35513 rhs = rhs1;
35514 rhs1 = NULL_TREE;
35515 goto stmt_done;
35516 }
35517 cp_parser_error (parser,
35518 "invalid form of %<#pragma omp atomic%>");
35519 goto saw_error;
35520 case CPP_MULT:
35521 opcode = MULT_EXPR;
35522 break;
35523 case CPP_DIV:
35524 opcode = TRUNC_DIV_EXPR;
35525 break;
35526 case CPP_PLUS:
35527 opcode = PLUS_EXPR;
35528 break;
35529 case CPP_MINUS:
35530 opcode = MINUS_EXPR;
35531 break;
35532 case CPP_LSHIFT:
35533 opcode = LSHIFT_EXPR;
35534 break;
35535 case CPP_RSHIFT:
35536 opcode = RSHIFT_EXPR;
35537 break;
35538 case CPP_AND:
35539 opcode = BIT_AND_EXPR;
35540 break;
35541 case CPP_OR:
35542 opcode = BIT_IOR_EXPR;
35543 break;
35544 case CPP_XOR:
35545 opcode = BIT_XOR_EXPR;
35546 break;
35547 default:
35548 cp_parser_error (parser,
35549 "invalid operator for %<#pragma omp atomic%>");
35550 goto saw_error;
35551 }
35552 oprec = TOKEN_PRECEDENCE (token);
35553 gcc_assert (oprec != PREC_NOT_OPERATOR);
35554 if (commutative_tree_code (opcode))
35555 oprec = (enum cp_parser_prec) (oprec - 1);
35556 cp_lexer_consume_token (parser->lexer);
35557 rhs = cp_parser_binary_expression (parser, false, false,
35558 oprec, NULL);
35559 if (rhs == error_mark_node)
35560 goto saw_error;
35561 goto stmt_done;
35562 /* FALLTHROUGH */
35563 default:
35564 cp_parser_error (parser,
35565 "invalid operator for %<#pragma omp atomic%>");
35566 goto saw_error;
35567 }
35568 cp_lexer_consume_token (parser->lexer);
35569
35570 rhs = cp_parser_expression (parser);
35571 if (rhs == error_mark_node)
35572 goto saw_error;
35573 break;
35574 }
35575 stmt_done:
35576 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35577 {
35578 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35579 goto saw_error;
35580 v = cp_parser_unary_expression (parser);
35581 if (v == error_mark_node)
35582 goto saw_error;
35583 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35584 goto saw_error;
35585 lhs1 = cp_parser_unary_expression (parser);
35586 if (lhs1 == error_mark_node)
35587 goto saw_error;
35588 }
35589 if (structured_block)
35590 {
35591 cp_parser_consume_semicolon_at_end_of_statement (parser);
35592 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35593 }
35594 done:
35595 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35596 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35597 rhs1, clauses, memory_order);
35598 if (!structured_block)
35599 cp_parser_consume_semicolon_at_end_of_statement (parser);
35600 return;
35601
35602 saw_error:
35603 cp_parser_skip_to_end_of_block_or_statement (parser);
35604 if (structured_block)
35605 {
35606 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35607 cp_lexer_consume_token (parser->lexer);
35608 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35609 {
35610 cp_parser_skip_to_end_of_block_or_statement (parser);
35611 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35612 cp_lexer_consume_token (parser->lexer);
35613 }
35614 }
35615 }
35616
35617
35618 /* OpenMP 2.5:
35619 # pragma omp barrier new-line */
35620
35621 static void
35622 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35623 {
35624 cp_parser_require_pragma_eol (parser, pragma_tok);
35625 finish_omp_barrier ();
35626 }
35627
35628 /* OpenMP 2.5:
35629 # pragma omp critical [(name)] new-line
35630 structured-block
35631
35632 OpenMP 4.5:
35633 # pragma omp critical [(name) [hint(expression)]] new-line
35634 structured-block */
35635
35636 #define OMP_CRITICAL_CLAUSE_MASK \
35637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35638
35639 static tree
35640 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35641 {
35642 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
35643
35644 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35645 {
35646 matching_parens parens;
35647 parens.consume_open (parser);
35648
35649 name = cp_parser_identifier (parser);
35650
35651 if (name == error_mark_node
35652 || !parens.require_close (parser))
35653 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35654 /*or_comma=*/false,
35655 /*consume_paren=*/true);
35656 if (name == error_mark_node)
35657 name = NULL;
35658
35659 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
35660 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35661 cp_lexer_consume_token (parser->lexer);
35662
35663 clauses = cp_parser_omp_all_clauses (parser,
35664 OMP_CRITICAL_CLAUSE_MASK,
35665 "#pragma omp critical", pragma_tok);
35666 }
35667 else
35668 cp_parser_require_pragma_eol (parser, pragma_tok);
35669
35670 stmt = cp_parser_omp_structured_block (parser, if_p);
35671 return c_finish_omp_critical (input_location, stmt, name, clauses);
35672 }
35673
35674 /* OpenMP 5.0:
35675 # pragma omp depobj ( depobj ) depobj-clause new-line
35676
35677 depobj-clause:
35678 depend (dependence-type : locator)
35679 destroy
35680 update (dependence-type)
35681
35682 dependence-type:
35683 in
35684 out
35685 inout
35686 mutexinout */
35687
35688 static void
35689 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
35690 {
35691 location_t loc = pragma_tok->location;
35692 matching_parens parens;
35693 if (!parens.require_open (parser))
35694 {
35695 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35696 return;
35697 }
35698
35699 tree depobj = cp_parser_assignment_expression (parser);
35700
35701 if (!parens.require_close (parser))
35702 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35703 /*or_comma=*/false,
35704 /*consume_paren=*/true);
35705
35706 tree clause = NULL_TREE;
35707 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
35708 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
35709 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35710 {
35711 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35712 const char *p = IDENTIFIER_POINTER (id);
35713
35714 cp_lexer_consume_token (parser->lexer);
35715 if (!strcmp ("depend", p))
35716 {
35717 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
35718 if (clause)
35719 clause = finish_omp_clauses (clause, C_ORT_OMP);
35720 if (!clause)
35721 clause = error_mark_node;
35722 }
35723 else if (!strcmp ("destroy", p))
35724 kind = OMP_CLAUSE_DEPEND_LAST;
35725 else if (!strcmp ("update", p))
35726 {
35727 matching_parens c_parens;
35728 if (c_parens.require_open (parser))
35729 {
35730 location_t c2_loc
35731 = cp_lexer_peek_token (parser->lexer)->location;
35732 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35733 {
35734 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
35735 const char *p2 = IDENTIFIER_POINTER (id2);
35736
35737 cp_lexer_consume_token (parser->lexer);
35738 if (!strcmp ("in", p2))
35739 kind = OMP_CLAUSE_DEPEND_IN;
35740 else if (!strcmp ("out", p2))
35741 kind = OMP_CLAUSE_DEPEND_OUT;
35742 else if (!strcmp ("inout", p2))
35743 kind = OMP_CLAUSE_DEPEND_INOUT;
35744 else if (!strcmp ("mutexinoutset", p2))
35745 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
35746 }
35747 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
35748 {
35749 clause = error_mark_node;
35750 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
35751 "%<mutexinoutset%>");
35752 }
35753 if (!c_parens.require_close (parser))
35754 cp_parser_skip_to_closing_parenthesis (parser,
35755 /*recovering=*/true,
35756 /*or_comma=*/false,
35757 /*consume_paren=*/true);
35758 }
35759 else
35760 clause = error_mark_node;
35761 }
35762 }
35763 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
35764 {
35765 clause = error_mark_node;
35766 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
35767 }
35768 cp_parser_require_pragma_eol (parser, pragma_tok);
35769
35770 finish_omp_depobj (loc, depobj, kind, clause);
35771 }
35772
35773
35774 /* OpenMP 2.5:
35775 # pragma omp flush flush-vars[opt] new-line
35776
35777 flush-vars:
35778 ( variable-list )
35779
35780 OpenMP 5.0:
35781 # pragma omp flush memory-order-clause new-line */
35782
35783 static void
35784 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
35785 {
35786 enum memmodel mo = MEMMODEL_LAST;
35787 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35788 {
35789 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35790 const char *p = IDENTIFIER_POINTER (id);
35791 if (!strcmp (p, "acq_rel"))
35792 mo = MEMMODEL_ACQ_REL;
35793 else if (!strcmp (p, "release"))
35794 mo = MEMMODEL_RELEASE;
35795 else if (!strcmp (p, "acquire"))
35796 mo = MEMMODEL_ACQUIRE;
35797 else
35798 error_at (cp_lexer_peek_token (parser->lexer)->location,
35799 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35800 cp_lexer_consume_token (parser->lexer);
35801 }
35802 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35803 {
35804 if (mo != MEMMODEL_LAST)
35805 error_at (cp_lexer_peek_token (parser->lexer)->location,
35806 "%<flush%> list specified together with memory order "
35807 "clause");
35808 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35809 }
35810 cp_parser_require_pragma_eol (parser, pragma_tok);
35811
35812 finish_omp_flush (mo);
35813 }
35814
35815 /* Helper function, to parse omp for increment expression. */
35816
35817 static tree
35818 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
35819 {
35820 tree cond = cp_parser_binary_expression (parser, false, true,
35821 PREC_NOT_OPERATOR, NULL);
35822 if (cond == error_mark_node
35823 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35824 {
35825 cp_parser_skip_to_end_of_statement (parser);
35826 return error_mark_node;
35827 }
35828
35829 switch (TREE_CODE (cond))
35830 {
35831 case GT_EXPR:
35832 case GE_EXPR:
35833 case LT_EXPR:
35834 case LE_EXPR:
35835 break;
35836 case NE_EXPR:
35837 if (code != OACC_LOOP)
35838 break;
35839 gcc_fallthrough ();
35840 default:
35841 return error_mark_node;
35842 }
35843
35844 /* If decl is an iterator, preserve LHS and RHS of the relational
35845 expr until finish_omp_for. */
35846 if (decl
35847 && (type_dependent_expression_p (decl)
35848 || CLASS_TYPE_P (TREE_TYPE (decl))))
35849 return cond;
35850
35851 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
35852 TREE_CODE (cond),
35853 TREE_OPERAND (cond, 0), ERROR_MARK,
35854 TREE_OPERAND (cond, 1), ERROR_MARK,
35855 /*overload=*/NULL, tf_warning_or_error);
35856 }
35857
35858 /* Helper function, to parse omp for increment expression. */
35859
35860 static tree
35861 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
35862 {
35863 cp_token *token = cp_lexer_peek_token (parser->lexer);
35864 enum tree_code op;
35865 tree lhs, rhs;
35866 cp_id_kind idk;
35867 bool decl_first;
35868
35869 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35870 {
35871 op = (token->type == CPP_PLUS_PLUS
35872 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
35873 cp_lexer_consume_token (parser->lexer);
35874 lhs = cp_parser_simple_cast_expression (parser);
35875 if (lhs != decl
35876 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35877 return error_mark_node;
35878 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35879 }
35880
35881 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
35882 if (lhs != decl
35883 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
35884 return error_mark_node;
35885
35886 token = cp_lexer_peek_token (parser->lexer);
35887 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
35888 {
35889 op = (token->type == CPP_PLUS_PLUS
35890 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
35891 cp_lexer_consume_token (parser->lexer);
35892 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
35893 }
35894
35895 op = cp_parser_assignment_operator_opt (parser);
35896 if (op == ERROR_MARK)
35897 return error_mark_node;
35898
35899 if (op != NOP_EXPR)
35900 {
35901 rhs = cp_parser_assignment_expression (parser);
35902 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
35903 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35904 }
35905
35906 lhs = cp_parser_binary_expression (parser, false, false,
35907 PREC_ADDITIVE_EXPRESSION, NULL);
35908 token = cp_lexer_peek_token (parser->lexer);
35909 decl_first = (lhs == decl
35910 || (processing_template_decl && cp_tree_equal (lhs, decl)));
35911 if (decl_first)
35912 lhs = NULL_TREE;
35913 if (token->type != CPP_PLUS
35914 && token->type != CPP_MINUS)
35915 return error_mark_node;
35916
35917 do
35918 {
35919 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
35920 cp_lexer_consume_token (parser->lexer);
35921 rhs = cp_parser_binary_expression (parser, false, false,
35922 PREC_ADDITIVE_EXPRESSION, NULL);
35923 token = cp_lexer_peek_token (parser->lexer);
35924 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
35925 {
35926 if (lhs == NULL_TREE)
35927 {
35928 if (op == PLUS_EXPR)
35929 lhs = rhs;
35930 else
35931 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
35932 tf_warning_or_error);
35933 }
35934 else
35935 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
35936 ERROR_MARK, NULL, tf_warning_or_error);
35937 }
35938 }
35939 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
35940
35941 if (!decl_first)
35942 {
35943 if ((rhs != decl
35944 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
35945 || op == MINUS_EXPR)
35946 return error_mark_node;
35947 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
35948 }
35949 else
35950 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
35951
35952 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
35953 }
35954
35955 /* Parse the initialization statement of an OpenMP for loop.
35956
35957 Return true if the resulting construct should have an
35958 OMP_CLAUSE_PRIVATE added to it. */
35959
35960 static tree
35961 cp_parser_omp_for_loop_init (cp_parser *parser,
35962 tree &this_pre_body,
35963 vec<tree, va_gc> *&for_block,
35964 tree &init,
35965 tree &orig_init,
35966 tree &decl,
35967 tree &real_decl)
35968 {
35969 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35970 return NULL_TREE;
35971
35972 tree add_private_clause = NULL_TREE;
35973
35974 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
35975
35976 init-expr:
35977 var = lb
35978 integer-type var = lb
35979 random-access-iterator-type var = lb
35980 pointer-type var = lb
35981 */
35982 cp_decl_specifier_seq type_specifiers;
35983
35984 /* First, try to parse as an initialized declaration. See
35985 cp_parser_condition, from whence the bulk of this is copied. */
35986
35987 cp_parser_parse_tentatively (parser);
35988 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
35989 /*is_trailing_return=*/false,
35990 &type_specifiers);
35991 if (cp_parser_parse_definitely (parser))
35992 {
35993 /* If parsing a type specifier seq succeeded, then this
35994 MUST be a initialized declaration. */
35995 tree asm_specification, attributes;
35996 cp_declarator *declarator;
35997
35998 declarator = cp_parser_declarator (parser,
35999 CP_PARSER_DECLARATOR_NAMED,
36000 /*ctor_dtor_or_conv_p=*/NULL,
36001 /*parenthesized_p=*/NULL,
36002 /*member_p=*/false,
36003 /*friend_p=*/false);
36004 attributes = cp_parser_attributes_opt (parser);
36005 asm_specification = cp_parser_asm_specification_opt (parser);
36006
36007 if (declarator == cp_error_declarator)
36008 cp_parser_skip_to_end_of_statement (parser);
36009
36010 else
36011 {
36012 tree pushed_scope, auto_node;
36013
36014 decl = start_decl (declarator, &type_specifiers,
36015 SD_INITIALIZED, attributes,
36016 /*prefix_attributes=*/NULL_TREE,
36017 &pushed_scope);
36018
36019 auto_node = type_uses_auto (TREE_TYPE (decl));
36020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36021 {
36022 if (cp_lexer_next_token_is (parser->lexer,
36023 CPP_OPEN_PAREN))
36024 error ("parenthesized initialization is not allowed in "
36025 "OpenMP %<for%> loop");
36026 else
36027 /* Trigger an error. */
36028 cp_parser_require (parser, CPP_EQ, RT_EQ);
36029
36030 init = error_mark_node;
36031 cp_parser_skip_to_end_of_statement (parser);
36032 }
36033 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36034 || type_dependent_expression_p (decl)
36035 || auto_node)
36036 {
36037 bool is_direct_init, is_non_constant_init;
36038
36039 init = cp_parser_initializer (parser,
36040 &is_direct_init,
36041 &is_non_constant_init);
36042
36043 if (auto_node)
36044 {
36045 TREE_TYPE (decl)
36046 = do_auto_deduction (TREE_TYPE (decl), init,
36047 auto_node);
36048
36049 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36050 && !type_dependent_expression_p (decl))
36051 goto non_class;
36052 }
36053
36054 cp_finish_decl (decl, init, !is_non_constant_init,
36055 asm_specification,
36056 LOOKUP_ONLYCONVERTING);
36057 orig_init = init;
36058 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36059 {
36060 vec_safe_push (for_block, this_pre_body);
36061 init = NULL_TREE;
36062 }
36063 else
36064 {
36065 init = pop_stmt_list (this_pre_body);
36066 if (init && TREE_CODE (init) == STATEMENT_LIST)
36067 {
36068 tree_stmt_iterator i = tsi_start (init);
36069 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36070 while (!tsi_end_p (i))
36071 {
36072 tree t = tsi_stmt (i);
36073 if (TREE_CODE (t) == DECL_EXPR
36074 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36075 {
36076 tsi_delink (&i);
36077 vec_safe_push (for_block, t);
36078 continue;
36079 }
36080 break;
36081 }
36082 if (tsi_one_before_end_p (i))
36083 {
36084 tree t = tsi_stmt (i);
36085 tsi_delink (&i);
36086 free_stmt_list (init);
36087 init = t;
36088 }
36089 }
36090 }
36091 this_pre_body = NULL_TREE;
36092 }
36093 else
36094 {
36095 /* Consume '='. */
36096 cp_lexer_consume_token (parser->lexer);
36097 init = cp_parser_assignment_expression (parser);
36098
36099 non_class:
36100 if (TYPE_REF_P (TREE_TYPE (decl)))
36101 init = error_mark_node;
36102 else
36103 cp_finish_decl (decl, NULL_TREE,
36104 /*init_const_expr_p=*/false,
36105 asm_specification,
36106 LOOKUP_ONLYCONVERTING);
36107 }
36108
36109 if (pushed_scope)
36110 pop_scope (pushed_scope);
36111 }
36112 }
36113 else
36114 {
36115 cp_id_kind idk;
36116 /* If parsing a type specifier sequence failed, then
36117 this MUST be a simple expression. */
36118 cp_parser_parse_tentatively (parser);
36119 decl = cp_parser_primary_expression (parser, false, false,
36120 false, &idk);
36121 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36122 if (!cp_parser_error_occurred (parser)
36123 && decl
36124 && (TREE_CODE (decl) == COMPONENT_REF
36125 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36126 {
36127 cp_parser_abort_tentative_parse (parser);
36128 cp_parser_parse_tentatively (parser);
36129 cp_token *token = cp_lexer_peek_token (parser->lexer);
36130 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36131 /*check_dependency_p=*/true,
36132 /*template_p=*/NULL,
36133 /*declarator_p=*/false,
36134 /*optional_p=*/false);
36135 if (name != error_mark_node
36136 && last_tok == cp_lexer_peek_token (parser->lexer))
36137 {
36138 decl = cp_parser_lookup_name_simple (parser, name,
36139 token->location);
36140 if (TREE_CODE (decl) == FIELD_DECL)
36141 add_private_clause = omp_privatize_field (decl, false);
36142 }
36143 cp_parser_abort_tentative_parse (parser);
36144 cp_parser_parse_tentatively (parser);
36145 decl = cp_parser_primary_expression (parser, false, false,
36146 false, &idk);
36147 }
36148 if (!cp_parser_error_occurred (parser)
36149 && decl
36150 && DECL_P (decl)
36151 && CLASS_TYPE_P (TREE_TYPE (decl)))
36152 {
36153 tree rhs;
36154
36155 cp_parser_parse_definitely (parser);
36156 cp_parser_require (parser, CPP_EQ, RT_EQ);
36157 rhs = cp_parser_assignment_expression (parser);
36158 orig_init = rhs;
36159 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36160 decl, NOP_EXPR,
36161 rhs,
36162 tf_warning_or_error));
36163 if (!add_private_clause)
36164 add_private_clause = decl;
36165 }
36166 else
36167 {
36168 decl = NULL;
36169 cp_parser_abort_tentative_parse (parser);
36170 init = cp_parser_expression (parser);
36171 if (init)
36172 {
36173 if (TREE_CODE (init) == MODIFY_EXPR
36174 || TREE_CODE (init) == MODOP_EXPR)
36175 real_decl = TREE_OPERAND (init, 0);
36176 }
36177 }
36178 }
36179 return add_private_clause;
36180 }
36181
36182 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36183
36184 void
36185 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36186 tree &decl, tree &orig_decl, tree &init,
36187 tree &orig_init, tree &cond, tree &incr)
36188 {
36189 tree begin, end, range_temp_decl = NULL_TREE;
36190 tree iter_type, begin_expr, end_expr;
36191
36192 if (processing_template_decl)
36193 {
36194 if (check_for_bare_parameter_packs (init))
36195 init = error_mark_node;
36196 if (!type_dependent_expression_p (init)
36197 /* do_auto_deduction doesn't mess with template init-lists. */
36198 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36199 {
36200 tree d = decl;
36201 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36202 {
36203 tree v = DECL_VALUE_EXPR (decl);
36204 if (TREE_CODE (v) == ARRAY_REF
36205 && VAR_P (TREE_OPERAND (v, 0))
36206 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36207 d = TREE_OPERAND (v, 0);
36208 }
36209 do_range_for_auto_deduction (d, init);
36210 }
36211 cond = global_namespace;
36212 incr = NULL_TREE;
36213 orig_init = init;
36214 if (this_pre_body)
36215 this_pre_body = pop_stmt_list (this_pre_body);
36216 return;
36217 }
36218
36219 init = mark_lvalue_use (init);
36220
36221 if (decl == error_mark_node || init == error_mark_node)
36222 /* If an error happened previously do nothing or else a lot of
36223 unhelpful errors would be issued. */
36224 begin_expr = end_expr = iter_type = error_mark_node;
36225 else
36226 {
36227 tree range_temp;
36228
36229 if (VAR_P (init)
36230 && array_of_runtime_bound_p (TREE_TYPE (init)))
36231 /* Can't bind a reference to an array of runtime bound. */
36232 range_temp = init;
36233 else
36234 {
36235 range_temp = build_range_temp (init);
36236 DECL_NAME (range_temp) = NULL_TREE;
36237 pushdecl (range_temp);
36238 cp_finish_decl (range_temp, init,
36239 /*is_constant_init*/false, NULL_TREE,
36240 LOOKUP_ONLYCONVERTING);
36241 range_temp_decl = range_temp;
36242 range_temp = convert_from_reference (range_temp);
36243 }
36244 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36245 &begin_expr, &end_expr);
36246 }
36247
36248 tree end_iter_type = iter_type;
36249 if (cxx_dialect >= cxx17)
36250 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36251 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36252 TREE_USED (end) = 1;
36253 DECL_ARTIFICIAL (end) = 1;
36254 pushdecl (end);
36255 cp_finish_decl (end, end_expr,
36256 /*is_constant_init*/false, NULL_TREE,
36257 LOOKUP_ONLYCONVERTING);
36258
36259 /* The new for initialization statement. */
36260 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36261 TREE_USED (begin) = 1;
36262 DECL_ARTIFICIAL (begin) = 1;
36263 pushdecl (begin);
36264 orig_init = init;
36265 if (CLASS_TYPE_P (iter_type))
36266 init = NULL_TREE;
36267 else
36268 {
36269 init = begin_expr;
36270 begin_expr = NULL_TREE;
36271 }
36272 cp_finish_decl (begin, begin_expr,
36273 /*is_constant_init*/false, NULL_TREE,
36274 LOOKUP_ONLYCONVERTING);
36275
36276 /* The new for condition. */
36277 if (CLASS_TYPE_P (iter_type))
36278 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36279 else
36280 cond = build_x_binary_op (input_location, NE_EXPR,
36281 begin, ERROR_MARK,
36282 end, ERROR_MARK,
36283 NULL, tf_warning_or_error);
36284
36285 /* The new increment expression. */
36286 if (CLASS_TYPE_P (iter_type))
36287 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36288 else
36289 incr = finish_unary_op_expr (input_location,
36290 PREINCREMENT_EXPR, begin,
36291 tf_warning_or_error);
36292
36293 orig_decl = decl;
36294 decl = begin;
36295 if (for_block)
36296 {
36297 vec_safe_push (for_block, this_pre_body);
36298 this_pre_body = NULL_TREE;
36299 }
36300
36301 tree decomp_first_name = NULL_TREE;
36302 unsigned decomp_cnt = 0;
36303 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36304 {
36305 tree v = DECL_VALUE_EXPR (orig_decl);
36306 if (TREE_CODE (v) == ARRAY_REF
36307 && VAR_P (TREE_OPERAND (v, 0))
36308 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36309 {
36310 tree d = orig_decl;
36311 orig_decl = TREE_OPERAND (v, 0);
36312 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36313 decomp_first_name = d;
36314 }
36315 }
36316
36317 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36318 if (auto_node)
36319 {
36320 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36321 tf_none);
36322 if (!error_operand_p (t))
36323 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36324 t, auto_node);
36325 }
36326
36327 tree v = make_tree_vec (decomp_cnt + 3);
36328 TREE_VEC_ELT (v, 0) = range_temp_decl;
36329 TREE_VEC_ELT (v, 1) = end;
36330 TREE_VEC_ELT (v, 2) = orig_decl;
36331 for (unsigned i = 0; i < decomp_cnt; i++)
36332 {
36333 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36334 decomp_first_name = DECL_CHAIN (decomp_first_name);
36335 }
36336 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36337 }
36338
36339 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36340 inside of the collapsed body. */
36341
36342 void
36343 cp_finish_omp_range_for (tree orig, tree begin)
36344 {
36345 gcc_assert (TREE_CODE (orig) == TREE_LIST
36346 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36347 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36348 tree decomp_first_name = NULL_TREE;
36349 unsigned int decomp_cnt = 0;
36350
36351 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36352 {
36353 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36354 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36355 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36356 }
36357
36358 /* The declaration is initialized with *__begin inside the loop body. */
36359 cp_finish_decl (decl,
36360 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36361 tf_warning_or_error),
36362 /*is_constant_init*/false, NULL_TREE,
36363 LOOKUP_ONLYCONVERTING);
36364 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36365 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36366 }
36367
36368 /* Parse the restricted form of the for statement allowed by OpenMP. */
36369
36370 static tree
36371 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36372 tree *cclauses, bool *if_p)
36373 {
36374 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36375 tree orig_decl;
36376 tree real_decl, initv, condv, incrv, declv, orig_declv;
36377 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36378 location_t loc_first;
36379 bool collapse_err = false;
36380 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36381 vec<tree, va_gc> *for_block = make_tree_vector ();
36382 auto_vec<tree, 4> orig_inits;
36383 bool tiling = false;
36384
36385 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36386 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36387 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36388 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36389 {
36390 tiling = true;
36391 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36392 }
36393 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36394 && OMP_CLAUSE_ORDERED_EXPR (cl))
36395 {
36396 ordered_cl = cl;
36397 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36398 }
36399
36400 if (ordered && ordered < collapse)
36401 {
36402 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36403 "%<ordered%> clause parameter is less than %<collapse%>");
36404 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36405 = build_int_cst (NULL_TREE, collapse);
36406 ordered = collapse;
36407 }
36408 if (ordered)
36409 {
36410 for (tree *pc = &clauses; *pc; )
36411 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36412 {
36413 error_at (OMP_CLAUSE_LOCATION (*pc),
36414 "%<linear%> clause may not be specified together "
36415 "with %<ordered%> clause with a parameter");
36416 *pc = OMP_CLAUSE_CHAIN (*pc);
36417 }
36418 else
36419 pc = &OMP_CLAUSE_CHAIN (*pc);
36420 }
36421
36422 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36423 count = ordered ? ordered : collapse;
36424
36425 declv = make_tree_vec (count);
36426 initv = make_tree_vec (count);
36427 condv = make_tree_vec (count);
36428 incrv = make_tree_vec (count);
36429 orig_declv = NULL_TREE;
36430
36431 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36432
36433 for (i = 0; i < count; i++)
36434 {
36435 int bracecount = 0;
36436 tree add_private_clause = NULL_TREE;
36437 location_t loc;
36438
36439 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36440 {
36441 if (!collapse_err)
36442 cp_parser_error (parser, "for statement expected");
36443 return NULL;
36444 }
36445 loc = cp_lexer_consume_token (parser->lexer)->location;
36446
36447 matching_parens parens;
36448 if (!parens.require_open (parser))
36449 return NULL;
36450
36451 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36452 this_pre_body = push_stmt_list ();
36453
36454 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36455 {
36456 /* Save tokens so that we can put them back. */
36457 cp_lexer_save_tokens (parser->lexer);
36458
36459 /* Look for ':' that is not nested in () or {}. */
36460 bool is_range_for
36461 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36462 /*recovering=*/false,
36463 CPP_COLON,
36464 /*consume_paren=*/
36465 false) == -1);
36466
36467 /* Roll back the tokens we skipped. */
36468 cp_lexer_rollback_tokens (parser->lexer);
36469
36470 if (is_range_for)
36471 {
36472 bool saved_colon_corrects_to_scope_p
36473 = parser->colon_corrects_to_scope_p;
36474
36475 /* A colon is used in range-based for. */
36476 parser->colon_corrects_to_scope_p = false;
36477
36478 /* Parse the declaration. */
36479 cp_parser_simple_declaration (parser,
36480 /*function_definition_allowed_p=*/
36481 false, &decl);
36482 parser->colon_corrects_to_scope_p
36483 = saved_colon_corrects_to_scope_p;
36484
36485 cp_parser_require (parser, CPP_COLON, RT_COLON);
36486
36487 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36488 false, 0, true);
36489
36490 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36491 orig_decl, init, orig_init,
36492 cond, incr);
36493 if (this_pre_body)
36494 {
36495 if (pre_body)
36496 {
36497 tree t = pre_body;
36498 pre_body = push_stmt_list ();
36499 add_stmt (t);
36500 add_stmt (this_pre_body);
36501 pre_body = pop_stmt_list (pre_body);
36502 }
36503 else
36504 pre_body = this_pre_body;
36505 }
36506
36507 if (ordered_cl)
36508 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36509 "%<ordered%> clause with parameter on "
36510 "range-based %<for%> loop");
36511
36512 goto parse_close_paren;
36513 }
36514 }
36515
36516 add_private_clause
36517 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36518 init, orig_init, decl, real_decl);
36519
36520 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36521 if (this_pre_body)
36522 {
36523 this_pre_body = pop_stmt_list (this_pre_body);
36524 if (pre_body)
36525 {
36526 tree t = pre_body;
36527 pre_body = push_stmt_list ();
36528 add_stmt (t);
36529 add_stmt (this_pre_body);
36530 pre_body = pop_stmt_list (pre_body);
36531 }
36532 else
36533 pre_body = this_pre_body;
36534 }
36535
36536 if (decl)
36537 real_decl = decl;
36538 if (cclauses != NULL
36539 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36540 && real_decl != NULL_TREE)
36541 {
36542 tree *c;
36543 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36544 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36545 && OMP_CLAUSE_DECL (*c) == real_decl)
36546 {
36547 error_at (loc, "iteration variable %qD"
36548 " should not be firstprivate", real_decl);
36549 *c = OMP_CLAUSE_CHAIN (*c);
36550 }
36551 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36552 && OMP_CLAUSE_DECL (*c) == real_decl)
36553 {
36554 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36555 tree l = *c;
36556 *c = OMP_CLAUSE_CHAIN (*c);
36557 if (code == OMP_SIMD)
36558 {
36559 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36560 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36561 }
36562 else
36563 {
36564 OMP_CLAUSE_CHAIN (l) = clauses;
36565 clauses = l;
36566 }
36567 add_private_clause = NULL_TREE;
36568 }
36569 else
36570 {
36571 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36572 && OMP_CLAUSE_DECL (*c) == real_decl)
36573 add_private_clause = NULL_TREE;
36574 c = &OMP_CLAUSE_CHAIN (*c);
36575 }
36576 }
36577
36578 if (add_private_clause)
36579 {
36580 tree c;
36581 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36582 {
36583 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36584 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36585 && OMP_CLAUSE_DECL (c) == decl)
36586 break;
36587 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36588 && OMP_CLAUSE_DECL (c) == decl)
36589 error_at (loc, "iteration variable %qD "
36590 "should not be firstprivate",
36591 decl);
36592 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36593 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36594 && OMP_CLAUSE_DECL (c) == decl)
36595 error_at (loc, "iteration variable %qD should not be reduction",
36596 decl);
36597 }
36598 if (c == NULL)
36599 {
36600 if (code != OMP_SIMD)
36601 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36602 else if (collapse == 1)
36603 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36604 else
36605 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36606 OMP_CLAUSE_DECL (c) = add_private_clause;
36607 c = finish_omp_clauses (c, C_ORT_OMP);
36608 if (c)
36609 {
36610 OMP_CLAUSE_CHAIN (c) = clauses;
36611 clauses = c;
36612 /* For linear, signal that we need to fill up
36613 the so far unknown linear step. */
36614 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36615 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36616 }
36617 }
36618 }
36619
36620 cond = NULL;
36621 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36622 cond = cp_parser_omp_for_cond (parser, decl, code);
36623 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36624
36625 incr = NULL;
36626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36627 {
36628 /* If decl is an iterator, preserve the operator on decl
36629 until finish_omp_for. */
36630 if (real_decl
36631 && ((processing_template_decl
36632 && (TREE_TYPE (real_decl) == NULL_TREE
36633 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
36634 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
36635 incr = cp_parser_omp_for_incr (parser, real_decl);
36636 else
36637 incr = cp_parser_expression (parser);
36638 if (!EXPR_HAS_LOCATION (incr))
36639 protected_set_expr_location (incr, input_location);
36640 }
36641
36642 parse_close_paren:
36643 if (!parens.require_close (parser))
36644 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36645 /*or_comma=*/false,
36646 /*consume_paren=*/true);
36647
36648 TREE_VEC_ELT (declv, i) = decl;
36649 TREE_VEC_ELT (initv, i) = init;
36650 TREE_VEC_ELT (condv, i) = cond;
36651 TREE_VEC_ELT (incrv, i) = incr;
36652 if (orig_init)
36653 {
36654 orig_inits.safe_grow_cleared (i + 1);
36655 orig_inits[i] = orig_init;
36656 }
36657 if (orig_decl)
36658 {
36659 if (!orig_declv)
36660 orig_declv = copy_node (declv);
36661 TREE_VEC_ELT (orig_declv, i) = orig_decl;
36662 }
36663 else if (orig_declv)
36664 TREE_VEC_ELT (orig_declv, i) = decl;
36665
36666 if (i == count - 1)
36667 break;
36668
36669 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36670 in between the collapsed for loops to be still considered perfectly
36671 nested. Hopefully the final version clarifies this.
36672 For now handle (multiple) {'s and empty statements. */
36673 cp_parser_parse_tentatively (parser);
36674 for (;;)
36675 {
36676 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36677 break;
36678 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36679 {
36680 cp_lexer_consume_token (parser->lexer);
36681 bracecount++;
36682 }
36683 else if (bracecount
36684 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36685 cp_lexer_consume_token (parser->lexer);
36686 else
36687 {
36688 loc = cp_lexer_peek_token (parser->lexer)->location;
36689 error_at (loc, "not enough for loops to collapse");
36690 collapse_err = true;
36691 cp_parser_abort_tentative_parse (parser);
36692 declv = NULL_TREE;
36693 break;
36694 }
36695 }
36696
36697 if (declv)
36698 {
36699 cp_parser_parse_definitely (parser);
36700 nbraces += bracecount;
36701 }
36702 }
36703
36704 if (nbraces)
36705 if_p = NULL;
36706
36707 /* Note that we saved the original contents of this flag when we entered
36708 the structured block, and so we don't need to re-save it here. */
36709 parser->in_statement = IN_OMP_FOR;
36710
36711 /* Note that the grammar doesn't call for a structured block here,
36712 though the loop as a whole is a structured block. */
36713 if (orig_declv)
36714 {
36715 body = begin_omp_structured_block ();
36716 for (i = 0; i < count; i++)
36717 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
36718 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
36719 TREE_VEC_ELT (declv, i));
36720 }
36721 else
36722 body = push_stmt_list ();
36723 cp_parser_statement (parser, NULL_TREE, false, if_p);
36724 if (orig_declv)
36725 body = finish_omp_structured_block (body);
36726 else
36727 body = pop_stmt_list (body);
36728
36729 if (declv == NULL_TREE)
36730 ret = NULL_TREE;
36731 else
36732 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
36733 incrv, body, pre_body, &orig_inits, clauses);
36734
36735 while (nbraces)
36736 {
36737 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36738 {
36739 cp_lexer_consume_token (parser->lexer);
36740 nbraces--;
36741 }
36742 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36743 cp_lexer_consume_token (parser->lexer);
36744 else
36745 {
36746 if (!collapse_err)
36747 {
36748 error_at (cp_lexer_peek_token (parser->lexer)->location,
36749 "collapsed loops not perfectly nested");
36750 }
36751 collapse_err = true;
36752 cp_parser_statement_seq_opt (parser, NULL);
36753 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
36754 break;
36755 }
36756 }
36757
36758 while (!for_block->is_empty ())
36759 {
36760 tree t = for_block->pop ();
36761 if (TREE_CODE (t) == STATEMENT_LIST)
36762 add_stmt (pop_stmt_list (t));
36763 else
36764 add_stmt (t);
36765 }
36766 release_tree_vector (for_block);
36767
36768 return ret;
36769 }
36770
36771 /* Helper function for OpenMP parsing, split clauses and call
36772 finish_omp_clauses on each of the set of clauses afterwards. */
36773
36774 static void
36775 cp_omp_split_clauses (location_t loc, enum tree_code code,
36776 omp_clause_mask mask, tree clauses, tree *cclauses)
36777 {
36778 int i;
36779 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
36780 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
36781 if (cclauses[i])
36782 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
36783 }
36784
36785 /* OpenMP 4.0:
36786 #pragma omp simd simd-clause[optseq] new-line
36787 for-loop */
36788
36789 #define OMP_SIMD_CLAUSE_MASK \
36790 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36800
36801 static tree
36802 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
36803 char *p_name, omp_clause_mask mask, tree *cclauses,
36804 bool *if_p)
36805 {
36806 tree clauses, sb, ret;
36807 unsigned int save;
36808 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36809
36810 strcat (p_name, " simd");
36811 mask |= OMP_SIMD_CLAUSE_MASK;
36812
36813 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36814 cclauses == NULL);
36815 if (cclauses)
36816 {
36817 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
36818 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
36819 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
36820 OMP_CLAUSE_ORDERED);
36821 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
36822 {
36823 error_at (OMP_CLAUSE_LOCATION (c),
36824 "%<ordered%> clause with parameter may not be specified "
36825 "on %qs construct", p_name);
36826 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
36827 }
36828 }
36829
36830 keep_next_level (true);
36831 sb = begin_omp_structured_block ();
36832 save = cp_parser_begin_omp_structured_block (parser);
36833
36834 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
36835
36836 cp_parser_end_omp_structured_block (parser, save);
36837 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36838
36839 return ret;
36840 }
36841
36842 /* OpenMP 2.5:
36843 #pragma omp for for-clause[optseq] new-line
36844 for-loop
36845
36846 OpenMP 4.0:
36847 #pragma omp for simd for-simd-clause[optseq] new-line
36848 for-loop */
36849
36850 #define OMP_FOR_CLAUSE_MASK \
36851 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
36857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
36858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
36860
36861 static tree
36862 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
36863 char *p_name, omp_clause_mask mask, tree *cclauses,
36864 bool *if_p)
36865 {
36866 tree clauses, sb, ret;
36867 unsigned int save;
36868 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36869
36870 strcat (p_name, " for");
36871 mask |= OMP_FOR_CLAUSE_MASK;
36872 /* parallel for{, simd} disallows nowait clause, but for
36873 target {teams distribute ,}parallel for{, simd} it should be accepted. */
36874 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
36875 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
36876 /* Composite distribute parallel for{, simd} disallows ordered clause. */
36877 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36878 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
36879
36880 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36881 {
36882 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36883 const char *p = IDENTIFIER_POINTER (id);
36884
36885 if (strcmp (p, "simd") == 0)
36886 {
36887 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36888 if (cclauses == NULL)
36889 cclauses = cclauses_buf;
36890
36891 cp_lexer_consume_token (parser->lexer);
36892 if (!flag_openmp) /* flag_openmp_simd */
36893 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36894 cclauses, if_p);
36895 sb = begin_omp_structured_block ();
36896 save = cp_parser_begin_omp_structured_block (parser);
36897 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36898 cclauses, if_p);
36899 cp_parser_end_omp_structured_block (parser, save);
36900 tree body = finish_omp_structured_block (sb);
36901 if (ret == NULL)
36902 return ret;
36903 ret = make_node (OMP_FOR);
36904 TREE_TYPE (ret) = void_type_node;
36905 OMP_FOR_BODY (ret) = body;
36906 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36907 SET_EXPR_LOCATION (ret, loc);
36908 add_stmt (ret);
36909 return ret;
36910 }
36911 }
36912 if (!flag_openmp) /* flag_openmp_simd */
36913 {
36914 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36915 return NULL_TREE;
36916 }
36917
36918 /* Composite distribute parallel for disallows linear clause. */
36919 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
36920 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
36921
36922 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36923 cclauses == NULL);
36924 if (cclauses)
36925 {
36926 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
36927 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36928 }
36929
36930 keep_next_level (true);
36931 sb = begin_omp_structured_block ();
36932 save = cp_parser_begin_omp_structured_block (parser);
36933
36934 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
36935
36936 cp_parser_end_omp_structured_block (parser, save);
36937 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
36938
36939 return ret;
36940 }
36941
36942 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
36943 omp_clause_mask, tree *, bool *);
36944
36945 /* OpenMP 2.5:
36946 # pragma omp master new-line
36947 structured-block */
36948
36949 static tree
36950 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
36951 char *p_name, omp_clause_mask mask, tree *cclauses,
36952 bool *if_p)
36953 {
36954 tree clauses, sb, ret;
36955 unsigned int save;
36956 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36957
36958 strcat (p_name, " master");
36959
36960 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36961 {
36962 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36963 const char *p = IDENTIFIER_POINTER (id);
36964
36965 if (strcmp (p, "taskloop") == 0)
36966 {
36967 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36968 if (cclauses == NULL)
36969 cclauses = cclauses_buf;
36970
36971 cp_lexer_consume_token (parser->lexer);
36972 if (!flag_openmp) /* flag_openmp_simd */
36973 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36974 cclauses, if_p);
36975 sb = begin_omp_structured_block ();
36976 save = cp_parser_begin_omp_structured_block (parser);
36977 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
36978 cclauses, if_p);
36979 cp_parser_end_omp_structured_block (parser, save);
36980 tree body = finish_omp_structured_block (sb);
36981 if (ret == NULL)
36982 return ret;
36983 return c_finish_omp_master (loc, body);
36984 }
36985 }
36986 if (!flag_openmp) /* flag_openmp_simd */
36987 {
36988 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36989 return NULL_TREE;
36990 }
36991
36992 if (cclauses)
36993 {
36994 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36995 false);
36996 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
36997 }
36998 else
36999 cp_parser_require_pragma_eol (parser, pragma_tok);
37000
37001 return c_finish_omp_master (loc,
37002 cp_parser_omp_structured_block (parser, if_p));
37003 }
37004
37005 /* OpenMP 2.5:
37006 # pragma omp ordered new-line
37007 structured-block
37008
37009 OpenMP 4.5:
37010 # pragma omp ordered ordered-clauses new-line
37011 structured-block */
37012
37013 #define OMP_ORDERED_CLAUSE_MASK \
37014 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37016
37017 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37018 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37019
37020 static bool
37021 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37022 enum pragma_context context, bool *if_p)
37023 {
37024 location_t loc = pragma_tok->location;
37025
37026 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37027 {
37028 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37029 const char *p = IDENTIFIER_POINTER (id);
37030
37031 if (strcmp (p, "depend") == 0)
37032 {
37033 if (!flag_openmp) /* flag_openmp_simd */
37034 {
37035 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37036 return false;
37037 }
37038 if (context == pragma_stmt)
37039 {
37040 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37041 "%<depend%> clause may only be used in compound "
37042 "statements");
37043 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37044 return false;
37045 }
37046 tree clauses
37047 = cp_parser_omp_all_clauses (parser,
37048 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37049 "#pragma omp ordered", pragma_tok);
37050 c_finish_omp_ordered (loc, clauses, NULL_TREE);
37051 return false;
37052 }
37053 }
37054
37055 tree clauses
37056 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37057 "#pragma omp ordered", pragma_tok);
37058
37059 if (!flag_openmp /* flag_openmp_simd */
37060 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37061 return false;
37062
37063 c_finish_omp_ordered (loc, clauses,
37064 cp_parser_omp_structured_block (parser, if_p));
37065 return true;
37066 }
37067
37068 /* OpenMP 2.5:
37069
37070 section-scope:
37071 { section-sequence }
37072
37073 section-sequence:
37074 section-directive[opt] structured-block
37075 section-sequence section-directive structured-block */
37076
37077 static tree
37078 cp_parser_omp_sections_scope (cp_parser *parser)
37079 {
37080 tree stmt, substmt;
37081 bool error_suppress = false;
37082 cp_token *tok;
37083
37084 matching_braces braces;
37085 if (!braces.require_open (parser))
37086 return NULL_TREE;
37087
37088 stmt = push_stmt_list ();
37089
37090 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37091 != PRAGMA_OMP_SECTION)
37092 {
37093 substmt = cp_parser_omp_structured_block (parser, NULL);
37094 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37095 add_stmt (substmt);
37096 }
37097
37098 while (1)
37099 {
37100 tok = cp_lexer_peek_token (parser->lexer);
37101 if (tok->type == CPP_CLOSE_BRACE)
37102 break;
37103 if (tok->type == CPP_EOF)
37104 break;
37105
37106 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37107 {
37108 cp_lexer_consume_token (parser->lexer);
37109 cp_parser_require_pragma_eol (parser, tok);
37110 error_suppress = false;
37111 }
37112 else if (!error_suppress)
37113 {
37114 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37115 error_suppress = true;
37116 }
37117
37118 substmt = cp_parser_omp_structured_block (parser, NULL);
37119 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37120 add_stmt (substmt);
37121 }
37122 braces.require_close (parser);
37123
37124 substmt = pop_stmt_list (stmt);
37125
37126 stmt = make_node (OMP_SECTIONS);
37127 TREE_TYPE (stmt) = void_type_node;
37128 OMP_SECTIONS_BODY (stmt) = substmt;
37129
37130 add_stmt (stmt);
37131 return stmt;
37132 }
37133
37134 /* OpenMP 2.5:
37135 # pragma omp sections sections-clause[optseq] newline
37136 sections-scope */
37137
37138 #define OMP_SECTIONS_CLAUSE_MASK \
37139 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37144
37145 static tree
37146 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37147 char *p_name, omp_clause_mask mask, tree *cclauses)
37148 {
37149 tree clauses, ret;
37150 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37151
37152 strcat (p_name, " sections");
37153 mask |= OMP_SECTIONS_CLAUSE_MASK;
37154 if (cclauses)
37155 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37156
37157 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37158 cclauses == NULL);
37159 if (cclauses)
37160 {
37161 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37162 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37163 }
37164
37165 ret = cp_parser_omp_sections_scope (parser);
37166 if (ret)
37167 OMP_SECTIONS_CLAUSES (ret) = clauses;
37168
37169 return ret;
37170 }
37171
37172 /* OpenMP 2.5:
37173 # pragma omp parallel parallel-clause[optseq] new-line
37174 structured-block
37175 # pragma omp parallel for parallel-for-clause[optseq] new-line
37176 structured-block
37177 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37178 structured-block
37179
37180 OpenMP 4.0:
37181 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37182 structured-block */
37183
37184 #define OMP_PARALLEL_CLAUSE_MASK \
37185 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37194
37195 static tree
37196 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37197 char *p_name, omp_clause_mask mask, tree *cclauses,
37198 bool *if_p)
37199 {
37200 tree stmt, clauses, block;
37201 unsigned int save;
37202 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37203
37204 strcat (p_name, " parallel");
37205 mask |= OMP_PARALLEL_CLAUSE_MASK;
37206 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37207 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37208 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37209 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37210
37211 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37212 {
37213 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37214 if (cclauses == NULL)
37215 cclauses = cclauses_buf;
37216
37217 cp_lexer_consume_token (parser->lexer);
37218 if (!flag_openmp) /* flag_openmp_simd */
37219 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37220 if_p);
37221 block = begin_omp_parallel ();
37222 save = cp_parser_begin_omp_structured_block (parser);
37223 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37224 if_p);
37225 cp_parser_end_omp_structured_block (parser, save);
37226 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37227 block);
37228 if (ret == NULL_TREE)
37229 return ret;
37230 OMP_PARALLEL_COMBINED (stmt) = 1;
37231 return stmt;
37232 }
37233 /* When combined with distribute, parallel has to be followed by for.
37234 #pragma omp target parallel is allowed though. */
37235 else if (cclauses
37236 && (mask & (OMP_CLAUSE_MASK_1
37237 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37238 {
37239 error_at (loc, "expected %<for%> after %qs", p_name);
37240 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37241 return NULL_TREE;
37242 }
37243 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37244 {
37245 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37246 const char *p = IDENTIFIER_POINTER (id);
37247 if (strcmp (p, "master") == 0)
37248 {
37249 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37250 cclauses = cclauses_buf;
37251
37252 cp_lexer_consume_token (parser->lexer);
37253 block = begin_omp_parallel ();
37254 save = cp_parser_begin_omp_structured_block (parser);
37255 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37256 cclauses, if_p);
37257 cp_parser_end_omp_structured_block (parser, save);
37258 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37259 block);
37260 OMP_PARALLEL_COMBINED (stmt) = 1;
37261 if (ret == NULL_TREE)
37262 return ret;
37263 return stmt;
37264 }
37265 else if (!flag_openmp) /* flag_openmp_simd */
37266 {
37267 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37268 return NULL_TREE;
37269 }
37270 else if (strcmp (p, "sections") == 0)
37271 {
37272 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37273 cclauses = cclauses_buf;
37274
37275 cp_lexer_consume_token (parser->lexer);
37276 block = begin_omp_parallel ();
37277 save = cp_parser_begin_omp_structured_block (parser);
37278 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37279 cp_parser_end_omp_structured_block (parser, save);
37280 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37281 block);
37282 OMP_PARALLEL_COMBINED (stmt) = 1;
37283 return stmt;
37284 }
37285 }
37286 else if (!flag_openmp) /* flag_openmp_simd */
37287 {
37288 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37289 return NULL_TREE;
37290 }
37291
37292 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37293 cclauses == NULL);
37294 if (cclauses)
37295 {
37296 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37297 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37298 }
37299
37300 block = begin_omp_parallel ();
37301 save = cp_parser_begin_omp_structured_block (parser);
37302 cp_parser_statement (parser, NULL_TREE, false, if_p);
37303 cp_parser_end_omp_structured_block (parser, save);
37304 stmt = finish_omp_parallel (clauses, block);
37305 return stmt;
37306 }
37307
37308 /* OpenMP 2.5:
37309 # pragma omp single single-clause[optseq] new-line
37310 structured-block */
37311
37312 #define OMP_SINGLE_CLAUSE_MASK \
37313 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37317
37318 static tree
37319 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37320 {
37321 tree stmt = make_node (OMP_SINGLE);
37322 TREE_TYPE (stmt) = void_type_node;
37323 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37324
37325 OMP_SINGLE_CLAUSES (stmt)
37326 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37327 "#pragma omp single", pragma_tok);
37328 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37329
37330 return add_stmt (stmt);
37331 }
37332
37333 /* OpenMP 3.0:
37334 # pragma omp task task-clause[optseq] new-line
37335 structured-block */
37336
37337 #define OMP_TASK_CLAUSE_MASK \
37338 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37349
37350 static tree
37351 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37352 {
37353 tree clauses, block;
37354 unsigned int save;
37355
37356 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37357 "#pragma omp task", pragma_tok);
37358 block = begin_omp_task ();
37359 save = cp_parser_begin_omp_structured_block (parser);
37360 cp_parser_statement (parser, NULL_TREE, false, if_p);
37361 cp_parser_end_omp_structured_block (parser, save);
37362 return finish_omp_task (clauses, block);
37363 }
37364
37365 /* OpenMP 3.0:
37366 # pragma omp taskwait new-line
37367
37368 OpenMP 5.0:
37369 # pragma omp taskwait taskwait-clause[opt] new-line */
37370
37371 #define OMP_TASKWAIT_CLAUSE_MASK \
37372 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37373
37374 static void
37375 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37376 {
37377 tree clauses
37378 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37379 "#pragma omp taskwait", pragma_tok);
37380
37381 if (clauses)
37382 {
37383 tree stmt = make_node (OMP_TASK);
37384 TREE_TYPE (stmt) = void_node;
37385 OMP_TASK_CLAUSES (stmt) = clauses;
37386 OMP_TASK_BODY (stmt) = NULL_TREE;
37387 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37388 add_stmt (stmt);
37389 }
37390 else
37391 finish_omp_taskwait ();
37392 }
37393
37394 /* OpenMP 3.1:
37395 # pragma omp taskyield new-line */
37396
37397 static void
37398 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37399 {
37400 cp_parser_require_pragma_eol (parser, pragma_tok);
37401 finish_omp_taskyield ();
37402 }
37403
37404 /* OpenMP 4.0:
37405 # pragma omp taskgroup new-line
37406 structured-block
37407
37408 OpenMP 5.0:
37409 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37410
37411 #define OMP_TASKGROUP_CLAUSE_MASK \
37412 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37413
37414 static tree
37415 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37416 {
37417 tree clauses
37418 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37419 "#pragma omp taskgroup", pragma_tok);
37420 return c_finish_omp_taskgroup (input_location,
37421 cp_parser_omp_structured_block (parser,
37422 if_p),
37423 clauses);
37424 }
37425
37426
37427 /* OpenMP 2.5:
37428 # pragma omp threadprivate (variable-list) */
37429
37430 static void
37431 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37432 {
37433 tree vars;
37434
37435 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37436 cp_parser_require_pragma_eol (parser, pragma_tok);
37437
37438 finish_omp_threadprivate (vars);
37439 }
37440
37441 /* OpenMP 4.0:
37442 # pragma omp cancel cancel-clause[optseq] new-line */
37443
37444 #define OMP_CANCEL_CLAUSE_MASK \
37445 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37450
37451 static void
37452 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37453 {
37454 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37455 "#pragma omp cancel", pragma_tok);
37456 finish_omp_cancel (clauses);
37457 }
37458
37459 /* OpenMP 4.0:
37460 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37461
37462 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37467
37468 static void
37469 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37470 enum pragma_context context)
37471 {
37472 tree clauses;
37473 bool point_seen = false;
37474
37475 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37476 {
37477 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37478 const char *p = IDENTIFIER_POINTER (id);
37479
37480 if (strcmp (p, "point") == 0)
37481 {
37482 cp_lexer_consume_token (parser->lexer);
37483 point_seen = true;
37484 }
37485 }
37486 if (!point_seen)
37487 {
37488 cp_parser_error (parser, "expected %<point%>");
37489 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37490 return;
37491 }
37492
37493 if (context != pragma_compound)
37494 {
37495 if (context == pragma_stmt)
37496 error_at (pragma_tok->location,
37497 "%<#pragma %s%> may only be used in compound statements",
37498 "omp cancellation point");
37499 else
37500 cp_parser_error (parser, "expected declaration specifiers");
37501 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37502 return;
37503 }
37504
37505 clauses = cp_parser_omp_all_clauses (parser,
37506 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37507 "#pragma omp cancellation point",
37508 pragma_tok);
37509 finish_omp_cancellation_point (clauses);
37510 }
37511
37512 /* OpenMP 4.0:
37513 #pragma omp distribute distribute-clause[optseq] new-line
37514 for-loop */
37515
37516 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37517 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37522
37523 static tree
37524 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37525 char *p_name, omp_clause_mask mask, tree *cclauses,
37526 bool *if_p)
37527 {
37528 tree clauses, sb, ret;
37529 unsigned int save;
37530 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37531
37532 strcat (p_name, " distribute");
37533 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37534
37535 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37536 {
37537 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37538 const char *p = IDENTIFIER_POINTER (id);
37539 bool simd = false;
37540 bool parallel = false;
37541
37542 if (strcmp (p, "simd") == 0)
37543 simd = true;
37544 else
37545 parallel = strcmp (p, "parallel") == 0;
37546 if (parallel || simd)
37547 {
37548 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37549 if (cclauses == NULL)
37550 cclauses = cclauses_buf;
37551 cp_lexer_consume_token (parser->lexer);
37552 if (!flag_openmp) /* flag_openmp_simd */
37553 {
37554 if (simd)
37555 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37556 cclauses, if_p);
37557 else
37558 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37559 cclauses, if_p);
37560 }
37561 sb = begin_omp_structured_block ();
37562 save = cp_parser_begin_omp_structured_block (parser);
37563 if (simd)
37564 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37565 cclauses, if_p);
37566 else
37567 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37568 cclauses, if_p);
37569 cp_parser_end_omp_structured_block (parser, save);
37570 tree body = finish_omp_structured_block (sb);
37571 if (ret == NULL)
37572 return ret;
37573 ret = make_node (OMP_DISTRIBUTE);
37574 TREE_TYPE (ret) = void_type_node;
37575 OMP_FOR_BODY (ret) = body;
37576 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37577 SET_EXPR_LOCATION (ret, loc);
37578 add_stmt (ret);
37579 return ret;
37580 }
37581 }
37582 if (!flag_openmp) /* flag_openmp_simd */
37583 {
37584 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37585 return NULL_TREE;
37586 }
37587
37588 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37589 cclauses == NULL);
37590 if (cclauses)
37591 {
37592 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37593 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37594 }
37595
37596 keep_next_level (true);
37597 sb = begin_omp_structured_block ();
37598 save = cp_parser_begin_omp_structured_block (parser);
37599
37600 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37601
37602 cp_parser_end_omp_structured_block (parser, save);
37603 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37604
37605 return ret;
37606 }
37607
37608 /* OpenMP 4.0:
37609 # pragma omp teams teams-clause[optseq] new-line
37610 structured-block */
37611
37612 #define OMP_TEAMS_CLAUSE_MASK \
37613 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37620
37621 static tree
37622 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37623 char *p_name, omp_clause_mask mask, tree *cclauses,
37624 bool *if_p)
37625 {
37626 tree clauses, sb, ret;
37627 unsigned int save;
37628 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37629
37630 strcat (p_name, " teams");
37631 mask |= OMP_TEAMS_CLAUSE_MASK;
37632
37633 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37634 {
37635 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37636 const char *p = IDENTIFIER_POINTER (id);
37637 if (strcmp (p, "distribute") == 0)
37638 {
37639 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37640 if (cclauses == NULL)
37641 cclauses = cclauses_buf;
37642
37643 cp_lexer_consume_token (parser->lexer);
37644 if (!flag_openmp) /* flag_openmp_simd */
37645 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37646 cclauses, if_p);
37647 keep_next_level (true);
37648 sb = begin_omp_structured_block ();
37649 save = cp_parser_begin_omp_structured_block (parser);
37650 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37651 cclauses, if_p);
37652 cp_parser_end_omp_structured_block (parser, save);
37653 tree body = finish_omp_structured_block (sb);
37654 if (ret == NULL)
37655 return ret;
37656 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37657 ret = make_node (OMP_TEAMS);
37658 TREE_TYPE (ret) = void_type_node;
37659 OMP_TEAMS_CLAUSES (ret) = clauses;
37660 OMP_TEAMS_BODY (ret) = body;
37661 OMP_TEAMS_COMBINED (ret) = 1;
37662 SET_EXPR_LOCATION (ret, loc);
37663 return add_stmt (ret);
37664 }
37665 }
37666 if (!flag_openmp) /* flag_openmp_simd */
37667 {
37668 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37669 return NULL_TREE;
37670 }
37671
37672 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37673 cclauses == NULL);
37674 if (cclauses)
37675 {
37676 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
37677 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37678 }
37679
37680 tree stmt = make_node (OMP_TEAMS);
37681 TREE_TYPE (stmt) = void_type_node;
37682 OMP_TEAMS_CLAUSES (stmt) = clauses;
37683 keep_next_level (true);
37684 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37685 SET_EXPR_LOCATION (stmt, loc);
37686
37687 return add_stmt (stmt);
37688 }
37689
37690 /* OpenMP 4.0:
37691 # pragma omp target data target-data-clause[optseq] new-line
37692 structured-block */
37693
37694 #define OMP_TARGET_DATA_CLAUSE_MASK \
37695 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37699
37700 static tree
37701 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37702 {
37703 tree clauses
37704 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
37705 "#pragma omp target data", pragma_tok);
37706 int map_seen = 0;
37707 for (tree *pc = &clauses; *pc;)
37708 {
37709 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37710 switch (OMP_CLAUSE_MAP_KIND (*pc))
37711 {
37712 case GOMP_MAP_TO:
37713 case GOMP_MAP_ALWAYS_TO:
37714 case GOMP_MAP_FROM:
37715 case GOMP_MAP_ALWAYS_FROM:
37716 case GOMP_MAP_TOFROM:
37717 case GOMP_MAP_ALWAYS_TOFROM:
37718 case GOMP_MAP_ALLOC:
37719 map_seen = 3;
37720 break;
37721 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37722 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37723 case GOMP_MAP_ALWAYS_POINTER:
37724 break;
37725 default:
37726 map_seen |= 1;
37727 error_at (OMP_CLAUSE_LOCATION (*pc),
37728 "%<#pragma omp target data%> with map-type other "
37729 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37730 "on %<map%> clause");
37731 *pc = OMP_CLAUSE_CHAIN (*pc);
37732 continue;
37733 }
37734 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
37735 map_seen = 3;
37736 pc = &OMP_CLAUSE_CHAIN (*pc);
37737 }
37738
37739 if (map_seen != 3)
37740 {
37741 if (map_seen == 0)
37742 error_at (pragma_tok->location,
37743 "%<#pragma omp target data%> must contain at least "
37744 "one %<map%> or %<use_device_ptr%> clause");
37745 return NULL_TREE;
37746 }
37747
37748 tree stmt = make_node (OMP_TARGET_DATA);
37749 TREE_TYPE (stmt) = void_type_node;
37750 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
37751
37752 keep_next_level (true);
37753 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37754
37755 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37756 return add_stmt (stmt);
37757 }
37758
37759 /* OpenMP 4.5:
37760 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37761 structured-block */
37762
37763 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37764 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37769
37770 static tree
37771 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
37772 enum pragma_context context)
37773 {
37774 bool data_seen = false;
37775 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37776 {
37777 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37778 const char *p = IDENTIFIER_POINTER (id);
37779
37780 if (strcmp (p, "data") == 0)
37781 {
37782 cp_lexer_consume_token (parser->lexer);
37783 data_seen = true;
37784 }
37785 }
37786 if (!data_seen)
37787 {
37788 cp_parser_error (parser, "expected %<data%>");
37789 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37790 return NULL_TREE;
37791 }
37792
37793 if (context == pragma_stmt)
37794 {
37795 error_at (pragma_tok->location,
37796 "%<#pragma %s%> may only be used in compound statements",
37797 "omp target enter data");
37798 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37799 return NULL_TREE;
37800 }
37801
37802 tree clauses
37803 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
37804 "#pragma omp target enter data", pragma_tok);
37805 int map_seen = 0;
37806 for (tree *pc = &clauses; *pc;)
37807 {
37808 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37809 switch (OMP_CLAUSE_MAP_KIND (*pc))
37810 {
37811 case GOMP_MAP_TO:
37812 case GOMP_MAP_ALWAYS_TO:
37813 case GOMP_MAP_ALLOC:
37814 map_seen = 3;
37815 break;
37816 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37817 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37818 case GOMP_MAP_ALWAYS_POINTER:
37819 break;
37820 default:
37821 map_seen |= 1;
37822 error_at (OMP_CLAUSE_LOCATION (*pc),
37823 "%<#pragma omp target enter data%> with map-type other "
37824 "than %<to%> or %<alloc%> on %<map%> clause");
37825 *pc = OMP_CLAUSE_CHAIN (*pc);
37826 continue;
37827 }
37828 pc = &OMP_CLAUSE_CHAIN (*pc);
37829 }
37830
37831 if (map_seen != 3)
37832 {
37833 if (map_seen == 0)
37834 error_at (pragma_tok->location,
37835 "%<#pragma omp target enter data%> must contain at least "
37836 "one %<map%> clause");
37837 return NULL_TREE;
37838 }
37839
37840 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
37841 TREE_TYPE (stmt) = void_type_node;
37842 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
37843 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37844 return add_stmt (stmt);
37845 }
37846
37847 /* OpenMP 4.5:
37848 # pragma omp target exit data target-enter-data-clause[optseq] new-line
37849 structured-block */
37850
37851 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
37852 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37857
37858 static tree
37859 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
37860 enum pragma_context context)
37861 {
37862 bool data_seen = false;
37863 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37864 {
37865 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37866 const char *p = IDENTIFIER_POINTER (id);
37867
37868 if (strcmp (p, "data") == 0)
37869 {
37870 cp_lexer_consume_token (parser->lexer);
37871 data_seen = true;
37872 }
37873 }
37874 if (!data_seen)
37875 {
37876 cp_parser_error (parser, "expected %<data%>");
37877 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37878 return NULL_TREE;
37879 }
37880
37881 if (context == pragma_stmt)
37882 {
37883 error_at (pragma_tok->location,
37884 "%<#pragma %s%> may only be used in compound statements",
37885 "omp target exit data");
37886 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37887 return NULL_TREE;
37888 }
37889
37890 tree clauses
37891 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
37892 "#pragma omp target exit data", pragma_tok);
37893 int map_seen = 0;
37894 for (tree *pc = &clauses; *pc;)
37895 {
37896 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37897 switch (OMP_CLAUSE_MAP_KIND (*pc))
37898 {
37899 case GOMP_MAP_FROM:
37900 case GOMP_MAP_ALWAYS_FROM:
37901 case GOMP_MAP_RELEASE:
37902 case GOMP_MAP_DELETE:
37903 map_seen = 3;
37904 break;
37905 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37906 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37907 case GOMP_MAP_ALWAYS_POINTER:
37908 break;
37909 default:
37910 map_seen |= 1;
37911 error_at (OMP_CLAUSE_LOCATION (*pc),
37912 "%<#pragma omp target exit data%> with map-type other "
37913 "than %<from%>, %<release%> or %<delete%> on %<map%>"
37914 " clause");
37915 *pc = OMP_CLAUSE_CHAIN (*pc);
37916 continue;
37917 }
37918 pc = &OMP_CLAUSE_CHAIN (*pc);
37919 }
37920
37921 if (map_seen != 3)
37922 {
37923 if (map_seen == 0)
37924 error_at (pragma_tok->location,
37925 "%<#pragma omp target exit data%> must contain at least "
37926 "one %<map%> clause");
37927 return NULL_TREE;
37928 }
37929
37930 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
37931 TREE_TYPE (stmt) = void_type_node;
37932 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
37933 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37934 return add_stmt (stmt);
37935 }
37936
37937 /* OpenMP 4.0:
37938 # pragma omp target update target-update-clause[optseq] new-line */
37939
37940 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
37941 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
37942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37947
37948 static bool
37949 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
37950 enum pragma_context context)
37951 {
37952 if (context == pragma_stmt)
37953 {
37954 error_at (pragma_tok->location,
37955 "%<#pragma %s%> may only be used in compound statements",
37956 "omp target update");
37957 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37958 return false;
37959 }
37960
37961 tree clauses
37962 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
37963 "#pragma omp target update", pragma_tok);
37964 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
37965 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
37966 {
37967 error_at (pragma_tok->location,
37968 "%<#pragma omp target update%> must contain at least one "
37969 "%<from%> or %<to%> clauses");
37970 return false;
37971 }
37972
37973 tree stmt = make_node (OMP_TARGET_UPDATE);
37974 TREE_TYPE (stmt) = void_type_node;
37975 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
37976 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37977 add_stmt (stmt);
37978 return false;
37979 }
37980
37981 /* OpenMP 4.0:
37982 # pragma omp target target-clause[optseq] new-line
37983 structured-block */
37984
37985 #define OMP_TARGET_CLAUSE_MASK \
37986 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
37994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
37995
37996 static bool
37997 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
37998 enum pragma_context context, bool *if_p)
37999 {
38000 tree *pc = NULL, stmt;
38001
38002 if (flag_openmp)
38003 omp_requires_mask
38004 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
38005
38006 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38007 {
38008 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38009 const char *p = IDENTIFIER_POINTER (id);
38010 enum tree_code ccode = ERROR_MARK;
38011
38012 if (strcmp (p, "teams") == 0)
38013 ccode = OMP_TEAMS;
38014 else if (strcmp (p, "parallel") == 0)
38015 ccode = OMP_PARALLEL;
38016 else if (strcmp (p, "simd") == 0)
38017 ccode = OMP_SIMD;
38018 if (ccode != ERROR_MARK)
38019 {
38020 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38021 char p_name[sizeof ("#pragma omp target teams distribute "
38022 "parallel for simd")];
38023
38024 cp_lexer_consume_token (parser->lexer);
38025 strcpy (p_name, "#pragma omp target");
38026 if (!flag_openmp) /* flag_openmp_simd */
38027 {
38028 tree stmt;
38029 switch (ccode)
38030 {
38031 case OMP_TEAMS:
38032 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38033 OMP_TARGET_CLAUSE_MASK,
38034 cclauses, if_p);
38035 break;
38036 case OMP_PARALLEL:
38037 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38038 OMP_TARGET_CLAUSE_MASK,
38039 cclauses, if_p);
38040 break;
38041 case OMP_SIMD:
38042 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38043 OMP_TARGET_CLAUSE_MASK,
38044 cclauses, if_p);
38045 break;
38046 default:
38047 gcc_unreachable ();
38048 }
38049 return stmt != NULL_TREE;
38050 }
38051 keep_next_level (true);
38052 tree sb = begin_omp_structured_block (), ret;
38053 unsigned save = cp_parser_begin_omp_structured_block (parser);
38054 switch (ccode)
38055 {
38056 case OMP_TEAMS:
38057 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38058 OMP_TARGET_CLAUSE_MASK, cclauses,
38059 if_p);
38060 break;
38061 case OMP_PARALLEL:
38062 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38063 OMP_TARGET_CLAUSE_MASK, cclauses,
38064 if_p);
38065 break;
38066 case OMP_SIMD:
38067 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38068 OMP_TARGET_CLAUSE_MASK, cclauses,
38069 if_p);
38070 break;
38071 default:
38072 gcc_unreachable ();
38073 }
38074 cp_parser_end_omp_structured_block (parser, save);
38075 tree body = finish_omp_structured_block (sb);
38076 if (ret == NULL_TREE)
38077 return false;
38078 if (ccode == OMP_TEAMS && !processing_template_decl)
38079 {
38080 /* For combined target teams, ensure the num_teams and
38081 thread_limit clause expressions are evaluated on the host,
38082 before entering the target construct. */
38083 tree c;
38084 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38085 c; c = OMP_CLAUSE_CHAIN (c))
38086 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38087 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38088 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38089 {
38090 tree expr = OMP_CLAUSE_OPERAND (c, 0);
38091 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38092 if (expr == error_mark_node)
38093 continue;
38094 tree tmp = TARGET_EXPR_SLOT (expr);
38095 add_stmt (expr);
38096 OMP_CLAUSE_OPERAND (c, 0) = expr;
38097 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38098 OMP_CLAUSE_FIRSTPRIVATE);
38099 OMP_CLAUSE_DECL (tc) = tmp;
38100 OMP_CLAUSE_CHAIN (tc)
38101 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38102 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38103 }
38104 }
38105 tree stmt = make_node (OMP_TARGET);
38106 TREE_TYPE (stmt) = void_type_node;
38107 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38108 OMP_TARGET_BODY (stmt) = body;
38109 OMP_TARGET_COMBINED (stmt) = 1;
38110 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38111 add_stmt (stmt);
38112 pc = &OMP_TARGET_CLAUSES (stmt);
38113 goto check_clauses;
38114 }
38115 else if (!flag_openmp) /* flag_openmp_simd */
38116 {
38117 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38118 return false;
38119 }
38120 else if (strcmp (p, "data") == 0)
38121 {
38122 cp_lexer_consume_token (parser->lexer);
38123 cp_parser_omp_target_data (parser, pragma_tok, if_p);
38124 return true;
38125 }
38126 else if (strcmp (p, "enter") == 0)
38127 {
38128 cp_lexer_consume_token (parser->lexer);
38129 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38130 return false;
38131 }
38132 else if (strcmp (p, "exit") == 0)
38133 {
38134 cp_lexer_consume_token (parser->lexer);
38135 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38136 return false;
38137 }
38138 else if (strcmp (p, "update") == 0)
38139 {
38140 cp_lexer_consume_token (parser->lexer);
38141 return cp_parser_omp_target_update (parser, pragma_tok, context);
38142 }
38143 }
38144 if (!flag_openmp) /* flag_openmp_simd */
38145 {
38146 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38147 return false;
38148 }
38149
38150 stmt = make_node (OMP_TARGET);
38151 TREE_TYPE (stmt) = void_type_node;
38152
38153 OMP_TARGET_CLAUSES (stmt)
38154 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38155 "#pragma omp target", pragma_tok);
38156 pc = &OMP_TARGET_CLAUSES (stmt);
38157 keep_next_level (true);
38158 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38159
38160 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38161 add_stmt (stmt);
38162
38163 check_clauses:
38164 while (*pc)
38165 {
38166 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38167 switch (OMP_CLAUSE_MAP_KIND (*pc))
38168 {
38169 case GOMP_MAP_TO:
38170 case GOMP_MAP_ALWAYS_TO:
38171 case GOMP_MAP_FROM:
38172 case GOMP_MAP_ALWAYS_FROM:
38173 case GOMP_MAP_TOFROM:
38174 case GOMP_MAP_ALWAYS_TOFROM:
38175 case GOMP_MAP_ALLOC:
38176 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38177 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38178 case GOMP_MAP_ALWAYS_POINTER:
38179 break;
38180 default:
38181 error_at (OMP_CLAUSE_LOCATION (*pc),
38182 "%<#pragma omp target%> with map-type other "
38183 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38184 "on %<map%> clause");
38185 *pc = OMP_CLAUSE_CHAIN (*pc);
38186 continue;
38187 }
38188 pc = &OMP_CLAUSE_CHAIN (*pc);
38189 }
38190 return true;
38191 }
38192
38193 /* OpenACC 2.0:
38194 # pragma acc cache (variable-list) new-line
38195 */
38196
38197 static tree
38198 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38199 {
38200 tree stmt, clauses;
38201
38202 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38203 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38204
38205 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38206
38207 stmt = make_node (OACC_CACHE);
38208 TREE_TYPE (stmt) = void_type_node;
38209 OACC_CACHE_CLAUSES (stmt) = clauses;
38210 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38211 add_stmt (stmt);
38212
38213 return stmt;
38214 }
38215
38216 /* OpenACC 2.0:
38217 # pragma acc data oacc-data-clause[optseq] new-line
38218 structured-block */
38219
38220 #define OACC_DATA_CLAUSE_MASK \
38221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38228
38229 static tree
38230 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38231 {
38232 tree stmt, clauses, block;
38233 unsigned int save;
38234
38235 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38236 "#pragma acc data", pragma_tok);
38237
38238 block = begin_omp_parallel ();
38239 save = cp_parser_begin_omp_structured_block (parser);
38240 cp_parser_statement (parser, NULL_TREE, false, if_p);
38241 cp_parser_end_omp_structured_block (parser, save);
38242 stmt = finish_oacc_data (clauses, block);
38243 return stmt;
38244 }
38245
38246 /* OpenACC 2.0:
38247 # pragma acc host_data <clauses> new-line
38248 structured-block */
38249
38250 #define OACC_HOST_DATA_CLAUSE_MASK \
38251 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38252
38253 static tree
38254 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38255 {
38256 tree stmt, clauses, block;
38257 unsigned int save;
38258
38259 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38260 "#pragma acc host_data", pragma_tok);
38261
38262 block = begin_omp_parallel ();
38263 save = cp_parser_begin_omp_structured_block (parser);
38264 cp_parser_statement (parser, NULL_TREE, false, if_p);
38265 cp_parser_end_omp_structured_block (parser, save);
38266 stmt = finish_oacc_host_data (clauses, block);
38267 return stmt;
38268 }
38269
38270 /* OpenACC 2.0:
38271 # pragma acc declare oacc-data-clause[optseq] new-line
38272 */
38273
38274 #define OACC_DECLARE_CLAUSE_MASK \
38275 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38283
38284 static tree
38285 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38286 {
38287 tree clauses, stmt;
38288 bool error = false;
38289
38290 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38291 "#pragma acc declare", pragma_tok, true);
38292
38293
38294 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38295 {
38296 error_at (pragma_tok->location,
38297 "no valid clauses specified in %<#pragma acc declare%>");
38298 return NULL_TREE;
38299 }
38300
38301 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38302 {
38303 location_t loc = OMP_CLAUSE_LOCATION (t);
38304 tree decl = OMP_CLAUSE_DECL (t);
38305 if (!DECL_P (decl))
38306 {
38307 error_at (loc, "array section in %<#pragma acc declare%>");
38308 error = true;
38309 continue;
38310 }
38311 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38312 switch (OMP_CLAUSE_MAP_KIND (t))
38313 {
38314 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38315 case GOMP_MAP_ALLOC:
38316 case GOMP_MAP_TO:
38317 case GOMP_MAP_FORCE_DEVICEPTR:
38318 case GOMP_MAP_DEVICE_RESIDENT:
38319 break;
38320
38321 case GOMP_MAP_LINK:
38322 if (!global_bindings_p ()
38323 && (TREE_STATIC (decl)
38324 || !DECL_EXTERNAL (decl)))
38325 {
38326 error_at (loc,
38327 "%qD must be a global variable in "
38328 "%<#pragma acc declare link%>",
38329 decl);
38330 error = true;
38331 continue;
38332 }
38333 break;
38334
38335 default:
38336 if (global_bindings_p ())
38337 {
38338 error_at (loc, "invalid OpenACC clause at file scope");
38339 error = true;
38340 continue;
38341 }
38342 if (DECL_EXTERNAL (decl))
38343 {
38344 error_at (loc,
38345 "invalid use of %<extern%> variable %qD "
38346 "in %<#pragma acc declare%>", decl);
38347 error = true;
38348 continue;
38349 }
38350 else if (TREE_PUBLIC (decl))
38351 {
38352 error_at (loc,
38353 "invalid use of %<global%> variable %qD "
38354 "in %<#pragma acc declare%>", decl);
38355 error = true;
38356 continue;
38357 }
38358 break;
38359 }
38360
38361 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38362 || lookup_attribute ("omp declare target link",
38363 DECL_ATTRIBUTES (decl)))
38364 {
38365 error_at (loc, "variable %qD used more than once with "
38366 "%<#pragma acc declare%>", decl);
38367 error = true;
38368 continue;
38369 }
38370
38371 if (!error)
38372 {
38373 tree id;
38374
38375 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38376 id = get_identifier ("omp declare target link");
38377 else
38378 id = get_identifier ("omp declare target");
38379
38380 DECL_ATTRIBUTES (decl)
38381 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38382 if (global_bindings_p ())
38383 {
38384 symtab_node *node = symtab_node::get (decl);
38385 if (node != NULL)
38386 {
38387 node->offloadable = 1;
38388 if (ENABLE_OFFLOADING)
38389 {
38390 g->have_offload = true;
38391 if (is_a <varpool_node *> (node))
38392 vec_safe_push (offload_vars, decl);
38393 }
38394 }
38395 }
38396 }
38397 }
38398
38399 if (error || global_bindings_p ())
38400 return NULL_TREE;
38401
38402 stmt = make_node (OACC_DECLARE);
38403 TREE_TYPE (stmt) = void_type_node;
38404 OACC_DECLARE_CLAUSES (stmt) = clauses;
38405 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38406
38407 add_stmt (stmt);
38408
38409 return NULL_TREE;
38410 }
38411
38412 /* OpenACC 2.0:
38413 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38414
38415 or
38416
38417 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38418
38419 LOC is the location of the #pragma token.
38420 */
38421
38422 #define OACC_ENTER_DATA_CLAUSE_MASK \
38423 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38427 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38428
38429 #define OACC_EXIT_DATA_CLAUSE_MASK \
38430 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38436
38437 static tree
38438 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38439 bool enter)
38440 {
38441 location_t loc = pragma_tok->location;
38442 tree stmt, clauses;
38443 const char *p = "";
38444
38445 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38446 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38447
38448 if (strcmp (p, "data") != 0)
38449 {
38450 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38451 enter ? "enter" : "exit");
38452 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38453 return NULL_TREE;
38454 }
38455
38456 cp_lexer_consume_token (parser->lexer);
38457
38458 if (enter)
38459 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38460 "#pragma acc enter data", pragma_tok);
38461 else
38462 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38463 "#pragma acc exit data", pragma_tok);
38464
38465 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38466 {
38467 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38468 enter ? "enter" : "exit");
38469 return NULL_TREE;
38470 }
38471
38472 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38473 TREE_TYPE (stmt) = void_type_node;
38474 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38475 SET_EXPR_LOCATION (stmt, loc);
38476 add_stmt (stmt);
38477 return stmt;
38478 }
38479
38480 /* OpenACC 2.0:
38481 # pragma acc loop oacc-loop-clause[optseq] new-line
38482 structured-block */
38483
38484 #define OACC_LOOP_CLAUSE_MASK \
38485 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38495
38496 static tree
38497 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38498 omp_clause_mask mask, tree *cclauses, bool *if_p)
38499 {
38500 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38501
38502 strcat (p_name, " loop");
38503 mask |= OACC_LOOP_CLAUSE_MASK;
38504
38505 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38506 cclauses == NULL);
38507 if (cclauses)
38508 {
38509 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38510 if (*cclauses)
38511 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38512 if (clauses)
38513 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38514 }
38515
38516 tree block = begin_omp_structured_block ();
38517 int save = cp_parser_begin_omp_structured_block (parser);
38518 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38519 cp_parser_end_omp_structured_block (parser, save);
38520 add_stmt (finish_omp_structured_block (block));
38521
38522 return stmt;
38523 }
38524
38525 /* OpenACC 2.0:
38526 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38527 structured-block
38528
38529 or
38530
38531 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38532 structured-block
38533 */
38534
38535 #define OACC_KERNELS_CLAUSE_MASK \
38536 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38546 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38549
38550 #define OACC_PARALLEL_CLAUSE_MASK \
38551 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38567
38568 static tree
38569 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38570 char *p_name, bool *if_p)
38571 {
38572 omp_clause_mask mask;
38573 enum tree_code code;
38574 switch (cp_parser_pragma_kind (pragma_tok))
38575 {
38576 case PRAGMA_OACC_KERNELS:
38577 strcat (p_name, " kernels");
38578 mask = OACC_KERNELS_CLAUSE_MASK;
38579 code = OACC_KERNELS;
38580 break;
38581 case PRAGMA_OACC_PARALLEL:
38582 strcat (p_name, " parallel");
38583 mask = OACC_PARALLEL_CLAUSE_MASK;
38584 code = OACC_PARALLEL;
38585 break;
38586 default:
38587 gcc_unreachable ();
38588 }
38589
38590 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38591 {
38592 const char *p
38593 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38594 if (strcmp (p, "loop") == 0)
38595 {
38596 cp_lexer_consume_token (parser->lexer);
38597 tree block = begin_omp_parallel ();
38598 tree clauses;
38599 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
38600 if_p);
38601 return finish_omp_construct (code, block, clauses);
38602 }
38603 }
38604
38605 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38606
38607 tree block = begin_omp_parallel ();
38608 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38609 cp_parser_statement (parser, NULL_TREE, false, if_p);
38610 cp_parser_end_omp_structured_block (parser, save);
38611 return finish_omp_construct (code, block, clauses);
38612 }
38613
38614 /* OpenACC 2.0:
38615 # pragma acc update oacc-update-clause[optseq] new-line
38616 */
38617
38618 #define OACC_UPDATE_CLAUSE_MASK \
38619 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38625
38626 static tree
38627 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38628 {
38629 tree stmt, clauses;
38630
38631 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
38632 "#pragma acc update", pragma_tok);
38633
38634 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38635 {
38636 error_at (pragma_tok->location,
38637 "%<#pragma acc update%> must contain at least one "
38638 "%<device%> or %<host%> or %<self%> clause");
38639 return NULL_TREE;
38640 }
38641
38642 stmt = make_node (OACC_UPDATE);
38643 TREE_TYPE (stmt) = void_type_node;
38644 OACC_UPDATE_CLAUSES (stmt) = clauses;
38645 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38646 add_stmt (stmt);
38647 return stmt;
38648 }
38649
38650 /* OpenACC 2.0:
38651 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38652
38653 LOC is the location of the #pragma token.
38654 */
38655
38656 #define OACC_WAIT_CLAUSE_MASK \
38657 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38658
38659 static tree
38660 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
38661 {
38662 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
38663 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38664
38665 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38666 list = cp_parser_oacc_wait_list (parser, loc, list);
38667
38668 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
38669 "#pragma acc wait", pragma_tok);
38670
38671 stmt = c_finish_oacc_wait (loc, list, clauses);
38672 stmt = finish_expr_stmt (stmt);
38673
38674 return stmt;
38675 }
38676
38677 /* OpenMP 4.0:
38678 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38679
38680 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38681 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38687
38688 static void
38689 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
38690 enum pragma_context context)
38691 {
38692 bool first_p = parser->omp_declare_simd == NULL;
38693 cp_omp_declare_simd_data data;
38694 if (first_p)
38695 {
38696 data.error_seen = false;
38697 data.fndecl_seen = false;
38698 data.tokens = vNULL;
38699 data.clauses = NULL_TREE;
38700 /* It is safe to take the address of a local variable; it will only be
38701 used while this scope is live. */
38702 parser->omp_declare_simd = &data;
38703 }
38704
38705 /* Store away all pragma tokens. */
38706 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38707 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38708 cp_lexer_consume_token (parser->lexer);
38709 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38710 parser->omp_declare_simd->error_seen = true;
38711 cp_parser_require_pragma_eol (parser, pragma_tok);
38712 struct cp_token_cache *cp
38713 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38714 parser->omp_declare_simd->tokens.safe_push (cp);
38715
38716 if (first_p)
38717 {
38718 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38719 cp_parser_pragma (parser, context, NULL);
38720 switch (context)
38721 {
38722 case pragma_external:
38723 cp_parser_declaration (parser);
38724 break;
38725 case pragma_member:
38726 cp_parser_member_declaration (parser);
38727 break;
38728 case pragma_objc_icode:
38729 cp_parser_block_declaration (parser, /*statement_p=*/false);
38730 break;
38731 default:
38732 cp_parser_declaration_statement (parser);
38733 break;
38734 }
38735 if (parser->omp_declare_simd
38736 && !parser->omp_declare_simd->error_seen
38737 && !parser->omp_declare_simd->fndecl_seen)
38738 error_at (pragma_tok->location,
38739 "%<#pragma omp declare simd%> not immediately followed by "
38740 "function declaration or definition");
38741 data.tokens.release ();
38742 parser->omp_declare_simd = NULL;
38743 }
38744 }
38745
38746 /* Finalize #pragma omp declare simd clauses after direct declarator has
38747 been parsed, and put that into "omp declare simd" attribute. */
38748
38749 static tree
38750 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
38751 {
38752 struct cp_token_cache *ce;
38753 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
38754 int i;
38755
38756 if (!data->error_seen && data->fndecl_seen)
38757 {
38758 error ("%<#pragma omp declare simd%> not immediately followed by "
38759 "a single function declaration or definition");
38760 data->error_seen = true;
38761 }
38762 if (data->error_seen)
38763 return attrs;
38764
38765 FOR_EACH_VEC_ELT (data->tokens, i, ce)
38766 {
38767 tree c, cl;
38768
38769 cp_parser_push_lexer_for_tokens (parser, ce);
38770 parser->lexer->in_pragma = true;
38771 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38772 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38773 cp_lexer_consume_token (parser->lexer);
38774 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
38775 "#pragma omp declare simd", pragma_tok);
38776 cp_parser_pop_lexer (parser);
38777 if (cl)
38778 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
38779 c = build_tree_list (get_identifier ("omp declare simd"), cl);
38780 TREE_CHAIN (c) = attrs;
38781 if (processing_template_decl)
38782 ATTR_IS_DEPENDENT (c) = 1;
38783 attrs = c;
38784 }
38785
38786 data->fndecl_seen = true;
38787 return attrs;
38788 }
38789
38790
38791 /* OpenMP 4.0:
38792 # pragma omp declare target new-line
38793 declarations and definitions
38794 # pragma omp end declare target new-line
38795
38796 OpenMP 4.5:
38797 # pragma omp declare target ( extended-list ) new-line
38798
38799 # pragma omp declare target declare-target-clauses[seq] new-line */
38800
38801 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38802 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38804
38805 static void
38806 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
38807 {
38808 tree clauses = NULL_TREE;
38809 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38810 clauses
38811 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
38812 "#pragma omp declare target", pragma_tok);
38813 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38814 {
38815 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
38816 clauses);
38817 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
38818 cp_parser_require_pragma_eol (parser, pragma_tok);
38819 }
38820 else
38821 {
38822 cp_parser_require_pragma_eol (parser, pragma_tok);
38823 scope_chain->omp_declare_target_attribute++;
38824 return;
38825 }
38826 if (scope_chain->omp_declare_target_attribute)
38827 error_at (pragma_tok->location,
38828 "%<#pragma omp declare target%> with clauses in between "
38829 "%<#pragma omp declare target%> without clauses and "
38830 "%<#pragma omp end declare target%>");
38831 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
38832 {
38833 tree t = OMP_CLAUSE_DECL (c), id;
38834 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
38835 tree at2 = lookup_attribute ("omp declare target link",
38836 DECL_ATTRIBUTES (t));
38837 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
38838 {
38839 id = get_identifier ("omp declare target link");
38840 std::swap (at1, at2);
38841 }
38842 else
38843 id = get_identifier ("omp declare target");
38844 if (at2)
38845 {
38846 error_at (OMP_CLAUSE_LOCATION (c),
38847 "%qD specified both in declare target %<link%> and %<to%>"
38848 " clauses", t);
38849 continue;
38850 }
38851 if (!at1)
38852 {
38853 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
38854 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
38855 continue;
38856
38857 symtab_node *node = symtab_node::get (t);
38858 if (node != NULL)
38859 {
38860 node->offloadable = 1;
38861 if (ENABLE_OFFLOADING)
38862 {
38863 g->have_offload = true;
38864 if (is_a <varpool_node *> (node))
38865 vec_safe_push (offload_vars, t);
38866 }
38867 }
38868 }
38869 }
38870 }
38871
38872 static void
38873 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
38874 {
38875 const char *p = "";
38876 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38877 {
38878 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38879 p = IDENTIFIER_POINTER (id);
38880 }
38881 if (strcmp (p, "declare") == 0)
38882 {
38883 cp_lexer_consume_token (parser->lexer);
38884 p = "";
38885 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38886 {
38887 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38888 p = IDENTIFIER_POINTER (id);
38889 }
38890 if (strcmp (p, "target") == 0)
38891 cp_lexer_consume_token (parser->lexer);
38892 else
38893 {
38894 cp_parser_error (parser, "expected %<target%>");
38895 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38896 return;
38897 }
38898 }
38899 else
38900 {
38901 cp_parser_error (parser, "expected %<declare%>");
38902 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38903 return;
38904 }
38905 cp_parser_require_pragma_eol (parser, pragma_tok);
38906 if (!scope_chain->omp_declare_target_attribute)
38907 error_at (pragma_tok->location,
38908 "%<#pragma omp end declare target%> without corresponding "
38909 "%<#pragma omp declare target%>");
38910 else
38911 scope_chain->omp_declare_target_attribute--;
38912 }
38913
38914 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
38915 expression and optional initializer clause of
38916 #pragma omp declare reduction. We store the expression(s) as
38917 either 3, 6 or 7 special statements inside of the artificial function's
38918 body. The first two statements are DECL_EXPRs for the artificial
38919 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
38920 expression that uses those variables.
38921 If there was any INITIALIZER clause, this is followed by further statements,
38922 the fourth and fifth statements are DECL_EXPRs for the artificial
38923 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
38924 constructor variant (first token after open paren is not omp_priv),
38925 then the sixth statement is a statement with the function call expression
38926 that uses the OMP_PRIV and optionally OMP_ORIG variable.
38927 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
38928 to initialize the OMP_PRIV artificial variable and there is seventh
38929 statement, a DECL_EXPR of the OMP_PRIV statement again. */
38930
38931 static bool
38932 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
38933 {
38934 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
38935 gcc_assert (TYPE_REF_P (type));
38936 type = TREE_TYPE (type);
38937 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
38938 DECL_ARTIFICIAL (omp_out) = 1;
38939 pushdecl (omp_out);
38940 add_decl_expr (omp_out);
38941 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
38942 DECL_ARTIFICIAL (omp_in) = 1;
38943 pushdecl (omp_in);
38944 add_decl_expr (omp_in);
38945 tree combiner;
38946 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
38947
38948 keep_next_level (true);
38949 tree block = begin_omp_structured_block ();
38950 combiner = cp_parser_expression (parser);
38951 finish_expr_stmt (combiner);
38952 block = finish_omp_structured_block (block);
38953 add_stmt (block);
38954
38955 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38956 return false;
38957
38958 const char *p = "";
38959 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38960 {
38961 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38962 p = IDENTIFIER_POINTER (id);
38963 }
38964
38965 if (strcmp (p, "initializer") == 0)
38966 {
38967 cp_lexer_consume_token (parser->lexer);
38968 matching_parens parens;
38969 if (!parens.require_open (parser))
38970 return false;
38971
38972 p = "";
38973 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38974 {
38975 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38976 p = IDENTIFIER_POINTER (id);
38977 }
38978
38979 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
38980 DECL_ARTIFICIAL (omp_priv) = 1;
38981 pushdecl (omp_priv);
38982 add_decl_expr (omp_priv);
38983 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
38984 DECL_ARTIFICIAL (omp_orig) = 1;
38985 pushdecl (omp_orig);
38986 add_decl_expr (omp_orig);
38987
38988 keep_next_level (true);
38989 block = begin_omp_structured_block ();
38990
38991 bool ctor = false;
38992 if (strcmp (p, "omp_priv") == 0)
38993 {
38994 bool is_direct_init, is_non_constant_init;
38995 ctor = true;
38996 cp_lexer_consume_token (parser->lexer);
38997 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
38998 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
38999 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39000 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
39001 == CPP_CLOSE_PAREN
39002 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
39003 == CPP_CLOSE_PAREN))
39004 {
39005 finish_omp_structured_block (block);
39006 error ("invalid initializer clause");
39007 return false;
39008 }
39009 initializer = cp_parser_initializer (parser, &is_direct_init,
39010 &is_non_constant_init);
39011 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
39012 NULL_TREE, LOOKUP_ONLYCONVERTING);
39013 }
39014 else
39015 {
39016 cp_parser_parse_tentatively (parser);
39017 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
39018 /*check_dependency_p=*/true,
39019 /*template_p=*/NULL,
39020 /*declarator_p=*/false,
39021 /*optional_p=*/false);
39022 vec<tree, va_gc> *args;
39023 if (fn_name == error_mark_node
39024 || cp_parser_error_occurred (parser)
39025 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39026 || ((args = cp_parser_parenthesized_expression_list
39027 (parser, non_attr, /*cast_p=*/false,
39028 /*allow_expansion_p=*/true,
39029 /*non_constant_p=*/NULL)),
39030 cp_parser_error_occurred (parser)))
39031 {
39032 finish_omp_structured_block (block);
39033 cp_parser_abort_tentative_parse (parser);
39034 cp_parser_error (parser, "expected id-expression (arguments)");
39035 return false;
39036 }
39037 unsigned int i;
39038 tree arg;
39039 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39040 if (arg == omp_priv
39041 || (TREE_CODE (arg) == ADDR_EXPR
39042 && TREE_OPERAND (arg, 0) == omp_priv))
39043 break;
39044 cp_parser_abort_tentative_parse (parser);
39045 if (arg == NULL_TREE)
39046 error ("one of the initializer call arguments should be %<omp_priv%>"
39047 " or %<&omp_priv%>");
39048 initializer = cp_parser_postfix_expression (parser, false, false, false,
39049 false, NULL);
39050 finish_expr_stmt (initializer);
39051 }
39052
39053 block = finish_omp_structured_block (block);
39054 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39055 add_stmt (block);
39056
39057 if (ctor)
39058 add_decl_expr (omp_orig);
39059
39060 if (!parens.require_close (parser))
39061 return false;
39062 }
39063
39064 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39065 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39066 UNKNOWN_LOCATION);
39067
39068 return true;
39069 }
39070
39071 /* OpenMP 4.0
39072 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39073 initializer-clause[opt] new-line
39074
39075 initializer-clause:
39076 initializer (omp_priv initializer)
39077 initializer (function-name (argument-list)) */
39078
39079 static void
39080 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39081 enum pragma_context)
39082 {
39083 auto_vec<tree> types;
39084 enum tree_code reduc_code = ERROR_MARK;
39085 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39086 unsigned int i;
39087 cp_token *first_token;
39088 cp_token_cache *cp;
39089 int errs;
39090 void *p;
39091
39092 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39093 p = obstack_alloc (&declarator_obstack, 0);
39094
39095 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39096 goto fail;
39097
39098 switch (cp_lexer_peek_token (parser->lexer)->type)
39099 {
39100 case CPP_PLUS:
39101 reduc_code = PLUS_EXPR;
39102 break;
39103 case CPP_MULT:
39104 reduc_code = MULT_EXPR;
39105 break;
39106 case CPP_MINUS:
39107 reduc_code = MINUS_EXPR;
39108 break;
39109 case CPP_AND:
39110 reduc_code = BIT_AND_EXPR;
39111 break;
39112 case CPP_XOR:
39113 reduc_code = BIT_XOR_EXPR;
39114 break;
39115 case CPP_OR:
39116 reduc_code = BIT_IOR_EXPR;
39117 break;
39118 case CPP_AND_AND:
39119 reduc_code = TRUTH_ANDIF_EXPR;
39120 break;
39121 case CPP_OR_OR:
39122 reduc_code = TRUTH_ORIF_EXPR;
39123 break;
39124 case CPP_NAME:
39125 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39126 break;
39127 default:
39128 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39129 "%<|%>, %<&&%>, %<||%> or identifier");
39130 goto fail;
39131 }
39132
39133 if (reduc_code != ERROR_MARK)
39134 cp_lexer_consume_token (parser->lexer);
39135
39136 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39137 if (reduc_id == error_mark_node)
39138 goto fail;
39139
39140 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39141 goto fail;
39142
39143 /* Types may not be defined in declare reduction type list. */
39144 const char *saved_message;
39145 saved_message = parser->type_definition_forbidden_message;
39146 parser->type_definition_forbidden_message
39147 = G_("types may not be defined in declare reduction type list");
39148 bool saved_colon_corrects_to_scope_p;
39149 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39150 parser->colon_corrects_to_scope_p = false;
39151 bool saved_colon_doesnt_start_class_def_p;
39152 saved_colon_doesnt_start_class_def_p
39153 = parser->colon_doesnt_start_class_def_p;
39154 parser->colon_doesnt_start_class_def_p = true;
39155
39156 while (true)
39157 {
39158 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39159 type = cp_parser_type_id (parser);
39160 if (type == error_mark_node)
39161 ;
39162 else if (ARITHMETIC_TYPE_P (type)
39163 && (orig_reduc_id == NULL_TREE
39164 || (TREE_CODE (type) != COMPLEX_TYPE
39165 && (id_equal (orig_reduc_id, "min")
39166 || id_equal (orig_reduc_id, "max")))))
39167 error_at (loc, "predeclared arithmetic type %qT in "
39168 "%<#pragma omp declare reduction%>", type);
39169 else if (TREE_CODE (type) == FUNCTION_TYPE
39170 || TREE_CODE (type) == METHOD_TYPE
39171 || TREE_CODE (type) == ARRAY_TYPE)
39172 error_at (loc, "function or array type %qT in "
39173 "%<#pragma omp declare reduction%>", type);
39174 else if (TYPE_REF_P (type))
39175 error_at (loc, "reference type %qT in "
39176 "%<#pragma omp declare reduction%>", type);
39177 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39178 error_at (loc, "const, volatile or __restrict qualified type %qT in "
39179 "%<#pragma omp declare reduction%>", type);
39180 else
39181 types.safe_push (type);
39182
39183 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39184 cp_lexer_consume_token (parser->lexer);
39185 else
39186 break;
39187 }
39188
39189 /* Restore the saved message. */
39190 parser->type_definition_forbidden_message = saved_message;
39191 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39192 parser->colon_doesnt_start_class_def_p
39193 = saved_colon_doesnt_start_class_def_p;
39194
39195 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39196 || types.is_empty ())
39197 {
39198 fail:
39199 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39200 goto done;
39201 }
39202
39203 first_token = cp_lexer_peek_token (parser->lexer);
39204 cp = NULL;
39205 errs = errorcount;
39206 FOR_EACH_VEC_ELT (types, i, type)
39207 {
39208 tree fntype
39209 = build_function_type_list (void_type_node,
39210 cp_build_reference_type (type, false),
39211 NULL_TREE);
39212 tree this_reduc_id = reduc_id;
39213 if (!dependent_type_p (type))
39214 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39215 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39216 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39217 DECL_ARTIFICIAL (fndecl) = 1;
39218 DECL_EXTERNAL (fndecl) = 1;
39219 DECL_DECLARED_INLINE_P (fndecl) = 1;
39220 DECL_IGNORED_P (fndecl) = 1;
39221 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39222 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39223 DECL_ATTRIBUTES (fndecl)
39224 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39225 DECL_ATTRIBUTES (fndecl));
39226 if (processing_template_decl)
39227 fndecl = push_template_decl (fndecl);
39228 bool block_scope = false;
39229 tree block = NULL_TREE;
39230 if (current_function_decl)
39231 {
39232 block_scope = true;
39233 DECL_CONTEXT (fndecl) = global_namespace;
39234 if (!processing_template_decl)
39235 pushdecl (fndecl);
39236 }
39237 else if (current_class_type)
39238 {
39239 if (cp == NULL)
39240 {
39241 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39242 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39243 cp_lexer_consume_token (parser->lexer);
39244 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39245 goto fail;
39246 cp = cp_token_cache_new (first_token,
39247 cp_lexer_peek_nth_token (parser->lexer,
39248 2));
39249 }
39250 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39251 finish_member_declaration (fndecl);
39252 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39253 DECL_PENDING_INLINE_P (fndecl) = 1;
39254 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39255 continue;
39256 }
39257 else
39258 {
39259 DECL_CONTEXT (fndecl) = current_namespace;
39260 pushdecl (fndecl);
39261 }
39262 if (!block_scope)
39263 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39264 else
39265 block = begin_omp_structured_block ();
39266 if (cp)
39267 {
39268 cp_parser_push_lexer_for_tokens (parser, cp);
39269 parser->lexer->in_pragma = true;
39270 }
39271 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39272 {
39273 if (!block_scope)
39274 finish_function (/*inline_p=*/false);
39275 else
39276 DECL_CONTEXT (fndecl) = current_function_decl;
39277 if (cp)
39278 cp_parser_pop_lexer (parser);
39279 goto fail;
39280 }
39281 if (cp)
39282 cp_parser_pop_lexer (parser);
39283 if (!block_scope)
39284 finish_function (/*inline_p=*/false);
39285 else
39286 {
39287 DECL_CONTEXT (fndecl) = current_function_decl;
39288 block = finish_omp_structured_block (block);
39289 if (TREE_CODE (block) == BIND_EXPR)
39290 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39291 else if (TREE_CODE (block) == STATEMENT_LIST)
39292 DECL_SAVED_TREE (fndecl) = block;
39293 if (processing_template_decl)
39294 add_decl_expr (fndecl);
39295 }
39296 cp_check_omp_declare_reduction (fndecl);
39297 if (cp == NULL && types.length () > 1)
39298 cp = cp_token_cache_new (first_token,
39299 cp_lexer_peek_nth_token (parser->lexer, 2));
39300 if (errs != errorcount)
39301 break;
39302 }
39303
39304 cp_parser_require_pragma_eol (parser, pragma_tok);
39305
39306 done:
39307 /* Free any declarators allocated. */
39308 obstack_free (&declarator_obstack, p);
39309 }
39310
39311 /* OpenMP 4.0
39312 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39313 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39314 initializer-clause[opt] new-line
39315 #pragma omp declare target new-line */
39316
39317 static bool
39318 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39319 enum pragma_context context)
39320 {
39321 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39322 {
39323 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39324 const char *p = IDENTIFIER_POINTER (id);
39325
39326 if (strcmp (p, "simd") == 0)
39327 {
39328 cp_lexer_consume_token (parser->lexer);
39329 cp_parser_omp_declare_simd (parser, pragma_tok,
39330 context);
39331 return true;
39332 }
39333 cp_ensure_no_omp_declare_simd (parser);
39334 if (strcmp (p, "reduction") == 0)
39335 {
39336 cp_lexer_consume_token (parser->lexer);
39337 cp_parser_omp_declare_reduction (parser, pragma_tok,
39338 context);
39339 return false;
39340 }
39341 if (!flag_openmp) /* flag_openmp_simd */
39342 {
39343 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39344 return false;
39345 }
39346 if (strcmp (p, "target") == 0)
39347 {
39348 cp_lexer_consume_token (parser->lexer);
39349 cp_parser_omp_declare_target (parser, pragma_tok);
39350 return false;
39351 }
39352 }
39353 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39354 "or %<target%>");
39355 cp_parser_require_pragma_eol (parser, pragma_tok);
39356 return false;
39357 }
39358
39359 /* OpenMP 5.0
39360 #pragma omp requires clauses[optseq] new-line */
39361
39362 static bool
39363 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39364 {
39365 bool first = true;
39366 enum omp_requires new_req = (enum omp_requires) 0;
39367
39368 location_t loc = pragma_tok->location;
39369 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39370 {
39371 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39372 cp_lexer_consume_token (parser->lexer);
39373
39374 first = false;
39375
39376 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39377 {
39378 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39379 const char *p = IDENTIFIER_POINTER (id);
39380 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39381 enum omp_requires this_req = (enum omp_requires) 0;
39382
39383 if (!strcmp (p, "unified_address"))
39384 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39385 else if (!strcmp (p, "unified_shared_memory"))
39386 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39387 else if (!strcmp (p, "dynamic_allocators"))
39388 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39389 else if (!strcmp (p, "reverse_offload"))
39390 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39391 else if (!strcmp (p, "atomic_default_mem_order"))
39392 {
39393 cp_lexer_consume_token (parser->lexer);
39394
39395 matching_parens parens;
39396 if (parens.require_open (parser))
39397 {
39398 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39399 {
39400 id = cp_lexer_peek_token (parser->lexer)->u.value;
39401 p = IDENTIFIER_POINTER (id);
39402
39403 if (!strcmp (p, "seq_cst"))
39404 this_req
39405 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39406 else if (!strcmp (p, "relaxed"))
39407 this_req
39408 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39409 else if (!strcmp (p, "acq_rel"))
39410 this_req
39411 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39412 }
39413 if (this_req == 0)
39414 {
39415 error_at (cp_lexer_peek_token (parser->lexer)->location,
39416 "expected %<seq_cst%>, %<relaxed%> or "
39417 "%<acq_rel%>");
39418 if (cp_lexer_nth_token_is (parser->lexer, 2,
39419 CPP_CLOSE_PAREN))
39420 cp_lexer_consume_token (parser->lexer);
39421 }
39422 else
39423 cp_lexer_consume_token (parser->lexer);
39424
39425 if (!parens.require_close (parser))
39426 cp_parser_skip_to_closing_parenthesis (parser,
39427 /*recovering=*/true,
39428 /*or_comma=*/false,
39429 /*consume_paren=*/
39430 true);
39431
39432 if (this_req == 0)
39433 {
39434 cp_parser_require_pragma_eol (parser, pragma_tok);
39435 return false;
39436 }
39437 }
39438 p = NULL;
39439 }
39440 else
39441 {
39442 error_at (cloc, "expected %<unified_address%>, "
39443 "%<unified_shared_memory%>, "
39444 "%<dynamic_allocators%>, "
39445 "%<reverse_offload%> "
39446 "or %<atomic_default_mem_order%> clause");
39447 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39448 return false;
39449 }
39450 if (p)
39451 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39452 "supported yet", p);
39453 if (p)
39454 cp_lexer_consume_token (parser->lexer);
39455 if (this_req)
39456 {
39457 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39458 {
39459 if ((this_req & new_req) != 0)
39460 error_at (cloc, "too many %qs clauses", p);
39461 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39462 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39463 error_at (cloc, "%qs clause used lexically after first "
39464 "target construct or offloading API", p);
39465 }
39466 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39467 {
39468 error_at (cloc, "too many %qs clauses",
39469 "atomic_default_mem_order");
39470 this_req = (enum omp_requires) 0;
39471 }
39472 else if ((omp_requires_mask
39473 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39474 {
39475 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39476 " clause in a single compilation unit");
39477 this_req
39478 = (enum omp_requires)
39479 (omp_requires_mask
39480 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39481 }
39482 else if ((omp_requires_mask
39483 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39484 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39485 "lexically after first %<atomic%> construct "
39486 "without memory order clause");
39487 new_req = (enum omp_requires) (new_req | this_req);
39488 omp_requires_mask
39489 = (enum omp_requires) (omp_requires_mask | this_req);
39490 continue;
39491 }
39492 }
39493 break;
39494 }
39495 cp_parser_require_pragma_eol (parser, pragma_tok);
39496
39497 if (new_req == 0)
39498 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39499 return false;
39500 }
39501
39502
39503 /* OpenMP 4.5:
39504 #pragma omp taskloop taskloop-clause[optseq] new-line
39505 for-loop
39506
39507 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39508 for-loop */
39509
39510 #define OMP_TASKLOOP_CLAUSE_MASK \
39511 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39527
39528 static tree
39529 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39530 char *p_name, omp_clause_mask mask, tree *cclauses,
39531 bool *if_p)
39532 {
39533 tree clauses, sb, ret;
39534 unsigned int save;
39535 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39536
39537 strcat (p_name, " taskloop");
39538 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39539 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39540 clause. */
39541 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39542 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39543
39544 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39545 {
39546 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39547 const char *p = IDENTIFIER_POINTER (id);
39548
39549 if (strcmp (p, "simd") == 0)
39550 {
39551 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39552 if (cclauses == NULL)
39553 cclauses = cclauses_buf;
39554
39555 cp_lexer_consume_token (parser->lexer);
39556 if (!flag_openmp) /* flag_openmp_simd */
39557 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39558 cclauses, if_p);
39559 sb = begin_omp_structured_block ();
39560 save = cp_parser_begin_omp_structured_block (parser);
39561 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39562 cclauses, if_p);
39563 cp_parser_end_omp_structured_block (parser, save);
39564 tree body = finish_omp_structured_block (sb);
39565 if (ret == NULL)
39566 return ret;
39567 ret = make_node (OMP_TASKLOOP);
39568 TREE_TYPE (ret) = void_type_node;
39569 OMP_FOR_BODY (ret) = body;
39570 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39571 SET_EXPR_LOCATION (ret, loc);
39572 add_stmt (ret);
39573 return ret;
39574 }
39575 }
39576 if (!flag_openmp) /* flag_openmp_simd */
39577 {
39578 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39579 return NULL_TREE;
39580 }
39581
39582 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39583 cclauses == NULL);
39584 if (cclauses)
39585 {
39586 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39587 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39588 }
39589
39590 keep_next_level (true);
39591 sb = begin_omp_structured_block ();
39592 save = cp_parser_begin_omp_structured_block (parser);
39593
39594 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39595 if_p);
39596
39597 cp_parser_end_omp_structured_block (parser, save);
39598 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39599
39600 return ret;
39601 }
39602
39603
39604 /* OpenACC 2.0:
39605 # pragma acc routine oacc-routine-clause[optseq] new-line
39606 function-definition
39607
39608 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39609 */
39610
39611 #define OACC_ROUTINE_CLAUSE_MASK \
39612 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39616
39617
39618 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39619 component, which must resolve to a declared namespace-scope
39620 function. The clauses are either processed directly (for a named
39621 function), or defered until the immediatley following declaration
39622 is parsed. */
39623
39624 static void
39625 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39626 enum pragma_context context)
39627 {
39628 gcc_checking_assert (context == pragma_external);
39629 /* The checking for "another pragma following this one" in the "no optional
39630 '( name )'" case makes sure that we dont re-enter. */
39631 gcc_checking_assert (parser->oacc_routine == NULL);
39632
39633 cp_oacc_routine_data data;
39634 data.error_seen = false;
39635 data.fndecl_seen = false;
39636 data.tokens = vNULL;
39637 data.clauses = NULL_TREE;
39638 data.loc = pragma_tok->location;
39639 /* It is safe to take the address of a local variable; it will only be
39640 used while this scope is live. */
39641 parser->oacc_routine = &data;
39642
39643 /* Look for optional '( name )'. */
39644 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39645 {
39646 matching_parens parens;
39647 parens.consume_open (parser); /* '(' */
39648
39649 /* We parse the name as an id-expression. If it resolves to
39650 anything other than a non-overloaded function at namespace
39651 scope, it's an error. */
39652 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
39653 tree name = cp_parser_id_expression (parser,
39654 /*template_keyword_p=*/false,
39655 /*check_dependency_p=*/false,
39656 /*template_p=*/NULL,
39657 /*declarator_p=*/false,
39658 /*optional_p=*/false);
39659 tree decl = (identifier_p (name)
39660 ? cp_parser_lookup_name_simple (parser, name, name_loc)
39661 : name);
39662 if (name != error_mark_node && decl == error_mark_node)
39663 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
39664
39665 if (decl == error_mark_node
39666 || !parens.require_close (parser))
39667 {
39668 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39669 parser->oacc_routine = NULL;
39670 return;
39671 }
39672
39673 data.clauses
39674 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39675 "#pragma acc routine",
39676 cp_lexer_peek_token (parser->lexer));
39677
39678 if (decl && is_overloaded_fn (decl)
39679 && (TREE_CODE (decl) != FUNCTION_DECL
39680 || DECL_FUNCTION_TEMPLATE_P (decl)))
39681 {
39682 error_at (name_loc,
39683 "%<#pragma acc routine%> names a set of overloads");
39684 parser->oacc_routine = NULL;
39685 return;
39686 }
39687
39688 /* Perhaps we should use the same rule as declarations in different
39689 namespaces? */
39690 if (!DECL_NAMESPACE_SCOPE_P (decl))
39691 {
39692 error_at (name_loc,
39693 "%qD does not refer to a namespace scope function", decl);
39694 parser->oacc_routine = NULL;
39695 return;
39696 }
39697
39698 if (TREE_CODE (decl) != FUNCTION_DECL)
39699 {
39700 error_at (name_loc, "%qD does not refer to a function", decl);
39701 parser->oacc_routine = NULL;
39702 return;
39703 }
39704
39705 cp_finalize_oacc_routine (parser, decl, false);
39706 parser->oacc_routine = NULL;
39707 }
39708 else /* No optional '( name )'. */
39709 {
39710 /* Store away all pragma tokens. */
39711 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39712 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39713 cp_lexer_consume_token (parser->lexer);
39714 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39715 parser->oacc_routine->error_seen = true;
39716 cp_parser_require_pragma_eol (parser, pragma_tok);
39717 struct cp_token_cache *cp
39718 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39719 parser->oacc_routine->tokens.safe_push (cp);
39720
39721 /* Emit a helpful diagnostic if there's another pragma following this
39722 one. */
39723 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39724 {
39725 cp_ensure_no_oacc_routine (parser);
39726 data.tokens.release ();
39727 /* ..., and then just keep going. */
39728 return;
39729 }
39730
39731 /* We only have to consider the pragma_external case here. */
39732 cp_parser_declaration (parser);
39733 if (parser->oacc_routine
39734 && !parser->oacc_routine->fndecl_seen)
39735 cp_ensure_no_oacc_routine (parser);
39736 else
39737 parser->oacc_routine = NULL;
39738 data.tokens.release ();
39739 }
39740 }
39741
39742 /* Finalize #pragma acc routine clauses after direct declarator has
39743 been parsed. */
39744
39745 static tree
39746 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
39747 {
39748 struct cp_token_cache *ce;
39749 cp_oacc_routine_data *data = parser->oacc_routine;
39750
39751 if (!data->error_seen && data->fndecl_seen)
39752 {
39753 error_at (data->loc,
39754 "%<#pragma acc routine%> not immediately followed by "
39755 "a single function declaration or definition");
39756 data->error_seen = true;
39757 }
39758 if (data->error_seen)
39759 return attrs;
39760
39761 gcc_checking_assert (data->tokens.length () == 1);
39762 ce = data->tokens[0];
39763
39764 cp_parser_push_lexer_for_tokens (parser, ce);
39765 parser->lexer->in_pragma = true;
39766 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39767
39768 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39769 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
39770 parser->oacc_routine->clauses
39771 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39772 "#pragma acc routine", pragma_tok);
39773 cp_parser_pop_lexer (parser);
39774 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39775 fndecl_seen. */
39776
39777 return attrs;
39778 }
39779
39780 /* Apply any saved OpenACC routine clauses to a just-parsed
39781 declaration. */
39782
39783 static void
39784 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
39785 {
39786 if (__builtin_expect (parser->oacc_routine != NULL, 0))
39787 {
39788 /* Keep going if we're in error reporting mode. */
39789 if (parser->oacc_routine->error_seen
39790 || fndecl == error_mark_node)
39791 return;
39792
39793 if (parser->oacc_routine->fndecl_seen)
39794 {
39795 error_at (parser->oacc_routine->loc,
39796 "%<#pragma acc routine%> not immediately followed by"
39797 " a single function declaration or definition");
39798 parser->oacc_routine = NULL;
39799 return;
39800 }
39801 if (TREE_CODE (fndecl) != FUNCTION_DECL)
39802 {
39803 cp_ensure_no_oacc_routine (parser);
39804 return;
39805 }
39806
39807 if (oacc_get_fn_attrib (fndecl))
39808 {
39809 error_at (parser->oacc_routine->loc,
39810 "%<#pragma acc routine%> already applied to %qD", fndecl);
39811 parser->oacc_routine = NULL;
39812 return;
39813 }
39814
39815 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
39816 {
39817 error_at (parser->oacc_routine->loc,
39818 TREE_USED (fndecl)
39819 ? G_("%<#pragma acc routine%> must be applied before use")
39820 : G_("%<#pragma acc routine%> must be applied before "
39821 "definition"));
39822 parser->oacc_routine = NULL;
39823 return;
39824 }
39825
39826 /* Process the routine's dimension clauses. */
39827 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
39828 oacc_replace_fn_attrib (fndecl, dims);
39829
39830 /* Add an "omp declare target" attribute. */
39831 DECL_ATTRIBUTES (fndecl)
39832 = tree_cons (get_identifier ("omp declare target"),
39833 NULL_TREE, DECL_ATTRIBUTES (fndecl));
39834
39835 /* Don't unset parser->oacc_routine here: we may still need it to
39836 diagnose wrong usage. But, remember that we've used this "#pragma acc
39837 routine". */
39838 parser->oacc_routine->fndecl_seen = true;
39839 }
39840 }
39841
39842 /* Main entry point to OpenMP statement pragmas. */
39843
39844 static void
39845 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
39846 {
39847 tree stmt;
39848 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
39849 omp_clause_mask mask (0);
39850
39851 switch (cp_parser_pragma_kind (pragma_tok))
39852 {
39853 case PRAGMA_OACC_ATOMIC:
39854 cp_parser_omp_atomic (parser, pragma_tok);
39855 return;
39856 case PRAGMA_OACC_CACHE:
39857 stmt = cp_parser_oacc_cache (parser, pragma_tok);
39858 break;
39859 case PRAGMA_OACC_DATA:
39860 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
39861 break;
39862 case PRAGMA_OACC_ENTER_DATA:
39863 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
39864 break;
39865 case PRAGMA_OACC_EXIT_DATA:
39866 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
39867 break;
39868 case PRAGMA_OACC_HOST_DATA:
39869 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
39870 break;
39871 case PRAGMA_OACC_KERNELS:
39872 case PRAGMA_OACC_PARALLEL:
39873 strcpy (p_name, "#pragma acc");
39874 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
39875 if_p);
39876 break;
39877 case PRAGMA_OACC_LOOP:
39878 strcpy (p_name, "#pragma acc");
39879 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
39880 if_p);
39881 break;
39882 case PRAGMA_OACC_UPDATE:
39883 stmt = cp_parser_oacc_update (parser, pragma_tok);
39884 break;
39885 case PRAGMA_OACC_WAIT:
39886 stmt = cp_parser_oacc_wait (parser, pragma_tok);
39887 break;
39888 case PRAGMA_OMP_ATOMIC:
39889 cp_parser_omp_atomic (parser, pragma_tok);
39890 return;
39891 case PRAGMA_OMP_CRITICAL:
39892 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
39893 break;
39894 case PRAGMA_OMP_DISTRIBUTE:
39895 strcpy (p_name, "#pragma omp");
39896 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
39897 if_p);
39898 break;
39899 case PRAGMA_OMP_FOR:
39900 strcpy (p_name, "#pragma omp");
39901 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
39902 if_p);
39903 break;
39904 case PRAGMA_OMP_MASTER:
39905 strcpy (p_name, "#pragma omp");
39906 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
39907 if_p);
39908 break;
39909 case PRAGMA_OMP_PARALLEL:
39910 strcpy (p_name, "#pragma omp");
39911 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
39912 if_p);
39913 break;
39914 case PRAGMA_OMP_SECTIONS:
39915 strcpy (p_name, "#pragma omp");
39916 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
39917 break;
39918 case PRAGMA_OMP_SIMD:
39919 strcpy (p_name, "#pragma omp");
39920 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
39921 if_p);
39922 break;
39923 case PRAGMA_OMP_SINGLE:
39924 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
39925 break;
39926 case PRAGMA_OMP_TASK:
39927 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
39928 break;
39929 case PRAGMA_OMP_TASKGROUP:
39930 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
39931 break;
39932 case PRAGMA_OMP_TASKLOOP:
39933 strcpy (p_name, "#pragma omp");
39934 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
39935 if_p);
39936 break;
39937 case PRAGMA_OMP_TEAMS:
39938 strcpy (p_name, "#pragma omp");
39939 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
39940 if_p);
39941 break;
39942 default:
39943 gcc_unreachable ();
39944 }
39945
39946 protected_set_expr_location (stmt, pragma_tok->location);
39947 }
39948 \f
39949 /* Transactional Memory parsing routines. */
39950
39951 /* Parse a transaction attribute.
39952
39953 txn-attribute:
39954 attribute
39955 [ [ identifier ] ]
39956
39957 We use this instead of cp_parser_attributes_opt for transactions to avoid
39958 the pedwarn in C++98 mode. */
39959
39960 static tree
39961 cp_parser_txn_attribute_opt (cp_parser *parser)
39962 {
39963 cp_token *token;
39964 tree attr_name, attr = NULL;
39965
39966 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
39967 return cp_parser_attributes_opt (parser);
39968
39969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
39970 return NULL_TREE;
39971 cp_lexer_consume_token (parser->lexer);
39972 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
39973 goto error1;
39974
39975 token = cp_lexer_peek_token (parser->lexer);
39976 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
39977 {
39978 token = cp_lexer_consume_token (parser->lexer);
39979
39980 attr_name = (token->type == CPP_KEYWORD
39981 /* For keywords, use the canonical spelling,
39982 not the parsed identifier. */
39983 ? ridpointers[(int) token->keyword]
39984 : token->u.value);
39985 attr = build_tree_list (attr_name, NULL_TREE);
39986 }
39987 else
39988 cp_parser_error (parser, "expected identifier");
39989
39990 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39991 error1:
39992 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
39993 return attr;
39994 }
39995
39996 /* Parse a __transaction_atomic or __transaction_relaxed statement.
39997
39998 transaction-statement:
39999 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40000 compound-statement
40001 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40002 */
40003
40004 static tree
40005 cp_parser_transaction (cp_parser *parser, cp_token *token)
40006 {
40007 unsigned char old_in = parser->in_transaction;
40008 unsigned char this_in = 1, new_in;
40009 enum rid keyword = token->keyword;
40010 tree stmt, attrs, noex;
40011
40012 cp_lexer_consume_token (parser->lexer);
40013
40014 if (keyword == RID_TRANSACTION_RELAXED
40015 || keyword == RID_SYNCHRONIZED)
40016 this_in |= TM_STMT_ATTR_RELAXED;
40017 else
40018 {
40019 attrs = cp_parser_txn_attribute_opt (parser);
40020 if (attrs)
40021 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40022 }
40023
40024 /* Parse a noexcept specification. */
40025 if (keyword == RID_ATOMIC_NOEXCEPT)
40026 noex = boolean_true_node;
40027 else if (keyword == RID_ATOMIC_CANCEL)
40028 {
40029 /* cancel-and-throw is unimplemented. */
40030 sorry ("atomic_cancel");
40031 noex = NULL_TREE;
40032 }
40033 else
40034 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
40035
40036 /* Keep track if we're in the lexical scope of an outer transaction. */
40037 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40038
40039 stmt = begin_transaction_stmt (token->location, NULL, this_in);
40040
40041 parser->in_transaction = new_in;
40042 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40043 parser->in_transaction = old_in;
40044
40045 finish_transaction_stmt (stmt, NULL, this_in, noex);
40046
40047 return stmt;
40048 }
40049
40050 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40051
40052 transaction-expression:
40053 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40054 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40055 */
40056
40057 static tree
40058 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40059 {
40060 unsigned char old_in = parser->in_transaction;
40061 unsigned char this_in = 1;
40062 cp_token *token;
40063 tree expr, noex;
40064 bool noex_expr;
40065 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40066
40067 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40068 || keyword == RID_TRANSACTION_RELAXED);
40069
40070 if (!flag_tm)
40071 error_at (loc,
40072 keyword == RID_TRANSACTION_RELAXED
40073 ? G_("%<__transaction_relaxed%> without transactional memory "
40074 "support enabled")
40075 : G_("%<__transaction_atomic%> without transactional memory "
40076 "support enabled"));
40077
40078 token = cp_parser_require_keyword (parser, keyword,
40079 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40080 : RT_TRANSACTION_RELAXED));
40081 gcc_assert (token != NULL);
40082
40083 if (keyword == RID_TRANSACTION_RELAXED)
40084 this_in |= TM_STMT_ATTR_RELAXED;
40085
40086 /* Set this early. This might mean that we allow transaction_cancel in
40087 an expression that we find out later actually has to be a constexpr.
40088 However, we expect that cxx_constant_value will be able to deal with
40089 this; also, if the noexcept has no constexpr, then what we parse next
40090 really is a transaction's body. */
40091 parser->in_transaction = this_in;
40092
40093 /* Parse a noexcept specification. */
40094 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
40095 true);
40096
40097 if (!noex || !noex_expr
40098 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40099 {
40100 matching_parens parens;
40101 parens.require_open (parser);
40102
40103 expr = cp_parser_expression (parser);
40104 expr = finish_parenthesized_expr (expr);
40105
40106 parens.require_close (parser);
40107 }
40108 else
40109 {
40110 /* The only expression that is available got parsed for the noexcept
40111 already. noexcept is true then. */
40112 expr = noex;
40113 noex = boolean_true_node;
40114 }
40115
40116 expr = build_transaction_expr (token->location, expr, this_in, noex);
40117 parser->in_transaction = old_in;
40118
40119 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40120 return error_mark_node;
40121
40122 return (flag_tm ? expr : error_mark_node);
40123 }
40124
40125 /* Parse a function-transaction-block.
40126
40127 function-transaction-block:
40128 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40129 function-body
40130 __transaction_atomic txn-attribute[opt] function-try-block
40131 __transaction_relaxed ctor-initializer[opt] function-body
40132 __transaction_relaxed function-try-block
40133 */
40134
40135 static void
40136 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40137 {
40138 unsigned char old_in = parser->in_transaction;
40139 unsigned char new_in = 1;
40140 tree compound_stmt, stmt, attrs;
40141 cp_token *token;
40142
40143 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40144 || keyword == RID_TRANSACTION_RELAXED);
40145 token = cp_parser_require_keyword (parser, keyword,
40146 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40147 : RT_TRANSACTION_RELAXED));
40148 gcc_assert (token != NULL);
40149
40150 if (keyword == RID_TRANSACTION_RELAXED)
40151 new_in |= TM_STMT_ATTR_RELAXED;
40152 else
40153 {
40154 attrs = cp_parser_txn_attribute_opt (parser);
40155 if (attrs)
40156 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40157 }
40158
40159 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40160
40161 parser->in_transaction = new_in;
40162
40163 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40164 cp_parser_function_try_block (parser);
40165 else
40166 cp_parser_ctor_initializer_opt_and_function_body
40167 (parser, /*in_function_try_block=*/false);
40168
40169 parser->in_transaction = old_in;
40170
40171 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40172 }
40173
40174 /* Parse a __transaction_cancel statement.
40175
40176 cancel-statement:
40177 __transaction_cancel txn-attribute[opt] ;
40178 __transaction_cancel txn-attribute[opt] throw-expression ;
40179
40180 ??? Cancel and throw is not yet implemented. */
40181
40182 static tree
40183 cp_parser_transaction_cancel (cp_parser *parser)
40184 {
40185 cp_token *token;
40186 bool is_outer = false;
40187 tree stmt, attrs;
40188
40189 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40190 RT_TRANSACTION_CANCEL);
40191 gcc_assert (token != NULL);
40192
40193 attrs = cp_parser_txn_attribute_opt (parser);
40194 if (attrs)
40195 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40196
40197 /* ??? Parse cancel-and-throw here. */
40198
40199 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40200
40201 if (!flag_tm)
40202 {
40203 error_at (token->location, "%<__transaction_cancel%> without "
40204 "transactional memory support enabled");
40205 return error_mark_node;
40206 }
40207 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40208 {
40209 error_at (token->location, "%<__transaction_cancel%> within a "
40210 "%<__transaction_relaxed%>");
40211 return error_mark_node;
40212 }
40213 else if (is_outer)
40214 {
40215 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40216 && !is_tm_may_cancel_outer (current_function_decl))
40217 {
40218 error_at (token->location, "outer %<__transaction_cancel%> not "
40219 "within outer %<__transaction_atomic%>");
40220 error_at (token->location,
40221 " or a %<transaction_may_cancel_outer%> function");
40222 return error_mark_node;
40223 }
40224 }
40225 else if (parser->in_transaction == 0)
40226 {
40227 error_at (token->location, "%<__transaction_cancel%> not within "
40228 "%<__transaction_atomic%>");
40229 return error_mark_node;
40230 }
40231
40232 stmt = build_tm_abort_call (token->location, is_outer);
40233 add_stmt (stmt);
40234
40235 return stmt;
40236 }
40237 \f
40238 /* The parser. */
40239
40240 static GTY (()) cp_parser *the_parser;
40241
40242 \f
40243 /* Special handling for the first token or line in the file. The first
40244 thing in the file might be #pragma GCC pch_preprocess, which loads a
40245 PCH file, which is a GC collection point. So we need to handle this
40246 first pragma without benefit of an existing lexer structure.
40247
40248 Always returns one token to the caller in *FIRST_TOKEN. This is
40249 either the true first token of the file, or the first token after
40250 the initial pragma. */
40251
40252 static void
40253 cp_parser_initial_pragma (cp_token *first_token)
40254 {
40255 tree name = NULL;
40256
40257 cp_lexer_get_preprocessor_token (NULL, first_token);
40258 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40259 return;
40260
40261 cp_lexer_get_preprocessor_token (NULL, first_token);
40262 if (first_token->type == CPP_STRING)
40263 {
40264 name = first_token->u.value;
40265
40266 cp_lexer_get_preprocessor_token (NULL, first_token);
40267 if (first_token->type != CPP_PRAGMA_EOL)
40268 error_at (first_token->location,
40269 "junk at end of %<#pragma GCC pch_preprocess%>");
40270 }
40271 else
40272 error_at (first_token->location, "expected string literal");
40273
40274 /* Skip to the end of the pragma. */
40275 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40276 cp_lexer_get_preprocessor_token (NULL, first_token);
40277
40278 /* Now actually load the PCH file. */
40279 if (name)
40280 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40281
40282 /* Read one more token to return to our caller. We have to do this
40283 after reading the PCH file in, since its pointers have to be
40284 live. */
40285 cp_lexer_get_preprocessor_token (NULL, first_token);
40286 }
40287
40288 /* Parse a pragma GCC ivdep. */
40289
40290 static bool
40291 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40292 {
40293 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40294 return true;
40295 }
40296
40297 /* Parse a pragma GCC unroll. */
40298
40299 static unsigned short
40300 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40301 {
40302 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40303 tree expr = cp_parser_constant_expression (parser);
40304 unsigned short unroll;
40305 expr = maybe_constant_value (expr);
40306 HOST_WIDE_INT lunroll = 0;
40307 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40308 || TREE_CODE (expr) != INTEGER_CST
40309 || (lunroll = tree_to_shwi (expr)) < 0
40310 || lunroll >= USHRT_MAX)
40311 {
40312 error_at (location, "%<#pragma GCC unroll%> requires an"
40313 " assignment-expression that evaluates to a non-negative"
40314 " integral constant less than %u", USHRT_MAX);
40315 unroll = 0;
40316 }
40317 else
40318 {
40319 unroll = (unsigned short)lunroll;
40320 if (unroll == 0)
40321 unroll = 1;
40322 }
40323 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40324 return unroll;
40325 }
40326
40327 /* Normal parsing of a pragma token. Here we can (and must) use the
40328 regular lexer. */
40329
40330 static bool
40331 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40332 {
40333 cp_token *pragma_tok;
40334 unsigned int id;
40335 tree stmt;
40336 bool ret;
40337
40338 pragma_tok = cp_lexer_consume_token (parser->lexer);
40339 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40340 parser->lexer->in_pragma = true;
40341
40342 id = cp_parser_pragma_kind (pragma_tok);
40343 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40344 cp_ensure_no_omp_declare_simd (parser);
40345 switch (id)
40346 {
40347 case PRAGMA_GCC_PCH_PREPROCESS:
40348 error_at (pragma_tok->location,
40349 "%<#pragma GCC pch_preprocess%> must be first");
40350 break;
40351
40352 case PRAGMA_OMP_BARRIER:
40353 switch (context)
40354 {
40355 case pragma_compound:
40356 cp_parser_omp_barrier (parser, pragma_tok);
40357 return false;
40358 case pragma_stmt:
40359 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40360 "used in compound statements", "omp barrier");
40361 break;
40362 default:
40363 goto bad_stmt;
40364 }
40365 break;
40366
40367 case PRAGMA_OMP_DEPOBJ:
40368 switch (context)
40369 {
40370 case pragma_compound:
40371 cp_parser_omp_depobj (parser, pragma_tok);
40372 return false;
40373 case pragma_stmt:
40374 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40375 "used in compound statements", "omp depobj");
40376 break;
40377 default:
40378 goto bad_stmt;
40379 }
40380 break;
40381
40382 case PRAGMA_OMP_FLUSH:
40383 switch (context)
40384 {
40385 case pragma_compound:
40386 cp_parser_omp_flush (parser, pragma_tok);
40387 return false;
40388 case pragma_stmt:
40389 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40390 "used in compound statements", "omp flush");
40391 break;
40392 default:
40393 goto bad_stmt;
40394 }
40395 break;
40396
40397 case PRAGMA_OMP_TASKWAIT:
40398 switch (context)
40399 {
40400 case pragma_compound:
40401 cp_parser_omp_taskwait (parser, pragma_tok);
40402 return false;
40403 case pragma_stmt:
40404 error_at (pragma_tok->location,
40405 "%<#pragma %s%> may only be used in compound statements",
40406 "omp taskwait");
40407 break;
40408 default:
40409 goto bad_stmt;
40410 }
40411 break;
40412
40413 case PRAGMA_OMP_TASKYIELD:
40414 switch (context)
40415 {
40416 case pragma_compound:
40417 cp_parser_omp_taskyield (parser, pragma_tok);
40418 return false;
40419 case pragma_stmt:
40420 error_at (pragma_tok->location,
40421 "%<#pragma %s%> may only be used in compound statements",
40422 "omp taskyield");
40423 break;
40424 default:
40425 goto bad_stmt;
40426 }
40427 break;
40428
40429 case PRAGMA_OMP_CANCEL:
40430 switch (context)
40431 {
40432 case pragma_compound:
40433 cp_parser_omp_cancel (parser, pragma_tok);
40434 return false;
40435 case pragma_stmt:
40436 error_at (pragma_tok->location,
40437 "%<#pragma %s%> may only be used in compound statements",
40438 "omp cancel");
40439 break;
40440 default:
40441 goto bad_stmt;
40442 }
40443 break;
40444
40445 case PRAGMA_OMP_CANCELLATION_POINT:
40446 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40447 return false;
40448
40449 case PRAGMA_OMP_THREADPRIVATE:
40450 cp_parser_omp_threadprivate (parser, pragma_tok);
40451 return false;
40452
40453 case PRAGMA_OMP_DECLARE:
40454 return cp_parser_omp_declare (parser, pragma_tok, context);
40455
40456 case PRAGMA_OACC_DECLARE:
40457 cp_parser_oacc_declare (parser, pragma_tok);
40458 return false;
40459
40460 case PRAGMA_OACC_ENTER_DATA:
40461 if (context == pragma_stmt)
40462 {
40463 error_at (pragma_tok->location,
40464 "%<#pragma %s%> may only be used in compound statements",
40465 "acc enter data");
40466 break;
40467 }
40468 else if (context != pragma_compound)
40469 goto bad_stmt;
40470 cp_parser_omp_construct (parser, pragma_tok, if_p);
40471 return true;
40472
40473 case PRAGMA_OACC_EXIT_DATA:
40474 if (context == pragma_stmt)
40475 {
40476 error_at (pragma_tok->location,
40477 "%<#pragma %s%> may only be used in compound statements",
40478 "acc exit data");
40479 break;
40480 }
40481 else if (context != pragma_compound)
40482 goto bad_stmt;
40483 cp_parser_omp_construct (parser, pragma_tok, if_p);
40484 return true;
40485
40486 case PRAGMA_OACC_ROUTINE:
40487 if (context != pragma_external)
40488 {
40489 error_at (pragma_tok->location,
40490 "%<#pragma acc routine%> must be at file scope");
40491 break;
40492 }
40493 cp_parser_oacc_routine (parser, pragma_tok, context);
40494 return false;
40495
40496 case PRAGMA_OACC_UPDATE:
40497 if (context == pragma_stmt)
40498 {
40499 error_at (pragma_tok->location,
40500 "%<#pragma %s%> may only be used in compound statements",
40501 "acc update");
40502 break;
40503 }
40504 else if (context != pragma_compound)
40505 goto bad_stmt;
40506 cp_parser_omp_construct (parser, pragma_tok, if_p);
40507 return true;
40508
40509 case PRAGMA_OACC_WAIT:
40510 if (context == pragma_stmt)
40511 {
40512 error_at (pragma_tok->location,
40513 "%<#pragma %s%> may only be used in compound statements",
40514 "acc wait");
40515 break;
40516 }
40517 else if (context != pragma_compound)
40518 goto bad_stmt;
40519 cp_parser_omp_construct (parser, pragma_tok, if_p);
40520 return true;
40521
40522 case PRAGMA_OACC_ATOMIC:
40523 case PRAGMA_OACC_CACHE:
40524 case PRAGMA_OACC_DATA:
40525 case PRAGMA_OACC_HOST_DATA:
40526 case PRAGMA_OACC_KERNELS:
40527 case PRAGMA_OACC_PARALLEL:
40528 case PRAGMA_OACC_LOOP:
40529 case PRAGMA_OMP_ATOMIC:
40530 case PRAGMA_OMP_CRITICAL:
40531 case PRAGMA_OMP_DISTRIBUTE:
40532 case PRAGMA_OMP_FOR:
40533 case PRAGMA_OMP_MASTER:
40534 case PRAGMA_OMP_PARALLEL:
40535 case PRAGMA_OMP_SECTIONS:
40536 case PRAGMA_OMP_SIMD:
40537 case PRAGMA_OMP_SINGLE:
40538 case PRAGMA_OMP_TASK:
40539 case PRAGMA_OMP_TASKGROUP:
40540 case PRAGMA_OMP_TASKLOOP:
40541 case PRAGMA_OMP_TEAMS:
40542 if (context != pragma_stmt && context != pragma_compound)
40543 goto bad_stmt;
40544 stmt = push_omp_privatization_clauses (false);
40545 cp_parser_omp_construct (parser, pragma_tok, if_p);
40546 pop_omp_privatization_clauses (stmt);
40547 return true;
40548
40549 case PRAGMA_OMP_REQUIRES:
40550 return cp_parser_omp_requires (parser, pragma_tok);
40551
40552 case PRAGMA_OMP_ORDERED:
40553 if (context != pragma_stmt && context != pragma_compound)
40554 goto bad_stmt;
40555 stmt = push_omp_privatization_clauses (false);
40556 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40557 pop_omp_privatization_clauses (stmt);
40558 return ret;
40559
40560 case PRAGMA_OMP_TARGET:
40561 if (context != pragma_stmt && context != pragma_compound)
40562 goto bad_stmt;
40563 stmt = push_omp_privatization_clauses (false);
40564 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40565 pop_omp_privatization_clauses (stmt);
40566 return ret;
40567
40568 case PRAGMA_OMP_END_DECLARE_TARGET:
40569 cp_parser_omp_end_declare_target (parser, pragma_tok);
40570 return false;
40571
40572 case PRAGMA_OMP_SECTION:
40573 error_at (pragma_tok->location,
40574 "%<#pragma omp section%> may only be used in "
40575 "%<#pragma omp sections%> construct");
40576 break;
40577
40578 case PRAGMA_IVDEP:
40579 {
40580 if (context == pragma_external)
40581 {
40582 error_at (pragma_tok->location,
40583 "%<#pragma GCC ivdep%> must be inside a function");
40584 break;
40585 }
40586 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40587 unsigned short unroll;
40588 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40589 if (tok->type == CPP_PRAGMA
40590 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40591 {
40592 tok = cp_lexer_consume_token (parser->lexer);
40593 unroll = cp_parser_pragma_unroll (parser, tok);
40594 tok = cp_lexer_peek_token (the_parser->lexer);
40595 }
40596 else
40597 unroll = 0;
40598 if (tok->type != CPP_KEYWORD
40599 || (tok->keyword != RID_FOR
40600 && tok->keyword != RID_WHILE
40601 && tok->keyword != RID_DO))
40602 {
40603 cp_parser_error (parser, "for, while or do statement expected");
40604 return false;
40605 }
40606 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40607 return true;
40608 }
40609
40610 case PRAGMA_UNROLL:
40611 {
40612 if (context == pragma_external)
40613 {
40614 error_at (pragma_tok->location,
40615 "%<#pragma GCC unroll%> must be inside a function");
40616 break;
40617 }
40618 const unsigned short unroll
40619 = cp_parser_pragma_unroll (parser, pragma_tok);
40620 bool ivdep;
40621 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40622 if (tok->type == CPP_PRAGMA
40623 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40624 {
40625 tok = cp_lexer_consume_token (parser->lexer);
40626 ivdep = cp_parser_pragma_ivdep (parser, tok);
40627 tok = cp_lexer_peek_token (the_parser->lexer);
40628 }
40629 else
40630 ivdep = false;
40631 if (tok->type != CPP_KEYWORD
40632 || (tok->keyword != RID_FOR
40633 && tok->keyword != RID_WHILE
40634 && tok->keyword != RID_DO))
40635 {
40636 cp_parser_error (parser, "for, while or do statement expected");
40637 return false;
40638 }
40639 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40640 return true;
40641 }
40642
40643 default:
40644 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
40645 c_invoke_pragma_handler (id);
40646 break;
40647
40648 bad_stmt:
40649 cp_parser_error (parser, "expected declaration specifiers");
40650 break;
40651 }
40652
40653 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40654 return false;
40655 }
40656
40657 /* The interface the pragma parsers have to the lexer. */
40658
40659 enum cpp_ttype
40660 pragma_lex (tree *value, location_t *loc)
40661 {
40662 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40663 enum cpp_ttype ret = tok->type;
40664
40665 *value = tok->u.value;
40666 if (loc)
40667 *loc = tok->location;
40668
40669 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
40670 ret = CPP_EOF;
40671 else if (ret == CPP_STRING)
40672 *value = cp_parser_string_literal (the_parser, false, false);
40673 else
40674 {
40675 if (ret == CPP_KEYWORD)
40676 ret = CPP_NAME;
40677 cp_lexer_consume_token (the_parser->lexer);
40678 }
40679
40680 return ret;
40681 }
40682
40683 \f
40684 /* External interface. */
40685
40686 /* Parse one entire translation unit. */
40687
40688 void
40689 c_parse_file (void)
40690 {
40691 static bool already_called = false;
40692
40693 if (already_called)
40694 fatal_error (input_location,
40695 "inter-module optimizations not implemented for C++");
40696 already_called = true;
40697
40698 the_parser = cp_parser_new ();
40699 push_deferring_access_checks (flag_access_control
40700 ? dk_no_deferred : dk_no_check);
40701 cp_parser_translation_unit (the_parser);
40702 the_parser = NULL;
40703
40704 finish_translation_unit ();
40705 }
40706
40707 /* Create an identifier for a generic parameter type (a synthesized
40708 template parameter implied by `auto' or a concept identifier). */
40709
40710 static GTY(()) int generic_parm_count;
40711 static tree
40712 make_generic_type_name ()
40713 {
40714 char buf[32];
40715 sprintf (buf, "auto:%d", ++generic_parm_count);
40716 return get_identifier (buf);
40717 }
40718
40719 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40720 (creating a new template parameter list if necessary). Returns the newly
40721 created template type parm. */
40722
40723 static tree
40724 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
40725 {
40726 gcc_assert (current_binding_level->kind == sk_function_parms);
40727
40728 /* Before committing to modifying any scope, if we're in an
40729 implicit template scope, and we're trying to synthesize a
40730 constrained parameter, try to find a previous parameter with
40731 the same name. This is the same-type rule for abbreviated
40732 function templates.
40733
40734 NOTE: We can generate implicit parameters when tentatively
40735 parsing a nested name specifier, only to reject that parse
40736 later. However, matching the same template-id as part of a
40737 direct-declarator should generate an identical template
40738 parameter, so this rule will merge them. */
40739 if (parser->implicit_template_scope && constr)
40740 {
40741 tree t = parser->implicit_template_parms;
40742 while (t)
40743 {
40744 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
40745 {
40746 tree d = TREE_VALUE (t);
40747 if (TREE_CODE (d) == PARM_DECL)
40748 /* Return the TEMPLATE_PARM_INDEX. */
40749 d = DECL_INITIAL (d);
40750 return d;
40751 }
40752 t = TREE_CHAIN (t);
40753 }
40754 }
40755
40756 /* We are either continuing a function template that already contains implicit
40757 template parameters, creating a new fully-implicit function template, or
40758 extending an existing explicit function template with implicit template
40759 parameters. */
40760
40761 cp_binding_level *const entry_scope = current_binding_level;
40762
40763 bool become_template = false;
40764 cp_binding_level *parent_scope = 0;
40765
40766 if (parser->implicit_template_scope)
40767 {
40768 gcc_assert (parser->implicit_template_parms);
40769
40770 current_binding_level = parser->implicit_template_scope;
40771 }
40772 else
40773 {
40774 /* Roll back to the existing template parameter scope (in the case of
40775 extending an explicit function template) or introduce a new template
40776 parameter scope ahead of the function parameter scope (or class scope
40777 in the case of out-of-line member definitions). The function scope is
40778 added back after template parameter synthesis below. */
40779
40780 cp_binding_level *scope = entry_scope;
40781
40782 while (scope->kind == sk_function_parms)
40783 {
40784 parent_scope = scope;
40785 scope = scope->level_chain;
40786 }
40787 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
40788 {
40789 /* If not defining a class, then any class scope is a scope level in
40790 an out-of-line member definition. In this case simply wind back
40791 beyond the first such scope to inject the template parameter list.
40792 Otherwise wind back to the class being defined. The latter can
40793 occur in class member friend declarations such as:
40794
40795 class A {
40796 void foo (auto);
40797 };
40798 class B {
40799 friend void A::foo (auto);
40800 };
40801
40802 The template parameter list synthesized for the friend declaration
40803 must be injected in the scope of 'B'. This can also occur in
40804 erroneous cases such as:
40805
40806 struct A {
40807 struct B {
40808 void foo (auto);
40809 };
40810 void B::foo (auto) {}
40811 };
40812
40813 Here the attempted definition of 'B::foo' within 'A' is ill-formed
40814 but, nevertheless, the template parameter list synthesized for the
40815 declarator should be injected into the scope of 'A' as if the
40816 ill-formed template was specified explicitly. */
40817
40818 while (scope->kind == sk_class && !scope->defining_class_p)
40819 {
40820 parent_scope = scope;
40821 scope = scope->level_chain;
40822 }
40823 }
40824
40825 current_binding_level = scope;
40826
40827 if (scope->kind != sk_template_parms
40828 || !function_being_declared_is_template_p (parser))
40829 {
40830 /* Introduce a new template parameter list for implicit template
40831 parameters. */
40832
40833 become_template = true;
40834
40835 parser->implicit_template_scope
40836 = begin_scope (sk_template_parms, NULL);
40837
40838 ++processing_template_decl;
40839
40840 parser->fully_implicit_function_template_p = true;
40841 ++parser->num_template_parameter_lists;
40842 }
40843 else
40844 {
40845 /* Synthesize implicit template parameters at the end of the explicit
40846 template parameter list. */
40847
40848 gcc_assert (current_template_parms);
40849
40850 parser->implicit_template_scope = scope;
40851
40852 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40853 parser->implicit_template_parms
40854 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
40855 }
40856 }
40857
40858 /* Synthesize a new template parameter and track the current template
40859 parameter chain with implicit_template_parms. */
40860
40861 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
40862 tree synth_id = make_generic_type_name ();
40863 tree synth_tmpl_parm;
40864 bool non_type = false;
40865
40866 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
40867 synth_tmpl_parm
40868 = finish_template_type_parm (class_type_node, synth_id);
40869 else if (TREE_CODE (proto) == TEMPLATE_DECL)
40870 synth_tmpl_parm
40871 = finish_constrained_template_template_parm (proto, synth_id);
40872 else
40873 {
40874 synth_tmpl_parm = copy_decl (proto);
40875 DECL_NAME (synth_tmpl_parm) = synth_id;
40876 non_type = true;
40877 }
40878
40879 // Attach the constraint to the parm before processing.
40880 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
40881 TREE_TYPE (node) = constr;
40882 tree new_parm
40883 = process_template_parm (parser->implicit_template_parms,
40884 input_location,
40885 node,
40886 /*non_type=*/non_type,
40887 /*param_pack=*/false);
40888
40889 // Chain the new parameter to the list of implicit parameters.
40890 if (parser->implicit_template_parms)
40891 parser->implicit_template_parms
40892 = TREE_CHAIN (parser->implicit_template_parms);
40893 else
40894 parser->implicit_template_parms = new_parm;
40895
40896 tree new_decl = get_local_decls ();
40897 if (non_type)
40898 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
40899 new_decl = DECL_INITIAL (new_decl);
40900
40901 /* If creating a fully implicit function template, start the new implicit
40902 template parameter list with this synthesized type, otherwise grow the
40903 current template parameter list. */
40904
40905 if (become_template)
40906 {
40907 parent_scope->level_chain = current_binding_level;
40908
40909 tree new_parms = make_tree_vec (1);
40910 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
40911 current_template_parms = tree_cons (size_int (processing_template_decl),
40912 new_parms, current_template_parms);
40913 }
40914 else
40915 {
40916 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
40917 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
40918 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
40919 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
40920 }
40921
40922 // If the new parameter was constrained, we need to add that to the
40923 // constraints in the template parameter list.
40924 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
40925 {
40926 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
40927 reqs = conjoin_constraints (reqs, req);
40928 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
40929 }
40930
40931 current_binding_level = entry_scope;
40932
40933 return new_decl;
40934 }
40935
40936 /* Finish the declaration of a fully implicit function template. Such a
40937 template has no explicit template parameter list so has not been through the
40938 normal template head and tail processing. synthesize_implicit_template_parm
40939 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
40940 provided if the declaration is a class member such that its template
40941 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
40942 form is returned. Otherwise NULL_TREE is returned. */
40943
40944 static tree
40945 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
40946 {
40947 gcc_assert (parser->fully_implicit_function_template_p);
40948
40949 if (member_decl_opt && member_decl_opt != error_mark_node
40950 && DECL_VIRTUAL_P (member_decl_opt))
40951 {
40952 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
40953 "implicit templates may not be %<virtual%>");
40954 DECL_VIRTUAL_P (member_decl_opt) = false;
40955 }
40956
40957 if (member_decl_opt)
40958 member_decl_opt = finish_member_template_decl (member_decl_opt);
40959 end_template_decl ();
40960
40961 parser->fully_implicit_function_template_p = false;
40962 parser->implicit_template_parms = 0;
40963 parser->implicit_template_scope = 0;
40964 --parser->num_template_parameter_lists;
40965
40966 return member_decl_opt;
40967 }
40968
40969 /* Like finish_fully_implicit_template, but to be used in error
40970 recovery, rearranging scopes so that we restore the state we had
40971 before synthesize_implicit_template_parm inserted the implement
40972 template parms scope. */
40973
40974 static void
40975 abort_fully_implicit_template (cp_parser *parser)
40976 {
40977 cp_binding_level *return_to_scope = current_binding_level;
40978
40979 if (parser->implicit_template_scope
40980 && return_to_scope != parser->implicit_template_scope)
40981 {
40982 cp_binding_level *child = return_to_scope;
40983 for (cp_binding_level *scope = child->level_chain;
40984 scope != parser->implicit_template_scope;
40985 scope = child->level_chain)
40986 child = scope;
40987 child->level_chain = parser->implicit_template_scope->level_chain;
40988 parser->implicit_template_scope->level_chain = return_to_scope;
40989 current_binding_level = parser->implicit_template_scope;
40990 }
40991 else
40992 return_to_scope = return_to_scope->level_chain;
40993
40994 finish_fully_implicit_template (parser, NULL);
40995
40996 gcc_assert (current_binding_level == return_to_scope);
40997 }
40998
40999 /* Helper function for diagnostics that have complained about things
41000 being used with 'extern "C"' linkage.
41001
41002 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41003
41004 void
41005 maybe_show_extern_c_location (void)
41006 {
41007 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
41008 inform (the_parser->innermost_linkage_specification_location,
41009 "%<extern \"C\"%> linkage started here");
41010 }
41011
41012 #include "gt-cp-parser.h"