]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
C++: more location wrapper nodes (PR c++/43064, PR c++/43486)
[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 /* When parsing a decl-specifier-seq, allow missing typename. */
1796 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20
1797 };
1798
1799 /* This type is used for parameters and variables which hold
1800 combinations of the above flags. */
1801 typedef int cp_parser_flags;
1802
1803 /* The different kinds of declarators we want to parse. */
1804
1805 enum cp_parser_declarator_kind
1806 {
1807 /* We want an abstract declarator. */
1808 CP_PARSER_DECLARATOR_ABSTRACT,
1809 /* We want a named declarator. */
1810 CP_PARSER_DECLARATOR_NAMED,
1811 /* We don't mind, but the name must be an unqualified-id. */
1812 CP_PARSER_DECLARATOR_EITHER
1813 };
1814
1815 /* The precedence values used to parse binary expressions. The minimum value
1816 of PREC must be 1, because zero is reserved to quickly discriminate
1817 binary operators from other tokens. */
1818
1819 enum cp_parser_prec
1820 {
1821 PREC_NOT_OPERATOR,
1822 PREC_LOGICAL_OR_EXPRESSION,
1823 PREC_LOGICAL_AND_EXPRESSION,
1824 PREC_INCLUSIVE_OR_EXPRESSION,
1825 PREC_EXCLUSIVE_OR_EXPRESSION,
1826 PREC_AND_EXPRESSION,
1827 PREC_EQUALITY_EXPRESSION,
1828 PREC_RELATIONAL_EXPRESSION,
1829 PREC_SHIFT_EXPRESSION,
1830 PREC_ADDITIVE_EXPRESSION,
1831 PREC_MULTIPLICATIVE_EXPRESSION,
1832 PREC_PM_EXPRESSION,
1833 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1834 };
1835
1836 /* A mapping from a token type to a corresponding tree node type, with a
1837 precedence value. */
1838
1839 struct cp_parser_binary_operations_map_node
1840 {
1841 /* The token type. */
1842 enum cpp_ttype token_type;
1843 /* The corresponding tree code. */
1844 enum tree_code tree_type;
1845 /* The precedence of this operator. */
1846 enum cp_parser_prec prec;
1847 };
1848
1849 struct cp_parser_expression_stack_entry
1850 {
1851 /* Left hand side of the binary operation we are currently
1852 parsing. */
1853 cp_expr lhs;
1854 /* Original tree code for left hand side, if it was a binary
1855 expression itself (used for -Wparentheses). */
1856 enum tree_code lhs_type;
1857 /* Tree code for the binary operation we are parsing. */
1858 enum tree_code tree_type;
1859 /* Precedence of the binary operation we are parsing. */
1860 enum cp_parser_prec prec;
1861 /* Location of the binary operation we are parsing. */
1862 location_t loc;
1863 };
1864
1865 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1866 entries because precedence levels on the stack are monotonically
1867 increasing. */
1868 typedef struct cp_parser_expression_stack_entry
1869 cp_parser_expression_stack[NUM_PREC_VALUES];
1870
1871 /* Prototypes. */
1872
1873 /* Constructors and destructors. */
1874
1875 static cp_parser_context *cp_parser_context_new
1876 (cp_parser_context *);
1877
1878 /* Class variables. */
1879
1880 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1881
1882 /* The operator-precedence table used by cp_parser_binary_expression.
1883 Transformed into an associative array (binops_by_token) by
1884 cp_parser_new. */
1885
1886 static const cp_parser_binary_operations_map_node binops[] = {
1887 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1888 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1889
1890 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1891 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1892 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1893
1894 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1895 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1896
1897 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1898 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1899
1900 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1901 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1902 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1903 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1904
1905 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1906 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1907
1908 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1909
1910 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1911
1912 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1913
1914 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1915
1916 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1917 };
1918
1919 /* The same as binops, but initialized by cp_parser_new so that
1920 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1921 for speed. */
1922 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1923
1924 /* Constructors and destructors. */
1925
1926 /* Construct a new context. The context below this one on the stack
1927 is given by NEXT. */
1928
1929 static cp_parser_context *
1930 cp_parser_context_new (cp_parser_context* next)
1931 {
1932 cp_parser_context *context;
1933
1934 /* Allocate the storage. */
1935 if (cp_parser_context_free_list != NULL)
1936 {
1937 /* Pull the first entry from the free list. */
1938 context = cp_parser_context_free_list;
1939 cp_parser_context_free_list = context->next;
1940 memset (context, 0, sizeof (*context));
1941 }
1942 else
1943 context = ggc_cleared_alloc<cp_parser_context> ();
1944
1945 /* No errors have occurred yet in this context. */
1946 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1947 /* If this is not the bottommost context, copy information that we
1948 need from the previous context. */
1949 if (next)
1950 {
1951 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1952 expression, then we are parsing one in this context, too. */
1953 context->object_type = next->object_type;
1954 /* Thread the stack. */
1955 context->next = next;
1956 }
1957
1958 return context;
1959 }
1960
1961 /* Managing the unparsed function queues. */
1962
1963 #define unparsed_funs_with_default_args \
1964 parser->unparsed_queues->last ().funs_with_default_args
1965 #define unparsed_funs_with_definitions \
1966 parser->unparsed_queues->last ().funs_with_definitions
1967 #define unparsed_nsdmis \
1968 parser->unparsed_queues->last ().nsdmis
1969 #define unparsed_classes \
1970 parser->unparsed_queues->last ().classes
1971
1972 static void
1973 push_unparsed_function_queues (cp_parser *parser)
1974 {
1975 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1976 vec_safe_push (parser->unparsed_queues, e);
1977 }
1978
1979 static void
1980 pop_unparsed_function_queues (cp_parser *parser)
1981 {
1982 release_tree_vector (unparsed_funs_with_definitions);
1983 parser->unparsed_queues->pop ();
1984 }
1985
1986 /* Prototypes. */
1987
1988 /* Constructors and destructors. */
1989
1990 static cp_parser *cp_parser_new
1991 (void);
1992
1993 /* Routines to parse various constructs.
1994
1995 Those that return `tree' will return the error_mark_node (rather
1996 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1997 Sometimes, they will return an ordinary node if error-recovery was
1998 attempted, even though a parse error occurred. So, to check
1999 whether or not a parse error occurred, you should always use
2000 cp_parser_error_occurred. If the construct is optional (indicated
2001 either by an `_opt' in the name of the function that does the
2002 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2003 the construct is not present. */
2004
2005 /* Lexical conventions [gram.lex] */
2006
2007 static cp_expr cp_parser_identifier
2008 (cp_parser *);
2009 static cp_expr cp_parser_string_literal
2010 (cp_parser *, bool, bool, bool);
2011 static cp_expr cp_parser_userdef_char_literal
2012 (cp_parser *);
2013 static tree cp_parser_userdef_string_literal
2014 (tree);
2015 static cp_expr cp_parser_userdef_numeric_literal
2016 (cp_parser *);
2017
2018 /* Basic concepts [gram.basic] */
2019
2020 static void cp_parser_translation_unit (cp_parser *);
2021
2022 /* Expressions [gram.expr] */
2023
2024 static cp_expr cp_parser_primary_expression
2025 (cp_parser *, bool, bool, bool, cp_id_kind *);
2026 static cp_expr cp_parser_id_expression
2027 (cp_parser *, bool, bool, bool *, bool, bool);
2028 static cp_expr cp_parser_unqualified_id
2029 (cp_parser *, bool, bool, bool, bool);
2030 static tree cp_parser_nested_name_specifier_opt
2031 (cp_parser *, bool, bool, bool, bool, bool = false);
2032 static tree cp_parser_nested_name_specifier
2033 (cp_parser *, bool, bool, bool, bool);
2034 static tree cp_parser_qualifying_entity
2035 (cp_parser *, bool, bool, bool, bool, bool);
2036 static cp_expr cp_parser_postfix_expression
2037 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2038 static tree cp_parser_postfix_open_square_expression
2039 (cp_parser *, tree, bool, bool);
2040 static tree cp_parser_postfix_dot_deref_expression
2041 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2042 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2043 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2044 bool = false);
2045 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2046 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2047 static void cp_parser_pseudo_destructor_name
2048 (cp_parser *, tree, tree *, tree *);
2049 static cp_expr cp_parser_unary_expression
2050 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2051 static enum tree_code cp_parser_unary_operator
2052 (cp_token *);
2053 static tree cp_parser_has_attribute_expression
2054 (cp_parser *);
2055 static tree cp_parser_new_expression
2056 (cp_parser *);
2057 static vec<tree, va_gc> *cp_parser_new_placement
2058 (cp_parser *);
2059 static tree cp_parser_new_type_id
2060 (cp_parser *, tree *);
2061 static cp_declarator *cp_parser_new_declarator_opt
2062 (cp_parser *);
2063 static cp_declarator *cp_parser_direct_new_declarator
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_initializer
2066 (cp_parser *);
2067 static tree cp_parser_delete_expression
2068 (cp_parser *);
2069 static cp_expr cp_parser_cast_expression
2070 (cp_parser *, bool, bool, bool, cp_id_kind *);
2071 static cp_expr cp_parser_binary_expression
2072 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2073 static tree cp_parser_question_colon_clause
2074 (cp_parser *, cp_expr);
2075 static cp_expr cp_parser_assignment_expression
2076 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2077 static enum tree_code cp_parser_assignment_operator_opt
2078 (cp_parser *);
2079 static cp_expr cp_parser_expression
2080 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2081 static cp_expr cp_parser_constant_expression
2082 (cp_parser *, bool = false, bool * = NULL, bool = false);
2083 static cp_expr cp_parser_builtin_offsetof
2084 (cp_parser *);
2085 static cp_expr cp_parser_lambda_expression
2086 (cp_parser *);
2087 static void cp_parser_lambda_introducer
2088 (cp_parser *, tree);
2089 static bool cp_parser_lambda_declarator_opt
2090 (cp_parser *, tree);
2091 static void cp_parser_lambda_body
2092 (cp_parser *, tree);
2093
2094 /* Statements [gram.stmt.stmt] */
2095
2096 static void cp_parser_statement
2097 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2098 static void cp_parser_label_for_labeled_statement
2099 (cp_parser *, tree);
2100 static tree cp_parser_expression_statement
2101 (cp_parser *, tree);
2102 static tree cp_parser_compound_statement
2103 (cp_parser *, tree, int, bool);
2104 static void cp_parser_statement_seq_opt
2105 (cp_parser *, tree);
2106 static tree cp_parser_selection_statement
2107 (cp_parser *, bool *, vec<tree> *);
2108 static tree cp_parser_condition
2109 (cp_parser *);
2110 static tree cp_parser_iteration_statement
2111 (cp_parser *, bool *, bool, unsigned short);
2112 static bool cp_parser_init_statement
2113 (cp_parser *, tree *decl);
2114 static tree cp_parser_for
2115 (cp_parser *, bool, unsigned short);
2116 static tree cp_parser_c_for
2117 (cp_parser *, tree, tree, bool, unsigned short);
2118 static tree cp_parser_range_for
2119 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2120 static void do_range_for_auto_deduction
2121 (tree, tree);
2122 static tree cp_parser_perform_range_for_lookup
2123 (tree, tree *, tree *);
2124 static tree cp_parser_range_for_member_function
2125 (tree, tree);
2126 static tree cp_parser_jump_statement
2127 (cp_parser *);
2128 static void cp_parser_declaration_statement
2129 (cp_parser *);
2130
2131 static tree cp_parser_implicitly_scoped_statement
2132 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2133 static void cp_parser_already_scoped_statement
2134 (cp_parser *, bool *, const token_indent_info &);
2135
2136 /* Declarations [gram.dcl.dcl] */
2137
2138 static void cp_parser_declaration_seq_opt
2139 (cp_parser *);
2140 static void cp_parser_declaration
2141 (cp_parser *);
2142 static void cp_parser_toplevel_declaration
2143 (cp_parser *);
2144 static void cp_parser_block_declaration
2145 (cp_parser *, bool);
2146 static void cp_parser_simple_declaration
2147 (cp_parser *, bool, tree *);
2148 static void cp_parser_decl_specifier_seq
2149 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2150 static tree cp_parser_storage_class_specifier_opt
2151 (cp_parser *);
2152 static tree cp_parser_function_specifier_opt
2153 (cp_parser *, cp_decl_specifier_seq *);
2154 static tree cp_parser_type_specifier
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2156 int *, bool *);
2157 static tree cp_parser_simple_type_specifier
2158 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2159 static tree cp_parser_type_name
2160 (cp_parser *, bool);
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_parser_flags, cp_decl_specifier_seq *,
2202 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2203 location_t *, tree *);
2204 static cp_declarator *cp_parser_declarator
2205 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2206 bool, bool);
2207 static cp_declarator *cp_parser_direct_declarator
2208 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool);
2209 static enum tree_code cp_parser_ptr_operator
2210 (cp_parser *, tree *, cp_cv_quals *, tree *);
2211 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2212 (cp_parser *);
2213 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2214 (cp_parser *);
2215 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2216 (cp_parser *);
2217 static tree cp_parser_tx_qualifier_opt
2218 (cp_parser *);
2219 static tree cp_parser_late_return_type_opt
2220 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2221 static tree cp_parser_declarator_id
2222 (cp_parser *, bool);
2223 static tree cp_parser_type_id
2224 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2225 static tree cp_parser_template_type_arg
2226 (cp_parser *);
2227 static tree cp_parser_trailing_type_id (cp_parser *);
2228 static tree cp_parser_type_id_1
2229 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2230 static void cp_parser_type_specifier_seq
2231 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2232 static tree cp_parser_parameter_declaration_clause
2233 (cp_parser *, cp_parser_flags);
2234 static tree cp_parser_parameter_declaration_list
2235 (cp_parser *, cp_parser_flags);
2236 static cp_parameter_declarator *cp_parser_parameter_declaration
2237 (cp_parser *, cp_parser_flags, bool, bool *);
2238 static tree cp_parser_default_argument
2239 (cp_parser *, bool);
2240 static void cp_parser_function_body
2241 (cp_parser *, bool);
2242 static tree cp_parser_initializer
2243 (cp_parser *, bool *, bool *, bool = false);
2244 static cp_expr cp_parser_initializer_clause
2245 (cp_parser *, bool *);
2246 static cp_expr cp_parser_braced_list
2247 (cp_parser*, bool*);
2248 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2249 (cp_parser *, bool *);
2250
2251 static void cp_parser_ctor_initializer_opt_and_function_body
2252 (cp_parser *, bool);
2253
2254 static tree cp_parser_late_parsing_omp_declare_simd
2255 (cp_parser *, tree);
2256
2257 static tree cp_parser_late_parsing_oacc_routine
2258 (cp_parser *, tree);
2259
2260 static tree synthesize_implicit_template_parm
2261 (cp_parser *, tree);
2262 static tree finish_fully_implicit_template
2263 (cp_parser *, tree);
2264 static void abort_fully_implicit_template
2265 (cp_parser *);
2266
2267 /* Classes [gram.class] */
2268
2269 static tree cp_parser_class_name
2270 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2271 static tree cp_parser_class_specifier
2272 (cp_parser *);
2273 static tree cp_parser_class_head
2274 (cp_parser *, bool *);
2275 static enum tag_types cp_parser_class_key
2276 (cp_parser *);
2277 static void cp_parser_type_parameter_key
2278 (cp_parser* parser);
2279 static void cp_parser_member_specification_opt
2280 (cp_parser *);
2281 static void cp_parser_member_declaration
2282 (cp_parser *);
2283 static tree cp_parser_pure_specifier
2284 (cp_parser *);
2285 static tree cp_parser_constant_initializer
2286 (cp_parser *);
2287
2288 /* Derived classes [gram.class.derived] */
2289
2290 static tree cp_parser_base_clause
2291 (cp_parser *);
2292 static tree cp_parser_base_specifier
2293 (cp_parser *);
2294
2295 /* Special member functions [gram.special] */
2296
2297 static tree cp_parser_conversion_function_id
2298 (cp_parser *);
2299 static tree cp_parser_conversion_type_id
2300 (cp_parser *);
2301 static cp_declarator *cp_parser_conversion_declarator_opt
2302 (cp_parser *);
2303 static void cp_parser_ctor_initializer_opt
2304 (cp_parser *);
2305 static void cp_parser_mem_initializer_list
2306 (cp_parser *);
2307 static tree cp_parser_mem_initializer
2308 (cp_parser *);
2309 static tree cp_parser_mem_initializer_id
2310 (cp_parser *);
2311
2312 /* Overloading [gram.over] */
2313
2314 static cp_expr cp_parser_operator_function_id
2315 (cp_parser *);
2316 static cp_expr cp_parser_operator
2317 (cp_parser *, location_t);
2318
2319 /* Templates [gram.temp] */
2320
2321 static void cp_parser_template_declaration
2322 (cp_parser *, bool);
2323 static tree cp_parser_template_parameter_list
2324 (cp_parser *);
2325 static tree cp_parser_template_parameter
2326 (cp_parser *, bool *, bool *);
2327 static tree cp_parser_type_parameter
2328 (cp_parser *, bool *);
2329 static tree cp_parser_template_id
2330 (cp_parser *, bool, bool, enum tag_types, bool);
2331 static tree cp_parser_template_name
2332 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2333 static tree cp_parser_template_argument_list
2334 (cp_parser *);
2335 static tree cp_parser_template_argument
2336 (cp_parser *);
2337 static void cp_parser_explicit_instantiation
2338 (cp_parser *);
2339 static void cp_parser_explicit_specialization
2340 (cp_parser *);
2341
2342 /* Exception handling [gram.exception] */
2343
2344 static tree cp_parser_try_block
2345 (cp_parser *);
2346 static void cp_parser_function_try_block
2347 (cp_parser *);
2348 static void cp_parser_handler_seq
2349 (cp_parser *);
2350 static void cp_parser_handler
2351 (cp_parser *);
2352 static tree cp_parser_exception_declaration
2353 (cp_parser *);
2354 static tree cp_parser_throw_expression
2355 (cp_parser *);
2356 static tree cp_parser_exception_specification_opt
2357 (cp_parser *);
2358 static tree cp_parser_type_id_list
2359 (cp_parser *);
2360
2361 /* GNU Extensions */
2362
2363 static tree cp_parser_asm_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_asm_operand_list
2366 (cp_parser *);
2367 static tree cp_parser_asm_clobber_list
2368 (cp_parser *);
2369 static tree cp_parser_asm_label_list
2370 (cp_parser *);
2371 static bool cp_next_tokens_can_be_attribute_p
2372 (cp_parser *);
2373 static bool cp_next_tokens_can_be_gnu_attribute_p
2374 (cp_parser *);
2375 static bool cp_next_tokens_can_be_std_attribute_p
2376 (cp_parser *);
2377 static bool cp_nth_tokens_can_be_std_attribute_p
2378 (cp_parser *, size_t);
2379 static bool cp_nth_tokens_can_be_gnu_attribute_p
2380 (cp_parser *, size_t);
2381 static bool cp_nth_tokens_can_be_attribute_p
2382 (cp_parser *, size_t);
2383 static tree cp_parser_attributes_opt
2384 (cp_parser *);
2385 static tree cp_parser_gnu_attributes_opt
2386 (cp_parser *);
2387 static tree cp_parser_gnu_attribute_list
2388 (cp_parser *, bool = false);
2389 static tree cp_parser_std_attribute
2390 (cp_parser *, tree);
2391 static tree cp_parser_std_attribute_spec
2392 (cp_parser *);
2393 static tree cp_parser_std_attribute_spec_seq
2394 (cp_parser *);
2395 static size_t cp_parser_skip_attributes_opt
2396 (cp_parser *, size_t);
2397 static bool cp_parser_extension_opt
2398 (cp_parser *, int *);
2399 static void cp_parser_label_declaration
2400 (cp_parser *);
2401
2402 /* Concept Extensions */
2403
2404 static tree cp_parser_requires_clause
2405 (cp_parser *);
2406 static tree cp_parser_requires_clause_opt
2407 (cp_parser *);
2408 static tree cp_parser_requires_expression
2409 (cp_parser *);
2410 static tree cp_parser_requirement_parameter_list
2411 (cp_parser *);
2412 static tree cp_parser_requirement_body
2413 (cp_parser *);
2414 static tree cp_parser_requirement_list
2415 (cp_parser *);
2416 static tree cp_parser_requirement
2417 (cp_parser *);
2418 static tree cp_parser_simple_requirement
2419 (cp_parser *);
2420 static tree cp_parser_compound_requirement
2421 (cp_parser *);
2422 static tree cp_parser_type_requirement
2423 (cp_parser *);
2424 static tree cp_parser_nested_requirement
2425 (cp_parser *);
2426
2427 /* Transactional Memory Extensions */
2428
2429 static tree cp_parser_transaction
2430 (cp_parser *, cp_token *);
2431 static tree cp_parser_transaction_expression
2432 (cp_parser *, enum rid);
2433 static void cp_parser_function_transaction
2434 (cp_parser *, enum rid);
2435 static tree cp_parser_transaction_cancel
2436 (cp_parser *);
2437
2438 enum pragma_context {
2439 pragma_external,
2440 pragma_member,
2441 pragma_objc_icode,
2442 pragma_stmt,
2443 pragma_compound
2444 };
2445 static bool cp_parser_pragma
2446 (cp_parser *, enum pragma_context, bool *);
2447
2448 /* Objective-C++ Productions */
2449
2450 static tree cp_parser_objc_message_receiver
2451 (cp_parser *);
2452 static tree cp_parser_objc_message_args
2453 (cp_parser *);
2454 static tree cp_parser_objc_message_expression
2455 (cp_parser *);
2456 static cp_expr cp_parser_objc_encode_expression
2457 (cp_parser *);
2458 static tree cp_parser_objc_defs_expression
2459 (cp_parser *);
2460 static tree cp_parser_objc_protocol_expression
2461 (cp_parser *);
2462 static tree cp_parser_objc_selector_expression
2463 (cp_parser *);
2464 static cp_expr cp_parser_objc_expression
2465 (cp_parser *);
2466 static bool cp_parser_objc_selector_p
2467 (enum cpp_ttype);
2468 static tree cp_parser_objc_selector
2469 (cp_parser *);
2470 static tree cp_parser_objc_protocol_refs_opt
2471 (cp_parser *);
2472 static void cp_parser_objc_declaration
2473 (cp_parser *, tree);
2474 static tree cp_parser_objc_statement
2475 (cp_parser *);
2476 static bool cp_parser_objc_valid_prefix_attributes
2477 (cp_parser *, tree *);
2478 static void cp_parser_objc_at_property_declaration
2479 (cp_parser *) ;
2480 static void cp_parser_objc_at_synthesize_declaration
2481 (cp_parser *) ;
2482 static void cp_parser_objc_at_dynamic_declaration
2483 (cp_parser *) ;
2484 static tree cp_parser_objc_struct_declaration
2485 (cp_parser *) ;
2486
2487 /* Utility Routines */
2488
2489 static cp_expr cp_parser_lookup_name
2490 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2491 static tree cp_parser_lookup_name_simple
2492 (cp_parser *, tree, location_t);
2493 static tree cp_parser_maybe_treat_template_as_class
2494 (tree, bool);
2495 static bool cp_parser_check_declarator_template_parameters
2496 (cp_parser *, cp_declarator *, location_t);
2497 static bool cp_parser_check_template_parameters
2498 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2499 static cp_expr cp_parser_simple_cast_expression
2500 (cp_parser *);
2501 static tree cp_parser_global_scope_opt
2502 (cp_parser *, bool);
2503 static bool cp_parser_constructor_declarator_p
2504 (cp_parser *, bool);
2505 static tree cp_parser_function_definition_from_specifiers_and_declarator
2506 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2507 static tree cp_parser_function_definition_after_declarator
2508 (cp_parser *, bool);
2509 static bool cp_parser_template_declaration_after_export
2510 (cp_parser *, bool);
2511 static void cp_parser_perform_template_parameter_access_checks
2512 (vec<deferred_access_check, va_gc> *);
2513 static tree cp_parser_single_declaration
2514 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2515 static cp_expr cp_parser_functional_cast
2516 (cp_parser *, tree);
2517 static tree cp_parser_save_member_function_body
2518 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2519 static tree cp_parser_save_nsdmi
2520 (cp_parser *);
2521 static tree cp_parser_enclosed_template_argument_list
2522 (cp_parser *);
2523 static void cp_parser_save_default_args
2524 (cp_parser *, tree);
2525 static void cp_parser_late_parsing_for_member
2526 (cp_parser *, tree);
2527 static tree cp_parser_late_parse_one_default_arg
2528 (cp_parser *, tree, tree, tree);
2529 static void cp_parser_late_parsing_nsdmi
2530 (cp_parser *, tree);
2531 static void cp_parser_late_parsing_default_args
2532 (cp_parser *, tree);
2533 static tree cp_parser_sizeof_operand
2534 (cp_parser *, enum rid);
2535 static cp_expr cp_parser_trait_expr
2536 (cp_parser *, enum rid);
2537 static bool cp_parser_declares_only_class_p
2538 (cp_parser *);
2539 static void cp_parser_set_storage_class
2540 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2541 static void cp_parser_set_decl_spec_type
2542 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2543 static void set_and_check_decl_spec_loc
2544 (cp_decl_specifier_seq *decl_specs,
2545 cp_decl_spec ds, cp_token *);
2546 static bool cp_parser_friend_p
2547 (const cp_decl_specifier_seq *);
2548 static void cp_parser_required_error
2549 (cp_parser *, required_token, bool, location_t);
2550 static cp_token *cp_parser_require
2551 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2552 static cp_token *cp_parser_require_keyword
2553 (cp_parser *, enum rid, required_token);
2554 static bool cp_parser_token_starts_function_definition_p
2555 (cp_token *);
2556 static bool cp_parser_next_token_starts_class_definition_p
2557 (cp_parser *);
2558 static bool cp_parser_next_token_ends_template_argument_p
2559 (cp_parser *);
2560 static bool cp_parser_nth_token_starts_template_argument_list_p
2561 (cp_parser *, size_t);
2562 static enum tag_types cp_parser_token_is_class_key
2563 (cp_token *);
2564 static enum tag_types cp_parser_token_is_type_parameter_key
2565 (cp_token *);
2566 static void cp_parser_check_class_key
2567 (enum tag_types, tree type);
2568 static void cp_parser_check_access_in_redeclaration
2569 (tree type, location_t location);
2570 static bool cp_parser_optional_template_keyword
2571 (cp_parser *);
2572 static void cp_parser_pre_parsed_nested_name_specifier
2573 (cp_parser *);
2574 static bool cp_parser_cache_group
2575 (cp_parser *, enum cpp_ttype, unsigned);
2576 static tree cp_parser_cache_defarg
2577 (cp_parser *parser, bool nsdmi);
2578 static void cp_parser_parse_tentatively
2579 (cp_parser *);
2580 static void cp_parser_commit_to_tentative_parse
2581 (cp_parser *);
2582 static void cp_parser_commit_to_topmost_tentative_parse
2583 (cp_parser *);
2584 static void cp_parser_abort_tentative_parse
2585 (cp_parser *);
2586 static bool cp_parser_parse_definitely
2587 (cp_parser *);
2588 static inline bool cp_parser_parsing_tentatively
2589 (cp_parser *);
2590 static bool cp_parser_uncommitted_to_tentative_parse_p
2591 (cp_parser *);
2592 static void cp_parser_error
2593 (cp_parser *, const char *);
2594 static void cp_parser_name_lookup_error
2595 (cp_parser *, tree, tree, name_lookup_error, location_t);
2596 static bool cp_parser_simulate_error
2597 (cp_parser *);
2598 static bool cp_parser_check_type_definition
2599 (cp_parser *);
2600 static void cp_parser_check_for_definition_in_return_type
2601 (cp_declarator *, tree, location_t type_location);
2602 static void cp_parser_check_for_invalid_template_id
2603 (cp_parser *, tree, enum tag_types, location_t location);
2604 static bool cp_parser_non_integral_constant_expression
2605 (cp_parser *, non_integral_constant);
2606 static void cp_parser_diagnose_invalid_type_name
2607 (cp_parser *, tree, location_t);
2608 static bool cp_parser_parse_and_diagnose_invalid_type_name
2609 (cp_parser *);
2610 static int cp_parser_skip_to_closing_parenthesis
2611 (cp_parser *, bool, bool, bool);
2612 static void cp_parser_skip_to_end_of_statement
2613 (cp_parser *);
2614 static void cp_parser_consume_semicolon_at_end_of_statement
2615 (cp_parser *);
2616 static void cp_parser_skip_to_end_of_block_or_statement
2617 (cp_parser *);
2618 static bool cp_parser_skip_to_closing_brace
2619 (cp_parser *);
2620 static void cp_parser_skip_to_end_of_template_parameter_list
2621 (cp_parser *);
2622 static void cp_parser_skip_to_pragma_eol
2623 (cp_parser*, cp_token *);
2624 static bool cp_parser_error_occurred
2625 (cp_parser *);
2626 static bool cp_parser_allow_gnu_extensions_p
2627 (cp_parser *);
2628 static bool cp_parser_is_pure_string_literal
2629 (cp_token *);
2630 static bool cp_parser_is_string_literal
2631 (cp_token *);
2632 static bool cp_parser_is_keyword
2633 (cp_token *, enum rid);
2634 static tree cp_parser_make_typename_type
2635 (cp_parser *, tree, location_t location);
2636 static cp_declarator * cp_parser_make_indirect_declarator
2637 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2638 static bool cp_parser_compound_literal_p
2639 (cp_parser *);
2640 static bool cp_parser_array_designator_p
2641 (cp_parser *);
2642 static bool cp_parser_init_statement_p
2643 (cp_parser *);
2644 static bool cp_parser_skip_to_closing_square_bracket
2645 (cp_parser *);
2646
2647 /* Concept-related syntactic transformations */
2648
2649 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2650 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2651
2652 // -------------------------------------------------------------------------- //
2653 // Unevaluated Operand Guard
2654 //
2655 // Implementation of an RAII helper for unevaluated operand parsing.
2656 cp_unevaluated::cp_unevaluated ()
2657 {
2658 ++cp_unevaluated_operand;
2659 ++c_inhibit_evaluation_warnings;
2660 }
2661
2662 cp_unevaluated::~cp_unevaluated ()
2663 {
2664 --c_inhibit_evaluation_warnings;
2665 --cp_unevaluated_operand;
2666 }
2667
2668 // -------------------------------------------------------------------------- //
2669 // Tentative Parsing
2670
2671 /* Returns nonzero if we are parsing tentatively. */
2672
2673 static inline bool
2674 cp_parser_parsing_tentatively (cp_parser* parser)
2675 {
2676 return parser->context->next != NULL;
2677 }
2678
2679 /* Returns nonzero if TOKEN is a string literal. */
2680
2681 static bool
2682 cp_parser_is_pure_string_literal (cp_token* token)
2683 {
2684 return (token->type == CPP_STRING ||
2685 token->type == CPP_STRING16 ||
2686 token->type == CPP_STRING32 ||
2687 token->type == CPP_WSTRING ||
2688 token->type == CPP_UTF8STRING);
2689 }
2690
2691 /* Returns nonzero if TOKEN is a string literal
2692 of a user-defined string literal. */
2693
2694 static bool
2695 cp_parser_is_string_literal (cp_token* token)
2696 {
2697 return (cp_parser_is_pure_string_literal (token) ||
2698 token->type == CPP_STRING_USERDEF ||
2699 token->type == CPP_STRING16_USERDEF ||
2700 token->type == CPP_STRING32_USERDEF ||
2701 token->type == CPP_WSTRING_USERDEF ||
2702 token->type == CPP_UTF8STRING_USERDEF);
2703 }
2704
2705 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2706
2707 static bool
2708 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2709 {
2710 return token->keyword == keyword;
2711 }
2712
2713 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2714 PRAGMA_NONE. */
2715
2716 static enum pragma_kind
2717 cp_parser_pragma_kind (cp_token *token)
2718 {
2719 if (token->type != CPP_PRAGMA)
2720 return PRAGMA_NONE;
2721 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2722 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2723 }
2724
2725 /* Helper function for cp_parser_error.
2726 Having peeked a token of kind TOK1_KIND that might signify
2727 a conflict marker, peek successor tokens to determine
2728 if we actually do have a conflict marker.
2729 Specifically, we consider a run of 7 '<', '=' or '>' characters
2730 at the start of a line as a conflict marker.
2731 These come through the lexer as three pairs and a single,
2732 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2733 If it returns true, *OUT_LOC is written to with the location/range
2734 of the marker. */
2735
2736 static bool
2737 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2738 location_t *out_loc)
2739 {
2740 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2741 if (token2->type != tok1_kind)
2742 return false;
2743 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2744 if (token3->type != tok1_kind)
2745 return false;
2746 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2747 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2748 return false;
2749
2750 /* It must be at the start of the line. */
2751 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2752 if (LOCATION_COLUMN (start_loc) != 1)
2753 return false;
2754
2755 /* We have a conflict marker. Construct a location of the form:
2756 <<<<<<<
2757 ^~~~~~~
2758 with start == caret, finishing at the end of the marker. */
2759 location_t finish_loc = get_finish (token4->location);
2760 *out_loc = make_location (start_loc, start_loc, finish_loc);
2761
2762 return true;
2763 }
2764
2765 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2766 RT_CLOSE_PAREN. */
2767
2768 static const char *
2769 get_matching_symbol (required_token token_desc)
2770 {
2771 switch (token_desc)
2772 {
2773 default:
2774 gcc_unreachable ();
2775 return "";
2776 case RT_CLOSE_BRACE:
2777 return "{";
2778 case RT_CLOSE_PAREN:
2779 return "(";
2780 }
2781 }
2782
2783 /* Attempt to convert TOKEN_DESC from a required_token to an
2784 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2785
2786 static enum cpp_ttype
2787 get_required_cpp_ttype (required_token token_desc)
2788 {
2789 switch (token_desc)
2790 {
2791 case RT_SEMICOLON:
2792 return CPP_SEMICOLON;
2793 case RT_OPEN_PAREN:
2794 return CPP_OPEN_PAREN;
2795 case RT_CLOSE_BRACE:
2796 return CPP_CLOSE_BRACE;
2797 case RT_OPEN_BRACE:
2798 return CPP_OPEN_BRACE;
2799 case RT_CLOSE_SQUARE:
2800 return CPP_CLOSE_SQUARE;
2801 case RT_OPEN_SQUARE:
2802 return CPP_OPEN_SQUARE;
2803 case RT_COMMA:
2804 return CPP_COMMA;
2805 case RT_COLON:
2806 return CPP_COLON;
2807 case RT_CLOSE_PAREN:
2808 return CPP_CLOSE_PAREN;
2809
2810 default:
2811 /* Use CPP_EOF as a "no completions possible" code. */
2812 return CPP_EOF;
2813 }
2814 }
2815
2816
2817 /* Subroutine of cp_parser_error and cp_parser_required_error.
2818
2819 Issue a diagnostic of the form
2820 FILE:LINE: MESSAGE before TOKEN
2821 where TOKEN is the next token in the input stream. MESSAGE
2822 (specified by the caller) is usually of the form "expected
2823 OTHER-TOKEN".
2824
2825 This bypasses the check for tentative passing, and potentially
2826 adds material needed by cp_parser_required_error.
2827
2828 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2829 suggesting insertion of the missing token.
2830
2831 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2832 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2833 location. */
2834
2835 static void
2836 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2837 required_token missing_token_desc,
2838 location_t matching_location)
2839 {
2840 cp_token *token = cp_lexer_peek_token (parser->lexer);
2841 /* This diagnostic makes more sense if it is tagged to the line
2842 of the token we just peeked at. */
2843 cp_lexer_set_source_position_from_token (token);
2844
2845 if (token->type == CPP_PRAGMA)
2846 {
2847 error_at (token->location,
2848 "%<#pragma%> is not allowed here");
2849 cp_parser_skip_to_pragma_eol (parser, token);
2850 return;
2851 }
2852
2853 /* If this is actually a conflict marker, report it as such. */
2854 if (token->type == CPP_LSHIFT
2855 || token->type == CPP_RSHIFT
2856 || token->type == CPP_EQ_EQ)
2857 {
2858 location_t loc;
2859 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2860 {
2861 error_at (loc, "version control conflict marker in file");
2862 expanded_location token_exploc = expand_location (token->location);
2863 /* Consume tokens until the end of the source line. */
2864 while (1)
2865 {
2866 cp_lexer_consume_token (parser->lexer);
2867 cp_token *next = cp_lexer_peek_token (parser->lexer);
2868 if (next == NULL)
2869 break;
2870 expanded_location next_exploc = expand_location (next->location);
2871 if (next_exploc.file != token_exploc.file)
2872 break;
2873 if (next_exploc.line != token_exploc.line)
2874 break;
2875 }
2876 return;
2877 }
2878 }
2879
2880 gcc_rich_location richloc (input_location);
2881
2882 bool added_matching_location = false;
2883
2884 if (missing_token_desc != RT_NONE)
2885 {
2886 /* Potentially supply a fix-it hint, suggesting to add the
2887 missing token immediately after the *previous* token.
2888 This may move the primary location within richloc. */
2889 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2890 location_t prev_token_loc
2891 = cp_lexer_previous_token (parser->lexer)->location;
2892 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2893
2894 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2895 Attempt to consolidate diagnostics by printing it as a
2896 secondary range within the main diagnostic. */
2897 if (matching_location != UNKNOWN_LOCATION)
2898 added_matching_location
2899 = richloc.add_location_if_nearby (matching_location);
2900 }
2901
2902 /* Actually emit the error. */
2903 c_parse_error (gmsgid,
2904 /* Because c_parser_error does not understand
2905 CPP_KEYWORD, keywords are treated like
2906 identifiers. */
2907 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2908 token->u.value, token->flags, &richloc);
2909
2910 if (missing_token_desc != RT_NONE)
2911 {
2912 /* If we weren't able to consolidate matching_location, then
2913 print it as a secondary diagnostic. */
2914 if (matching_location != UNKNOWN_LOCATION
2915 && !added_matching_location)
2916 inform (matching_location, "to match this %qs",
2917 get_matching_symbol (missing_token_desc));
2918 }
2919 }
2920
2921 /* If not parsing tentatively, issue a diagnostic of the form
2922 FILE:LINE: MESSAGE before TOKEN
2923 where TOKEN is the next token in the input stream. MESSAGE
2924 (specified by the caller) is usually of the form "expected
2925 OTHER-TOKEN". */
2926
2927 static void
2928 cp_parser_error (cp_parser* parser, const char* gmsgid)
2929 {
2930 if (!cp_parser_simulate_error (parser))
2931 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2932 }
2933
2934 /* Issue an error about name-lookup failing. NAME is the
2935 IDENTIFIER_NODE DECL is the result of
2936 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2937 the thing that we hoped to find. */
2938
2939 static void
2940 cp_parser_name_lookup_error (cp_parser* parser,
2941 tree name,
2942 tree decl,
2943 name_lookup_error desired,
2944 location_t location)
2945 {
2946 /* If name lookup completely failed, tell the user that NAME was not
2947 declared. */
2948 if (decl == error_mark_node)
2949 {
2950 if (parser->scope && parser->scope != global_namespace)
2951 error_at (location, "%<%E::%E%> has not been declared",
2952 parser->scope, name);
2953 else if (parser->scope == global_namespace)
2954 error_at (location, "%<::%E%> has not been declared", name);
2955 else if (parser->object_scope
2956 && !CLASS_TYPE_P (parser->object_scope))
2957 error_at (location, "request for member %qE in non-class type %qT",
2958 name, parser->object_scope);
2959 else if (parser->object_scope)
2960 error_at (location, "%<%T::%E%> has not been declared",
2961 parser->object_scope, name);
2962 else
2963 error_at (location, "%qE has not been declared", name);
2964 }
2965 else if (parser->scope && parser->scope != global_namespace)
2966 {
2967 switch (desired)
2968 {
2969 case NLE_TYPE:
2970 error_at (location, "%<%E::%E%> is not a type",
2971 parser->scope, name);
2972 break;
2973 case NLE_CXX98:
2974 error_at (location, "%<%E::%E%> is not a class or namespace",
2975 parser->scope, name);
2976 break;
2977 case NLE_NOT_CXX98:
2978 error_at (location,
2979 "%<%E::%E%> is not a class, namespace, or enumeration",
2980 parser->scope, name);
2981 break;
2982 default:
2983 gcc_unreachable ();
2984
2985 }
2986 }
2987 else if (parser->scope == global_namespace)
2988 {
2989 switch (desired)
2990 {
2991 case NLE_TYPE:
2992 error_at (location, "%<::%E%> is not a type", name);
2993 break;
2994 case NLE_CXX98:
2995 error_at (location, "%<::%E%> is not a class or namespace", name);
2996 break;
2997 case NLE_NOT_CXX98:
2998 error_at (location,
2999 "%<::%E%> is not a class, namespace, or enumeration",
3000 name);
3001 break;
3002 default:
3003 gcc_unreachable ();
3004 }
3005 }
3006 else
3007 {
3008 switch (desired)
3009 {
3010 case NLE_TYPE:
3011 error_at (location, "%qE is not a type", name);
3012 break;
3013 case NLE_CXX98:
3014 error_at (location, "%qE is not a class or namespace", name);
3015 break;
3016 case NLE_NOT_CXX98:
3017 error_at (location,
3018 "%qE is not a class, namespace, or enumeration", name);
3019 break;
3020 default:
3021 gcc_unreachable ();
3022 }
3023 }
3024 }
3025
3026 /* If we are parsing tentatively, remember that an error has occurred
3027 during this tentative parse. Returns true if the error was
3028 simulated; false if a message should be issued by the caller. */
3029
3030 static bool
3031 cp_parser_simulate_error (cp_parser* parser)
3032 {
3033 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3034 {
3035 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3036 return true;
3037 }
3038 return false;
3039 }
3040
3041 /* This function is called when a type is defined. If type
3042 definitions are forbidden at this point, an error message is
3043 issued. */
3044
3045 static bool
3046 cp_parser_check_type_definition (cp_parser* parser)
3047 {
3048 /* If types are forbidden here, issue a message. */
3049 if (parser->type_definition_forbidden_message)
3050 {
3051 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3052 in the message need to be interpreted. */
3053 error (parser->type_definition_forbidden_message);
3054 return false;
3055 }
3056 return true;
3057 }
3058
3059 /* This function is called when the DECLARATOR is processed. The TYPE
3060 was a type defined in the decl-specifiers. If it is invalid to
3061 define a type in the decl-specifiers for DECLARATOR, an error is
3062 issued. TYPE_LOCATION is the location of TYPE and is used
3063 for error reporting. */
3064
3065 static void
3066 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3067 tree type, location_t type_location)
3068 {
3069 /* [dcl.fct] forbids type definitions in return types.
3070 Unfortunately, it's not easy to know whether or not we are
3071 processing a return type until after the fact. */
3072 while (declarator
3073 && (declarator->kind == cdk_pointer
3074 || declarator->kind == cdk_reference
3075 || declarator->kind == cdk_ptrmem))
3076 declarator = declarator->declarator;
3077 if (declarator
3078 && declarator->kind == cdk_function)
3079 {
3080 error_at (type_location,
3081 "new types may not be defined in a return type");
3082 inform (type_location,
3083 "(perhaps a semicolon is missing after the definition of %qT)",
3084 type);
3085 }
3086 }
3087
3088 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3089 "<" in any valid C++ program. If the next token is indeed "<",
3090 issue a message warning the user about what appears to be an
3091 invalid attempt to form a template-id. LOCATION is the location
3092 of the type-specifier (TYPE) */
3093
3094 static void
3095 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3096 tree type,
3097 enum tag_types tag_type,
3098 location_t location)
3099 {
3100 cp_token_position start = 0;
3101
3102 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3103 {
3104 if (TREE_CODE (type) == TYPE_DECL)
3105 type = TREE_TYPE (type);
3106 if (TYPE_P (type) && !template_placeholder_p (type))
3107 error_at (location, "%qT is not a template", type);
3108 else if (identifier_p (type))
3109 {
3110 if (tag_type != none_type)
3111 error_at (location, "%qE is not a class template", type);
3112 else
3113 error_at (location, "%qE is not a template", type);
3114 }
3115 else
3116 error_at (location, "invalid template-id");
3117 /* Remember the location of the invalid "<". */
3118 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3119 start = cp_lexer_token_position (parser->lexer, true);
3120 /* Consume the "<". */
3121 cp_lexer_consume_token (parser->lexer);
3122 /* Parse the template arguments. */
3123 cp_parser_enclosed_template_argument_list (parser);
3124 /* Permanently remove the invalid template arguments so that
3125 this error message is not issued again. */
3126 if (start)
3127 cp_lexer_purge_tokens_after (parser->lexer, start);
3128 }
3129 }
3130
3131 /* If parsing an integral constant-expression, issue an error message
3132 about the fact that THING appeared and return true. Otherwise,
3133 return false. In either case, set
3134 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3135
3136 static bool
3137 cp_parser_non_integral_constant_expression (cp_parser *parser,
3138 non_integral_constant thing)
3139 {
3140 parser->non_integral_constant_expression_p = true;
3141 if (parser->integral_constant_expression_p)
3142 {
3143 if (!parser->allow_non_integral_constant_expression_p)
3144 {
3145 const char *msg = NULL;
3146 switch (thing)
3147 {
3148 case NIC_FLOAT:
3149 pedwarn (input_location, OPT_Wpedantic,
3150 "ISO C++ forbids using a floating-point literal "
3151 "in a constant-expression");
3152 return true;
3153 case NIC_CAST:
3154 error ("a cast to a type other than an integral or "
3155 "enumeration type cannot appear in a "
3156 "constant-expression");
3157 return true;
3158 case NIC_TYPEID:
3159 error ("%<typeid%> operator "
3160 "cannot appear in a constant-expression");
3161 return true;
3162 case NIC_NCC:
3163 error ("non-constant compound literals "
3164 "cannot appear in a constant-expression");
3165 return true;
3166 case NIC_FUNC_CALL:
3167 error ("a function call "
3168 "cannot appear in a constant-expression");
3169 return true;
3170 case NIC_INC:
3171 error ("an increment "
3172 "cannot appear in a constant-expression");
3173 return true;
3174 case NIC_DEC:
3175 error ("an decrement "
3176 "cannot appear in a constant-expression");
3177 return true;
3178 case NIC_ARRAY_REF:
3179 error ("an array reference "
3180 "cannot appear in a constant-expression");
3181 return true;
3182 case NIC_ADDR_LABEL:
3183 error ("the address of a label "
3184 "cannot appear in a constant-expression");
3185 return true;
3186 case NIC_OVERLOADED:
3187 error ("calls to overloaded operators "
3188 "cannot appear in a constant-expression");
3189 return true;
3190 case NIC_ASSIGNMENT:
3191 error ("an assignment cannot appear in a constant-expression");
3192 return true;
3193 case NIC_COMMA:
3194 error ("a comma operator "
3195 "cannot appear in a constant-expression");
3196 return true;
3197 case NIC_CONSTRUCTOR:
3198 error ("a call to a constructor "
3199 "cannot appear in a constant-expression");
3200 return true;
3201 case NIC_TRANSACTION:
3202 error ("a transaction expression "
3203 "cannot appear in a constant-expression");
3204 return true;
3205 case NIC_THIS:
3206 msg = "this";
3207 break;
3208 case NIC_FUNC_NAME:
3209 msg = "__FUNCTION__";
3210 break;
3211 case NIC_PRETTY_FUNC:
3212 msg = "__PRETTY_FUNCTION__";
3213 break;
3214 case NIC_C99_FUNC:
3215 msg = "__func__";
3216 break;
3217 case NIC_VA_ARG:
3218 msg = "va_arg";
3219 break;
3220 case NIC_ARROW:
3221 msg = "->";
3222 break;
3223 case NIC_POINT:
3224 msg = ".";
3225 break;
3226 case NIC_STAR:
3227 msg = "*";
3228 break;
3229 case NIC_ADDR:
3230 msg = "&";
3231 break;
3232 case NIC_PREINCREMENT:
3233 msg = "++";
3234 break;
3235 case NIC_PREDECREMENT:
3236 msg = "--";
3237 break;
3238 case NIC_NEW:
3239 msg = "new";
3240 break;
3241 case NIC_DEL:
3242 msg = "delete";
3243 break;
3244 default:
3245 gcc_unreachable ();
3246 }
3247 if (msg)
3248 error ("%qs cannot appear in a constant-expression", msg);
3249 return true;
3250 }
3251 }
3252 return false;
3253 }
3254
3255 /* Emit a diagnostic for an invalid type name. This function commits
3256 to the current active tentative parse, if any. (Otherwise, the
3257 problematic construct might be encountered again later, resulting
3258 in duplicate error messages.) LOCATION is the location of ID. */
3259
3260 static void
3261 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3262 location_t location)
3263 {
3264 tree decl, ambiguous_decls;
3265 cp_parser_commit_to_tentative_parse (parser);
3266 /* Try to lookup the identifier. */
3267 decl = cp_parser_lookup_name (parser, id, none_type,
3268 /*is_template=*/false,
3269 /*is_namespace=*/false,
3270 /*check_dependency=*/true,
3271 &ambiguous_decls, location);
3272 if (ambiguous_decls)
3273 /* If the lookup was ambiguous, an error will already have
3274 been issued. */
3275 return;
3276 /* If the lookup found a template-name, it means that the user forgot
3277 to specify an argument list. Emit a useful error message. */
3278 if (DECL_TYPE_TEMPLATE_P (decl))
3279 {
3280 auto_diagnostic_group d;
3281 error_at (location,
3282 "invalid use of template-name %qE without an argument list",
3283 decl);
3284 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3285 inform (location, "class template argument deduction is only available "
3286 "with -std=c++17 or -std=gnu++17");
3287 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3288 }
3289 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3290 error_at (location, "invalid use of destructor %qD as a type", id);
3291 else if (TREE_CODE (decl) == TYPE_DECL)
3292 /* Something like 'unsigned A a;' */
3293 error_at (location, "invalid combination of multiple type-specifiers");
3294 else if (!parser->scope)
3295 {
3296 /* Issue an error message. */
3297 auto_diagnostic_group d;
3298 name_hint hint;
3299 if (TREE_CODE (id) == IDENTIFIER_NODE)
3300 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3301 if (const char *suggestion = hint.suggestion ())
3302 {
3303 gcc_rich_location richloc (location);
3304 richloc.add_fixit_replace (suggestion);
3305 error_at (&richloc,
3306 "%qE does not name a type; did you mean %qs?",
3307 id, suggestion);
3308 }
3309 else
3310 error_at (location, "%qE does not name a type", id);
3311 /* If we're in a template class, it's possible that the user was
3312 referring to a type from a base class. For example:
3313
3314 template <typename T> struct A { typedef T X; };
3315 template <typename T> struct B : public A<T> { X x; };
3316
3317 The user should have said "typename A<T>::X". */
3318 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3319 inform (location, "C++11 %<constexpr%> only available with "
3320 "-std=c++11 or -std=gnu++11");
3321 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3322 inform (location, "C++11 %<noexcept%> only available with "
3323 "-std=c++11 or -std=gnu++11");
3324 else if (cxx_dialect < cxx11
3325 && TREE_CODE (id) == IDENTIFIER_NODE
3326 && id_equal (id, "thread_local"))
3327 inform (location, "C++11 %<thread_local%> only available with "
3328 "-std=c++11 or -std=gnu++11");
3329 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3330 inform (location, "%<concept%> only available with -fconcepts");
3331 else if (processing_template_decl && current_class_type
3332 && TYPE_BINFO (current_class_type))
3333 {
3334 tree b;
3335
3336 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3337 b;
3338 b = TREE_CHAIN (b))
3339 {
3340 tree base_type = BINFO_TYPE (b);
3341 if (CLASS_TYPE_P (base_type)
3342 && dependent_type_p (base_type))
3343 {
3344 tree field;
3345 /* Go from a particular instantiation of the
3346 template (which will have an empty TYPE_FIELDs),
3347 to the main version. */
3348 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3349 for (field = TYPE_FIELDS (base_type);
3350 field;
3351 field = DECL_CHAIN (field))
3352 if (TREE_CODE (field) == TYPE_DECL
3353 && DECL_NAME (field) == id)
3354 {
3355 inform (location,
3356 "(perhaps %<typename %T::%E%> was intended)",
3357 BINFO_TYPE (b), id);
3358 break;
3359 }
3360 if (field)
3361 break;
3362 }
3363 }
3364 }
3365 }
3366 /* Here we diagnose qualified-ids where the scope is actually correct,
3367 but the identifier does not resolve to a valid type name. */
3368 else if (parser->scope != error_mark_node)
3369 {
3370 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3371 {
3372 auto_diagnostic_group d;
3373 name_hint hint;
3374 if (decl == error_mark_node)
3375 hint = suggest_alternative_in_explicit_scope (location, id,
3376 parser->scope);
3377 const char *suggestion = hint.suggestion ();
3378 gcc_rich_location richloc (location_of (id));
3379 if (suggestion)
3380 richloc.add_fixit_replace (suggestion);
3381 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3382 {
3383 if (suggestion)
3384 error_at (&richloc,
3385 "%qE in namespace %qE does not name a template"
3386 " type; did you mean %qs?",
3387 id, parser->scope, suggestion);
3388 else
3389 error_at (&richloc,
3390 "%qE in namespace %qE does not name a template type",
3391 id, parser->scope);
3392 }
3393 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3394 {
3395 if (suggestion)
3396 error_at (&richloc,
3397 "%qE in namespace %qE does not name a template"
3398 " type; did you mean %qs?",
3399 TREE_OPERAND (id, 0), parser->scope, suggestion);
3400 else
3401 error_at (&richloc,
3402 "%qE in namespace %qE does not name a template"
3403 " type",
3404 TREE_OPERAND (id, 0), parser->scope);
3405 }
3406 else
3407 {
3408 if (suggestion)
3409 error_at (&richloc,
3410 "%qE in namespace %qE does not name a type"
3411 "; did you mean %qs?",
3412 id, parser->scope, suggestion);
3413 else
3414 error_at (&richloc,
3415 "%qE in namespace %qE does not name a type",
3416 id, parser->scope);
3417 }
3418 if (DECL_P (decl))
3419 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3420 }
3421 else if (CLASS_TYPE_P (parser->scope)
3422 && constructor_name_p (id, parser->scope))
3423 {
3424 /* A<T>::A<T>() */
3425 auto_diagnostic_group d;
3426 error_at (location, "%<%T::%E%> names the constructor, not"
3427 " the type", parser->scope, id);
3428 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3429 error_at (location, "and %qT has no template constructors",
3430 parser->scope);
3431 }
3432 else if (TYPE_P (parser->scope)
3433 && dependent_scope_p (parser->scope))
3434 {
3435 gcc_rich_location richloc (location);
3436 richloc.add_fixit_insert_before ("typename ");
3437 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3438 error_at (&richloc,
3439 "need %<typename%> before %<%T::%D::%E%> because "
3440 "%<%T::%D%> is a dependent scope",
3441 TYPE_CONTEXT (parser->scope),
3442 TYPENAME_TYPE_FULLNAME (parser->scope),
3443 id,
3444 TYPE_CONTEXT (parser->scope),
3445 TYPENAME_TYPE_FULLNAME (parser->scope));
3446 else
3447 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3448 "%qT is a dependent scope",
3449 parser->scope, id, parser->scope);
3450 }
3451 else if (TYPE_P (parser->scope))
3452 {
3453 auto_diagnostic_group d;
3454 if (!COMPLETE_TYPE_P (parser->scope))
3455 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3456 parser->scope);
3457 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3458 error_at (location_of (id),
3459 "%qE in %q#T does not name a template type",
3460 id, parser->scope);
3461 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3462 error_at (location_of (id),
3463 "%qE in %q#T does not name a template type",
3464 TREE_OPERAND (id, 0), parser->scope);
3465 else
3466 error_at (location_of (id),
3467 "%qE in %q#T does not name a type",
3468 id, parser->scope);
3469 if (DECL_P (decl))
3470 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3471 }
3472 else
3473 gcc_unreachable ();
3474 }
3475 }
3476
3477 /* Check for a common situation where a type-name should be present,
3478 but is not, and issue a sensible error message. Returns true if an
3479 invalid type-name was detected.
3480
3481 The situation handled by this function are variable declarations of the
3482 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3483 Usually, `ID' should name a type, but if we got here it means that it
3484 does not. We try to emit the best possible error message depending on
3485 how exactly the id-expression looks like. */
3486
3487 static bool
3488 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3489 {
3490 tree id;
3491 cp_token *token = cp_lexer_peek_token (parser->lexer);
3492
3493 /* Avoid duplicate error about ambiguous lookup. */
3494 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3495 {
3496 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3497 if (next->type == CPP_NAME && next->error_reported)
3498 goto out;
3499 }
3500
3501 cp_parser_parse_tentatively (parser);
3502 id = cp_parser_id_expression (parser,
3503 /*template_keyword_p=*/false,
3504 /*check_dependency_p=*/true,
3505 /*template_p=*/NULL,
3506 /*declarator_p=*/false,
3507 /*optional_p=*/false);
3508 /* If the next token is a (, this is a function with no explicit return
3509 type, i.e. constructor, destructor or conversion op. */
3510 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3511 || TREE_CODE (id) == TYPE_DECL)
3512 {
3513 cp_parser_abort_tentative_parse (parser);
3514 return false;
3515 }
3516 if (!cp_parser_parse_definitely (parser))
3517 return false;
3518
3519 /* Emit a diagnostic for the invalid type. */
3520 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3521 out:
3522 /* If we aren't in the middle of a declarator (i.e. in a
3523 parameter-declaration-clause), skip to the end of the declaration;
3524 there's no point in trying to process it. */
3525 if (!parser->in_declarator_p)
3526 cp_parser_skip_to_end_of_block_or_statement (parser);
3527 return true;
3528 }
3529
3530 /* Consume tokens up to, and including, the next non-nested closing `)'.
3531 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3532 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3533 found an unnested token of that type. */
3534
3535 static int
3536 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3537 bool recovering,
3538 cpp_ttype or_ttype,
3539 bool consume_paren)
3540 {
3541 unsigned paren_depth = 0;
3542 unsigned brace_depth = 0;
3543 unsigned square_depth = 0;
3544 unsigned condop_depth = 0;
3545
3546 if (recovering && or_ttype == CPP_EOF
3547 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3548 return 0;
3549
3550 while (true)
3551 {
3552 cp_token * token = cp_lexer_peek_token (parser->lexer);
3553
3554 /* Have we found what we're looking for before the closing paren? */
3555 if (token->type == or_ttype && or_ttype != CPP_EOF
3556 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3557 return -1;
3558
3559 switch (token->type)
3560 {
3561 case CPP_PRAGMA_EOL:
3562 if (!parser->lexer->in_pragma)
3563 break;
3564 /* FALLTHRU */
3565 case CPP_EOF:
3566 /* If we've run out of tokens, then there is no closing `)'. */
3567 return 0;
3568
3569 /* This is good for lambda expression capture-lists. */
3570 case CPP_OPEN_SQUARE:
3571 ++square_depth;
3572 break;
3573 case CPP_CLOSE_SQUARE:
3574 if (!square_depth--)
3575 return 0;
3576 break;
3577
3578 case CPP_SEMICOLON:
3579 /* This matches the processing in skip_to_end_of_statement. */
3580 if (!brace_depth)
3581 return 0;
3582 break;
3583
3584 case CPP_OPEN_BRACE:
3585 ++brace_depth;
3586 break;
3587 case CPP_CLOSE_BRACE:
3588 if (!brace_depth--)
3589 return 0;
3590 break;
3591
3592 case CPP_OPEN_PAREN:
3593 if (!brace_depth)
3594 ++paren_depth;
3595 break;
3596
3597 case CPP_CLOSE_PAREN:
3598 if (!brace_depth && !paren_depth--)
3599 {
3600 if (consume_paren)
3601 cp_lexer_consume_token (parser->lexer);
3602 return 1;
3603 }
3604 break;
3605
3606 case CPP_QUERY:
3607 if (!brace_depth && !paren_depth && !square_depth)
3608 ++condop_depth;
3609 break;
3610
3611 case CPP_COLON:
3612 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3613 condop_depth--;
3614 break;
3615
3616 default:
3617 break;
3618 }
3619
3620 /* Consume the token. */
3621 cp_lexer_consume_token (parser->lexer);
3622 }
3623 }
3624
3625 /* Consume tokens up to, and including, the next non-nested closing `)'.
3626 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3627 are doing error recovery. Returns -1 if OR_COMMA is true and we
3628 found an unnested token of that type. */
3629
3630 static int
3631 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3632 bool recovering,
3633 bool or_comma,
3634 bool consume_paren)
3635 {
3636 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3637 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3638 ttype, consume_paren);
3639 }
3640
3641 /* Consume tokens until we reach the end of the current statement.
3642 Normally, that will be just before consuming a `;'. However, if a
3643 non-nested `}' comes first, then we stop before consuming that. */
3644
3645 static void
3646 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3647 {
3648 unsigned nesting_depth = 0;
3649
3650 /* Unwind generic function template scope if necessary. */
3651 if (parser->fully_implicit_function_template_p)
3652 abort_fully_implicit_template (parser);
3653
3654 while (true)
3655 {
3656 cp_token *token = cp_lexer_peek_token (parser->lexer);
3657
3658 switch (token->type)
3659 {
3660 case CPP_PRAGMA_EOL:
3661 if (!parser->lexer->in_pragma)
3662 break;
3663 /* FALLTHRU */
3664 case CPP_EOF:
3665 /* If we've run out of tokens, stop. */
3666 return;
3667
3668 case CPP_SEMICOLON:
3669 /* If the next token is a `;', we have reached the end of the
3670 statement. */
3671 if (!nesting_depth)
3672 return;
3673 break;
3674
3675 case CPP_CLOSE_BRACE:
3676 /* If this is a non-nested '}', stop before consuming it.
3677 That way, when confronted with something like:
3678
3679 { 3 + }
3680
3681 we stop before consuming the closing '}', even though we
3682 have not yet reached a `;'. */
3683 if (nesting_depth == 0)
3684 return;
3685
3686 /* If it is the closing '}' for a block that we have
3687 scanned, stop -- but only after consuming the token.
3688 That way given:
3689
3690 void f g () { ... }
3691 typedef int I;
3692
3693 we will stop after the body of the erroneously declared
3694 function, but before consuming the following `typedef'
3695 declaration. */
3696 if (--nesting_depth == 0)
3697 {
3698 cp_lexer_consume_token (parser->lexer);
3699 return;
3700 }
3701 break;
3702
3703 case CPP_OPEN_BRACE:
3704 ++nesting_depth;
3705 break;
3706
3707 default:
3708 break;
3709 }
3710
3711 /* Consume the token. */
3712 cp_lexer_consume_token (parser->lexer);
3713 }
3714 }
3715
3716 /* This function is called at the end of a statement or declaration.
3717 If the next token is a semicolon, it is consumed; otherwise, error
3718 recovery is attempted. */
3719
3720 static void
3721 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3722 {
3723 /* Look for the trailing `;'. */
3724 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3725 {
3726 /* If there is additional (erroneous) input, skip to the end of
3727 the statement. */
3728 cp_parser_skip_to_end_of_statement (parser);
3729 /* If the next token is now a `;', consume it. */
3730 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3731 cp_lexer_consume_token (parser->lexer);
3732 }
3733 }
3734
3735 /* Skip tokens until we have consumed an entire block, or until we
3736 have consumed a non-nested `;'. */
3737
3738 static void
3739 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3740 {
3741 int nesting_depth = 0;
3742
3743 /* Unwind generic function template scope if necessary. */
3744 if (parser->fully_implicit_function_template_p)
3745 abort_fully_implicit_template (parser);
3746
3747 while (nesting_depth >= 0)
3748 {
3749 cp_token *token = cp_lexer_peek_token (parser->lexer);
3750
3751 switch (token->type)
3752 {
3753 case CPP_PRAGMA_EOL:
3754 if (!parser->lexer->in_pragma)
3755 break;
3756 /* FALLTHRU */
3757 case CPP_EOF:
3758 /* If we've run out of tokens, stop. */
3759 return;
3760
3761 case CPP_SEMICOLON:
3762 /* Stop if this is an unnested ';'. */
3763 if (!nesting_depth)
3764 nesting_depth = -1;
3765 break;
3766
3767 case CPP_CLOSE_BRACE:
3768 /* Stop if this is an unnested '}', or closes the outermost
3769 nesting level. */
3770 nesting_depth--;
3771 if (nesting_depth < 0)
3772 return;
3773 if (!nesting_depth)
3774 nesting_depth = -1;
3775 break;
3776
3777 case CPP_OPEN_BRACE:
3778 /* Nest. */
3779 nesting_depth++;
3780 break;
3781
3782 default:
3783 break;
3784 }
3785
3786 /* Consume the token. */
3787 cp_lexer_consume_token (parser->lexer);
3788 }
3789 }
3790
3791 /* Skip tokens until a non-nested closing curly brace is the next
3792 token, or there are no more tokens. Return true in the first case,
3793 false otherwise. */
3794
3795 static bool
3796 cp_parser_skip_to_closing_brace (cp_parser *parser)
3797 {
3798 unsigned nesting_depth = 0;
3799
3800 while (true)
3801 {
3802 cp_token *token = cp_lexer_peek_token (parser->lexer);
3803
3804 switch (token->type)
3805 {
3806 case CPP_PRAGMA_EOL:
3807 if (!parser->lexer->in_pragma)
3808 break;
3809 /* FALLTHRU */
3810 case CPP_EOF:
3811 /* If we've run out of tokens, stop. */
3812 return false;
3813
3814 case CPP_CLOSE_BRACE:
3815 /* If the next token is a non-nested `}', then we have reached
3816 the end of the current block. */
3817 if (nesting_depth-- == 0)
3818 return true;
3819 break;
3820
3821 case CPP_OPEN_BRACE:
3822 /* If it the next token is a `{', then we are entering a new
3823 block. Consume the entire block. */
3824 ++nesting_depth;
3825 break;
3826
3827 default:
3828 break;
3829 }
3830
3831 /* Consume the token. */
3832 cp_lexer_consume_token (parser->lexer);
3833 }
3834 }
3835
3836 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3837 parameter is the PRAGMA token, allowing us to purge the entire pragma
3838 sequence. */
3839
3840 static void
3841 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3842 {
3843 cp_token *token;
3844
3845 parser->lexer->in_pragma = false;
3846
3847 do
3848 token = cp_lexer_consume_token (parser->lexer);
3849 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3850
3851 /* Ensure that the pragma is not parsed again. */
3852 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3853 }
3854
3855 /* Require pragma end of line, resyncing with it as necessary. The
3856 arguments are as for cp_parser_skip_to_pragma_eol. */
3857
3858 static void
3859 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3860 {
3861 parser->lexer->in_pragma = false;
3862 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3863 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3864 }
3865
3866 /* This is a simple wrapper around make_typename_type. When the id is
3867 an unresolved identifier node, we can provide a superior diagnostic
3868 using cp_parser_diagnose_invalid_type_name. */
3869
3870 static tree
3871 cp_parser_make_typename_type (cp_parser *parser, tree id,
3872 location_t id_location)
3873 {
3874 tree result;
3875 if (identifier_p (id))
3876 {
3877 result = make_typename_type (parser->scope, id, typename_type,
3878 /*complain=*/tf_none);
3879 if (result == error_mark_node)
3880 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3881 return result;
3882 }
3883 return make_typename_type (parser->scope, id, typename_type, tf_error);
3884 }
3885
3886 /* This is a wrapper around the
3887 make_{pointer,ptrmem,reference}_declarator functions that decides
3888 which one to call based on the CODE and CLASS_TYPE arguments. The
3889 CODE argument should be one of the values returned by
3890 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3891 appertain to the pointer or reference. */
3892
3893 static cp_declarator *
3894 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3895 cp_cv_quals cv_qualifiers,
3896 cp_declarator *target,
3897 tree attributes)
3898 {
3899 if (code == ERROR_MARK || target == cp_error_declarator)
3900 return cp_error_declarator;
3901
3902 if (code == INDIRECT_REF)
3903 if (class_type == NULL_TREE)
3904 return make_pointer_declarator (cv_qualifiers, target, attributes);
3905 else
3906 return make_ptrmem_declarator (cv_qualifiers, class_type,
3907 target, attributes);
3908 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3909 return make_reference_declarator (cv_qualifiers, target,
3910 false, attributes);
3911 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3912 return make_reference_declarator (cv_qualifiers, target,
3913 true, attributes);
3914 gcc_unreachable ();
3915 }
3916
3917 /* Create a new C++ parser. */
3918
3919 static cp_parser *
3920 cp_parser_new (void)
3921 {
3922 cp_parser *parser;
3923 cp_lexer *lexer;
3924 unsigned i;
3925
3926 /* cp_lexer_new_main is called before doing GC allocation because
3927 cp_lexer_new_main might load a PCH file. */
3928 lexer = cp_lexer_new_main ();
3929
3930 /* Initialize the binops_by_token so that we can get the tree
3931 directly from the token. */
3932 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3933 binops_by_token[binops[i].token_type] = binops[i];
3934
3935 parser = ggc_cleared_alloc<cp_parser> ();
3936 parser->lexer = lexer;
3937 parser->context = cp_parser_context_new (NULL);
3938
3939 /* For now, we always accept GNU extensions. */
3940 parser->allow_gnu_extensions_p = 1;
3941
3942 /* The `>' token is a greater-than operator, not the end of a
3943 template-id. */
3944 parser->greater_than_is_operator_p = true;
3945
3946 parser->default_arg_ok_p = true;
3947
3948 /* We are not parsing a constant-expression. */
3949 parser->integral_constant_expression_p = false;
3950 parser->allow_non_integral_constant_expression_p = false;
3951 parser->non_integral_constant_expression_p = false;
3952
3953 /* Local variable names are not forbidden. */
3954 parser->local_variables_forbidden_p = false;
3955
3956 /* We are not processing an `extern "C"' declaration. */
3957 parser->in_unbraced_linkage_specification_p = false;
3958
3959 /* We are not processing a declarator. */
3960 parser->in_declarator_p = false;
3961
3962 /* We are not processing a template-argument-list. */
3963 parser->in_template_argument_list_p = false;
3964
3965 /* We are not in an iteration statement. */
3966 parser->in_statement = 0;
3967
3968 /* We are not in a switch statement. */
3969 parser->in_switch_statement_p = false;
3970
3971 /* We are not parsing a type-id inside an expression. */
3972 parser->in_type_id_in_expr_p = false;
3973
3974 /* String literals should be translated to the execution character set. */
3975 parser->translate_strings_p = true;
3976
3977 /* We are not parsing a function body. */
3978 parser->in_function_body = false;
3979
3980 /* We can correct until told otherwise. */
3981 parser->colon_corrects_to_scope_p = true;
3982
3983 /* The unparsed function queue is empty. */
3984 push_unparsed_function_queues (parser);
3985
3986 /* There are no classes being defined. */
3987 parser->num_classes_being_defined = 0;
3988
3989 /* No template parameters apply. */
3990 parser->num_template_parameter_lists = 0;
3991
3992 /* Special parsing data structures. */
3993 parser->omp_declare_simd = NULL;
3994 parser->oacc_routine = NULL;
3995
3996 /* Not declaring an implicit function template. */
3997 parser->auto_is_implicit_function_template_parm_p = false;
3998 parser->fully_implicit_function_template_p = false;
3999 parser->implicit_template_parms = 0;
4000 parser->implicit_template_scope = 0;
4001
4002 /* Allow constrained-type-specifiers. */
4003 parser->prevent_constrained_type_specifiers = 0;
4004
4005 /* We haven't yet seen an 'extern "C"'. */
4006 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4007
4008 return parser;
4009 }
4010
4011 /* Create a cp_lexer structure which will emit the tokens in CACHE
4012 and push it onto the parser's lexer stack. This is used for delayed
4013 parsing of in-class method bodies and default arguments, and should
4014 not be confused with tentative parsing. */
4015 static void
4016 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4017 {
4018 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4019 lexer->next = parser->lexer;
4020 parser->lexer = lexer;
4021
4022 /* Move the current source position to that of the first token in the
4023 new lexer. */
4024 cp_lexer_set_source_position_from_token (lexer->next_token);
4025 }
4026
4027 /* Pop the top lexer off the parser stack. This is never used for the
4028 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4029 static void
4030 cp_parser_pop_lexer (cp_parser *parser)
4031 {
4032 cp_lexer *lexer = parser->lexer;
4033 parser->lexer = lexer->next;
4034 cp_lexer_destroy (lexer);
4035
4036 /* Put the current source position back where it was before this
4037 lexer was pushed. */
4038 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4039 }
4040
4041 /* Lexical conventions [gram.lex] */
4042
4043 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4044 identifier. */
4045
4046 static cp_expr
4047 cp_parser_identifier (cp_parser* parser)
4048 {
4049 cp_token *token;
4050
4051 /* Look for the identifier. */
4052 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4053 /* Return the value. */
4054 if (token)
4055 return cp_expr (token->u.value, token->location);
4056 else
4057 return error_mark_node;
4058 }
4059
4060 /* Parse a sequence of adjacent string constants. Returns a
4061 TREE_STRING representing the combined, nul-terminated string
4062 constant. If TRANSLATE is true, translate the string to the
4063 execution character set. If WIDE_OK is true, a wide string is
4064 invalid here.
4065
4066 C++98 [lex.string] says that if a narrow string literal token is
4067 adjacent to a wide string literal token, the behavior is undefined.
4068 However, C99 6.4.5p4 says that this results in a wide string literal.
4069 We follow C99 here, for consistency with the C front end.
4070
4071 This code is largely lifted from lex_string() in c-lex.c.
4072
4073 FUTURE: ObjC++ will need to handle @-strings here. */
4074 static cp_expr
4075 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4076 bool lookup_udlit = true)
4077 {
4078 tree value;
4079 size_t count;
4080 struct obstack str_ob;
4081 struct obstack loc_ob;
4082 cpp_string str, istr, *strs;
4083 cp_token *tok;
4084 enum cpp_ttype type, curr_type;
4085 int have_suffix_p = 0;
4086 tree string_tree;
4087 tree suffix_id = NULL_TREE;
4088 bool curr_tok_is_userdef_p = false;
4089
4090 tok = cp_lexer_peek_token (parser->lexer);
4091 if (!cp_parser_is_string_literal (tok))
4092 {
4093 cp_parser_error (parser, "expected string-literal");
4094 return error_mark_node;
4095 }
4096
4097 location_t loc = tok->location;
4098
4099 if (cpp_userdef_string_p (tok->type))
4100 {
4101 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4102 curr_type = cpp_userdef_string_remove_type (tok->type);
4103 curr_tok_is_userdef_p = true;
4104 }
4105 else
4106 {
4107 string_tree = tok->u.value;
4108 curr_type = tok->type;
4109 }
4110 type = curr_type;
4111
4112 /* Try to avoid the overhead of creating and destroying an obstack
4113 for the common case of just one string. */
4114 if (!cp_parser_is_string_literal
4115 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4116 {
4117 cp_lexer_consume_token (parser->lexer);
4118
4119 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4120 str.len = TREE_STRING_LENGTH (string_tree);
4121 count = 1;
4122
4123 if (curr_tok_is_userdef_p)
4124 {
4125 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4126 have_suffix_p = 1;
4127 curr_type = cpp_userdef_string_remove_type (tok->type);
4128 }
4129 else
4130 curr_type = tok->type;
4131
4132 strs = &str;
4133 }
4134 else
4135 {
4136 location_t last_tok_loc = tok->location;
4137 gcc_obstack_init (&str_ob);
4138 gcc_obstack_init (&loc_ob);
4139 count = 0;
4140
4141 do
4142 {
4143 cp_lexer_consume_token (parser->lexer);
4144 count++;
4145 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4146 str.len = TREE_STRING_LENGTH (string_tree);
4147
4148 if (curr_tok_is_userdef_p)
4149 {
4150 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4151 if (have_suffix_p == 0)
4152 {
4153 suffix_id = curr_suffix_id;
4154 have_suffix_p = 1;
4155 }
4156 else if (have_suffix_p == 1
4157 && curr_suffix_id != suffix_id)
4158 {
4159 error ("inconsistent user-defined literal suffixes"
4160 " %qD and %qD in string literal",
4161 suffix_id, curr_suffix_id);
4162 have_suffix_p = -1;
4163 }
4164 curr_type = cpp_userdef_string_remove_type (tok->type);
4165 }
4166 else
4167 curr_type = tok->type;
4168
4169 if (type != curr_type)
4170 {
4171 if (type == CPP_STRING)
4172 type = curr_type;
4173 else if (curr_type != CPP_STRING)
4174 {
4175 rich_location rich_loc (line_table, tok->location);
4176 rich_loc.add_range (last_tok_loc);
4177 error_at (&rich_loc,
4178 "unsupported non-standard concatenation "
4179 "of string literals");
4180 }
4181 }
4182
4183 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4184 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4185
4186 last_tok_loc = tok->location;
4187
4188 tok = cp_lexer_peek_token (parser->lexer);
4189 if (cpp_userdef_string_p (tok->type))
4190 {
4191 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4192 curr_type = cpp_userdef_string_remove_type (tok->type);
4193 curr_tok_is_userdef_p = true;
4194 }
4195 else
4196 {
4197 string_tree = tok->u.value;
4198 curr_type = tok->type;
4199 curr_tok_is_userdef_p = false;
4200 }
4201 }
4202 while (cp_parser_is_string_literal (tok));
4203
4204 /* A string literal built by concatenation has its caret=start at
4205 the start of the initial string, and its finish at the finish of
4206 the final string literal. */
4207 loc = make_location (loc, loc, get_finish (last_tok_loc));
4208
4209 strs = (cpp_string *) obstack_finish (&str_ob);
4210 }
4211
4212 if (type != CPP_STRING && !wide_ok)
4213 {
4214 cp_parser_error (parser, "a wide string is invalid in this context");
4215 type = CPP_STRING;
4216 }
4217
4218 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4219 (parse_in, strs, count, &istr, type))
4220 {
4221 value = build_string (istr.len, (const char *)istr.text);
4222 free (CONST_CAST (unsigned char *, istr.text));
4223 if (count > 1)
4224 {
4225 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4226 gcc_assert (g_string_concat_db);
4227 g_string_concat_db->record_string_concatenation (count, locs);
4228 }
4229
4230 switch (type)
4231 {
4232 default:
4233 case CPP_STRING:
4234 case CPP_UTF8STRING:
4235 TREE_TYPE (value) = char_array_type_node;
4236 break;
4237 case CPP_STRING16:
4238 TREE_TYPE (value) = char16_array_type_node;
4239 break;
4240 case CPP_STRING32:
4241 TREE_TYPE (value) = char32_array_type_node;
4242 break;
4243 case CPP_WSTRING:
4244 TREE_TYPE (value) = wchar_array_type_node;
4245 break;
4246 }
4247
4248 value = fix_string_type (value);
4249
4250 if (have_suffix_p)
4251 {
4252 tree literal = build_userdef_literal (suffix_id, value,
4253 OT_NONE, NULL_TREE);
4254 if (lookup_udlit)
4255 value = cp_parser_userdef_string_literal (literal);
4256 else
4257 value = literal;
4258 }
4259 }
4260 else
4261 /* cpp_interpret_string has issued an error. */
4262 value = error_mark_node;
4263
4264 if (count > 1)
4265 {
4266 obstack_free (&str_ob, 0);
4267 obstack_free (&loc_ob, 0);
4268 }
4269
4270 return cp_expr (value, loc);
4271 }
4272
4273 /* Look up a literal operator with the name and the exact arguments. */
4274
4275 static tree
4276 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4277 {
4278 tree decl = lookup_name (name);
4279 if (!decl || !is_overloaded_fn (decl))
4280 return error_mark_node;
4281
4282 for (lkp_iterator iter (decl); iter; ++iter)
4283 {
4284 tree fn = *iter;
4285
4286 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4287 {
4288 unsigned int ix;
4289 bool found = true;
4290
4291 for (ix = 0;
4292 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4293 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4294 {
4295 tree tparm = TREE_VALUE (parmtypes);
4296 tree targ = TREE_TYPE ((*args)[ix]);
4297 bool ptr = TYPE_PTR_P (tparm);
4298 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4299 if ((ptr || arr || !same_type_p (tparm, targ))
4300 && (!ptr || !arr
4301 || !same_type_p (TREE_TYPE (tparm),
4302 TREE_TYPE (targ))))
4303 found = false;
4304 }
4305
4306 if (found
4307 && ix == vec_safe_length (args)
4308 /* May be this should be sufficient_parms_p instead,
4309 depending on how exactly should user-defined literals
4310 work in presence of default arguments on the literal
4311 operator parameters. */
4312 && parmtypes == void_list_node)
4313 return decl;
4314 }
4315 }
4316
4317 return error_mark_node;
4318 }
4319
4320 /* Parse a user-defined char constant. Returns a call to a user-defined
4321 literal operator taking the character as an argument. */
4322
4323 static cp_expr
4324 cp_parser_userdef_char_literal (cp_parser *parser)
4325 {
4326 cp_token *token = cp_lexer_consume_token (parser->lexer);
4327 tree literal = token->u.value;
4328 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4329 tree value = USERDEF_LITERAL_VALUE (literal);
4330 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4331 tree decl, result;
4332
4333 /* Build up a call to the user-defined operator */
4334 /* Lookup the name we got back from the id-expression. */
4335 vec<tree, va_gc> *args = make_tree_vector ();
4336 vec_safe_push (args, value);
4337 decl = lookup_literal_operator (name, args);
4338 if (!decl || decl == error_mark_node)
4339 {
4340 error ("unable to find character literal operator %qD with %qT argument",
4341 name, TREE_TYPE (value));
4342 release_tree_vector (args);
4343 return error_mark_node;
4344 }
4345 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4346 release_tree_vector (args);
4347 return result;
4348 }
4349
4350 /* A subroutine of cp_parser_userdef_numeric_literal to
4351 create a char... template parameter pack from a string node. */
4352
4353 static tree
4354 make_char_string_pack (tree value)
4355 {
4356 tree charvec;
4357 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4358 const char *str = TREE_STRING_POINTER (value);
4359 int i, len = TREE_STRING_LENGTH (value) - 1;
4360 tree argvec = make_tree_vec (1);
4361
4362 /* Fill in CHARVEC with all of the parameters. */
4363 charvec = make_tree_vec (len);
4364 for (i = 0; i < len; ++i)
4365 {
4366 unsigned char s[3] = { '\'', str[i], '\'' };
4367 cpp_string in = { 3, s };
4368 cpp_string out = { 0, 0 };
4369 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4370 return NULL_TREE;
4371 gcc_assert (out.len == 2);
4372 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4373 out.text[0]);
4374 }
4375
4376 /* Build the argument packs. */
4377 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4378
4379 TREE_VEC_ELT (argvec, 0) = argpack;
4380
4381 return argvec;
4382 }
4383
4384 /* A subroutine of cp_parser_userdef_numeric_literal to
4385 create a char... template parameter pack from a string node. */
4386
4387 static tree
4388 make_string_pack (tree value)
4389 {
4390 tree charvec;
4391 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4392 const unsigned char *str
4393 = (const unsigned char *) TREE_STRING_POINTER (value);
4394 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4395 int len = TREE_STRING_LENGTH (value) / sz - 1;
4396 tree argvec = make_tree_vec (2);
4397
4398 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4399 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4400
4401 /* First template parm is character type. */
4402 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4403
4404 /* Fill in CHARVEC with all of the parameters. */
4405 charvec = make_tree_vec (len);
4406 for (int i = 0; i < len; ++i)
4407 TREE_VEC_ELT (charvec, i)
4408 = double_int_to_tree (str_char_type_node,
4409 double_int::from_buffer (str + i * sz, sz));
4410
4411 /* Build the argument packs. */
4412 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4413
4414 TREE_VEC_ELT (argvec, 1) = argpack;
4415
4416 return argvec;
4417 }
4418
4419 /* Parse a user-defined numeric constant. returns a call to a user-defined
4420 literal operator. */
4421
4422 static cp_expr
4423 cp_parser_userdef_numeric_literal (cp_parser *parser)
4424 {
4425 cp_token *token = cp_lexer_consume_token (parser->lexer);
4426 tree literal = token->u.value;
4427 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4428 tree value = USERDEF_LITERAL_VALUE (literal);
4429 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4430 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4431 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4432 tree decl, result;
4433 vec<tree, va_gc> *args;
4434
4435 /* Look for a literal operator taking the exact type of numeric argument
4436 as the literal value. */
4437 args = make_tree_vector ();
4438 vec_safe_push (args, value);
4439 decl = lookup_literal_operator (name, args);
4440 if (decl && decl != error_mark_node)
4441 {
4442 result = finish_call_expr (decl, &args, false, true,
4443 tf_warning_or_error);
4444
4445 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4446 {
4447 warning_at (token->location, OPT_Woverflow,
4448 "integer literal exceeds range of %qT type",
4449 long_long_unsigned_type_node);
4450 }
4451 else
4452 {
4453 if (overflow > 0)
4454 warning_at (token->location, OPT_Woverflow,
4455 "floating literal exceeds range of %qT type",
4456 long_double_type_node);
4457 else if (overflow < 0)
4458 warning_at (token->location, OPT_Woverflow,
4459 "floating literal truncated to zero");
4460 }
4461
4462 release_tree_vector (args);
4463 return result;
4464 }
4465 release_tree_vector (args);
4466
4467 /* If the numeric argument didn't work, look for a raw literal
4468 operator taking a const char* argument consisting of the number
4469 in string format. */
4470 args = make_tree_vector ();
4471 vec_safe_push (args, num_string);
4472 decl = lookup_literal_operator (name, args);
4473 if (decl && decl != error_mark_node)
4474 {
4475 result = finish_call_expr (decl, &args, false, true,
4476 tf_warning_or_error);
4477 release_tree_vector (args);
4478 return result;
4479 }
4480 release_tree_vector (args);
4481
4482 /* If the raw literal didn't work, look for a non-type template
4483 function with parameter pack char.... Call the function with
4484 template parameter characters representing the number. */
4485 args = make_tree_vector ();
4486 decl = lookup_literal_operator (name, args);
4487 if (decl && decl != error_mark_node)
4488 {
4489 tree tmpl_args = make_char_string_pack (num_string);
4490 if (tmpl_args == NULL_TREE)
4491 {
4492 error ("failed to translate literal to execution character set %qT",
4493 num_string);
4494 return error_mark_node;
4495 }
4496 decl = lookup_template_function (decl, tmpl_args);
4497 result = finish_call_expr (decl, &args, false, true,
4498 tf_warning_or_error);
4499 release_tree_vector (args);
4500 return result;
4501 }
4502
4503 release_tree_vector (args);
4504
4505 /* In C++14 the standard library defines complex number suffixes that
4506 conflict with GNU extensions. Prefer them if <complex> is #included. */
4507 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4508 bool i14 = (cxx_dialect > cxx11
4509 && (id_equal (suffix_id, "i")
4510 || id_equal (suffix_id, "if")
4511 || id_equal (suffix_id, "il")));
4512 diagnostic_t kind = DK_ERROR;
4513 int opt = 0;
4514
4515 if (i14 && ext)
4516 {
4517 tree cxlit = lookup_qualified_name (std_node,
4518 get_identifier ("complex_literals"),
4519 0, false, false);
4520 if (cxlit == error_mark_node)
4521 {
4522 /* No <complex>, so pedwarn and use GNU semantics. */
4523 kind = DK_PEDWARN;
4524 opt = OPT_Wpedantic;
4525 }
4526 }
4527
4528 bool complained
4529 = emit_diagnostic (kind, input_location, opt,
4530 "unable to find numeric literal operator %qD", name);
4531
4532 if (!complained)
4533 /* Don't inform either. */;
4534 else if (i14)
4535 {
4536 inform (token->location, "add %<using namespace std::complex_literals%> "
4537 "(from <complex>) to enable the C++14 user-defined literal "
4538 "suffixes");
4539 if (ext)
4540 inform (token->location, "or use %<j%> instead of %<i%> for the "
4541 "GNU built-in suffix");
4542 }
4543 else if (!ext)
4544 inform (token->location, "use -fext-numeric-literals "
4545 "to enable more built-in suffixes");
4546
4547 if (kind == DK_ERROR)
4548 value = error_mark_node;
4549 else
4550 {
4551 /* Use the built-in semantics. */
4552 tree type;
4553 if (id_equal (suffix_id, "i"))
4554 {
4555 if (TREE_CODE (value) == INTEGER_CST)
4556 type = integer_type_node;
4557 else
4558 type = double_type_node;
4559 }
4560 else if (id_equal (suffix_id, "if"))
4561 type = float_type_node;
4562 else /* if (id_equal (suffix_id, "il")) */
4563 type = long_double_type_node;
4564
4565 value = build_complex (build_complex_type (type),
4566 fold_convert (type, integer_zero_node),
4567 fold_convert (type, value));
4568 }
4569
4570 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4571 /* Avoid repeated diagnostics. */
4572 token->u.value = value;
4573 return value;
4574 }
4575
4576 /* Parse a user-defined string constant. Returns a call to a user-defined
4577 literal operator taking a character pointer and the length of the string
4578 as arguments. */
4579
4580 static tree
4581 cp_parser_userdef_string_literal (tree literal)
4582 {
4583 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4584 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4585 tree value = USERDEF_LITERAL_VALUE (literal);
4586 int len = TREE_STRING_LENGTH (value)
4587 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4588 tree decl;
4589
4590 /* Build up a call to the user-defined operator. */
4591 /* Lookup the name we got back from the id-expression. */
4592 releasing_vec rargs;
4593 vec<tree, va_gc> *&args = rargs.get_ref();
4594 vec_safe_push (args, value);
4595 vec_safe_push (args, build_int_cst (size_type_node, len));
4596 decl = lookup_literal_operator (name, args);
4597
4598 if (decl && decl != error_mark_node)
4599 return finish_call_expr (decl, &args, false, true,
4600 tf_warning_or_error);
4601
4602 /* Look for a suitable template function, either (C++20) with a single
4603 parameter of class type, or (N3599) with typename parameter CharT and
4604 parameter pack CharT... */
4605 args->truncate (0);
4606 decl = lookup_literal_operator (name, args);
4607 if (decl && decl != error_mark_node)
4608 {
4609 /* Use resolve_nondeduced_context to try to choose one form of template
4610 or the other. */
4611 tree tmpl_args = make_tree_vec (1);
4612 TREE_VEC_ELT (tmpl_args, 0) = value;
4613 decl = lookup_template_function (decl, tmpl_args);
4614 tree res = resolve_nondeduced_context (decl, tf_none);
4615 if (DECL_P (res))
4616 decl = res;
4617 else
4618 {
4619 TREE_OPERAND (decl, 1) = make_string_pack (value);
4620 res = resolve_nondeduced_context (decl, tf_none);
4621 if (DECL_P (res))
4622 decl = res;
4623 }
4624 if (!DECL_P (decl) && cxx_dialect > cxx17)
4625 TREE_OPERAND (decl, 1) = tmpl_args;
4626 return finish_call_expr (decl, &args, false, true,
4627 tf_warning_or_error);
4628 }
4629
4630 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4631 name, TREE_TYPE (value), size_type_node);
4632 return error_mark_node;
4633 }
4634
4635
4636 /* Basic concepts [gram.basic] */
4637
4638 /* Parse a translation-unit.
4639
4640 translation-unit:
4641 declaration-seq [opt] */
4642
4643 static void
4644 cp_parser_translation_unit (cp_parser* parser)
4645 {
4646 gcc_checking_assert (!cp_error_declarator);
4647
4648 /* Create the declarator obstack. */
4649 gcc_obstack_init (&declarator_obstack);
4650 /* Create the error declarator. */
4651 cp_error_declarator = make_declarator (cdk_error);
4652 /* Create the empty parameter list. */
4653 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4654 UNKNOWN_LOCATION);
4655 /* Remember where the base of the declarator obstack lies. */
4656 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4657
4658 bool implicit_extern_c = false;
4659
4660 for (;;)
4661 {
4662 cp_token *token = cp_lexer_peek_token (parser->lexer);
4663
4664 /* If we're entering or exiting a region that's implicitly
4665 extern "C", modify the lang context appropriately. */
4666 if (implicit_extern_c
4667 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4668 {
4669 implicit_extern_c = !implicit_extern_c;
4670 if (implicit_extern_c)
4671 push_lang_context (lang_name_c);
4672 else
4673 pop_lang_context ();
4674 }
4675
4676 if (token->type == CPP_EOF)
4677 break;
4678
4679 if (token->type == CPP_CLOSE_BRACE)
4680 {
4681 cp_parser_error (parser, "expected declaration");
4682 cp_lexer_consume_token (parser->lexer);
4683 /* If the next token is now a `;', consume it. */
4684 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4685 cp_lexer_consume_token (parser->lexer);
4686 }
4687 else
4688 cp_parser_toplevel_declaration (parser);
4689 }
4690
4691 /* Get rid of the token array; we don't need it any more. */
4692 cp_lexer_destroy (parser->lexer);
4693 parser->lexer = NULL;
4694
4695 /* The EOF should have reset this. */
4696 gcc_checking_assert (!implicit_extern_c);
4697
4698 /* Make sure the declarator obstack was fully cleaned up. */
4699 gcc_assert (obstack_next_free (&declarator_obstack)
4700 == declarator_obstack_base);
4701 }
4702
4703 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4704 decltype context. */
4705
4706 static inline tsubst_flags_t
4707 complain_flags (bool decltype_p)
4708 {
4709 tsubst_flags_t complain = tf_warning_or_error;
4710 if (decltype_p)
4711 complain |= tf_decltype;
4712 return complain;
4713 }
4714
4715 /* We're about to parse a collection of statements. If we're currently
4716 parsing tentatively, set up a firewall so that any nested
4717 cp_parser_commit_to_tentative_parse won't affect the current context. */
4718
4719 static cp_token_position
4720 cp_parser_start_tentative_firewall (cp_parser *parser)
4721 {
4722 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4723 return 0;
4724
4725 cp_parser_parse_tentatively (parser);
4726 cp_parser_commit_to_topmost_tentative_parse (parser);
4727 return cp_lexer_token_position (parser->lexer, false);
4728 }
4729
4730 /* We've finished parsing the collection of statements. Wrap up the
4731 firewall and replace the relevant tokens with the parsed form. */
4732
4733 static void
4734 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4735 tree expr)
4736 {
4737 if (!start)
4738 return;
4739
4740 /* Finish the firewall level. */
4741 cp_parser_parse_definitely (parser);
4742 /* And remember the result of the parse for when we try again. */
4743 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4744 token->type = CPP_PREPARSED_EXPR;
4745 token->u.value = expr;
4746 token->keyword = RID_MAX;
4747 cp_lexer_purge_tokens_after (parser->lexer, start);
4748 }
4749
4750 /* Like the above functions, but let the user modify the tokens. Used by
4751 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4752 later parses, so it makes sense to localize the effects of
4753 cp_parser_commit_to_tentative_parse. */
4754
4755 struct tentative_firewall
4756 {
4757 cp_parser *parser;
4758 bool set;
4759
4760 tentative_firewall (cp_parser *p): parser(p)
4761 {
4762 /* If we're currently parsing tentatively, start a committed level as a
4763 firewall and then an inner tentative parse. */
4764 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4765 {
4766 cp_parser_parse_tentatively (parser);
4767 cp_parser_commit_to_topmost_tentative_parse (parser);
4768 cp_parser_parse_tentatively (parser);
4769 }
4770 }
4771
4772 ~tentative_firewall()
4773 {
4774 if (set)
4775 {
4776 /* Finish the inner tentative parse and the firewall, propagating any
4777 uncommitted error state to the outer tentative parse. */
4778 bool err = cp_parser_error_occurred (parser);
4779 cp_parser_parse_definitely (parser);
4780 cp_parser_parse_definitely (parser);
4781 if (err)
4782 cp_parser_simulate_error (parser);
4783 }
4784 }
4785 };
4786
4787 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4788 This class is for tracking such a matching pair of symbols.
4789 In particular, it tracks the location of the first token,
4790 so that if the second token is missing, we can highlight the
4791 location of the first token when notifying the user about the
4792 problem. */
4793
4794 template <typename traits_t>
4795 class token_pair
4796 {
4797 public:
4798 /* token_pair's ctor. */
4799 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4800
4801 /* If the next token is the opening symbol for this pair, consume it and
4802 return true.
4803 Otherwise, issue an error and return false.
4804 In either case, record the location of the opening token. */
4805
4806 bool require_open (cp_parser *parser)
4807 {
4808 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4809 return cp_parser_require (parser, traits_t::open_token_type,
4810 traits_t::required_token_open);
4811 }
4812
4813 /* Consume the next token from PARSER, recording its location as
4814 that of the opening token within the pair. */
4815
4816 cp_token * consume_open (cp_parser *parser)
4817 {
4818 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4819 gcc_assert (tok->type == traits_t::open_token_type);
4820 m_open_loc = tok->location;
4821 return tok;
4822 }
4823
4824 /* If the next token is the closing symbol for this pair, consume it
4825 and return it.
4826 Otherwise, issue an error, highlighting the location of the
4827 corresponding opening token, and return NULL. */
4828
4829 cp_token *require_close (cp_parser *parser) const
4830 {
4831 return cp_parser_require (parser, traits_t::close_token_type,
4832 traits_t::required_token_close,
4833 m_open_loc);
4834 }
4835
4836 private:
4837 location_t m_open_loc;
4838 };
4839
4840 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4841
4842 struct matching_paren_traits
4843 {
4844 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4845 static const enum required_token required_token_open = RT_OPEN_PAREN;
4846 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4847 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4848 };
4849
4850 /* "matching_parens" is a token_pair<T> class for tracking matching
4851 pairs of parentheses. */
4852
4853 typedef token_pair<matching_paren_traits> matching_parens;
4854
4855 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4856
4857 struct matching_brace_traits
4858 {
4859 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4860 static const enum required_token required_token_open = RT_OPEN_BRACE;
4861 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4862 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4863 };
4864
4865 /* "matching_braces" is a token_pair<T> class for tracking matching
4866 pairs of braces. */
4867
4868 typedef token_pair<matching_brace_traits> matching_braces;
4869
4870
4871 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4872 enclosing parentheses. */
4873
4874 static cp_expr
4875 cp_parser_statement_expr (cp_parser *parser)
4876 {
4877 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4878
4879 /* Consume the '('. */
4880 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4881 matching_parens parens;
4882 parens.consume_open (parser);
4883 /* Start the statement-expression. */
4884 tree expr = begin_stmt_expr ();
4885 /* Parse the compound-statement. */
4886 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4887 /* Finish up. */
4888 expr = finish_stmt_expr (expr, false);
4889 /* Consume the ')'. */
4890 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4891 if (!parens.require_close (parser))
4892 cp_parser_skip_to_end_of_statement (parser);
4893
4894 cp_parser_end_tentative_firewall (parser, start, expr);
4895 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4896 return cp_expr (expr, combined_loc);
4897 }
4898
4899 /* Expressions [gram.expr] */
4900
4901 /* Parse a fold-operator.
4902
4903 fold-operator:
4904 - * / % ^ & | = < > << >>
4905 = -= *= /= %= ^= &= |= <<= >>=
4906 == != <= >= && || , .* ->*
4907
4908 This returns the tree code corresponding to the matched operator
4909 as an int. When the current token matches a compound assignment
4910 opertor, the resulting tree code is the negative value of the
4911 non-assignment operator. */
4912
4913 static int
4914 cp_parser_fold_operator (cp_token *token)
4915 {
4916 switch (token->type)
4917 {
4918 case CPP_PLUS: return PLUS_EXPR;
4919 case CPP_MINUS: return MINUS_EXPR;
4920 case CPP_MULT: return MULT_EXPR;
4921 case CPP_DIV: return TRUNC_DIV_EXPR;
4922 case CPP_MOD: return TRUNC_MOD_EXPR;
4923 case CPP_XOR: return BIT_XOR_EXPR;
4924 case CPP_AND: return BIT_AND_EXPR;
4925 case CPP_OR: return BIT_IOR_EXPR;
4926 case CPP_LSHIFT: return LSHIFT_EXPR;
4927 case CPP_RSHIFT: return RSHIFT_EXPR;
4928
4929 case CPP_EQ: return -NOP_EXPR;
4930 case CPP_PLUS_EQ: return -PLUS_EXPR;
4931 case CPP_MINUS_EQ: return -MINUS_EXPR;
4932 case CPP_MULT_EQ: return -MULT_EXPR;
4933 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4934 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4935 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4936 case CPP_AND_EQ: return -BIT_AND_EXPR;
4937 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4938 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4939 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4940
4941 case CPP_EQ_EQ: return EQ_EXPR;
4942 case CPP_NOT_EQ: return NE_EXPR;
4943 case CPP_LESS: return LT_EXPR;
4944 case CPP_GREATER: return GT_EXPR;
4945 case CPP_LESS_EQ: return LE_EXPR;
4946 case CPP_GREATER_EQ: return GE_EXPR;
4947
4948 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4949 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4950
4951 case CPP_COMMA: return COMPOUND_EXPR;
4952
4953 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4954 case CPP_DEREF_STAR: return MEMBER_REF;
4955
4956 default: return ERROR_MARK;
4957 }
4958 }
4959
4960 /* Returns true if CODE indicates a binary expression, which is not allowed in
4961 the LHS of a fold-expression. More codes will need to be added to use this
4962 function in other contexts. */
4963
4964 static bool
4965 is_binary_op (tree_code code)
4966 {
4967 switch (code)
4968 {
4969 case PLUS_EXPR:
4970 case POINTER_PLUS_EXPR:
4971 case MINUS_EXPR:
4972 case MULT_EXPR:
4973 case TRUNC_DIV_EXPR:
4974 case TRUNC_MOD_EXPR:
4975 case BIT_XOR_EXPR:
4976 case BIT_AND_EXPR:
4977 case BIT_IOR_EXPR:
4978 case LSHIFT_EXPR:
4979 case RSHIFT_EXPR:
4980
4981 case MODOP_EXPR:
4982
4983 case EQ_EXPR:
4984 case NE_EXPR:
4985 case LE_EXPR:
4986 case GE_EXPR:
4987 case LT_EXPR:
4988 case GT_EXPR:
4989
4990 case TRUTH_ANDIF_EXPR:
4991 case TRUTH_ORIF_EXPR:
4992
4993 case COMPOUND_EXPR:
4994
4995 case DOTSTAR_EXPR:
4996 case MEMBER_REF:
4997 return true;
4998
4999 default:
5000 return false;
5001 }
5002 }
5003
5004 /* If the next token is a suitable fold operator, consume it and return as
5005 the function above. */
5006
5007 static int
5008 cp_parser_fold_operator (cp_parser *parser)
5009 {
5010 cp_token* token = cp_lexer_peek_token (parser->lexer);
5011 int code = cp_parser_fold_operator (token);
5012 if (code != ERROR_MARK)
5013 cp_lexer_consume_token (parser->lexer);
5014 return code;
5015 }
5016
5017 /* Parse a fold-expression.
5018
5019 fold-expression:
5020 ( ... folding-operator cast-expression)
5021 ( cast-expression folding-operator ... )
5022 ( cast-expression folding operator ... folding-operator cast-expression)
5023
5024 Note that the '(' and ')' are matched in primary expression. */
5025
5026 static cp_expr
5027 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5028 {
5029 cp_id_kind pidk;
5030
5031 // Left fold.
5032 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5033 {
5034 cp_lexer_consume_token (parser->lexer);
5035 int op = cp_parser_fold_operator (parser);
5036 if (op == ERROR_MARK)
5037 {
5038 cp_parser_error (parser, "expected binary operator");
5039 return error_mark_node;
5040 }
5041
5042 tree expr = cp_parser_cast_expression (parser, false, false,
5043 false, &pidk);
5044 if (expr == error_mark_node)
5045 return error_mark_node;
5046 return finish_left_unary_fold_expr (expr, op);
5047 }
5048
5049 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5050 int op = cp_parser_fold_operator (parser);
5051 if (op == ERROR_MARK)
5052 {
5053 cp_parser_error (parser, "expected binary operator");
5054 return error_mark_node;
5055 }
5056
5057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5058 {
5059 cp_parser_error (parser, "expected ...");
5060 return error_mark_node;
5061 }
5062 cp_lexer_consume_token (parser->lexer);
5063
5064 /* The operands of a fold-expression are cast-expressions, so binary or
5065 conditional expressions are not allowed. We check this here to avoid
5066 tentative parsing. */
5067 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5068 /* OK, the expression was parenthesized. */;
5069 else if (is_binary_op (TREE_CODE (expr1)))
5070 error_at (location_of (expr1),
5071 "binary expression in operand of fold-expression");
5072 else if (TREE_CODE (expr1) == COND_EXPR
5073 || (REFERENCE_REF_P (expr1)
5074 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5075 error_at (location_of (expr1),
5076 "conditional expression in operand of fold-expression");
5077
5078 // Right fold.
5079 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5080 return finish_right_unary_fold_expr (expr1, op);
5081
5082 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5083 {
5084 cp_parser_error (parser, "mismatched operator in fold-expression");
5085 return error_mark_node;
5086 }
5087 cp_lexer_consume_token (parser->lexer);
5088
5089 // Binary left or right fold.
5090 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5091 if (expr2 == error_mark_node)
5092 return error_mark_node;
5093 return finish_binary_fold_expr (expr1, expr2, op);
5094 }
5095
5096 /* Parse a primary-expression.
5097
5098 primary-expression:
5099 literal
5100 this
5101 ( expression )
5102 id-expression
5103 lambda-expression (C++11)
5104
5105 GNU Extensions:
5106
5107 primary-expression:
5108 ( compound-statement )
5109 __builtin_va_arg ( assignment-expression , type-id )
5110 __builtin_offsetof ( type-id , offsetof-expression )
5111
5112 C++ Extensions:
5113 __has_nothrow_assign ( type-id )
5114 __has_nothrow_constructor ( type-id )
5115 __has_nothrow_copy ( type-id )
5116 __has_trivial_assign ( type-id )
5117 __has_trivial_constructor ( type-id )
5118 __has_trivial_copy ( type-id )
5119 __has_trivial_destructor ( type-id )
5120 __has_virtual_destructor ( type-id )
5121 __is_abstract ( type-id )
5122 __is_base_of ( type-id , type-id )
5123 __is_class ( type-id )
5124 __is_empty ( type-id )
5125 __is_enum ( type-id )
5126 __is_final ( type-id )
5127 __is_literal_type ( type-id )
5128 __is_pod ( type-id )
5129 __is_polymorphic ( type-id )
5130 __is_std_layout ( type-id )
5131 __is_trivial ( type-id )
5132 __is_union ( type-id )
5133
5134 Objective-C++ Extension:
5135
5136 primary-expression:
5137 objc-expression
5138
5139 literal:
5140 __null
5141
5142 ADDRESS_P is true iff this expression was immediately preceded by
5143 "&" and therefore might denote a pointer-to-member. CAST_P is true
5144 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5145 true iff this expression is a template argument.
5146
5147 Returns a representation of the expression. Upon return, *IDK
5148 indicates what kind of id-expression (if any) was present. */
5149
5150 static cp_expr
5151 cp_parser_primary_expression (cp_parser *parser,
5152 bool address_p,
5153 bool cast_p,
5154 bool template_arg_p,
5155 bool decltype_p,
5156 cp_id_kind *idk)
5157 {
5158 cp_token *token = NULL;
5159
5160 /* Assume the primary expression is not an id-expression. */
5161 *idk = CP_ID_KIND_NONE;
5162
5163 /* Peek at the next token. */
5164 token = cp_lexer_peek_token (parser->lexer);
5165 switch ((int) token->type)
5166 {
5167 /* literal:
5168 integer-literal
5169 character-literal
5170 floating-literal
5171 string-literal
5172 boolean-literal
5173 pointer-literal
5174 user-defined-literal */
5175 case CPP_CHAR:
5176 case CPP_CHAR16:
5177 case CPP_CHAR32:
5178 case CPP_WCHAR:
5179 case CPP_UTF8CHAR:
5180 case CPP_NUMBER:
5181 case CPP_PREPARSED_EXPR:
5182 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5183 return cp_parser_userdef_numeric_literal (parser);
5184 token = cp_lexer_consume_token (parser->lexer);
5185 if (TREE_CODE (token->u.value) == FIXED_CST)
5186 {
5187 error_at (token->location,
5188 "fixed-point types not supported in C++");
5189 return error_mark_node;
5190 }
5191 /* Floating-point literals are only allowed in an integral
5192 constant expression if they are cast to an integral or
5193 enumeration type. */
5194 if (TREE_CODE (token->u.value) == REAL_CST
5195 && parser->integral_constant_expression_p
5196 && pedantic)
5197 {
5198 /* CAST_P will be set even in invalid code like "int(2.7 +
5199 ...)". Therefore, we have to check that the next token
5200 is sure to end the cast. */
5201 if (cast_p)
5202 {
5203 cp_token *next_token;
5204
5205 next_token = cp_lexer_peek_token (parser->lexer);
5206 if (/* The comma at the end of an
5207 enumerator-definition. */
5208 next_token->type != CPP_COMMA
5209 /* The curly brace at the end of an enum-specifier. */
5210 && next_token->type != CPP_CLOSE_BRACE
5211 /* The end of a statement. */
5212 && next_token->type != CPP_SEMICOLON
5213 /* The end of the cast-expression. */
5214 && next_token->type != CPP_CLOSE_PAREN
5215 /* The end of an array bound. */
5216 && next_token->type != CPP_CLOSE_SQUARE
5217 /* The closing ">" in a template-argument-list. */
5218 && (next_token->type != CPP_GREATER
5219 || parser->greater_than_is_operator_p)
5220 /* C++0x only: A ">>" treated like two ">" tokens,
5221 in a template-argument-list. */
5222 && (next_token->type != CPP_RSHIFT
5223 || (cxx_dialect == cxx98)
5224 || parser->greater_than_is_operator_p))
5225 cast_p = false;
5226 }
5227
5228 /* If we are within a cast, then the constraint that the
5229 cast is to an integral or enumeration type will be
5230 checked at that point. If we are not within a cast, then
5231 this code is invalid. */
5232 if (!cast_p)
5233 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5234 }
5235 return (cp_expr (token->u.value, token->location)
5236 .maybe_add_location_wrapper ());
5237
5238 case CPP_CHAR_USERDEF:
5239 case CPP_CHAR16_USERDEF:
5240 case CPP_CHAR32_USERDEF:
5241 case CPP_WCHAR_USERDEF:
5242 case CPP_UTF8CHAR_USERDEF:
5243 return cp_parser_userdef_char_literal (parser);
5244
5245 case CPP_STRING:
5246 case CPP_STRING16:
5247 case CPP_STRING32:
5248 case CPP_WSTRING:
5249 case CPP_UTF8STRING:
5250 case CPP_STRING_USERDEF:
5251 case CPP_STRING16_USERDEF:
5252 case CPP_STRING32_USERDEF:
5253 case CPP_WSTRING_USERDEF:
5254 case CPP_UTF8STRING_USERDEF:
5255 /* ??? Should wide strings be allowed when parser->translate_strings_p
5256 is false (i.e. in attributes)? If not, we can kill the third
5257 argument to cp_parser_string_literal. */
5258 return (cp_parser_string_literal (parser,
5259 parser->translate_strings_p,
5260 true)
5261 .maybe_add_location_wrapper ());
5262
5263 case CPP_OPEN_PAREN:
5264 /* If we see `( { ' then we are looking at the beginning of
5265 a GNU statement-expression. */
5266 if (cp_parser_allow_gnu_extensions_p (parser)
5267 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5268 {
5269 /* Statement-expressions are not allowed by the standard. */
5270 pedwarn (token->location, OPT_Wpedantic,
5271 "ISO C++ forbids braced-groups within expressions");
5272
5273 /* And they're not allowed outside of a function-body; you
5274 cannot, for example, write:
5275
5276 int i = ({ int j = 3; j + 1; });
5277
5278 at class or namespace scope. */
5279 if (!parser->in_function_body
5280 || parser->in_template_argument_list_p)
5281 {
5282 error_at (token->location,
5283 "statement-expressions are not allowed outside "
5284 "functions nor in template-argument lists");
5285 cp_parser_skip_to_end_of_block_or_statement (parser);
5286 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5287 cp_lexer_consume_token (parser->lexer);
5288 return error_mark_node;
5289 }
5290 else
5291 return cp_parser_statement_expr (parser);
5292 }
5293 /* Otherwise it's a normal parenthesized expression. */
5294 {
5295 cp_expr expr;
5296 bool saved_greater_than_is_operator_p;
5297
5298 location_t open_paren_loc = token->location;
5299
5300 /* Consume the `('. */
5301 matching_parens parens;
5302 parens.consume_open (parser);
5303 /* Within a parenthesized expression, a `>' token is always
5304 the greater-than operator. */
5305 saved_greater_than_is_operator_p
5306 = parser->greater_than_is_operator_p;
5307 parser->greater_than_is_operator_p = true;
5308
5309 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5310 /* Left fold expression. */
5311 expr = NULL_TREE;
5312 else
5313 /* Parse the parenthesized expression. */
5314 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5315
5316 token = cp_lexer_peek_token (parser->lexer);
5317 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5318 {
5319 expr = cp_parser_fold_expression (parser, expr);
5320 if (expr != error_mark_node
5321 && cxx_dialect < cxx17
5322 && !in_system_header_at (input_location))
5323 pedwarn (input_location, 0, "fold-expressions only available "
5324 "with -std=c++17 or -std=gnu++17");
5325 }
5326 else
5327 /* Let the front end know that this expression was
5328 enclosed in parentheses. This matters in case, for
5329 example, the expression is of the form `A::B', since
5330 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5331 not. */
5332 expr = finish_parenthesized_expr (expr);
5333
5334 /* DR 705: Wrapping an unqualified name in parentheses
5335 suppresses arg-dependent lookup. We want to pass back
5336 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5337 (c++/37862), but none of the others. */
5338 if (*idk != CP_ID_KIND_QUALIFIED)
5339 *idk = CP_ID_KIND_NONE;
5340
5341 /* The `>' token might be the end of a template-id or
5342 template-parameter-list now. */
5343 parser->greater_than_is_operator_p
5344 = saved_greater_than_is_operator_p;
5345
5346 /* Consume the `)'. */
5347 token = cp_lexer_peek_token (parser->lexer);
5348 location_t close_paren_loc = token->location;
5349 expr.set_range (open_paren_loc, close_paren_loc);
5350 if (!parens.require_close (parser)
5351 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5352 cp_parser_skip_to_end_of_statement (parser);
5353
5354 return expr;
5355 }
5356
5357 case CPP_OPEN_SQUARE:
5358 {
5359 if (c_dialect_objc ())
5360 {
5361 /* We might have an Objective-C++ message. */
5362 cp_parser_parse_tentatively (parser);
5363 tree msg = cp_parser_objc_message_expression (parser);
5364 /* If that works out, we're done ... */
5365 if (cp_parser_parse_definitely (parser))
5366 return msg;
5367 /* ... else, fall though to see if it's a lambda. */
5368 }
5369 cp_expr lam = cp_parser_lambda_expression (parser);
5370 /* Don't warn about a failed tentative parse. */
5371 if (cp_parser_error_occurred (parser))
5372 return error_mark_node;
5373 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5374 return lam;
5375 }
5376
5377 case CPP_OBJC_STRING:
5378 if (c_dialect_objc ())
5379 /* We have an Objective-C++ string literal. */
5380 return cp_parser_objc_expression (parser);
5381 cp_parser_error (parser, "expected primary-expression");
5382 return error_mark_node;
5383
5384 case CPP_KEYWORD:
5385 switch (token->keyword)
5386 {
5387 /* These two are the boolean literals. */
5388 case RID_TRUE:
5389 cp_lexer_consume_token (parser->lexer);
5390 return cp_expr (boolean_true_node, token->location);
5391 case RID_FALSE:
5392 cp_lexer_consume_token (parser->lexer);
5393 return cp_expr (boolean_false_node, token->location);
5394
5395 /* The `__null' literal. */
5396 case RID_NULL:
5397 cp_lexer_consume_token (parser->lexer);
5398 return cp_expr (null_node, token->location);
5399
5400 /* The `nullptr' literal. */
5401 case RID_NULLPTR:
5402 cp_lexer_consume_token (parser->lexer);
5403 return cp_expr (nullptr_node, token->location);
5404
5405 /* Recognize the `this' keyword. */
5406 case RID_THIS:
5407 cp_lexer_consume_token (parser->lexer);
5408 if (parser->local_variables_forbidden_p)
5409 {
5410 error_at (token->location,
5411 "%<this%> may not be used in this context");
5412 return error_mark_node;
5413 }
5414 /* Pointers cannot appear in constant-expressions. */
5415 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5416 return error_mark_node;
5417 return cp_expr (finish_this_expr (), token->location);
5418
5419 /* The `operator' keyword can be the beginning of an
5420 id-expression. */
5421 case RID_OPERATOR:
5422 goto id_expression;
5423
5424 case RID_FUNCTION_NAME:
5425 case RID_PRETTY_FUNCTION_NAME:
5426 case RID_C99_FUNCTION_NAME:
5427 {
5428 non_integral_constant name;
5429
5430 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5431 __func__ are the names of variables -- but they are
5432 treated specially. Therefore, they are handled here,
5433 rather than relying on the generic id-expression logic
5434 below. Grammatically, these names are id-expressions.
5435
5436 Consume the token. */
5437 token = cp_lexer_consume_token (parser->lexer);
5438
5439 switch (token->keyword)
5440 {
5441 case RID_FUNCTION_NAME:
5442 name = NIC_FUNC_NAME;
5443 break;
5444 case RID_PRETTY_FUNCTION_NAME:
5445 name = NIC_PRETTY_FUNC;
5446 break;
5447 case RID_C99_FUNCTION_NAME:
5448 name = NIC_C99_FUNC;
5449 break;
5450 default:
5451 gcc_unreachable ();
5452 }
5453
5454 if (cp_parser_non_integral_constant_expression (parser, name))
5455 return error_mark_node;
5456
5457 /* Look up the name. */
5458 return finish_fname (token->u.value);
5459 }
5460
5461 case RID_VA_ARG:
5462 {
5463 tree expression;
5464 tree type;
5465 location_t type_location;
5466 location_t start_loc
5467 = cp_lexer_peek_token (parser->lexer)->location;
5468 /* The `__builtin_va_arg' construct is used to handle
5469 `va_arg'. Consume the `__builtin_va_arg' token. */
5470 cp_lexer_consume_token (parser->lexer);
5471 /* Look for the opening `('. */
5472 matching_parens parens;
5473 parens.require_open (parser);
5474 /* Now, parse the assignment-expression. */
5475 expression = cp_parser_assignment_expression (parser);
5476 /* Look for the `,'. */
5477 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5478 type_location = cp_lexer_peek_token (parser->lexer)->location;
5479 /* Parse the type-id. */
5480 {
5481 type_id_in_expr_sentinel s (parser);
5482 type = cp_parser_type_id (parser);
5483 }
5484 /* Look for the closing `)'. */
5485 location_t finish_loc
5486 = cp_lexer_peek_token (parser->lexer)->location;
5487 parens.require_close (parser);
5488 /* Using `va_arg' in a constant-expression is not
5489 allowed. */
5490 if (cp_parser_non_integral_constant_expression (parser,
5491 NIC_VA_ARG))
5492 return error_mark_node;
5493 /* Construct a location of the form:
5494 __builtin_va_arg (v, int)
5495 ~~~~~~~~~~~~~~~~~~~~~^~~~
5496 with the caret at the type, ranging from the start of the
5497 "__builtin_va_arg" token to the close paren. */
5498 location_t combined_loc
5499 = make_location (type_location, start_loc, finish_loc);
5500 return build_x_va_arg (combined_loc, expression, type);
5501 }
5502
5503 case RID_OFFSETOF:
5504 return cp_parser_builtin_offsetof (parser);
5505
5506 case RID_HAS_NOTHROW_ASSIGN:
5507 case RID_HAS_NOTHROW_CONSTRUCTOR:
5508 case RID_HAS_NOTHROW_COPY:
5509 case RID_HAS_TRIVIAL_ASSIGN:
5510 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5511 case RID_HAS_TRIVIAL_COPY:
5512 case RID_HAS_TRIVIAL_DESTRUCTOR:
5513 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5514 case RID_HAS_VIRTUAL_DESTRUCTOR:
5515 case RID_IS_ABSTRACT:
5516 case RID_IS_AGGREGATE:
5517 case RID_IS_BASE_OF:
5518 case RID_IS_CLASS:
5519 case RID_IS_EMPTY:
5520 case RID_IS_ENUM:
5521 case RID_IS_FINAL:
5522 case RID_IS_LITERAL_TYPE:
5523 case RID_IS_POD:
5524 case RID_IS_POLYMORPHIC:
5525 case RID_IS_SAME_AS:
5526 case RID_IS_STD_LAYOUT:
5527 case RID_IS_TRIVIAL:
5528 case RID_IS_TRIVIALLY_ASSIGNABLE:
5529 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5530 case RID_IS_TRIVIALLY_COPYABLE:
5531 case RID_IS_UNION:
5532 case RID_IS_ASSIGNABLE:
5533 case RID_IS_CONSTRUCTIBLE:
5534 return cp_parser_trait_expr (parser, token->keyword);
5535
5536 // C++ concepts
5537 case RID_REQUIRES:
5538 return cp_parser_requires_expression (parser);
5539
5540 /* Objective-C++ expressions. */
5541 case RID_AT_ENCODE:
5542 case RID_AT_PROTOCOL:
5543 case RID_AT_SELECTOR:
5544 return cp_parser_objc_expression (parser);
5545
5546 case RID_TEMPLATE:
5547 if (parser->in_function_body
5548 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5549 == CPP_LESS))
5550 {
5551 error_at (token->location,
5552 "a template declaration cannot appear at block scope");
5553 cp_parser_skip_to_end_of_block_or_statement (parser);
5554 return error_mark_node;
5555 }
5556 /* FALLTHRU */
5557 default:
5558 cp_parser_error (parser, "expected primary-expression");
5559 return error_mark_node;
5560 }
5561
5562 /* An id-expression can start with either an identifier, a
5563 `::' as the beginning of a qualified-id, or the "operator"
5564 keyword. */
5565 case CPP_NAME:
5566 case CPP_SCOPE:
5567 case CPP_TEMPLATE_ID:
5568 case CPP_NESTED_NAME_SPECIFIER:
5569 {
5570 id_expression:
5571 cp_expr id_expression;
5572 cp_expr decl;
5573 const char *error_msg;
5574 bool template_p;
5575 bool done;
5576 cp_token *id_expr_token;
5577
5578 /* Parse the id-expression. */
5579 id_expression
5580 = cp_parser_id_expression (parser,
5581 /*template_keyword_p=*/false,
5582 /*check_dependency_p=*/true,
5583 &template_p,
5584 /*declarator_p=*/false,
5585 /*optional_p=*/false);
5586 if (id_expression == error_mark_node)
5587 return error_mark_node;
5588 id_expr_token = token;
5589 token = cp_lexer_peek_token (parser->lexer);
5590 done = (token->type != CPP_OPEN_SQUARE
5591 && token->type != CPP_OPEN_PAREN
5592 && token->type != CPP_DOT
5593 && token->type != CPP_DEREF
5594 && token->type != CPP_PLUS_PLUS
5595 && token->type != CPP_MINUS_MINUS);
5596 /* If we have a template-id, then no further lookup is
5597 required. If the template-id was for a template-class, we
5598 will sometimes have a TYPE_DECL at this point. */
5599 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5600 || TREE_CODE (id_expression) == TYPE_DECL)
5601 decl = id_expression;
5602 /* Look up the name. */
5603 else
5604 {
5605 tree ambiguous_decls;
5606
5607 /* If we already know that this lookup is ambiguous, then
5608 we've already issued an error message; there's no reason
5609 to check again. */
5610 if (id_expr_token->type == CPP_NAME
5611 && id_expr_token->error_reported)
5612 {
5613 cp_parser_simulate_error (parser);
5614 return error_mark_node;
5615 }
5616
5617 decl = cp_parser_lookup_name (parser, id_expression,
5618 none_type,
5619 template_p,
5620 /*is_namespace=*/false,
5621 /*check_dependency=*/true,
5622 &ambiguous_decls,
5623 id_expression.get_location ());
5624 /* If the lookup was ambiguous, an error will already have
5625 been issued. */
5626 if (ambiguous_decls)
5627 return error_mark_node;
5628
5629 /* In Objective-C++, we may have an Objective-C 2.0
5630 dot-syntax for classes here. */
5631 if (c_dialect_objc ()
5632 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5633 && TREE_CODE (decl) == TYPE_DECL
5634 && objc_is_class_name (decl))
5635 {
5636 tree component;
5637 cp_lexer_consume_token (parser->lexer);
5638 component = cp_parser_identifier (parser);
5639 if (component == error_mark_node)
5640 return error_mark_node;
5641
5642 tree result = objc_build_class_component_ref (id_expression,
5643 component);
5644 /* Build a location of the form:
5645 expr.component
5646 ~~~~~^~~~~~~~~
5647 with caret at the start of the component name (at
5648 input_location), ranging from the start of the id_expression
5649 to the end of the component name. */
5650 location_t combined_loc
5651 = make_location (input_location, id_expression.get_start (),
5652 get_finish (input_location));
5653 protected_set_expr_location (result, combined_loc);
5654 return result;
5655 }
5656
5657 /* In Objective-C++, an instance variable (ivar) may be preferred
5658 to whatever cp_parser_lookup_name() found.
5659 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5660 rest of c-family, we have to do a little extra work to preserve
5661 any location information in cp_expr "decl". Given that
5662 objc_lookup_ivar is implemented in "c-family" and "objc", we
5663 have a trip through the pure "tree" type, rather than cp_expr.
5664 Naively copying it back to "decl" would implicitly give the
5665 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5666 store an EXPR_LOCATION. Hence we only update "decl" (and
5667 hence its location_t) if we get back a different tree node. */
5668 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5669 id_expression);
5670 if (decl_tree != decl.get_value ())
5671 decl = cp_expr (decl_tree);
5672
5673 /* If name lookup gives us a SCOPE_REF, then the
5674 qualifying scope was dependent. */
5675 if (TREE_CODE (decl) == SCOPE_REF)
5676 {
5677 /* At this point, we do not know if DECL is a valid
5678 integral constant expression. We assume that it is
5679 in fact such an expression, so that code like:
5680
5681 template <int N> struct A {
5682 int a[B<N>::i];
5683 };
5684
5685 is accepted. At template-instantiation time, we
5686 will check that B<N>::i is actually a constant. */
5687 return decl;
5688 }
5689 /* Check to see if DECL is a local variable in a context
5690 where that is forbidden. */
5691 if (parser->local_variables_forbidden_p
5692 && local_variable_p (decl))
5693 {
5694 error_at (id_expression.get_location (),
5695 "local variable %qD may not appear in this context",
5696 decl.get_value ());
5697 return error_mark_node;
5698 }
5699 }
5700
5701 decl = (finish_id_expression
5702 (id_expression, decl, parser->scope,
5703 idk,
5704 parser->integral_constant_expression_p,
5705 parser->allow_non_integral_constant_expression_p,
5706 &parser->non_integral_constant_expression_p,
5707 template_p, done, address_p,
5708 template_arg_p,
5709 &error_msg,
5710 id_expression.get_location ()));
5711 if (error_msg)
5712 cp_parser_error (parser, error_msg);
5713 decl.set_location (id_expression.get_location ());
5714 decl.set_range (id_expr_token->location, id_expression.get_finish ());
5715 return decl;
5716 }
5717
5718 /* Anything else is an error. */
5719 default:
5720 cp_parser_error (parser, "expected primary-expression");
5721 return error_mark_node;
5722 }
5723 }
5724
5725 static inline cp_expr
5726 cp_parser_primary_expression (cp_parser *parser,
5727 bool address_p,
5728 bool cast_p,
5729 bool template_arg_p,
5730 cp_id_kind *idk)
5731 {
5732 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5733 /*decltype*/false, idk);
5734 }
5735
5736 /* Parse an id-expression.
5737
5738 id-expression:
5739 unqualified-id
5740 qualified-id
5741
5742 qualified-id:
5743 :: [opt] nested-name-specifier template [opt] unqualified-id
5744 :: identifier
5745 :: operator-function-id
5746 :: template-id
5747
5748 Return a representation of the unqualified portion of the
5749 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5750 a `::' or nested-name-specifier.
5751
5752 Often, if the id-expression was a qualified-id, the caller will
5753 want to make a SCOPE_REF to represent the qualified-id. This
5754 function does not do this in order to avoid wastefully creating
5755 SCOPE_REFs when they are not required.
5756
5757 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5758 `template' keyword.
5759
5760 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5761 uninstantiated templates.
5762
5763 If *TEMPLATE_P is non-NULL, it is set to true iff the
5764 `template' keyword is used to explicitly indicate that the entity
5765 named is a template.
5766
5767 If DECLARATOR_P is true, the id-expression is appearing as part of
5768 a declarator, rather than as part of an expression. */
5769
5770 static cp_expr
5771 cp_parser_id_expression (cp_parser *parser,
5772 bool template_keyword_p,
5773 bool check_dependency_p,
5774 bool *template_p,
5775 bool declarator_p,
5776 bool optional_p)
5777 {
5778 bool global_scope_p;
5779 bool nested_name_specifier_p;
5780
5781 /* Assume the `template' keyword was not used. */
5782 if (template_p)
5783 *template_p = template_keyword_p;
5784
5785 /* Look for the optional `::' operator. */
5786 global_scope_p
5787 = (!template_keyword_p
5788 && (cp_parser_global_scope_opt (parser,
5789 /*current_scope_valid_p=*/false)
5790 != NULL_TREE));
5791
5792 /* Look for the optional nested-name-specifier. */
5793 nested_name_specifier_p
5794 = (cp_parser_nested_name_specifier_opt (parser,
5795 /*typename_keyword_p=*/false,
5796 check_dependency_p,
5797 /*type_p=*/false,
5798 declarator_p,
5799 template_keyword_p)
5800 != NULL_TREE);
5801
5802 /* If there is a nested-name-specifier, then we are looking at
5803 the first qualified-id production. */
5804 if (nested_name_specifier_p)
5805 {
5806 tree saved_scope;
5807 tree saved_object_scope;
5808 tree saved_qualifying_scope;
5809 cp_expr unqualified_id;
5810 bool is_template;
5811
5812 /* See if the next token is the `template' keyword. */
5813 if (!template_p)
5814 template_p = &is_template;
5815 *template_p = cp_parser_optional_template_keyword (parser);
5816 /* Name lookup we do during the processing of the
5817 unqualified-id might obliterate SCOPE. */
5818 saved_scope = parser->scope;
5819 saved_object_scope = parser->object_scope;
5820 saved_qualifying_scope = parser->qualifying_scope;
5821 /* Process the final unqualified-id. */
5822 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5823 check_dependency_p,
5824 declarator_p,
5825 /*optional_p=*/false);
5826 /* Restore the SAVED_SCOPE for our caller. */
5827 parser->scope = saved_scope;
5828 parser->object_scope = saved_object_scope;
5829 parser->qualifying_scope = saved_qualifying_scope;
5830
5831 return unqualified_id;
5832 }
5833 /* Otherwise, if we are in global scope, then we are looking at one
5834 of the other qualified-id productions. */
5835 else if (global_scope_p)
5836 {
5837 cp_token *token;
5838 tree id;
5839
5840 /* Peek at the next token. */
5841 token = cp_lexer_peek_token (parser->lexer);
5842
5843 /* If it's an identifier, and the next token is not a "<", then
5844 we can avoid the template-id case. This is an optimization
5845 for this common case. */
5846 if (token->type == CPP_NAME
5847 && !cp_parser_nth_token_starts_template_argument_list_p
5848 (parser, 2))
5849 return cp_parser_identifier (parser);
5850
5851 cp_parser_parse_tentatively (parser);
5852 /* Try a template-id. */
5853 id = cp_parser_template_id (parser,
5854 /*template_keyword_p=*/false,
5855 /*check_dependency_p=*/true,
5856 none_type,
5857 declarator_p);
5858 /* If that worked, we're done. */
5859 if (cp_parser_parse_definitely (parser))
5860 return id;
5861
5862 /* Peek at the next token. (Changes in the token buffer may
5863 have invalidated the pointer obtained above.) */
5864 token = cp_lexer_peek_token (parser->lexer);
5865
5866 switch (token->type)
5867 {
5868 case CPP_NAME:
5869 return cp_parser_identifier (parser);
5870
5871 case CPP_KEYWORD:
5872 if (token->keyword == RID_OPERATOR)
5873 return cp_parser_operator_function_id (parser);
5874 /* Fall through. */
5875
5876 default:
5877 cp_parser_error (parser, "expected id-expression");
5878 return error_mark_node;
5879 }
5880 }
5881 else
5882 return cp_parser_unqualified_id (parser, template_keyword_p,
5883 /*check_dependency_p=*/true,
5884 declarator_p,
5885 optional_p);
5886 }
5887
5888 /* Parse an unqualified-id.
5889
5890 unqualified-id:
5891 identifier
5892 operator-function-id
5893 conversion-function-id
5894 ~ class-name
5895 template-id
5896
5897 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5898 keyword, in a construct like `A::template ...'.
5899
5900 Returns a representation of unqualified-id. For the `identifier'
5901 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5902 production a BIT_NOT_EXPR is returned; the operand of the
5903 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5904 other productions, see the documentation accompanying the
5905 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5906 names are looked up in uninstantiated templates. If DECLARATOR_P
5907 is true, the unqualified-id is appearing as part of a declarator,
5908 rather than as part of an expression. */
5909
5910 static cp_expr
5911 cp_parser_unqualified_id (cp_parser* parser,
5912 bool template_keyword_p,
5913 bool check_dependency_p,
5914 bool declarator_p,
5915 bool optional_p)
5916 {
5917 cp_token *token;
5918
5919 /* Peek at the next token. */
5920 token = cp_lexer_peek_token (parser->lexer);
5921
5922 switch ((int) token->type)
5923 {
5924 case CPP_NAME:
5925 {
5926 tree id;
5927
5928 /* We don't know yet whether or not this will be a
5929 template-id. */
5930 cp_parser_parse_tentatively (parser);
5931 /* Try a template-id. */
5932 id = cp_parser_template_id (parser, template_keyword_p,
5933 check_dependency_p,
5934 none_type,
5935 declarator_p);
5936 /* If it worked, we're done. */
5937 if (cp_parser_parse_definitely (parser))
5938 return id;
5939 /* Otherwise, it's an ordinary identifier. */
5940 return cp_parser_identifier (parser);
5941 }
5942
5943 case CPP_TEMPLATE_ID:
5944 return cp_parser_template_id (parser, template_keyword_p,
5945 check_dependency_p,
5946 none_type,
5947 declarator_p);
5948
5949 case CPP_COMPL:
5950 {
5951 tree type_decl;
5952 tree qualifying_scope;
5953 tree object_scope;
5954 tree scope;
5955 bool done;
5956
5957 /* Consume the `~' token. */
5958 cp_lexer_consume_token (parser->lexer);
5959 /* Parse the class-name. The standard, as written, seems to
5960 say that:
5961
5962 template <typename T> struct S { ~S (); };
5963 template <typename T> S<T>::~S() {}
5964
5965 is invalid, since `~' must be followed by a class-name, but
5966 `S<T>' is dependent, and so not known to be a class.
5967 That's not right; we need to look in uninstantiated
5968 templates. A further complication arises from:
5969
5970 template <typename T> void f(T t) {
5971 t.T::~T();
5972 }
5973
5974 Here, it is not possible to look up `T' in the scope of `T'
5975 itself. We must look in both the current scope, and the
5976 scope of the containing complete expression.
5977
5978 Yet another issue is:
5979
5980 struct S {
5981 int S;
5982 ~S();
5983 };
5984
5985 S::~S() {}
5986
5987 The standard does not seem to say that the `S' in `~S'
5988 should refer to the type `S' and not the data member
5989 `S::S'. */
5990
5991 /* DR 244 says that we look up the name after the "~" in the
5992 same scope as we looked up the qualifying name. That idea
5993 isn't fully worked out; it's more complicated than that. */
5994 scope = parser->scope;
5995 object_scope = parser->object_scope;
5996 qualifying_scope = parser->qualifying_scope;
5997
5998 /* Check for invalid scopes. */
5999 if (scope == error_mark_node)
6000 {
6001 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6002 cp_lexer_consume_token (parser->lexer);
6003 return error_mark_node;
6004 }
6005 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6006 {
6007 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6008 error_at (token->location,
6009 "scope %qT before %<~%> is not a class-name",
6010 scope);
6011 cp_parser_simulate_error (parser);
6012 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6013 cp_lexer_consume_token (parser->lexer);
6014 return error_mark_node;
6015 }
6016 gcc_assert (!scope || TYPE_P (scope));
6017
6018 /* If the name is of the form "X::~X" it's OK even if X is a
6019 typedef. */
6020 token = cp_lexer_peek_token (parser->lexer);
6021 if (scope
6022 && token->type == CPP_NAME
6023 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6024 != CPP_LESS)
6025 && (token->u.value == TYPE_IDENTIFIER (scope)
6026 || (CLASS_TYPE_P (scope)
6027 && constructor_name_p (token->u.value, scope))))
6028 {
6029 cp_lexer_consume_token (parser->lexer);
6030 return build_nt (BIT_NOT_EXPR, scope);
6031 }
6032
6033 /* ~auto means the destructor of whatever the object is. */
6034 if (cp_parser_is_keyword (token, RID_AUTO))
6035 {
6036 if (cxx_dialect < cxx14)
6037 pedwarn (input_location, 0,
6038 "%<~auto%> only available with "
6039 "-std=c++14 or -std=gnu++14");
6040 cp_lexer_consume_token (parser->lexer);
6041 return build_nt (BIT_NOT_EXPR, make_auto ());
6042 }
6043
6044 /* If there was an explicit qualification (S::~T), first look
6045 in the scope given by the qualification (i.e., S).
6046
6047 Note: in the calls to cp_parser_class_name below we pass
6048 typename_type so that lookup finds the injected-class-name
6049 rather than the constructor. */
6050 done = false;
6051 type_decl = NULL_TREE;
6052 if (scope)
6053 {
6054 cp_parser_parse_tentatively (parser);
6055 type_decl = cp_parser_class_name (parser,
6056 /*typename_keyword_p=*/false,
6057 /*template_keyword_p=*/false,
6058 typename_type,
6059 /*check_dependency=*/false,
6060 /*class_head_p=*/false,
6061 declarator_p);
6062 if (cp_parser_parse_definitely (parser))
6063 done = true;
6064 }
6065 /* In "N::S::~S", look in "N" as well. */
6066 if (!done && scope && qualifying_scope)
6067 {
6068 cp_parser_parse_tentatively (parser);
6069 parser->scope = qualifying_scope;
6070 parser->object_scope = NULL_TREE;
6071 parser->qualifying_scope = NULL_TREE;
6072 type_decl
6073 = cp_parser_class_name (parser,
6074 /*typename_keyword_p=*/false,
6075 /*template_keyword_p=*/false,
6076 typename_type,
6077 /*check_dependency=*/false,
6078 /*class_head_p=*/false,
6079 declarator_p);
6080 if (cp_parser_parse_definitely (parser))
6081 done = true;
6082 }
6083 /* In "p->S::~T", look in the scope given by "*p" as well. */
6084 else if (!done && object_scope)
6085 {
6086 cp_parser_parse_tentatively (parser);
6087 parser->scope = object_scope;
6088 parser->object_scope = NULL_TREE;
6089 parser->qualifying_scope = NULL_TREE;
6090 type_decl
6091 = cp_parser_class_name (parser,
6092 /*typename_keyword_p=*/false,
6093 /*template_keyword_p=*/false,
6094 typename_type,
6095 /*check_dependency=*/false,
6096 /*class_head_p=*/false,
6097 declarator_p);
6098 if (cp_parser_parse_definitely (parser))
6099 done = true;
6100 }
6101 /* Look in the surrounding context. */
6102 if (!done)
6103 {
6104 parser->scope = NULL_TREE;
6105 parser->object_scope = NULL_TREE;
6106 parser->qualifying_scope = NULL_TREE;
6107 if (processing_template_decl)
6108 cp_parser_parse_tentatively (parser);
6109 type_decl
6110 = cp_parser_class_name (parser,
6111 /*typename_keyword_p=*/false,
6112 /*template_keyword_p=*/false,
6113 typename_type,
6114 /*check_dependency=*/false,
6115 /*class_head_p=*/false,
6116 declarator_p);
6117 if (processing_template_decl
6118 && ! cp_parser_parse_definitely (parser))
6119 {
6120 /* We couldn't find a type with this name. If we're parsing
6121 tentatively, fail and try something else. */
6122 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6123 {
6124 cp_parser_simulate_error (parser);
6125 return error_mark_node;
6126 }
6127 /* Otherwise, accept it and check for a match at instantiation
6128 time. */
6129 type_decl = cp_parser_identifier (parser);
6130 if (type_decl != error_mark_node)
6131 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6132 return type_decl;
6133 }
6134 }
6135 /* If an error occurred, assume that the name of the
6136 destructor is the same as the name of the qualifying
6137 class. That allows us to keep parsing after running
6138 into ill-formed destructor names. */
6139 if (type_decl == error_mark_node && scope)
6140 return build_nt (BIT_NOT_EXPR, scope);
6141 else if (type_decl == error_mark_node)
6142 return error_mark_node;
6143
6144 /* Check that destructor name and scope match. */
6145 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6146 {
6147 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6148 error_at (token->location,
6149 "declaration of %<~%T%> as member of %qT",
6150 type_decl, scope);
6151 cp_parser_simulate_error (parser);
6152 return error_mark_node;
6153 }
6154
6155 /* [class.dtor]
6156
6157 A typedef-name that names a class shall not be used as the
6158 identifier in the declarator for a destructor declaration. */
6159 if (declarator_p
6160 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6161 && !DECL_SELF_REFERENCE_P (type_decl)
6162 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6163 error_at (token->location,
6164 "typedef-name %qD used as destructor declarator",
6165 type_decl);
6166
6167 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6168 }
6169
6170 case CPP_KEYWORD:
6171 if (token->keyword == RID_OPERATOR)
6172 {
6173 cp_expr id;
6174
6175 /* This could be a template-id, so we try that first. */
6176 cp_parser_parse_tentatively (parser);
6177 /* Try a template-id. */
6178 id = cp_parser_template_id (parser, template_keyword_p,
6179 /*check_dependency_p=*/true,
6180 none_type,
6181 declarator_p);
6182 /* If that worked, we're done. */
6183 if (cp_parser_parse_definitely (parser))
6184 return id;
6185 /* We still don't know whether we're looking at an
6186 operator-function-id or a conversion-function-id. */
6187 cp_parser_parse_tentatively (parser);
6188 /* Try an operator-function-id. */
6189 id = cp_parser_operator_function_id (parser);
6190 /* If that didn't work, try a conversion-function-id. */
6191 if (!cp_parser_parse_definitely (parser))
6192 id = cp_parser_conversion_function_id (parser);
6193
6194 return id;
6195 }
6196 /* Fall through. */
6197
6198 default:
6199 if (optional_p)
6200 return NULL_TREE;
6201 cp_parser_error (parser, "expected unqualified-id");
6202 return error_mark_node;
6203 }
6204 }
6205
6206 /* Parse an (optional) nested-name-specifier.
6207
6208 nested-name-specifier: [C++98]
6209 class-or-namespace-name :: nested-name-specifier [opt]
6210 class-or-namespace-name :: template nested-name-specifier [opt]
6211
6212 nested-name-specifier: [C++0x]
6213 type-name ::
6214 namespace-name ::
6215 nested-name-specifier identifier ::
6216 nested-name-specifier template [opt] simple-template-id ::
6217
6218 PARSER->SCOPE should be set appropriately before this function is
6219 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6220 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6221 in name lookups.
6222
6223 Sets PARSER->SCOPE to the class (TYPE) or namespace
6224 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6225 it unchanged if there is no nested-name-specifier. Returns the new
6226 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6227
6228 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6229 part of a declaration and/or decl-specifier. */
6230
6231 static tree
6232 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6233 bool typename_keyword_p,
6234 bool check_dependency_p,
6235 bool type_p,
6236 bool is_declaration,
6237 bool template_keyword_p /* = false */)
6238 {
6239 bool success = false;
6240 cp_token_position start = 0;
6241 cp_token *token;
6242
6243 /* Remember where the nested-name-specifier starts. */
6244 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6245 {
6246 start = cp_lexer_token_position (parser->lexer, false);
6247 push_deferring_access_checks (dk_deferred);
6248 }
6249
6250 while (true)
6251 {
6252 tree new_scope;
6253 tree old_scope;
6254 tree saved_qualifying_scope;
6255
6256 /* Spot cases that cannot be the beginning of a
6257 nested-name-specifier. */
6258 token = cp_lexer_peek_token (parser->lexer);
6259
6260 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6261 the already parsed nested-name-specifier. */
6262 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6263 {
6264 /* Grab the nested-name-specifier and continue the loop. */
6265 cp_parser_pre_parsed_nested_name_specifier (parser);
6266 /* If we originally encountered this nested-name-specifier
6267 with IS_DECLARATION set to false, we will not have
6268 resolved TYPENAME_TYPEs, so we must do so here. */
6269 if (is_declaration
6270 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6271 {
6272 new_scope = resolve_typename_type (parser->scope,
6273 /*only_current_p=*/false);
6274 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6275 parser->scope = new_scope;
6276 }
6277 success = true;
6278 continue;
6279 }
6280
6281 /* Spot cases that cannot be the beginning of a
6282 nested-name-specifier. On the second and subsequent times
6283 through the loop, we look for the `template' keyword. */
6284 if (success && token->keyword == RID_TEMPLATE)
6285 ;
6286 /* A template-id can start a nested-name-specifier. */
6287 else if (token->type == CPP_TEMPLATE_ID)
6288 ;
6289 /* DR 743: decltype can be used in a nested-name-specifier. */
6290 else if (token_is_decltype (token))
6291 ;
6292 else
6293 {
6294 /* If the next token is not an identifier, then it is
6295 definitely not a type-name or namespace-name. */
6296 if (token->type != CPP_NAME)
6297 break;
6298 /* If the following token is neither a `<' (to begin a
6299 template-id), nor a `::', then we are not looking at a
6300 nested-name-specifier. */
6301 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6302
6303 if (token->type == CPP_COLON
6304 && parser->colon_corrects_to_scope_p
6305 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6306 {
6307 gcc_rich_location richloc (token->location);
6308 richloc.add_fixit_replace ("::");
6309 error_at (&richloc,
6310 "found %<:%> in nested-name-specifier, "
6311 "expected %<::%>");
6312 token->type = CPP_SCOPE;
6313 }
6314
6315 if (token->type != CPP_SCOPE
6316 && !cp_parser_nth_token_starts_template_argument_list_p
6317 (parser, 2))
6318 break;
6319 }
6320
6321 /* The nested-name-specifier is optional, so we parse
6322 tentatively. */
6323 cp_parser_parse_tentatively (parser);
6324
6325 /* Look for the optional `template' keyword, if this isn't the
6326 first time through the loop. */
6327 if (success)
6328 template_keyword_p = cp_parser_optional_template_keyword (parser);
6329
6330 /* Save the old scope since the name lookup we are about to do
6331 might destroy it. */
6332 old_scope = parser->scope;
6333 saved_qualifying_scope = parser->qualifying_scope;
6334 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6335 look up names in "X<T>::I" in order to determine that "Y" is
6336 a template. So, if we have a typename at this point, we make
6337 an effort to look through it. */
6338 if (is_declaration
6339 && !typename_keyword_p
6340 && parser->scope
6341 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6342 parser->scope = resolve_typename_type (parser->scope,
6343 /*only_current_p=*/false);
6344 /* Parse the qualifying entity. */
6345 new_scope
6346 = cp_parser_qualifying_entity (parser,
6347 typename_keyword_p,
6348 template_keyword_p,
6349 check_dependency_p,
6350 type_p,
6351 is_declaration);
6352 /* Look for the `::' token. */
6353 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6354
6355 /* If we found what we wanted, we keep going; otherwise, we're
6356 done. */
6357 if (!cp_parser_parse_definitely (parser))
6358 {
6359 bool error_p = false;
6360
6361 /* Restore the OLD_SCOPE since it was valid before the
6362 failed attempt at finding the last
6363 class-or-namespace-name. */
6364 parser->scope = old_scope;
6365 parser->qualifying_scope = saved_qualifying_scope;
6366
6367 /* If the next token is a decltype, and the one after that is a
6368 `::', then the decltype has failed to resolve to a class or
6369 enumeration type. Give this error even when parsing
6370 tentatively since it can't possibly be valid--and we're going
6371 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6372 won't get another chance.*/
6373 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6374 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6375 == CPP_SCOPE))
6376 {
6377 token = cp_lexer_consume_token (parser->lexer);
6378 error_at (token->location, "decltype evaluates to %qT, "
6379 "which is not a class or enumeration type",
6380 token->u.tree_check_value->value);
6381 parser->scope = error_mark_node;
6382 error_p = true;
6383 /* As below. */
6384 success = true;
6385 cp_lexer_consume_token (parser->lexer);
6386 }
6387
6388 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6389 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6390 {
6391 /* If we have a non-type template-id followed by ::, it can't
6392 possibly be valid. */
6393 token = cp_lexer_peek_token (parser->lexer);
6394 tree tid = token->u.tree_check_value->value;
6395 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6396 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6397 {
6398 tree tmpl = NULL_TREE;
6399 if (is_overloaded_fn (tid))
6400 {
6401 tree fns = get_fns (tid);
6402 if (OVL_SINGLE_P (fns))
6403 tmpl = OVL_FIRST (fns);
6404 error_at (token->location, "function template-id %qD "
6405 "in nested-name-specifier", tid);
6406 }
6407 else
6408 {
6409 /* Variable template. */
6410 tmpl = TREE_OPERAND (tid, 0);
6411 gcc_assert (variable_template_p (tmpl));
6412 error_at (token->location, "variable template-id %qD "
6413 "in nested-name-specifier", tid);
6414 }
6415 if (tmpl)
6416 inform (DECL_SOURCE_LOCATION (tmpl),
6417 "%qD declared here", tmpl);
6418
6419 parser->scope = error_mark_node;
6420 error_p = true;
6421 /* As below. */
6422 success = true;
6423 cp_lexer_consume_token (parser->lexer);
6424 cp_lexer_consume_token (parser->lexer);
6425 }
6426 }
6427
6428 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6429 break;
6430 /* If the next token is an identifier, and the one after
6431 that is a `::', then any valid interpretation would have
6432 found a class-or-namespace-name. */
6433 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6434 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6435 == CPP_SCOPE)
6436 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6437 != CPP_COMPL))
6438 {
6439 token = cp_lexer_consume_token (parser->lexer);
6440 if (!error_p)
6441 {
6442 if (!token->error_reported)
6443 {
6444 tree decl;
6445 tree ambiguous_decls;
6446
6447 decl = cp_parser_lookup_name (parser, token->u.value,
6448 none_type,
6449 /*is_template=*/false,
6450 /*is_namespace=*/false,
6451 /*check_dependency=*/true,
6452 &ambiguous_decls,
6453 token->location);
6454 if (TREE_CODE (decl) == TEMPLATE_DECL)
6455 error_at (token->location,
6456 "%qD used without template arguments",
6457 decl);
6458 else if (ambiguous_decls)
6459 {
6460 // cp_parser_lookup_name has the same diagnostic,
6461 // thus make sure to emit it at most once.
6462 if (cp_parser_uncommitted_to_tentative_parse_p
6463 (parser))
6464 {
6465 error_at (token->location,
6466 "reference to %qD is ambiguous",
6467 token->u.value);
6468 print_candidates (ambiguous_decls);
6469 }
6470 decl = error_mark_node;
6471 }
6472 else
6473 {
6474 if (cxx_dialect != cxx98)
6475 cp_parser_name_lookup_error
6476 (parser, token->u.value, decl, NLE_NOT_CXX98,
6477 token->location);
6478 else
6479 cp_parser_name_lookup_error
6480 (parser, token->u.value, decl, NLE_CXX98,
6481 token->location);
6482 }
6483 }
6484 parser->scope = error_mark_node;
6485 error_p = true;
6486 /* Treat this as a successful nested-name-specifier
6487 due to:
6488
6489 [basic.lookup.qual]
6490
6491 If the name found is not a class-name (clause
6492 _class_) or namespace-name (_namespace.def_), the
6493 program is ill-formed. */
6494 success = true;
6495 }
6496 cp_lexer_consume_token (parser->lexer);
6497 }
6498 break;
6499 }
6500 /* We've found one valid nested-name-specifier. */
6501 success = true;
6502 /* Name lookup always gives us a DECL. */
6503 if (TREE_CODE (new_scope) == TYPE_DECL)
6504 new_scope = TREE_TYPE (new_scope);
6505 /* Uses of "template" must be followed by actual templates. */
6506 if (template_keyword_p
6507 && !(CLASS_TYPE_P (new_scope)
6508 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6509 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6510 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6511 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6512 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6513 == TEMPLATE_ID_EXPR)))
6514 permerror (input_location, TYPE_P (new_scope)
6515 ? G_("%qT is not a template")
6516 : G_("%qD is not a template"),
6517 new_scope);
6518 /* If it is a class scope, try to complete it; we are about to
6519 be looking up names inside the class. */
6520 if (TYPE_P (new_scope)
6521 /* Since checking types for dependency can be expensive,
6522 avoid doing it if the type is already complete. */
6523 && !COMPLETE_TYPE_P (new_scope)
6524 /* Do not try to complete dependent types. */
6525 && !dependent_type_p (new_scope))
6526 {
6527 new_scope = complete_type (new_scope);
6528 /* If it is a typedef to current class, use the current
6529 class instead, as the typedef won't have any names inside
6530 it yet. */
6531 if (!COMPLETE_TYPE_P (new_scope)
6532 && currently_open_class (new_scope))
6533 new_scope = TYPE_MAIN_VARIANT (new_scope);
6534 }
6535 /* Make sure we look in the right scope the next time through
6536 the loop. */
6537 parser->scope = new_scope;
6538 }
6539
6540 /* If parsing tentatively, replace the sequence of tokens that makes
6541 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6542 token. That way, should we re-parse the token stream, we will
6543 not have to repeat the effort required to do the parse, nor will
6544 we issue duplicate error messages. */
6545 if (success && start)
6546 {
6547 cp_token *token;
6548
6549 token = cp_lexer_token_at (parser->lexer, start);
6550 /* Reset the contents of the START token. */
6551 token->type = CPP_NESTED_NAME_SPECIFIER;
6552 /* Retrieve any deferred checks. Do not pop this access checks yet
6553 so the memory will not be reclaimed during token replacing below. */
6554 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6555 token->u.tree_check_value->value = parser->scope;
6556 token->u.tree_check_value->checks = get_deferred_access_checks ();
6557 token->u.tree_check_value->qualifying_scope =
6558 parser->qualifying_scope;
6559 token->keyword = RID_MAX;
6560
6561 /* Purge all subsequent tokens. */
6562 cp_lexer_purge_tokens_after (parser->lexer, start);
6563 }
6564
6565 if (start)
6566 pop_to_parent_deferring_access_checks ();
6567
6568 return success ? parser->scope : NULL_TREE;
6569 }
6570
6571 /* Parse a nested-name-specifier. See
6572 cp_parser_nested_name_specifier_opt for details. This function
6573 behaves identically, except that it will an issue an error if no
6574 nested-name-specifier is present. */
6575
6576 static tree
6577 cp_parser_nested_name_specifier (cp_parser *parser,
6578 bool typename_keyword_p,
6579 bool check_dependency_p,
6580 bool type_p,
6581 bool is_declaration)
6582 {
6583 tree scope;
6584
6585 /* Look for the nested-name-specifier. */
6586 scope = cp_parser_nested_name_specifier_opt (parser,
6587 typename_keyword_p,
6588 check_dependency_p,
6589 type_p,
6590 is_declaration);
6591 /* If it was not present, issue an error message. */
6592 if (!scope)
6593 {
6594 cp_parser_error (parser, "expected nested-name-specifier");
6595 parser->scope = NULL_TREE;
6596 }
6597
6598 return scope;
6599 }
6600
6601 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6602 this is either a class-name or a namespace-name (which corresponds
6603 to the class-or-namespace-name production in the grammar). For
6604 C++0x, it can also be a type-name that refers to an enumeration
6605 type or a simple-template-id.
6606
6607 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6608 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6609 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6610 TYPE_P is TRUE iff the next name should be taken as a class-name,
6611 even the same name is declared to be another entity in the same
6612 scope.
6613
6614 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6615 specified by the class-or-namespace-name. If neither is found the
6616 ERROR_MARK_NODE is returned. */
6617
6618 static tree
6619 cp_parser_qualifying_entity (cp_parser *parser,
6620 bool typename_keyword_p,
6621 bool template_keyword_p,
6622 bool check_dependency_p,
6623 bool type_p,
6624 bool is_declaration)
6625 {
6626 tree saved_scope;
6627 tree saved_qualifying_scope;
6628 tree saved_object_scope;
6629 tree scope;
6630 bool only_class_p;
6631 bool successful_parse_p;
6632
6633 /* DR 743: decltype can appear in a nested-name-specifier. */
6634 if (cp_lexer_next_token_is_decltype (parser->lexer))
6635 {
6636 scope = cp_parser_decltype (parser);
6637 if (TREE_CODE (scope) != ENUMERAL_TYPE
6638 && !MAYBE_CLASS_TYPE_P (scope))
6639 {
6640 cp_parser_simulate_error (parser);
6641 return error_mark_node;
6642 }
6643 if (TYPE_NAME (scope))
6644 scope = TYPE_NAME (scope);
6645 return scope;
6646 }
6647
6648 /* Before we try to parse the class-name, we must save away the
6649 current PARSER->SCOPE since cp_parser_class_name will destroy
6650 it. */
6651 saved_scope = parser->scope;
6652 saved_qualifying_scope = parser->qualifying_scope;
6653 saved_object_scope = parser->object_scope;
6654 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6655 there is no need to look for a namespace-name. */
6656 only_class_p = template_keyword_p
6657 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6658 if (!only_class_p)
6659 cp_parser_parse_tentatively (parser);
6660 scope = cp_parser_class_name (parser,
6661 typename_keyword_p,
6662 template_keyword_p,
6663 type_p ? class_type : none_type,
6664 check_dependency_p,
6665 /*class_head_p=*/false,
6666 is_declaration,
6667 /*enum_ok=*/cxx_dialect > cxx98);
6668 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6669 /* If that didn't work, try for a namespace-name. */
6670 if (!only_class_p && !successful_parse_p)
6671 {
6672 /* Restore the saved scope. */
6673 parser->scope = saved_scope;
6674 parser->qualifying_scope = saved_qualifying_scope;
6675 parser->object_scope = saved_object_scope;
6676 /* If we are not looking at an identifier followed by the scope
6677 resolution operator, then this is not part of a
6678 nested-name-specifier. (Note that this function is only used
6679 to parse the components of a nested-name-specifier.) */
6680 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6681 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6682 return error_mark_node;
6683 scope = cp_parser_namespace_name (parser);
6684 }
6685
6686 return scope;
6687 }
6688
6689 /* Return true if we are looking at a compound-literal, false otherwise. */
6690
6691 static bool
6692 cp_parser_compound_literal_p (cp_parser *parser)
6693 {
6694 cp_lexer_save_tokens (parser->lexer);
6695
6696 /* Skip tokens until the next token is a closing parenthesis.
6697 If we find the closing `)', and the next token is a `{', then
6698 we are looking at a compound-literal. */
6699 bool compound_literal_p
6700 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6701 /*consume_paren=*/true)
6702 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6703
6704 /* Roll back the tokens we skipped. */
6705 cp_lexer_rollback_tokens (parser->lexer);
6706
6707 return compound_literal_p;
6708 }
6709
6710 /* Return true if EXPR is the integer constant zero or a complex constant
6711 of zero, without any folding, but ignoring location wrappers. */
6712
6713 bool
6714 literal_integer_zerop (const_tree expr)
6715 {
6716 return (location_wrapper_p (expr)
6717 && integer_zerop (TREE_OPERAND (expr, 0)));
6718 }
6719
6720 /* Parse a postfix-expression.
6721
6722 postfix-expression:
6723 primary-expression
6724 postfix-expression [ expression ]
6725 postfix-expression ( expression-list [opt] )
6726 simple-type-specifier ( expression-list [opt] )
6727 typename :: [opt] nested-name-specifier identifier
6728 ( expression-list [opt] )
6729 typename :: [opt] nested-name-specifier template [opt] template-id
6730 ( expression-list [opt] )
6731 postfix-expression . template [opt] id-expression
6732 postfix-expression -> template [opt] id-expression
6733 postfix-expression . pseudo-destructor-name
6734 postfix-expression -> pseudo-destructor-name
6735 postfix-expression ++
6736 postfix-expression --
6737 dynamic_cast < type-id > ( expression )
6738 static_cast < type-id > ( expression )
6739 reinterpret_cast < type-id > ( expression )
6740 const_cast < type-id > ( expression )
6741 typeid ( expression )
6742 typeid ( type-id )
6743
6744 GNU Extension:
6745
6746 postfix-expression:
6747 ( type-id ) { initializer-list , [opt] }
6748
6749 This extension is a GNU version of the C99 compound-literal
6750 construct. (The C99 grammar uses `type-name' instead of `type-id',
6751 but they are essentially the same concept.)
6752
6753 If ADDRESS_P is true, the postfix expression is the operand of the
6754 `&' operator. CAST_P is true if this expression is the target of a
6755 cast.
6756
6757 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6758 class member access expressions [expr.ref].
6759
6760 Returns a representation of the expression. */
6761
6762 static cp_expr
6763 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6764 bool member_access_only_p, bool decltype_p,
6765 cp_id_kind * pidk_return)
6766 {
6767 cp_token *token;
6768 location_t loc;
6769 enum rid keyword;
6770 cp_id_kind idk = CP_ID_KIND_NONE;
6771 cp_expr postfix_expression = NULL_TREE;
6772 bool is_member_access = false;
6773
6774 /* Peek at the next token. */
6775 token = cp_lexer_peek_token (parser->lexer);
6776 loc = token->location;
6777 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6778
6779 /* Some of the productions are determined by keywords. */
6780 keyword = token->keyword;
6781 switch (keyword)
6782 {
6783 case RID_DYNCAST:
6784 case RID_STATCAST:
6785 case RID_REINTCAST:
6786 case RID_CONSTCAST:
6787 {
6788 tree type;
6789 cp_expr expression;
6790 const char *saved_message;
6791 bool saved_in_type_id_in_expr_p;
6792
6793 /* All of these can be handled in the same way from the point
6794 of view of parsing. Begin by consuming the token
6795 identifying the cast. */
6796 cp_lexer_consume_token (parser->lexer);
6797
6798 /* New types cannot be defined in the cast. */
6799 saved_message = parser->type_definition_forbidden_message;
6800 parser->type_definition_forbidden_message
6801 = G_("types may not be defined in casts");
6802
6803 /* Look for the opening `<'. */
6804 cp_parser_require (parser, CPP_LESS, RT_LESS);
6805 /* Parse the type to which we are casting. */
6806 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6807 parser->in_type_id_in_expr_p = true;
6808 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6809 NULL);
6810 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6811 /* Look for the closing `>'. */
6812 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6813 /* Restore the old message. */
6814 parser->type_definition_forbidden_message = saved_message;
6815
6816 bool saved_greater_than_is_operator_p
6817 = parser->greater_than_is_operator_p;
6818 parser->greater_than_is_operator_p = true;
6819
6820 /* And the expression which is being cast. */
6821 matching_parens parens;
6822 parens.require_open (parser);
6823 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6824 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6825 RT_CLOSE_PAREN);
6826 location_t end_loc = close_paren ?
6827 close_paren->location : UNKNOWN_LOCATION;
6828
6829 parser->greater_than_is_operator_p
6830 = saved_greater_than_is_operator_p;
6831
6832 /* Only type conversions to integral or enumeration types
6833 can be used in constant-expressions. */
6834 if (!cast_valid_in_integral_constant_expression_p (type)
6835 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6836 {
6837 postfix_expression = error_mark_node;
6838 break;
6839 }
6840
6841 switch (keyword)
6842 {
6843 case RID_DYNCAST:
6844 postfix_expression
6845 = build_dynamic_cast (type, expression, tf_warning_or_error);
6846 break;
6847 case RID_STATCAST:
6848 postfix_expression
6849 = build_static_cast (type, expression, tf_warning_or_error);
6850 break;
6851 case RID_REINTCAST:
6852 postfix_expression
6853 = build_reinterpret_cast (type, expression,
6854 tf_warning_or_error);
6855 break;
6856 case RID_CONSTCAST:
6857 postfix_expression
6858 = build_const_cast (type, expression, tf_warning_or_error);
6859 break;
6860 default:
6861 gcc_unreachable ();
6862 }
6863
6864 /* Construct a location e.g. :
6865 reinterpret_cast <int *> (expr)
6866 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6867 ranging from the start of the "*_cast" token to the final closing
6868 paren, with the caret at the start. */
6869 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6870 postfix_expression.set_location (cp_cast_loc);
6871 }
6872 break;
6873
6874 case RID_TYPEID:
6875 {
6876 tree type;
6877 const char *saved_message;
6878 bool saved_in_type_id_in_expr_p;
6879
6880 /* Consume the `typeid' token. */
6881 cp_lexer_consume_token (parser->lexer);
6882 /* Look for the `(' token. */
6883 matching_parens parens;
6884 parens.require_open (parser);
6885 /* Types cannot be defined in a `typeid' expression. */
6886 saved_message = parser->type_definition_forbidden_message;
6887 parser->type_definition_forbidden_message
6888 = G_("types may not be defined in a %<typeid%> expression");
6889 /* We can't be sure yet whether we're looking at a type-id or an
6890 expression. */
6891 cp_parser_parse_tentatively (parser);
6892 /* Try a type-id first. */
6893 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6894 parser->in_type_id_in_expr_p = true;
6895 type = cp_parser_type_id (parser);
6896 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6897 /* Look for the `)' token. Otherwise, we can't be sure that
6898 we're not looking at an expression: consider `typeid (int
6899 (3))', for example. */
6900 cp_token *close_paren = parens.require_close (parser);
6901 /* If all went well, simply lookup the type-id. */
6902 if (cp_parser_parse_definitely (parser))
6903 postfix_expression = get_typeid (type, tf_warning_or_error);
6904 /* Otherwise, fall back to the expression variant. */
6905 else
6906 {
6907 tree expression;
6908
6909 /* Look for an expression. */
6910 expression = cp_parser_expression (parser, & idk);
6911 /* Compute its typeid. */
6912 postfix_expression = build_typeid (expression, tf_warning_or_error);
6913 /* Look for the `)' token. */
6914 close_paren = parens.require_close (parser);
6915 }
6916 /* Restore the saved message. */
6917 parser->type_definition_forbidden_message = saved_message;
6918 /* `typeid' may not appear in an integral constant expression. */
6919 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6920 postfix_expression = error_mark_node;
6921
6922 /* Construct a location e.g. :
6923 typeid (expr)
6924 ^~~~~~~~~~~~~
6925 ranging from the start of the "typeid" token to the final closing
6926 paren, with the caret at the start. */
6927 if (close_paren)
6928 {
6929 location_t typeid_loc
6930 = make_location (start_loc, start_loc, close_paren->location);
6931 postfix_expression.set_location (typeid_loc);
6932 postfix_expression.maybe_add_location_wrapper ();
6933 }
6934 }
6935 break;
6936
6937 case RID_TYPENAME:
6938 {
6939 tree type;
6940 /* The syntax permitted here is the same permitted for an
6941 elaborated-type-specifier. */
6942 ++parser->prevent_constrained_type_specifiers;
6943 type = cp_parser_elaborated_type_specifier (parser,
6944 /*is_friend=*/false,
6945 /*is_declaration=*/false);
6946 --parser->prevent_constrained_type_specifiers;
6947 postfix_expression = cp_parser_functional_cast (parser, type);
6948 }
6949 break;
6950
6951 case RID_ADDRESSOF:
6952 case RID_BUILTIN_SHUFFLE:
6953 case RID_BUILTIN_LAUNDER:
6954 {
6955 vec<tree, va_gc> *vec;
6956 unsigned int i;
6957 tree p;
6958
6959 cp_lexer_consume_token (parser->lexer);
6960 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6961 /*cast_p=*/false, /*allow_expansion_p=*/true,
6962 /*non_constant_p=*/NULL);
6963 if (vec == NULL)
6964 {
6965 postfix_expression = error_mark_node;
6966 break;
6967 }
6968
6969 FOR_EACH_VEC_ELT (*vec, i, p)
6970 mark_exp_read (p);
6971
6972 switch (keyword)
6973 {
6974 case RID_ADDRESSOF:
6975 if (vec->length () == 1)
6976 postfix_expression
6977 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6978 else
6979 {
6980 error_at (loc, "wrong number of arguments to "
6981 "%<__builtin_addressof%>");
6982 postfix_expression = error_mark_node;
6983 }
6984 break;
6985
6986 case RID_BUILTIN_LAUNDER:
6987 if (vec->length () == 1)
6988 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6989 tf_warning_or_error);
6990 else
6991 {
6992 error_at (loc, "wrong number of arguments to "
6993 "%<__builtin_launder%>");
6994 postfix_expression = error_mark_node;
6995 }
6996 break;
6997
6998 case RID_BUILTIN_SHUFFLE:
6999 if (vec->length () == 2)
7000 postfix_expression
7001 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7002 (*vec)[1], tf_warning_or_error);
7003 else if (vec->length () == 3)
7004 postfix_expression
7005 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7006 (*vec)[2], tf_warning_or_error);
7007 else
7008 {
7009 error_at (loc, "wrong number of arguments to "
7010 "%<__builtin_shuffle%>");
7011 postfix_expression = error_mark_node;
7012 }
7013 break;
7014
7015 default:
7016 gcc_unreachable ();
7017 }
7018 break;
7019 }
7020
7021 default:
7022 {
7023 tree type;
7024
7025 /* If the next thing is a simple-type-specifier, we may be
7026 looking at a functional cast. We could also be looking at
7027 an id-expression. So, we try the functional cast, and if
7028 that doesn't work we fall back to the primary-expression. */
7029 cp_parser_parse_tentatively (parser);
7030 /* Look for the simple-type-specifier. */
7031 ++parser->prevent_constrained_type_specifiers;
7032 type = cp_parser_simple_type_specifier (parser,
7033 /*decl_specs=*/NULL,
7034 CP_PARSER_FLAGS_NONE);
7035 --parser->prevent_constrained_type_specifiers;
7036 /* Parse the cast itself. */
7037 if (!cp_parser_error_occurred (parser))
7038 postfix_expression
7039 = cp_parser_functional_cast (parser, type);
7040 /* If that worked, we're done. */
7041 if (cp_parser_parse_definitely (parser))
7042 break;
7043
7044 /* If the functional-cast didn't work out, try a
7045 compound-literal. */
7046 if (cp_parser_allow_gnu_extensions_p (parser)
7047 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7048 {
7049 cp_expr initializer = NULL_TREE;
7050
7051 cp_parser_parse_tentatively (parser);
7052
7053 matching_parens parens;
7054 parens.consume_open (parser);
7055
7056 /* Avoid calling cp_parser_type_id pointlessly, see comment
7057 in cp_parser_cast_expression about c++/29234. */
7058 if (!cp_parser_compound_literal_p (parser))
7059 cp_parser_simulate_error (parser);
7060 else
7061 {
7062 /* Parse the type. */
7063 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7064 parser->in_type_id_in_expr_p = true;
7065 type = cp_parser_type_id (parser);
7066 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7067 parens.require_close (parser);
7068 }
7069
7070 /* If things aren't going well, there's no need to
7071 keep going. */
7072 if (!cp_parser_error_occurred (parser))
7073 {
7074 bool non_constant_p;
7075 /* Parse the brace-enclosed initializer list. */
7076 initializer = cp_parser_braced_list (parser,
7077 &non_constant_p);
7078 }
7079 /* If that worked, we're definitely looking at a
7080 compound-literal expression. */
7081 if (cp_parser_parse_definitely (parser))
7082 {
7083 /* Warn the user that a compound literal is not
7084 allowed in standard C++. */
7085 pedwarn (input_location, OPT_Wpedantic,
7086 "ISO C++ forbids compound-literals");
7087 /* For simplicity, we disallow compound literals in
7088 constant-expressions. We could
7089 allow compound literals of integer type, whose
7090 initializer was a constant, in constant
7091 expressions. Permitting that usage, as a further
7092 extension, would not change the meaning of any
7093 currently accepted programs. (Of course, as
7094 compound literals are not part of ISO C++, the
7095 standard has nothing to say.) */
7096 if (cp_parser_non_integral_constant_expression (parser,
7097 NIC_NCC))
7098 {
7099 postfix_expression = error_mark_node;
7100 break;
7101 }
7102 /* Form the representation of the compound-literal. */
7103 postfix_expression
7104 = finish_compound_literal (type, initializer,
7105 tf_warning_or_error, fcl_c99);
7106 postfix_expression.set_location (initializer.get_location ());
7107 break;
7108 }
7109 }
7110
7111 /* It must be a primary-expression. */
7112 postfix_expression
7113 = cp_parser_primary_expression (parser, address_p, cast_p,
7114 /*template_arg_p=*/false,
7115 decltype_p,
7116 &idk);
7117 }
7118 break;
7119 }
7120
7121 /* Note that we don't need to worry about calling build_cplus_new on a
7122 class-valued CALL_EXPR in decltype when it isn't the end of the
7123 postfix-expression; unary_complex_lvalue will take care of that for
7124 all these cases. */
7125
7126 /* Keep looping until the postfix-expression is complete. */
7127 while (true)
7128 {
7129 if (idk == CP_ID_KIND_UNQUALIFIED
7130 && identifier_p (postfix_expression)
7131 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7132 /* It is not a Koenig lookup function call. */
7133 postfix_expression
7134 = unqualified_name_lookup_error (postfix_expression);
7135
7136 /* Peek at the next token. */
7137 token = cp_lexer_peek_token (parser->lexer);
7138
7139 switch (token->type)
7140 {
7141 case CPP_OPEN_SQUARE:
7142 if (cp_next_tokens_can_be_std_attribute_p (parser))
7143 {
7144 cp_parser_error (parser,
7145 "two consecutive %<[%> shall "
7146 "only introduce an attribute");
7147 return error_mark_node;
7148 }
7149 postfix_expression
7150 = cp_parser_postfix_open_square_expression (parser,
7151 postfix_expression,
7152 false,
7153 decltype_p);
7154 postfix_expression.set_range (start_loc,
7155 postfix_expression.get_location ());
7156
7157 idk = CP_ID_KIND_NONE;
7158 is_member_access = false;
7159 break;
7160
7161 case CPP_OPEN_PAREN:
7162 /* postfix-expression ( expression-list [opt] ) */
7163 {
7164 bool koenig_p;
7165 bool is_builtin_constant_p;
7166 bool saved_integral_constant_expression_p = false;
7167 bool saved_non_integral_constant_expression_p = false;
7168 tsubst_flags_t complain = complain_flags (decltype_p);
7169 vec<tree, va_gc> *args;
7170 location_t close_paren_loc = UNKNOWN_LOCATION;
7171
7172 is_member_access = false;
7173
7174 tree stripped_expression
7175 = tree_strip_any_location_wrapper (postfix_expression);
7176 is_builtin_constant_p
7177 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7178 if (is_builtin_constant_p)
7179 {
7180 /* The whole point of __builtin_constant_p is to allow
7181 non-constant expressions to appear as arguments. */
7182 saved_integral_constant_expression_p
7183 = parser->integral_constant_expression_p;
7184 saved_non_integral_constant_expression_p
7185 = parser->non_integral_constant_expression_p;
7186 parser->integral_constant_expression_p = false;
7187 }
7188 args = (cp_parser_parenthesized_expression_list
7189 (parser, non_attr,
7190 /*cast_p=*/false, /*allow_expansion_p=*/true,
7191 /*non_constant_p=*/NULL,
7192 /*close_paren_loc=*/&close_paren_loc,
7193 /*wrap_locations_p=*/true));
7194 if (is_builtin_constant_p)
7195 {
7196 parser->integral_constant_expression_p
7197 = saved_integral_constant_expression_p;
7198 parser->non_integral_constant_expression_p
7199 = saved_non_integral_constant_expression_p;
7200 }
7201
7202 if (args == NULL)
7203 {
7204 postfix_expression = error_mark_node;
7205 break;
7206 }
7207
7208 /* Function calls are not permitted in
7209 constant-expressions. */
7210 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7211 && cp_parser_non_integral_constant_expression (parser,
7212 NIC_FUNC_CALL))
7213 {
7214 postfix_expression = error_mark_node;
7215 release_tree_vector (args);
7216 break;
7217 }
7218
7219 koenig_p = false;
7220 if (idk == CP_ID_KIND_UNQUALIFIED
7221 || idk == CP_ID_KIND_TEMPLATE_ID)
7222 {
7223 if (identifier_p (postfix_expression)
7224 /* In C++2A, we may need to perform ADL for a template
7225 name. */
7226 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7227 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7228 {
7229 if (!args->is_empty ())
7230 {
7231 koenig_p = true;
7232 if (!any_type_dependent_arguments_p (args))
7233 postfix_expression
7234 = perform_koenig_lookup (postfix_expression, args,
7235 complain);
7236 }
7237 else
7238 postfix_expression
7239 = unqualified_fn_lookup_error (postfix_expression);
7240 }
7241 /* We do not perform argument-dependent lookup if
7242 normal lookup finds a non-function, in accordance
7243 with the expected resolution of DR 218. */
7244 else if (!args->is_empty ()
7245 && is_overloaded_fn (postfix_expression))
7246 {
7247 /* We only need to look at the first function,
7248 because all the fns share the attribute we're
7249 concerned with (all member fns or all local
7250 fns). */
7251 tree fn = get_first_fn (postfix_expression);
7252 fn = STRIP_TEMPLATE (fn);
7253
7254 /* Do not do argument dependent lookup if regular
7255 lookup finds a member function or a block-scope
7256 function declaration. [basic.lookup.argdep]/3 */
7257 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7258 || DECL_FUNCTION_MEMBER_P (fn)
7259 || DECL_LOCAL_FUNCTION_P (fn)))
7260 {
7261 koenig_p = true;
7262 if (!any_type_dependent_arguments_p (args))
7263 postfix_expression
7264 = perform_koenig_lookup (postfix_expression, args,
7265 complain);
7266 }
7267 }
7268 }
7269
7270 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7271 {
7272 tree instance = TREE_OPERAND (postfix_expression, 0);
7273 tree fn = TREE_OPERAND (postfix_expression, 1);
7274
7275 if (processing_template_decl
7276 && (type_dependent_object_expression_p (instance)
7277 || (!BASELINK_P (fn)
7278 && TREE_CODE (fn) != FIELD_DECL)
7279 || type_dependent_expression_p (fn)
7280 || any_type_dependent_arguments_p (args)))
7281 {
7282 maybe_generic_this_capture (instance, fn);
7283 postfix_expression
7284 = build_min_nt_call_vec (postfix_expression, args);
7285 release_tree_vector (args);
7286 break;
7287 }
7288
7289 if (BASELINK_P (fn))
7290 {
7291 postfix_expression
7292 = (build_new_method_call
7293 (instance, fn, &args, NULL_TREE,
7294 (idk == CP_ID_KIND_QUALIFIED
7295 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7296 : LOOKUP_NORMAL),
7297 /*fn_p=*/NULL,
7298 complain));
7299 }
7300 else
7301 postfix_expression
7302 = finish_call_expr (postfix_expression, &args,
7303 /*disallow_virtual=*/false,
7304 /*koenig_p=*/false,
7305 complain);
7306 }
7307 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7308 || TREE_CODE (postfix_expression) == MEMBER_REF
7309 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7310 postfix_expression = (build_offset_ref_call_from_tree
7311 (postfix_expression, &args,
7312 complain));
7313 else if (idk == CP_ID_KIND_QUALIFIED)
7314 /* A call to a static class member, or a namespace-scope
7315 function. */
7316 postfix_expression
7317 = finish_call_expr (postfix_expression, &args,
7318 /*disallow_virtual=*/true,
7319 koenig_p,
7320 complain);
7321 else
7322 /* All other function calls. */
7323 postfix_expression
7324 = finish_call_expr (postfix_expression, &args,
7325 /*disallow_virtual=*/false,
7326 koenig_p,
7327 complain);
7328
7329 if (close_paren_loc != UNKNOWN_LOCATION)
7330 {
7331 location_t combined_loc = make_location (token->location,
7332 start_loc,
7333 close_paren_loc);
7334 postfix_expression.set_location (combined_loc);
7335 }
7336
7337 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7338 idk = CP_ID_KIND_NONE;
7339
7340 release_tree_vector (args);
7341 }
7342 break;
7343
7344 case CPP_DOT:
7345 case CPP_DEREF:
7346 /* postfix-expression . template [opt] id-expression
7347 postfix-expression . pseudo-destructor-name
7348 postfix-expression -> template [opt] id-expression
7349 postfix-expression -> pseudo-destructor-name */
7350
7351 /* Consume the `.' or `->' operator. */
7352 cp_lexer_consume_token (parser->lexer);
7353
7354 postfix_expression
7355 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7356 postfix_expression,
7357 false, &idk, loc);
7358
7359 is_member_access = true;
7360 break;
7361
7362 case CPP_PLUS_PLUS:
7363 /* postfix-expression ++ */
7364 /* Consume the `++' token. */
7365 cp_lexer_consume_token (parser->lexer);
7366 /* Generate a representation for the complete expression. */
7367 postfix_expression
7368 = finish_increment_expr (postfix_expression,
7369 POSTINCREMENT_EXPR);
7370 /* Increments may not appear in constant-expressions. */
7371 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7372 postfix_expression = error_mark_node;
7373 idk = CP_ID_KIND_NONE;
7374 is_member_access = false;
7375 break;
7376
7377 case CPP_MINUS_MINUS:
7378 /* postfix-expression -- */
7379 /* Consume the `--' token. */
7380 cp_lexer_consume_token (parser->lexer);
7381 /* Generate a representation for the complete expression. */
7382 postfix_expression
7383 = finish_increment_expr (postfix_expression,
7384 POSTDECREMENT_EXPR);
7385 /* Decrements may not appear in constant-expressions. */
7386 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7387 postfix_expression = error_mark_node;
7388 idk = CP_ID_KIND_NONE;
7389 is_member_access = false;
7390 break;
7391
7392 default:
7393 if (pidk_return != NULL)
7394 * pidk_return = idk;
7395 if (member_access_only_p)
7396 return is_member_access
7397 ? postfix_expression
7398 : cp_expr (error_mark_node);
7399 else
7400 return postfix_expression;
7401 }
7402 }
7403
7404 /* We should never get here. */
7405 gcc_unreachable ();
7406 return error_mark_node;
7407 }
7408
7409 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7410 by cp_parser_builtin_offsetof. We're looking for
7411
7412 postfix-expression [ expression ]
7413 postfix-expression [ braced-init-list ] (C++11)
7414
7415 FOR_OFFSETOF is set if we're being called in that context, which
7416 changes how we deal with integer constant expressions. */
7417
7418 static tree
7419 cp_parser_postfix_open_square_expression (cp_parser *parser,
7420 tree postfix_expression,
7421 bool for_offsetof,
7422 bool decltype_p)
7423 {
7424 tree index = NULL_TREE;
7425 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7426 bool saved_greater_than_is_operator_p;
7427
7428 /* Consume the `[' token. */
7429 cp_lexer_consume_token (parser->lexer);
7430
7431 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7432 parser->greater_than_is_operator_p = true;
7433
7434 /* Parse the index expression. */
7435 /* ??? For offsetof, there is a question of what to allow here. If
7436 offsetof is not being used in an integral constant expression context,
7437 then we *could* get the right answer by computing the value at runtime.
7438 If we are in an integral constant expression context, then we might
7439 could accept any constant expression; hard to say without analysis.
7440 Rather than open the barn door too wide right away, allow only integer
7441 constant expressions here. */
7442 if (for_offsetof)
7443 index = cp_parser_constant_expression (parser);
7444 else
7445 {
7446 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7447 {
7448 bool expr_nonconst_p;
7449 cp_lexer_set_source_position (parser->lexer);
7450 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7451 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7452 }
7453 else
7454 index = cp_parser_expression (parser);
7455 }
7456
7457 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7458
7459 /* Look for the closing `]'. */
7460 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7461
7462 /* Build the ARRAY_REF. */
7463 postfix_expression = grok_array_decl (loc, postfix_expression,
7464 index, decltype_p);
7465
7466 /* When not doing offsetof, array references are not permitted in
7467 constant-expressions. */
7468 if (!for_offsetof
7469 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7470 postfix_expression = error_mark_node;
7471
7472 return postfix_expression;
7473 }
7474
7475 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7476 dereference of incomplete type, returns true if error_mark_node should
7477 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7478 and *DEPENDENT_P. */
7479
7480 bool
7481 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7482 bool *dependent_p)
7483 {
7484 /* In a template, be permissive by treating an object expression
7485 of incomplete type as dependent (after a pedwarn). */
7486 diagnostic_t kind = (processing_template_decl
7487 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7488
7489 switch (TREE_CODE (*postfix_expression))
7490 {
7491 case CAST_EXPR:
7492 case REINTERPRET_CAST_EXPR:
7493 case CONST_CAST_EXPR:
7494 case STATIC_CAST_EXPR:
7495 case DYNAMIC_CAST_EXPR:
7496 case IMPLICIT_CONV_EXPR:
7497 case VIEW_CONVERT_EXPR:
7498 case NON_LVALUE_EXPR:
7499 kind = DK_ERROR;
7500 break;
7501 case OVERLOAD:
7502 /* Don't emit any diagnostic for OVERLOADs. */
7503 kind = DK_IGNORED;
7504 break;
7505 default:
7506 /* Avoid clobbering e.g. DECLs. */
7507 if (!EXPR_P (*postfix_expression))
7508 kind = DK_ERROR;
7509 break;
7510 }
7511
7512 if (kind == DK_IGNORED)
7513 return false;
7514
7515 location_t exploc = location_of (*postfix_expression);
7516 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7517 if (!MAYBE_CLASS_TYPE_P (*scope))
7518 return true;
7519 if (kind == DK_ERROR)
7520 *scope = *postfix_expression = error_mark_node;
7521 else if (processing_template_decl)
7522 {
7523 *dependent_p = true;
7524 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7525 }
7526 return false;
7527 }
7528
7529 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7530 by cp_parser_builtin_offsetof. We're looking for
7531
7532 postfix-expression . template [opt] id-expression
7533 postfix-expression . pseudo-destructor-name
7534 postfix-expression -> template [opt] id-expression
7535 postfix-expression -> pseudo-destructor-name
7536
7537 FOR_OFFSETOF is set if we're being called in that context. That sorta
7538 limits what of the above we'll actually accept, but nevermind.
7539 TOKEN_TYPE is the "." or "->" token, which will already have been
7540 removed from the stream. */
7541
7542 static tree
7543 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7544 enum cpp_ttype token_type,
7545 cp_expr postfix_expression,
7546 bool for_offsetof, cp_id_kind *idk,
7547 location_t location)
7548 {
7549 tree name;
7550 bool dependent_p;
7551 bool pseudo_destructor_p;
7552 tree scope = NULL_TREE;
7553 location_t start_loc = postfix_expression.get_start ();
7554
7555 /* If this is a `->' operator, dereference the pointer. */
7556 if (token_type == CPP_DEREF)
7557 postfix_expression = build_x_arrow (location, postfix_expression,
7558 tf_warning_or_error);
7559 /* Check to see whether or not the expression is type-dependent and
7560 not the current instantiation. */
7561 dependent_p = type_dependent_object_expression_p (postfix_expression);
7562 /* The identifier following the `->' or `.' is not qualified. */
7563 parser->scope = NULL_TREE;
7564 parser->qualifying_scope = NULL_TREE;
7565 parser->object_scope = NULL_TREE;
7566 *idk = CP_ID_KIND_NONE;
7567
7568 /* Enter the scope corresponding to the type of the object
7569 given by the POSTFIX_EXPRESSION. */
7570 if (!dependent_p)
7571 {
7572 scope = TREE_TYPE (postfix_expression);
7573 /* According to the standard, no expression should ever have
7574 reference type. Unfortunately, we do not currently match
7575 the standard in this respect in that our internal representation
7576 of an expression may have reference type even when the standard
7577 says it does not. Therefore, we have to manually obtain the
7578 underlying type here. */
7579 scope = non_reference (scope);
7580 /* The type of the POSTFIX_EXPRESSION must be complete. */
7581 /* Unlike the object expression in other contexts, *this is not
7582 required to be of complete type for purposes of class member
7583 access (5.2.5) outside the member function body. */
7584 if (postfix_expression != current_class_ref
7585 && scope != error_mark_node
7586 && !currently_open_class (scope))
7587 {
7588 scope = complete_type (scope);
7589 if (!COMPLETE_TYPE_P (scope)
7590 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7591 &dependent_p))
7592 return error_mark_node;
7593 }
7594
7595 if (!dependent_p)
7596 {
7597 /* Let the name lookup machinery know that we are processing a
7598 class member access expression. */
7599 parser->context->object_type = scope;
7600 /* If something went wrong, we want to be able to discern that case,
7601 as opposed to the case where there was no SCOPE due to the type
7602 of expression being dependent. */
7603 if (!scope)
7604 scope = error_mark_node;
7605 /* If the SCOPE was erroneous, make the various semantic analysis
7606 functions exit quickly -- and without issuing additional error
7607 messages. */
7608 if (scope == error_mark_node)
7609 postfix_expression = error_mark_node;
7610 }
7611 }
7612
7613 if (dependent_p)
7614 /* Tell cp_parser_lookup_name that there was an object, even though it's
7615 type-dependent. */
7616 parser->context->object_type = unknown_type_node;
7617
7618 /* Assume this expression is not a pseudo-destructor access. */
7619 pseudo_destructor_p = false;
7620
7621 /* If the SCOPE is a scalar type, then, if this is a valid program,
7622 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7623 is type dependent, it can be pseudo-destructor-name or something else.
7624 Try to parse it as pseudo-destructor-name first. */
7625 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7626 {
7627 tree s;
7628 tree type;
7629
7630 cp_parser_parse_tentatively (parser);
7631 /* Parse the pseudo-destructor-name. */
7632 s = NULL_TREE;
7633 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7634 &s, &type);
7635 if (dependent_p
7636 && (cp_parser_error_occurred (parser)
7637 || !SCALAR_TYPE_P (type)))
7638 cp_parser_abort_tentative_parse (parser);
7639 else if (cp_parser_parse_definitely (parser))
7640 {
7641 pseudo_destructor_p = true;
7642 postfix_expression
7643 = finish_pseudo_destructor_expr (postfix_expression,
7644 s, type, location);
7645 }
7646 }
7647
7648 if (!pseudo_destructor_p)
7649 {
7650 /* If the SCOPE is not a scalar type, we are looking at an
7651 ordinary class member access expression, rather than a
7652 pseudo-destructor-name. */
7653 bool template_p;
7654 cp_token *token = cp_lexer_peek_token (parser->lexer);
7655 /* Parse the id-expression. */
7656 name = (cp_parser_id_expression
7657 (parser,
7658 cp_parser_optional_template_keyword (parser),
7659 /*check_dependency_p=*/true,
7660 &template_p,
7661 /*declarator_p=*/false,
7662 /*optional_p=*/false));
7663 /* In general, build a SCOPE_REF if the member name is qualified.
7664 However, if the name was not dependent and has already been
7665 resolved; there is no need to build the SCOPE_REF. For example;
7666
7667 struct X { void f(); };
7668 template <typename T> void f(T* t) { t->X::f(); }
7669
7670 Even though "t" is dependent, "X::f" is not and has been resolved
7671 to a BASELINK; there is no need to include scope information. */
7672
7673 /* But we do need to remember that there was an explicit scope for
7674 virtual function calls. */
7675 if (parser->scope)
7676 *idk = CP_ID_KIND_QUALIFIED;
7677
7678 /* If the name is a template-id that names a type, we will get a
7679 TYPE_DECL here. That is invalid code. */
7680 if (TREE_CODE (name) == TYPE_DECL)
7681 {
7682 error_at (token->location, "invalid use of %qD", name);
7683 postfix_expression = error_mark_node;
7684 }
7685 else
7686 {
7687 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7688 {
7689 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7690 {
7691 error_at (token->location, "%<%D::%D%> is not a class member",
7692 parser->scope, name);
7693 postfix_expression = error_mark_node;
7694 }
7695 else
7696 name = build_qualified_name (/*type=*/NULL_TREE,
7697 parser->scope,
7698 name,
7699 template_p);
7700 parser->scope = NULL_TREE;
7701 parser->qualifying_scope = NULL_TREE;
7702 parser->object_scope = NULL_TREE;
7703 }
7704 if (parser->scope && name && BASELINK_P (name))
7705 adjust_result_of_qualified_name_lookup
7706 (name, parser->scope, scope);
7707 postfix_expression
7708 = finish_class_member_access_expr (postfix_expression, name,
7709 template_p,
7710 tf_warning_or_error);
7711 /* Build a location e.g.:
7712 ptr->access_expr
7713 ~~~^~~~~~~~~~~~~
7714 where the caret is at the deref token, ranging from
7715 the start of postfix_expression to the end of the access expr. */
7716 location_t end_loc
7717 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7718 location_t combined_loc
7719 = make_location (input_location, start_loc, end_loc);
7720 protected_set_expr_location (postfix_expression, combined_loc);
7721 }
7722 }
7723
7724 /* We no longer need to look up names in the scope of the object on
7725 the left-hand side of the `.' or `->' operator. */
7726 parser->context->object_type = NULL_TREE;
7727
7728 /* Outside of offsetof, these operators may not appear in
7729 constant-expressions. */
7730 if (!for_offsetof
7731 && (cp_parser_non_integral_constant_expression
7732 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7733 postfix_expression = error_mark_node;
7734
7735 return postfix_expression;
7736 }
7737
7738 /* Parse a parenthesized expression-list.
7739
7740 expression-list:
7741 assignment-expression
7742 expression-list, assignment-expression
7743
7744 attribute-list:
7745 expression-list
7746 identifier
7747 identifier, expression-list
7748
7749 CAST_P is true if this expression is the target of a cast.
7750
7751 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7752 argument pack.
7753
7754 WRAP_LOCATIONS_P is true if expressions within this list for which
7755 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7756 their source locations.
7757
7758 Returns a vector of trees. Each element is a representation of an
7759 assignment-expression. NULL is returned if the ( and or ) are
7760 missing. An empty, but allocated, vector is returned on no
7761 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7762 if we are parsing an attribute list for an attribute that wants a
7763 plain identifier argument, normal_attr for an attribute that wants
7764 an expression, or non_attr if we aren't parsing an attribute list. If
7765 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7766 not all of the expressions in the list were constant.
7767 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7768 will be written to with the location of the closing parenthesis. If
7769 an error occurs, it may or may not be written to. */
7770
7771 static vec<tree, va_gc> *
7772 cp_parser_parenthesized_expression_list (cp_parser* parser,
7773 int is_attribute_list,
7774 bool cast_p,
7775 bool allow_expansion_p,
7776 bool *non_constant_p,
7777 location_t *close_paren_loc,
7778 bool wrap_locations_p)
7779 {
7780 vec<tree, va_gc> *expression_list;
7781 bool fold_expr_p = is_attribute_list != non_attr;
7782 tree identifier = NULL_TREE;
7783 bool saved_greater_than_is_operator_p;
7784
7785 /* Assume all the expressions will be constant. */
7786 if (non_constant_p)
7787 *non_constant_p = false;
7788
7789 matching_parens parens;
7790 if (!parens.require_open (parser))
7791 return NULL;
7792
7793 expression_list = make_tree_vector ();
7794
7795 /* Within a parenthesized expression, a `>' token is always
7796 the greater-than operator. */
7797 saved_greater_than_is_operator_p
7798 = parser->greater_than_is_operator_p;
7799 parser->greater_than_is_operator_p = true;
7800
7801 cp_expr expr (NULL_TREE);
7802
7803 /* Consume expressions until there are no more. */
7804 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7805 while (true)
7806 {
7807 /* At the beginning of attribute lists, check to see if the
7808 next token is an identifier. */
7809 if (is_attribute_list == id_attr
7810 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7811 {
7812 cp_token *token;
7813
7814 /* Consume the identifier. */
7815 token = cp_lexer_consume_token (parser->lexer);
7816 /* Save the identifier. */
7817 identifier = token->u.value;
7818 }
7819 else
7820 {
7821 bool expr_non_constant_p;
7822
7823 /* Parse the next assignment-expression. */
7824 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7825 {
7826 /* A braced-init-list. */
7827 cp_lexer_set_source_position (parser->lexer);
7828 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7829 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7830 if (non_constant_p && expr_non_constant_p)
7831 *non_constant_p = true;
7832 }
7833 else if (non_constant_p)
7834 {
7835 expr = (cp_parser_constant_expression
7836 (parser, /*allow_non_constant_p=*/true,
7837 &expr_non_constant_p));
7838 if (expr_non_constant_p)
7839 *non_constant_p = true;
7840 }
7841 else
7842 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7843 cast_p);
7844
7845 if (fold_expr_p)
7846 expr = instantiate_non_dependent_expr (expr);
7847
7848 /* If we have an ellipsis, then this is an expression
7849 expansion. */
7850 if (allow_expansion_p
7851 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7852 {
7853 /* Consume the `...'. */
7854 cp_lexer_consume_token (parser->lexer);
7855
7856 /* Build the argument pack. */
7857 expr = make_pack_expansion (expr);
7858 }
7859
7860 if (wrap_locations_p)
7861 expr.maybe_add_location_wrapper ();
7862
7863 /* Add it to the list. We add error_mark_node
7864 expressions to the list, so that we can still tell if
7865 the correct form for a parenthesized expression-list
7866 is found. That gives better errors. */
7867 vec_safe_push (expression_list, expr.get_value ());
7868
7869 if (expr == error_mark_node)
7870 goto skip_comma;
7871 }
7872
7873 /* After the first item, attribute lists look the same as
7874 expression lists. */
7875 is_attribute_list = non_attr;
7876
7877 get_comma:;
7878 /* If the next token isn't a `,', then we are done. */
7879 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7880 break;
7881
7882 /* Otherwise, consume the `,' and keep going. */
7883 cp_lexer_consume_token (parser->lexer);
7884 }
7885
7886 if (close_paren_loc)
7887 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7888
7889 if (!parens.require_close (parser))
7890 {
7891 int ending;
7892
7893 skip_comma:;
7894 /* We try and resync to an unnested comma, as that will give the
7895 user better diagnostics. */
7896 ending = cp_parser_skip_to_closing_parenthesis (parser,
7897 /*recovering=*/true,
7898 /*or_comma=*/true,
7899 /*consume_paren=*/true);
7900 if (ending < 0)
7901 goto get_comma;
7902 if (!ending)
7903 {
7904 parser->greater_than_is_operator_p
7905 = saved_greater_than_is_operator_p;
7906 return NULL;
7907 }
7908 }
7909
7910 parser->greater_than_is_operator_p
7911 = saved_greater_than_is_operator_p;
7912
7913 if (identifier)
7914 vec_safe_insert (expression_list, 0, identifier);
7915
7916 return expression_list;
7917 }
7918
7919 /* Parse a pseudo-destructor-name.
7920
7921 pseudo-destructor-name:
7922 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7923 :: [opt] nested-name-specifier template template-id :: ~ type-name
7924 :: [opt] nested-name-specifier [opt] ~ type-name
7925
7926 If either of the first two productions is used, sets *SCOPE to the
7927 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7928 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7929 or ERROR_MARK_NODE if the parse fails. */
7930
7931 static void
7932 cp_parser_pseudo_destructor_name (cp_parser* parser,
7933 tree object,
7934 tree* scope,
7935 tree* type)
7936 {
7937 bool nested_name_specifier_p;
7938
7939 /* Handle ~auto. */
7940 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7941 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7942 && !type_dependent_expression_p (object))
7943 {
7944 if (cxx_dialect < cxx14)
7945 pedwarn (input_location, 0,
7946 "%<~auto%> only available with "
7947 "-std=c++14 or -std=gnu++14");
7948 cp_lexer_consume_token (parser->lexer);
7949 cp_lexer_consume_token (parser->lexer);
7950 *scope = NULL_TREE;
7951 *type = TREE_TYPE (object);
7952 return;
7953 }
7954
7955 /* Assume that things will not work out. */
7956 *type = error_mark_node;
7957
7958 /* Look for the optional `::' operator. */
7959 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7960 /* Look for the optional nested-name-specifier. */
7961 nested_name_specifier_p
7962 = (cp_parser_nested_name_specifier_opt (parser,
7963 /*typename_keyword_p=*/false,
7964 /*check_dependency_p=*/true,
7965 /*type_p=*/false,
7966 /*is_declaration=*/false)
7967 != NULL_TREE);
7968 /* Now, if we saw a nested-name-specifier, we might be doing the
7969 second production. */
7970 if (nested_name_specifier_p
7971 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7972 {
7973 /* Consume the `template' keyword. */
7974 cp_lexer_consume_token (parser->lexer);
7975 /* Parse the template-id. */
7976 cp_parser_template_id (parser,
7977 /*template_keyword_p=*/true,
7978 /*check_dependency_p=*/false,
7979 class_type,
7980 /*is_declaration=*/true);
7981 /* Look for the `::' token. */
7982 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7983 }
7984 /* If the next token is not a `~', then there might be some
7985 additional qualification. */
7986 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7987 {
7988 /* At this point, we're looking for "type-name :: ~". The type-name
7989 must not be a class-name, since this is a pseudo-destructor. So,
7990 it must be either an enum-name, or a typedef-name -- both of which
7991 are just identifiers. So, we peek ahead to check that the "::"
7992 and "~" tokens are present; if they are not, then we can avoid
7993 calling type_name. */
7994 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7995 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7996 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7997 {
7998 cp_parser_error (parser, "non-scalar type");
7999 return;
8000 }
8001
8002 /* Look for the type-name. */
8003 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8004 if (*scope == error_mark_node)
8005 return;
8006
8007 /* Look for the `::' token. */
8008 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8009 }
8010 else
8011 *scope = NULL_TREE;
8012
8013 /* Look for the `~'. */
8014 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8015
8016 /* Once we see the ~, this has to be a pseudo-destructor. */
8017 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8018 cp_parser_commit_to_topmost_tentative_parse (parser);
8019
8020 /* Look for the type-name again. We are not responsible for
8021 checking that it matches the first type-name. */
8022 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8023 }
8024
8025 /* Parse a unary-expression.
8026
8027 unary-expression:
8028 postfix-expression
8029 ++ cast-expression
8030 -- cast-expression
8031 unary-operator cast-expression
8032 sizeof unary-expression
8033 sizeof ( type-id )
8034 alignof ( type-id ) [C++0x]
8035 new-expression
8036 delete-expression
8037
8038 GNU Extensions:
8039
8040 unary-expression:
8041 __extension__ cast-expression
8042 __alignof__ unary-expression
8043 __alignof__ ( type-id )
8044 alignof unary-expression [C++0x]
8045 __real__ cast-expression
8046 __imag__ cast-expression
8047 && identifier
8048 sizeof ( type-id ) { initializer-list , [opt] }
8049 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8050 __alignof__ ( type-id ) { initializer-list , [opt] }
8051
8052 ADDRESS_P is true iff the unary-expression is appearing as the
8053 operand of the `&' operator. CAST_P is true if this expression is
8054 the target of a cast.
8055
8056 Returns a representation of the expression. */
8057
8058 static cp_expr
8059 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8060 bool address_p, bool cast_p, bool decltype_p)
8061 {
8062 cp_token *token;
8063 enum tree_code unary_operator;
8064
8065 /* Peek at the next token. */
8066 token = cp_lexer_peek_token (parser->lexer);
8067 /* Some keywords give away the kind of expression. */
8068 if (token->type == CPP_KEYWORD)
8069 {
8070 enum rid keyword = token->keyword;
8071
8072 switch (keyword)
8073 {
8074 case RID_ALIGNOF:
8075 case RID_SIZEOF:
8076 {
8077 tree operand, ret;
8078 enum tree_code op;
8079 location_t start_loc = token->location;
8080
8081 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8082 bool std_alignof = id_equal (token->u.value, "alignof");
8083
8084 /* Consume the token. */
8085 cp_lexer_consume_token (parser->lexer);
8086 /* Parse the operand. */
8087 operand = cp_parser_sizeof_operand (parser, keyword);
8088
8089 if (TYPE_P (operand))
8090 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8091 true);
8092 else
8093 {
8094 /* ISO C++ defines alignof only with types, not with
8095 expressions. So pedwarn if alignof is used with a non-
8096 type expression. However, __alignof__ is ok. */
8097 if (std_alignof)
8098 pedwarn (token->location, OPT_Wpedantic,
8099 "ISO C++ does not allow %<alignof%> "
8100 "with a non-type");
8101
8102 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8103 }
8104 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8105 SIZEOF_EXPR with the original operand. */
8106 if (op == SIZEOF_EXPR && ret != error_mark_node)
8107 {
8108 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8109 {
8110 if (!processing_template_decl && TYPE_P (operand))
8111 {
8112 ret = build_min (SIZEOF_EXPR, size_type_node,
8113 build1 (NOP_EXPR, operand,
8114 error_mark_node));
8115 SIZEOF_EXPR_TYPE_P (ret) = 1;
8116 }
8117 else
8118 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8119 TREE_SIDE_EFFECTS (ret) = 0;
8120 TREE_READONLY (ret) = 1;
8121 }
8122 }
8123
8124 /* Construct a location e.g. :
8125 alignof (expr)
8126 ^~~~~~~~~~~~~~
8127 with start == caret at the start of the "alignof"/"sizeof"
8128 token, with the endpoint at the final closing paren. */
8129 location_t finish_loc
8130 = cp_lexer_previous_token (parser->lexer)->location;
8131 location_t compound_loc
8132 = make_location (start_loc, start_loc, finish_loc);
8133
8134 cp_expr ret_expr (ret);
8135 ret_expr.set_location (compound_loc);
8136 ret_expr = ret_expr.maybe_add_location_wrapper ();
8137 return ret_expr;
8138 }
8139
8140 case RID_BUILTIN_HAS_ATTRIBUTE:
8141 return cp_parser_has_attribute_expression (parser);
8142
8143 case RID_NEW:
8144 return cp_parser_new_expression (parser);
8145
8146 case RID_DELETE:
8147 return cp_parser_delete_expression (parser);
8148
8149 case RID_EXTENSION:
8150 {
8151 /* The saved value of the PEDANTIC flag. */
8152 int saved_pedantic;
8153 tree expr;
8154
8155 /* Save away the PEDANTIC flag. */
8156 cp_parser_extension_opt (parser, &saved_pedantic);
8157 /* Parse the cast-expression. */
8158 expr = cp_parser_simple_cast_expression (parser);
8159 /* Restore the PEDANTIC flag. */
8160 pedantic = saved_pedantic;
8161
8162 return expr;
8163 }
8164
8165 case RID_REALPART:
8166 case RID_IMAGPART:
8167 {
8168 tree expression;
8169
8170 /* Consume the `__real__' or `__imag__' token. */
8171 cp_lexer_consume_token (parser->lexer);
8172 /* Parse the cast-expression. */
8173 expression = cp_parser_simple_cast_expression (parser);
8174 /* Create the complete representation. */
8175 return build_x_unary_op (token->location,
8176 (keyword == RID_REALPART
8177 ? REALPART_EXPR : IMAGPART_EXPR),
8178 expression,
8179 tf_warning_or_error);
8180 }
8181 break;
8182
8183 case RID_TRANSACTION_ATOMIC:
8184 case RID_TRANSACTION_RELAXED:
8185 return cp_parser_transaction_expression (parser, keyword);
8186
8187 case RID_NOEXCEPT:
8188 {
8189 tree expr;
8190 const char *saved_message;
8191 bool saved_integral_constant_expression_p;
8192 bool saved_non_integral_constant_expression_p;
8193 bool saved_greater_than_is_operator_p;
8194
8195 location_t start_loc = token->location;
8196
8197 cp_lexer_consume_token (parser->lexer);
8198 matching_parens parens;
8199 parens.require_open (parser);
8200
8201 saved_message = parser->type_definition_forbidden_message;
8202 parser->type_definition_forbidden_message
8203 = G_("types may not be defined in %<noexcept%> expressions");
8204
8205 saved_integral_constant_expression_p
8206 = parser->integral_constant_expression_p;
8207 saved_non_integral_constant_expression_p
8208 = parser->non_integral_constant_expression_p;
8209 parser->integral_constant_expression_p = false;
8210
8211 saved_greater_than_is_operator_p
8212 = parser->greater_than_is_operator_p;
8213 parser->greater_than_is_operator_p = true;
8214
8215 ++cp_unevaluated_operand;
8216 ++c_inhibit_evaluation_warnings;
8217 ++cp_noexcept_operand;
8218 expr = cp_parser_expression (parser);
8219 --cp_noexcept_operand;
8220 --c_inhibit_evaluation_warnings;
8221 --cp_unevaluated_operand;
8222
8223 parser->greater_than_is_operator_p
8224 = saved_greater_than_is_operator_p;
8225
8226 parser->integral_constant_expression_p
8227 = saved_integral_constant_expression_p;
8228 parser->non_integral_constant_expression_p
8229 = saved_non_integral_constant_expression_p;
8230
8231 parser->type_definition_forbidden_message = saved_message;
8232
8233 location_t finish_loc
8234 = cp_lexer_peek_token (parser->lexer)->location;
8235 parens.require_close (parser);
8236
8237 /* Construct a location of the form:
8238 noexcept (expr)
8239 ^~~~~~~~~~~~~~~
8240 with start == caret, finishing at the close-paren. */
8241 location_t noexcept_loc
8242 = make_location (start_loc, start_loc, finish_loc);
8243
8244 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8245 noexcept_loc);
8246 }
8247
8248 default:
8249 break;
8250 }
8251 }
8252
8253 /* Look for the `:: new' and `:: delete', which also signal the
8254 beginning of a new-expression, or delete-expression,
8255 respectively. If the next token is `::', then it might be one of
8256 these. */
8257 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8258 {
8259 enum rid keyword;
8260
8261 /* See if the token after the `::' is one of the keywords in
8262 which we're interested. */
8263 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8264 /* If it's `new', we have a new-expression. */
8265 if (keyword == RID_NEW)
8266 return cp_parser_new_expression (parser);
8267 /* Similarly, for `delete'. */
8268 else if (keyword == RID_DELETE)
8269 return cp_parser_delete_expression (parser);
8270 }
8271
8272 /* Look for a unary operator. */
8273 unary_operator = cp_parser_unary_operator (token);
8274 /* The `++' and `--' operators can be handled similarly, even though
8275 they are not technically unary-operators in the grammar. */
8276 if (unary_operator == ERROR_MARK)
8277 {
8278 if (token->type == CPP_PLUS_PLUS)
8279 unary_operator = PREINCREMENT_EXPR;
8280 else if (token->type == CPP_MINUS_MINUS)
8281 unary_operator = PREDECREMENT_EXPR;
8282 /* Handle the GNU address-of-label extension. */
8283 else if (cp_parser_allow_gnu_extensions_p (parser)
8284 && token->type == CPP_AND_AND)
8285 {
8286 tree identifier;
8287 tree expression;
8288 location_t start_loc = token->location;
8289
8290 /* Consume the '&&' token. */
8291 cp_lexer_consume_token (parser->lexer);
8292 /* Look for the identifier. */
8293 location_t finish_loc
8294 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8295 identifier = cp_parser_identifier (parser);
8296 /* Construct a location of the form:
8297 &&label
8298 ^~~~~~~
8299 with caret==start at the "&&", finish at the end of the label. */
8300 location_t combined_loc
8301 = make_location (start_loc, start_loc, finish_loc);
8302 /* Create an expression representing the address. */
8303 expression = finish_label_address_expr (identifier, combined_loc);
8304 if (cp_parser_non_integral_constant_expression (parser,
8305 NIC_ADDR_LABEL))
8306 expression = error_mark_node;
8307 return expression;
8308 }
8309 }
8310 if (unary_operator != ERROR_MARK)
8311 {
8312 cp_expr cast_expression;
8313 cp_expr expression = error_mark_node;
8314 non_integral_constant non_constant_p = NIC_NONE;
8315 location_t loc = token->location;
8316 tsubst_flags_t complain = complain_flags (decltype_p);
8317
8318 /* Consume the operator token. */
8319 token = cp_lexer_consume_token (parser->lexer);
8320 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8321
8322 /* Parse the cast-expression. */
8323 cast_expression
8324 = cp_parser_cast_expression (parser,
8325 unary_operator == ADDR_EXPR,
8326 /*cast_p=*/false,
8327 /*decltype*/false,
8328 pidk);
8329
8330 /* Make a location:
8331 OP_TOKEN CAST_EXPRESSION
8332 ^~~~~~~~~~~~~~~~~~~~~~~~~
8333 with start==caret at the operator token, and
8334 extending to the end of the cast_expression. */
8335 loc = make_location (loc, loc, cast_expression.get_finish ());
8336
8337 /* Now, build an appropriate representation. */
8338 switch (unary_operator)
8339 {
8340 case INDIRECT_REF:
8341 non_constant_p = NIC_STAR;
8342 expression = build_x_indirect_ref (loc, cast_expression,
8343 RO_UNARY_STAR,
8344 complain);
8345 /* TODO: build_x_indirect_ref does not always honor the
8346 location, so ensure it is set. */
8347 expression.set_location (loc);
8348 break;
8349
8350 case ADDR_EXPR:
8351 non_constant_p = NIC_ADDR;
8352 /* Fall through. */
8353 case BIT_NOT_EXPR:
8354 expression = build_x_unary_op (loc, unary_operator,
8355 cast_expression,
8356 complain);
8357 /* TODO: build_x_unary_op does not always honor the location,
8358 so ensure it is set. */
8359 expression.set_location (loc);
8360 break;
8361
8362 case PREINCREMENT_EXPR:
8363 case PREDECREMENT_EXPR:
8364 non_constant_p = unary_operator == PREINCREMENT_EXPR
8365 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8366 /* Fall through. */
8367 case NEGATE_EXPR:
8368 /* Immediately fold negation of a constant, unless the constant is 0
8369 (since -0 == 0) or it would overflow. */
8370 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8371 {
8372 tree stripped_expr
8373 = tree_strip_any_location_wrapper (cast_expression);
8374 if (CONSTANT_CLASS_P (stripped_expr)
8375 && !integer_zerop (stripped_expr)
8376 && !TREE_OVERFLOW (stripped_expr))
8377 {
8378 tree folded = fold_build1 (unary_operator,
8379 TREE_TYPE (stripped_expr),
8380 stripped_expr);
8381 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8382 {
8383 expression = maybe_wrap_with_location (folded, loc);
8384 break;
8385 }
8386 }
8387 }
8388 /* Fall through. */
8389 case UNARY_PLUS_EXPR:
8390 case TRUTH_NOT_EXPR:
8391 expression = finish_unary_op_expr (loc, unary_operator,
8392 cast_expression, complain);
8393 break;
8394
8395 default:
8396 gcc_unreachable ();
8397 }
8398
8399 if (non_constant_p != NIC_NONE
8400 && cp_parser_non_integral_constant_expression (parser,
8401 non_constant_p))
8402 expression = error_mark_node;
8403
8404 return expression;
8405 }
8406
8407 return cp_parser_postfix_expression (parser, address_p, cast_p,
8408 /*member_access_only_p=*/false,
8409 decltype_p,
8410 pidk);
8411 }
8412
8413 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8414 unary-operator, the corresponding tree code is returned. */
8415
8416 static enum tree_code
8417 cp_parser_unary_operator (cp_token* token)
8418 {
8419 switch (token->type)
8420 {
8421 case CPP_MULT:
8422 return INDIRECT_REF;
8423
8424 case CPP_AND:
8425 return ADDR_EXPR;
8426
8427 case CPP_PLUS:
8428 return UNARY_PLUS_EXPR;
8429
8430 case CPP_MINUS:
8431 return NEGATE_EXPR;
8432
8433 case CPP_NOT:
8434 return TRUTH_NOT_EXPR;
8435
8436 case CPP_COMPL:
8437 return BIT_NOT_EXPR;
8438
8439 default:
8440 return ERROR_MARK;
8441 }
8442 }
8443
8444 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8445 Returns a representation of the expression. */
8446
8447 static tree
8448 cp_parser_has_attribute_expression (cp_parser *parser)
8449 {
8450 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8451
8452 /* Consume the __builtin_has_attribute token. */
8453 cp_lexer_consume_token (parser->lexer);
8454
8455 matching_parens parens;
8456 if (!parens.require_open (parser))
8457 return error_mark_node;
8458
8459 /* Types cannot be defined in a `sizeof' expression. Save away the
8460 old message. */
8461 const char *saved_message = parser->type_definition_forbidden_message;
8462 /* And create the new one. */
8463 const int kwd = RID_BUILTIN_HAS_ATTRIBUTE;
8464 char *tmp = concat ("types may not be defined in %<",
8465 IDENTIFIER_POINTER (ridpointers[kwd]),
8466 "%> expressions", NULL);
8467 parser->type_definition_forbidden_message = tmp;
8468
8469 /* The restrictions on constant-expressions do not apply inside
8470 sizeof expressions. */
8471 bool saved_integral_constant_expression_p
8472 = parser->integral_constant_expression_p;
8473 bool saved_non_integral_constant_expression_p
8474 = parser->non_integral_constant_expression_p;
8475 parser->integral_constant_expression_p = false;
8476
8477 /* Do not actually evaluate the expression. */
8478 ++cp_unevaluated_operand;
8479 ++c_inhibit_evaluation_warnings;
8480
8481 tree oper = NULL_TREE;
8482
8483 /* We can't be sure yet whether we're looking at a type-id or an
8484 expression. */
8485 cp_parser_parse_tentatively (parser);
8486
8487 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8488 parser->in_type_id_in_expr_p = true;
8489 /* Look for the type-id. */
8490 oper = cp_parser_type_id (parser);
8491 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8492
8493 cp_parser_parse_definitely (parser);
8494
8495 /* If the type-id production did not work out, then we must be
8496 looking at the unary-expression production. */
8497 if (!oper || oper == error_mark_node)
8498 oper = cp_parser_unary_expression (parser);
8499
8500 STRIP_ANY_LOCATION_WRAPPER (oper);
8501
8502 /* Go back to evaluating expressions. */
8503 --cp_unevaluated_operand;
8504 --c_inhibit_evaluation_warnings;
8505
8506 /* Free the message we created. */
8507 free (tmp);
8508 /* And restore the old one. */
8509 parser->type_definition_forbidden_message = saved_message;
8510 parser->integral_constant_expression_p
8511 = saved_integral_constant_expression_p;
8512 parser->non_integral_constant_expression_p
8513 = saved_non_integral_constant_expression_p;
8514
8515 /* Consume the comma if it's there. */
8516 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8517 {
8518 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8519 /*consume_paren=*/true);
8520 return error_mark_node;
8521 }
8522
8523 /* Parse the attribute specification. */
8524 bool ret = false;
8525 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8526 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8527 {
8528 if (oper != error_mark_node)
8529 {
8530 /* Fold constant expressions used in attributes first. */
8531 cp_check_const_attributes (attr);
8532
8533 /* Finally, see if OPER has been declared with ATTR. */
8534 ret = has_attribute (atloc, oper, attr, default_conversion);
8535 }
8536
8537 parens.require_close (parser);
8538 }
8539 else
8540 {
8541 error_at (atloc, "expected identifier");
8542 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8543 }
8544
8545 /* Construct a location e.g. :
8546 __builtin_has_attribute (oper, attr)
8547 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8548 with start == caret at the start of the built-in token,
8549 and with the endpoint at the final closing paren. */
8550 location_t finish_loc
8551 = cp_lexer_previous_token (parser->lexer)->location;
8552 location_t compound_loc
8553 = make_location (start_loc, start_loc, finish_loc);
8554
8555 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8556 ret_expr.set_location (compound_loc);
8557 ret_expr = ret_expr.maybe_add_location_wrapper ();
8558 return ret_expr;
8559 }
8560
8561 /* Parse a new-expression.
8562
8563 new-expression:
8564 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8565 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8566
8567 Returns a representation of the expression. */
8568
8569 static tree
8570 cp_parser_new_expression (cp_parser* parser)
8571 {
8572 bool global_scope_p;
8573 vec<tree, va_gc> *placement;
8574 tree type;
8575 vec<tree, va_gc> *initializer;
8576 tree nelts = NULL_TREE;
8577 tree ret;
8578
8579 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8580
8581 /* Look for the optional `::' operator. */
8582 global_scope_p
8583 = (cp_parser_global_scope_opt (parser,
8584 /*current_scope_valid_p=*/false)
8585 != NULL_TREE);
8586 /* Look for the `new' operator. */
8587 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8588 /* There's no easy way to tell a new-placement from the
8589 `( type-id )' construct. */
8590 cp_parser_parse_tentatively (parser);
8591 /* Look for a new-placement. */
8592 placement = cp_parser_new_placement (parser);
8593 /* If that didn't work out, there's no new-placement. */
8594 if (!cp_parser_parse_definitely (parser))
8595 {
8596 if (placement != NULL)
8597 release_tree_vector (placement);
8598 placement = NULL;
8599 }
8600
8601 /* If the next token is a `(', then we have a parenthesized
8602 type-id. */
8603 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8604 {
8605 cp_token *token;
8606 const char *saved_message = parser->type_definition_forbidden_message;
8607
8608 /* Consume the `('. */
8609 matching_parens parens;
8610 parens.consume_open (parser);
8611
8612 /* Parse the type-id. */
8613 parser->type_definition_forbidden_message
8614 = G_("types may not be defined in a new-expression");
8615 {
8616 type_id_in_expr_sentinel s (parser);
8617 type = cp_parser_type_id (parser);
8618 }
8619 parser->type_definition_forbidden_message = saved_message;
8620
8621 /* Look for the closing `)'. */
8622 parens.require_close (parser);
8623 token = cp_lexer_peek_token (parser->lexer);
8624 /* There should not be a direct-new-declarator in this production,
8625 but GCC used to allowed this, so we check and emit a sensible error
8626 message for this case. */
8627 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8628 {
8629 error_at (token->location,
8630 "array bound forbidden after parenthesized type-id");
8631 inform (token->location,
8632 "try removing the parentheses around the type-id");
8633 cp_parser_direct_new_declarator (parser);
8634 }
8635 }
8636 /* Otherwise, there must be a new-type-id. */
8637 else
8638 type = cp_parser_new_type_id (parser, &nelts);
8639
8640 /* If the next token is a `(' or '{', then we have a new-initializer. */
8641 cp_token *token = cp_lexer_peek_token (parser->lexer);
8642 if (token->type == CPP_OPEN_PAREN
8643 || token->type == CPP_OPEN_BRACE)
8644 initializer = cp_parser_new_initializer (parser);
8645 else
8646 initializer = NULL;
8647
8648 /* A new-expression may not appear in an integral constant
8649 expression. */
8650 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8651 ret = error_mark_node;
8652 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8653 of a new-type-id or type-id of a new-expression, the new-expression shall
8654 contain a new-initializer of the form ( assignment-expression )".
8655 Additionally, consistently with the spirit of DR 1467, we want to accept
8656 'new auto { 2 }' too. */
8657 else if ((ret = type_uses_auto (type))
8658 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8659 && (vec_safe_length (initializer) != 1
8660 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8661 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8662 {
8663 error_at (token->location,
8664 "initialization of new-expression for type %<auto%> "
8665 "requires exactly one element");
8666 ret = error_mark_node;
8667 }
8668 else
8669 {
8670 /* Construct a location e.g.:
8671 ptr = new int[100]
8672 ^~~~~~~~~~~~
8673 with caret == start at the start of the "new" token, and the end
8674 at the end of the final token we consumed. */
8675 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8676 location_t end_loc = get_finish (end_tok->location);
8677 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8678
8679 /* Create a representation of the new-expression. */
8680 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8681 tf_warning_or_error);
8682 protected_set_expr_location (ret, combined_loc);
8683 }
8684
8685 if (placement != NULL)
8686 release_tree_vector (placement);
8687 if (initializer != NULL)
8688 release_tree_vector (initializer);
8689
8690 return ret;
8691 }
8692
8693 /* Parse a new-placement.
8694
8695 new-placement:
8696 ( expression-list )
8697
8698 Returns the same representation as for an expression-list. */
8699
8700 static vec<tree, va_gc> *
8701 cp_parser_new_placement (cp_parser* parser)
8702 {
8703 vec<tree, va_gc> *expression_list;
8704
8705 /* Parse the expression-list. */
8706 expression_list = (cp_parser_parenthesized_expression_list
8707 (parser, non_attr, /*cast_p=*/false,
8708 /*allow_expansion_p=*/true,
8709 /*non_constant_p=*/NULL));
8710
8711 if (expression_list && expression_list->is_empty ())
8712 error ("expected expression-list or type-id");
8713
8714 return expression_list;
8715 }
8716
8717 /* Parse a new-type-id.
8718
8719 new-type-id:
8720 type-specifier-seq new-declarator [opt]
8721
8722 Returns the TYPE allocated. If the new-type-id indicates an array
8723 type, *NELTS is set to the number of elements in the last array
8724 bound; the TYPE will not include the last array bound. */
8725
8726 static tree
8727 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8728 {
8729 cp_decl_specifier_seq type_specifier_seq;
8730 cp_declarator *new_declarator;
8731 cp_declarator *declarator;
8732 cp_declarator *outer_declarator;
8733 const char *saved_message;
8734
8735 /* The type-specifier sequence must not contain type definitions.
8736 (It cannot contain declarations of new types either, but if they
8737 are not definitions we will catch that because they are not
8738 complete.) */
8739 saved_message = parser->type_definition_forbidden_message;
8740 parser->type_definition_forbidden_message
8741 = G_("types may not be defined in a new-type-id");
8742 /* Parse the type-specifier-seq. */
8743 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8744 /*is_declaration=*/false,
8745 /*is_trailing_return=*/false,
8746 &type_specifier_seq);
8747 /* Restore the old message. */
8748 parser->type_definition_forbidden_message = saved_message;
8749
8750 if (type_specifier_seq.type == error_mark_node)
8751 return error_mark_node;
8752
8753 /* Parse the new-declarator. */
8754 new_declarator = cp_parser_new_declarator_opt (parser);
8755
8756 /* Determine the number of elements in the last array dimension, if
8757 any. */
8758 *nelts = NULL_TREE;
8759 /* Skip down to the last array dimension. */
8760 declarator = new_declarator;
8761 outer_declarator = NULL;
8762 while (declarator && (declarator->kind == cdk_pointer
8763 || declarator->kind == cdk_ptrmem))
8764 {
8765 outer_declarator = declarator;
8766 declarator = declarator->declarator;
8767 }
8768 while (declarator
8769 && declarator->kind == cdk_array
8770 && declarator->declarator
8771 && declarator->declarator->kind == cdk_array)
8772 {
8773 outer_declarator = declarator;
8774 declarator = declarator->declarator;
8775 }
8776
8777 if (declarator && declarator->kind == cdk_array)
8778 {
8779 *nelts = declarator->u.array.bounds;
8780 if (*nelts == error_mark_node)
8781 *nelts = integer_one_node;
8782
8783 if (outer_declarator)
8784 outer_declarator->declarator = declarator->declarator;
8785 else
8786 new_declarator = NULL;
8787 }
8788
8789 return groktypename (&type_specifier_seq, new_declarator, false);
8790 }
8791
8792 /* Parse an (optional) new-declarator.
8793
8794 new-declarator:
8795 ptr-operator new-declarator [opt]
8796 direct-new-declarator
8797
8798 Returns the declarator. */
8799
8800 static cp_declarator *
8801 cp_parser_new_declarator_opt (cp_parser* parser)
8802 {
8803 enum tree_code code;
8804 tree type, std_attributes = NULL_TREE;
8805 cp_cv_quals cv_quals;
8806
8807 /* We don't know if there's a ptr-operator next, or not. */
8808 cp_parser_parse_tentatively (parser);
8809 /* Look for a ptr-operator. */
8810 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8811 /* If that worked, look for more new-declarators. */
8812 if (cp_parser_parse_definitely (parser))
8813 {
8814 cp_declarator *declarator;
8815
8816 /* Parse another optional declarator. */
8817 declarator = cp_parser_new_declarator_opt (parser);
8818
8819 declarator = cp_parser_make_indirect_declarator
8820 (code, type, cv_quals, declarator, std_attributes);
8821
8822 return declarator;
8823 }
8824
8825 /* If the next token is a `[', there is a direct-new-declarator. */
8826 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8827 return cp_parser_direct_new_declarator (parser);
8828
8829 return NULL;
8830 }
8831
8832 /* Parse a direct-new-declarator.
8833
8834 direct-new-declarator:
8835 [ expression ]
8836 direct-new-declarator [constant-expression]
8837
8838 */
8839
8840 static cp_declarator *
8841 cp_parser_direct_new_declarator (cp_parser* parser)
8842 {
8843 cp_declarator *declarator = NULL;
8844
8845 while (true)
8846 {
8847 tree expression;
8848 cp_token *token;
8849
8850 /* Look for the opening `['. */
8851 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8852
8853 token = cp_lexer_peek_token (parser->lexer);
8854 expression = cp_parser_expression (parser);
8855 /* The standard requires that the expression have integral
8856 type. DR 74 adds enumeration types. We believe that the
8857 real intent is that these expressions be handled like the
8858 expression in a `switch' condition, which also allows
8859 classes with a single conversion to integral or
8860 enumeration type. */
8861 if (!processing_template_decl)
8862 {
8863 expression
8864 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8865 expression,
8866 /*complain=*/true);
8867 if (!expression)
8868 {
8869 error_at (token->location,
8870 "expression in new-declarator must have integral "
8871 "or enumeration type");
8872 expression = error_mark_node;
8873 }
8874 }
8875
8876 /* Look for the closing `]'. */
8877 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8878
8879 /* Add this bound to the declarator. */
8880 declarator = make_array_declarator (declarator, expression);
8881
8882 /* If the next token is not a `[', then there are no more
8883 bounds. */
8884 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8885 break;
8886 }
8887
8888 return declarator;
8889 }
8890
8891 /* Parse a new-initializer.
8892
8893 new-initializer:
8894 ( expression-list [opt] )
8895 braced-init-list
8896
8897 Returns a representation of the expression-list. */
8898
8899 static vec<tree, va_gc> *
8900 cp_parser_new_initializer (cp_parser* parser)
8901 {
8902 vec<tree, va_gc> *expression_list;
8903
8904 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8905 {
8906 tree t;
8907 bool expr_non_constant_p;
8908 cp_lexer_set_source_position (parser->lexer);
8909 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8910 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8911 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8912 expression_list = make_tree_vector_single (t);
8913 }
8914 else
8915 expression_list = (cp_parser_parenthesized_expression_list
8916 (parser, non_attr, /*cast_p=*/false,
8917 /*allow_expansion_p=*/true,
8918 /*non_constant_p=*/NULL));
8919
8920 return expression_list;
8921 }
8922
8923 /* Parse a delete-expression.
8924
8925 delete-expression:
8926 :: [opt] delete cast-expression
8927 :: [opt] delete [ ] cast-expression
8928
8929 Returns a representation of the expression. */
8930
8931 static tree
8932 cp_parser_delete_expression (cp_parser* parser)
8933 {
8934 bool global_scope_p;
8935 bool array_p;
8936 tree expression;
8937
8938 /* Look for the optional `::' operator. */
8939 global_scope_p
8940 = (cp_parser_global_scope_opt (parser,
8941 /*current_scope_valid_p=*/false)
8942 != NULL_TREE);
8943 /* Look for the `delete' keyword. */
8944 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8945 /* See if the array syntax is in use. */
8946 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8947 {
8948 /* Consume the `[' token. */
8949 cp_lexer_consume_token (parser->lexer);
8950 /* Look for the `]' token. */
8951 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8952 /* Remember that this is the `[]' construct. */
8953 array_p = true;
8954 }
8955 else
8956 array_p = false;
8957
8958 /* Parse the cast-expression. */
8959 expression = cp_parser_simple_cast_expression (parser);
8960
8961 /* A delete-expression may not appear in an integral constant
8962 expression. */
8963 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8964 return error_mark_node;
8965
8966 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8967 tf_warning_or_error);
8968 }
8969
8970 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8971 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8972 0 otherwise. */
8973
8974 static int
8975 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8976 {
8977 cp_token *token = cp_lexer_peek_token (parser->lexer);
8978 switch (token->type)
8979 {
8980 case CPP_COMMA:
8981 case CPP_SEMICOLON:
8982 case CPP_QUERY:
8983 case CPP_COLON:
8984 case CPP_CLOSE_SQUARE:
8985 case CPP_CLOSE_PAREN:
8986 case CPP_CLOSE_BRACE:
8987 case CPP_OPEN_BRACE:
8988 case CPP_DOT:
8989 case CPP_DOT_STAR:
8990 case CPP_DEREF:
8991 case CPP_DEREF_STAR:
8992 case CPP_DIV:
8993 case CPP_MOD:
8994 case CPP_LSHIFT:
8995 case CPP_RSHIFT:
8996 case CPP_LESS:
8997 case CPP_GREATER:
8998 case CPP_LESS_EQ:
8999 case CPP_GREATER_EQ:
9000 case CPP_EQ_EQ:
9001 case CPP_NOT_EQ:
9002 case CPP_EQ:
9003 case CPP_MULT_EQ:
9004 case CPP_DIV_EQ:
9005 case CPP_MOD_EQ:
9006 case CPP_PLUS_EQ:
9007 case CPP_MINUS_EQ:
9008 case CPP_RSHIFT_EQ:
9009 case CPP_LSHIFT_EQ:
9010 case CPP_AND_EQ:
9011 case CPP_XOR_EQ:
9012 case CPP_OR_EQ:
9013 case CPP_XOR:
9014 case CPP_OR:
9015 case CPP_OR_OR:
9016 case CPP_EOF:
9017 case CPP_ELLIPSIS:
9018 return 0;
9019
9020 case CPP_OPEN_PAREN:
9021 /* In ((type ()) () the last () isn't a valid cast-expression,
9022 so the whole must be parsed as postfix-expression. */
9023 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9024 != CPP_CLOSE_PAREN;
9025
9026 case CPP_OPEN_SQUARE:
9027 /* '[' may start a primary-expression in obj-c++ and in C++11,
9028 as a lambda-expression, eg, '(void)[]{}'. */
9029 if (cxx_dialect >= cxx11)
9030 return -1;
9031 return c_dialect_objc ();
9032
9033 case CPP_PLUS_PLUS:
9034 case CPP_MINUS_MINUS:
9035 /* '++' and '--' may or may not start a cast-expression:
9036
9037 struct T { void operator++(int); };
9038 void f() { (T())++; }
9039
9040 vs
9041
9042 int a;
9043 (int)++a; */
9044 return -1;
9045
9046 default:
9047 return 1;
9048 }
9049 }
9050
9051 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9052 in the order: const_cast, static_cast, reinterpret_cast.
9053
9054 Don't suggest dynamic_cast.
9055
9056 Return the first legal cast kind found, or NULL otherwise. */
9057
9058 static const char *
9059 get_cast_suggestion (tree dst_type, tree orig_expr)
9060 {
9061 tree trial;
9062
9063 /* Reuse the parser logic by attempting to build the various kinds of
9064 cast, with "complain" disabled.
9065 Identify the first such cast that is valid. */
9066
9067 /* Don't attempt to run such logic within template processing. */
9068 if (processing_template_decl)
9069 return NULL;
9070
9071 /* First try const_cast. */
9072 trial = build_const_cast (dst_type, orig_expr, tf_none);
9073 if (trial != error_mark_node)
9074 return "const_cast";
9075
9076 /* If that fails, try static_cast. */
9077 trial = build_static_cast (dst_type, orig_expr, tf_none);
9078 if (trial != error_mark_node)
9079 return "static_cast";
9080
9081 /* Finally, try reinterpret_cast. */
9082 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9083 if (trial != error_mark_node)
9084 return "reinterpret_cast";
9085
9086 /* No such cast possible. */
9087 return NULL;
9088 }
9089
9090 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9091 suggesting how to convert a C-style cast of the form:
9092
9093 (DST_TYPE)ORIG_EXPR
9094
9095 to a C++-style cast.
9096
9097 The primary range of RICHLOC is asssumed to be that of the original
9098 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9099 of the parens in the C-style cast. */
9100
9101 static void
9102 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9103 location_t close_paren_loc, tree orig_expr,
9104 tree dst_type)
9105 {
9106 /* This function is non-trivial, so bail out now if the warning isn't
9107 going to be emitted. */
9108 if (!warn_old_style_cast)
9109 return;
9110
9111 /* Try to find a legal C++ cast, trying them in order:
9112 const_cast, static_cast, reinterpret_cast. */
9113 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9114 if (!cast_suggestion)
9115 return;
9116
9117 /* Replace the open paren with "CAST_SUGGESTION<". */
9118 pretty_printer pp;
9119 pp_printf (&pp, "%s<", cast_suggestion);
9120 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9121
9122 /* Replace the close paren with "> (". */
9123 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9124
9125 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9126 rich_loc->add_fixit_insert_after (")");
9127 }
9128
9129
9130 /* Parse a cast-expression.
9131
9132 cast-expression:
9133 unary-expression
9134 ( type-id ) cast-expression
9135
9136 ADDRESS_P is true iff the unary-expression is appearing as the
9137 operand of the `&' operator. CAST_P is true if this expression is
9138 the target of a cast.
9139
9140 Returns a representation of the expression. */
9141
9142 static cp_expr
9143 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9144 bool decltype_p, cp_id_kind * pidk)
9145 {
9146 /* If it's a `(', then we might be looking at a cast. */
9147 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9148 {
9149 tree type = NULL_TREE;
9150 cp_expr expr (NULL_TREE);
9151 int cast_expression = 0;
9152 const char *saved_message;
9153
9154 /* There's no way to know yet whether or not this is a cast.
9155 For example, `(int (3))' is a unary-expression, while `(int)
9156 3' is a cast. So, we resort to parsing tentatively. */
9157 cp_parser_parse_tentatively (parser);
9158 /* Types may not be defined in a cast. */
9159 saved_message = parser->type_definition_forbidden_message;
9160 parser->type_definition_forbidden_message
9161 = G_("types may not be defined in casts");
9162 /* Consume the `('. */
9163 matching_parens parens;
9164 cp_token *open_paren = parens.consume_open (parser);
9165 location_t open_paren_loc = open_paren->location;
9166 location_t close_paren_loc = UNKNOWN_LOCATION;
9167
9168 /* A very tricky bit is that `(struct S) { 3 }' is a
9169 compound-literal (which we permit in C++ as an extension).
9170 But, that construct is not a cast-expression -- it is a
9171 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9172 is legal; if the compound-literal were a cast-expression,
9173 you'd need an extra set of parentheses.) But, if we parse
9174 the type-id, and it happens to be a class-specifier, then we
9175 will commit to the parse at that point, because we cannot
9176 undo the action that is done when creating a new class. So,
9177 then we cannot back up and do a postfix-expression.
9178
9179 Another tricky case is the following (c++/29234):
9180
9181 struct S { void operator () (); };
9182
9183 void foo ()
9184 {
9185 ( S()() );
9186 }
9187
9188 As a type-id we parse the parenthesized S()() as a function
9189 returning a function, groktypename complains and we cannot
9190 back up in this case either.
9191
9192 Therefore, we scan ahead to the closing `)', and check to see
9193 if the tokens after the `)' can start a cast-expression. Otherwise
9194 we are dealing with an unary-expression, a postfix-expression
9195 or something else.
9196
9197 Yet another tricky case, in C++11, is the following (c++/54891):
9198
9199 (void)[]{};
9200
9201 The issue is that usually, besides the case of lambda-expressions,
9202 the parenthesized type-id cannot be followed by '[', and, eg, we
9203 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9204 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9205 we don't commit, we try a cast-expression, then an unary-expression.
9206
9207 Save tokens so that we can put them back. */
9208 cp_lexer_save_tokens (parser->lexer);
9209
9210 /* We may be looking at a cast-expression. */
9211 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9212 /*consume_paren=*/true))
9213 cast_expression
9214 = cp_parser_tokens_start_cast_expression (parser);
9215
9216 /* Roll back the tokens we skipped. */
9217 cp_lexer_rollback_tokens (parser->lexer);
9218 /* If we aren't looking at a cast-expression, simulate an error so
9219 that the call to cp_parser_error_occurred below returns true. */
9220 if (!cast_expression)
9221 cp_parser_simulate_error (parser);
9222 else
9223 {
9224 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9225 parser->in_type_id_in_expr_p = true;
9226 /* Look for the type-id. */
9227 type = cp_parser_type_id (parser);
9228 /* Look for the closing `)'. */
9229 cp_token *close_paren = parens.require_close (parser);
9230 if (close_paren)
9231 close_paren_loc = close_paren->location;
9232 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9233 }
9234
9235 /* Restore the saved message. */
9236 parser->type_definition_forbidden_message = saved_message;
9237
9238 /* At this point this can only be either a cast or a
9239 parenthesized ctor such as `(T ())' that looks like a cast to
9240 function returning T. */
9241 if (!cp_parser_error_occurred (parser))
9242 {
9243 /* Only commit if the cast-expression doesn't start with
9244 '++', '--', or '[' in C++11. */
9245 if (cast_expression > 0)
9246 cp_parser_commit_to_topmost_tentative_parse (parser);
9247
9248 expr = cp_parser_cast_expression (parser,
9249 /*address_p=*/false,
9250 /*cast_p=*/true,
9251 /*decltype_p=*/false,
9252 pidk);
9253
9254 if (cp_parser_parse_definitely (parser))
9255 {
9256 /* Warn about old-style casts, if so requested. */
9257 if (warn_old_style_cast
9258 && !in_system_header_at (input_location)
9259 && !VOID_TYPE_P (type)
9260 && current_lang_name != lang_name_c)
9261 {
9262 gcc_rich_location rich_loc (input_location);
9263 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9264 expr, type);
9265 warning_at (&rich_loc, OPT_Wold_style_cast,
9266 "use of old-style cast to %q#T", type);
9267 }
9268
9269 /* Only type conversions to integral or enumeration types
9270 can be used in constant-expressions. */
9271 if (!cast_valid_in_integral_constant_expression_p (type)
9272 && cp_parser_non_integral_constant_expression (parser,
9273 NIC_CAST))
9274 return error_mark_node;
9275
9276 /* Perform the cast. */
9277 /* Make a location:
9278 (TYPE) EXPR
9279 ^~~~~~~~~~~
9280 with start==caret at the open paren, extending to the
9281 end of "expr". */
9282 location_t cast_loc = make_location (open_paren_loc,
9283 open_paren_loc,
9284 expr.get_finish ());
9285 expr = build_c_cast (cast_loc, type, expr);
9286 return expr;
9287 }
9288 }
9289 else
9290 cp_parser_abort_tentative_parse (parser);
9291 }
9292
9293 /* If we get here, then it's not a cast, so it must be a
9294 unary-expression. */
9295 return cp_parser_unary_expression (parser, pidk, address_p,
9296 cast_p, decltype_p);
9297 }
9298
9299 /* Parse a binary expression of the general form:
9300
9301 pm-expression:
9302 cast-expression
9303 pm-expression .* cast-expression
9304 pm-expression ->* cast-expression
9305
9306 multiplicative-expression:
9307 pm-expression
9308 multiplicative-expression * pm-expression
9309 multiplicative-expression / pm-expression
9310 multiplicative-expression % pm-expression
9311
9312 additive-expression:
9313 multiplicative-expression
9314 additive-expression + multiplicative-expression
9315 additive-expression - multiplicative-expression
9316
9317 shift-expression:
9318 additive-expression
9319 shift-expression << additive-expression
9320 shift-expression >> additive-expression
9321
9322 relational-expression:
9323 shift-expression
9324 relational-expression < shift-expression
9325 relational-expression > shift-expression
9326 relational-expression <= shift-expression
9327 relational-expression >= shift-expression
9328
9329 GNU Extension:
9330
9331 relational-expression:
9332 relational-expression <? shift-expression
9333 relational-expression >? shift-expression
9334
9335 equality-expression:
9336 relational-expression
9337 equality-expression == relational-expression
9338 equality-expression != relational-expression
9339
9340 and-expression:
9341 equality-expression
9342 and-expression & equality-expression
9343
9344 exclusive-or-expression:
9345 and-expression
9346 exclusive-or-expression ^ and-expression
9347
9348 inclusive-or-expression:
9349 exclusive-or-expression
9350 inclusive-or-expression | exclusive-or-expression
9351
9352 logical-and-expression:
9353 inclusive-or-expression
9354 logical-and-expression && inclusive-or-expression
9355
9356 logical-or-expression:
9357 logical-and-expression
9358 logical-or-expression || logical-and-expression
9359
9360 All these are implemented with a single function like:
9361
9362 binary-expression:
9363 simple-cast-expression
9364 binary-expression <token> binary-expression
9365
9366 CAST_P is true if this expression is the target of a cast.
9367
9368 The binops_by_token map is used to get the tree codes for each <token> type.
9369 binary-expressions are associated according to a precedence table. */
9370
9371 #define TOKEN_PRECEDENCE(token) \
9372 (((token->type == CPP_GREATER \
9373 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9374 && !parser->greater_than_is_operator_p) \
9375 ? PREC_NOT_OPERATOR \
9376 : binops_by_token[token->type].prec)
9377
9378 static cp_expr
9379 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9380 bool no_toplevel_fold_p,
9381 bool decltype_p,
9382 enum cp_parser_prec prec,
9383 cp_id_kind * pidk)
9384 {
9385 cp_parser_expression_stack stack;
9386 cp_parser_expression_stack_entry *sp = &stack[0];
9387 cp_parser_expression_stack_entry current;
9388 cp_expr rhs;
9389 cp_token *token;
9390 enum tree_code rhs_type;
9391 enum cp_parser_prec new_prec, lookahead_prec;
9392 tree overload;
9393
9394 /* Parse the first expression. */
9395 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9396 ? TRUTH_NOT_EXPR : ERROR_MARK);
9397 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9398 cast_p, decltype_p, pidk);
9399 current.prec = prec;
9400
9401 if (cp_parser_error_occurred (parser))
9402 return error_mark_node;
9403
9404 for (;;)
9405 {
9406 /* Get an operator token. */
9407 token = cp_lexer_peek_token (parser->lexer);
9408
9409 if (warn_cxx11_compat
9410 && token->type == CPP_RSHIFT
9411 && !parser->greater_than_is_operator_p)
9412 {
9413 if (warning_at (token->location, OPT_Wc__11_compat,
9414 "%<>>%> operator is treated"
9415 " as two right angle brackets in C++11"))
9416 inform (token->location,
9417 "suggest parentheses around %<>>%> expression");
9418 }
9419
9420 new_prec = TOKEN_PRECEDENCE (token);
9421 if (new_prec != PREC_NOT_OPERATOR
9422 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9423 /* This is a fold-expression; handle it later. */
9424 new_prec = PREC_NOT_OPERATOR;
9425
9426 /* Popping an entry off the stack means we completed a subexpression:
9427 - either we found a token which is not an operator (`>' where it is not
9428 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9429 will happen repeatedly;
9430 - or, we found an operator which has lower priority. This is the case
9431 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9432 parsing `3 * 4'. */
9433 if (new_prec <= current.prec)
9434 {
9435 if (sp == stack)
9436 break;
9437 else
9438 goto pop;
9439 }
9440
9441 get_rhs:
9442 current.tree_type = binops_by_token[token->type].tree_type;
9443 current.loc = token->location;
9444
9445 /* We used the operator token. */
9446 cp_lexer_consume_token (parser->lexer);
9447
9448 /* For "false && x" or "true || x", x will never be executed;
9449 disable warnings while evaluating it. */
9450 if (current.tree_type == TRUTH_ANDIF_EXPR)
9451 c_inhibit_evaluation_warnings +=
9452 cp_fully_fold (current.lhs) == truthvalue_false_node;
9453 else if (current.tree_type == TRUTH_ORIF_EXPR)
9454 c_inhibit_evaluation_warnings +=
9455 cp_fully_fold (current.lhs) == truthvalue_true_node;
9456
9457 /* Extract another operand. It may be the RHS of this expression
9458 or the LHS of a new, higher priority expression. */
9459 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9460 ? TRUTH_NOT_EXPR : ERROR_MARK);
9461 rhs = cp_parser_simple_cast_expression (parser);
9462
9463 /* Get another operator token. Look up its precedence to avoid
9464 building a useless (immediately popped) stack entry for common
9465 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9466 token = cp_lexer_peek_token (parser->lexer);
9467 lookahead_prec = TOKEN_PRECEDENCE (token);
9468 if (lookahead_prec != PREC_NOT_OPERATOR
9469 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9470 lookahead_prec = PREC_NOT_OPERATOR;
9471 if (lookahead_prec > new_prec)
9472 {
9473 /* ... and prepare to parse the RHS of the new, higher priority
9474 expression. Since precedence levels on the stack are
9475 monotonically increasing, we do not have to care about
9476 stack overflows. */
9477 *sp = current;
9478 ++sp;
9479 current.lhs = rhs;
9480 current.lhs_type = rhs_type;
9481 current.prec = new_prec;
9482 new_prec = lookahead_prec;
9483 goto get_rhs;
9484
9485 pop:
9486 lookahead_prec = new_prec;
9487 /* If the stack is not empty, we have parsed into LHS the right side
9488 (`4' in the example above) of an expression we had suspended.
9489 We can use the information on the stack to recover the LHS (`3')
9490 from the stack together with the tree code (`MULT_EXPR'), and
9491 the precedence of the higher level subexpression
9492 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9493 which will be used to actually build the additive expression. */
9494 rhs = current.lhs;
9495 rhs_type = current.lhs_type;
9496 --sp;
9497 current = *sp;
9498 }
9499
9500 /* Undo the disabling of warnings done above. */
9501 if (current.tree_type == TRUTH_ANDIF_EXPR)
9502 c_inhibit_evaluation_warnings -=
9503 cp_fully_fold (current.lhs) == truthvalue_false_node;
9504 else if (current.tree_type == TRUTH_ORIF_EXPR)
9505 c_inhibit_evaluation_warnings -=
9506 cp_fully_fold (current.lhs) == truthvalue_true_node;
9507
9508 if (warn_logical_not_paren
9509 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9510 && current.lhs_type == TRUTH_NOT_EXPR
9511 /* Avoid warning for !!x == y. */
9512 && (TREE_CODE (current.lhs) != NE_EXPR
9513 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9514 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9515 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9516 /* Avoid warning for !b == y where b is boolean. */
9517 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9518 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9519 != BOOLEAN_TYPE))))
9520 /* Avoid warning for !!b == y where b is boolean. */
9521 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9522 || TREE_TYPE (current.lhs) == NULL_TREE
9523 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9524 warn_logical_not_parentheses (current.loc, current.tree_type,
9525 current.lhs, maybe_constant_value (rhs));
9526
9527 overload = NULL;
9528
9529 location_t combined_loc = make_location (current.loc,
9530 current.lhs.get_start (),
9531 rhs.get_finish ());
9532
9533 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9534 ERROR_MARK for everything that is not a binary expression.
9535 This makes warn_about_parentheses miss some warnings that
9536 involve unary operators. For unary expressions we should
9537 pass the correct tree_code unless the unary expression was
9538 surrounded by parentheses.
9539 */
9540 if (no_toplevel_fold_p
9541 && lookahead_prec <= current.prec
9542 && sp == stack)
9543 {
9544 if (current.lhs == error_mark_node || rhs == error_mark_node)
9545 current.lhs = error_mark_node;
9546 else
9547 {
9548 current.lhs
9549 = build_min (current.tree_type,
9550 TREE_CODE_CLASS (current.tree_type)
9551 == tcc_comparison
9552 ? boolean_type_node : TREE_TYPE (current.lhs),
9553 current.lhs.get_value (), rhs.get_value ());
9554 SET_EXPR_LOCATION (current.lhs, combined_loc);
9555 }
9556 }
9557 else
9558 {
9559 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9560 current.lhs, current.lhs_type,
9561 rhs, rhs_type, &overload,
9562 complain_flags (decltype_p));
9563 /* TODO: build_x_binary_op doesn't always honor the location. */
9564 current.lhs.set_location (combined_loc);
9565 }
9566 current.lhs_type = current.tree_type;
9567
9568 /* If the binary operator required the use of an overloaded operator,
9569 then this expression cannot be an integral constant-expression.
9570 An overloaded operator can be used even if both operands are
9571 otherwise permissible in an integral constant-expression if at
9572 least one of the operands is of enumeration type. */
9573
9574 if (overload
9575 && cp_parser_non_integral_constant_expression (parser,
9576 NIC_OVERLOADED))
9577 return error_mark_node;
9578 }
9579
9580 return current.lhs;
9581 }
9582
9583 static cp_expr
9584 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9585 bool no_toplevel_fold_p,
9586 enum cp_parser_prec prec,
9587 cp_id_kind * pidk)
9588 {
9589 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9590 /*decltype*/false, prec, pidk);
9591 }
9592
9593 /* Parse the `? expression : assignment-expression' part of a
9594 conditional-expression. The LOGICAL_OR_EXPR is the
9595 logical-or-expression that started the conditional-expression.
9596 Returns a representation of the entire conditional-expression.
9597
9598 This routine is used by cp_parser_assignment_expression.
9599
9600 ? expression : assignment-expression
9601
9602 GNU Extensions:
9603
9604 ? : assignment-expression */
9605
9606 static tree
9607 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9608 {
9609 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9610 cp_expr assignment_expr;
9611 struct cp_token *token;
9612 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9613
9614 /* Consume the `?' token. */
9615 cp_lexer_consume_token (parser->lexer);
9616 token = cp_lexer_peek_token (parser->lexer);
9617 if (cp_parser_allow_gnu_extensions_p (parser)
9618 && token->type == CPP_COLON)
9619 {
9620 pedwarn (token->location, OPT_Wpedantic,
9621 "ISO C++ does not allow ?: with omitted middle operand");
9622 /* Implicit true clause. */
9623 expr = NULL_TREE;
9624 c_inhibit_evaluation_warnings +=
9625 folded_logical_or_expr == truthvalue_true_node;
9626 warn_for_omitted_condop (token->location, logical_or_expr);
9627 }
9628 else
9629 {
9630 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9631 parser->colon_corrects_to_scope_p = false;
9632 /* Parse the expression. */
9633 c_inhibit_evaluation_warnings +=
9634 folded_logical_or_expr == truthvalue_false_node;
9635 expr = cp_parser_expression (parser);
9636 c_inhibit_evaluation_warnings +=
9637 ((folded_logical_or_expr == truthvalue_true_node)
9638 - (folded_logical_or_expr == truthvalue_false_node));
9639 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9640 }
9641
9642 /* The next token should be a `:'. */
9643 cp_parser_require (parser, CPP_COLON, RT_COLON);
9644 /* Parse the assignment-expression. */
9645 assignment_expr = cp_parser_assignment_expression (parser);
9646 c_inhibit_evaluation_warnings -=
9647 folded_logical_or_expr == truthvalue_true_node;
9648
9649 /* Make a location:
9650 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9651 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9652 with the caret at the "?", ranging from the start of
9653 the logical_or_expr to the end of the assignment_expr. */
9654 loc = make_location (loc,
9655 logical_or_expr.get_start (),
9656 assignment_expr.get_finish ());
9657
9658 /* Build the conditional-expression. */
9659 return build_x_conditional_expr (loc, logical_or_expr,
9660 expr,
9661 assignment_expr,
9662 tf_warning_or_error);
9663 }
9664
9665 /* Parse an assignment-expression.
9666
9667 assignment-expression:
9668 conditional-expression
9669 logical-or-expression assignment-operator assignment_expression
9670 throw-expression
9671
9672 CAST_P is true if this expression is the target of a cast.
9673 DECLTYPE_P is true if this expression is the operand of decltype.
9674
9675 Returns a representation for the expression. */
9676
9677 static cp_expr
9678 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9679 bool cast_p, bool decltype_p)
9680 {
9681 cp_expr expr;
9682
9683 /* If the next token is the `throw' keyword, then we're looking at
9684 a throw-expression. */
9685 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9686 expr = cp_parser_throw_expression (parser);
9687 /* Otherwise, it must be that we are looking at a
9688 logical-or-expression. */
9689 else
9690 {
9691 /* Parse the binary expressions (logical-or-expression). */
9692 expr = cp_parser_binary_expression (parser, cast_p, false,
9693 decltype_p,
9694 PREC_NOT_OPERATOR, pidk);
9695 /* If the next token is a `?' then we're actually looking at a
9696 conditional-expression. */
9697 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9698 return cp_parser_question_colon_clause (parser, expr);
9699 else
9700 {
9701 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9702
9703 /* If it's an assignment-operator, we're using the second
9704 production. */
9705 enum tree_code assignment_operator
9706 = cp_parser_assignment_operator_opt (parser);
9707 if (assignment_operator != ERROR_MARK)
9708 {
9709 bool non_constant_p;
9710
9711 /* Parse the right-hand side of the assignment. */
9712 cp_expr rhs = cp_parser_initializer_clause (parser,
9713 &non_constant_p);
9714
9715 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9716 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9717
9718 /* An assignment may not appear in a
9719 constant-expression. */
9720 if (cp_parser_non_integral_constant_expression (parser,
9721 NIC_ASSIGNMENT))
9722 return error_mark_node;
9723 /* Build the assignment expression. Its default
9724 location:
9725 LHS = RHS
9726 ~~~~^~~~~
9727 is the location of the '=' token as the
9728 caret, ranging from the start of the lhs to the
9729 end of the rhs. */
9730 loc = make_location (loc,
9731 expr.get_start (),
9732 rhs.get_finish ());
9733 expr = build_x_modify_expr (loc, expr,
9734 assignment_operator,
9735 rhs,
9736 complain_flags (decltype_p));
9737 /* TODO: build_x_modify_expr doesn't honor the location,
9738 so we must set it here. */
9739 expr.set_location (loc);
9740 }
9741 }
9742 }
9743
9744 return expr;
9745 }
9746
9747 /* Parse an (optional) assignment-operator.
9748
9749 assignment-operator: one of
9750 = *= /= %= += -= >>= <<= &= ^= |=
9751
9752 GNU Extension:
9753
9754 assignment-operator: one of
9755 <?= >?=
9756
9757 If the next token is an assignment operator, the corresponding tree
9758 code is returned, and the token is consumed. For example, for
9759 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9760 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9761 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9762 operator, ERROR_MARK is returned. */
9763
9764 static enum tree_code
9765 cp_parser_assignment_operator_opt (cp_parser* parser)
9766 {
9767 enum tree_code op;
9768 cp_token *token;
9769
9770 /* Peek at the next token. */
9771 token = cp_lexer_peek_token (parser->lexer);
9772
9773 switch (token->type)
9774 {
9775 case CPP_EQ:
9776 op = NOP_EXPR;
9777 break;
9778
9779 case CPP_MULT_EQ:
9780 op = MULT_EXPR;
9781 break;
9782
9783 case CPP_DIV_EQ:
9784 op = TRUNC_DIV_EXPR;
9785 break;
9786
9787 case CPP_MOD_EQ:
9788 op = TRUNC_MOD_EXPR;
9789 break;
9790
9791 case CPP_PLUS_EQ:
9792 op = PLUS_EXPR;
9793 break;
9794
9795 case CPP_MINUS_EQ:
9796 op = MINUS_EXPR;
9797 break;
9798
9799 case CPP_RSHIFT_EQ:
9800 op = RSHIFT_EXPR;
9801 break;
9802
9803 case CPP_LSHIFT_EQ:
9804 op = LSHIFT_EXPR;
9805 break;
9806
9807 case CPP_AND_EQ:
9808 op = BIT_AND_EXPR;
9809 break;
9810
9811 case CPP_XOR_EQ:
9812 op = BIT_XOR_EXPR;
9813 break;
9814
9815 case CPP_OR_EQ:
9816 op = BIT_IOR_EXPR;
9817 break;
9818
9819 default:
9820 /* Nothing else is an assignment operator. */
9821 op = ERROR_MARK;
9822 }
9823
9824 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9825 if (op != ERROR_MARK
9826 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9827 op = ERROR_MARK;
9828
9829 /* If it was an assignment operator, consume it. */
9830 if (op != ERROR_MARK)
9831 cp_lexer_consume_token (parser->lexer);
9832
9833 return op;
9834 }
9835
9836 /* Parse an expression.
9837
9838 expression:
9839 assignment-expression
9840 expression , assignment-expression
9841
9842 CAST_P is true if this expression is the target of a cast.
9843 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9844 except possibly parenthesized or on the RHS of a comma (N3276).
9845
9846 Returns a representation of the expression. */
9847
9848 static cp_expr
9849 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9850 bool cast_p, bool decltype_p)
9851 {
9852 cp_expr expression = NULL_TREE;
9853 location_t loc = UNKNOWN_LOCATION;
9854
9855 while (true)
9856 {
9857 cp_expr assignment_expression;
9858
9859 /* Parse the next assignment-expression. */
9860 assignment_expression
9861 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9862
9863 /* We don't create a temporary for a call that is the immediate operand
9864 of decltype or on the RHS of a comma. But when we see a comma, we
9865 need to create a temporary for a call on the LHS. */
9866 if (decltype_p && !processing_template_decl
9867 && TREE_CODE (assignment_expression) == CALL_EXPR
9868 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9869 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9870 assignment_expression
9871 = build_cplus_new (TREE_TYPE (assignment_expression),
9872 assignment_expression, tf_warning_or_error);
9873
9874 /* If this is the first assignment-expression, we can just
9875 save it away. */
9876 if (!expression)
9877 expression = assignment_expression;
9878 else
9879 {
9880 /* Create a location with caret at the comma, ranging
9881 from the start of the LHS to the end of the RHS. */
9882 loc = make_location (loc,
9883 expression.get_start (),
9884 assignment_expression.get_finish ());
9885 expression = build_x_compound_expr (loc, expression,
9886 assignment_expression,
9887 complain_flags (decltype_p));
9888 expression.set_location (loc);
9889 }
9890 /* If the next token is not a comma, or we're in a fold-expression, then
9891 we are done with the expression. */
9892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9893 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9894 break;
9895 /* Consume the `,'. */
9896 loc = cp_lexer_peek_token (parser->lexer)->location;
9897 cp_lexer_consume_token (parser->lexer);
9898 /* A comma operator cannot appear in a constant-expression. */
9899 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9900 expression = error_mark_node;
9901 }
9902
9903 return expression;
9904 }
9905
9906 /* Parse a constant-expression.
9907
9908 constant-expression:
9909 conditional-expression
9910
9911 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9912 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9913 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9914 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9915 only parse a conditional-expression, otherwise parse an
9916 assignment-expression. See below for rationale. */
9917
9918 static cp_expr
9919 cp_parser_constant_expression (cp_parser* parser,
9920 bool allow_non_constant_p,
9921 bool *non_constant_p,
9922 bool strict_p)
9923 {
9924 bool saved_integral_constant_expression_p;
9925 bool saved_allow_non_integral_constant_expression_p;
9926 bool saved_non_integral_constant_expression_p;
9927 cp_expr expression;
9928
9929 /* It might seem that we could simply parse the
9930 conditional-expression, and then check to see if it were
9931 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9932 one that the compiler can figure out is constant, possibly after
9933 doing some simplifications or optimizations. The standard has a
9934 precise definition of constant-expression, and we must honor
9935 that, even though it is somewhat more restrictive.
9936
9937 For example:
9938
9939 int i[(2, 3)];
9940
9941 is not a legal declaration, because `(2, 3)' is not a
9942 constant-expression. The `,' operator is forbidden in a
9943 constant-expression. However, GCC's constant-folding machinery
9944 will fold this operation to an INTEGER_CST for `3'. */
9945
9946 /* Save the old settings. */
9947 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9948 saved_allow_non_integral_constant_expression_p
9949 = parser->allow_non_integral_constant_expression_p;
9950 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9951 /* We are now parsing a constant-expression. */
9952 parser->integral_constant_expression_p = true;
9953 parser->allow_non_integral_constant_expression_p
9954 = (allow_non_constant_p || cxx_dialect >= cxx11);
9955 parser->non_integral_constant_expression_p = false;
9956 /* Although the grammar says "conditional-expression", when not STRICT_P,
9957 we parse an "assignment-expression", which also permits
9958 "throw-expression" and the use of assignment operators. In the case
9959 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9960 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9961 actually essential that we look for an assignment-expression.
9962 For example, cp_parser_initializer_clauses uses this function to
9963 determine whether a particular assignment-expression is in fact
9964 constant. */
9965 if (strict_p)
9966 {
9967 /* Parse the binary expressions (logical-or-expression). */
9968 expression = cp_parser_binary_expression (parser, false, false, false,
9969 PREC_NOT_OPERATOR, NULL);
9970 /* If the next token is a `?' then we're actually looking at
9971 a conditional-expression; otherwise we're done. */
9972 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9973 expression = cp_parser_question_colon_clause (parser, expression);
9974 }
9975 else
9976 expression = cp_parser_assignment_expression (parser);
9977 /* Restore the old settings. */
9978 parser->integral_constant_expression_p
9979 = saved_integral_constant_expression_p;
9980 parser->allow_non_integral_constant_expression_p
9981 = saved_allow_non_integral_constant_expression_p;
9982 if (cxx_dialect >= cxx11)
9983 {
9984 /* Require an rvalue constant expression here; that's what our
9985 callers expect. Reference constant expressions are handled
9986 separately in e.g. cp_parser_template_argument. */
9987 tree decay = expression;
9988 if (TREE_TYPE (expression)
9989 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9990 decay = build_address (expression);
9991 bool is_const = potential_rvalue_constant_expression (decay);
9992 parser->non_integral_constant_expression_p = !is_const;
9993 if (!is_const && !allow_non_constant_p)
9994 require_potential_rvalue_constant_expression (decay);
9995 }
9996 if (allow_non_constant_p)
9997 *non_constant_p = parser->non_integral_constant_expression_p;
9998 parser->non_integral_constant_expression_p
9999 = saved_non_integral_constant_expression_p;
10000
10001 return expression;
10002 }
10003
10004 /* Parse __builtin_offsetof.
10005
10006 offsetof-expression:
10007 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10008
10009 offsetof-member-designator:
10010 id-expression
10011 | offsetof-member-designator "." id-expression
10012 | offsetof-member-designator "[" expression "]"
10013 | offsetof-member-designator "->" id-expression */
10014
10015 static cp_expr
10016 cp_parser_builtin_offsetof (cp_parser *parser)
10017 {
10018 int save_ice_p, save_non_ice_p;
10019 tree type;
10020 cp_expr expr;
10021 cp_id_kind dummy;
10022 cp_token *token;
10023 location_t finish_loc;
10024
10025 /* We're about to accept non-integral-constant things, but will
10026 definitely yield an integral constant expression. Save and
10027 restore these values around our local parsing. */
10028 save_ice_p = parser->integral_constant_expression_p;
10029 save_non_ice_p = parser->non_integral_constant_expression_p;
10030
10031 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10032
10033 /* Consume the "__builtin_offsetof" token. */
10034 cp_lexer_consume_token (parser->lexer);
10035 /* Consume the opening `('. */
10036 matching_parens parens;
10037 parens.require_open (parser);
10038 /* Parse the type-id. */
10039 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10040 {
10041 const char *saved_message = parser->type_definition_forbidden_message;
10042 parser->type_definition_forbidden_message
10043 = G_("types may not be defined within __builtin_offsetof");
10044 type = cp_parser_type_id (parser);
10045 parser->type_definition_forbidden_message = saved_message;
10046 }
10047 /* Look for the `,'. */
10048 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10049 token = cp_lexer_peek_token (parser->lexer);
10050
10051 /* Build the (type *)null that begins the traditional offsetof macro. */
10052 tree object_ptr
10053 = build_static_cast (build_pointer_type (type), null_pointer_node,
10054 tf_warning_or_error);
10055
10056 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10057 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10058 true, &dummy, token->location);
10059 while (true)
10060 {
10061 token = cp_lexer_peek_token (parser->lexer);
10062 switch (token->type)
10063 {
10064 case CPP_OPEN_SQUARE:
10065 /* offsetof-member-designator "[" expression "]" */
10066 expr = cp_parser_postfix_open_square_expression (parser, expr,
10067 true, false);
10068 break;
10069
10070 case CPP_DEREF:
10071 /* offsetof-member-designator "->" identifier */
10072 expr = grok_array_decl (token->location, expr,
10073 integer_zero_node, false);
10074 /* FALLTHRU */
10075
10076 case CPP_DOT:
10077 /* offsetof-member-designator "." identifier */
10078 cp_lexer_consume_token (parser->lexer);
10079 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10080 expr, true, &dummy,
10081 token->location);
10082 break;
10083
10084 case CPP_CLOSE_PAREN:
10085 /* Consume the ")" token. */
10086 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10087 cp_lexer_consume_token (parser->lexer);
10088 goto success;
10089
10090 default:
10091 /* Error. We know the following require will fail, but
10092 that gives the proper error message. */
10093 parens.require_close (parser);
10094 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10095 expr = error_mark_node;
10096 goto failure;
10097 }
10098 }
10099
10100 success:
10101 /* Make a location of the form:
10102 __builtin_offsetof (struct s, f)
10103 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10104 with caret at the type-id, ranging from the start of the
10105 "_builtin_offsetof" token to the close paren. */
10106 loc = make_location (loc, start_loc, finish_loc);
10107 /* The result will be an INTEGER_CST, so we need to explicitly
10108 preserve the location. */
10109 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10110
10111 failure:
10112 parser->integral_constant_expression_p = save_ice_p;
10113 parser->non_integral_constant_expression_p = save_non_ice_p;
10114
10115 expr = expr.maybe_add_location_wrapper ();
10116 return expr;
10117 }
10118
10119 /* Parse a trait expression.
10120
10121 Returns a representation of the expression, the underlying type
10122 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10123
10124 static cp_expr
10125 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10126 {
10127 cp_trait_kind kind;
10128 tree type1, type2 = NULL_TREE;
10129 bool binary = false;
10130 bool variadic = false;
10131
10132 switch (keyword)
10133 {
10134 case RID_HAS_NOTHROW_ASSIGN:
10135 kind = CPTK_HAS_NOTHROW_ASSIGN;
10136 break;
10137 case RID_HAS_NOTHROW_CONSTRUCTOR:
10138 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10139 break;
10140 case RID_HAS_NOTHROW_COPY:
10141 kind = CPTK_HAS_NOTHROW_COPY;
10142 break;
10143 case RID_HAS_TRIVIAL_ASSIGN:
10144 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10145 break;
10146 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10147 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10148 break;
10149 case RID_HAS_TRIVIAL_COPY:
10150 kind = CPTK_HAS_TRIVIAL_COPY;
10151 break;
10152 case RID_HAS_TRIVIAL_DESTRUCTOR:
10153 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10154 break;
10155 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10156 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10157 break;
10158 case RID_HAS_VIRTUAL_DESTRUCTOR:
10159 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10160 break;
10161 case RID_IS_ABSTRACT:
10162 kind = CPTK_IS_ABSTRACT;
10163 break;
10164 case RID_IS_AGGREGATE:
10165 kind = CPTK_IS_AGGREGATE;
10166 break;
10167 case RID_IS_BASE_OF:
10168 kind = CPTK_IS_BASE_OF;
10169 binary = true;
10170 break;
10171 case RID_IS_CLASS:
10172 kind = CPTK_IS_CLASS;
10173 break;
10174 case RID_IS_EMPTY:
10175 kind = CPTK_IS_EMPTY;
10176 break;
10177 case RID_IS_ENUM:
10178 kind = CPTK_IS_ENUM;
10179 break;
10180 case RID_IS_FINAL:
10181 kind = CPTK_IS_FINAL;
10182 break;
10183 case RID_IS_LITERAL_TYPE:
10184 kind = CPTK_IS_LITERAL_TYPE;
10185 break;
10186 case RID_IS_POD:
10187 kind = CPTK_IS_POD;
10188 break;
10189 case RID_IS_POLYMORPHIC:
10190 kind = CPTK_IS_POLYMORPHIC;
10191 break;
10192 case RID_IS_SAME_AS:
10193 kind = CPTK_IS_SAME_AS;
10194 binary = true;
10195 break;
10196 case RID_IS_STD_LAYOUT:
10197 kind = CPTK_IS_STD_LAYOUT;
10198 break;
10199 case RID_IS_TRIVIAL:
10200 kind = CPTK_IS_TRIVIAL;
10201 break;
10202 case RID_IS_TRIVIALLY_ASSIGNABLE:
10203 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10204 binary = true;
10205 break;
10206 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10207 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10208 variadic = true;
10209 break;
10210 case RID_IS_TRIVIALLY_COPYABLE:
10211 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10212 break;
10213 case RID_IS_UNION:
10214 kind = CPTK_IS_UNION;
10215 break;
10216 case RID_UNDERLYING_TYPE:
10217 kind = CPTK_UNDERLYING_TYPE;
10218 break;
10219 case RID_BASES:
10220 kind = CPTK_BASES;
10221 break;
10222 case RID_DIRECT_BASES:
10223 kind = CPTK_DIRECT_BASES;
10224 break;
10225 case RID_IS_ASSIGNABLE:
10226 kind = CPTK_IS_ASSIGNABLE;
10227 binary = true;
10228 break;
10229 case RID_IS_CONSTRUCTIBLE:
10230 kind = CPTK_IS_CONSTRUCTIBLE;
10231 variadic = true;
10232 break;
10233 default:
10234 gcc_unreachable ();
10235 }
10236
10237 /* Get location of initial token. */
10238 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10239
10240 /* Consume the token. */
10241 cp_lexer_consume_token (parser->lexer);
10242
10243 matching_parens parens;
10244 parens.require_open (parser);
10245
10246 {
10247 type_id_in_expr_sentinel s (parser);
10248 type1 = cp_parser_type_id (parser);
10249 }
10250
10251 if (type1 == error_mark_node)
10252 return error_mark_node;
10253
10254 if (binary)
10255 {
10256 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10257
10258 {
10259 type_id_in_expr_sentinel s (parser);
10260 type2 = cp_parser_type_id (parser);
10261 }
10262
10263 if (type2 == error_mark_node)
10264 return error_mark_node;
10265 }
10266 else if (variadic)
10267 {
10268 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10269 {
10270 cp_lexer_consume_token (parser->lexer);
10271 tree elt = cp_parser_type_id (parser);
10272 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10273 {
10274 cp_lexer_consume_token (parser->lexer);
10275 elt = make_pack_expansion (elt);
10276 }
10277 if (elt == error_mark_node)
10278 return error_mark_node;
10279 type2 = tree_cons (NULL_TREE, elt, type2);
10280 }
10281 }
10282
10283 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10284 parens.require_close (parser);
10285
10286 /* Construct a location of the form:
10287 __is_trivially_copyable(_Tp)
10288 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10289 with start == caret, finishing at the close-paren. */
10290 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10291
10292 /* Complete the trait expression, which may mean either processing
10293 the trait expr now or saving it for template instantiation. */
10294 switch (kind)
10295 {
10296 case CPTK_UNDERLYING_TYPE:
10297 return cp_expr (finish_underlying_type (type1), trait_loc);
10298 case CPTK_BASES:
10299 return cp_expr (finish_bases (type1, false), trait_loc);
10300 case CPTK_DIRECT_BASES:
10301 return cp_expr (finish_bases (type1, true), trait_loc);
10302 default:
10303 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10304 }
10305 }
10306
10307 /* Parse a lambda expression.
10308
10309 lambda-expression:
10310 lambda-introducer lambda-declarator [opt] compound-statement
10311
10312 Returns a representation of the expression. */
10313
10314 static cp_expr
10315 cp_parser_lambda_expression (cp_parser* parser)
10316 {
10317 tree lambda_expr = build_lambda_expr ();
10318 tree type;
10319 bool ok = true;
10320 cp_token *token = cp_lexer_peek_token (parser->lexer);
10321 cp_token_position start = 0;
10322
10323 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10324
10325 if (cxx_dialect >= cxx2a)
10326 /* C++20 allows lambdas in unevaluated context. */;
10327 else if (cp_unevaluated_operand)
10328 {
10329 if (!token->error_reported)
10330 {
10331 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10332 "lambda-expression in unevaluated context"
10333 " only available with -std=c++2a or -std=gnu++2a");
10334 token->error_reported = true;
10335 }
10336 ok = false;
10337 }
10338 else if (parser->in_template_argument_list_p)
10339 {
10340 if (!token->error_reported)
10341 {
10342 error_at (token->location, "lambda-expression in template-argument"
10343 " only available with -std=c++2a or -std=gnu++2a");
10344 token->error_reported = true;
10345 }
10346 ok = false;
10347 }
10348
10349 /* We may be in the middle of deferred access check. Disable
10350 it now. */
10351 push_deferring_access_checks (dk_no_deferred);
10352
10353 cp_parser_lambda_introducer (parser, lambda_expr);
10354 if (cp_parser_error_occurred (parser))
10355 return error_mark_node;
10356
10357 type = begin_lambda_type (lambda_expr);
10358 if (type == error_mark_node)
10359 return error_mark_node;
10360
10361 record_lambda_scope (lambda_expr);
10362
10363 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10364 determine_visibility (TYPE_NAME (type));
10365
10366 /* Now that we've started the type, add the capture fields for any
10367 explicit captures. */
10368 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10369
10370 {
10371 /* Inside the class, surrounding template-parameter-lists do not apply. */
10372 unsigned int saved_num_template_parameter_lists
10373 = parser->num_template_parameter_lists;
10374 unsigned char in_statement = parser->in_statement;
10375 bool in_switch_statement_p = parser->in_switch_statement_p;
10376 bool fully_implicit_function_template_p
10377 = parser->fully_implicit_function_template_p;
10378 tree implicit_template_parms = parser->implicit_template_parms;
10379 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10380 bool auto_is_implicit_function_template_parm_p
10381 = parser->auto_is_implicit_function_template_parm_p;
10382
10383 parser->num_template_parameter_lists = 0;
10384 parser->in_statement = 0;
10385 parser->in_switch_statement_p = false;
10386 parser->fully_implicit_function_template_p = false;
10387 parser->implicit_template_parms = 0;
10388 parser->implicit_template_scope = 0;
10389 parser->auto_is_implicit_function_template_parm_p = false;
10390
10391 /* By virtue of defining a local class, a lambda expression has access to
10392 the private variables of enclosing classes. */
10393
10394 if (cp_parser_start_tentative_firewall (parser))
10395 start = token;
10396
10397 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10398
10399 if (ok && cp_parser_error_occurred (parser))
10400 ok = false;
10401
10402 if (ok)
10403 {
10404 cp_parser_lambda_body (parser, lambda_expr);
10405 }
10406 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10407 {
10408 if (cp_parser_skip_to_closing_brace (parser))
10409 cp_lexer_consume_token (parser->lexer);
10410 }
10411
10412 /* The capture list was built up in reverse order; fix that now. */
10413 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10414 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10415
10416 if (ok)
10417 maybe_add_lambda_conv_op (type);
10418
10419 type = finish_struct (type, /*attributes=*/NULL_TREE);
10420
10421 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10422 parser->in_statement = in_statement;
10423 parser->in_switch_statement_p = in_switch_statement_p;
10424 parser->fully_implicit_function_template_p
10425 = fully_implicit_function_template_p;
10426 parser->implicit_template_parms = implicit_template_parms;
10427 parser->implicit_template_scope = implicit_template_scope;
10428 parser->auto_is_implicit_function_template_parm_p
10429 = auto_is_implicit_function_template_parm_p;
10430 }
10431
10432 /* This field is only used during parsing of the lambda. */
10433 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10434
10435 /* This lambda shouldn't have any proxies left at this point. */
10436 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10437 /* And now that we're done, push proxies for an enclosing lambda. */
10438 insert_pending_capture_proxies ();
10439
10440 /* Update the lambda expression to a range. */
10441 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10442 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10443 token->location,
10444 end_tok->location);
10445
10446 if (ok)
10447 lambda_expr = build_lambda_object (lambda_expr);
10448 else
10449 lambda_expr = error_mark_node;
10450
10451 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10452
10453 pop_deferring_access_checks ();
10454
10455 return lambda_expr;
10456 }
10457
10458 /* Parse the beginning of a lambda expression.
10459
10460 lambda-introducer:
10461 [ lambda-capture [opt] ]
10462
10463 LAMBDA_EXPR is the current representation of the lambda expression. */
10464
10465 static void
10466 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10467 {
10468 /* Need commas after the first capture. */
10469 bool first = true;
10470
10471 /* Eat the leading `['. */
10472 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10473
10474 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10475 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10476 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10477 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10478 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10479 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10480
10481 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10482 {
10483 cp_lexer_consume_token (parser->lexer);
10484 first = false;
10485
10486 if (!(at_function_scope_p () || parsing_nsdmi ()))
10487 error ("non-local lambda expression cannot have a capture-default");
10488 }
10489
10490 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10491 {
10492 cp_token* capture_token;
10493 tree capture_id;
10494 tree capture_init_expr;
10495 cp_id_kind idk = CP_ID_KIND_NONE;
10496 bool explicit_init_p = false;
10497
10498 enum capture_kind_type
10499 {
10500 BY_COPY,
10501 BY_REFERENCE
10502 };
10503 enum capture_kind_type capture_kind = BY_COPY;
10504
10505 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10506 {
10507 error ("expected end of capture-list");
10508 return;
10509 }
10510
10511 if (first)
10512 first = false;
10513 else
10514 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10515
10516 /* Possibly capture `this'. */
10517 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10518 {
10519 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10520 if (cxx_dialect < cxx2a
10521 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10522 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10523 "with by-copy capture default");
10524 cp_lexer_consume_token (parser->lexer);
10525 add_capture (lambda_expr,
10526 /*id=*/this_identifier,
10527 /*initializer=*/finish_this_expr (),
10528 /*by_reference_p=*/true,
10529 explicit_init_p);
10530 continue;
10531 }
10532
10533 /* Possibly capture `*this'. */
10534 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10535 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10536 {
10537 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10538 if (cxx_dialect < cxx17)
10539 pedwarn (loc, 0, "%<*this%> capture only available with "
10540 "-std=c++17 or -std=gnu++17");
10541 cp_lexer_consume_token (parser->lexer);
10542 cp_lexer_consume_token (parser->lexer);
10543 add_capture (lambda_expr,
10544 /*id=*/this_identifier,
10545 /*initializer=*/finish_this_expr (),
10546 /*by_reference_p=*/false,
10547 explicit_init_p);
10548 continue;
10549 }
10550
10551 bool init_pack_expansion = false;
10552 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10553 {
10554 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10555 if (cxx_dialect < cxx2a)
10556 pedwarn (loc, 0, "pack init-capture only available with "
10557 "-std=c++2a or -std=gnu++2a");
10558 cp_lexer_consume_token (parser->lexer);
10559 init_pack_expansion = true;
10560 }
10561
10562 /* Remember whether we want to capture as a reference or not. */
10563 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10564 {
10565 capture_kind = BY_REFERENCE;
10566 cp_lexer_consume_token (parser->lexer);
10567 }
10568
10569 /* Get the identifier. */
10570 capture_token = cp_lexer_peek_token (parser->lexer);
10571 capture_id = cp_parser_identifier (parser);
10572
10573 if (capture_id == error_mark_node)
10574 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10575 delimiters, but I modified this to stop on unnested ']' as well. It
10576 was already changed to stop on unnested '}', so the
10577 "closing_parenthesis" name is no more misleading with my change. */
10578 {
10579 cp_parser_skip_to_closing_parenthesis (parser,
10580 /*recovering=*/true,
10581 /*or_comma=*/true,
10582 /*consume_paren=*/true);
10583 break;
10584 }
10585
10586 /* Find the initializer for this capture. */
10587 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10588 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10589 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10590 {
10591 bool direct, non_constant;
10592 /* An explicit initializer exists. */
10593 if (cxx_dialect < cxx14)
10594 pedwarn (input_location, 0,
10595 "lambda capture initializers "
10596 "only available with -std=c++14 or -std=gnu++14");
10597 capture_init_expr = cp_parser_initializer (parser, &direct,
10598 &non_constant, true);
10599 explicit_init_p = true;
10600 if (capture_init_expr == NULL_TREE)
10601 {
10602 error ("empty initializer for lambda init-capture");
10603 capture_init_expr = error_mark_node;
10604 }
10605 if (init_pack_expansion)
10606 capture_init_expr = make_pack_expansion (capture_init_expr);
10607 }
10608 else
10609 {
10610 const char* error_msg;
10611
10612 /* Turn the identifier into an id-expression. */
10613 capture_init_expr
10614 = cp_parser_lookup_name_simple (parser, capture_id,
10615 capture_token->location);
10616
10617 if (capture_init_expr == error_mark_node)
10618 {
10619 unqualified_name_lookup_error (capture_id);
10620 continue;
10621 }
10622 else if (!VAR_P (capture_init_expr)
10623 && TREE_CODE (capture_init_expr) != PARM_DECL)
10624 {
10625 error_at (capture_token->location,
10626 "capture of non-variable %qE",
10627 capture_init_expr);
10628 if (DECL_P (capture_init_expr))
10629 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10630 "%q#D declared here", capture_init_expr);
10631 continue;
10632 }
10633 if (VAR_P (capture_init_expr)
10634 && decl_storage_duration (capture_init_expr) != dk_auto)
10635 {
10636 if (pedwarn (capture_token->location, 0, "capture of variable "
10637 "%qD with non-automatic storage duration",
10638 capture_init_expr))
10639 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10640 "%q#D declared here", capture_init_expr);
10641 continue;
10642 }
10643
10644 capture_init_expr
10645 = finish_id_expression
10646 (capture_id,
10647 capture_init_expr,
10648 parser->scope,
10649 &idk,
10650 /*integral_constant_expression_p=*/false,
10651 /*allow_non_integral_constant_expression_p=*/false,
10652 /*non_integral_constant_expression_p=*/NULL,
10653 /*template_p=*/false,
10654 /*done=*/true,
10655 /*address_p=*/false,
10656 /*template_arg_p=*/false,
10657 &error_msg,
10658 capture_token->location);
10659
10660 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10661 {
10662 cp_lexer_consume_token (parser->lexer);
10663 capture_init_expr = make_pack_expansion (capture_init_expr);
10664 }
10665 }
10666
10667 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10668 && !explicit_init_p)
10669 {
10670 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10671 && capture_kind == BY_COPY)
10672 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10673 "of %qD redundant with by-copy capture default",
10674 capture_id);
10675 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10676 && capture_kind == BY_REFERENCE)
10677 pedwarn (capture_token->location, 0, "explicit by-reference "
10678 "capture of %qD redundant with by-reference capture "
10679 "default", capture_id);
10680 }
10681
10682 add_capture (lambda_expr,
10683 capture_id,
10684 capture_init_expr,
10685 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10686 explicit_init_p);
10687
10688 /* If there is any qualification still in effect, clear it
10689 now; we will be starting fresh with the next capture. */
10690 parser->scope = NULL_TREE;
10691 parser->qualifying_scope = NULL_TREE;
10692 parser->object_scope = NULL_TREE;
10693 }
10694
10695 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10696 }
10697
10698 /* Parse the (optional) middle of a lambda expression.
10699
10700 lambda-declarator:
10701 < template-parameter-list [opt] >
10702 ( parameter-declaration-clause [opt] )
10703 attribute-specifier [opt]
10704 decl-specifier-seq [opt]
10705 exception-specification [opt]
10706 lambda-return-type-clause [opt]
10707
10708 LAMBDA_EXPR is the current representation of the lambda expression. */
10709
10710 static bool
10711 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10712 {
10713 /* 5.1.1.4 of the standard says:
10714 If a lambda-expression does not include a lambda-declarator, it is as if
10715 the lambda-declarator were ().
10716 This means an empty parameter list, no attributes, and no exception
10717 specification. */
10718 tree param_list = void_list_node;
10719 tree attributes = NULL_TREE;
10720 tree exception_spec = NULL_TREE;
10721 tree template_param_list = NULL_TREE;
10722 tree tx_qual = NULL_TREE;
10723 tree return_type = NULL_TREE;
10724 cp_decl_specifier_seq lambda_specs;
10725 clear_decl_specs (&lambda_specs);
10726
10727 /* The template-parameter-list is optional, but must begin with
10728 an opening angle if present. */
10729 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10730 {
10731 if (cxx_dialect < cxx14)
10732 pedwarn (parser->lexer->next_token->location, 0,
10733 "lambda templates are only available with "
10734 "-std=c++14 or -std=gnu++14");
10735 else if (cxx_dialect < cxx2a)
10736 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10737 "lambda templates are only available with "
10738 "-std=c++2a or -std=gnu++2a");
10739
10740 cp_lexer_consume_token (parser->lexer);
10741
10742 template_param_list = cp_parser_template_parameter_list (parser);
10743
10744 cp_parser_skip_to_end_of_template_parameter_list (parser);
10745
10746 /* We just processed one more parameter list. */
10747 ++parser->num_template_parameter_lists;
10748 }
10749
10750 /* The parameter-declaration-clause is optional (unless
10751 template-parameter-list was given), but must begin with an
10752 opening parenthesis if present. */
10753 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10754 {
10755 matching_parens parens;
10756 parens.consume_open (parser);
10757
10758 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10759
10760 /* Parse parameters. */
10761 param_list
10762 = cp_parser_parameter_declaration_clause
10763 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10764
10765 /* Default arguments shall not be specified in the
10766 parameter-declaration-clause of a lambda-declarator. */
10767 if (cxx_dialect < cxx14)
10768 for (tree t = param_list; t; t = TREE_CHAIN (t))
10769 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10770 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10771 "default argument specified for lambda parameter");
10772
10773 parens.require_close (parser);
10774
10775 /* In the decl-specifier-seq of the lambda-declarator, each
10776 decl-specifier shall either be mutable or constexpr. */
10777 int declares_class_or_enum;
10778 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10779 cp_parser_decl_specifier_seq (parser,
10780 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10781 &lambda_specs, &declares_class_or_enum);
10782 if (lambda_specs.storage_class == sc_mutable)
10783 {
10784 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10785 if (lambda_specs.conflicting_specifiers_p)
10786 error_at (lambda_specs.locations[ds_storage_class],
10787 "duplicate %<mutable%>");
10788 }
10789
10790 tx_qual = cp_parser_tx_qualifier_opt (parser);
10791
10792 /* Parse optional exception specification. */
10793 exception_spec = cp_parser_exception_specification_opt (parser);
10794
10795 attributes = cp_parser_std_attribute_spec_seq (parser);
10796
10797 /* Parse optional trailing return type. */
10798 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10799 {
10800 cp_lexer_consume_token (parser->lexer);
10801 return_type = cp_parser_trailing_type_id (parser);
10802 }
10803
10804 /* The function parameters must be in scope all the way until after the
10805 trailing-return-type in case of decltype. */
10806 pop_bindings_and_leave_scope ();
10807 }
10808 else if (template_param_list != NULL_TREE) // generate diagnostic
10809 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10810
10811 /* Create the function call operator.
10812
10813 Messing with declarators like this is no uglier than building up the
10814 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10815 other code. */
10816 {
10817 cp_decl_specifier_seq return_type_specs;
10818 cp_declarator* declarator;
10819 tree fco;
10820 int quals;
10821 void *p;
10822
10823 clear_decl_specs (&return_type_specs);
10824 return_type_specs.type = make_auto ();
10825
10826 if (lambda_specs.locations[ds_constexpr])
10827 {
10828 if (cxx_dialect >= cxx17)
10829 return_type_specs.locations[ds_constexpr]
10830 = lambda_specs.locations[ds_constexpr];
10831 else
10832 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10833 "lambda only available with -std=c++17 or -std=gnu++17");
10834 }
10835
10836 p = obstack_alloc (&declarator_obstack, 0);
10837
10838 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10839 LAMBDA_EXPR_LOCATION (lambda_expr));
10840
10841 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10842 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10843 declarator = make_call_declarator (declarator, param_list, quals,
10844 VIRT_SPEC_UNSPECIFIED,
10845 REF_QUAL_NONE,
10846 tx_qual,
10847 exception_spec,
10848 return_type,
10849 /*requires_clause*/NULL_TREE);
10850 declarator->std_attributes = attributes;
10851
10852 fco = grokmethod (&return_type_specs,
10853 declarator,
10854 NULL_TREE);
10855 if (fco != error_mark_node)
10856 {
10857 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10858 DECL_ARTIFICIAL (fco) = 1;
10859 /* Give the object parameter a different name. */
10860 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10861 DECL_LAMBDA_FUNCTION (fco) = 1;
10862 }
10863 if (template_param_list)
10864 {
10865 fco = finish_member_template_decl (fco);
10866 finish_template_decl (template_param_list);
10867 --parser->num_template_parameter_lists;
10868 }
10869 else if (parser->fully_implicit_function_template_p)
10870 fco = finish_fully_implicit_template (parser, fco);
10871
10872 finish_member_declaration (fco);
10873
10874 obstack_free (&declarator_obstack, p);
10875
10876 return (fco != error_mark_node);
10877 }
10878 }
10879
10880 /* Parse the body of a lambda expression, which is simply
10881
10882 compound-statement
10883
10884 but which requires special handling.
10885 LAMBDA_EXPR is the current representation of the lambda expression. */
10886
10887 static void
10888 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10889 {
10890 bool nested = (current_function_decl != NULL_TREE);
10891 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10892 bool in_function_body = parser->in_function_body;
10893
10894 /* The body of a lambda-expression is not a subexpression of the enclosing
10895 expression. */
10896 cp_evaluated ev;
10897
10898 if (nested)
10899 push_function_context ();
10900 else
10901 /* Still increment function_depth so that we don't GC in the
10902 middle of an expression. */
10903 ++function_depth;
10904
10905 vec<tree> omp_privatization_save;
10906 save_omp_privatization_clauses (omp_privatization_save);
10907 /* Clear this in case we're in the middle of a default argument. */
10908 parser->local_variables_forbidden_p = false;
10909 parser->in_function_body = true;
10910
10911 {
10912 local_specialization_stack s (lss_copy);
10913 tree fco = lambda_function (lambda_expr);
10914 tree body = start_lambda_function (fco, lambda_expr);
10915 matching_braces braces;
10916
10917 if (braces.require_open (parser))
10918 {
10919 tree compound_stmt = begin_compound_stmt (0);
10920
10921 /* Originally C++11 required us to peek for 'return expr'; and
10922 process it specially here to deduce the return type. N3638
10923 removed the need for that. */
10924
10925 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10926 cp_parser_label_declaration (parser);
10927 cp_parser_statement_seq_opt (parser, NULL_TREE);
10928 braces.require_close (parser);
10929
10930 finish_compound_stmt (compound_stmt);
10931 }
10932
10933 finish_lambda_function (body);
10934 }
10935
10936 restore_omp_privatization_clauses (omp_privatization_save);
10937 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10938 parser->in_function_body = in_function_body;
10939 if (nested)
10940 pop_function_context();
10941 else
10942 --function_depth;
10943 }
10944
10945 /* Statements [gram.stmt.stmt] */
10946
10947 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10948
10949 static void
10950 add_debug_begin_stmt (location_t loc)
10951 {
10952 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
10953 return;
10954 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
10955 /* A concept is never expanded normally. */
10956 return;
10957
10958 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
10959 SET_EXPR_LOCATION (stmt, loc);
10960 add_stmt (stmt);
10961 }
10962
10963 /* Parse a statement.
10964
10965 statement:
10966 labeled-statement
10967 expression-statement
10968 compound-statement
10969 selection-statement
10970 iteration-statement
10971 jump-statement
10972 declaration-statement
10973 try-block
10974
10975 C++11:
10976
10977 statement:
10978 labeled-statement
10979 attribute-specifier-seq (opt) expression-statement
10980 attribute-specifier-seq (opt) compound-statement
10981 attribute-specifier-seq (opt) selection-statement
10982 attribute-specifier-seq (opt) iteration-statement
10983 attribute-specifier-seq (opt) jump-statement
10984 declaration-statement
10985 attribute-specifier-seq (opt) try-block
10986
10987 init-statement:
10988 expression-statement
10989 simple-declaration
10990
10991 TM Extension:
10992
10993 statement:
10994 atomic-statement
10995
10996 IN_COMPOUND is true when the statement is nested inside a
10997 cp_parser_compound_statement; this matters for certain pragmas.
10998
10999 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11000 is a (possibly labeled) if statement which is not enclosed in braces
11001 and has an else clause. This is used to implement -Wparentheses.
11002
11003 CHAIN is a vector of if-else-if conditions. */
11004
11005 static void
11006 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11007 bool in_compound, bool *if_p, vec<tree> *chain,
11008 location_t *loc_after_labels)
11009 {
11010 tree statement, std_attrs = NULL_TREE;
11011 cp_token *token;
11012 location_t statement_location, attrs_location;
11013
11014 restart:
11015 if (if_p != NULL)
11016 *if_p = false;
11017 /* There is no statement yet. */
11018 statement = NULL_TREE;
11019
11020 saved_token_sentinel saved_tokens (parser->lexer);
11021 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
11022 if (c_dialect_objc ())
11023 /* In obj-c++, seeing '[[' might be the either the beginning of
11024 c++11 attributes, or a nested objc-message-expression. So
11025 let's parse the c++11 attributes tentatively. */
11026 cp_parser_parse_tentatively (parser);
11027 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11028 if (c_dialect_objc ())
11029 {
11030 if (!cp_parser_parse_definitely (parser))
11031 std_attrs = NULL_TREE;
11032 }
11033
11034 /* Peek at the next token. */
11035 token = cp_lexer_peek_token (parser->lexer);
11036 /* Remember the location of the first token in the statement. */
11037 cp_token *statement_token = token;
11038 statement_location = token->location;
11039 add_debug_begin_stmt (statement_location);
11040 /* If this is a keyword, then that will often determine what kind of
11041 statement we have. */
11042 if (token->type == CPP_KEYWORD)
11043 {
11044 enum rid keyword = token->keyword;
11045
11046 switch (keyword)
11047 {
11048 case RID_CASE:
11049 case RID_DEFAULT:
11050 /* Looks like a labeled-statement with a case label.
11051 Parse the label, and then use tail recursion to parse
11052 the statement. */
11053 cp_parser_label_for_labeled_statement (parser, std_attrs);
11054 in_compound = false;
11055 goto restart;
11056
11057 case RID_IF:
11058 case RID_SWITCH:
11059 std_attrs = process_stmt_hotness_attribute (std_attrs);
11060 statement = cp_parser_selection_statement (parser, if_p, chain);
11061 break;
11062
11063 case RID_WHILE:
11064 case RID_DO:
11065 case RID_FOR:
11066 std_attrs = process_stmt_hotness_attribute (std_attrs);
11067 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11068 break;
11069
11070 case RID_BREAK:
11071 case RID_CONTINUE:
11072 case RID_RETURN:
11073 case RID_GOTO:
11074 std_attrs = process_stmt_hotness_attribute (std_attrs);
11075 statement = cp_parser_jump_statement (parser);
11076 break;
11077
11078 /* Objective-C++ exception-handling constructs. */
11079 case RID_AT_TRY:
11080 case RID_AT_CATCH:
11081 case RID_AT_FINALLY:
11082 case RID_AT_SYNCHRONIZED:
11083 case RID_AT_THROW:
11084 std_attrs = process_stmt_hotness_attribute (std_attrs);
11085 statement = cp_parser_objc_statement (parser);
11086 break;
11087
11088 case RID_TRY:
11089 std_attrs = process_stmt_hotness_attribute (std_attrs);
11090 statement = cp_parser_try_block (parser);
11091 break;
11092
11093 case RID_NAMESPACE:
11094 /* This must be a namespace alias definition. */
11095 if (std_attrs != NULL_TREE)
11096 {
11097 /* Attributes should be parsed as part of the the
11098 declaration, so let's un-parse them. */
11099 saved_tokens.rollback();
11100 std_attrs = NULL_TREE;
11101 }
11102 cp_parser_declaration_statement (parser);
11103 return;
11104
11105 case RID_TRANSACTION_ATOMIC:
11106 case RID_TRANSACTION_RELAXED:
11107 case RID_SYNCHRONIZED:
11108 case RID_ATOMIC_NOEXCEPT:
11109 case RID_ATOMIC_CANCEL:
11110 std_attrs = process_stmt_hotness_attribute (std_attrs);
11111 statement = cp_parser_transaction (parser, token);
11112 break;
11113 case RID_TRANSACTION_CANCEL:
11114 std_attrs = process_stmt_hotness_attribute (std_attrs);
11115 statement = cp_parser_transaction_cancel (parser);
11116 break;
11117
11118 default:
11119 /* It might be a keyword like `int' that can start a
11120 declaration-statement. */
11121 break;
11122 }
11123 }
11124 else if (token->type == CPP_NAME)
11125 {
11126 /* If the next token is a `:', then we are looking at a
11127 labeled-statement. */
11128 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11129 if (token->type == CPP_COLON)
11130 {
11131 /* Looks like a labeled-statement with an ordinary label.
11132 Parse the label, and then use tail recursion to parse
11133 the statement. */
11134
11135 cp_parser_label_for_labeled_statement (parser, std_attrs);
11136 in_compound = false;
11137 goto restart;
11138 }
11139 }
11140 /* Anything that starts with a `{' must be a compound-statement. */
11141 else if (token->type == CPP_OPEN_BRACE)
11142 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11143 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11144 a statement all its own. */
11145 else if (token->type == CPP_PRAGMA)
11146 {
11147 /* Only certain OpenMP pragmas are attached to statements, and thus
11148 are considered statements themselves. All others are not. In
11149 the context of a compound, accept the pragma as a "statement" and
11150 return so that we can check for a close brace. Otherwise we
11151 require a real statement and must go back and read one. */
11152 if (in_compound)
11153 cp_parser_pragma (parser, pragma_compound, if_p);
11154 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11155 goto restart;
11156 return;
11157 }
11158 else if (token->type == CPP_EOF)
11159 {
11160 cp_parser_error (parser, "expected statement");
11161 return;
11162 }
11163
11164 /* Everything else must be a declaration-statement or an
11165 expression-statement. Try for the declaration-statement
11166 first, unless we are looking at a `;', in which case we know that
11167 we have an expression-statement. */
11168 if (!statement)
11169 {
11170 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11171 {
11172 if (std_attrs != NULL_TREE)
11173 /* Attributes should be parsed as part of the declaration,
11174 so let's un-parse them. */
11175 saved_tokens.rollback();
11176
11177 cp_parser_parse_tentatively (parser);
11178 /* Try to parse the declaration-statement. */
11179 cp_parser_declaration_statement (parser);
11180 /* If that worked, we're done. */
11181 if (cp_parser_parse_definitely (parser))
11182 return;
11183 /* It didn't work, restore the post-attribute position. */
11184 if (std_attrs)
11185 cp_lexer_set_token_position (parser->lexer, statement_token);
11186 }
11187 /* All preceding labels have been parsed at this point. */
11188 if (loc_after_labels != NULL)
11189 *loc_after_labels = statement_location;
11190
11191 std_attrs = process_stmt_hotness_attribute (std_attrs);
11192
11193 /* Look for an expression-statement instead. */
11194 statement = cp_parser_expression_statement (parser, in_statement_expr);
11195
11196 /* Handle [[fallthrough]];. */
11197 if (attribute_fallthrough_p (std_attrs))
11198 {
11199 /* The next token after the fallthrough attribute is ';'. */
11200 if (statement == NULL_TREE)
11201 {
11202 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11203 statement = build_call_expr_internal_loc (statement_location,
11204 IFN_FALLTHROUGH,
11205 void_type_node, 0);
11206 finish_expr_stmt (statement);
11207 }
11208 else
11209 warning_at (statement_location, OPT_Wattributes,
11210 "%<fallthrough%> attribute not followed by %<;%>");
11211 std_attrs = NULL_TREE;
11212 }
11213 }
11214
11215 /* Set the line number for the statement. */
11216 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11217 SET_EXPR_LOCATION (statement, statement_location);
11218
11219 /* Allow "[[fallthrough]];", but warn otherwise. */
11220 if (std_attrs != NULL_TREE)
11221 warning_at (attrs_location,
11222 OPT_Wattributes,
11223 "attributes at the beginning of statement are ignored");
11224 }
11225
11226 /* Append ATTR to attribute list ATTRS. */
11227
11228 static tree
11229 attr_chainon (tree attrs, tree attr)
11230 {
11231 if (attrs == error_mark_node)
11232 return error_mark_node;
11233 if (attr == error_mark_node)
11234 return error_mark_node;
11235 return chainon (attrs, attr);
11236 }
11237
11238 /* Parse the label for a labeled-statement, i.e.
11239
11240 identifier :
11241 case constant-expression :
11242 default :
11243
11244 GNU Extension:
11245 case constant-expression ... constant-expression : statement
11246
11247 When a label is parsed without errors, the label is added to the
11248 parse tree by the finish_* functions, so this function doesn't
11249 have to return the label. */
11250
11251 static void
11252 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11253 {
11254 cp_token *token;
11255 tree label = NULL_TREE;
11256 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11257
11258 /* The next token should be an identifier. */
11259 token = cp_lexer_peek_token (parser->lexer);
11260 if (token->type != CPP_NAME
11261 && token->type != CPP_KEYWORD)
11262 {
11263 cp_parser_error (parser, "expected labeled-statement");
11264 return;
11265 }
11266
11267 /* Remember whether this case or a user-defined label is allowed to fall
11268 through to. */
11269 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11270
11271 parser->colon_corrects_to_scope_p = false;
11272 switch (token->keyword)
11273 {
11274 case RID_CASE:
11275 {
11276 tree expr, expr_hi;
11277 cp_token *ellipsis;
11278
11279 /* Consume the `case' token. */
11280 cp_lexer_consume_token (parser->lexer);
11281 /* Parse the constant-expression. */
11282 expr = cp_parser_constant_expression (parser);
11283 if (check_for_bare_parameter_packs (expr))
11284 expr = error_mark_node;
11285
11286 ellipsis = cp_lexer_peek_token (parser->lexer);
11287 if (ellipsis->type == CPP_ELLIPSIS)
11288 {
11289 /* Consume the `...' token. */
11290 cp_lexer_consume_token (parser->lexer);
11291 expr_hi = cp_parser_constant_expression (parser);
11292 if (check_for_bare_parameter_packs (expr_hi))
11293 expr_hi = error_mark_node;
11294
11295 /* We don't need to emit warnings here, as the common code
11296 will do this for us. */
11297 }
11298 else
11299 expr_hi = NULL_TREE;
11300
11301 if (parser->in_switch_statement_p)
11302 {
11303 tree l = finish_case_label (token->location, expr, expr_hi);
11304 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11305 {
11306 label = CASE_LABEL (l);
11307 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11308 }
11309 }
11310 else
11311 error_at (token->location,
11312 "case label %qE not within a switch statement",
11313 expr);
11314 }
11315 break;
11316
11317 case RID_DEFAULT:
11318 /* Consume the `default' token. */
11319 cp_lexer_consume_token (parser->lexer);
11320
11321 if (parser->in_switch_statement_p)
11322 {
11323 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11324 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11325 {
11326 label = CASE_LABEL (l);
11327 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11328 }
11329 }
11330 else
11331 error_at (token->location, "case label not within a switch statement");
11332 break;
11333
11334 default:
11335 /* Anything else must be an ordinary label. */
11336 label = finish_label_stmt (cp_parser_identifier (parser));
11337 if (label && TREE_CODE (label) == LABEL_DECL)
11338 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11339 break;
11340 }
11341
11342 /* Require the `:' token. */
11343 cp_parser_require (parser, CPP_COLON, RT_COLON);
11344
11345 /* An ordinary label may optionally be followed by attributes.
11346 However, this is only permitted if the attributes are then
11347 followed by a semicolon. This is because, for backward
11348 compatibility, when parsing
11349 lab: __attribute__ ((unused)) int i;
11350 we want the attribute to attach to "i", not "lab". */
11351 if (label != NULL_TREE
11352 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11353 {
11354 tree attrs;
11355 cp_parser_parse_tentatively (parser);
11356 attrs = cp_parser_gnu_attributes_opt (parser);
11357 if (attrs == NULL_TREE
11358 /* And fallthrough always binds to the expression-statement. */
11359 || attribute_fallthrough_p (attrs)
11360 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11361 cp_parser_abort_tentative_parse (parser);
11362 else if (!cp_parser_parse_definitely (parser))
11363 ;
11364 else
11365 attributes = attr_chainon (attributes, attrs);
11366 }
11367
11368 if (attributes != NULL_TREE)
11369 cplus_decl_attributes (&label, attributes, 0);
11370
11371 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11372 }
11373
11374 /* Parse an expression-statement.
11375
11376 expression-statement:
11377 expression [opt] ;
11378
11379 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11380 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11381 indicates whether this expression-statement is part of an
11382 expression statement. */
11383
11384 static tree
11385 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11386 {
11387 tree statement = NULL_TREE;
11388 cp_token *token = cp_lexer_peek_token (parser->lexer);
11389 location_t loc = token->location;
11390
11391 /* There might be attribute fallthrough. */
11392 tree attr = cp_parser_gnu_attributes_opt (parser);
11393
11394 /* If the next token is a ';', then there is no expression
11395 statement. */
11396 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11397 {
11398 statement = cp_parser_expression (parser);
11399 if (statement == error_mark_node
11400 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11401 {
11402 cp_parser_skip_to_end_of_block_or_statement (parser);
11403 return error_mark_node;
11404 }
11405 }
11406
11407 /* Handle [[fallthrough]];. */
11408 if (attribute_fallthrough_p (attr))
11409 {
11410 /* The next token after the fallthrough attribute is ';'. */
11411 if (statement == NULL_TREE)
11412 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11413 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11414 void_type_node, 0);
11415 else
11416 warning_at (loc, OPT_Wattributes,
11417 "%<fallthrough%> attribute not followed by %<;%>");
11418 attr = NULL_TREE;
11419 }
11420
11421 /* Allow "[[fallthrough]];", but warn otherwise. */
11422 if (attr != NULL_TREE)
11423 warning_at (loc, OPT_Wattributes,
11424 "attributes at the beginning of statement are ignored");
11425
11426 /* Give a helpful message for "A<T>::type t;" and the like. */
11427 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11428 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11429 {
11430 if (TREE_CODE (statement) == SCOPE_REF)
11431 error_at (token->location, "need %<typename%> before %qE because "
11432 "%qT is a dependent scope",
11433 statement, TREE_OPERAND (statement, 0));
11434 else if (is_overloaded_fn (statement)
11435 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11436 {
11437 /* A::A a; */
11438 tree fn = get_first_fn (statement);
11439 error_at (token->location,
11440 "%<%T::%D%> names the constructor, not the type",
11441 DECL_CONTEXT (fn), DECL_NAME (fn));
11442 }
11443 }
11444
11445 /* Consume the final `;'. */
11446 cp_parser_consume_semicolon_at_end_of_statement (parser);
11447
11448 if (in_statement_expr
11449 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11450 /* This is the final expression statement of a statement
11451 expression. */
11452 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11453 else if (statement)
11454 statement = finish_expr_stmt (statement);
11455
11456 return statement;
11457 }
11458
11459 /* Parse a compound-statement.
11460
11461 compound-statement:
11462 { statement-seq [opt] }
11463
11464 GNU extension:
11465
11466 compound-statement:
11467 { label-declaration-seq [opt] statement-seq [opt] }
11468
11469 label-declaration-seq:
11470 label-declaration
11471 label-declaration-seq label-declaration
11472
11473 Returns a tree representing the statement. */
11474
11475 static tree
11476 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11477 int bcs_flags, bool function_body)
11478 {
11479 tree compound_stmt;
11480 matching_braces braces;
11481
11482 /* Consume the `{'. */
11483 if (!braces.require_open (parser))
11484 return error_mark_node;
11485 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11486 && !function_body && cxx_dialect < cxx14)
11487 pedwarn (input_location, OPT_Wpedantic,
11488 "compound-statement in %<constexpr%> function");
11489 /* Begin the compound-statement. */
11490 compound_stmt = begin_compound_stmt (bcs_flags);
11491 /* If the next keyword is `__label__' we have a label declaration. */
11492 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11493 cp_parser_label_declaration (parser);
11494 /* Parse an (optional) statement-seq. */
11495 cp_parser_statement_seq_opt (parser, in_statement_expr);
11496 /* Finish the compound-statement. */
11497 finish_compound_stmt (compound_stmt);
11498 /* Consume the `}'. */
11499 braces.require_close (parser);
11500
11501 return compound_stmt;
11502 }
11503
11504 /* Parse an (optional) statement-seq.
11505
11506 statement-seq:
11507 statement
11508 statement-seq [opt] statement */
11509
11510 static void
11511 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11512 {
11513 /* Scan statements until there aren't any more. */
11514 while (true)
11515 {
11516 cp_token *token = cp_lexer_peek_token (parser->lexer);
11517
11518 /* If we are looking at a `}', then we have run out of
11519 statements; the same is true if we have reached the end
11520 of file, or have stumbled upon a stray '@end'. */
11521 if (token->type == CPP_CLOSE_BRACE
11522 || token->type == CPP_EOF
11523 || token->type == CPP_PRAGMA_EOL
11524 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11525 break;
11526
11527 /* If we are in a compound statement and find 'else' then
11528 something went wrong. */
11529 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11530 {
11531 if (parser->in_statement & IN_IF_STMT)
11532 break;
11533 else
11534 {
11535 token = cp_lexer_consume_token (parser->lexer);
11536 error_at (token->location, "%<else%> without a previous %<if%>");
11537 }
11538 }
11539
11540 /* Parse the statement. */
11541 cp_parser_statement (parser, in_statement_expr, true, NULL);
11542 }
11543 }
11544
11545 /* Return true if this is the C++20 version of range-based-for with
11546 init-statement. */
11547
11548 static bool
11549 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11550 {
11551 bool r = false;
11552
11553 /* Save tokens so that we can put them back. */
11554 cp_lexer_save_tokens (parser->lexer);
11555
11556 /* There has to be an unnested ; followed by an unnested :. */
11557 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11558 /*recovering=*/false,
11559 CPP_SEMICOLON,
11560 /*consume_paren=*/false) != -1)
11561 goto out;
11562
11563 /* We found the semicolon, eat it now. */
11564 cp_lexer_consume_token (parser->lexer);
11565
11566 /* Now look for ':' that is not nested in () or {}. */
11567 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11568 /*recovering=*/false,
11569 CPP_COLON,
11570 /*consume_paren=*/false) == -1);
11571
11572 out:
11573 /* Roll back the tokens we skipped. */
11574 cp_lexer_rollback_tokens (parser->lexer);
11575
11576 return r;
11577 }
11578
11579 /* Return true if we're looking at (init; cond), false otherwise. */
11580
11581 static bool
11582 cp_parser_init_statement_p (cp_parser *parser)
11583 {
11584 /* Save tokens so that we can put them back. */
11585 cp_lexer_save_tokens (parser->lexer);
11586
11587 /* Look for ';' that is not nested in () or {}. */
11588 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11589 /*recovering=*/false,
11590 CPP_SEMICOLON,
11591 /*consume_paren=*/false);
11592
11593 /* Roll back the tokens we skipped. */
11594 cp_lexer_rollback_tokens (parser->lexer);
11595
11596 return ret == -1;
11597 }
11598
11599 /* Parse a selection-statement.
11600
11601 selection-statement:
11602 if ( init-statement [opt] condition ) statement
11603 if ( init-statement [opt] condition ) statement else statement
11604 switch ( init-statement [opt] condition ) statement
11605
11606 Returns the new IF_STMT or SWITCH_STMT.
11607
11608 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11609 is a (possibly labeled) if statement which is not enclosed in
11610 braces and has an else clause. This is used to implement
11611 -Wparentheses.
11612
11613 CHAIN is a vector of if-else-if conditions. This is used to implement
11614 -Wduplicated-cond. */
11615
11616 static tree
11617 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11618 vec<tree> *chain)
11619 {
11620 cp_token *token;
11621 enum rid keyword;
11622 token_indent_info guard_tinfo;
11623
11624 if (if_p != NULL)
11625 *if_p = false;
11626
11627 /* Peek at the next token. */
11628 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11629 guard_tinfo = get_token_indent_info (token);
11630
11631 /* See what kind of keyword it is. */
11632 keyword = token->keyword;
11633 switch (keyword)
11634 {
11635 case RID_IF:
11636 case RID_SWITCH:
11637 {
11638 tree statement;
11639 tree condition;
11640
11641 bool cx = false;
11642 if (keyword == RID_IF
11643 && cp_lexer_next_token_is_keyword (parser->lexer,
11644 RID_CONSTEXPR))
11645 {
11646 cx = true;
11647 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11648 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11649 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11650 "with -std=c++17 or -std=gnu++17");
11651 }
11652
11653 /* Look for the `('. */
11654 matching_parens parens;
11655 if (!parens.require_open (parser))
11656 {
11657 cp_parser_skip_to_end_of_statement (parser);
11658 return error_mark_node;
11659 }
11660
11661 /* Begin the selection-statement. */
11662 if (keyword == RID_IF)
11663 {
11664 statement = begin_if_stmt ();
11665 IF_STMT_CONSTEXPR_P (statement) = cx;
11666 }
11667 else
11668 statement = begin_switch_stmt ();
11669
11670 /* Parse the optional init-statement. */
11671 if (cp_parser_init_statement_p (parser))
11672 {
11673 tree decl;
11674 if (cxx_dialect < cxx17)
11675 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11676 "init-statement in selection statements only available "
11677 "with -std=c++17 or -std=gnu++17");
11678 cp_parser_init_statement (parser, &decl);
11679 }
11680
11681 /* Parse the condition. */
11682 condition = cp_parser_condition (parser);
11683 /* Look for the `)'. */
11684 if (!parens.require_close (parser))
11685 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11686 /*consume_paren=*/true);
11687
11688 if (keyword == RID_IF)
11689 {
11690 bool nested_if;
11691 unsigned char in_statement;
11692
11693 /* Add the condition. */
11694 condition = finish_if_stmt_cond (condition, statement);
11695
11696 if (warn_duplicated_cond)
11697 warn_duplicated_cond_add_or_warn (token->location, condition,
11698 &chain);
11699
11700 /* Parse the then-clause. */
11701 in_statement = parser->in_statement;
11702 parser->in_statement |= IN_IF_STMT;
11703
11704 /* Outside a template, the non-selected branch of a constexpr
11705 if is a 'discarded statement', i.e. unevaluated. */
11706 bool was_discarded = in_discarded_stmt;
11707 bool discard_then = (cx && !processing_template_decl
11708 && integer_zerop (condition));
11709 if (discard_then)
11710 {
11711 in_discarded_stmt = true;
11712 ++c_inhibit_evaluation_warnings;
11713 }
11714
11715 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11716 guard_tinfo);
11717
11718 parser->in_statement = in_statement;
11719
11720 finish_then_clause (statement);
11721
11722 if (discard_then)
11723 {
11724 THEN_CLAUSE (statement) = NULL_TREE;
11725 in_discarded_stmt = was_discarded;
11726 --c_inhibit_evaluation_warnings;
11727 }
11728
11729 /* If the next token is `else', parse the else-clause. */
11730 if (cp_lexer_next_token_is_keyword (parser->lexer,
11731 RID_ELSE))
11732 {
11733 bool discard_else = (cx && !processing_template_decl
11734 && integer_nonzerop (condition));
11735 if (discard_else)
11736 {
11737 in_discarded_stmt = true;
11738 ++c_inhibit_evaluation_warnings;
11739 }
11740
11741 guard_tinfo
11742 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11743 /* Consume the `else' keyword. */
11744 cp_lexer_consume_token (parser->lexer);
11745 if (warn_duplicated_cond)
11746 {
11747 if (cp_lexer_next_token_is_keyword (parser->lexer,
11748 RID_IF)
11749 && chain == NULL)
11750 {
11751 /* We've got "if (COND) else if (COND2)". Start
11752 the condition chain and add COND as the first
11753 element. */
11754 chain = new vec<tree> ();
11755 if (!CONSTANT_CLASS_P (condition)
11756 && !TREE_SIDE_EFFECTS (condition))
11757 {
11758 /* Wrap it in a NOP_EXPR so that we can set the
11759 location of the condition. */
11760 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11761 condition);
11762 SET_EXPR_LOCATION (e, token->location);
11763 chain->safe_push (e);
11764 }
11765 }
11766 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11767 RID_IF))
11768 {
11769 /* This is if-else without subsequent if. Zap the
11770 condition chain; we would have already warned at
11771 this point. */
11772 delete chain;
11773 chain = NULL;
11774 }
11775 }
11776 begin_else_clause (statement);
11777 /* Parse the else-clause. */
11778 cp_parser_implicitly_scoped_statement (parser, NULL,
11779 guard_tinfo, chain);
11780
11781 finish_else_clause (statement);
11782
11783 /* If we are currently parsing a then-clause, then
11784 IF_P will not be NULL. We set it to true to
11785 indicate that this if statement has an else clause.
11786 This may trigger the Wparentheses warning below
11787 when we get back up to the parent if statement. */
11788 if (if_p != NULL)
11789 *if_p = true;
11790
11791 if (discard_else)
11792 {
11793 ELSE_CLAUSE (statement) = NULL_TREE;
11794 in_discarded_stmt = was_discarded;
11795 --c_inhibit_evaluation_warnings;
11796 }
11797 }
11798 else
11799 {
11800 /* This if statement does not have an else clause. If
11801 NESTED_IF is true, then the then-clause has an if
11802 statement which does have an else clause. We warn
11803 about the potential ambiguity. */
11804 if (nested_if)
11805 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11806 "suggest explicit braces to avoid ambiguous"
11807 " %<else%>");
11808 if (warn_duplicated_cond)
11809 {
11810 /* We don't need the condition chain anymore. */
11811 delete chain;
11812 chain = NULL;
11813 }
11814 }
11815
11816 /* Now we're all done with the if-statement. */
11817 finish_if_stmt (statement);
11818 }
11819 else
11820 {
11821 bool in_switch_statement_p;
11822 unsigned char in_statement;
11823
11824 /* Add the condition. */
11825 finish_switch_cond (condition, statement);
11826
11827 /* Parse the body of the switch-statement. */
11828 in_switch_statement_p = parser->in_switch_statement_p;
11829 in_statement = parser->in_statement;
11830 parser->in_switch_statement_p = true;
11831 parser->in_statement |= IN_SWITCH_STMT;
11832 cp_parser_implicitly_scoped_statement (parser, if_p,
11833 guard_tinfo);
11834 parser->in_switch_statement_p = in_switch_statement_p;
11835 parser->in_statement = in_statement;
11836
11837 /* Now we're all done with the switch-statement. */
11838 finish_switch_stmt (statement);
11839 }
11840
11841 return statement;
11842 }
11843 break;
11844
11845 default:
11846 cp_parser_error (parser, "expected selection-statement");
11847 return error_mark_node;
11848 }
11849 }
11850
11851 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11852 If we have seen at least one decl-specifier, and the next token
11853 is not a parenthesis, then we must be looking at a declaration.
11854 (After "int (" we might be looking at a functional cast.) */
11855
11856 static void
11857 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11858 bool any_specifiers_p)
11859 {
11860 if (any_specifiers_p
11861 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11862 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11863 && !cp_parser_error_occurred (parser))
11864 cp_parser_commit_to_tentative_parse (parser);
11865 }
11866
11867 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11868 The declarator shall not specify a function or an array. Returns
11869 TRUE if the declarator is valid, FALSE otherwise. */
11870
11871 static bool
11872 cp_parser_check_condition_declarator (cp_parser* parser,
11873 cp_declarator *declarator,
11874 location_t loc)
11875 {
11876 if (declarator == cp_error_declarator
11877 || function_declarator_p (declarator)
11878 || declarator->kind == cdk_array)
11879 {
11880 if (declarator == cp_error_declarator)
11881 /* Already complained. */;
11882 else if (declarator->kind == cdk_array)
11883 error_at (loc, "condition declares an array");
11884 else
11885 error_at (loc, "condition declares a function");
11886 if (parser->fully_implicit_function_template_p)
11887 abort_fully_implicit_template (parser);
11888 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11889 /*or_comma=*/false,
11890 /*consume_paren=*/false);
11891 return false;
11892 }
11893 else
11894 return true;
11895 }
11896
11897 /* Parse a condition.
11898
11899 condition:
11900 expression
11901 type-specifier-seq declarator = initializer-clause
11902 type-specifier-seq declarator braced-init-list
11903
11904 GNU Extension:
11905
11906 condition:
11907 type-specifier-seq declarator asm-specification [opt]
11908 attributes [opt] = assignment-expression
11909
11910 Returns the expression that should be tested. */
11911
11912 static tree
11913 cp_parser_condition (cp_parser* parser)
11914 {
11915 cp_decl_specifier_seq type_specifiers;
11916 const char *saved_message;
11917 int declares_class_or_enum;
11918
11919 /* Try the declaration first. */
11920 cp_parser_parse_tentatively (parser);
11921 /* New types are not allowed in the type-specifier-seq for a
11922 condition. */
11923 saved_message = parser->type_definition_forbidden_message;
11924 parser->type_definition_forbidden_message
11925 = G_("types may not be defined in conditions");
11926 /* Parse the type-specifier-seq. */
11927 cp_parser_decl_specifier_seq (parser,
11928 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11929 &type_specifiers,
11930 &declares_class_or_enum);
11931 /* Restore the saved message. */
11932 parser->type_definition_forbidden_message = saved_message;
11933
11934 cp_parser_maybe_commit_to_declaration (parser,
11935 type_specifiers.any_specifiers_p);
11936
11937 /* If all is well, we might be looking at a declaration. */
11938 if (!cp_parser_error_occurred (parser))
11939 {
11940 tree decl;
11941 tree asm_specification;
11942 tree attributes;
11943 cp_declarator *declarator;
11944 tree initializer = NULL_TREE;
11945 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11946
11947 /* Parse the declarator. */
11948 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11949 CP_PARSER_FLAGS_NONE,
11950 /*ctor_dtor_or_conv_p=*/NULL,
11951 /*parenthesized_p=*/NULL,
11952 /*member_p=*/false,
11953 /*friend_p=*/false);
11954 /* Parse the attributes. */
11955 attributes = cp_parser_attributes_opt (parser);
11956 /* Parse the asm-specification. */
11957 asm_specification = cp_parser_asm_specification_opt (parser);
11958 /* If the next token is not an `=' or '{', then we might still be
11959 looking at an expression. For example:
11960
11961 if (A(a).x)
11962
11963 looks like a decl-specifier-seq and a declarator -- but then
11964 there is no `=', so this is an expression. */
11965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11966 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11967 cp_parser_simulate_error (parser);
11968
11969 /* If we did see an `=' or '{', then we are looking at a declaration
11970 for sure. */
11971 if (cp_parser_parse_definitely (parser))
11972 {
11973 tree pushed_scope;
11974 bool non_constant_p = false;
11975 int flags = LOOKUP_ONLYCONVERTING;
11976
11977 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
11978 return error_mark_node;
11979
11980 /* Create the declaration. */
11981 decl = start_decl (declarator, &type_specifiers,
11982 /*initialized_p=*/true,
11983 attributes, /*prefix_attributes=*/NULL_TREE,
11984 &pushed_scope);
11985
11986 /* Parse the initializer. */
11987 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11988 {
11989 initializer = cp_parser_braced_list (parser, &non_constant_p);
11990 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11991 flags = 0;
11992 }
11993 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11994 {
11995 /* Consume the `='. */
11996 cp_lexer_consume_token (parser->lexer);
11997 initializer = cp_parser_initializer_clause (parser,
11998 &non_constant_p);
11999 }
12000 else
12001 {
12002 cp_parser_error (parser, "expected initializer");
12003 initializer = error_mark_node;
12004 }
12005 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12006 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12007
12008 /* Process the initializer. */
12009 cp_finish_decl (decl,
12010 initializer, !non_constant_p,
12011 asm_specification,
12012 flags);
12013
12014 if (pushed_scope)
12015 pop_scope (pushed_scope);
12016
12017 return convert_from_reference (decl);
12018 }
12019 }
12020 /* If we didn't even get past the declarator successfully, we are
12021 definitely not looking at a declaration. */
12022 else
12023 cp_parser_abort_tentative_parse (parser);
12024
12025 /* Otherwise, we are looking at an expression. */
12026 return cp_parser_expression (parser);
12027 }
12028
12029 /* Parses a for-statement or range-for-statement until the closing ')',
12030 not included. */
12031
12032 static tree
12033 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12034 {
12035 tree init, scope, decl;
12036 bool is_range_for;
12037
12038 /* Begin the for-statement. */
12039 scope = begin_for_scope (&init);
12040
12041 /* Parse the initialization. */
12042 is_range_for = cp_parser_init_statement (parser, &decl);
12043
12044 if (is_range_for)
12045 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12046 false);
12047 else
12048 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12049 }
12050
12051 static tree
12052 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12053 unsigned short unroll)
12054 {
12055 /* Normal for loop */
12056 tree condition = NULL_TREE;
12057 tree expression = NULL_TREE;
12058 tree stmt;
12059
12060 stmt = begin_for_stmt (scope, init);
12061 /* The init-statement has already been parsed in
12062 cp_parser_init_statement, so no work is needed here. */
12063 finish_init_stmt (stmt);
12064
12065 /* If there's a condition, process it. */
12066 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12067 condition = cp_parser_condition (parser);
12068 else if (ivdep)
12069 {
12070 cp_parser_error (parser, "missing loop condition in loop with "
12071 "%<GCC ivdep%> pragma");
12072 condition = error_mark_node;
12073 }
12074 else if (unroll)
12075 {
12076 cp_parser_error (parser, "missing loop condition in loop with "
12077 "%<GCC unroll%> pragma");
12078 condition = error_mark_node;
12079 }
12080 finish_for_cond (condition, stmt, ivdep, unroll);
12081 /* Look for the `;'. */
12082 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12083
12084 /* If there's an expression, process it. */
12085 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12086 expression = cp_parser_expression (parser);
12087 finish_for_expr (expression, stmt);
12088
12089 return stmt;
12090 }
12091
12092 /* Tries to parse a range-based for-statement:
12093
12094 range-based-for:
12095 decl-specifier-seq declarator : expression
12096
12097 The decl-specifier-seq declarator and the `:' are already parsed by
12098 cp_parser_init_statement. If processing_template_decl it returns a
12099 newly created RANGE_FOR_STMT; if not, it is converted to a
12100 regular FOR_STMT. */
12101
12102 static tree
12103 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12104 bool ivdep, unsigned short unroll, bool is_omp)
12105 {
12106 tree stmt, range_expr;
12107 auto_vec <cxx_binding *, 16> bindings;
12108 auto_vec <tree, 16> names;
12109 tree decomp_first_name = NULL_TREE;
12110 unsigned int decomp_cnt = 0;
12111
12112 /* Get the range declaration momentarily out of the way so that
12113 the range expression doesn't clash with it. */
12114 if (range_decl != error_mark_node)
12115 {
12116 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12117 {
12118 tree v = DECL_VALUE_EXPR (range_decl);
12119 /* For decomposition declaration get all of the corresponding
12120 declarations out of the way. */
12121 if (TREE_CODE (v) == ARRAY_REF
12122 && VAR_P (TREE_OPERAND (v, 0))
12123 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12124 {
12125 tree d = range_decl;
12126 range_decl = TREE_OPERAND (v, 0);
12127 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12128 decomp_first_name = d;
12129 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12130 {
12131 tree name = DECL_NAME (d);
12132 names.safe_push (name);
12133 bindings.safe_push (IDENTIFIER_BINDING (name));
12134 IDENTIFIER_BINDING (name)
12135 = IDENTIFIER_BINDING (name)->previous;
12136 }
12137 }
12138 }
12139 if (names.is_empty ())
12140 {
12141 tree name = DECL_NAME (range_decl);
12142 names.safe_push (name);
12143 bindings.safe_push (IDENTIFIER_BINDING (name));
12144 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12145 }
12146 }
12147
12148 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12149 {
12150 bool expr_non_constant_p;
12151 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12152 }
12153 else
12154 range_expr = cp_parser_expression (parser);
12155
12156 /* Put the range declaration(s) back into scope. */
12157 for (unsigned int i = 0; i < names.length (); i++)
12158 {
12159 cxx_binding *binding = bindings[i];
12160 binding->previous = IDENTIFIER_BINDING (names[i]);
12161 IDENTIFIER_BINDING (names[i]) = binding;
12162 }
12163
12164 /* finish_omp_for has its own code for the following, so just
12165 return the range_expr instead. */
12166 if (is_omp)
12167 return range_expr;
12168
12169 /* If in template, STMT is converted to a normal for-statement
12170 at instantiation. If not, it is done just ahead. */
12171 if (processing_template_decl)
12172 {
12173 if (check_for_bare_parameter_packs (range_expr))
12174 range_expr = error_mark_node;
12175 stmt = begin_range_for_stmt (scope, init);
12176 if (ivdep)
12177 RANGE_FOR_IVDEP (stmt) = 1;
12178 if (unroll)
12179 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12180 finish_range_for_decl (stmt, range_decl, range_expr);
12181 if (!type_dependent_expression_p (range_expr)
12182 /* do_auto_deduction doesn't mess with template init-lists. */
12183 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12184 do_range_for_auto_deduction (range_decl, range_expr);
12185 }
12186 else
12187 {
12188 stmt = begin_for_stmt (scope, init);
12189 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12190 decomp_first_name, decomp_cnt, ivdep,
12191 unroll);
12192 }
12193 return stmt;
12194 }
12195
12196 /* Subroutine of cp_convert_range_for: given the initializer expression,
12197 builds up the range temporary. */
12198
12199 static tree
12200 build_range_temp (tree range_expr)
12201 {
12202 tree range_type, range_temp;
12203
12204 /* Find out the type deduced by the declaration
12205 `auto &&__range = range_expr'. */
12206 range_type = cp_build_reference_type (make_auto (), true);
12207 range_type = do_auto_deduction (range_type, range_expr,
12208 type_uses_auto (range_type));
12209
12210 /* Create the __range variable. */
12211 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12212 range_type);
12213 TREE_USED (range_temp) = 1;
12214 DECL_ARTIFICIAL (range_temp) = 1;
12215
12216 return range_temp;
12217 }
12218
12219 /* Used by cp_parser_range_for in template context: we aren't going to
12220 do a full conversion yet, but we still need to resolve auto in the
12221 type of the for-range-declaration if present. This is basically
12222 a shortcut version of cp_convert_range_for. */
12223
12224 static void
12225 do_range_for_auto_deduction (tree decl, tree range_expr)
12226 {
12227 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12228 if (auto_node)
12229 {
12230 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12231 range_temp = convert_from_reference (build_range_temp (range_expr));
12232 iter_type = (cp_parser_perform_range_for_lookup
12233 (range_temp, &begin_dummy, &end_dummy));
12234 if (iter_type)
12235 {
12236 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12237 iter_type);
12238 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12239 RO_UNARY_STAR,
12240 tf_warning_or_error);
12241 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12242 iter_decl, auto_node);
12243 }
12244 }
12245 }
12246
12247 /* Converts a range-based for-statement into a normal
12248 for-statement, as per the definition.
12249
12250 for (RANGE_DECL : RANGE_EXPR)
12251 BLOCK
12252
12253 should be equivalent to:
12254
12255 {
12256 auto &&__range = RANGE_EXPR;
12257 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12258 __begin != __end;
12259 ++__begin)
12260 {
12261 RANGE_DECL = *__begin;
12262 BLOCK
12263 }
12264 }
12265
12266 If RANGE_EXPR is an array:
12267 BEGIN_EXPR = __range
12268 END_EXPR = __range + ARRAY_SIZE(__range)
12269 Else if RANGE_EXPR has a member 'begin' or 'end':
12270 BEGIN_EXPR = __range.begin()
12271 END_EXPR = __range.end()
12272 Else:
12273 BEGIN_EXPR = begin(__range)
12274 END_EXPR = end(__range);
12275
12276 If __range has a member 'begin' but not 'end', or vice versa, we must
12277 still use the second alternative (it will surely fail, however).
12278 When calling begin()/end() in the third alternative we must use
12279 argument dependent lookup, but always considering 'std' as an associated
12280 namespace. */
12281
12282 tree
12283 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12284 tree decomp_first_name, unsigned int decomp_cnt,
12285 bool ivdep, unsigned short unroll)
12286 {
12287 tree begin, end;
12288 tree iter_type, begin_expr, end_expr;
12289 tree condition, expression;
12290
12291 range_expr = mark_lvalue_use (range_expr);
12292
12293 if (range_decl == error_mark_node || range_expr == error_mark_node)
12294 /* If an error happened previously do nothing or else a lot of
12295 unhelpful errors would be issued. */
12296 begin_expr = end_expr = iter_type = error_mark_node;
12297 else
12298 {
12299 tree range_temp;
12300
12301 if (VAR_P (range_expr)
12302 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12303 /* Can't bind a reference to an array of runtime bound. */
12304 range_temp = range_expr;
12305 else
12306 {
12307 range_temp = build_range_temp (range_expr);
12308 pushdecl (range_temp);
12309 cp_finish_decl (range_temp, range_expr,
12310 /*is_constant_init*/false, NULL_TREE,
12311 LOOKUP_ONLYCONVERTING);
12312 range_temp = convert_from_reference (range_temp);
12313 }
12314 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12315 &begin_expr, &end_expr);
12316 }
12317
12318 /* The new for initialization statement. */
12319 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12320 iter_type);
12321 TREE_USED (begin) = 1;
12322 DECL_ARTIFICIAL (begin) = 1;
12323 pushdecl (begin);
12324 cp_finish_decl (begin, begin_expr,
12325 /*is_constant_init*/false, NULL_TREE,
12326 LOOKUP_ONLYCONVERTING);
12327
12328 if (cxx_dialect >= cxx17)
12329 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12330 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12331 TREE_USED (end) = 1;
12332 DECL_ARTIFICIAL (end) = 1;
12333 pushdecl (end);
12334 cp_finish_decl (end, end_expr,
12335 /*is_constant_init*/false, NULL_TREE,
12336 LOOKUP_ONLYCONVERTING);
12337
12338 finish_init_stmt (statement);
12339
12340 /* The new for condition. */
12341 condition = build_x_binary_op (input_location, NE_EXPR,
12342 begin, ERROR_MARK,
12343 end, ERROR_MARK,
12344 NULL, tf_warning_or_error);
12345 finish_for_cond (condition, statement, ivdep, unroll);
12346
12347 /* The new increment expression. */
12348 expression = finish_unary_op_expr (input_location,
12349 PREINCREMENT_EXPR, begin,
12350 tf_warning_or_error);
12351 finish_for_expr (expression, statement);
12352
12353 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12354 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12355
12356 /* The declaration is initialized with *__begin inside the loop body. */
12357 cp_finish_decl (range_decl,
12358 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12359 tf_warning_or_error),
12360 /*is_constant_init*/false, NULL_TREE,
12361 LOOKUP_ONLYCONVERTING);
12362 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12363 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12364
12365 return statement;
12366 }
12367
12368 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12369 We need to solve both at the same time because the method used
12370 depends on the existence of members begin or end.
12371 Returns the type deduced for the iterator expression. */
12372
12373 static tree
12374 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12375 {
12376 if (error_operand_p (range))
12377 {
12378 *begin = *end = error_mark_node;
12379 return error_mark_node;
12380 }
12381
12382 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12383 {
12384 error ("range-based %<for%> expression of type %qT "
12385 "has incomplete type", TREE_TYPE (range));
12386 *begin = *end = error_mark_node;
12387 return error_mark_node;
12388 }
12389 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12390 {
12391 /* If RANGE is an array, we will use pointer arithmetic. */
12392 *begin = decay_conversion (range, tf_warning_or_error);
12393 *end = build_binary_op (input_location, PLUS_EXPR,
12394 range,
12395 array_type_nelts_top (TREE_TYPE (range)),
12396 false);
12397 return TREE_TYPE (*begin);
12398 }
12399 else
12400 {
12401 /* If it is not an array, we must do a bit of magic. */
12402 tree id_begin, id_end;
12403 tree member_begin, member_end;
12404
12405 *begin = *end = error_mark_node;
12406
12407 id_begin = get_identifier ("begin");
12408 id_end = get_identifier ("end");
12409 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12410 /*protect=*/2, /*want_type=*/false,
12411 tf_warning_or_error);
12412 member_end = lookup_member (TREE_TYPE (range), id_end,
12413 /*protect=*/2, /*want_type=*/false,
12414 tf_warning_or_error);
12415
12416 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12417 {
12418 /* Use the member functions. */
12419 *begin = cp_parser_range_for_member_function (range, id_begin);
12420 *end = cp_parser_range_for_member_function (range, id_end);
12421 }
12422 else
12423 {
12424 /* Use global functions with ADL. */
12425 vec<tree, va_gc> *vec;
12426 vec = make_tree_vector ();
12427
12428 vec_safe_push (vec, range);
12429
12430 member_begin = perform_koenig_lookup (id_begin, vec,
12431 tf_warning_or_error);
12432 *begin = finish_call_expr (member_begin, &vec, false, true,
12433 tf_warning_or_error);
12434 member_end = perform_koenig_lookup (id_end, vec,
12435 tf_warning_or_error);
12436 *end = finish_call_expr (member_end, &vec, false, true,
12437 tf_warning_or_error);
12438
12439 release_tree_vector (vec);
12440 }
12441
12442 /* Last common checks. */
12443 if (*begin == error_mark_node || *end == error_mark_node)
12444 {
12445 /* If one of the expressions is an error do no more checks. */
12446 *begin = *end = error_mark_node;
12447 return error_mark_node;
12448 }
12449 else if (type_dependent_expression_p (*begin)
12450 || type_dependent_expression_p (*end))
12451 /* Can happen, when, eg, in a template context, Koenig lookup
12452 can't resolve begin/end (c++/58503). */
12453 return NULL_TREE;
12454 else
12455 {
12456 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12457 /* The unqualified type of the __begin and __end temporaries should
12458 be the same, as required by the multiple auto declaration. */
12459 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12460 {
12461 if (cxx_dialect >= cxx17
12462 && (build_x_binary_op (input_location, NE_EXPR,
12463 *begin, ERROR_MARK,
12464 *end, ERROR_MARK,
12465 NULL, tf_none)
12466 != error_mark_node))
12467 /* P0184R0 allows __begin and __end to have different types,
12468 but make sure they are comparable so we can give a better
12469 diagnostic. */;
12470 else
12471 error ("inconsistent begin/end types in range-based %<for%> "
12472 "statement: %qT and %qT",
12473 TREE_TYPE (*begin), TREE_TYPE (*end));
12474 }
12475 return iter_type;
12476 }
12477 }
12478 }
12479
12480 /* Helper function for cp_parser_perform_range_for_lookup.
12481 Builds a tree for RANGE.IDENTIFIER(). */
12482
12483 static tree
12484 cp_parser_range_for_member_function (tree range, tree identifier)
12485 {
12486 tree member, res;
12487 vec<tree, va_gc> *vec;
12488
12489 member = finish_class_member_access_expr (range, identifier,
12490 false, tf_warning_or_error);
12491 if (member == error_mark_node)
12492 return error_mark_node;
12493
12494 vec = make_tree_vector ();
12495 res = finish_call_expr (member, &vec,
12496 /*disallow_virtual=*/false,
12497 /*koenig_p=*/false,
12498 tf_warning_or_error);
12499 release_tree_vector (vec);
12500 return res;
12501 }
12502
12503 /* Parse an iteration-statement.
12504
12505 iteration-statement:
12506 while ( condition ) statement
12507 do statement while ( expression ) ;
12508 for ( init-statement condition [opt] ; expression [opt] )
12509 statement
12510
12511 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12512
12513 static tree
12514 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12515 unsigned short unroll)
12516 {
12517 cp_token *token;
12518 enum rid keyword;
12519 tree statement;
12520 unsigned char in_statement;
12521 token_indent_info guard_tinfo;
12522
12523 /* Peek at the next token. */
12524 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12525 if (!token)
12526 return error_mark_node;
12527
12528 guard_tinfo = get_token_indent_info (token);
12529
12530 /* Remember whether or not we are already within an iteration
12531 statement. */
12532 in_statement = parser->in_statement;
12533
12534 /* See what kind of keyword it is. */
12535 keyword = token->keyword;
12536 switch (keyword)
12537 {
12538 case RID_WHILE:
12539 {
12540 tree condition;
12541
12542 /* Begin the while-statement. */
12543 statement = begin_while_stmt ();
12544 /* Look for the `('. */
12545 matching_parens parens;
12546 parens.require_open (parser);
12547 /* Parse the condition. */
12548 condition = cp_parser_condition (parser);
12549 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12550 /* Look for the `)'. */
12551 parens.require_close (parser);
12552 /* Parse the dependent statement. */
12553 parser->in_statement = IN_ITERATION_STMT;
12554 bool prev = note_iteration_stmt_body_start ();
12555 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12556 note_iteration_stmt_body_end (prev);
12557 parser->in_statement = in_statement;
12558 /* We're done with the while-statement. */
12559 finish_while_stmt (statement);
12560 }
12561 break;
12562
12563 case RID_DO:
12564 {
12565 tree expression;
12566
12567 /* Begin the do-statement. */
12568 statement = begin_do_stmt ();
12569 /* Parse the body of the do-statement. */
12570 parser->in_statement = IN_ITERATION_STMT;
12571 bool prev = note_iteration_stmt_body_start ();
12572 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12573 note_iteration_stmt_body_end (prev);
12574 parser->in_statement = in_statement;
12575 finish_do_body (statement);
12576 /* Look for the `while' keyword. */
12577 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12578 /* Look for the `('. */
12579 matching_parens parens;
12580 parens.require_open (parser);
12581 /* Parse the expression. */
12582 expression = cp_parser_expression (parser);
12583 /* We're done with the do-statement. */
12584 finish_do_stmt (expression, statement, ivdep, unroll);
12585 /* Look for the `)'. */
12586 parens.require_close (parser);
12587 /* Look for the `;'. */
12588 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12589 }
12590 break;
12591
12592 case RID_FOR:
12593 {
12594 /* Look for the `('. */
12595 matching_parens parens;
12596 parens.require_open (parser);
12597
12598 statement = cp_parser_for (parser, ivdep, unroll);
12599
12600 /* Look for the `)'. */
12601 parens.require_close (parser);
12602
12603 /* Parse the body of the for-statement. */
12604 parser->in_statement = IN_ITERATION_STMT;
12605 bool prev = note_iteration_stmt_body_start ();
12606 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12607 note_iteration_stmt_body_end (prev);
12608 parser->in_statement = in_statement;
12609
12610 /* We're done with the for-statement. */
12611 finish_for_stmt (statement);
12612 }
12613 break;
12614
12615 default:
12616 cp_parser_error (parser, "expected iteration-statement");
12617 statement = error_mark_node;
12618 break;
12619 }
12620
12621 return statement;
12622 }
12623
12624 /* Parse a init-statement or the declarator of a range-based-for.
12625 Returns true if a range-based-for declaration is seen.
12626
12627 init-statement:
12628 expression-statement
12629 simple-declaration */
12630
12631 static bool
12632 cp_parser_init_statement (cp_parser *parser, tree *decl)
12633 {
12634 /* If the next token is a `;', then we have an empty
12635 expression-statement. Grammatically, this is also a
12636 simple-declaration, but an invalid one, because it does not
12637 declare anything. Therefore, if we did not handle this case
12638 specially, we would issue an error message about an invalid
12639 declaration. */
12640 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12641 {
12642 bool is_range_for = false;
12643 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12644
12645 /* Try to parse the init-statement. */
12646 if (cp_parser_range_based_for_with_init_p (parser))
12647 {
12648 tree dummy;
12649 cp_parser_parse_tentatively (parser);
12650 /* Parse the declaration. */
12651 cp_parser_simple_declaration (parser,
12652 /*function_definition_allowed_p=*/false,
12653 &dummy);
12654 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12655 if (!cp_parser_parse_definitely (parser))
12656 /* That didn't work, try to parse it as an expression-statement. */
12657 cp_parser_expression_statement (parser, NULL_TREE);
12658
12659 if (cxx_dialect < cxx2a)
12660 {
12661 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12662 "range-based %<for%> loops with initializer only "
12663 "available with -std=c++2a or -std=gnu++2a");
12664 *decl = error_mark_node;
12665 }
12666 }
12667
12668 /* A colon is used in range-based for. */
12669 parser->colon_corrects_to_scope_p = false;
12670
12671 /* We're going to speculatively look for a declaration, falling back
12672 to an expression, if necessary. */
12673 cp_parser_parse_tentatively (parser);
12674 /* Parse the declaration. */
12675 cp_parser_simple_declaration (parser,
12676 /*function_definition_allowed_p=*/false,
12677 decl);
12678 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12679 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12680 {
12681 /* It is a range-for, consume the ':'. */
12682 cp_lexer_consume_token (parser->lexer);
12683 is_range_for = true;
12684 if (cxx_dialect < cxx11)
12685 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12686 "range-based %<for%> loops only available with "
12687 "-std=c++11 or -std=gnu++11");
12688 }
12689 else
12690 /* The ';' is not consumed yet because we told
12691 cp_parser_simple_declaration not to. */
12692 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12693
12694 if (cp_parser_parse_definitely (parser))
12695 return is_range_for;
12696 /* If the tentative parse failed, then we shall need to look for an
12697 expression-statement. */
12698 }
12699 /* If we are here, it is an expression-statement. */
12700 cp_parser_expression_statement (parser, NULL_TREE);
12701 return false;
12702 }
12703
12704 /* Parse a jump-statement.
12705
12706 jump-statement:
12707 break ;
12708 continue ;
12709 return expression [opt] ;
12710 return braced-init-list ;
12711 goto identifier ;
12712
12713 GNU extension:
12714
12715 jump-statement:
12716 goto * expression ;
12717
12718 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12719
12720 static tree
12721 cp_parser_jump_statement (cp_parser* parser)
12722 {
12723 tree statement = error_mark_node;
12724 cp_token *token;
12725 enum rid keyword;
12726 unsigned char in_statement;
12727
12728 /* Peek at the next token. */
12729 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12730 if (!token)
12731 return error_mark_node;
12732
12733 /* See what kind of keyword it is. */
12734 keyword = token->keyword;
12735 switch (keyword)
12736 {
12737 case RID_BREAK:
12738 in_statement = parser->in_statement & ~IN_IF_STMT;
12739 switch (in_statement)
12740 {
12741 case 0:
12742 error_at (token->location, "break statement not within loop or switch");
12743 break;
12744 default:
12745 gcc_assert ((in_statement & IN_SWITCH_STMT)
12746 || in_statement == IN_ITERATION_STMT);
12747 statement = finish_break_stmt ();
12748 if (in_statement == IN_ITERATION_STMT)
12749 break_maybe_infinite_loop ();
12750 break;
12751 case IN_OMP_BLOCK:
12752 error_at (token->location, "invalid exit from OpenMP structured block");
12753 break;
12754 case IN_OMP_FOR:
12755 error_at (token->location, "break statement used with OpenMP for loop");
12756 break;
12757 }
12758 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12759 break;
12760
12761 case RID_CONTINUE:
12762 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12763 {
12764 case 0:
12765 error_at (token->location, "continue statement not within a loop");
12766 break;
12767 /* Fall through. */
12768 case IN_ITERATION_STMT:
12769 case IN_OMP_FOR:
12770 statement = finish_continue_stmt ();
12771 break;
12772 case IN_OMP_BLOCK:
12773 error_at (token->location, "invalid exit from OpenMP structured block");
12774 break;
12775 default:
12776 gcc_unreachable ();
12777 }
12778 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12779 break;
12780
12781 case RID_RETURN:
12782 {
12783 tree expr;
12784 bool expr_non_constant_p;
12785
12786 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12787 {
12788 cp_lexer_set_source_position (parser->lexer);
12789 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12790 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12791 }
12792 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12793 expr = cp_parser_expression (parser);
12794 else
12795 /* If the next token is a `;', then there is no
12796 expression. */
12797 expr = NULL_TREE;
12798 /* Build the return-statement. */
12799 if (current_function_auto_return_pattern && in_discarded_stmt)
12800 /* Don't deduce from a discarded return statement. */;
12801 else
12802 statement = finish_return_stmt (expr);
12803 /* Look for the final `;'. */
12804 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12805 }
12806 break;
12807
12808 case RID_GOTO:
12809 if (parser->in_function_body
12810 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12811 {
12812 error ("%<goto%> in %<constexpr%> function");
12813 cp_function_chain->invalid_constexpr = true;
12814 }
12815
12816 /* Create the goto-statement. */
12817 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12818 {
12819 /* Issue a warning about this use of a GNU extension. */
12820 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12821 /* Consume the '*' token. */
12822 cp_lexer_consume_token (parser->lexer);
12823 /* Parse the dependent expression. */
12824 finish_goto_stmt (cp_parser_expression (parser));
12825 }
12826 else
12827 finish_goto_stmt (cp_parser_identifier (parser));
12828 /* Look for the final `;'. */
12829 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12830 break;
12831
12832 default:
12833 cp_parser_error (parser, "expected jump-statement");
12834 break;
12835 }
12836
12837 return statement;
12838 }
12839
12840 /* Parse a declaration-statement.
12841
12842 declaration-statement:
12843 block-declaration */
12844
12845 static void
12846 cp_parser_declaration_statement (cp_parser* parser)
12847 {
12848 void *p;
12849
12850 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12851 p = obstack_alloc (&declarator_obstack, 0);
12852
12853 /* Parse the block-declaration. */
12854 cp_parser_block_declaration (parser, /*statement_p=*/true);
12855
12856 /* Free any declarators allocated. */
12857 obstack_free (&declarator_obstack, p);
12858 }
12859
12860 /* Some dependent statements (like `if (cond) statement'), are
12861 implicitly in their own scope. In other words, if the statement is
12862 a single statement (as opposed to a compound-statement), it is
12863 none-the-less treated as if it were enclosed in braces. Any
12864 declarations appearing in the dependent statement are out of scope
12865 after control passes that point. This function parses a statement,
12866 but ensures that is in its own scope, even if it is not a
12867 compound-statement.
12868
12869 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12870 is a (possibly labeled) if statement which is not enclosed in
12871 braces and has an else clause. This is used to implement
12872 -Wparentheses.
12873
12874 CHAIN is a vector of if-else-if conditions. This is used to implement
12875 -Wduplicated-cond.
12876
12877 Returns the new statement. */
12878
12879 static tree
12880 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12881 const token_indent_info &guard_tinfo,
12882 vec<tree> *chain)
12883 {
12884 tree statement;
12885 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12886 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12887 token_indent_info body_tinfo
12888 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12889
12890 if (if_p != NULL)
12891 *if_p = false;
12892
12893 /* Mark if () ; with a special NOP_EXPR. */
12894 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12895 {
12896 cp_lexer_consume_token (parser->lexer);
12897 statement = add_stmt (build_empty_stmt (body_loc));
12898
12899 if (guard_tinfo.keyword == RID_IF
12900 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12901 warning_at (body_loc, OPT_Wempty_body,
12902 "suggest braces around empty body in an %<if%> statement");
12903 else if (guard_tinfo.keyword == RID_ELSE)
12904 warning_at (body_loc, OPT_Wempty_body,
12905 "suggest braces around empty body in an %<else%> statement");
12906 }
12907 /* if a compound is opened, we simply parse the statement directly. */
12908 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12909 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12910 /* If the token is not a `{', then we must take special action. */
12911 else
12912 {
12913 /* Create a compound-statement. */
12914 statement = begin_compound_stmt (0);
12915 /* Parse the dependent-statement. */
12916 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12917 &body_loc_after_labels);
12918 /* Finish the dummy compound-statement. */
12919 finish_compound_stmt (statement);
12920 }
12921
12922 token_indent_info next_tinfo
12923 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12924 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12925
12926 if (body_loc_after_labels != UNKNOWN_LOCATION
12927 && next_tinfo.type != CPP_SEMICOLON)
12928 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12929 guard_tinfo.location, guard_tinfo.keyword);
12930
12931 /* Return the statement. */
12932 return statement;
12933 }
12934
12935 /* For some dependent statements (like `while (cond) statement'), we
12936 have already created a scope. Therefore, even if the dependent
12937 statement is a compound-statement, we do not want to create another
12938 scope. */
12939
12940 static void
12941 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12942 const token_indent_info &guard_tinfo)
12943 {
12944 /* If the token is a `{', then we must take special action. */
12945 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12946 {
12947 token_indent_info body_tinfo
12948 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12949 location_t loc_after_labels = UNKNOWN_LOCATION;
12950
12951 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12952 &loc_after_labels);
12953 token_indent_info next_tinfo
12954 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12955 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12956
12957 if (loc_after_labels != UNKNOWN_LOCATION
12958 && next_tinfo.type != CPP_SEMICOLON)
12959 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12960 guard_tinfo.location,
12961 guard_tinfo.keyword);
12962 }
12963 else
12964 {
12965 /* Avoid calling cp_parser_compound_statement, so that we
12966 don't create a new scope. Do everything else by hand. */
12967 matching_braces braces;
12968 braces.require_open (parser);
12969 /* If the next keyword is `__label__' we have a label declaration. */
12970 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12971 cp_parser_label_declaration (parser);
12972 /* Parse an (optional) statement-seq. */
12973 cp_parser_statement_seq_opt (parser, NULL_TREE);
12974 braces.require_close (parser);
12975 }
12976 }
12977
12978 /* Declarations [gram.dcl.dcl] */
12979
12980 /* Parse an optional declaration-sequence.
12981
12982 declaration-seq:
12983 declaration
12984 declaration-seq declaration */
12985
12986 static void
12987 cp_parser_declaration_seq_opt (cp_parser* parser)
12988 {
12989 while (true)
12990 {
12991 cp_token *token = cp_lexer_peek_token (parser->lexer);
12992
12993 if (token->type == CPP_CLOSE_BRACE
12994 || token->type == CPP_EOF)
12995 break;
12996 else
12997 cp_parser_toplevel_declaration (parser);
12998 }
12999 }
13000
13001 /* Parse a declaration.
13002
13003 declaration:
13004 block-declaration
13005 function-definition
13006 template-declaration
13007 explicit-instantiation
13008 explicit-specialization
13009 linkage-specification
13010 namespace-definition
13011
13012 C++17:
13013 deduction-guide
13014
13015 GNU extension:
13016
13017 declaration:
13018 __extension__ declaration */
13019
13020 static void
13021 cp_parser_declaration (cp_parser* parser)
13022 {
13023 cp_token token1;
13024 cp_token token2;
13025 int saved_pedantic;
13026 void *p;
13027 tree attributes = NULL_TREE;
13028
13029 /* Check for the `__extension__' keyword. */
13030 if (cp_parser_extension_opt (parser, &saved_pedantic))
13031 {
13032 /* Parse the qualified declaration. */
13033 cp_parser_declaration (parser);
13034 /* Restore the PEDANTIC flag. */
13035 pedantic = saved_pedantic;
13036
13037 return;
13038 }
13039
13040 /* Try to figure out what kind of declaration is present. */
13041 token1 = *cp_lexer_peek_token (parser->lexer);
13042
13043 if (token1.type != CPP_EOF)
13044 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13045 else
13046 {
13047 token2.type = CPP_EOF;
13048 token2.keyword = RID_MAX;
13049 }
13050
13051 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13052 p = obstack_alloc (&declarator_obstack, 0);
13053
13054 /* If the next token is `extern' and the following token is a string
13055 literal, then we have a linkage specification. */
13056 if (token1.keyword == RID_EXTERN
13057 && cp_parser_is_pure_string_literal (&token2))
13058 cp_parser_linkage_specification (parser);
13059 /* If the next token is `template', then we have either a template
13060 declaration, an explicit instantiation, or an explicit
13061 specialization. */
13062 else if (token1.keyword == RID_TEMPLATE)
13063 {
13064 /* `template <>' indicates a template specialization. */
13065 if (token2.type == CPP_LESS
13066 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13067 cp_parser_explicit_specialization (parser);
13068 /* `template <' indicates a template declaration. */
13069 else if (token2.type == CPP_LESS)
13070 cp_parser_template_declaration (parser, /*member_p=*/false);
13071 /* Anything else must be an explicit instantiation. */
13072 else
13073 cp_parser_explicit_instantiation (parser);
13074 }
13075 /* If the next token is `export', then we have a template
13076 declaration. */
13077 else if (token1.keyword == RID_EXPORT)
13078 cp_parser_template_declaration (parser, /*member_p=*/false);
13079 /* If the next token is `extern', 'static' or 'inline' and the one
13080 after that is `template', we have a GNU extended explicit
13081 instantiation directive. */
13082 else if (cp_parser_allow_gnu_extensions_p (parser)
13083 && (token1.keyword == RID_EXTERN
13084 || token1.keyword == RID_STATIC
13085 || token1.keyword == RID_INLINE)
13086 && token2.keyword == RID_TEMPLATE)
13087 cp_parser_explicit_instantiation (parser);
13088 /* If the next token is `namespace', check for a named or unnamed
13089 namespace definition. */
13090 else if (token1.keyword == RID_NAMESPACE
13091 && (/* A named namespace definition. */
13092 (token2.type == CPP_NAME
13093 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13094 != CPP_EQ))
13095 || (token2.type == CPP_OPEN_SQUARE
13096 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13097 == CPP_OPEN_SQUARE)
13098 /* An unnamed namespace definition. */
13099 || token2.type == CPP_OPEN_BRACE
13100 || token2.keyword == RID_ATTRIBUTE))
13101 cp_parser_namespace_definition (parser);
13102 /* An inline (associated) namespace definition. */
13103 else if (token1.keyword == RID_INLINE
13104 && token2.keyword == RID_NAMESPACE)
13105 cp_parser_namespace_definition (parser);
13106 /* Objective-C++ declaration/definition. */
13107 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13108 cp_parser_objc_declaration (parser, NULL_TREE);
13109 else if (c_dialect_objc ()
13110 && token1.keyword == RID_ATTRIBUTE
13111 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13112 cp_parser_objc_declaration (parser, attributes);
13113 /* At this point we may have a template declared by a concept
13114 introduction. */
13115 else if (flag_concepts
13116 && cp_parser_template_declaration_after_export (parser,
13117 /*member_p=*/false))
13118 /* We did. */;
13119 else
13120 /* Try to parse a block-declaration, or a function-definition. */
13121 cp_parser_block_declaration (parser, /*statement_p=*/false);
13122
13123 /* Free any declarators allocated. */
13124 obstack_free (&declarator_obstack, p);
13125 }
13126
13127 /* Parse a namespace-scope declaration. */
13128
13129 static void
13130 cp_parser_toplevel_declaration (cp_parser* parser)
13131 {
13132 cp_token *token = cp_lexer_peek_token (parser->lexer);
13133
13134 if (token->type == CPP_PRAGMA)
13135 /* A top-level declaration can consist solely of a #pragma. A
13136 nested declaration cannot, so this is done here and not in
13137 cp_parser_declaration. (A #pragma at block scope is
13138 handled in cp_parser_statement.) */
13139 cp_parser_pragma (parser, pragma_external, NULL);
13140 else if (token->type == CPP_SEMICOLON)
13141 {
13142 /* A declaration consisting of a single semicolon is
13143 invalid. Allow it unless we're being pedantic. */
13144 cp_lexer_consume_token (parser->lexer);
13145 if (!in_system_header_at (input_location))
13146 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13147 }
13148 else
13149 /* Parse the declaration itself. */
13150 cp_parser_declaration (parser);
13151 }
13152
13153 /* Parse a block-declaration.
13154
13155 block-declaration:
13156 simple-declaration
13157 asm-definition
13158 namespace-alias-definition
13159 using-declaration
13160 using-directive
13161
13162 GNU Extension:
13163
13164 block-declaration:
13165 __extension__ block-declaration
13166
13167 C++0x Extension:
13168
13169 block-declaration:
13170 static_assert-declaration
13171
13172 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13173 part of a declaration-statement. */
13174
13175 static void
13176 cp_parser_block_declaration (cp_parser *parser,
13177 bool statement_p)
13178 {
13179 cp_token *token1;
13180 int saved_pedantic;
13181
13182 /* Check for the `__extension__' keyword. */
13183 if (cp_parser_extension_opt (parser, &saved_pedantic))
13184 {
13185 /* Parse the qualified declaration. */
13186 cp_parser_block_declaration (parser, statement_p);
13187 /* Restore the PEDANTIC flag. */
13188 pedantic = saved_pedantic;
13189
13190 return;
13191 }
13192
13193 /* Peek at the next token to figure out which kind of declaration is
13194 present. */
13195 token1 = cp_lexer_peek_token (parser->lexer);
13196
13197 /* If the next keyword is `asm', we have an asm-definition. */
13198 if (token1->keyword == RID_ASM)
13199 {
13200 if (statement_p)
13201 cp_parser_commit_to_tentative_parse (parser);
13202 cp_parser_asm_definition (parser);
13203 }
13204 /* If the next keyword is `namespace', we have a
13205 namespace-alias-definition. */
13206 else if (token1->keyword == RID_NAMESPACE)
13207 cp_parser_namespace_alias_definition (parser);
13208 /* If the next keyword is `using', we have a
13209 using-declaration, a using-directive, or an alias-declaration. */
13210 else if (token1->keyword == RID_USING)
13211 {
13212 cp_token *token2;
13213
13214 if (statement_p)
13215 cp_parser_commit_to_tentative_parse (parser);
13216 /* If the token after `using' is `namespace', then we have a
13217 using-directive. */
13218 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13219 if (token2->keyword == RID_NAMESPACE)
13220 cp_parser_using_directive (parser);
13221 /* If the second token after 'using' is '=', then we have an
13222 alias-declaration. */
13223 else if (cxx_dialect >= cxx11
13224 && token2->type == CPP_NAME
13225 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13226 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13227 cp_parser_alias_declaration (parser);
13228 /* Otherwise, it's a using-declaration. */
13229 else
13230 cp_parser_using_declaration (parser,
13231 /*access_declaration_p=*/false);
13232 }
13233 /* If the next keyword is `__label__' we have a misplaced label
13234 declaration. */
13235 else if (token1->keyword == RID_LABEL)
13236 {
13237 cp_lexer_consume_token (parser->lexer);
13238 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13239 cp_parser_skip_to_end_of_statement (parser);
13240 /* If the next token is now a `;', consume it. */
13241 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13242 cp_lexer_consume_token (parser->lexer);
13243 }
13244 /* If the next token is `static_assert' we have a static assertion. */
13245 else if (token1->keyword == RID_STATIC_ASSERT)
13246 cp_parser_static_assert (parser, /*member_p=*/false);
13247 /* Anything else must be a simple-declaration. */
13248 else
13249 cp_parser_simple_declaration (parser, !statement_p,
13250 /*maybe_range_for_decl*/NULL);
13251 }
13252
13253 /* Parse a simple-declaration.
13254
13255 simple-declaration:
13256 decl-specifier-seq [opt] init-declarator-list [opt] ;
13257 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13258 brace-or-equal-initializer ;
13259
13260 init-declarator-list:
13261 init-declarator
13262 init-declarator-list , init-declarator
13263
13264 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13265 function-definition as a simple-declaration.
13266
13267 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13268 parsed declaration if it is an uninitialized single declarator not followed
13269 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13270 if present, will not be consumed. */
13271
13272 static void
13273 cp_parser_simple_declaration (cp_parser* parser,
13274 bool function_definition_allowed_p,
13275 tree *maybe_range_for_decl)
13276 {
13277 cp_decl_specifier_seq decl_specifiers;
13278 int declares_class_or_enum;
13279 bool saw_declarator;
13280 location_t comma_loc = UNKNOWN_LOCATION;
13281 location_t init_loc = UNKNOWN_LOCATION;
13282
13283 if (maybe_range_for_decl)
13284 *maybe_range_for_decl = NULL_TREE;
13285
13286 /* Defer access checks until we know what is being declared; the
13287 checks for names appearing in the decl-specifier-seq should be
13288 done as if we were in the scope of the thing being declared. */
13289 push_deferring_access_checks (dk_deferred);
13290
13291 /* Parse the decl-specifier-seq. We have to keep track of whether
13292 or not the decl-specifier-seq declares a named class or
13293 enumeration type, since that is the only case in which the
13294 init-declarator-list is allowed to be empty.
13295
13296 [dcl.dcl]
13297
13298 In a simple-declaration, the optional init-declarator-list can be
13299 omitted only when declaring a class or enumeration, that is when
13300 the decl-specifier-seq contains either a class-specifier, an
13301 elaborated-type-specifier, or an enum-specifier. */
13302 cp_parser_decl_specifier_seq (parser,
13303 CP_PARSER_FLAGS_OPTIONAL,
13304 &decl_specifiers,
13305 &declares_class_or_enum);
13306 /* We no longer need to defer access checks. */
13307 stop_deferring_access_checks ();
13308
13309 /* In a block scope, a valid declaration must always have a
13310 decl-specifier-seq. By not trying to parse declarators, we can
13311 resolve the declaration/expression ambiguity more quickly. */
13312 if (!function_definition_allowed_p
13313 && !decl_specifiers.any_specifiers_p)
13314 {
13315 cp_parser_error (parser, "expected declaration");
13316 goto done;
13317 }
13318
13319 /* If the next two tokens are both identifiers, the code is
13320 erroneous. The usual cause of this situation is code like:
13321
13322 T t;
13323
13324 where "T" should name a type -- but does not. */
13325 if (!decl_specifiers.any_type_specifiers_p
13326 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13327 {
13328 /* If parsing tentatively, we should commit; we really are
13329 looking at a declaration. */
13330 cp_parser_commit_to_tentative_parse (parser);
13331 /* Give up. */
13332 goto done;
13333 }
13334
13335 cp_parser_maybe_commit_to_declaration (parser,
13336 decl_specifiers.any_specifiers_p);
13337
13338 /* Look for C++17 decomposition declaration. */
13339 for (size_t n = 1; ; n++)
13340 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13341 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13342 continue;
13343 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13344 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13345 && decl_specifiers.any_specifiers_p)
13346 {
13347 tree decl
13348 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13349 maybe_range_for_decl,
13350 &init_loc);
13351
13352 /* The next token should be either a `,' or a `;'. */
13353 cp_token *token = cp_lexer_peek_token (parser->lexer);
13354 /* If it's a `;', we are done. */
13355 if (token->type == CPP_SEMICOLON)
13356 goto finish;
13357 else if (maybe_range_for_decl)
13358 {
13359 if (*maybe_range_for_decl == NULL_TREE)
13360 *maybe_range_for_decl = error_mark_node;
13361 goto finish;
13362 }
13363 /* Anything else is an error. */
13364 else
13365 {
13366 /* If we have already issued an error message we don't need
13367 to issue another one. */
13368 if ((decl != error_mark_node
13369 && DECL_INITIAL (decl) != error_mark_node)
13370 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13371 cp_parser_error (parser, "expected %<,%> or %<;%>");
13372 /* Skip tokens until we reach the end of the statement. */
13373 cp_parser_skip_to_end_of_statement (parser);
13374 /* If the next token is now a `;', consume it. */
13375 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13376 cp_lexer_consume_token (parser->lexer);
13377 goto done;
13378 }
13379 }
13380 else
13381 break;
13382
13383 tree last_type;
13384 bool auto_specifier_p;
13385 /* NULL_TREE if both variable and function declaration are allowed,
13386 error_mark_node if function declaration are not allowed and
13387 a FUNCTION_DECL that should be diagnosed if it is followed by
13388 variable declarations. */
13389 tree auto_function_declaration;
13390
13391 last_type = NULL_TREE;
13392 auto_specifier_p
13393 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13394 auto_function_declaration = NULL_TREE;
13395
13396 /* Keep going until we hit the `;' at the end of the simple
13397 declaration. */
13398 saw_declarator = false;
13399 while (cp_lexer_next_token_is_not (parser->lexer,
13400 CPP_SEMICOLON))
13401 {
13402 cp_token *token;
13403 bool function_definition_p;
13404 tree decl;
13405 tree auto_result = NULL_TREE;
13406
13407 if (saw_declarator)
13408 {
13409 /* If we are processing next declarator, comma is expected */
13410 token = cp_lexer_peek_token (parser->lexer);
13411 gcc_assert (token->type == CPP_COMMA);
13412 cp_lexer_consume_token (parser->lexer);
13413 if (maybe_range_for_decl)
13414 {
13415 *maybe_range_for_decl = error_mark_node;
13416 if (comma_loc == UNKNOWN_LOCATION)
13417 comma_loc = token->location;
13418 }
13419 }
13420 else
13421 saw_declarator = true;
13422
13423 /* Parse the init-declarator. */
13424 decl = cp_parser_init_declarator (parser,
13425 CP_PARSER_FLAGS_NONE,
13426 &decl_specifiers,
13427 /*checks=*/NULL,
13428 function_definition_allowed_p,
13429 /*member_p=*/false,
13430 declares_class_or_enum,
13431 &function_definition_p,
13432 maybe_range_for_decl,
13433 &init_loc,
13434 &auto_result);
13435 /* If an error occurred while parsing tentatively, exit quickly.
13436 (That usually happens when in the body of a function; each
13437 statement is treated as a declaration-statement until proven
13438 otherwise.) */
13439 if (cp_parser_error_occurred (parser))
13440 goto done;
13441
13442 if (auto_specifier_p && cxx_dialect >= cxx14)
13443 {
13444 /* If the init-declarator-list contains more than one
13445 init-declarator, they shall all form declarations of
13446 variables. */
13447 if (auto_function_declaration == NULL_TREE)
13448 auto_function_declaration
13449 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13450 else if (TREE_CODE (decl) == FUNCTION_DECL
13451 || auto_function_declaration != error_mark_node)
13452 {
13453 error_at (decl_specifiers.locations[ds_type_spec],
13454 "non-variable %qD in declaration with more than one "
13455 "declarator with placeholder type",
13456 TREE_CODE (decl) == FUNCTION_DECL
13457 ? decl : auto_function_declaration);
13458 auto_function_declaration = error_mark_node;
13459 }
13460 }
13461
13462 if (auto_result
13463 && (!processing_template_decl || !type_uses_auto (auto_result)))
13464 {
13465 if (last_type
13466 && last_type != error_mark_node
13467 && !same_type_p (auto_result, last_type))
13468 {
13469 /* If the list of declarators contains more than one declarator,
13470 the type of each declared variable is determined as described
13471 above. If the type deduced for the template parameter U is not
13472 the same in each deduction, the program is ill-formed. */
13473 error_at (decl_specifiers.locations[ds_type_spec],
13474 "inconsistent deduction for %qT: %qT and then %qT",
13475 decl_specifiers.type, last_type, auto_result);
13476 last_type = error_mark_node;
13477 }
13478 else
13479 last_type = auto_result;
13480 }
13481
13482 /* Handle function definitions specially. */
13483 if (function_definition_p)
13484 {
13485 /* If the next token is a `,', then we are probably
13486 processing something like:
13487
13488 void f() {}, *p;
13489
13490 which is erroneous. */
13491 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13492 {
13493 cp_token *token = cp_lexer_peek_token (parser->lexer);
13494 error_at (token->location,
13495 "mixing"
13496 " declarations and function-definitions is forbidden");
13497 }
13498 /* Otherwise, we're done with the list of declarators. */
13499 else
13500 {
13501 pop_deferring_access_checks ();
13502 return;
13503 }
13504 }
13505 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13506 *maybe_range_for_decl = decl;
13507 /* The next token should be either a `,' or a `;'. */
13508 token = cp_lexer_peek_token (parser->lexer);
13509 /* If it's a `,', there are more declarators to come. */
13510 if (token->type == CPP_COMMA)
13511 /* will be consumed next time around */;
13512 /* If it's a `;', we are done. */
13513 else if (token->type == CPP_SEMICOLON)
13514 break;
13515 else if (maybe_range_for_decl)
13516 {
13517 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13518 permerror (decl_specifiers.locations[ds_type_spec],
13519 "types may not be defined in a for-range-declaration");
13520 break;
13521 }
13522 /* Anything else is an error. */
13523 else
13524 {
13525 /* If we have already issued an error message we don't need
13526 to issue another one. */
13527 if ((decl != error_mark_node
13528 && DECL_INITIAL (decl) != error_mark_node)
13529 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13530 cp_parser_error (parser, "expected %<,%> or %<;%>");
13531 /* Skip tokens until we reach the end of the statement. */
13532 cp_parser_skip_to_end_of_statement (parser);
13533 /* If the next token is now a `;', consume it. */
13534 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13535 cp_lexer_consume_token (parser->lexer);
13536 goto done;
13537 }
13538 /* After the first time around, a function-definition is not
13539 allowed -- even if it was OK at first. For example:
13540
13541 int i, f() {}
13542
13543 is not valid. */
13544 function_definition_allowed_p = false;
13545 }
13546
13547 /* Issue an error message if no declarators are present, and the
13548 decl-specifier-seq does not itself declare a class or
13549 enumeration: [dcl.dcl]/3. */
13550 if (!saw_declarator)
13551 {
13552 if (cp_parser_declares_only_class_p (parser))
13553 {
13554 if (!declares_class_or_enum
13555 && decl_specifiers.type
13556 && OVERLOAD_TYPE_P (decl_specifiers.type))
13557 /* Ensure an error is issued anyway when finish_decltype_type,
13558 called via cp_parser_decl_specifier_seq, returns a class or
13559 an enumeration (c++/51786). */
13560 decl_specifiers.type = NULL_TREE;
13561 shadow_tag (&decl_specifiers);
13562 }
13563 /* Perform any deferred access checks. */
13564 perform_deferred_access_checks (tf_warning_or_error);
13565 }
13566
13567 /* Consume the `;'. */
13568 finish:
13569 if (!maybe_range_for_decl)
13570 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13571 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13572 {
13573 if (init_loc != UNKNOWN_LOCATION)
13574 error_at (init_loc, "initializer in range-based %<for%> loop");
13575 if (comma_loc != UNKNOWN_LOCATION)
13576 error_at (comma_loc,
13577 "multiple declarations in range-based %<for%> loop");
13578 }
13579
13580 done:
13581 pop_deferring_access_checks ();
13582 }
13583
13584 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13585 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13586 initializer ; */
13587
13588 static tree
13589 cp_parser_decomposition_declaration (cp_parser *parser,
13590 cp_decl_specifier_seq *decl_specifiers,
13591 tree *maybe_range_for_decl,
13592 location_t *init_loc)
13593 {
13594 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13595 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13596 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13597
13598 /* Parse the identifier-list. */
13599 auto_vec<cp_expr, 10> v;
13600 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13601 while (true)
13602 {
13603 cp_expr e = cp_parser_identifier (parser);
13604 if (e.get_value () == error_mark_node)
13605 break;
13606 v.safe_push (e);
13607 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13608 break;
13609 cp_lexer_consume_token (parser->lexer);
13610 }
13611
13612 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13613 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13614 {
13615 end_loc = UNKNOWN_LOCATION;
13616 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13617 false);
13618 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13619 cp_lexer_consume_token (parser->lexer);
13620 else
13621 {
13622 cp_parser_skip_to_end_of_statement (parser);
13623 return error_mark_node;
13624 }
13625 }
13626
13627 if (cxx_dialect < cxx17)
13628 pedwarn (loc, 0, "structured bindings only available with "
13629 "-std=c++17 or -std=gnu++17");
13630
13631 tree pushed_scope;
13632 cp_declarator *declarator = make_declarator (cdk_decomp);
13633 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13634 declarator->id_loc = loc;
13635 if (ref_qual != REF_QUAL_NONE)
13636 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13637 ref_qual == REF_QUAL_RVALUE,
13638 NULL_TREE);
13639 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13640 NULL_TREE, decl_specifiers->attributes,
13641 &pushed_scope);
13642 tree orig_decl = decl;
13643
13644 unsigned int i;
13645 cp_expr e;
13646 cp_decl_specifier_seq decl_specs;
13647 clear_decl_specs (&decl_specs);
13648 decl_specs.type = make_auto ();
13649 tree prev = decl;
13650 FOR_EACH_VEC_ELT (v, i, e)
13651 {
13652 if (i == 0)
13653 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13654 sfk_none, e.get_location ());
13655 else
13656 {
13657 declarator->u.id.unqualified_name = e.get_value ();
13658 declarator->id_loc = e.get_location ();
13659 }
13660 tree elt_pushed_scope;
13661 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13662 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13663 if (decl2 == error_mark_node)
13664 decl = error_mark_node;
13665 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13666 {
13667 /* Ensure we've diagnosed redeclaration if we aren't creating
13668 a new VAR_DECL. */
13669 gcc_assert (errorcount);
13670 decl = error_mark_node;
13671 }
13672 else
13673 prev = decl2;
13674 if (elt_pushed_scope)
13675 pop_scope (elt_pushed_scope);
13676 }
13677
13678 if (v.is_empty ())
13679 {
13680 error_at (loc, "empty structured binding declaration");
13681 decl = error_mark_node;
13682 }
13683
13684 if (maybe_range_for_decl == NULL
13685 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13686 {
13687 bool non_constant_p = false, is_direct_init = false;
13688 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13689 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13690 &non_constant_p);
13691 if (initializer == NULL_TREE
13692 || (TREE_CODE (initializer) == TREE_LIST
13693 && TREE_CHAIN (initializer))
13694 || (is_direct_init
13695 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13696 && CONSTRUCTOR_NELTS (initializer) != 1))
13697 {
13698 error_at (loc, "invalid initializer for structured binding "
13699 "declaration");
13700 initializer = error_mark_node;
13701 }
13702
13703 if (decl != error_mark_node)
13704 {
13705 cp_maybe_mangle_decomp (decl, prev, v.length ());
13706 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13707 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13708 cp_finish_decomp (decl, prev, v.length ());
13709 }
13710 }
13711 else if (decl != error_mark_node)
13712 {
13713 *maybe_range_for_decl = prev;
13714 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13715 the underlying DECL. */
13716 cp_finish_decomp (decl, prev, v.length ());
13717 }
13718
13719 if (pushed_scope)
13720 pop_scope (pushed_scope);
13721
13722 if (decl == error_mark_node && DECL_P (orig_decl))
13723 {
13724 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13725 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13726 }
13727
13728 return decl;
13729 }
13730
13731 /* Parse a decl-specifier-seq.
13732
13733 decl-specifier-seq:
13734 decl-specifier-seq [opt] decl-specifier
13735 decl-specifier attribute-specifier-seq [opt] (C++11)
13736
13737 decl-specifier:
13738 storage-class-specifier
13739 type-specifier
13740 function-specifier
13741 friend
13742 typedef
13743
13744 GNU Extension:
13745
13746 decl-specifier:
13747 attributes
13748
13749 Concepts Extension:
13750
13751 decl-specifier:
13752 concept
13753
13754 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13755
13756 The parser flags FLAGS is used to control type-specifier parsing.
13757
13758 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13759 flags:
13760
13761 1: one of the decl-specifiers is an elaborated-type-specifier
13762 (i.e., a type declaration)
13763 2: one of the decl-specifiers is an enum-specifier or a
13764 class-specifier (i.e., a type definition)
13765
13766 */
13767
13768 static void
13769 cp_parser_decl_specifier_seq (cp_parser* parser,
13770 cp_parser_flags flags,
13771 cp_decl_specifier_seq *decl_specs,
13772 int* declares_class_or_enum)
13773 {
13774 bool constructor_possible_p = !parser->in_declarator_p;
13775 bool found_decl_spec = false;
13776 cp_token *start_token = NULL;
13777 cp_decl_spec ds;
13778
13779 /* Clear DECL_SPECS. */
13780 clear_decl_specs (decl_specs);
13781
13782 /* Assume no class or enumeration type is declared. */
13783 *declares_class_or_enum = 0;
13784
13785 /* Keep reading specifiers until there are no more to read. */
13786 while (true)
13787 {
13788 bool constructor_p;
13789 cp_token *token;
13790 ds = ds_last;
13791
13792 /* Peek at the next token. */
13793 token = cp_lexer_peek_token (parser->lexer);
13794
13795 /* Save the first token of the decl spec list for error
13796 reporting. */
13797 if (!start_token)
13798 start_token = token;
13799 /* Handle attributes. */
13800 if (cp_next_tokens_can_be_attribute_p (parser))
13801 {
13802 /* Parse the attributes. */
13803 tree attrs = cp_parser_attributes_opt (parser);
13804
13805 /* In a sequence of declaration specifiers, c++11 attributes
13806 appertain to the type that precede them. In that case
13807 [dcl.spec]/1 says:
13808
13809 The attribute-specifier-seq affects the type only for
13810 the declaration it appears in, not other declarations
13811 involving the same type.
13812
13813 But for now let's force the user to position the
13814 attribute either at the beginning of the declaration or
13815 after the declarator-id, which would clearly mean that it
13816 applies to the declarator. */
13817 if (cxx11_attribute_p (attrs))
13818 {
13819 if (!found_decl_spec)
13820 /* The c++11 attribute is at the beginning of the
13821 declaration. It appertains to the entity being
13822 declared. */;
13823 else
13824 {
13825 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13826 {
13827 /* This is an attribute following a
13828 class-specifier. */
13829 if (decl_specs->type_definition_p)
13830 warn_misplaced_attr_for_class_type (token->location,
13831 decl_specs->type);
13832 attrs = NULL_TREE;
13833 }
13834 else
13835 {
13836 decl_specs->std_attributes
13837 = attr_chainon (decl_specs->std_attributes, attrs);
13838 if (decl_specs->locations[ds_std_attribute] == 0)
13839 decl_specs->locations[ds_std_attribute] = token->location;
13840 }
13841 continue;
13842 }
13843 }
13844
13845 decl_specs->attributes
13846 = attr_chainon (decl_specs->attributes, attrs);
13847 if (decl_specs->locations[ds_attribute] == 0)
13848 decl_specs->locations[ds_attribute] = token->location;
13849 continue;
13850 }
13851 /* Assume we will find a decl-specifier keyword. */
13852 found_decl_spec = true;
13853 /* If the next token is an appropriate keyword, we can simply
13854 add it to the list. */
13855 switch (token->keyword)
13856 {
13857 /* decl-specifier:
13858 friend
13859 constexpr */
13860 case RID_FRIEND:
13861 if (!at_class_scope_p ())
13862 {
13863 gcc_rich_location richloc (token->location);
13864 richloc.add_fixit_remove ();
13865 error_at (&richloc, "%<friend%> used outside of class");
13866 cp_lexer_purge_token (parser->lexer);
13867 }
13868 else
13869 {
13870 ds = ds_friend;
13871 /* Consume the token. */
13872 cp_lexer_consume_token (parser->lexer);
13873 }
13874 break;
13875
13876 case RID_CONSTEXPR:
13877 ds = ds_constexpr;
13878 cp_lexer_consume_token (parser->lexer);
13879 break;
13880
13881 case RID_CONCEPT:
13882 ds = ds_concept;
13883 cp_lexer_consume_token (parser->lexer);
13884 break;
13885
13886 /* function-specifier:
13887 inline
13888 virtual
13889 explicit */
13890 case RID_INLINE:
13891 case RID_VIRTUAL:
13892 case RID_EXPLICIT:
13893 cp_parser_function_specifier_opt (parser, decl_specs);
13894 break;
13895
13896 /* decl-specifier:
13897 typedef */
13898 case RID_TYPEDEF:
13899 ds = ds_typedef;
13900 /* Consume the token. */
13901 cp_lexer_consume_token (parser->lexer);
13902 /* A constructor declarator cannot appear in a typedef. */
13903 constructor_possible_p = false;
13904 /* The "typedef" keyword can only occur in a declaration; we
13905 may as well commit at this point. */
13906 cp_parser_commit_to_tentative_parse (parser);
13907
13908 if (decl_specs->storage_class != sc_none)
13909 decl_specs->conflicting_specifiers_p = true;
13910 break;
13911
13912 /* storage-class-specifier:
13913 auto
13914 register
13915 static
13916 extern
13917 mutable
13918
13919 GNU Extension:
13920 thread */
13921 case RID_AUTO:
13922 if (cxx_dialect == cxx98)
13923 {
13924 /* Consume the token. */
13925 cp_lexer_consume_token (parser->lexer);
13926
13927 /* Complain about `auto' as a storage specifier, if
13928 we're complaining about C++0x compatibility. */
13929 gcc_rich_location richloc (token->location);
13930 richloc.add_fixit_remove ();
13931 warning_at (&richloc, OPT_Wc__11_compat,
13932 "%<auto%> changes meaning in C++11; "
13933 "please remove it");
13934
13935 /* Set the storage class anyway. */
13936 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13937 token);
13938 }
13939 else
13940 /* C++0x auto type-specifier. */
13941 found_decl_spec = false;
13942 break;
13943
13944 case RID_REGISTER:
13945 case RID_STATIC:
13946 case RID_EXTERN:
13947 case RID_MUTABLE:
13948 /* Consume the token. */
13949 cp_lexer_consume_token (parser->lexer);
13950 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13951 token);
13952 break;
13953 case RID_THREAD:
13954 /* Consume the token. */
13955 ds = ds_thread;
13956 cp_lexer_consume_token (parser->lexer);
13957 break;
13958
13959 default:
13960 /* We did not yet find a decl-specifier yet. */
13961 found_decl_spec = false;
13962 break;
13963 }
13964
13965 if (found_decl_spec
13966 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13967 && token->keyword != RID_CONSTEXPR)
13968 error ("decl-specifier invalid in condition");
13969
13970 if (found_decl_spec
13971 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13972 && token->keyword != RID_MUTABLE
13973 && token->keyword != RID_CONSTEXPR)
13974 error_at (token->location, "%qD invalid in lambda",
13975 ridpointers[token->keyword]);
13976
13977 if (ds != ds_last)
13978 set_and_check_decl_spec_loc (decl_specs, ds, token);
13979
13980 /* Constructors are a special case. The `S' in `S()' is not a
13981 decl-specifier; it is the beginning of the declarator. */
13982 constructor_p
13983 = (!found_decl_spec
13984 && constructor_possible_p
13985 && (cp_parser_constructor_declarator_p
13986 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13987
13988 /* If we don't have a DECL_SPEC yet, then we must be looking at
13989 a type-specifier. */
13990 if (!found_decl_spec && !constructor_p)
13991 {
13992 int decl_spec_declares_class_or_enum;
13993 bool is_cv_qualifier;
13994 tree type_spec;
13995
13996 type_spec
13997 = cp_parser_type_specifier (parser, flags,
13998 decl_specs,
13999 /*is_declaration=*/true,
14000 &decl_spec_declares_class_or_enum,
14001 &is_cv_qualifier);
14002 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14003
14004 /* If this type-specifier referenced a user-defined type
14005 (a typedef, class-name, etc.), then we can't allow any
14006 more such type-specifiers henceforth.
14007
14008 [dcl.spec]
14009
14010 The longest sequence of decl-specifiers that could
14011 possibly be a type name is taken as the
14012 decl-specifier-seq of a declaration. The sequence shall
14013 be self-consistent as described below.
14014
14015 [dcl.type]
14016
14017 As a general rule, at most one type-specifier is allowed
14018 in the complete decl-specifier-seq of a declaration. The
14019 only exceptions are the following:
14020
14021 -- const or volatile can be combined with any other
14022 type-specifier.
14023
14024 -- signed or unsigned can be combined with char, long,
14025 short, or int.
14026
14027 -- ..
14028
14029 Example:
14030
14031 typedef char* Pc;
14032 void g (const int Pc);
14033
14034 Here, Pc is *not* part of the decl-specifier seq; it's
14035 the declarator. Therefore, once we see a type-specifier
14036 (other than a cv-qualifier), we forbid any additional
14037 user-defined types. We *do* still allow things like `int
14038 int' to be considered a decl-specifier-seq, and issue the
14039 error message later. */
14040 if (type_spec && !is_cv_qualifier)
14041 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14042 /* A constructor declarator cannot follow a type-specifier. */
14043 if (type_spec)
14044 {
14045 constructor_possible_p = false;
14046 found_decl_spec = true;
14047 if (!is_cv_qualifier)
14048 decl_specs->any_type_specifiers_p = true;
14049
14050 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14051 error_at (token->location, "type-specifier invalid in lambda");
14052 }
14053 }
14054
14055 /* If we still do not have a DECL_SPEC, then there are no more
14056 decl-specifiers. */
14057 if (!found_decl_spec)
14058 break;
14059
14060 decl_specs->any_specifiers_p = true;
14061 /* After we see one decl-specifier, further decl-specifiers are
14062 always optional. */
14063 flags |= CP_PARSER_FLAGS_OPTIONAL;
14064 }
14065
14066 /* Don't allow a friend specifier with a class definition. */
14067 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14068 && (*declares_class_or_enum & 2))
14069 error_at (decl_specs->locations[ds_friend],
14070 "class definition may not be declared a friend");
14071 }
14072
14073 /* Parse an (optional) storage-class-specifier.
14074
14075 storage-class-specifier:
14076 auto
14077 register
14078 static
14079 extern
14080 mutable
14081
14082 GNU Extension:
14083
14084 storage-class-specifier:
14085 thread
14086
14087 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14088
14089 static tree
14090 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14091 {
14092 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14093 {
14094 case RID_AUTO:
14095 if (cxx_dialect != cxx98)
14096 return NULL_TREE;
14097 /* Fall through for C++98. */
14098 gcc_fallthrough ();
14099
14100 case RID_REGISTER:
14101 case RID_STATIC:
14102 case RID_EXTERN:
14103 case RID_MUTABLE:
14104 case RID_THREAD:
14105 /* Consume the token. */
14106 return cp_lexer_consume_token (parser->lexer)->u.value;
14107
14108 default:
14109 return NULL_TREE;
14110 }
14111 }
14112
14113 /* Parse an (optional) function-specifier.
14114
14115 function-specifier:
14116 inline
14117 virtual
14118 explicit
14119
14120 C++2A Extension:
14121 explicit(constant-expression)
14122
14123 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14124 Updates DECL_SPECS, if it is non-NULL. */
14125
14126 static tree
14127 cp_parser_function_specifier_opt (cp_parser* parser,
14128 cp_decl_specifier_seq *decl_specs)
14129 {
14130 cp_token *token = cp_lexer_peek_token (parser->lexer);
14131 switch (token->keyword)
14132 {
14133 case RID_INLINE:
14134 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14135 break;
14136
14137 case RID_VIRTUAL:
14138 /* 14.5.2.3 [temp.mem]
14139
14140 A member function template shall not be virtual. */
14141 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14142 && current_class_type)
14143 error_at (token->location, "templates may not be %<virtual%>");
14144 else
14145 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14146 break;
14147
14148 case RID_EXPLICIT:
14149 {
14150 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14151 /* If we see '(', it's C++20 explicit(bool). */
14152 tree expr;
14153 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14154 {
14155 matching_parens parens;
14156 parens.consume_open (parser);
14157
14158 /* New types are not allowed in an explicit-specifier. */
14159 const char *saved_message
14160 = parser->type_definition_forbidden_message;
14161 parser->type_definition_forbidden_message
14162 = G_("types may not be defined in explicit-specifier");
14163
14164 if (cxx_dialect < cxx2a)
14165 pedwarn (token->location, 0,
14166 "%<explicit(bool)%> only available with -std=c++2a "
14167 "or -std=gnu++2a");
14168
14169 /* Parse the constant-expression. */
14170 expr = cp_parser_constant_expression (parser);
14171
14172 /* Restore the saved message. */
14173 parser->type_definition_forbidden_message = saved_message;
14174 parens.require_close (parser);
14175 }
14176 else
14177 /* The explicit-specifier explicit without a constant-expression is
14178 equivalent to the explicit-specifier explicit(true). */
14179 expr = boolean_true_node;
14180
14181 /* [dcl.fct.spec]
14182 "the constant-expression, if supplied, shall be a contextually
14183 converted constant expression of type bool." */
14184 expr = build_explicit_specifier (expr, tf_warning_or_error);
14185 /* We could evaluate it -- mark the decl as appropriate. */
14186 if (expr == boolean_true_node)
14187 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14188 else if (expr == boolean_false_node)
14189 /* Don't mark the decl as explicit. */;
14190 else if (decl_specs)
14191 /* The expression was value-dependent. Remember it so that we can
14192 substitute it later. */
14193 decl_specs->explicit_specifier = expr;
14194 return id;
14195 }
14196
14197 default:
14198 return NULL_TREE;
14199 }
14200
14201 /* Consume the token. */
14202 return cp_lexer_consume_token (parser->lexer)->u.value;
14203 }
14204
14205 /* Parse a linkage-specification.
14206
14207 linkage-specification:
14208 extern string-literal { declaration-seq [opt] }
14209 extern string-literal declaration */
14210
14211 static void
14212 cp_parser_linkage_specification (cp_parser* parser)
14213 {
14214 tree linkage;
14215
14216 /* Look for the `extern' keyword. */
14217 cp_token *extern_token
14218 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14219
14220 /* Look for the string-literal. */
14221 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14222 linkage = cp_parser_string_literal (parser, false, false);
14223
14224 /* Transform the literal into an identifier. If the literal is a
14225 wide-character string, or contains embedded NULs, then we can't
14226 handle it as the user wants. */
14227 if (strlen (TREE_STRING_POINTER (linkage))
14228 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14229 {
14230 cp_parser_error (parser, "invalid linkage-specification");
14231 /* Assume C++ linkage. */
14232 linkage = lang_name_cplusplus;
14233 }
14234 else
14235 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14236
14237 /* We're now using the new linkage. */
14238 push_lang_context (linkage);
14239
14240 /* Preserve the location of the the innermost linkage specification,
14241 tracking the locations of nested specifications via a local. */
14242 location_t saved_location
14243 = parser->innermost_linkage_specification_location;
14244 /* Construct a location ranging from the start of the "extern" to
14245 the end of the string-literal, with the caret at the start, e.g.:
14246 extern "C" {
14247 ^~~~~~~~~~
14248 */
14249 parser->innermost_linkage_specification_location
14250 = make_location (extern_token->location,
14251 extern_token->location,
14252 get_finish (string_token->location));
14253
14254 /* If the next token is a `{', then we're using the first
14255 production. */
14256 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14257 {
14258 cp_ensure_no_omp_declare_simd (parser);
14259 cp_ensure_no_oacc_routine (parser);
14260
14261 /* Consume the `{' token. */
14262 matching_braces braces;
14263 braces.consume_open (parser);
14264 /* Parse the declarations. */
14265 cp_parser_declaration_seq_opt (parser);
14266 /* Look for the closing `}'. */
14267 braces.require_close (parser);
14268 }
14269 /* Otherwise, there's just one declaration. */
14270 else
14271 {
14272 bool saved_in_unbraced_linkage_specification_p;
14273
14274 saved_in_unbraced_linkage_specification_p
14275 = parser->in_unbraced_linkage_specification_p;
14276 parser->in_unbraced_linkage_specification_p = true;
14277 cp_parser_declaration (parser);
14278 parser->in_unbraced_linkage_specification_p
14279 = saved_in_unbraced_linkage_specification_p;
14280 }
14281
14282 /* We're done with the linkage-specification. */
14283 pop_lang_context ();
14284
14285 /* Restore location of parent linkage specification, if any. */
14286 parser->innermost_linkage_specification_location = saved_location;
14287 }
14288
14289 /* Parse a static_assert-declaration.
14290
14291 static_assert-declaration:
14292 static_assert ( constant-expression , string-literal ) ;
14293 static_assert ( constant-expression ) ; (C++17)
14294
14295 If MEMBER_P, this static_assert is a class member. */
14296
14297 static void
14298 cp_parser_static_assert(cp_parser *parser, bool member_p)
14299 {
14300 cp_expr condition;
14301 location_t token_loc;
14302 tree message;
14303 bool dummy;
14304
14305 /* Peek at the `static_assert' token so we can keep track of exactly
14306 where the static assertion started. */
14307 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14308
14309 /* Look for the `static_assert' keyword. */
14310 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14311 RT_STATIC_ASSERT))
14312 return;
14313
14314 /* We know we are in a static assertion; commit to any tentative
14315 parse. */
14316 if (cp_parser_parsing_tentatively (parser))
14317 cp_parser_commit_to_tentative_parse (parser);
14318
14319 /* Parse the `(' starting the static assertion condition. */
14320 matching_parens parens;
14321 parens.require_open (parser);
14322
14323 /* Parse the constant-expression. Allow a non-constant expression
14324 here in order to give better diagnostics in finish_static_assert. */
14325 condition =
14326 cp_parser_constant_expression (parser,
14327 /*allow_non_constant_p=*/true,
14328 /*non_constant_p=*/&dummy);
14329
14330 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14331 {
14332 if (cxx_dialect < cxx17)
14333 pedwarn (input_location, OPT_Wpedantic,
14334 "static_assert without a message "
14335 "only available with -std=c++17 or -std=gnu++17");
14336 /* Eat the ')' */
14337 cp_lexer_consume_token (parser->lexer);
14338 message = build_string (1, "");
14339 TREE_TYPE (message) = char_array_type_node;
14340 fix_string_type (message);
14341 }
14342 else
14343 {
14344 /* Parse the separating `,'. */
14345 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14346
14347 /* Parse the string-literal message. */
14348 message = cp_parser_string_literal (parser,
14349 /*translate=*/false,
14350 /*wide_ok=*/true);
14351
14352 /* A `)' completes the static assertion. */
14353 if (!parens.require_close (parser))
14354 cp_parser_skip_to_closing_parenthesis (parser,
14355 /*recovering=*/true,
14356 /*or_comma=*/false,
14357 /*consume_paren=*/true);
14358 }
14359
14360 /* A semicolon terminates the declaration. */
14361 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14362
14363 /* Get the location for the static assertion. Use that of the
14364 condition if available, otherwise, use that of the "static_assert"
14365 token. */
14366 location_t assert_loc = condition.get_location ();
14367 if (assert_loc == UNKNOWN_LOCATION)
14368 assert_loc = token_loc;
14369
14370 /* Complete the static assertion, which may mean either processing
14371 the static assert now or saving it for template instantiation. */
14372 finish_static_assert (condition, message, assert_loc, member_p);
14373 }
14374
14375 /* Parse the expression in decltype ( expression ). */
14376
14377 static tree
14378 cp_parser_decltype_expr (cp_parser *parser,
14379 bool &id_expression_or_member_access_p)
14380 {
14381 cp_token *id_expr_start_token;
14382 tree expr;
14383
14384 /* Since we're going to preserve any side-effects from this parse, set up a
14385 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14386 in the expression. */
14387 tentative_firewall firewall (parser);
14388
14389 /* First, try parsing an id-expression. */
14390 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14391 cp_parser_parse_tentatively (parser);
14392 expr = cp_parser_id_expression (parser,
14393 /*template_keyword_p=*/false,
14394 /*check_dependency_p=*/true,
14395 /*template_p=*/NULL,
14396 /*declarator_p=*/false,
14397 /*optional_p=*/false);
14398
14399 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14400 {
14401 bool non_integral_constant_expression_p = false;
14402 tree id_expression = expr;
14403 cp_id_kind idk;
14404 const char *error_msg;
14405
14406 if (identifier_p (expr))
14407 /* Lookup the name we got back from the id-expression. */
14408 expr = cp_parser_lookup_name_simple (parser, expr,
14409 id_expr_start_token->location);
14410
14411 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14412 /* A template without args is not a complete id-expression. */
14413 expr = error_mark_node;
14414
14415 if (expr
14416 && expr != error_mark_node
14417 && TREE_CODE (expr) != TYPE_DECL
14418 && (TREE_CODE (expr) != BIT_NOT_EXPR
14419 || !TYPE_P (TREE_OPERAND (expr, 0)))
14420 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14421 {
14422 /* Complete lookup of the id-expression. */
14423 expr = (finish_id_expression
14424 (id_expression, expr, parser->scope, &idk,
14425 /*integral_constant_expression_p=*/false,
14426 /*allow_non_integral_constant_expression_p=*/true,
14427 &non_integral_constant_expression_p,
14428 /*template_p=*/false,
14429 /*done=*/true,
14430 /*address_p=*/false,
14431 /*template_arg_p=*/false,
14432 &error_msg,
14433 id_expr_start_token->location));
14434
14435 if (expr == error_mark_node)
14436 /* We found an id-expression, but it was something that we
14437 should not have found. This is an error, not something
14438 we can recover from, so note that we found an
14439 id-expression and we'll recover as gracefully as
14440 possible. */
14441 id_expression_or_member_access_p = true;
14442 }
14443
14444 if (expr
14445 && expr != error_mark_node
14446 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14447 /* We have an id-expression. */
14448 id_expression_or_member_access_p = true;
14449 }
14450
14451 if (!id_expression_or_member_access_p)
14452 {
14453 /* Abort the id-expression parse. */
14454 cp_parser_abort_tentative_parse (parser);
14455
14456 /* Parsing tentatively, again. */
14457 cp_parser_parse_tentatively (parser);
14458
14459 /* Parse a class member access. */
14460 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14461 /*cast_p=*/false, /*decltype*/true,
14462 /*member_access_only_p=*/true, NULL);
14463
14464 if (expr
14465 && expr != error_mark_node
14466 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14467 /* We have an id-expression. */
14468 id_expression_or_member_access_p = true;
14469 }
14470
14471 if (id_expression_or_member_access_p)
14472 /* We have parsed the complete id-expression or member access. */
14473 cp_parser_parse_definitely (parser);
14474 else
14475 {
14476 /* Abort our attempt to parse an id-expression or member access
14477 expression. */
14478 cp_parser_abort_tentative_parse (parser);
14479
14480 /* Commit to the tentative_firewall so we get syntax errors. */
14481 cp_parser_commit_to_tentative_parse (parser);
14482
14483 /* Parse a full expression. */
14484 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14485 /*decltype_p=*/true);
14486 }
14487
14488 return expr;
14489 }
14490
14491 /* Parse a `decltype' type. Returns the type.
14492
14493 simple-type-specifier:
14494 decltype ( expression )
14495 C++14 proposal:
14496 decltype ( auto ) */
14497
14498 static tree
14499 cp_parser_decltype (cp_parser *parser)
14500 {
14501 bool id_expression_or_member_access_p = false;
14502 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14503
14504 if (start_token->type == CPP_DECLTYPE)
14505 {
14506 /* Already parsed. */
14507 cp_lexer_consume_token (parser->lexer);
14508 return saved_checks_value (start_token->u.tree_check_value);
14509 }
14510
14511 /* Look for the `decltype' token. */
14512 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14513 return error_mark_node;
14514
14515 /* Parse the opening `('. */
14516 matching_parens parens;
14517 if (!parens.require_open (parser))
14518 return error_mark_node;
14519
14520 push_deferring_access_checks (dk_deferred);
14521
14522 tree expr = NULL_TREE;
14523
14524 if (cxx_dialect >= cxx14
14525 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14526 /* decltype (auto) */
14527 cp_lexer_consume_token (parser->lexer);
14528 else
14529 {
14530 /* decltype (expression) */
14531
14532 /* Types cannot be defined in a `decltype' expression. Save away the
14533 old message and set the new one. */
14534 const char *saved_message = parser->type_definition_forbidden_message;
14535 parser->type_definition_forbidden_message
14536 = G_("types may not be defined in %<decltype%> expressions");
14537
14538 /* The restrictions on constant-expressions do not apply inside
14539 decltype expressions. */
14540 bool saved_integral_constant_expression_p
14541 = parser->integral_constant_expression_p;
14542 bool saved_non_integral_constant_expression_p
14543 = parser->non_integral_constant_expression_p;
14544 parser->integral_constant_expression_p = false;
14545
14546 /* Within a parenthesized expression, a `>' token is always
14547 the greater-than operator. */
14548 bool saved_greater_than_is_operator_p
14549 = parser->greater_than_is_operator_p;
14550 parser->greater_than_is_operator_p = true;
14551
14552 /* Do not actually evaluate the expression. */
14553 ++cp_unevaluated_operand;
14554
14555 /* Do not warn about problems with the expression. */
14556 ++c_inhibit_evaluation_warnings;
14557
14558 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14559 STRIP_ANY_LOCATION_WRAPPER (expr);
14560
14561 /* Go back to evaluating expressions. */
14562 --cp_unevaluated_operand;
14563 --c_inhibit_evaluation_warnings;
14564
14565 /* The `>' token might be the end of a template-id or
14566 template-parameter-list now. */
14567 parser->greater_than_is_operator_p
14568 = saved_greater_than_is_operator_p;
14569
14570 /* Restore the old message and the integral constant expression
14571 flags. */
14572 parser->type_definition_forbidden_message = saved_message;
14573 parser->integral_constant_expression_p
14574 = saved_integral_constant_expression_p;
14575 parser->non_integral_constant_expression_p
14576 = saved_non_integral_constant_expression_p;
14577 }
14578
14579 /* Parse to the closing `)'. */
14580 if (!parens.require_close (parser))
14581 {
14582 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14583 /*consume_paren=*/true);
14584 pop_deferring_access_checks ();
14585 return error_mark_node;
14586 }
14587
14588 if (!expr)
14589 {
14590 /* Build auto. */
14591 expr = make_decltype_auto ();
14592 AUTO_IS_DECLTYPE (expr) = true;
14593 }
14594 else
14595 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14596 tf_warning_or_error);
14597
14598 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14599 it again. */
14600 start_token->type = CPP_DECLTYPE;
14601 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14602 start_token->u.tree_check_value->value = expr;
14603 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14604 start_token->keyword = RID_MAX;
14605 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14606
14607 pop_to_parent_deferring_access_checks ();
14608
14609 return expr;
14610 }
14611
14612 /* Special member functions [gram.special] */
14613
14614 /* Parse a conversion-function-id.
14615
14616 conversion-function-id:
14617 operator conversion-type-id
14618
14619 Returns an IDENTIFIER_NODE representing the operator. */
14620
14621 static tree
14622 cp_parser_conversion_function_id (cp_parser* parser)
14623 {
14624 tree type;
14625 tree saved_scope;
14626 tree saved_qualifying_scope;
14627 tree saved_object_scope;
14628 tree pushed_scope = NULL_TREE;
14629
14630 /* Look for the `operator' token. */
14631 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14632 return error_mark_node;
14633 /* When we parse the conversion-type-id, the current scope will be
14634 reset. However, we need that information in able to look up the
14635 conversion function later, so we save it here. */
14636 saved_scope = parser->scope;
14637 saved_qualifying_scope = parser->qualifying_scope;
14638 saved_object_scope = parser->object_scope;
14639 /* We must enter the scope of the class so that the names of
14640 entities declared within the class are available in the
14641 conversion-type-id. For example, consider:
14642
14643 struct S {
14644 typedef int I;
14645 operator I();
14646 };
14647
14648 S::operator I() { ... }
14649
14650 In order to see that `I' is a type-name in the definition, we
14651 must be in the scope of `S'. */
14652 if (saved_scope)
14653 pushed_scope = push_scope (saved_scope);
14654 /* Parse the conversion-type-id. */
14655 type = cp_parser_conversion_type_id (parser);
14656 /* Leave the scope of the class, if any. */
14657 if (pushed_scope)
14658 pop_scope (pushed_scope);
14659 /* Restore the saved scope. */
14660 parser->scope = saved_scope;
14661 parser->qualifying_scope = saved_qualifying_scope;
14662 parser->object_scope = saved_object_scope;
14663 /* If the TYPE is invalid, indicate failure. */
14664 if (type == error_mark_node)
14665 return error_mark_node;
14666 return make_conv_op_name (type);
14667 }
14668
14669 /* Parse a conversion-type-id:
14670
14671 conversion-type-id:
14672 type-specifier-seq conversion-declarator [opt]
14673
14674 Returns the TYPE specified. */
14675
14676 static tree
14677 cp_parser_conversion_type_id (cp_parser* parser)
14678 {
14679 tree attributes;
14680 cp_decl_specifier_seq type_specifiers;
14681 cp_declarator *declarator;
14682 tree type_specified;
14683 const char *saved_message;
14684
14685 /* Parse the attributes. */
14686 attributes = cp_parser_attributes_opt (parser);
14687
14688 saved_message = parser->type_definition_forbidden_message;
14689 parser->type_definition_forbidden_message
14690 = G_("types may not be defined in a conversion-type-id");
14691
14692 /* Parse the type-specifiers. */
14693 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
14694 /*is_declaration=*/false,
14695 /*is_trailing_return=*/false,
14696 &type_specifiers);
14697
14698 parser->type_definition_forbidden_message = saved_message;
14699
14700 /* If that didn't work, stop. */
14701 if (type_specifiers.type == error_mark_node)
14702 return error_mark_node;
14703 /* Parse the conversion-declarator. */
14704 declarator = cp_parser_conversion_declarator_opt (parser);
14705
14706 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14707 /*initialized=*/0, &attributes);
14708 if (attributes)
14709 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14710
14711 /* Don't give this error when parsing tentatively. This happens to
14712 work because we always parse this definitively once. */
14713 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14714 && type_uses_auto (type_specified))
14715 {
14716 if (cxx_dialect < cxx14)
14717 {
14718 error ("invalid use of %<auto%> in conversion operator");
14719 return error_mark_node;
14720 }
14721 else if (template_parm_scope_p ())
14722 warning (0, "use of %<auto%> in member template "
14723 "conversion operator can never be deduced");
14724 }
14725
14726 return type_specified;
14727 }
14728
14729 /* Parse an (optional) conversion-declarator.
14730
14731 conversion-declarator:
14732 ptr-operator conversion-declarator [opt]
14733
14734 */
14735
14736 static cp_declarator *
14737 cp_parser_conversion_declarator_opt (cp_parser* parser)
14738 {
14739 enum tree_code code;
14740 tree class_type, std_attributes = NULL_TREE;
14741 cp_cv_quals cv_quals;
14742
14743 /* We don't know if there's a ptr-operator next, or not. */
14744 cp_parser_parse_tentatively (parser);
14745 /* Try the ptr-operator. */
14746 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14747 &std_attributes);
14748 /* If it worked, look for more conversion-declarators. */
14749 if (cp_parser_parse_definitely (parser))
14750 {
14751 cp_declarator *declarator;
14752
14753 /* Parse another optional declarator. */
14754 declarator = cp_parser_conversion_declarator_opt (parser);
14755
14756 declarator = cp_parser_make_indirect_declarator
14757 (code, class_type, cv_quals, declarator, std_attributes);
14758
14759 return declarator;
14760 }
14761
14762 return NULL;
14763 }
14764
14765 /* Parse an (optional) ctor-initializer.
14766
14767 ctor-initializer:
14768 : mem-initializer-list */
14769
14770 static void
14771 cp_parser_ctor_initializer_opt (cp_parser* parser)
14772 {
14773 /* If the next token is not a `:', then there is no
14774 ctor-initializer. */
14775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14776 {
14777 /* Do default initialization of any bases and members. */
14778 if (DECL_CONSTRUCTOR_P (current_function_decl))
14779 finish_mem_initializers (NULL_TREE);
14780 return;
14781 }
14782
14783 /* Consume the `:' token. */
14784 cp_lexer_consume_token (parser->lexer);
14785 /* And the mem-initializer-list. */
14786 cp_parser_mem_initializer_list (parser);
14787 }
14788
14789 /* Parse a mem-initializer-list.
14790
14791 mem-initializer-list:
14792 mem-initializer ... [opt]
14793 mem-initializer ... [opt] , mem-initializer-list */
14794
14795 static void
14796 cp_parser_mem_initializer_list (cp_parser* parser)
14797 {
14798 tree mem_initializer_list = NULL_TREE;
14799 tree target_ctor = error_mark_node;
14800 cp_token *token = cp_lexer_peek_token (parser->lexer);
14801
14802 /* Let the semantic analysis code know that we are starting the
14803 mem-initializer-list. */
14804 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14805 error_at (token->location,
14806 "only constructors take member initializers");
14807
14808 /* Loop through the list. */
14809 while (true)
14810 {
14811 tree mem_initializer;
14812
14813 token = cp_lexer_peek_token (parser->lexer);
14814 /* Parse the mem-initializer. */
14815 mem_initializer = cp_parser_mem_initializer (parser);
14816 /* If the next token is a `...', we're expanding member initializers. */
14817 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14818 if (ellipsis
14819 || (mem_initializer != error_mark_node
14820 && check_for_bare_parameter_packs (TREE_PURPOSE
14821 (mem_initializer))))
14822 {
14823 /* Consume the `...'. */
14824 if (ellipsis)
14825 cp_lexer_consume_token (parser->lexer);
14826
14827 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14828 can be expanded but members cannot. */
14829 if (mem_initializer != error_mark_node
14830 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14831 {
14832 error_at (token->location,
14833 "cannot expand initializer for member %qD",
14834 TREE_PURPOSE (mem_initializer));
14835 mem_initializer = error_mark_node;
14836 }
14837
14838 /* Construct the pack expansion type. */
14839 if (mem_initializer != error_mark_node)
14840 mem_initializer = make_pack_expansion (mem_initializer);
14841 }
14842 if (target_ctor != error_mark_node
14843 && mem_initializer != error_mark_node)
14844 {
14845 error ("mem-initializer for %qD follows constructor delegation",
14846 TREE_PURPOSE (mem_initializer));
14847 mem_initializer = error_mark_node;
14848 }
14849 /* Look for a target constructor. */
14850 if (mem_initializer != error_mark_node
14851 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14852 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14853 {
14854 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14855 if (mem_initializer_list)
14856 {
14857 error ("constructor delegation follows mem-initializer for %qD",
14858 TREE_PURPOSE (mem_initializer_list));
14859 mem_initializer = error_mark_node;
14860 }
14861 target_ctor = mem_initializer;
14862 }
14863 /* Add it to the list, unless it was erroneous. */
14864 if (mem_initializer != error_mark_node)
14865 {
14866 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14867 mem_initializer_list = mem_initializer;
14868 }
14869 /* If the next token is not a `,', we're done. */
14870 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14871 break;
14872 /* Consume the `,' token. */
14873 cp_lexer_consume_token (parser->lexer);
14874 }
14875
14876 /* Perform semantic analysis. */
14877 if (DECL_CONSTRUCTOR_P (current_function_decl))
14878 finish_mem_initializers (mem_initializer_list);
14879 }
14880
14881 /* Parse a mem-initializer.
14882
14883 mem-initializer:
14884 mem-initializer-id ( expression-list [opt] )
14885 mem-initializer-id braced-init-list
14886
14887 GNU extension:
14888
14889 mem-initializer:
14890 ( expression-list [opt] )
14891
14892 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14893 class) or FIELD_DECL (for a non-static data member) to initialize;
14894 the TREE_VALUE is the expression-list. An empty initialization
14895 list is represented by void_list_node. */
14896
14897 static tree
14898 cp_parser_mem_initializer (cp_parser* parser)
14899 {
14900 tree mem_initializer_id;
14901 tree expression_list;
14902 tree member;
14903 cp_token *token = cp_lexer_peek_token (parser->lexer);
14904
14905 /* Find out what is being initialized. */
14906 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14907 {
14908 permerror (token->location,
14909 "anachronistic old-style base class initializer");
14910 mem_initializer_id = NULL_TREE;
14911 }
14912 else
14913 {
14914 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14915 if (mem_initializer_id == error_mark_node)
14916 return mem_initializer_id;
14917 }
14918 member = expand_member_init (mem_initializer_id);
14919 if (member && !DECL_P (member))
14920 in_base_initializer = 1;
14921
14922 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14923 {
14924 bool expr_non_constant_p;
14925 cp_lexer_set_source_position (parser->lexer);
14926 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14927 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14928 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14929 expression_list = build_tree_list (NULL_TREE, expression_list);
14930 }
14931 else
14932 {
14933 vec<tree, va_gc> *vec;
14934 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14935 /*cast_p=*/false,
14936 /*allow_expansion_p=*/true,
14937 /*non_constant_p=*/NULL,
14938 /*close_paren_loc=*/NULL,
14939 /*wrap_locations_p=*/true);
14940 if (vec == NULL)
14941 return error_mark_node;
14942 expression_list = build_tree_list_vec (vec);
14943 release_tree_vector (vec);
14944 }
14945
14946 if (expression_list == error_mark_node)
14947 return error_mark_node;
14948 if (!expression_list)
14949 expression_list = void_type_node;
14950
14951 in_base_initializer = 0;
14952
14953 return member ? build_tree_list (member, expression_list) : error_mark_node;
14954 }
14955
14956 /* Parse a mem-initializer-id.
14957
14958 mem-initializer-id:
14959 :: [opt] nested-name-specifier [opt] class-name
14960 decltype-specifier (C++11)
14961 identifier
14962
14963 Returns a TYPE indicating the class to be initialized for the first
14964 production (and the second in C++11). Returns an IDENTIFIER_NODE
14965 indicating the data member to be initialized for the last production. */
14966
14967 static tree
14968 cp_parser_mem_initializer_id (cp_parser* parser)
14969 {
14970 bool global_scope_p;
14971 bool nested_name_specifier_p;
14972 bool template_p = false;
14973 tree id;
14974
14975 cp_token *token = cp_lexer_peek_token (parser->lexer);
14976
14977 /* `typename' is not allowed in this context ([temp.res]). */
14978 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14979 {
14980 error_at (token->location,
14981 "keyword %<typename%> not allowed in this context (a qualified "
14982 "member initializer is implicitly a type)");
14983 cp_lexer_consume_token (parser->lexer);
14984 }
14985 /* Look for the optional `::' operator. */
14986 global_scope_p
14987 = (cp_parser_global_scope_opt (parser,
14988 /*current_scope_valid_p=*/false)
14989 != NULL_TREE);
14990 /* Look for the optional nested-name-specifier. The simplest way to
14991 implement:
14992
14993 [temp.res]
14994
14995 The keyword `typename' is not permitted in a base-specifier or
14996 mem-initializer; in these contexts a qualified name that
14997 depends on a template-parameter is implicitly assumed to be a
14998 type name.
14999
15000 is to assume that we have seen the `typename' keyword at this
15001 point. */
15002 nested_name_specifier_p
15003 = (cp_parser_nested_name_specifier_opt (parser,
15004 /*typename_keyword_p=*/true,
15005 /*check_dependency_p=*/true,
15006 /*type_p=*/true,
15007 /*is_declaration=*/true)
15008 != NULL_TREE);
15009 if (nested_name_specifier_p)
15010 template_p = cp_parser_optional_template_keyword (parser);
15011 /* If there is a `::' operator or a nested-name-specifier, then we
15012 are definitely looking for a class-name. */
15013 if (global_scope_p || nested_name_specifier_p)
15014 return cp_parser_class_name (parser,
15015 /*typename_keyword_p=*/true,
15016 /*template_keyword_p=*/template_p,
15017 typename_type,
15018 /*check_dependency_p=*/true,
15019 /*class_head_p=*/false,
15020 /*is_declaration=*/true);
15021 /* Otherwise, we could also be looking for an ordinary identifier. */
15022 cp_parser_parse_tentatively (parser);
15023 if (cp_lexer_next_token_is_decltype (parser->lexer))
15024 /* Try a decltype-specifier. */
15025 id = cp_parser_decltype (parser);
15026 else
15027 /* Otherwise, try a class-name. */
15028 id = cp_parser_class_name (parser,
15029 /*typename_keyword_p=*/true,
15030 /*template_keyword_p=*/false,
15031 none_type,
15032 /*check_dependency_p=*/true,
15033 /*class_head_p=*/false,
15034 /*is_declaration=*/true);
15035 /* If we found one, we're done. */
15036 if (cp_parser_parse_definitely (parser))
15037 return id;
15038 /* Otherwise, look for an ordinary identifier. */
15039 return cp_parser_identifier (parser);
15040 }
15041
15042 /* Overloading [gram.over] */
15043
15044 /* Parse an operator-function-id.
15045
15046 operator-function-id:
15047 operator operator
15048
15049 Returns an IDENTIFIER_NODE for the operator which is a
15050 human-readable spelling of the identifier, e.g., `operator +'. */
15051
15052 static cp_expr
15053 cp_parser_operator_function_id (cp_parser* parser)
15054 {
15055 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15056 /* Look for the `operator' keyword. */
15057 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15058 return error_mark_node;
15059 /* And then the name of the operator itself. */
15060 return cp_parser_operator (parser, start_loc);
15061 }
15062
15063 /* Return an identifier node for a user-defined literal operator.
15064 The suffix identifier is chained to the operator name identifier. */
15065
15066 tree
15067 cp_literal_operator_id (const char* name)
15068 {
15069 tree identifier;
15070 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15071 + strlen (name) + 10);
15072 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15073 identifier = get_identifier (buffer);
15074
15075 return identifier;
15076 }
15077
15078 /* Parse an operator.
15079
15080 operator:
15081 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15082 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15083 || ++ -- , ->* -> () []
15084
15085 GNU Extensions:
15086
15087 operator:
15088 <? >? <?= >?=
15089
15090 Returns an IDENTIFIER_NODE for the operator which is a
15091 human-readable spelling of the identifier, e.g., `operator +'. */
15092
15093 static cp_expr
15094 cp_parser_operator (cp_parser* parser, location_t start_loc)
15095 {
15096 tree id = NULL_TREE;
15097 cp_token *token;
15098 bool utf8 = false;
15099
15100 /* Peek at the next token. */
15101 token = cp_lexer_peek_token (parser->lexer);
15102
15103 location_t end_loc = token->location;
15104
15105 /* Figure out which operator we have. */
15106 enum tree_code op = ERROR_MARK;
15107 bool assop = false;
15108 bool consumed = false;
15109 switch (token->type)
15110 {
15111 case CPP_KEYWORD:
15112 {
15113 /* The keyword should be either `new' or `delete'. */
15114 if (token->keyword == RID_NEW)
15115 op = NEW_EXPR;
15116 else if (token->keyword == RID_DELETE)
15117 op = DELETE_EXPR;
15118 else
15119 break;
15120
15121 /* Consume the `new' or `delete' token. */
15122 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15123
15124 /* Peek at the next token. */
15125 token = cp_lexer_peek_token (parser->lexer);
15126 /* If it's a `[' token then this is the array variant of the
15127 operator. */
15128 if (token->type == CPP_OPEN_SQUARE)
15129 {
15130 /* Consume the `[' token. */
15131 cp_lexer_consume_token (parser->lexer);
15132 /* Look for the `]' token. */
15133 if (cp_token *close_token
15134 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15135 end_loc = close_token->location;
15136 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15137 }
15138 consumed = true;
15139 break;
15140 }
15141
15142 case CPP_PLUS:
15143 op = PLUS_EXPR;
15144 break;
15145
15146 case CPP_MINUS:
15147 op = MINUS_EXPR;
15148 break;
15149
15150 case CPP_MULT:
15151 op = MULT_EXPR;
15152 break;
15153
15154 case CPP_DIV:
15155 op = TRUNC_DIV_EXPR;
15156 break;
15157
15158 case CPP_MOD:
15159 op = TRUNC_MOD_EXPR;
15160 break;
15161
15162 case CPP_XOR:
15163 op = BIT_XOR_EXPR;
15164 break;
15165
15166 case CPP_AND:
15167 op = BIT_AND_EXPR;
15168 break;
15169
15170 case CPP_OR:
15171 op = BIT_IOR_EXPR;
15172 break;
15173
15174 case CPP_COMPL:
15175 op = BIT_NOT_EXPR;
15176 break;
15177
15178 case CPP_NOT:
15179 op = TRUTH_NOT_EXPR;
15180 break;
15181
15182 case CPP_EQ:
15183 assop = true;
15184 op = NOP_EXPR;
15185 break;
15186
15187 case CPP_LESS:
15188 op = LT_EXPR;
15189 break;
15190
15191 case CPP_GREATER:
15192 op = GT_EXPR;
15193 break;
15194
15195 case CPP_PLUS_EQ:
15196 assop = true;
15197 op = PLUS_EXPR;
15198 break;
15199
15200 case CPP_MINUS_EQ:
15201 assop = true;
15202 op = MINUS_EXPR;
15203 break;
15204
15205 case CPP_MULT_EQ:
15206 assop = true;
15207 op = MULT_EXPR;
15208 break;
15209
15210 case CPP_DIV_EQ:
15211 assop = true;
15212 op = TRUNC_DIV_EXPR;
15213 break;
15214
15215 case CPP_MOD_EQ:
15216 assop = true;
15217 op = TRUNC_MOD_EXPR;
15218 break;
15219
15220 case CPP_XOR_EQ:
15221 assop = true;
15222 op = BIT_XOR_EXPR;
15223 break;
15224
15225 case CPP_AND_EQ:
15226 assop = true;
15227 op = BIT_AND_EXPR;
15228 break;
15229
15230 case CPP_OR_EQ:
15231 assop = true;
15232 op = BIT_IOR_EXPR;
15233 break;
15234
15235 case CPP_LSHIFT:
15236 op = LSHIFT_EXPR;
15237 break;
15238
15239 case CPP_RSHIFT:
15240 op = RSHIFT_EXPR;
15241 break;
15242
15243 case CPP_LSHIFT_EQ:
15244 assop = true;
15245 op = LSHIFT_EXPR;
15246 break;
15247
15248 case CPP_RSHIFT_EQ:
15249 assop = true;
15250 op = RSHIFT_EXPR;
15251 break;
15252
15253 case CPP_EQ_EQ:
15254 op = EQ_EXPR;
15255 break;
15256
15257 case CPP_NOT_EQ:
15258 op = NE_EXPR;
15259 break;
15260
15261 case CPP_LESS_EQ:
15262 op = LE_EXPR;
15263 break;
15264
15265 case CPP_GREATER_EQ:
15266 op = GE_EXPR;
15267 break;
15268
15269 case CPP_AND_AND:
15270 op = TRUTH_ANDIF_EXPR;
15271 break;
15272
15273 case CPP_OR_OR:
15274 op = TRUTH_ORIF_EXPR;
15275 break;
15276
15277 case CPP_PLUS_PLUS:
15278 op = POSTINCREMENT_EXPR;
15279 break;
15280
15281 case CPP_MINUS_MINUS:
15282 op = PREDECREMENT_EXPR;
15283 break;
15284
15285 case CPP_COMMA:
15286 op = COMPOUND_EXPR;
15287 break;
15288
15289 case CPP_DEREF_STAR:
15290 op = MEMBER_REF;
15291 break;
15292
15293 case CPP_DEREF:
15294 op = COMPONENT_REF;
15295 break;
15296
15297 case CPP_OPEN_PAREN:
15298 {
15299 /* Consume the `('. */
15300 matching_parens parens;
15301 parens.consume_open (parser);
15302 /* Look for the matching `)'. */
15303 token = parens.require_close (parser);
15304 if (token)
15305 end_loc = token->location;
15306 op = CALL_EXPR;
15307 consumed = true;
15308 break;
15309 }
15310
15311 case CPP_OPEN_SQUARE:
15312 /* Consume the `['. */
15313 cp_lexer_consume_token (parser->lexer);
15314 /* Look for the matching `]'. */
15315 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15316 if (token)
15317 end_loc = token->location;
15318 op = ARRAY_REF;
15319 consumed = true;
15320 break;
15321
15322 case CPP_UTF8STRING:
15323 case CPP_UTF8STRING_USERDEF:
15324 utf8 = true;
15325 /* FALLTHRU */
15326 case CPP_STRING:
15327 case CPP_WSTRING:
15328 case CPP_STRING16:
15329 case CPP_STRING32:
15330 case CPP_STRING_USERDEF:
15331 case CPP_WSTRING_USERDEF:
15332 case CPP_STRING16_USERDEF:
15333 case CPP_STRING32_USERDEF:
15334 {
15335 cp_expr str;
15336 tree string_tree;
15337 int sz, len;
15338
15339 if (cxx_dialect == cxx98)
15340 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15341
15342 /* Consume the string. */
15343 str = cp_parser_string_literal (parser, /*translate=*/true,
15344 /*wide_ok=*/true, /*lookup_udlit=*/false);
15345 if (str == error_mark_node)
15346 return error_mark_node;
15347 else if (TREE_CODE (str) == USERDEF_LITERAL)
15348 {
15349 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15350 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15351 end_loc = str.get_location ();
15352 }
15353 else
15354 {
15355 string_tree = str;
15356 /* Look for the suffix identifier. */
15357 token = cp_lexer_peek_token (parser->lexer);
15358 if (token->type == CPP_NAME)
15359 {
15360 id = cp_parser_identifier (parser);
15361 end_loc = token->location;
15362 }
15363 else if (token->type == CPP_KEYWORD)
15364 {
15365 error ("unexpected keyword;"
15366 " remove space between quotes and suffix identifier");
15367 return error_mark_node;
15368 }
15369 else
15370 {
15371 error ("expected suffix identifier");
15372 return error_mark_node;
15373 }
15374 }
15375 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15376 (TREE_TYPE (TREE_TYPE (string_tree))));
15377 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15378 if (len != 0)
15379 {
15380 error ("expected empty string after %<operator%> keyword");
15381 return error_mark_node;
15382 }
15383 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15384 != char_type_node)
15385 {
15386 error ("invalid encoding prefix in literal operator");
15387 return error_mark_node;
15388 }
15389 if (id != error_mark_node)
15390 {
15391 const char *name = IDENTIFIER_POINTER (id);
15392 id = cp_literal_operator_id (name);
15393 }
15394 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15395 return cp_expr (id, start_loc);
15396 }
15397
15398 default:
15399 /* Anything else is an error. */
15400 break;
15401 }
15402
15403 /* If we have selected an identifier, we need to consume the
15404 operator token. */
15405 if (op != ERROR_MARK)
15406 {
15407 id = ovl_op_identifier (assop, op);
15408 if (!consumed)
15409 cp_lexer_consume_token (parser->lexer);
15410 }
15411 /* Otherwise, no valid operator name was present. */
15412 else
15413 {
15414 cp_parser_error (parser, "expected operator");
15415 id = error_mark_node;
15416 }
15417
15418 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15419 return cp_expr (id, start_loc);
15420 }
15421
15422 /* Parse a template-declaration.
15423
15424 template-declaration:
15425 export [opt] template < template-parameter-list > declaration
15426
15427 If MEMBER_P is TRUE, this template-declaration occurs within a
15428 class-specifier.
15429
15430 The grammar rule given by the standard isn't correct. What
15431 is really meant is:
15432
15433 template-declaration:
15434 export [opt] template-parameter-list-seq
15435 decl-specifier-seq [opt] init-declarator [opt] ;
15436 export [opt] template-parameter-list-seq
15437 function-definition
15438
15439 template-parameter-list-seq:
15440 template-parameter-list-seq [opt]
15441 template < template-parameter-list >
15442
15443 Concept Extensions:
15444
15445 template-parameter-list-seq:
15446 template < template-parameter-list > requires-clause [opt]
15447
15448 requires-clause:
15449 requires logical-or-expression */
15450
15451 static void
15452 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15453 {
15454 /* Check for `export'. */
15455 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15456 {
15457 /* Consume the `export' token. */
15458 cp_lexer_consume_token (parser->lexer);
15459 /* Warn that we do not support `export'. */
15460 warning (0, "keyword %<export%> not implemented, and will be ignored");
15461 }
15462
15463 cp_parser_template_declaration_after_export (parser, member_p);
15464 }
15465
15466 /* Parse a template-parameter-list.
15467
15468 template-parameter-list:
15469 template-parameter
15470 template-parameter-list , template-parameter
15471
15472 Returns a TREE_LIST. Each node represents a template parameter.
15473 The nodes are connected via their TREE_CHAINs. */
15474
15475 static tree
15476 cp_parser_template_parameter_list (cp_parser* parser)
15477 {
15478 tree parameter_list = NULL_TREE;
15479
15480 /* Don't create wrapper nodes within a template-parameter-list,
15481 since we don't want to have different types based on the
15482 spelling location of constants and decls within them. */
15483 auto_suppress_location_wrappers sentinel;
15484
15485 begin_template_parm_list ();
15486
15487 /* The loop below parses the template parms. We first need to know
15488 the total number of template parms to be able to compute proper
15489 canonical types of each dependent type. So after the loop, when
15490 we know the total number of template parms,
15491 end_template_parm_list computes the proper canonical types and
15492 fixes up the dependent types accordingly. */
15493 while (true)
15494 {
15495 tree parameter;
15496 bool is_non_type;
15497 bool is_parameter_pack;
15498 location_t parm_loc;
15499
15500 /* Parse the template-parameter. */
15501 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15502 parameter = cp_parser_template_parameter (parser,
15503 &is_non_type,
15504 &is_parameter_pack);
15505 /* Add it to the list. */
15506 if (parameter != error_mark_node)
15507 parameter_list = process_template_parm (parameter_list,
15508 parm_loc,
15509 parameter,
15510 is_non_type,
15511 is_parameter_pack);
15512 else
15513 {
15514 tree err_parm = build_tree_list (parameter, parameter);
15515 parameter_list = chainon (parameter_list, err_parm);
15516 }
15517
15518 /* If the next token is not a `,', we're done. */
15519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15520 break;
15521 /* Otherwise, consume the `,' token. */
15522 cp_lexer_consume_token (parser->lexer);
15523 }
15524
15525 return end_template_parm_list (parameter_list);
15526 }
15527
15528 /* Parse a introduction-list.
15529
15530 introduction-list:
15531 introduced-parameter
15532 introduction-list , introduced-parameter
15533
15534 introduced-parameter:
15535 ...[opt] identifier
15536
15537 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15538 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15539 WILDCARD_DECL will also have DECL_NAME set and token location in
15540 DECL_SOURCE_LOCATION. */
15541
15542 static tree
15543 cp_parser_introduction_list (cp_parser *parser)
15544 {
15545 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15546
15547 while (true)
15548 {
15549 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15550 if (is_pack)
15551 cp_lexer_consume_token (parser->lexer);
15552
15553 tree identifier = cp_parser_identifier (parser);
15554 if (identifier == error_mark_node)
15555 break;
15556
15557 /* Build placeholder. */
15558 tree parm = build_nt (WILDCARD_DECL);
15559 DECL_SOURCE_LOCATION (parm)
15560 = cp_lexer_peek_token (parser->lexer)->location;
15561 DECL_NAME (parm) = identifier;
15562 WILDCARD_PACK_P (parm) = is_pack;
15563 vec_safe_push (introduction_vec, parm);
15564
15565 /* If the next token is not a `,', we're done. */
15566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15567 break;
15568 /* Otherwise, consume the `,' token. */
15569 cp_lexer_consume_token (parser->lexer);
15570 }
15571
15572 /* Convert the vec into a TREE_VEC. */
15573 tree introduction_list = make_tree_vec (introduction_vec->length ());
15574 unsigned int n;
15575 tree parm;
15576 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15577 TREE_VEC_ELT (introduction_list, n) = parm;
15578
15579 release_tree_vector (introduction_vec);
15580 return introduction_list;
15581 }
15582
15583 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15584 is an abstract declarator. */
15585
15586 static inline cp_declarator*
15587 get_id_declarator (cp_declarator *declarator)
15588 {
15589 cp_declarator *d = declarator;
15590 while (d && d->kind != cdk_id)
15591 d = d->declarator;
15592 return d;
15593 }
15594
15595 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15596 is an abstract declarator. */
15597
15598 static inline tree
15599 get_unqualified_id (cp_declarator *declarator)
15600 {
15601 declarator = get_id_declarator (declarator);
15602 if (declarator)
15603 return declarator->u.id.unqualified_name;
15604 else
15605 return NULL_TREE;
15606 }
15607
15608 /* Returns true if DECL represents a constrained-parameter. */
15609
15610 static inline bool
15611 is_constrained_parameter (tree decl)
15612 {
15613 return (decl
15614 && TREE_CODE (decl) == TYPE_DECL
15615 && CONSTRAINED_PARM_CONCEPT (decl)
15616 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15617 }
15618
15619 /* Returns true if PARM declares a constrained-parameter. */
15620
15621 static inline bool
15622 is_constrained_parameter (cp_parameter_declarator *parm)
15623 {
15624 return is_constrained_parameter (parm->decl_specifiers.type);
15625 }
15626
15627 /* Check that the type parameter is only a declarator-id, and that its
15628 type is not cv-qualified. */
15629
15630 bool
15631 cp_parser_check_constrained_type_parm (cp_parser *parser,
15632 cp_parameter_declarator *parm)
15633 {
15634 if (!parm->declarator)
15635 return true;
15636
15637 if (parm->declarator->kind != cdk_id)
15638 {
15639 cp_parser_error (parser, "invalid constrained type parameter");
15640 return false;
15641 }
15642
15643 /* Don't allow cv-qualified type parameters. */
15644 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15645 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15646 {
15647 cp_parser_error (parser, "cv-qualified type parameter");
15648 return false;
15649 }
15650
15651 return true;
15652 }
15653
15654 /* Finish parsing/processing a template type parameter and checking
15655 various restrictions. */
15656
15657 static inline tree
15658 cp_parser_constrained_type_template_parm (cp_parser *parser,
15659 tree id,
15660 cp_parameter_declarator* parmdecl)
15661 {
15662 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15663 return finish_template_type_parm (class_type_node, id);
15664 else
15665 return error_mark_node;
15666 }
15667
15668 static tree
15669 finish_constrained_template_template_parm (tree proto, tree id)
15670 {
15671 /* FIXME: This should probably be copied, and we may need to adjust
15672 the template parameter depths. */
15673 tree saved_parms = current_template_parms;
15674 begin_template_parm_list ();
15675 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15676 end_template_parm_list ();
15677
15678 tree parm = finish_template_template_parm (class_type_node, id);
15679 current_template_parms = saved_parms;
15680
15681 return parm;
15682 }
15683
15684 /* Finish parsing/processing a template template parameter by borrowing
15685 the template parameter list from the prototype parameter. */
15686
15687 static tree
15688 cp_parser_constrained_template_template_parm (cp_parser *parser,
15689 tree proto,
15690 tree id,
15691 cp_parameter_declarator *parmdecl)
15692 {
15693 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15694 return error_mark_node;
15695 return finish_constrained_template_template_parm (proto, id);
15696 }
15697
15698 /* Create a new non-type template parameter from the given PARM
15699 declarator. */
15700
15701 static tree
15702 constrained_non_type_template_parm (bool *is_non_type,
15703 cp_parameter_declarator *parm)
15704 {
15705 *is_non_type = true;
15706 cp_declarator *decl = parm->declarator;
15707 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15708 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15709 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15710 }
15711
15712 /* Build a constrained template parameter based on the PARMDECL
15713 declarator. The type of PARMDECL is the constrained type, which
15714 refers to the prototype template parameter that ultimately
15715 specifies the type of the declared parameter. */
15716
15717 static tree
15718 finish_constrained_parameter (cp_parser *parser,
15719 cp_parameter_declarator *parmdecl,
15720 bool *is_non_type,
15721 bool *is_parameter_pack)
15722 {
15723 tree decl = parmdecl->decl_specifiers.type;
15724 tree id = get_unqualified_id (parmdecl->declarator);
15725 tree def = parmdecl->default_argument;
15726 tree proto = DECL_INITIAL (decl);
15727
15728 /* A template parameter constrained by a variadic concept shall also
15729 be declared as a template parameter pack. */
15730 bool is_variadic = template_parameter_pack_p (proto);
15731 if (is_variadic && !*is_parameter_pack)
15732 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15733
15734 /* Build the parameter. Return an error if the declarator was invalid. */
15735 tree parm;
15736 if (TREE_CODE (proto) == TYPE_DECL)
15737 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15738 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15739 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15740 parmdecl);
15741 else
15742 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15743 if (parm == error_mark_node)
15744 return error_mark_node;
15745
15746 /* Finish the parameter decl and create a node attaching the
15747 default argument and constraint. */
15748 parm = build_tree_list (def, parm);
15749 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15750
15751 return parm;
15752 }
15753
15754 /* Returns true if the parsed type actually represents the declaration
15755 of a type template-parameter. */
15756
15757 static inline bool
15758 declares_constrained_type_template_parameter (tree type)
15759 {
15760 return (is_constrained_parameter (type)
15761 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15762 }
15763
15764
15765 /* Returns true if the parsed type actually represents the declaration of
15766 a template template-parameter. */
15767
15768 static bool
15769 declares_constrained_template_template_parameter (tree type)
15770 {
15771 return (is_constrained_parameter (type)
15772 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15773 }
15774
15775 /* Parse a default argument for a type template-parameter.
15776 Note that diagnostics are handled in cp_parser_template_parameter. */
15777
15778 static tree
15779 cp_parser_default_type_template_argument (cp_parser *parser)
15780 {
15781 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15782
15783 /* Consume the `=' token. */
15784 cp_lexer_consume_token (parser->lexer);
15785
15786 cp_token *token = cp_lexer_peek_token (parser->lexer);
15787
15788 /* Parse the default-argument. */
15789 push_deferring_access_checks (dk_no_deferred);
15790 tree default_argument = cp_parser_type_id (parser,
15791 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15792 NULL);
15793 pop_deferring_access_checks ();
15794
15795 if (flag_concepts && type_uses_auto (default_argument))
15796 {
15797 error_at (token->location,
15798 "invalid use of %<auto%> in default template argument");
15799 return error_mark_node;
15800 }
15801
15802 return default_argument;
15803 }
15804
15805 /* Parse a default argument for a template template-parameter. */
15806
15807 static tree
15808 cp_parser_default_template_template_argument (cp_parser *parser)
15809 {
15810 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15811
15812 bool is_template;
15813
15814 /* Consume the `='. */
15815 cp_lexer_consume_token (parser->lexer);
15816 /* Parse the id-expression. */
15817 push_deferring_access_checks (dk_no_deferred);
15818 /* save token before parsing the id-expression, for error
15819 reporting */
15820 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15821 tree default_argument
15822 = cp_parser_id_expression (parser,
15823 /*template_keyword_p=*/false,
15824 /*check_dependency_p=*/true,
15825 /*template_p=*/&is_template,
15826 /*declarator_p=*/false,
15827 /*optional_p=*/false);
15828 if (TREE_CODE (default_argument) == TYPE_DECL)
15829 /* If the id-expression was a template-id that refers to
15830 a template-class, we already have the declaration here,
15831 so no further lookup is needed. */
15832 ;
15833 else
15834 /* Look up the name. */
15835 default_argument
15836 = cp_parser_lookup_name (parser, default_argument,
15837 none_type,
15838 /*is_template=*/is_template,
15839 /*is_namespace=*/false,
15840 /*check_dependency=*/true,
15841 /*ambiguous_decls=*/NULL,
15842 token->location);
15843 /* See if the default argument is valid. */
15844 default_argument = check_template_template_default_arg (default_argument);
15845 pop_deferring_access_checks ();
15846 return default_argument;
15847 }
15848
15849 /* Parse a template-parameter.
15850
15851 template-parameter:
15852 type-parameter
15853 parameter-declaration
15854
15855 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15856 the parameter. The TREE_PURPOSE is the default value, if any.
15857 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15858 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15859 set to true iff this parameter is a parameter pack. */
15860
15861 static tree
15862 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15863 bool *is_parameter_pack)
15864 {
15865 cp_token *token;
15866 cp_parameter_declarator *parameter_declarator;
15867 tree parm;
15868
15869 /* Assume it is a type parameter or a template parameter. */
15870 *is_non_type = false;
15871 /* Assume it not a parameter pack. */
15872 *is_parameter_pack = false;
15873 /* Peek at the next token. */
15874 token = cp_lexer_peek_token (parser->lexer);
15875 /* If it is `template', we have a type-parameter. */
15876 if (token->keyword == RID_TEMPLATE)
15877 return cp_parser_type_parameter (parser, is_parameter_pack);
15878 /* If it is `class' or `typename' we do not know yet whether it is a
15879 type parameter or a non-type parameter. Consider:
15880
15881 template <typename T, typename T::X X> ...
15882
15883 or:
15884
15885 template <class C, class D*> ...
15886
15887 Here, the first parameter is a type parameter, and the second is
15888 a non-type parameter. We can tell by looking at the token after
15889 the identifier -- if it is a `,', `=', or `>' then we have a type
15890 parameter. */
15891 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15892 {
15893 /* Peek at the token after `class' or `typename'. */
15894 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15895 /* If it's an ellipsis, we have a template type parameter
15896 pack. */
15897 if (token->type == CPP_ELLIPSIS)
15898 return cp_parser_type_parameter (parser, is_parameter_pack);
15899 /* If it's an identifier, skip it. */
15900 if (token->type == CPP_NAME)
15901 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15902 /* Now, see if the token looks like the end of a template
15903 parameter. */
15904 if (token->type == CPP_COMMA
15905 || token->type == CPP_EQ
15906 || token->type == CPP_GREATER)
15907 return cp_parser_type_parameter (parser, is_parameter_pack);
15908 }
15909
15910 /* Otherwise, it is a non-type parameter or a constrained parameter.
15911
15912 [temp.param]
15913
15914 When parsing a default template-argument for a non-type
15915 template-parameter, the first non-nested `>' is taken as the end
15916 of the template parameter-list rather than a greater-than
15917 operator. */
15918 parameter_declarator
15919 = cp_parser_parameter_declaration (parser,
15920 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15921 /*template_parm_p=*/true,
15922 /*parenthesized_p=*/NULL);
15923
15924 if (!parameter_declarator)
15925 return error_mark_node;
15926
15927 /* If the parameter declaration is marked as a parameter pack, set
15928 *IS_PARAMETER_PACK to notify the caller. */
15929 if (parameter_declarator->template_parameter_pack_p)
15930 *is_parameter_pack = true;
15931
15932 if (parameter_declarator->default_argument)
15933 {
15934 /* Can happen in some cases of erroneous input (c++/34892). */
15935 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15936 /* Consume the `...' for better error recovery. */
15937 cp_lexer_consume_token (parser->lexer);
15938 }
15939
15940 // The parameter may have been constrained.
15941 if (is_constrained_parameter (parameter_declarator))
15942 return finish_constrained_parameter (parser,
15943 parameter_declarator,
15944 is_non_type,
15945 is_parameter_pack);
15946
15947 // Now we're sure that the parameter is a non-type parameter.
15948 *is_non_type = true;
15949
15950 parm = grokdeclarator (parameter_declarator->declarator,
15951 &parameter_declarator->decl_specifiers,
15952 TPARM, /*initialized=*/0,
15953 /*attrlist=*/NULL);
15954 if (parm == error_mark_node)
15955 return error_mark_node;
15956
15957 return build_tree_list (parameter_declarator->default_argument, parm);
15958 }
15959
15960 /* Parse a type-parameter.
15961
15962 type-parameter:
15963 class identifier [opt]
15964 class identifier [opt] = type-id
15965 typename identifier [opt]
15966 typename identifier [opt] = type-id
15967 template < template-parameter-list > class identifier [opt]
15968 template < template-parameter-list > class identifier [opt]
15969 = id-expression
15970
15971 GNU Extension (variadic templates):
15972
15973 type-parameter:
15974 class ... identifier [opt]
15975 typename ... identifier [opt]
15976
15977 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15978 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15979 the declaration of the parameter.
15980
15981 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15982
15983 static tree
15984 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15985 {
15986 cp_token *token;
15987 tree parameter;
15988
15989 /* Look for a keyword to tell us what kind of parameter this is. */
15990 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15991 if (!token)
15992 return error_mark_node;
15993
15994 switch (token->keyword)
15995 {
15996 case RID_CLASS:
15997 case RID_TYPENAME:
15998 {
15999 tree identifier;
16000 tree default_argument;
16001
16002 /* If the next token is an ellipsis, we have a template
16003 argument pack. */
16004 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16005 {
16006 /* Consume the `...' token. */
16007 cp_lexer_consume_token (parser->lexer);
16008 maybe_warn_variadic_templates ();
16009
16010 *is_parameter_pack = true;
16011 }
16012
16013 /* If the next token is an identifier, then it names the
16014 parameter. */
16015 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16016 identifier = cp_parser_identifier (parser);
16017 else
16018 identifier = NULL_TREE;
16019
16020 /* Create the parameter. */
16021 parameter = finish_template_type_parm (class_type_node, identifier);
16022
16023 /* If the next token is an `=', we have a default argument. */
16024 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16025 {
16026 default_argument
16027 = cp_parser_default_type_template_argument (parser);
16028
16029 /* Template parameter packs cannot have default
16030 arguments. */
16031 if (*is_parameter_pack)
16032 {
16033 if (identifier)
16034 error_at (token->location,
16035 "template parameter pack %qD cannot have a "
16036 "default argument", identifier);
16037 else
16038 error_at (token->location,
16039 "template parameter packs cannot have "
16040 "default arguments");
16041 default_argument = NULL_TREE;
16042 }
16043 else if (check_for_bare_parameter_packs (default_argument))
16044 default_argument = error_mark_node;
16045 }
16046 else
16047 default_argument = NULL_TREE;
16048
16049 /* Create the combined representation of the parameter and the
16050 default argument. */
16051 parameter = build_tree_list (default_argument, parameter);
16052 }
16053 break;
16054
16055 case RID_TEMPLATE:
16056 {
16057 tree identifier;
16058 tree default_argument;
16059
16060 /* Look for the `<'. */
16061 cp_parser_require (parser, CPP_LESS, RT_LESS);
16062 /* Parse the template-parameter-list. */
16063 cp_parser_template_parameter_list (parser);
16064 /* Look for the `>'. */
16065 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16066
16067 // If template requirements are present, parse them.
16068 if (flag_concepts)
16069 {
16070 tree reqs = get_shorthand_constraints (current_template_parms);
16071 if (tree r = cp_parser_requires_clause_opt (parser))
16072 reqs = conjoin_constraints (reqs, normalize_expression (r));
16073 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16074 }
16075
16076 /* Look for the `class' or 'typename' keywords. */
16077 cp_parser_type_parameter_key (parser);
16078 /* If the next token is an ellipsis, we have a template
16079 argument pack. */
16080 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16081 {
16082 /* Consume the `...' token. */
16083 cp_lexer_consume_token (parser->lexer);
16084 maybe_warn_variadic_templates ();
16085
16086 *is_parameter_pack = true;
16087 }
16088 /* If the next token is an `=', then there is a
16089 default-argument. If the next token is a `>', we are at
16090 the end of the parameter-list. If the next token is a `,',
16091 then we are at the end of this parameter. */
16092 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16093 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16094 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16095 {
16096 identifier = cp_parser_identifier (parser);
16097 /* Treat invalid names as if the parameter were nameless. */
16098 if (identifier == error_mark_node)
16099 identifier = NULL_TREE;
16100 }
16101 else
16102 identifier = NULL_TREE;
16103
16104 /* Create the template parameter. */
16105 parameter = finish_template_template_parm (class_type_node,
16106 identifier);
16107
16108 /* If the next token is an `=', then there is a
16109 default-argument. */
16110 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16111 {
16112 default_argument
16113 = cp_parser_default_template_template_argument (parser);
16114
16115 /* Template parameter packs cannot have default
16116 arguments. */
16117 if (*is_parameter_pack)
16118 {
16119 if (identifier)
16120 error_at (token->location,
16121 "template parameter pack %qD cannot "
16122 "have a default argument",
16123 identifier);
16124 else
16125 error_at (token->location, "template parameter packs cannot "
16126 "have default arguments");
16127 default_argument = NULL_TREE;
16128 }
16129 }
16130 else
16131 default_argument = NULL_TREE;
16132
16133 /* Create the combined representation of the parameter and the
16134 default argument. */
16135 parameter = build_tree_list (default_argument, parameter);
16136 }
16137 break;
16138
16139 default:
16140 gcc_unreachable ();
16141 break;
16142 }
16143
16144 return parameter;
16145 }
16146
16147 /* Parse a template-id.
16148
16149 template-id:
16150 template-name < template-argument-list [opt] >
16151
16152 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16153 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16154 returned. Otherwise, if the template-name names a function, or set
16155 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16156 names a class, returns a TYPE_DECL for the specialization.
16157
16158 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16159 uninstantiated templates. */
16160
16161 static tree
16162 cp_parser_template_id (cp_parser *parser,
16163 bool template_keyword_p,
16164 bool check_dependency_p,
16165 enum tag_types tag_type,
16166 bool is_declaration)
16167 {
16168 tree templ;
16169 tree arguments;
16170 tree template_id;
16171 cp_token_position start_of_id = 0;
16172 cp_token *next_token = NULL, *next_token_2 = NULL;
16173 bool is_identifier;
16174
16175 /* If the next token corresponds to a template-id, there is no need
16176 to reparse it. */
16177 cp_token *token = cp_lexer_peek_token (parser->lexer);
16178 if (token->type == CPP_TEMPLATE_ID)
16179 {
16180 cp_lexer_consume_token (parser->lexer);
16181 return saved_checks_value (token->u.tree_check_value);
16182 }
16183
16184 /* Avoid performing name lookup if there is no possibility of
16185 finding a template-id. */
16186 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16187 || (token->type == CPP_NAME
16188 && !cp_parser_nth_token_starts_template_argument_list_p
16189 (parser, 2)))
16190 {
16191 cp_parser_error (parser, "expected template-id");
16192 return error_mark_node;
16193 }
16194
16195 /* Remember where the template-id starts. */
16196 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16197 start_of_id = cp_lexer_token_position (parser->lexer, false);
16198
16199 push_deferring_access_checks (dk_deferred);
16200
16201 /* Parse the template-name. */
16202 is_identifier = false;
16203 templ = cp_parser_template_name (parser, template_keyword_p,
16204 check_dependency_p,
16205 is_declaration,
16206 tag_type,
16207 &is_identifier);
16208
16209 /* Push any access checks inside the firewall we're about to create. */
16210 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16211 pop_deferring_access_checks ();
16212 if (templ == error_mark_node || is_identifier)
16213 return templ;
16214
16215 /* Since we're going to preserve any side-effects from this parse, set up a
16216 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16217 in the template arguments. */
16218 tentative_firewall firewall (parser);
16219 reopen_deferring_access_checks (checks);
16220
16221 /* If we find the sequence `[:' after a template-name, it's probably
16222 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16223 parse correctly the argument list. */
16224 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16225 == CPP_OPEN_SQUARE)
16226 && next_token->flags & DIGRAPH
16227 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16228 == CPP_COLON)
16229 && !(next_token_2->flags & PREV_WHITE))
16230 {
16231 cp_parser_parse_tentatively (parser);
16232 /* Change `:' into `::'. */
16233 next_token_2->type = CPP_SCOPE;
16234 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16235 CPP_LESS. */
16236 cp_lexer_consume_token (parser->lexer);
16237
16238 /* Parse the arguments. */
16239 arguments = cp_parser_enclosed_template_argument_list (parser);
16240 if (!cp_parser_parse_definitely (parser))
16241 {
16242 /* If we couldn't parse an argument list, then we revert our changes
16243 and return simply an error. Maybe this is not a template-id
16244 after all. */
16245 next_token_2->type = CPP_COLON;
16246 cp_parser_error (parser, "expected %<<%>");
16247 pop_deferring_access_checks ();
16248 return error_mark_node;
16249 }
16250 /* Otherwise, emit an error about the invalid digraph, but continue
16251 parsing because we got our argument list. */
16252 if (permerror (next_token->location,
16253 "%<<::%> cannot begin a template-argument list"))
16254 {
16255 static bool hint = false;
16256 inform (next_token->location,
16257 "%<<:%> is an alternate spelling for %<[%>."
16258 " Insert whitespace between %<<%> and %<::%>");
16259 if (!hint && !flag_permissive)
16260 {
16261 inform (next_token->location, "(if you use %<-fpermissive%> "
16262 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16263 "accept your code)");
16264 hint = true;
16265 }
16266 }
16267 }
16268 else
16269 {
16270 /* Look for the `<' that starts the template-argument-list. */
16271 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16272 {
16273 pop_deferring_access_checks ();
16274 return error_mark_node;
16275 }
16276 /* Parse the arguments. */
16277 arguments = cp_parser_enclosed_template_argument_list (parser);
16278
16279 if ((cxx_dialect > cxx17)
16280 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16281 && !template_keyword_p
16282 && (cp_parser_error_occurred (parser)
16283 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16284 {
16285 /* This didn't go well. */
16286 if (TREE_CODE (templ) == FUNCTION_DECL)
16287 {
16288 /* C++2A says that "function-name < a;" is now ill-formed. */
16289 if (cp_parser_error_occurred (parser))
16290 {
16291 error_at (token->location, "invalid template-argument-list");
16292 inform (token->location, "function name as the left hand "
16293 "operand of %<<%> is ill-formed in C++2a; wrap the "
16294 "function name in %<()%>");
16295 }
16296 else
16297 /* We expect "f<targs>" to be followed by "(args)". */
16298 error_at (cp_lexer_peek_token (parser->lexer)->location,
16299 "expected %<(%> after template-argument-list");
16300 if (start_of_id)
16301 /* Purge all subsequent tokens. */
16302 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16303 }
16304 else
16305 cp_parser_simulate_error (parser);
16306 pop_deferring_access_checks ();
16307 return error_mark_node;
16308 }
16309 }
16310
16311 /* Set the location to be of the form:
16312 template-name < template-argument-list [opt] >
16313 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16314 with caret == start at the start of the template-name,
16315 ranging until the closing '>'. */
16316 location_t finish_loc
16317 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16318 location_t combined_loc
16319 = make_location (token->location, token->location, finish_loc);
16320
16321 /* Check for concepts autos where they don't belong. We could
16322 identify types in some cases of idnetifier TEMPL, looking ahead
16323 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16324 types. We reject them in functions, but if what we have is an
16325 identifier, even with none_type we can't conclude it's NOT a
16326 type, we have to wait for template substitution. */
16327 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16328 template_id = error_mark_node;
16329 /* Build a representation of the specialization. */
16330 else if (identifier_p (templ))
16331 template_id = build_min_nt_loc (combined_loc,
16332 TEMPLATE_ID_EXPR,
16333 templ, arguments);
16334 else if (DECL_TYPE_TEMPLATE_P (templ)
16335 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16336 {
16337 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16338 template (rather than some instantiation thereof) only if
16339 is not nested within some other construct. For example, in
16340 "template <typename T> void f(T) { A<T>::", A<T> is just an
16341 instantiation of A. */
16342 bool entering_scope
16343 = (template_parm_scope_p ()
16344 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16345 template_id
16346 = finish_template_type (templ, arguments, entering_scope);
16347 }
16348 /* A template-like identifier may be a partial concept id. */
16349 else if (flag_concepts
16350 && (template_id = (cp_parser_maybe_partial_concept_id
16351 (parser, templ, arguments))))
16352 return template_id;
16353 else if (variable_template_p (templ))
16354 {
16355 template_id = lookup_template_variable (templ, arguments);
16356 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16357 SET_EXPR_LOCATION (template_id, combined_loc);
16358 }
16359 else
16360 {
16361 /* If it's not a class-template or a template-template, it should be
16362 a function-template. */
16363 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16364 || TREE_CODE (templ) == OVERLOAD
16365 || TREE_CODE (templ) == FUNCTION_DECL
16366 || BASELINK_P (templ)));
16367
16368 template_id = lookup_template_function (templ, arguments);
16369 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16370 SET_EXPR_LOCATION (template_id, combined_loc);
16371 }
16372
16373 /* If parsing tentatively, replace the sequence of tokens that makes
16374 up the template-id with a CPP_TEMPLATE_ID token. That way,
16375 should we re-parse the token stream, we will not have to repeat
16376 the effort required to do the parse, nor will we issue duplicate
16377 error messages about problems during instantiation of the
16378 template. */
16379 if (start_of_id
16380 /* Don't do this if we had a parse error in a declarator; re-parsing
16381 might succeed if a name changes meaning (60361). */
16382 && !(cp_parser_error_occurred (parser)
16383 && cp_parser_parsing_tentatively (parser)
16384 && parser->in_declarator_p))
16385 {
16386 /* Reset the contents of the START_OF_ID token. */
16387 token->type = CPP_TEMPLATE_ID;
16388 token->location = combined_loc;
16389
16390 /* Retrieve any deferred checks. Do not pop this access checks yet
16391 so the memory will not be reclaimed during token replacing below. */
16392 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16393 token->u.tree_check_value->value = template_id;
16394 token->u.tree_check_value->checks = get_deferred_access_checks ();
16395 token->keyword = RID_MAX;
16396
16397 /* Purge all subsequent tokens. */
16398 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16399
16400 /* ??? Can we actually assume that, if template_id ==
16401 error_mark_node, we will have issued a diagnostic to the
16402 user, as opposed to simply marking the tentative parse as
16403 failed? */
16404 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16405 error_at (token->location, "parse error in template argument list");
16406 }
16407
16408 pop_to_parent_deferring_access_checks ();
16409 return template_id;
16410 }
16411
16412 /* Parse a template-name.
16413
16414 template-name:
16415 identifier
16416
16417 The standard should actually say:
16418
16419 template-name:
16420 identifier
16421 operator-function-id
16422
16423 A defect report has been filed about this issue.
16424
16425 A conversion-function-id cannot be a template name because they cannot
16426 be part of a template-id. In fact, looking at this code:
16427
16428 a.operator K<int>()
16429
16430 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16431 It is impossible to call a templated conversion-function-id with an
16432 explicit argument list, since the only allowed template parameter is
16433 the type to which it is converting.
16434
16435 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16436 `template' keyword, in a construction like:
16437
16438 T::template f<3>()
16439
16440 In that case `f' is taken to be a template-name, even though there
16441 is no way of knowing for sure.
16442
16443 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16444 name refers to a set of overloaded functions, at least one of which
16445 is a template, or an IDENTIFIER_NODE with the name of the template,
16446 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16447 names are looked up inside uninstantiated templates. */
16448
16449 static tree
16450 cp_parser_template_name (cp_parser* parser,
16451 bool template_keyword_p,
16452 bool check_dependency_p,
16453 bool is_declaration,
16454 enum tag_types tag_type,
16455 bool *is_identifier)
16456 {
16457 tree identifier;
16458 tree decl;
16459 cp_token *token = cp_lexer_peek_token (parser->lexer);
16460
16461 /* If the next token is `operator', then we have either an
16462 operator-function-id or a conversion-function-id. */
16463 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16464 {
16465 /* We don't know whether we're looking at an
16466 operator-function-id or a conversion-function-id. */
16467 cp_parser_parse_tentatively (parser);
16468 /* Try an operator-function-id. */
16469 identifier = cp_parser_operator_function_id (parser);
16470 /* If that didn't work, try a conversion-function-id. */
16471 if (!cp_parser_parse_definitely (parser))
16472 {
16473 cp_parser_error (parser, "expected template-name");
16474 return error_mark_node;
16475 }
16476 }
16477 /* Look for the identifier. */
16478 else
16479 identifier = cp_parser_identifier (parser);
16480
16481 /* If we didn't find an identifier, we don't have a template-id. */
16482 if (identifier == error_mark_node)
16483 return error_mark_node;
16484
16485 /* If the name immediately followed the `template' keyword, then it
16486 is a template-name. However, if the next token is not `<', then
16487 we do not treat it as a template-name, since it is not being used
16488 as part of a template-id. This enables us to handle constructs
16489 like:
16490
16491 template <typename T> struct S { S(); };
16492 template <typename T> S<T>::S();
16493
16494 correctly. We would treat `S' as a template -- if it were `S<T>'
16495 -- but we do not if there is no `<'. */
16496
16497 if (processing_template_decl
16498 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16499 {
16500 /* In a declaration, in a dependent context, we pretend that the
16501 "template" keyword was present in order to improve error
16502 recovery. For example, given:
16503
16504 template <typename T> void f(T::X<int>);
16505
16506 we want to treat "X<int>" as a template-id. */
16507 if (is_declaration
16508 && !template_keyword_p
16509 && parser->scope && TYPE_P (parser->scope)
16510 && check_dependency_p
16511 && dependent_scope_p (parser->scope)
16512 /* Do not do this for dtors (or ctors), since they never
16513 need the template keyword before their name. */
16514 && !constructor_name_p (identifier, parser->scope))
16515 {
16516 cp_token_position start = 0;
16517
16518 /* Explain what went wrong. */
16519 error_at (token->location, "non-template %qD used as template",
16520 identifier);
16521 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16522 parser->scope, identifier);
16523 /* If parsing tentatively, find the location of the "<" token. */
16524 if (cp_parser_simulate_error (parser))
16525 start = cp_lexer_token_position (parser->lexer, true);
16526 /* Parse the template arguments so that we can issue error
16527 messages about them. */
16528 cp_lexer_consume_token (parser->lexer);
16529 cp_parser_enclosed_template_argument_list (parser);
16530 /* Skip tokens until we find a good place from which to
16531 continue parsing. */
16532 cp_parser_skip_to_closing_parenthesis (parser,
16533 /*recovering=*/true,
16534 /*or_comma=*/true,
16535 /*consume_paren=*/false);
16536 /* If parsing tentatively, permanently remove the
16537 template argument list. That will prevent duplicate
16538 error messages from being issued about the missing
16539 "template" keyword. */
16540 if (start)
16541 cp_lexer_purge_tokens_after (parser->lexer, start);
16542 if (is_identifier)
16543 *is_identifier = true;
16544 parser->context->object_type = NULL_TREE;
16545 return identifier;
16546 }
16547
16548 /* If the "template" keyword is present, then there is generally
16549 no point in doing name-lookup, so we just return IDENTIFIER.
16550 But, if the qualifying scope is non-dependent then we can
16551 (and must) do name-lookup normally. */
16552 if (template_keyword_p)
16553 {
16554 tree scope = (parser->scope ? parser->scope
16555 : parser->context->object_type);
16556 if (scope && TYPE_P (scope)
16557 && (!CLASS_TYPE_P (scope)
16558 || (check_dependency_p && dependent_type_p (scope))))
16559 {
16560 /* We're optimizing away the call to cp_parser_lookup_name, but
16561 we still need to do this. */
16562 parser->context->object_type = NULL_TREE;
16563 return identifier;
16564 }
16565 }
16566 }
16567
16568 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16569 const bool scoped_p = ((parser->scope ? parser->scope
16570 : parser->context->object_type) != NULL_TREE);
16571
16572 /* Look up the name. */
16573 decl = cp_parser_lookup_name (parser, identifier,
16574 tag_type,
16575 /*is_template=*/true,
16576 /*is_namespace=*/false,
16577 check_dependency_p,
16578 /*ambiguous_decls=*/NULL,
16579 token->location);
16580
16581 decl = strip_using_decl (decl);
16582
16583 /* If DECL is a template, then the name was a template-name. */
16584 if (TREE_CODE (decl) == TEMPLATE_DECL)
16585 {
16586 if (TREE_DEPRECATED (decl)
16587 && deprecated_state != DEPRECATED_SUPPRESS)
16588 warn_deprecated_use (decl, NULL_TREE);
16589 }
16590 else
16591 {
16592 /* The standard does not explicitly indicate whether a name that
16593 names a set of overloaded declarations, some of which are
16594 templates, is a template-name. However, such a name should
16595 be a template-name; otherwise, there is no way to form a
16596 template-id for the overloaded templates. */
16597 bool found = false;
16598
16599 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16600 !found && iter; ++iter)
16601 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16602 found = true;
16603
16604 if (!found
16605 && (cxx_dialect > cxx17)
16606 && !scoped_p
16607 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16608 && tag_type == none_type)
16609 {
16610 /* [temp.names] says "A name is also considered to refer to a template
16611 if it is an unqualified-id followed by a < and name lookup finds
16612 either one or more functions or finds nothing." */
16613
16614 /* The "more functions" case. Just use the OVERLOAD as normally.
16615 We don't use is_overloaded_fn here to avoid considering
16616 BASELINKs. */
16617 if (TREE_CODE (decl) == OVERLOAD
16618 /* Name lookup found one function. */
16619 || TREE_CODE (decl) == FUNCTION_DECL)
16620 found = true;
16621 /* Name lookup found nothing. */
16622 else if (decl == error_mark_node)
16623 return identifier;
16624 }
16625
16626 if (!found)
16627 {
16628 /* The name does not name a template. */
16629 cp_parser_error (parser, "expected template-name");
16630 return error_mark_node;
16631 }
16632 }
16633
16634 return decl;
16635 }
16636
16637 /* Parse a template-argument-list.
16638
16639 template-argument-list:
16640 template-argument ... [opt]
16641 template-argument-list , template-argument ... [opt]
16642
16643 Returns a TREE_VEC containing the arguments. */
16644
16645 static tree
16646 cp_parser_template_argument_list (cp_parser* parser)
16647 {
16648 tree fixed_args[10];
16649 unsigned n_args = 0;
16650 unsigned alloced = 10;
16651 tree *arg_ary = fixed_args;
16652 tree vec;
16653 bool saved_in_template_argument_list_p;
16654 bool saved_ice_p;
16655 bool saved_non_ice_p;
16656
16657 /* Don't create location wrapper nodes within a template-argument-list. */
16658 auto_suppress_location_wrappers sentinel;
16659
16660 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16661 parser->in_template_argument_list_p = true;
16662 /* Even if the template-id appears in an integral
16663 constant-expression, the contents of the argument list do
16664 not. */
16665 saved_ice_p = parser->integral_constant_expression_p;
16666 parser->integral_constant_expression_p = false;
16667 saved_non_ice_p = parser->non_integral_constant_expression_p;
16668 parser->non_integral_constant_expression_p = false;
16669
16670 /* Parse the arguments. */
16671 do
16672 {
16673 tree argument;
16674
16675 if (n_args)
16676 /* Consume the comma. */
16677 cp_lexer_consume_token (parser->lexer);
16678
16679 /* Parse the template-argument. */
16680 argument = cp_parser_template_argument (parser);
16681
16682 /* If the next token is an ellipsis, we're expanding a template
16683 argument pack. */
16684 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16685 {
16686 if (argument == error_mark_node)
16687 {
16688 cp_token *token = cp_lexer_peek_token (parser->lexer);
16689 error_at (token->location,
16690 "expected parameter pack before %<...%>");
16691 }
16692 /* Consume the `...' token. */
16693 cp_lexer_consume_token (parser->lexer);
16694
16695 /* Make the argument into a TYPE_PACK_EXPANSION or
16696 EXPR_PACK_EXPANSION. */
16697 argument = make_pack_expansion (argument);
16698 }
16699
16700 if (n_args == alloced)
16701 {
16702 alloced *= 2;
16703
16704 if (arg_ary == fixed_args)
16705 {
16706 arg_ary = XNEWVEC (tree, alloced);
16707 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16708 }
16709 else
16710 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16711 }
16712 arg_ary[n_args++] = argument;
16713 }
16714 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16715
16716 vec = make_tree_vec (n_args);
16717
16718 while (n_args--)
16719 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16720
16721 if (arg_ary != fixed_args)
16722 free (arg_ary);
16723 parser->non_integral_constant_expression_p = saved_non_ice_p;
16724 parser->integral_constant_expression_p = saved_ice_p;
16725 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16726 if (CHECKING_P)
16727 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16728 return vec;
16729 }
16730
16731 /* Parse a template-argument.
16732
16733 template-argument:
16734 assignment-expression
16735 type-id
16736 id-expression
16737
16738 The representation is that of an assignment-expression, type-id, or
16739 id-expression -- except that the qualified id-expression is
16740 evaluated, so that the value returned is either a DECL or an
16741 OVERLOAD.
16742
16743 Although the standard says "assignment-expression", it forbids
16744 throw-expressions or assignments in the template argument.
16745 Therefore, we use "conditional-expression" instead. */
16746
16747 static tree
16748 cp_parser_template_argument (cp_parser* parser)
16749 {
16750 tree argument;
16751 bool template_p;
16752 bool address_p;
16753 bool maybe_type_id = false;
16754 cp_token *token = NULL, *argument_start_token = NULL;
16755 location_t loc = 0;
16756 cp_id_kind idk;
16757
16758 /* There's really no way to know what we're looking at, so we just
16759 try each alternative in order.
16760
16761 [temp.arg]
16762
16763 In a template-argument, an ambiguity between a type-id and an
16764 expression is resolved to a type-id, regardless of the form of
16765 the corresponding template-parameter.
16766
16767 Therefore, we try a type-id first. */
16768 cp_parser_parse_tentatively (parser);
16769 argument = cp_parser_template_type_arg (parser);
16770 /* If there was no error parsing the type-id but the next token is a
16771 '>>', our behavior depends on which dialect of C++ we're
16772 parsing. In C++98, we probably found a typo for '> >'. But there
16773 are type-id which are also valid expressions. For instance:
16774
16775 struct X { int operator >> (int); };
16776 template <int V> struct Foo {};
16777 Foo<X () >> 5> r;
16778
16779 Here 'X()' is a valid type-id of a function type, but the user just
16780 wanted to write the expression "X() >> 5". Thus, we remember that we
16781 found a valid type-id, but we still try to parse the argument as an
16782 expression to see what happens.
16783
16784 In C++0x, the '>>' will be considered two separate '>'
16785 tokens. */
16786 if (!cp_parser_error_occurred (parser)
16787 && cxx_dialect == cxx98
16788 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16789 {
16790 maybe_type_id = true;
16791 cp_parser_abort_tentative_parse (parser);
16792 }
16793 else
16794 {
16795 /* If the next token isn't a `,' or a `>', then this argument wasn't
16796 really finished. This means that the argument is not a valid
16797 type-id. */
16798 if (!cp_parser_next_token_ends_template_argument_p (parser))
16799 cp_parser_error (parser, "expected template-argument");
16800 /* If that worked, we're done. */
16801 if (cp_parser_parse_definitely (parser))
16802 return argument;
16803 }
16804 /* We're still not sure what the argument will be. */
16805 cp_parser_parse_tentatively (parser);
16806 /* Try a template. */
16807 argument_start_token = cp_lexer_peek_token (parser->lexer);
16808 argument = cp_parser_id_expression (parser,
16809 /*template_keyword_p=*/false,
16810 /*check_dependency_p=*/true,
16811 &template_p,
16812 /*declarator_p=*/false,
16813 /*optional_p=*/false);
16814 /* If the next token isn't a `,' or a `>', then this argument wasn't
16815 really finished. */
16816 if (!cp_parser_next_token_ends_template_argument_p (parser))
16817 cp_parser_error (parser, "expected template-argument");
16818 if (!cp_parser_error_occurred (parser))
16819 {
16820 /* Figure out what is being referred to. If the id-expression
16821 was for a class template specialization, then we will have a
16822 TYPE_DECL at this point. There is no need to do name lookup
16823 at this point in that case. */
16824 if (TREE_CODE (argument) != TYPE_DECL)
16825 argument = cp_parser_lookup_name (parser, argument,
16826 none_type,
16827 /*is_template=*/template_p,
16828 /*is_namespace=*/false,
16829 /*check_dependency=*/true,
16830 /*ambiguous_decls=*/NULL,
16831 argument_start_token->location);
16832 /* Handle a constrained-type-specifier for a non-type template
16833 parameter. */
16834 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16835 argument = decl;
16836 else if (TREE_CODE (argument) != TEMPLATE_DECL
16837 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16838 cp_parser_error (parser, "expected template-name");
16839 }
16840 if (cp_parser_parse_definitely (parser))
16841 {
16842 if (TREE_DEPRECATED (argument))
16843 warn_deprecated_use (argument, NULL_TREE);
16844 return argument;
16845 }
16846 /* It must be a non-type argument. In C++17 any constant-expression is
16847 allowed. */
16848 if (cxx_dialect > cxx14)
16849 goto general_expr;
16850
16851 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16852
16853 -- an integral constant-expression of integral or enumeration
16854 type; or
16855
16856 -- the name of a non-type template-parameter; or
16857
16858 -- the name of an object or function with external linkage...
16859
16860 -- the address of an object or function with external linkage...
16861
16862 -- a pointer to member... */
16863 /* Look for a non-type template parameter. */
16864 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16865 {
16866 cp_parser_parse_tentatively (parser);
16867 argument = cp_parser_primary_expression (parser,
16868 /*address_p=*/false,
16869 /*cast_p=*/false,
16870 /*template_arg_p=*/true,
16871 &idk);
16872 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16873 || !cp_parser_next_token_ends_template_argument_p (parser))
16874 cp_parser_simulate_error (parser);
16875 if (cp_parser_parse_definitely (parser))
16876 return argument;
16877 }
16878
16879 /* If the next token is "&", the argument must be the address of an
16880 object or function with external linkage. */
16881 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16882 if (address_p)
16883 {
16884 loc = cp_lexer_peek_token (parser->lexer)->location;
16885 cp_lexer_consume_token (parser->lexer);
16886 }
16887 /* See if we might have an id-expression. */
16888 token = cp_lexer_peek_token (parser->lexer);
16889 if (token->type == CPP_NAME
16890 || token->keyword == RID_OPERATOR
16891 || token->type == CPP_SCOPE
16892 || token->type == CPP_TEMPLATE_ID
16893 || token->type == CPP_NESTED_NAME_SPECIFIER)
16894 {
16895 cp_parser_parse_tentatively (parser);
16896 argument = cp_parser_primary_expression (parser,
16897 address_p,
16898 /*cast_p=*/false,
16899 /*template_arg_p=*/true,
16900 &idk);
16901 if (cp_parser_error_occurred (parser)
16902 || !cp_parser_next_token_ends_template_argument_p (parser))
16903 cp_parser_abort_tentative_parse (parser);
16904 else
16905 {
16906 tree probe;
16907
16908 if (INDIRECT_REF_P (argument))
16909 {
16910 /* Strip the dereference temporarily. */
16911 gcc_assert (REFERENCE_REF_P (argument));
16912 argument = TREE_OPERAND (argument, 0);
16913 }
16914
16915 /* If we're in a template, we represent a qualified-id referring
16916 to a static data member as a SCOPE_REF even if the scope isn't
16917 dependent so that we can check access control later. */
16918 probe = argument;
16919 if (TREE_CODE (probe) == SCOPE_REF)
16920 probe = TREE_OPERAND (probe, 1);
16921 if (VAR_P (probe))
16922 {
16923 /* A variable without external linkage might still be a
16924 valid constant-expression, so no error is issued here
16925 if the external-linkage check fails. */
16926 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16927 cp_parser_simulate_error (parser);
16928 }
16929 else if (is_overloaded_fn (argument))
16930 /* All overloaded functions are allowed; if the external
16931 linkage test does not pass, an error will be issued
16932 later. */
16933 ;
16934 else if (address_p
16935 && (TREE_CODE (argument) == OFFSET_REF
16936 || TREE_CODE (argument) == SCOPE_REF))
16937 /* A pointer-to-member. */
16938 ;
16939 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16940 ;
16941 else
16942 cp_parser_simulate_error (parser);
16943
16944 if (cp_parser_parse_definitely (parser))
16945 {
16946 if (address_p)
16947 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16948 tf_warning_or_error);
16949 else
16950 argument = convert_from_reference (argument);
16951 return argument;
16952 }
16953 }
16954 }
16955 /* If the argument started with "&", there are no other valid
16956 alternatives at this point. */
16957 if (address_p)
16958 {
16959 cp_parser_error (parser, "invalid non-type template argument");
16960 return error_mark_node;
16961 }
16962
16963 general_expr:
16964 /* If the argument wasn't successfully parsed as a type-id followed
16965 by '>>', the argument can only be a constant expression now.
16966 Otherwise, we try parsing the constant-expression tentatively,
16967 because the argument could really be a type-id. */
16968 if (maybe_type_id)
16969 cp_parser_parse_tentatively (parser);
16970
16971 if (cxx_dialect <= cxx14)
16972 argument = cp_parser_constant_expression (parser);
16973 else
16974 {
16975 /* With C++17 generalized non-type template arguments we need to handle
16976 lvalue constant expressions, too. */
16977 argument = cp_parser_assignment_expression (parser);
16978 require_potential_constant_expression (argument);
16979 }
16980
16981 if (!maybe_type_id)
16982 return argument;
16983 if (!cp_parser_next_token_ends_template_argument_p (parser))
16984 cp_parser_error (parser, "expected template-argument");
16985 if (cp_parser_parse_definitely (parser))
16986 return argument;
16987 /* We did our best to parse the argument as a non type-id, but that
16988 was the only alternative that matched (albeit with a '>' after
16989 it). We can assume it's just a typo from the user, and a
16990 diagnostic will then be issued. */
16991 return cp_parser_template_type_arg (parser);
16992 }
16993
16994 /* Parse an explicit-instantiation.
16995
16996 explicit-instantiation:
16997 template declaration
16998
16999 Although the standard says `declaration', what it really means is:
17000
17001 explicit-instantiation:
17002 template decl-specifier-seq [opt] declarator [opt] ;
17003
17004 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17005 supposed to be allowed. A defect report has been filed about this
17006 issue.
17007
17008 GNU Extension:
17009
17010 explicit-instantiation:
17011 storage-class-specifier template
17012 decl-specifier-seq [opt] declarator [opt] ;
17013 function-specifier template
17014 decl-specifier-seq [opt] declarator [opt] ; */
17015
17016 static void
17017 cp_parser_explicit_instantiation (cp_parser* parser)
17018 {
17019 int declares_class_or_enum;
17020 cp_decl_specifier_seq decl_specifiers;
17021 tree extension_specifier = NULL_TREE;
17022
17023 timevar_push (TV_TEMPLATE_INST);
17024
17025 /* Look for an (optional) storage-class-specifier or
17026 function-specifier. */
17027 if (cp_parser_allow_gnu_extensions_p (parser))
17028 {
17029 extension_specifier
17030 = cp_parser_storage_class_specifier_opt (parser);
17031 if (!extension_specifier)
17032 extension_specifier
17033 = cp_parser_function_specifier_opt (parser,
17034 /*decl_specs=*/NULL);
17035 }
17036
17037 /* Look for the `template' keyword. */
17038 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17039 /* Let the front end know that we are processing an explicit
17040 instantiation. */
17041 begin_explicit_instantiation ();
17042 /* [temp.explicit] says that we are supposed to ignore access
17043 control while processing explicit instantiation directives. */
17044 push_deferring_access_checks (dk_no_check);
17045 /* Parse a decl-specifier-seq. */
17046 cp_parser_decl_specifier_seq (parser,
17047 CP_PARSER_FLAGS_OPTIONAL,
17048 &decl_specifiers,
17049 &declares_class_or_enum);
17050 /* If there was exactly one decl-specifier, and it declared a class,
17051 and there's no declarator, then we have an explicit type
17052 instantiation. */
17053 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17054 {
17055 tree type;
17056
17057 type = check_tag_decl (&decl_specifiers,
17058 /*explicit_type_instantiation_p=*/true);
17059 /* Turn access control back on for names used during
17060 template instantiation. */
17061 pop_deferring_access_checks ();
17062 if (type)
17063 do_type_instantiation (type, extension_specifier,
17064 /*complain=*/tf_error);
17065 }
17066 else
17067 {
17068 cp_declarator *declarator;
17069 tree decl;
17070
17071 /* Parse the declarator. */
17072 declarator
17073 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17074 CP_PARSER_FLAGS_NONE,
17075 /*ctor_dtor_or_conv_p=*/NULL,
17076 /*parenthesized_p=*/NULL,
17077 /*member_p=*/false,
17078 /*friend_p=*/false);
17079 if (declares_class_or_enum & 2)
17080 cp_parser_check_for_definition_in_return_type (declarator,
17081 decl_specifiers.type,
17082 decl_specifiers.locations[ds_type_spec]);
17083 if (declarator != cp_error_declarator)
17084 {
17085 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17086 permerror (decl_specifiers.locations[ds_inline],
17087 "explicit instantiation shall not use"
17088 " %<inline%> specifier");
17089 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17090 permerror (decl_specifiers.locations[ds_constexpr],
17091 "explicit instantiation shall not use"
17092 " %<constexpr%> specifier");
17093
17094 decl = grokdeclarator (declarator, &decl_specifiers,
17095 NORMAL, 0, &decl_specifiers.attributes);
17096 /* Turn access control back on for names used during
17097 template instantiation. */
17098 pop_deferring_access_checks ();
17099 /* Do the explicit instantiation. */
17100 do_decl_instantiation (decl, extension_specifier);
17101 }
17102 else
17103 {
17104 pop_deferring_access_checks ();
17105 /* Skip the body of the explicit instantiation. */
17106 cp_parser_skip_to_end_of_statement (parser);
17107 }
17108 }
17109 /* We're done with the instantiation. */
17110 end_explicit_instantiation ();
17111
17112 cp_parser_consume_semicolon_at_end_of_statement (parser);
17113
17114 timevar_pop (TV_TEMPLATE_INST);
17115 }
17116
17117 /* Parse an explicit-specialization.
17118
17119 explicit-specialization:
17120 template < > declaration
17121
17122 Although the standard says `declaration', what it really means is:
17123
17124 explicit-specialization:
17125 template <> decl-specifier [opt] init-declarator [opt] ;
17126 template <> function-definition
17127 template <> explicit-specialization
17128 template <> template-declaration */
17129
17130 static void
17131 cp_parser_explicit_specialization (cp_parser* parser)
17132 {
17133 bool need_lang_pop;
17134 cp_token *token = cp_lexer_peek_token (parser->lexer);
17135
17136 /* Look for the `template' keyword. */
17137 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17138 /* Look for the `<'. */
17139 cp_parser_require (parser, CPP_LESS, RT_LESS);
17140 /* Look for the `>'. */
17141 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17142 /* We have processed another parameter list. */
17143 ++parser->num_template_parameter_lists;
17144 /* [temp]
17145
17146 A template ... explicit specialization ... shall not have C
17147 linkage. */
17148 if (current_lang_name == lang_name_c)
17149 {
17150 error_at (token->location, "template specialization with C linkage");
17151 maybe_show_extern_c_location ();
17152 /* Give it C++ linkage to avoid confusing other parts of the
17153 front end. */
17154 push_lang_context (lang_name_cplusplus);
17155 need_lang_pop = true;
17156 }
17157 else
17158 need_lang_pop = false;
17159 /* Let the front end know that we are beginning a specialization. */
17160 if (!begin_specialization ())
17161 {
17162 end_specialization ();
17163 return;
17164 }
17165
17166 /* If the next keyword is `template', we need to figure out whether
17167 or not we're looking a template-declaration. */
17168 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17169 {
17170 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17171 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17172 cp_parser_template_declaration_after_export (parser,
17173 /*member_p=*/false);
17174 else
17175 cp_parser_explicit_specialization (parser);
17176 }
17177 else
17178 /* Parse the dependent declaration. */
17179 cp_parser_single_declaration (parser,
17180 /*checks=*/NULL,
17181 /*member_p=*/false,
17182 /*explicit_specialization_p=*/true,
17183 /*friend_p=*/NULL);
17184 /* We're done with the specialization. */
17185 end_specialization ();
17186 /* For the erroneous case of a template with C linkage, we pushed an
17187 implicit C++ linkage scope; exit that scope now. */
17188 if (need_lang_pop)
17189 pop_lang_context ();
17190 /* We're done with this parameter list. */
17191 --parser->num_template_parameter_lists;
17192 }
17193
17194 /* Parse a type-specifier.
17195
17196 type-specifier:
17197 simple-type-specifier
17198 class-specifier
17199 enum-specifier
17200 elaborated-type-specifier
17201 cv-qualifier
17202
17203 GNU Extension:
17204
17205 type-specifier:
17206 __complex__
17207
17208 Returns a representation of the type-specifier. For a
17209 class-specifier, enum-specifier, or elaborated-type-specifier, a
17210 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17211
17212 The parser flags FLAGS is used to control type-specifier parsing.
17213
17214 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17215 in a decl-specifier-seq.
17216
17217 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17218 class-specifier, enum-specifier, or elaborated-type-specifier, then
17219 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17220 if a type is declared; 2 if it is defined. Otherwise, it is set to
17221 zero.
17222
17223 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17224 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17225 is set to FALSE. */
17226
17227 static tree
17228 cp_parser_type_specifier (cp_parser* parser,
17229 cp_parser_flags flags,
17230 cp_decl_specifier_seq *decl_specs,
17231 bool is_declaration,
17232 int* declares_class_or_enum,
17233 bool* is_cv_qualifier)
17234 {
17235 tree type_spec = NULL_TREE;
17236 cp_token *token;
17237 enum rid keyword;
17238 cp_decl_spec ds = ds_last;
17239
17240 /* Assume this type-specifier does not declare a new type. */
17241 if (declares_class_or_enum)
17242 *declares_class_or_enum = 0;
17243 /* And that it does not specify a cv-qualifier. */
17244 if (is_cv_qualifier)
17245 *is_cv_qualifier = false;
17246 /* Peek at the next token. */
17247 token = cp_lexer_peek_token (parser->lexer);
17248
17249 /* If we're looking at a keyword, we can use that to guide the
17250 production we choose. */
17251 keyword = token->keyword;
17252 switch (keyword)
17253 {
17254 case RID_ENUM:
17255 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17256 goto elaborated_type_specifier;
17257
17258 /* Look for the enum-specifier. */
17259 type_spec = cp_parser_enum_specifier (parser);
17260 /* If that worked, we're done. */
17261 if (type_spec)
17262 {
17263 if (declares_class_or_enum)
17264 *declares_class_or_enum = 2;
17265 if (decl_specs)
17266 cp_parser_set_decl_spec_type (decl_specs,
17267 type_spec,
17268 token,
17269 /*type_definition_p=*/true);
17270 return type_spec;
17271 }
17272 else
17273 goto elaborated_type_specifier;
17274
17275 /* Any of these indicate either a class-specifier, or an
17276 elaborated-type-specifier. */
17277 case RID_CLASS:
17278 case RID_STRUCT:
17279 case RID_UNION:
17280 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17281 goto elaborated_type_specifier;
17282
17283 /* Parse tentatively so that we can back up if we don't find a
17284 class-specifier. */
17285 cp_parser_parse_tentatively (parser);
17286 /* Look for the class-specifier. */
17287 type_spec = cp_parser_class_specifier (parser);
17288 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17289 /* If that worked, we're done. */
17290 if (cp_parser_parse_definitely (parser))
17291 {
17292 if (declares_class_or_enum)
17293 *declares_class_or_enum = 2;
17294 if (decl_specs)
17295 cp_parser_set_decl_spec_type (decl_specs,
17296 type_spec,
17297 token,
17298 /*type_definition_p=*/true);
17299 return type_spec;
17300 }
17301
17302 /* Fall through. */
17303 elaborated_type_specifier:
17304 /* We're declaring (not defining) a class or enum. */
17305 if (declares_class_or_enum)
17306 *declares_class_or_enum = 1;
17307
17308 /* Fall through. */
17309 case RID_TYPENAME:
17310 /* Look for an elaborated-type-specifier. */
17311 type_spec
17312 = (cp_parser_elaborated_type_specifier
17313 (parser,
17314 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17315 is_declaration));
17316 if (decl_specs)
17317 cp_parser_set_decl_spec_type (decl_specs,
17318 type_spec,
17319 token,
17320 /*type_definition_p=*/false);
17321 return type_spec;
17322
17323 case RID_CONST:
17324 ds = ds_const;
17325 if (is_cv_qualifier)
17326 *is_cv_qualifier = true;
17327 break;
17328
17329 case RID_VOLATILE:
17330 ds = ds_volatile;
17331 if (is_cv_qualifier)
17332 *is_cv_qualifier = true;
17333 break;
17334
17335 case RID_RESTRICT:
17336 ds = ds_restrict;
17337 if (is_cv_qualifier)
17338 *is_cv_qualifier = true;
17339 break;
17340
17341 case RID_COMPLEX:
17342 /* The `__complex__' keyword is a GNU extension. */
17343 ds = ds_complex;
17344 break;
17345
17346 default:
17347 break;
17348 }
17349
17350 /* Handle simple keywords. */
17351 if (ds != ds_last)
17352 {
17353 if (decl_specs)
17354 {
17355 set_and_check_decl_spec_loc (decl_specs, ds, token);
17356 decl_specs->any_specifiers_p = true;
17357 }
17358 return cp_lexer_consume_token (parser->lexer)->u.value;
17359 }
17360
17361 /* If we do not already have a type-specifier, assume we are looking
17362 at a simple-type-specifier. */
17363 type_spec = cp_parser_simple_type_specifier (parser,
17364 decl_specs,
17365 flags);
17366
17367 /* If we didn't find a type-specifier, and a type-specifier was not
17368 optional in this context, issue an error message. */
17369 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17370 {
17371 cp_parser_error (parser, "expected type specifier");
17372 return error_mark_node;
17373 }
17374
17375 return type_spec;
17376 }
17377
17378 /* Parse a simple-type-specifier.
17379
17380 simple-type-specifier:
17381 :: [opt] nested-name-specifier [opt] type-name
17382 :: [opt] nested-name-specifier template template-id
17383 char
17384 wchar_t
17385 bool
17386 short
17387 int
17388 long
17389 signed
17390 unsigned
17391 float
17392 double
17393 void
17394
17395 C++11 Extension:
17396
17397 simple-type-specifier:
17398 auto
17399 decltype ( expression )
17400 char16_t
17401 char32_t
17402 __underlying_type ( type-id )
17403
17404 C++17 extension:
17405
17406 nested-name-specifier(opt) template-name
17407
17408 GNU Extension:
17409
17410 simple-type-specifier:
17411 __int128
17412 __typeof__ unary-expression
17413 __typeof__ ( type-id )
17414 __typeof__ ( type-id ) { initializer-list , [opt] }
17415
17416 Concepts Extension:
17417
17418 simple-type-specifier:
17419 constrained-type-specifier
17420
17421 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17422 appropriately updated. */
17423
17424 static tree
17425 cp_parser_simple_type_specifier (cp_parser* parser,
17426 cp_decl_specifier_seq *decl_specs,
17427 cp_parser_flags flags)
17428 {
17429 tree type = NULL_TREE;
17430 cp_token *token;
17431 int idx;
17432
17433 /* Peek at the next token. */
17434 token = cp_lexer_peek_token (parser->lexer);
17435
17436 /* If we're looking at a keyword, things are easy. */
17437 switch (token->keyword)
17438 {
17439 case RID_CHAR:
17440 if (decl_specs)
17441 decl_specs->explicit_char_p = true;
17442 type = char_type_node;
17443 break;
17444 case RID_CHAR16:
17445 type = char16_type_node;
17446 break;
17447 case RID_CHAR32:
17448 type = char32_type_node;
17449 break;
17450 case RID_WCHAR:
17451 type = wchar_type_node;
17452 break;
17453 case RID_BOOL:
17454 type = boolean_type_node;
17455 break;
17456 case RID_SHORT:
17457 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17458 type = short_integer_type_node;
17459 break;
17460 case RID_INT:
17461 if (decl_specs)
17462 decl_specs->explicit_int_p = true;
17463 type = integer_type_node;
17464 break;
17465 case RID_INT_N_0:
17466 case RID_INT_N_1:
17467 case RID_INT_N_2:
17468 case RID_INT_N_3:
17469 idx = token->keyword - RID_INT_N_0;
17470 if (! int_n_enabled_p [idx])
17471 break;
17472 if (decl_specs)
17473 {
17474 decl_specs->explicit_intN_p = true;
17475 decl_specs->int_n_idx = idx;
17476 }
17477 type = int_n_trees [idx].signed_type;
17478 break;
17479 case RID_LONG:
17480 if (decl_specs)
17481 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17482 type = long_integer_type_node;
17483 break;
17484 case RID_SIGNED:
17485 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17486 type = integer_type_node;
17487 break;
17488 case RID_UNSIGNED:
17489 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17490 type = unsigned_type_node;
17491 break;
17492 case RID_FLOAT:
17493 type = float_type_node;
17494 break;
17495 case RID_DOUBLE:
17496 type = double_type_node;
17497 break;
17498 case RID_VOID:
17499 type = void_type_node;
17500 break;
17501
17502 case RID_AUTO:
17503 maybe_warn_cpp0x (CPP0X_AUTO);
17504 if (parser->auto_is_implicit_function_template_parm_p)
17505 {
17506 /* The 'auto' might be the placeholder return type for a function decl
17507 with trailing return type. */
17508 bool have_trailing_return_fn_decl = false;
17509
17510 cp_parser_parse_tentatively (parser);
17511 cp_lexer_consume_token (parser->lexer);
17512 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17513 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17514 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17515 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17516 {
17517 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17518 {
17519 cp_lexer_consume_token (parser->lexer);
17520 cp_parser_skip_to_closing_parenthesis (parser,
17521 /*recovering*/false,
17522 /*or_comma*/false,
17523 /*consume_paren*/true);
17524 continue;
17525 }
17526
17527 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17528 {
17529 have_trailing_return_fn_decl = true;
17530 break;
17531 }
17532
17533 cp_lexer_consume_token (parser->lexer);
17534 }
17535 cp_parser_abort_tentative_parse (parser);
17536
17537 if (have_trailing_return_fn_decl)
17538 {
17539 type = make_auto ();
17540 break;
17541 }
17542
17543 if (cxx_dialect >= cxx14)
17544 {
17545 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17546 type = TREE_TYPE (type);
17547 }
17548 else
17549 type = error_mark_node;
17550
17551 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17552 {
17553 if (cxx_dialect < cxx14)
17554 error_at (token->location,
17555 "use of %<auto%> in lambda parameter declaration "
17556 "only available with "
17557 "-std=c++14 or -std=gnu++14");
17558 }
17559 else if (cxx_dialect < cxx14)
17560 error_at (token->location,
17561 "use of %<auto%> in parameter declaration "
17562 "only available with "
17563 "-std=c++14 or -std=gnu++14");
17564 else if (!flag_concepts)
17565 pedwarn (token->location, 0,
17566 "use of %<auto%> in parameter declaration "
17567 "only available with -fconcepts");
17568 }
17569 else
17570 type = make_auto ();
17571 break;
17572
17573 case RID_DECLTYPE:
17574 /* Since DR 743, decltype can either be a simple-type-specifier by
17575 itself or begin a nested-name-specifier. Parsing it will replace
17576 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17577 handling below decide what to do. */
17578 cp_parser_decltype (parser);
17579 cp_lexer_set_token_position (parser->lexer, token);
17580 break;
17581
17582 case RID_TYPEOF:
17583 /* Consume the `typeof' token. */
17584 cp_lexer_consume_token (parser->lexer);
17585 /* Parse the operand to `typeof'. */
17586 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17587 /* If it is not already a TYPE, take its type. */
17588 if (!TYPE_P (type))
17589 type = finish_typeof (type);
17590
17591 if (decl_specs)
17592 cp_parser_set_decl_spec_type (decl_specs, type,
17593 token,
17594 /*type_definition_p=*/false);
17595
17596 return type;
17597
17598 case RID_UNDERLYING_TYPE:
17599 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17600 if (decl_specs)
17601 cp_parser_set_decl_spec_type (decl_specs, type,
17602 token,
17603 /*type_definition_p=*/false);
17604
17605 return type;
17606
17607 case RID_BASES:
17608 case RID_DIRECT_BASES:
17609 type = cp_parser_trait_expr (parser, token->keyword);
17610 if (decl_specs)
17611 cp_parser_set_decl_spec_type (decl_specs, type,
17612 token,
17613 /*type_definition_p=*/false);
17614 return type;
17615 default:
17616 break;
17617 }
17618
17619 /* If token is an already-parsed decltype not followed by ::,
17620 it's a simple-type-specifier. */
17621 if (token->type == CPP_DECLTYPE
17622 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17623 {
17624 type = saved_checks_value (token->u.tree_check_value);
17625 if (decl_specs)
17626 {
17627 cp_parser_set_decl_spec_type (decl_specs, type,
17628 token,
17629 /*type_definition_p=*/false);
17630 /* Remember that we are handling a decltype in order to
17631 implement the resolution of DR 1510 when the argument
17632 isn't instantiation dependent. */
17633 decl_specs->decltype_p = true;
17634 }
17635 cp_lexer_consume_token (parser->lexer);
17636 return type;
17637 }
17638
17639 /* If the type-specifier was for a built-in type, we're done. */
17640 if (type)
17641 {
17642 /* Record the type. */
17643 if (decl_specs
17644 && (token->keyword != RID_SIGNED
17645 && token->keyword != RID_UNSIGNED
17646 && token->keyword != RID_SHORT
17647 && token->keyword != RID_LONG))
17648 cp_parser_set_decl_spec_type (decl_specs,
17649 type,
17650 token,
17651 /*type_definition_p=*/false);
17652 if (decl_specs)
17653 decl_specs->any_specifiers_p = true;
17654
17655 /* Consume the token. */
17656 cp_lexer_consume_token (parser->lexer);
17657
17658 if (type == error_mark_node)
17659 return error_mark_node;
17660
17661 /* There is no valid C++ program where a non-template type is
17662 followed by a "<". That usually indicates that the user thought
17663 that the type was a template. */
17664 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17665 token->location);
17666
17667 return TYPE_NAME (type);
17668 }
17669
17670 /* The type-specifier must be a user-defined type. */
17671 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17672 {
17673 bool qualified_p;
17674 bool global_p;
17675 const bool typename_p = (cxx_dialect >= cxx2a
17676 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17677
17678 /* Don't gobble tokens or issue error messages if this is an
17679 optional type-specifier. */
17680 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17681 cp_parser_parse_tentatively (parser);
17682
17683 token = cp_lexer_peek_token (parser->lexer);
17684
17685 /* Look for the optional `::' operator. */
17686 global_p
17687 = (cp_parser_global_scope_opt (parser,
17688 /*current_scope_valid_p=*/false)
17689 != NULL_TREE);
17690 /* Look for the nested-name specifier. */
17691 qualified_p
17692 = (cp_parser_nested_name_specifier_opt (parser,
17693 /*typename_keyword_p=*/false,
17694 /*check_dependency_p=*/true,
17695 /*type_p=*/false,
17696 /*is_declaration=*/false)
17697 != NULL_TREE);
17698 /* If we have seen a nested-name-specifier, and the next token
17699 is `template', then we are using the template-id production. */
17700 if (parser->scope
17701 && cp_parser_optional_template_keyword (parser))
17702 {
17703 /* Look for the template-id. */
17704 type = cp_parser_template_id (parser,
17705 /*template_keyword_p=*/true,
17706 /*check_dependency_p=*/true,
17707 none_type,
17708 /*is_declaration=*/false);
17709 /* If the template-id did not name a type, we are out of
17710 luck. */
17711 if (TREE_CODE (type) != TYPE_DECL)
17712 {
17713 /* ...unless we pretend we have seen 'typename'. */
17714 if (typename_p)
17715 type = cp_parser_make_typename_type (parser, type,
17716 token->location);
17717 else
17718 {
17719 cp_parser_error (parser, "expected template-id for type");
17720 type = NULL_TREE;
17721 }
17722 }
17723 }
17724 /* Otherwise, look for a type-name. */
17725 else
17726 type = cp_parser_type_name (parser, (qualified_p && typename_p));
17727
17728 /* Keep track of all name-lookups performed in class scopes. */
17729 if (type
17730 && !global_p
17731 && !qualified_p
17732 && TREE_CODE (type) == TYPE_DECL
17733 && identifier_p (DECL_NAME (type)))
17734 maybe_note_name_used_in_class (DECL_NAME (type), type);
17735 /* If it didn't work out, we don't have a TYPE. */
17736 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17737 && !cp_parser_parse_definitely (parser))
17738 type = NULL_TREE;
17739 if (!type && cxx_dialect >= cxx17)
17740 {
17741 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17742 cp_parser_parse_tentatively (parser);
17743
17744 cp_parser_global_scope_opt (parser,
17745 /*current_scope_valid_p=*/false);
17746 cp_parser_nested_name_specifier_opt (parser,
17747 /*typename_keyword_p=*/false,
17748 /*check_dependency_p=*/true,
17749 /*type_p=*/false,
17750 /*is_declaration=*/false);
17751 tree name = cp_parser_identifier (parser);
17752 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17753 && parser->scope != error_mark_node)
17754 {
17755 tree tmpl = cp_parser_lookup_name (parser, name,
17756 none_type,
17757 /*is_template=*/false,
17758 /*is_namespace=*/false,
17759 /*check_dependency=*/true,
17760 /*ambiguous_decls=*/NULL,
17761 token->location);
17762 if (tmpl && tmpl != error_mark_node
17763 && (DECL_CLASS_TEMPLATE_P (tmpl)
17764 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17765 type = make_template_placeholder (tmpl);
17766 else
17767 {
17768 type = error_mark_node;
17769 if (!cp_parser_simulate_error (parser))
17770 cp_parser_name_lookup_error (parser, name, tmpl,
17771 NLE_TYPE, token->location);
17772 }
17773 }
17774 else
17775 type = error_mark_node;
17776
17777 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17778 && !cp_parser_parse_definitely (parser))
17779 type = NULL_TREE;
17780 }
17781 if (type && decl_specs)
17782 cp_parser_set_decl_spec_type (decl_specs, type,
17783 token,
17784 /*type_definition_p=*/false);
17785 }
17786
17787 /* If we didn't get a type-name, issue an error message. */
17788 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17789 {
17790 cp_parser_error (parser, "expected type-name");
17791 return error_mark_node;
17792 }
17793
17794 if (type && type != error_mark_node)
17795 {
17796 /* See if TYPE is an Objective-C type, and if so, parse and
17797 accept any protocol references following it. Do this before
17798 the cp_parser_check_for_invalid_template_id() call, because
17799 Objective-C types can be followed by '<...>' which would
17800 enclose protocol names rather than template arguments, and so
17801 everything is fine. */
17802 if (c_dialect_objc () && !parser->scope
17803 && (objc_is_id (type) || objc_is_class_name (type)))
17804 {
17805 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17806 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17807
17808 /* Clobber the "unqualified" type previously entered into
17809 DECL_SPECS with the new, improved protocol-qualified version. */
17810 if (decl_specs)
17811 decl_specs->type = qual_type;
17812
17813 return qual_type;
17814 }
17815
17816 /* There is no valid C++ program where a non-template type is
17817 followed by a "<". That usually indicates that the user
17818 thought that the type was a template. */
17819 cp_parser_check_for_invalid_template_id (parser, type,
17820 none_type,
17821 token->location);
17822 }
17823
17824 return type;
17825 }
17826
17827 /* Parse a type-name.
17828
17829 type-name:
17830 class-name
17831 enum-name
17832 typedef-name
17833 simple-template-id [in c++0x]
17834
17835 enum-name:
17836 identifier
17837
17838 typedef-name:
17839 identifier
17840
17841 Concepts:
17842
17843 type-name:
17844 concept-name
17845 partial-concept-id
17846
17847 concept-name:
17848 identifier
17849
17850 Returns a TYPE_DECL for the type. */
17851
17852 static tree
17853 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17854 {
17855 tree type_decl;
17856
17857 /* We can't know yet whether it is a class-name or not. */
17858 cp_parser_parse_tentatively (parser);
17859 /* Try a class-name. */
17860 type_decl = cp_parser_class_name (parser,
17861 typename_keyword_p,
17862 /*template_keyword_p=*/false,
17863 none_type,
17864 /*check_dependency_p=*/true,
17865 /*class_head_p=*/false,
17866 /*is_declaration=*/false);
17867 /* If it's not a class-name, keep looking. */
17868 if (!cp_parser_parse_definitely (parser))
17869 {
17870 if (cxx_dialect < cxx11)
17871 /* It must be a typedef-name or an enum-name. */
17872 return cp_parser_nonclass_name (parser);
17873
17874 cp_parser_parse_tentatively (parser);
17875 /* It is either a simple-template-id representing an
17876 instantiation of an alias template... */
17877 type_decl = cp_parser_template_id (parser,
17878 /*template_keyword_p=*/false,
17879 /*check_dependency_p=*/true,
17880 none_type,
17881 /*is_declaration=*/false);
17882 /* Note that this must be an instantiation of an alias template
17883 because [temp.names]/6 says:
17884
17885 A template-id that names an alias template specialization
17886 is a type-name.
17887
17888 Whereas [temp.names]/7 says:
17889
17890 A simple-template-id that names a class template
17891 specialization is a class-name.
17892
17893 With concepts, this could also be a partial-concept-id that
17894 declares a non-type template parameter. */
17895 if (type_decl != NULL_TREE
17896 && TREE_CODE (type_decl) == TYPE_DECL
17897 && TYPE_DECL_ALIAS_P (type_decl))
17898 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17899 else if (is_constrained_parameter (type_decl))
17900 /* Don't do anything. */ ;
17901 else
17902 cp_parser_simulate_error (parser);
17903
17904 if (!cp_parser_parse_definitely (parser))
17905 /* ... Or a typedef-name or an enum-name. */
17906 return cp_parser_nonclass_name (parser);
17907 }
17908
17909 return type_decl;
17910 }
17911
17912 /* Check if DECL and ARGS can form a constrained-type-specifier.
17913 If ARGS is non-null, we try to form a concept check of the
17914 form DECL<?, ARGS> where ? is a wildcard that matches any
17915 kind of template argument. If ARGS is NULL, then we try to
17916 form a concept check of the form DECL<?>. */
17917
17918 static tree
17919 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17920 tree decl, tree args)
17921 {
17922 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17923
17924 /* If we a constrained-type-specifier cannot be deduced. */
17925 if (parser->prevent_constrained_type_specifiers)
17926 return NULL_TREE;
17927
17928 /* A constrained type specifier can only be found in an
17929 overload set or as a reference to a template declaration.
17930
17931 FIXME: This might be masking a bug. It's possible that
17932 that the deduction below is causing template specializations
17933 to be formed with the wildcard as an argument. */
17934 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17935 return NULL_TREE;
17936
17937 /* Try to build a call expression that evaluates the
17938 concept. This can fail if the overload set refers
17939 only to non-templates. */
17940 tree placeholder = build_nt (WILDCARD_DECL);
17941 tree check = build_concept_check (decl, placeholder, args);
17942 if (check == error_mark_node)
17943 return NULL_TREE;
17944
17945 /* Deduce the checked constraint and the prototype parameter.
17946
17947 FIXME: In certain cases, failure to deduce should be a
17948 diagnosable error. */
17949 tree conc;
17950 tree proto;
17951 if (!deduce_constrained_parameter (check, conc, proto))
17952 return NULL_TREE;
17953
17954 /* In template parameter scope, this results in a constrained
17955 parameter. Return a descriptor of that parm. */
17956 if (processing_template_parmlist)
17957 return build_constrained_parameter (conc, proto, args);
17958
17959 /* In a parameter-declaration-clause, constrained-type
17960 specifiers result in invented template parameters. */
17961 if (parser->auto_is_implicit_function_template_parm_p)
17962 {
17963 tree x = build_constrained_parameter (conc, proto, args);
17964 return synthesize_implicit_template_parm (parser, x);
17965 }
17966 else
17967 {
17968 /* Otherwise, we're in a context where the constrained
17969 type name is deduced and the constraint applies
17970 after deduction. */
17971 return make_constrained_auto (conc, args);
17972 }
17973
17974 return NULL_TREE;
17975 }
17976
17977 /* If DECL refers to a concept, return a TYPE_DECL representing
17978 the result of using the constrained type specifier in the
17979 current context. DECL refers to a concept if
17980
17981 - it is an overload set containing a function concept taking a single
17982 type argument, or
17983
17984 - it is a variable concept taking a single type argument. */
17985
17986 static tree
17987 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17988 {
17989 if (flag_concepts
17990 && (TREE_CODE (decl) == OVERLOAD
17991 || BASELINK_P (decl)
17992 || variable_concept_p (decl)))
17993 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17994 else
17995 return NULL_TREE;
17996 }
17997
17998 /* Check if DECL and ARGS form a partial-concept-id. If so,
17999 assign ID to the resulting constrained placeholder.
18000
18001 Returns true if the partial-concept-id designates a placeholder
18002 and false otherwise. Note that *id is set to NULL_TREE in
18003 this case. */
18004
18005 static tree
18006 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
18007 {
18008 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
18009 }
18010
18011 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18012 or a concept-name.
18013
18014 enum-name:
18015 identifier
18016
18017 typedef-name:
18018 identifier
18019
18020 concept-name:
18021 identifier
18022
18023 Returns a TYPE_DECL for the type. */
18024
18025 static tree
18026 cp_parser_nonclass_name (cp_parser* parser)
18027 {
18028 tree type_decl;
18029 tree identifier;
18030
18031 cp_token *token = cp_lexer_peek_token (parser->lexer);
18032 identifier = cp_parser_identifier (parser);
18033 if (identifier == error_mark_node)
18034 return error_mark_node;
18035
18036 /* Look up the type-name. */
18037 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18038
18039 type_decl = strip_using_decl (type_decl);
18040
18041 /* If we found an overload set, then it may refer to a concept-name. */
18042 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
18043 type_decl = decl;
18044
18045 if (TREE_CODE (type_decl) != TYPE_DECL
18046 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18047 {
18048 /* See if this is an Objective-C type. */
18049 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18050 tree type = objc_get_protocol_qualified_type (identifier, protos);
18051 if (type)
18052 type_decl = TYPE_NAME (type);
18053 }
18054
18055 /* Issue an error if we did not find a type-name. */
18056 if (TREE_CODE (type_decl) != TYPE_DECL
18057 /* In Objective-C, we have the complication that class names are
18058 normally type names and start declarations (eg, the
18059 "NSObject" in "NSObject *object;"), but can be used in an
18060 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18061 is an expression. So, a classname followed by a dot is not a
18062 valid type-name. */
18063 || (objc_is_class_name (TREE_TYPE (type_decl))
18064 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18065 {
18066 if (!cp_parser_simulate_error (parser))
18067 cp_parser_name_lookup_error (parser, identifier, type_decl,
18068 NLE_TYPE, token->location);
18069 return error_mark_node;
18070 }
18071 /* Remember that the name was used in the definition of the
18072 current class so that we can check later to see if the
18073 meaning would have been different after the class was
18074 entirely defined. */
18075 else if (type_decl != error_mark_node
18076 && !parser->scope)
18077 maybe_note_name_used_in_class (identifier, type_decl);
18078
18079 return type_decl;
18080 }
18081
18082 /* Parse an elaborated-type-specifier. Note that the grammar given
18083 here incorporates the resolution to DR68.
18084
18085 elaborated-type-specifier:
18086 class-key :: [opt] nested-name-specifier [opt] identifier
18087 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18088 enum-key :: [opt] nested-name-specifier [opt] identifier
18089 typename :: [opt] nested-name-specifier identifier
18090 typename :: [opt] nested-name-specifier template [opt]
18091 template-id
18092
18093 GNU extension:
18094
18095 elaborated-type-specifier:
18096 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18097 class-key attributes :: [opt] nested-name-specifier [opt]
18098 template [opt] template-id
18099 enum attributes :: [opt] nested-name-specifier [opt] identifier
18100
18101 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18102 declared `friend'. If IS_DECLARATION is TRUE, then this
18103 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18104 something is being declared.
18105
18106 Returns the TYPE specified. */
18107
18108 static tree
18109 cp_parser_elaborated_type_specifier (cp_parser* parser,
18110 bool is_friend,
18111 bool is_declaration)
18112 {
18113 enum tag_types tag_type;
18114 tree identifier;
18115 tree type = NULL_TREE;
18116 tree attributes = NULL_TREE;
18117 tree globalscope;
18118 cp_token *token = NULL;
18119
18120 /* See if we're looking at the `enum' keyword. */
18121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18122 {
18123 /* Consume the `enum' token. */
18124 cp_lexer_consume_token (parser->lexer);
18125 /* Remember that it's an enumeration type. */
18126 tag_type = enum_type;
18127 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18128 enums) is used here. */
18129 cp_token *token = cp_lexer_peek_token (parser->lexer);
18130 if (cp_parser_is_keyword (token, RID_CLASS)
18131 || cp_parser_is_keyword (token, RID_STRUCT))
18132 {
18133 gcc_rich_location richloc (token->location);
18134 richloc.add_range (input_location);
18135 richloc.add_fixit_remove ();
18136 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18137 "a scoped enum must not use the %qD keyword",
18138 token->u.value);
18139 /* Consume the `struct' or `class' and parse it anyway. */
18140 cp_lexer_consume_token (parser->lexer);
18141 }
18142 /* Parse the attributes. */
18143 attributes = cp_parser_attributes_opt (parser);
18144 }
18145 /* Or, it might be `typename'. */
18146 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18147 RID_TYPENAME))
18148 {
18149 /* Consume the `typename' token. */
18150 cp_lexer_consume_token (parser->lexer);
18151 /* Remember that it's a `typename' type. */
18152 tag_type = typename_type;
18153 }
18154 /* Otherwise it must be a class-key. */
18155 else
18156 {
18157 tag_type = cp_parser_class_key (parser);
18158 if (tag_type == none_type)
18159 return error_mark_node;
18160 /* Parse the attributes. */
18161 attributes = cp_parser_attributes_opt (parser);
18162 }
18163
18164 /* Look for the `::' operator. */
18165 globalscope = cp_parser_global_scope_opt (parser,
18166 /*current_scope_valid_p=*/false);
18167 /* Look for the nested-name-specifier. */
18168 tree nested_name_specifier;
18169 if (tag_type == typename_type && !globalscope)
18170 {
18171 nested_name_specifier
18172 = cp_parser_nested_name_specifier (parser,
18173 /*typename_keyword_p=*/true,
18174 /*check_dependency_p=*/true,
18175 /*type_p=*/true,
18176 is_declaration);
18177 if (!nested_name_specifier)
18178 return error_mark_node;
18179 }
18180 else
18181 /* Even though `typename' is not present, the proposed resolution
18182 to Core Issue 180 says that in `class A<T>::B', `B' should be
18183 considered a type-name, even if `A<T>' is dependent. */
18184 nested_name_specifier
18185 = cp_parser_nested_name_specifier_opt (parser,
18186 /*typename_keyword_p=*/true,
18187 /*check_dependency_p=*/true,
18188 /*type_p=*/true,
18189 is_declaration);
18190 /* For everything but enumeration types, consider a template-id.
18191 For an enumeration type, consider only a plain identifier. */
18192 if (tag_type != enum_type)
18193 {
18194 bool template_p = false;
18195 tree decl;
18196
18197 /* Allow the `template' keyword. */
18198 template_p = cp_parser_optional_template_keyword (parser);
18199 /* If we didn't see `template', we don't know if there's a
18200 template-id or not. */
18201 if (!template_p)
18202 cp_parser_parse_tentatively (parser);
18203 /* The `template' keyword must follow a nested-name-specifier. */
18204 else if (!nested_name_specifier)
18205 {
18206 cp_parser_error (parser, "%<template%> must follow a nested-"
18207 "name-specifier");
18208 return error_mark_node;
18209 }
18210
18211 /* Parse the template-id. */
18212 token = cp_lexer_peek_token (parser->lexer);
18213 decl = cp_parser_template_id (parser, template_p,
18214 /*check_dependency_p=*/true,
18215 tag_type,
18216 is_declaration);
18217 /* If we didn't find a template-id, look for an ordinary
18218 identifier. */
18219 if (!template_p && !cp_parser_parse_definitely (parser))
18220 ;
18221 /* We can get here when cp_parser_template_id, called by
18222 cp_parser_class_name with tag_type == none_type, succeeds
18223 and caches a BASELINK. Then, when called again here,
18224 instead of failing and returning an error_mark_node
18225 returns it (see template/typename17.C in C++11).
18226 ??? Could we diagnose this earlier? */
18227 else if (tag_type == typename_type && BASELINK_P (decl))
18228 {
18229 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18230 type = error_mark_node;
18231 }
18232 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18233 in effect, then we must assume that, upon instantiation, the
18234 template will correspond to a class. */
18235 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18236 && tag_type == typename_type)
18237 type = make_typename_type (parser->scope, decl,
18238 typename_type,
18239 /*complain=*/tf_error);
18240 /* If the `typename' keyword is in effect and DECL is not a type
18241 decl, then type is non existent. */
18242 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18243 ;
18244 else if (TREE_CODE (decl) == TYPE_DECL)
18245 {
18246 type = check_elaborated_type_specifier (tag_type, decl,
18247 /*allow_template_p=*/true);
18248
18249 /* If the next token is a semicolon, this must be a specialization,
18250 instantiation, or friend declaration. Check the scope while we
18251 still know whether or not we had a nested-name-specifier. */
18252 if (type != error_mark_node
18253 && !nested_name_specifier && !is_friend
18254 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18255 check_unqualified_spec_or_inst (type, token->location);
18256 }
18257 else if (decl == error_mark_node)
18258 type = error_mark_node;
18259 }
18260
18261 if (!type)
18262 {
18263 token = cp_lexer_peek_token (parser->lexer);
18264 identifier = cp_parser_identifier (parser);
18265
18266 if (identifier == error_mark_node)
18267 {
18268 parser->scope = NULL_TREE;
18269 return error_mark_node;
18270 }
18271
18272 /* For a `typename', we needn't call xref_tag. */
18273 if (tag_type == typename_type
18274 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18275 return cp_parser_make_typename_type (parser, identifier,
18276 token->location);
18277
18278 /* Template parameter lists apply only if we are not within a
18279 function parameter list. */
18280 bool template_parm_lists_apply
18281 = parser->num_template_parameter_lists;
18282 if (template_parm_lists_apply)
18283 for (cp_binding_level *s = current_binding_level;
18284 s && s->kind != sk_template_parms;
18285 s = s->level_chain)
18286 if (s->kind == sk_function_parms)
18287 template_parm_lists_apply = false;
18288
18289 /* Look up a qualified name in the usual way. */
18290 if (parser->scope)
18291 {
18292 tree decl;
18293 tree ambiguous_decls;
18294
18295 decl = cp_parser_lookup_name (parser, identifier,
18296 tag_type,
18297 /*is_template=*/false,
18298 /*is_namespace=*/false,
18299 /*check_dependency=*/true,
18300 &ambiguous_decls,
18301 token->location);
18302
18303 /* If the lookup was ambiguous, an error will already have been
18304 issued. */
18305 if (ambiguous_decls)
18306 return error_mark_node;
18307
18308 /* If we are parsing friend declaration, DECL may be a
18309 TEMPLATE_DECL tree node here. However, we need to check
18310 whether this TEMPLATE_DECL results in valid code. Consider
18311 the following example:
18312
18313 namespace N {
18314 template <class T> class C {};
18315 }
18316 class X {
18317 template <class T> friend class N::C; // #1, valid code
18318 };
18319 template <class T> class Y {
18320 friend class N::C; // #2, invalid code
18321 };
18322
18323 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18324 name lookup of `N::C'. We see that friend declaration must
18325 be template for the code to be valid. Note that
18326 processing_template_decl does not work here since it is
18327 always 1 for the above two cases. */
18328
18329 decl = (cp_parser_maybe_treat_template_as_class
18330 (decl, /*tag_name_p=*/is_friend
18331 && template_parm_lists_apply));
18332
18333 if (TREE_CODE (decl) != TYPE_DECL)
18334 {
18335 cp_parser_diagnose_invalid_type_name (parser,
18336 identifier,
18337 token->location);
18338 return error_mark_node;
18339 }
18340
18341 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18342 {
18343 bool allow_template = (template_parm_lists_apply
18344 || DECL_SELF_REFERENCE_P (decl));
18345 type = check_elaborated_type_specifier (tag_type, decl,
18346 allow_template);
18347
18348 if (type == error_mark_node)
18349 return error_mark_node;
18350 }
18351
18352 /* Forward declarations of nested types, such as
18353
18354 class C1::C2;
18355 class C1::C2::C3;
18356
18357 are invalid unless all components preceding the final '::'
18358 are complete. If all enclosing types are complete, these
18359 declarations become merely pointless.
18360
18361 Invalid forward declarations of nested types are errors
18362 caught elsewhere in parsing. Those that are pointless arrive
18363 here. */
18364
18365 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18366 && !is_friend && !processing_explicit_instantiation)
18367 warning (0, "declaration %qD does not declare anything", decl);
18368
18369 type = TREE_TYPE (decl);
18370 }
18371 else
18372 {
18373 /* An elaborated-type-specifier sometimes introduces a new type and
18374 sometimes names an existing type. Normally, the rule is that it
18375 introduces a new type only if there is not an existing type of
18376 the same name already in scope. For example, given:
18377
18378 struct S {};
18379 void f() { struct S s; }
18380
18381 the `struct S' in the body of `f' is the same `struct S' as in
18382 the global scope; the existing definition is used. However, if
18383 there were no global declaration, this would introduce a new
18384 local class named `S'.
18385
18386 An exception to this rule applies to the following code:
18387
18388 namespace N { struct S; }
18389
18390 Here, the elaborated-type-specifier names a new type
18391 unconditionally; even if there is already an `S' in the
18392 containing scope this declaration names a new type.
18393 This exception only applies if the elaborated-type-specifier
18394 forms the complete declaration:
18395
18396 [class.name]
18397
18398 A declaration consisting solely of `class-key identifier ;' is
18399 either a redeclaration of the name in the current scope or a
18400 forward declaration of the identifier as a class name. It
18401 introduces the name into the current scope.
18402
18403 We are in this situation precisely when the next token is a `;'.
18404
18405 An exception to the exception is that a `friend' declaration does
18406 *not* name a new type; i.e., given:
18407
18408 struct S { friend struct T; };
18409
18410 `T' is not a new type in the scope of `S'.
18411
18412 Also, `new struct S' or `sizeof (struct S)' never results in the
18413 definition of a new type; a new type can only be declared in a
18414 declaration context. */
18415
18416 tag_scope ts;
18417 bool template_p;
18418
18419 if (is_friend)
18420 /* Friends have special name lookup rules. */
18421 ts = ts_within_enclosing_non_class;
18422 else if (is_declaration
18423 && cp_lexer_next_token_is (parser->lexer,
18424 CPP_SEMICOLON))
18425 /* This is a `class-key identifier ;' */
18426 ts = ts_current;
18427 else
18428 ts = ts_global;
18429
18430 template_p =
18431 (template_parm_lists_apply
18432 && (cp_parser_next_token_starts_class_definition_p (parser)
18433 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18434 /* An unqualified name was used to reference this type, so
18435 there were no qualifying templates. */
18436 if (template_parm_lists_apply
18437 && !cp_parser_check_template_parameters (parser,
18438 /*num_templates=*/0,
18439 /*template_id*/false,
18440 token->location,
18441 /*declarator=*/NULL))
18442 return error_mark_node;
18443 type = xref_tag (tag_type, identifier, ts, template_p);
18444 }
18445 }
18446
18447 if (type == error_mark_node)
18448 return error_mark_node;
18449
18450 /* Allow attributes on forward declarations of classes. */
18451 if (attributes)
18452 {
18453 if (TREE_CODE (type) == TYPENAME_TYPE)
18454 warning (OPT_Wattributes,
18455 "attributes ignored on uninstantiated type");
18456 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18457 && ! processing_explicit_instantiation)
18458 warning (OPT_Wattributes,
18459 "attributes ignored on template instantiation");
18460 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18461 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18462 else
18463 warning (OPT_Wattributes,
18464 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18465 }
18466
18467 if (tag_type != enum_type)
18468 {
18469 /* Indicate whether this class was declared as a `class' or as a
18470 `struct'. */
18471 if (CLASS_TYPE_P (type))
18472 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18473 cp_parser_check_class_key (tag_type, type);
18474 }
18475
18476 /* A "<" cannot follow an elaborated type specifier. If that
18477 happens, the user was probably trying to form a template-id. */
18478 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18479 token->location);
18480
18481 return type;
18482 }
18483
18484 /* Parse an enum-specifier.
18485
18486 enum-specifier:
18487 enum-head { enumerator-list [opt] }
18488 enum-head { enumerator-list , } [C++0x]
18489
18490 enum-head:
18491 enum-key identifier [opt] enum-base [opt]
18492 enum-key nested-name-specifier identifier enum-base [opt]
18493
18494 enum-key:
18495 enum
18496 enum class [C++0x]
18497 enum struct [C++0x]
18498
18499 enum-base: [C++0x]
18500 : type-specifier-seq
18501
18502 opaque-enum-specifier:
18503 enum-key identifier enum-base [opt] ;
18504
18505 GNU Extensions:
18506 enum-key attributes[opt] identifier [opt] enum-base [opt]
18507 { enumerator-list [opt] }attributes[opt]
18508 enum-key attributes[opt] identifier [opt] enum-base [opt]
18509 { enumerator-list, }attributes[opt] [C++0x]
18510
18511 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18512 if the token stream isn't an enum-specifier after all. */
18513
18514 static tree
18515 cp_parser_enum_specifier (cp_parser* parser)
18516 {
18517 tree identifier;
18518 tree type = NULL_TREE;
18519 tree prev_scope;
18520 tree nested_name_specifier = NULL_TREE;
18521 tree attributes;
18522 bool scoped_enum_p = false;
18523 bool has_underlying_type = false;
18524 bool nested_being_defined = false;
18525 bool new_value_list = false;
18526 bool is_new_type = false;
18527 bool is_unnamed = false;
18528 tree underlying_type = NULL_TREE;
18529 cp_token *type_start_token = NULL;
18530 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18531
18532 parser->colon_corrects_to_scope_p = false;
18533
18534 /* Parse tentatively so that we can back up if we don't find a
18535 enum-specifier. */
18536 cp_parser_parse_tentatively (parser);
18537
18538 /* Caller guarantees that the current token is 'enum', an identifier
18539 possibly follows, and the token after that is an opening brace.
18540 If we don't have an identifier, fabricate an anonymous name for
18541 the enumeration being defined. */
18542 cp_lexer_consume_token (parser->lexer);
18543
18544 /* Parse the "class" or "struct", which indicates a scoped
18545 enumeration type in C++0x. */
18546 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18547 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18548 {
18549 if (cxx_dialect < cxx11)
18550 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18551
18552 /* Consume the `struct' or `class' token. */
18553 cp_lexer_consume_token (parser->lexer);
18554
18555 scoped_enum_p = true;
18556 }
18557
18558 attributes = cp_parser_attributes_opt (parser);
18559
18560 /* Clear the qualification. */
18561 parser->scope = NULL_TREE;
18562 parser->qualifying_scope = NULL_TREE;
18563 parser->object_scope = NULL_TREE;
18564
18565 /* Figure out in what scope the declaration is being placed. */
18566 prev_scope = current_scope ();
18567
18568 type_start_token = cp_lexer_peek_token (parser->lexer);
18569
18570 push_deferring_access_checks (dk_no_check);
18571 nested_name_specifier
18572 = cp_parser_nested_name_specifier_opt (parser,
18573 /*typename_keyword_p=*/true,
18574 /*check_dependency_p=*/false,
18575 /*type_p=*/false,
18576 /*is_declaration=*/false);
18577
18578 if (nested_name_specifier)
18579 {
18580 tree name;
18581
18582 identifier = cp_parser_identifier (parser);
18583 name = cp_parser_lookup_name (parser, identifier,
18584 enum_type,
18585 /*is_template=*/false,
18586 /*is_namespace=*/false,
18587 /*check_dependency=*/true,
18588 /*ambiguous_decls=*/NULL,
18589 input_location);
18590 if (name && name != error_mark_node)
18591 {
18592 type = TREE_TYPE (name);
18593 if (TREE_CODE (type) == TYPENAME_TYPE)
18594 {
18595 /* Are template enums allowed in ISO? */
18596 if (template_parm_scope_p ())
18597 pedwarn (type_start_token->location, OPT_Wpedantic,
18598 "%qD is an enumeration template", name);
18599 /* ignore a typename reference, for it will be solved by name
18600 in start_enum. */
18601 type = NULL_TREE;
18602 }
18603 }
18604 else if (nested_name_specifier == error_mark_node)
18605 /* We already issued an error. */;
18606 else
18607 {
18608 error_at (type_start_token->location,
18609 "%qD does not name an enumeration in %qT",
18610 identifier, nested_name_specifier);
18611 nested_name_specifier = error_mark_node;
18612 }
18613 }
18614 else
18615 {
18616 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18617 identifier = cp_parser_identifier (parser);
18618 else
18619 {
18620 identifier = make_anon_name ();
18621 is_unnamed = true;
18622 if (scoped_enum_p)
18623 error_at (type_start_token->location,
18624 "unnamed scoped enum is not allowed");
18625 }
18626 }
18627 pop_deferring_access_checks ();
18628
18629 /* Check for the `:' that denotes a specified underlying type in C++0x.
18630 Note that a ':' could also indicate a bitfield width, however. */
18631 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18632 {
18633 cp_decl_specifier_seq type_specifiers;
18634
18635 /* Consume the `:'. */
18636 cp_lexer_consume_token (parser->lexer);
18637
18638 /* Parse the type-specifier-seq. */
18639 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
18640 /*is_declaration=*/false,
18641 /*is_trailing_return=*/false,
18642 &type_specifiers);
18643
18644 /* At this point this is surely not elaborated type specifier. */
18645 if (!cp_parser_parse_definitely (parser))
18646 return NULL_TREE;
18647
18648 if (cxx_dialect < cxx11)
18649 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18650
18651 has_underlying_type = true;
18652
18653 /* If that didn't work, stop. */
18654 if (type_specifiers.type != error_mark_node)
18655 {
18656 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18657 /*initialized=*/0, NULL);
18658 if (underlying_type == error_mark_node
18659 || check_for_bare_parameter_packs (underlying_type))
18660 underlying_type = NULL_TREE;
18661 }
18662 }
18663
18664 /* Look for the `{' but don't consume it yet. */
18665 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18666 {
18667 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18668 {
18669 cp_parser_error (parser, "expected %<{%>");
18670 if (has_underlying_type)
18671 {
18672 type = NULL_TREE;
18673 goto out;
18674 }
18675 }
18676 /* An opaque-enum-specifier must have a ';' here. */
18677 if ((scoped_enum_p || underlying_type)
18678 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18679 {
18680 cp_parser_error (parser, "expected %<;%> or %<{%>");
18681 if (has_underlying_type)
18682 {
18683 type = NULL_TREE;
18684 goto out;
18685 }
18686 }
18687 }
18688
18689 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18690 return NULL_TREE;
18691
18692 if (nested_name_specifier)
18693 {
18694 if (CLASS_TYPE_P (nested_name_specifier))
18695 {
18696 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18697 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18698 push_scope (nested_name_specifier);
18699 }
18700 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18701 {
18702 push_nested_namespace (nested_name_specifier);
18703 }
18704 }
18705
18706 /* Issue an error message if type-definitions are forbidden here. */
18707 if (!cp_parser_check_type_definition (parser))
18708 type = error_mark_node;
18709 else
18710 /* Create the new type. We do this before consuming the opening
18711 brace so the enum will be recorded as being on the line of its
18712 tag (or the 'enum' keyword, if there is no tag). */
18713 type = start_enum (identifier, type, underlying_type,
18714 attributes, scoped_enum_p, &is_new_type);
18715
18716 /* If the next token is not '{' it is an opaque-enum-specifier or an
18717 elaborated-type-specifier. */
18718 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18719 {
18720 timevar_push (TV_PARSE_ENUM);
18721 if (nested_name_specifier
18722 && nested_name_specifier != error_mark_node)
18723 {
18724 /* The following catches invalid code such as:
18725 enum class S<int>::E { A, B, C }; */
18726 if (!processing_specialization
18727 && CLASS_TYPE_P (nested_name_specifier)
18728 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18729 error_at (type_start_token->location, "cannot add an enumerator "
18730 "list to a template instantiation");
18731
18732 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18733 {
18734 error_at (type_start_token->location,
18735 "%<%T::%E%> has not been declared",
18736 TYPE_CONTEXT (nested_name_specifier),
18737 nested_name_specifier);
18738 type = error_mark_node;
18739 }
18740 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18741 && !CLASS_TYPE_P (nested_name_specifier))
18742 {
18743 error_at (type_start_token->location, "nested name specifier "
18744 "%qT for enum declaration does not name a class "
18745 "or namespace", nested_name_specifier);
18746 type = error_mark_node;
18747 }
18748 /* If that scope does not contain the scope in which the
18749 class was originally declared, the program is invalid. */
18750 else if (prev_scope && !is_ancestor (prev_scope,
18751 nested_name_specifier))
18752 {
18753 if (at_namespace_scope_p ())
18754 error_at (type_start_token->location,
18755 "declaration of %qD in namespace %qD which does not "
18756 "enclose %qD",
18757 type, prev_scope, nested_name_specifier);
18758 else
18759 error_at (type_start_token->location,
18760 "declaration of %qD in %qD which does not "
18761 "enclose %qD",
18762 type, prev_scope, nested_name_specifier);
18763 type = error_mark_node;
18764 }
18765 /* If that scope is the scope where the declaration is being placed
18766 the program is invalid. */
18767 else if (CLASS_TYPE_P (nested_name_specifier)
18768 && CLASS_TYPE_P (prev_scope)
18769 && same_type_p (nested_name_specifier, prev_scope))
18770 {
18771 permerror (type_start_token->location,
18772 "extra qualification not allowed");
18773 nested_name_specifier = NULL_TREE;
18774 }
18775 }
18776
18777 if (scoped_enum_p)
18778 begin_scope (sk_scoped_enum, type);
18779
18780 /* Consume the opening brace. */
18781 matching_braces braces;
18782 braces.consume_open (parser);
18783
18784 if (type == error_mark_node)
18785 ; /* Nothing to add */
18786 else if (OPAQUE_ENUM_P (type)
18787 || (cxx_dialect > cxx98 && processing_specialization))
18788 {
18789 new_value_list = true;
18790 SET_OPAQUE_ENUM_P (type, false);
18791 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18792 }
18793 else
18794 {
18795 error_at (type_start_token->location,
18796 "multiple definition of %q#T", type);
18797 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18798 "previous definition here");
18799 type = error_mark_node;
18800 }
18801
18802 if (type == error_mark_node)
18803 cp_parser_skip_to_end_of_block_or_statement (parser);
18804 /* If the next token is not '}', then there are some enumerators. */
18805 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18806 {
18807 if (is_unnamed && !scoped_enum_p)
18808 pedwarn (type_start_token->location, OPT_Wpedantic,
18809 "ISO C++ forbids empty unnamed enum");
18810 }
18811 else
18812 cp_parser_enumerator_list (parser, type);
18813
18814 /* Consume the final '}'. */
18815 braces.require_close (parser);
18816
18817 if (scoped_enum_p)
18818 finish_scope ();
18819 timevar_pop (TV_PARSE_ENUM);
18820 }
18821 else
18822 {
18823 /* If a ';' follows, then it is an opaque-enum-specifier
18824 and additional restrictions apply. */
18825 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18826 {
18827 if (is_unnamed)
18828 error_at (type_start_token->location,
18829 "opaque-enum-specifier without name");
18830 else if (nested_name_specifier)
18831 error_at (type_start_token->location,
18832 "opaque-enum-specifier must use a simple identifier");
18833 }
18834 }
18835
18836 /* Look for trailing attributes to apply to this enumeration, and
18837 apply them if appropriate. */
18838 if (cp_parser_allow_gnu_extensions_p (parser))
18839 {
18840 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18841 cplus_decl_attributes (&type,
18842 trailing_attr,
18843 (int) ATTR_FLAG_TYPE_IN_PLACE);
18844 }
18845
18846 /* Finish up the enumeration. */
18847 if (type != error_mark_node)
18848 {
18849 if (new_value_list)
18850 finish_enum_value_list (type);
18851 if (is_new_type)
18852 finish_enum (type);
18853 }
18854
18855 if (nested_name_specifier)
18856 {
18857 if (CLASS_TYPE_P (nested_name_specifier))
18858 {
18859 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18860 pop_scope (nested_name_specifier);
18861 }
18862 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18863 {
18864 pop_nested_namespace (nested_name_specifier);
18865 }
18866 }
18867 out:
18868 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18869 return type;
18870 }
18871
18872 /* Parse an enumerator-list. The enumerators all have the indicated
18873 TYPE.
18874
18875 enumerator-list:
18876 enumerator-definition
18877 enumerator-list , enumerator-definition */
18878
18879 static void
18880 cp_parser_enumerator_list (cp_parser* parser, tree type)
18881 {
18882 while (true)
18883 {
18884 /* Parse an enumerator-definition. */
18885 cp_parser_enumerator_definition (parser, type);
18886
18887 /* If the next token is not a ',', we've reached the end of
18888 the list. */
18889 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18890 break;
18891 /* Otherwise, consume the `,' and keep going. */
18892 cp_lexer_consume_token (parser->lexer);
18893 /* If the next token is a `}', there is a trailing comma. */
18894 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18895 {
18896 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18897 pedwarn (input_location, OPT_Wpedantic,
18898 "comma at end of enumerator list");
18899 break;
18900 }
18901 }
18902 }
18903
18904 /* Parse an enumerator-definition. The enumerator has the indicated
18905 TYPE.
18906
18907 enumerator-definition:
18908 enumerator
18909 enumerator = constant-expression
18910
18911 enumerator:
18912 identifier
18913
18914 GNU Extensions:
18915
18916 enumerator-definition:
18917 enumerator attributes [opt]
18918 enumerator attributes [opt] = constant-expression */
18919
18920 static void
18921 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18922 {
18923 tree identifier;
18924 tree value;
18925 location_t loc;
18926
18927 /* Save the input location because we are interested in the location
18928 of the identifier and not the location of the explicit value. */
18929 loc = cp_lexer_peek_token (parser->lexer)->location;
18930
18931 /* Look for the identifier. */
18932 identifier = cp_parser_identifier (parser);
18933 if (identifier == error_mark_node)
18934 return;
18935
18936 /* Parse any specified attributes. */
18937 tree attrs = cp_parser_attributes_opt (parser);
18938
18939 /* If the next token is an '=', then there is an explicit value. */
18940 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18941 {
18942 /* Consume the `=' token. */
18943 cp_lexer_consume_token (parser->lexer);
18944 /* Parse the value. */
18945 value = cp_parser_constant_expression (parser);
18946 }
18947 else
18948 value = NULL_TREE;
18949
18950 /* If we are processing a template, make sure the initializer of the
18951 enumerator doesn't contain any bare template parameter pack. */
18952 if (check_for_bare_parameter_packs (value))
18953 value = error_mark_node;
18954
18955 /* Create the enumerator. */
18956 build_enumerator (identifier, value, type, attrs, loc);
18957 }
18958
18959 /* Parse a namespace-name.
18960
18961 namespace-name:
18962 original-namespace-name
18963 namespace-alias
18964
18965 Returns the NAMESPACE_DECL for the namespace. */
18966
18967 static tree
18968 cp_parser_namespace_name (cp_parser* parser)
18969 {
18970 tree identifier;
18971 tree namespace_decl;
18972
18973 cp_token *token = cp_lexer_peek_token (parser->lexer);
18974
18975 /* Get the name of the namespace. */
18976 identifier = cp_parser_identifier (parser);
18977 if (identifier == error_mark_node)
18978 return error_mark_node;
18979
18980 /* Look up the identifier in the currently active scope. Look only
18981 for namespaces, due to:
18982
18983 [basic.lookup.udir]
18984
18985 When looking up a namespace-name in a using-directive or alias
18986 definition, only namespace names are considered.
18987
18988 And:
18989
18990 [basic.lookup.qual]
18991
18992 During the lookup of a name preceding the :: scope resolution
18993 operator, object, function, and enumerator names are ignored.
18994
18995 (Note that cp_parser_qualifying_entity only calls this
18996 function if the token after the name is the scope resolution
18997 operator.) */
18998 namespace_decl = cp_parser_lookup_name (parser, identifier,
18999 none_type,
19000 /*is_template=*/false,
19001 /*is_namespace=*/true,
19002 /*check_dependency=*/true,
19003 /*ambiguous_decls=*/NULL,
19004 token->location);
19005 /* If it's not a namespace, issue an error. */
19006 if (namespace_decl == error_mark_node
19007 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19008 {
19009 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19010 {
19011 auto_diagnostic_group d;
19012 name_hint hint;
19013 if (namespace_decl == error_mark_node
19014 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19015 hint = suggest_alternative_in_explicit_scope (token->location,
19016 identifier,
19017 parser->scope);
19018 if (const char *suggestion = hint.suggestion ())
19019 {
19020 gcc_rich_location richloc (token->location);
19021 richloc.add_fixit_replace (suggestion);
19022 error_at (&richloc,
19023 "%qD is not a namespace-name; did you mean %qs?",
19024 identifier, suggestion);
19025 }
19026 else
19027 error_at (token->location, "%qD is not a namespace-name",
19028 identifier);
19029 }
19030 else
19031 cp_parser_error (parser, "expected namespace-name");
19032 namespace_decl = error_mark_node;
19033 }
19034
19035 return namespace_decl;
19036 }
19037
19038 /* Parse a namespace-definition.
19039
19040 namespace-definition:
19041 named-namespace-definition
19042 unnamed-namespace-definition
19043
19044 named-namespace-definition:
19045 original-namespace-definition
19046 extension-namespace-definition
19047
19048 original-namespace-definition:
19049 namespace identifier { namespace-body }
19050
19051 extension-namespace-definition:
19052 namespace original-namespace-name { namespace-body }
19053
19054 unnamed-namespace-definition:
19055 namespace { namespace-body } */
19056
19057 static void
19058 cp_parser_namespace_definition (cp_parser* parser)
19059 {
19060 tree identifier;
19061 int nested_definition_count = 0;
19062
19063 cp_ensure_no_omp_declare_simd (parser);
19064 cp_ensure_no_oacc_routine (parser);
19065
19066 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19067 const bool topmost_inline_p = is_inline;
19068
19069 if (is_inline)
19070 {
19071 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19072 cp_lexer_consume_token (parser->lexer);
19073 }
19074
19075 /* Look for the `namespace' keyword. */
19076 cp_token* token
19077 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19078
19079 /* Parse any specified attributes before the identifier. */
19080 tree attribs = cp_parser_attributes_opt (parser);
19081
19082 for (;;)
19083 {
19084 identifier = NULL_TREE;
19085
19086 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19087 RID_INLINE);
19088 if (nested_inline_p && nested_definition_count != 0)
19089 {
19090 if (cxx_dialect < cxx2a)
19091 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19092 OPT_Wpedantic, "nested inline namespace definitions only "
19093 "available with -std=c++2a or -std=gnu++2a");
19094 cp_lexer_consume_token (parser->lexer);
19095 }
19096
19097 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19098 {
19099 identifier = cp_parser_identifier (parser);
19100
19101 if (cp_next_tokens_can_be_std_attribute_p (parser))
19102 pedwarn (input_location, OPT_Wpedantic,
19103 "standard attributes on namespaces must precede "
19104 "the namespace name");
19105
19106 /* Parse any attributes specified after the identifier. */
19107 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19108 }
19109
19110 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19111 {
19112 /* Don't forget that the innermost namespace might have been
19113 marked as inline. Use |= because we cannot overwrite
19114 IS_INLINE in case the outermost namespace is inline, but
19115 there are no nested inlines. */
19116 is_inline |= nested_inline_p;
19117 break;
19118 }
19119
19120 if (!nested_definition_count && cxx_dialect < cxx17)
19121 pedwarn (input_location, OPT_Wpedantic,
19122 "nested namespace definitions only available with "
19123 "-std=c++17 or -std=gnu++17");
19124
19125 /* Nested namespace names can create new namespaces (unlike
19126 other qualified-ids). */
19127 if (int count = (identifier
19128 ? push_namespace (identifier, nested_inline_p)
19129 : 0))
19130 nested_definition_count += count;
19131 else
19132 cp_parser_error (parser, "nested namespace name required");
19133 cp_lexer_consume_token (parser->lexer);
19134 }
19135
19136 if (nested_definition_count && !identifier)
19137 cp_parser_error (parser, "namespace name required");
19138
19139 if (nested_definition_count && attribs)
19140 error_at (token->location,
19141 "a nested namespace definition cannot have attributes");
19142 if (nested_definition_count && topmost_inline_p)
19143 error_at (token->location,
19144 "a nested namespace definition cannot be inline");
19145
19146 /* Start the namespace. */
19147 nested_definition_count += push_namespace (identifier, is_inline);
19148
19149 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19150
19151 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19152
19153 /* Look for the `{' to validate starting the namespace. */
19154 matching_braces braces;
19155 if (braces.require_open (parser))
19156 {
19157 /* Parse the body of the namespace. */
19158 cp_parser_namespace_body (parser);
19159
19160 /* Look for the final `}'. */
19161 braces.require_close (parser);
19162 }
19163
19164 if (has_visibility)
19165 pop_visibility (1);
19166
19167 /* Pop the nested namespace definitions. */
19168 while (nested_definition_count--)
19169 pop_namespace ();
19170 }
19171
19172 /* Parse a namespace-body.
19173
19174 namespace-body:
19175 declaration-seq [opt] */
19176
19177 static void
19178 cp_parser_namespace_body (cp_parser* parser)
19179 {
19180 cp_parser_declaration_seq_opt (parser);
19181 }
19182
19183 /* Parse a namespace-alias-definition.
19184
19185 namespace-alias-definition:
19186 namespace identifier = qualified-namespace-specifier ; */
19187
19188 static void
19189 cp_parser_namespace_alias_definition (cp_parser* parser)
19190 {
19191 tree identifier;
19192 tree namespace_specifier;
19193
19194 cp_token *token = cp_lexer_peek_token (parser->lexer);
19195
19196 /* Look for the `namespace' keyword. */
19197 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19198 /* Look for the identifier. */
19199 identifier = cp_parser_identifier (parser);
19200 if (identifier == error_mark_node)
19201 return;
19202 /* Look for the `=' token. */
19203 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19204 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19205 {
19206 error_at (token->location, "%<namespace%> definition is not allowed here");
19207 /* Skip the definition. */
19208 cp_lexer_consume_token (parser->lexer);
19209 if (cp_parser_skip_to_closing_brace (parser))
19210 cp_lexer_consume_token (parser->lexer);
19211 return;
19212 }
19213 cp_parser_require (parser, CPP_EQ, RT_EQ);
19214 /* Look for the qualified-namespace-specifier. */
19215 namespace_specifier
19216 = cp_parser_qualified_namespace_specifier (parser);
19217 /* Look for the `;' token. */
19218 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19219
19220 /* Register the alias in the symbol table. */
19221 do_namespace_alias (identifier, namespace_specifier);
19222 }
19223
19224 /* Parse a qualified-namespace-specifier.
19225
19226 qualified-namespace-specifier:
19227 :: [opt] nested-name-specifier [opt] namespace-name
19228
19229 Returns a NAMESPACE_DECL corresponding to the specified
19230 namespace. */
19231
19232 static tree
19233 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19234 {
19235 /* Look for the optional `::'. */
19236 cp_parser_global_scope_opt (parser,
19237 /*current_scope_valid_p=*/false);
19238
19239 /* Look for the optional nested-name-specifier. */
19240 cp_parser_nested_name_specifier_opt (parser,
19241 /*typename_keyword_p=*/false,
19242 /*check_dependency_p=*/true,
19243 /*type_p=*/false,
19244 /*is_declaration=*/true);
19245
19246 return cp_parser_namespace_name (parser);
19247 }
19248
19249 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19250 access declaration.
19251
19252 using-declaration:
19253 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19254 using :: unqualified-id ;
19255
19256 access-declaration:
19257 qualified-id ;
19258
19259 */
19260
19261 static bool
19262 cp_parser_using_declaration (cp_parser* parser,
19263 bool access_declaration_p)
19264 {
19265 cp_token *token;
19266 bool typename_p = false;
19267 bool global_scope_p;
19268 tree decl;
19269 tree identifier;
19270 tree qscope;
19271 int oldcount = errorcount;
19272 cp_token *diag_token = NULL;
19273
19274 if (access_declaration_p)
19275 {
19276 diag_token = cp_lexer_peek_token (parser->lexer);
19277 cp_parser_parse_tentatively (parser);
19278 }
19279 else
19280 {
19281 /* Look for the `using' keyword. */
19282 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19283
19284 again:
19285 /* Peek at the next token. */
19286 token = cp_lexer_peek_token (parser->lexer);
19287 /* See if it's `typename'. */
19288 if (token->keyword == RID_TYPENAME)
19289 {
19290 /* Remember that we've seen it. */
19291 typename_p = true;
19292 /* Consume the `typename' token. */
19293 cp_lexer_consume_token (parser->lexer);
19294 }
19295 }
19296
19297 /* Look for the optional global scope qualification. */
19298 global_scope_p
19299 = (cp_parser_global_scope_opt (parser,
19300 /*current_scope_valid_p=*/false)
19301 != NULL_TREE);
19302
19303 /* If we saw `typename', or didn't see `::', then there must be a
19304 nested-name-specifier present. */
19305 if (typename_p || !global_scope_p)
19306 {
19307 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19308 /*check_dependency_p=*/true,
19309 /*type_p=*/false,
19310 /*is_declaration=*/true);
19311 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19312 {
19313 cp_parser_skip_to_end_of_block_or_statement (parser);
19314 return false;
19315 }
19316 }
19317 /* Otherwise, we could be in either of the two productions. In that
19318 case, treat the nested-name-specifier as optional. */
19319 else
19320 qscope = cp_parser_nested_name_specifier_opt (parser,
19321 /*typename_keyword_p=*/false,
19322 /*check_dependency_p=*/true,
19323 /*type_p=*/false,
19324 /*is_declaration=*/true);
19325 if (!qscope)
19326 qscope = global_namespace;
19327 else if (UNSCOPED_ENUM_P (qscope))
19328 qscope = CP_TYPE_CONTEXT (qscope);
19329
19330 if (access_declaration_p && cp_parser_error_occurred (parser))
19331 /* Something has already gone wrong; there's no need to parse
19332 further. Since an error has occurred, the return value of
19333 cp_parser_parse_definitely will be false, as required. */
19334 return cp_parser_parse_definitely (parser);
19335
19336 token = cp_lexer_peek_token (parser->lexer);
19337 /* Parse the unqualified-id. */
19338 identifier = cp_parser_unqualified_id (parser,
19339 /*template_keyword_p=*/false,
19340 /*check_dependency_p=*/true,
19341 /*declarator_p=*/true,
19342 /*optional_p=*/false);
19343
19344 if (access_declaration_p)
19345 {
19346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19347 cp_parser_simulate_error (parser);
19348 if (!cp_parser_parse_definitely (parser))
19349 return false;
19350 }
19351 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19352 {
19353 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19354 if (cxx_dialect < cxx17
19355 && !in_system_header_at (ell->location))
19356 pedwarn (ell->location, 0,
19357 "pack expansion in using-declaration only available "
19358 "with -std=c++17 or -std=gnu++17");
19359 qscope = make_pack_expansion (qscope);
19360 }
19361
19362 /* The function we call to handle a using-declaration is different
19363 depending on what scope we are in. */
19364 if (qscope == error_mark_node || identifier == error_mark_node)
19365 ;
19366 else if (!identifier_p (identifier)
19367 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19368 /* [namespace.udecl]
19369
19370 A using declaration shall not name a template-id. */
19371 error_at (token->location,
19372 "a template-id may not appear in a using-declaration");
19373 else
19374 {
19375 if (at_class_scope_p ())
19376 {
19377 /* Create the USING_DECL. */
19378 decl = do_class_using_decl (qscope, identifier);
19379
19380 if (decl && typename_p)
19381 USING_DECL_TYPENAME_P (decl) = 1;
19382
19383 if (check_for_bare_parameter_packs (decl))
19384 {
19385 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19386 return false;
19387 }
19388 else
19389 /* Add it to the list of members in this class. */
19390 finish_member_declaration (decl);
19391 }
19392 else
19393 {
19394 decl = cp_parser_lookup_name_simple (parser,
19395 identifier,
19396 token->location);
19397 if (decl == error_mark_node)
19398 cp_parser_name_lookup_error (parser, identifier,
19399 decl, NLE_NULL,
19400 token->location);
19401 else if (check_for_bare_parameter_packs (decl))
19402 {
19403 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19404 return false;
19405 }
19406 else if (!at_namespace_scope_p ())
19407 finish_local_using_decl (decl, qscope, identifier);
19408 else
19409 finish_namespace_using_decl (decl, qscope, identifier);
19410 }
19411 }
19412
19413 if (!access_declaration_p
19414 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19415 {
19416 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19417 if (cxx_dialect < cxx17)
19418 pedwarn (comma->location, 0,
19419 "comma-separated list in using-declaration only available "
19420 "with -std=c++17 or -std=gnu++17");
19421 goto again;
19422 }
19423
19424 /* Look for the final `;'. */
19425 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19426
19427 if (access_declaration_p && errorcount == oldcount)
19428 warning_at (diag_token->location, OPT_Wdeprecated,
19429 "access declarations are deprecated "
19430 "in favour of using-declarations; "
19431 "suggestion: add the %<using%> keyword");
19432
19433 return true;
19434 }
19435
19436 /* Parse an alias-declaration.
19437
19438 alias-declaration:
19439 using identifier attribute-specifier-seq [opt] = type-id */
19440
19441 static tree
19442 cp_parser_alias_declaration (cp_parser* parser)
19443 {
19444 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19445 location_t id_location, type_location;
19446 cp_declarator *declarator;
19447 cp_decl_specifier_seq decl_specs;
19448 bool member_p;
19449 const char *saved_message = NULL;
19450
19451 /* Look for the `using' keyword. */
19452 cp_token *using_token
19453 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19454 if (using_token == NULL)
19455 return error_mark_node;
19456
19457 id_location = cp_lexer_peek_token (parser->lexer)->location;
19458 id = cp_parser_identifier (parser);
19459 if (id == error_mark_node)
19460 return error_mark_node;
19461
19462 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19463 attributes = cp_parser_attributes_opt (parser);
19464 if (attributes == error_mark_node)
19465 return error_mark_node;
19466
19467 cp_parser_require (parser, CPP_EQ, RT_EQ);
19468
19469 if (cp_parser_error_occurred (parser))
19470 return error_mark_node;
19471
19472 cp_parser_commit_to_tentative_parse (parser);
19473
19474 /* Now we are going to parse the type-id of the declaration. */
19475
19476 /*
19477 [dcl.type]/3 says:
19478
19479 "A type-specifier-seq shall not define a class or enumeration
19480 unless it appears in the type-id of an alias-declaration (7.1.3) that
19481 is not the declaration of a template-declaration."
19482
19483 In other words, if we currently are in an alias template, the
19484 type-id should not define a type.
19485
19486 So let's set parser->type_definition_forbidden_message in that
19487 case; cp_parser_check_type_definition (called by
19488 cp_parser_class_specifier) will then emit an error if a type is
19489 defined in the type-id. */
19490 if (parser->num_template_parameter_lists)
19491 {
19492 saved_message = parser->type_definition_forbidden_message;
19493 parser->type_definition_forbidden_message =
19494 G_("types may not be defined in alias template declarations");
19495 }
19496
19497 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19498 &type_location);
19499
19500 /* Restore the error message if need be. */
19501 if (parser->num_template_parameter_lists)
19502 parser->type_definition_forbidden_message = saved_message;
19503
19504 if (type == error_mark_node
19505 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19506 {
19507 cp_parser_skip_to_end_of_block_or_statement (parser);
19508 return error_mark_node;
19509 }
19510
19511 /* A typedef-name can also be introduced by an alias-declaration. The
19512 identifier following the using keyword becomes a typedef-name. It has
19513 the same semantics as if it were introduced by the typedef
19514 specifier. In particular, it does not define a new type and it shall
19515 not appear in the type-id. */
19516
19517 clear_decl_specs (&decl_specs);
19518 decl_specs.type = type;
19519 if (attributes != NULL_TREE)
19520 {
19521 decl_specs.attributes = attributes;
19522 set_and_check_decl_spec_loc (&decl_specs,
19523 ds_attribute,
19524 attrs_token);
19525 }
19526 set_and_check_decl_spec_loc (&decl_specs,
19527 ds_typedef,
19528 using_token);
19529 set_and_check_decl_spec_loc (&decl_specs,
19530 ds_alias,
19531 using_token);
19532 decl_specs.locations[ds_type_spec] = type_location;
19533
19534 if (parser->num_template_parameter_lists
19535 && !cp_parser_check_template_parameters (parser,
19536 /*num_templates=*/0,
19537 /*template_id*/false,
19538 id_location,
19539 /*declarator=*/NULL))
19540 return error_mark_node;
19541
19542 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19543
19544 member_p = at_class_scope_p ();
19545 if (member_p)
19546 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19547 NULL_TREE, attributes);
19548 else
19549 decl = start_decl (declarator, &decl_specs, 0,
19550 attributes, NULL_TREE, &pushed_scope);
19551 if (decl == error_mark_node)
19552 return decl;
19553
19554 // Attach constraints to the alias declaration.
19555 if (flag_concepts && current_template_parms)
19556 {
19557 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19558 tree constr = build_constraints (reqs, NULL_TREE);
19559 set_constraints (decl, constr);
19560 }
19561
19562 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19563
19564 if (pushed_scope)
19565 pop_scope (pushed_scope);
19566
19567 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19568 added into the symbol table; otherwise, return the TYPE_DECL. */
19569 if (DECL_LANG_SPECIFIC (decl)
19570 && DECL_TEMPLATE_INFO (decl)
19571 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19572 {
19573 decl = DECL_TI_TEMPLATE (decl);
19574 if (member_p)
19575 check_member_template (decl);
19576 }
19577
19578 return decl;
19579 }
19580
19581 /* Parse a using-directive.
19582
19583 using-directive:
19584 using namespace :: [opt] nested-name-specifier [opt]
19585 namespace-name ; */
19586
19587 static void
19588 cp_parser_using_directive (cp_parser* parser)
19589 {
19590 tree namespace_decl;
19591 tree attribs;
19592
19593 /* Look for the `using' keyword. */
19594 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19595 /* And the `namespace' keyword. */
19596 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19597 /* Look for the optional `::' operator. */
19598 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19599 /* And the optional nested-name-specifier. */
19600 cp_parser_nested_name_specifier_opt (parser,
19601 /*typename_keyword_p=*/false,
19602 /*check_dependency_p=*/true,
19603 /*type_p=*/false,
19604 /*is_declaration=*/true);
19605 /* Get the namespace being used. */
19606 namespace_decl = cp_parser_namespace_name (parser);
19607 /* And any specified attributes. */
19608 attribs = cp_parser_attributes_opt (parser);
19609
19610 /* Update the symbol table. */
19611 if (namespace_bindings_p ())
19612 finish_namespace_using_directive (namespace_decl, attribs);
19613 else
19614 finish_local_using_directive (namespace_decl, attribs);
19615
19616 /* Look for the final `;'. */
19617 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19618 }
19619
19620 /* Parse an asm-definition.
19621
19622 asm-qualifier:
19623 volatile
19624 inline
19625 goto
19626
19627 asm-qualifier-list:
19628 asm-qualifier
19629 asm-qualifier-list asm-qualifier
19630
19631 asm-definition:
19632 asm ( string-literal ) ;
19633
19634 GNU Extension:
19635
19636 asm-definition:
19637 asm asm-qualifier-list [opt] ( string-literal ) ;
19638 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19639 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19640 : asm-operand-list [opt] ) ;
19641 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19642 : asm-operand-list [opt]
19643 : asm-clobber-list [opt] ) ;
19644 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19645 : asm-clobber-list [opt]
19646 : asm-goto-list ) ;
19647
19648 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19649 contains goto, and is the only allowed form in that case. No duplicates are
19650 allowed in an asm-qualifier-list. */
19651
19652 static void
19653 cp_parser_asm_definition (cp_parser* parser)
19654 {
19655 tree string;
19656 tree outputs = NULL_TREE;
19657 tree inputs = NULL_TREE;
19658 tree clobbers = NULL_TREE;
19659 tree labels = NULL_TREE;
19660 tree asm_stmt;
19661 bool volatile_p = false;
19662 bool extended_p = false;
19663 bool invalid_inputs_p = false;
19664 bool invalid_outputs_p = false;
19665 bool inline_p = false;
19666 bool goto_p = false;
19667 required_token missing = RT_NONE;
19668
19669 /* Look for the `asm' keyword. */
19670 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19671
19672 if (parser->in_function_body
19673 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19674 {
19675 error ("%<asm%> in %<constexpr%> function");
19676 cp_function_chain->invalid_constexpr = true;
19677 }
19678
19679 /* See if the next token is `volatile'. */
19680 if (cp_parser_allow_gnu_extensions_p (parser))
19681 for (bool done = false; !done ; )
19682 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19683 {
19684 case RID_VOLATILE:
19685 if (!volatile_p)
19686 {
19687 /* Remember that we saw the `volatile' keyword. */
19688 volatile_p = true;
19689 /* Consume the token. */
19690 cp_lexer_consume_token (parser->lexer);
19691 }
19692 else
19693 done = true;
19694 break;
19695 case RID_INLINE:
19696 if (!inline_p && parser->in_function_body)
19697 {
19698 /* Remember that we saw the `inline' keyword. */
19699 inline_p = true;
19700 /* Consume the token. */
19701 cp_lexer_consume_token (parser->lexer);
19702 }
19703 else
19704 done = true;
19705 break;
19706 case RID_GOTO:
19707 if (!goto_p && parser->in_function_body)
19708 {
19709 /* Remember that we saw the `goto' keyword. */
19710 goto_p = true;
19711 /* Consume the token. */
19712 cp_lexer_consume_token (parser->lexer);
19713 }
19714 else
19715 done = true;
19716 break;
19717 default:
19718 done = true;
19719 }
19720
19721 /* Look for the opening `('. */
19722 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19723 return;
19724 /* Look for the string. */
19725 string = cp_parser_string_literal (parser, false, false);
19726 if (string == error_mark_node)
19727 {
19728 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19729 /*consume_paren=*/true);
19730 return;
19731 }
19732
19733 /* If we're allowing GNU extensions, check for the extended assembly
19734 syntax. Unfortunately, the `:' tokens need not be separated by
19735 a space in C, and so, for compatibility, we tolerate that here
19736 too. Doing that means that we have to treat the `::' operator as
19737 two `:' tokens. */
19738 if (cp_parser_allow_gnu_extensions_p (parser)
19739 && parser->in_function_body
19740 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19741 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19742 {
19743 bool inputs_p = false;
19744 bool clobbers_p = false;
19745 bool labels_p = false;
19746
19747 /* The extended syntax was used. */
19748 extended_p = true;
19749
19750 /* Look for outputs. */
19751 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19752 {
19753 /* Consume the `:'. */
19754 cp_lexer_consume_token (parser->lexer);
19755 /* Parse the output-operands. */
19756 if (cp_lexer_next_token_is_not (parser->lexer,
19757 CPP_COLON)
19758 && cp_lexer_next_token_is_not (parser->lexer,
19759 CPP_SCOPE)
19760 && cp_lexer_next_token_is_not (parser->lexer,
19761 CPP_CLOSE_PAREN)
19762 && !goto_p)
19763 {
19764 outputs = cp_parser_asm_operand_list (parser);
19765 if (outputs == error_mark_node)
19766 invalid_outputs_p = true;
19767 }
19768 }
19769 /* If the next token is `::', there are no outputs, and the
19770 next token is the beginning of the inputs. */
19771 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19772 /* The inputs are coming next. */
19773 inputs_p = true;
19774
19775 /* Look for inputs. */
19776 if (inputs_p
19777 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19778 {
19779 /* Consume the `:' or `::'. */
19780 cp_lexer_consume_token (parser->lexer);
19781 /* Parse the output-operands. */
19782 if (cp_lexer_next_token_is_not (parser->lexer,
19783 CPP_COLON)
19784 && cp_lexer_next_token_is_not (parser->lexer,
19785 CPP_SCOPE)
19786 && cp_lexer_next_token_is_not (parser->lexer,
19787 CPP_CLOSE_PAREN))
19788 {
19789 inputs = cp_parser_asm_operand_list (parser);
19790 if (inputs == error_mark_node)
19791 invalid_inputs_p = true;
19792 }
19793 }
19794 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19795 /* The clobbers are coming next. */
19796 clobbers_p = true;
19797
19798 /* Look for clobbers. */
19799 if (clobbers_p
19800 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19801 {
19802 clobbers_p = true;
19803 /* Consume the `:' or `::'. */
19804 cp_lexer_consume_token (parser->lexer);
19805 /* Parse the clobbers. */
19806 if (cp_lexer_next_token_is_not (parser->lexer,
19807 CPP_COLON)
19808 && cp_lexer_next_token_is_not (parser->lexer,
19809 CPP_CLOSE_PAREN))
19810 clobbers = cp_parser_asm_clobber_list (parser);
19811 }
19812 else if (goto_p
19813 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19814 /* The labels are coming next. */
19815 labels_p = true;
19816
19817 /* Look for labels. */
19818 if (labels_p
19819 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19820 {
19821 labels_p = true;
19822 /* Consume the `:' or `::'. */
19823 cp_lexer_consume_token (parser->lexer);
19824 /* Parse the labels. */
19825 labels = cp_parser_asm_label_list (parser);
19826 }
19827
19828 if (goto_p && !labels_p)
19829 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19830 }
19831 else if (goto_p)
19832 missing = RT_COLON_SCOPE;
19833
19834 /* Look for the closing `)'. */
19835 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19836 missing ? missing : RT_CLOSE_PAREN))
19837 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19838 /*consume_paren=*/true);
19839 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19840
19841 if (!invalid_inputs_p && !invalid_outputs_p)
19842 {
19843 /* Create the ASM_EXPR. */
19844 if (parser->in_function_body)
19845 {
19846 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19847 inputs, clobbers, labels, inline_p);
19848 /* If the extended syntax was not used, mark the ASM_EXPR. */
19849 if (!extended_p)
19850 {
19851 tree temp = asm_stmt;
19852 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19853 temp = TREE_OPERAND (temp, 0);
19854
19855 ASM_INPUT_P (temp) = 1;
19856 }
19857 }
19858 else
19859 symtab->finalize_toplevel_asm (string);
19860 }
19861 }
19862
19863 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19864 type that comes from the decl-specifier-seq. */
19865
19866 static tree
19867 strip_declarator_types (tree type, cp_declarator *declarator)
19868 {
19869 for (cp_declarator *d = declarator; d;)
19870 switch (d->kind)
19871 {
19872 case cdk_id:
19873 case cdk_decomp:
19874 case cdk_error:
19875 d = NULL;
19876 break;
19877
19878 default:
19879 if (TYPE_PTRMEMFUNC_P (type))
19880 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19881 type = TREE_TYPE (type);
19882 d = d->declarator;
19883 break;
19884 }
19885
19886 return type;
19887 }
19888
19889 /* Declarators [gram.dcl.decl] */
19890
19891 /* Parse an init-declarator.
19892
19893 init-declarator:
19894 declarator initializer [opt]
19895
19896 GNU Extension:
19897
19898 init-declarator:
19899 declarator asm-specification [opt] attributes [opt] initializer [opt]
19900
19901 function-definition:
19902 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19903 function-body
19904 decl-specifier-seq [opt] declarator function-try-block
19905
19906 GNU Extension:
19907
19908 function-definition:
19909 __extension__ function-definition
19910
19911 TM Extension:
19912
19913 function-definition:
19914 decl-specifier-seq [opt] declarator function-transaction-block
19915
19916 The parser flags FLAGS is used to control type-specifier parsing.
19917
19918 The DECL_SPECIFIERS apply to this declarator. Returns a
19919 representation of the entity declared. If MEMBER_P is TRUE, then
19920 this declarator appears in a class scope. The new DECL created by
19921 this declarator is returned.
19922
19923 The CHECKS are access checks that should be performed once we know
19924 what entity is being declared (and, therefore, what classes have
19925 befriended it).
19926
19927 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19928 for a function-definition here as well. If the declarator is a
19929 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19930 be TRUE upon return. By that point, the function-definition will
19931 have been completely parsed.
19932
19933 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19934 is FALSE.
19935
19936 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19937 parsed declaration if it is an uninitialized single declarator not followed
19938 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19939 if present, will not be consumed. If returned, this declarator will be
19940 created with SD_INITIALIZED but will not call cp_finish_decl.
19941
19942 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19943 and there is an initializer, the pointed location_t is set to the
19944 location of the '=' or `(', or '{' in C++11 token introducing the
19945 initializer. */
19946
19947 static tree
19948 cp_parser_init_declarator (cp_parser* parser,
19949 cp_parser_flags flags,
19950 cp_decl_specifier_seq *decl_specifiers,
19951 vec<deferred_access_check, va_gc> *checks,
19952 bool function_definition_allowed_p,
19953 bool member_p,
19954 int declares_class_or_enum,
19955 bool* function_definition_p,
19956 tree* maybe_range_for_decl,
19957 location_t* init_loc,
19958 tree* auto_result)
19959 {
19960 cp_token *token = NULL, *asm_spec_start_token = NULL,
19961 *attributes_start_token = NULL;
19962 cp_declarator *declarator;
19963 tree prefix_attributes;
19964 tree attributes = NULL;
19965 tree asm_specification;
19966 tree initializer;
19967 tree decl = NULL_TREE;
19968 tree scope;
19969 int is_initialized;
19970 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19971 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19972 "(...)". */
19973 enum cpp_ttype initialization_kind;
19974 bool is_direct_init = false;
19975 bool is_non_constant_init;
19976 int ctor_dtor_or_conv_p;
19977 bool friend_p = cp_parser_friend_p (decl_specifiers);
19978 tree pushed_scope = NULL_TREE;
19979 bool range_for_decl_p = false;
19980 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19981 location_t tmp_init_loc = UNKNOWN_LOCATION;
19982
19983 /* Gather the attributes that were provided with the
19984 decl-specifiers. */
19985 prefix_attributes = decl_specifiers->attributes;
19986
19987 /* Assume that this is not the declarator for a function
19988 definition. */
19989 if (function_definition_p)
19990 *function_definition_p = false;
19991
19992 /* Default arguments are only permitted for function parameters. */
19993 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19994 parser->default_arg_ok_p = false;
19995
19996 /* Defer access checks while parsing the declarator; we cannot know
19997 what names are accessible until we know what is being
19998 declared. */
19999 resume_deferring_access_checks ();
20000
20001 token = cp_lexer_peek_token (parser->lexer);
20002
20003 /* Parse the declarator. */
20004 declarator
20005 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20006 flags, &ctor_dtor_or_conv_p,
20007 /*parenthesized_p=*/NULL,
20008 member_p, friend_p);
20009 /* Gather up the deferred checks. */
20010 stop_deferring_access_checks ();
20011
20012 parser->default_arg_ok_p = saved_default_arg_ok_p;
20013
20014 /* If the DECLARATOR was erroneous, there's no need to go
20015 further. */
20016 if (declarator == cp_error_declarator)
20017 return error_mark_node;
20018
20019 /* Check that the number of template-parameter-lists is OK. */
20020 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20021 token->location))
20022 return error_mark_node;
20023
20024 if (declares_class_or_enum & 2)
20025 cp_parser_check_for_definition_in_return_type (declarator,
20026 decl_specifiers->type,
20027 decl_specifiers->locations[ds_type_spec]);
20028
20029 /* Figure out what scope the entity declared by the DECLARATOR is
20030 located in. `grokdeclarator' sometimes changes the scope, so
20031 we compute it now. */
20032 scope = get_scope_of_declarator (declarator);
20033
20034 /* Perform any lookups in the declared type which were thought to be
20035 dependent, but are not in the scope of the declarator. */
20036 decl_specifiers->type
20037 = maybe_update_decl_type (decl_specifiers->type, scope);
20038
20039 /* If we're allowing GNU extensions, look for an
20040 asm-specification. */
20041 if (cp_parser_allow_gnu_extensions_p (parser))
20042 {
20043 /* Look for an asm-specification. */
20044 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20045 asm_specification = cp_parser_asm_specification_opt (parser);
20046 }
20047 else
20048 asm_specification = NULL_TREE;
20049
20050 /* Look for attributes. */
20051 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20052 attributes = cp_parser_attributes_opt (parser);
20053
20054 /* Peek at the next token. */
20055 token = cp_lexer_peek_token (parser->lexer);
20056
20057 bool bogus_implicit_tmpl = false;
20058
20059 if (function_declarator_p (declarator))
20060 {
20061 /* Handle C++17 deduction guides. */
20062 if (!decl_specifiers->type
20063 && ctor_dtor_or_conv_p <= 0
20064 && cxx_dialect >= cxx17)
20065 {
20066 cp_declarator *id = get_id_declarator (declarator);
20067 tree name = id->u.id.unqualified_name;
20068 parser->scope = id->u.id.qualifying_scope;
20069 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20070 if (tmpl
20071 && (DECL_CLASS_TEMPLATE_P (tmpl)
20072 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20073 {
20074 id->u.id.unqualified_name = dguide_name (tmpl);
20075 id->u.id.sfk = sfk_deduction_guide;
20076 ctor_dtor_or_conv_p = 1;
20077 }
20078 }
20079
20080 /* Check to see if the token indicates the start of a
20081 function-definition. */
20082 if (cp_parser_token_starts_function_definition_p (token))
20083 {
20084 if (!function_definition_allowed_p)
20085 {
20086 /* If a function-definition should not appear here, issue an
20087 error message. */
20088 cp_parser_error (parser,
20089 "a function-definition is not allowed here");
20090 return error_mark_node;
20091 }
20092
20093 location_t func_brace_location
20094 = cp_lexer_peek_token (parser->lexer)->location;
20095
20096 /* Neither attributes nor an asm-specification are allowed
20097 on a function-definition. */
20098 if (asm_specification)
20099 error_at (asm_spec_start_token->location,
20100 "an asm-specification is not allowed "
20101 "on a function-definition");
20102 if (attributes)
20103 error_at (attributes_start_token->location,
20104 "attributes are not allowed "
20105 "on a function-definition");
20106 /* This is a function-definition. */
20107 *function_definition_p = true;
20108
20109 /* Parse the function definition. */
20110 if (member_p)
20111 decl = cp_parser_save_member_function_body (parser,
20112 decl_specifiers,
20113 declarator,
20114 prefix_attributes);
20115 else
20116 decl =
20117 (cp_parser_function_definition_from_specifiers_and_declarator
20118 (parser, decl_specifiers, prefix_attributes, declarator));
20119
20120 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20121 {
20122 /* This is where the prologue starts... */
20123 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20124 = func_brace_location;
20125 }
20126
20127 return decl;
20128 }
20129 }
20130 else if (parser->fully_implicit_function_template_p)
20131 {
20132 /* A non-template declaration involving a function parameter list
20133 containing an implicit template parameter will be made into a
20134 template. If the resulting declaration is not going to be an
20135 actual function then finish the template scope here to prevent it.
20136 An error message will be issued once we have a decl to talk about.
20137
20138 FIXME probably we should do type deduction rather than create an
20139 implicit template, but the standard currently doesn't allow it. */
20140 bogus_implicit_tmpl = true;
20141 finish_fully_implicit_template (parser, NULL_TREE);
20142 }
20143
20144 /* [dcl.dcl]
20145
20146 Only in function declarations for constructors, destructors, type
20147 conversions, and deduction guides can the decl-specifier-seq be omitted.
20148
20149 We explicitly postpone this check past the point where we handle
20150 function-definitions because we tolerate function-definitions
20151 that are missing their return types in some modes. */
20152 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20153 {
20154 cp_parser_error (parser,
20155 "expected constructor, destructor, or type conversion");
20156 return error_mark_node;
20157 }
20158
20159 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20160 if (token->type == CPP_EQ
20161 || token->type == CPP_OPEN_PAREN
20162 || token->type == CPP_OPEN_BRACE)
20163 {
20164 is_initialized = SD_INITIALIZED;
20165 initialization_kind = token->type;
20166 if (maybe_range_for_decl)
20167 *maybe_range_for_decl = error_mark_node;
20168 tmp_init_loc = token->location;
20169 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20170 *init_loc = tmp_init_loc;
20171
20172 if (token->type == CPP_EQ
20173 && function_declarator_p (declarator))
20174 {
20175 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20176 if (t2->keyword == RID_DEFAULT)
20177 is_initialized = SD_DEFAULTED;
20178 else if (t2->keyword == RID_DELETE)
20179 is_initialized = SD_DELETED;
20180 }
20181 }
20182 else
20183 {
20184 /* If the init-declarator isn't initialized and isn't followed by a
20185 `,' or `;', it's not a valid init-declarator. */
20186 if (token->type != CPP_COMMA
20187 && token->type != CPP_SEMICOLON)
20188 {
20189 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20190 range_for_decl_p = true;
20191 else
20192 {
20193 if (!maybe_range_for_decl)
20194 cp_parser_error (parser, "expected initializer");
20195 return error_mark_node;
20196 }
20197 }
20198 is_initialized = SD_UNINITIALIZED;
20199 initialization_kind = CPP_EOF;
20200 }
20201
20202 /* Because start_decl has side-effects, we should only call it if we
20203 know we're going ahead. By this point, we know that we cannot
20204 possibly be looking at any other construct. */
20205 cp_parser_commit_to_tentative_parse (parser);
20206
20207 /* Enter the newly declared entry in the symbol table. If we're
20208 processing a declaration in a class-specifier, we wait until
20209 after processing the initializer. */
20210 if (!member_p)
20211 {
20212 if (parser->in_unbraced_linkage_specification_p)
20213 decl_specifiers->storage_class = sc_extern;
20214 decl = start_decl (declarator, decl_specifiers,
20215 range_for_decl_p? SD_INITIALIZED : is_initialized,
20216 attributes, prefix_attributes, &pushed_scope);
20217 cp_finalize_omp_declare_simd (parser, decl);
20218 cp_finalize_oacc_routine (parser, decl, false);
20219 /* Adjust location of decl if declarator->id_loc is more appropriate:
20220 set, and decl wasn't merged with another decl, in which case its
20221 location would be different from input_location, and more accurate. */
20222 if (DECL_P (decl)
20223 && declarator->id_loc != UNKNOWN_LOCATION
20224 && DECL_SOURCE_LOCATION (decl) == input_location)
20225 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20226 }
20227 else if (scope)
20228 /* Enter the SCOPE. That way unqualified names appearing in the
20229 initializer will be looked up in SCOPE. */
20230 pushed_scope = push_scope (scope);
20231
20232 /* Perform deferred access control checks, now that we know in which
20233 SCOPE the declared entity resides. */
20234 if (!member_p && decl)
20235 {
20236 tree saved_current_function_decl = NULL_TREE;
20237
20238 /* If the entity being declared is a function, pretend that we
20239 are in its scope. If it is a `friend', it may have access to
20240 things that would not otherwise be accessible. */
20241 if (TREE_CODE (decl) == FUNCTION_DECL)
20242 {
20243 saved_current_function_decl = current_function_decl;
20244 current_function_decl = decl;
20245 }
20246
20247 /* Perform access checks for template parameters. */
20248 cp_parser_perform_template_parameter_access_checks (checks);
20249
20250 /* Perform the access control checks for the declarator and the
20251 decl-specifiers. */
20252 perform_deferred_access_checks (tf_warning_or_error);
20253
20254 /* Restore the saved value. */
20255 if (TREE_CODE (decl) == FUNCTION_DECL)
20256 current_function_decl = saved_current_function_decl;
20257 }
20258
20259 /* Parse the initializer. */
20260 initializer = NULL_TREE;
20261 is_direct_init = false;
20262 is_non_constant_init = true;
20263 if (is_initialized)
20264 {
20265 if (function_declarator_p (declarator))
20266 {
20267 if (initialization_kind == CPP_EQ)
20268 initializer = cp_parser_pure_specifier (parser);
20269 else
20270 {
20271 /* If the declaration was erroneous, we don't really
20272 know what the user intended, so just silently
20273 consume the initializer. */
20274 if (decl != error_mark_node)
20275 error_at (tmp_init_loc, "initializer provided for function");
20276 cp_parser_skip_to_closing_parenthesis (parser,
20277 /*recovering=*/true,
20278 /*or_comma=*/false,
20279 /*consume_paren=*/true);
20280 }
20281 }
20282 else
20283 {
20284 /* We want to record the extra mangling scope for in-class
20285 initializers of class members and initializers of static data
20286 member templates. The former involves deferring
20287 parsing of the initializer until end of class as with default
20288 arguments. So right here we only handle the latter. */
20289 if (!member_p && processing_template_decl && decl != error_mark_node)
20290 start_lambda_scope (decl);
20291 initializer = cp_parser_initializer (parser,
20292 &is_direct_init,
20293 &is_non_constant_init);
20294 if (!member_p && processing_template_decl && decl != error_mark_node)
20295 finish_lambda_scope ();
20296 if (initializer == error_mark_node)
20297 cp_parser_skip_to_end_of_statement (parser);
20298 }
20299 }
20300
20301 /* The old parser allows attributes to appear after a parenthesized
20302 initializer. Mark Mitchell proposed removing this functionality
20303 on the GCC mailing lists on 2002-08-13. This parser accepts the
20304 attributes -- but ignores them. Made a permerror in GCC 8. */
20305 if (cp_parser_allow_gnu_extensions_p (parser)
20306 && initialization_kind == CPP_OPEN_PAREN
20307 && cp_parser_attributes_opt (parser)
20308 && permerror (input_location,
20309 "attributes after parenthesized initializer ignored"))
20310 {
20311 static bool hint;
20312 if (flag_permissive && !hint)
20313 {
20314 hint = true;
20315 inform (input_location,
20316 "this flexibility is deprecated and will be removed");
20317 }
20318 }
20319
20320 /* And now complain about a non-function implicit template. */
20321 if (bogus_implicit_tmpl && decl != error_mark_node)
20322 error_at (DECL_SOURCE_LOCATION (decl),
20323 "non-function %qD declared as implicit template", decl);
20324
20325 /* For an in-class declaration, use `grokfield' to create the
20326 declaration. */
20327 if (member_p)
20328 {
20329 if (pushed_scope)
20330 {
20331 pop_scope (pushed_scope);
20332 pushed_scope = NULL_TREE;
20333 }
20334 decl = grokfield (declarator, decl_specifiers,
20335 initializer, !is_non_constant_init,
20336 /*asmspec=*/NULL_TREE,
20337 attr_chainon (attributes, prefix_attributes));
20338 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20339 cp_parser_save_default_args (parser, decl);
20340 cp_finalize_omp_declare_simd (parser, decl);
20341 cp_finalize_oacc_routine (parser, decl, false);
20342 }
20343
20344 /* Finish processing the declaration. But, skip member
20345 declarations. */
20346 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20347 {
20348 cp_finish_decl (decl,
20349 initializer, !is_non_constant_init,
20350 asm_specification,
20351 /* If the initializer is in parentheses, then this is
20352 a direct-initialization, which means that an
20353 `explicit' constructor is OK. Otherwise, an
20354 `explicit' constructor cannot be used. */
20355 ((is_direct_init || !is_initialized)
20356 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20357 }
20358 else if ((cxx_dialect != cxx98) && friend_p
20359 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20360 /* Core issue #226 (C++0x only): A default template-argument
20361 shall not be specified in a friend class template
20362 declaration. */
20363 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20364 /*is_partial=*/false, /*is_friend_decl=*/1);
20365
20366 if (!friend_p && pushed_scope)
20367 pop_scope (pushed_scope);
20368
20369 if (function_declarator_p (declarator)
20370 && parser->fully_implicit_function_template_p)
20371 {
20372 if (member_p)
20373 decl = finish_fully_implicit_template (parser, decl);
20374 else
20375 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20376 }
20377
20378 if (auto_result && is_initialized && decl_specifiers->type
20379 && type_uses_auto (decl_specifiers->type))
20380 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20381
20382 return decl;
20383 }
20384
20385 /* Parse a declarator.
20386
20387 declarator:
20388 direct-declarator
20389 ptr-operator declarator
20390
20391 abstract-declarator:
20392 ptr-operator abstract-declarator [opt]
20393 direct-abstract-declarator
20394
20395 GNU Extensions:
20396
20397 declarator:
20398 attributes [opt] direct-declarator
20399 attributes [opt] ptr-operator declarator
20400
20401 abstract-declarator:
20402 attributes [opt] ptr-operator abstract-declarator [opt]
20403 attributes [opt] direct-abstract-declarator
20404
20405 The parser flags FLAGS is used to control type-specifier parsing.
20406
20407 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20408 detect constructors, destructors, deduction guides, or conversion operators.
20409 It is set to -1 if the declarator is a name, and +1 if it is a
20410 function. Otherwise it is set to zero. Usually you just want to
20411 test for >0, but internally the negative value is used.
20412
20413 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20414 a decl-specifier-seq unless it declares a constructor, destructor,
20415 or conversion. It might seem that we could check this condition in
20416 semantic analysis, rather than parsing, but that makes it difficult
20417 to handle something like `f()'. We want to notice that there are
20418 no decl-specifiers, and therefore realize that this is an
20419 expression, not a declaration.)
20420
20421 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20422 the declarator is a direct-declarator of the form "(...)".
20423
20424 MEMBER_P is true iff this declarator is a member-declarator.
20425
20426 FRIEND_P is true iff this declarator is a friend. */
20427
20428 static cp_declarator *
20429 cp_parser_declarator (cp_parser* parser,
20430 cp_parser_declarator_kind dcl_kind,
20431 cp_parser_flags flags,
20432 int* ctor_dtor_or_conv_p,
20433 bool* parenthesized_p,
20434 bool member_p, bool friend_p)
20435 {
20436 cp_declarator *declarator;
20437 enum tree_code code;
20438 cp_cv_quals cv_quals;
20439 tree class_type;
20440 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20441
20442 /* Assume this is not a constructor, destructor, or type-conversion
20443 operator. */
20444 if (ctor_dtor_or_conv_p)
20445 *ctor_dtor_or_conv_p = 0;
20446
20447 if (cp_parser_allow_gnu_extensions_p (parser))
20448 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20449
20450 /* Check for the ptr-operator production. */
20451 cp_parser_parse_tentatively (parser);
20452 /* Parse the ptr-operator. */
20453 code = cp_parser_ptr_operator (parser,
20454 &class_type,
20455 &cv_quals,
20456 &std_attributes);
20457
20458 /* If that worked, then we have a ptr-operator. */
20459 if (cp_parser_parse_definitely (parser))
20460 {
20461 /* If a ptr-operator was found, then this declarator was not
20462 parenthesized. */
20463 if (parenthesized_p)
20464 *parenthesized_p = true;
20465 /* The dependent declarator is optional if we are parsing an
20466 abstract-declarator. */
20467 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20468 cp_parser_parse_tentatively (parser);
20469
20470 /* Parse the dependent declarator. */
20471 declarator = cp_parser_declarator (parser, dcl_kind,
20472 CP_PARSER_FLAGS_NONE,
20473 /*ctor_dtor_or_conv_p=*/NULL,
20474 /*parenthesized_p=*/NULL,
20475 /*member_p=*/false,
20476 friend_p);
20477
20478 /* If we are parsing an abstract-declarator, we must handle the
20479 case where the dependent declarator is absent. */
20480 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20481 && !cp_parser_parse_definitely (parser))
20482 declarator = NULL;
20483
20484 declarator = cp_parser_make_indirect_declarator
20485 (code, class_type, cv_quals, declarator, std_attributes);
20486 }
20487 /* Everything else is a direct-declarator. */
20488 else
20489 {
20490 if (parenthesized_p)
20491 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20492 CPP_OPEN_PAREN);
20493 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20494 flags, ctor_dtor_or_conv_p,
20495 member_p, friend_p);
20496 }
20497
20498 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20499 declarator->attributes = gnu_attributes;
20500 return declarator;
20501 }
20502
20503 /* Parse a direct-declarator or direct-abstract-declarator.
20504
20505 direct-declarator:
20506 declarator-id
20507 direct-declarator ( parameter-declaration-clause )
20508 cv-qualifier-seq [opt]
20509 ref-qualifier [opt]
20510 exception-specification [opt]
20511 direct-declarator [ constant-expression [opt] ]
20512 ( declarator )
20513
20514 direct-abstract-declarator:
20515 direct-abstract-declarator [opt]
20516 ( parameter-declaration-clause )
20517 cv-qualifier-seq [opt]
20518 ref-qualifier [opt]
20519 exception-specification [opt]
20520 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20521 ( abstract-declarator )
20522
20523 Returns a representation of the declarator. DCL_KIND is
20524 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20525 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20526 we are parsing a direct-declarator. It is
20527 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20528 of ambiguity we prefer an abstract declarator, as per
20529 [dcl.ambig.res].
20530 The parser flags FLAGS is used to control type-specifier parsing.
20531 CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
20532 as for cp_parser_declarator. */
20533
20534 static cp_declarator *
20535 cp_parser_direct_declarator (cp_parser* parser,
20536 cp_parser_declarator_kind dcl_kind,
20537 cp_parser_flags flags,
20538 int* ctor_dtor_or_conv_p,
20539 bool member_p, bool friend_p)
20540 {
20541 cp_token *token;
20542 cp_declarator *declarator = NULL;
20543 tree scope = NULL_TREE;
20544 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20545 bool saved_in_declarator_p = parser->in_declarator_p;
20546 bool first = true;
20547 tree pushed_scope = NULL_TREE;
20548 cp_token *open_paren = NULL, *close_paren = NULL;
20549
20550 while (true)
20551 {
20552 /* Peek at the next token. */
20553 token = cp_lexer_peek_token (parser->lexer);
20554 if (token->type == CPP_OPEN_PAREN)
20555 {
20556 /* This is either a parameter-declaration-clause, or a
20557 parenthesized declarator. When we know we are parsing a
20558 named declarator, it must be a parenthesized declarator
20559 if FIRST is true. For instance, `(int)' is a
20560 parameter-declaration-clause, with an omitted
20561 direct-abstract-declarator. But `((*))', is a
20562 parenthesized abstract declarator. Finally, when T is a
20563 template parameter `(T)' is a
20564 parameter-declaration-clause, and not a parenthesized
20565 named declarator.
20566
20567 We first try and parse a parameter-declaration-clause,
20568 and then try a nested declarator (if FIRST is true).
20569
20570 It is not an error for it not to be a
20571 parameter-declaration-clause, even when FIRST is
20572 false. Consider,
20573
20574 int i (int);
20575 int i (3);
20576
20577 The first is the declaration of a function while the
20578 second is the definition of a variable, including its
20579 initializer.
20580
20581 Having seen only the parenthesis, we cannot know which of
20582 these two alternatives should be selected. Even more
20583 complex are examples like:
20584
20585 int i (int (a));
20586 int i (int (3));
20587
20588 The former is a function-declaration; the latter is a
20589 variable initialization.
20590
20591 Thus again, we try a parameter-declaration-clause, and if
20592 that fails, we back out and return. */
20593
20594 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20595 {
20596 tree params;
20597 bool is_declarator = false;
20598
20599 open_paren = NULL;
20600
20601 /* In a member-declarator, the only valid interpretation
20602 of a parenthesis is the start of a
20603 parameter-declaration-clause. (It is invalid to
20604 initialize a static data member with a parenthesized
20605 initializer; only the "=" form of initialization is
20606 permitted.) */
20607 if (!member_p)
20608 cp_parser_parse_tentatively (parser);
20609
20610 /* Consume the `('. */
20611 matching_parens parens;
20612 parens.consume_open (parser);
20613 if (first)
20614 {
20615 /* If this is going to be an abstract declarator, we're
20616 in a declarator and we can't have default args. */
20617 parser->default_arg_ok_p = false;
20618 parser->in_declarator_p = true;
20619 }
20620
20621 begin_scope (sk_function_parms, NULL_TREE);
20622
20623 /* Parse the parameter-declaration-clause. */
20624 params
20625 = cp_parser_parameter_declaration_clause (parser, flags);
20626
20627 /* Consume the `)'. */
20628 parens.require_close (parser);
20629
20630 /* If all went well, parse the cv-qualifier-seq,
20631 ref-qualifier and the exception-specification. */
20632 if (member_p || cp_parser_parse_definitely (parser))
20633 {
20634 cp_cv_quals cv_quals;
20635 cp_virt_specifiers virt_specifiers;
20636 cp_ref_qualifier ref_qual;
20637 tree exception_specification;
20638 tree late_return;
20639 tree attrs;
20640 bool memfn = (member_p || (pushed_scope
20641 && CLASS_TYPE_P (pushed_scope)));
20642
20643 is_declarator = true;
20644
20645 if (ctor_dtor_or_conv_p)
20646 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20647 first = false;
20648
20649 /* Parse the cv-qualifier-seq. */
20650 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20651 /* Parse the ref-qualifier. */
20652 ref_qual = cp_parser_ref_qualifier_opt (parser);
20653 /* Parse the tx-qualifier. */
20654 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20655 /* And the exception-specification. */
20656 exception_specification
20657 = cp_parser_exception_specification_opt (parser);
20658
20659 attrs = cp_parser_std_attribute_spec_seq (parser);
20660
20661 /* In here, we handle cases where attribute is used after
20662 the function declaration. For example:
20663 void func (int x) __attribute__((vector(..))); */
20664 tree gnu_attrs = NULL_TREE;
20665 tree requires_clause = NULL_TREE;
20666 late_return = (cp_parser_late_return_type_opt
20667 (parser, declarator, requires_clause,
20668 memfn ? cv_quals : -1));
20669
20670 /* Parse the virt-specifier-seq. */
20671 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20672
20673 /* Create the function-declarator. */
20674 declarator = make_call_declarator (declarator,
20675 params,
20676 cv_quals,
20677 virt_specifiers,
20678 ref_qual,
20679 tx_qual,
20680 exception_specification,
20681 late_return,
20682 requires_clause);
20683 declarator->std_attributes = attrs;
20684 declarator->attributes = gnu_attrs;
20685 /* Any subsequent parameter lists are to do with
20686 return type, so are not those of the declared
20687 function. */
20688 parser->default_arg_ok_p = false;
20689 }
20690
20691 /* Remove the function parms from scope. */
20692 pop_bindings_and_leave_scope ();
20693
20694 if (is_declarator)
20695 /* Repeat the main loop. */
20696 continue;
20697 }
20698
20699 /* If this is the first, we can try a parenthesized
20700 declarator. */
20701 if (first)
20702 {
20703 bool saved_in_type_id_in_expr_p;
20704
20705 parser->default_arg_ok_p = saved_default_arg_ok_p;
20706 parser->in_declarator_p = saved_in_declarator_p;
20707
20708 open_paren = token;
20709 /* Consume the `('. */
20710 matching_parens parens;
20711 parens.consume_open (parser);
20712 /* Parse the nested declarator. */
20713 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20714 parser->in_type_id_in_expr_p = true;
20715 declarator
20716 = cp_parser_declarator (parser, dcl_kind, flags,
20717 ctor_dtor_or_conv_p,
20718 /*parenthesized_p=*/NULL,
20719 member_p, friend_p);
20720 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20721 first = false;
20722 /* Expect a `)'. */
20723 close_paren = cp_lexer_peek_token (parser->lexer);
20724 if (!parens.require_close (parser))
20725 declarator = cp_error_declarator;
20726 if (declarator == cp_error_declarator)
20727 break;
20728
20729 goto handle_declarator;
20730 }
20731 /* Otherwise, we must be done. */
20732 else
20733 break;
20734 }
20735 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20736 && token->type == CPP_OPEN_SQUARE
20737 && !cp_next_tokens_can_be_attribute_p (parser))
20738 {
20739 /* Parse an array-declarator. */
20740 tree bounds, attrs;
20741
20742 if (ctor_dtor_or_conv_p)
20743 *ctor_dtor_or_conv_p = 0;
20744
20745 open_paren = NULL;
20746 first = false;
20747 parser->default_arg_ok_p = false;
20748 parser->in_declarator_p = true;
20749 /* Consume the `['. */
20750 cp_lexer_consume_token (parser->lexer);
20751 /* Peek at the next token. */
20752 token = cp_lexer_peek_token (parser->lexer);
20753 /* If the next token is `]', then there is no
20754 constant-expression. */
20755 if (token->type != CPP_CLOSE_SQUARE)
20756 {
20757 bool non_constant_p;
20758 bounds
20759 = cp_parser_constant_expression (parser,
20760 /*allow_non_constant=*/true,
20761 &non_constant_p);
20762 if (!non_constant_p)
20763 /* OK */;
20764 else if (error_operand_p (bounds))
20765 /* Already gave an error. */;
20766 else if (!parser->in_function_body
20767 || current_binding_level->kind == sk_function_parms)
20768 {
20769 /* Normally, the array bound must be an integral constant
20770 expression. However, as an extension, we allow VLAs
20771 in function scopes as long as they aren't part of a
20772 parameter declaration. */
20773 cp_parser_error (parser,
20774 "array bound is not an integer constant");
20775 bounds = error_mark_node;
20776 }
20777 else if (processing_template_decl
20778 && !type_dependent_expression_p (bounds))
20779 {
20780 /* Remember this wasn't a constant-expression. */
20781 bounds = build_nop (TREE_TYPE (bounds), bounds);
20782 TREE_SIDE_EFFECTS (bounds) = 1;
20783 }
20784 }
20785 else
20786 bounds = NULL_TREE;
20787 /* Look for the closing `]'. */
20788 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20789 {
20790 declarator = cp_error_declarator;
20791 break;
20792 }
20793
20794 attrs = cp_parser_std_attribute_spec_seq (parser);
20795 declarator = make_array_declarator (declarator, bounds);
20796 declarator->std_attributes = attrs;
20797 }
20798 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20799 {
20800 {
20801 tree qualifying_scope;
20802 tree unqualified_name;
20803 tree attrs;
20804 special_function_kind sfk;
20805 bool abstract_ok;
20806 bool pack_expansion_p = false;
20807 cp_token *declarator_id_start_token;
20808
20809 /* Parse a declarator-id */
20810 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20811 if (abstract_ok)
20812 {
20813 cp_parser_parse_tentatively (parser);
20814
20815 /* If we see an ellipsis, we should be looking at a
20816 parameter pack. */
20817 if (token->type == CPP_ELLIPSIS)
20818 {
20819 /* Consume the `...' */
20820 cp_lexer_consume_token (parser->lexer);
20821
20822 pack_expansion_p = true;
20823 }
20824 }
20825
20826 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20827 unqualified_name
20828 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20829 qualifying_scope = parser->scope;
20830 if (abstract_ok)
20831 {
20832 bool okay = false;
20833
20834 if (!unqualified_name && pack_expansion_p)
20835 {
20836 /* Check whether an error occurred. */
20837 okay = !cp_parser_error_occurred (parser);
20838
20839 /* We already consumed the ellipsis to mark a
20840 parameter pack, but we have no way to report it,
20841 so abort the tentative parse. We will be exiting
20842 immediately anyway. */
20843 cp_parser_abort_tentative_parse (parser);
20844 }
20845 else
20846 okay = cp_parser_parse_definitely (parser);
20847
20848 if (!okay)
20849 unqualified_name = error_mark_node;
20850 else if (unqualified_name
20851 && (qualifying_scope
20852 || (!identifier_p (unqualified_name))))
20853 {
20854 cp_parser_error (parser, "expected unqualified-id");
20855 unqualified_name = error_mark_node;
20856 }
20857 }
20858
20859 if (!unqualified_name)
20860 return NULL;
20861 if (unqualified_name == error_mark_node)
20862 {
20863 declarator = cp_error_declarator;
20864 pack_expansion_p = false;
20865 declarator->parameter_pack_p = false;
20866 break;
20867 }
20868
20869 attrs = cp_parser_std_attribute_spec_seq (parser);
20870
20871 if (qualifying_scope && at_namespace_scope_p ()
20872 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20873 {
20874 /* In the declaration of a member of a template class
20875 outside of the class itself, the SCOPE will sometimes
20876 be a TYPENAME_TYPE. For example, given:
20877
20878 template <typename T>
20879 int S<T>::R::i = 3;
20880
20881 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20882 this context, we must resolve S<T>::R to an ordinary
20883 type, rather than a typename type.
20884
20885 The reason we normally avoid resolving TYPENAME_TYPEs
20886 is that a specialization of `S' might render
20887 `S<T>::R' not a type. However, if `S' is
20888 specialized, then this `i' will not be used, so there
20889 is no harm in resolving the types here. */
20890 tree type;
20891
20892 /* Resolve the TYPENAME_TYPE. */
20893 type = resolve_typename_type (qualifying_scope,
20894 /*only_current_p=*/false);
20895 /* If that failed, the declarator is invalid. */
20896 if (TREE_CODE (type) == TYPENAME_TYPE)
20897 {
20898 if (typedef_variant_p (type))
20899 error_at (declarator_id_start_token->location,
20900 "cannot define member of dependent typedef "
20901 "%qT", type);
20902 else
20903 error_at (declarator_id_start_token->location,
20904 "%<%T::%E%> is not a type",
20905 TYPE_CONTEXT (qualifying_scope),
20906 TYPE_IDENTIFIER (qualifying_scope));
20907 }
20908 qualifying_scope = type;
20909 }
20910
20911 sfk = sfk_none;
20912
20913 if (unqualified_name)
20914 {
20915 tree class_type;
20916
20917 if (qualifying_scope
20918 && CLASS_TYPE_P (qualifying_scope))
20919 class_type = qualifying_scope;
20920 else
20921 class_type = current_class_type;
20922
20923 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20924 {
20925 tree name_type = TREE_TYPE (unqualified_name);
20926
20927 if (!class_type || !same_type_p (name_type, class_type))
20928 {
20929 /* We do not attempt to print the declarator
20930 here because we do not have enough
20931 information about its original syntactic
20932 form. */
20933 cp_parser_error (parser, "invalid declarator");
20934 declarator = cp_error_declarator;
20935 break;
20936 }
20937 else if (qualifying_scope
20938 && CLASSTYPE_USE_TEMPLATE (name_type))
20939 {
20940 error_at (declarator_id_start_token->location,
20941 "invalid use of constructor as a template");
20942 inform (declarator_id_start_token->location,
20943 "use %<%T::%D%> instead of %<%T::%D%> to "
20944 "name the constructor in a qualified name",
20945 class_type,
20946 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20947 class_type, name_type);
20948 declarator = cp_error_declarator;
20949 break;
20950 }
20951 unqualified_name = constructor_name (class_type);
20952 }
20953
20954 if (class_type)
20955 {
20956 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20957 sfk = sfk_destructor;
20958 else if (identifier_p (unqualified_name)
20959 && IDENTIFIER_CONV_OP_P (unqualified_name))
20960 sfk = sfk_conversion;
20961 else if (/* There's no way to declare a constructor
20962 for an unnamed type, even if the type
20963 got a name for linkage purposes. */
20964 !TYPE_WAS_UNNAMED (class_type)
20965 /* Handle correctly (c++/19200):
20966
20967 struct S {
20968 struct T{};
20969 friend void S(T);
20970 };
20971
20972 and also:
20973
20974 namespace N {
20975 void S();
20976 }
20977
20978 struct S {
20979 friend void N::S();
20980 }; */
20981 && (!friend_p || class_type == qualifying_scope)
20982 && constructor_name_p (unqualified_name,
20983 class_type))
20984 sfk = sfk_constructor;
20985 else if (is_overloaded_fn (unqualified_name)
20986 && DECL_CONSTRUCTOR_P (get_first_fn
20987 (unqualified_name)))
20988 sfk = sfk_constructor;
20989
20990 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20991 *ctor_dtor_or_conv_p = -1;
20992 }
20993 }
20994 declarator = make_id_declarator (qualifying_scope,
20995 unqualified_name,
20996 sfk, token->location);
20997 declarator->std_attributes = attrs;
20998 declarator->parameter_pack_p = pack_expansion_p;
20999
21000 if (pack_expansion_p)
21001 maybe_warn_variadic_templates ();
21002 }
21003
21004 handle_declarator:;
21005 scope = get_scope_of_declarator (declarator);
21006 if (scope)
21007 {
21008 /* Any names that appear after the declarator-id for a
21009 member are looked up in the containing scope. */
21010 if (at_function_scope_p ())
21011 {
21012 /* But declarations with qualified-ids can't appear in a
21013 function. */
21014 cp_parser_error (parser, "qualified-id in declaration");
21015 declarator = cp_error_declarator;
21016 break;
21017 }
21018 pushed_scope = push_scope (scope);
21019 }
21020 parser->in_declarator_p = true;
21021 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21022 || (declarator && declarator->kind == cdk_id))
21023 /* Default args are only allowed on function
21024 declarations. */
21025 parser->default_arg_ok_p = saved_default_arg_ok_p;
21026 else
21027 parser->default_arg_ok_p = false;
21028
21029 first = false;
21030 }
21031 /* We're done. */
21032 else
21033 break;
21034 }
21035
21036 /* For an abstract declarator, we might wind up with nothing at this
21037 point. That's an error; the declarator is not optional. */
21038 if (!declarator)
21039 cp_parser_error (parser, "expected declarator");
21040 else if (open_paren)
21041 {
21042 /* Record overly parenthesized declarator so we can give a
21043 diagnostic about confusing decl/expr disambiguation. */
21044 if (declarator->kind == cdk_array)
21045 {
21046 /* If the open and close parens are on different lines, this
21047 is probably a formatting thing, so ignore. */
21048 expanded_location open = expand_location (open_paren->location);
21049 expanded_location close = expand_location (close_paren->location);
21050 if (open.line != close.line || open.file != close.file)
21051 open_paren = NULL;
21052 }
21053 if (open_paren)
21054 declarator->parenthesized = open_paren->location;
21055 }
21056
21057 /* If we entered a scope, we must exit it now. */
21058 if (pushed_scope)
21059 pop_scope (pushed_scope);
21060
21061 parser->default_arg_ok_p = saved_default_arg_ok_p;
21062 parser->in_declarator_p = saved_in_declarator_p;
21063
21064 return declarator;
21065 }
21066
21067 /* Parse a ptr-operator.
21068
21069 ptr-operator:
21070 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21071 * cv-qualifier-seq [opt]
21072 &
21073 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21074 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21075
21076 GNU Extension:
21077
21078 ptr-operator:
21079 & cv-qualifier-seq [opt]
21080
21081 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21082 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21083 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21084 filled in with the TYPE containing the member. *CV_QUALS is
21085 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21086 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21087 Note that the tree codes returned by this function have nothing
21088 to do with the types of trees that will be eventually be created
21089 to represent the pointer or reference type being parsed. They are
21090 just constants with suggestive names. */
21091 static enum tree_code
21092 cp_parser_ptr_operator (cp_parser* parser,
21093 tree* type,
21094 cp_cv_quals *cv_quals,
21095 tree *attributes)
21096 {
21097 enum tree_code code = ERROR_MARK;
21098 cp_token *token;
21099 tree attrs = NULL_TREE;
21100
21101 /* Assume that it's not a pointer-to-member. */
21102 *type = NULL_TREE;
21103 /* And that there are no cv-qualifiers. */
21104 *cv_quals = TYPE_UNQUALIFIED;
21105
21106 /* Peek at the next token. */
21107 token = cp_lexer_peek_token (parser->lexer);
21108
21109 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21110 if (token->type == CPP_MULT)
21111 code = INDIRECT_REF;
21112 else if (token->type == CPP_AND)
21113 code = ADDR_EXPR;
21114 else if ((cxx_dialect != cxx98) &&
21115 token->type == CPP_AND_AND) /* C++0x only */
21116 code = NON_LVALUE_EXPR;
21117
21118 if (code != ERROR_MARK)
21119 {
21120 /* Consume the `*', `&' or `&&'. */
21121 cp_lexer_consume_token (parser->lexer);
21122
21123 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21124 `&', if we are allowing GNU extensions. (The only qualifier
21125 that can legally appear after `&' is `restrict', but that is
21126 enforced during semantic analysis. */
21127 if (code == INDIRECT_REF
21128 || cp_parser_allow_gnu_extensions_p (parser))
21129 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21130
21131 attrs = cp_parser_std_attribute_spec_seq (parser);
21132 if (attributes != NULL)
21133 *attributes = attrs;
21134 }
21135 else
21136 {
21137 /* Try the pointer-to-member case. */
21138 cp_parser_parse_tentatively (parser);
21139 /* Look for the optional `::' operator. */
21140 cp_parser_global_scope_opt (parser,
21141 /*current_scope_valid_p=*/false);
21142 /* Look for the nested-name specifier. */
21143 token = cp_lexer_peek_token (parser->lexer);
21144 cp_parser_nested_name_specifier (parser,
21145 /*typename_keyword_p=*/false,
21146 /*check_dependency_p=*/true,
21147 /*type_p=*/false,
21148 /*is_declaration=*/false);
21149 /* If we found it, and the next token is a `*', then we are
21150 indeed looking at a pointer-to-member operator. */
21151 if (!cp_parser_error_occurred (parser)
21152 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21153 {
21154 /* Indicate that the `*' operator was used. */
21155 code = INDIRECT_REF;
21156
21157 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21158 error_at (token->location, "%qD is a namespace", parser->scope);
21159 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21160 error_at (token->location, "cannot form pointer to member of "
21161 "non-class %q#T", parser->scope);
21162 else
21163 {
21164 /* The type of which the member is a member is given by the
21165 current SCOPE. */
21166 *type = parser->scope;
21167 /* The next name will not be qualified. */
21168 parser->scope = NULL_TREE;
21169 parser->qualifying_scope = NULL_TREE;
21170 parser->object_scope = NULL_TREE;
21171 /* Look for optional c++11 attributes. */
21172 attrs = cp_parser_std_attribute_spec_seq (parser);
21173 if (attributes != NULL)
21174 *attributes = attrs;
21175 /* Look for the optional cv-qualifier-seq. */
21176 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21177 }
21178 }
21179 /* If that didn't work we don't have a ptr-operator. */
21180 if (!cp_parser_parse_definitely (parser))
21181 cp_parser_error (parser, "expected ptr-operator");
21182 }
21183
21184 return code;
21185 }
21186
21187 /* Parse an (optional) cv-qualifier-seq.
21188
21189 cv-qualifier-seq:
21190 cv-qualifier cv-qualifier-seq [opt]
21191
21192 cv-qualifier:
21193 const
21194 volatile
21195
21196 GNU Extension:
21197
21198 cv-qualifier:
21199 __restrict__
21200
21201 Returns a bitmask representing the cv-qualifiers. */
21202
21203 static cp_cv_quals
21204 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21205 {
21206 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21207
21208 while (true)
21209 {
21210 cp_token *token;
21211 cp_cv_quals cv_qualifier;
21212
21213 /* Peek at the next token. */
21214 token = cp_lexer_peek_token (parser->lexer);
21215 /* See if it's a cv-qualifier. */
21216 switch (token->keyword)
21217 {
21218 case RID_CONST:
21219 cv_qualifier = TYPE_QUAL_CONST;
21220 break;
21221
21222 case RID_VOLATILE:
21223 cv_qualifier = TYPE_QUAL_VOLATILE;
21224 break;
21225
21226 case RID_RESTRICT:
21227 cv_qualifier = TYPE_QUAL_RESTRICT;
21228 break;
21229
21230 default:
21231 cv_qualifier = TYPE_UNQUALIFIED;
21232 break;
21233 }
21234
21235 if (!cv_qualifier)
21236 break;
21237
21238 if (cv_quals & cv_qualifier)
21239 {
21240 gcc_rich_location richloc (token->location);
21241 richloc.add_fixit_remove ();
21242 error_at (&richloc, "duplicate cv-qualifier");
21243 cp_lexer_purge_token (parser->lexer);
21244 }
21245 else
21246 {
21247 cp_lexer_consume_token (parser->lexer);
21248 cv_quals |= cv_qualifier;
21249 }
21250 }
21251
21252 return cv_quals;
21253 }
21254
21255 /* Parse an (optional) ref-qualifier
21256
21257 ref-qualifier:
21258 &
21259 &&
21260
21261 Returns cp_ref_qualifier representing ref-qualifier. */
21262
21263 static cp_ref_qualifier
21264 cp_parser_ref_qualifier_opt (cp_parser* parser)
21265 {
21266 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21267
21268 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21269 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21270 return ref_qual;
21271
21272 while (true)
21273 {
21274 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21275 cp_token *token = cp_lexer_peek_token (parser->lexer);
21276
21277 switch (token->type)
21278 {
21279 case CPP_AND:
21280 curr_ref_qual = REF_QUAL_LVALUE;
21281 break;
21282
21283 case CPP_AND_AND:
21284 curr_ref_qual = REF_QUAL_RVALUE;
21285 break;
21286
21287 default:
21288 curr_ref_qual = REF_QUAL_NONE;
21289 break;
21290 }
21291
21292 if (!curr_ref_qual)
21293 break;
21294 else if (ref_qual)
21295 {
21296 error_at (token->location, "multiple ref-qualifiers");
21297 cp_lexer_purge_token (parser->lexer);
21298 }
21299 else
21300 {
21301 ref_qual = curr_ref_qual;
21302 cp_lexer_consume_token (parser->lexer);
21303 }
21304 }
21305
21306 return ref_qual;
21307 }
21308
21309 /* Parse an optional tx-qualifier.
21310
21311 tx-qualifier:
21312 transaction_safe
21313 transaction_safe_dynamic */
21314
21315 static tree
21316 cp_parser_tx_qualifier_opt (cp_parser *parser)
21317 {
21318 cp_token *token = cp_lexer_peek_token (parser->lexer);
21319 if (token->type == CPP_NAME)
21320 {
21321 tree name = token->u.value;
21322 const char *p = IDENTIFIER_POINTER (name);
21323 const int len = strlen ("transaction_safe");
21324 if (!strncmp (p, "transaction_safe", len))
21325 {
21326 p += len;
21327 if (*p == '\0'
21328 || !strcmp (p, "_dynamic"))
21329 {
21330 cp_lexer_consume_token (parser->lexer);
21331 if (!flag_tm)
21332 {
21333 error ("%qE requires %<-fgnu-tm%>", name);
21334 return NULL_TREE;
21335 }
21336 else
21337 return name;
21338 }
21339 }
21340 }
21341 return NULL_TREE;
21342 }
21343
21344 /* Parse an (optional) virt-specifier-seq.
21345
21346 virt-specifier-seq:
21347 virt-specifier virt-specifier-seq [opt]
21348
21349 virt-specifier:
21350 override
21351 final
21352
21353 Returns a bitmask representing the virt-specifiers. */
21354
21355 static cp_virt_specifiers
21356 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21357 {
21358 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21359
21360 while (true)
21361 {
21362 cp_token *token;
21363 cp_virt_specifiers virt_specifier;
21364
21365 /* Peek at the next token. */
21366 token = cp_lexer_peek_token (parser->lexer);
21367 /* See if it's a virt-specifier-qualifier. */
21368 if (token->type != CPP_NAME)
21369 break;
21370 if (id_equal (token->u.value, "override"))
21371 {
21372 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21373 virt_specifier = VIRT_SPEC_OVERRIDE;
21374 }
21375 else if (id_equal (token->u.value, "final"))
21376 {
21377 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21378 virt_specifier = VIRT_SPEC_FINAL;
21379 }
21380 else if (id_equal (token->u.value, "__final"))
21381 {
21382 virt_specifier = VIRT_SPEC_FINAL;
21383 }
21384 else
21385 break;
21386
21387 if (virt_specifiers & virt_specifier)
21388 {
21389 gcc_rich_location richloc (token->location);
21390 richloc.add_fixit_remove ();
21391 error_at (&richloc, "duplicate virt-specifier");
21392 cp_lexer_purge_token (parser->lexer);
21393 }
21394 else
21395 {
21396 cp_lexer_consume_token (parser->lexer);
21397 virt_specifiers |= virt_specifier;
21398 }
21399 }
21400 return virt_specifiers;
21401 }
21402
21403 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21404 is in scope even though it isn't real. */
21405
21406 void
21407 inject_this_parameter (tree ctype, cp_cv_quals quals)
21408 {
21409 tree this_parm;
21410
21411 if (current_class_ptr)
21412 {
21413 /* We don't clear this between NSDMIs. Is it already what we want? */
21414 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21415 if (DECL_P (current_class_ptr)
21416 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21417 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21418 && cp_type_quals (type) == quals)
21419 return;
21420 }
21421
21422 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21423 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21424 current_class_ptr = NULL_TREE;
21425 current_class_ref
21426 = cp_build_fold_indirect_ref (this_parm);
21427 current_class_ptr = this_parm;
21428 }
21429
21430 /* Return true iff our current scope is a non-static data member
21431 initializer. */
21432
21433 bool
21434 parsing_nsdmi (void)
21435 {
21436 /* We recognize NSDMI context by the context-less 'this' pointer set up
21437 by the function above. */
21438 if (current_class_ptr
21439 && TREE_CODE (current_class_ptr) == PARM_DECL
21440 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21441 return true;
21442 return false;
21443 }
21444
21445 /* Parse a late-specified return type, if any. This is not a separate
21446 non-terminal, but part of a function declarator, which looks like
21447
21448 -> trailing-type-specifier-seq abstract-declarator(opt)
21449
21450 Returns the type indicated by the type-id.
21451
21452 In addition to this, parse any queued up #pragma omp declare simd
21453 clauses, and #pragma acc routine clauses.
21454
21455 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21456 function. */
21457
21458 static tree
21459 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21460 tree& requires_clause, cp_cv_quals quals)
21461 {
21462 cp_token *token;
21463 tree type = NULL_TREE;
21464 bool declare_simd_p = (parser->omp_declare_simd
21465 && declarator
21466 && declarator->kind == cdk_id);
21467
21468 bool oacc_routine_p = (parser->oacc_routine
21469 && declarator
21470 && declarator->kind == cdk_id);
21471
21472 /* Peek at the next token. */
21473 token = cp_lexer_peek_token (parser->lexer);
21474 /* A late-specified return type is indicated by an initial '->'. */
21475 if (token->type != CPP_DEREF
21476 && token->keyword != RID_REQUIRES
21477 && !(token->type == CPP_NAME
21478 && token->u.value == ridpointers[RID_REQUIRES])
21479 && !(declare_simd_p || oacc_routine_p))
21480 return NULL_TREE;
21481
21482 tree save_ccp = current_class_ptr;
21483 tree save_ccr = current_class_ref;
21484 if (quals >= 0)
21485 {
21486 /* DR 1207: 'this' is in scope in the trailing return type. */
21487 inject_this_parameter (current_class_type, quals);
21488 }
21489
21490 if (token->type == CPP_DEREF)
21491 {
21492 /* Consume the ->. */
21493 cp_lexer_consume_token (parser->lexer);
21494
21495 type = cp_parser_trailing_type_id (parser);
21496 }
21497
21498 /* Function declarations may be followed by a trailing
21499 requires-clause. */
21500 requires_clause = cp_parser_requires_clause_opt (parser);
21501
21502 if (declare_simd_p)
21503 declarator->attributes
21504 = cp_parser_late_parsing_omp_declare_simd (parser,
21505 declarator->attributes);
21506 if (oacc_routine_p)
21507 declarator->attributes
21508 = cp_parser_late_parsing_oacc_routine (parser,
21509 declarator->attributes);
21510
21511 if (quals >= 0)
21512 {
21513 current_class_ptr = save_ccp;
21514 current_class_ref = save_ccr;
21515 }
21516
21517 return type;
21518 }
21519
21520 /* Parse a declarator-id.
21521
21522 declarator-id:
21523 id-expression
21524 :: [opt] nested-name-specifier [opt] type-name
21525
21526 In the `id-expression' case, the value returned is as for
21527 cp_parser_id_expression if the id-expression was an unqualified-id.
21528 If the id-expression was a qualified-id, then a SCOPE_REF is
21529 returned. The first operand is the scope (either a NAMESPACE_DECL
21530 or TREE_TYPE), but the second is still just a representation of an
21531 unqualified-id. */
21532
21533 static tree
21534 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21535 {
21536 tree id;
21537 /* The expression must be an id-expression. Assume that qualified
21538 names are the names of types so that:
21539
21540 template <class T>
21541 int S<T>::R::i = 3;
21542
21543 will work; we must treat `S<T>::R' as the name of a type.
21544 Similarly, assume that qualified names are templates, where
21545 required, so that:
21546
21547 template <class T>
21548 int S<T>::R<T>::i = 3;
21549
21550 will work, too. */
21551 id = cp_parser_id_expression (parser,
21552 /*template_keyword_p=*/false,
21553 /*check_dependency_p=*/false,
21554 /*template_p=*/NULL,
21555 /*declarator_p=*/true,
21556 optional_p);
21557 if (id && BASELINK_P (id))
21558 id = BASELINK_FUNCTIONS (id);
21559 return id;
21560 }
21561
21562 /* Parse a type-id.
21563
21564 type-id:
21565 type-specifier-seq abstract-declarator [opt]
21566
21567 The parser flags FLAGS is used to control type-specifier parsing.
21568
21569 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21570
21571 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21572 i.e. we've just seen "->".
21573
21574 Returns the TYPE specified. */
21575
21576 static tree
21577 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
21578 bool is_template_arg, bool is_trailing_return,
21579 location_t *type_location)
21580 {
21581 cp_decl_specifier_seq type_specifier_seq;
21582 cp_declarator *abstract_declarator;
21583
21584 /* Parse the type-specifier-seq. */
21585 cp_parser_type_specifier_seq (parser, flags,
21586 /*is_declaration=*/false,
21587 is_trailing_return,
21588 &type_specifier_seq);
21589 if (type_location)
21590 *type_location = type_specifier_seq.locations[ds_type_spec];
21591
21592 if (is_template_arg && type_specifier_seq.type
21593 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21594 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21595 /* A bare template name as a template argument is a template template
21596 argument, not a placeholder, so fail parsing it as a type argument. */
21597 {
21598 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21599 cp_parser_simulate_error (parser);
21600 return error_mark_node;
21601 }
21602 if (type_specifier_seq.type == error_mark_node)
21603 return error_mark_node;
21604
21605 /* There might or might not be an abstract declarator. */
21606 cp_parser_parse_tentatively (parser);
21607 /* Look for the declarator. */
21608 abstract_declarator
21609 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
21610 CP_PARSER_FLAGS_NONE, NULL,
21611 /*parenthesized_p=*/NULL,
21612 /*member_p=*/false,
21613 /*friend_p=*/false);
21614 /* Check to see if there really was a declarator. */
21615 if (!cp_parser_parse_definitely (parser))
21616 abstract_declarator = NULL;
21617
21618 if (type_specifier_seq.type
21619 /* The concepts TS allows 'auto' as a type-id. */
21620 && (!flag_concepts || parser->in_type_id_in_expr_p)
21621 /* None of the valid uses of 'auto' in C++14 involve the type-id
21622 nonterminal, but it is valid in a trailing-return-type. */
21623 && !(cxx_dialect >= cxx14 && is_trailing_return))
21624 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21625 {
21626 /* A type-id with type 'auto' is only ok if the abstract declarator
21627 is a function declarator with a late-specified return type.
21628
21629 A type-id with 'auto' is also valid in a trailing-return-type
21630 in a compound-requirement. */
21631 if (abstract_declarator
21632 && abstract_declarator->kind == cdk_function
21633 && abstract_declarator->u.function.late_return_type)
21634 /* OK */;
21635 else if (parser->in_result_type_constraint_p)
21636 /* OK */;
21637 else
21638 {
21639 location_t loc = type_specifier_seq.locations[ds_type_spec];
21640 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21641 {
21642 error_at (loc, "missing template arguments after %qT",
21643 auto_node);
21644 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21645 tmpl);
21646 }
21647 else
21648 error_at (loc, "invalid use of %qT", auto_node);
21649 return error_mark_node;
21650 }
21651 }
21652
21653 return groktypename (&type_specifier_seq, abstract_declarator,
21654 is_template_arg);
21655 }
21656
21657 /* Wrapper for cp_parser_type_id_1. */
21658
21659 static tree
21660 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
21661 location_t *type_location)
21662 {
21663 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
21664 }
21665
21666 /* Wrapper for cp_parser_type_id_1. */
21667
21668 static tree
21669 cp_parser_template_type_arg (cp_parser *parser)
21670 {
21671 tree r;
21672 const char *saved_message = parser->type_definition_forbidden_message;
21673 parser->type_definition_forbidden_message
21674 = G_("types may not be defined in template arguments");
21675 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
21676 parser->type_definition_forbidden_message = saved_message;
21677 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21678 {
21679 error ("invalid use of %<auto%> in template argument");
21680 r = error_mark_node;
21681 }
21682 return r;
21683 }
21684
21685 /* Wrapper for cp_parser_type_id_1. */
21686
21687 static tree
21688 cp_parser_trailing_type_id (cp_parser *parser)
21689 {
21690 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21691 false, true, NULL);
21692 }
21693
21694 /* Parse a type-specifier-seq.
21695
21696 type-specifier-seq:
21697 type-specifier type-specifier-seq [opt]
21698
21699 GNU extension:
21700
21701 type-specifier-seq:
21702 attributes type-specifier-seq [opt]
21703
21704 The parser flags FLAGS is used to control type-specifier parsing.
21705
21706 If IS_DECLARATION is true, we are at the start of a "condition" or
21707 exception-declaration, so we might be followed by a declarator-id.
21708
21709 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21710 i.e. we've just seen "->".
21711
21712 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21713
21714 static void
21715 cp_parser_type_specifier_seq (cp_parser* parser,
21716 cp_parser_flags flags,
21717 bool is_declaration,
21718 bool is_trailing_return,
21719 cp_decl_specifier_seq *type_specifier_seq)
21720 {
21721 bool seen_type_specifier = false;
21722 cp_token *start_token = NULL;
21723
21724 /* Clear the TYPE_SPECIFIER_SEQ. */
21725 clear_decl_specs (type_specifier_seq);
21726
21727 flags |= CP_PARSER_FLAGS_OPTIONAL;
21728 /* In the context of a trailing return type, enum E { } is an
21729 elaborated-type-specifier followed by a function-body, not an
21730 enum-specifier. */
21731 if (is_trailing_return)
21732 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21733
21734 /* Parse the type-specifiers and attributes. */
21735 while (true)
21736 {
21737 tree type_specifier;
21738 bool is_cv_qualifier;
21739
21740 /* Check for attributes first. */
21741 if (cp_next_tokens_can_be_attribute_p (parser))
21742 {
21743 type_specifier_seq->attributes
21744 = attr_chainon (type_specifier_seq->attributes,
21745 cp_parser_attributes_opt (parser));
21746 continue;
21747 }
21748
21749 /* record the token of the beginning of the type specifier seq,
21750 for error reporting purposes*/
21751 if (!start_token)
21752 start_token = cp_lexer_peek_token (parser->lexer);
21753
21754 /* Look for the type-specifier. */
21755 type_specifier = cp_parser_type_specifier (parser,
21756 flags,
21757 type_specifier_seq,
21758 /*is_declaration=*/false,
21759 NULL,
21760 &is_cv_qualifier);
21761 if (!type_specifier)
21762 {
21763 /* If the first type-specifier could not be found, this is not a
21764 type-specifier-seq at all. */
21765 if (!seen_type_specifier)
21766 {
21767 /* Set in_declarator_p to avoid skipping to the semicolon. */
21768 int in_decl = parser->in_declarator_p;
21769 parser->in_declarator_p = true;
21770
21771 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21772 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21773 cp_parser_error (parser, "expected type-specifier");
21774
21775 parser->in_declarator_p = in_decl;
21776
21777 type_specifier_seq->type = error_mark_node;
21778 return;
21779 }
21780 /* If subsequent type-specifiers could not be found, the
21781 type-specifier-seq is complete. */
21782 break;
21783 }
21784
21785 seen_type_specifier = true;
21786 /* The standard says that a condition can be:
21787
21788 type-specifier-seq declarator = assignment-expression
21789
21790 However, given:
21791
21792 struct S {};
21793 if (int S = ...)
21794
21795 we should treat the "S" as a declarator, not as a
21796 type-specifier. The standard doesn't say that explicitly for
21797 type-specifier-seq, but it does say that for
21798 decl-specifier-seq in an ordinary declaration. Perhaps it
21799 would be clearer just to allow a decl-specifier-seq here, and
21800 then add a semantic restriction that if any decl-specifiers
21801 that are not type-specifiers appear, the program is invalid. */
21802 if (is_declaration && !is_cv_qualifier)
21803 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21804 }
21805 }
21806
21807 /* Return whether the function currently being declared has an associated
21808 template parameter list. */
21809
21810 static bool
21811 function_being_declared_is_template_p (cp_parser* parser)
21812 {
21813 if (!current_template_parms || processing_template_parmlist)
21814 return false;
21815
21816 if (parser->implicit_template_scope)
21817 return true;
21818
21819 if (at_class_scope_p ()
21820 && TYPE_BEING_DEFINED (current_class_type))
21821 return parser->num_template_parameter_lists != 0;
21822
21823 return ((int) parser->num_template_parameter_lists > template_class_depth
21824 (current_class_type));
21825 }
21826
21827 /* Parse a parameter-declaration-clause.
21828
21829 parameter-declaration-clause:
21830 parameter-declaration-list [opt] ... [opt]
21831 parameter-declaration-list , ...
21832
21833 The parser flags FLAGS is used to control type-specifier parsing.
21834
21835 Returns a representation for the parameter declarations. A return
21836 value of NULL indicates a parameter-declaration-clause consisting
21837 only of an ellipsis. */
21838
21839 static tree
21840 cp_parser_parameter_declaration_clause (cp_parser* parser,
21841 cp_parser_flags flags)
21842 {
21843 tree parameters;
21844 cp_token *token;
21845 bool ellipsis_p;
21846
21847 temp_override<bool> cleanup
21848 (parser->auto_is_implicit_function_template_parm_p);
21849
21850 if (!processing_specialization
21851 && !processing_template_parmlist
21852 && !processing_explicit_instantiation
21853 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21854 actual function or a random abstract declarator. */
21855 && parser->default_arg_ok_p)
21856 if (!current_function_decl
21857 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21858 parser->auto_is_implicit_function_template_parm_p = true;
21859
21860 /* Peek at the next token. */
21861 token = cp_lexer_peek_token (parser->lexer);
21862 /* Check for trivial parameter-declaration-clauses. */
21863 if (token->type == CPP_ELLIPSIS)
21864 {
21865 /* Consume the `...' token. */
21866 cp_lexer_consume_token (parser->lexer);
21867 return NULL_TREE;
21868 }
21869 else if (token->type == CPP_CLOSE_PAREN)
21870 /* There are no parameters. */
21871 return void_list_node;
21872 /* Check for `(void)', too, which is a special case. */
21873 else if (token->keyword == RID_VOID
21874 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21875 == CPP_CLOSE_PAREN))
21876 {
21877 /* Consume the `void' token. */
21878 cp_lexer_consume_token (parser->lexer);
21879 /* There are no parameters. */
21880 return void_list_node;
21881 }
21882
21883 /* Parse the parameter-declaration-list. */
21884 parameters = cp_parser_parameter_declaration_list (parser, flags);
21885 /* If a parse error occurred while parsing the
21886 parameter-declaration-list, then the entire
21887 parameter-declaration-clause is erroneous. */
21888 if (parameters == error_mark_node)
21889 return NULL_TREE;
21890
21891 /* Peek at the next token. */
21892 token = cp_lexer_peek_token (parser->lexer);
21893 /* If it's a `,', the clause should terminate with an ellipsis. */
21894 if (token->type == CPP_COMMA)
21895 {
21896 /* Consume the `,'. */
21897 cp_lexer_consume_token (parser->lexer);
21898 /* Expect an ellipsis. */
21899 ellipsis_p
21900 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21901 }
21902 /* It might also be `...' if the optional trailing `,' was
21903 omitted. */
21904 else if (token->type == CPP_ELLIPSIS)
21905 {
21906 /* Consume the `...' token. */
21907 cp_lexer_consume_token (parser->lexer);
21908 /* And remember that we saw it. */
21909 ellipsis_p = true;
21910 }
21911 else
21912 ellipsis_p = false;
21913
21914 /* Finish the parameter list. */
21915 if (!ellipsis_p)
21916 parameters = chainon (parameters, void_list_node);
21917
21918 return parameters;
21919 }
21920
21921 /* Parse a parameter-declaration-list.
21922
21923 parameter-declaration-list:
21924 parameter-declaration
21925 parameter-declaration-list , parameter-declaration
21926
21927 The parser flags FLAGS is used to control type-specifier parsing.
21928
21929 Returns a representation of the parameter-declaration-list, as for
21930 cp_parser_parameter_declaration_clause. However, the
21931 `void_list_node' is never appended to the list. */
21932
21933 static tree
21934 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
21935 {
21936 tree parameters = NULL_TREE;
21937 tree *tail = &parameters;
21938 bool saved_in_unbraced_linkage_specification_p;
21939 int index = 0;
21940
21941 /* The special considerations that apply to a function within an
21942 unbraced linkage specifications do not apply to the parameters
21943 to the function. */
21944 saved_in_unbraced_linkage_specification_p
21945 = parser->in_unbraced_linkage_specification_p;
21946 parser->in_unbraced_linkage_specification_p = false;
21947
21948 /* Look for more parameters. */
21949 while (true)
21950 {
21951 cp_parameter_declarator *parameter;
21952 tree decl = error_mark_node;
21953 bool parenthesized_p = false;
21954
21955 /* Parse the parameter. */
21956 parameter
21957 = cp_parser_parameter_declaration (parser, flags,
21958 /*template_parm_p=*/false,
21959 &parenthesized_p);
21960
21961 /* We don't know yet if the enclosing context is deprecated, so wait
21962 and warn in grokparms if appropriate. */
21963 deprecated_state = DEPRECATED_SUPPRESS;
21964
21965 if (parameter)
21966 {
21967 decl = grokdeclarator (parameter->declarator,
21968 &parameter->decl_specifiers,
21969 PARM,
21970 parameter->default_argument != NULL_TREE,
21971 &parameter->decl_specifiers.attributes);
21972 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21973 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21974 }
21975
21976 deprecated_state = DEPRECATED_NORMAL;
21977
21978 /* If a parse error occurred parsing the parameter declaration,
21979 then the entire parameter-declaration-list is erroneous. */
21980 if (decl == error_mark_node)
21981 {
21982 parameters = error_mark_node;
21983 break;
21984 }
21985
21986 if (parameter->decl_specifiers.attributes)
21987 cplus_decl_attributes (&decl,
21988 parameter->decl_specifiers.attributes,
21989 0);
21990 if (DECL_NAME (decl))
21991 decl = pushdecl (decl);
21992
21993 if (decl != error_mark_node)
21994 {
21995 retrofit_lang_decl (decl);
21996 DECL_PARM_INDEX (decl) = ++index;
21997 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21998 }
21999
22000 /* Add the new parameter to the list. */
22001 *tail = build_tree_list (parameter->default_argument, decl);
22002 tail = &TREE_CHAIN (*tail);
22003
22004 /* Peek at the next token. */
22005 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22006 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22007 /* These are for Objective-C++ */
22008 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22009 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22010 /* The parameter-declaration-list is complete. */
22011 break;
22012 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22013 {
22014 cp_token *token;
22015
22016 /* Peek at the next token. */
22017 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22018 /* If it's an ellipsis, then the list is complete. */
22019 if (token->type == CPP_ELLIPSIS)
22020 break;
22021 /* Otherwise, there must be more parameters. Consume the
22022 `,'. */
22023 cp_lexer_consume_token (parser->lexer);
22024 /* When parsing something like:
22025
22026 int i(float f, double d)
22027
22028 we can tell after seeing the declaration for "f" that we
22029 are not looking at an initialization of a variable "i",
22030 but rather at the declaration of a function "i".
22031
22032 Due to the fact that the parsing of template arguments
22033 (as specified to a template-id) requires backtracking we
22034 cannot use this technique when inside a template argument
22035 list. */
22036 if (!parser->in_template_argument_list_p
22037 && !parser->in_type_id_in_expr_p
22038 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22039 /* However, a parameter-declaration of the form
22040 "float(f)" (which is a valid declaration of a
22041 parameter "f") can also be interpreted as an
22042 expression (the conversion of "f" to "float"). */
22043 && !parenthesized_p)
22044 cp_parser_commit_to_tentative_parse (parser);
22045 }
22046 else
22047 {
22048 cp_parser_error (parser, "expected %<,%> or %<...%>");
22049 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22050 cp_parser_skip_to_closing_parenthesis (parser,
22051 /*recovering=*/true,
22052 /*or_comma=*/false,
22053 /*consume_paren=*/false);
22054 break;
22055 }
22056 }
22057
22058 parser->in_unbraced_linkage_specification_p
22059 = saved_in_unbraced_linkage_specification_p;
22060
22061 /* Reset implicit_template_scope if we are about to leave the function
22062 parameter list that introduced it. Note that for out-of-line member
22063 definitions, there will be one or more class scopes before we get to
22064 the template parameter scope. */
22065
22066 if (cp_binding_level *its = parser->implicit_template_scope)
22067 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22068 {
22069 while (maybe_its->kind == sk_class)
22070 maybe_its = maybe_its->level_chain;
22071 if (maybe_its == its)
22072 {
22073 parser->implicit_template_parms = 0;
22074 parser->implicit_template_scope = 0;
22075 }
22076 }
22077
22078 return parameters;
22079 }
22080
22081 /* Parse a parameter declaration.
22082
22083 parameter-declaration:
22084 decl-specifier-seq ... [opt] declarator
22085 decl-specifier-seq declarator = assignment-expression
22086 decl-specifier-seq ... [opt] abstract-declarator [opt]
22087 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22088
22089 The parser flags FLAGS is used to control type-specifier parsing.
22090
22091 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22092 declares a template parameter. (In that case, a non-nested `>'
22093 token encountered during the parsing of the assignment-expression
22094 is not interpreted as a greater-than operator.)
22095
22096 Returns a representation of the parameter, or NULL if an error
22097 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22098 true iff the declarator is of the form "(p)". */
22099
22100 static cp_parameter_declarator *
22101 cp_parser_parameter_declaration (cp_parser *parser,
22102 cp_parser_flags flags,
22103 bool template_parm_p,
22104 bool *parenthesized_p)
22105 {
22106 int declares_class_or_enum;
22107 cp_decl_specifier_seq decl_specifiers;
22108 cp_declarator *declarator;
22109 tree default_argument;
22110 cp_token *token = NULL, *declarator_token_start = NULL;
22111 const char *saved_message;
22112 bool template_parameter_pack_p = false;
22113
22114 /* In a template parameter, `>' is not an operator.
22115
22116 [temp.param]
22117
22118 When parsing a default template-argument for a non-type
22119 template-parameter, the first non-nested `>' is taken as the end
22120 of the template parameter-list rather than a greater-than
22121 operator. */
22122
22123 /* Type definitions may not appear in parameter types. */
22124 saved_message = parser->type_definition_forbidden_message;
22125 parser->type_definition_forbidden_message
22126 = G_("types may not be defined in parameter types");
22127
22128 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22129 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22130 (current_template_parms)) : 0);
22131
22132 /* Parse the declaration-specifiers. */
22133 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22134 cp_parser_decl_specifier_seq (parser,
22135 flags,
22136 &decl_specifiers,
22137 &declares_class_or_enum);
22138
22139 /* Complain about missing 'typename' or other invalid type names. */
22140 if (!decl_specifiers.any_type_specifiers_p
22141 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22142 decl_specifiers.type = error_mark_node;
22143
22144 /* If an error occurred, there's no reason to attempt to parse the
22145 rest of the declaration. */
22146 if (cp_parser_error_occurred (parser))
22147 {
22148 parser->type_definition_forbidden_message = saved_message;
22149 return NULL;
22150 }
22151
22152 /* Peek at the next token. */
22153 token = cp_lexer_peek_token (parser->lexer);
22154
22155 /* If the next token is a `)', `,', `=', `>', or `...', then there
22156 is no declarator. However, when variadic templates are enabled,
22157 there may be a declarator following `...'. */
22158 if (token->type == CPP_CLOSE_PAREN
22159 || token->type == CPP_COMMA
22160 || token->type == CPP_EQ
22161 || token->type == CPP_GREATER)
22162 {
22163 declarator = NULL;
22164 if (parenthesized_p)
22165 *parenthesized_p = false;
22166 }
22167 /* Otherwise, there should be a declarator. */
22168 else
22169 {
22170 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22171 parser->default_arg_ok_p = false;
22172
22173 /* After seeing a decl-specifier-seq, if the next token is not a
22174 "(", there is no possibility that the code is a valid
22175 expression. Therefore, if parsing tentatively, we commit at
22176 this point. */
22177 if (!parser->in_template_argument_list_p
22178 /* In an expression context, having seen:
22179
22180 (int((char ...
22181
22182 we cannot be sure whether we are looking at a
22183 function-type (taking a "char" as a parameter) or a cast
22184 of some object of type "char" to "int". */
22185 && !parser->in_type_id_in_expr_p
22186 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22187 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22188 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22189 cp_parser_commit_to_tentative_parse (parser);
22190 /* Parse the declarator. */
22191 declarator_token_start = token;
22192 declarator = cp_parser_declarator (parser,
22193 CP_PARSER_DECLARATOR_EITHER,
22194 CP_PARSER_FLAGS_NONE,
22195 /*ctor_dtor_or_conv_p=*/NULL,
22196 parenthesized_p,
22197 /*member_p=*/false,
22198 /*friend_p=*/false);
22199 parser->default_arg_ok_p = saved_default_arg_ok_p;
22200 /* After the declarator, allow more attributes. */
22201 decl_specifiers.attributes
22202 = attr_chainon (decl_specifiers.attributes,
22203 cp_parser_attributes_opt (parser));
22204
22205 /* If the declarator is a template parameter pack, remember that and
22206 clear the flag in the declarator itself so we don't get errors
22207 from grokdeclarator. */
22208 if (template_parm_p && declarator && declarator->parameter_pack_p)
22209 {
22210 declarator->parameter_pack_p = false;
22211 template_parameter_pack_p = true;
22212 }
22213 }
22214
22215 /* If the next token is an ellipsis, and we have not seen a declarator
22216 name, and if either the type of the declarator contains parameter
22217 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22218 for, eg, abbreviated integral type names), then we actually have a
22219 parameter pack expansion expression. Otherwise, leave the ellipsis
22220 for a C-style variadic function. */
22221 token = cp_lexer_peek_token (parser->lexer);
22222
22223 /* If a function parameter pack was specified and an implicit template
22224 parameter was introduced during cp_parser_parameter_declaration,
22225 change any implicit parameters introduced into packs. */
22226 if (parser->implicit_template_parms
22227 && ((token->type == CPP_ELLIPSIS
22228 && declarator_can_be_parameter_pack (declarator))
22229 || (declarator && declarator->parameter_pack_p)))
22230 {
22231 int latest_template_parm_idx = TREE_VEC_LENGTH
22232 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22233
22234 if (latest_template_parm_idx != template_parm_idx)
22235 decl_specifiers.type = convert_generic_types_to_packs
22236 (decl_specifiers.type,
22237 template_parm_idx, latest_template_parm_idx);
22238 }
22239
22240 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22241 {
22242 tree type = decl_specifiers.type;
22243
22244 if (type && DECL_P (type))
22245 type = TREE_TYPE (type);
22246
22247 if (((type
22248 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22249 && (template_parm_p || uses_parameter_packs (type)))
22250 || (!type && template_parm_p))
22251 && declarator_can_be_parameter_pack (declarator))
22252 {
22253 /* Consume the `...'. */
22254 cp_lexer_consume_token (parser->lexer);
22255 maybe_warn_variadic_templates ();
22256
22257 /* Build a pack expansion type */
22258 if (template_parm_p)
22259 template_parameter_pack_p = true;
22260 else if (declarator)
22261 declarator->parameter_pack_p = true;
22262 else
22263 decl_specifiers.type = make_pack_expansion (type);
22264 }
22265 }
22266
22267 /* The restriction on defining new types applies only to the type
22268 of the parameter, not to the default argument. */
22269 parser->type_definition_forbidden_message = saved_message;
22270
22271 /* If the next token is `=', then process a default argument. */
22272 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22273 {
22274 tree type = decl_specifiers.type;
22275 token = cp_lexer_peek_token (parser->lexer);
22276 /* If we are defining a class, then the tokens that make up the
22277 default argument must be saved and processed later. */
22278 if (!template_parm_p && at_class_scope_p ()
22279 && TYPE_BEING_DEFINED (current_class_type)
22280 && !LAMBDA_TYPE_P (current_class_type))
22281 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22282
22283 // A constrained-type-specifier may declare a type template-parameter.
22284 else if (declares_constrained_type_template_parameter (type))
22285 default_argument
22286 = cp_parser_default_type_template_argument (parser);
22287
22288 // A constrained-type-specifier may declare a template-template-parameter.
22289 else if (declares_constrained_template_template_parameter (type))
22290 default_argument
22291 = cp_parser_default_template_template_argument (parser);
22292
22293 /* Outside of a class definition, we can just parse the
22294 assignment-expression. */
22295 else
22296 default_argument
22297 = cp_parser_default_argument (parser, template_parm_p);
22298
22299 if (!parser->default_arg_ok_p)
22300 {
22301 permerror (token->location,
22302 "default arguments are only "
22303 "permitted for function parameters");
22304 }
22305 else if ((declarator && declarator->parameter_pack_p)
22306 || template_parameter_pack_p
22307 || (decl_specifiers.type
22308 && PACK_EXPANSION_P (decl_specifiers.type)))
22309 {
22310 /* Find the name of the parameter pack. */
22311 cp_declarator *id_declarator = declarator;
22312 while (id_declarator && id_declarator->kind != cdk_id)
22313 id_declarator = id_declarator->declarator;
22314
22315 if (id_declarator && id_declarator->kind == cdk_id)
22316 error_at (declarator_token_start->location,
22317 template_parm_p
22318 ? G_("template parameter pack %qD "
22319 "cannot have a default argument")
22320 : G_("parameter pack %qD cannot have "
22321 "a default argument"),
22322 id_declarator->u.id.unqualified_name);
22323 else
22324 error_at (declarator_token_start->location,
22325 template_parm_p
22326 ? G_("template parameter pack cannot have "
22327 "a default argument")
22328 : G_("parameter pack cannot have a "
22329 "default argument"));
22330
22331 default_argument = NULL_TREE;
22332 }
22333 }
22334 else
22335 default_argument = NULL_TREE;
22336
22337 if (default_argument)
22338 STRIP_ANY_LOCATION_WRAPPER (default_argument);
22339
22340 /* Generate a location for the parameter, ranging from the start of the
22341 initial token to the end of the final token (using input_location for
22342 the latter, set up by cp_lexer_set_source_position_from_token when
22343 consuming tokens).
22344
22345 If we have a identifier, then use it for the caret location, e.g.
22346
22347 extern int callee (int one, int (*two)(int, int), float three);
22348 ~~~~~~^~~~~~~~~~~~~~
22349
22350 otherwise, reuse the start location for the caret location e.g.:
22351
22352 extern int callee (int one, int (*)(int, int), float three);
22353 ^~~~~~~~~~~~~~~~~
22354
22355 */
22356 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22357 ? declarator->id_loc
22358 : decl_spec_token_start->location);
22359 location_t param_loc = make_location (caret_loc,
22360 decl_spec_token_start->location,
22361 input_location);
22362
22363 return make_parameter_declarator (&decl_specifiers,
22364 declarator,
22365 default_argument,
22366 param_loc,
22367 template_parameter_pack_p);
22368 }
22369
22370 /* Parse a default argument and return it.
22371
22372 TEMPLATE_PARM_P is true if this is a default argument for a
22373 non-type template parameter. */
22374 static tree
22375 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22376 {
22377 tree default_argument = NULL_TREE;
22378 bool saved_greater_than_is_operator_p;
22379 bool saved_local_variables_forbidden_p;
22380 bool non_constant_p, is_direct_init;
22381
22382 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22383 set correctly. */
22384 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22385 parser->greater_than_is_operator_p = !template_parm_p;
22386 /* Local variable names (and the `this' keyword) may not
22387 appear in a default argument. */
22388 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22389 parser->local_variables_forbidden_p = true;
22390 /* Parse the assignment-expression. */
22391 if (template_parm_p)
22392 push_deferring_access_checks (dk_no_deferred);
22393 tree saved_class_ptr = NULL_TREE;
22394 tree saved_class_ref = NULL_TREE;
22395 /* The "this" pointer is not valid in a default argument. */
22396 if (cfun)
22397 {
22398 saved_class_ptr = current_class_ptr;
22399 cp_function_chain->x_current_class_ptr = NULL_TREE;
22400 saved_class_ref = current_class_ref;
22401 cp_function_chain->x_current_class_ref = NULL_TREE;
22402 }
22403 default_argument
22404 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22405 /* Restore the "this" pointer. */
22406 if (cfun)
22407 {
22408 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22409 cp_function_chain->x_current_class_ref = saved_class_ref;
22410 }
22411 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22412 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22413 if (template_parm_p)
22414 pop_deferring_access_checks ();
22415 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22416 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22417
22418 return default_argument;
22419 }
22420
22421 /* Parse a function-body.
22422
22423 function-body:
22424 compound_statement */
22425
22426 static void
22427 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22428 {
22429 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22430 ? BCS_TRY_BLOCK : BCS_NORMAL),
22431 true);
22432 }
22433
22434 /* Parse a ctor-initializer-opt followed by a function-body. Return
22435 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22436 is true we are parsing a function-try-block. */
22437
22438 static void
22439 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22440 bool in_function_try_block)
22441 {
22442 tree body, list;
22443 const bool check_body_p =
22444 DECL_CONSTRUCTOR_P (current_function_decl)
22445 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22446 tree last = NULL;
22447
22448 /* Begin the function body. */
22449 body = begin_function_body ();
22450 /* Parse the optional ctor-initializer. */
22451 cp_parser_ctor_initializer_opt (parser);
22452
22453 /* If we're parsing a constexpr constructor definition, we need
22454 to check that the constructor body is indeed empty. However,
22455 before we get to cp_parser_function_body lot of junk has been
22456 generated, so we can't just check that we have an empty block.
22457 Rather we take a snapshot of the outermost block, and check whether
22458 cp_parser_function_body changed its state. */
22459 if (check_body_p)
22460 {
22461 list = cur_stmt_list;
22462 if (STATEMENT_LIST_TAIL (list))
22463 last = STATEMENT_LIST_TAIL (list)->stmt;
22464 }
22465 /* Parse the function-body. */
22466 cp_parser_function_body (parser, in_function_try_block);
22467 if (check_body_p)
22468 check_constexpr_ctor_body (last, list, /*complain=*/true);
22469 /* Finish the function body. */
22470 finish_function_body (body);
22471 }
22472
22473 /* Parse an initializer.
22474
22475 initializer:
22476 = initializer-clause
22477 ( expression-list )
22478
22479 Returns an expression representing the initializer. If no
22480 initializer is present, NULL_TREE is returned.
22481
22482 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22483 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22484 set to TRUE if there is no initializer present. If there is an
22485 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22486 is set to true; otherwise it is set to false. */
22487
22488 static tree
22489 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22490 bool* non_constant_p, bool subexpression_p)
22491 {
22492 cp_token *token;
22493 tree init;
22494
22495 /* Peek at the next token. */
22496 token = cp_lexer_peek_token (parser->lexer);
22497
22498 /* Let our caller know whether or not this initializer was
22499 parenthesized. */
22500 *is_direct_init = (token->type != CPP_EQ);
22501 /* Assume that the initializer is constant. */
22502 *non_constant_p = false;
22503
22504 if (token->type == CPP_EQ)
22505 {
22506 /* Consume the `='. */
22507 cp_lexer_consume_token (parser->lexer);
22508 /* Parse the initializer-clause. */
22509 init = cp_parser_initializer_clause (parser, non_constant_p);
22510 }
22511 else if (token->type == CPP_OPEN_PAREN)
22512 {
22513 vec<tree, va_gc> *vec;
22514 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22515 /*cast_p=*/false,
22516 /*allow_expansion_p=*/true,
22517 non_constant_p);
22518 if (vec == NULL)
22519 return error_mark_node;
22520 init = build_tree_list_vec (vec);
22521 release_tree_vector (vec);
22522 }
22523 else if (token->type == CPP_OPEN_BRACE)
22524 {
22525 cp_lexer_set_source_position (parser->lexer);
22526 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22527 init = cp_parser_braced_list (parser, non_constant_p);
22528 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22529 }
22530 else
22531 {
22532 /* Anything else is an error. */
22533 cp_parser_error (parser, "expected initializer");
22534 init = error_mark_node;
22535 }
22536
22537 if (!subexpression_p && check_for_bare_parameter_packs (init))
22538 init = error_mark_node;
22539
22540 return init;
22541 }
22542
22543 /* Parse an initializer-clause.
22544
22545 initializer-clause:
22546 assignment-expression
22547 braced-init-list
22548
22549 Returns an expression representing the initializer.
22550
22551 If the `assignment-expression' production is used the value
22552 returned is simply a representation for the expression.
22553
22554 Otherwise, calls cp_parser_braced_list. */
22555
22556 static cp_expr
22557 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22558 {
22559 cp_expr initializer;
22560
22561 /* Assume the expression is constant. */
22562 *non_constant_p = false;
22563
22564 /* If it is not a `{', then we are looking at an
22565 assignment-expression. */
22566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22567 {
22568 initializer
22569 = cp_parser_constant_expression (parser,
22570 /*allow_non_constant_p=*/true,
22571 non_constant_p);
22572 }
22573 else
22574 initializer = cp_parser_braced_list (parser, non_constant_p);
22575
22576 return initializer;
22577 }
22578
22579 /* Parse a brace-enclosed initializer list.
22580
22581 braced-init-list:
22582 { initializer-list , [opt] }
22583 { designated-initializer-list , [opt] }
22584 { }
22585
22586 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22587 the elements of the initializer-list (or NULL, if the last
22588 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22589 NULL_TREE. There is no way to detect whether or not the optional
22590 trailing `,' was provided. NON_CONSTANT_P is as for
22591 cp_parser_initializer. */
22592
22593 static cp_expr
22594 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22595 {
22596 tree initializer;
22597 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22598
22599 /* Consume the `{' token. */
22600 matching_braces braces;
22601 braces.require_open (parser);
22602 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22603 initializer = make_node (CONSTRUCTOR);
22604 /* If it's not a `}', then there is a non-trivial initializer. */
22605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22606 {
22607 /* Parse the initializer list. */
22608 CONSTRUCTOR_ELTS (initializer)
22609 = cp_parser_initializer_list (parser, non_constant_p);
22610 /* A trailing `,' token is allowed. */
22611 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22612 cp_lexer_consume_token (parser->lexer);
22613 }
22614 else
22615 *non_constant_p = false;
22616 /* Now, there should be a trailing `}'. */
22617 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22618 braces.require_close (parser);
22619 TREE_TYPE (initializer) = init_list_type_node;
22620
22621 cp_expr result (initializer);
22622 /* Build a location of the form:
22623 { ... }
22624 ^~~~~~~
22625 with caret==start at the open brace, finish at the close brace. */
22626 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22627 result.set_location (combined_loc);
22628 return result;
22629 }
22630
22631 /* Consume tokens up to, and including, the next non-nested closing `]'.
22632 Returns true iff we found a closing `]'. */
22633
22634 static bool
22635 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22636 {
22637 unsigned square_depth = 0;
22638
22639 while (true)
22640 {
22641 cp_token * token = cp_lexer_peek_token (parser->lexer);
22642
22643 switch (token->type)
22644 {
22645 case CPP_PRAGMA_EOL:
22646 if (!parser->lexer->in_pragma)
22647 break;
22648 /* FALLTHRU */
22649 case CPP_EOF:
22650 /* If we've run out of tokens, then there is no closing `]'. */
22651 return false;
22652
22653 case CPP_OPEN_SQUARE:
22654 ++square_depth;
22655 break;
22656
22657 case CPP_CLOSE_SQUARE:
22658 if (!square_depth--)
22659 {
22660 cp_lexer_consume_token (parser->lexer);
22661 return true;
22662 }
22663 break;
22664
22665 default:
22666 break;
22667 }
22668
22669 /* Consume the token. */
22670 cp_lexer_consume_token (parser->lexer);
22671 }
22672 }
22673
22674 /* Return true if we are looking at an array-designator, false otherwise. */
22675
22676 static bool
22677 cp_parser_array_designator_p (cp_parser *parser)
22678 {
22679 /* Consume the `['. */
22680 cp_lexer_consume_token (parser->lexer);
22681
22682 cp_lexer_save_tokens (parser->lexer);
22683
22684 /* Skip tokens until the next token is a closing square bracket.
22685 If we find the closing `]', and the next token is a `=', then
22686 we are looking at an array designator. */
22687 bool array_designator_p
22688 = (cp_parser_skip_to_closing_square_bracket (parser)
22689 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22690
22691 /* Roll back the tokens we skipped. */
22692 cp_lexer_rollback_tokens (parser->lexer);
22693
22694 return array_designator_p;
22695 }
22696
22697 /* Parse an initializer-list.
22698
22699 initializer-list:
22700 initializer-clause ... [opt]
22701 initializer-list , initializer-clause ... [opt]
22702
22703 C++2A Extension:
22704
22705 designated-initializer-list:
22706 designated-initializer-clause
22707 designated-initializer-list , designated-initializer-clause
22708
22709 designated-initializer-clause:
22710 designator brace-or-equal-initializer
22711
22712 designator:
22713 . identifier
22714
22715 GNU Extension:
22716
22717 initializer-list:
22718 designation initializer-clause ...[opt]
22719 initializer-list , designation initializer-clause ...[opt]
22720
22721 designation:
22722 . identifier =
22723 identifier :
22724 [ constant-expression ] =
22725
22726 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22727 for the initializer. If the INDEX of the elt is non-NULL, it is the
22728 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22729 as for cp_parser_initializer. */
22730
22731 static vec<constructor_elt, va_gc> *
22732 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22733 {
22734 vec<constructor_elt, va_gc> *v = NULL;
22735 bool first_p = true;
22736 tree first_designator = NULL_TREE;
22737
22738 /* Assume all of the expressions are constant. */
22739 *non_constant_p = false;
22740
22741 /* Parse the rest of the list. */
22742 while (true)
22743 {
22744 cp_token *token;
22745 tree designator;
22746 tree initializer;
22747 bool clause_non_constant_p;
22748 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22749
22750 /* Handle the C++2A syntax, '. id ='. */
22751 if ((cxx_dialect >= cxx2a
22752 || cp_parser_allow_gnu_extensions_p (parser))
22753 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22754 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22755 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22756 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22757 == CPP_OPEN_BRACE)))
22758 {
22759 if (cxx_dialect < cxx2a)
22760 pedwarn (loc, OPT_Wpedantic,
22761 "C++ designated initializers only available with "
22762 "-std=c++2a or -std=gnu++2a");
22763 /* Consume the `.'. */
22764 cp_lexer_consume_token (parser->lexer);
22765 /* Consume the identifier. */
22766 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22767 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22768 /* Consume the `='. */
22769 cp_lexer_consume_token (parser->lexer);
22770 }
22771 /* Also, if the next token is an identifier and the following one is a
22772 colon, we are looking at the GNU designated-initializer
22773 syntax. */
22774 else if (cp_parser_allow_gnu_extensions_p (parser)
22775 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22776 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22777 == CPP_COLON))
22778 {
22779 /* Warn the user that they are using an extension. */
22780 pedwarn (loc, OPT_Wpedantic,
22781 "ISO C++ does not allow GNU designated initializers");
22782 /* Consume the identifier. */
22783 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22784 /* Consume the `:'. */
22785 cp_lexer_consume_token (parser->lexer);
22786 }
22787 /* Also handle C99 array designators, '[ const ] ='. */
22788 else if (cp_parser_allow_gnu_extensions_p (parser)
22789 && !c_dialect_objc ()
22790 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22791 {
22792 /* In C++11, [ could start a lambda-introducer. */
22793 bool non_const = false;
22794
22795 cp_parser_parse_tentatively (parser);
22796
22797 if (!cp_parser_array_designator_p (parser))
22798 {
22799 cp_parser_simulate_error (parser);
22800 designator = NULL_TREE;
22801 }
22802 else
22803 {
22804 designator = cp_parser_constant_expression (parser, true,
22805 &non_const);
22806 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22807 cp_parser_require (parser, CPP_EQ, RT_EQ);
22808 }
22809
22810 if (!cp_parser_parse_definitely (parser))
22811 designator = NULL_TREE;
22812 else if (non_const
22813 && (!require_potential_rvalue_constant_expression
22814 (designator)))
22815 designator = NULL_TREE;
22816 if (designator)
22817 /* Warn the user that they are using an extension. */
22818 pedwarn (loc, OPT_Wpedantic,
22819 "ISO C++ does not allow C99 designated initializers");
22820 }
22821 else
22822 designator = NULL_TREE;
22823
22824 if (first_p)
22825 {
22826 first_designator = designator;
22827 first_p = false;
22828 }
22829 else if (cxx_dialect >= cxx2a
22830 && first_designator != error_mark_node
22831 && (!first_designator != !designator))
22832 {
22833 error_at (loc, "either all initializer clauses should be designated "
22834 "or none of them should be");
22835 first_designator = error_mark_node;
22836 }
22837 else if (cxx_dialect < cxx2a && !first_designator)
22838 first_designator = designator;
22839
22840 /* Parse the initializer. */
22841 initializer = cp_parser_initializer_clause (parser,
22842 &clause_non_constant_p);
22843 /* If any clause is non-constant, so is the entire initializer. */
22844 if (clause_non_constant_p)
22845 *non_constant_p = true;
22846
22847 /* If we have an ellipsis, this is an initializer pack
22848 expansion. */
22849 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22850 {
22851 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22852
22853 /* Consume the `...'. */
22854 cp_lexer_consume_token (parser->lexer);
22855
22856 if (designator && cxx_dialect >= cxx2a)
22857 error_at (loc,
22858 "%<...%> not allowed in designated initializer list");
22859
22860 /* Turn the initializer into an initializer expansion. */
22861 initializer = make_pack_expansion (initializer);
22862 }
22863
22864 /* Add it to the vector. */
22865 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22866
22867 /* If the next token is not a comma, we have reached the end of
22868 the list. */
22869 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22870 break;
22871
22872 /* Peek at the next token. */
22873 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22874 /* If the next token is a `}', then we're still done. An
22875 initializer-clause can have a trailing `,' after the
22876 initializer-list and before the closing `}'. */
22877 if (token->type == CPP_CLOSE_BRACE)
22878 break;
22879
22880 /* Consume the `,' token. */
22881 cp_lexer_consume_token (parser->lexer);
22882 }
22883
22884 /* The same identifier shall not appear in multiple designators
22885 of a designated-initializer-list. */
22886 if (first_designator)
22887 {
22888 unsigned int i;
22889 tree designator, val;
22890 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22891 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22892 {
22893 if (IDENTIFIER_MARKED (designator))
22894 {
22895 error_at (cp_expr_loc_or_loc (val, input_location),
22896 "%<.%s%> designator used multiple times in "
22897 "the same initializer list",
22898 IDENTIFIER_POINTER (designator));
22899 (*v)[i].index = error_mark_node;
22900 }
22901 else
22902 IDENTIFIER_MARKED (designator) = 1;
22903 }
22904 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
22905 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
22906 IDENTIFIER_MARKED (designator) = 0;
22907 }
22908
22909 return v;
22910 }
22911
22912 /* Classes [gram.class] */
22913
22914 /* Parse a class-name.
22915
22916 class-name:
22917 identifier
22918 template-id
22919
22920 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22921 to indicate that names looked up in dependent types should be
22922 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22923 keyword has been used to indicate that the name that appears next
22924 is a template. TAG_TYPE indicates the explicit tag given before
22925 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22926 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22927 is the class being defined in a class-head. If ENUM_OK is TRUE,
22928 enum-names are also accepted.
22929
22930 Returns the TYPE_DECL representing the class. */
22931
22932 static tree
22933 cp_parser_class_name (cp_parser *parser,
22934 bool typename_keyword_p,
22935 bool template_keyword_p,
22936 enum tag_types tag_type,
22937 bool check_dependency_p,
22938 bool class_head_p,
22939 bool is_declaration,
22940 bool enum_ok)
22941 {
22942 tree decl;
22943 tree scope;
22944 bool typename_p;
22945 cp_token *token;
22946 tree identifier = NULL_TREE;
22947
22948 /* All class-names start with an identifier. */
22949 token = cp_lexer_peek_token (parser->lexer);
22950 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22951 {
22952 cp_parser_error (parser, "expected class-name");
22953 return error_mark_node;
22954 }
22955
22956 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22957 to a template-id, so we save it here. */
22958 scope = parser->scope;
22959 if (scope == error_mark_node)
22960 return error_mark_node;
22961
22962 /* Any name names a type if we're following the `typename' keyword
22963 in a qualified name where the enclosing scope is type-dependent. */
22964 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22965 && dependent_type_p (scope));
22966 /* Handle the common case (an identifier, but not a template-id)
22967 efficiently. */
22968 if (token->type == CPP_NAME
22969 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22970 {
22971 cp_token *identifier_token;
22972 bool ambiguous_p;
22973
22974 /* Look for the identifier. */
22975 identifier_token = cp_lexer_peek_token (parser->lexer);
22976 ambiguous_p = identifier_token->error_reported;
22977 identifier = cp_parser_identifier (parser);
22978 /* If the next token isn't an identifier, we are certainly not
22979 looking at a class-name. */
22980 if (identifier == error_mark_node)
22981 decl = error_mark_node;
22982 /* If we know this is a type-name, there's no need to look it
22983 up. */
22984 else if (typename_p)
22985 decl = identifier;
22986 else
22987 {
22988 tree ambiguous_decls;
22989 /* If we already know that this lookup is ambiguous, then
22990 we've already issued an error message; there's no reason
22991 to check again. */
22992 if (ambiguous_p)
22993 {
22994 cp_parser_simulate_error (parser);
22995 return error_mark_node;
22996 }
22997 /* If the next token is a `::', then the name must be a type
22998 name.
22999
23000 [basic.lookup.qual]
23001
23002 During the lookup for a name preceding the :: scope
23003 resolution operator, object, function, and enumerator
23004 names are ignored. */
23005 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23006 tag_type = scope_type;
23007 /* Look up the name. */
23008 decl = cp_parser_lookup_name (parser, identifier,
23009 tag_type,
23010 /*is_template=*/false,
23011 /*is_namespace=*/false,
23012 check_dependency_p,
23013 &ambiguous_decls,
23014 identifier_token->location);
23015 if (ambiguous_decls)
23016 {
23017 if (cp_parser_parsing_tentatively (parser))
23018 cp_parser_simulate_error (parser);
23019 return error_mark_node;
23020 }
23021 }
23022 }
23023 else
23024 {
23025 /* Try a template-id. */
23026 decl = cp_parser_template_id (parser, template_keyword_p,
23027 check_dependency_p,
23028 tag_type,
23029 is_declaration);
23030 if (decl == error_mark_node)
23031 return error_mark_node;
23032 }
23033
23034 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23035
23036 /* If this is a typename, create a TYPENAME_TYPE. */
23037 if (typename_p && decl != error_mark_node)
23038 {
23039 decl = make_typename_type (scope, decl, typename_type,
23040 /*complain=*/tf_error);
23041 if (decl != error_mark_node)
23042 decl = TYPE_NAME (decl);
23043 }
23044
23045 decl = strip_using_decl (decl);
23046
23047 /* Check to see that it is really the name of a class. */
23048 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23049 && identifier_p (TREE_OPERAND (decl, 0))
23050 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23051 /* Situations like this:
23052
23053 template <typename T> struct A {
23054 typename T::template X<int>::I i;
23055 };
23056
23057 are problematic. Is `T::template X<int>' a class-name? The
23058 standard does not seem to be definitive, but there is no other
23059 valid interpretation of the following `::'. Therefore, those
23060 names are considered class-names. */
23061 {
23062 decl = make_typename_type (scope, decl, tag_type, tf_error);
23063 if (decl != error_mark_node)
23064 decl = TYPE_NAME (decl);
23065 }
23066 else if (TREE_CODE (decl) != TYPE_DECL
23067 || TREE_TYPE (decl) == error_mark_node
23068 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23069 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23070 /* In Objective-C 2.0, a classname followed by '.' starts a
23071 dot-syntax expression, and it's not a type-name. */
23072 || (c_dialect_objc ()
23073 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23074 && objc_is_class_name (decl)))
23075 decl = error_mark_node;
23076
23077 if (decl == error_mark_node)
23078 cp_parser_error (parser, "expected class-name");
23079 else if (identifier && !parser->scope)
23080 maybe_note_name_used_in_class (identifier, decl);
23081
23082 return decl;
23083 }
23084
23085 /* Parse a class-specifier.
23086
23087 class-specifier:
23088 class-head { member-specification [opt] }
23089
23090 Returns the TREE_TYPE representing the class. */
23091
23092 static tree
23093 cp_parser_class_specifier_1 (cp_parser* parser)
23094 {
23095 tree type;
23096 tree attributes = NULL_TREE;
23097 bool nested_name_specifier_p;
23098 unsigned saved_num_template_parameter_lists;
23099 bool saved_in_function_body;
23100 unsigned char in_statement;
23101 bool in_switch_statement_p;
23102 bool saved_in_unbraced_linkage_specification_p;
23103 tree old_scope = NULL_TREE;
23104 tree scope = NULL_TREE;
23105 cp_token *closing_brace;
23106
23107 push_deferring_access_checks (dk_no_deferred);
23108
23109 /* Parse the class-head. */
23110 type = cp_parser_class_head (parser,
23111 &nested_name_specifier_p);
23112 /* If the class-head was a semantic disaster, skip the entire body
23113 of the class. */
23114 if (!type)
23115 {
23116 cp_parser_skip_to_end_of_block_or_statement (parser);
23117 pop_deferring_access_checks ();
23118 return error_mark_node;
23119 }
23120
23121 /* Look for the `{'. */
23122 matching_braces braces;
23123 if (!braces.require_open (parser))
23124 {
23125 pop_deferring_access_checks ();
23126 return error_mark_node;
23127 }
23128
23129 cp_ensure_no_omp_declare_simd (parser);
23130 cp_ensure_no_oacc_routine (parser);
23131
23132 /* Issue an error message if type-definitions are forbidden here. */
23133 cp_parser_check_type_definition (parser);
23134 /* Remember that we are defining one more class. */
23135 ++parser->num_classes_being_defined;
23136 /* Inside the class, surrounding template-parameter-lists do not
23137 apply. */
23138 saved_num_template_parameter_lists
23139 = parser->num_template_parameter_lists;
23140 parser->num_template_parameter_lists = 0;
23141 /* We are not in a function body. */
23142 saved_in_function_body = parser->in_function_body;
23143 parser->in_function_body = false;
23144 /* Or in a loop. */
23145 in_statement = parser->in_statement;
23146 parser->in_statement = 0;
23147 /* Or in a switch. */
23148 in_switch_statement_p = parser->in_switch_statement_p;
23149 parser->in_switch_statement_p = false;
23150 /* We are not immediately inside an extern "lang" block. */
23151 saved_in_unbraced_linkage_specification_p
23152 = parser->in_unbraced_linkage_specification_p;
23153 parser->in_unbraced_linkage_specification_p = false;
23154
23155 // Associate constraints with the type.
23156 if (flag_concepts)
23157 type = associate_classtype_constraints (type);
23158
23159 /* Start the class. */
23160 if (nested_name_specifier_p)
23161 {
23162 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23163 old_scope = push_inner_scope (scope);
23164 }
23165 type = begin_class_definition (type);
23166
23167 if (type == error_mark_node)
23168 /* If the type is erroneous, skip the entire body of the class. */
23169 cp_parser_skip_to_closing_brace (parser);
23170 else
23171 /* Parse the member-specification. */
23172 cp_parser_member_specification_opt (parser);
23173
23174 /* Look for the trailing `}'. */
23175 closing_brace = braces.require_close (parser);
23176 /* Look for trailing attributes to apply to this class. */
23177 if (cp_parser_allow_gnu_extensions_p (parser))
23178 attributes = cp_parser_gnu_attributes_opt (parser);
23179 if (type != error_mark_node)
23180 type = finish_struct (type, attributes);
23181 if (nested_name_specifier_p)
23182 pop_inner_scope (old_scope, scope);
23183
23184 /* We've finished a type definition. Check for the common syntax
23185 error of forgetting a semicolon after the definition. We need to
23186 be careful, as we can't just check for not-a-semicolon and be done
23187 with it; the user might have typed:
23188
23189 class X { } c = ...;
23190 class X { } *p = ...;
23191
23192 and so forth. Instead, enumerate all the possible tokens that
23193 might follow this production; if we don't see one of them, then
23194 complain and silently insert the semicolon. */
23195 {
23196 cp_token *token = cp_lexer_peek_token (parser->lexer);
23197 bool want_semicolon = true;
23198
23199 if (cp_next_tokens_can_be_std_attribute_p (parser))
23200 /* Don't try to parse c++11 attributes here. As per the
23201 grammar, that should be a task for
23202 cp_parser_decl_specifier_seq. */
23203 want_semicolon = false;
23204
23205 switch (token->type)
23206 {
23207 case CPP_NAME:
23208 case CPP_SEMICOLON:
23209 case CPP_MULT:
23210 case CPP_AND:
23211 case CPP_OPEN_PAREN:
23212 case CPP_CLOSE_PAREN:
23213 case CPP_COMMA:
23214 want_semicolon = false;
23215 break;
23216
23217 /* While it's legal for type qualifiers and storage class
23218 specifiers to follow type definitions in the grammar, only
23219 compiler testsuites contain code like that. Assume that if
23220 we see such code, then what we're really seeing is a case
23221 like:
23222
23223 class X { }
23224 const <type> var = ...;
23225
23226 or
23227
23228 class Y { }
23229 static <type> func (...) ...
23230
23231 i.e. the qualifier or specifier applies to the next
23232 declaration. To do so, however, we need to look ahead one
23233 more token to see if *that* token is a type specifier.
23234
23235 This code could be improved to handle:
23236
23237 class Z { }
23238 static const <type> var = ...; */
23239 case CPP_KEYWORD:
23240 if (keyword_is_decl_specifier (token->keyword))
23241 {
23242 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23243
23244 /* Handling user-defined types here would be nice, but very
23245 tricky. */
23246 want_semicolon
23247 = (lookahead->type == CPP_KEYWORD
23248 && keyword_begins_type_specifier (lookahead->keyword));
23249 }
23250 break;
23251 default:
23252 break;
23253 }
23254
23255 /* If we don't have a type, then something is very wrong and we
23256 shouldn't try to do anything clever. Likewise for not seeing the
23257 closing brace. */
23258 if (closing_brace && TYPE_P (type) && want_semicolon)
23259 {
23260 /* Locate the closing brace. */
23261 cp_token_position prev
23262 = cp_lexer_previous_token_position (parser->lexer);
23263 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23264 location_t loc = prev_token->location;
23265
23266 /* We want to suggest insertion of a ';' immediately *after* the
23267 closing brace, so, if we can, offset the location by 1 column. */
23268 location_t next_loc = loc;
23269 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23270 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23271
23272 rich_location richloc (line_table, next_loc);
23273
23274 /* If we successfully offset the location, suggest the fix-it. */
23275 if (next_loc != loc)
23276 richloc.add_fixit_insert_before (next_loc, ";");
23277
23278 if (CLASSTYPE_DECLARED_CLASS (type))
23279 error_at (&richloc,
23280 "expected %<;%> after class definition");
23281 else if (TREE_CODE (type) == RECORD_TYPE)
23282 error_at (&richloc,
23283 "expected %<;%> after struct definition");
23284 else if (TREE_CODE (type) == UNION_TYPE)
23285 error_at (&richloc,
23286 "expected %<;%> after union definition");
23287 else
23288 gcc_unreachable ();
23289
23290 /* Unget one token and smash it to look as though we encountered
23291 a semicolon in the input stream. */
23292 cp_lexer_set_token_position (parser->lexer, prev);
23293 token = cp_lexer_peek_token (parser->lexer);
23294 token->type = CPP_SEMICOLON;
23295 token->keyword = RID_MAX;
23296 }
23297 }
23298
23299 /* If this class is not itself within the scope of another class,
23300 then we need to parse the bodies of all of the queued function
23301 definitions. Note that the queued functions defined in a class
23302 are not always processed immediately following the
23303 class-specifier for that class. Consider:
23304
23305 struct A {
23306 struct B { void f() { sizeof (A); } };
23307 };
23308
23309 If `f' were processed before the processing of `A' were
23310 completed, there would be no way to compute the size of `A'.
23311 Note that the nesting we are interested in here is lexical --
23312 not the semantic nesting given by TYPE_CONTEXT. In particular,
23313 for:
23314
23315 struct A { struct B; };
23316 struct A::B { void f() { } };
23317
23318 there is no need to delay the parsing of `A::B::f'. */
23319 if (--parser->num_classes_being_defined == 0)
23320 {
23321 tree decl;
23322 tree class_type = NULL_TREE;
23323 tree pushed_scope = NULL_TREE;
23324 unsigned ix;
23325 cp_default_arg_entry *e;
23326 tree save_ccp, save_ccr;
23327
23328 if (any_erroneous_template_args_p (type))
23329 {
23330 /* Skip default arguments, NSDMIs, etc, in order to improve
23331 error recovery (c++/71169, c++/71832). */
23332 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23333 vec_safe_truncate (unparsed_nsdmis, 0);
23334 vec_safe_truncate (unparsed_classes, 0);
23335 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23336 }
23337
23338 /* In a first pass, parse default arguments to the functions.
23339 Then, in a second pass, parse the bodies of the functions.
23340 This two-phased approach handles cases like:
23341
23342 struct S {
23343 void f() { g(); }
23344 void g(int i = 3);
23345 };
23346
23347 */
23348 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23349 {
23350 decl = e->decl;
23351 /* If there are default arguments that have not yet been processed,
23352 take care of them now. */
23353 if (class_type != e->class_type)
23354 {
23355 if (pushed_scope)
23356 pop_scope (pushed_scope);
23357 class_type = e->class_type;
23358 pushed_scope = push_scope (class_type);
23359 }
23360 /* Make sure that any template parameters are in scope. */
23361 maybe_begin_member_template_processing (decl);
23362 /* Parse the default argument expressions. */
23363 cp_parser_late_parsing_default_args (parser, decl);
23364 /* Remove any template parameters from the symbol table. */
23365 maybe_end_member_template_processing ();
23366 }
23367 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23368 /* Now parse any NSDMIs. */
23369 save_ccp = current_class_ptr;
23370 save_ccr = current_class_ref;
23371 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23372 {
23373 if (class_type != DECL_CONTEXT (decl))
23374 {
23375 if (pushed_scope)
23376 pop_scope (pushed_scope);
23377 class_type = DECL_CONTEXT (decl);
23378 pushed_scope = push_scope (class_type);
23379 }
23380 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23381 cp_parser_late_parsing_nsdmi (parser, decl);
23382 }
23383 vec_safe_truncate (unparsed_nsdmis, 0);
23384 current_class_ptr = save_ccp;
23385 current_class_ref = save_ccr;
23386 if (pushed_scope)
23387 pop_scope (pushed_scope);
23388
23389 /* Now do some post-NSDMI bookkeeping. */
23390 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23391 after_nsdmi_defaulted_late_checks (class_type);
23392 vec_safe_truncate (unparsed_classes, 0);
23393 after_nsdmi_defaulted_late_checks (type);
23394
23395 /* Now parse the body of the functions. */
23396 if (flag_openmp)
23397 {
23398 /* OpenMP UDRs need to be parsed before all other functions. */
23399 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23400 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23401 cp_parser_late_parsing_for_member (parser, decl);
23402 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23403 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23404 cp_parser_late_parsing_for_member (parser, decl);
23405 }
23406 else
23407 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23408 cp_parser_late_parsing_for_member (parser, decl);
23409 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23410 }
23411 else
23412 vec_safe_push (unparsed_classes, type);
23413
23414 /* Put back any saved access checks. */
23415 pop_deferring_access_checks ();
23416
23417 /* Restore saved state. */
23418 parser->in_switch_statement_p = in_switch_statement_p;
23419 parser->in_statement = in_statement;
23420 parser->in_function_body = saved_in_function_body;
23421 parser->num_template_parameter_lists
23422 = saved_num_template_parameter_lists;
23423 parser->in_unbraced_linkage_specification_p
23424 = saved_in_unbraced_linkage_specification_p;
23425
23426 return type;
23427 }
23428
23429 static tree
23430 cp_parser_class_specifier (cp_parser* parser)
23431 {
23432 tree ret;
23433 timevar_push (TV_PARSE_STRUCT);
23434 ret = cp_parser_class_specifier_1 (parser);
23435 timevar_pop (TV_PARSE_STRUCT);
23436 return ret;
23437 }
23438
23439 /* Parse a class-head.
23440
23441 class-head:
23442 class-key identifier [opt] base-clause [opt]
23443 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23444 class-key nested-name-specifier [opt] template-id
23445 base-clause [opt]
23446
23447 class-virt-specifier:
23448 final
23449
23450 GNU Extensions:
23451 class-key attributes identifier [opt] base-clause [opt]
23452 class-key attributes nested-name-specifier identifier base-clause [opt]
23453 class-key attributes nested-name-specifier [opt] template-id
23454 base-clause [opt]
23455
23456 Upon return BASES is initialized to the list of base classes (or
23457 NULL, if there are none) in the same form returned by
23458 cp_parser_base_clause.
23459
23460 Returns the TYPE of the indicated class. Sets
23461 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23462 involving a nested-name-specifier was used, and FALSE otherwise.
23463
23464 Returns error_mark_node if this is not a class-head.
23465
23466 Returns NULL_TREE if the class-head is syntactically valid, but
23467 semantically invalid in a way that means we should skip the entire
23468 body of the class. */
23469
23470 static tree
23471 cp_parser_class_head (cp_parser* parser,
23472 bool* nested_name_specifier_p)
23473 {
23474 tree nested_name_specifier;
23475 enum tag_types class_key;
23476 tree id = NULL_TREE;
23477 tree type = NULL_TREE;
23478 tree attributes;
23479 tree bases;
23480 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23481 bool template_id_p = false;
23482 bool qualified_p = false;
23483 bool invalid_nested_name_p = false;
23484 bool invalid_explicit_specialization_p = false;
23485 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23486 tree pushed_scope = NULL_TREE;
23487 unsigned num_templates;
23488 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23489 /* Assume no nested-name-specifier will be present. */
23490 *nested_name_specifier_p = false;
23491 /* Assume no template parameter lists will be used in defining the
23492 type. */
23493 num_templates = 0;
23494 parser->colon_corrects_to_scope_p = false;
23495
23496 /* Look for the class-key. */
23497 class_key = cp_parser_class_key (parser);
23498 if (class_key == none_type)
23499 return error_mark_node;
23500
23501 location_t class_head_start_location = input_location;
23502
23503 /* Parse the attributes. */
23504 attributes = cp_parser_attributes_opt (parser);
23505
23506 /* If the next token is `::', that is invalid -- but sometimes
23507 people do try to write:
23508
23509 struct ::S {};
23510
23511 Handle this gracefully by accepting the extra qualifier, and then
23512 issuing an error about it later if this really is a
23513 class-head. If it turns out just to be an elaborated type
23514 specifier, remain silent. */
23515 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23516 qualified_p = true;
23517
23518 push_deferring_access_checks (dk_no_check);
23519
23520 /* Determine the name of the class. Begin by looking for an
23521 optional nested-name-specifier. */
23522 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23523 nested_name_specifier
23524 = cp_parser_nested_name_specifier_opt (parser,
23525 /*typename_keyword_p=*/false,
23526 /*check_dependency_p=*/false,
23527 /*type_p=*/true,
23528 /*is_declaration=*/false);
23529 /* If there was a nested-name-specifier, then there *must* be an
23530 identifier. */
23531
23532 cp_token *bad_template_keyword = NULL;
23533
23534 if (nested_name_specifier)
23535 {
23536 type_start_token = cp_lexer_peek_token (parser->lexer);
23537 /* Although the grammar says `identifier', it really means
23538 `class-name' or `template-name'. You are only allowed to
23539 define a class that has already been declared with this
23540 syntax.
23541
23542 The proposed resolution for Core Issue 180 says that wherever
23543 you see `class T::X' you should treat `X' as a type-name.
23544
23545 It is OK to define an inaccessible class; for example:
23546
23547 class A { class B; };
23548 class A::B {};
23549
23550 We do not know if we will see a class-name, or a
23551 template-name. We look for a class-name first, in case the
23552 class-name is a template-id; if we looked for the
23553 template-name first we would stop after the template-name. */
23554 cp_parser_parse_tentatively (parser);
23555 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23556 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23557 type = cp_parser_class_name (parser,
23558 /*typename_keyword_p=*/false,
23559 /*template_keyword_p=*/false,
23560 class_type,
23561 /*check_dependency_p=*/false,
23562 /*class_head_p=*/true,
23563 /*is_declaration=*/false);
23564 /* If that didn't work, ignore the nested-name-specifier. */
23565 if (!cp_parser_parse_definitely (parser))
23566 {
23567 invalid_nested_name_p = true;
23568 type_start_token = cp_lexer_peek_token (parser->lexer);
23569 id = cp_parser_identifier (parser);
23570 if (id == error_mark_node)
23571 id = NULL_TREE;
23572 }
23573 /* If we could not find a corresponding TYPE, treat this
23574 declaration like an unqualified declaration. */
23575 if (type == error_mark_node)
23576 nested_name_specifier = NULL_TREE;
23577 /* Otherwise, count the number of templates used in TYPE and its
23578 containing scopes. */
23579 else
23580 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23581 }
23582 /* Otherwise, the identifier is optional. */
23583 else
23584 {
23585 /* We don't know whether what comes next is a template-id,
23586 an identifier, or nothing at all. */
23587 cp_parser_parse_tentatively (parser);
23588 /* Check for a template-id. */
23589 type_start_token = cp_lexer_peek_token (parser->lexer);
23590 id = cp_parser_template_id (parser,
23591 /*template_keyword_p=*/false,
23592 /*check_dependency_p=*/true,
23593 class_key,
23594 /*is_declaration=*/true);
23595 /* If that didn't work, it could still be an identifier. */
23596 if (!cp_parser_parse_definitely (parser))
23597 {
23598 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23599 {
23600 type_start_token = cp_lexer_peek_token (parser->lexer);
23601 id = cp_parser_identifier (parser);
23602 }
23603 else
23604 id = NULL_TREE;
23605 }
23606 else
23607 {
23608 template_id_p = true;
23609 ++num_templates;
23610 }
23611 }
23612
23613 pop_deferring_access_checks ();
23614
23615 if (id)
23616 {
23617 cp_parser_check_for_invalid_template_id (parser, id,
23618 class_key,
23619 type_start_token->location);
23620 }
23621 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23622
23623 /* If it's not a `:' or a `{' then we can't really be looking at a
23624 class-head, since a class-head only appears as part of a
23625 class-specifier. We have to detect this situation before calling
23626 xref_tag, since that has irreversible side-effects. */
23627 if (!cp_parser_next_token_starts_class_definition_p (parser))
23628 {
23629 cp_parser_error (parser, "expected %<{%> or %<:%>");
23630 type = error_mark_node;
23631 goto out;
23632 }
23633
23634 /* At this point, we're going ahead with the class-specifier, even
23635 if some other problem occurs. */
23636 cp_parser_commit_to_tentative_parse (parser);
23637 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23638 {
23639 cp_parser_error (parser,
23640 "cannot specify %<override%> for a class");
23641 type = error_mark_node;
23642 goto out;
23643 }
23644 /* Issue the error about the overly-qualified name now. */
23645 if (qualified_p)
23646 {
23647 cp_parser_error (parser,
23648 "global qualification of class name is invalid");
23649 type = error_mark_node;
23650 goto out;
23651 }
23652 else if (invalid_nested_name_p)
23653 {
23654 cp_parser_error (parser,
23655 "qualified name does not name a class");
23656 type = error_mark_node;
23657 goto out;
23658 }
23659 else if (nested_name_specifier)
23660 {
23661 tree scope;
23662
23663 if (bad_template_keyword)
23664 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23665 keyword template shall not appear at the top level. */
23666 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23667 "keyword %<template%> not allowed in class-head-name");
23668
23669 /* Reject typedef-names in class heads. */
23670 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23671 {
23672 error_at (type_start_token->location,
23673 "invalid class name in declaration of %qD",
23674 type);
23675 type = NULL_TREE;
23676 goto done;
23677 }
23678
23679 /* Figure out in what scope the declaration is being placed. */
23680 scope = current_scope ();
23681 /* If that scope does not contain the scope in which the
23682 class was originally declared, the program is invalid. */
23683 if (scope && !is_ancestor (scope, nested_name_specifier))
23684 {
23685 if (at_namespace_scope_p ())
23686 error_at (type_start_token->location,
23687 "declaration of %qD in namespace %qD which does not "
23688 "enclose %qD",
23689 type, scope, nested_name_specifier);
23690 else
23691 error_at (type_start_token->location,
23692 "declaration of %qD in %qD which does not enclose %qD",
23693 type, scope, nested_name_specifier);
23694 type = NULL_TREE;
23695 goto done;
23696 }
23697 /* [dcl.meaning]
23698
23699 A declarator-id shall not be qualified except for the
23700 definition of a ... nested class outside of its class
23701 ... [or] the definition or explicit instantiation of a
23702 class member of a namespace outside of its namespace. */
23703 if (scope == nested_name_specifier)
23704 {
23705 permerror (nested_name_specifier_token_start->location,
23706 "extra qualification not allowed");
23707 nested_name_specifier = NULL_TREE;
23708 num_templates = 0;
23709 }
23710 }
23711 /* An explicit-specialization must be preceded by "template <>". If
23712 it is not, try to recover gracefully. */
23713 if (at_namespace_scope_p ()
23714 && parser->num_template_parameter_lists == 0
23715 && !processing_template_parmlist
23716 && template_id_p)
23717 {
23718 /* Build a location of this form:
23719 struct typename <ARGS>
23720 ^~~~~~~~~~~~~~~~~~~~~~
23721 with caret==start at the start token, and
23722 finishing at the end of the type. */
23723 location_t reported_loc
23724 = make_location (class_head_start_location,
23725 class_head_start_location,
23726 get_finish (type_start_token->location));
23727 rich_location richloc (line_table, reported_loc);
23728 richloc.add_fixit_insert_before (class_head_start_location,
23729 "template <> ");
23730 error_at (&richloc,
23731 "an explicit specialization must be preceded by"
23732 " %<template <>%>");
23733 invalid_explicit_specialization_p = true;
23734 /* Take the same action that would have been taken by
23735 cp_parser_explicit_specialization. */
23736 ++parser->num_template_parameter_lists;
23737 begin_specialization ();
23738 }
23739 /* There must be no "return" statements between this point and the
23740 end of this function; set "type "to the correct return value and
23741 use "goto done;" to return. */
23742 /* Make sure that the right number of template parameters were
23743 present. */
23744 if (!cp_parser_check_template_parameters (parser, num_templates,
23745 template_id_p,
23746 type_start_token->location,
23747 /*declarator=*/NULL))
23748 {
23749 /* If something went wrong, there is no point in even trying to
23750 process the class-definition. */
23751 type = NULL_TREE;
23752 goto done;
23753 }
23754
23755 /* Look up the type. */
23756 if (template_id_p)
23757 {
23758 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23759 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23760 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23761 {
23762 error_at (type_start_token->location,
23763 "function template %qD redeclared as a class template", id);
23764 type = error_mark_node;
23765 }
23766 else
23767 {
23768 type = TREE_TYPE (id);
23769 type = maybe_process_partial_specialization (type);
23770
23771 /* Check the scope while we still know whether or not we had a
23772 nested-name-specifier. */
23773 if (type != error_mark_node)
23774 check_unqualified_spec_or_inst (type, type_start_token->location);
23775 }
23776 if (nested_name_specifier)
23777 pushed_scope = push_scope (nested_name_specifier);
23778 }
23779 else if (nested_name_specifier)
23780 {
23781 tree class_type;
23782
23783 /* Given:
23784
23785 template <typename T> struct S { struct T };
23786 template <typename T> struct S<T>::T { };
23787
23788 we will get a TYPENAME_TYPE when processing the definition of
23789 `S::T'. We need to resolve it to the actual type before we
23790 try to define it. */
23791 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23792 {
23793 class_type = resolve_typename_type (TREE_TYPE (type),
23794 /*only_current_p=*/false);
23795 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23796 type = TYPE_NAME (class_type);
23797 else
23798 {
23799 cp_parser_error (parser, "could not resolve typename type");
23800 type = error_mark_node;
23801 }
23802 }
23803
23804 if (maybe_process_partial_specialization (TREE_TYPE (type))
23805 == error_mark_node)
23806 {
23807 type = NULL_TREE;
23808 goto done;
23809 }
23810
23811 class_type = current_class_type;
23812 /* Enter the scope indicated by the nested-name-specifier. */
23813 pushed_scope = push_scope (nested_name_specifier);
23814 /* Get the canonical version of this type. */
23815 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23816 /* Call push_template_decl if it seems like we should be defining a
23817 template either from the template headers or the type we're
23818 defining, so that we diagnose both extra and missing headers. */
23819 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23820 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23821 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23822 {
23823 type = push_template_decl (type);
23824 if (type == error_mark_node)
23825 {
23826 type = NULL_TREE;
23827 goto done;
23828 }
23829 }
23830
23831 type = TREE_TYPE (type);
23832 *nested_name_specifier_p = true;
23833 }
23834 else /* The name is not a nested name. */
23835 {
23836 /* If the class was unnamed, create a dummy name. */
23837 if (!id)
23838 id = make_anon_name ();
23839 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23840 ? ts_within_enclosing_non_class
23841 : ts_current);
23842 type = xref_tag (class_key, id, tag_scope,
23843 parser->num_template_parameter_lists);
23844 }
23845
23846 /* Indicate whether this class was declared as a `class' or as a
23847 `struct'. */
23848 if (TREE_CODE (type) == RECORD_TYPE)
23849 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23850 cp_parser_check_class_key (class_key, type);
23851
23852 /* If this type was already complete, and we see another definition,
23853 that's an error. */
23854 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23855 {
23856 error_at (type_start_token->location, "redefinition of %q#T",
23857 type);
23858 inform (location_of (type), "previous definition of %q#T",
23859 type);
23860 type = NULL_TREE;
23861 goto done;
23862 }
23863 else if (type == error_mark_node)
23864 type = NULL_TREE;
23865
23866 if (type)
23867 {
23868 /* Apply attributes now, before any use of the class as a template
23869 argument in its base list. */
23870 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23871 fixup_attribute_variants (type);
23872 }
23873
23874 /* We will have entered the scope containing the class; the names of
23875 base classes should be looked up in that context. For example:
23876
23877 struct A { struct B {}; struct C; };
23878 struct A::C : B {};
23879
23880 is valid. */
23881
23882 /* Get the list of base-classes, if there is one. */
23883 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23884 {
23885 /* PR59482: enter the class scope so that base-specifiers are looked
23886 up correctly. */
23887 if (type)
23888 pushclass (type);
23889 bases = cp_parser_base_clause (parser);
23890 /* PR59482: get out of the previously pushed class scope so that the
23891 subsequent pops pop the right thing. */
23892 if (type)
23893 popclass ();
23894 }
23895 else
23896 bases = NULL_TREE;
23897
23898 /* If we're really defining a class, process the base classes.
23899 If they're invalid, fail. */
23900 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23901 xref_basetypes (type, bases);
23902
23903 done:
23904 /* Leave the scope given by the nested-name-specifier. We will
23905 enter the class scope itself while processing the members. */
23906 if (pushed_scope)
23907 pop_scope (pushed_scope);
23908
23909 if (invalid_explicit_specialization_p)
23910 {
23911 end_specialization ();
23912 --parser->num_template_parameter_lists;
23913 }
23914
23915 if (type)
23916 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23917 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23918 CLASSTYPE_FINAL (type) = 1;
23919 out:
23920 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23921 return type;
23922 }
23923
23924 /* Parse a class-key.
23925
23926 class-key:
23927 class
23928 struct
23929 union
23930
23931 Returns the kind of class-key specified, or none_type to indicate
23932 error. */
23933
23934 static enum tag_types
23935 cp_parser_class_key (cp_parser* parser)
23936 {
23937 cp_token *token;
23938 enum tag_types tag_type;
23939
23940 /* Look for the class-key. */
23941 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23942 if (!token)
23943 return none_type;
23944
23945 /* Check to see if the TOKEN is a class-key. */
23946 tag_type = cp_parser_token_is_class_key (token);
23947 if (!tag_type)
23948 cp_parser_error (parser, "expected class-key");
23949 return tag_type;
23950 }
23951
23952 /* Parse a type-parameter-key.
23953
23954 type-parameter-key:
23955 class
23956 typename
23957 */
23958
23959 static void
23960 cp_parser_type_parameter_key (cp_parser* parser)
23961 {
23962 /* Look for the type-parameter-key. */
23963 enum tag_types tag_type = none_type;
23964 cp_token *token = cp_lexer_peek_token (parser->lexer);
23965 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23966 {
23967 cp_lexer_consume_token (parser->lexer);
23968 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23969 /* typename is not allowed in a template template parameter
23970 by the standard until C++17. */
23971 pedwarn (token->location, OPT_Wpedantic,
23972 "ISO C++ forbids typename key in template template parameter;"
23973 " use -std=c++17 or -std=gnu++17");
23974 }
23975 else
23976 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23977
23978 return;
23979 }
23980
23981 /* Parse an (optional) member-specification.
23982
23983 member-specification:
23984 member-declaration member-specification [opt]
23985 access-specifier : member-specification [opt] */
23986
23987 static void
23988 cp_parser_member_specification_opt (cp_parser* parser)
23989 {
23990 while (true)
23991 {
23992 cp_token *token;
23993 enum rid keyword;
23994
23995 /* Peek at the next token. */
23996 token = cp_lexer_peek_token (parser->lexer);
23997 /* If it's a `}', or EOF then we've seen all the members. */
23998 if (token->type == CPP_CLOSE_BRACE
23999 || token->type == CPP_EOF
24000 || token->type == CPP_PRAGMA_EOL)
24001 break;
24002
24003 /* See if this token is a keyword. */
24004 keyword = token->keyword;
24005 switch (keyword)
24006 {
24007 case RID_PUBLIC:
24008 case RID_PROTECTED:
24009 case RID_PRIVATE:
24010 /* Consume the access-specifier. */
24011 cp_lexer_consume_token (parser->lexer);
24012 /* Remember which access-specifier is active. */
24013 current_access_specifier = token->u.value;
24014 /* Look for the `:'. */
24015 cp_parser_require (parser, CPP_COLON, RT_COLON);
24016 break;
24017
24018 default:
24019 /* Accept #pragmas at class scope. */
24020 if (token->type == CPP_PRAGMA)
24021 {
24022 cp_parser_pragma (parser, pragma_member, NULL);
24023 break;
24024 }
24025
24026 /* Otherwise, the next construction must be a
24027 member-declaration. */
24028 cp_parser_member_declaration (parser);
24029 }
24030 }
24031 }
24032
24033 /* Parse a member-declaration.
24034
24035 member-declaration:
24036 decl-specifier-seq [opt] member-declarator-list [opt] ;
24037 function-definition ; [opt]
24038 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24039 using-declaration
24040 template-declaration
24041 alias-declaration
24042
24043 member-declarator-list:
24044 member-declarator
24045 member-declarator-list , member-declarator
24046
24047 member-declarator:
24048 declarator pure-specifier [opt]
24049 declarator constant-initializer [opt]
24050 identifier [opt] : constant-expression
24051
24052 GNU Extensions:
24053
24054 member-declaration:
24055 __extension__ member-declaration
24056
24057 member-declarator:
24058 declarator attributes [opt] pure-specifier [opt]
24059 declarator attributes [opt] constant-initializer [opt]
24060 identifier [opt] attributes [opt] : constant-expression
24061
24062 C++0x Extensions:
24063
24064 member-declaration:
24065 static_assert-declaration */
24066
24067 static void
24068 cp_parser_member_declaration (cp_parser* parser)
24069 {
24070 cp_decl_specifier_seq decl_specifiers;
24071 tree prefix_attributes;
24072 tree decl;
24073 int declares_class_or_enum;
24074 bool friend_p;
24075 cp_token *token = NULL;
24076 cp_token *decl_spec_token_start = NULL;
24077 cp_token *initializer_token_start = NULL;
24078 int saved_pedantic;
24079 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24080
24081 /* Check for the `__extension__' keyword. */
24082 if (cp_parser_extension_opt (parser, &saved_pedantic))
24083 {
24084 /* Recurse. */
24085 cp_parser_member_declaration (parser);
24086 /* Restore the old value of the PEDANTIC flag. */
24087 pedantic = saved_pedantic;
24088
24089 return;
24090 }
24091
24092 /* Check for a template-declaration. */
24093 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24094 {
24095 /* An explicit specialization here is an error condition, and we
24096 expect the specialization handler to detect and report this. */
24097 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24098 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24099 cp_parser_explicit_specialization (parser);
24100 else
24101 cp_parser_template_declaration (parser, /*member_p=*/true);
24102
24103 return;
24104 }
24105 /* Check for a template introduction. */
24106 else if (cp_parser_template_declaration_after_export (parser, true))
24107 return;
24108
24109 /* Check for a using-declaration. */
24110 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24111 {
24112 if (cxx_dialect < cxx11)
24113 {
24114 /* Parse the using-declaration. */
24115 cp_parser_using_declaration (parser,
24116 /*access_declaration_p=*/false);
24117 return;
24118 }
24119 else
24120 {
24121 tree decl;
24122 bool alias_decl_expected;
24123 cp_parser_parse_tentatively (parser);
24124 decl = cp_parser_alias_declaration (parser);
24125 /* Note that if we actually see the '=' token after the
24126 identifier, cp_parser_alias_declaration commits the
24127 tentative parse. In that case, we really expect an
24128 alias-declaration. Otherwise, we expect a using
24129 declaration. */
24130 alias_decl_expected =
24131 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24132 cp_parser_parse_definitely (parser);
24133
24134 if (alias_decl_expected)
24135 finish_member_declaration (decl);
24136 else
24137 cp_parser_using_declaration (parser,
24138 /*access_declaration_p=*/false);
24139 return;
24140 }
24141 }
24142
24143 /* Check for @defs. */
24144 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24145 {
24146 tree ivar, member;
24147 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24148 ivar = ivar_chains;
24149 while (ivar)
24150 {
24151 member = ivar;
24152 ivar = TREE_CHAIN (member);
24153 TREE_CHAIN (member) = NULL_TREE;
24154 finish_member_declaration (member);
24155 }
24156 return;
24157 }
24158
24159 /* If the next token is `static_assert' we have a static assertion. */
24160 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24161 {
24162 cp_parser_static_assert (parser, /*member_p=*/true);
24163 return;
24164 }
24165
24166 parser->colon_corrects_to_scope_p = false;
24167
24168 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24169 goto out;
24170
24171 /* Parse the decl-specifier-seq. */
24172 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24173 cp_parser_decl_specifier_seq (parser,
24174 (CP_PARSER_FLAGS_OPTIONAL
24175 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24176 &decl_specifiers,
24177 &declares_class_or_enum);
24178 /* Check for an invalid type-name. */
24179 if (!decl_specifiers.any_type_specifiers_p
24180 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24181 goto out;
24182 /* If there is no declarator, then the decl-specifier-seq should
24183 specify a type. */
24184 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24185 {
24186 /* If there was no decl-specifier-seq, and the next token is a
24187 `;', then we have something like:
24188
24189 struct S { ; };
24190
24191 [class.mem]
24192
24193 Each member-declaration shall declare at least one member
24194 name of the class. */
24195 if (!decl_specifiers.any_specifiers_p)
24196 {
24197 cp_token *token = cp_lexer_peek_token (parser->lexer);
24198 if (!in_system_header_at (token->location))
24199 {
24200 gcc_rich_location richloc (token->location);
24201 richloc.add_fixit_remove ();
24202 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24203 }
24204 }
24205 else
24206 {
24207 tree type;
24208
24209 /* See if this declaration is a friend. */
24210 friend_p = cp_parser_friend_p (&decl_specifiers);
24211 /* If there were decl-specifiers, check to see if there was
24212 a class-declaration. */
24213 type = check_tag_decl (&decl_specifiers,
24214 /*explicit_type_instantiation_p=*/false);
24215 /* Nested classes have already been added to the class, but
24216 a `friend' needs to be explicitly registered. */
24217 if (friend_p)
24218 {
24219 /* If the `friend' keyword was present, the friend must
24220 be introduced with a class-key. */
24221 if (!declares_class_or_enum && cxx_dialect < cxx11)
24222 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24223 "in C++03 a class-key must be used "
24224 "when declaring a friend");
24225 /* In this case:
24226
24227 template <typename T> struct A {
24228 friend struct A<T>::B;
24229 };
24230
24231 A<T>::B will be represented by a TYPENAME_TYPE, and
24232 therefore not recognized by check_tag_decl. */
24233 if (!type)
24234 {
24235 type = decl_specifiers.type;
24236 if (type && TREE_CODE (type) == TYPE_DECL)
24237 type = TREE_TYPE (type);
24238 }
24239 if (!type || !TYPE_P (type))
24240 error_at (decl_spec_token_start->location,
24241 "friend declaration does not name a class or "
24242 "function");
24243 else
24244 make_friend_class (current_class_type, type,
24245 /*complain=*/true);
24246 }
24247 /* If there is no TYPE, an error message will already have
24248 been issued. */
24249 else if (!type || type == error_mark_node)
24250 ;
24251 /* An anonymous aggregate has to be handled specially; such
24252 a declaration really declares a data member (with a
24253 particular type), as opposed to a nested class. */
24254 else if (ANON_AGGR_TYPE_P (type))
24255 {
24256 /* C++11 9.5/6. */
24257 if (decl_specifiers.storage_class != sc_none)
24258 error_at (decl_spec_token_start->location,
24259 "a storage class on an anonymous aggregate "
24260 "in class scope is not allowed");
24261
24262 /* Remove constructors and such from TYPE, now that we
24263 know it is an anonymous aggregate. */
24264 fixup_anonymous_aggr (type);
24265 /* And make the corresponding data member. */
24266 decl = build_decl (decl_spec_token_start->location,
24267 FIELD_DECL, NULL_TREE, type);
24268 /* Add it to the class. */
24269 finish_member_declaration (decl);
24270 }
24271 else
24272 cp_parser_check_access_in_redeclaration
24273 (TYPE_NAME (type),
24274 decl_spec_token_start->location);
24275 }
24276 }
24277 else
24278 {
24279 bool assume_semicolon = false;
24280
24281 /* Clear attributes from the decl_specifiers but keep them
24282 around as prefix attributes that apply them to the entity
24283 being declared. */
24284 prefix_attributes = decl_specifiers.attributes;
24285 decl_specifiers.attributes = NULL_TREE;
24286
24287 /* See if these declarations will be friends. */
24288 friend_p = cp_parser_friend_p (&decl_specifiers);
24289
24290 /* Keep going until we hit the `;' at the end of the
24291 declaration. */
24292 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24293 {
24294 tree attributes = NULL_TREE;
24295 tree first_attribute;
24296 tree initializer;
24297 bool named_bitfld = false;
24298
24299 /* Peek at the next token. */
24300 token = cp_lexer_peek_token (parser->lexer);
24301
24302 /* The following code wants to know early if it is a bit-field
24303 or some other declaration. Attributes can appear before
24304 the `:' token. Skip over them without consuming any tokens
24305 to peek if they are followed by `:'. */
24306 if (cp_next_tokens_can_be_attribute_p (parser)
24307 || (token->type == CPP_NAME
24308 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24309 && (named_bitfld = true)))
24310 {
24311 size_t n
24312 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24313 token = cp_lexer_peek_nth_token (parser->lexer, n);
24314 }
24315
24316 /* Check for a bitfield declaration. */
24317 if (token->type == CPP_COLON
24318 || (token->type == CPP_NAME
24319 && token == cp_lexer_peek_token (parser->lexer)
24320 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24321 && (named_bitfld = true)))
24322 {
24323 tree identifier;
24324 tree width;
24325 tree late_attributes = NULL_TREE;
24326 location_t id_location
24327 = cp_lexer_peek_token (parser->lexer)->location;
24328
24329 if (named_bitfld)
24330 identifier = cp_parser_identifier (parser);
24331 else
24332 identifier = NULL_TREE;
24333
24334 /* Look for attributes that apply to the bitfield. */
24335 attributes = cp_parser_attributes_opt (parser);
24336
24337 /* Consume the `:' token. */
24338 cp_lexer_consume_token (parser->lexer);
24339
24340 /* Get the width of the bitfield. */
24341 width = cp_parser_constant_expression (parser, false, NULL,
24342 cxx_dialect >= cxx11);
24343
24344 /* In C++2A and as extension for C++11 and above we allow
24345 default member initializers for bit-fields. */
24346 initializer = NULL_TREE;
24347 if (cxx_dialect >= cxx11
24348 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24349 || cp_lexer_next_token_is (parser->lexer,
24350 CPP_OPEN_BRACE)))
24351 {
24352 location_t loc
24353 = cp_lexer_peek_token (parser->lexer)->location;
24354 if (cxx_dialect < cxx2a
24355 && !in_system_header_at (loc)
24356 && identifier != NULL_TREE)
24357 pedwarn (loc, 0,
24358 "default member initializers for bit-fields "
24359 "only available with -std=c++2a or "
24360 "-std=gnu++2a");
24361
24362 initializer = cp_parser_save_nsdmi (parser);
24363 if (identifier == NULL_TREE)
24364 {
24365 error_at (loc, "default member initializer for "
24366 "unnamed bit-field");
24367 initializer = NULL_TREE;
24368 }
24369 }
24370 else
24371 {
24372 /* Look for attributes that apply to the bitfield after
24373 the `:' token and width. This is where GCC used to
24374 parse attributes in the past, pedwarn if there is
24375 a std attribute. */
24376 if (cp_next_tokens_can_be_std_attribute_p (parser))
24377 pedwarn (input_location, OPT_Wpedantic,
24378 "ISO C++ allows bit-field attributes only "
24379 "before the %<:%> token");
24380
24381 late_attributes = cp_parser_attributes_opt (parser);
24382 }
24383
24384 attributes = attr_chainon (attributes, late_attributes);
24385
24386 /* Remember which attributes are prefix attributes and
24387 which are not. */
24388 first_attribute = attributes;
24389 /* Combine the attributes. */
24390 attributes = attr_chainon (prefix_attributes, attributes);
24391
24392 /* Create the bitfield declaration. */
24393 decl = grokbitfield (identifier
24394 ? make_id_declarator (NULL_TREE,
24395 identifier,
24396 sfk_none,
24397 id_location)
24398 : NULL,
24399 &decl_specifiers,
24400 width, initializer,
24401 attributes);
24402 }
24403 else
24404 {
24405 cp_declarator *declarator;
24406 tree asm_specification;
24407 int ctor_dtor_or_conv_p;
24408
24409 /* Parse the declarator. */
24410 declarator
24411 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24412 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24413 &ctor_dtor_or_conv_p,
24414 /*parenthesized_p=*/NULL,
24415 /*member_p=*/true,
24416 friend_p);
24417
24418 /* If something went wrong parsing the declarator, make sure
24419 that we at least consume some tokens. */
24420 if (declarator == cp_error_declarator)
24421 {
24422 /* Skip to the end of the statement. */
24423 cp_parser_skip_to_end_of_statement (parser);
24424 /* If the next token is not a semicolon, that is
24425 probably because we just skipped over the body of
24426 a function. So, we consume a semicolon if
24427 present, but do not issue an error message if it
24428 is not present. */
24429 if (cp_lexer_next_token_is (parser->lexer,
24430 CPP_SEMICOLON))
24431 cp_lexer_consume_token (parser->lexer);
24432 goto out;
24433 }
24434
24435 if (declares_class_or_enum & 2)
24436 cp_parser_check_for_definition_in_return_type
24437 (declarator, decl_specifiers.type,
24438 decl_specifiers.locations[ds_type_spec]);
24439
24440 /* Look for an asm-specification. */
24441 asm_specification = cp_parser_asm_specification_opt (parser);
24442 /* Look for attributes that apply to the declaration. */
24443 attributes = cp_parser_attributes_opt (parser);
24444 /* Remember which attributes are prefix attributes and
24445 which are not. */
24446 first_attribute = attributes;
24447 /* Combine the attributes. */
24448 attributes = attr_chainon (prefix_attributes, attributes);
24449
24450 /* If it's an `=', then we have a constant-initializer or a
24451 pure-specifier. It is not correct to parse the
24452 initializer before registering the member declaration
24453 since the member declaration should be in scope while
24454 its initializer is processed. However, the rest of the
24455 front end does not yet provide an interface that allows
24456 us to handle this correctly. */
24457 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24458 {
24459 /* In [class.mem]:
24460
24461 A pure-specifier shall be used only in the declaration of
24462 a virtual function.
24463
24464 A member-declarator can contain a constant-initializer
24465 only if it declares a static member of integral or
24466 enumeration type.
24467
24468 Therefore, if the DECLARATOR is for a function, we look
24469 for a pure-specifier; otherwise, we look for a
24470 constant-initializer. When we call `grokfield', it will
24471 perform more stringent semantics checks. */
24472 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24473 if (function_declarator_p (declarator)
24474 || (decl_specifiers.type
24475 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24476 && declarator->kind == cdk_id
24477 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24478 == FUNCTION_TYPE)))
24479 initializer = cp_parser_pure_specifier (parser);
24480 else if (decl_specifiers.storage_class != sc_static)
24481 initializer = cp_parser_save_nsdmi (parser);
24482 else if (cxx_dialect >= cxx11)
24483 {
24484 bool nonconst;
24485 /* Don't require a constant rvalue in C++11, since we
24486 might want a reference constant. We'll enforce
24487 constancy later. */
24488 cp_lexer_consume_token (parser->lexer);
24489 /* Parse the initializer. */
24490 initializer = cp_parser_initializer_clause (parser,
24491 &nonconst);
24492 }
24493 else
24494 /* Parse the initializer. */
24495 initializer = cp_parser_constant_initializer (parser);
24496 }
24497 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24498 && !function_declarator_p (declarator))
24499 {
24500 bool x;
24501 if (decl_specifiers.storage_class != sc_static)
24502 initializer = cp_parser_save_nsdmi (parser);
24503 else
24504 initializer = cp_parser_initializer (parser, &x, &x);
24505 }
24506 /* Otherwise, there is no initializer. */
24507 else
24508 initializer = NULL_TREE;
24509
24510 /* See if we are probably looking at a function
24511 definition. We are certainly not looking at a
24512 member-declarator. Calling `grokfield' has
24513 side-effects, so we must not do it unless we are sure
24514 that we are looking at a member-declarator. */
24515 if (cp_parser_token_starts_function_definition_p
24516 (cp_lexer_peek_token (parser->lexer)))
24517 {
24518 /* The grammar does not allow a pure-specifier to be
24519 used when a member function is defined. (It is
24520 possible that this fact is an oversight in the
24521 standard, since a pure function may be defined
24522 outside of the class-specifier. */
24523 if (initializer && initializer_token_start)
24524 error_at (initializer_token_start->location,
24525 "pure-specifier on function-definition");
24526 decl = cp_parser_save_member_function_body (parser,
24527 &decl_specifiers,
24528 declarator,
24529 attributes);
24530 if (parser->fully_implicit_function_template_p)
24531 decl = finish_fully_implicit_template (parser, decl);
24532 /* If the member was not a friend, declare it here. */
24533 if (!friend_p)
24534 finish_member_declaration (decl);
24535 /* Peek at the next token. */
24536 token = cp_lexer_peek_token (parser->lexer);
24537 /* If the next token is a semicolon, consume it. */
24538 if (token->type == CPP_SEMICOLON)
24539 {
24540 location_t semicolon_loc
24541 = cp_lexer_consume_token (parser->lexer)->location;
24542 gcc_rich_location richloc (semicolon_loc);
24543 richloc.add_fixit_remove ();
24544 warning_at (&richloc, OPT_Wextra_semi,
24545 "extra %<;%> after in-class "
24546 "function definition");
24547 }
24548 goto out;
24549 }
24550 else
24551 if (declarator->kind == cdk_function)
24552 declarator->id_loc = token->location;
24553 /* Create the declaration. */
24554 decl = grokfield (declarator, &decl_specifiers,
24555 initializer, /*init_const_expr_p=*/true,
24556 asm_specification, attributes);
24557 if (parser->fully_implicit_function_template_p)
24558 {
24559 if (friend_p)
24560 finish_fully_implicit_template (parser, 0);
24561 else
24562 decl = finish_fully_implicit_template (parser, decl);
24563 }
24564 }
24565
24566 cp_finalize_omp_declare_simd (parser, decl);
24567 cp_finalize_oacc_routine (parser, decl, false);
24568
24569 /* Reset PREFIX_ATTRIBUTES. */
24570 if (attributes != error_mark_node)
24571 {
24572 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24573 attributes = TREE_CHAIN (attributes);
24574 if (attributes)
24575 TREE_CHAIN (attributes) = NULL_TREE;
24576 }
24577
24578 /* If there is any qualification still in effect, clear it
24579 now; we will be starting fresh with the next declarator. */
24580 parser->scope = NULL_TREE;
24581 parser->qualifying_scope = NULL_TREE;
24582 parser->object_scope = NULL_TREE;
24583 /* If it's a `,', then there are more declarators. */
24584 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24585 {
24586 cp_lexer_consume_token (parser->lexer);
24587 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24588 {
24589 cp_token *token = cp_lexer_previous_token (parser->lexer);
24590 gcc_rich_location richloc (token->location);
24591 richloc.add_fixit_remove ();
24592 error_at (&richloc, "stray %<,%> at end of "
24593 "member declaration");
24594 }
24595 }
24596 /* If the next token isn't a `;', then we have a parse error. */
24597 else if (cp_lexer_next_token_is_not (parser->lexer,
24598 CPP_SEMICOLON))
24599 {
24600 /* The next token might be a ways away from where the
24601 actual semicolon is missing. Find the previous token
24602 and use that for our error position. */
24603 cp_token *token = cp_lexer_previous_token (parser->lexer);
24604 gcc_rich_location richloc (token->location);
24605 richloc.add_fixit_insert_after (";");
24606 error_at (&richloc, "expected %<;%> at end of "
24607 "member declaration");
24608
24609 /* Assume that the user meant to provide a semicolon. If
24610 we were to cp_parser_skip_to_end_of_statement, we might
24611 skip to a semicolon inside a member function definition
24612 and issue nonsensical error messages. */
24613 assume_semicolon = true;
24614 }
24615
24616 if (decl)
24617 {
24618 /* Add DECL to the list of members. */
24619 if (!friend_p
24620 /* Explicitly include, eg, NSDMIs, for better error
24621 recovery (c++/58650). */
24622 || !DECL_DECLARES_FUNCTION_P (decl))
24623 finish_member_declaration (decl);
24624
24625 if (TREE_CODE (decl) == FUNCTION_DECL)
24626 cp_parser_save_default_args (parser, decl);
24627 else if (TREE_CODE (decl) == FIELD_DECL
24628 && DECL_INITIAL (decl))
24629 /* Add DECL to the queue of NSDMI to be parsed later. */
24630 vec_safe_push (unparsed_nsdmis, decl);
24631 }
24632
24633 if (assume_semicolon)
24634 goto out;
24635 }
24636 }
24637
24638 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24639 out:
24640 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24641 }
24642
24643 /* Parse a pure-specifier.
24644
24645 pure-specifier:
24646 = 0
24647
24648 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24649 Otherwise, ERROR_MARK_NODE is returned. */
24650
24651 static tree
24652 cp_parser_pure_specifier (cp_parser* parser)
24653 {
24654 cp_token *token;
24655
24656 /* Look for the `=' token. */
24657 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24658 return error_mark_node;
24659 /* Look for the `0' token. */
24660 token = cp_lexer_peek_token (parser->lexer);
24661
24662 if (token->type == CPP_EOF
24663 || token->type == CPP_PRAGMA_EOL)
24664 return error_mark_node;
24665
24666 cp_lexer_consume_token (parser->lexer);
24667
24668 /* Accept = default or = delete in c++0x mode. */
24669 if (token->keyword == RID_DEFAULT
24670 || token->keyword == RID_DELETE)
24671 {
24672 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24673 return token->u.value;
24674 }
24675
24676 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24677 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24678 {
24679 cp_parser_error (parser,
24680 "invalid pure specifier (only %<= 0%> is allowed)");
24681 cp_parser_skip_to_end_of_statement (parser);
24682 return error_mark_node;
24683 }
24684 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24685 {
24686 error_at (token->location, "templates may not be %<virtual%>");
24687 return error_mark_node;
24688 }
24689
24690 return integer_zero_node;
24691 }
24692
24693 /* Parse a constant-initializer.
24694
24695 constant-initializer:
24696 = constant-expression
24697
24698 Returns a representation of the constant-expression. */
24699
24700 static tree
24701 cp_parser_constant_initializer (cp_parser* parser)
24702 {
24703 /* Look for the `=' token. */
24704 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24705 return error_mark_node;
24706
24707 /* It is invalid to write:
24708
24709 struct S { static const int i = { 7 }; };
24710
24711 */
24712 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24713 {
24714 cp_parser_error (parser,
24715 "a brace-enclosed initializer is not allowed here");
24716 /* Consume the opening brace. */
24717 matching_braces braces;
24718 braces.consume_open (parser);
24719 /* Skip the initializer. */
24720 cp_parser_skip_to_closing_brace (parser);
24721 /* Look for the trailing `}'. */
24722 braces.require_close (parser);
24723
24724 return error_mark_node;
24725 }
24726
24727 return cp_parser_constant_expression (parser);
24728 }
24729
24730 /* Derived classes [gram.class.derived] */
24731
24732 /* Parse a base-clause.
24733
24734 base-clause:
24735 : base-specifier-list
24736
24737 base-specifier-list:
24738 base-specifier ... [opt]
24739 base-specifier-list , base-specifier ... [opt]
24740
24741 Returns a TREE_LIST representing the base-classes, in the order in
24742 which they were declared. The representation of each node is as
24743 described by cp_parser_base_specifier.
24744
24745 In the case that no bases are specified, this function will return
24746 NULL_TREE, not ERROR_MARK_NODE. */
24747
24748 static tree
24749 cp_parser_base_clause (cp_parser* parser)
24750 {
24751 tree bases = NULL_TREE;
24752
24753 /* Look for the `:' that begins the list. */
24754 cp_parser_require (parser, CPP_COLON, RT_COLON);
24755
24756 /* Scan the base-specifier-list. */
24757 while (true)
24758 {
24759 cp_token *token;
24760 tree base;
24761 bool pack_expansion_p = false;
24762
24763 /* Look for the base-specifier. */
24764 base = cp_parser_base_specifier (parser);
24765 /* Look for the (optional) ellipsis. */
24766 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24767 {
24768 /* Consume the `...'. */
24769 cp_lexer_consume_token (parser->lexer);
24770
24771 pack_expansion_p = true;
24772 }
24773
24774 /* Add BASE to the front of the list. */
24775 if (base && base != error_mark_node)
24776 {
24777 if (pack_expansion_p)
24778 /* Make this a pack expansion type. */
24779 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24780
24781 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24782 {
24783 TREE_CHAIN (base) = bases;
24784 bases = base;
24785 }
24786 }
24787 /* Peek at the next token. */
24788 token = cp_lexer_peek_token (parser->lexer);
24789 /* If it's not a comma, then the list is complete. */
24790 if (token->type != CPP_COMMA)
24791 break;
24792 /* Consume the `,'. */
24793 cp_lexer_consume_token (parser->lexer);
24794 }
24795
24796 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24797 base class had a qualified name. However, the next name that
24798 appears is certainly not qualified. */
24799 parser->scope = NULL_TREE;
24800 parser->qualifying_scope = NULL_TREE;
24801 parser->object_scope = NULL_TREE;
24802
24803 return nreverse (bases);
24804 }
24805
24806 /* Parse a base-specifier.
24807
24808 base-specifier:
24809 :: [opt] nested-name-specifier [opt] class-name
24810 virtual access-specifier [opt] :: [opt] nested-name-specifier
24811 [opt] class-name
24812 access-specifier virtual [opt] :: [opt] nested-name-specifier
24813 [opt] class-name
24814
24815 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24816 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24817 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24818 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24819
24820 static tree
24821 cp_parser_base_specifier (cp_parser* parser)
24822 {
24823 cp_token *token;
24824 bool done = false;
24825 bool virtual_p = false;
24826 bool duplicate_virtual_error_issued_p = false;
24827 bool duplicate_access_error_issued_p = false;
24828 bool class_scope_p, template_p;
24829 tree access = access_default_node;
24830 tree type;
24831
24832 /* Process the optional `virtual' and `access-specifier'. */
24833 while (!done)
24834 {
24835 /* Peek at the next token. */
24836 token = cp_lexer_peek_token (parser->lexer);
24837 /* Process `virtual'. */
24838 switch (token->keyword)
24839 {
24840 case RID_VIRTUAL:
24841 /* If `virtual' appears more than once, issue an error. */
24842 if (virtual_p && !duplicate_virtual_error_issued_p)
24843 {
24844 cp_parser_error (parser,
24845 "%<virtual%> specified more than once in base-specifier");
24846 duplicate_virtual_error_issued_p = true;
24847 }
24848
24849 virtual_p = true;
24850
24851 /* Consume the `virtual' token. */
24852 cp_lexer_consume_token (parser->lexer);
24853
24854 break;
24855
24856 case RID_PUBLIC:
24857 case RID_PROTECTED:
24858 case RID_PRIVATE:
24859 /* If more than one access specifier appears, issue an
24860 error. */
24861 if (access != access_default_node
24862 && !duplicate_access_error_issued_p)
24863 {
24864 cp_parser_error (parser,
24865 "more than one access specifier in base-specifier");
24866 duplicate_access_error_issued_p = true;
24867 }
24868
24869 access = ridpointers[(int) token->keyword];
24870
24871 /* Consume the access-specifier. */
24872 cp_lexer_consume_token (parser->lexer);
24873
24874 break;
24875
24876 default:
24877 done = true;
24878 break;
24879 }
24880 }
24881 /* It is not uncommon to see programs mechanically, erroneously, use
24882 the 'typename' keyword to denote (dependent) qualified types
24883 as base classes. */
24884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24885 {
24886 token = cp_lexer_peek_token (parser->lexer);
24887 if (!processing_template_decl)
24888 error_at (token->location,
24889 "keyword %<typename%> not allowed outside of templates");
24890 else
24891 error_at (token->location,
24892 "keyword %<typename%> not allowed in this context "
24893 "(the base class is implicitly a type)");
24894 cp_lexer_consume_token (parser->lexer);
24895 }
24896
24897 /* Look for the optional `::' operator. */
24898 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24899 /* Look for the nested-name-specifier. The simplest way to
24900 implement:
24901
24902 [temp.res]
24903
24904 The keyword `typename' is not permitted in a base-specifier or
24905 mem-initializer; in these contexts a qualified name that
24906 depends on a template-parameter is implicitly assumed to be a
24907 type name.
24908
24909 is to pretend that we have seen the `typename' keyword at this
24910 point. */
24911 cp_parser_nested_name_specifier_opt (parser,
24912 /*typename_keyword_p=*/true,
24913 /*check_dependency_p=*/true,
24914 /*type_p=*/true,
24915 /*is_declaration=*/true);
24916 /* If the base class is given by a qualified name, assume that names
24917 we see are type names or templates, as appropriate. */
24918 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24919 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24920
24921 if (!parser->scope
24922 && cp_lexer_next_token_is_decltype (parser->lexer))
24923 /* DR 950 allows decltype as a base-specifier. */
24924 type = cp_parser_decltype (parser);
24925 else
24926 {
24927 /* Otherwise, look for the class-name. */
24928 type = cp_parser_class_name (parser,
24929 class_scope_p,
24930 template_p,
24931 typename_type,
24932 /*check_dependency_p=*/true,
24933 /*class_head_p=*/false,
24934 /*is_declaration=*/true);
24935 type = TREE_TYPE (type);
24936 }
24937
24938 if (type == error_mark_node)
24939 return error_mark_node;
24940
24941 return finish_base_specifier (type, access, virtual_p);
24942 }
24943
24944 /* Exception handling [gram.exception] */
24945
24946 /* Parse an (optional) noexcept-specification.
24947
24948 noexcept-specification:
24949 noexcept ( constant-expression ) [opt]
24950
24951 If no noexcept-specification is present, returns NULL_TREE.
24952 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24953 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24954 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24955 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24956 in which case a boolean condition is returned instead. */
24957
24958 static tree
24959 cp_parser_noexcept_specification_opt (cp_parser* parser,
24960 bool require_constexpr,
24961 bool* consumed_expr,
24962 bool return_cond)
24963 {
24964 cp_token *token;
24965 const char *saved_message;
24966
24967 /* Peek at the next token. */
24968 token = cp_lexer_peek_token (parser->lexer);
24969
24970 /* Is it a noexcept-specification? */
24971 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24972 {
24973 tree expr;
24974 cp_lexer_consume_token (parser->lexer);
24975
24976 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24977 {
24978 matching_parens parens;
24979 parens.consume_open (parser);
24980
24981 tree save_ccp = current_class_ptr;
24982 tree save_ccr = current_class_ref;
24983
24984 if (current_class_type)
24985 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
24986
24987 if (require_constexpr)
24988 {
24989 /* Types may not be defined in an exception-specification. */
24990 saved_message = parser->type_definition_forbidden_message;
24991 parser->type_definition_forbidden_message
24992 = G_("types may not be defined in an exception-specification");
24993
24994 expr = cp_parser_constant_expression (parser);
24995
24996 /* Restore the saved message. */
24997 parser->type_definition_forbidden_message = saved_message;
24998 }
24999 else
25000 {
25001 expr = cp_parser_expression (parser);
25002 *consumed_expr = true;
25003 }
25004
25005 parens.require_close (parser);
25006
25007 current_class_ptr = save_ccp;
25008 current_class_ref = save_ccr;
25009 }
25010 else
25011 {
25012 expr = boolean_true_node;
25013 if (!require_constexpr)
25014 *consumed_expr = false;
25015 }
25016
25017 /* We cannot build a noexcept-spec right away because this will check
25018 that expr is a constexpr. */
25019 if (!return_cond)
25020 return build_noexcept_spec (expr, tf_warning_or_error);
25021 else
25022 return expr;
25023 }
25024 else
25025 return NULL_TREE;
25026 }
25027
25028 /* Parse an (optional) exception-specification.
25029
25030 exception-specification:
25031 throw ( type-id-list [opt] )
25032
25033 Returns a TREE_LIST representing the exception-specification. The
25034 TREE_VALUE of each node is a type. */
25035
25036 static tree
25037 cp_parser_exception_specification_opt (cp_parser* parser)
25038 {
25039 cp_token *token;
25040 tree type_id_list;
25041 const char *saved_message;
25042
25043 /* Peek at the next token. */
25044 token = cp_lexer_peek_token (parser->lexer);
25045
25046 /* Is it a noexcept-specification? */
25047 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
25048 false);
25049 if (type_id_list != NULL_TREE)
25050 return type_id_list;
25051
25052 /* If it's not `throw', then there's no exception-specification. */
25053 if (!cp_parser_is_keyword (token, RID_THROW))
25054 return NULL_TREE;
25055
25056 location_t loc = token->location;
25057
25058 /* Consume the `throw'. */
25059 cp_lexer_consume_token (parser->lexer);
25060
25061 /* Look for the `('. */
25062 matching_parens parens;
25063 parens.require_open (parser);
25064
25065 /* Peek at the next token. */
25066 token = cp_lexer_peek_token (parser->lexer);
25067 /* If it's not a `)', then there is a type-id-list. */
25068 if (token->type != CPP_CLOSE_PAREN)
25069 {
25070 /* Types may not be defined in an exception-specification. */
25071 saved_message = parser->type_definition_forbidden_message;
25072 parser->type_definition_forbidden_message
25073 = G_("types may not be defined in an exception-specification");
25074 /* Parse the type-id-list. */
25075 type_id_list = cp_parser_type_id_list (parser);
25076 /* Restore the saved message. */
25077 parser->type_definition_forbidden_message = saved_message;
25078
25079 if (cxx_dialect >= cxx17)
25080 {
25081 error_at (loc, "ISO C++17 does not allow dynamic exception "
25082 "specifications");
25083 type_id_list = NULL_TREE;
25084 }
25085 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
25086 warning_at (loc, OPT_Wdeprecated,
25087 "dynamic exception specifications are deprecated in "
25088 "C++11");
25089 }
25090 /* In C++17, throw() is equivalent to noexcept (true). throw()
25091 is deprecated in C++11 and above as well, but is still widely used,
25092 so don't warn about it yet. */
25093 else if (cxx_dialect >= cxx17)
25094 type_id_list = noexcept_true_spec;
25095 else
25096 type_id_list = empty_except_spec;
25097
25098 /* Look for the `)'. */
25099 parens.require_close (parser);
25100
25101 return type_id_list;
25102 }
25103
25104 /* Parse an (optional) type-id-list.
25105
25106 type-id-list:
25107 type-id ... [opt]
25108 type-id-list , type-id ... [opt]
25109
25110 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25111 in the order that the types were presented. */
25112
25113 static tree
25114 cp_parser_type_id_list (cp_parser* parser)
25115 {
25116 tree types = NULL_TREE;
25117
25118 while (true)
25119 {
25120 cp_token *token;
25121 tree type;
25122
25123 token = cp_lexer_peek_token (parser->lexer);
25124
25125 /* Get the next type-id. */
25126 type = cp_parser_type_id (parser);
25127 /* Check for invalid 'auto'. */
25128 if (flag_concepts && type_uses_auto (type))
25129 {
25130 error_at (token->location,
25131 "invalid use of %<auto%> in exception-specification");
25132 type = error_mark_node;
25133 }
25134 /* Parse the optional ellipsis. */
25135 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25136 {
25137 /* Consume the `...'. */
25138 cp_lexer_consume_token (parser->lexer);
25139
25140 /* Turn the type into a pack expansion expression. */
25141 type = make_pack_expansion (type);
25142 }
25143 /* Add it to the list. */
25144 types = add_exception_specifier (types, type, /*complain=*/1);
25145 /* Peek at the next token. */
25146 token = cp_lexer_peek_token (parser->lexer);
25147 /* If it is not a `,', we are done. */
25148 if (token->type != CPP_COMMA)
25149 break;
25150 /* Consume the `,'. */
25151 cp_lexer_consume_token (parser->lexer);
25152 }
25153
25154 return nreverse (types);
25155 }
25156
25157 /* Parse a try-block.
25158
25159 try-block:
25160 try compound-statement handler-seq */
25161
25162 static tree
25163 cp_parser_try_block (cp_parser* parser)
25164 {
25165 tree try_block;
25166
25167 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25168 if (parser->in_function_body
25169 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
25170 error ("%<try%> in %<constexpr%> function");
25171
25172 try_block = begin_try_block ();
25173 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25174 finish_try_block (try_block);
25175 cp_parser_handler_seq (parser);
25176 finish_handler_sequence (try_block);
25177
25178 return try_block;
25179 }
25180
25181 /* Parse a function-try-block.
25182
25183 function-try-block:
25184 try ctor-initializer [opt] function-body handler-seq */
25185
25186 static void
25187 cp_parser_function_try_block (cp_parser* parser)
25188 {
25189 tree compound_stmt;
25190 tree try_block;
25191
25192 /* Look for the `try' keyword. */
25193 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25194 return;
25195 /* Let the rest of the front end know where we are. */
25196 try_block = begin_function_try_block (&compound_stmt);
25197 /* Parse the function-body. */
25198 cp_parser_ctor_initializer_opt_and_function_body
25199 (parser, /*in_function_try_block=*/true);
25200 /* We're done with the `try' part. */
25201 finish_function_try_block (try_block);
25202 /* Parse the handlers. */
25203 cp_parser_handler_seq (parser);
25204 /* We're done with the handlers. */
25205 finish_function_handler_sequence (try_block, compound_stmt);
25206 }
25207
25208 /* Parse a handler-seq.
25209
25210 handler-seq:
25211 handler handler-seq [opt] */
25212
25213 static void
25214 cp_parser_handler_seq (cp_parser* parser)
25215 {
25216 while (true)
25217 {
25218 cp_token *token;
25219
25220 /* Parse the handler. */
25221 cp_parser_handler (parser);
25222 /* Peek at the next token. */
25223 token = cp_lexer_peek_token (parser->lexer);
25224 /* If it's not `catch' then there are no more handlers. */
25225 if (!cp_parser_is_keyword (token, RID_CATCH))
25226 break;
25227 }
25228 }
25229
25230 /* Parse a handler.
25231
25232 handler:
25233 catch ( exception-declaration ) compound-statement */
25234
25235 static void
25236 cp_parser_handler (cp_parser* parser)
25237 {
25238 tree handler;
25239 tree declaration;
25240
25241 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25242 handler = begin_handler ();
25243 matching_parens parens;
25244 parens.require_open (parser);
25245 declaration = cp_parser_exception_declaration (parser);
25246 finish_handler_parms (declaration, handler);
25247 parens.require_close (parser);
25248 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25249 finish_handler (handler);
25250 }
25251
25252 /* Parse an exception-declaration.
25253
25254 exception-declaration:
25255 type-specifier-seq declarator
25256 type-specifier-seq abstract-declarator
25257 type-specifier-seq
25258 ...
25259
25260 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25261 ellipsis variant is used. */
25262
25263 static tree
25264 cp_parser_exception_declaration (cp_parser* parser)
25265 {
25266 cp_decl_specifier_seq type_specifiers;
25267 cp_declarator *declarator;
25268 const char *saved_message;
25269
25270 /* If it's an ellipsis, it's easy to handle. */
25271 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25272 {
25273 /* Consume the `...' token. */
25274 cp_lexer_consume_token (parser->lexer);
25275 return NULL_TREE;
25276 }
25277
25278 /* Types may not be defined in exception-declarations. */
25279 saved_message = parser->type_definition_forbidden_message;
25280 parser->type_definition_forbidden_message
25281 = G_("types may not be defined in exception-declarations");
25282
25283 /* Parse the type-specifier-seq. */
25284 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25285 /*is_declaration=*/true,
25286 /*is_trailing_return=*/false,
25287 &type_specifiers);
25288 /* If it's a `)', then there is no declarator. */
25289 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25290 declarator = NULL;
25291 else
25292 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25293 CP_PARSER_FLAGS_NONE,
25294 /*ctor_dtor_or_conv_p=*/NULL,
25295 /*parenthesized_p=*/NULL,
25296 /*member_p=*/false,
25297 /*friend_p=*/false);
25298
25299 /* Restore the saved message. */
25300 parser->type_definition_forbidden_message = saved_message;
25301
25302 if (!type_specifiers.any_specifiers_p)
25303 return error_mark_node;
25304
25305 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25306 }
25307
25308 /* Parse a throw-expression.
25309
25310 throw-expression:
25311 throw assignment-expression [opt]
25312
25313 Returns a THROW_EXPR representing the throw-expression. */
25314
25315 static tree
25316 cp_parser_throw_expression (cp_parser* parser)
25317 {
25318 tree expression;
25319 cp_token* token;
25320
25321 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25322 token = cp_lexer_peek_token (parser->lexer);
25323 /* Figure out whether or not there is an assignment-expression
25324 following the "throw" keyword. */
25325 if (token->type == CPP_COMMA
25326 || token->type == CPP_SEMICOLON
25327 || token->type == CPP_CLOSE_PAREN
25328 || token->type == CPP_CLOSE_SQUARE
25329 || token->type == CPP_CLOSE_BRACE
25330 || token->type == CPP_COLON)
25331 expression = NULL_TREE;
25332 else
25333 expression = cp_parser_assignment_expression (parser);
25334
25335 return build_throw (expression);
25336 }
25337
25338 /* GNU Extensions */
25339
25340 /* Parse an (optional) asm-specification.
25341
25342 asm-specification:
25343 asm ( string-literal )
25344
25345 If the asm-specification is present, returns a STRING_CST
25346 corresponding to the string-literal. Otherwise, returns
25347 NULL_TREE. */
25348
25349 static tree
25350 cp_parser_asm_specification_opt (cp_parser* parser)
25351 {
25352 cp_token *token;
25353 tree asm_specification;
25354
25355 /* Peek at the next token. */
25356 token = cp_lexer_peek_token (parser->lexer);
25357 /* If the next token isn't the `asm' keyword, then there's no
25358 asm-specification. */
25359 if (!cp_parser_is_keyword (token, RID_ASM))
25360 return NULL_TREE;
25361
25362 /* Consume the `asm' token. */
25363 cp_lexer_consume_token (parser->lexer);
25364 /* Look for the `('. */
25365 matching_parens parens;
25366 parens.require_open (parser);
25367
25368 /* Look for the string-literal. */
25369 asm_specification = cp_parser_string_literal (parser, false, false);
25370
25371 /* Look for the `)'. */
25372 parens.require_close (parser);
25373
25374 return asm_specification;
25375 }
25376
25377 /* Parse an asm-operand-list.
25378
25379 asm-operand-list:
25380 asm-operand
25381 asm-operand-list , asm-operand
25382
25383 asm-operand:
25384 string-literal ( expression )
25385 [ string-literal ] string-literal ( expression )
25386
25387 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25388 each node is the expression. The TREE_PURPOSE is itself a
25389 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25390 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25391 is a STRING_CST for the string literal before the parenthesis. Returns
25392 ERROR_MARK_NODE if any of the operands are invalid. */
25393
25394 static tree
25395 cp_parser_asm_operand_list (cp_parser* parser)
25396 {
25397 tree asm_operands = NULL_TREE;
25398 bool invalid_operands = false;
25399
25400 while (true)
25401 {
25402 tree string_literal;
25403 tree expression;
25404 tree name;
25405
25406 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25407 {
25408 /* Consume the `[' token. */
25409 cp_lexer_consume_token (parser->lexer);
25410 /* Read the operand name. */
25411 name = cp_parser_identifier (parser);
25412 if (name != error_mark_node)
25413 name = build_string (IDENTIFIER_LENGTH (name),
25414 IDENTIFIER_POINTER (name));
25415 /* Look for the closing `]'. */
25416 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25417 }
25418 else
25419 name = NULL_TREE;
25420 /* Look for the string-literal. */
25421 string_literal = cp_parser_string_literal (parser, false, false);
25422
25423 /* Look for the `('. */
25424 matching_parens parens;
25425 parens.require_open (parser);
25426 /* Parse the expression. */
25427 expression = cp_parser_expression (parser);
25428 /* Look for the `)'. */
25429 parens.require_close (parser);
25430
25431 if (name == error_mark_node
25432 || string_literal == error_mark_node
25433 || expression == error_mark_node)
25434 invalid_operands = true;
25435
25436 /* Add this operand to the list. */
25437 asm_operands = tree_cons (build_tree_list (name, string_literal),
25438 expression,
25439 asm_operands);
25440 /* If the next token is not a `,', there are no more
25441 operands. */
25442 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25443 break;
25444 /* Consume the `,'. */
25445 cp_lexer_consume_token (parser->lexer);
25446 }
25447
25448 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25449 }
25450
25451 /* Parse an asm-clobber-list.
25452
25453 asm-clobber-list:
25454 string-literal
25455 asm-clobber-list , string-literal
25456
25457 Returns a TREE_LIST, indicating the clobbers in the order that they
25458 appeared. The TREE_VALUE of each node is a STRING_CST. */
25459
25460 static tree
25461 cp_parser_asm_clobber_list (cp_parser* parser)
25462 {
25463 tree clobbers = NULL_TREE;
25464
25465 while (true)
25466 {
25467 tree string_literal;
25468
25469 /* Look for the string literal. */
25470 string_literal = cp_parser_string_literal (parser, false, false);
25471 /* Add it to the list. */
25472 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25473 /* If the next token is not a `,', then the list is
25474 complete. */
25475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25476 break;
25477 /* Consume the `,' token. */
25478 cp_lexer_consume_token (parser->lexer);
25479 }
25480
25481 return clobbers;
25482 }
25483
25484 /* Parse an asm-label-list.
25485
25486 asm-label-list:
25487 identifier
25488 asm-label-list , identifier
25489
25490 Returns a TREE_LIST, indicating the labels in the order that they
25491 appeared. The TREE_VALUE of each node is a label. */
25492
25493 static tree
25494 cp_parser_asm_label_list (cp_parser* parser)
25495 {
25496 tree labels = NULL_TREE;
25497
25498 while (true)
25499 {
25500 tree identifier, label, name;
25501
25502 /* Look for the identifier. */
25503 identifier = cp_parser_identifier (parser);
25504 if (!error_operand_p (identifier))
25505 {
25506 label = lookup_label (identifier);
25507 if (TREE_CODE (label) == LABEL_DECL)
25508 {
25509 TREE_USED (label) = 1;
25510 check_goto (label);
25511 name = build_string (IDENTIFIER_LENGTH (identifier),
25512 IDENTIFIER_POINTER (identifier));
25513 labels = tree_cons (name, label, labels);
25514 }
25515 }
25516 /* If the next token is not a `,', then the list is
25517 complete. */
25518 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25519 break;
25520 /* Consume the `,' token. */
25521 cp_lexer_consume_token (parser->lexer);
25522 }
25523
25524 return nreverse (labels);
25525 }
25526
25527 /* Return TRUE iff the next tokens in the stream are possibly the
25528 beginning of a GNU extension attribute. */
25529
25530 static bool
25531 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25532 {
25533 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25534 }
25535
25536 /* Return TRUE iff the next tokens in the stream are possibly the
25537 beginning of a standard C++-11 attribute specifier. */
25538
25539 static bool
25540 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25541 {
25542 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25543 }
25544
25545 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25546 beginning of a standard C++-11 attribute specifier. */
25547
25548 static bool
25549 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25550 {
25551 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25552
25553 return (cxx_dialect >= cxx11
25554 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25555 || (token->type == CPP_OPEN_SQUARE
25556 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25557 && token->type == CPP_OPEN_SQUARE)));
25558 }
25559
25560 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25561 beginning of a GNU extension attribute. */
25562
25563 static bool
25564 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25565 {
25566 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25567
25568 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25569 }
25570
25571 /* Return true iff the next tokens can be the beginning of either a
25572 GNU attribute list, or a standard C++11 attribute sequence. */
25573
25574 static bool
25575 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25576 {
25577 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25578 || cp_next_tokens_can_be_std_attribute_p (parser));
25579 }
25580
25581 /* Return true iff the next Nth tokens can be the beginning of either
25582 a GNU attribute list, or a standard C++11 attribute sequence. */
25583
25584 static bool
25585 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25586 {
25587 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25588 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25589 }
25590
25591 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25592 of GNU attributes, or return NULL. */
25593
25594 static tree
25595 cp_parser_attributes_opt (cp_parser *parser)
25596 {
25597 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25598 return cp_parser_gnu_attributes_opt (parser);
25599 return cp_parser_std_attribute_spec_seq (parser);
25600 }
25601
25602 /* Parse an (optional) series of attributes.
25603
25604 attributes:
25605 attributes attribute
25606
25607 attribute:
25608 __attribute__ (( attribute-list [opt] ))
25609
25610 The return value is as for cp_parser_gnu_attribute_list. */
25611
25612 static tree
25613 cp_parser_gnu_attributes_opt (cp_parser* parser)
25614 {
25615 tree attributes = NULL_TREE;
25616
25617 temp_override<bool> cleanup
25618 (parser->auto_is_implicit_function_template_parm_p, false);
25619
25620 while (true)
25621 {
25622 cp_token *token;
25623 tree attribute_list;
25624 bool ok = true;
25625
25626 /* Peek at the next token. */
25627 token = cp_lexer_peek_token (parser->lexer);
25628 /* If it's not `__attribute__', then we're done. */
25629 if (token->keyword != RID_ATTRIBUTE)
25630 break;
25631
25632 /* Consume the `__attribute__' keyword. */
25633 cp_lexer_consume_token (parser->lexer);
25634 /* Look for the two `(' tokens. */
25635 matching_parens outer_parens;
25636 outer_parens.require_open (parser);
25637 matching_parens inner_parens;
25638 inner_parens.require_open (parser);
25639
25640 /* Peek at the next token. */
25641 token = cp_lexer_peek_token (parser->lexer);
25642 if (token->type != CPP_CLOSE_PAREN)
25643 /* Parse the attribute-list. */
25644 attribute_list = cp_parser_gnu_attribute_list (parser);
25645 else
25646 /* If the next token is a `)', then there is no attribute
25647 list. */
25648 attribute_list = NULL;
25649
25650 /* Look for the two `)' tokens. */
25651 if (!inner_parens.require_close (parser))
25652 ok = false;
25653 if (!outer_parens.require_close (parser))
25654 ok = false;
25655 if (!ok)
25656 cp_parser_skip_to_end_of_statement (parser);
25657
25658 /* Add these new attributes to the list. */
25659 attributes = attr_chainon (attributes, attribute_list);
25660 }
25661
25662 return attributes;
25663 }
25664
25665 /* Parse a GNU attribute-list.
25666
25667 attribute-list:
25668 attribute
25669 attribute-list , attribute
25670
25671 attribute:
25672 identifier
25673 identifier ( identifier )
25674 identifier ( identifier , expression-list )
25675 identifier ( expression-list )
25676
25677 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25678 to an attribute. The TREE_PURPOSE of each node is the identifier
25679 indicating which attribute is in use. The TREE_VALUE represents
25680 the arguments, if any. */
25681
25682 static tree
25683 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
25684 {
25685 tree attribute_list = NULL_TREE;
25686 bool save_translate_strings_p = parser->translate_strings_p;
25687
25688 /* Don't create wrapper nodes within attributes: the
25689 handlers don't know how to handle them. */
25690 auto_suppress_location_wrappers sentinel;
25691
25692 parser->translate_strings_p = false;
25693 while (true)
25694 {
25695 cp_token *token;
25696 tree identifier;
25697 tree attribute;
25698
25699 /* Look for the identifier. We also allow keywords here; for
25700 example `__attribute__ ((const))' is legal. */
25701 token = cp_lexer_peek_token (parser->lexer);
25702 if (token->type == CPP_NAME
25703 || token->type == CPP_KEYWORD)
25704 {
25705 tree arguments = NULL_TREE;
25706
25707 /* Consume the token, but save it since we need it for the
25708 SIMD enabled function parsing. */
25709 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25710
25711 /* Save away the identifier that indicates which attribute
25712 this is. */
25713 identifier = (token->type == CPP_KEYWORD)
25714 /* For keywords, use the canonical spelling, not the
25715 parsed identifier. */
25716 ? ridpointers[(int) token->keyword]
25717 : id_token->u.value;
25718
25719 identifier = canonicalize_attr_name (identifier);
25720 attribute = build_tree_list (identifier, NULL_TREE);
25721
25722 /* Peek at the next token. */
25723 token = cp_lexer_peek_token (parser->lexer);
25724 /* If it's an `(', then parse the attribute arguments. */
25725 if (token->type == CPP_OPEN_PAREN)
25726 {
25727 vec<tree, va_gc> *vec;
25728 int attr_flag = (attribute_takes_identifier_p (identifier)
25729 ? id_attr : normal_attr);
25730 vec = cp_parser_parenthesized_expression_list
25731 (parser, attr_flag, /*cast_p=*/false,
25732 /*allow_expansion_p=*/false,
25733 /*non_constant_p=*/NULL);
25734 if (vec == NULL)
25735 arguments = error_mark_node;
25736 else
25737 {
25738 arguments = build_tree_list_vec (vec);
25739 release_tree_vector (vec);
25740 }
25741 /* Save the arguments away. */
25742 TREE_VALUE (attribute) = arguments;
25743 }
25744
25745 if (arguments != error_mark_node)
25746 {
25747 /* Add this attribute to the list. */
25748 TREE_CHAIN (attribute) = attribute_list;
25749 attribute_list = attribute;
25750 }
25751
25752 token = cp_lexer_peek_token (parser->lexer);
25753 }
25754 /* Unless EXACTLY_ONE is set look for more attributes.
25755 If the next token isn't a `,', we're done. */
25756 if (exactly_one || token->type != CPP_COMMA)
25757 break;
25758
25759 /* Consume the comma and keep going. */
25760 cp_lexer_consume_token (parser->lexer);
25761 }
25762 parser->translate_strings_p = save_translate_strings_p;
25763
25764 /* We built up the list in reverse order. */
25765 return nreverse (attribute_list);
25766 }
25767
25768 /* Parse a standard C++11 attribute.
25769
25770 The returned representation is a TREE_LIST which TREE_PURPOSE is
25771 the scoped name of the attribute, and the TREE_VALUE is its
25772 arguments list.
25773
25774 Note that the scoped name of the attribute is itself a TREE_LIST
25775 which TREE_PURPOSE is the namespace of the attribute, and
25776 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25777 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25778 and which TREE_PURPOSE is directly the attribute name.
25779
25780 Clients of the attribute code should use get_attribute_namespace
25781 and get_attribute_name to get the actual namespace and name of
25782 attributes, regardless of their being GNU or C++11 attributes.
25783
25784 attribute:
25785 attribute-token attribute-argument-clause [opt]
25786
25787 attribute-token:
25788 identifier
25789 attribute-scoped-token
25790
25791 attribute-scoped-token:
25792 attribute-namespace :: identifier
25793
25794 attribute-namespace:
25795 identifier
25796
25797 attribute-argument-clause:
25798 ( balanced-token-seq )
25799
25800 balanced-token-seq:
25801 balanced-token [opt]
25802 balanced-token-seq balanced-token
25803
25804 balanced-token:
25805 ( balanced-token-seq )
25806 [ balanced-token-seq ]
25807 { balanced-token-seq }. */
25808
25809 static tree
25810 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25811 {
25812 tree attribute, attr_id = NULL_TREE, arguments;
25813 cp_token *token;
25814
25815 temp_override<bool> cleanup
25816 (parser->auto_is_implicit_function_template_parm_p, false);
25817
25818 /* First, parse name of the attribute, a.k.a attribute-token. */
25819
25820 token = cp_lexer_peek_token (parser->lexer);
25821 if (token->type == CPP_NAME)
25822 attr_id = token->u.value;
25823 else if (token->type == CPP_KEYWORD)
25824 attr_id = ridpointers[(int) token->keyword];
25825 else if (token->flags & NAMED_OP)
25826 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25827
25828 if (attr_id == NULL_TREE)
25829 return NULL_TREE;
25830
25831 cp_lexer_consume_token (parser->lexer);
25832
25833 token = cp_lexer_peek_token (parser->lexer);
25834 if (token->type == CPP_SCOPE)
25835 {
25836 /* We are seeing a scoped attribute token. */
25837
25838 cp_lexer_consume_token (parser->lexer);
25839 if (attr_ns)
25840 error_at (token->location, "attribute using prefix used together "
25841 "with scoped attribute token");
25842 attr_ns = attr_id;
25843
25844 token = cp_lexer_consume_token (parser->lexer);
25845 if (token->type == CPP_NAME)
25846 attr_id = token->u.value;
25847 else if (token->type == CPP_KEYWORD)
25848 attr_id = ridpointers[(int) token->keyword];
25849 else if (token->flags & NAMED_OP)
25850 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25851 else
25852 {
25853 error_at (token->location,
25854 "expected an identifier for the attribute name");
25855 return error_mark_node;
25856 }
25857
25858 attr_ns = canonicalize_attr_name (attr_ns);
25859 attr_id = canonicalize_attr_name (attr_id);
25860 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25861 NULL_TREE);
25862 token = cp_lexer_peek_token (parser->lexer);
25863 }
25864 else if (attr_ns)
25865 {
25866 attr_ns = canonicalize_attr_name (attr_ns);
25867 attr_id = canonicalize_attr_name (attr_id);
25868 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25869 NULL_TREE);
25870 }
25871 else
25872 {
25873 attr_id = canonicalize_attr_name (attr_id);
25874 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25875 NULL_TREE);
25876 /* C++11 noreturn attribute is equivalent to GNU's. */
25877 if (is_attribute_p ("noreturn", attr_id))
25878 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25879 /* C++14 deprecated attribute is equivalent to GNU's. */
25880 else if (is_attribute_p ("deprecated", attr_id))
25881 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25882 /* C++17 fallthrough attribute is equivalent to GNU's. */
25883 else if (is_attribute_p ("fallthrough", attr_id))
25884 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
25885 /* Transactional Memory TS optimize_for_synchronized attribute is
25886 equivalent to GNU transaction_callable. */
25887 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25888 TREE_PURPOSE (attribute)
25889 = get_identifier ("transaction_callable");
25890 /* Transactional Memory attributes are GNU attributes. */
25891 else if (tm_attr_to_mask (attr_id))
25892 TREE_PURPOSE (attribute) = attr_id;
25893 }
25894
25895 /* Now parse the optional argument clause of the attribute. */
25896
25897 if (token->type != CPP_OPEN_PAREN)
25898 return attribute;
25899
25900 {
25901 vec<tree, va_gc> *vec;
25902 int attr_flag = normal_attr;
25903
25904 if (attr_ns == gnu_identifier
25905 && attribute_takes_identifier_p (attr_id))
25906 /* A GNU attribute that takes an identifier in parameter. */
25907 attr_flag = id_attr;
25908
25909 vec = cp_parser_parenthesized_expression_list
25910 (parser, attr_flag, /*cast_p=*/false,
25911 /*allow_expansion_p=*/true,
25912 /*non_constant_p=*/NULL);
25913 if (vec == NULL)
25914 arguments = error_mark_node;
25915 else
25916 {
25917 arguments = build_tree_list_vec (vec);
25918 release_tree_vector (vec);
25919 }
25920
25921 if (arguments == error_mark_node)
25922 attribute = error_mark_node;
25923 else
25924 TREE_VALUE (attribute) = arguments;
25925 }
25926
25927 return attribute;
25928 }
25929
25930 /* Check that the attribute ATTRIBUTE appears at most once in the
25931 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25932 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25933 isn't implemented yet in GCC. */
25934
25935 static void
25936 cp_parser_check_std_attribute (tree attributes, tree attribute)
25937 {
25938 if (attributes)
25939 {
25940 tree name = get_attribute_name (attribute);
25941 if (is_attribute_p ("noreturn", name)
25942 && lookup_attribute ("noreturn", attributes))
25943 error ("attribute %<noreturn%> can appear at most once "
25944 "in an attribute-list");
25945 else if (is_attribute_p ("deprecated", name)
25946 && lookup_attribute ("deprecated", attributes))
25947 error ("attribute %<deprecated%> can appear at most once "
25948 "in an attribute-list");
25949 }
25950 }
25951
25952 /* Parse a list of standard C++-11 attributes.
25953
25954 attribute-list:
25955 attribute [opt]
25956 attribute-list , attribute[opt]
25957 attribute ...
25958 attribute-list , attribute ...
25959 */
25960
25961 static tree
25962 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25963 {
25964 tree attributes = NULL_TREE, attribute = NULL_TREE;
25965 cp_token *token = NULL;
25966
25967 while (true)
25968 {
25969 attribute = cp_parser_std_attribute (parser, attr_ns);
25970 if (attribute == error_mark_node)
25971 break;
25972 if (attribute != NULL_TREE)
25973 {
25974 cp_parser_check_std_attribute (attributes, attribute);
25975 TREE_CHAIN (attribute) = attributes;
25976 attributes = attribute;
25977 }
25978 token = cp_lexer_peek_token (parser->lexer);
25979 if (token->type == CPP_ELLIPSIS)
25980 {
25981 cp_lexer_consume_token (parser->lexer);
25982 if (attribute == NULL_TREE)
25983 error_at (token->location,
25984 "expected attribute before %<...%>");
25985 else
25986 {
25987 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25988 if (pack == error_mark_node)
25989 return error_mark_node;
25990 TREE_VALUE (attribute) = pack;
25991 }
25992 token = cp_lexer_peek_token (parser->lexer);
25993 }
25994 if (token->type != CPP_COMMA)
25995 break;
25996 cp_lexer_consume_token (parser->lexer);
25997 }
25998 attributes = nreverse (attributes);
25999 return attributes;
26000 }
26001
26002 /* Parse a standard C++-11 attribute specifier.
26003
26004 attribute-specifier:
26005 [ [ attribute-using-prefix [opt] attribute-list ] ]
26006 alignment-specifier
26007
26008 attribute-using-prefix:
26009 using attribute-namespace :
26010
26011 alignment-specifier:
26012 alignas ( type-id ... [opt] )
26013 alignas ( alignment-expression ... [opt] ). */
26014
26015 static tree
26016 cp_parser_std_attribute_spec (cp_parser *parser)
26017 {
26018 tree attributes = NULL_TREE;
26019 cp_token *token = cp_lexer_peek_token (parser->lexer);
26020
26021 if (token->type == CPP_OPEN_SQUARE
26022 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26023 {
26024 tree attr_ns = NULL_TREE;
26025
26026 cp_lexer_consume_token (parser->lexer);
26027 cp_lexer_consume_token (parser->lexer);
26028
26029 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26030 {
26031 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26032 if (token->type == CPP_NAME)
26033 attr_ns = token->u.value;
26034 else if (token->type == CPP_KEYWORD)
26035 attr_ns = ridpointers[(int) token->keyword];
26036 else if (token->flags & NAMED_OP)
26037 attr_ns = get_identifier (cpp_type2name (token->type,
26038 token->flags));
26039 if (attr_ns
26040 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26041 {
26042 if (cxx_dialect < cxx17
26043 && !in_system_header_at (input_location))
26044 pedwarn (input_location, 0,
26045 "attribute using prefix only available "
26046 "with -std=c++17 or -std=gnu++17");
26047
26048 cp_lexer_consume_token (parser->lexer);
26049 cp_lexer_consume_token (parser->lexer);
26050 cp_lexer_consume_token (parser->lexer);
26051 }
26052 else
26053 attr_ns = NULL_TREE;
26054 }
26055
26056 attributes = cp_parser_std_attribute_list (parser, attr_ns);
26057
26058 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26059 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26060 cp_parser_skip_to_end_of_statement (parser);
26061 else
26062 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26063 when we are sure that we have actually parsed them. */
26064 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26065 }
26066 else
26067 {
26068 tree alignas_expr;
26069
26070 /* Look for an alignment-specifier. */
26071
26072 token = cp_lexer_peek_token (parser->lexer);
26073
26074 if (token->type != CPP_KEYWORD
26075 || token->keyword != RID_ALIGNAS)
26076 return NULL_TREE;
26077
26078 cp_lexer_consume_token (parser->lexer);
26079 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26080
26081 matching_parens parens;
26082 if (!parens.require_open (parser))
26083 return error_mark_node;
26084
26085 cp_parser_parse_tentatively (parser);
26086 alignas_expr = cp_parser_type_id (parser);
26087
26088 if (!cp_parser_parse_definitely (parser))
26089 {
26090 alignas_expr = cp_parser_assignment_expression (parser);
26091 if (alignas_expr == error_mark_node)
26092 cp_parser_skip_to_end_of_statement (parser);
26093 if (alignas_expr == NULL_TREE
26094 || alignas_expr == error_mark_node)
26095 return alignas_expr;
26096 }
26097
26098 alignas_expr = cxx_alignas_expr (alignas_expr);
26099 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26100
26101 /* Handle alignas (pack...). */
26102 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26103 {
26104 cp_lexer_consume_token (parser->lexer);
26105 alignas_expr = make_pack_expansion (alignas_expr);
26106 }
26107
26108 /* Something went wrong, so don't build the attribute. */
26109 if (alignas_expr == error_mark_node)
26110 return error_mark_node;
26111
26112 if (!parens.require_close (parser))
26113 return error_mark_node;
26114
26115 /* Build the C++-11 representation of an 'aligned'
26116 attribute. */
26117 attributes
26118 = build_tree_list (build_tree_list (gnu_identifier,
26119 aligned_identifier), alignas_expr);
26120 }
26121
26122 return attributes;
26123 }
26124
26125 /* Parse a standard C++-11 attribute-specifier-seq.
26126
26127 attribute-specifier-seq:
26128 attribute-specifier-seq [opt] attribute-specifier
26129 */
26130
26131 static tree
26132 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26133 {
26134 tree attr_specs = NULL_TREE;
26135 tree attr_last = NULL_TREE;
26136
26137 /* Don't create wrapper nodes within attributes: the
26138 handlers don't know how to handle them. */
26139 auto_suppress_location_wrappers sentinel;
26140
26141 while (true)
26142 {
26143 tree attr_spec = cp_parser_std_attribute_spec (parser);
26144 if (attr_spec == NULL_TREE)
26145 break;
26146 if (attr_spec == error_mark_node)
26147 return error_mark_node;
26148
26149 if (attr_last)
26150 TREE_CHAIN (attr_last) = attr_spec;
26151 else
26152 attr_specs = attr_last = attr_spec;
26153 attr_last = tree_last (attr_last);
26154 }
26155
26156 return attr_specs;
26157 }
26158
26159 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26160 return index of the first token after balanced-token, or N on failure. */
26161
26162 static size_t
26163 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26164 {
26165 size_t orig_n = n;
26166 int nparens = 0, nbraces = 0, nsquares = 0;
26167 do
26168 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26169 {
26170 case CPP_PRAGMA_EOL:
26171 if (!parser->lexer->in_pragma)
26172 break;
26173 /* FALLTHRU */
26174 case CPP_EOF:
26175 /* Ran out of tokens. */
26176 return orig_n;
26177 case CPP_OPEN_PAREN:
26178 ++nparens;
26179 break;
26180 case CPP_OPEN_BRACE:
26181 ++nbraces;
26182 break;
26183 case CPP_OPEN_SQUARE:
26184 ++nsquares;
26185 break;
26186 case CPP_CLOSE_PAREN:
26187 --nparens;
26188 break;
26189 case CPP_CLOSE_BRACE:
26190 --nbraces;
26191 break;
26192 case CPP_CLOSE_SQUARE:
26193 --nsquares;
26194 break;
26195 default:
26196 break;
26197 }
26198 while (nparens || nbraces || nsquares);
26199 return n;
26200 }
26201
26202 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26203 return index of the first token after the GNU attribute tokens, or N on
26204 failure. */
26205
26206 static size_t
26207 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26208 {
26209 while (true)
26210 {
26211 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26212 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26213 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26214 break;
26215
26216 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26217 if (n2 == n + 2)
26218 break;
26219 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26220 break;
26221 n = n2 + 1;
26222 }
26223 return n;
26224 }
26225
26226 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26227 next token), return index of the first token after the standard C++11
26228 attribute tokens, or N on failure. */
26229
26230 static size_t
26231 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26232 {
26233 while (true)
26234 {
26235 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26236 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26237 {
26238 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26239 if (n2 == n + 1)
26240 break;
26241 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26242 break;
26243 n = n2 + 1;
26244 }
26245 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26246 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26247 {
26248 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26249 if (n2 == n + 1)
26250 break;
26251 n = n2;
26252 }
26253 else
26254 break;
26255 }
26256 return n;
26257 }
26258
26259 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26260 as the next token), return index of the first token after the attribute
26261 tokens, or N on failure. */
26262
26263 static size_t
26264 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26265 {
26266 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26267 return cp_parser_skip_gnu_attributes_opt (parser, n);
26268 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26269 }
26270
26271 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26272 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26273 current value of the PEDANTIC flag, regardless of whether or not
26274 the `__extension__' keyword is present. The caller is responsible
26275 for restoring the value of the PEDANTIC flag. */
26276
26277 static bool
26278 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26279 {
26280 /* Save the old value of the PEDANTIC flag. */
26281 *saved_pedantic = pedantic;
26282
26283 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26284 {
26285 /* Consume the `__extension__' token. */
26286 cp_lexer_consume_token (parser->lexer);
26287 /* We're not being pedantic while the `__extension__' keyword is
26288 in effect. */
26289 pedantic = 0;
26290
26291 return true;
26292 }
26293
26294 return false;
26295 }
26296
26297 /* Parse a label declaration.
26298
26299 label-declaration:
26300 __label__ label-declarator-seq ;
26301
26302 label-declarator-seq:
26303 identifier , label-declarator-seq
26304 identifier */
26305
26306 static void
26307 cp_parser_label_declaration (cp_parser* parser)
26308 {
26309 /* Look for the `__label__' keyword. */
26310 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26311
26312 while (true)
26313 {
26314 tree identifier;
26315
26316 /* Look for an identifier. */
26317 identifier = cp_parser_identifier (parser);
26318 /* If we failed, stop. */
26319 if (identifier == error_mark_node)
26320 break;
26321 /* Declare it as a label. */
26322 finish_label_decl (identifier);
26323 /* If the next token is a `;', stop. */
26324 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26325 break;
26326 /* Look for the `,' separating the label declarations. */
26327 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26328 }
26329
26330 /* Look for the final `;'. */
26331 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26332 }
26333
26334 // -------------------------------------------------------------------------- //
26335 // Requires Clause
26336
26337 // Parse a requires clause.
26338 //
26339 // requires-clause:
26340 // 'requires' logical-or-expression
26341 //
26342 // The required logical-or-expression must be a constant expression. Note
26343 // that we don't check that the expression is constepxr here. We defer until
26344 // we analyze constraints and then, we only check atomic constraints.
26345 static tree
26346 cp_parser_requires_clause (cp_parser *parser)
26347 {
26348 // Parse the requires clause so that it is not automatically folded.
26349 ++processing_template_decl;
26350 tree expr = cp_parser_binary_expression (parser, false, false,
26351 PREC_NOT_OPERATOR, NULL);
26352 if (check_for_bare_parameter_packs (expr))
26353 expr = error_mark_node;
26354 --processing_template_decl;
26355 return expr;
26356 }
26357
26358 // Optionally parse a requires clause:
26359 static tree
26360 cp_parser_requires_clause_opt (cp_parser *parser)
26361 {
26362 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26363 if (tok->keyword != RID_REQUIRES)
26364 {
26365 if (!flag_concepts && tok->type == CPP_NAME
26366 && tok->u.value == ridpointers[RID_REQUIRES])
26367 {
26368 error_at (cp_lexer_peek_token (parser->lexer)->location,
26369 "%<requires%> only available with -fconcepts");
26370 /* Parse and discard the requires-clause. */
26371 cp_lexer_consume_token (parser->lexer);
26372 cp_parser_requires_clause (parser);
26373 }
26374 return NULL_TREE;
26375 }
26376 cp_lexer_consume_token (parser->lexer);
26377 return cp_parser_requires_clause (parser);
26378 }
26379
26380
26381 /*---------------------------------------------------------------------------
26382 Requires expressions
26383 ---------------------------------------------------------------------------*/
26384
26385 /* Parse a requires expression
26386
26387 requirement-expression:
26388 'requires' requirement-parameter-list [opt] requirement-body */
26389 static tree
26390 cp_parser_requires_expression (cp_parser *parser)
26391 {
26392 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26393 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26394
26395 /* A requires-expression shall appear only within a concept
26396 definition or a requires-clause.
26397
26398 TODO: Implement this diagnostic correctly. */
26399 if (!processing_template_decl)
26400 {
26401 error_at (loc, "a requires expression cannot appear outside a template");
26402 cp_parser_skip_to_end_of_statement (parser);
26403 return error_mark_node;
26404 }
26405
26406 tree parms, reqs;
26407 {
26408 /* Local parameters are delared as variables within the scope
26409 of the expression. They are not visible past the end of
26410 the expression. Expressions within the requires-expression
26411 are unevaluated. */
26412 struct scope_sentinel
26413 {
26414 scope_sentinel ()
26415 {
26416 ++cp_unevaluated_operand;
26417 begin_scope (sk_block, NULL_TREE);
26418 }
26419
26420 ~scope_sentinel ()
26421 {
26422 pop_bindings_and_leave_scope ();
26423 --cp_unevaluated_operand;
26424 }
26425 } s;
26426
26427 /* Parse the optional parameter list. */
26428 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26429 {
26430 parms = cp_parser_requirement_parameter_list (parser);
26431 if (parms == error_mark_node)
26432 return error_mark_node;
26433 }
26434 else
26435 parms = NULL_TREE;
26436
26437 /* Parse the requirement body. */
26438 reqs = cp_parser_requirement_body (parser);
26439 if (reqs == error_mark_node)
26440 return error_mark_node;
26441 }
26442
26443 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26444 the parm chain. */
26445 grokparms (parms, &parms);
26446 return finish_requires_expr (parms, reqs);
26447 }
26448
26449 /* Parse a parameterized requirement.
26450
26451 requirement-parameter-list:
26452 '(' parameter-declaration-clause ')' */
26453 static tree
26454 cp_parser_requirement_parameter_list (cp_parser *parser)
26455 {
26456 matching_parens parens;
26457 if (!parens.require_open (parser))
26458 return error_mark_node;
26459
26460 tree parms
26461 = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
26462
26463 if (!parens.require_close (parser))
26464 return error_mark_node;
26465
26466 return parms;
26467 }
26468
26469 /* Parse the body of a requirement.
26470
26471 requirement-body:
26472 '{' requirement-list '}' */
26473 static tree
26474 cp_parser_requirement_body (cp_parser *parser)
26475 {
26476 matching_braces braces;
26477 if (!braces.require_open (parser))
26478 return error_mark_node;
26479
26480 tree reqs = cp_parser_requirement_list (parser);
26481
26482 if (!braces.require_close (parser))
26483 return error_mark_node;
26484
26485 return reqs;
26486 }
26487
26488 /* Parse a list of requirements.
26489
26490 requirement-list:
26491 requirement
26492 requirement-list ';' requirement[opt] */
26493 static tree
26494 cp_parser_requirement_list (cp_parser *parser)
26495 {
26496 tree result = NULL_TREE;
26497 while (true)
26498 {
26499 tree req = cp_parser_requirement (parser);
26500 if (req == error_mark_node)
26501 return error_mark_node;
26502
26503 result = tree_cons (NULL_TREE, req, result);
26504
26505 /* If we see a semi-colon, consume it. */
26506 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26507 cp_lexer_consume_token (parser->lexer);
26508
26509 /* Stop processing at the end of the list. */
26510 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26511 break;
26512 }
26513
26514 /* Reverse the order of requirements so they are analyzed in
26515 declaration order. */
26516 return nreverse (result);
26517 }
26518
26519 /* Parse a syntactic requirement or type requirement.
26520
26521 requirement:
26522 simple-requirement
26523 compound-requirement
26524 type-requirement
26525 nested-requirement */
26526 static tree
26527 cp_parser_requirement (cp_parser *parser)
26528 {
26529 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26530 return cp_parser_compound_requirement (parser);
26531 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26532 return cp_parser_type_requirement (parser);
26533 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26534 return cp_parser_nested_requirement (parser);
26535 else
26536 return cp_parser_simple_requirement (parser);
26537 }
26538
26539 /* Parse a simple requirement.
26540
26541 simple-requirement:
26542 expression ';' */
26543 static tree
26544 cp_parser_simple_requirement (cp_parser *parser)
26545 {
26546 tree expr = cp_parser_expression (parser, NULL, false, false);
26547 if (!expr || expr == error_mark_node)
26548 return error_mark_node;
26549
26550 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26551 return error_mark_node;
26552
26553 return finish_simple_requirement (expr);
26554 }
26555
26556 /* Parse a type requirement
26557
26558 type-requirement
26559 nested-name-specifier [opt] required-type-name ';'
26560
26561 required-type-name:
26562 type-name
26563 'template' [opt] simple-template-id */
26564 static tree
26565 cp_parser_type_requirement (cp_parser *parser)
26566 {
26567 cp_lexer_consume_token (parser->lexer);
26568
26569 // Save the scope before parsing name specifiers.
26570 tree saved_scope = parser->scope;
26571 tree saved_object_scope = parser->object_scope;
26572 tree saved_qualifying_scope = parser->qualifying_scope;
26573 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26574 cp_parser_nested_name_specifier_opt (parser,
26575 /*typename_keyword_p=*/true,
26576 /*check_dependency_p=*/false,
26577 /*type_p=*/true,
26578 /*is_declaration=*/false);
26579
26580 tree type;
26581 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26582 {
26583 cp_lexer_consume_token (parser->lexer);
26584 type = cp_parser_template_id (parser,
26585 /*template_keyword_p=*/true,
26586 /*check_dependency=*/false,
26587 /*tag_type=*/none_type,
26588 /*is_declaration=*/false);
26589 type = make_typename_type (parser->scope, type, typename_type,
26590 /*complain=*/tf_error);
26591 }
26592 else
26593 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26594
26595 if (TREE_CODE (type) == TYPE_DECL)
26596 type = TREE_TYPE (type);
26597
26598 parser->scope = saved_scope;
26599 parser->object_scope = saved_object_scope;
26600 parser->qualifying_scope = saved_qualifying_scope;
26601
26602 if (type == error_mark_node)
26603 cp_parser_skip_to_end_of_statement (parser);
26604
26605 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26606 return error_mark_node;
26607 if (type == error_mark_node)
26608 return error_mark_node;
26609
26610 return finish_type_requirement (type);
26611 }
26612
26613 /* Parse a compound requirement
26614
26615 compound-requirement:
26616 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26617 static tree
26618 cp_parser_compound_requirement (cp_parser *parser)
26619 {
26620 /* Parse an expression enclosed in '{ }'s. */
26621 matching_braces braces;
26622 if (!braces.require_open (parser))
26623 return error_mark_node;
26624
26625 tree expr = cp_parser_expression (parser, NULL, false, false);
26626 if (!expr || expr == error_mark_node)
26627 return error_mark_node;
26628
26629 if (!braces.require_close (parser))
26630 return error_mark_node;
26631
26632 /* Parse the optional noexcept. */
26633 bool noexcept_p = false;
26634 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26635 {
26636 cp_lexer_consume_token (parser->lexer);
26637 noexcept_p = true;
26638 }
26639
26640 /* Parse the optional trailing return type. */
26641 tree type = NULL_TREE;
26642 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26643 {
26644 cp_lexer_consume_token (parser->lexer);
26645 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26646 parser->in_result_type_constraint_p = true;
26647 type = cp_parser_trailing_type_id (parser);
26648 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26649 if (type == error_mark_node)
26650 return error_mark_node;
26651 }
26652
26653 return finish_compound_requirement (expr, type, noexcept_p);
26654 }
26655
26656 /* Parse a nested requirement. This is the same as a requires clause.
26657
26658 nested-requirement:
26659 requires-clause */
26660 static tree
26661 cp_parser_nested_requirement (cp_parser *parser)
26662 {
26663 cp_lexer_consume_token (parser->lexer);
26664 tree req = cp_parser_requires_clause (parser);
26665 if (req == error_mark_node)
26666 return error_mark_node;
26667 return finish_nested_requirement (req);
26668 }
26669
26670 /* Support Functions */
26671
26672 /* Return the appropriate prefer_type argument for lookup_name_real based on
26673 tag_type and template_mem_access. */
26674
26675 static inline int
26676 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26677 {
26678 /* DR 141: When looking in the current enclosing context for a template-name
26679 after -> or ., only consider class templates. */
26680 if (template_mem_access)
26681 return 2;
26682 switch (tag_type)
26683 {
26684 case none_type: return 0; // No preference.
26685 case scope_type: return 1; // Type or namespace.
26686 default: return 2; // Type only.
26687 }
26688 }
26689
26690 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26691 NAME should have one of the representations used for an
26692 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26693 is returned. If PARSER->SCOPE is a dependent type, then a
26694 SCOPE_REF is returned.
26695
26696 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26697 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26698 was formed. Abstractly, such entities should not be passed to this
26699 function, because they do not need to be looked up, but it is
26700 simpler to check for this special case here, rather than at the
26701 call-sites.
26702
26703 In cases not explicitly covered above, this function returns a
26704 DECL, OVERLOAD, or baselink representing the result of the lookup.
26705 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26706 is returned.
26707
26708 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26709 (e.g., "struct") that was used. In that case bindings that do not
26710 refer to types are ignored.
26711
26712 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26713 ignored.
26714
26715 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26716 are ignored.
26717
26718 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26719 types.
26720
26721 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26722 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26723 NULL_TREE otherwise. */
26724
26725 static cp_expr
26726 cp_parser_lookup_name (cp_parser *parser, tree name,
26727 enum tag_types tag_type,
26728 bool is_template,
26729 bool is_namespace,
26730 bool check_dependency,
26731 tree *ambiguous_decls,
26732 location_t name_location)
26733 {
26734 tree decl;
26735 tree object_type = parser->context->object_type;
26736
26737 /* Assume that the lookup will be unambiguous. */
26738 if (ambiguous_decls)
26739 *ambiguous_decls = NULL_TREE;
26740
26741 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26742 no longer valid. Note that if we are parsing tentatively, and
26743 the parse fails, OBJECT_TYPE will be automatically restored. */
26744 parser->context->object_type = NULL_TREE;
26745
26746 if (name == error_mark_node)
26747 return error_mark_node;
26748
26749 /* A template-id has already been resolved; there is no lookup to
26750 do. */
26751 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26752 return name;
26753 if (BASELINK_P (name))
26754 {
26755 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26756 == TEMPLATE_ID_EXPR);
26757 return name;
26758 }
26759
26760 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26761 it should already have been checked to make sure that the name
26762 used matches the type being destroyed. */
26763 if (TREE_CODE (name) == BIT_NOT_EXPR)
26764 {
26765 tree type;
26766
26767 /* Figure out to which type this destructor applies. */
26768 if (parser->scope)
26769 type = parser->scope;
26770 else if (object_type)
26771 type = object_type;
26772 else
26773 type = current_class_type;
26774 /* If that's not a class type, there is no destructor. */
26775 if (!type || !CLASS_TYPE_P (type))
26776 return error_mark_node;
26777
26778 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26779 lazily_declare_fn (sfk_destructor, type);
26780
26781 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26782 return dtor;
26783
26784 return error_mark_node;
26785 }
26786
26787 /* By this point, the NAME should be an ordinary identifier. If
26788 the id-expression was a qualified name, the qualifying scope is
26789 stored in PARSER->SCOPE at this point. */
26790 gcc_assert (identifier_p (name));
26791
26792 /* Perform the lookup. */
26793 if (parser->scope)
26794 {
26795 bool dependent_p;
26796
26797 if (parser->scope == error_mark_node)
26798 return error_mark_node;
26799
26800 /* If the SCOPE is dependent, the lookup must be deferred until
26801 the template is instantiated -- unless we are explicitly
26802 looking up names in uninstantiated templates. Even then, we
26803 cannot look up the name if the scope is not a class type; it
26804 might, for example, be a template type parameter. */
26805 dependent_p = (TYPE_P (parser->scope)
26806 && dependent_scope_p (parser->scope));
26807 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26808 && dependent_p)
26809 /* Defer lookup. */
26810 decl = error_mark_node;
26811 else
26812 {
26813 tree pushed_scope = NULL_TREE;
26814
26815 /* If PARSER->SCOPE is a dependent type, then it must be a
26816 class type, and we must not be checking dependencies;
26817 otherwise, we would have processed this lookup above. So
26818 that PARSER->SCOPE is not considered a dependent base by
26819 lookup_member, we must enter the scope here. */
26820 if (dependent_p)
26821 pushed_scope = push_scope (parser->scope);
26822
26823 /* If the PARSER->SCOPE is a template specialization, it
26824 may be instantiated during name lookup. In that case,
26825 errors may be issued. Even if we rollback the current
26826 tentative parse, those errors are valid. */
26827 decl = lookup_qualified_name (parser->scope, name,
26828 prefer_type_arg (tag_type),
26829 /*complain=*/true);
26830
26831 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26832 lookup result and the nested-name-specifier nominates a class C:
26833 * if the name specified after the nested-name-specifier, when
26834 looked up in C, is the injected-class-name of C (Clause 9), or
26835 * if the name specified after the nested-name-specifier is the
26836 same as the identifier or the simple-template-id's template-
26837 name in the last component of the nested-name-specifier,
26838 the name is instead considered to name the constructor of
26839 class C. [ Note: for example, the constructor is not an
26840 acceptable lookup result in an elaborated-type-specifier so
26841 the constructor would not be used in place of the
26842 injected-class-name. --end note ] Such a constructor name
26843 shall be used only in the declarator-id of a declaration that
26844 names a constructor or in a using-declaration. */
26845 if (tag_type == none_type
26846 && DECL_SELF_REFERENCE_P (decl)
26847 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26848 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26849 prefer_type_arg (tag_type),
26850 /*complain=*/true);
26851
26852 /* If we have a single function from a using decl, pull it out. */
26853 if (TREE_CODE (decl) == OVERLOAD
26854 && !really_overloaded_fn (decl))
26855 decl = OVL_FUNCTION (decl);
26856
26857 if (pushed_scope)
26858 pop_scope (pushed_scope);
26859 }
26860
26861 /* If the scope is a dependent type and either we deferred lookup or
26862 we did lookup but didn't find the name, rememeber the name. */
26863 if (decl == error_mark_node && TYPE_P (parser->scope)
26864 && dependent_type_p (parser->scope))
26865 {
26866 if (tag_type)
26867 {
26868 tree type;
26869
26870 /* The resolution to Core Issue 180 says that `struct
26871 A::B' should be considered a type-name, even if `A'
26872 is dependent. */
26873 type = make_typename_type (parser->scope, name, tag_type,
26874 /*complain=*/tf_error);
26875 if (type != error_mark_node)
26876 decl = TYPE_NAME (type);
26877 }
26878 else if (is_template
26879 && (cp_parser_next_token_ends_template_argument_p (parser)
26880 || cp_lexer_next_token_is (parser->lexer,
26881 CPP_CLOSE_PAREN)))
26882 decl = make_unbound_class_template (parser->scope,
26883 name, NULL_TREE,
26884 /*complain=*/tf_error);
26885 else
26886 decl = build_qualified_name (/*type=*/NULL_TREE,
26887 parser->scope, name,
26888 is_template);
26889 }
26890 parser->qualifying_scope = parser->scope;
26891 parser->object_scope = NULL_TREE;
26892 }
26893 else if (object_type)
26894 {
26895 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26896 OBJECT_TYPE is not a class. */
26897 if (CLASS_TYPE_P (object_type))
26898 /* If the OBJECT_TYPE is a template specialization, it may
26899 be instantiated during name lookup. In that case, errors
26900 may be issued. Even if we rollback the current tentative
26901 parse, those errors are valid. */
26902 decl = lookup_member (object_type,
26903 name,
26904 /*protect=*/0,
26905 prefer_type_arg (tag_type),
26906 tf_warning_or_error);
26907 else
26908 decl = NULL_TREE;
26909
26910 if (!decl)
26911 /* Look it up in the enclosing context. DR 141: When looking for a
26912 template-name after -> or ., only consider class templates. */
26913 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26914 /*nonclass=*/0,
26915 /*block_p=*/true, is_namespace, 0);
26916 if (object_type == unknown_type_node)
26917 /* The object is type-dependent, so we can't look anything up; we used
26918 this to get the DR 141 behavior. */
26919 object_type = NULL_TREE;
26920 parser->object_scope = object_type;
26921 parser->qualifying_scope = NULL_TREE;
26922 }
26923 else
26924 {
26925 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26926 /*nonclass=*/0,
26927 /*block_p=*/true, is_namespace, 0);
26928 parser->qualifying_scope = NULL_TREE;
26929 parser->object_scope = NULL_TREE;
26930 }
26931
26932 /* If the lookup failed, let our caller know. */
26933 if (!decl || decl == error_mark_node)
26934 return error_mark_node;
26935
26936 /* Pull out the template from an injected-class-name (or multiple). */
26937 if (is_template)
26938 decl = maybe_get_template_decl_from_type_decl (decl);
26939
26940 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26941 if (TREE_CODE (decl) == TREE_LIST)
26942 {
26943 if (ambiguous_decls)
26944 *ambiguous_decls = decl;
26945 /* The error message we have to print is too complicated for
26946 cp_parser_error, so we incorporate its actions directly. */
26947 if (!cp_parser_simulate_error (parser))
26948 {
26949 error_at (name_location, "reference to %qD is ambiguous",
26950 name);
26951 print_candidates (decl);
26952 }
26953 return error_mark_node;
26954 }
26955
26956 gcc_assert (DECL_P (decl)
26957 || TREE_CODE (decl) == OVERLOAD
26958 || TREE_CODE (decl) == SCOPE_REF
26959 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26960 || BASELINK_P (decl));
26961
26962 /* If we have resolved the name of a member declaration, check to
26963 see if the declaration is accessible. When the name resolves to
26964 set of overloaded functions, accessibility is checked when
26965 overload resolution is done.
26966
26967 During an explicit instantiation, access is not checked at all,
26968 as per [temp.explicit]. */
26969 if (DECL_P (decl))
26970 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26971
26972 maybe_record_typedef_use (decl);
26973
26974 return cp_expr (decl, name_location);
26975 }
26976
26977 /* Like cp_parser_lookup_name, but for use in the typical case where
26978 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26979 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26980
26981 static tree
26982 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26983 {
26984 return cp_parser_lookup_name (parser, name,
26985 none_type,
26986 /*is_template=*/false,
26987 /*is_namespace=*/false,
26988 /*check_dependency=*/true,
26989 /*ambiguous_decls=*/NULL,
26990 location);
26991 }
26992
26993 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26994 the current context, return the TYPE_DECL. If TAG_NAME_P is
26995 true, the DECL indicates the class being defined in a class-head,
26996 or declared in an elaborated-type-specifier.
26997
26998 Otherwise, return DECL. */
26999
27000 static tree
27001 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
27002 {
27003 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27004 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27005
27006 struct A {
27007 template <typename T> struct B;
27008 };
27009
27010 template <typename T> struct A::B {};
27011
27012 Similarly, in an elaborated-type-specifier:
27013
27014 namespace N { struct X{}; }
27015
27016 struct A {
27017 template <typename T> friend struct N::X;
27018 };
27019
27020 However, if the DECL refers to a class type, and we are in
27021 the scope of the class, then the name lookup automatically
27022 finds the TYPE_DECL created by build_self_reference rather
27023 than a TEMPLATE_DECL. For example, in:
27024
27025 template <class T> struct S {
27026 S s;
27027 };
27028
27029 there is no need to handle such case. */
27030
27031 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
27032 return DECL_TEMPLATE_RESULT (decl);
27033
27034 return decl;
27035 }
27036
27037 /* If too many, or too few, template-parameter lists apply to the
27038 declarator, issue an error message. Returns TRUE if all went well,
27039 and FALSE otherwise. */
27040
27041 static bool
27042 cp_parser_check_declarator_template_parameters (cp_parser* parser,
27043 cp_declarator *declarator,
27044 location_t declarator_location)
27045 {
27046 switch (declarator->kind)
27047 {
27048 case cdk_id:
27049 {
27050 unsigned num_templates = 0;
27051 tree scope = declarator->u.id.qualifying_scope;
27052 bool template_id_p = false;
27053
27054 if (scope)
27055 num_templates = num_template_headers_for_class (scope);
27056 else if (TREE_CODE (declarator->u.id.unqualified_name)
27057 == TEMPLATE_ID_EXPR)
27058 {
27059 /* If the DECLARATOR has the form `X<y>' then it uses one
27060 additional level of template parameters. */
27061 ++num_templates;
27062 template_id_p = true;
27063 }
27064
27065 return cp_parser_check_template_parameters
27066 (parser, num_templates, template_id_p, declarator_location,
27067 declarator);
27068 }
27069
27070 case cdk_function:
27071 case cdk_array:
27072 case cdk_pointer:
27073 case cdk_reference:
27074 case cdk_ptrmem:
27075 return (cp_parser_check_declarator_template_parameters
27076 (parser, declarator->declarator, declarator_location));
27077
27078 case cdk_decomp:
27079 case cdk_error:
27080 return true;
27081
27082 default:
27083 gcc_unreachable ();
27084 }
27085 return false;
27086 }
27087
27088 /* NUM_TEMPLATES were used in the current declaration. If that is
27089 invalid, return FALSE and issue an error messages. Otherwise,
27090 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27091 declarator and we can print more accurate diagnostics. */
27092
27093 static bool
27094 cp_parser_check_template_parameters (cp_parser* parser,
27095 unsigned num_templates,
27096 bool template_id_p,
27097 location_t location,
27098 cp_declarator *declarator)
27099 {
27100 /* If there are the same number of template classes and parameter
27101 lists, that's OK. */
27102 if (parser->num_template_parameter_lists == num_templates)
27103 return true;
27104 /* If there are more, but only one more, and the name ends in an identifier,
27105 then we are declaring a primary template. That's OK too. */
27106 if (!template_id_p
27107 && parser->num_template_parameter_lists == num_templates + 1)
27108 return true;
27109 /* If there are more template classes than parameter lists, we have
27110 something like:
27111
27112 template <class T> void S<T>::R<T>::f (); */
27113 if (parser->num_template_parameter_lists < num_templates)
27114 {
27115 if (declarator && !current_function_decl)
27116 error_at (location, "specializing member %<%T::%E%> "
27117 "requires %<template<>%> syntax",
27118 declarator->u.id.qualifying_scope,
27119 declarator->u.id.unqualified_name);
27120 else if (declarator)
27121 error_at (location, "invalid declaration of %<%T::%E%>",
27122 declarator->u.id.qualifying_scope,
27123 declarator->u.id.unqualified_name);
27124 else
27125 error_at (location, "too few template-parameter-lists");
27126 return false;
27127 }
27128 /* Otherwise, there are too many template parameter lists. We have
27129 something like:
27130
27131 template <class T> template <class U> void S::f(); */
27132 error_at (location, "too many template-parameter-lists");
27133 return false;
27134 }
27135
27136 /* Parse an optional `::' token indicating that the following name is
27137 from the global namespace. If so, PARSER->SCOPE is set to the
27138 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27139 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27140 Returns the new value of PARSER->SCOPE, if the `::' token is
27141 present, and NULL_TREE otherwise. */
27142
27143 static tree
27144 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
27145 {
27146 cp_token *token;
27147
27148 /* Peek at the next token. */
27149 token = cp_lexer_peek_token (parser->lexer);
27150 /* If we're looking at a `::' token then we're starting from the
27151 global namespace, not our current location. */
27152 if (token->type == CPP_SCOPE)
27153 {
27154 /* Consume the `::' token. */
27155 cp_lexer_consume_token (parser->lexer);
27156 /* Set the SCOPE so that we know where to start the lookup. */
27157 parser->scope = global_namespace;
27158 parser->qualifying_scope = global_namespace;
27159 parser->object_scope = NULL_TREE;
27160
27161 return parser->scope;
27162 }
27163 else if (!current_scope_valid_p)
27164 {
27165 parser->scope = NULL_TREE;
27166 parser->qualifying_scope = NULL_TREE;
27167 parser->object_scope = NULL_TREE;
27168 }
27169
27170 return NULL_TREE;
27171 }
27172
27173 /* Returns TRUE if the upcoming token sequence is the start of a
27174 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27175 declarator is preceded by the `friend' specifier. */
27176
27177 static bool
27178 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
27179 {
27180 bool constructor_p;
27181 bool outside_class_specifier_p;
27182 tree nested_name_specifier;
27183 cp_token *next_token;
27184
27185 /* The common case is that this is not a constructor declarator, so
27186 try to avoid doing lots of work if at all possible. It's not
27187 valid declare a constructor at function scope. */
27188 if (parser->in_function_body)
27189 return false;
27190 /* And only certain tokens can begin a constructor declarator. */
27191 next_token = cp_lexer_peek_token (parser->lexer);
27192 if (next_token->type != CPP_NAME
27193 && next_token->type != CPP_SCOPE
27194 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27195 && next_token->type != CPP_TEMPLATE_ID)
27196 return false;
27197
27198 /* Parse tentatively; we are going to roll back all of the tokens
27199 consumed here. */
27200 cp_parser_parse_tentatively (parser);
27201 /* Assume that we are looking at a constructor declarator. */
27202 constructor_p = true;
27203
27204 /* Look for the optional `::' operator. */
27205 cp_parser_global_scope_opt (parser,
27206 /*current_scope_valid_p=*/false);
27207 /* Look for the nested-name-specifier. */
27208 nested_name_specifier
27209 = (cp_parser_nested_name_specifier_opt (parser,
27210 /*typename_keyword_p=*/false,
27211 /*check_dependency_p=*/false,
27212 /*type_p=*/false,
27213 /*is_declaration=*/false));
27214
27215 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27216 if (nested_name_specifier
27217 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
27218 {
27219 tree s = resolve_typename_type (nested_name_specifier,
27220 /*only_current_p=*/false);
27221 if (TREE_CODE (s) != TYPENAME_TYPE)
27222 nested_name_specifier = s;
27223 }
27224
27225 outside_class_specifier_p = (!at_class_scope_p ()
27226 || !TYPE_BEING_DEFINED (current_class_type)
27227 || friend_p);
27228
27229 /* Outside of a class-specifier, there must be a
27230 nested-name-specifier. Except in C++17 mode, where we
27231 might be declaring a guiding declaration. */
27232 if (!nested_name_specifier && outside_class_specifier_p
27233 && cxx_dialect < cxx17)
27234 constructor_p = false;
27235 else if (nested_name_specifier == error_mark_node)
27236 constructor_p = false;
27237
27238 /* If we have a class scope, this is easy; DR 147 says that S::S always
27239 names the constructor, and no other qualified name could. */
27240 if (constructor_p && nested_name_specifier
27241 && CLASS_TYPE_P (nested_name_specifier))
27242 {
27243 tree id = cp_parser_unqualified_id (parser,
27244 /*template_keyword_p=*/false,
27245 /*check_dependency_p=*/false,
27246 /*declarator_p=*/true,
27247 /*optional_p=*/false);
27248 if (is_overloaded_fn (id))
27249 id = DECL_NAME (get_first_fn (id));
27250 if (!constructor_name_p (id, nested_name_specifier))
27251 constructor_p = false;
27252 }
27253 /* If we still think that this might be a constructor-declarator,
27254 look for a class-name. */
27255 else if (constructor_p)
27256 {
27257 /* If we have:
27258
27259 template <typename T> struct S {
27260 S();
27261 };
27262
27263 we must recognize that the nested `S' names a class. */
27264 if (cxx_dialect >= cxx17)
27265 cp_parser_parse_tentatively (parser);
27266
27267 tree type_decl;
27268 type_decl = cp_parser_class_name (parser,
27269 /*typename_keyword_p=*/false,
27270 /*template_keyword_p=*/false,
27271 none_type,
27272 /*check_dependency_p=*/false,
27273 /*class_head_p=*/false,
27274 /*is_declaration=*/false);
27275
27276 if (cxx_dialect >= cxx17
27277 && !cp_parser_parse_definitely (parser))
27278 {
27279 type_decl = NULL_TREE;
27280 tree tmpl = cp_parser_template_name (parser,
27281 /*template_keyword*/false,
27282 /*check_dependency_p*/false,
27283 /*is_declaration*/false,
27284 none_type,
27285 /*is_identifier*/NULL);
27286 if (DECL_CLASS_TEMPLATE_P (tmpl)
27287 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27288 /* It's a deduction guide, return true. */;
27289 else
27290 cp_parser_simulate_error (parser);
27291 }
27292
27293 /* If there was no class-name, then this is not a constructor.
27294 Otherwise, if we are in a class-specifier and we aren't
27295 handling a friend declaration, check that its type matches
27296 current_class_type (c++/38313). Note: error_mark_node
27297 is left alone for error recovery purposes. */
27298 constructor_p = (!cp_parser_error_occurred (parser)
27299 && (outside_class_specifier_p
27300 || type_decl == NULL_TREE
27301 || type_decl == error_mark_node
27302 || same_type_p (current_class_type,
27303 TREE_TYPE (type_decl))));
27304
27305 /* If we're still considering a constructor, we have to see a `(',
27306 to begin the parameter-declaration-clause, followed by either a
27307 `)', an `...', or a decl-specifier. We need to check for a
27308 type-specifier to avoid being fooled into thinking that:
27309
27310 S (f) (int);
27311
27312 is a constructor. (It is actually a function named `f' that
27313 takes one parameter (of type `int') and returns a value of type
27314 `S'. */
27315 if (constructor_p
27316 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27317 constructor_p = false;
27318
27319 if (constructor_p
27320 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27321 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27322 /* A parameter declaration begins with a decl-specifier,
27323 which is either the "attribute" keyword, a storage class
27324 specifier, or (usually) a type-specifier. */
27325 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27326 {
27327 tree type;
27328 tree pushed_scope = NULL_TREE;
27329 unsigned saved_num_template_parameter_lists;
27330
27331 /* Names appearing in the type-specifier should be looked up
27332 in the scope of the class. */
27333 if (current_class_type)
27334 type = NULL_TREE;
27335 else if (type_decl)
27336 {
27337 type = TREE_TYPE (type_decl);
27338 if (TREE_CODE (type) == TYPENAME_TYPE)
27339 {
27340 type = resolve_typename_type (type,
27341 /*only_current_p=*/false);
27342 if (TREE_CODE (type) == TYPENAME_TYPE)
27343 {
27344 cp_parser_abort_tentative_parse (parser);
27345 return false;
27346 }
27347 }
27348 pushed_scope = push_scope (type);
27349 }
27350
27351 /* Inside the constructor parameter list, surrounding
27352 template-parameter-lists do not apply. */
27353 saved_num_template_parameter_lists
27354 = parser->num_template_parameter_lists;
27355 parser->num_template_parameter_lists = 0;
27356
27357 /* Look for the type-specifier. */
27358 cp_parser_type_specifier (parser,
27359 CP_PARSER_FLAGS_NONE,
27360 /*decl_specs=*/NULL,
27361 /*is_declarator=*/true,
27362 /*declares_class_or_enum=*/NULL,
27363 /*is_cv_qualifier=*/NULL);
27364
27365 parser->num_template_parameter_lists
27366 = saved_num_template_parameter_lists;
27367
27368 /* Leave the scope of the class. */
27369 if (pushed_scope)
27370 pop_scope (pushed_scope);
27371
27372 constructor_p = !cp_parser_error_occurred (parser);
27373 }
27374 }
27375
27376 /* We did not really want to consume any tokens. */
27377 cp_parser_abort_tentative_parse (parser);
27378
27379 return constructor_p;
27380 }
27381
27382 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27383 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27384 they must be performed once we are in the scope of the function.
27385
27386 Returns the function defined. */
27387
27388 static tree
27389 cp_parser_function_definition_from_specifiers_and_declarator
27390 (cp_parser* parser,
27391 cp_decl_specifier_seq *decl_specifiers,
27392 tree attributes,
27393 const cp_declarator *declarator)
27394 {
27395 tree fn;
27396 bool success_p;
27397
27398 /* Begin the function-definition. */
27399 success_p = start_function (decl_specifiers, declarator, attributes);
27400
27401 /* The things we're about to see are not directly qualified by any
27402 template headers we've seen thus far. */
27403 reset_specialization ();
27404
27405 /* If there were names looked up in the decl-specifier-seq that we
27406 did not check, check them now. We must wait until we are in the
27407 scope of the function to perform the checks, since the function
27408 might be a friend. */
27409 perform_deferred_access_checks (tf_warning_or_error);
27410
27411 if (success_p)
27412 {
27413 cp_finalize_omp_declare_simd (parser, current_function_decl);
27414 parser->omp_declare_simd = NULL;
27415 cp_finalize_oacc_routine (parser, current_function_decl, true);
27416 parser->oacc_routine = NULL;
27417 }
27418
27419 if (!success_p)
27420 {
27421 /* Skip the entire function. */
27422 cp_parser_skip_to_end_of_block_or_statement (parser);
27423 fn = error_mark_node;
27424 }
27425 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27426 {
27427 /* Seen already, skip it. An error message has already been output. */
27428 cp_parser_skip_to_end_of_block_or_statement (parser);
27429 fn = current_function_decl;
27430 current_function_decl = NULL_TREE;
27431 /* If this is a function from a class, pop the nested class. */
27432 if (current_class_name)
27433 pop_nested_class ();
27434 }
27435 else
27436 {
27437 timevar_id_t tv;
27438 if (DECL_DECLARED_INLINE_P (current_function_decl))
27439 tv = TV_PARSE_INLINE;
27440 else
27441 tv = TV_PARSE_FUNC;
27442 timevar_push (tv);
27443 fn = cp_parser_function_definition_after_declarator (parser,
27444 /*inline_p=*/false);
27445 timevar_pop (tv);
27446 }
27447
27448 return fn;
27449 }
27450
27451 /* Parse the part of a function-definition that follows the
27452 declarator. INLINE_P is TRUE iff this function is an inline
27453 function defined within a class-specifier.
27454
27455 Returns the function defined. */
27456
27457 static tree
27458 cp_parser_function_definition_after_declarator (cp_parser* parser,
27459 bool inline_p)
27460 {
27461 tree fn;
27462 bool saved_in_unbraced_linkage_specification_p;
27463 bool saved_in_function_body;
27464 unsigned saved_num_template_parameter_lists;
27465 cp_token *token;
27466 bool fully_implicit_function_template_p
27467 = parser->fully_implicit_function_template_p;
27468 parser->fully_implicit_function_template_p = false;
27469 tree implicit_template_parms
27470 = parser->implicit_template_parms;
27471 parser->implicit_template_parms = 0;
27472 cp_binding_level* implicit_template_scope
27473 = parser->implicit_template_scope;
27474 parser->implicit_template_scope = 0;
27475
27476 saved_in_function_body = parser->in_function_body;
27477 parser->in_function_body = true;
27478 /* If the next token is `return', then the code may be trying to
27479 make use of the "named return value" extension that G++ used to
27480 support. */
27481 token = cp_lexer_peek_token (parser->lexer);
27482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27483 {
27484 /* Consume the `return' keyword. */
27485 cp_lexer_consume_token (parser->lexer);
27486 /* Look for the identifier that indicates what value is to be
27487 returned. */
27488 cp_parser_identifier (parser);
27489 /* Issue an error message. */
27490 error_at (token->location,
27491 "named return values are no longer supported");
27492 /* Skip tokens until we reach the start of the function body. */
27493 while (true)
27494 {
27495 cp_token *token = cp_lexer_peek_token (parser->lexer);
27496 if (token->type == CPP_OPEN_BRACE
27497 || token->type == CPP_EOF
27498 || token->type == CPP_PRAGMA_EOL)
27499 break;
27500 cp_lexer_consume_token (parser->lexer);
27501 }
27502 }
27503 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27504 anything declared inside `f'. */
27505 saved_in_unbraced_linkage_specification_p
27506 = parser->in_unbraced_linkage_specification_p;
27507 parser->in_unbraced_linkage_specification_p = false;
27508 /* Inside the function, surrounding template-parameter-lists do not
27509 apply. */
27510 saved_num_template_parameter_lists
27511 = parser->num_template_parameter_lists;
27512 parser->num_template_parameter_lists = 0;
27513
27514 /* If the next token is `try', `__transaction_atomic', or
27515 `__transaction_relaxed`, then we are looking at either function-try-block
27516 or function-transaction-block. Note that all of these include the
27517 function-body. */
27518 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27519 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27520 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27521 RID_TRANSACTION_RELAXED))
27522 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27523 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27524 cp_parser_function_try_block (parser);
27525 else
27526 cp_parser_ctor_initializer_opt_and_function_body
27527 (parser, /*in_function_try_block=*/false);
27528
27529 /* Finish the function. */
27530 fn = finish_function (inline_p);
27531 /* Generate code for it, if necessary. */
27532 expand_or_defer_fn (fn);
27533 /* Restore the saved values. */
27534 parser->in_unbraced_linkage_specification_p
27535 = saved_in_unbraced_linkage_specification_p;
27536 parser->num_template_parameter_lists
27537 = saved_num_template_parameter_lists;
27538 parser->in_function_body = saved_in_function_body;
27539
27540 parser->fully_implicit_function_template_p
27541 = fully_implicit_function_template_p;
27542 parser->implicit_template_parms
27543 = implicit_template_parms;
27544 parser->implicit_template_scope
27545 = implicit_template_scope;
27546
27547 if (parser->fully_implicit_function_template_p)
27548 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27549
27550 return fn;
27551 }
27552
27553 /* Parse a template-declaration body (following argument list). */
27554
27555 static void
27556 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27557 tree parameter_list,
27558 bool member_p)
27559 {
27560 tree decl = NULL_TREE;
27561 bool friend_p = false;
27562
27563 /* We just processed one more parameter list. */
27564 ++parser->num_template_parameter_lists;
27565
27566 /* Get the deferred access checks from the parameter list. These
27567 will be checked once we know what is being declared, as for a
27568 member template the checks must be performed in the scope of the
27569 class containing the member. */
27570 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27571
27572 /* Tentatively parse for a new template parameter list, which can either be
27573 the template keyword or a template introduction. */
27574 if (cp_parser_template_declaration_after_export (parser, member_p))
27575 /* OK */;
27576 else if (cxx_dialect >= cxx11
27577 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27578 decl = cp_parser_alias_declaration (parser);
27579 else
27580 {
27581 /* There are no access checks when parsing a template, as we do not
27582 know if a specialization will be a friend. */
27583 push_deferring_access_checks (dk_no_check);
27584 cp_token *token = cp_lexer_peek_token (parser->lexer);
27585 decl = cp_parser_single_declaration (parser,
27586 checks,
27587 member_p,
27588 /*explicit_specialization_p=*/false,
27589 &friend_p);
27590 pop_deferring_access_checks ();
27591
27592 /* If this is a member template declaration, let the front
27593 end know. */
27594 if (member_p && !friend_p && decl)
27595 {
27596 if (TREE_CODE (decl) == TYPE_DECL)
27597 cp_parser_check_access_in_redeclaration (decl, token->location);
27598
27599 decl = finish_member_template_decl (decl);
27600 }
27601 else if (friend_p && decl
27602 && DECL_DECLARES_TYPE_P (decl))
27603 make_friend_class (current_class_type, TREE_TYPE (decl),
27604 /*complain=*/true);
27605 }
27606 /* We are done with the current parameter list. */
27607 --parser->num_template_parameter_lists;
27608
27609 pop_deferring_access_checks ();
27610
27611 /* Finish up. */
27612 finish_template_decl (parameter_list);
27613
27614 /* Check the template arguments for a literal operator template. */
27615 if (decl
27616 && DECL_DECLARES_FUNCTION_P (decl)
27617 && UDLIT_OPER_P (DECL_NAME (decl)))
27618 {
27619 bool ok = true;
27620 if (parameter_list == NULL_TREE)
27621 ok = false;
27622 else
27623 {
27624 int num_parms = TREE_VEC_LENGTH (parameter_list);
27625 if (num_parms == 1)
27626 {
27627 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27628 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27629 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27630 /* OK, C++20 string literal operator template. We don't need
27631 to warn in lower dialects here because we will have already
27632 warned about the template parameter. */;
27633 else if (TREE_TYPE (parm) != char_type_node
27634 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27635 ok = false;
27636 }
27637 else if (num_parms == 2 && cxx_dialect >= cxx14)
27638 {
27639 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27640 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27641 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27642 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27643 if (parm == error_mark_node
27644 || TREE_TYPE (parm) != TREE_TYPE (type)
27645 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27646 ok = false;
27647 else
27648 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27649 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27650 "ISO C++ did not adopt string literal operator templa"
27651 "tes taking an argument pack of characters");
27652 }
27653 else
27654 ok = false;
27655 }
27656 if (!ok)
27657 {
27658 if (cxx_dialect > cxx17)
27659 error ("literal operator template %qD has invalid parameter list;"
27660 " Expected non-type template parameter pack <char...> "
27661 " or single non-type parameter of class type",
27662 decl);
27663 else
27664 error ("literal operator template %qD has invalid parameter list."
27665 " Expected non-type template parameter pack <char...>",
27666 decl);
27667 }
27668 }
27669
27670 /* Register member declarations. */
27671 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27672 finish_member_declaration (decl);
27673 /* If DECL is a function template, we must return to parse it later.
27674 (Even though there is no definition, there might be default
27675 arguments that need handling.) */
27676 if (member_p && decl
27677 && DECL_DECLARES_FUNCTION_P (decl))
27678 vec_safe_push (unparsed_funs_with_definitions, decl);
27679 }
27680
27681 /* Parse a template introduction header for a template-declaration. Returns
27682 false if tentative parse fails. */
27683
27684 static bool
27685 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27686 {
27687 cp_parser_parse_tentatively (parser);
27688
27689 tree saved_scope = parser->scope;
27690 tree saved_object_scope = parser->object_scope;
27691 tree saved_qualifying_scope = parser->qualifying_scope;
27692
27693 /* Look for the optional `::' operator. */
27694 cp_parser_global_scope_opt (parser,
27695 /*current_scope_valid_p=*/false);
27696 /* Look for the nested-name-specifier. */
27697 cp_parser_nested_name_specifier_opt (parser,
27698 /*typename_keyword_p=*/false,
27699 /*check_dependency_p=*/true,
27700 /*type_p=*/false,
27701 /*is_declaration=*/false);
27702
27703 cp_token *token = cp_lexer_peek_token (parser->lexer);
27704 tree concept_name = cp_parser_identifier (parser);
27705
27706 /* Look up the concept for which we will be matching
27707 template parameters. */
27708 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27709 token->location);
27710 parser->scope = saved_scope;
27711 parser->object_scope = saved_object_scope;
27712 parser->qualifying_scope = saved_qualifying_scope;
27713
27714 if (concept_name == error_mark_node)
27715 cp_parser_simulate_error (parser);
27716
27717 /* Look for opening brace for introduction. */
27718 matching_braces braces;
27719 braces.require_open (parser);
27720
27721 if (!cp_parser_parse_definitely (parser))
27722 return false;
27723
27724 push_deferring_access_checks (dk_deferred);
27725
27726 /* Build vector of placeholder parameters and grab
27727 matching identifiers. */
27728 tree introduction_list = cp_parser_introduction_list (parser);
27729
27730 /* Look for closing brace for introduction. */
27731 if (!braces.require_close (parser))
27732 return true;
27733
27734 /* The introduction-list shall not be empty. */
27735 int nargs = TREE_VEC_LENGTH (introduction_list);
27736 if (nargs == 0)
27737 {
27738 /* In cp_parser_introduction_list we have already issued an error. */
27739 return true;
27740 }
27741
27742 if (tmpl_decl == error_mark_node)
27743 {
27744 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27745 token->location);
27746 return true;
27747 }
27748
27749 /* Build and associate the constraint. */
27750 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27751 if (parms && parms != error_mark_node)
27752 {
27753 cp_parser_template_declaration_after_parameters (parser, parms,
27754 member_p);
27755 return true;
27756 }
27757
27758 error_at (token->location, "no matching concept for template-introduction");
27759 return true;
27760 }
27761
27762 /* Parse a normal template-declaration following the template keyword. */
27763
27764 static void
27765 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27766 {
27767 tree parameter_list;
27768 bool need_lang_pop;
27769 location_t location = input_location;
27770
27771 /* Look for the `<' token. */
27772 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27773 return;
27774 if (at_class_scope_p () && current_function_decl)
27775 {
27776 /* 14.5.2.2 [temp.mem]
27777
27778 A local class shall not have member templates. */
27779 error_at (location,
27780 "invalid declaration of member template in local class");
27781 cp_parser_skip_to_end_of_block_or_statement (parser);
27782 return;
27783 }
27784 /* [temp]
27785
27786 A template ... shall not have C linkage. */
27787 if (current_lang_name == lang_name_c)
27788 {
27789 error_at (location, "template with C linkage");
27790 maybe_show_extern_c_location ();
27791 /* Give it C++ linkage to avoid confusing other parts of the
27792 front end. */
27793 push_lang_context (lang_name_cplusplus);
27794 need_lang_pop = true;
27795 }
27796 else
27797 need_lang_pop = false;
27798
27799 /* We cannot perform access checks on the template parameter
27800 declarations until we know what is being declared, just as we
27801 cannot check the decl-specifier list. */
27802 push_deferring_access_checks (dk_deferred);
27803
27804 /* If the next token is `>', then we have an invalid
27805 specialization. Rather than complain about an invalid template
27806 parameter, issue an error message here. */
27807 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27808 {
27809 cp_parser_error (parser, "invalid explicit specialization");
27810 begin_specialization ();
27811 parameter_list = NULL_TREE;
27812 }
27813 else
27814 {
27815 /* Parse the template parameters. */
27816 parameter_list = cp_parser_template_parameter_list (parser);
27817 }
27818
27819 /* Look for the `>'. */
27820 cp_parser_skip_to_end_of_template_parameter_list (parser);
27821
27822 /* Manage template requirements */
27823 if (flag_concepts)
27824 {
27825 tree reqs = get_shorthand_constraints (current_template_parms);
27826 if (tree r = cp_parser_requires_clause_opt (parser))
27827 reqs = conjoin_constraints (reqs, normalize_expression (r));
27828 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27829 }
27830
27831 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27832 member_p);
27833
27834 /* For the erroneous case of a template with C linkage, we pushed an
27835 implicit C++ linkage scope; exit that scope now. */
27836 if (need_lang_pop)
27837 pop_lang_context ();
27838 }
27839
27840 /* Parse a template-declaration, assuming that the `export' (and
27841 `extern') keywords, if present, has already been scanned. MEMBER_P
27842 is as for cp_parser_template_declaration. */
27843
27844 static bool
27845 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27846 {
27847 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27848 {
27849 cp_lexer_consume_token (parser->lexer);
27850 cp_parser_explicit_template_declaration (parser, member_p);
27851 return true;
27852 }
27853 else if (flag_concepts)
27854 return cp_parser_template_introduction (parser, member_p);
27855
27856 return false;
27857 }
27858
27859 /* Perform the deferred access checks from a template-parameter-list.
27860 CHECKS is a TREE_LIST of access checks, as returned by
27861 get_deferred_access_checks. */
27862
27863 static void
27864 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27865 {
27866 ++processing_template_parmlist;
27867 perform_access_checks (checks, tf_warning_or_error);
27868 --processing_template_parmlist;
27869 }
27870
27871 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27872 `function-definition' sequence that follows a template header.
27873 If MEMBER_P is true, this declaration appears in a class scope.
27874
27875 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27876 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27877
27878 static tree
27879 cp_parser_single_declaration (cp_parser* parser,
27880 vec<deferred_access_check, va_gc> *checks,
27881 bool member_p,
27882 bool explicit_specialization_p,
27883 bool* friend_p)
27884 {
27885 int declares_class_or_enum;
27886 tree decl = NULL_TREE;
27887 cp_decl_specifier_seq decl_specifiers;
27888 bool function_definition_p = false;
27889 cp_token *decl_spec_token_start;
27890
27891 /* This function is only used when processing a template
27892 declaration. */
27893 gcc_assert (innermost_scope_kind () == sk_template_parms
27894 || innermost_scope_kind () == sk_template_spec);
27895
27896 /* Defer access checks until we know what is being declared. */
27897 push_deferring_access_checks (dk_deferred);
27898
27899 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27900 alternative. */
27901 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27902 cp_parser_decl_specifier_seq (parser,
27903 (CP_PARSER_FLAGS_OPTIONAL
27904 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
27905 &decl_specifiers,
27906 &declares_class_or_enum);
27907 if (friend_p)
27908 *friend_p = cp_parser_friend_p (&decl_specifiers);
27909
27910 /* There are no template typedefs. */
27911 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27912 {
27913 error_at (decl_spec_token_start->location,
27914 "template declaration of %<typedef%>");
27915 decl = error_mark_node;
27916 }
27917
27918 /* Gather up the access checks that occurred the
27919 decl-specifier-seq. */
27920 stop_deferring_access_checks ();
27921
27922 /* Check for the declaration of a template class. */
27923 if (declares_class_or_enum)
27924 {
27925 if (cp_parser_declares_only_class_p (parser)
27926 || (declares_class_or_enum & 2))
27927 {
27928 // If this is a declaration, but not a definition, associate
27929 // any constraints with the type declaration. Constraints
27930 // are associated with definitions in cp_parser_class_specifier.
27931 if (declares_class_or_enum == 1)
27932 associate_classtype_constraints (decl_specifiers.type);
27933
27934 decl = shadow_tag (&decl_specifiers);
27935
27936 /* In this case:
27937
27938 struct C {
27939 friend template <typename T> struct A<T>::B;
27940 };
27941
27942 A<T>::B will be represented by a TYPENAME_TYPE, and
27943 therefore not recognized by shadow_tag. */
27944 if (friend_p && *friend_p
27945 && !decl
27946 && decl_specifiers.type
27947 && TYPE_P (decl_specifiers.type))
27948 decl = decl_specifiers.type;
27949
27950 if (decl && decl != error_mark_node)
27951 decl = TYPE_NAME (decl);
27952 else
27953 decl = error_mark_node;
27954
27955 /* Perform access checks for template parameters. */
27956 cp_parser_perform_template_parameter_access_checks (checks);
27957
27958 /* Give a helpful diagnostic for
27959 template <class T> struct A { } a;
27960 if we aren't already recovering from an error. */
27961 if (!cp_parser_declares_only_class_p (parser)
27962 && !seen_error ())
27963 {
27964 error_at (cp_lexer_peek_token (parser->lexer)->location,
27965 "a class template declaration must not declare "
27966 "anything else");
27967 cp_parser_skip_to_end_of_block_or_statement (parser);
27968 goto out;
27969 }
27970 }
27971 }
27972
27973 /* Complain about missing 'typename' or other invalid type names. */
27974 if (!decl_specifiers.any_type_specifiers_p
27975 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27976 {
27977 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27978 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27979 the rest of this declaration. */
27980 decl = error_mark_node;
27981 goto out;
27982 }
27983
27984 /* If it's not a template class, try for a template function. If
27985 the next token is a `;', then this declaration does not declare
27986 anything. But, if there were errors in the decl-specifiers, then
27987 the error might well have come from an attempted class-specifier.
27988 In that case, there's no need to warn about a missing declarator. */
27989 if (!decl
27990 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27991 || decl_specifiers.type != error_mark_node))
27992 {
27993 decl = cp_parser_init_declarator (parser,
27994 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
27995 &decl_specifiers,
27996 checks,
27997 /*function_definition_allowed_p=*/true,
27998 member_p,
27999 declares_class_or_enum,
28000 &function_definition_p,
28001 NULL, NULL, NULL);
28002
28003 /* 7.1.1-1 [dcl.stc]
28004
28005 A storage-class-specifier shall not be specified in an explicit
28006 specialization... */
28007 if (decl
28008 && explicit_specialization_p
28009 && decl_specifiers.storage_class != sc_none)
28010 {
28011 error_at (decl_spec_token_start->location,
28012 "explicit template specialization cannot have a storage class");
28013 decl = error_mark_node;
28014 }
28015
28016 if (decl && VAR_P (decl))
28017 check_template_variable (decl);
28018 }
28019
28020 /* Look for a trailing `;' after the declaration. */
28021 if (!function_definition_p
28022 && (decl == error_mark_node
28023 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
28024 cp_parser_skip_to_end_of_block_or_statement (parser);
28025
28026 out:
28027 pop_deferring_access_checks ();
28028
28029 /* Clear any current qualification; whatever comes next is the start
28030 of something new. */
28031 parser->scope = NULL_TREE;
28032 parser->qualifying_scope = NULL_TREE;
28033 parser->object_scope = NULL_TREE;
28034
28035 return decl;
28036 }
28037
28038 /* Parse a cast-expression that is not the operand of a unary "&". */
28039
28040 static cp_expr
28041 cp_parser_simple_cast_expression (cp_parser *parser)
28042 {
28043 return cp_parser_cast_expression (parser, /*address_p=*/false,
28044 /*cast_p=*/false, /*decltype*/false, NULL);
28045 }
28046
28047 /* Parse a functional cast to TYPE. Returns an expression
28048 representing the cast. */
28049
28050 static cp_expr
28051 cp_parser_functional_cast (cp_parser* parser, tree type)
28052 {
28053 vec<tree, va_gc> *vec;
28054 tree expression_list;
28055 cp_expr cast;
28056 bool nonconst_p;
28057
28058 location_t start_loc = input_location;
28059
28060 if (!type)
28061 type = error_mark_node;
28062
28063 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28064 {
28065 cp_lexer_set_source_position (parser->lexer);
28066 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28067 expression_list = cp_parser_braced_list (parser, &nonconst_p);
28068 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
28069 if (TREE_CODE (type) == TYPE_DECL)
28070 type = TREE_TYPE (type);
28071
28072 cast = finish_compound_literal (type, expression_list,
28073 tf_warning_or_error, fcl_functional);
28074 /* Create a location of the form:
28075 type_name{i, f}
28076 ^~~~~~~~~~~~~~~
28077 with caret == start at the start of the type name,
28078 finishing at the closing brace. */
28079 location_t finish_loc
28080 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28081 location_t combined_loc = make_location (start_loc, start_loc,
28082 finish_loc);
28083 cast.set_location (combined_loc);
28084 return cast;
28085 }
28086
28087
28088 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
28089 /*cast_p=*/true,
28090 /*allow_expansion_p=*/true,
28091 /*non_constant_p=*/NULL);
28092 if (vec == NULL)
28093 expression_list = error_mark_node;
28094 else
28095 {
28096 expression_list = build_tree_list_vec (vec);
28097 release_tree_vector (vec);
28098 }
28099
28100 cast = build_functional_cast (type, expression_list,
28101 tf_warning_or_error);
28102 /* [expr.const]/1: In an integral constant expression "only type
28103 conversions to integral or enumeration type can be used". */
28104 if (TREE_CODE (type) == TYPE_DECL)
28105 type = TREE_TYPE (type);
28106 if (cast != error_mark_node
28107 && !cast_valid_in_integral_constant_expression_p (type)
28108 && cp_parser_non_integral_constant_expression (parser,
28109 NIC_CONSTRUCTOR))
28110 return error_mark_node;
28111
28112 /* Create a location of the form:
28113 float(i)
28114 ^~~~~~~~
28115 with caret == start at the start of the type name,
28116 finishing at the closing paren. */
28117 location_t finish_loc
28118 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28119 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
28120 cast.set_location (combined_loc);
28121 return cast;
28122 }
28123
28124 /* Save the tokens that make up the body of a member function defined
28125 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28126 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28127 specifiers applied to the declaration. Returns the FUNCTION_DECL
28128 for the member function. */
28129
28130 static tree
28131 cp_parser_save_member_function_body (cp_parser* parser,
28132 cp_decl_specifier_seq *decl_specifiers,
28133 cp_declarator *declarator,
28134 tree attributes)
28135 {
28136 cp_token *first;
28137 cp_token *last;
28138 tree fn;
28139 bool function_try_block = false;
28140
28141 /* Create the FUNCTION_DECL. */
28142 fn = grokmethod (decl_specifiers, declarator, attributes);
28143 cp_finalize_omp_declare_simd (parser, fn);
28144 cp_finalize_oacc_routine (parser, fn, true);
28145 /* If something went badly wrong, bail out now. */
28146 if (fn == error_mark_node)
28147 {
28148 /* If there's a function-body, skip it. */
28149 if (cp_parser_token_starts_function_definition_p
28150 (cp_lexer_peek_token (parser->lexer)))
28151 cp_parser_skip_to_end_of_block_or_statement (parser);
28152 return error_mark_node;
28153 }
28154
28155 /* Remember it, if there default args to post process. */
28156 cp_parser_save_default_args (parser, fn);
28157
28158 /* Save away the tokens that make up the body of the
28159 function. */
28160 first = parser->lexer->next_token;
28161
28162 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
28163 cp_lexer_consume_token (parser->lexer);
28164 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28165 RID_TRANSACTION_ATOMIC))
28166 {
28167 cp_lexer_consume_token (parser->lexer);
28168 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28169 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
28170 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
28171 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
28172 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28173 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
28174 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28175 {
28176 cp_lexer_consume_token (parser->lexer);
28177 cp_lexer_consume_token (parser->lexer);
28178 cp_lexer_consume_token (parser->lexer);
28179 cp_lexer_consume_token (parser->lexer);
28180 cp_lexer_consume_token (parser->lexer);
28181 }
28182 else
28183 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28184 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28185 {
28186 cp_lexer_consume_token (parser->lexer);
28187 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28188 break;
28189 }
28190 }
28191
28192 /* Handle function try blocks. */
28193 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28194 {
28195 cp_lexer_consume_token (parser->lexer);
28196 function_try_block = true;
28197 }
28198 /* We can have braced-init-list mem-initializers before the fn body. */
28199 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28200 {
28201 cp_lexer_consume_token (parser->lexer);
28202 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28203 {
28204 /* cache_group will stop after an un-nested { } pair, too. */
28205 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28206 break;
28207
28208 /* variadic mem-inits have ... after the ')'. */
28209 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28210 cp_lexer_consume_token (parser->lexer);
28211 }
28212 }
28213 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28214 /* Handle function try blocks. */
28215 if (function_try_block)
28216 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28217 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28218 last = parser->lexer->next_token;
28219
28220 /* Save away the inline definition; we will process it when the
28221 class is complete. */
28222 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28223 DECL_PENDING_INLINE_P (fn) = 1;
28224
28225 /* We need to know that this was defined in the class, so that
28226 friend templates are handled correctly. */
28227 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28228
28229 /* Add FN to the queue of functions to be parsed later. */
28230 vec_safe_push (unparsed_funs_with_definitions, fn);
28231
28232 return fn;
28233 }
28234
28235 /* Save the tokens that make up the in-class initializer for a non-static
28236 data member. Returns a DEFAULT_ARG. */
28237
28238 static tree
28239 cp_parser_save_nsdmi (cp_parser* parser)
28240 {
28241 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28242 }
28243
28244 /* Parse a template-argument-list, as well as the trailing ">" (but
28245 not the opening "<"). See cp_parser_template_argument_list for the
28246 return value. */
28247
28248 static tree
28249 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28250 {
28251 tree arguments;
28252 tree saved_scope;
28253 tree saved_qualifying_scope;
28254 tree saved_object_scope;
28255 bool saved_greater_than_is_operator_p;
28256
28257 /* [temp.names]
28258
28259 When parsing a template-id, the first non-nested `>' is taken as
28260 the end of the template-argument-list rather than a greater-than
28261 operator. */
28262 saved_greater_than_is_operator_p
28263 = parser->greater_than_is_operator_p;
28264 parser->greater_than_is_operator_p = false;
28265 /* Parsing the argument list may modify SCOPE, so we save it
28266 here. */
28267 saved_scope = parser->scope;
28268 saved_qualifying_scope = parser->qualifying_scope;
28269 saved_object_scope = parser->object_scope;
28270 /* We need to evaluate the template arguments, even though this
28271 template-id may be nested within a "sizeof". */
28272 cp_evaluated ev;
28273 /* Parse the template-argument-list itself. */
28274 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28275 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28276 arguments = NULL_TREE;
28277 else
28278 arguments = cp_parser_template_argument_list (parser);
28279 /* Look for the `>' that ends the template-argument-list. If we find
28280 a '>>' instead, it's probably just a typo. */
28281 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28282 {
28283 if (cxx_dialect != cxx98)
28284 {
28285 /* In C++0x, a `>>' in a template argument list or cast
28286 expression is considered to be two separate `>'
28287 tokens. So, change the current token to a `>', but don't
28288 consume it: it will be consumed later when the outer
28289 template argument list (or cast expression) is parsed.
28290 Note that this replacement of `>' for `>>' is necessary
28291 even if we are parsing tentatively: in the tentative
28292 case, after calling
28293 cp_parser_enclosed_template_argument_list we will always
28294 throw away all of the template arguments and the first
28295 closing `>', either because the template argument list
28296 was erroneous or because we are replacing those tokens
28297 with a CPP_TEMPLATE_ID token. The second `>' (which will
28298 not have been thrown away) is needed either to close an
28299 outer template argument list or to complete a new-style
28300 cast. */
28301 cp_token *token = cp_lexer_peek_token (parser->lexer);
28302 token->type = CPP_GREATER;
28303 }
28304 else if (!saved_greater_than_is_operator_p)
28305 {
28306 /* If we're in a nested template argument list, the '>>' has
28307 to be a typo for '> >'. We emit the error message, but we
28308 continue parsing and we push a '>' as next token, so that
28309 the argument list will be parsed correctly. Note that the
28310 global source location is still on the token before the
28311 '>>', so we need to say explicitly where we want it. */
28312 cp_token *token = cp_lexer_peek_token (parser->lexer);
28313 gcc_rich_location richloc (token->location);
28314 richloc.add_fixit_replace ("> >");
28315 error_at (&richloc, "%<>>%> should be %<> >%> "
28316 "within a nested template argument list");
28317
28318 token->type = CPP_GREATER;
28319 }
28320 else
28321 {
28322 /* If this is not a nested template argument list, the '>>'
28323 is a typo for '>'. Emit an error message and continue.
28324 Same deal about the token location, but here we can get it
28325 right by consuming the '>>' before issuing the diagnostic. */
28326 cp_token *token = cp_lexer_consume_token (parser->lexer);
28327 error_at (token->location,
28328 "spurious %<>>%>, use %<>%> to terminate "
28329 "a template argument list");
28330 }
28331 }
28332 else
28333 cp_parser_skip_to_end_of_template_parameter_list (parser);
28334 /* The `>' token might be a greater-than operator again now. */
28335 parser->greater_than_is_operator_p
28336 = saved_greater_than_is_operator_p;
28337 /* Restore the SAVED_SCOPE. */
28338 parser->scope = saved_scope;
28339 parser->qualifying_scope = saved_qualifying_scope;
28340 parser->object_scope = saved_object_scope;
28341
28342 return arguments;
28343 }
28344
28345 /* MEMBER_FUNCTION is a member function, or a friend. If default
28346 arguments, or the body of the function have not yet been parsed,
28347 parse them now. */
28348
28349 static void
28350 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28351 {
28352 timevar_push (TV_PARSE_INMETH);
28353 /* If this member is a template, get the underlying
28354 FUNCTION_DECL. */
28355 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28356 member_function = DECL_TEMPLATE_RESULT (member_function);
28357
28358 /* There should not be any class definitions in progress at this
28359 point; the bodies of members are only parsed outside of all class
28360 definitions. */
28361 gcc_assert (parser->num_classes_being_defined == 0);
28362 /* While we're parsing the member functions we might encounter more
28363 classes. We want to handle them right away, but we don't want
28364 them getting mixed up with functions that are currently in the
28365 queue. */
28366 push_unparsed_function_queues (parser);
28367
28368 /* Make sure that any template parameters are in scope. */
28369 maybe_begin_member_template_processing (member_function);
28370
28371 /* If the body of the function has not yet been parsed, parse it
28372 now. */
28373 if (DECL_PENDING_INLINE_P (member_function))
28374 {
28375 tree function_scope;
28376 cp_token_cache *tokens;
28377
28378 /* The function is no longer pending; we are processing it. */
28379 tokens = DECL_PENDING_INLINE_INFO (member_function);
28380 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28381 DECL_PENDING_INLINE_P (member_function) = 0;
28382
28383 /* If this is a local class, enter the scope of the containing
28384 function. */
28385 function_scope = current_function_decl;
28386 if (function_scope)
28387 push_function_context ();
28388
28389 /* Push the body of the function onto the lexer stack. */
28390 cp_parser_push_lexer_for_tokens (parser, tokens);
28391
28392 /* Let the front end know that we going to be defining this
28393 function. */
28394 start_preparsed_function (member_function, NULL_TREE,
28395 SF_PRE_PARSED | SF_INCLASS_INLINE);
28396
28397 /* Don't do access checking if it is a templated function. */
28398 if (processing_template_decl)
28399 push_deferring_access_checks (dk_no_check);
28400
28401 /* #pragma omp declare reduction needs special parsing. */
28402 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28403 {
28404 parser->lexer->in_pragma = true;
28405 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28406 finish_function (/*inline_p=*/true);
28407 cp_check_omp_declare_reduction (member_function);
28408 }
28409 else
28410 /* Now, parse the body of the function. */
28411 cp_parser_function_definition_after_declarator (parser,
28412 /*inline_p=*/true);
28413
28414 if (processing_template_decl)
28415 pop_deferring_access_checks ();
28416
28417 /* Leave the scope of the containing function. */
28418 if (function_scope)
28419 pop_function_context ();
28420 cp_parser_pop_lexer (parser);
28421 }
28422
28423 /* Remove any template parameters from the symbol table. */
28424 maybe_end_member_template_processing ();
28425
28426 /* Restore the queue. */
28427 pop_unparsed_function_queues (parser);
28428 timevar_pop (TV_PARSE_INMETH);
28429 }
28430
28431 /* If DECL contains any default args, remember it on the unparsed
28432 functions queue. */
28433
28434 static void
28435 cp_parser_save_default_args (cp_parser* parser, tree decl)
28436 {
28437 tree probe;
28438
28439 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28440 probe;
28441 probe = TREE_CHAIN (probe))
28442 if (TREE_PURPOSE (probe))
28443 {
28444 cp_default_arg_entry entry = {current_class_type, decl};
28445 vec_safe_push (unparsed_funs_with_default_args, entry);
28446 break;
28447 }
28448 }
28449
28450 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28451 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28452 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28453 from the parameter-type-list. */
28454
28455 static tree
28456 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28457 tree default_arg, tree parmtype)
28458 {
28459 cp_token_cache *tokens;
28460 tree parsed_arg;
28461 bool dummy;
28462
28463 if (default_arg == error_mark_node)
28464 return error_mark_node;
28465
28466 /* Push the saved tokens for the default argument onto the parser's
28467 lexer stack. */
28468 tokens = DEFARG_TOKENS (default_arg);
28469 cp_parser_push_lexer_for_tokens (parser, tokens);
28470
28471 start_lambda_scope (decl);
28472
28473 /* Parse the default argument. */
28474 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28475 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28476 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28477
28478 finish_lambda_scope ();
28479
28480 if (parsed_arg == error_mark_node)
28481 cp_parser_skip_to_end_of_statement (parser);
28482
28483 if (!processing_template_decl)
28484 {
28485 /* In a non-template class, check conversions now. In a template,
28486 we'll wait and instantiate these as needed. */
28487 if (TREE_CODE (decl) == PARM_DECL)
28488 parsed_arg = check_default_argument (parmtype, parsed_arg,
28489 tf_warning_or_error);
28490 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28491 parsed_arg = error_mark_node;
28492 else
28493 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28494 }
28495
28496 /* If the token stream has not been completely used up, then
28497 there was extra junk after the end of the default
28498 argument. */
28499 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28500 {
28501 if (TREE_CODE (decl) == PARM_DECL)
28502 cp_parser_error (parser, "expected %<,%>");
28503 else
28504 cp_parser_error (parser, "expected %<;%>");
28505 }
28506
28507 /* Revert to the main lexer. */
28508 cp_parser_pop_lexer (parser);
28509
28510 return parsed_arg;
28511 }
28512
28513 /* FIELD is a non-static data member with an initializer which we saved for
28514 later; parse it now. */
28515
28516 static void
28517 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28518 {
28519 tree def;
28520
28521 maybe_begin_member_template_processing (field);
28522
28523 push_unparsed_function_queues (parser);
28524 def = cp_parser_late_parse_one_default_arg (parser, field,
28525 DECL_INITIAL (field),
28526 NULL_TREE);
28527 pop_unparsed_function_queues (parser);
28528
28529 maybe_end_member_template_processing ();
28530
28531 DECL_INITIAL (field) = def;
28532 }
28533
28534 /* FN is a FUNCTION_DECL which may contains a parameter with an
28535 unparsed DEFAULT_ARG. Parse the default args now. This function
28536 assumes that the current scope is the scope in which the default
28537 argument should be processed. */
28538
28539 static void
28540 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28541 {
28542 bool saved_local_variables_forbidden_p;
28543 tree parm, parmdecl;
28544
28545 /* While we're parsing the default args, we might (due to the
28546 statement expression extension) encounter more classes. We want
28547 to handle them right away, but we don't want them getting mixed
28548 up with default args that are currently in the queue. */
28549 push_unparsed_function_queues (parser);
28550
28551 /* Local variable names (and the `this' keyword) may not appear
28552 in a default argument. */
28553 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28554 parser->local_variables_forbidden_p = true;
28555
28556 push_defarg_context (fn);
28557
28558 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28559 parmdecl = DECL_ARGUMENTS (fn);
28560 parm && parm != void_list_node;
28561 parm = TREE_CHAIN (parm),
28562 parmdecl = DECL_CHAIN (parmdecl))
28563 {
28564 tree default_arg = TREE_PURPOSE (parm);
28565 tree parsed_arg;
28566 vec<tree, va_gc> *insts;
28567 tree copy;
28568 unsigned ix;
28569
28570 if (!default_arg)
28571 continue;
28572
28573 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28574 /* This can happen for a friend declaration for a function
28575 already declared with default arguments. */
28576 continue;
28577
28578 parsed_arg
28579 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28580 default_arg,
28581 TREE_VALUE (parm));
28582 TREE_PURPOSE (parm) = parsed_arg;
28583
28584 /* Update any instantiations we've already created. */
28585 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28586 vec_safe_iterate (insts, ix, &copy); ix++)
28587 TREE_PURPOSE (copy) = parsed_arg;
28588 }
28589
28590 pop_defarg_context ();
28591
28592 /* Make sure no default arg is missing. */
28593 check_default_args (fn);
28594
28595 /* Restore the state of local_variables_forbidden_p. */
28596 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28597
28598 /* Restore the queue. */
28599 pop_unparsed_function_queues (parser);
28600 }
28601
28602 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28603
28604 sizeof ... ( identifier )
28605
28606 where the 'sizeof' token has already been consumed. */
28607
28608 static tree
28609 cp_parser_sizeof_pack (cp_parser *parser)
28610 {
28611 /* Consume the `...'. */
28612 cp_lexer_consume_token (parser->lexer);
28613 maybe_warn_variadic_templates ();
28614
28615 matching_parens parens;
28616 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28617 if (paren)
28618 parens.consume_open (parser);
28619 else
28620 permerror (cp_lexer_peek_token (parser->lexer)->location,
28621 "%<sizeof...%> argument must be surrounded by parentheses");
28622
28623 cp_token *token = cp_lexer_peek_token (parser->lexer);
28624 tree name = cp_parser_identifier (parser);
28625 if (name == error_mark_node)
28626 return error_mark_node;
28627 /* The name is not qualified. */
28628 parser->scope = NULL_TREE;
28629 parser->qualifying_scope = NULL_TREE;
28630 parser->object_scope = NULL_TREE;
28631 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28632 if (expr == error_mark_node)
28633 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28634 token->location);
28635 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28636 expr = TREE_TYPE (expr);
28637 else if (TREE_CODE (expr) == CONST_DECL)
28638 expr = DECL_INITIAL (expr);
28639 expr = make_pack_expansion (expr);
28640 PACK_EXPANSION_SIZEOF_P (expr) = true;
28641
28642 if (paren)
28643 parens.require_close (parser);
28644
28645 return expr;
28646 }
28647
28648 /* Parse the operand of `sizeof' (or a similar operator). Returns
28649 either a TYPE or an expression, depending on the form of the
28650 input. The KEYWORD indicates which kind of expression we have
28651 encountered. */
28652
28653 static tree
28654 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28655 {
28656 tree expr = NULL_TREE;
28657 const char *saved_message;
28658 char *tmp;
28659 bool saved_integral_constant_expression_p;
28660 bool saved_non_integral_constant_expression_p;
28661
28662 /* If it's a `...', then we are computing the length of a parameter
28663 pack. */
28664 if (keyword == RID_SIZEOF
28665 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28666 return cp_parser_sizeof_pack (parser);
28667
28668 /* Types cannot be defined in a `sizeof' expression. Save away the
28669 old message. */
28670 saved_message = parser->type_definition_forbidden_message;
28671 /* And create the new one. */
28672 tmp = concat ("types may not be defined in %<",
28673 IDENTIFIER_POINTER (ridpointers[keyword]),
28674 "%> expressions", NULL);
28675 parser->type_definition_forbidden_message = tmp;
28676
28677 /* The restrictions on constant-expressions do not apply inside
28678 sizeof expressions. */
28679 saved_integral_constant_expression_p
28680 = parser->integral_constant_expression_p;
28681 saved_non_integral_constant_expression_p
28682 = parser->non_integral_constant_expression_p;
28683 parser->integral_constant_expression_p = false;
28684
28685 /* Do not actually evaluate the expression. */
28686 ++cp_unevaluated_operand;
28687 ++c_inhibit_evaluation_warnings;
28688 /* If it's a `(', then we might be looking at the type-id
28689 construction. */
28690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28691 {
28692 tree type = NULL_TREE;
28693
28694 /* We can't be sure yet whether we're looking at a type-id or an
28695 expression. */
28696 cp_parser_parse_tentatively (parser);
28697
28698 matching_parens parens;
28699 parens.consume_open (parser);
28700
28701 /* Note: as a GNU Extension, compound literals are considered
28702 postfix-expressions as they are in C99, so they are valid
28703 arguments to sizeof. See comment in cp_parser_cast_expression
28704 for details. */
28705 if (cp_parser_compound_literal_p (parser))
28706 cp_parser_simulate_error (parser);
28707 else
28708 {
28709 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28710 parser->in_type_id_in_expr_p = true;
28711 /* Look for the type-id. */
28712 type = cp_parser_type_id (parser);
28713 /* Look for the closing `)'. */
28714 parens.require_close (parser);
28715 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28716 }
28717
28718 /* If all went well, then we're done. */
28719 if (cp_parser_parse_definitely (parser))
28720 expr = type;
28721 }
28722
28723 /* If the type-id production did not work out, then we must be
28724 looking at the unary-expression production. */
28725 if (!expr)
28726 expr = cp_parser_unary_expression (parser);
28727
28728 /* Go back to evaluating expressions. */
28729 --cp_unevaluated_operand;
28730 --c_inhibit_evaluation_warnings;
28731
28732 /* Free the message we created. */
28733 free (tmp);
28734 /* And restore the old one. */
28735 parser->type_definition_forbidden_message = saved_message;
28736 parser->integral_constant_expression_p
28737 = saved_integral_constant_expression_p;
28738 parser->non_integral_constant_expression_p
28739 = saved_non_integral_constant_expression_p;
28740
28741 return expr;
28742 }
28743
28744 /* If the current declaration has no declarator, return true. */
28745
28746 static bool
28747 cp_parser_declares_only_class_p (cp_parser *parser)
28748 {
28749 /* If the next token is a `;' or a `,' then there is no
28750 declarator. */
28751 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28752 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28753 }
28754
28755 /* Update the DECL_SPECS to reflect the storage class indicated by
28756 KEYWORD. */
28757
28758 static void
28759 cp_parser_set_storage_class (cp_parser *parser,
28760 cp_decl_specifier_seq *decl_specs,
28761 enum rid keyword,
28762 cp_token *token)
28763 {
28764 cp_storage_class storage_class;
28765
28766 if (parser->in_unbraced_linkage_specification_p)
28767 {
28768 error_at (token->location, "invalid use of %qD in linkage specification",
28769 ridpointers[keyword]);
28770 return;
28771 }
28772 else if (decl_specs->storage_class != sc_none)
28773 {
28774 decl_specs->conflicting_specifiers_p = true;
28775 return;
28776 }
28777
28778 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28779 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28780 && decl_specs->gnu_thread_keyword_p)
28781 {
28782 pedwarn (decl_specs->locations[ds_thread], 0,
28783 "%<__thread%> before %qD", ridpointers[keyword]);
28784 }
28785
28786 switch (keyword)
28787 {
28788 case RID_AUTO:
28789 storage_class = sc_auto;
28790 break;
28791 case RID_REGISTER:
28792 storage_class = sc_register;
28793 break;
28794 case RID_STATIC:
28795 storage_class = sc_static;
28796 break;
28797 case RID_EXTERN:
28798 storage_class = sc_extern;
28799 break;
28800 case RID_MUTABLE:
28801 storage_class = sc_mutable;
28802 break;
28803 default:
28804 gcc_unreachable ();
28805 }
28806 decl_specs->storage_class = storage_class;
28807 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28808
28809 /* A storage class specifier cannot be applied alongside a typedef
28810 specifier. If there is a typedef specifier present then set
28811 conflicting_specifiers_p which will trigger an error later
28812 on in grokdeclarator. */
28813 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28814 decl_specs->conflicting_specifiers_p = true;
28815 }
28816
28817 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28818 is true, the type is a class or enum definition. */
28819
28820 static void
28821 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28822 tree type_spec,
28823 cp_token *token,
28824 bool type_definition_p)
28825 {
28826 decl_specs->any_specifiers_p = true;
28827
28828 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28829 (with, for example, in "typedef int wchar_t;") we remember that
28830 this is what happened. In system headers, we ignore these
28831 declarations so that G++ can work with system headers that are not
28832 C++-safe. */
28833 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28834 && !type_definition_p
28835 && (type_spec == boolean_type_node
28836 || type_spec == char16_type_node
28837 || type_spec == char32_type_node
28838 || type_spec == wchar_type_node)
28839 && (decl_specs->type
28840 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28841 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28842 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28843 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28844 {
28845 decl_specs->redefined_builtin_type = type_spec;
28846 set_and_check_decl_spec_loc (decl_specs,
28847 ds_redefined_builtin_type_spec,
28848 token);
28849 if (!decl_specs->type)
28850 {
28851 decl_specs->type = type_spec;
28852 decl_specs->type_definition_p = false;
28853 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28854 }
28855 }
28856 else if (decl_specs->type)
28857 decl_specs->multiple_types_p = true;
28858 else
28859 {
28860 decl_specs->type = type_spec;
28861 decl_specs->type_definition_p = type_definition_p;
28862 decl_specs->redefined_builtin_type = NULL_TREE;
28863 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28864 }
28865 }
28866
28867 /* True iff TOKEN is the GNU keyword __thread. */
28868
28869 static bool
28870 token_is__thread (cp_token *token)
28871 {
28872 gcc_assert (token->keyword == RID_THREAD);
28873 return id_equal (token->u.value, "__thread");
28874 }
28875
28876 /* Set the location for a declarator specifier and check if it is
28877 duplicated.
28878
28879 DECL_SPECS is the sequence of declarator specifiers onto which to
28880 set the location.
28881
28882 DS is the single declarator specifier to set which location is to
28883 be set onto the existing sequence of declarators.
28884
28885 LOCATION is the location for the declarator specifier to
28886 consider. */
28887
28888 static void
28889 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28890 cp_decl_spec ds, cp_token *token)
28891 {
28892 gcc_assert (ds < ds_last);
28893
28894 if (decl_specs == NULL)
28895 return;
28896
28897 location_t location = token->location;
28898
28899 if (decl_specs->locations[ds] == 0)
28900 {
28901 decl_specs->locations[ds] = location;
28902 if (ds == ds_thread)
28903 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28904 }
28905 else
28906 {
28907 if (ds == ds_long)
28908 {
28909 if (decl_specs->locations[ds_long_long] != 0)
28910 error_at (location,
28911 "%<long long long%> is too long for GCC");
28912 else
28913 {
28914 decl_specs->locations[ds_long_long] = location;
28915 pedwarn_cxx98 (location,
28916 OPT_Wlong_long,
28917 "ISO C++ 1998 does not support %<long long%>");
28918 }
28919 }
28920 else if (ds == ds_thread)
28921 {
28922 bool gnu = token_is__thread (token);
28923 gcc_rich_location richloc (location);
28924 if (gnu != decl_specs->gnu_thread_keyword_p)
28925 {
28926 richloc.add_range (decl_specs->locations[ds_thread]);
28927 error_at (&richloc,
28928 "both %<__thread%> and %<thread_local%> specified");
28929 }
28930 else
28931 {
28932 richloc.add_fixit_remove ();
28933 error_at (&richloc, "duplicate %qD", token->u.value);
28934 }
28935 }
28936 else
28937 {
28938 static const char *const decl_spec_names[] = {
28939 "signed",
28940 "unsigned",
28941 "short",
28942 "long",
28943 "const",
28944 "volatile",
28945 "restrict",
28946 "inline",
28947 "virtual",
28948 "explicit",
28949 "friend",
28950 "typedef",
28951 "using",
28952 "constexpr",
28953 "__complex"
28954 };
28955 gcc_rich_location richloc (location);
28956 richloc.add_fixit_remove ();
28957 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
28958 }
28959 }
28960 }
28961
28962 /* Return true iff the declarator specifier DS is present in the
28963 sequence of declarator specifiers DECL_SPECS. */
28964
28965 bool
28966 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28967 cp_decl_spec ds)
28968 {
28969 gcc_assert (ds < ds_last);
28970
28971 if (decl_specs == NULL)
28972 return false;
28973
28974 return decl_specs->locations[ds] != 0;
28975 }
28976
28977 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28978 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28979
28980 static bool
28981 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28982 {
28983 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28984 }
28985
28986 /* Issue an error message indicating that TOKEN_DESC was expected.
28987 If KEYWORD is true, it indicated this function is called by
28988 cp_parser_require_keword and the required token can only be
28989 a indicated keyword.
28990
28991 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28992 within any error as the location of an "opening" token matching
28993 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28994 RT_CLOSE_PAREN). */
28995
28996 static void
28997 cp_parser_required_error (cp_parser *parser,
28998 required_token token_desc,
28999 bool keyword,
29000 location_t matching_location)
29001 {
29002 if (cp_parser_simulate_error (parser))
29003 return;
29004
29005 const char *gmsgid = NULL;
29006 switch (token_desc)
29007 {
29008 case RT_NEW:
29009 gmsgid = G_("expected %<new%>");
29010 break;
29011 case RT_DELETE:
29012 gmsgid = G_("expected %<delete%>");
29013 break;
29014 case RT_RETURN:
29015 gmsgid = G_("expected %<return%>");
29016 break;
29017 case RT_WHILE:
29018 gmsgid = G_("expected %<while%>");
29019 break;
29020 case RT_EXTERN:
29021 gmsgid = G_("expected %<extern%>");
29022 break;
29023 case RT_STATIC_ASSERT:
29024 gmsgid = G_("expected %<static_assert%>");
29025 break;
29026 case RT_DECLTYPE:
29027 gmsgid = G_("expected %<decltype%>");
29028 break;
29029 case RT_OPERATOR:
29030 gmsgid = G_("expected %<operator%>");
29031 break;
29032 case RT_CLASS:
29033 gmsgid = G_("expected %<class%>");
29034 break;
29035 case RT_TEMPLATE:
29036 gmsgid = G_("expected %<template%>");
29037 break;
29038 case RT_NAMESPACE:
29039 gmsgid = G_("expected %<namespace%>");
29040 break;
29041 case RT_USING:
29042 gmsgid = G_("expected %<using%>");
29043 break;
29044 case RT_ASM:
29045 gmsgid = G_("expected %<asm%>");
29046 break;
29047 case RT_TRY:
29048 gmsgid = G_("expected %<try%>");
29049 break;
29050 case RT_CATCH:
29051 gmsgid = G_("expected %<catch%>");
29052 break;
29053 case RT_THROW:
29054 gmsgid = G_("expected %<throw%>");
29055 break;
29056 case RT_LABEL:
29057 gmsgid = G_("expected %<__label__%>");
29058 break;
29059 case RT_AT_TRY:
29060 gmsgid = G_("expected %<@try%>");
29061 break;
29062 case RT_AT_SYNCHRONIZED:
29063 gmsgid = G_("expected %<@synchronized%>");
29064 break;
29065 case RT_AT_THROW:
29066 gmsgid = G_("expected %<@throw%>");
29067 break;
29068 case RT_TRANSACTION_ATOMIC:
29069 gmsgid = G_("expected %<__transaction_atomic%>");
29070 break;
29071 case RT_TRANSACTION_RELAXED:
29072 gmsgid = G_("expected %<__transaction_relaxed%>");
29073 break;
29074 default:
29075 break;
29076 }
29077
29078 if (!gmsgid && !keyword)
29079 {
29080 switch (token_desc)
29081 {
29082 case RT_SEMICOLON:
29083 gmsgid = G_("expected %<;%>");
29084 break;
29085 case RT_OPEN_PAREN:
29086 gmsgid = G_("expected %<(%>");
29087 break;
29088 case RT_CLOSE_BRACE:
29089 gmsgid = G_("expected %<}%>");
29090 break;
29091 case RT_OPEN_BRACE:
29092 gmsgid = G_("expected %<{%>");
29093 break;
29094 case RT_CLOSE_SQUARE:
29095 gmsgid = G_("expected %<]%>");
29096 break;
29097 case RT_OPEN_SQUARE:
29098 gmsgid = G_("expected %<[%>");
29099 break;
29100 case RT_COMMA:
29101 gmsgid = G_("expected %<,%>");
29102 break;
29103 case RT_SCOPE:
29104 gmsgid = G_("expected %<::%>");
29105 break;
29106 case RT_LESS:
29107 gmsgid = G_("expected %<<%>");
29108 break;
29109 case RT_GREATER:
29110 gmsgid = G_("expected %<>%>");
29111 break;
29112 case RT_EQ:
29113 gmsgid = G_("expected %<=%>");
29114 break;
29115 case RT_ELLIPSIS:
29116 gmsgid = G_("expected %<...%>");
29117 break;
29118 case RT_MULT:
29119 gmsgid = G_("expected %<*%>");
29120 break;
29121 case RT_COMPL:
29122 gmsgid = G_("expected %<~%>");
29123 break;
29124 case RT_COLON:
29125 gmsgid = G_("expected %<:%>");
29126 break;
29127 case RT_COLON_SCOPE:
29128 gmsgid = G_("expected %<:%> or %<::%>");
29129 break;
29130 case RT_CLOSE_PAREN:
29131 gmsgid = G_("expected %<)%>");
29132 break;
29133 case RT_COMMA_CLOSE_PAREN:
29134 gmsgid = G_("expected %<,%> or %<)%>");
29135 break;
29136 case RT_PRAGMA_EOL:
29137 gmsgid = G_("expected end of line");
29138 break;
29139 case RT_NAME:
29140 gmsgid = G_("expected identifier");
29141 break;
29142 case RT_SELECT:
29143 gmsgid = G_("expected selection-statement");
29144 break;
29145 case RT_ITERATION:
29146 gmsgid = G_("expected iteration-statement");
29147 break;
29148 case RT_JUMP:
29149 gmsgid = G_("expected jump-statement");
29150 break;
29151 case RT_CLASS_KEY:
29152 gmsgid = G_("expected class-key");
29153 break;
29154 case RT_CLASS_TYPENAME_TEMPLATE:
29155 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
29156 break;
29157 default:
29158 gcc_unreachable ();
29159 }
29160 }
29161
29162 if (gmsgid)
29163 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
29164 }
29165
29166
29167 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29168 issue an error message indicating that TOKEN_DESC was expected.
29169
29170 Returns the token consumed, if the token had the appropriate type.
29171 Otherwise, returns NULL.
29172
29173 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29174 within any error as the location of an "opening" token matching
29175 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29176 RT_CLOSE_PAREN). */
29177
29178 static cp_token *
29179 cp_parser_require (cp_parser* parser,
29180 enum cpp_ttype type,
29181 required_token token_desc,
29182 location_t matching_location)
29183 {
29184 if (cp_lexer_next_token_is (parser->lexer, type))
29185 return cp_lexer_consume_token (parser->lexer);
29186 else
29187 {
29188 /* Output the MESSAGE -- unless we're parsing tentatively. */
29189 if (!cp_parser_simulate_error (parser))
29190 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29191 matching_location);
29192 return NULL;
29193 }
29194 }
29195
29196 /* An error message is produced if the next token is not '>'.
29197 All further tokens are skipped until the desired token is
29198 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29199
29200 static void
29201 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29202 {
29203 /* Current level of '< ... >'. */
29204 unsigned level = 0;
29205 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29206 unsigned nesting_depth = 0;
29207
29208 /* Are we ready, yet? If not, issue error message. */
29209 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29210 return;
29211
29212 /* Skip tokens until the desired token is found. */
29213 while (true)
29214 {
29215 /* Peek at the next token. */
29216 switch (cp_lexer_peek_token (parser->lexer)->type)
29217 {
29218 case CPP_LESS:
29219 if (!nesting_depth)
29220 ++level;
29221 break;
29222
29223 case CPP_RSHIFT:
29224 if (cxx_dialect == cxx98)
29225 /* C++0x views the `>>' operator as two `>' tokens, but
29226 C++98 does not. */
29227 break;
29228 else if (!nesting_depth && level-- == 0)
29229 {
29230 /* We've hit a `>>' where the first `>' closes the
29231 template argument list, and the second `>' is
29232 spurious. Just consume the `>>' and stop; we've
29233 already produced at least one error. */
29234 cp_lexer_consume_token (parser->lexer);
29235 return;
29236 }
29237 /* Fall through for C++0x, so we handle the second `>' in
29238 the `>>'. */
29239 gcc_fallthrough ();
29240
29241 case CPP_GREATER:
29242 if (!nesting_depth && level-- == 0)
29243 {
29244 /* We've reached the token we want, consume it and stop. */
29245 cp_lexer_consume_token (parser->lexer);
29246 return;
29247 }
29248 break;
29249
29250 case CPP_OPEN_PAREN:
29251 case CPP_OPEN_SQUARE:
29252 ++nesting_depth;
29253 break;
29254
29255 case CPP_CLOSE_PAREN:
29256 case CPP_CLOSE_SQUARE:
29257 if (nesting_depth-- == 0)
29258 return;
29259 break;
29260
29261 case CPP_EOF:
29262 case CPP_PRAGMA_EOL:
29263 case CPP_SEMICOLON:
29264 case CPP_OPEN_BRACE:
29265 case CPP_CLOSE_BRACE:
29266 /* The '>' was probably forgotten, don't look further. */
29267 return;
29268
29269 default:
29270 break;
29271 }
29272
29273 /* Consume this token. */
29274 cp_lexer_consume_token (parser->lexer);
29275 }
29276 }
29277
29278 /* If the next token is the indicated keyword, consume it. Otherwise,
29279 issue an error message indicating that TOKEN_DESC was expected.
29280
29281 Returns the token consumed, if the token had the appropriate type.
29282 Otherwise, returns NULL. */
29283
29284 static cp_token *
29285 cp_parser_require_keyword (cp_parser* parser,
29286 enum rid keyword,
29287 required_token token_desc)
29288 {
29289 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29290
29291 if (token && token->keyword != keyword)
29292 {
29293 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29294 UNKNOWN_LOCATION);
29295 return NULL;
29296 }
29297
29298 return token;
29299 }
29300
29301 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29302 function-definition. */
29303
29304 static bool
29305 cp_parser_token_starts_function_definition_p (cp_token* token)
29306 {
29307 return (/* An ordinary function-body begins with an `{'. */
29308 token->type == CPP_OPEN_BRACE
29309 /* A ctor-initializer begins with a `:'. */
29310 || token->type == CPP_COLON
29311 /* A function-try-block begins with `try'. */
29312 || token->keyword == RID_TRY
29313 /* A function-transaction-block begins with `__transaction_atomic'
29314 or `__transaction_relaxed'. */
29315 || token->keyword == RID_TRANSACTION_ATOMIC
29316 || token->keyword == RID_TRANSACTION_RELAXED
29317 /* The named return value extension begins with `return'. */
29318 || token->keyword == RID_RETURN);
29319 }
29320
29321 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29322 definition. */
29323
29324 static bool
29325 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29326 {
29327 cp_token *token;
29328
29329 token = cp_lexer_peek_token (parser->lexer);
29330 return (token->type == CPP_OPEN_BRACE
29331 || (token->type == CPP_COLON
29332 && !parser->colon_doesnt_start_class_def_p));
29333 }
29334
29335 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29336 C++0x) ending a template-argument. */
29337
29338 static bool
29339 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29340 {
29341 cp_token *token;
29342
29343 token = cp_lexer_peek_token (parser->lexer);
29344 return (token->type == CPP_COMMA
29345 || token->type == CPP_GREATER
29346 || token->type == CPP_ELLIPSIS
29347 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29348 }
29349
29350 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29351 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29352
29353 static bool
29354 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29355 size_t n)
29356 {
29357 cp_token *token;
29358
29359 token = cp_lexer_peek_nth_token (parser->lexer, n);
29360 if (token->type == CPP_LESS)
29361 return true;
29362 /* Check for the sequence `<::' in the original code. It would be lexed as
29363 `[:', where `[' is a digraph, and there is no whitespace before
29364 `:'. */
29365 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29366 {
29367 cp_token *token2;
29368 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29369 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29370 return true;
29371 }
29372 return false;
29373 }
29374
29375 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29376 or none_type otherwise. */
29377
29378 static enum tag_types
29379 cp_parser_token_is_class_key (cp_token* token)
29380 {
29381 switch (token->keyword)
29382 {
29383 case RID_CLASS:
29384 return class_type;
29385 case RID_STRUCT:
29386 return record_type;
29387 case RID_UNION:
29388 return union_type;
29389
29390 default:
29391 return none_type;
29392 }
29393 }
29394
29395 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29396 or none_type otherwise or if the token is null. */
29397
29398 static enum tag_types
29399 cp_parser_token_is_type_parameter_key (cp_token* token)
29400 {
29401 if (!token)
29402 return none_type;
29403
29404 switch (token->keyword)
29405 {
29406 case RID_CLASS:
29407 return class_type;
29408 case RID_TYPENAME:
29409 return typename_type;
29410
29411 default:
29412 return none_type;
29413 }
29414 }
29415
29416 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29417
29418 static void
29419 cp_parser_check_class_key (enum tag_types class_key, tree type)
29420 {
29421 if (type == error_mark_node)
29422 return;
29423 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29424 {
29425 if (permerror (input_location, "%qs tag used in naming %q#T",
29426 class_key == union_type ? "union"
29427 : class_key == record_type ? "struct" : "class",
29428 type))
29429 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29430 "%q#T was previously declared here", type);
29431 }
29432 }
29433
29434 /* Issue an error message if DECL is redeclared with different
29435 access than its original declaration [class.access.spec/3].
29436 This applies to nested classes, nested class templates and
29437 enumerations [class.mem/1]. */
29438
29439 static void
29440 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29441 {
29442 if (!decl
29443 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29444 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29445 return;
29446
29447 if ((TREE_PRIVATE (decl)
29448 != (current_access_specifier == access_private_node))
29449 || (TREE_PROTECTED (decl)
29450 != (current_access_specifier == access_protected_node)))
29451 error_at (location, "%qD redeclared with different access", decl);
29452 }
29453
29454 /* Look for the `template' keyword, as a syntactic disambiguator.
29455 Return TRUE iff it is present, in which case it will be
29456 consumed. */
29457
29458 static bool
29459 cp_parser_optional_template_keyword (cp_parser *parser)
29460 {
29461 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29462 {
29463 /* In C++98 the `template' keyword can only be used within templates;
29464 outside templates the parser can always figure out what is a
29465 template and what is not. In C++11, per the resolution of DR 468,
29466 `template' is allowed in cases where it is not strictly necessary. */
29467 if (!processing_template_decl
29468 && pedantic && cxx_dialect == cxx98)
29469 {
29470 cp_token *token = cp_lexer_peek_token (parser->lexer);
29471 pedwarn (token->location, OPT_Wpedantic,
29472 "in C++98 %<template%> (as a disambiguator) is only "
29473 "allowed within templates");
29474 /* If this part of the token stream is rescanned, the same
29475 error message would be generated. So, we purge the token
29476 from the stream. */
29477 cp_lexer_purge_token (parser->lexer);
29478 return false;
29479 }
29480 else
29481 {
29482 /* Consume the `template' keyword. */
29483 cp_lexer_consume_token (parser->lexer);
29484 return true;
29485 }
29486 }
29487 return false;
29488 }
29489
29490 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29491 set PARSER->SCOPE, and perform other related actions. */
29492
29493 static void
29494 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29495 {
29496 struct tree_check *check_value;
29497
29498 /* Get the stored value. */
29499 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29500 /* Set the scope from the stored value. */
29501 parser->scope = saved_checks_value (check_value);
29502 parser->qualifying_scope = check_value->qualifying_scope;
29503 parser->object_scope = NULL_TREE;
29504 }
29505
29506 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29507 encounter the end of a block before what we were looking for. */
29508
29509 static bool
29510 cp_parser_cache_group (cp_parser *parser,
29511 enum cpp_ttype end,
29512 unsigned depth)
29513 {
29514 while (true)
29515 {
29516 cp_token *token = cp_lexer_peek_token (parser->lexer);
29517
29518 /* Abort a parenthesized expression if we encounter a semicolon. */
29519 if ((end == CPP_CLOSE_PAREN || depth == 0)
29520 && token->type == CPP_SEMICOLON)
29521 return true;
29522 /* If we've reached the end of the file, stop. */
29523 if (token->type == CPP_EOF
29524 || (end != CPP_PRAGMA_EOL
29525 && token->type == CPP_PRAGMA_EOL))
29526 return true;
29527 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29528 /* We've hit the end of an enclosing block, so there's been some
29529 kind of syntax error. */
29530 return true;
29531
29532 /* Consume the token. */
29533 cp_lexer_consume_token (parser->lexer);
29534 /* See if it starts a new group. */
29535 if (token->type == CPP_OPEN_BRACE)
29536 {
29537 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29538 /* In theory this should probably check end == '}', but
29539 cp_parser_save_member_function_body needs it to exit
29540 after either '}' or ')' when called with ')'. */
29541 if (depth == 0)
29542 return false;
29543 }
29544 else if (token->type == CPP_OPEN_PAREN)
29545 {
29546 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29547 if (depth == 0 && end == CPP_CLOSE_PAREN)
29548 return false;
29549 }
29550 else if (token->type == CPP_PRAGMA)
29551 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29552 else if (token->type == end)
29553 return false;
29554 }
29555 }
29556
29557 /* Like above, for caching a default argument or NSDMI. Both of these are
29558 terminated by a non-nested comma, but it can be unclear whether or not a
29559 comma is nested in a template argument list unless we do more parsing.
29560 In order to handle this ambiguity, when we encounter a ',' after a '<'
29561 we try to parse what follows as a parameter-declaration-list (in the
29562 case of a default argument) or a member-declarator (in the case of an
29563 NSDMI). If that succeeds, then we stop caching. */
29564
29565 static tree
29566 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29567 {
29568 unsigned depth = 0;
29569 int maybe_template_id = 0;
29570 cp_token *first_token;
29571 cp_token *token;
29572 tree default_argument;
29573
29574 /* Add tokens until we have processed the entire default
29575 argument. We add the range [first_token, token). */
29576 first_token = cp_lexer_peek_token (parser->lexer);
29577 if (first_token->type == CPP_OPEN_BRACE)
29578 {
29579 /* For list-initialization, this is straightforward. */
29580 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29581 token = cp_lexer_peek_token (parser->lexer);
29582 }
29583 else while (true)
29584 {
29585 bool done = false;
29586
29587 /* Peek at the next token. */
29588 token = cp_lexer_peek_token (parser->lexer);
29589 /* What we do depends on what token we have. */
29590 switch (token->type)
29591 {
29592 /* In valid code, a default argument must be
29593 immediately followed by a `,' `)', or `...'. */
29594 case CPP_COMMA:
29595 if (depth == 0 && maybe_template_id)
29596 {
29597 /* If we've seen a '<', we might be in a
29598 template-argument-list. Until Core issue 325 is
29599 resolved, we don't know how this situation ought
29600 to be handled, so try to DTRT. We check whether
29601 what comes after the comma is a valid parameter
29602 declaration list. If it is, then the comma ends
29603 the default argument; otherwise the default
29604 argument continues. */
29605 bool error = false;
29606 cp_token *peek;
29607
29608 /* Set ITALP so cp_parser_parameter_declaration_list
29609 doesn't decide to commit to this parse. */
29610 bool saved_italp = parser->in_template_argument_list_p;
29611 parser->in_template_argument_list_p = true;
29612
29613 cp_parser_parse_tentatively (parser);
29614
29615 if (nsdmi)
29616 {
29617 /* Parse declarators until we reach a non-comma or
29618 somthing that cannot be an initializer.
29619 Just checking whether we're looking at a single
29620 declarator is insufficient. Consider:
29621 int var = tuple<T,U>::x;
29622 The template parameter 'U' looks exactly like a
29623 declarator. */
29624 do
29625 {
29626 int ctor_dtor_or_conv_p;
29627 cp_lexer_consume_token (parser->lexer);
29628 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29629 CP_PARSER_FLAGS_NONE,
29630 &ctor_dtor_or_conv_p,
29631 /*parenthesized_p=*/NULL,
29632 /*member_p=*/true,
29633 /*friend_p=*/false);
29634 peek = cp_lexer_peek_token (parser->lexer);
29635 if (cp_parser_error_occurred (parser))
29636 break;
29637 }
29638 while (peek->type == CPP_COMMA);
29639 /* If we met an '=' or ';' then the original comma
29640 was the end of the NSDMI. Otherwise assume
29641 we're still in the NSDMI. */
29642 error = (peek->type != CPP_EQ
29643 && peek->type != CPP_SEMICOLON);
29644 }
29645 else
29646 {
29647 cp_lexer_consume_token (parser->lexer);
29648 begin_scope (sk_function_parms, NULL_TREE);
29649 tree t = cp_parser_parameter_declaration_list
29650 (parser, CP_PARSER_FLAGS_NONE);
29651 if (t == error_mark_node)
29652 error = true;
29653 pop_bindings_and_leave_scope ();
29654 }
29655 if (!cp_parser_error_occurred (parser) && !error)
29656 done = true;
29657 cp_parser_abort_tentative_parse (parser);
29658
29659 parser->in_template_argument_list_p = saved_italp;
29660 break;
29661 }
29662 /* FALLTHRU */
29663 case CPP_CLOSE_PAREN:
29664 case CPP_ELLIPSIS:
29665 /* If we run into a non-nested `;', `}', or `]',
29666 then the code is invalid -- but the default
29667 argument is certainly over. */
29668 case CPP_SEMICOLON:
29669 case CPP_CLOSE_BRACE:
29670 case CPP_CLOSE_SQUARE:
29671 if (depth == 0
29672 /* Handle correctly int n = sizeof ... ( p ); */
29673 && token->type != CPP_ELLIPSIS)
29674 done = true;
29675 /* Update DEPTH, if necessary. */
29676 else if (token->type == CPP_CLOSE_PAREN
29677 || token->type == CPP_CLOSE_BRACE
29678 || token->type == CPP_CLOSE_SQUARE)
29679 --depth;
29680 break;
29681
29682 case CPP_OPEN_PAREN:
29683 case CPP_OPEN_SQUARE:
29684 case CPP_OPEN_BRACE:
29685 ++depth;
29686 break;
29687
29688 case CPP_LESS:
29689 if (depth == 0)
29690 /* This might be the comparison operator, or it might
29691 start a template argument list. */
29692 ++maybe_template_id;
29693 break;
29694
29695 case CPP_RSHIFT:
29696 if (cxx_dialect == cxx98)
29697 break;
29698 /* Fall through for C++0x, which treats the `>>'
29699 operator like two `>' tokens in certain
29700 cases. */
29701 gcc_fallthrough ();
29702
29703 case CPP_GREATER:
29704 if (depth == 0)
29705 {
29706 /* This might be an operator, or it might close a
29707 template argument list. But if a previous '<'
29708 started a template argument list, this will have
29709 closed it, so we can't be in one anymore. */
29710 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29711 if (maybe_template_id < 0)
29712 maybe_template_id = 0;
29713 }
29714 break;
29715
29716 /* If we run out of tokens, issue an error message. */
29717 case CPP_EOF:
29718 case CPP_PRAGMA_EOL:
29719 error_at (token->location, "file ends in default argument");
29720 return error_mark_node;
29721
29722 case CPP_NAME:
29723 case CPP_SCOPE:
29724 /* In these cases, we should look for template-ids.
29725 For example, if the default argument is
29726 `X<int, double>()', we need to do name lookup to
29727 figure out whether or not `X' is a template; if
29728 so, the `,' does not end the default argument.
29729
29730 That is not yet done. */
29731 break;
29732
29733 default:
29734 break;
29735 }
29736
29737 /* If we've reached the end, stop. */
29738 if (done)
29739 break;
29740
29741 /* Add the token to the token block. */
29742 token = cp_lexer_consume_token (parser->lexer);
29743 }
29744
29745 /* Create a DEFAULT_ARG to represent the unparsed default
29746 argument. */
29747 default_argument = make_node (DEFAULT_ARG);
29748 DEFARG_TOKENS (default_argument)
29749 = cp_token_cache_new (first_token, token);
29750 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29751
29752 return default_argument;
29753 }
29754
29755 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29756
29757 location_t
29758 defarg_location (tree default_argument)
29759 {
29760 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29761 location_t start = tokens->first->location;
29762 location_t end = tokens->last->location;
29763 return make_location (start, start, end);
29764 }
29765
29766 /* Begin parsing tentatively. We always save tokens while parsing
29767 tentatively so that if the tentative parsing fails we can restore the
29768 tokens. */
29769
29770 static void
29771 cp_parser_parse_tentatively (cp_parser* parser)
29772 {
29773 /* Enter a new parsing context. */
29774 parser->context = cp_parser_context_new (parser->context);
29775 /* Begin saving tokens. */
29776 cp_lexer_save_tokens (parser->lexer);
29777 /* In order to avoid repetitive access control error messages,
29778 access checks are queued up until we are no longer parsing
29779 tentatively. */
29780 push_deferring_access_checks (dk_deferred);
29781 }
29782
29783 /* Commit to the currently active tentative parse. */
29784
29785 static void
29786 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29787 {
29788 cp_parser_context *context;
29789 cp_lexer *lexer;
29790
29791 /* Mark all of the levels as committed. */
29792 lexer = parser->lexer;
29793 for (context = parser->context; context->next; context = context->next)
29794 {
29795 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29796 break;
29797 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29798 while (!cp_lexer_saving_tokens (lexer))
29799 lexer = lexer->next;
29800 cp_lexer_commit_tokens (lexer);
29801 }
29802 }
29803
29804 /* Commit to the topmost currently active tentative parse.
29805
29806 Note that this function shouldn't be called when there are
29807 irreversible side-effects while in a tentative state. For
29808 example, we shouldn't create a permanent entry in the symbol
29809 table, or issue an error message that might not apply if the
29810 tentative parse is aborted. */
29811
29812 static void
29813 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29814 {
29815 cp_parser_context *context = parser->context;
29816 cp_lexer *lexer = parser->lexer;
29817
29818 if (context)
29819 {
29820 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29821 return;
29822 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29823
29824 while (!cp_lexer_saving_tokens (lexer))
29825 lexer = lexer->next;
29826 cp_lexer_commit_tokens (lexer);
29827 }
29828 }
29829
29830 /* Abort the currently active tentative parse. All consumed tokens
29831 will be rolled back, and no diagnostics will be issued. */
29832
29833 static void
29834 cp_parser_abort_tentative_parse (cp_parser* parser)
29835 {
29836 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29837 || errorcount > 0);
29838 cp_parser_simulate_error (parser);
29839 /* Now, pretend that we want to see if the construct was
29840 successfully parsed. */
29841 cp_parser_parse_definitely (parser);
29842 }
29843
29844 /* Stop parsing tentatively. If a parse error has occurred, restore the
29845 token stream. Otherwise, commit to the tokens we have consumed.
29846 Returns true if no error occurred; false otherwise. */
29847
29848 static bool
29849 cp_parser_parse_definitely (cp_parser* parser)
29850 {
29851 bool error_occurred;
29852 cp_parser_context *context;
29853
29854 /* Remember whether or not an error occurred, since we are about to
29855 destroy that information. */
29856 error_occurred = cp_parser_error_occurred (parser);
29857 /* Remove the topmost context from the stack. */
29858 context = parser->context;
29859 parser->context = context->next;
29860 /* If no parse errors occurred, commit to the tentative parse. */
29861 if (!error_occurred)
29862 {
29863 /* Commit to the tokens read tentatively, unless that was
29864 already done. */
29865 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29866 cp_lexer_commit_tokens (parser->lexer);
29867
29868 pop_to_parent_deferring_access_checks ();
29869 }
29870 /* Otherwise, if errors occurred, roll back our state so that things
29871 are just as they were before we began the tentative parse. */
29872 else
29873 {
29874 cp_lexer_rollback_tokens (parser->lexer);
29875 pop_deferring_access_checks ();
29876 }
29877 /* Add the context to the front of the free list. */
29878 context->next = cp_parser_context_free_list;
29879 cp_parser_context_free_list = context;
29880
29881 return !error_occurred;
29882 }
29883
29884 /* Returns true if we are parsing tentatively and are not committed to
29885 this tentative parse. */
29886
29887 static bool
29888 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29889 {
29890 return (cp_parser_parsing_tentatively (parser)
29891 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29892 }
29893
29894 /* Returns nonzero iff an error has occurred during the most recent
29895 tentative parse. */
29896
29897 static bool
29898 cp_parser_error_occurred (cp_parser* parser)
29899 {
29900 return (cp_parser_parsing_tentatively (parser)
29901 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29902 }
29903
29904 /* Returns nonzero if GNU extensions are allowed. */
29905
29906 static bool
29907 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29908 {
29909 return parser->allow_gnu_extensions_p;
29910 }
29911 \f
29912 /* Objective-C++ Productions */
29913
29914
29915 /* Parse an Objective-C expression, which feeds into a primary-expression
29916 above.
29917
29918 objc-expression:
29919 objc-message-expression
29920 objc-string-literal
29921 objc-encode-expression
29922 objc-protocol-expression
29923 objc-selector-expression
29924
29925 Returns a tree representation of the expression. */
29926
29927 static cp_expr
29928 cp_parser_objc_expression (cp_parser* parser)
29929 {
29930 /* Try to figure out what kind of declaration is present. */
29931 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29932
29933 switch (kwd->type)
29934 {
29935 case CPP_OPEN_SQUARE:
29936 return cp_parser_objc_message_expression (parser);
29937
29938 case CPP_OBJC_STRING:
29939 kwd = cp_lexer_consume_token (parser->lexer);
29940 return objc_build_string_object (kwd->u.value);
29941
29942 case CPP_KEYWORD:
29943 switch (kwd->keyword)
29944 {
29945 case RID_AT_ENCODE:
29946 return cp_parser_objc_encode_expression (parser);
29947
29948 case RID_AT_PROTOCOL:
29949 return cp_parser_objc_protocol_expression (parser);
29950
29951 case RID_AT_SELECTOR:
29952 return cp_parser_objc_selector_expression (parser);
29953
29954 default:
29955 break;
29956 }
29957 /* FALLTHRU */
29958 default:
29959 error_at (kwd->location,
29960 "misplaced %<@%D%> Objective-C++ construct",
29961 kwd->u.value);
29962 cp_parser_skip_to_end_of_block_or_statement (parser);
29963 }
29964
29965 return error_mark_node;
29966 }
29967
29968 /* Parse an Objective-C message expression.
29969
29970 objc-message-expression:
29971 [ objc-message-receiver objc-message-args ]
29972
29973 Returns a representation of an Objective-C message. */
29974
29975 static tree
29976 cp_parser_objc_message_expression (cp_parser* parser)
29977 {
29978 tree receiver, messageargs;
29979
29980 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29981 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29982 receiver = cp_parser_objc_message_receiver (parser);
29983 messageargs = cp_parser_objc_message_args (parser);
29984 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29985 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29986
29987 tree result = objc_build_message_expr (receiver, messageargs);
29988
29989 /* Construct a location e.g.
29990 [self func1:5]
29991 ^~~~~~~~~~~~~~
29992 ranging from the '[' to the ']', with the caret at the start. */
29993 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29994 protected_set_expr_location (result, combined_loc);
29995
29996 return result;
29997 }
29998
29999 /* Parse an objc-message-receiver.
30000
30001 objc-message-receiver:
30002 expression
30003 simple-type-specifier
30004
30005 Returns a representation of the type or expression. */
30006
30007 static tree
30008 cp_parser_objc_message_receiver (cp_parser* parser)
30009 {
30010 tree rcv;
30011
30012 /* An Objective-C message receiver may be either (1) a type
30013 or (2) an expression. */
30014 cp_parser_parse_tentatively (parser);
30015 rcv = cp_parser_expression (parser);
30016
30017 /* If that worked out, fine. */
30018 if (cp_parser_parse_definitely (parser))
30019 return rcv;
30020
30021 cp_parser_parse_tentatively (parser);
30022 rcv = cp_parser_simple_type_specifier (parser,
30023 /*decl_specs=*/NULL,
30024 CP_PARSER_FLAGS_NONE);
30025
30026 if (cp_parser_parse_definitely (parser))
30027 return objc_get_class_reference (rcv);
30028
30029 cp_parser_error (parser, "objective-c++ message receiver expected");
30030 return error_mark_node;
30031 }
30032
30033 /* Parse the arguments and selectors comprising an Objective-C message.
30034
30035 objc-message-args:
30036 objc-selector
30037 objc-selector-args
30038 objc-selector-args , objc-comma-args
30039
30040 objc-selector-args:
30041 objc-selector [opt] : assignment-expression
30042 objc-selector-args objc-selector [opt] : assignment-expression
30043
30044 objc-comma-args:
30045 assignment-expression
30046 objc-comma-args , assignment-expression
30047
30048 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30049 selector arguments and TREE_VALUE containing a list of comma
30050 arguments. */
30051
30052 static tree
30053 cp_parser_objc_message_args (cp_parser* parser)
30054 {
30055 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
30056 bool maybe_unary_selector_p = true;
30057 cp_token *token = cp_lexer_peek_token (parser->lexer);
30058
30059 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30060 {
30061 tree selector = NULL_TREE, arg;
30062
30063 if (token->type != CPP_COLON)
30064 selector = cp_parser_objc_selector (parser);
30065
30066 /* Detect if we have a unary selector. */
30067 if (maybe_unary_selector_p
30068 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30069 return build_tree_list (selector, NULL_TREE);
30070
30071 maybe_unary_selector_p = false;
30072 cp_parser_require (parser, CPP_COLON, RT_COLON);
30073 arg = cp_parser_assignment_expression (parser);
30074
30075 sel_args
30076 = chainon (sel_args,
30077 build_tree_list (selector, arg));
30078
30079 token = cp_lexer_peek_token (parser->lexer);
30080 }
30081
30082 /* Handle non-selector arguments, if any. */
30083 while (token->type == CPP_COMMA)
30084 {
30085 tree arg;
30086
30087 cp_lexer_consume_token (parser->lexer);
30088 arg = cp_parser_assignment_expression (parser);
30089
30090 addl_args
30091 = chainon (addl_args,
30092 build_tree_list (NULL_TREE, arg));
30093
30094 token = cp_lexer_peek_token (parser->lexer);
30095 }
30096
30097 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
30098 {
30099 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
30100 return build_tree_list (error_mark_node, error_mark_node);
30101 }
30102
30103 return build_tree_list (sel_args, addl_args);
30104 }
30105
30106 /* Parse an Objective-C encode expression.
30107
30108 objc-encode-expression:
30109 @encode objc-typename
30110
30111 Returns an encoded representation of the type argument. */
30112
30113 static cp_expr
30114 cp_parser_objc_encode_expression (cp_parser* parser)
30115 {
30116 tree type;
30117 cp_token *token;
30118 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30119
30120 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
30121 matching_parens parens;
30122 parens.require_open (parser);
30123 token = cp_lexer_peek_token (parser->lexer);
30124 type = complete_type (cp_parser_type_id (parser));
30125 parens.require_close (parser);
30126
30127 if (!type)
30128 {
30129 error_at (token->location,
30130 "%<@encode%> must specify a type as an argument");
30131 return error_mark_node;
30132 }
30133
30134 /* This happens if we find @encode(T) (where T is a template
30135 typename or something dependent on a template typename) when
30136 parsing a template. In that case, we can't compile it
30137 immediately, but we rather create an AT_ENCODE_EXPR which will
30138 need to be instantiated when the template is used.
30139 */
30140 if (dependent_type_p (type))
30141 {
30142 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
30143 TREE_READONLY (value) = 1;
30144 return value;
30145 }
30146
30147
30148 /* Build a location of the form:
30149 @encode(int)
30150 ^~~~~~~~~~~~
30151 with caret==start at the @ token, finishing at the close paren. */
30152 location_t combined_loc
30153 = make_location (start_loc, start_loc,
30154 cp_lexer_previous_token (parser->lexer)->location);
30155
30156 return cp_expr (objc_build_encode_expr (type), combined_loc);
30157 }
30158
30159 /* Parse an Objective-C @defs expression. */
30160
30161 static tree
30162 cp_parser_objc_defs_expression (cp_parser *parser)
30163 {
30164 tree name;
30165
30166 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
30167 matching_parens parens;
30168 parens.require_open (parser);
30169 name = cp_parser_identifier (parser);
30170 parens.require_close (parser);
30171
30172 return objc_get_class_ivars (name);
30173 }
30174
30175 /* Parse an Objective-C protocol expression.
30176
30177 objc-protocol-expression:
30178 @protocol ( identifier )
30179
30180 Returns a representation of the protocol expression. */
30181
30182 static tree
30183 cp_parser_objc_protocol_expression (cp_parser* parser)
30184 {
30185 tree proto;
30186 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30187
30188 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30189 matching_parens parens;
30190 parens.require_open (parser);
30191 proto = cp_parser_identifier (parser);
30192 parens.require_close (parser);
30193
30194 /* Build a location of the form:
30195 @protocol(prot)
30196 ^~~~~~~~~~~~~~~
30197 with caret==start at the @ token, finishing at the close paren. */
30198 location_t combined_loc
30199 = make_location (start_loc, start_loc,
30200 cp_lexer_previous_token (parser->lexer)->location);
30201 tree result = objc_build_protocol_expr (proto);
30202 protected_set_expr_location (result, combined_loc);
30203 return result;
30204 }
30205
30206 /* Parse an Objective-C selector expression.
30207
30208 objc-selector-expression:
30209 @selector ( objc-method-signature )
30210
30211 objc-method-signature:
30212 objc-selector
30213 objc-selector-seq
30214
30215 objc-selector-seq:
30216 objc-selector :
30217 objc-selector-seq objc-selector :
30218
30219 Returns a representation of the method selector. */
30220
30221 static tree
30222 cp_parser_objc_selector_expression (cp_parser* parser)
30223 {
30224 tree sel_seq = NULL_TREE;
30225 bool maybe_unary_selector_p = true;
30226 cp_token *token;
30227 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30228
30229 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30230 matching_parens parens;
30231 parens.require_open (parser);
30232 token = cp_lexer_peek_token (parser->lexer);
30233
30234 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30235 || token->type == CPP_SCOPE)
30236 {
30237 tree selector = NULL_TREE;
30238
30239 if (token->type != CPP_COLON
30240 || token->type == CPP_SCOPE)
30241 selector = cp_parser_objc_selector (parser);
30242
30243 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30244 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30245 {
30246 /* Detect if we have a unary selector. */
30247 if (maybe_unary_selector_p)
30248 {
30249 sel_seq = selector;
30250 goto finish_selector;
30251 }
30252 else
30253 {
30254 cp_parser_error (parser, "expected %<:%>");
30255 }
30256 }
30257 maybe_unary_selector_p = false;
30258 token = cp_lexer_consume_token (parser->lexer);
30259
30260 if (token->type == CPP_SCOPE)
30261 {
30262 sel_seq
30263 = chainon (sel_seq,
30264 build_tree_list (selector, NULL_TREE));
30265 sel_seq
30266 = chainon (sel_seq,
30267 build_tree_list (NULL_TREE, NULL_TREE));
30268 }
30269 else
30270 sel_seq
30271 = chainon (sel_seq,
30272 build_tree_list (selector, NULL_TREE));
30273
30274 token = cp_lexer_peek_token (parser->lexer);
30275 }
30276
30277 finish_selector:
30278 parens.require_close (parser);
30279
30280
30281 /* Build a location of the form:
30282 @selector(func)
30283 ^~~~~~~~~~~~~~~
30284 with caret==start at the @ token, finishing at the close paren. */
30285 location_t combined_loc
30286 = make_location (loc, loc,
30287 cp_lexer_previous_token (parser->lexer)->location);
30288 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30289 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30290 protected_set_expr_location (result, combined_loc);
30291 return result;
30292 }
30293
30294 /* Parse a list of identifiers.
30295
30296 objc-identifier-list:
30297 identifier
30298 objc-identifier-list , identifier
30299
30300 Returns a TREE_LIST of identifier nodes. */
30301
30302 static tree
30303 cp_parser_objc_identifier_list (cp_parser* parser)
30304 {
30305 tree identifier;
30306 tree list;
30307 cp_token *sep;
30308
30309 identifier = cp_parser_identifier (parser);
30310 if (identifier == error_mark_node)
30311 return error_mark_node;
30312
30313 list = build_tree_list (NULL_TREE, identifier);
30314 sep = cp_lexer_peek_token (parser->lexer);
30315
30316 while (sep->type == CPP_COMMA)
30317 {
30318 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30319 identifier = cp_parser_identifier (parser);
30320 if (identifier == error_mark_node)
30321 return list;
30322
30323 list = chainon (list, build_tree_list (NULL_TREE,
30324 identifier));
30325 sep = cp_lexer_peek_token (parser->lexer);
30326 }
30327
30328 return list;
30329 }
30330
30331 /* Parse an Objective-C alias declaration.
30332
30333 objc-alias-declaration:
30334 @compatibility_alias identifier identifier ;
30335
30336 This function registers the alias mapping with the Objective-C front end.
30337 It returns nothing. */
30338
30339 static void
30340 cp_parser_objc_alias_declaration (cp_parser* parser)
30341 {
30342 tree alias, orig;
30343
30344 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30345 alias = cp_parser_identifier (parser);
30346 orig = cp_parser_identifier (parser);
30347 objc_declare_alias (alias, orig);
30348 cp_parser_consume_semicolon_at_end_of_statement (parser);
30349 }
30350
30351 /* Parse an Objective-C class forward-declaration.
30352
30353 objc-class-declaration:
30354 @class objc-identifier-list ;
30355
30356 The function registers the forward declarations with the Objective-C
30357 front end. It returns nothing. */
30358
30359 static void
30360 cp_parser_objc_class_declaration (cp_parser* parser)
30361 {
30362 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30363 while (true)
30364 {
30365 tree id;
30366
30367 id = cp_parser_identifier (parser);
30368 if (id == error_mark_node)
30369 break;
30370
30371 objc_declare_class (id);
30372
30373 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30374 cp_lexer_consume_token (parser->lexer);
30375 else
30376 break;
30377 }
30378 cp_parser_consume_semicolon_at_end_of_statement (parser);
30379 }
30380
30381 /* Parse a list of Objective-C protocol references.
30382
30383 objc-protocol-refs-opt:
30384 objc-protocol-refs [opt]
30385
30386 objc-protocol-refs:
30387 < objc-identifier-list >
30388
30389 Returns a TREE_LIST of identifiers, if any. */
30390
30391 static tree
30392 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30393 {
30394 tree protorefs = NULL_TREE;
30395
30396 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30397 {
30398 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30399 protorefs = cp_parser_objc_identifier_list (parser);
30400 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30401 }
30402
30403 return protorefs;
30404 }
30405
30406 /* Parse a Objective-C visibility specification. */
30407
30408 static void
30409 cp_parser_objc_visibility_spec (cp_parser* parser)
30410 {
30411 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30412
30413 switch (vis->keyword)
30414 {
30415 case RID_AT_PRIVATE:
30416 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30417 break;
30418 case RID_AT_PROTECTED:
30419 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30420 break;
30421 case RID_AT_PUBLIC:
30422 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30423 break;
30424 case RID_AT_PACKAGE:
30425 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30426 break;
30427 default:
30428 return;
30429 }
30430
30431 /* Eat '@private'/'@protected'/'@public'. */
30432 cp_lexer_consume_token (parser->lexer);
30433 }
30434
30435 /* Parse an Objective-C method type. Return 'true' if it is a class
30436 (+) method, and 'false' if it is an instance (-) method. */
30437
30438 static inline bool
30439 cp_parser_objc_method_type (cp_parser* parser)
30440 {
30441 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30442 return true;
30443 else
30444 return false;
30445 }
30446
30447 /* Parse an Objective-C protocol qualifier. */
30448
30449 static tree
30450 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30451 {
30452 tree quals = NULL_TREE, node;
30453 cp_token *token = cp_lexer_peek_token (parser->lexer);
30454
30455 node = token->u.value;
30456
30457 while (node && identifier_p (node)
30458 && (node == ridpointers [(int) RID_IN]
30459 || node == ridpointers [(int) RID_OUT]
30460 || node == ridpointers [(int) RID_INOUT]
30461 || node == ridpointers [(int) RID_BYCOPY]
30462 || node == ridpointers [(int) RID_BYREF]
30463 || node == ridpointers [(int) RID_ONEWAY]))
30464 {
30465 quals = tree_cons (NULL_TREE, node, quals);
30466 cp_lexer_consume_token (parser->lexer);
30467 token = cp_lexer_peek_token (parser->lexer);
30468 node = token->u.value;
30469 }
30470
30471 return quals;
30472 }
30473
30474 /* Parse an Objective-C typename. */
30475
30476 static tree
30477 cp_parser_objc_typename (cp_parser* parser)
30478 {
30479 tree type_name = NULL_TREE;
30480
30481 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30482 {
30483 tree proto_quals, cp_type = NULL_TREE;
30484
30485 matching_parens parens;
30486 parens.consume_open (parser); /* Eat '('. */
30487 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30488
30489 /* An ObjC type name may consist of just protocol qualifiers, in which
30490 case the type shall default to 'id'. */
30491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30492 {
30493 cp_type = cp_parser_type_id (parser);
30494
30495 /* If the type could not be parsed, an error has already
30496 been produced. For error recovery, behave as if it had
30497 not been specified, which will use the default type
30498 'id'. */
30499 if (cp_type == error_mark_node)
30500 {
30501 cp_type = NULL_TREE;
30502 /* We need to skip to the closing parenthesis as
30503 cp_parser_type_id() does not seem to do it for
30504 us. */
30505 cp_parser_skip_to_closing_parenthesis (parser,
30506 /*recovering=*/true,
30507 /*or_comma=*/false,
30508 /*consume_paren=*/false);
30509 }
30510 }
30511
30512 parens.require_close (parser);
30513 type_name = build_tree_list (proto_quals, cp_type);
30514 }
30515
30516 return type_name;
30517 }
30518
30519 /* Check to see if TYPE refers to an Objective-C selector name. */
30520
30521 static bool
30522 cp_parser_objc_selector_p (enum cpp_ttype type)
30523 {
30524 return (type == CPP_NAME || type == CPP_KEYWORD
30525 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30526 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30527 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30528 || type == CPP_XOR || type == CPP_XOR_EQ);
30529 }
30530
30531 /* Parse an Objective-C selector. */
30532
30533 static tree
30534 cp_parser_objc_selector (cp_parser* parser)
30535 {
30536 cp_token *token = cp_lexer_consume_token (parser->lexer);
30537
30538 if (!cp_parser_objc_selector_p (token->type))
30539 {
30540 error_at (token->location, "invalid Objective-C++ selector name");
30541 return error_mark_node;
30542 }
30543
30544 /* C++ operator names are allowed to appear in ObjC selectors. */
30545 switch (token->type)
30546 {
30547 case CPP_AND_AND: return get_identifier ("and");
30548 case CPP_AND_EQ: return get_identifier ("and_eq");
30549 case CPP_AND: return get_identifier ("bitand");
30550 case CPP_OR: return get_identifier ("bitor");
30551 case CPP_COMPL: return get_identifier ("compl");
30552 case CPP_NOT: return get_identifier ("not");
30553 case CPP_NOT_EQ: return get_identifier ("not_eq");
30554 case CPP_OR_OR: return get_identifier ("or");
30555 case CPP_OR_EQ: return get_identifier ("or_eq");
30556 case CPP_XOR: return get_identifier ("xor");
30557 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30558 default: return token->u.value;
30559 }
30560 }
30561
30562 /* Parse an Objective-C params list. */
30563
30564 static tree
30565 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30566 {
30567 tree params = NULL_TREE;
30568 bool maybe_unary_selector_p = true;
30569 cp_token *token = cp_lexer_peek_token (parser->lexer);
30570
30571 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30572 {
30573 tree selector = NULL_TREE, type_name, identifier;
30574 tree parm_attr = NULL_TREE;
30575
30576 if (token->keyword == RID_ATTRIBUTE)
30577 break;
30578
30579 if (token->type != CPP_COLON)
30580 selector = cp_parser_objc_selector (parser);
30581
30582 /* Detect if we have a unary selector. */
30583 if (maybe_unary_selector_p
30584 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30585 {
30586 params = selector; /* Might be followed by attributes. */
30587 break;
30588 }
30589
30590 maybe_unary_selector_p = false;
30591 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30592 {
30593 /* Something went quite wrong. There should be a colon
30594 here, but there is not. Stop parsing parameters. */
30595 break;
30596 }
30597 type_name = cp_parser_objc_typename (parser);
30598 /* New ObjC allows attributes on parameters too. */
30599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30600 parm_attr = cp_parser_attributes_opt (parser);
30601 identifier = cp_parser_identifier (parser);
30602
30603 params
30604 = chainon (params,
30605 objc_build_keyword_decl (selector,
30606 type_name,
30607 identifier,
30608 parm_attr));
30609
30610 token = cp_lexer_peek_token (parser->lexer);
30611 }
30612
30613 if (params == NULL_TREE)
30614 {
30615 cp_parser_error (parser, "objective-c++ method declaration is expected");
30616 return error_mark_node;
30617 }
30618
30619 /* We allow tail attributes for the method. */
30620 if (token->keyword == RID_ATTRIBUTE)
30621 {
30622 *attributes = cp_parser_attributes_opt (parser);
30623 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30624 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30625 return params;
30626 cp_parser_error (parser,
30627 "method attributes must be specified at the end");
30628 return error_mark_node;
30629 }
30630
30631 if (params == NULL_TREE)
30632 {
30633 cp_parser_error (parser, "objective-c++ method declaration is expected");
30634 return error_mark_node;
30635 }
30636 return params;
30637 }
30638
30639 /* Parse the non-keyword Objective-C params. */
30640
30641 static tree
30642 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30643 tree* attributes)
30644 {
30645 tree params = make_node (TREE_LIST);
30646 cp_token *token = cp_lexer_peek_token (parser->lexer);
30647 *ellipsisp = false; /* Initially, assume no ellipsis. */
30648
30649 while (token->type == CPP_COMMA)
30650 {
30651 cp_parameter_declarator *parmdecl;
30652 tree parm;
30653
30654 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30655 token = cp_lexer_peek_token (parser->lexer);
30656
30657 if (token->type == CPP_ELLIPSIS)
30658 {
30659 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30660 *ellipsisp = true;
30661 token = cp_lexer_peek_token (parser->lexer);
30662 break;
30663 }
30664
30665 /* TODO: parse attributes for tail parameters. */
30666 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
30667 false, NULL);
30668 parm = grokdeclarator (parmdecl->declarator,
30669 &parmdecl->decl_specifiers,
30670 PARM, /*initialized=*/0,
30671 /*attrlist=*/NULL);
30672
30673 chainon (params, build_tree_list (NULL_TREE, parm));
30674 token = cp_lexer_peek_token (parser->lexer);
30675 }
30676
30677 /* We allow tail attributes for the method. */
30678 if (token->keyword == RID_ATTRIBUTE)
30679 {
30680 if (*attributes == NULL_TREE)
30681 {
30682 *attributes = cp_parser_attributes_opt (parser);
30683 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30684 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30685 return params;
30686 }
30687 else
30688 /* We have an error, but parse the attributes, so that we can
30689 carry on. */
30690 *attributes = cp_parser_attributes_opt (parser);
30691
30692 cp_parser_error (parser,
30693 "method attributes must be specified at the end");
30694 return error_mark_node;
30695 }
30696
30697 return params;
30698 }
30699
30700 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30701
30702 static void
30703 cp_parser_objc_interstitial_code (cp_parser* parser)
30704 {
30705 cp_token *token = cp_lexer_peek_token (parser->lexer);
30706
30707 /* If the next token is `extern' and the following token is a string
30708 literal, then we have a linkage specification. */
30709 if (token->keyword == RID_EXTERN
30710 && cp_parser_is_pure_string_literal
30711 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30712 cp_parser_linkage_specification (parser);
30713 /* Handle #pragma, if any. */
30714 else if (token->type == CPP_PRAGMA)
30715 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30716 /* Allow stray semicolons. */
30717 else if (token->type == CPP_SEMICOLON)
30718 cp_lexer_consume_token (parser->lexer);
30719 /* Mark methods as optional or required, when building protocols. */
30720 else if (token->keyword == RID_AT_OPTIONAL)
30721 {
30722 cp_lexer_consume_token (parser->lexer);
30723 objc_set_method_opt (true);
30724 }
30725 else if (token->keyword == RID_AT_REQUIRED)
30726 {
30727 cp_lexer_consume_token (parser->lexer);
30728 objc_set_method_opt (false);
30729 }
30730 else if (token->keyword == RID_NAMESPACE)
30731 cp_parser_namespace_definition (parser);
30732 /* Other stray characters must generate errors. */
30733 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30734 {
30735 cp_lexer_consume_token (parser->lexer);
30736 error ("stray %qs between Objective-C++ methods",
30737 token->type == CPP_OPEN_BRACE ? "{" : "}");
30738 }
30739 /* Finally, try to parse a block-declaration, or a function-definition. */
30740 else
30741 cp_parser_block_declaration (parser, /*statement_p=*/false);
30742 }
30743
30744 /* Parse a method signature. */
30745
30746 static tree
30747 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30748 {
30749 tree rettype, kwdparms, optparms;
30750 bool ellipsis = false;
30751 bool is_class_method;
30752
30753 is_class_method = cp_parser_objc_method_type (parser);
30754 rettype = cp_parser_objc_typename (parser);
30755 *attributes = NULL_TREE;
30756 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30757 if (kwdparms == error_mark_node)
30758 return error_mark_node;
30759 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30760 if (optparms == error_mark_node)
30761 return error_mark_node;
30762
30763 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30764 }
30765
30766 static bool
30767 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30768 {
30769 tree tattr;
30770 cp_lexer_save_tokens (parser->lexer);
30771 tattr = cp_parser_attributes_opt (parser);
30772 gcc_assert (tattr) ;
30773
30774 /* If the attributes are followed by a method introducer, this is not allowed.
30775 Dump the attributes and flag the situation. */
30776 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30777 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30778 return true;
30779
30780 /* Otherwise, the attributes introduce some interstitial code, possibly so
30781 rewind to allow that check. */
30782 cp_lexer_rollback_tokens (parser->lexer);
30783 return false;
30784 }
30785
30786 /* Parse an Objective-C method prototype list. */
30787
30788 static void
30789 cp_parser_objc_method_prototype_list (cp_parser* parser)
30790 {
30791 cp_token *token = cp_lexer_peek_token (parser->lexer);
30792
30793 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30794 {
30795 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30796 {
30797 tree attributes, sig;
30798 bool is_class_method;
30799 if (token->type == CPP_PLUS)
30800 is_class_method = true;
30801 else
30802 is_class_method = false;
30803 sig = cp_parser_objc_method_signature (parser, &attributes);
30804 if (sig == error_mark_node)
30805 {
30806 cp_parser_skip_to_end_of_block_or_statement (parser);
30807 token = cp_lexer_peek_token (parser->lexer);
30808 continue;
30809 }
30810 objc_add_method_declaration (is_class_method, sig, attributes);
30811 cp_parser_consume_semicolon_at_end_of_statement (parser);
30812 }
30813 else if (token->keyword == RID_AT_PROPERTY)
30814 cp_parser_objc_at_property_declaration (parser);
30815 else if (token->keyword == RID_ATTRIBUTE
30816 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30817 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30818 OPT_Wattributes,
30819 "prefix attributes are ignored for methods");
30820 else
30821 /* Allow for interspersed non-ObjC++ code. */
30822 cp_parser_objc_interstitial_code (parser);
30823
30824 token = cp_lexer_peek_token (parser->lexer);
30825 }
30826
30827 if (token->type != CPP_EOF)
30828 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30829 else
30830 cp_parser_error (parser, "expected %<@end%>");
30831
30832 objc_finish_interface ();
30833 }
30834
30835 /* Parse an Objective-C method definition list. */
30836
30837 static void
30838 cp_parser_objc_method_definition_list (cp_parser* parser)
30839 {
30840 cp_token *token = cp_lexer_peek_token (parser->lexer);
30841
30842 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30843 {
30844 tree meth;
30845
30846 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30847 {
30848 cp_token *ptk;
30849 tree sig, attribute;
30850 bool is_class_method;
30851 if (token->type == CPP_PLUS)
30852 is_class_method = true;
30853 else
30854 is_class_method = false;
30855 push_deferring_access_checks (dk_deferred);
30856 sig = cp_parser_objc_method_signature (parser, &attribute);
30857 if (sig == error_mark_node)
30858 {
30859 cp_parser_skip_to_end_of_block_or_statement (parser);
30860 token = cp_lexer_peek_token (parser->lexer);
30861 continue;
30862 }
30863 objc_start_method_definition (is_class_method, sig, attribute,
30864 NULL_TREE);
30865
30866 /* For historical reasons, we accept an optional semicolon. */
30867 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30868 cp_lexer_consume_token (parser->lexer);
30869
30870 ptk = cp_lexer_peek_token (parser->lexer);
30871 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30872 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30873 {
30874 perform_deferred_access_checks (tf_warning_or_error);
30875 stop_deferring_access_checks ();
30876 meth = cp_parser_function_definition_after_declarator (parser,
30877 false);
30878 pop_deferring_access_checks ();
30879 objc_finish_method_definition (meth);
30880 }
30881 }
30882 /* The following case will be removed once @synthesize is
30883 completely implemented. */
30884 else if (token->keyword == RID_AT_PROPERTY)
30885 cp_parser_objc_at_property_declaration (parser);
30886 else if (token->keyword == RID_AT_SYNTHESIZE)
30887 cp_parser_objc_at_synthesize_declaration (parser);
30888 else if (token->keyword == RID_AT_DYNAMIC)
30889 cp_parser_objc_at_dynamic_declaration (parser);
30890 else if (token->keyword == RID_ATTRIBUTE
30891 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30892 warning_at (token->location, OPT_Wattributes,
30893 "prefix attributes are ignored for methods");
30894 else
30895 /* Allow for interspersed non-ObjC++ code. */
30896 cp_parser_objc_interstitial_code (parser);
30897
30898 token = cp_lexer_peek_token (parser->lexer);
30899 }
30900
30901 if (token->type != CPP_EOF)
30902 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30903 else
30904 cp_parser_error (parser, "expected %<@end%>");
30905
30906 objc_finish_implementation ();
30907 }
30908
30909 /* Parse Objective-C ivars. */
30910
30911 static void
30912 cp_parser_objc_class_ivars (cp_parser* parser)
30913 {
30914 cp_token *token = cp_lexer_peek_token (parser->lexer);
30915
30916 if (token->type != CPP_OPEN_BRACE)
30917 return; /* No ivars specified. */
30918
30919 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30920 token = cp_lexer_peek_token (parser->lexer);
30921
30922 while (token->type != CPP_CLOSE_BRACE
30923 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30924 {
30925 cp_decl_specifier_seq declspecs;
30926 int decl_class_or_enum_p;
30927 tree prefix_attributes;
30928
30929 cp_parser_objc_visibility_spec (parser);
30930
30931 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30932 break;
30933
30934 cp_parser_decl_specifier_seq (parser,
30935 CP_PARSER_FLAGS_OPTIONAL,
30936 &declspecs,
30937 &decl_class_or_enum_p);
30938
30939 /* auto, register, static, extern, mutable. */
30940 if (declspecs.storage_class != sc_none)
30941 {
30942 cp_parser_error (parser, "invalid type for instance variable");
30943 declspecs.storage_class = sc_none;
30944 }
30945
30946 /* thread_local. */
30947 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30948 {
30949 cp_parser_error (parser, "invalid type for instance variable");
30950 declspecs.locations[ds_thread] = 0;
30951 }
30952
30953 /* typedef. */
30954 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30955 {
30956 cp_parser_error (parser, "invalid type for instance variable");
30957 declspecs.locations[ds_typedef] = 0;
30958 }
30959
30960 prefix_attributes = declspecs.attributes;
30961 declspecs.attributes = NULL_TREE;
30962
30963 /* Keep going until we hit the `;' at the end of the
30964 declaration. */
30965 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30966 {
30967 tree width = NULL_TREE, attributes, first_attribute, decl;
30968 cp_declarator *declarator = NULL;
30969 int ctor_dtor_or_conv_p;
30970
30971 /* Check for a (possibly unnamed) bitfield declaration. */
30972 token = cp_lexer_peek_token (parser->lexer);
30973 if (token->type == CPP_COLON)
30974 goto eat_colon;
30975
30976 if (token->type == CPP_NAME
30977 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30978 == CPP_COLON))
30979 {
30980 /* Get the name of the bitfield. */
30981 declarator = make_id_declarator (NULL_TREE,
30982 cp_parser_identifier (parser),
30983 sfk_none, token->location);
30984
30985 eat_colon:
30986 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30987 /* Get the width of the bitfield. */
30988 width
30989 = cp_parser_constant_expression (parser);
30990 }
30991 else
30992 {
30993 /* Parse the declarator. */
30994 declarator
30995 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30996 CP_PARSER_FLAGS_NONE,
30997 &ctor_dtor_or_conv_p,
30998 /*parenthesized_p=*/NULL,
30999 /*member_p=*/false,
31000 /*friend_p=*/false);
31001 }
31002
31003 /* Look for attributes that apply to the ivar. */
31004 attributes = cp_parser_attributes_opt (parser);
31005 /* Remember which attributes are prefix attributes and
31006 which are not. */
31007 first_attribute = attributes;
31008 /* Combine the attributes. */
31009 attributes = attr_chainon (prefix_attributes, attributes);
31010
31011 if (width)
31012 /* Create the bitfield declaration. */
31013 decl = grokbitfield (declarator, &declspecs,
31014 width, NULL_TREE, attributes);
31015 else
31016 decl = grokfield (declarator, &declspecs,
31017 NULL_TREE, /*init_const_expr_p=*/false,
31018 NULL_TREE, attributes);
31019
31020 /* Add the instance variable. */
31021 if (decl != error_mark_node && decl != NULL_TREE)
31022 objc_add_instance_variable (decl);
31023
31024 /* Reset PREFIX_ATTRIBUTES. */
31025 if (attributes != error_mark_node)
31026 {
31027 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31028 attributes = TREE_CHAIN (attributes);
31029 if (attributes)
31030 TREE_CHAIN (attributes) = NULL_TREE;
31031 }
31032
31033 token = cp_lexer_peek_token (parser->lexer);
31034
31035 if (token->type == CPP_COMMA)
31036 {
31037 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31038 continue;
31039 }
31040 break;
31041 }
31042
31043 cp_parser_consume_semicolon_at_end_of_statement (parser);
31044 token = cp_lexer_peek_token (parser->lexer);
31045 }
31046
31047 if (token->keyword == RID_AT_END)
31048 cp_parser_error (parser, "expected %<}%>");
31049
31050 /* Do not consume the RID_AT_END, so it will be read again as terminating
31051 the @interface of @implementation. */
31052 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
31053 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
31054
31055 /* For historical reasons, we accept an optional semicolon. */
31056 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31057 cp_lexer_consume_token (parser->lexer);
31058 }
31059
31060 /* Parse an Objective-C protocol declaration. */
31061
31062 static void
31063 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
31064 {
31065 tree proto, protorefs;
31066 cp_token *tok;
31067
31068 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31070 {
31071 tok = cp_lexer_peek_token (parser->lexer);
31072 error_at (tok->location, "identifier expected after %<@protocol%>");
31073 cp_parser_consume_semicolon_at_end_of_statement (parser);
31074 return;
31075 }
31076
31077 /* See if we have a forward declaration or a definition. */
31078 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
31079
31080 /* Try a forward declaration first. */
31081 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
31082 {
31083 while (true)
31084 {
31085 tree id;
31086
31087 id = cp_parser_identifier (parser);
31088 if (id == error_mark_node)
31089 break;
31090
31091 objc_declare_protocol (id, attributes);
31092
31093 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31094 cp_lexer_consume_token (parser->lexer);
31095 else
31096 break;
31097 }
31098 cp_parser_consume_semicolon_at_end_of_statement (parser);
31099 }
31100
31101 /* Ok, we got a full-fledged definition (or at least should). */
31102 else
31103 {
31104 proto = cp_parser_identifier (parser);
31105 protorefs = cp_parser_objc_protocol_refs_opt (parser);
31106 objc_start_protocol (proto, protorefs, attributes);
31107 cp_parser_objc_method_prototype_list (parser);
31108 }
31109 }
31110
31111 /* Parse an Objective-C superclass or category. */
31112
31113 static void
31114 cp_parser_objc_superclass_or_category (cp_parser *parser,
31115 bool iface_p,
31116 tree *super,
31117 tree *categ, bool *is_class_extension)
31118 {
31119 cp_token *next = cp_lexer_peek_token (parser->lexer);
31120
31121 *super = *categ = NULL_TREE;
31122 *is_class_extension = false;
31123 if (next->type == CPP_COLON)
31124 {
31125 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31126 *super = cp_parser_identifier (parser);
31127 }
31128 else if (next->type == CPP_OPEN_PAREN)
31129 {
31130 matching_parens parens;
31131 parens.consume_open (parser); /* Eat '('. */
31132
31133 /* If there is no category name, and this is an @interface, we
31134 have a class extension. */
31135 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31136 {
31137 *categ = NULL_TREE;
31138 *is_class_extension = true;
31139 }
31140 else
31141 *categ = cp_parser_identifier (parser);
31142
31143 parens.require_close (parser);
31144 }
31145 }
31146
31147 /* Parse an Objective-C class interface. */
31148
31149 static void
31150 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
31151 {
31152 tree name, super, categ, protos;
31153 bool is_class_extension;
31154
31155 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
31156 name = cp_parser_identifier (parser);
31157 if (name == error_mark_node)
31158 {
31159 /* It's hard to recover because even if valid @interface stuff
31160 is to follow, we can't compile it (or validate it) if we
31161 don't even know which class it refers to. Let's assume this
31162 was a stray '@interface' token in the stream and skip it.
31163 */
31164 return;
31165 }
31166 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
31167 &is_class_extension);
31168 protos = cp_parser_objc_protocol_refs_opt (parser);
31169
31170 /* We have either a class or a category on our hands. */
31171 if (categ || is_class_extension)
31172 objc_start_category_interface (name, categ, protos, attributes);
31173 else
31174 {
31175 objc_start_class_interface (name, super, protos, attributes);
31176 /* Handle instance variable declarations, if any. */
31177 cp_parser_objc_class_ivars (parser);
31178 objc_continue_interface ();
31179 }
31180
31181 cp_parser_objc_method_prototype_list (parser);
31182 }
31183
31184 /* Parse an Objective-C class implementation. */
31185
31186 static void
31187 cp_parser_objc_class_implementation (cp_parser* parser)
31188 {
31189 tree name, super, categ;
31190 bool is_class_extension;
31191
31192 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
31193 name = cp_parser_identifier (parser);
31194 if (name == error_mark_node)
31195 {
31196 /* It's hard to recover because even if valid @implementation
31197 stuff is to follow, we can't compile it (or validate it) if
31198 we don't even know which class it refers to. Let's assume
31199 this was a stray '@implementation' token in the stream and
31200 skip it.
31201 */
31202 return;
31203 }
31204 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31205 &is_class_extension);
31206
31207 /* We have either a class or a category on our hands. */
31208 if (categ)
31209 objc_start_category_implementation (name, categ);
31210 else
31211 {
31212 objc_start_class_implementation (name, super);
31213 /* Handle instance variable declarations, if any. */
31214 cp_parser_objc_class_ivars (parser);
31215 objc_continue_implementation ();
31216 }
31217
31218 cp_parser_objc_method_definition_list (parser);
31219 }
31220
31221 /* Consume the @end token and finish off the implementation. */
31222
31223 static void
31224 cp_parser_objc_end_implementation (cp_parser* parser)
31225 {
31226 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31227 objc_finish_implementation ();
31228 }
31229
31230 /* Parse an Objective-C declaration. */
31231
31232 static void
31233 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31234 {
31235 /* Try to figure out what kind of declaration is present. */
31236 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31237
31238 if (attributes)
31239 switch (kwd->keyword)
31240 {
31241 case RID_AT_ALIAS:
31242 case RID_AT_CLASS:
31243 case RID_AT_END:
31244 error_at (kwd->location, "attributes may not be specified before"
31245 " the %<@%D%> Objective-C++ keyword",
31246 kwd->u.value);
31247 attributes = NULL;
31248 break;
31249 case RID_AT_IMPLEMENTATION:
31250 warning_at (kwd->location, OPT_Wattributes,
31251 "prefix attributes are ignored before %<@%D%>",
31252 kwd->u.value);
31253 attributes = NULL;
31254 default:
31255 break;
31256 }
31257
31258 switch (kwd->keyword)
31259 {
31260 case RID_AT_ALIAS:
31261 cp_parser_objc_alias_declaration (parser);
31262 break;
31263 case RID_AT_CLASS:
31264 cp_parser_objc_class_declaration (parser);
31265 break;
31266 case RID_AT_PROTOCOL:
31267 cp_parser_objc_protocol_declaration (parser, attributes);
31268 break;
31269 case RID_AT_INTERFACE:
31270 cp_parser_objc_class_interface (parser, attributes);
31271 break;
31272 case RID_AT_IMPLEMENTATION:
31273 cp_parser_objc_class_implementation (parser);
31274 break;
31275 case RID_AT_END:
31276 cp_parser_objc_end_implementation (parser);
31277 break;
31278 default:
31279 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31280 kwd->u.value);
31281 cp_parser_skip_to_end_of_block_or_statement (parser);
31282 }
31283 }
31284
31285 /* Parse an Objective-C try-catch-finally statement.
31286
31287 objc-try-catch-finally-stmt:
31288 @try compound-statement objc-catch-clause-seq [opt]
31289 objc-finally-clause [opt]
31290
31291 objc-catch-clause-seq:
31292 objc-catch-clause objc-catch-clause-seq [opt]
31293
31294 objc-catch-clause:
31295 @catch ( objc-exception-declaration ) compound-statement
31296
31297 objc-finally-clause:
31298 @finally compound-statement
31299
31300 objc-exception-declaration:
31301 parameter-declaration
31302 '...'
31303
31304 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31305
31306 Returns NULL_TREE.
31307
31308 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31309 for C. Keep them in sync. */
31310
31311 static tree
31312 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31313 {
31314 location_t location;
31315 tree stmt;
31316
31317 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31318 location = cp_lexer_peek_token (parser->lexer)->location;
31319 objc_maybe_warn_exceptions (location);
31320 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31321 node, lest it get absorbed into the surrounding block. */
31322 stmt = push_stmt_list ();
31323 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31324 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31325
31326 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31327 {
31328 cp_parameter_declarator *parm;
31329 tree parameter_declaration = error_mark_node;
31330 bool seen_open_paren = false;
31331 matching_parens parens;
31332
31333 cp_lexer_consume_token (parser->lexer);
31334 if (parens.require_open (parser))
31335 seen_open_paren = true;
31336 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31337 {
31338 /* We have "@catch (...)" (where the '...' are literally
31339 what is in the code). Skip the '...'.
31340 parameter_declaration is set to NULL_TREE, and
31341 objc_being_catch_clauses() knows that that means
31342 '...'. */
31343 cp_lexer_consume_token (parser->lexer);
31344 parameter_declaration = NULL_TREE;
31345 }
31346 else
31347 {
31348 /* We have "@catch (NSException *exception)" or something
31349 like that. Parse the parameter declaration. */
31350 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31351 false, NULL);
31352 if (parm == NULL)
31353 parameter_declaration = error_mark_node;
31354 else
31355 parameter_declaration = grokdeclarator (parm->declarator,
31356 &parm->decl_specifiers,
31357 PARM, /*initialized=*/0,
31358 /*attrlist=*/NULL);
31359 }
31360 if (seen_open_paren)
31361 parens.require_close (parser);
31362 else
31363 {
31364 /* If there was no open parenthesis, we are recovering from
31365 an error, and we are trying to figure out what mistake
31366 the user has made. */
31367
31368 /* If there is an immediate closing parenthesis, the user
31369 probably forgot the opening one (ie, they typed "@catch
31370 NSException *e)". Parse the closing parenthesis and keep
31371 going. */
31372 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31373 cp_lexer_consume_token (parser->lexer);
31374
31375 /* If these is no immediate closing parenthesis, the user
31376 probably doesn't know that parenthesis are required at
31377 all (ie, they typed "@catch NSException *e"). So, just
31378 forget about the closing parenthesis and keep going. */
31379 }
31380 objc_begin_catch_clause (parameter_declaration);
31381 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31382 objc_finish_catch_clause ();
31383 }
31384 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31385 {
31386 cp_lexer_consume_token (parser->lexer);
31387 location = cp_lexer_peek_token (parser->lexer)->location;
31388 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31389 node, lest it get absorbed into the surrounding block. */
31390 stmt = push_stmt_list ();
31391 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31392 objc_build_finally_clause (location, pop_stmt_list (stmt));
31393 }
31394
31395 return objc_finish_try_stmt ();
31396 }
31397
31398 /* Parse an Objective-C synchronized statement.
31399
31400 objc-synchronized-stmt:
31401 @synchronized ( expression ) compound-statement
31402
31403 Returns NULL_TREE. */
31404
31405 static tree
31406 cp_parser_objc_synchronized_statement (cp_parser *parser)
31407 {
31408 location_t location;
31409 tree lock, stmt;
31410
31411 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31412
31413 location = cp_lexer_peek_token (parser->lexer)->location;
31414 objc_maybe_warn_exceptions (location);
31415 matching_parens parens;
31416 parens.require_open (parser);
31417 lock = cp_parser_expression (parser);
31418 parens.require_close (parser);
31419
31420 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31421 node, lest it get absorbed into the surrounding block. */
31422 stmt = push_stmt_list ();
31423 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31424
31425 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31426 }
31427
31428 /* Parse an Objective-C throw statement.
31429
31430 objc-throw-stmt:
31431 @throw assignment-expression [opt] ;
31432
31433 Returns a constructed '@throw' statement. */
31434
31435 static tree
31436 cp_parser_objc_throw_statement (cp_parser *parser)
31437 {
31438 tree expr = NULL_TREE;
31439 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31440
31441 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31442
31443 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31444 expr = cp_parser_expression (parser);
31445
31446 cp_parser_consume_semicolon_at_end_of_statement (parser);
31447
31448 return objc_build_throw_stmt (loc, expr);
31449 }
31450
31451 /* Parse an Objective-C statement. */
31452
31453 static tree
31454 cp_parser_objc_statement (cp_parser * parser)
31455 {
31456 /* Try to figure out what kind of declaration is present. */
31457 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31458
31459 switch (kwd->keyword)
31460 {
31461 case RID_AT_TRY:
31462 return cp_parser_objc_try_catch_finally_statement (parser);
31463 case RID_AT_SYNCHRONIZED:
31464 return cp_parser_objc_synchronized_statement (parser);
31465 case RID_AT_THROW:
31466 return cp_parser_objc_throw_statement (parser);
31467 default:
31468 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31469 kwd->u.value);
31470 cp_parser_skip_to_end_of_block_or_statement (parser);
31471 }
31472
31473 return error_mark_node;
31474 }
31475
31476 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31477 look ahead to see if an objc keyword follows the attributes. This
31478 is to detect the use of prefix attributes on ObjC @interface and
31479 @protocol. */
31480
31481 static bool
31482 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31483 {
31484 cp_lexer_save_tokens (parser->lexer);
31485 *attrib = cp_parser_attributes_opt (parser);
31486 gcc_assert (*attrib);
31487 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31488 {
31489 cp_lexer_commit_tokens (parser->lexer);
31490 return true;
31491 }
31492 cp_lexer_rollback_tokens (parser->lexer);
31493 return false;
31494 }
31495
31496 /* This routine is a minimal replacement for
31497 c_parser_struct_declaration () used when parsing the list of
31498 types/names or ObjC++ properties. For example, when parsing the
31499 code
31500
31501 @property (readonly) int a, b, c;
31502
31503 this function is responsible for parsing "int a, int b, int c" and
31504 returning the declarations as CHAIN of DECLs.
31505
31506 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31507 similar parsing. */
31508 static tree
31509 cp_parser_objc_struct_declaration (cp_parser *parser)
31510 {
31511 tree decls = NULL_TREE;
31512 cp_decl_specifier_seq declspecs;
31513 int decl_class_or_enum_p;
31514 tree prefix_attributes;
31515
31516 cp_parser_decl_specifier_seq (parser,
31517 CP_PARSER_FLAGS_NONE,
31518 &declspecs,
31519 &decl_class_or_enum_p);
31520
31521 if (declspecs.type == error_mark_node)
31522 return error_mark_node;
31523
31524 /* auto, register, static, extern, mutable. */
31525 if (declspecs.storage_class != sc_none)
31526 {
31527 cp_parser_error (parser, "invalid type for property");
31528 declspecs.storage_class = sc_none;
31529 }
31530
31531 /* thread_local. */
31532 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31533 {
31534 cp_parser_error (parser, "invalid type for property");
31535 declspecs.locations[ds_thread] = 0;
31536 }
31537
31538 /* typedef. */
31539 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31540 {
31541 cp_parser_error (parser, "invalid type for property");
31542 declspecs.locations[ds_typedef] = 0;
31543 }
31544
31545 prefix_attributes = declspecs.attributes;
31546 declspecs.attributes = NULL_TREE;
31547
31548 /* Keep going until we hit the `;' at the end of the declaration. */
31549 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31550 {
31551 tree attributes, first_attribute, decl;
31552 cp_declarator *declarator;
31553 cp_token *token;
31554
31555 /* Parse the declarator. */
31556 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31557 CP_PARSER_FLAGS_NONE,
31558 NULL, NULL, false, false);
31559
31560 /* Look for attributes that apply to the ivar. */
31561 attributes = cp_parser_attributes_opt (parser);
31562 /* Remember which attributes are prefix attributes and
31563 which are not. */
31564 first_attribute = attributes;
31565 /* Combine the attributes. */
31566 attributes = attr_chainon (prefix_attributes, attributes);
31567
31568 decl = grokfield (declarator, &declspecs,
31569 NULL_TREE, /*init_const_expr_p=*/false,
31570 NULL_TREE, attributes);
31571
31572 if (decl == error_mark_node || decl == NULL_TREE)
31573 return error_mark_node;
31574
31575 /* Reset PREFIX_ATTRIBUTES. */
31576 if (attributes != error_mark_node)
31577 {
31578 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31579 attributes = TREE_CHAIN (attributes);
31580 if (attributes)
31581 TREE_CHAIN (attributes) = NULL_TREE;
31582 }
31583
31584 DECL_CHAIN (decl) = decls;
31585 decls = decl;
31586
31587 token = cp_lexer_peek_token (parser->lexer);
31588 if (token->type == CPP_COMMA)
31589 {
31590 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31591 continue;
31592 }
31593 else
31594 break;
31595 }
31596 return decls;
31597 }
31598
31599 /* Parse an Objective-C @property declaration. The syntax is:
31600
31601 objc-property-declaration:
31602 '@property' objc-property-attributes[opt] struct-declaration ;
31603
31604 objc-property-attributes:
31605 '(' objc-property-attribute-list ')'
31606
31607 objc-property-attribute-list:
31608 objc-property-attribute
31609 objc-property-attribute-list, objc-property-attribute
31610
31611 objc-property-attribute
31612 'getter' = identifier
31613 'setter' = identifier
31614 'readonly'
31615 'readwrite'
31616 'assign'
31617 'retain'
31618 'copy'
31619 'nonatomic'
31620
31621 For example:
31622 @property NSString *name;
31623 @property (readonly) id object;
31624 @property (retain, nonatomic, getter=getTheName) id name;
31625 @property int a, b, c;
31626
31627 PS: This function is identical to
31628 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31629 static void
31630 cp_parser_objc_at_property_declaration (cp_parser *parser)
31631 {
31632 /* The following variables hold the attributes of the properties as
31633 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31634 seen. When we see an attribute, we set them to 'true' (if they
31635 are boolean properties) or to the identifier (if they have an
31636 argument, ie, for getter and setter). Note that here we only
31637 parse the list of attributes, check the syntax and accumulate the
31638 attributes that we find. objc_add_property_declaration() will
31639 then process the information. */
31640 bool property_assign = false;
31641 bool property_copy = false;
31642 tree property_getter_ident = NULL_TREE;
31643 bool property_nonatomic = false;
31644 bool property_readonly = false;
31645 bool property_readwrite = false;
31646 bool property_retain = false;
31647 tree property_setter_ident = NULL_TREE;
31648
31649 /* 'properties' is the list of properties that we read. Usually a
31650 single one, but maybe more (eg, in "@property int a, b, c;" there
31651 are three). */
31652 tree properties;
31653 location_t loc;
31654
31655 loc = cp_lexer_peek_token (parser->lexer)->location;
31656
31657 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31658
31659 /* Parse the optional attribute list... */
31660 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31661 {
31662 /* Eat the '('. */
31663 matching_parens parens;
31664 parens.consume_open (parser);
31665
31666 while (true)
31667 {
31668 bool syntax_error = false;
31669 cp_token *token = cp_lexer_peek_token (parser->lexer);
31670 enum rid keyword;
31671
31672 if (token->type != CPP_NAME)
31673 {
31674 cp_parser_error (parser, "expected identifier");
31675 break;
31676 }
31677 keyword = C_RID_CODE (token->u.value);
31678 cp_lexer_consume_token (parser->lexer);
31679 switch (keyword)
31680 {
31681 case RID_ASSIGN: property_assign = true; break;
31682 case RID_COPY: property_copy = true; break;
31683 case RID_NONATOMIC: property_nonatomic = true; break;
31684 case RID_READONLY: property_readonly = true; break;
31685 case RID_READWRITE: property_readwrite = true; break;
31686 case RID_RETAIN: property_retain = true; break;
31687
31688 case RID_GETTER:
31689 case RID_SETTER:
31690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31691 {
31692 if (keyword == RID_GETTER)
31693 cp_parser_error (parser,
31694 "missing %<=%> (after %<getter%> attribute)");
31695 else
31696 cp_parser_error (parser,
31697 "missing %<=%> (after %<setter%> attribute)");
31698 syntax_error = true;
31699 break;
31700 }
31701 cp_lexer_consume_token (parser->lexer); /* eat the = */
31702 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31703 {
31704 cp_parser_error (parser, "expected identifier");
31705 syntax_error = true;
31706 break;
31707 }
31708 if (keyword == RID_SETTER)
31709 {
31710 if (property_setter_ident != NULL_TREE)
31711 {
31712 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31713 cp_lexer_consume_token (parser->lexer);
31714 }
31715 else
31716 property_setter_ident = cp_parser_objc_selector (parser);
31717 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31718 cp_parser_error (parser, "setter name must terminate with %<:%>");
31719 else
31720 cp_lexer_consume_token (parser->lexer);
31721 }
31722 else
31723 {
31724 if (property_getter_ident != NULL_TREE)
31725 {
31726 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31727 cp_lexer_consume_token (parser->lexer);
31728 }
31729 else
31730 property_getter_ident = cp_parser_objc_selector (parser);
31731 }
31732 break;
31733 default:
31734 cp_parser_error (parser, "unknown property attribute");
31735 syntax_error = true;
31736 break;
31737 }
31738
31739 if (syntax_error)
31740 break;
31741
31742 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31743 cp_lexer_consume_token (parser->lexer);
31744 else
31745 break;
31746 }
31747
31748 /* FIXME: "@property (setter, assign);" will generate a spurious
31749 "error: expected ‘)’ before ‘,’ token". This is because
31750 cp_parser_require, unlike the C counterpart, will produce an
31751 error even if we are in error recovery. */
31752 if (!parens.require_close (parser))
31753 {
31754 cp_parser_skip_to_closing_parenthesis (parser,
31755 /*recovering=*/true,
31756 /*or_comma=*/false,
31757 /*consume_paren=*/true);
31758 }
31759 }
31760
31761 /* ... and the property declaration(s). */
31762 properties = cp_parser_objc_struct_declaration (parser);
31763
31764 if (properties == error_mark_node)
31765 {
31766 cp_parser_skip_to_end_of_statement (parser);
31767 /* If the next token is now a `;', consume it. */
31768 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31769 cp_lexer_consume_token (parser->lexer);
31770 return;
31771 }
31772
31773 if (properties == NULL_TREE)
31774 cp_parser_error (parser, "expected identifier");
31775 else
31776 {
31777 /* Comma-separated properties are chained together in
31778 reverse order; add them one by one. */
31779 properties = nreverse (properties);
31780
31781 for (; properties; properties = TREE_CHAIN (properties))
31782 objc_add_property_declaration (loc, copy_node (properties),
31783 property_readonly, property_readwrite,
31784 property_assign, property_retain,
31785 property_copy, property_nonatomic,
31786 property_getter_ident, property_setter_ident);
31787 }
31788
31789 cp_parser_consume_semicolon_at_end_of_statement (parser);
31790 }
31791
31792 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31793
31794 objc-synthesize-declaration:
31795 @synthesize objc-synthesize-identifier-list ;
31796
31797 objc-synthesize-identifier-list:
31798 objc-synthesize-identifier
31799 objc-synthesize-identifier-list, objc-synthesize-identifier
31800
31801 objc-synthesize-identifier
31802 identifier
31803 identifier = identifier
31804
31805 For example:
31806 @synthesize MyProperty;
31807 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31808
31809 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31810 for C. Keep them in sync.
31811 */
31812 static void
31813 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31814 {
31815 tree list = NULL_TREE;
31816 location_t loc;
31817 loc = cp_lexer_peek_token (parser->lexer)->location;
31818
31819 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31820 while (true)
31821 {
31822 tree property, ivar;
31823 property = cp_parser_identifier (parser);
31824 if (property == error_mark_node)
31825 {
31826 cp_parser_consume_semicolon_at_end_of_statement (parser);
31827 return;
31828 }
31829 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31830 {
31831 cp_lexer_consume_token (parser->lexer);
31832 ivar = cp_parser_identifier (parser);
31833 if (ivar == error_mark_node)
31834 {
31835 cp_parser_consume_semicolon_at_end_of_statement (parser);
31836 return;
31837 }
31838 }
31839 else
31840 ivar = NULL_TREE;
31841 list = chainon (list, build_tree_list (ivar, property));
31842 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31843 cp_lexer_consume_token (parser->lexer);
31844 else
31845 break;
31846 }
31847 cp_parser_consume_semicolon_at_end_of_statement (parser);
31848 objc_add_synthesize_declaration (loc, list);
31849 }
31850
31851 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31852
31853 objc-dynamic-declaration:
31854 @dynamic identifier-list ;
31855
31856 For example:
31857 @dynamic MyProperty;
31858 @dynamic MyProperty, AnotherProperty;
31859
31860 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31861 for C. Keep them in sync.
31862 */
31863 static void
31864 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31865 {
31866 tree list = NULL_TREE;
31867 location_t loc;
31868 loc = cp_lexer_peek_token (parser->lexer)->location;
31869
31870 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31871 while (true)
31872 {
31873 tree property;
31874 property = cp_parser_identifier (parser);
31875 if (property == error_mark_node)
31876 {
31877 cp_parser_consume_semicolon_at_end_of_statement (parser);
31878 return;
31879 }
31880 list = chainon (list, build_tree_list (NULL, property));
31881 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31882 cp_lexer_consume_token (parser->lexer);
31883 else
31884 break;
31885 }
31886 cp_parser_consume_semicolon_at_end_of_statement (parser);
31887 objc_add_dynamic_declaration (loc, list);
31888 }
31889
31890 \f
31891 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
31892
31893 /* Returns name of the next clause.
31894 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31895 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31896 returned and the token is consumed. */
31897
31898 static pragma_omp_clause
31899 cp_parser_omp_clause_name (cp_parser *parser)
31900 {
31901 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31902
31903 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31904 result = PRAGMA_OACC_CLAUSE_AUTO;
31905 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31906 result = PRAGMA_OMP_CLAUSE_IF;
31907 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31908 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31909 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31910 result = PRAGMA_OACC_CLAUSE_DELETE;
31911 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31912 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31913 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31914 result = PRAGMA_OMP_CLAUSE_FOR;
31915 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31916 {
31917 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31918 const char *p = IDENTIFIER_POINTER (id);
31919
31920 switch (p[0])
31921 {
31922 case 'a':
31923 if (!strcmp ("aligned", p))
31924 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31925 else if (!strcmp ("async", p))
31926 result = PRAGMA_OACC_CLAUSE_ASYNC;
31927 break;
31928 case 'c':
31929 if (!strcmp ("collapse", p))
31930 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31931 else if (!strcmp ("copy", p))
31932 result = PRAGMA_OACC_CLAUSE_COPY;
31933 else if (!strcmp ("copyin", p))
31934 result = PRAGMA_OMP_CLAUSE_COPYIN;
31935 else if (!strcmp ("copyout", p))
31936 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31937 else if (!strcmp ("copyprivate", p))
31938 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31939 else if (!strcmp ("create", p))
31940 result = PRAGMA_OACC_CLAUSE_CREATE;
31941 break;
31942 case 'd':
31943 if (!strcmp ("defaultmap", p))
31944 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31945 else if (!strcmp ("depend", p))
31946 result = PRAGMA_OMP_CLAUSE_DEPEND;
31947 else if (!strcmp ("device", p))
31948 result = PRAGMA_OMP_CLAUSE_DEVICE;
31949 else if (!strcmp ("deviceptr", p))
31950 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31951 else if (!strcmp ("device_resident", p))
31952 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31953 else if (!strcmp ("dist_schedule", p))
31954 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31955 break;
31956 case 'f':
31957 if (!strcmp ("final", p))
31958 result = PRAGMA_OMP_CLAUSE_FINAL;
31959 else if (!strcmp ("finalize", p))
31960 result = PRAGMA_OACC_CLAUSE_FINALIZE;
31961 else if (!strcmp ("firstprivate", p))
31962 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31963 else if (!strcmp ("from", p))
31964 result = PRAGMA_OMP_CLAUSE_FROM;
31965 break;
31966 case 'g':
31967 if (!strcmp ("gang", p))
31968 result = PRAGMA_OACC_CLAUSE_GANG;
31969 else if (!strcmp ("grainsize", p))
31970 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31971 break;
31972 case 'h':
31973 if (!strcmp ("hint", p))
31974 result = PRAGMA_OMP_CLAUSE_HINT;
31975 else if (!strcmp ("host", p))
31976 result = PRAGMA_OACC_CLAUSE_HOST;
31977 break;
31978 case 'i':
31979 if (!strcmp ("if_present", p))
31980 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
31981 else if (!strcmp ("in_reduction", p))
31982 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
31983 else if (!strcmp ("inbranch", p))
31984 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31985 else if (!strcmp ("independent", p))
31986 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31987 else if (!strcmp ("is_device_ptr", p))
31988 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31989 break;
31990 case 'l':
31991 if (!strcmp ("lastprivate", p))
31992 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31993 else if (!strcmp ("linear", p))
31994 result = PRAGMA_OMP_CLAUSE_LINEAR;
31995 else if (!strcmp ("link", p))
31996 result = PRAGMA_OMP_CLAUSE_LINK;
31997 break;
31998 case 'm':
31999 if (!strcmp ("map", p))
32000 result = PRAGMA_OMP_CLAUSE_MAP;
32001 else if (!strcmp ("mergeable", p))
32002 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
32003 break;
32004 case 'n':
32005 if (!strcmp ("nogroup", p))
32006 result = PRAGMA_OMP_CLAUSE_NOGROUP;
32007 else if (!strcmp ("nontemporal", p))
32008 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
32009 else if (!strcmp ("notinbranch", p))
32010 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
32011 else if (!strcmp ("nowait", p))
32012 result = PRAGMA_OMP_CLAUSE_NOWAIT;
32013 else if (!strcmp ("num_gangs", p))
32014 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
32015 else if (!strcmp ("num_tasks", p))
32016 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
32017 else if (!strcmp ("num_teams", p))
32018 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
32019 else if (!strcmp ("num_threads", p))
32020 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
32021 else if (!strcmp ("num_workers", p))
32022 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
32023 break;
32024 case 'o':
32025 if (!strcmp ("ordered", p))
32026 result = PRAGMA_OMP_CLAUSE_ORDERED;
32027 break;
32028 case 'p':
32029 if (!strcmp ("parallel", p))
32030 result = PRAGMA_OMP_CLAUSE_PARALLEL;
32031 else if (!strcmp ("present", p))
32032 result = PRAGMA_OACC_CLAUSE_PRESENT;
32033 else if (!strcmp ("present_or_copy", p)
32034 || !strcmp ("pcopy", p))
32035 result = PRAGMA_OACC_CLAUSE_COPY;
32036 else if (!strcmp ("present_or_copyin", p)
32037 || !strcmp ("pcopyin", p))
32038 result = PRAGMA_OACC_CLAUSE_COPYIN;
32039 else if (!strcmp ("present_or_copyout", p)
32040 || !strcmp ("pcopyout", p))
32041 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32042 else if (!strcmp ("present_or_create", p)
32043 || !strcmp ("pcreate", p))
32044 result = PRAGMA_OACC_CLAUSE_CREATE;
32045 else if (!strcmp ("priority", p))
32046 result = PRAGMA_OMP_CLAUSE_PRIORITY;
32047 else if (!strcmp ("proc_bind", p))
32048 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
32049 break;
32050 case 'r':
32051 if (!strcmp ("reduction", p))
32052 result = PRAGMA_OMP_CLAUSE_REDUCTION;
32053 break;
32054 case 's':
32055 if (!strcmp ("safelen", p))
32056 result = PRAGMA_OMP_CLAUSE_SAFELEN;
32057 else if (!strcmp ("schedule", p))
32058 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
32059 else if (!strcmp ("sections", p))
32060 result = PRAGMA_OMP_CLAUSE_SECTIONS;
32061 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
32062 result = PRAGMA_OACC_CLAUSE_HOST;
32063 else if (!strcmp ("seq", p))
32064 result = PRAGMA_OACC_CLAUSE_SEQ;
32065 else if (!strcmp ("shared", p))
32066 result = PRAGMA_OMP_CLAUSE_SHARED;
32067 else if (!strcmp ("simd", p))
32068 result = PRAGMA_OMP_CLAUSE_SIMD;
32069 else if (!strcmp ("simdlen", p))
32070 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
32071 break;
32072 case 't':
32073 if (!strcmp ("task_reduction", p))
32074 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
32075 else if (!strcmp ("taskgroup", p))
32076 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
32077 else if (!strcmp ("thread_limit", p))
32078 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
32079 else if (!strcmp ("threads", p))
32080 result = PRAGMA_OMP_CLAUSE_THREADS;
32081 else if (!strcmp ("tile", p))
32082 result = PRAGMA_OACC_CLAUSE_TILE;
32083 else if (!strcmp ("to", p))
32084 result = PRAGMA_OMP_CLAUSE_TO;
32085 break;
32086 case 'u':
32087 if (!strcmp ("uniform", p))
32088 result = PRAGMA_OMP_CLAUSE_UNIFORM;
32089 else if (!strcmp ("untied", p))
32090 result = PRAGMA_OMP_CLAUSE_UNTIED;
32091 else if (!strcmp ("use_device", p))
32092 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
32093 else if (!strcmp ("use_device_ptr", p))
32094 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
32095 break;
32096 case 'v':
32097 if (!strcmp ("vector", p))
32098 result = PRAGMA_OACC_CLAUSE_VECTOR;
32099 else if (!strcmp ("vector_length", p))
32100 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
32101 break;
32102 case 'w':
32103 if (!strcmp ("wait", p))
32104 result = PRAGMA_OACC_CLAUSE_WAIT;
32105 else if (!strcmp ("worker", p))
32106 result = PRAGMA_OACC_CLAUSE_WORKER;
32107 break;
32108 }
32109 }
32110
32111 if (result != PRAGMA_OMP_CLAUSE_NONE)
32112 cp_lexer_consume_token (parser->lexer);
32113
32114 return result;
32115 }
32116
32117 /* Validate that a clause of the given type does not already exist. */
32118
32119 static void
32120 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
32121 const char *name, location_t location)
32122 {
32123 tree c;
32124
32125 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
32126 if (OMP_CLAUSE_CODE (c) == code)
32127 {
32128 error_at (location, "too many %qs clauses", name);
32129 break;
32130 }
32131 }
32132
32133 /* OpenMP 2.5:
32134 variable-list:
32135 identifier
32136 variable-list , identifier
32137
32138 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32139 colon). An opening parenthesis will have been consumed by the caller.
32140
32141 If KIND is nonzero, create the appropriate node and install the decl
32142 in OMP_CLAUSE_DECL and add the node to the head of the list.
32143
32144 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32145 return the list created.
32146
32147 COLON can be NULL if only closing parenthesis should end the list,
32148 or pointer to bool which will receive false if the list is terminated
32149 by closing parenthesis or true if the list is terminated by colon. */
32150
32151 static tree
32152 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
32153 tree list, bool *colon)
32154 {
32155 cp_token *token;
32156 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32157 if (colon)
32158 {
32159 parser->colon_corrects_to_scope_p = false;
32160 *colon = false;
32161 }
32162 while (1)
32163 {
32164 tree name, decl;
32165
32166 if (kind == OMP_CLAUSE_DEPEND)
32167 cp_parser_parse_tentatively (parser);
32168 token = cp_lexer_peek_token (parser->lexer);
32169 if (kind != 0
32170 && current_class_ptr
32171 && cp_parser_is_keyword (token, RID_THIS))
32172 {
32173 decl = finish_this_expr ();
32174 if (TREE_CODE (decl) == NON_LVALUE_EXPR
32175 || CONVERT_EXPR_P (decl))
32176 decl = TREE_OPERAND (decl, 0);
32177 cp_lexer_consume_token (parser->lexer);
32178 }
32179 else
32180 {
32181 name = cp_parser_id_expression (parser, /*template_p=*/false,
32182 /*check_dependency_p=*/true,
32183 /*template_p=*/NULL,
32184 /*declarator_p=*/false,
32185 /*optional_p=*/false);
32186 if (name == error_mark_node)
32187 {
32188 if (kind == OMP_CLAUSE_DEPEND
32189 && cp_parser_simulate_error (parser))
32190 goto depend_lvalue;
32191 goto skip_comma;
32192 }
32193
32194 if (identifier_p (name))
32195 decl = cp_parser_lookup_name_simple (parser, name, token->location);
32196 else
32197 decl = name;
32198 if (decl == error_mark_node)
32199 {
32200 if (kind == OMP_CLAUSE_DEPEND
32201 && cp_parser_simulate_error (parser))
32202 goto depend_lvalue;
32203 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32204 token->location);
32205 }
32206 }
32207 if (decl == error_mark_node)
32208 ;
32209 else if (kind != 0)
32210 {
32211 switch (kind)
32212 {
32213 case OMP_CLAUSE__CACHE_:
32214 /* The OpenACC cache directive explicitly only allows "array
32215 elements or subarrays". */
32216 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32217 {
32218 error_at (token->location, "expected %<[%>");
32219 decl = error_mark_node;
32220 break;
32221 }
32222 /* FALLTHROUGH. */
32223 case OMP_CLAUSE_MAP:
32224 case OMP_CLAUSE_FROM:
32225 case OMP_CLAUSE_TO:
32226 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32227 {
32228 location_t loc
32229 = cp_lexer_peek_token (parser->lexer)->location;
32230 cp_id_kind idk = CP_ID_KIND_NONE;
32231 cp_lexer_consume_token (parser->lexer);
32232 decl = convert_from_reference (decl);
32233 decl
32234 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32235 decl, false,
32236 &idk, loc);
32237 }
32238 /* FALLTHROUGH. */
32239 case OMP_CLAUSE_DEPEND:
32240 case OMP_CLAUSE_REDUCTION:
32241 case OMP_CLAUSE_IN_REDUCTION:
32242 case OMP_CLAUSE_TASK_REDUCTION:
32243 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32244 {
32245 tree low_bound = NULL_TREE, length = NULL_TREE;
32246
32247 parser->colon_corrects_to_scope_p = false;
32248 cp_lexer_consume_token (parser->lexer);
32249 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32250 low_bound = cp_parser_expression (parser);
32251 if (!colon)
32252 parser->colon_corrects_to_scope_p
32253 = saved_colon_corrects_to_scope_p;
32254 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32255 length = integer_one_node;
32256 else
32257 {
32258 /* Look for `:'. */
32259 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32260 {
32261 if (kind == OMP_CLAUSE_DEPEND
32262 && cp_parser_simulate_error (parser))
32263 goto depend_lvalue;
32264 goto skip_comma;
32265 }
32266 if (kind == OMP_CLAUSE_DEPEND)
32267 cp_parser_commit_to_tentative_parse (parser);
32268 if (!cp_lexer_next_token_is (parser->lexer,
32269 CPP_CLOSE_SQUARE))
32270 length = cp_parser_expression (parser);
32271 }
32272 /* Look for the closing `]'. */
32273 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32274 RT_CLOSE_SQUARE))
32275 {
32276 if (kind == OMP_CLAUSE_DEPEND
32277 && cp_parser_simulate_error (parser))
32278 goto depend_lvalue;
32279 goto skip_comma;
32280 }
32281
32282 decl = tree_cons (low_bound, length, decl);
32283 }
32284 break;
32285 default:
32286 break;
32287 }
32288
32289 if (kind == OMP_CLAUSE_DEPEND)
32290 {
32291 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32292 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32293 && cp_parser_simulate_error (parser))
32294 {
32295 depend_lvalue:
32296 cp_parser_abort_tentative_parse (parser);
32297 decl = cp_parser_assignment_expression (parser, NULL,
32298 false, false);
32299 }
32300 else
32301 cp_parser_parse_definitely (parser);
32302 }
32303
32304 tree u = build_omp_clause (token->location, kind);
32305 OMP_CLAUSE_DECL (u) = decl;
32306 OMP_CLAUSE_CHAIN (u) = list;
32307 list = u;
32308 }
32309 else
32310 list = tree_cons (decl, NULL_TREE, list);
32311
32312 get_comma:
32313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32314 break;
32315 cp_lexer_consume_token (parser->lexer);
32316 }
32317
32318 if (colon)
32319 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32320
32321 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32322 {
32323 *colon = true;
32324 cp_parser_require (parser, CPP_COLON, RT_COLON);
32325 return list;
32326 }
32327
32328 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32329 {
32330 int ending;
32331
32332 /* Try to resync to an unnested comma. Copied from
32333 cp_parser_parenthesized_expression_list. */
32334 skip_comma:
32335 if (colon)
32336 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32337 ending = cp_parser_skip_to_closing_parenthesis (parser,
32338 /*recovering=*/true,
32339 /*or_comma=*/true,
32340 /*consume_paren=*/true);
32341 if (ending < 0)
32342 goto get_comma;
32343 }
32344
32345 return list;
32346 }
32347
32348 /* Similarly, but expect leading and trailing parenthesis. This is a very
32349 common case for omp clauses. */
32350
32351 static tree
32352 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32353 {
32354 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32355 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32356 return list;
32357 }
32358
32359 /* OpenACC 2.0:
32360 copy ( variable-list )
32361 copyin ( variable-list )
32362 copyout ( variable-list )
32363 create ( variable-list )
32364 delete ( variable-list )
32365 present ( variable-list ) */
32366
32367 static tree
32368 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32369 tree list)
32370 {
32371 enum gomp_map_kind kind;
32372 switch (c_kind)
32373 {
32374 case PRAGMA_OACC_CLAUSE_COPY:
32375 kind = GOMP_MAP_TOFROM;
32376 break;
32377 case PRAGMA_OACC_CLAUSE_COPYIN:
32378 kind = GOMP_MAP_TO;
32379 break;
32380 case PRAGMA_OACC_CLAUSE_COPYOUT:
32381 kind = GOMP_MAP_FROM;
32382 break;
32383 case PRAGMA_OACC_CLAUSE_CREATE:
32384 kind = GOMP_MAP_ALLOC;
32385 break;
32386 case PRAGMA_OACC_CLAUSE_DELETE:
32387 kind = GOMP_MAP_RELEASE;
32388 break;
32389 case PRAGMA_OACC_CLAUSE_DEVICE:
32390 kind = GOMP_MAP_FORCE_TO;
32391 break;
32392 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32393 kind = GOMP_MAP_DEVICE_RESIDENT;
32394 break;
32395 case PRAGMA_OACC_CLAUSE_HOST:
32396 kind = GOMP_MAP_FORCE_FROM;
32397 break;
32398 case PRAGMA_OACC_CLAUSE_LINK:
32399 kind = GOMP_MAP_LINK;
32400 break;
32401 case PRAGMA_OACC_CLAUSE_PRESENT:
32402 kind = GOMP_MAP_FORCE_PRESENT;
32403 break;
32404 default:
32405 gcc_unreachable ();
32406 }
32407 tree nl, c;
32408 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32409
32410 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32411 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32412
32413 return nl;
32414 }
32415
32416 /* OpenACC 2.0:
32417 deviceptr ( variable-list ) */
32418
32419 static tree
32420 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32421 {
32422 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32423 tree vars, t;
32424
32425 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32426 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32427 variable-list must only allow for pointer variables. */
32428 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32429 for (t = vars; t; t = TREE_CHAIN (t))
32430 {
32431 tree v = TREE_PURPOSE (t);
32432 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32433 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32434 OMP_CLAUSE_DECL (u) = v;
32435 OMP_CLAUSE_CHAIN (u) = list;
32436 list = u;
32437 }
32438
32439 return list;
32440 }
32441
32442 /* OpenACC 2.5:
32443 auto
32444 finalize
32445 independent
32446 nohost
32447 seq */
32448
32449 static tree
32450 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
32451 enum omp_clause_code code,
32452 tree list, location_t location)
32453 {
32454 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32455 tree c = build_omp_clause (location, code);
32456 OMP_CLAUSE_CHAIN (c) = list;
32457 return c;
32458 }
32459
32460 /* OpenACC:
32461 num_gangs ( expression )
32462 num_workers ( expression )
32463 vector_length ( expression ) */
32464
32465 static tree
32466 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32467 const char *str, tree list)
32468 {
32469 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32470
32471 matching_parens parens;
32472 if (!parens.require_open (parser))
32473 return list;
32474
32475 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32476
32477 if (t == error_mark_node
32478 || !parens.require_close (parser))
32479 {
32480 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32481 /*or_comma=*/false,
32482 /*consume_paren=*/true);
32483 return list;
32484 }
32485
32486 check_no_duplicate_clause (list, code, str, loc);
32487
32488 tree c = build_omp_clause (loc, code);
32489 OMP_CLAUSE_OPERAND (c, 0) = t;
32490 OMP_CLAUSE_CHAIN (c) = list;
32491 return c;
32492 }
32493
32494 /* OpenACC:
32495
32496 gang [( gang-arg-list )]
32497 worker [( [num:] int-expr )]
32498 vector [( [length:] int-expr )]
32499
32500 where gang-arg is one of:
32501
32502 [num:] int-expr
32503 static: size-expr
32504
32505 and size-expr may be:
32506
32507 *
32508 int-expr
32509 */
32510
32511 static tree
32512 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
32513 const char *str, tree list)
32514 {
32515 const char *id = "num";
32516 cp_lexer *lexer = parser->lexer;
32517 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32518 location_t loc = cp_lexer_peek_token (lexer)->location;
32519
32520 if (kind == OMP_CLAUSE_VECTOR)
32521 id = "length";
32522
32523 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32524 {
32525 matching_parens parens;
32526 parens.consume_open (parser);
32527
32528 do
32529 {
32530 cp_token *next = cp_lexer_peek_token (lexer);
32531 int idx = 0;
32532
32533 /* Gang static argument. */
32534 if (kind == OMP_CLAUSE_GANG
32535 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32536 {
32537 cp_lexer_consume_token (lexer);
32538
32539 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32540 goto cleanup_error;
32541
32542 idx = 1;
32543 if (ops[idx] != NULL)
32544 {
32545 cp_parser_error (parser, "too many %<static%> arguments");
32546 goto cleanup_error;
32547 }
32548
32549 /* Check for the '*' argument. */
32550 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32551 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32552 || cp_lexer_nth_token_is (parser->lexer, 2,
32553 CPP_CLOSE_PAREN)))
32554 {
32555 cp_lexer_consume_token (lexer);
32556 ops[idx] = integer_minus_one_node;
32557
32558 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32559 {
32560 cp_lexer_consume_token (lexer);
32561 continue;
32562 }
32563 else break;
32564 }
32565 }
32566 /* Worker num: argument and vector length: arguments. */
32567 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32568 && id_equal (next->u.value, id)
32569 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32570 {
32571 cp_lexer_consume_token (lexer); /* id */
32572 cp_lexer_consume_token (lexer); /* ':' */
32573 }
32574
32575 /* Now collect the actual argument. */
32576 if (ops[idx] != NULL_TREE)
32577 {
32578 cp_parser_error (parser, "unexpected argument");
32579 goto cleanup_error;
32580 }
32581
32582 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32583 false);
32584 if (expr == error_mark_node)
32585 goto cleanup_error;
32586
32587 mark_exp_read (expr);
32588 ops[idx] = expr;
32589
32590 if (kind == OMP_CLAUSE_GANG
32591 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32592 {
32593 cp_lexer_consume_token (lexer);
32594 continue;
32595 }
32596 break;
32597 }
32598 while (1);
32599
32600 if (!parens.require_close (parser))
32601 goto cleanup_error;
32602 }
32603
32604 check_no_duplicate_clause (list, kind, str, loc);
32605
32606 c = build_omp_clause (loc, kind);
32607
32608 if (ops[1])
32609 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32610
32611 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32612 OMP_CLAUSE_CHAIN (c) = list;
32613
32614 return c;
32615
32616 cleanup_error:
32617 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32618 return list;
32619 }
32620
32621 /* OpenACC 2.0:
32622 tile ( size-expr-list ) */
32623
32624 static tree
32625 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32626 {
32627 tree c, expr = error_mark_node;
32628 tree tile = NULL_TREE;
32629
32630 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32631 so, but the spec authors never considered such a case and have
32632 differing opinions on what it might mean, including 'not
32633 allowed'.) */
32634 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32635 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32636 clause_loc);
32637
32638 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32639 return list;
32640
32641 do
32642 {
32643 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32644 return list;
32645
32646 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32647 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32648 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32649 {
32650 cp_lexer_consume_token (parser->lexer);
32651 expr = integer_zero_node;
32652 }
32653 else
32654 expr = cp_parser_constant_expression (parser);
32655
32656 tile = tree_cons (NULL_TREE, expr, tile);
32657 }
32658 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32659
32660 /* Consume the trailing ')'. */
32661 cp_lexer_consume_token (parser->lexer);
32662
32663 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32664 tile = nreverse (tile);
32665 OMP_CLAUSE_TILE_LIST (c) = tile;
32666 OMP_CLAUSE_CHAIN (c) = list;
32667 return c;
32668 }
32669
32670 /* OpenACC 2.0
32671 Parse wait clause or directive parameters. */
32672
32673 static tree
32674 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32675 {
32676 vec<tree, va_gc> *args;
32677 tree t, args_tree;
32678
32679 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32680 /*cast_p=*/false,
32681 /*allow_expansion_p=*/true,
32682 /*non_constant_p=*/NULL);
32683
32684 if (args == NULL || args->length () == 0)
32685 {
32686 if (args != NULL)
32687 {
32688 cp_parser_error (parser, "expected integer expression list");
32689 release_tree_vector (args);
32690 }
32691 return list;
32692 }
32693
32694 args_tree = build_tree_list_vec (args);
32695
32696 release_tree_vector (args);
32697
32698 for (t = args_tree; t; t = TREE_CHAIN (t))
32699 {
32700 tree targ = TREE_VALUE (t);
32701
32702 if (targ != error_mark_node)
32703 {
32704 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32705 error ("%<wait%> expression must be integral");
32706 else
32707 {
32708 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32709
32710 targ = mark_rvalue_use (targ);
32711 OMP_CLAUSE_DECL (c) = targ;
32712 OMP_CLAUSE_CHAIN (c) = list;
32713 list = c;
32714 }
32715 }
32716 }
32717
32718 return list;
32719 }
32720
32721 /* OpenACC:
32722 wait ( int-expr-list ) */
32723
32724 static tree
32725 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32726 {
32727 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32728
32729 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
32730 return list;
32731
32732 list = cp_parser_oacc_wait_list (parser, location, list);
32733
32734 return list;
32735 }
32736
32737 /* OpenMP 3.0:
32738 collapse ( constant-expression ) */
32739
32740 static tree
32741 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32742 {
32743 tree c, num;
32744 location_t loc;
32745 HOST_WIDE_INT n;
32746
32747 loc = cp_lexer_peek_token (parser->lexer)->location;
32748 matching_parens parens;
32749 if (!parens.require_open (parser))
32750 return list;
32751
32752 num = cp_parser_constant_expression (parser);
32753
32754 if (!parens.require_close (parser))
32755 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32756 /*or_comma=*/false,
32757 /*consume_paren=*/true);
32758
32759 if (num == error_mark_node)
32760 return list;
32761 num = fold_non_dependent_expr (num);
32762 if (!tree_fits_shwi_p (num)
32763 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32764 || (n = tree_to_shwi (num)) <= 0
32765 || (int) n != n)
32766 {
32767 error_at (loc, "collapse argument needs positive constant integer expression");
32768 return list;
32769 }
32770
32771 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32772 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32773 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32774 OMP_CLAUSE_CHAIN (c) = list;
32775 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32776
32777 return c;
32778 }
32779
32780 /* OpenMP 2.5:
32781 default ( none | shared )
32782
32783 OpenACC:
32784 default ( none | present ) */
32785
32786 static tree
32787 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32788 location_t location, bool is_oacc)
32789 {
32790 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32791 tree c;
32792
32793 matching_parens parens;
32794 if (!parens.require_open (parser))
32795 return list;
32796 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32797 {
32798 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32799 const char *p = IDENTIFIER_POINTER (id);
32800
32801 switch (p[0])
32802 {
32803 case 'n':
32804 if (strcmp ("none", p) != 0)
32805 goto invalid_kind;
32806 kind = OMP_CLAUSE_DEFAULT_NONE;
32807 break;
32808
32809 case 'p':
32810 if (strcmp ("present", p) != 0 || !is_oacc)
32811 goto invalid_kind;
32812 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32813 break;
32814
32815 case 's':
32816 if (strcmp ("shared", p) != 0 || is_oacc)
32817 goto invalid_kind;
32818 kind = OMP_CLAUSE_DEFAULT_SHARED;
32819 break;
32820
32821 default:
32822 goto invalid_kind;
32823 }
32824
32825 cp_lexer_consume_token (parser->lexer);
32826 }
32827 else
32828 {
32829 invalid_kind:
32830 if (is_oacc)
32831 cp_parser_error (parser, "expected %<none%> or %<present%>");
32832 else
32833 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32834 }
32835
32836 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32837 || !parens.require_close (parser))
32838 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32839 /*or_comma=*/false,
32840 /*consume_paren=*/true);
32841
32842 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32843 return list;
32844
32845 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32846 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32847 OMP_CLAUSE_CHAIN (c) = list;
32848 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32849
32850 return c;
32851 }
32852
32853 /* OpenMP 3.1:
32854 final ( expression ) */
32855
32856 static tree
32857 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32858 {
32859 tree t, c;
32860
32861 matching_parens parens;
32862 if (!parens.require_open (parser))
32863 return list;
32864
32865 t = cp_parser_assignment_expression (parser);
32866
32867 if (t == error_mark_node
32868 || !parens.require_close (parser))
32869 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32870 /*or_comma=*/false,
32871 /*consume_paren=*/true);
32872
32873 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32874
32875 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32876 OMP_CLAUSE_FINAL_EXPR (c) = t;
32877 OMP_CLAUSE_CHAIN (c) = list;
32878
32879 return c;
32880 }
32881
32882 /* OpenMP 2.5:
32883 if ( expression )
32884
32885 OpenMP 4.5:
32886 if ( directive-name-modifier : expression )
32887
32888 directive-name-modifier:
32889 parallel | task | taskloop | target data | target | target update
32890 | target enter data | target exit data
32891
32892 OpenMP 5.0:
32893 directive-name-modifier:
32894 ... | simd | cancel */
32895
32896 static tree
32897 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32898 bool is_omp)
32899 {
32900 tree t, c;
32901 enum tree_code if_modifier = ERROR_MARK;
32902
32903 matching_parens parens;
32904 if (!parens.require_open (parser))
32905 return list;
32906
32907 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32908 {
32909 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32910 const char *p = IDENTIFIER_POINTER (id);
32911 int n = 2;
32912
32913 if (strcmp ("cancel", p) == 0)
32914 if_modifier = VOID_CST;
32915 else if (strcmp ("parallel", p) == 0)
32916 if_modifier = OMP_PARALLEL;
32917 else if (strcmp ("simd", p) == 0)
32918 if_modifier = OMP_SIMD;
32919 else if (strcmp ("task", p) == 0)
32920 if_modifier = OMP_TASK;
32921 else if (strcmp ("taskloop", p) == 0)
32922 if_modifier = OMP_TASKLOOP;
32923 else if (strcmp ("target", p) == 0)
32924 {
32925 if_modifier = OMP_TARGET;
32926 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32927 {
32928 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32929 p = IDENTIFIER_POINTER (id);
32930 if (strcmp ("data", p) == 0)
32931 if_modifier = OMP_TARGET_DATA;
32932 else if (strcmp ("update", p) == 0)
32933 if_modifier = OMP_TARGET_UPDATE;
32934 else if (strcmp ("enter", p) == 0)
32935 if_modifier = OMP_TARGET_ENTER_DATA;
32936 else if (strcmp ("exit", p) == 0)
32937 if_modifier = OMP_TARGET_EXIT_DATA;
32938 if (if_modifier != OMP_TARGET)
32939 n = 3;
32940 else
32941 {
32942 location_t loc
32943 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32944 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32945 "or %<exit%>");
32946 if_modifier = ERROR_MARK;
32947 }
32948 if (if_modifier == OMP_TARGET_ENTER_DATA
32949 || if_modifier == OMP_TARGET_EXIT_DATA)
32950 {
32951 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32952 {
32953 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32954 p = IDENTIFIER_POINTER (id);
32955 if (strcmp ("data", p) == 0)
32956 n = 4;
32957 }
32958 if (n != 4)
32959 {
32960 location_t loc
32961 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32962 error_at (loc, "expected %<data%>");
32963 if_modifier = ERROR_MARK;
32964 }
32965 }
32966 }
32967 }
32968 if (if_modifier != ERROR_MARK)
32969 {
32970 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32971 {
32972 while (n-- > 0)
32973 cp_lexer_consume_token (parser->lexer);
32974 }
32975 else
32976 {
32977 if (n > 2)
32978 {
32979 location_t loc
32980 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32981 error_at (loc, "expected %<:%>");
32982 }
32983 if_modifier = ERROR_MARK;
32984 }
32985 }
32986 }
32987
32988 t = cp_parser_assignment_expression (parser);
32989
32990 if (t == error_mark_node
32991 || !parens.require_close (parser))
32992 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32993 /*or_comma=*/false,
32994 /*consume_paren=*/true);
32995
32996 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32997 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32998 {
32999 if (if_modifier != ERROR_MARK
33000 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33001 {
33002 const char *p = NULL;
33003 switch (if_modifier)
33004 {
33005 case VOID_CST: p = "cancel"; break;
33006 case OMP_PARALLEL: p = "parallel"; break;
33007 case OMP_SIMD: p = "simd"; break;
33008 case OMP_TASK: p = "task"; break;
33009 case OMP_TASKLOOP: p = "taskloop"; break;
33010 case OMP_TARGET_DATA: p = "target data"; break;
33011 case OMP_TARGET: p = "target"; break;
33012 case OMP_TARGET_UPDATE: p = "target update"; break;
33013 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
33014 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
33015 default: gcc_unreachable ();
33016 }
33017 error_at (location, "too many %<if%> clauses with %qs modifier",
33018 p);
33019 return list;
33020 }
33021 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33022 {
33023 if (!is_omp)
33024 error_at (location, "too many %<if%> clauses");
33025 else
33026 error_at (location, "too many %<if%> clauses without modifier");
33027 return list;
33028 }
33029 else if (if_modifier == ERROR_MARK
33030 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
33031 {
33032 error_at (location, "if any %<if%> clause has modifier, then all "
33033 "%<if%> clauses have to use modifier");
33034 return list;
33035 }
33036 }
33037
33038 c = build_omp_clause (location, OMP_CLAUSE_IF);
33039 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
33040 OMP_CLAUSE_IF_EXPR (c) = t;
33041 OMP_CLAUSE_CHAIN (c) = list;
33042
33043 return c;
33044 }
33045
33046 /* OpenMP 3.1:
33047 mergeable */
33048
33049 static tree
33050 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
33051 tree list, location_t location)
33052 {
33053 tree c;
33054
33055 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
33056 location);
33057
33058 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
33059 OMP_CLAUSE_CHAIN (c) = list;
33060 return c;
33061 }
33062
33063 /* OpenMP 2.5:
33064 nowait */
33065
33066 static tree
33067 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
33068 tree list, location_t location)
33069 {
33070 tree c;
33071
33072 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
33073
33074 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
33075 OMP_CLAUSE_CHAIN (c) = list;
33076 return c;
33077 }
33078
33079 /* OpenMP 2.5:
33080 num_threads ( expression ) */
33081
33082 static tree
33083 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
33084 location_t location)
33085 {
33086 tree t, c;
33087
33088 matching_parens parens;
33089 if (!parens.require_open (parser))
33090 return list;
33091
33092 t = cp_parser_assignment_expression (parser);
33093
33094 if (t == error_mark_node
33095 || !parens.require_close (parser))
33096 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33097 /*or_comma=*/false,
33098 /*consume_paren=*/true);
33099
33100 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
33101 "num_threads", location);
33102
33103 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
33104 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
33105 OMP_CLAUSE_CHAIN (c) = list;
33106
33107 return c;
33108 }
33109
33110 /* OpenMP 4.5:
33111 num_tasks ( expression ) */
33112
33113 static tree
33114 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
33115 location_t location)
33116 {
33117 tree t, c;
33118
33119 matching_parens parens;
33120 if (!parens.require_open (parser))
33121 return list;
33122
33123 t = cp_parser_assignment_expression (parser);
33124
33125 if (t == error_mark_node
33126 || !parens.require_close (parser))
33127 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33128 /*or_comma=*/false,
33129 /*consume_paren=*/true);
33130
33131 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
33132 "num_tasks", location);
33133
33134 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
33135 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
33136 OMP_CLAUSE_CHAIN (c) = list;
33137
33138 return c;
33139 }
33140
33141 /* OpenMP 4.5:
33142 grainsize ( expression ) */
33143
33144 static tree
33145 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
33146 location_t location)
33147 {
33148 tree t, c;
33149
33150 matching_parens parens;
33151 if (!parens.require_open (parser))
33152 return list;
33153
33154 t = cp_parser_assignment_expression (parser);
33155
33156 if (t == error_mark_node
33157 || !parens.require_close (parser))
33158 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33159 /*or_comma=*/false,
33160 /*consume_paren=*/true);
33161
33162 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
33163 "grainsize", location);
33164
33165 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
33166 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
33167 OMP_CLAUSE_CHAIN (c) = list;
33168
33169 return c;
33170 }
33171
33172 /* OpenMP 4.5:
33173 priority ( expression ) */
33174
33175 static tree
33176 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
33177 location_t location)
33178 {
33179 tree t, c;
33180
33181 matching_parens parens;
33182 if (!parens.require_open (parser))
33183 return list;
33184
33185 t = cp_parser_assignment_expression (parser);
33186
33187 if (t == error_mark_node
33188 || !parens.require_close (parser))
33189 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33190 /*or_comma=*/false,
33191 /*consume_paren=*/true);
33192
33193 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33194 "priority", location);
33195
33196 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33197 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33198 OMP_CLAUSE_CHAIN (c) = list;
33199
33200 return c;
33201 }
33202
33203 /* OpenMP 4.5:
33204 hint ( expression ) */
33205
33206 static tree
33207 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33208 {
33209 tree t, c;
33210
33211 matching_parens parens;
33212 if (!parens.require_open (parser))
33213 return list;
33214
33215 t = cp_parser_assignment_expression (parser);
33216
33217 if (t == error_mark_node
33218 || !parens.require_close (parser))
33219 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33220 /*or_comma=*/false,
33221 /*consume_paren=*/true);
33222
33223 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33224
33225 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33226 OMP_CLAUSE_HINT_EXPR (c) = t;
33227 OMP_CLAUSE_CHAIN (c) = list;
33228
33229 return c;
33230 }
33231
33232 /* OpenMP 4.5:
33233 defaultmap ( tofrom : scalar )
33234
33235 OpenMP 5.0:
33236 defaultmap ( implicit-behavior [ : variable-category ] ) */
33237
33238 static tree
33239 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33240 location_t location)
33241 {
33242 tree c, id;
33243 const char *p;
33244 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33245 enum omp_clause_defaultmap_kind category
33246 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33247
33248 matching_parens parens;
33249 if (!parens.require_open (parser))
33250 return list;
33251
33252 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33253 p = "default";
33254 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33255 {
33256 invalid_behavior:
33257 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33258 "%<tofrom%>, %<firstprivate%>, %<none%> "
33259 "or %<default%>");
33260 goto out_err;
33261 }
33262 else
33263 {
33264 id = cp_lexer_peek_token (parser->lexer)->u.value;
33265 p = IDENTIFIER_POINTER (id);
33266 }
33267
33268 switch (p[0])
33269 {
33270 case 'a':
33271 if (strcmp ("alloc", p) == 0)
33272 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33273 else
33274 goto invalid_behavior;
33275 break;
33276
33277 case 'd':
33278 if (strcmp ("default", p) == 0)
33279 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33280 else
33281 goto invalid_behavior;
33282 break;
33283
33284 case 'f':
33285 if (strcmp ("firstprivate", p) == 0)
33286 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33287 else if (strcmp ("from", p) == 0)
33288 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33289 else
33290 goto invalid_behavior;
33291 break;
33292
33293 case 'n':
33294 if (strcmp ("none", p) == 0)
33295 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33296 else
33297 goto invalid_behavior;
33298 break;
33299
33300 case 't':
33301 if (strcmp ("tofrom", p) == 0)
33302 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33303 else if (strcmp ("to", p) == 0)
33304 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33305 else
33306 goto invalid_behavior;
33307 break;
33308
33309 default:
33310 goto invalid_behavior;
33311 }
33312 cp_lexer_consume_token (parser->lexer);
33313
33314 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33315 {
33316 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33317 goto out_err;
33318
33319 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33320 {
33321 invalid_category:
33322 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33323 "%<pointer%>");
33324 goto out_err;
33325 }
33326 id = cp_lexer_peek_token (parser->lexer)->u.value;
33327 p = IDENTIFIER_POINTER (id);
33328
33329 switch (p[0])
33330 {
33331 case 'a':
33332 if (strcmp ("aggregate", p) == 0)
33333 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33334 else
33335 goto invalid_category;
33336 break;
33337
33338 case 'p':
33339 if (strcmp ("pointer", p) == 0)
33340 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33341 else
33342 goto invalid_category;
33343 break;
33344
33345 case 's':
33346 if (strcmp ("scalar", p) == 0)
33347 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33348 else
33349 goto invalid_category;
33350 break;
33351
33352 default:
33353 goto invalid_category;
33354 }
33355
33356 cp_lexer_consume_token (parser->lexer);
33357 }
33358 if (!parens.require_close (parser))
33359 goto out_err;
33360
33361 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33362 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33363 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33364 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33365 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33366 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33367 {
33368 enum omp_clause_defaultmap_kind cat = category;
33369 location_t loc = OMP_CLAUSE_LOCATION (c);
33370 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33371 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33372 p = NULL;
33373 switch (cat)
33374 {
33375 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33376 p = NULL;
33377 break;
33378 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33379 p = "aggregate";
33380 break;
33381 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33382 p = "pointer";
33383 break;
33384 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33385 p = "scalar";
33386 break;
33387 default:
33388 gcc_unreachable ();
33389 }
33390 if (p)
33391 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33392 p);
33393 else
33394 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33395 "category");
33396 break;
33397 }
33398
33399 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33400 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33401 OMP_CLAUSE_CHAIN (c) = list;
33402 return c;
33403
33404 out_err:
33405 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33406 /*or_comma=*/false,
33407 /*consume_paren=*/true);
33408 return list;
33409 }
33410
33411 /* OpenMP 2.5:
33412 ordered
33413
33414 OpenMP 4.5:
33415 ordered ( constant-expression ) */
33416
33417 static tree
33418 cp_parser_omp_clause_ordered (cp_parser *parser,
33419 tree list, location_t location)
33420 {
33421 tree c, num = NULL_TREE;
33422 HOST_WIDE_INT n;
33423
33424 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33425 "ordered", location);
33426
33427 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33428 {
33429 matching_parens parens;
33430 parens.consume_open (parser);
33431
33432 num = cp_parser_constant_expression (parser);
33433
33434 if (!parens.require_close (parser))
33435 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33436 /*or_comma=*/false,
33437 /*consume_paren=*/true);
33438
33439 if (num == error_mark_node)
33440 return list;
33441 num = fold_non_dependent_expr (num);
33442 if (!tree_fits_shwi_p (num)
33443 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33444 || (n = tree_to_shwi (num)) <= 0
33445 || (int) n != n)
33446 {
33447 error_at (location,
33448 "ordered argument needs positive constant integer "
33449 "expression");
33450 return list;
33451 }
33452 }
33453
33454 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33455 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33456 OMP_CLAUSE_CHAIN (c) = list;
33457 return c;
33458 }
33459
33460 /* OpenMP 2.5:
33461 reduction ( reduction-operator : variable-list )
33462
33463 reduction-operator:
33464 One of: + * - & ^ | && ||
33465
33466 OpenMP 3.1:
33467
33468 reduction-operator:
33469 One of: + * - & ^ | && || min max
33470
33471 OpenMP 4.0:
33472
33473 reduction-operator:
33474 One of: + * - & ^ | && ||
33475 id-expression
33476
33477 OpenMP 5.0:
33478 reduction ( reduction-modifier, reduction-operator : variable-list )
33479 in_reduction ( reduction-operator : variable-list )
33480 task_reduction ( reduction-operator : variable-list ) */
33481
33482 static tree
33483 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33484 bool is_omp, tree list)
33485 {
33486 enum tree_code code = ERROR_MARK;
33487 tree nlist, c, id = NULL_TREE;
33488 bool task = false;
33489 bool inscan = false;
33490
33491 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33492 return list;
33493
33494 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33495 {
33496 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33497 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33498 {
33499 cp_lexer_consume_token (parser->lexer);
33500 cp_lexer_consume_token (parser->lexer);
33501 }
33502 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33503 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33504 {
33505 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33506 const char *p = IDENTIFIER_POINTER (id);
33507 if (strcmp (p, "task") == 0)
33508 task = true;
33509 else if (strcmp (p, "inscan") == 0)
33510 {
33511 inscan = true;
33512 sorry ("%<inscan%> modifier on %<reduction%> clause "
33513 "not supported yet");
33514 }
33515 if (task || inscan)
33516 {
33517 cp_lexer_consume_token (parser->lexer);
33518 cp_lexer_consume_token (parser->lexer);
33519 }
33520 }
33521 }
33522
33523 switch (cp_lexer_peek_token (parser->lexer)->type)
33524 {
33525 case CPP_PLUS: code = PLUS_EXPR; break;
33526 case CPP_MULT: code = MULT_EXPR; break;
33527 case CPP_MINUS: code = MINUS_EXPR; break;
33528 case CPP_AND: code = BIT_AND_EXPR; break;
33529 case CPP_XOR: code = BIT_XOR_EXPR; break;
33530 case CPP_OR: code = BIT_IOR_EXPR; break;
33531 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33532 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33533 default: break;
33534 }
33535
33536 if (code != ERROR_MARK)
33537 cp_lexer_consume_token (parser->lexer);
33538 else
33539 {
33540 bool saved_colon_corrects_to_scope_p;
33541 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33542 parser->colon_corrects_to_scope_p = false;
33543 id = cp_parser_id_expression (parser, /*template_p=*/false,
33544 /*check_dependency_p=*/true,
33545 /*template_p=*/NULL,
33546 /*declarator_p=*/false,
33547 /*optional_p=*/false);
33548 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33549 if (identifier_p (id))
33550 {
33551 const char *p = IDENTIFIER_POINTER (id);
33552
33553 if (strcmp (p, "min") == 0)
33554 code = MIN_EXPR;
33555 else if (strcmp (p, "max") == 0)
33556 code = MAX_EXPR;
33557 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33558 code = PLUS_EXPR;
33559 else if (id == ovl_op_identifier (false, MULT_EXPR))
33560 code = MULT_EXPR;
33561 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33562 code = MINUS_EXPR;
33563 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33564 code = BIT_AND_EXPR;
33565 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33566 code = BIT_IOR_EXPR;
33567 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33568 code = BIT_XOR_EXPR;
33569 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33570 code = TRUTH_ANDIF_EXPR;
33571 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33572 code = TRUTH_ORIF_EXPR;
33573 id = omp_reduction_id (code, id, NULL_TREE);
33574 tree scope = parser->scope;
33575 if (scope)
33576 id = build_qualified_name (NULL_TREE, scope, id, false);
33577 parser->scope = NULL_TREE;
33578 parser->qualifying_scope = NULL_TREE;
33579 parser->object_scope = NULL_TREE;
33580 }
33581 else
33582 {
33583 error ("invalid reduction-identifier");
33584 resync_fail:
33585 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33586 /*or_comma=*/false,
33587 /*consume_paren=*/true);
33588 return list;
33589 }
33590 }
33591
33592 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33593 goto resync_fail;
33594
33595 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33596 NULL);
33597 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33598 {
33599 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33600 if (task)
33601 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33602 else if (inscan)
33603 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33604 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33605 }
33606
33607 return nlist;
33608 }
33609
33610 /* OpenMP 2.5:
33611 schedule ( schedule-kind )
33612 schedule ( schedule-kind , expression )
33613
33614 schedule-kind:
33615 static | dynamic | guided | runtime | auto
33616
33617 OpenMP 4.5:
33618 schedule ( schedule-modifier : schedule-kind )
33619 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33620
33621 schedule-modifier:
33622 simd
33623 monotonic
33624 nonmonotonic */
33625
33626 static tree
33627 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33628 {
33629 tree c, t;
33630 int modifiers = 0, nmodifiers = 0;
33631
33632 matching_parens parens;
33633 if (!parens.require_open (parser))
33634 return list;
33635
33636 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33637
33638 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33639 {
33640 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33641 const char *p = IDENTIFIER_POINTER (id);
33642 if (strcmp ("simd", p) == 0)
33643 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33644 else if (strcmp ("monotonic", p) == 0)
33645 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33646 else if (strcmp ("nonmonotonic", p) == 0)
33647 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33648 else
33649 break;
33650 cp_lexer_consume_token (parser->lexer);
33651 if (nmodifiers++ == 0
33652 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33653 cp_lexer_consume_token (parser->lexer);
33654 else
33655 {
33656 cp_parser_require (parser, CPP_COLON, RT_COLON);
33657 break;
33658 }
33659 }
33660
33661 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33662 {
33663 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33664 const char *p = IDENTIFIER_POINTER (id);
33665
33666 switch (p[0])
33667 {
33668 case 'd':
33669 if (strcmp ("dynamic", p) != 0)
33670 goto invalid_kind;
33671 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33672 break;
33673
33674 case 'g':
33675 if (strcmp ("guided", p) != 0)
33676 goto invalid_kind;
33677 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33678 break;
33679
33680 case 'r':
33681 if (strcmp ("runtime", p) != 0)
33682 goto invalid_kind;
33683 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33684 break;
33685
33686 default:
33687 goto invalid_kind;
33688 }
33689 }
33690 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33691 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33692 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33693 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33694 else
33695 goto invalid_kind;
33696 cp_lexer_consume_token (parser->lexer);
33697
33698 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33699 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33700 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33701 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33702 {
33703 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33704 "specified");
33705 modifiers = 0;
33706 }
33707
33708 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33709 {
33710 cp_token *token;
33711 cp_lexer_consume_token (parser->lexer);
33712
33713 token = cp_lexer_peek_token (parser->lexer);
33714 t = cp_parser_assignment_expression (parser);
33715
33716 if (t == error_mark_node)
33717 goto resync_fail;
33718 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33719 error_at (token->location, "schedule %<runtime%> does not take "
33720 "a %<chunk_size%> parameter");
33721 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33722 error_at (token->location, "schedule %<auto%> does not take "
33723 "a %<chunk_size%> parameter");
33724 else
33725 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33726
33727 if (!parens.require_close (parser))
33728 goto resync_fail;
33729 }
33730 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33731 goto resync_fail;
33732
33733 OMP_CLAUSE_SCHEDULE_KIND (c)
33734 = (enum omp_clause_schedule_kind)
33735 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33736
33737 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33738 OMP_CLAUSE_CHAIN (c) = list;
33739 return c;
33740
33741 invalid_kind:
33742 cp_parser_error (parser, "invalid schedule kind");
33743 resync_fail:
33744 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33745 /*or_comma=*/false,
33746 /*consume_paren=*/true);
33747 return list;
33748 }
33749
33750 /* OpenMP 3.0:
33751 untied */
33752
33753 static tree
33754 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33755 tree list, location_t location)
33756 {
33757 tree c;
33758
33759 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33760
33761 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33762 OMP_CLAUSE_CHAIN (c) = list;
33763 return c;
33764 }
33765
33766 /* OpenMP 4.0:
33767 inbranch
33768 notinbranch */
33769
33770 static tree
33771 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33772 tree list, location_t location)
33773 {
33774 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33775 tree c = build_omp_clause (location, code);
33776 OMP_CLAUSE_CHAIN (c) = list;
33777 return c;
33778 }
33779
33780 /* OpenMP 4.0:
33781 parallel
33782 for
33783 sections
33784 taskgroup */
33785
33786 static tree
33787 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33788 enum omp_clause_code code,
33789 tree list, location_t location)
33790 {
33791 tree c = build_omp_clause (location, code);
33792 OMP_CLAUSE_CHAIN (c) = list;
33793 return c;
33794 }
33795
33796 /* OpenMP 4.5:
33797 nogroup */
33798
33799 static tree
33800 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33801 tree list, location_t location)
33802 {
33803 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33804 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33805 OMP_CLAUSE_CHAIN (c) = list;
33806 return c;
33807 }
33808
33809 /* OpenMP 4.5:
33810 simd
33811 threads */
33812
33813 static tree
33814 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33815 enum omp_clause_code code,
33816 tree list, location_t location)
33817 {
33818 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33819 tree c = build_omp_clause (location, code);
33820 OMP_CLAUSE_CHAIN (c) = list;
33821 return c;
33822 }
33823
33824 /* OpenMP 4.0:
33825 num_teams ( expression ) */
33826
33827 static tree
33828 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33829 location_t location)
33830 {
33831 tree t, c;
33832
33833 matching_parens parens;
33834 if (!parens.require_open (parser))
33835 return list;
33836
33837 t = cp_parser_assignment_expression (parser);
33838
33839 if (t == error_mark_node
33840 || !parens.require_close (parser))
33841 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33842 /*or_comma=*/false,
33843 /*consume_paren=*/true);
33844
33845 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
33846 "num_teams", location);
33847
33848 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
33849 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
33850 OMP_CLAUSE_CHAIN (c) = list;
33851
33852 return c;
33853 }
33854
33855 /* OpenMP 4.0:
33856 thread_limit ( expression ) */
33857
33858 static tree
33859 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
33860 location_t location)
33861 {
33862 tree t, c;
33863
33864 matching_parens parens;
33865 if (!parens.require_open (parser))
33866 return list;
33867
33868 t = cp_parser_assignment_expression (parser);
33869
33870 if (t == error_mark_node
33871 || !parens.require_close (parser))
33872 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33873 /*or_comma=*/false,
33874 /*consume_paren=*/true);
33875
33876 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
33877 "thread_limit", location);
33878
33879 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
33880 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
33881 OMP_CLAUSE_CHAIN (c) = list;
33882
33883 return c;
33884 }
33885
33886 /* OpenMP 4.0:
33887 aligned ( variable-list )
33888 aligned ( variable-list : constant-expression ) */
33889
33890 static tree
33891 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
33892 {
33893 tree nlist, c, alignment = NULL_TREE;
33894 bool colon;
33895
33896 matching_parens parens;
33897 if (!parens.require_open (parser))
33898 return list;
33899
33900 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
33901 &colon);
33902
33903 if (colon)
33904 {
33905 alignment = cp_parser_constant_expression (parser);
33906
33907 if (!parens.require_close (parser))
33908 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33909 /*or_comma=*/false,
33910 /*consume_paren=*/true);
33911
33912 if (alignment == error_mark_node)
33913 alignment = NULL_TREE;
33914 }
33915
33916 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33917 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
33918
33919 return nlist;
33920 }
33921
33922 /* OpenMP 2.5:
33923 lastprivate ( variable-list )
33924
33925 OpenMP 5.0:
33926 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
33927
33928 static tree
33929 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
33930 {
33931 bool conditional = false;
33932
33933 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33934 return list;
33935
33936 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33937 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
33938 {
33939 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33940 const char *p = IDENTIFIER_POINTER (id);
33941
33942 if (strcmp ("conditional", p) == 0)
33943 {
33944 conditional = true;
33945 cp_lexer_consume_token (parser->lexer);
33946 cp_lexer_consume_token (parser->lexer);
33947 }
33948 }
33949
33950 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
33951 list, NULL);
33952
33953 if (conditional)
33954 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33955 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
33956 return nlist;
33957 }
33958
33959 /* OpenMP 4.0:
33960 linear ( variable-list )
33961 linear ( variable-list : expression )
33962
33963 OpenMP 4.5:
33964 linear ( modifier ( variable-list ) )
33965 linear ( modifier ( variable-list ) : expression ) */
33966
33967 static tree
33968 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
33969 bool declare_simd)
33970 {
33971 tree nlist, c, step = integer_one_node;
33972 bool colon;
33973 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
33974
33975 matching_parens parens;
33976 if (!parens.require_open (parser))
33977 return list;
33978
33979 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33980 {
33981 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33982 const char *p = IDENTIFIER_POINTER (id);
33983
33984 if (strcmp ("ref", p) == 0)
33985 kind = OMP_CLAUSE_LINEAR_REF;
33986 else if (strcmp ("val", p) == 0)
33987 kind = OMP_CLAUSE_LINEAR_VAL;
33988 else if (strcmp ("uval", p) == 0)
33989 kind = OMP_CLAUSE_LINEAR_UVAL;
33990 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
33991 cp_lexer_consume_token (parser->lexer);
33992 else
33993 kind = OMP_CLAUSE_LINEAR_DEFAULT;
33994 }
33995
33996 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
33997 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
33998 &colon);
33999 else
34000 {
34001 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
34002 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
34003 if (colon)
34004 cp_parser_require (parser, CPP_COLON, RT_COLON);
34005 else if (!parens.require_close (parser))
34006 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34007 /*or_comma=*/false,
34008 /*consume_paren=*/true);
34009 }
34010
34011 if (colon)
34012 {
34013 step = NULL_TREE;
34014 if (declare_simd
34015 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34016 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
34017 {
34018 cp_token *token = cp_lexer_peek_token (parser->lexer);
34019 cp_parser_parse_tentatively (parser);
34020 step = cp_parser_id_expression (parser, /*template_p=*/false,
34021 /*check_dependency_p=*/true,
34022 /*template_p=*/NULL,
34023 /*declarator_p=*/false,
34024 /*optional_p=*/false);
34025 if (step != error_mark_node)
34026 step = cp_parser_lookup_name_simple (parser, step, token->location);
34027 if (step == error_mark_node)
34028 {
34029 step = NULL_TREE;
34030 cp_parser_abort_tentative_parse (parser);
34031 }
34032 else if (!cp_parser_parse_definitely (parser))
34033 step = NULL_TREE;
34034 }
34035 if (!step)
34036 step = cp_parser_assignment_expression (parser);
34037
34038 if (!parens.require_close (parser))
34039 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34040 /*or_comma=*/false,
34041 /*consume_paren=*/true);
34042
34043 if (step == error_mark_node)
34044 return list;
34045 }
34046
34047 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34048 {
34049 OMP_CLAUSE_LINEAR_STEP (c) = step;
34050 OMP_CLAUSE_LINEAR_KIND (c) = kind;
34051 }
34052
34053 return nlist;
34054 }
34055
34056 /* OpenMP 4.0:
34057 safelen ( constant-expression ) */
34058
34059 static tree
34060 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
34061 location_t location)
34062 {
34063 tree t, c;
34064
34065 matching_parens parens;
34066 if (!parens.require_open (parser))
34067 return list;
34068
34069 t = cp_parser_constant_expression (parser);
34070
34071 if (t == error_mark_node
34072 || !parens.require_close (parser))
34073 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34074 /*or_comma=*/false,
34075 /*consume_paren=*/true);
34076
34077 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
34078
34079 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
34080 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
34081 OMP_CLAUSE_CHAIN (c) = list;
34082
34083 return c;
34084 }
34085
34086 /* OpenMP 4.0:
34087 simdlen ( constant-expression ) */
34088
34089 static tree
34090 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
34091 location_t location)
34092 {
34093 tree t, c;
34094
34095 matching_parens parens;
34096 if (!parens.require_open (parser))
34097 return list;
34098
34099 t = cp_parser_constant_expression (parser);
34100
34101 if (t == error_mark_node
34102 || !parens.require_close (parser))
34103 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34104 /*or_comma=*/false,
34105 /*consume_paren=*/true);
34106
34107 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
34108
34109 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
34110 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
34111 OMP_CLAUSE_CHAIN (c) = list;
34112
34113 return c;
34114 }
34115
34116 /* OpenMP 4.5:
34117 vec:
34118 identifier [+/- integer]
34119 vec , identifier [+/- integer]
34120 */
34121
34122 static tree
34123 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
34124 tree list)
34125 {
34126 tree vec = NULL;
34127
34128 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34129 {
34130 cp_parser_error (parser, "expected identifier");
34131 return list;
34132 }
34133
34134 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34135 {
34136 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
34137 tree t, identifier = cp_parser_identifier (parser);
34138 tree addend = NULL;
34139
34140 if (identifier == error_mark_node)
34141 t = error_mark_node;
34142 else
34143 {
34144 t = cp_parser_lookup_name_simple
34145 (parser, identifier,
34146 cp_lexer_peek_token (parser->lexer)->location);
34147 if (t == error_mark_node)
34148 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
34149 id_loc);
34150 }
34151
34152 bool neg = false;
34153 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34154 neg = true;
34155 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
34156 {
34157 addend = integer_zero_node;
34158 goto add_to_vector;
34159 }
34160 cp_lexer_consume_token (parser->lexer);
34161
34162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
34163 {
34164 cp_parser_error (parser, "expected integer");
34165 return list;
34166 }
34167
34168 addend = cp_lexer_peek_token (parser->lexer)->u.value;
34169 if (TREE_CODE (addend) != INTEGER_CST)
34170 {
34171 cp_parser_error (parser, "expected integer");
34172 return list;
34173 }
34174 cp_lexer_consume_token (parser->lexer);
34175
34176 add_to_vector:
34177 if (t != error_mark_node)
34178 {
34179 vec = tree_cons (addend, t, vec);
34180 if (neg)
34181 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
34182 }
34183
34184 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34185 break;
34186
34187 cp_lexer_consume_token (parser->lexer);
34188 }
34189
34190 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34191 {
34192 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34193 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34194 OMP_CLAUSE_DECL (u) = nreverse (vec);
34195 OMP_CLAUSE_CHAIN (u) = list;
34196 return u;
34197 }
34198 return list;
34199 }
34200
34201 /* OpenMP 5.0:
34202 iterators ( iterators-definition )
34203
34204 iterators-definition:
34205 iterator-specifier
34206 iterator-specifier , iterators-definition
34207
34208 iterator-specifier:
34209 identifier = range-specification
34210 iterator-type identifier = range-specification
34211
34212 range-specification:
34213 begin : end
34214 begin : end : step */
34215
34216 static tree
34217 cp_parser_omp_iterators (cp_parser *parser)
34218 {
34219 tree ret = NULL_TREE, *last = &ret;
34220 cp_lexer_consume_token (parser->lexer);
34221
34222 matching_parens parens;
34223 if (!parens.require_open (parser))
34224 return error_mark_node;
34225
34226 bool saved_colon_corrects_to_scope_p
34227 = parser->colon_corrects_to_scope_p;
34228 bool saved_colon_doesnt_start_class_def_p
34229 = parser->colon_doesnt_start_class_def_p;
34230
34231 do
34232 {
34233 tree iter_type;
34234 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34235 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34236 iter_type = integer_type_node;
34237 else
34238 {
34239 const char *saved_message
34240 = parser->type_definition_forbidden_message;
34241 parser->type_definition_forbidden_message
34242 = G_("types may not be defined in iterator type");
34243
34244 iter_type = cp_parser_type_id (parser);
34245
34246 parser->type_definition_forbidden_message = saved_message;
34247 }
34248
34249 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34250 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34251 {
34252 cp_parser_error (parser, "expected identifier");
34253 break;
34254 }
34255
34256 tree id = cp_parser_identifier (parser);
34257 if (id == error_mark_node)
34258 break;
34259
34260 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34261 break;
34262
34263 parser->colon_corrects_to_scope_p = false;
34264 parser->colon_doesnt_start_class_def_p = true;
34265 tree begin = cp_parser_assignment_expression (parser);
34266
34267 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34268 break;
34269
34270 tree end = cp_parser_assignment_expression (parser);
34271
34272 tree step = integer_one_node;
34273 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34274 {
34275 cp_lexer_consume_token (parser->lexer);
34276 step = cp_parser_assignment_expression (parser);
34277 }
34278
34279 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34280 DECL_ARTIFICIAL (iter_var) = 1;
34281 DECL_CONTEXT (iter_var) = current_function_decl;
34282 pushdecl (iter_var);
34283
34284 *last = make_tree_vec (6);
34285 TREE_VEC_ELT (*last, 0) = iter_var;
34286 TREE_VEC_ELT (*last, 1) = begin;
34287 TREE_VEC_ELT (*last, 2) = end;
34288 TREE_VEC_ELT (*last, 3) = step;
34289 last = &TREE_CHAIN (*last);
34290
34291 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34292 {
34293 cp_lexer_consume_token (parser->lexer);
34294 continue;
34295 }
34296 break;
34297 }
34298 while (1);
34299
34300 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34301 parser->colon_doesnt_start_class_def_p
34302 = saved_colon_doesnt_start_class_def_p;
34303
34304 if (!parens.require_close (parser))
34305 cp_parser_skip_to_closing_parenthesis (parser,
34306 /*recovering=*/true,
34307 /*or_comma=*/false,
34308 /*consume_paren=*/true);
34309
34310 return ret ? ret : error_mark_node;
34311 }
34312
34313 /* OpenMP 4.0:
34314 depend ( depend-kind : variable-list )
34315
34316 depend-kind:
34317 in | out | inout
34318
34319 OpenMP 4.5:
34320 depend ( source )
34321
34322 depend ( sink : vec )
34323
34324 OpenMP 5.0:
34325 depend ( depend-modifier , depend-kind: variable-list )
34326
34327 depend-kind:
34328 in | out | inout | mutexinoutset | depobj
34329
34330 depend-modifier:
34331 iterator ( iterators-definition ) */
34332
34333 static tree
34334 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34335 {
34336 tree nlist, c, iterators = NULL_TREE;
34337 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34338
34339 matching_parens parens;
34340 if (!parens.require_open (parser))
34341 return list;
34342
34343 do
34344 {
34345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34346 goto invalid_kind;
34347
34348 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34349 const char *p = IDENTIFIER_POINTER (id);
34350
34351 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34352 {
34353 begin_scope (sk_omp, NULL);
34354 iterators = cp_parser_omp_iterators (parser);
34355 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34356 continue;
34357 }
34358 if (strcmp ("in", p) == 0)
34359 kind = OMP_CLAUSE_DEPEND_IN;
34360 else if (strcmp ("inout", p) == 0)
34361 kind = OMP_CLAUSE_DEPEND_INOUT;
34362 else if (strcmp ("mutexinoutset", p) == 0)
34363 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34364 else if (strcmp ("out", p) == 0)
34365 kind = OMP_CLAUSE_DEPEND_OUT;
34366 else if (strcmp ("depobj", p) == 0)
34367 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34368 else if (strcmp ("sink", p) == 0)
34369 kind = OMP_CLAUSE_DEPEND_SINK;
34370 else if (strcmp ("source", p) == 0)
34371 kind = OMP_CLAUSE_DEPEND_SOURCE;
34372 else
34373 goto invalid_kind;
34374 break;
34375 }
34376 while (1);
34377
34378 cp_lexer_consume_token (parser->lexer);
34379
34380 if (iterators
34381 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34382 {
34383 poplevel (0, 1, 0);
34384 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34385 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34386 iterators = NULL_TREE;
34387 }
34388
34389 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34390 {
34391 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34392 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34393 OMP_CLAUSE_DECL (c) = NULL_TREE;
34394 OMP_CLAUSE_CHAIN (c) = list;
34395 if (!parens.require_close (parser))
34396 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34397 /*or_comma=*/false,
34398 /*consume_paren=*/true);
34399 return c;
34400 }
34401
34402 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34403 goto resync_fail;
34404
34405 if (kind == OMP_CLAUSE_DEPEND_SINK)
34406 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34407 else
34408 {
34409 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34410 list, NULL);
34411
34412 if (iterators)
34413 {
34414 tree block = poplevel (1, 1, 0);
34415 if (iterators == error_mark_node)
34416 iterators = NULL_TREE;
34417 else
34418 TREE_VEC_ELT (iterators, 5) = block;
34419 }
34420
34421 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34422 {
34423 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34424 if (iterators)
34425 OMP_CLAUSE_DECL (c)
34426 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34427 }
34428 }
34429 return nlist;
34430
34431 invalid_kind:
34432 cp_parser_error (parser, "invalid depend kind");
34433 resync_fail:
34434 if (iterators)
34435 poplevel (0, 1, 0);
34436 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34437 /*or_comma=*/false,
34438 /*consume_paren=*/true);
34439 return list;
34440 }
34441
34442 /* OpenMP 4.0:
34443 map ( map-kind : variable-list )
34444 map ( variable-list )
34445
34446 map-kind:
34447 alloc | to | from | tofrom
34448
34449 OpenMP 4.5:
34450 map-kind:
34451 alloc | to | from | tofrom | release | delete
34452
34453 map ( always [,] map-kind: variable-list ) */
34454
34455 static tree
34456 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34457 {
34458 tree nlist, c;
34459 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34460 bool always = false;
34461
34462 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34463 return list;
34464
34465 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34466 {
34467 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34468 const char *p = IDENTIFIER_POINTER (id);
34469
34470 if (strcmp ("always", p) == 0)
34471 {
34472 int nth = 2;
34473 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34474 nth++;
34475 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34476 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34477 == RID_DELETE))
34478 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34479 == CPP_COLON))
34480 {
34481 always = true;
34482 cp_lexer_consume_token (parser->lexer);
34483 if (nth == 3)
34484 cp_lexer_consume_token (parser->lexer);
34485 }
34486 }
34487 }
34488
34489 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34490 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34491 {
34492 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34493 const char *p = IDENTIFIER_POINTER (id);
34494
34495 if (strcmp ("alloc", p) == 0)
34496 kind = GOMP_MAP_ALLOC;
34497 else if (strcmp ("to", p) == 0)
34498 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34499 else if (strcmp ("from", p) == 0)
34500 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34501 else if (strcmp ("tofrom", p) == 0)
34502 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34503 else if (strcmp ("release", p) == 0)
34504 kind = GOMP_MAP_RELEASE;
34505 else
34506 {
34507 cp_parser_error (parser, "invalid map kind");
34508 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34509 /*or_comma=*/false,
34510 /*consume_paren=*/true);
34511 return list;
34512 }
34513 cp_lexer_consume_token (parser->lexer);
34514 cp_lexer_consume_token (parser->lexer);
34515 }
34516 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34517 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34518 {
34519 kind = GOMP_MAP_DELETE;
34520 cp_lexer_consume_token (parser->lexer);
34521 cp_lexer_consume_token (parser->lexer);
34522 }
34523
34524 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34525 NULL);
34526
34527 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34528 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34529
34530 return nlist;
34531 }
34532
34533 /* OpenMP 4.0:
34534 device ( expression ) */
34535
34536 static tree
34537 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34538 location_t location)
34539 {
34540 tree t, c;
34541
34542 matching_parens parens;
34543 if (!parens.require_open (parser))
34544 return list;
34545
34546 t = cp_parser_assignment_expression (parser);
34547
34548 if (t == error_mark_node
34549 || !parens.require_close (parser))
34550 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34551 /*or_comma=*/false,
34552 /*consume_paren=*/true);
34553
34554 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34555 "device", location);
34556
34557 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34558 OMP_CLAUSE_DEVICE_ID (c) = t;
34559 OMP_CLAUSE_CHAIN (c) = list;
34560
34561 return c;
34562 }
34563
34564 /* OpenMP 4.0:
34565 dist_schedule ( static )
34566 dist_schedule ( static , expression ) */
34567
34568 static tree
34569 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34570 location_t location)
34571 {
34572 tree c, t;
34573
34574 matching_parens parens;
34575 if (!parens.require_open (parser))
34576 return list;
34577
34578 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34579
34580 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34581 goto invalid_kind;
34582 cp_lexer_consume_token (parser->lexer);
34583
34584 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34585 {
34586 cp_lexer_consume_token (parser->lexer);
34587
34588 t = cp_parser_assignment_expression (parser);
34589
34590 if (t == error_mark_node)
34591 goto resync_fail;
34592 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34593
34594 if (!parens.require_close (parser))
34595 goto resync_fail;
34596 }
34597 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34598 goto resync_fail;
34599
34600 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34601 location);
34602 OMP_CLAUSE_CHAIN (c) = list;
34603 return c;
34604
34605 invalid_kind:
34606 cp_parser_error (parser, "invalid dist_schedule kind");
34607 resync_fail:
34608 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34609 /*or_comma=*/false,
34610 /*consume_paren=*/true);
34611 return list;
34612 }
34613
34614 /* OpenMP 4.0:
34615 proc_bind ( proc-bind-kind )
34616
34617 proc-bind-kind:
34618 master | close | spread */
34619
34620 static tree
34621 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34622 location_t location)
34623 {
34624 tree c;
34625 enum omp_clause_proc_bind_kind kind;
34626
34627 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34628 return list;
34629
34630 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34631 {
34632 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34633 const char *p = IDENTIFIER_POINTER (id);
34634
34635 if (strcmp ("master", p) == 0)
34636 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34637 else if (strcmp ("close", p) == 0)
34638 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34639 else if (strcmp ("spread", p) == 0)
34640 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34641 else
34642 goto invalid_kind;
34643 }
34644 else
34645 goto invalid_kind;
34646
34647 cp_lexer_consume_token (parser->lexer);
34648 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34649 goto resync_fail;
34650
34651 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34652 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34653 location);
34654 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34655 OMP_CLAUSE_CHAIN (c) = list;
34656 return c;
34657
34658 invalid_kind:
34659 cp_parser_error (parser, "invalid depend kind");
34660 resync_fail:
34661 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34662 /*or_comma=*/false,
34663 /*consume_paren=*/true);
34664 return list;
34665 }
34666
34667 /* OpenACC:
34668 async [( int-expr )] */
34669
34670 static tree
34671 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34672 {
34673 tree c, t;
34674 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34675
34676 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34677
34678 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34679 {
34680 matching_parens parens;
34681 parens.consume_open (parser);
34682
34683 t = cp_parser_expression (parser);
34684 if (t == error_mark_node
34685 || !parens.require_close (parser))
34686 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34687 /*or_comma=*/false,
34688 /*consume_paren=*/true);
34689 }
34690
34691 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34692
34693 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34694 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34695 OMP_CLAUSE_CHAIN (c) = list;
34696 list = c;
34697
34698 return list;
34699 }
34700
34701 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34702 is a bitmask in MASK. Return the list of clauses found. */
34703
34704 static tree
34705 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34706 const char *where, cp_token *pragma_tok,
34707 bool finish_p = true)
34708 {
34709 tree clauses = NULL;
34710 bool first = true;
34711
34712 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34713 {
34714 location_t here;
34715 pragma_omp_clause c_kind;
34716 omp_clause_code code;
34717 const char *c_name;
34718 tree prev = clauses;
34719
34720 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34721 cp_lexer_consume_token (parser->lexer);
34722
34723 here = cp_lexer_peek_token (parser->lexer)->location;
34724 c_kind = cp_parser_omp_clause_name (parser);
34725
34726 switch (c_kind)
34727 {
34728 case PRAGMA_OACC_CLAUSE_ASYNC:
34729 clauses = cp_parser_oacc_clause_async (parser, clauses);
34730 c_name = "async";
34731 break;
34732 case PRAGMA_OACC_CLAUSE_AUTO:
34733 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
34734 clauses, here);
34735 c_name = "auto";
34736 break;
34737 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34738 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34739 c_name = "collapse";
34740 break;
34741 case PRAGMA_OACC_CLAUSE_COPY:
34742 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34743 c_name = "copy";
34744 break;
34745 case PRAGMA_OACC_CLAUSE_COPYIN:
34746 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34747 c_name = "copyin";
34748 break;
34749 case PRAGMA_OACC_CLAUSE_COPYOUT:
34750 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34751 c_name = "copyout";
34752 break;
34753 case PRAGMA_OACC_CLAUSE_CREATE:
34754 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34755 c_name = "create";
34756 break;
34757 case PRAGMA_OACC_CLAUSE_DELETE:
34758 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34759 c_name = "delete";
34760 break;
34761 case PRAGMA_OMP_CLAUSE_DEFAULT:
34762 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34763 c_name = "default";
34764 break;
34765 case PRAGMA_OACC_CLAUSE_DEVICE:
34766 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34767 c_name = "device";
34768 break;
34769 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34770 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34771 c_name = "deviceptr";
34772 break;
34773 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34774 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34775 c_name = "device_resident";
34776 break;
34777 case PRAGMA_OACC_CLAUSE_FINALIZE:
34778 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_FINALIZE,
34779 clauses, here);
34780 c_name = "finalize";
34781 break;
34782 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34783 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34784 clauses);
34785 c_name = "firstprivate";
34786 break;
34787 case PRAGMA_OACC_CLAUSE_GANG:
34788 c_name = "gang";
34789 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
34790 c_name, clauses);
34791 break;
34792 case PRAGMA_OACC_CLAUSE_HOST:
34793 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34794 c_name = "host";
34795 break;
34796 case PRAGMA_OACC_CLAUSE_IF:
34797 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34798 c_name = "if";
34799 break;
34800 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34801 clauses = cp_parser_oacc_simple_clause (parser,
34802 OMP_CLAUSE_IF_PRESENT,
34803 clauses, here);
34804 c_name = "if_present";
34805 break;
34806 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34807 clauses = cp_parser_oacc_simple_clause (parser,
34808 OMP_CLAUSE_INDEPENDENT,
34809 clauses, here);
34810 c_name = "independent";
34811 break;
34812 case PRAGMA_OACC_CLAUSE_LINK:
34813 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34814 c_name = "link";
34815 break;
34816 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34817 code = OMP_CLAUSE_NUM_GANGS;
34818 c_name = "num_gangs";
34819 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34820 clauses);
34821 break;
34822 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34823 c_name = "num_workers";
34824 code = OMP_CLAUSE_NUM_WORKERS;
34825 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34826 clauses);
34827 break;
34828 case PRAGMA_OACC_CLAUSE_PRESENT:
34829 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34830 c_name = "present";
34831 break;
34832 case PRAGMA_OACC_CLAUSE_PRIVATE:
34833 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34834 clauses);
34835 c_name = "private";
34836 break;
34837 case PRAGMA_OACC_CLAUSE_REDUCTION:
34838 clauses
34839 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34840 false, clauses);
34841 c_name = "reduction";
34842 break;
34843 case PRAGMA_OACC_CLAUSE_SEQ:
34844 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
34845 clauses, here);
34846 c_name = "seq";
34847 break;
34848 case PRAGMA_OACC_CLAUSE_TILE:
34849 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
34850 c_name = "tile";
34851 break;
34852 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
34853 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34854 clauses);
34855 c_name = "use_device";
34856 break;
34857 case PRAGMA_OACC_CLAUSE_VECTOR:
34858 c_name = "vector";
34859 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
34860 c_name, clauses);
34861 break;
34862 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
34863 c_name = "vector_length";
34864 code = OMP_CLAUSE_VECTOR_LENGTH;
34865 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34866 clauses);
34867 break;
34868 case PRAGMA_OACC_CLAUSE_WAIT:
34869 clauses = cp_parser_oacc_clause_wait (parser, clauses);
34870 c_name = "wait";
34871 break;
34872 case PRAGMA_OACC_CLAUSE_WORKER:
34873 c_name = "worker";
34874 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
34875 c_name, clauses);
34876 break;
34877 default:
34878 cp_parser_error (parser, "expected %<#pragma acc%> clause");
34879 goto saw_error;
34880 }
34881
34882 first = false;
34883
34884 if (((mask >> c_kind) & 1) == 0)
34885 {
34886 /* Remove the invalid clause(s) from the list to avoid
34887 confusing the rest of the compiler. */
34888 clauses = prev;
34889 error_at (here, "%qs is not valid for %qs", c_name, where);
34890 }
34891 }
34892
34893 saw_error:
34894 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34895
34896 if (finish_p)
34897 return finish_omp_clauses (clauses, C_ORT_ACC);
34898
34899 return clauses;
34900 }
34901
34902 /* Parse all OpenMP clauses. The set clauses allowed by the directive
34903 is a bitmask in MASK. Return the list of clauses found; the result
34904 of clause default goes in *pdefault. */
34905
34906 static tree
34907 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
34908 const char *where, cp_token *pragma_tok,
34909 bool finish_p = true)
34910 {
34911 tree clauses = NULL;
34912 bool first = true;
34913 cp_token *token = NULL;
34914
34915 /* Don't create location wrapper nodes within OpenMP clauses. */
34916 auto_suppress_location_wrappers sentinel;
34917
34918 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34919 {
34920 pragma_omp_clause c_kind;
34921 const char *c_name;
34922 tree prev = clauses;
34923
34924 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34925 cp_lexer_consume_token (parser->lexer);
34926
34927 token = cp_lexer_peek_token (parser->lexer);
34928 c_kind = cp_parser_omp_clause_name (parser);
34929
34930 switch (c_kind)
34931 {
34932 case PRAGMA_OMP_CLAUSE_COLLAPSE:
34933 clauses = cp_parser_omp_clause_collapse (parser, clauses,
34934 token->location);
34935 c_name = "collapse";
34936 break;
34937 case PRAGMA_OMP_CLAUSE_COPYIN:
34938 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
34939 c_name = "copyin";
34940 break;
34941 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
34942 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
34943 clauses);
34944 c_name = "copyprivate";
34945 break;
34946 case PRAGMA_OMP_CLAUSE_DEFAULT:
34947 clauses = cp_parser_omp_clause_default (parser, clauses,
34948 token->location, false);
34949 c_name = "default";
34950 break;
34951 case PRAGMA_OMP_CLAUSE_FINAL:
34952 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
34953 c_name = "final";
34954 break;
34955 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
34956 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34957 clauses);
34958 c_name = "firstprivate";
34959 break;
34960 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
34961 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
34962 token->location);
34963 c_name = "grainsize";
34964 break;
34965 case PRAGMA_OMP_CLAUSE_HINT:
34966 clauses = cp_parser_omp_clause_hint (parser, clauses,
34967 token->location);
34968 c_name = "hint";
34969 break;
34970 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
34971 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
34972 token->location);
34973 c_name = "defaultmap";
34974 break;
34975 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
34976 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
34977 clauses);
34978 c_name = "use_device_ptr";
34979 break;
34980 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
34981 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
34982 clauses);
34983 c_name = "is_device_ptr";
34984 break;
34985 case PRAGMA_OMP_CLAUSE_IF:
34986 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
34987 true);
34988 c_name = "if";
34989 break;
34990 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
34991 clauses
34992 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
34993 true, clauses);
34994 c_name = "in_reduction";
34995 break;
34996 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
34997 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
34998 c_name = "lastprivate";
34999 break;
35000 case PRAGMA_OMP_CLAUSE_MERGEABLE:
35001 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
35002 token->location);
35003 c_name = "mergeable";
35004 break;
35005 case PRAGMA_OMP_CLAUSE_NOWAIT:
35006 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
35007 c_name = "nowait";
35008 break;
35009 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
35010 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
35011 token->location);
35012 c_name = "num_tasks";
35013 break;
35014 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
35015 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
35016 token->location);
35017 c_name = "num_threads";
35018 break;
35019 case PRAGMA_OMP_CLAUSE_ORDERED:
35020 clauses = cp_parser_omp_clause_ordered (parser, clauses,
35021 token->location);
35022 c_name = "ordered";
35023 break;
35024 case PRAGMA_OMP_CLAUSE_PRIORITY:
35025 clauses = cp_parser_omp_clause_priority (parser, clauses,
35026 token->location);
35027 c_name = "priority";
35028 break;
35029 case PRAGMA_OMP_CLAUSE_PRIVATE:
35030 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35031 clauses);
35032 c_name = "private";
35033 break;
35034 case PRAGMA_OMP_CLAUSE_REDUCTION:
35035 clauses
35036 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35037 true, clauses);
35038 c_name = "reduction";
35039 break;
35040 case PRAGMA_OMP_CLAUSE_SCHEDULE:
35041 clauses = cp_parser_omp_clause_schedule (parser, clauses,
35042 token->location);
35043 c_name = "schedule";
35044 break;
35045 case PRAGMA_OMP_CLAUSE_SHARED:
35046 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
35047 clauses);
35048 c_name = "shared";
35049 break;
35050 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
35051 clauses
35052 = cp_parser_omp_clause_reduction (parser,
35053 OMP_CLAUSE_TASK_REDUCTION,
35054 true, clauses);
35055 c_name = "task_reduction";
35056 break;
35057 case PRAGMA_OMP_CLAUSE_UNTIED:
35058 clauses = cp_parser_omp_clause_untied (parser, clauses,
35059 token->location);
35060 c_name = "untied";
35061 break;
35062 case PRAGMA_OMP_CLAUSE_INBRANCH:
35063 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
35064 clauses, token->location);
35065 c_name = "inbranch";
35066 break;
35067 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
35068 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
35069 clauses);
35070 c_name = "nontemporal";
35071 break;
35072 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
35073 clauses = cp_parser_omp_clause_branch (parser,
35074 OMP_CLAUSE_NOTINBRANCH,
35075 clauses, token->location);
35076 c_name = "notinbranch";
35077 break;
35078 case PRAGMA_OMP_CLAUSE_PARALLEL:
35079 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
35080 clauses, token->location);
35081 c_name = "parallel";
35082 if (!first)
35083 {
35084 clause_not_first:
35085 error_at (token->location, "%qs must be the first clause of %qs",
35086 c_name, where);
35087 clauses = prev;
35088 }
35089 break;
35090 case PRAGMA_OMP_CLAUSE_FOR:
35091 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
35092 clauses, token->location);
35093 c_name = "for";
35094 if (!first)
35095 goto clause_not_first;
35096 break;
35097 case PRAGMA_OMP_CLAUSE_SECTIONS:
35098 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
35099 clauses, token->location);
35100 c_name = "sections";
35101 if (!first)
35102 goto clause_not_first;
35103 break;
35104 case PRAGMA_OMP_CLAUSE_TASKGROUP:
35105 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
35106 clauses, token->location);
35107 c_name = "taskgroup";
35108 if (!first)
35109 goto clause_not_first;
35110 break;
35111 case PRAGMA_OMP_CLAUSE_LINK:
35112 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
35113 c_name = "to";
35114 break;
35115 case PRAGMA_OMP_CLAUSE_TO:
35116 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
35117 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35118 clauses);
35119 else
35120 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
35121 c_name = "to";
35122 break;
35123 case PRAGMA_OMP_CLAUSE_FROM:
35124 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
35125 c_name = "from";
35126 break;
35127 case PRAGMA_OMP_CLAUSE_UNIFORM:
35128 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
35129 clauses);
35130 c_name = "uniform";
35131 break;
35132 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
35133 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
35134 token->location);
35135 c_name = "num_teams";
35136 break;
35137 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
35138 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
35139 token->location);
35140 c_name = "thread_limit";
35141 break;
35142 case PRAGMA_OMP_CLAUSE_ALIGNED:
35143 clauses = cp_parser_omp_clause_aligned (parser, clauses);
35144 c_name = "aligned";
35145 break;
35146 case PRAGMA_OMP_CLAUSE_LINEAR:
35147 {
35148 bool declare_simd = false;
35149 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
35150 declare_simd = true;
35151 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
35152 }
35153 c_name = "linear";
35154 break;
35155 case PRAGMA_OMP_CLAUSE_DEPEND:
35156 clauses = cp_parser_omp_clause_depend (parser, clauses,
35157 token->location);
35158 c_name = "depend";
35159 break;
35160 case PRAGMA_OMP_CLAUSE_MAP:
35161 clauses = cp_parser_omp_clause_map (parser, clauses);
35162 c_name = "map";
35163 break;
35164 case PRAGMA_OMP_CLAUSE_DEVICE:
35165 clauses = cp_parser_omp_clause_device (parser, clauses,
35166 token->location);
35167 c_name = "device";
35168 break;
35169 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
35170 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
35171 token->location);
35172 c_name = "dist_schedule";
35173 break;
35174 case PRAGMA_OMP_CLAUSE_PROC_BIND:
35175 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
35176 token->location);
35177 c_name = "proc_bind";
35178 break;
35179 case PRAGMA_OMP_CLAUSE_SAFELEN:
35180 clauses = cp_parser_omp_clause_safelen (parser, clauses,
35181 token->location);
35182 c_name = "safelen";
35183 break;
35184 case PRAGMA_OMP_CLAUSE_SIMDLEN:
35185 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35186 token->location);
35187 c_name = "simdlen";
35188 break;
35189 case PRAGMA_OMP_CLAUSE_NOGROUP:
35190 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35191 token->location);
35192 c_name = "nogroup";
35193 break;
35194 case PRAGMA_OMP_CLAUSE_THREADS:
35195 clauses
35196 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35197 clauses, token->location);
35198 c_name = "threads";
35199 break;
35200 case PRAGMA_OMP_CLAUSE_SIMD:
35201 clauses
35202 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35203 clauses, token->location);
35204 c_name = "simd";
35205 break;
35206 default:
35207 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35208 goto saw_error;
35209 }
35210
35211 first = false;
35212
35213 if (((mask >> c_kind) & 1) == 0)
35214 {
35215 /* Remove the invalid clause(s) from the list to avoid
35216 confusing the rest of the compiler. */
35217 clauses = prev;
35218 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35219 }
35220 }
35221 saw_error:
35222 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35223 if (finish_p)
35224 {
35225 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35226 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35227 else
35228 return finish_omp_clauses (clauses, C_ORT_OMP);
35229 }
35230 return clauses;
35231 }
35232
35233 /* OpenMP 2.5:
35234 structured-block:
35235 statement
35236
35237 In practice, we're also interested in adding the statement to an
35238 outer node. So it is convenient if we work around the fact that
35239 cp_parser_statement calls add_stmt. */
35240
35241 static unsigned
35242 cp_parser_begin_omp_structured_block (cp_parser *parser)
35243 {
35244 unsigned save = parser->in_statement;
35245
35246 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35247 This preserves the "not within loop or switch" style error messages
35248 for nonsense cases like
35249 void foo() {
35250 #pragma omp single
35251 break;
35252 }
35253 */
35254 if (parser->in_statement)
35255 parser->in_statement = IN_OMP_BLOCK;
35256
35257 return save;
35258 }
35259
35260 static void
35261 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35262 {
35263 parser->in_statement = save;
35264 }
35265
35266 static tree
35267 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35268 {
35269 tree stmt = begin_omp_structured_block ();
35270 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35271
35272 cp_parser_statement (parser, NULL_TREE, false, if_p);
35273
35274 cp_parser_end_omp_structured_block (parser, save);
35275 return finish_omp_structured_block (stmt);
35276 }
35277
35278 /* OpenMP 2.5:
35279 # pragma omp atomic new-line
35280 expression-stmt
35281
35282 expression-stmt:
35283 x binop= expr | x++ | ++x | x-- | --x
35284 binop:
35285 +, *, -, /, &, ^, |, <<, >>
35286
35287 where x is an lvalue expression with scalar type.
35288
35289 OpenMP 3.1:
35290 # pragma omp atomic new-line
35291 update-stmt
35292
35293 # pragma omp atomic read new-line
35294 read-stmt
35295
35296 # pragma omp atomic write new-line
35297 write-stmt
35298
35299 # pragma omp atomic update new-line
35300 update-stmt
35301
35302 # pragma omp atomic capture new-line
35303 capture-stmt
35304
35305 # pragma omp atomic capture new-line
35306 capture-block
35307
35308 read-stmt:
35309 v = x
35310 write-stmt:
35311 x = expr
35312 update-stmt:
35313 expression-stmt | x = x binop expr
35314 capture-stmt:
35315 v = expression-stmt
35316 capture-block:
35317 { v = x; update-stmt; } | { update-stmt; v = x; }
35318
35319 OpenMP 4.0:
35320 update-stmt:
35321 expression-stmt | x = x binop expr | x = expr binop x
35322 capture-stmt:
35323 v = update-stmt
35324 capture-block:
35325 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35326
35327 where x and v are lvalue expressions with scalar type. */
35328
35329 static void
35330 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35331 {
35332 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35333 tree rhs1 = NULL_TREE, orig_lhs;
35334 location_t loc = pragma_tok->location;
35335 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35336 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35337 bool structured_block = false;
35338 bool first = true;
35339 tree clauses = NULL_TREE;
35340
35341 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35342 {
35343 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35344 cp_lexer_consume_token (parser->lexer);
35345
35346 first = false;
35347
35348 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35349 {
35350 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35351 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35352 const char *p = IDENTIFIER_POINTER (id);
35353 enum tree_code new_code = ERROR_MARK;
35354 enum omp_memory_order new_memory_order
35355 = OMP_MEMORY_ORDER_UNSPECIFIED;
35356
35357 if (!strcmp (p, "read"))
35358 new_code = OMP_ATOMIC_READ;
35359 else if (!strcmp (p, "write"))
35360 new_code = NOP_EXPR;
35361 else if (!strcmp (p, "update"))
35362 new_code = OMP_ATOMIC;
35363 else if (!strcmp (p, "capture"))
35364 new_code = OMP_ATOMIC_CAPTURE_NEW;
35365 else if (!strcmp (p, "seq_cst"))
35366 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35367 else if (!strcmp (p, "acq_rel"))
35368 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35369 else if (!strcmp (p, "release"))
35370 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35371 else if (!strcmp (p, "acquire"))
35372 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35373 else if (!strcmp (p, "relaxed"))
35374 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35375 else if (!strcmp (p, "hint"))
35376 {
35377 cp_lexer_consume_token (parser->lexer);
35378 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35379 continue;
35380 }
35381 else
35382 {
35383 p = NULL;
35384 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35385 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35386 "%<release%>, %<relaxed%> or %<hint%> clause");
35387 }
35388 if (p)
35389 {
35390 if (new_code != ERROR_MARK)
35391 {
35392 if (code != ERROR_MARK)
35393 error_at (cloc, "too many atomic clauses");
35394 else
35395 code = new_code;
35396 }
35397 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35398 {
35399 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35400 error_at (cloc, "too many memory order clauses");
35401 else
35402 memory_order = new_memory_order;
35403 }
35404 cp_lexer_consume_token (parser->lexer);
35405 continue;
35406 }
35407 }
35408 break;
35409 }
35410 cp_parser_require_pragma_eol (parser, pragma_tok);
35411
35412 if (code == ERROR_MARK)
35413 code = OMP_ATOMIC;
35414 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35415 {
35416 omp_requires_mask
35417 = (enum omp_requires) (omp_requires_mask
35418 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35419 switch ((enum omp_memory_order)
35420 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35421 {
35422 case OMP_MEMORY_ORDER_UNSPECIFIED:
35423 case OMP_MEMORY_ORDER_RELAXED:
35424 memory_order = OMP_MEMORY_ORDER_RELAXED;
35425 break;
35426 case OMP_MEMORY_ORDER_SEQ_CST:
35427 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35428 break;
35429 case OMP_MEMORY_ORDER_ACQ_REL:
35430 switch (code)
35431 {
35432 case OMP_ATOMIC_READ:
35433 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35434 break;
35435 case NOP_EXPR: /* atomic write */
35436 case OMP_ATOMIC:
35437 memory_order = OMP_MEMORY_ORDER_RELEASE;
35438 break;
35439 default:
35440 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35441 break;
35442 }
35443 break;
35444 default:
35445 gcc_unreachable ();
35446 }
35447 }
35448 else
35449 switch (code)
35450 {
35451 case OMP_ATOMIC_READ:
35452 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35453 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35454 {
35455 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35456 "%<acq_rel%> or %<release%> clauses");
35457 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35458 }
35459 break;
35460 case NOP_EXPR: /* atomic write */
35461 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35462 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35463 {
35464 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35465 "%<acq_rel%> or %<acquire%> clauses");
35466 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35467 }
35468 break;
35469 case OMP_ATOMIC:
35470 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35471 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35472 {
35473 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35474 "%<acq_rel%> or %<acquire%> clauses");
35475 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35476 }
35477 break;
35478 default:
35479 break;
35480 }
35481
35482 switch (code)
35483 {
35484 case OMP_ATOMIC_READ:
35485 case NOP_EXPR: /* atomic write */
35486 v = cp_parser_unary_expression (parser);
35487 if (v == error_mark_node)
35488 goto saw_error;
35489 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35490 goto saw_error;
35491 if (code == NOP_EXPR)
35492 lhs = cp_parser_expression (parser);
35493 else
35494 lhs = cp_parser_unary_expression (parser);
35495 if (lhs == error_mark_node)
35496 goto saw_error;
35497 if (code == NOP_EXPR)
35498 {
35499 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35500 opcode. */
35501 code = OMP_ATOMIC;
35502 rhs = lhs;
35503 lhs = v;
35504 v = NULL_TREE;
35505 }
35506 goto done;
35507 case OMP_ATOMIC_CAPTURE_NEW:
35508 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35509 {
35510 cp_lexer_consume_token (parser->lexer);
35511 structured_block = true;
35512 }
35513 else
35514 {
35515 v = cp_parser_unary_expression (parser);
35516 if (v == error_mark_node)
35517 goto saw_error;
35518 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35519 goto saw_error;
35520 }
35521 default:
35522 break;
35523 }
35524
35525 restart:
35526 lhs = cp_parser_unary_expression (parser);
35527 orig_lhs = lhs;
35528 switch (TREE_CODE (lhs))
35529 {
35530 case ERROR_MARK:
35531 goto saw_error;
35532
35533 case POSTINCREMENT_EXPR:
35534 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35535 code = OMP_ATOMIC_CAPTURE_OLD;
35536 /* FALLTHROUGH */
35537 case PREINCREMENT_EXPR:
35538 lhs = TREE_OPERAND (lhs, 0);
35539 opcode = PLUS_EXPR;
35540 rhs = integer_one_node;
35541 break;
35542
35543 case POSTDECREMENT_EXPR:
35544 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35545 code = OMP_ATOMIC_CAPTURE_OLD;
35546 /* FALLTHROUGH */
35547 case PREDECREMENT_EXPR:
35548 lhs = TREE_OPERAND (lhs, 0);
35549 opcode = MINUS_EXPR;
35550 rhs = integer_one_node;
35551 break;
35552
35553 case COMPOUND_EXPR:
35554 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35555 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35556 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35557 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35558 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35559 (TREE_OPERAND (lhs, 1), 0), 0)))
35560 == BOOLEAN_TYPE)
35561 /* Undo effects of boolean_increment for post {in,de}crement. */
35562 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35563 /* FALLTHRU */
35564 case MODIFY_EXPR:
35565 if (TREE_CODE (lhs) == MODIFY_EXPR
35566 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35567 {
35568 /* Undo effects of boolean_increment. */
35569 if (integer_onep (TREE_OPERAND (lhs, 1)))
35570 {
35571 /* This is pre or post increment. */
35572 rhs = TREE_OPERAND (lhs, 1);
35573 lhs = TREE_OPERAND (lhs, 0);
35574 opcode = NOP_EXPR;
35575 if (code == OMP_ATOMIC_CAPTURE_NEW
35576 && !structured_block
35577 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35578 code = OMP_ATOMIC_CAPTURE_OLD;
35579 break;
35580 }
35581 }
35582 /* FALLTHRU */
35583 default:
35584 switch (cp_lexer_peek_token (parser->lexer)->type)
35585 {
35586 case CPP_MULT_EQ:
35587 opcode = MULT_EXPR;
35588 break;
35589 case CPP_DIV_EQ:
35590 opcode = TRUNC_DIV_EXPR;
35591 break;
35592 case CPP_PLUS_EQ:
35593 opcode = PLUS_EXPR;
35594 break;
35595 case CPP_MINUS_EQ:
35596 opcode = MINUS_EXPR;
35597 break;
35598 case CPP_LSHIFT_EQ:
35599 opcode = LSHIFT_EXPR;
35600 break;
35601 case CPP_RSHIFT_EQ:
35602 opcode = RSHIFT_EXPR;
35603 break;
35604 case CPP_AND_EQ:
35605 opcode = BIT_AND_EXPR;
35606 break;
35607 case CPP_OR_EQ:
35608 opcode = BIT_IOR_EXPR;
35609 break;
35610 case CPP_XOR_EQ:
35611 opcode = BIT_XOR_EXPR;
35612 break;
35613 case CPP_EQ:
35614 enum cp_parser_prec oprec;
35615 cp_token *token;
35616 cp_lexer_consume_token (parser->lexer);
35617 cp_parser_parse_tentatively (parser);
35618 rhs1 = cp_parser_simple_cast_expression (parser);
35619 if (rhs1 == error_mark_node)
35620 {
35621 cp_parser_abort_tentative_parse (parser);
35622 cp_parser_simple_cast_expression (parser);
35623 goto saw_error;
35624 }
35625 token = cp_lexer_peek_token (parser->lexer);
35626 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35627 {
35628 cp_parser_abort_tentative_parse (parser);
35629 cp_parser_parse_tentatively (parser);
35630 rhs = cp_parser_binary_expression (parser, false, true,
35631 PREC_NOT_OPERATOR, NULL);
35632 if (rhs == error_mark_node)
35633 {
35634 cp_parser_abort_tentative_parse (parser);
35635 cp_parser_binary_expression (parser, false, true,
35636 PREC_NOT_OPERATOR, NULL);
35637 goto saw_error;
35638 }
35639 switch (TREE_CODE (rhs))
35640 {
35641 case MULT_EXPR:
35642 case TRUNC_DIV_EXPR:
35643 case RDIV_EXPR:
35644 case PLUS_EXPR:
35645 case MINUS_EXPR:
35646 case LSHIFT_EXPR:
35647 case RSHIFT_EXPR:
35648 case BIT_AND_EXPR:
35649 case BIT_IOR_EXPR:
35650 case BIT_XOR_EXPR:
35651 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35652 {
35653 if (cp_parser_parse_definitely (parser))
35654 {
35655 opcode = TREE_CODE (rhs);
35656 rhs1 = TREE_OPERAND (rhs, 0);
35657 rhs = TREE_OPERAND (rhs, 1);
35658 goto stmt_done;
35659 }
35660 else
35661 goto saw_error;
35662 }
35663 break;
35664 default:
35665 break;
35666 }
35667 cp_parser_abort_tentative_parse (parser);
35668 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35669 {
35670 rhs = cp_parser_expression (parser);
35671 if (rhs == error_mark_node)
35672 goto saw_error;
35673 opcode = NOP_EXPR;
35674 rhs1 = NULL_TREE;
35675 goto stmt_done;
35676 }
35677 cp_parser_error (parser,
35678 "invalid form of %<#pragma omp atomic%>");
35679 goto saw_error;
35680 }
35681 if (!cp_parser_parse_definitely (parser))
35682 goto saw_error;
35683 switch (token->type)
35684 {
35685 case CPP_SEMICOLON:
35686 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35687 {
35688 code = OMP_ATOMIC_CAPTURE_OLD;
35689 v = lhs;
35690 lhs = NULL_TREE;
35691 lhs1 = rhs1;
35692 rhs1 = NULL_TREE;
35693 cp_lexer_consume_token (parser->lexer);
35694 goto restart;
35695 }
35696 else if (structured_block)
35697 {
35698 opcode = NOP_EXPR;
35699 rhs = rhs1;
35700 rhs1 = NULL_TREE;
35701 goto stmt_done;
35702 }
35703 cp_parser_error (parser,
35704 "invalid form of %<#pragma omp atomic%>");
35705 goto saw_error;
35706 case CPP_MULT:
35707 opcode = MULT_EXPR;
35708 break;
35709 case CPP_DIV:
35710 opcode = TRUNC_DIV_EXPR;
35711 break;
35712 case CPP_PLUS:
35713 opcode = PLUS_EXPR;
35714 break;
35715 case CPP_MINUS:
35716 opcode = MINUS_EXPR;
35717 break;
35718 case CPP_LSHIFT:
35719 opcode = LSHIFT_EXPR;
35720 break;
35721 case CPP_RSHIFT:
35722 opcode = RSHIFT_EXPR;
35723 break;
35724 case CPP_AND:
35725 opcode = BIT_AND_EXPR;
35726 break;
35727 case CPP_OR:
35728 opcode = BIT_IOR_EXPR;
35729 break;
35730 case CPP_XOR:
35731 opcode = BIT_XOR_EXPR;
35732 break;
35733 default:
35734 cp_parser_error (parser,
35735 "invalid operator for %<#pragma omp atomic%>");
35736 goto saw_error;
35737 }
35738 oprec = TOKEN_PRECEDENCE (token);
35739 gcc_assert (oprec != PREC_NOT_OPERATOR);
35740 if (commutative_tree_code (opcode))
35741 oprec = (enum cp_parser_prec) (oprec - 1);
35742 cp_lexer_consume_token (parser->lexer);
35743 rhs = cp_parser_binary_expression (parser, false, false,
35744 oprec, NULL);
35745 if (rhs == error_mark_node)
35746 goto saw_error;
35747 goto stmt_done;
35748 /* FALLTHROUGH */
35749 default:
35750 cp_parser_error (parser,
35751 "invalid operator for %<#pragma omp atomic%>");
35752 goto saw_error;
35753 }
35754 cp_lexer_consume_token (parser->lexer);
35755
35756 rhs = cp_parser_expression (parser);
35757 if (rhs == error_mark_node)
35758 goto saw_error;
35759 break;
35760 }
35761 stmt_done:
35762 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35763 {
35764 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35765 goto saw_error;
35766 v = cp_parser_unary_expression (parser);
35767 if (v == error_mark_node)
35768 goto saw_error;
35769 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35770 goto saw_error;
35771 lhs1 = cp_parser_unary_expression (parser);
35772 if (lhs1 == error_mark_node)
35773 goto saw_error;
35774 }
35775 if (structured_block)
35776 {
35777 cp_parser_consume_semicolon_at_end_of_statement (parser);
35778 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35779 }
35780 done:
35781 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35782 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35783 rhs1, clauses, memory_order);
35784 if (!structured_block)
35785 cp_parser_consume_semicolon_at_end_of_statement (parser);
35786 return;
35787
35788 saw_error:
35789 cp_parser_skip_to_end_of_block_or_statement (parser);
35790 if (structured_block)
35791 {
35792 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35793 cp_lexer_consume_token (parser->lexer);
35794 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35795 {
35796 cp_parser_skip_to_end_of_block_or_statement (parser);
35797 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35798 cp_lexer_consume_token (parser->lexer);
35799 }
35800 }
35801 }
35802
35803
35804 /* OpenMP 2.5:
35805 # pragma omp barrier new-line */
35806
35807 static void
35808 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35809 {
35810 cp_parser_require_pragma_eol (parser, pragma_tok);
35811 finish_omp_barrier ();
35812 }
35813
35814 /* OpenMP 2.5:
35815 # pragma omp critical [(name)] new-line
35816 structured-block
35817
35818 OpenMP 4.5:
35819 # pragma omp critical [(name) [hint(expression)]] new-line
35820 structured-block */
35821
35822 #define OMP_CRITICAL_CLAUSE_MASK \
35823 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35824
35825 static tree
35826 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35827 {
35828 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
35829
35830 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35831 {
35832 matching_parens parens;
35833 parens.consume_open (parser);
35834
35835 name = cp_parser_identifier (parser);
35836
35837 if (name == error_mark_node
35838 || !parens.require_close (parser))
35839 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35840 /*or_comma=*/false,
35841 /*consume_paren=*/true);
35842 if (name == error_mark_node)
35843 name = NULL;
35844
35845 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
35846 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
35847 cp_lexer_consume_token (parser->lexer);
35848
35849 clauses = cp_parser_omp_all_clauses (parser,
35850 OMP_CRITICAL_CLAUSE_MASK,
35851 "#pragma omp critical", pragma_tok);
35852 }
35853 else
35854 cp_parser_require_pragma_eol (parser, pragma_tok);
35855
35856 stmt = cp_parser_omp_structured_block (parser, if_p);
35857 return c_finish_omp_critical (input_location, stmt, name, clauses);
35858 }
35859
35860 /* OpenMP 5.0:
35861 # pragma omp depobj ( depobj ) depobj-clause new-line
35862
35863 depobj-clause:
35864 depend (dependence-type : locator)
35865 destroy
35866 update (dependence-type)
35867
35868 dependence-type:
35869 in
35870 out
35871 inout
35872 mutexinout */
35873
35874 static void
35875 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
35876 {
35877 location_t loc = pragma_tok->location;
35878 matching_parens parens;
35879 if (!parens.require_open (parser))
35880 {
35881 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35882 return;
35883 }
35884
35885 tree depobj = cp_parser_assignment_expression (parser);
35886
35887 if (!parens.require_close (parser))
35888 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35889 /*or_comma=*/false,
35890 /*consume_paren=*/true);
35891
35892 tree clause = NULL_TREE;
35893 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
35894 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
35895 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35896 {
35897 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35898 const char *p = IDENTIFIER_POINTER (id);
35899
35900 cp_lexer_consume_token (parser->lexer);
35901 if (!strcmp ("depend", p))
35902 {
35903 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
35904 if (clause)
35905 clause = finish_omp_clauses (clause, C_ORT_OMP);
35906 if (!clause)
35907 clause = error_mark_node;
35908 }
35909 else if (!strcmp ("destroy", p))
35910 kind = OMP_CLAUSE_DEPEND_LAST;
35911 else if (!strcmp ("update", p))
35912 {
35913 matching_parens c_parens;
35914 if (c_parens.require_open (parser))
35915 {
35916 location_t c2_loc
35917 = cp_lexer_peek_token (parser->lexer)->location;
35918 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35919 {
35920 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
35921 const char *p2 = IDENTIFIER_POINTER (id2);
35922
35923 cp_lexer_consume_token (parser->lexer);
35924 if (!strcmp ("in", p2))
35925 kind = OMP_CLAUSE_DEPEND_IN;
35926 else if (!strcmp ("out", p2))
35927 kind = OMP_CLAUSE_DEPEND_OUT;
35928 else if (!strcmp ("inout", p2))
35929 kind = OMP_CLAUSE_DEPEND_INOUT;
35930 else if (!strcmp ("mutexinoutset", p2))
35931 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
35932 }
35933 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
35934 {
35935 clause = error_mark_node;
35936 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
35937 "%<mutexinoutset%>");
35938 }
35939 if (!c_parens.require_close (parser))
35940 cp_parser_skip_to_closing_parenthesis (parser,
35941 /*recovering=*/true,
35942 /*or_comma=*/false,
35943 /*consume_paren=*/true);
35944 }
35945 else
35946 clause = error_mark_node;
35947 }
35948 }
35949 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
35950 {
35951 clause = error_mark_node;
35952 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
35953 }
35954 cp_parser_require_pragma_eol (parser, pragma_tok);
35955
35956 finish_omp_depobj (loc, depobj, kind, clause);
35957 }
35958
35959
35960 /* OpenMP 2.5:
35961 # pragma omp flush flush-vars[opt] new-line
35962
35963 flush-vars:
35964 ( variable-list )
35965
35966 OpenMP 5.0:
35967 # pragma omp flush memory-order-clause new-line */
35968
35969 static void
35970 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
35971 {
35972 enum memmodel mo = MEMMODEL_LAST;
35973 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35974 {
35975 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35976 const char *p = IDENTIFIER_POINTER (id);
35977 if (!strcmp (p, "acq_rel"))
35978 mo = MEMMODEL_ACQ_REL;
35979 else if (!strcmp (p, "release"))
35980 mo = MEMMODEL_RELEASE;
35981 else if (!strcmp (p, "acquire"))
35982 mo = MEMMODEL_ACQUIRE;
35983 else
35984 error_at (cp_lexer_peek_token (parser->lexer)->location,
35985 "expected %<acq_rel%>, %<release%> or %<acquire%>");
35986 cp_lexer_consume_token (parser->lexer);
35987 }
35988 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35989 {
35990 if (mo != MEMMODEL_LAST)
35991 error_at (cp_lexer_peek_token (parser->lexer)->location,
35992 "%<flush%> list specified together with memory order "
35993 "clause");
35994 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35995 }
35996 cp_parser_require_pragma_eol (parser, pragma_tok);
35997
35998 finish_omp_flush (mo);
35999 }
36000
36001 /* Helper function, to parse omp for increment expression. */
36002
36003 static tree
36004 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
36005 {
36006 tree cond = cp_parser_binary_expression (parser, false, true,
36007 PREC_NOT_OPERATOR, NULL);
36008 if (cond == error_mark_node
36009 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36010 {
36011 cp_parser_skip_to_end_of_statement (parser);
36012 return error_mark_node;
36013 }
36014
36015 switch (TREE_CODE (cond))
36016 {
36017 case GT_EXPR:
36018 case GE_EXPR:
36019 case LT_EXPR:
36020 case LE_EXPR:
36021 break;
36022 case NE_EXPR:
36023 if (code != OACC_LOOP)
36024 break;
36025 gcc_fallthrough ();
36026 default:
36027 return error_mark_node;
36028 }
36029
36030 /* If decl is an iterator, preserve LHS and RHS of the relational
36031 expr until finish_omp_for. */
36032 if (decl
36033 && (type_dependent_expression_p (decl)
36034 || CLASS_TYPE_P (TREE_TYPE (decl))))
36035 return cond;
36036
36037 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
36038 TREE_CODE (cond),
36039 TREE_OPERAND (cond, 0), ERROR_MARK,
36040 TREE_OPERAND (cond, 1), ERROR_MARK,
36041 /*overload=*/NULL, tf_warning_or_error);
36042 }
36043
36044 /* Helper function, to parse omp for increment expression. */
36045
36046 static tree
36047 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
36048 {
36049 cp_token *token = cp_lexer_peek_token (parser->lexer);
36050 enum tree_code op;
36051 tree lhs, rhs;
36052 cp_id_kind idk;
36053 bool decl_first;
36054
36055 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36056 {
36057 op = (token->type == CPP_PLUS_PLUS
36058 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
36059 cp_lexer_consume_token (parser->lexer);
36060 lhs = cp_parser_simple_cast_expression (parser);
36061 if (lhs != decl
36062 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36063 return error_mark_node;
36064 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36065 }
36066
36067 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
36068 if (lhs != decl
36069 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36070 return error_mark_node;
36071
36072 token = cp_lexer_peek_token (parser->lexer);
36073 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36074 {
36075 op = (token->type == CPP_PLUS_PLUS
36076 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
36077 cp_lexer_consume_token (parser->lexer);
36078 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36079 }
36080
36081 op = cp_parser_assignment_operator_opt (parser);
36082 if (op == ERROR_MARK)
36083 return error_mark_node;
36084
36085 if (op != NOP_EXPR)
36086 {
36087 rhs = cp_parser_assignment_expression (parser);
36088 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
36089 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36090 }
36091
36092 lhs = cp_parser_binary_expression (parser, false, false,
36093 PREC_ADDITIVE_EXPRESSION, NULL);
36094 token = cp_lexer_peek_token (parser->lexer);
36095 decl_first = (lhs == decl
36096 || (processing_template_decl && cp_tree_equal (lhs, decl)));
36097 if (decl_first)
36098 lhs = NULL_TREE;
36099 if (token->type != CPP_PLUS
36100 && token->type != CPP_MINUS)
36101 return error_mark_node;
36102
36103 do
36104 {
36105 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
36106 cp_lexer_consume_token (parser->lexer);
36107 rhs = cp_parser_binary_expression (parser, false, false,
36108 PREC_ADDITIVE_EXPRESSION, NULL);
36109 token = cp_lexer_peek_token (parser->lexer);
36110 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
36111 {
36112 if (lhs == NULL_TREE)
36113 {
36114 if (op == PLUS_EXPR)
36115 lhs = rhs;
36116 else
36117 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
36118 tf_warning_or_error);
36119 }
36120 else
36121 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
36122 ERROR_MARK, NULL, tf_warning_or_error);
36123 }
36124 }
36125 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
36126
36127 if (!decl_first)
36128 {
36129 if ((rhs != decl
36130 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
36131 || op == MINUS_EXPR)
36132 return error_mark_node;
36133 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
36134 }
36135 else
36136 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
36137
36138 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36139 }
36140
36141 /* Parse the initialization statement of an OpenMP for loop.
36142
36143 Return true if the resulting construct should have an
36144 OMP_CLAUSE_PRIVATE added to it. */
36145
36146 static tree
36147 cp_parser_omp_for_loop_init (cp_parser *parser,
36148 tree &this_pre_body,
36149 vec<tree, va_gc> *&for_block,
36150 tree &init,
36151 tree &orig_init,
36152 tree &decl,
36153 tree &real_decl)
36154 {
36155 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36156 return NULL_TREE;
36157
36158 tree add_private_clause = NULL_TREE;
36159
36160 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36161
36162 init-expr:
36163 var = lb
36164 integer-type var = lb
36165 random-access-iterator-type var = lb
36166 pointer-type var = lb
36167 */
36168 cp_decl_specifier_seq type_specifiers;
36169
36170 /* First, try to parse as an initialized declaration. See
36171 cp_parser_condition, from whence the bulk of this is copied. */
36172
36173 cp_parser_parse_tentatively (parser);
36174 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
36175 /*is_declaration=*/true,
36176 /*is_trailing_return=*/false,
36177 &type_specifiers);
36178 if (cp_parser_parse_definitely (parser))
36179 {
36180 /* If parsing a type specifier seq succeeded, then this
36181 MUST be a initialized declaration. */
36182 tree asm_specification, attributes;
36183 cp_declarator *declarator;
36184
36185 declarator = cp_parser_declarator (parser,
36186 CP_PARSER_DECLARATOR_NAMED,
36187 CP_PARSER_FLAGS_NONE,
36188 /*ctor_dtor_or_conv_p=*/NULL,
36189 /*parenthesized_p=*/NULL,
36190 /*member_p=*/false,
36191 /*friend_p=*/false);
36192 attributes = cp_parser_attributes_opt (parser);
36193 asm_specification = cp_parser_asm_specification_opt (parser);
36194
36195 if (declarator == cp_error_declarator)
36196 cp_parser_skip_to_end_of_statement (parser);
36197
36198 else
36199 {
36200 tree pushed_scope, auto_node;
36201
36202 decl = start_decl (declarator, &type_specifiers,
36203 SD_INITIALIZED, attributes,
36204 /*prefix_attributes=*/NULL_TREE,
36205 &pushed_scope);
36206
36207 auto_node = type_uses_auto (TREE_TYPE (decl));
36208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36209 {
36210 if (cp_lexer_next_token_is (parser->lexer,
36211 CPP_OPEN_PAREN))
36212 error ("parenthesized initialization is not allowed in "
36213 "OpenMP %<for%> loop");
36214 else
36215 /* Trigger an error. */
36216 cp_parser_require (parser, CPP_EQ, RT_EQ);
36217
36218 init = error_mark_node;
36219 cp_parser_skip_to_end_of_statement (parser);
36220 }
36221 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36222 || type_dependent_expression_p (decl)
36223 || auto_node)
36224 {
36225 bool is_direct_init, is_non_constant_init;
36226
36227 init = cp_parser_initializer (parser,
36228 &is_direct_init,
36229 &is_non_constant_init);
36230
36231 if (auto_node)
36232 {
36233 TREE_TYPE (decl)
36234 = do_auto_deduction (TREE_TYPE (decl), init,
36235 auto_node);
36236
36237 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36238 && !type_dependent_expression_p (decl))
36239 goto non_class;
36240 }
36241
36242 cp_finish_decl (decl, init, !is_non_constant_init,
36243 asm_specification,
36244 LOOKUP_ONLYCONVERTING);
36245 orig_init = init;
36246 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36247 {
36248 vec_safe_push (for_block, this_pre_body);
36249 init = NULL_TREE;
36250 }
36251 else
36252 {
36253 init = pop_stmt_list (this_pre_body);
36254 if (init && TREE_CODE (init) == STATEMENT_LIST)
36255 {
36256 tree_stmt_iterator i = tsi_start (init);
36257 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36258 while (!tsi_end_p (i))
36259 {
36260 tree t = tsi_stmt (i);
36261 if (TREE_CODE (t) == DECL_EXPR
36262 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36263 {
36264 tsi_delink (&i);
36265 vec_safe_push (for_block, t);
36266 continue;
36267 }
36268 break;
36269 }
36270 if (tsi_one_before_end_p (i))
36271 {
36272 tree t = tsi_stmt (i);
36273 tsi_delink (&i);
36274 free_stmt_list (init);
36275 init = t;
36276 }
36277 }
36278 }
36279 this_pre_body = NULL_TREE;
36280 }
36281 else
36282 {
36283 /* Consume '='. */
36284 cp_lexer_consume_token (parser->lexer);
36285 init = cp_parser_assignment_expression (parser);
36286
36287 non_class:
36288 if (TYPE_REF_P (TREE_TYPE (decl)))
36289 init = error_mark_node;
36290 else
36291 cp_finish_decl (decl, NULL_TREE,
36292 /*init_const_expr_p=*/false,
36293 asm_specification,
36294 LOOKUP_ONLYCONVERTING);
36295 }
36296
36297 if (pushed_scope)
36298 pop_scope (pushed_scope);
36299 }
36300 }
36301 else
36302 {
36303 cp_id_kind idk;
36304 /* If parsing a type specifier sequence failed, then
36305 this MUST be a simple expression. */
36306 cp_parser_parse_tentatively (parser);
36307 decl = cp_parser_primary_expression (parser, false, false,
36308 false, &idk);
36309 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36310 if (!cp_parser_error_occurred (parser)
36311 && decl
36312 && (TREE_CODE (decl) == COMPONENT_REF
36313 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36314 {
36315 cp_parser_abort_tentative_parse (parser);
36316 cp_parser_parse_tentatively (parser);
36317 cp_token *token = cp_lexer_peek_token (parser->lexer);
36318 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36319 /*check_dependency_p=*/true,
36320 /*template_p=*/NULL,
36321 /*declarator_p=*/false,
36322 /*optional_p=*/false);
36323 if (name != error_mark_node
36324 && last_tok == cp_lexer_peek_token (parser->lexer))
36325 {
36326 decl = cp_parser_lookup_name_simple (parser, name,
36327 token->location);
36328 if (TREE_CODE (decl) == FIELD_DECL)
36329 add_private_clause = omp_privatize_field (decl, false);
36330 }
36331 cp_parser_abort_tentative_parse (parser);
36332 cp_parser_parse_tentatively (parser);
36333 decl = cp_parser_primary_expression (parser, false, false,
36334 false, &idk);
36335 }
36336 if (!cp_parser_error_occurred (parser)
36337 && decl
36338 && DECL_P (decl)
36339 && CLASS_TYPE_P (TREE_TYPE (decl)))
36340 {
36341 tree rhs;
36342
36343 cp_parser_parse_definitely (parser);
36344 cp_parser_require (parser, CPP_EQ, RT_EQ);
36345 rhs = cp_parser_assignment_expression (parser);
36346 orig_init = rhs;
36347 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36348 decl, NOP_EXPR,
36349 rhs,
36350 tf_warning_or_error));
36351 if (!add_private_clause)
36352 add_private_clause = decl;
36353 }
36354 else
36355 {
36356 decl = NULL;
36357 cp_parser_abort_tentative_parse (parser);
36358 init = cp_parser_expression (parser);
36359 if (init)
36360 {
36361 if (TREE_CODE (init) == MODIFY_EXPR
36362 || TREE_CODE (init) == MODOP_EXPR)
36363 real_decl = TREE_OPERAND (init, 0);
36364 }
36365 }
36366 }
36367 return add_private_clause;
36368 }
36369
36370 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36371
36372 void
36373 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36374 tree &decl, tree &orig_decl, tree &init,
36375 tree &orig_init, tree &cond, tree &incr)
36376 {
36377 tree begin, end, range_temp_decl = NULL_TREE;
36378 tree iter_type, begin_expr, end_expr;
36379
36380 if (processing_template_decl)
36381 {
36382 if (check_for_bare_parameter_packs (init))
36383 init = error_mark_node;
36384 if (!type_dependent_expression_p (init)
36385 /* do_auto_deduction doesn't mess with template init-lists. */
36386 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36387 {
36388 tree d = decl;
36389 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36390 {
36391 tree v = DECL_VALUE_EXPR (decl);
36392 if (TREE_CODE (v) == ARRAY_REF
36393 && VAR_P (TREE_OPERAND (v, 0))
36394 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36395 d = TREE_OPERAND (v, 0);
36396 }
36397 do_range_for_auto_deduction (d, init);
36398 }
36399 cond = global_namespace;
36400 incr = NULL_TREE;
36401 orig_init = init;
36402 if (this_pre_body)
36403 this_pre_body = pop_stmt_list (this_pre_body);
36404 return;
36405 }
36406
36407 init = mark_lvalue_use (init);
36408
36409 if (decl == error_mark_node || init == error_mark_node)
36410 /* If an error happened previously do nothing or else a lot of
36411 unhelpful errors would be issued. */
36412 begin_expr = end_expr = iter_type = error_mark_node;
36413 else
36414 {
36415 tree range_temp;
36416
36417 if (VAR_P (init)
36418 && array_of_runtime_bound_p (TREE_TYPE (init)))
36419 /* Can't bind a reference to an array of runtime bound. */
36420 range_temp = init;
36421 else
36422 {
36423 range_temp = build_range_temp (init);
36424 DECL_NAME (range_temp) = NULL_TREE;
36425 pushdecl (range_temp);
36426 cp_finish_decl (range_temp, init,
36427 /*is_constant_init*/false, NULL_TREE,
36428 LOOKUP_ONLYCONVERTING);
36429 range_temp_decl = range_temp;
36430 range_temp = convert_from_reference (range_temp);
36431 }
36432 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36433 &begin_expr, &end_expr);
36434 }
36435
36436 tree end_iter_type = iter_type;
36437 if (cxx_dialect >= cxx17)
36438 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36439 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36440 TREE_USED (end) = 1;
36441 DECL_ARTIFICIAL (end) = 1;
36442 pushdecl (end);
36443 cp_finish_decl (end, end_expr,
36444 /*is_constant_init*/false, NULL_TREE,
36445 LOOKUP_ONLYCONVERTING);
36446
36447 /* The new for initialization statement. */
36448 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36449 TREE_USED (begin) = 1;
36450 DECL_ARTIFICIAL (begin) = 1;
36451 pushdecl (begin);
36452 orig_init = init;
36453 if (CLASS_TYPE_P (iter_type))
36454 init = NULL_TREE;
36455 else
36456 {
36457 init = begin_expr;
36458 begin_expr = NULL_TREE;
36459 }
36460 cp_finish_decl (begin, begin_expr,
36461 /*is_constant_init*/false, NULL_TREE,
36462 LOOKUP_ONLYCONVERTING);
36463
36464 /* The new for condition. */
36465 if (CLASS_TYPE_P (iter_type))
36466 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36467 else
36468 cond = build_x_binary_op (input_location, NE_EXPR,
36469 begin, ERROR_MARK,
36470 end, ERROR_MARK,
36471 NULL, tf_warning_or_error);
36472
36473 /* The new increment expression. */
36474 if (CLASS_TYPE_P (iter_type))
36475 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36476 else
36477 incr = finish_unary_op_expr (input_location,
36478 PREINCREMENT_EXPR, begin,
36479 tf_warning_or_error);
36480
36481 orig_decl = decl;
36482 decl = begin;
36483 if (for_block)
36484 {
36485 vec_safe_push (for_block, this_pre_body);
36486 this_pre_body = NULL_TREE;
36487 }
36488
36489 tree decomp_first_name = NULL_TREE;
36490 unsigned decomp_cnt = 0;
36491 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36492 {
36493 tree v = DECL_VALUE_EXPR (orig_decl);
36494 if (TREE_CODE (v) == ARRAY_REF
36495 && VAR_P (TREE_OPERAND (v, 0))
36496 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36497 {
36498 tree d = orig_decl;
36499 orig_decl = TREE_OPERAND (v, 0);
36500 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36501 decomp_first_name = d;
36502 }
36503 }
36504
36505 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36506 if (auto_node)
36507 {
36508 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36509 tf_none);
36510 if (!error_operand_p (t))
36511 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36512 t, auto_node);
36513 }
36514
36515 tree v = make_tree_vec (decomp_cnt + 3);
36516 TREE_VEC_ELT (v, 0) = range_temp_decl;
36517 TREE_VEC_ELT (v, 1) = end;
36518 TREE_VEC_ELT (v, 2) = orig_decl;
36519 for (unsigned i = 0; i < decomp_cnt; i++)
36520 {
36521 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36522 decomp_first_name = DECL_CHAIN (decomp_first_name);
36523 }
36524 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36525 }
36526
36527 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36528 inside of the collapsed body. */
36529
36530 void
36531 cp_finish_omp_range_for (tree orig, tree begin)
36532 {
36533 gcc_assert (TREE_CODE (orig) == TREE_LIST
36534 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36535 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36536 tree decomp_first_name = NULL_TREE;
36537 unsigned int decomp_cnt = 0;
36538
36539 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36540 {
36541 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36542 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36543 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36544 }
36545
36546 /* The declaration is initialized with *__begin inside the loop body. */
36547 cp_finish_decl (decl,
36548 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36549 tf_warning_or_error),
36550 /*is_constant_init*/false, NULL_TREE,
36551 LOOKUP_ONLYCONVERTING);
36552 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36553 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36554 }
36555
36556 /* Parse the restricted form of the for statement allowed by OpenMP. */
36557
36558 static tree
36559 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36560 tree *cclauses, bool *if_p)
36561 {
36562 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36563 tree orig_decl;
36564 tree real_decl, initv, condv, incrv, declv, orig_declv;
36565 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36566 location_t loc_first;
36567 bool collapse_err = false;
36568 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36569 vec<tree, va_gc> *for_block = make_tree_vector ();
36570 auto_vec<tree, 4> orig_inits;
36571 bool tiling = false;
36572
36573 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36574 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36575 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36576 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36577 {
36578 tiling = true;
36579 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36580 }
36581 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36582 && OMP_CLAUSE_ORDERED_EXPR (cl))
36583 {
36584 ordered_cl = cl;
36585 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36586 }
36587
36588 if (ordered && ordered < collapse)
36589 {
36590 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36591 "%<ordered%> clause parameter is less than %<collapse%>");
36592 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36593 = build_int_cst (NULL_TREE, collapse);
36594 ordered = collapse;
36595 }
36596 if (ordered)
36597 {
36598 for (tree *pc = &clauses; *pc; )
36599 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36600 {
36601 error_at (OMP_CLAUSE_LOCATION (*pc),
36602 "%<linear%> clause may not be specified together "
36603 "with %<ordered%> clause with a parameter");
36604 *pc = OMP_CLAUSE_CHAIN (*pc);
36605 }
36606 else
36607 pc = &OMP_CLAUSE_CHAIN (*pc);
36608 }
36609
36610 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36611 count = ordered ? ordered : collapse;
36612
36613 declv = make_tree_vec (count);
36614 initv = make_tree_vec (count);
36615 condv = make_tree_vec (count);
36616 incrv = make_tree_vec (count);
36617 orig_declv = NULL_TREE;
36618
36619 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36620
36621 for (i = 0; i < count; i++)
36622 {
36623 int bracecount = 0;
36624 tree add_private_clause = NULL_TREE;
36625 location_t loc;
36626
36627 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36628 {
36629 if (!collapse_err)
36630 cp_parser_error (parser, "for statement expected");
36631 return NULL;
36632 }
36633 loc = cp_lexer_consume_token (parser->lexer)->location;
36634
36635 /* Don't create location wrapper nodes within an OpenMP "for"
36636 statement. */
36637 auto_suppress_location_wrappers sentinel;
36638
36639 matching_parens parens;
36640 if (!parens.require_open (parser))
36641 return NULL;
36642
36643 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36644 this_pre_body = push_stmt_list ();
36645
36646 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36647 {
36648 /* Save tokens so that we can put them back. */
36649 cp_lexer_save_tokens (parser->lexer);
36650
36651 /* Look for ':' that is not nested in () or {}. */
36652 bool is_range_for
36653 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36654 /*recovering=*/false,
36655 CPP_COLON,
36656 /*consume_paren=*/
36657 false) == -1);
36658
36659 /* Roll back the tokens we skipped. */
36660 cp_lexer_rollback_tokens (parser->lexer);
36661
36662 if (is_range_for)
36663 {
36664 bool saved_colon_corrects_to_scope_p
36665 = parser->colon_corrects_to_scope_p;
36666
36667 /* A colon is used in range-based for. */
36668 parser->colon_corrects_to_scope_p = false;
36669
36670 /* Parse the declaration. */
36671 cp_parser_simple_declaration (parser,
36672 /*function_definition_allowed_p=*/
36673 false, &decl);
36674 parser->colon_corrects_to_scope_p
36675 = saved_colon_corrects_to_scope_p;
36676
36677 cp_parser_require (parser, CPP_COLON, RT_COLON);
36678
36679 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36680 false, 0, true);
36681
36682 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36683 orig_decl, init, orig_init,
36684 cond, incr);
36685 if (this_pre_body)
36686 {
36687 if (pre_body)
36688 {
36689 tree t = pre_body;
36690 pre_body = push_stmt_list ();
36691 add_stmt (t);
36692 add_stmt (this_pre_body);
36693 pre_body = pop_stmt_list (pre_body);
36694 }
36695 else
36696 pre_body = this_pre_body;
36697 }
36698
36699 if (ordered_cl)
36700 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36701 "%<ordered%> clause with parameter on "
36702 "range-based %<for%> loop");
36703
36704 goto parse_close_paren;
36705 }
36706 }
36707
36708 add_private_clause
36709 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36710 init, orig_init, decl, real_decl);
36711
36712 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36713 if (this_pre_body)
36714 {
36715 this_pre_body = pop_stmt_list (this_pre_body);
36716 if (pre_body)
36717 {
36718 tree t = pre_body;
36719 pre_body = push_stmt_list ();
36720 add_stmt (t);
36721 add_stmt (this_pre_body);
36722 pre_body = pop_stmt_list (pre_body);
36723 }
36724 else
36725 pre_body = this_pre_body;
36726 }
36727
36728 if (decl)
36729 real_decl = decl;
36730 if (cclauses != NULL
36731 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36732 && real_decl != NULL_TREE)
36733 {
36734 tree *c;
36735 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36736 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36737 && OMP_CLAUSE_DECL (*c) == real_decl)
36738 {
36739 error_at (loc, "iteration variable %qD"
36740 " should not be firstprivate", real_decl);
36741 *c = OMP_CLAUSE_CHAIN (*c);
36742 }
36743 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36744 && OMP_CLAUSE_DECL (*c) == real_decl)
36745 {
36746 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36747 tree l = *c;
36748 *c = OMP_CLAUSE_CHAIN (*c);
36749 if (code == OMP_SIMD)
36750 {
36751 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36752 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36753 }
36754 else
36755 {
36756 OMP_CLAUSE_CHAIN (l) = clauses;
36757 clauses = l;
36758 }
36759 add_private_clause = NULL_TREE;
36760 }
36761 else
36762 {
36763 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36764 && OMP_CLAUSE_DECL (*c) == real_decl)
36765 add_private_clause = NULL_TREE;
36766 c = &OMP_CLAUSE_CHAIN (*c);
36767 }
36768 }
36769
36770 if (add_private_clause)
36771 {
36772 tree c;
36773 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36774 {
36775 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36776 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36777 && OMP_CLAUSE_DECL (c) == decl)
36778 break;
36779 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36780 && OMP_CLAUSE_DECL (c) == decl)
36781 error_at (loc, "iteration variable %qD "
36782 "should not be firstprivate",
36783 decl);
36784 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36785 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36786 && OMP_CLAUSE_DECL (c) == decl)
36787 error_at (loc, "iteration variable %qD should not be reduction",
36788 decl);
36789 }
36790 if (c == NULL)
36791 {
36792 if (code != OMP_SIMD)
36793 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36794 else if (collapse == 1)
36795 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36796 else
36797 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36798 OMP_CLAUSE_DECL (c) = add_private_clause;
36799 c = finish_omp_clauses (c, C_ORT_OMP);
36800 if (c)
36801 {
36802 OMP_CLAUSE_CHAIN (c) = clauses;
36803 clauses = c;
36804 /* For linear, signal that we need to fill up
36805 the so far unknown linear step. */
36806 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36807 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36808 }
36809 }
36810 }
36811
36812 cond = NULL;
36813 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36814 cond = cp_parser_omp_for_cond (parser, decl, code);
36815 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36816
36817 incr = NULL;
36818 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36819 {
36820 /* If decl is an iterator, preserve the operator on decl
36821 until finish_omp_for. */
36822 if (real_decl
36823 && ((processing_template_decl
36824 && (TREE_TYPE (real_decl) == NULL_TREE
36825 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
36826 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
36827 incr = cp_parser_omp_for_incr (parser, real_decl);
36828 else
36829 incr = cp_parser_expression (parser);
36830 if (!EXPR_HAS_LOCATION (incr))
36831 protected_set_expr_location (incr, input_location);
36832 }
36833
36834 parse_close_paren:
36835 if (!parens.require_close (parser))
36836 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36837 /*or_comma=*/false,
36838 /*consume_paren=*/true);
36839
36840 TREE_VEC_ELT (declv, i) = decl;
36841 TREE_VEC_ELT (initv, i) = init;
36842 TREE_VEC_ELT (condv, i) = cond;
36843 TREE_VEC_ELT (incrv, i) = incr;
36844 if (orig_init)
36845 {
36846 orig_inits.safe_grow_cleared (i + 1);
36847 orig_inits[i] = orig_init;
36848 }
36849 if (orig_decl)
36850 {
36851 if (!orig_declv)
36852 orig_declv = copy_node (declv);
36853 TREE_VEC_ELT (orig_declv, i) = orig_decl;
36854 }
36855 else if (orig_declv)
36856 TREE_VEC_ELT (orig_declv, i) = decl;
36857
36858 if (i == count - 1)
36859 break;
36860
36861 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
36862 in between the collapsed for loops to be still considered perfectly
36863 nested. Hopefully the final version clarifies this.
36864 For now handle (multiple) {'s and empty statements. */
36865 cp_parser_parse_tentatively (parser);
36866 for (;;)
36867 {
36868 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36869 break;
36870 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
36871 {
36872 cp_lexer_consume_token (parser->lexer);
36873 bracecount++;
36874 }
36875 else if (bracecount
36876 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36877 cp_lexer_consume_token (parser->lexer);
36878 else
36879 {
36880 loc = cp_lexer_peek_token (parser->lexer)->location;
36881 error_at (loc, "not enough for loops to collapse");
36882 collapse_err = true;
36883 cp_parser_abort_tentative_parse (parser);
36884 declv = NULL_TREE;
36885 break;
36886 }
36887 }
36888
36889 if (declv)
36890 {
36891 cp_parser_parse_definitely (parser);
36892 nbraces += bracecount;
36893 }
36894 }
36895
36896 if (nbraces)
36897 if_p = NULL;
36898
36899 /* Note that we saved the original contents of this flag when we entered
36900 the structured block, and so we don't need to re-save it here. */
36901 parser->in_statement = IN_OMP_FOR;
36902
36903 /* Note that the grammar doesn't call for a structured block here,
36904 though the loop as a whole is a structured block. */
36905 if (orig_declv)
36906 {
36907 body = begin_omp_structured_block ();
36908 for (i = 0; i < count; i++)
36909 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
36910 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
36911 TREE_VEC_ELT (declv, i));
36912 }
36913 else
36914 body = push_stmt_list ();
36915 cp_parser_statement (parser, NULL_TREE, false, if_p);
36916 if (orig_declv)
36917 body = finish_omp_structured_block (body);
36918 else
36919 body = pop_stmt_list (body);
36920
36921 if (declv == NULL_TREE)
36922 ret = NULL_TREE;
36923 else
36924 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
36925 incrv, body, pre_body, &orig_inits, clauses);
36926
36927 while (nbraces)
36928 {
36929 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
36930 {
36931 cp_lexer_consume_token (parser->lexer);
36932 nbraces--;
36933 }
36934 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36935 cp_lexer_consume_token (parser->lexer);
36936 else
36937 {
36938 if (!collapse_err)
36939 {
36940 error_at (cp_lexer_peek_token (parser->lexer)->location,
36941 "collapsed loops not perfectly nested");
36942 }
36943 collapse_err = true;
36944 cp_parser_statement_seq_opt (parser, NULL);
36945 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
36946 break;
36947 }
36948 }
36949
36950 while (!for_block->is_empty ())
36951 {
36952 tree t = for_block->pop ();
36953 if (TREE_CODE (t) == STATEMENT_LIST)
36954 add_stmt (pop_stmt_list (t));
36955 else
36956 add_stmt (t);
36957 }
36958 release_tree_vector (for_block);
36959
36960 return ret;
36961 }
36962
36963 /* Helper function for OpenMP parsing, split clauses and call
36964 finish_omp_clauses on each of the set of clauses afterwards. */
36965
36966 static void
36967 cp_omp_split_clauses (location_t loc, enum tree_code code,
36968 omp_clause_mask mask, tree clauses, tree *cclauses)
36969 {
36970 int i;
36971 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
36972 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
36973 if (cclauses[i])
36974 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
36975 }
36976
36977 /* OpenMP 4.0:
36978 #pragma omp simd simd-clause[optseq] new-line
36979 for-loop */
36980
36981 #define OMP_SIMD_CLAUSE_MASK \
36982 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
36983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
36992
36993 static tree
36994 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
36995 char *p_name, omp_clause_mask mask, tree *cclauses,
36996 bool *if_p)
36997 {
36998 tree clauses, sb, ret;
36999 unsigned int save;
37000 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37001
37002 strcat (p_name, " simd");
37003 mask |= OMP_SIMD_CLAUSE_MASK;
37004
37005 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37006 cclauses == NULL);
37007 if (cclauses)
37008 {
37009 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
37010 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
37011 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
37012 OMP_CLAUSE_ORDERED);
37013 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
37014 {
37015 error_at (OMP_CLAUSE_LOCATION (c),
37016 "%<ordered%> clause with parameter may not be specified "
37017 "on %qs construct", p_name);
37018 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
37019 }
37020 }
37021
37022 keep_next_level (true);
37023 sb = begin_omp_structured_block ();
37024 save = cp_parser_begin_omp_structured_block (parser);
37025
37026 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
37027
37028 cp_parser_end_omp_structured_block (parser, save);
37029 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37030
37031 return ret;
37032 }
37033
37034 /* OpenMP 2.5:
37035 #pragma omp for for-clause[optseq] new-line
37036 for-loop
37037
37038 OpenMP 4.0:
37039 #pragma omp for simd for-simd-clause[optseq] new-line
37040 for-loop */
37041
37042 #define OMP_FOR_CLAUSE_MASK \
37043 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37052
37053 static tree
37054 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
37055 char *p_name, omp_clause_mask mask, tree *cclauses,
37056 bool *if_p)
37057 {
37058 tree clauses, sb, ret;
37059 unsigned int save;
37060 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37061
37062 strcat (p_name, " for");
37063 mask |= OMP_FOR_CLAUSE_MASK;
37064 /* parallel for{, simd} disallows nowait clause, but for
37065 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37066 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
37067 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37068 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37069 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37070 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
37071
37072 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37073 {
37074 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37075 const char *p = IDENTIFIER_POINTER (id);
37076
37077 if (strcmp (p, "simd") == 0)
37078 {
37079 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37080 if (cclauses == NULL)
37081 cclauses = cclauses_buf;
37082
37083 cp_lexer_consume_token (parser->lexer);
37084 if (!flag_openmp) /* flag_openmp_simd */
37085 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37086 cclauses, if_p);
37087 sb = begin_omp_structured_block ();
37088 save = cp_parser_begin_omp_structured_block (parser);
37089 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37090 cclauses, if_p);
37091 cp_parser_end_omp_structured_block (parser, save);
37092 tree body = finish_omp_structured_block (sb);
37093 if (ret == NULL)
37094 return ret;
37095 ret = make_node (OMP_FOR);
37096 TREE_TYPE (ret) = void_type_node;
37097 OMP_FOR_BODY (ret) = body;
37098 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37099 SET_EXPR_LOCATION (ret, loc);
37100 add_stmt (ret);
37101 return ret;
37102 }
37103 }
37104 if (!flag_openmp) /* flag_openmp_simd */
37105 {
37106 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37107 return NULL_TREE;
37108 }
37109
37110 /* Composite distribute parallel for disallows linear clause. */
37111 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37112 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
37113
37114 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37115 cclauses == NULL);
37116 if (cclauses)
37117 {
37118 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
37119 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37120 }
37121
37122 keep_next_level (true);
37123 sb = begin_omp_structured_block ();
37124 save = cp_parser_begin_omp_structured_block (parser);
37125
37126 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
37127
37128 cp_parser_end_omp_structured_block (parser, save);
37129 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37130
37131 return ret;
37132 }
37133
37134 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
37135 omp_clause_mask, tree *, bool *);
37136
37137 /* OpenMP 2.5:
37138 # pragma omp master new-line
37139 structured-block */
37140
37141 static tree
37142 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
37143 char *p_name, omp_clause_mask mask, tree *cclauses,
37144 bool *if_p)
37145 {
37146 tree clauses, sb, ret;
37147 unsigned int save;
37148 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37149
37150 strcat (p_name, " master");
37151
37152 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37153 {
37154 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37155 const char *p = IDENTIFIER_POINTER (id);
37156
37157 if (strcmp (p, "taskloop") == 0)
37158 {
37159 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37160 if (cclauses == NULL)
37161 cclauses = cclauses_buf;
37162
37163 cp_lexer_consume_token (parser->lexer);
37164 if (!flag_openmp) /* flag_openmp_simd */
37165 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37166 cclauses, if_p);
37167 sb = begin_omp_structured_block ();
37168 save = cp_parser_begin_omp_structured_block (parser);
37169 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37170 cclauses, if_p);
37171 cp_parser_end_omp_structured_block (parser, save);
37172 tree body = finish_omp_structured_block (sb);
37173 if (ret == NULL)
37174 return ret;
37175 return c_finish_omp_master (loc, body);
37176 }
37177 }
37178 if (!flag_openmp) /* flag_openmp_simd */
37179 {
37180 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37181 return NULL_TREE;
37182 }
37183
37184 if (cclauses)
37185 {
37186 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37187 false);
37188 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
37189 }
37190 else
37191 cp_parser_require_pragma_eol (parser, pragma_tok);
37192
37193 return c_finish_omp_master (loc,
37194 cp_parser_omp_structured_block (parser, if_p));
37195 }
37196
37197 /* OpenMP 2.5:
37198 # pragma omp ordered new-line
37199 structured-block
37200
37201 OpenMP 4.5:
37202 # pragma omp ordered ordered-clauses new-line
37203 structured-block */
37204
37205 #define OMP_ORDERED_CLAUSE_MASK \
37206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37208
37209 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37210 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37211
37212 static bool
37213 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37214 enum pragma_context context, bool *if_p)
37215 {
37216 location_t loc = pragma_tok->location;
37217
37218 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37219 {
37220 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37221 const char *p = IDENTIFIER_POINTER (id);
37222
37223 if (strcmp (p, "depend") == 0)
37224 {
37225 if (!flag_openmp) /* flag_openmp_simd */
37226 {
37227 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37228 return false;
37229 }
37230 if (context == pragma_stmt)
37231 {
37232 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37233 "%<depend%> clause may only be used in compound "
37234 "statements");
37235 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37236 return false;
37237 }
37238 tree clauses
37239 = cp_parser_omp_all_clauses (parser,
37240 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37241 "#pragma omp ordered", pragma_tok);
37242 c_finish_omp_ordered (loc, clauses, NULL_TREE);
37243 return false;
37244 }
37245 }
37246
37247 tree clauses
37248 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37249 "#pragma omp ordered", pragma_tok);
37250
37251 if (!flag_openmp /* flag_openmp_simd */
37252 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37253 return false;
37254
37255 c_finish_omp_ordered (loc, clauses,
37256 cp_parser_omp_structured_block (parser, if_p));
37257 return true;
37258 }
37259
37260 /* OpenMP 2.5:
37261
37262 section-scope:
37263 { section-sequence }
37264
37265 section-sequence:
37266 section-directive[opt] structured-block
37267 section-sequence section-directive structured-block */
37268
37269 static tree
37270 cp_parser_omp_sections_scope (cp_parser *parser)
37271 {
37272 tree stmt, substmt;
37273 bool error_suppress = false;
37274 cp_token *tok;
37275
37276 matching_braces braces;
37277 if (!braces.require_open (parser))
37278 return NULL_TREE;
37279
37280 stmt = push_stmt_list ();
37281
37282 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37283 != PRAGMA_OMP_SECTION)
37284 {
37285 substmt = cp_parser_omp_structured_block (parser, NULL);
37286 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37287 add_stmt (substmt);
37288 }
37289
37290 while (1)
37291 {
37292 tok = cp_lexer_peek_token (parser->lexer);
37293 if (tok->type == CPP_CLOSE_BRACE)
37294 break;
37295 if (tok->type == CPP_EOF)
37296 break;
37297
37298 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37299 {
37300 cp_lexer_consume_token (parser->lexer);
37301 cp_parser_require_pragma_eol (parser, tok);
37302 error_suppress = false;
37303 }
37304 else if (!error_suppress)
37305 {
37306 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37307 error_suppress = true;
37308 }
37309
37310 substmt = cp_parser_omp_structured_block (parser, NULL);
37311 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37312 add_stmt (substmt);
37313 }
37314 braces.require_close (parser);
37315
37316 substmt = pop_stmt_list (stmt);
37317
37318 stmt = make_node (OMP_SECTIONS);
37319 TREE_TYPE (stmt) = void_type_node;
37320 OMP_SECTIONS_BODY (stmt) = substmt;
37321
37322 add_stmt (stmt);
37323 return stmt;
37324 }
37325
37326 /* OpenMP 2.5:
37327 # pragma omp sections sections-clause[optseq] newline
37328 sections-scope */
37329
37330 #define OMP_SECTIONS_CLAUSE_MASK \
37331 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37336
37337 static tree
37338 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37339 char *p_name, omp_clause_mask mask, tree *cclauses)
37340 {
37341 tree clauses, ret;
37342 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37343
37344 strcat (p_name, " sections");
37345 mask |= OMP_SECTIONS_CLAUSE_MASK;
37346 if (cclauses)
37347 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37348
37349 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37350 cclauses == NULL);
37351 if (cclauses)
37352 {
37353 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37354 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37355 }
37356
37357 ret = cp_parser_omp_sections_scope (parser);
37358 if (ret)
37359 OMP_SECTIONS_CLAUSES (ret) = clauses;
37360
37361 return ret;
37362 }
37363
37364 /* OpenMP 2.5:
37365 # pragma omp parallel parallel-clause[optseq] new-line
37366 structured-block
37367 # pragma omp parallel for parallel-for-clause[optseq] new-line
37368 structured-block
37369 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37370 structured-block
37371
37372 OpenMP 4.0:
37373 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37374 structured-block */
37375
37376 #define OMP_PARALLEL_CLAUSE_MASK \
37377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37386
37387 static tree
37388 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37389 char *p_name, omp_clause_mask mask, tree *cclauses,
37390 bool *if_p)
37391 {
37392 tree stmt, clauses, block;
37393 unsigned int save;
37394 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37395
37396 strcat (p_name, " parallel");
37397 mask |= OMP_PARALLEL_CLAUSE_MASK;
37398 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37399 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37400 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37401 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37402
37403 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37404 {
37405 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37406 if (cclauses == NULL)
37407 cclauses = cclauses_buf;
37408
37409 cp_lexer_consume_token (parser->lexer);
37410 if (!flag_openmp) /* flag_openmp_simd */
37411 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37412 if_p);
37413 block = begin_omp_parallel ();
37414 save = cp_parser_begin_omp_structured_block (parser);
37415 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37416 if_p);
37417 cp_parser_end_omp_structured_block (parser, save);
37418 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37419 block);
37420 if (ret == NULL_TREE)
37421 return ret;
37422 OMP_PARALLEL_COMBINED (stmt) = 1;
37423 return stmt;
37424 }
37425 /* When combined with distribute, parallel has to be followed by for.
37426 #pragma omp target parallel is allowed though. */
37427 else if (cclauses
37428 && (mask & (OMP_CLAUSE_MASK_1
37429 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37430 {
37431 error_at (loc, "expected %<for%> after %qs", p_name);
37432 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37433 return NULL_TREE;
37434 }
37435 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37436 {
37437 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37438 const char *p = IDENTIFIER_POINTER (id);
37439 if (strcmp (p, "master") == 0)
37440 {
37441 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37442 cclauses = cclauses_buf;
37443
37444 cp_lexer_consume_token (parser->lexer);
37445 block = begin_omp_parallel ();
37446 save = cp_parser_begin_omp_structured_block (parser);
37447 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37448 cclauses, if_p);
37449 cp_parser_end_omp_structured_block (parser, save);
37450 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37451 block);
37452 OMP_PARALLEL_COMBINED (stmt) = 1;
37453 if (ret == NULL_TREE)
37454 return ret;
37455 return stmt;
37456 }
37457 else if (!flag_openmp) /* flag_openmp_simd */
37458 {
37459 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37460 return NULL_TREE;
37461 }
37462 else if (strcmp (p, "sections") == 0)
37463 {
37464 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37465 cclauses = cclauses_buf;
37466
37467 cp_lexer_consume_token (parser->lexer);
37468 block = begin_omp_parallel ();
37469 save = cp_parser_begin_omp_structured_block (parser);
37470 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37471 cp_parser_end_omp_structured_block (parser, save);
37472 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37473 block);
37474 OMP_PARALLEL_COMBINED (stmt) = 1;
37475 return stmt;
37476 }
37477 }
37478 else if (!flag_openmp) /* flag_openmp_simd */
37479 {
37480 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37481 return NULL_TREE;
37482 }
37483
37484 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37485 cclauses == NULL);
37486 if (cclauses)
37487 {
37488 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37489 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37490 }
37491
37492 block = begin_omp_parallel ();
37493 save = cp_parser_begin_omp_structured_block (parser);
37494 cp_parser_statement (parser, NULL_TREE, false, if_p);
37495 cp_parser_end_omp_structured_block (parser, save);
37496 stmt = finish_omp_parallel (clauses, block);
37497 return stmt;
37498 }
37499
37500 /* OpenMP 2.5:
37501 # pragma omp single single-clause[optseq] new-line
37502 structured-block */
37503
37504 #define OMP_SINGLE_CLAUSE_MASK \
37505 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37509
37510 static tree
37511 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37512 {
37513 tree stmt = make_node (OMP_SINGLE);
37514 TREE_TYPE (stmt) = void_type_node;
37515 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37516
37517 OMP_SINGLE_CLAUSES (stmt)
37518 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37519 "#pragma omp single", pragma_tok);
37520 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37521
37522 return add_stmt (stmt);
37523 }
37524
37525 /* OpenMP 3.0:
37526 # pragma omp task task-clause[optseq] new-line
37527 structured-block */
37528
37529 #define OMP_TASK_CLAUSE_MASK \
37530 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37541
37542 static tree
37543 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37544 {
37545 tree clauses, block;
37546 unsigned int save;
37547
37548 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37549 "#pragma omp task", pragma_tok);
37550 block = begin_omp_task ();
37551 save = cp_parser_begin_omp_structured_block (parser);
37552 cp_parser_statement (parser, NULL_TREE, false, if_p);
37553 cp_parser_end_omp_structured_block (parser, save);
37554 return finish_omp_task (clauses, block);
37555 }
37556
37557 /* OpenMP 3.0:
37558 # pragma omp taskwait new-line
37559
37560 OpenMP 5.0:
37561 # pragma omp taskwait taskwait-clause[opt] new-line */
37562
37563 #define OMP_TASKWAIT_CLAUSE_MASK \
37564 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37565
37566 static void
37567 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37568 {
37569 tree clauses
37570 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37571 "#pragma omp taskwait", pragma_tok);
37572
37573 if (clauses)
37574 {
37575 tree stmt = make_node (OMP_TASK);
37576 TREE_TYPE (stmt) = void_node;
37577 OMP_TASK_CLAUSES (stmt) = clauses;
37578 OMP_TASK_BODY (stmt) = NULL_TREE;
37579 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37580 add_stmt (stmt);
37581 }
37582 else
37583 finish_omp_taskwait ();
37584 }
37585
37586 /* OpenMP 3.1:
37587 # pragma omp taskyield new-line */
37588
37589 static void
37590 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37591 {
37592 cp_parser_require_pragma_eol (parser, pragma_tok);
37593 finish_omp_taskyield ();
37594 }
37595
37596 /* OpenMP 4.0:
37597 # pragma omp taskgroup new-line
37598 structured-block
37599
37600 OpenMP 5.0:
37601 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37602
37603 #define OMP_TASKGROUP_CLAUSE_MASK \
37604 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37605
37606 static tree
37607 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37608 {
37609 tree clauses
37610 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37611 "#pragma omp taskgroup", pragma_tok);
37612 return c_finish_omp_taskgroup (input_location,
37613 cp_parser_omp_structured_block (parser,
37614 if_p),
37615 clauses);
37616 }
37617
37618
37619 /* OpenMP 2.5:
37620 # pragma omp threadprivate (variable-list) */
37621
37622 static void
37623 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37624 {
37625 tree vars;
37626
37627 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37628 cp_parser_require_pragma_eol (parser, pragma_tok);
37629
37630 finish_omp_threadprivate (vars);
37631 }
37632
37633 /* OpenMP 4.0:
37634 # pragma omp cancel cancel-clause[optseq] new-line */
37635
37636 #define OMP_CANCEL_CLAUSE_MASK \
37637 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37642
37643 static void
37644 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37645 {
37646 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37647 "#pragma omp cancel", pragma_tok);
37648 finish_omp_cancel (clauses);
37649 }
37650
37651 /* OpenMP 4.0:
37652 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37653
37654 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37655 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37659
37660 static void
37661 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37662 enum pragma_context context)
37663 {
37664 tree clauses;
37665 bool point_seen = false;
37666
37667 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37668 {
37669 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37670 const char *p = IDENTIFIER_POINTER (id);
37671
37672 if (strcmp (p, "point") == 0)
37673 {
37674 cp_lexer_consume_token (parser->lexer);
37675 point_seen = true;
37676 }
37677 }
37678 if (!point_seen)
37679 {
37680 cp_parser_error (parser, "expected %<point%>");
37681 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37682 return;
37683 }
37684
37685 if (context != pragma_compound)
37686 {
37687 if (context == pragma_stmt)
37688 error_at (pragma_tok->location,
37689 "%<#pragma %s%> may only be used in compound statements",
37690 "omp cancellation point");
37691 else
37692 cp_parser_error (parser, "expected declaration specifiers");
37693 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37694 return;
37695 }
37696
37697 clauses = cp_parser_omp_all_clauses (parser,
37698 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37699 "#pragma omp cancellation point",
37700 pragma_tok);
37701 finish_omp_cancellation_point (clauses);
37702 }
37703
37704 /* OpenMP 4.0:
37705 #pragma omp distribute distribute-clause[optseq] new-line
37706 for-loop */
37707
37708 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37709 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37714
37715 static tree
37716 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37717 char *p_name, omp_clause_mask mask, tree *cclauses,
37718 bool *if_p)
37719 {
37720 tree clauses, sb, ret;
37721 unsigned int save;
37722 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37723
37724 strcat (p_name, " distribute");
37725 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37726
37727 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37728 {
37729 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37730 const char *p = IDENTIFIER_POINTER (id);
37731 bool simd = false;
37732 bool parallel = false;
37733
37734 if (strcmp (p, "simd") == 0)
37735 simd = true;
37736 else
37737 parallel = strcmp (p, "parallel") == 0;
37738 if (parallel || simd)
37739 {
37740 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37741 if (cclauses == NULL)
37742 cclauses = cclauses_buf;
37743 cp_lexer_consume_token (parser->lexer);
37744 if (!flag_openmp) /* flag_openmp_simd */
37745 {
37746 if (simd)
37747 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37748 cclauses, if_p);
37749 else
37750 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37751 cclauses, if_p);
37752 }
37753 sb = begin_omp_structured_block ();
37754 save = cp_parser_begin_omp_structured_block (parser);
37755 if (simd)
37756 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37757 cclauses, if_p);
37758 else
37759 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37760 cclauses, if_p);
37761 cp_parser_end_omp_structured_block (parser, save);
37762 tree body = finish_omp_structured_block (sb);
37763 if (ret == NULL)
37764 return ret;
37765 ret = make_node (OMP_DISTRIBUTE);
37766 TREE_TYPE (ret) = void_type_node;
37767 OMP_FOR_BODY (ret) = body;
37768 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37769 SET_EXPR_LOCATION (ret, loc);
37770 add_stmt (ret);
37771 return ret;
37772 }
37773 }
37774 if (!flag_openmp) /* flag_openmp_simd */
37775 {
37776 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37777 return NULL_TREE;
37778 }
37779
37780 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37781 cclauses == NULL);
37782 if (cclauses)
37783 {
37784 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37785 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37786 }
37787
37788 keep_next_level (true);
37789 sb = begin_omp_structured_block ();
37790 save = cp_parser_begin_omp_structured_block (parser);
37791
37792 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37793
37794 cp_parser_end_omp_structured_block (parser, save);
37795 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37796
37797 return ret;
37798 }
37799
37800 /* OpenMP 4.0:
37801 # pragma omp teams teams-clause[optseq] new-line
37802 structured-block */
37803
37804 #define OMP_TEAMS_CLAUSE_MASK \
37805 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37812
37813 static tree
37814 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37815 char *p_name, omp_clause_mask mask, tree *cclauses,
37816 bool *if_p)
37817 {
37818 tree clauses, sb, ret;
37819 unsigned int save;
37820 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37821
37822 strcat (p_name, " teams");
37823 mask |= OMP_TEAMS_CLAUSE_MASK;
37824
37825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37826 {
37827 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37828 const char *p = IDENTIFIER_POINTER (id);
37829 if (strcmp (p, "distribute") == 0)
37830 {
37831 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37832 if (cclauses == NULL)
37833 cclauses = cclauses_buf;
37834
37835 cp_lexer_consume_token (parser->lexer);
37836 if (!flag_openmp) /* flag_openmp_simd */
37837 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37838 cclauses, if_p);
37839 keep_next_level (true);
37840 sb = begin_omp_structured_block ();
37841 save = cp_parser_begin_omp_structured_block (parser);
37842 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37843 cclauses, if_p);
37844 cp_parser_end_omp_structured_block (parser, save);
37845 tree body = finish_omp_structured_block (sb);
37846 if (ret == NULL)
37847 return ret;
37848 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37849 ret = make_node (OMP_TEAMS);
37850 TREE_TYPE (ret) = void_type_node;
37851 OMP_TEAMS_CLAUSES (ret) = clauses;
37852 OMP_TEAMS_BODY (ret) = body;
37853 OMP_TEAMS_COMBINED (ret) = 1;
37854 SET_EXPR_LOCATION (ret, loc);
37855 return add_stmt (ret);
37856 }
37857 }
37858 if (!flag_openmp) /* flag_openmp_simd */
37859 {
37860 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37861 return NULL_TREE;
37862 }
37863
37864 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37865 cclauses == NULL);
37866 if (cclauses)
37867 {
37868 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
37869 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
37870 }
37871
37872 tree stmt = make_node (OMP_TEAMS);
37873 TREE_TYPE (stmt) = void_type_node;
37874 OMP_TEAMS_CLAUSES (stmt) = clauses;
37875 keep_next_level (true);
37876 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37877 SET_EXPR_LOCATION (stmt, loc);
37878
37879 return add_stmt (stmt);
37880 }
37881
37882 /* OpenMP 4.0:
37883 # pragma omp target data target-data-clause[optseq] new-line
37884 structured-block */
37885
37886 #define OMP_TARGET_DATA_CLAUSE_MASK \
37887 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
37891
37892 static tree
37893 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37894 {
37895 tree clauses
37896 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
37897 "#pragma omp target data", pragma_tok);
37898 int map_seen = 0;
37899 for (tree *pc = &clauses; *pc;)
37900 {
37901 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
37902 switch (OMP_CLAUSE_MAP_KIND (*pc))
37903 {
37904 case GOMP_MAP_TO:
37905 case GOMP_MAP_ALWAYS_TO:
37906 case GOMP_MAP_FROM:
37907 case GOMP_MAP_ALWAYS_FROM:
37908 case GOMP_MAP_TOFROM:
37909 case GOMP_MAP_ALWAYS_TOFROM:
37910 case GOMP_MAP_ALLOC:
37911 map_seen = 3;
37912 break;
37913 case GOMP_MAP_FIRSTPRIVATE_POINTER:
37914 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
37915 case GOMP_MAP_ALWAYS_POINTER:
37916 break;
37917 default:
37918 map_seen |= 1;
37919 error_at (OMP_CLAUSE_LOCATION (*pc),
37920 "%<#pragma omp target data%> with map-type other "
37921 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
37922 "on %<map%> clause");
37923 *pc = OMP_CLAUSE_CHAIN (*pc);
37924 continue;
37925 }
37926 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
37927 map_seen = 3;
37928 pc = &OMP_CLAUSE_CHAIN (*pc);
37929 }
37930
37931 if (map_seen != 3)
37932 {
37933 if (map_seen == 0)
37934 error_at (pragma_tok->location,
37935 "%<#pragma omp target data%> must contain at least "
37936 "one %<map%> or %<use_device_ptr%> clause");
37937 return NULL_TREE;
37938 }
37939
37940 tree stmt = make_node (OMP_TARGET_DATA);
37941 TREE_TYPE (stmt) = void_type_node;
37942 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
37943
37944 keep_next_level (true);
37945 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37946
37947 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37948 return add_stmt (stmt);
37949 }
37950
37951 /* OpenMP 4.5:
37952 # pragma omp target enter data target-enter-data-clause[optseq] new-line
37953 structured-block */
37954
37955 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
37956 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
37957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
37958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37961
37962 static tree
37963 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
37964 enum pragma_context context)
37965 {
37966 bool data_seen = false;
37967 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37968 {
37969 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37970 const char *p = IDENTIFIER_POINTER (id);
37971
37972 if (strcmp (p, "data") == 0)
37973 {
37974 cp_lexer_consume_token (parser->lexer);
37975 data_seen = true;
37976 }
37977 }
37978 if (!data_seen)
37979 {
37980 cp_parser_error (parser, "expected %<data%>");
37981 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37982 return NULL_TREE;
37983 }
37984
37985 if (context == pragma_stmt)
37986 {
37987 error_at (pragma_tok->location,
37988 "%<#pragma %s%> may only be used in compound statements",
37989 "omp target enter data");
37990 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37991 return NULL_TREE;
37992 }
37993
37994 tree clauses
37995 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
37996 "#pragma omp target enter data", pragma_tok);
37997 int map_seen = 0;
37998 for (tree *pc = &clauses; *pc;)
37999 {
38000 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38001 switch (OMP_CLAUSE_MAP_KIND (*pc))
38002 {
38003 case GOMP_MAP_TO:
38004 case GOMP_MAP_ALWAYS_TO:
38005 case GOMP_MAP_ALLOC:
38006 map_seen = 3;
38007 break;
38008 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38009 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38010 case GOMP_MAP_ALWAYS_POINTER:
38011 break;
38012 default:
38013 map_seen |= 1;
38014 error_at (OMP_CLAUSE_LOCATION (*pc),
38015 "%<#pragma omp target enter data%> with map-type other "
38016 "than %<to%> or %<alloc%> on %<map%> clause");
38017 *pc = OMP_CLAUSE_CHAIN (*pc);
38018 continue;
38019 }
38020 pc = &OMP_CLAUSE_CHAIN (*pc);
38021 }
38022
38023 if (map_seen != 3)
38024 {
38025 if (map_seen == 0)
38026 error_at (pragma_tok->location,
38027 "%<#pragma omp target enter data%> must contain at least "
38028 "one %<map%> clause");
38029 return NULL_TREE;
38030 }
38031
38032 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
38033 TREE_TYPE (stmt) = void_type_node;
38034 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
38035 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38036 return add_stmt (stmt);
38037 }
38038
38039 /* OpenMP 4.5:
38040 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38041 structured-block */
38042
38043 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38044 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38049
38050 static tree
38051 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
38052 enum pragma_context context)
38053 {
38054 bool data_seen = false;
38055 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38056 {
38057 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38058 const char *p = IDENTIFIER_POINTER (id);
38059
38060 if (strcmp (p, "data") == 0)
38061 {
38062 cp_lexer_consume_token (parser->lexer);
38063 data_seen = true;
38064 }
38065 }
38066 if (!data_seen)
38067 {
38068 cp_parser_error (parser, "expected %<data%>");
38069 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38070 return NULL_TREE;
38071 }
38072
38073 if (context == pragma_stmt)
38074 {
38075 error_at (pragma_tok->location,
38076 "%<#pragma %s%> may only be used in compound statements",
38077 "omp target exit data");
38078 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38079 return NULL_TREE;
38080 }
38081
38082 tree clauses
38083 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
38084 "#pragma omp target exit data", pragma_tok);
38085 int map_seen = 0;
38086 for (tree *pc = &clauses; *pc;)
38087 {
38088 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38089 switch (OMP_CLAUSE_MAP_KIND (*pc))
38090 {
38091 case GOMP_MAP_FROM:
38092 case GOMP_MAP_ALWAYS_FROM:
38093 case GOMP_MAP_RELEASE:
38094 case GOMP_MAP_DELETE:
38095 map_seen = 3;
38096 break;
38097 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38098 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38099 case GOMP_MAP_ALWAYS_POINTER:
38100 break;
38101 default:
38102 map_seen |= 1;
38103 error_at (OMP_CLAUSE_LOCATION (*pc),
38104 "%<#pragma omp target exit data%> with map-type other "
38105 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38106 " clause");
38107 *pc = OMP_CLAUSE_CHAIN (*pc);
38108 continue;
38109 }
38110 pc = &OMP_CLAUSE_CHAIN (*pc);
38111 }
38112
38113 if (map_seen != 3)
38114 {
38115 if (map_seen == 0)
38116 error_at (pragma_tok->location,
38117 "%<#pragma omp target exit data%> must contain at least "
38118 "one %<map%> clause");
38119 return NULL_TREE;
38120 }
38121
38122 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
38123 TREE_TYPE (stmt) = void_type_node;
38124 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
38125 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38126 return add_stmt (stmt);
38127 }
38128
38129 /* OpenMP 4.0:
38130 # pragma omp target update target-update-clause[optseq] new-line */
38131
38132 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38133 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38134 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38139
38140 static bool
38141 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
38142 enum pragma_context context)
38143 {
38144 if (context == pragma_stmt)
38145 {
38146 error_at (pragma_tok->location,
38147 "%<#pragma %s%> may only be used in compound statements",
38148 "omp target update");
38149 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38150 return false;
38151 }
38152
38153 tree clauses
38154 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
38155 "#pragma omp target update", pragma_tok);
38156 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
38157 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
38158 {
38159 error_at (pragma_tok->location,
38160 "%<#pragma omp target update%> must contain at least one "
38161 "%<from%> or %<to%> clauses");
38162 return false;
38163 }
38164
38165 tree stmt = make_node (OMP_TARGET_UPDATE);
38166 TREE_TYPE (stmt) = void_type_node;
38167 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
38168 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38169 add_stmt (stmt);
38170 return false;
38171 }
38172
38173 /* OpenMP 4.0:
38174 # pragma omp target target-clause[optseq] new-line
38175 structured-block */
38176
38177 #define OMP_TARGET_CLAUSE_MASK \
38178 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38187
38188 static bool
38189 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
38190 enum pragma_context context, bool *if_p)
38191 {
38192 tree *pc = NULL, stmt;
38193
38194 if (flag_openmp)
38195 omp_requires_mask
38196 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
38197
38198 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38199 {
38200 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38201 const char *p = IDENTIFIER_POINTER (id);
38202 enum tree_code ccode = ERROR_MARK;
38203
38204 if (strcmp (p, "teams") == 0)
38205 ccode = OMP_TEAMS;
38206 else if (strcmp (p, "parallel") == 0)
38207 ccode = OMP_PARALLEL;
38208 else if (strcmp (p, "simd") == 0)
38209 ccode = OMP_SIMD;
38210 if (ccode != ERROR_MARK)
38211 {
38212 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38213 char p_name[sizeof ("#pragma omp target teams distribute "
38214 "parallel for simd")];
38215
38216 cp_lexer_consume_token (parser->lexer);
38217 strcpy (p_name, "#pragma omp target");
38218 if (!flag_openmp) /* flag_openmp_simd */
38219 {
38220 tree stmt;
38221 switch (ccode)
38222 {
38223 case OMP_TEAMS:
38224 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38225 OMP_TARGET_CLAUSE_MASK,
38226 cclauses, if_p);
38227 break;
38228 case OMP_PARALLEL:
38229 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38230 OMP_TARGET_CLAUSE_MASK,
38231 cclauses, if_p);
38232 break;
38233 case OMP_SIMD:
38234 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38235 OMP_TARGET_CLAUSE_MASK,
38236 cclauses, if_p);
38237 break;
38238 default:
38239 gcc_unreachable ();
38240 }
38241 return stmt != NULL_TREE;
38242 }
38243 keep_next_level (true);
38244 tree sb = begin_omp_structured_block (), ret;
38245 unsigned save = cp_parser_begin_omp_structured_block (parser);
38246 switch (ccode)
38247 {
38248 case OMP_TEAMS:
38249 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38250 OMP_TARGET_CLAUSE_MASK, cclauses,
38251 if_p);
38252 break;
38253 case OMP_PARALLEL:
38254 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38255 OMP_TARGET_CLAUSE_MASK, cclauses,
38256 if_p);
38257 break;
38258 case OMP_SIMD:
38259 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38260 OMP_TARGET_CLAUSE_MASK, cclauses,
38261 if_p);
38262 break;
38263 default:
38264 gcc_unreachable ();
38265 }
38266 cp_parser_end_omp_structured_block (parser, save);
38267 tree body = finish_omp_structured_block (sb);
38268 if (ret == NULL_TREE)
38269 return false;
38270 if (ccode == OMP_TEAMS && !processing_template_decl)
38271 {
38272 /* For combined target teams, ensure the num_teams and
38273 thread_limit clause expressions are evaluated on the host,
38274 before entering the target construct. */
38275 tree c;
38276 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38277 c; c = OMP_CLAUSE_CHAIN (c))
38278 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38279 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38280 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38281 {
38282 tree expr = OMP_CLAUSE_OPERAND (c, 0);
38283 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38284 if (expr == error_mark_node)
38285 continue;
38286 tree tmp = TARGET_EXPR_SLOT (expr);
38287 add_stmt (expr);
38288 OMP_CLAUSE_OPERAND (c, 0) = expr;
38289 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38290 OMP_CLAUSE_FIRSTPRIVATE);
38291 OMP_CLAUSE_DECL (tc) = tmp;
38292 OMP_CLAUSE_CHAIN (tc)
38293 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38294 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38295 }
38296 }
38297 tree stmt = make_node (OMP_TARGET);
38298 TREE_TYPE (stmt) = void_type_node;
38299 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38300 OMP_TARGET_BODY (stmt) = body;
38301 OMP_TARGET_COMBINED (stmt) = 1;
38302 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38303 add_stmt (stmt);
38304 pc = &OMP_TARGET_CLAUSES (stmt);
38305 goto check_clauses;
38306 }
38307 else if (!flag_openmp) /* flag_openmp_simd */
38308 {
38309 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38310 return false;
38311 }
38312 else if (strcmp (p, "data") == 0)
38313 {
38314 cp_lexer_consume_token (parser->lexer);
38315 cp_parser_omp_target_data (parser, pragma_tok, if_p);
38316 return true;
38317 }
38318 else if (strcmp (p, "enter") == 0)
38319 {
38320 cp_lexer_consume_token (parser->lexer);
38321 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38322 return false;
38323 }
38324 else if (strcmp (p, "exit") == 0)
38325 {
38326 cp_lexer_consume_token (parser->lexer);
38327 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38328 return false;
38329 }
38330 else if (strcmp (p, "update") == 0)
38331 {
38332 cp_lexer_consume_token (parser->lexer);
38333 return cp_parser_omp_target_update (parser, pragma_tok, context);
38334 }
38335 }
38336 if (!flag_openmp) /* flag_openmp_simd */
38337 {
38338 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38339 return false;
38340 }
38341
38342 stmt = make_node (OMP_TARGET);
38343 TREE_TYPE (stmt) = void_type_node;
38344
38345 OMP_TARGET_CLAUSES (stmt)
38346 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38347 "#pragma omp target", pragma_tok);
38348 pc = &OMP_TARGET_CLAUSES (stmt);
38349 keep_next_level (true);
38350 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38351
38352 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38353 add_stmt (stmt);
38354
38355 check_clauses:
38356 while (*pc)
38357 {
38358 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38359 switch (OMP_CLAUSE_MAP_KIND (*pc))
38360 {
38361 case GOMP_MAP_TO:
38362 case GOMP_MAP_ALWAYS_TO:
38363 case GOMP_MAP_FROM:
38364 case GOMP_MAP_ALWAYS_FROM:
38365 case GOMP_MAP_TOFROM:
38366 case GOMP_MAP_ALWAYS_TOFROM:
38367 case GOMP_MAP_ALLOC:
38368 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38369 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38370 case GOMP_MAP_ALWAYS_POINTER:
38371 break;
38372 default:
38373 error_at (OMP_CLAUSE_LOCATION (*pc),
38374 "%<#pragma omp target%> with map-type other "
38375 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38376 "on %<map%> clause");
38377 *pc = OMP_CLAUSE_CHAIN (*pc);
38378 continue;
38379 }
38380 pc = &OMP_CLAUSE_CHAIN (*pc);
38381 }
38382 return true;
38383 }
38384
38385 /* OpenACC 2.0:
38386 # pragma acc cache (variable-list) new-line
38387 */
38388
38389 static tree
38390 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38391 {
38392 tree stmt, clauses;
38393
38394 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38395 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38396
38397 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38398
38399 stmt = make_node (OACC_CACHE);
38400 TREE_TYPE (stmt) = void_type_node;
38401 OACC_CACHE_CLAUSES (stmt) = clauses;
38402 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38403 add_stmt (stmt);
38404
38405 return stmt;
38406 }
38407
38408 /* OpenACC 2.0:
38409 # pragma acc data oacc-data-clause[optseq] new-line
38410 structured-block */
38411
38412 #define OACC_DATA_CLAUSE_MASK \
38413 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38420
38421 static tree
38422 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38423 {
38424 tree stmt, clauses, block;
38425 unsigned int save;
38426
38427 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38428 "#pragma acc data", pragma_tok);
38429
38430 block = begin_omp_parallel ();
38431 save = cp_parser_begin_omp_structured_block (parser);
38432 cp_parser_statement (parser, NULL_TREE, false, if_p);
38433 cp_parser_end_omp_structured_block (parser, save);
38434 stmt = finish_oacc_data (clauses, block);
38435 return stmt;
38436 }
38437
38438 /* OpenACC 2.0:
38439 # pragma acc host_data <clauses> new-line
38440 structured-block */
38441
38442 #define OACC_HOST_DATA_CLAUSE_MASK \
38443 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38444
38445 static tree
38446 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38447 {
38448 tree stmt, clauses, block;
38449 unsigned int save;
38450
38451 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38452 "#pragma acc host_data", pragma_tok);
38453
38454 block = begin_omp_parallel ();
38455 save = cp_parser_begin_omp_structured_block (parser);
38456 cp_parser_statement (parser, NULL_TREE, false, if_p);
38457 cp_parser_end_omp_structured_block (parser, save);
38458 stmt = finish_oacc_host_data (clauses, block);
38459 return stmt;
38460 }
38461
38462 /* OpenACC 2.0:
38463 # pragma acc declare oacc-data-clause[optseq] new-line
38464 */
38465
38466 #define OACC_DECLARE_CLAUSE_MASK \
38467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38475
38476 static tree
38477 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38478 {
38479 tree clauses, stmt;
38480 bool error = false;
38481
38482 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38483 "#pragma acc declare", pragma_tok, true);
38484
38485
38486 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38487 {
38488 error_at (pragma_tok->location,
38489 "no valid clauses specified in %<#pragma acc declare%>");
38490 return NULL_TREE;
38491 }
38492
38493 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38494 {
38495 location_t loc = OMP_CLAUSE_LOCATION (t);
38496 tree decl = OMP_CLAUSE_DECL (t);
38497 if (!DECL_P (decl))
38498 {
38499 error_at (loc, "array section in %<#pragma acc declare%>");
38500 error = true;
38501 continue;
38502 }
38503 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38504 switch (OMP_CLAUSE_MAP_KIND (t))
38505 {
38506 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38507 case GOMP_MAP_ALLOC:
38508 case GOMP_MAP_TO:
38509 case GOMP_MAP_FORCE_DEVICEPTR:
38510 case GOMP_MAP_DEVICE_RESIDENT:
38511 break;
38512
38513 case GOMP_MAP_LINK:
38514 if (!global_bindings_p ()
38515 && (TREE_STATIC (decl)
38516 || !DECL_EXTERNAL (decl)))
38517 {
38518 error_at (loc,
38519 "%qD must be a global variable in "
38520 "%<#pragma acc declare link%>",
38521 decl);
38522 error = true;
38523 continue;
38524 }
38525 break;
38526
38527 default:
38528 if (global_bindings_p ())
38529 {
38530 error_at (loc, "invalid OpenACC clause at file scope");
38531 error = true;
38532 continue;
38533 }
38534 if (DECL_EXTERNAL (decl))
38535 {
38536 error_at (loc,
38537 "invalid use of %<extern%> variable %qD "
38538 "in %<#pragma acc declare%>", decl);
38539 error = true;
38540 continue;
38541 }
38542 else if (TREE_PUBLIC (decl))
38543 {
38544 error_at (loc,
38545 "invalid use of %<global%> variable %qD "
38546 "in %<#pragma acc declare%>", decl);
38547 error = true;
38548 continue;
38549 }
38550 break;
38551 }
38552
38553 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38554 || lookup_attribute ("omp declare target link",
38555 DECL_ATTRIBUTES (decl)))
38556 {
38557 error_at (loc, "variable %qD used more than once with "
38558 "%<#pragma acc declare%>", decl);
38559 error = true;
38560 continue;
38561 }
38562
38563 if (!error)
38564 {
38565 tree id;
38566
38567 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38568 id = get_identifier ("omp declare target link");
38569 else
38570 id = get_identifier ("omp declare target");
38571
38572 DECL_ATTRIBUTES (decl)
38573 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38574 if (global_bindings_p ())
38575 {
38576 symtab_node *node = symtab_node::get (decl);
38577 if (node != NULL)
38578 {
38579 node->offloadable = 1;
38580 if (ENABLE_OFFLOADING)
38581 {
38582 g->have_offload = true;
38583 if (is_a <varpool_node *> (node))
38584 vec_safe_push (offload_vars, decl);
38585 }
38586 }
38587 }
38588 }
38589 }
38590
38591 if (error || global_bindings_p ())
38592 return NULL_TREE;
38593
38594 stmt = make_node (OACC_DECLARE);
38595 TREE_TYPE (stmt) = void_type_node;
38596 OACC_DECLARE_CLAUSES (stmt) = clauses;
38597 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38598
38599 add_stmt (stmt);
38600
38601 return NULL_TREE;
38602 }
38603
38604 /* OpenACC 2.0:
38605 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38606
38607 or
38608
38609 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38610
38611 LOC is the location of the #pragma token.
38612 */
38613
38614 #define OACC_ENTER_DATA_CLAUSE_MASK \
38615 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38620
38621 #define OACC_EXIT_DATA_CLAUSE_MASK \
38622 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38628
38629 static tree
38630 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38631 bool enter)
38632 {
38633 location_t loc = pragma_tok->location;
38634 tree stmt, clauses;
38635 const char *p = "";
38636
38637 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38638 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38639
38640 if (strcmp (p, "data") != 0)
38641 {
38642 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38643 enter ? "enter" : "exit");
38644 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38645 return NULL_TREE;
38646 }
38647
38648 cp_lexer_consume_token (parser->lexer);
38649
38650 if (enter)
38651 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38652 "#pragma acc enter data", pragma_tok);
38653 else
38654 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38655 "#pragma acc exit data", pragma_tok);
38656
38657 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38658 {
38659 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38660 enter ? "enter" : "exit");
38661 return NULL_TREE;
38662 }
38663
38664 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38665 TREE_TYPE (stmt) = void_type_node;
38666 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38667 SET_EXPR_LOCATION (stmt, loc);
38668 add_stmt (stmt);
38669 return stmt;
38670 }
38671
38672 /* OpenACC 2.0:
38673 # pragma acc loop oacc-loop-clause[optseq] new-line
38674 structured-block */
38675
38676 #define OACC_LOOP_CLAUSE_MASK \
38677 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38687
38688 static tree
38689 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38690 omp_clause_mask mask, tree *cclauses, bool *if_p)
38691 {
38692 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38693
38694 strcat (p_name, " loop");
38695 mask |= OACC_LOOP_CLAUSE_MASK;
38696
38697 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38698 cclauses == NULL);
38699 if (cclauses)
38700 {
38701 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38702 if (*cclauses)
38703 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38704 if (clauses)
38705 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38706 }
38707
38708 tree block = begin_omp_structured_block ();
38709 int save = cp_parser_begin_omp_structured_block (parser);
38710 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38711 cp_parser_end_omp_structured_block (parser, save);
38712 add_stmt (finish_omp_structured_block (block));
38713
38714 return stmt;
38715 }
38716
38717 /* OpenACC 2.0:
38718 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38719 structured-block
38720
38721 or
38722
38723 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38724 structured-block
38725 */
38726
38727 #define OACC_KERNELS_CLAUSE_MASK \
38728 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38735 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38741
38742 #define OACC_PARALLEL_CLAUSE_MASK \
38743 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38759
38760 static tree
38761 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38762 char *p_name, bool *if_p)
38763 {
38764 omp_clause_mask mask;
38765 enum tree_code code;
38766 switch (cp_parser_pragma_kind (pragma_tok))
38767 {
38768 case PRAGMA_OACC_KERNELS:
38769 strcat (p_name, " kernels");
38770 mask = OACC_KERNELS_CLAUSE_MASK;
38771 code = OACC_KERNELS;
38772 break;
38773 case PRAGMA_OACC_PARALLEL:
38774 strcat (p_name, " parallel");
38775 mask = OACC_PARALLEL_CLAUSE_MASK;
38776 code = OACC_PARALLEL;
38777 break;
38778 default:
38779 gcc_unreachable ();
38780 }
38781
38782 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38783 {
38784 const char *p
38785 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38786 if (strcmp (p, "loop") == 0)
38787 {
38788 cp_lexer_consume_token (parser->lexer);
38789 tree block = begin_omp_parallel ();
38790 tree clauses;
38791 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
38792 &clauses, if_p);
38793 protected_set_expr_location (stmt, pragma_tok->location);
38794 return finish_omp_construct (code, block, clauses);
38795 }
38796 }
38797
38798 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38799
38800 tree block = begin_omp_parallel ();
38801 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38802 cp_parser_statement (parser, NULL_TREE, false, if_p);
38803 cp_parser_end_omp_structured_block (parser, save);
38804 return finish_omp_construct (code, block, clauses);
38805 }
38806
38807 /* OpenACC 2.0:
38808 # pragma acc update oacc-update-clause[optseq] new-line
38809 */
38810
38811 #define OACC_UPDATE_CLAUSE_MASK \
38812 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38818
38819 static tree
38820 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38821 {
38822 tree stmt, clauses;
38823
38824 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
38825 "#pragma acc update", pragma_tok);
38826
38827 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38828 {
38829 error_at (pragma_tok->location,
38830 "%<#pragma acc update%> must contain at least one "
38831 "%<device%> or %<host%> or %<self%> clause");
38832 return NULL_TREE;
38833 }
38834
38835 stmt = make_node (OACC_UPDATE);
38836 TREE_TYPE (stmt) = void_type_node;
38837 OACC_UPDATE_CLAUSES (stmt) = clauses;
38838 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38839 add_stmt (stmt);
38840 return stmt;
38841 }
38842
38843 /* OpenACC 2.0:
38844 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
38845
38846 LOC is the location of the #pragma token.
38847 */
38848
38849 #define OACC_WAIT_CLAUSE_MASK \
38850 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
38851
38852 static tree
38853 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
38854 {
38855 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
38856 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38857
38858 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38859 list = cp_parser_oacc_wait_list (parser, loc, list);
38860
38861 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
38862 "#pragma acc wait", pragma_tok);
38863
38864 stmt = c_finish_oacc_wait (loc, list, clauses);
38865 stmt = finish_expr_stmt (stmt);
38866
38867 return stmt;
38868 }
38869
38870 /* OpenMP 4.0:
38871 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
38872
38873 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
38874 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
38878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
38879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
38880
38881 static void
38882 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
38883 enum pragma_context context)
38884 {
38885 bool first_p = parser->omp_declare_simd == NULL;
38886 cp_omp_declare_simd_data data;
38887 if (first_p)
38888 {
38889 data.error_seen = false;
38890 data.fndecl_seen = false;
38891 data.tokens = vNULL;
38892 data.clauses = NULL_TREE;
38893 /* It is safe to take the address of a local variable; it will only be
38894 used while this scope is live. */
38895 parser->omp_declare_simd = &data;
38896 }
38897
38898 /* Store away all pragma tokens. */
38899 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38900 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38901 cp_lexer_consume_token (parser->lexer);
38902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38903 parser->omp_declare_simd->error_seen = true;
38904 cp_parser_require_pragma_eol (parser, pragma_tok);
38905 struct cp_token_cache *cp
38906 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38907 parser->omp_declare_simd->tokens.safe_push (cp);
38908
38909 if (first_p)
38910 {
38911 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38912 cp_parser_pragma (parser, context, NULL);
38913 switch (context)
38914 {
38915 case pragma_external:
38916 cp_parser_declaration (parser);
38917 break;
38918 case pragma_member:
38919 cp_parser_member_declaration (parser);
38920 break;
38921 case pragma_objc_icode:
38922 cp_parser_block_declaration (parser, /*statement_p=*/false);
38923 break;
38924 default:
38925 cp_parser_declaration_statement (parser);
38926 break;
38927 }
38928 if (parser->omp_declare_simd
38929 && !parser->omp_declare_simd->error_seen
38930 && !parser->omp_declare_simd->fndecl_seen)
38931 error_at (pragma_tok->location,
38932 "%<#pragma omp declare simd%> not immediately followed by "
38933 "function declaration or definition");
38934 data.tokens.release ();
38935 parser->omp_declare_simd = NULL;
38936 }
38937 }
38938
38939 /* Finalize #pragma omp declare simd clauses after direct declarator has
38940 been parsed, and put that into "omp declare simd" attribute. */
38941
38942 static tree
38943 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
38944 {
38945 struct cp_token_cache *ce;
38946 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
38947 int i;
38948
38949 if (!data->error_seen && data->fndecl_seen)
38950 {
38951 error ("%<#pragma omp declare simd%> not immediately followed by "
38952 "a single function declaration or definition");
38953 data->error_seen = true;
38954 }
38955 if (data->error_seen)
38956 return attrs;
38957
38958 FOR_EACH_VEC_ELT (data->tokens, i, ce)
38959 {
38960 tree c, cl;
38961
38962 cp_parser_push_lexer_for_tokens (parser, ce);
38963 parser->lexer->in_pragma = true;
38964 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38965 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38966 cp_lexer_consume_token (parser->lexer);
38967 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
38968 "#pragma omp declare simd", pragma_tok);
38969 cp_parser_pop_lexer (parser);
38970 if (cl)
38971 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
38972 c = build_tree_list (get_identifier ("omp declare simd"), cl);
38973 TREE_CHAIN (c) = attrs;
38974 if (processing_template_decl)
38975 ATTR_IS_DEPENDENT (c) = 1;
38976 attrs = c;
38977 }
38978
38979 data->fndecl_seen = true;
38980 return attrs;
38981 }
38982
38983
38984 /* OpenMP 4.0:
38985 # pragma omp declare target new-line
38986 declarations and definitions
38987 # pragma omp end declare target new-line
38988
38989 OpenMP 4.5:
38990 # pragma omp declare target ( extended-list ) new-line
38991
38992 # pragma omp declare target declare-target-clauses[seq] new-line */
38993
38994 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
38995 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
38997
38998 static void
38999 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
39000 {
39001 tree clauses = NULL_TREE;
39002 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39003 clauses
39004 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
39005 "#pragma omp declare target", pragma_tok);
39006 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39007 {
39008 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
39009 clauses);
39010 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39011 cp_parser_require_pragma_eol (parser, pragma_tok);
39012 }
39013 else
39014 {
39015 cp_parser_require_pragma_eol (parser, pragma_tok);
39016 scope_chain->omp_declare_target_attribute++;
39017 return;
39018 }
39019 if (scope_chain->omp_declare_target_attribute)
39020 error_at (pragma_tok->location,
39021 "%<#pragma omp declare target%> with clauses in between "
39022 "%<#pragma omp declare target%> without clauses and "
39023 "%<#pragma omp end declare target%>");
39024 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39025 {
39026 tree t = OMP_CLAUSE_DECL (c), id;
39027 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
39028 tree at2 = lookup_attribute ("omp declare target link",
39029 DECL_ATTRIBUTES (t));
39030 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
39031 {
39032 id = get_identifier ("omp declare target link");
39033 std::swap (at1, at2);
39034 }
39035 else
39036 id = get_identifier ("omp declare target");
39037 if (at2)
39038 {
39039 error_at (OMP_CLAUSE_LOCATION (c),
39040 "%qD specified both in declare target %<link%> and %<to%>"
39041 " clauses", t);
39042 continue;
39043 }
39044 if (!at1)
39045 {
39046 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39047 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
39048 continue;
39049
39050 symtab_node *node = symtab_node::get (t);
39051 if (node != NULL)
39052 {
39053 node->offloadable = 1;
39054 if (ENABLE_OFFLOADING)
39055 {
39056 g->have_offload = true;
39057 if (is_a <varpool_node *> (node))
39058 vec_safe_push (offload_vars, t);
39059 }
39060 }
39061 }
39062 }
39063 }
39064
39065 static void
39066 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
39067 {
39068 const char *p = "";
39069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39070 {
39071 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39072 p = IDENTIFIER_POINTER (id);
39073 }
39074 if (strcmp (p, "declare") == 0)
39075 {
39076 cp_lexer_consume_token (parser->lexer);
39077 p = "";
39078 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39079 {
39080 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39081 p = IDENTIFIER_POINTER (id);
39082 }
39083 if (strcmp (p, "target") == 0)
39084 cp_lexer_consume_token (parser->lexer);
39085 else
39086 {
39087 cp_parser_error (parser, "expected %<target%>");
39088 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39089 return;
39090 }
39091 }
39092 else
39093 {
39094 cp_parser_error (parser, "expected %<declare%>");
39095 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39096 return;
39097 }
39098 cp_parser_require_pragma_eol (parser, pragma_tok);
39099 if (!scope_chain->omp_declare_target_attribute)
39100 error_at (pragma_tok->location,
39101 "%<#pragma omp end declare target%> without corresponding "
39102 "%<#pragma omp declare target%>");
39103 else
39104 scope_chain->omp_declare_target_attribute--;
39105 }
39106
39107 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39108 expression and optional initializer clause of
39109 #pragma omp declare reduction. We store the expression(s) as
39110 either 3, 6 or 7 special statements inside of the artificial function's
39111 body. The first two statements are DECL_EXPRs for the artificial
39112 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39113 expression that uses those variables.
39114 If there was any INITIALIZER clause, this is followed by further statements,
39115 the fourth and fifth statements are DECL_EXPRs for the artificial
39116 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39117 constructor variant (first token after open paren is not omp_priv),
39118 then the sixth statement is a statement with the function call expression
39119 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39120 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39121 to initialize the OMP_PRIV artificial variable and there is seventh
39122 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39123
39124 static bool
39125 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
39126 {
39127 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
39128 gcc_assert (TYPE_REF_P (type));
39129 type = TREE_TYPE (type);
39130 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
39131 DECL_ARTIFICIAL (omp_out) = 1;
39132 pushdecl (omp_out);
39133 add_decl_expr (omp_out);
39134 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
39135 DECL_ARTIFICIAL (omp_in) = 1;
39136 pushdecl (omp_in);
39137 add_decl_expr (omp_in);
39138 tree combiner;
39139 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
39140
39141 keep_next_level (true);
39142 tree block = begin_omp_structured_block ();
39143 combiner = cp_parser_expression (parser);
39144 finish_expr_stmt (combiner);
39145 block = finish_omp_structured_block (block);
39146 add_stmt (block);
39147
39148 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
39149 return false;
39150
39151 const char *p = "";
39152 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39153 {
39154 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39155 p = IDENTIFIER_POINTER (id);
39156 }
39157
39158 if (strcmp (p, "initializer") == 0)
39159 {
39160 cp_lexer_consume_token (parser->lexer);
39161 matching_parens parens;
39162 if (!parens.require_open (parser))
39163 return false;
39164
39165 p = "";
39166 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39167 {
39168 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39169 p = IDENTIFIER_POINTER (id);
39170 }
39171
39172 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
39173 DECL_ARTIFICIAL (omp_priv) = 1;
39174 pushdecl (omp_priv);
39175 add_decl_expr (omp_priv);
39176 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
39177 DECL_ARTIFICIAL (omp_orig) = 1;
39178 pushdecl (omp_orig);
39179 add_decl_expr (omp_orig);
39180
39181 keep_next_level (true);
39182 block = begin_omp_structured_block ();
39183
39184 bool ctor = false;
39185 if (strcmp (p, "omp_priv") == 0)
39186 {
39187 bool is_direct_init, is_non_constant_init;
39188 ctor = true;
39189 cp_lexer_consume_token (parser->lexer);
39190 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39191 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
39192 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39193 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
39194 == CPP_CLOSE_PAREN
39195 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
39196 == CPP_CLOSE_PAREN))
39197 {
39198 finish_omp_structured_block (block);
39199 error ("invalid initializer clause");
39200 return false;
39201 }
39202 initializer = cp_parser_initializer (parser, &is_direct_init,
39203 &is_non_constant_init);
39204 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
39205 NULL_TREE, LOOKUP_ONLYCONVERTING);
39206 }
39207 else
39208 {
39209 cp_parser_parse_tentatively (parser);
39210 /* Don't create location wrapper nodes here. */
39211 auto_suppress_location_wrappers sentinel;
39212 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
39213 /*check_dependency_p=*/true,
39214 /*template_p=*/NULL,
39215 /*declarator_p=*/false,
39216 /*optional_p=*/false);
39217 vec<tree, va_gc> *args;
39218 if (fn_name == error_mark_node
39219 || cp_parser_error_occurred (parser)
39220 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39221 || ((args = cp_parser_parenthesized_expression_list
39222 (parser, non_attr, /*cast_p=*/false,
39223 /*allow_expansion_p=*/true,
39224 /*non_constant_p=*/NULL)),
39225 cp_parser_error_occurred (parser)))
39226 {
39227 finish_omp_structured_block (block);
39228 cp_parser_abort_tentative_parse (parser);
39229 cp_parser_error (parser, "expected id-expression (arguments)");
39230 return false;
39231 }
39232 unsigned int i;
39233 tree arg;
39234 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39235 if (arg == omp_priv
39236 || (TREE_CODE (arg) == ADDR_EXPR
39237 && TREE_OPERAND (arg, 0) == omp_priv))
39238 break;
39239 cp_parser_abort_tentative_parse (parser);
39240 if (arg == NULL_TREE)
39241 error ("one of the initializer call arguments should be %<omp_priv%>"
39242 " or %<&omp_priv%>");
39243 initializer = cp_parser_postfix_expression (parser, false, false, false,
39244 false, NULL);
39245 finish_expr_stmt (initializer);
39246 }
39247
39248 block = finish_omp_structured_block (block);
39249 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39250 add_stmt (block);
39251
39252 if (ctor)
39253 add_decl_expr (omp_orig);
39254
39255 if (!parens.require_close (parser))
39256 return false;
39257 }
39258
39259 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39260 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39261 UNKNOWN_LOCATION);
39262
39263 return true;
39264 }
39265
39266 /* OpenMP 4.0
39267 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39268 initializer-clause[opt] new-line
39269
39270 initializer-clause:
39271 initializer (omp_priv initializer)
39272 initializer (function-name (argument-list)) */
39273
39274 static void
39275 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39276 enum pragma_context)
39277 {
39278 auto_vec<tree> types;
39279 enum tree_code reduc_code = ERROR_MARK;
39280 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39281 unsigned int i;
39282 cp_token *first_token;
39283 cp_token_cache *cp;
39284 int errs;
39285 void *p;
39286
39287 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39288 p = obstack_alloc (&declarator_obstack, 0);
39289
39290 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39291 goto fail;
39292
39293 switch (cp_lexer_peek_token (parser->lexer)->type)
39294 {
39295 case CPP_PLUS:
39296 reduc_code = PLUS_EXPR;
39297 break;
39298 case CPP_MULT:
39299 reduc_code = MULT_EXPR;
39300 break;
39301 case CPP_MINUS:
39302 reduc_code = MINUS_EXPR;
39303 break;
39304 case CPP_AND:
39305 reduc_code = BIT_AND_EXPR;
39306 break;
39307 case CPP_XOR:
39308 reduc_code = BIT_XOR_EXPR;
39309 break;
39310 case CPP_OR:
39311 reduc_code = BIT_IOR_EXPR;
39312 break;
39313 case CPP_AND_AND:
39314 reduc_code = TRUTH_ANDIF_EXPR;
39315 break;
39316 case CPP_OR_OR:
39317 reduc_code = TRUTH_ORIF_EXPR;
39318 break;
39319 case CPP_NAME:
39320 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39321 break;
39322 default:
39323 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39324 "%<|%>, %<&&%>, %<||%> or identifier");
39325 goto fail;
39326 }
39327
39328 if (reduc_code != ERROR_MARK)
39329 cp_lexer_consume_token (parser->lexer);
39330
39331 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39332 if (reduc_id == error_mark_node)
39333 goto fail;
39334
39335 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39336 goto fail;
39337
39338 /* Types may not be defined in declare reduction type list. */
39339 const char *saved_message;
39340 saved_message = parser->type_definition_forbidden_message;
39341 parser->type_definition_forbidden_message
39342 = G_("types may not be defined in declare reduction type list");
39343 bool saved_colon_corrects_to_scope_p;
39344 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39345 parser->colon_corrects_to_scope_p = false;
39346 bool saved_colon_doesnt_start_class_def_p;
39347 saved_colon_doesnt_start_class_def_p
39348 = parser->colon_doesnt_start_class_def_p;
39349 parser->colon_doesnt_start_class_def_p = true;
39350
39351 while (true)
39352 {
39353 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39354 type = cp_parser_type_id (parser);
39355 if (type == error_mark_node)
39356 ;
39357 else if (ARITHMETIC_TYPE_P (type)
39358 && (orig_reduc_id == NULL_TREE
39359 || (TREE_CODE (type) != COMPLEX_TYPE
39360 && (id_equal (orig_reduc_id, "min")
39361 || id_equal (orig_reduc_id, "max")))))
39362 error_at (loc, "predeclared arithmetic type %qT in "
39363 "%<#pragma omp declare reduction%>", type);
39364 else if (TREE_CODE (type) == FUNCTION_TYPE
39365 || TREE_CODE (type) == METHOD_TYPE
39366 || TREE_CODE (type) == ARRAY_TYPE)
39367 error_at (loc, "function or array type %qT in "
39368 "%<#pragma omp declare reduction%>", type);
39369 else if (TYPE_REF_P (type))
39370 error_at (loc, "reference type %qT in "
39371 "%<#pragma omp declare reduction%>", type);
39372 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39373 error_at (loc, "const, volatile or __restrict qualified type %qT in "
39374 "%<#pragma omp declare reduction%>", type);
39375 else
39376 types.safe_push (type);
39377
39378 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39379 cp_lexer_consume_token (parser->lexer);
39380 else
39381 break;
39382 }
39383
39384 /* Restore the saved message. */
39385 parser->type_definition_forbidden_message = saved_message;
39386 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39387 parser->colon_doesnt_start_class_def_p
39388 = saved_colon_doesnt_start_class_def_p;
39389
39390 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39391 || types.is_empty ())
39392 {
39393 fail:
39394 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39395 goto done;
39396 }
39397
39398 first_token = cp_lexer_peek_token (parser->lexer);
39399 cp = NULL;
39400 errs = errorcount;
39401 FOR_EACH_VEC_ELT (types, i, type)
39402 {
39403 tree fntype
39404 = build_function_type_list (void_type_node,
39405 cp_build_reference_type (type, false),
39406 NULL_TREE);
39407 tree this_reduc_id = reduc_id;
39408 if (!dependent_type_p (type))
39409 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39410 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39411 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39412 DECL_ARTIFICIAL (fndecl) = 1;
39413 DECL_EXTERNAL (fndecl) = 1;
39414 DECL_DECLARED_INLINE_P (fndecl) = 1;
39415 DECL_IGNORED_P (fndecl) = 1;
39416 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39417 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39418 DECL_ATTRIBUTES (fndecl)
39419 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39420 DECL_ATTRIBUTES (fndecl));
39421 if (processing_template_decl)
39422 fndecl = push_template_decl (fndecl);
39423 bool block_scope = false;
39424 tree block = NULL_TREE;
39425 if (current_function_decl)
39426 {
39427 block_scope = true;
39428 DECL_CONTEXT (fndecl) = global_namespace;
39429 if (!processing_template_decl)
39430 pushdecl (fndecl);
39431 }
39432 else if (current_class_type)
39433 {
39434 if (cp == NULL)
39435 {
39436 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39437 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39438 cp_lexer_consume_token (parser->lexer);
39439 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39440 goto fail;
39441 cp = cp_token_cache_new (first_token,
39442 cp_lexer_peek_nth_token (parser->lexer,
39443 2));
39444 }
39445 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39446 finish_member_declaration (fndecl);
39447 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39448 DECL_PENDING_INLINE_P (fndecl) = 1;
39449 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39450 continue;
39451 }
39452 else
39453 {
39454 DECL_CONTEXT (fndecl) = current_namespace;
39455 pushdecl (fndecl);
39456 }
39457 if (!block_scope)
39458 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39459 else
39460 block = begin_omp_structured_block ();
39461 if (cp)
39462 {
39463 cp_parser_push_lexer_for_tokens (parser, cp);
39464 parser->lexer->in_pragma = true;
39465 }
39466 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39467 {
39468 if (!block_scope)
39469 finish_function (/*inline_p=*/false);
39470 else
39471 DECL_CONTEXT (fndecl) = current_function_decl;
39472 if (cp)
39473 cp_parser_pop_lexer (parser);
39474 goto fail;
39475 }
39476 if (cp)
39477 cp_parser_pop_lexer (parser);
39478 if (!block_scope)
39479 finish_function (/*inline_p=*/false);
39480 else
39481 {
39482 DECL_CONTEXT (fndecl) = current_function_decl;
39483 block = finish_omp_structured_block (block);
39484 if (TREE_CODE (block) == BIND_EXPR)
39485 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39486 else if (TREE_CODE (block) == STATEMENT_LIST)
39487 DECL_SAVED_TREE (fndecl) = block;
39488 if (processing_template_decl)
39489 add_decl_expr (fndecl);
39490 }
39491 cp_check_omp_declare_reduction (fndecl);
39492 if (cp == NULL && types.length () > 1)
39493 cp = cp_token_cache_new (first_token,
39494 cp_lexer_peek_nth_token (parser->lexer, 2));
39495 if (errs != errorcount)
39496 break;
39497 }
39498
39499 cp_parser_require_pragma_eol (parser, pragma_tok);
39500
39501 done:
39502 /* Free any declarators allocated. */
39503 obstack_free (&declarator_obstack, p);
39504 }
39505
39506 /* OpenMP 4.0
39507 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39508 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39509 initializer-clause[opt] new-line
39510 #pragma omp declare target new-line */
39511
39512 static bool
39513 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39514 enum pragma_context context)
39515 {
39516 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39517 {
39518 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39519 const char *p = IDENTIFIER_POINTER (id);
39520
39521 if (strcmp (p, "simd") == 0)
39522 {
39523 cp_lexer_consume_token (parser->lexer);
39524 cp_parser_omp_declare_simd (parser, pragma_tok,
39525 context);
39526 return true;
39527 }
39528 cp_ensure_no_omp_declare_simd (parser);
39529 if (strcmp (p, "reduction") == 0)
39530 {
39531 cp_lexer_consume_token (parser->lexer);
39532 cp_parser_omp_declare_reduction (parser, pragma_tok,
39533 context);
39534 return false;
39535 }
39536 if (!flag_openmp) /* flag_openmp_simd */
39537 {
39538 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39539 return false;
39540 }
39541 if (strcmp (p, "target") == 0)
39542 {
39543 cp_lexer_consume_token (parser->lexer);
39544 cp_parser_omp_declare_target (parser, pragma_tok);
39545 return false;
39546 }
39547 }
39548 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39549 "or %<target%>");
39550 cp_parser_require_pragma_eol (parser, pragma_tok);
39551 return false;
39552 }
39553
39554 /* OpenMP 5.0
39555 #pragma omp requires clauses[optseq] new-line */
39556
39557 static bool
39558 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39559 {
39560 bool first = true;
39561 enum omp_requires new_req = (enum omp_requires) 0;
39562
39563 location_t loc = pragma_tok->location;
39564 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39565 {
39566 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39567 cp_lexer_consume_token (parser->lexer);
39568
39569 first = false;
39570
39571 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39572 {
39573 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39574 const char *p = IDENTIFIER_POINTER (id);
39575 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39576 enum omp_requires this_req = (enum omp_requires) 0;
39577
39578 if (!strcmp (p, "unified_address"))
39579 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39580 else if (!strcmp (p, "unified_shared_memory"))
39581 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39582 else if (!strcmp (p, "dynamic_allocators"))
39583 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39584 else if (!strcmp (p, "reverse_offload"))
39585 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39586 else if (!strcmp (p, "atomic_default_mem_order"))
39587 {
39588 cp_lexer_consume_token (parser->lexer);
39589
39590 matching_parens parens;
39591 if (parens.require_open (parser))
39592 {
39593 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39594 {
39595 id = cp_lexer_peek_token (parser->lexer)->u.value;
39596 p = IDENTIFIER_POINTER (id);
39597
39598 if (!strcmp (p, "seq_cst"))
39599 this_req
39600 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39601 else if (!strcmp (p, "relaxed"))
39602 this_req
39603 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39604 else if (!strcmp (p, "acq_rel"))
39605 this_req
39606 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39607 }
39608 if (this_req == 0)
39609 {
39610 error_at (cp_lexer_peek_token (parser->lexer)->location,
39611 "expected %<seq_cst%>, %<relaxed%> or "
39612 "%<acq_rel%>");
39613 if (cp_lexer_nth_token_is (parser->lexer, 2,
39614 CPP_CLOSE_PAREN))
39615 cp_lexer_consume_token (parser->lexer);
39616 }
39617 else
39618 cp_lexer_consume_token (parser->lexer);
39619
39620 if (!parens.require_close (parser))
39621 cp_parser_skip_to_closing_parenthesis (parser,
39622 /*recovering=*/true,
39623 /*or_comma=*/false,
39624 /*consume_paren=*/
39625 true);
39626
39627 if (this_req == 0)
39628 {
39629 cp_parser_require_pragma_eol (parser, pragma_tok);
39630 return false;
39631 }
39632 }
39633 p = NULL;
39634 }
39635 else
39636 {
39637 error_at (cloc, "expected %<unified_address%>, "
39638 "%<unified_shared_memory%>, "
39639 "%<dynamic_allocators%>, "
39640 "%<reverse_offload%> "
39641 "or %<atomic_default_mem_order%> clause");
39642 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39643 return false;
39644 }
39645 if (p)
39646 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39647 "supported yet", p);
39648 if (p)
39649 cp_lexer_consume_token (parser->lexer);
39650 if (this_req)
39651 {
39652 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39653 {
39654 if ((this_req & new_req) != 0)
39655 error_at (cloc, "too many %qs clauses", p);
39656 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39657 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39658 error_at (cloc, "%qs clause used lexically after first "
39659 "target construct or offloading API", p);
39660 }
39661 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39662 {
39663 error_at (cloc, "too many %qs clauses",
39664 "atomic_default_mem_order");
39665 this_req = (enum omp_requires) 0;
39666 }
39667 else if ((omp_requires_mask
39668 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39669 {
39670 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39671 " clause in a single compilation unit");
39672 this_req
39673 = (enum omp_requires)
39674 (omp_requires_mask
39675 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39676 }
39677 else if ((omp_requires_mask
39678 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39679 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39680 "lexically after first %<atomic%> construct "
39681 "without memory order clause");
39682 new_req = (enum omp_requires) (new_req | this_req);
39683 omp_requires_mask
39684 = (enum omp_requires) (omp_requires_mask | this_req);
39685 continue;
39686 }
39687 }
39688 break;
39689 }
39690 cp_parser_require_pragma_eol (parser, pragma_tok);
39691
39692 if (new_req == 0)
39693 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39694 return false;
39695 }
39696
39697
39698 /* OpenMP 4.5:
39699 #pragma omp taskloop taskloop-clause[optseq] new-line
39700 for-loop
39701
39702 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39703 for-loop */
39704
39705 #define OMP_TASKLOOP_CLAUSE_MASK \
39706 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39722
39723 static tree
39724 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39725 char *p_name, omp_clause_mask mask, tree *cclauses,
39726 bool *if_p)
39727 {
39728 tree clauses, sb, ret;
39729 unsigned int save;
39730 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39731
39732 strcat (p_name, " taskloop");
39733 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39734 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39735 clause. */
39736 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39737 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39738
39739 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39740 {
39741 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39742 const char *p = IDENTIFIER_POINTER (id);
39743
39744 if (strcmp (p, "simd") == 0)
39745 {
39746 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39747 if (cclauses == NULL)
39748 cclauses = cclauses_buf;
39749
39750 cp_lexer_consume_token (parser->lexer);
39751 if (!flag_openmp) /* flag_openmp_simd */
39752 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39753 cclauses, if_p);
39754 sb = begin_omp_structured_block ();
39755 save = cp_parser_begin_omp_structured_block (parser);
39756 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39757 cclauses, if_p);
39758 cp_parser_end_omp_structured_block (parser, save);
39759 tree body = finish_omp_structured_block (sb);
39760 if (ret == NULL)
39761 return ret;
39762 ret = make_node (OMP_TASKLOOP);
39763 TREE_TYPE (ret) = void_type_node;
39764 OMP_FOR_BODY (ret) = body;
39765 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39766 SET_EXPR_LOCATION (ret, loc);
39767 add_stmt (ret);
39768 return ret;
39769 }
39770 }
39771 if (!flag_openmp) /* flag_openmp_simd */
39772 {
39773 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39774 return NULL_TREE;
39775 }
39776
39777 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39778 cclauses == NULL);
39779 if (cclauses)
39780 {
39781 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39782 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39783 }
39784
39785 keep_next_level (true);
39786 sb = begin_omp_structured_block ();
39787 save = cp_parser_begin_omp_structured_block (parser);
39788
39789 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39790 if_p);
39791
39792 cp_parser_end_omp_structured_block (parser, save);
39793 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39794
39795 return ret;
39796 }
39797
39798
39799 /* OpenACC 2.0:
39800 # pragma acc routine oacc-routine-clause[optseq] new-line
39801 function-definition
39802
39803 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39804 */
39805
39806 #define OACC_ROUTINE_CLAUSE_MASK \
39807 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39811
39812
39813 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39814 component, which must resolve to a declared namespace-scope
39815 function. The clauses are either processed directly (for a named
39816 function), or defered until the immediatley following declaration
39817 is parsed. */
39818
39819 static void
39820 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39821 enum pragma_context context)
39822 {
39823 gcc_checking_assert (context == pragma_external);
39824 /* The checking for "another pragma following this one" in the "no optional
39825 '( name )'" case makes sure that we dont re-enter. */
39826 gcc_checking_assert (parser->oacc_routine == NULL);
39827
39828 cp_oacc_routine_data data;
39829 data.error_seen = false;
39830 data.fndecl_seen = false;
39831 data.tokens = vNULL;
39832 data.clauses = NULL_TREE;
39833 data.loc = pragma_tok->location;
39834 /* It is safe to take the address of a local variable; it will only be
39835 used while this scope is live. */
39836 parser->oacc_routine = &data;
39837
39838 /* Look for optional '( name )'. */
39839 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39840 {
39841 matching_parens parens;
39842 parens.consume_open (parser); /* '(' */
39843
39844 /* We parse the name as an id-expression. If it resolves to
39845 anything other than a non-overloaded function at namespace
39846 scope, it's an error. */
39847 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
39848 tree name = cp_parser_id_expression (parser,
39849 /*template_keyword_p=*/false,
39850 /*check_dependency_p=*/false,
39851 /*template_p=*/NULL,
39852 /*declarator_p=*/false,
39853 /*optional_p=*/false);
39854 tree decl = (identifier_p (name)
39855 ? cp_parser_lookup_name_simple (parser, name, name_loc)
39856 : name);
39857 if (name != error_mark_node && decl == error_mark_node)
39858 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
39859
39860 if (decl == error_mark_node
39861 || !parens.require_close (parser))
39862 {
39863 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39864 parser->oacc_routine = NULL;
39865 return;
39866 }
39867
39868 data.clauses
39869 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39870 "#pragma acc routine",
39871 cp_lexer_peek_token (parser->lexer));
39872
39873 if (decl && is_overloaded_fn (decl)
39874 && (TREE_CODE (decl) != FUNCTION_DECL
39875 || DECL_FUNCTION_TEMPLATE_P (decl)))
39876 {
39877 error_at (name_loc,
39878 "%<#pragma acc routine%> names a set of overloads");
39879 parser->oacc_routine = NULL;
39880 return;
39881 }
39882
39883 /* Perhaps we should use the same rule as declarations in different
39884 namespaces? */
39885 if (!DECL_NAMESPACE_SCOPE_P (decl))
39886 {
39887 error_at (name_loc,
39888 "%qD does not refer to a namespace scope function", decl);
39889 parser->oacc_routine = NULL;
39890 return;
39891 }
39892
39893 if (TREE_CODE (decl) != FUNCTION_DECL)
39894 {
39895 error_at (name_loc, "%qD does not refer to a function", decl);
39896 parser->oacc_routine = NULL;
39897 return;
39898 }
39899
39900 cp_finalize_oacc_routine (parser, decl, false);
39901 parser->oacc_routine = NULL;
39902 }
39903 else /* No optional '( name )'. */
39904 {
39905 /* Store away all pragma tokens. */
39906 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39907 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39908 cp_lexer_consume_token (parser->lexer);
39909 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39910 parser->oacc_routine->error_seen = true;
39911 cp_parser_require_pragma_eol (parser, pragma_tok);
39912 struct cp_token_cache *cp
39913 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39914 parser->oacc_routine->tokens.safe_push (cp);
39915
39916 /* Emit a helpful diagnostic if there's another pragma following this
39917 one. */
39918 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39919 {
39920 cp_ensure_no_oacc_routine (parser);
39921 data.tokens.release ();
39922 /* ..., and then just keep going. */
39923 return;
39924 }
39925
39926 /* We only have to consider the pragma_external case here. */
39927 cp_parser_declaration (parser);
39928 if (parser->oacc_routine
39929 && !parser->oacc_routine->fndecl_seen)
39930 cp_ensure_no_oacc_routine (parser);
39931 else
39932 parser->oacc_routine = NULL;
39933 data.tokens.release ();
39934 }
39935 }
39936
39937 /* Finalize #pragma acc routine clauses after direct declarator has
39938 been parsed. */
39939
39940 static tree
39941 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
39942 {
39943 struct cp_token_cache *ce;
39944 cp_oacc_routine_data *data = parser->oacc_routine;
39945
39946 if (!data->error_seen && data->fndecl_seen)
39947 {
39948 error_at (data->loc,
39949 "%<#pragma acc routine%> not immediately followed by "
39950 "a single function declaration or definition");
39951 data->error_seen = true;
39952 }
39953 if (data->error_seen)
39954 return attrs;
39955
39956 gcc_checking_assert (data->tokens.length () == 1);
39957 ce = data->tokens[0];
39958
39959 cp_parser_push_lexer_for_tokens (parser, ce);
39960 parser->lexer->in_pragma = true;
39961 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39962
39963 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39964 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
39965 parser->oacc_routine->clauses
39966 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
39967 "#pragma acc routine", pragma_tok);
39968 cp_parser_pop_lexer (parser);
39969 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
39970 fndecl_seen. */
39971
39972 return attrs;
39973 }
39974
39975 /* Apply any saved OpenACC routine clauses to a just-parsed
39976 declaration. */
39977
39978 static void
39979 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
39980 {
39981 if (__builtin_expect (parser->oacc_routine != NULL, 0))
39982 {
39983 /* Keep going if we're in error reporting mode. */
39984 if (parser->oacc_routine->error_seen
39985 || fndecl == error_mark_node)
39986 return;
39987
39988 if (parser->oacc_routine->fndecl_seen)
39989 {
39990 error_at (parser->oacc_routine->loc,
39991 "%<#pragma acc routine%> not immediately followed by"
39992 " a single function declaration or definition");
39993 parser->oacc_routine = NULL;
39994 return;
39995 }
39996 if (TREE_CODE (fndecl) != FUNCTION_DECL)
39997 {
39998 cp_ensure_no_oacc_routine (parser);
39999 return;
40000 }
40001
40002 if (oacc_get_fn_attrib (fndecl))
40003 {
40004 error_at (parser->oacc_routine->loc,
40005 "%<#pragma acc routine%> already applied to %qD", fndecl);
40006 parser->oacc_routine = NULL;
40007 return;
40008 }
40009
40010 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
40011 {
40012 error_at (parser->oacc_routine->loc,
40013 TREE_USED (fndecl)
40014 ? G_("%<#pragma acc routine%> must be applied before use")
40015 : G_("%<#pragma acc routine%> must be applied before "
40016 "definition"));
40017 parser->oacc_routine = NULL;
40018 return;
40019 }
40020
40021 /* Process the routine's dimension clauses. */
40022 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
40023 oacc_replace_fn_attrib (fndecl, dims);
40024
40025 /* Add an "omp declare target" attribute. */
40026 DECL_ATTRIBUTES (fndecl)
40027 = tree_cons (get_identifier ("omp declare target"),
40028 NULL_TREE, DECL_ATTRIBUTES (fndecl));
40029
40030 /* Don't unset parser->oacc_routine here: we may still need it to
40031 diagnose wrong usage. But, remember that we've used this "#pragma acc
40032 routine". */
40033 parser->oacc_routine->fndecl_seen = true;
40034 }
40035 }
40036
40037 /* Main entry point to OpenMP statement pragmas. */
40038
40039 static void
40040 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40041 {
40042 tree stmt;
40043 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
40044 omp_clause_mask mask (0);
40045
40046 switch (cp_parser_pragma_kind (pragma_tok))
40047 {
40048 case PRAGMA_OACC_ATOMIC:
40049 cp_parser_omp_atomic (parser, pragma_tok);
40050 return;
40051 case PRAGMA_OACC_CACHE:
40052 stmt = cp_parser_oacc_cache (parser, pragma_tok);
40053 break;
40054 case PRAGMA_OACC_DATA:
40055 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
40056 break;
40057 case PRAGMA_OACC_ENTER_DATA:
40058 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
40059 break;
40060 case PRAGMA_OACC_EXIT_DATA:
40061 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
40062 break;
40063 case PRAGMA_OACC_HOST_DATA:
40064 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
40065 break;
40066 case PRAGMA_OACC_KERNELS:
40067 case PRAGMA_OACC_PARALLEL:
40068 strcpy (p_name, "#pragma acc");
40069 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
40070 if_p);
40071 break;
40072 case PRAGMA_OACC_LOOP:
40073 strcpy (p_name, "#pragma acc");
40074 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
40075 if_p);
40076 break;
40077 case PRAGMA_OACC_UPDATE:
40078 stmt = cp_parser_oacc_update (parser, pragma_tok);
40079 break;
40080 case PRAGMA_OACC_WAIT:
40081 stmt = cp_parser_oacc_wait (parser, pragma_tok);
40082 break;
40083 case PRAGMA_OMP_ATOMIC:
40084 cp_parser_omp_atomic (parser, pragma_tok);
40085 return;
40086 case PRAGMA_OMP_CRITICAL:
40087 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
40088 break;
40089 case PRAGMA_OMP_DISTRIBUTE:
40090 strcpy (p_name, "#pragma omp");
40091 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
40092 if_p);
40093 break;
40094 case PRAGMA_OMP_FOR:
40095 strcpy (p_name, "#pragma omp");
40096 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
40097 if_p);
40098 break;
40099 case PRAGMA_OMP_MASTER:
40100 strcpy (p_name, "#pragma omp");
40101 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
40102 if_p);
40103 break;
40104 case PRAGMA_OMP_PARALLEL:
40105 strcpy (p_name, "#pragma omp");
40106 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
40107 if_p);
40108 break;
40109 case PRAGMA_OMP_SECTIONS:
40110 strcpy (p_name, "#pragma omp");
40111 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
40112 break;
40113 case PRAGMA_OMP_SIMD:
40114 strcpy (p_name, "#pragma omp");
40115 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
40116 if_p);
40117 break;
40118 case PRAGMA_OMP_SINGLE:
40119 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
40120 break;
40121 case PRAGMA_OMP_TASK:
40122 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
40123 break;
40124 case PRAGMA_OMP_TASKGROUP:
40125 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
40126 break;
40127 case PRAGMA_OMP_TASKLOOP:
40128 strcpy (p_name, "#pragma omp");
40129 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
40130 if_p);
40131 break;
40132 case PRAGMA_OMP_TEAMS:
40133 strcpy (p_name, "#pragma omp");
40134 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
40135 if_p);
40136 break;
40137 default:
40138 gcc_unreachable ();
40139 }
40140
40141 protected_set_expr_location (stmt, pragma_tok->location);
40142 }
40143 \f
40144 /* Transactional Memory parsing routines. */
40145
40146 /* Parse a transaction attribute.
40147
40148 txn-attribute:
40149 attribute
40150 [ [ identifier ] ]
40151
40152 We use this instead of cp_parser_attributes_opt for transactions to avoid
40153 the pedwarn in C++98 mode. */
40154
40155 static tree
40156 cp_parser_txn_attribute_opt (cp_parser *parser)
40157 {
40158 cp_token *token;
40159 tree attr_name, attr = NULL;
40160
40161 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
40162 return cp_parser_attributes_opt (parser);
40163
40164 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
40165 return NULL_TREE;
40166 cp_lexer_consume_token (parser->lexer);
40167 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
40168 goto error1;
40169
40170 token = cp_lexer_peek_token (parser->lexer);
40171 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
40172 {
40173 token = cp_lexer_consume_token (parser->lexer);
40174
40175 attr_name = (token->type == CPP_KEYWORD
40176 /* For keywords, use the canonical spelling,
40177 not the parsed identifier. */
40178 ? ridpointers[(int) token->keyword]
40179 : token->u.value);
40180 attr = build_tree_list (attr_name, NULL_TREE);
40181 }
40182 else
40183 cp_parser_error (parser, "expected identifier");
40184
40185 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40186 error1:
40187 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40188 return attr;
40189 }
40190
40191 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40192
40193 transaction-statement:
40194 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40195 compound-statement
40196 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40197 */
40198
40199 static tree
40200 cp_parser_transaction (cp_parser *parser, cp_token *token)
40201 {
40202 unsigned char old_in = parser->in_transaction;
40203 unsigned char this_in = 1, new_in;
40204 enum rid keyword = token->keyword;
40205 tree stmt, attrs, noex;
40206
40207 cp_lexer_consume_token (parser->lexer);
40208
40209 if (keyword == RID_TRANSACTION_RELAXED
40210 || keyword == RID_SYNCHRONIZED)
40211 this_in |= TM_STMT_ATTR_RELAXED;
40212 else
40213 {
40214 attrs = cp_parser_txn_attribute_opt (parser);
40215 if (attrs)
40216 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40217 }
40218
40219 /* Parse a noexcept specification. */
40220 if (keyword == RID_ATOMIC_NOEXCEPT)
40221 noex = boolean_true_node;
40222 else if (keyword == RID_ATOMIC_CANCEL)
40223 {
40224 /* cancel-and-throw is unimplemented. */
40225 sorry ("atomic_cancel");
40226 noex = NULL_TREE;
40227 }
40228 else
40229 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
40230
40231 /* Keep track if we're in the lexical scope of an outer transaction. */
40232 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40233
40234 stmt = begin_transaction_stmt (token->location, NULL, this_in);
40235
40236 parser->in_transaction = new_in;
40237 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40238 parser->in_transaction = old_in;
40239
40240 finish_transaction_stmt (stmt, NULL, this_in, noex);
40241
40242 return stmt;
40243 }
40244
40245 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40246
40247 transaction-expression:
40248 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40249 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40250 */
40251
40252 static tree
40253 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40254 {
40255 unsigned char old_in = parser->in_transaction;
40256 unsigned char this_in = 1;
40257 cp_token *token;
40258 tree expr, noex;
40259 bool noex_expr;
40260 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40261
40262 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40263 || keyword == RID_TRANSACTION_RELAXED);
40264
40265 if (!flag_tm)
40266 error_at (loc,
40267 keyword == RID_TRANSACTION_RELAXED
40268 ? G_("%<__transaction_relaxed%> without transactional memory "
40269 "support enabled")
40270 : G_("%<__transaction_atomic%> without transactional memory "
40271 "support enabled"));
40272
40273 token = cp_parser_require_keyword (parser, keyword,
40274 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40275 : RT_TRANSACTION_RELAXED));
40276 gcc_assert (token != NULL);
40277
40278 if (keyword == RID_TRANSACTION_RELAXED)
40279 this_in |= TM_STMT_ATTR_RELAXED;
40280
40281 /* Set this early. This might mean that we allow transaction_cancel in
40282 an expression that we find out later actually has to be a constexpr.
40283 However, we expect that cxx_constant_value will be able to deal with
40284 this; also, if the noexcept has no constexpr, then what we parse next
40285 really is a transaction's body. */
40286 parser->in_transaction = this_in;
40287
40288 /* Parse a noexcept specification. */
40289 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
40290 true);
40291
40292 if (!noex || !noex_expr
40293 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40294 {
40295 matching_parens parens;
40296 parens.require_open (parser);
40297
40298 expr = cp_parser_expression (parser);
40299 expr = finish_parenthesized_expr (expr);
40300
40301 parens.require_close (parser);
40302 }
40303 else
40304 {
40305 /* The only expression that is available got parsed for the noexcept
40306 already. noexcept is true then. */
40307 expr = noex;
40308 noex = boolean_true_node;
40309 }
40310
40311 expr = build_transaction_expr (token->location, expr, this_in, noex);
40312 parser->in_transaction = old_in;
40313
40314 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40315 return error_mark_node;
40316
40317 return (flag_tm ? expr : error_mark_node);
40318 }
40319
40320 /* Parse a function-transaction-block.
40321
40322 function-transaction-block:
40323 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40324 function-body
40325 __transaction_atomic txn-attribute[opt] function-try-block
40326 __transaction_relaxed ctor-initializer[opt] function-body
40327 __transaction_relaxed function-try-block
40328 */
40329
40330 static void
40331 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40332 {
40333 unsigned char old_in = parser->in_transaction;
40334 unsigned char new_in = 1;
40335 tree compound_stmt, stmt, attrs;
40336 cp_token *token;
40337
40338 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40339 || keyword == RID_TRANSACTION_RELAXED);
40340 token = cp_parser_require_keyword (parser, keyword,
40341 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40342 : RT_TRANSACTION_RELAXED));
40343 gcc_assert (token != NULL);
40344
40345 if (keyword == RID_TRANSACTION_RELAXED)
40346 new_in |= TM_STMT_ATTR_RELAXED;
40347 else
40348 {
40349 attrs = cp_parser_txn_attribute_opt (parser);
40350 if (attrs)
40351 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40352 }
40353
40354 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40355
40356 parser->in_transaction = new_in;
40357
40358 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40359 cp_parser_function_try_block (parser);
40360 else
40361 cp_parser_ctor_initializer_opt_and_function_body
40362 (parser, /*in_function_try_block=*/false);
40363
40364 parser->in_transaction = old_in;
40365
40366 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40367 }
40368
40369 /* Parse a __transaction_cancel statement.
40370
40371 cancel-statement:
40372 __transaction_cancel txn-attribute[opt] ;
40373 __transaction_cancel txn-attribute[opt] throw-expression ;
40374
40375 ??? Cancel and throw is not yet implemented. */
40376
40377 static tree
40378 cp_parser_transaction_cancel (cp_parser *parser)
40379 {
40380 cp_token *token;
40381 bool is_outer = false;
40382 tree stmt, attrs;
40383
40384 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40385 RT_TRANSACTION_CANCEL);
40386 gcc_assert (token != NULL);
40387
40388 attrs = cp_parser_txn_attribute_opt (parser);
40389 if (attrs)
40390 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40391
40392 /* ??? Parse cancel-and-throw here. */
40393
40394 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40395
40396 if (!flag_tm)
40397 {
40398 error_at (token->location, "%<__transaction_cancel%> without "
40399 "transactional memory support enabled");
40400 return error_mark_node;
40401 }
40402 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40403 {
40404 error_at (token->location, "%<__transaction_cancel%> within a "
40405 "%<__transaction_relaxed%>");
40406 return error_mark_node;
40407 }
40408 else if (is_outer)
40409 {
40410 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40411 && !is_tm_may_cancel_outer (current_function_decl))
40412 {
40413 error_at (token->location, "outer %<__transaction_cancel%> not "
40414 "within outer %<__transaction_atomic%>");
40415 error_at (token->location,
40416 " or a %<transaction_may_cancel_outer%> function");
40417 return error_mark_node;
40418 }
40419 }
40420 else if (parser->in_transaction == 0)
40421 {
40422 error_at (token->location, "%<__transaction_cancel%> not within "
40423 "%<__transaction_atomic%>");
40424 return error_mark_node;
40425 }
40426
40427 stmt = build_tm_abort_call (token->location, is_outer);
40428 add_stmt (stmt);
40429
40430 return stmt;
40431 }
40432 \f
40433 /* The parser. */
40434
40435 static GTY (()) cp_parser *the_parser;
40436
40437 \f
40438 /* Special handling for the first token or line in the file. The first
40439 thing in the file might be #pragma GCC pch_preprocess, which loads a
40440 PCH file, which is a GC collection point. So we need to handle this
40441 first pragma without benefit of an existing lexer structure.
40442
40443 Always returns one token to the caller in *FIRST_TOKEN. This is
40444 either the true first token of the file, or the first token after
40445 the initial pragma. */
40446
40447 static void
40448 cp_parser_initial_pragma (cp_token *first_token)
40449 {
40450 tree name = NULL;
40451
40452 cp_lexer_get_preprocessor_token (NULL, first_token);
40453 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40454 return;
40455
40456 cp_lexer_get_preprocessor_token (NULL, first_token);
40457 if (first_token->type == CPP_STRING)
40458 {
40459 name = first_token->u.value;
40460
40461 cp_lexer_get_preprocessor_token (NULL, first_token);
40462 if (first_token->type != CPP_PRAGMA_EOL)
40463 error_at (first_token->location,
40464 "junk at end of %<#pragma GCC pch_preprocess%>");
40465 }
40466 else
40467 error_at (first_token->location, "expected string literal");
40468
40469 /* Skip to the end of the pragma. */
40470 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40471 cp_lexer_get_preprocessor_token (NULL, first_token);
40472
40473 /* Now actually load the PCH file. */
40474 if (name)
40475 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40476
40477 /* Read one more token to return to our caller. We have to do this
40478 after reading the PCH file in, since its pointers have to be
40479 live. */
40480 cp_lexer_get_preprocessor_token (NULL, first_token);
40481 }
40482
40483 /* Parse a pragma GCC ivdep. */
40484
40485 static bool
40486 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40487 {
40488 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40489 return true;
40490 }
40491
40492 /* Parse a pragma GCC unroll. */
40493
40494 static unsigned short
40495 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40496 {
40497 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40498 tree expr = cp_parser_constant_expression (parser);
40499 unsigned short unroll;
40500 expr = maybe_constant_value (expr);
40501 HOST_WIDE_INT lunroll = 0;
40502 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40503 || TREE_CODE (expr) != INTEGER_CST
40504 || (lunroll = tree_to_shwi (expr)) < 0
40505 || lunroll >= USHRT_MAX)
40506 {
40507 error_at (location, "%<#pragma GCC unroll%> requires an"
40508 " assignment-expression that evaluates to a non-negative"
40509 " integral constant less than %u", USHRT_MAX);
40510 unroll = 0;
40511 }
40512 else
40513 {
40514 unroll = (unsigned short)lunroll;
40515 if (unroll == 0)
40516 unroll = 1;
40517 }
40518 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40519 return unroll;
40520 }
40521
40522 /* Normal parsing of a pragma token. Here we can (and must) use the
40523 regular lexer. */
40524
40525 static bool
40526 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40527 {
40528 cp_token *pragma_tok;
40529 unsigned int id;
40530 tree stmt;
40531 bool ret;
40532
40533 pragma_tok = cp_lexer_consume_token (parser->lexer);
40534 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40535 parser->lexer->in_pragma = true;
40536
40537 id = cp_parser_pragma_kind (pragma_tok);
40538 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40539 cp_ensure_no_omp_declare_simd (parser);
40540 switch (id)
40541 {
40542 case PRAGMA_GCC_PCH_PREPROCESS:
40543 error_at (pragma_tok->location,
40544 "%<#pragma GCC pch_preprocess%> must be first");
40545 break;
40546
40547 case PRAGMA_OMP_BARRIER:
40548 switch (context)
40549 {
40550 case pragma_compound:
40551 cp_parser_omp_barrier (parser, pragma_tok);
40552 return false;
40553 case pragma_stmt:
40554 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40555 "used in compound statements", "omp barrier");
40556 break;
40557 default:
40558 goto bad_stmt;
40559 }
40560 break;
40561
40562 case PRAGMA_OMP_DEPOBJ:
40563 switch (context)
40564 {
40565 case pragma_compound:
40566 cp_parser_omp_depobj (parser, pragma_tok);
40567 return false;
40568 case pragma_stmt:
40569 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40570 "used in compound statements", "omp depobj");
40571 break;
40572 default:
40573 goto bad_stmt;
40574 }
40575 break;
40576
40577 case PRAGMA_OMP_FLUSH:
40578 switch (context)
40579 {
40580 case pragma_compound:
40581 cp_parser_omp_flush (parser, pragma_tok);
40582 return false;
40583 case pragma_stmt:
40584 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40585 "used in compound statements", "omp flush");
40586 break;
40587 default:
40588 goto bad_stmt;
40589 }
40590 break;
40591
40592 case PRAGMA_OMP_TASKWAIT:
40593 switch (context)
40594 {
40595 case pragma_compound:
40596 cp_parser_omp_taskwait (parser, pragma_tok);
40597 return false;
40598 case pragma_stmt:
40599 error_at (pragma_tok->location,
40600 "%<#pragma %s%> may only be used in compound statements",
40601 "omp taskwait");
40602 break;
40603 default:
40604 goto bad_stmt;
40605 }
40606 break;
40607
40608 case PRAGMA_OMP_TASKYIELD:
40609 switch (context)
40610 {
40611 case pragma_compound:
40612 cp_parser_omp_taskyield (parser, pragma_tok);
40613 return false;
40614 case pragma_stmt:
40615 error_at (pragma_tok->location,
40616 "%<#pragma %s%> may only be used in compound statements",
40617 "omp taskyield");
40618 break;
40619 default:
40620 goto bad_stmt;
40621 }
40622 break;
40623
40624 case PRAGMA_OMP_CANCEL:
40625 switch (context)
40626 {
40627 case pragma_compound:
40628 cp_parser_omp_cancel (parser, pragma_tok);
40629 return false;
40630 case pragma_stmt:
40631 error_at (pragma_tok->location,
40632 "%<#pragma %s%> may only be used in compound statements",
40633 "omp cancel");
40634 break;
40635 default:
40636 goto bad_stmt;
40637 }
40638 break;
40639
40640 case PRAGMA_OMP_CANCELLATION_POINT:
40641 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40642 return false;
40643
40644 case PRAGMA_OMP_THREADPRIVATE:
40645 cp_parser_omp_threadprivate (parser, pragma_tok);
40646 return false;
40647
40648 case PRAGMA_OMP_DECLARE:
40649 return cp_parser_omp_declare (parser, pragma_tok, context);
40650
40651 case PRAGMA_OACC_DECLARE:
40652 cp_parser_oacc_declare (parser, pragma_tok);
40653 return false;
40654
40655 case PRAGMA_OACC_ENTER_DATA:
40656 if (context == pragma_stmt)
40657 {
40658 error_at (pragma_tok->location,
40659 "%<#pragma %s%> may only be used in compound statements",
40660 "acc enter data");
40661 break;
40662 }
40663 else if (context != pragma_compound)
40664 goto bad_stmt;
40665 cp_parser_omp_construct (parser, pragma_tok, if_p);
40666 return true;
40667
40668 case PRAGMA_OACC_EXIT_DATA:
40669 if (context == pragma_stmt)
40670 {
40671 error_at (pragma_tok->location,
40672 "%<#pragma %s%> may only be used in compound statements",
40673 "acc exit data");
40674 break;
40675 }
40676 else if (context != pragma_compound)
40677 goto bad_stmt;
40678 cp_parser_omp_construct (parser, pragma_tok, if_p);
40679 return true;
40680
40681 case PRAGMA_OACC_ROUTINE:
40682 if (context != pragma_external)
40683 {
40684 error_at (pragma_tok->location,
40685 "%<#pragma acc routine%> must be at file scope");
40686 break;
40687 }
40688 cp_parser_oacc_routine (parser, pragma_tok, context);
40689 return false;
40690
40691 case PRAGMA_OACC_UPDATE:
40692 if (context == pragma_stmt)
40693 {
40694 error_at (pragma_tok->location,
40695 "%<#pragma %s%> may only be used in compound statements",
40696 "acc update");
40697 break;
40698 }
40699 else if (context != pragma_compound)
40700 goto bad_stmt;
40701 cp_parser_omp_construct (parser, pragma_tok, if_p);
40702 return true;
40703
40704 case PRAGMA_OACC_WAIT:
40705 if (context == pragma_stmt)
40706 {
40707 error_at (pragma_tok->location,
40708 "%<#pragma %s%> may only be used in compound statements",
40709 "acc wait");
40710 break;
40711 }
40712 else if (context != pragma_compound)
40713 goto bad_stmt;
40714 cp_parser_omp_construct (parser, pragma_tok, if_p);
40715 return true;
40716
40717 case PRAGMA_OACC_ATOMIC:
40718 case PRAGMA_OACC_CACHE:
40719 case PRAGMA_OACC_DATA:
40720 case PRAGMA_OACC_HOST_DATA:
40721 case PRAGMA_OACC_KERNELS:
40722 case PRAGMA_OACC_PARALLEL:
40723 case PRAGMA_OACC_LOOP:
40724 case PRAGMA_OMP_ATOMIC:
40725 case PRAGMA_OMP_CRITICAL:
40726 case PRAGMA_OMP_DISTRIBUTE:
40727 case PRAGMA_OMP_FOR:
40728 case PRAGMA_OMP_MASTER:
40729 case PRAGMA_OMP_PARALLEL:
40730 case PRAGMA_OMP_SECTIONS:
40731 case PRAGMA_OMP_SIMD:
40732 case PRAGMA_OMP_SINGLE:
40733 case PRAGMA_OMP_TASK:
40734 case PRAGMA_OMP_TASKGROUP:
40735 case PRAGMA_OMP_TASKLOOP:
40736 case PRAGMA_OMP_TEAMS:
40737 if (context != pragma_stmt && context != pragma_compound)
40738 goto bad_stmt;
40739 stmt = push_omp_privatization_clauses (false);
40740 cp_parser_omp_construct (parser, pragma_tok, if_p);
40741 pop_omp_privatization_clauses (stmt);
40742 return true;
40743
40744 case PRAGMA_OMP_REQUIRES:
40745 return cp_parser_omp_requires (parser, pragma_tok);
40746
40747 case PRAGMA_OMP_ORDERED:
40748 if (context != pragma_stmt && context != pragma_compound)
40749 goto bad_stmt;
40750 stmt = push_omp_privatization_clauses (false);
40751 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40752 pop_omp_privatization_clauses (stmt);
40753 return ret;
40754
40755 case PRAGMA_OMP_TARGET:
40756 if (context != pragma_stmt && context != pragma_compound)
40757 goto bad_stmt;
40758 stmt = push_omp_privatization_clauses (false);
40759 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40760 pop_omp_privatization_clauses (stmt);
40761 return ret;
40762
40763 case PRAGMA_OMP_END_DECLARE_TARGET:
40764 cp_parser_omp_end_declare_target (parser, pragma_tok);
40765 return false;
40766
40767 case PRAGMA_OMP_SECTION:
40768 error_at (pragma_tok->location,
40769 "%<#pragma omp section%> may only be used in "
40770 "%<#pragma omp sections%> construct");
40771 break;
40772
40773 case PRAGMA_IVDEP:
40774 {
40775 if (context == pragma_external)
40776 {
40777 error_at (pragma_tok->location,
40778 "%<#pragma GCC ivdep%> must be inside a function");
40779 break;
40780 }
40781 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40782 unsigned short unroll;
40783 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40784 if (tok->type == CPP_PRAGMA
40785 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40786 {
40787 tok = cp_lexer_consume_token (parser->lexer);
40788 unroll = cp_parser_pragma_unroll (parser, tok);
40789 tok = cp_lexer_peek_token (the_parser->lexer);
40790 }
40791 else
40792 unroll = 0;
40793 if (tok->type != CPP_KEYWORD
40794 || (tok->keyword != RID_FOR
40795 && tok->keyword != RID_WHILE
40796 && tok->keyword != RID_DO))
40797 {
40798 cp_parser_error (parser, "for, while or do statement expected");
40799 return false;
40800 }
40801 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40802 return true;
40803 }
40804
40805 case PRAGMA_UNROLL:
40806 {
40807 if (context == pragma_external)
40808 {
40809 error_at (pragma_tok->location,
40810 "%<#pragma GCC unroll%> must be inside a function");
40811 break;
40812 }
40813 const unsigned short unroll
40814 = cp_parser_pragma_unroll (parser, pragma_tok);
40815 bool ivdep;
40816 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40817 if (tok->type == CPP_PRAGMA
40818 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40819 {
40820 tok = cp_lexer_consume_token (parser->lexer);
40821 ivdep = cp_parser_pragma_ivdep (parser, tok);
40822 tok = cp_lexer_peek_token (the_parser->lexer);
40823 }
40824 else
40825 ivdep = false;
40826 if (tok->type != CPP_KEYWORD
40827 || (tok->keyword != RID_FOR
40828 && tok->keyword != RID_WHILE
40829 && tok->keyword != RID_DO))
40830 {
40831 cp_parser_error (parser, "for, while or do statement expected");
40832 return false;
40833 }
40834 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40835 return true;
40836 }
40837
40838 default:
40839 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
40840 c_invoke_pragma_handler (id);
40841 break;
40842
40843 bad_stmt:
40844 cp_parser_error (parser, "expected declaration specifiers");
40845 break;
40846 }
40847
40848 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40849 return false;
40850 }
40851
40852 /* The interface the pragma parsers have to the lexer. */
40853
40854 enum cpp_ttype
40855 pragma_lex (tree *value, location_t *loc)
40856 {
40857 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40858 enum cpp_ttype ret = tok->type;
40859
40860 *value = tok->u.value;
40861 if (loc)
40862 *loc = tok->location;
40863
40864 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
40865 ret = CPP_EOF;
40866 else if (ret == CPP_STRING)
40867 *value = cp_parser_string_literal (the_parser, false, false);
40868 else
40869 {
40870 if (ret == CPP_KEYWORD)
40871 ret = CPP_NAME;
40872 cp_lexer_consume_token (the_parser->lexer);
40873 }
40874
40875 return ret;
40876 }
40877
40878 \f
40879 /* External interface. */
40880
40881 /* Parse one entire translation unit. */
40882
40883 void
40884 c_parse_file (void)
40885 {
40886 static bool already_called = false;
40887
40888 if (already_called)
40889 fatal_error (input_location,
40890 "inter-module optimizations not implemented for C++");
40891 already_called = true;
40892
40893 the_parser = cp_parser_new ();
40894 push_deferring_access_checks (flag_access_control
40895 ? dk_no_deferred : dk_no_check);
40896 cp_parser_translation_unit (the_parser);
40897 the_parser = NULL;
40898
40899 finish_translation_unit ();
40900 }
40901
40902 /* Create an identifier for a generic parameter type (a synthesized
40903 template parameter implied by `auto' or a concept identifier). */
40904
40905 static GTY(()) int generic_parm_count;
40906 static tree
40907 make_generic_type_name ()
40908 {
40909 char buf[32];
40910 sprintf (buf, "auto:%d", ++generic_parm_count);
40911 return get_identifier (buf);
40912 }
40913
40914 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
40915 (creating a new template parameter list if necessary). Returns the newly
40916 created template type parm. */
40917
40918 static tree
40919 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
40920 {
40921 gcc_assert (current_binding_level->kind == sk_function_parms);
40922
40923 /* Before committing to modifying any scope, if we're in an
40924 implicit template scope, and we're trying to synthesize a
40925 constrained parameter, try to find a previous parameter with
40926 the same name. This is the same-type rule for abbreviated
40927 function templates.
40928
40929 NOTE: We can generate implicit parameters when tentatively
40930 parsing a nested name specifier, only to reject that parse
40931 later. However, matching the same template-id as part of a
40932 direct-declarator should generate an identical template
40933 parameter, so this rule will merge them. */
40934 if (parser->implicit_template_scope && constr)
40935 {
40936 tree t = parser->implicit_template_parms;
40937 while (t)
40938 {
40939 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
40940 {
40941 tree d = TREE_VALUE (t);
40942 if (TREE_CODE (d) == PARM_DECL)
40943 /* Return the TEMPLATE_PARM_INDEX. */
40944 d = DECL_INITIAL (d);
40945 return d;
40946 }
40947 t = TREE_CHAIN (t);
40948 }
40949 }
40950
40951 /* We are either continuing a function template that already contains implicit
40952 template parameters, creating a new fully-implicit function template, or
40953 extending an existing explicit function template with implicit template
40954 parameters. */
40955
40956 cp_binding_level *const entry_scope = current_binding_level;
40957
40958 bool become_template = false;
40959 cp_binding_level *parent_scope = 0;
40960
40961 if (parser->implicit_template_scope)
40962 {
40963 gcc_assert (parser->implicit_template_parms);
40964
40965 current_binding_level = parser->implicit_template_scope;
40966 }
40967 else
40968 {
40969 /* Roll back to the existing template parameter scope (in the case of
40970 extending an explicit function template) or introduce a new template
40971 parameter scope ahead of the function parameter scope (or class scope
40972 in the case of out-of-line member definitions). The function scope is
40973 added back after template parameter synthesis below. */
40974
40975 cp_binding_level *scope = entry_scope;
40976
40977 while (scope->kind == sk_function_parms)
40978 {
40979 parent_scope = scope;
40980 scope = scope->level_chain;
40981 }
40982 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
40983 {
40984 /* If not defining a class, then any class scope is a scope level in
40985 an out-of-line member definition. In this case simply wind back
40986 beyond the first such scope to inject the template parameter list.
40987 Otherwise wind back to the class being defined. The latter can
40988 occur in class member friend declarations such as:
40989
40990 class A {
40991 void foo (auto);
40992 };
40993 class B {
40994 friend void A::foo (auto);
40995 };
40996
40997 The template parameter list synthesized for the friend declaration
40998 must be injected in the scope of 'B'. This can also occur in
40999 erroneous cases such as:
41000
41001 struct A {
41002 struct B {
41003 void foo (auto);
41004 };
41005 void B::foo (auto) {}
41006 };
41007
41008 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41009 but, nevertheless, the template parameter list synthesized for the
41010 declarator should be injected into the scope of 'A' as if the
41011 ill-formed template was specified explicitly. */
41012
41013 while (scope->kind == sk_class && !scope->defining_class_p)
41014 {
41015 parent_scope = scope;
41016 scope = scope->level_chain;
41017 }
41018 }
41019
41020 current_binding_level = scope;
41021
41022 if (scope->kind != sk_template_parms
41023 || !function_being_declared_is_template_p (parser))
41024 {
41025 /* Introduce a new template parameter list for implicit template
41026 parameters. */
41027
41028 become_template = true;
41029
41030 parser->implicit_template_scope
41031 = begin_scope (sk_template_parms, NULL);
41032
41033 ++processing_template_decl;
41034
41035 parser->fully_implicit_function_template_p = true;
41036 ++parser->num_template_parameter_lists;
41037 }
41038 else
41039 {
41040 /* Synthesize implicit template parameters at the end of the explicit
41041 template parameter list. */
41042
41043 gcc_assert (current_template_parms);
41044
41045 parser->implicit_template_scope = scope;
41046
41047 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41048 parser->implicit_template_parms
41049 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
41050 }
41051 }
41052
41053 /* Synthesize a new template parameter and track the current template
41054 parameter chain with implicit_template_parms. */
41055
41056 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
41057 tree synth_id = make_generic_type_name ();
41058 tree synth_tmpl_parm;
41059 bool non_type = false;
41060
41061 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
41062 synth_tmpl_parm
41063 = finish_template_type_parm (class_type_node, synth_id);
41064 else if (TREE_CODE (proto) == TEMPLATE_DECL)
41065 synth_tmpl_parm
41066 = finish_constrained_template_template_parm (proto, synth_id);
41067 else
41068 {
41069 synth_tmpl_parm = copy_decl (proto);
41070 DECL_NAME (synth_tmpl_parm) = synth_id;
41071 non_type = true;
41072 }
41073
41074 // Attach the constraint to the parm before processing.
41075 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
41076 TREE_TYPE (node) = constr;
41077 tree new_parm
41078 = process_template_parm (parser->implicit_template_parms,
41079 input_location,
41080 node,
41081 /*non_type=*/non_type,
41082 /*param_pack=*/false);
41083
41084 // Chain the new parameter to the list of implicit parameters.
41085 if (parser->implicit_template_parms)
41086 parser->implicit_template_parms
41087 = TREE_CHAIN (parser->implicit_template_parms);
41088 else
41089 parser->implicit_template_parms = new_parm;
41090
41091 tree new_decl = get_local_decls ();
41092 if (non_type)
41093 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41094 new_decl = DECL_INITIAL (new_decl);
41095
41096 /* If creating a fully implicit function template, start the new implicit
41097 template parameter list with this synthesized type, otherwise grow the
41098 current template parameter list. */
41099
41100 if (become_template)
41101 {
41102 parent_scope->level_chain = current_binding_level;
41103
41104 tree new_parms = make_tree_vec (1);
41105 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
41106 current_template_parms = tree_cons (size_int (processing_template_decl),
41107 new_parms, current_template_parms);
41108 }
41109 else
41110 {
41111 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41112 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
41113 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
41114 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
41115 }
41116
41117 // If the new parameter was constrained, we need to add that to the
41118 // constraints in the template parameter list.
41119 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
41120 {
41121 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
41122 reqs = conjoin_constraints (reqs, req);
41123 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
41124 }
41125
41126 current_binding_level = entry_scope;
41127
41128 return new_decl;
41129 }
41130
41131 /* Finish the declaration of a fully implicit function template. Such a
41132 template has no explicit template parameter list so has not been through the
41133 normal template head and tail processing. synthesize_implicit_template_parm
41134 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41135 provided if the declaration is a class member such that its template
41136 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41137 form is returned. Otherwise NULL_TREE is returned. */
41138
41139 static tree
41140 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
41141 {
41142 gcc_assert (parser->fully_implicit_function_template_p);
41143
41144 if (member_decl_opt && member_decl_opt != error_mark_node
41145 && DECL_VIRTUAL_P (member_decl_opt))
41146 {
41147 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
41148 "implicit templates may not be %<virtual%>");
41149 DECL_VIRTUAL_P (member_decl_opt) = false;
41150 }
41151
41152 if (member_decl_opt)
41153 member_decl_opt = finish_member_template_decl (member_decl_opt);
41154 end_template_decl ();
41155
41156 parser->fully_implicit_function_template_p = false;
41157 parser->implicit_template_parms = 0;
41158 parser->implicit_template_scope = 0;
41159 --parser->num_template_parameter_lists;
41160
41161 return member_decl_opt;
41162 }
41163
41164 /* Like finish_fully_implicit_template, but to be used in error
41165 recovery, rearranging scopes so that we restore the state we had
41166 before synthesize_implicit_template_parm inserted the implement
41167 template parms scope. */
41168
41169 static void
41170 abort_fully_implicit_template (cp_parser *parser)
41171 {
41172 cp_binding_level *return_to_scope = current_binding_level;
41173
41174 if (parser->implicit_template_scope
41175 && return_to_scope != parser->implicit_template_scope)
41176 {
41177 cp_binding_level *child = return_to_scope;
41178 for (cp_binding_level *scope = child->level_chain;
41179 scope != parser->implicit_template_scope;
41180 scope = child->level_chain)
41181 child = scope;
41182 child->level_chain = parser->implicit_template_scope->level_chain;
41183 parser->implicit_template_scope->level_chain = return_to_scope;
41184 current_binding_level = parser->implicit_template_scope;
41185 }
41186 else
41187 return_to_scope = return_to_scope->level_chain;
41188
41189 finish_fully_implicit_template (parser, NULL);
41190
41191 gcc_assert (current_binding_level == return_to_scope);
41192 }
41193
41194 /* Helper function for diagnostics that have complained about things
41195 being used with 'extern "C"' linkage.
41196
41197 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41198
41199 void
41200 maybe_show_extern_c_location (void)
41201 {
41202 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
41203 inform (the_parser->innermost_linkage_specification_location,
41204 "%<extern \"C\"%> linkage started here");
41205 }
41206
41207 #include "gt-cp-parser.h"