]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
[C, C++] Use correct location information for OpenACC shape and simple clauses
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2019 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #define INCLUDE_UNIQUE_PTR
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48
49 \f
50 /* The lexer. */
51
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
54
55 static cp_token eof_token =
56 {
57 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 };
59
60 /* The various kinds of non integral constant we encounter. */
61 enum non_integral_constant {
62 NIC_NONE,
63 /* floating-point literal */
64 NIC_FLOAT,
65 /* %<this%> */
66 NIC_THIS,
67 /* %<__FUNCTION__%> */
68 NIC_FUNC_NAME,
69 /* %<__PRETTY_FUNCTION__%> */
70 NIC_PRETTY_FUNC,
71 /* %<__func__%> */
72 NIC_C99_FUNC,
73 /* "%<va_arg%> */
74 NIC_VA_ARG,
75 /* a cast */
76 NIC_CAST,
77 /* %<typeid%> operator */
78 NIC_TYPEID,
79 /* non-constant compound literals */
80 NIC_NCC,
81 /* a function call */
82 NIC_FUNC_CALL,
83 /* an increment */
84 NIC_INC,
85 /* an decrement */
86 NIC_DEC,
87 /* an array reference */
88 NIC_ARRAY_REF,
89 /* %<->%> */
90 NIC_ARROW,
91 /* %<.%> */
92 NIC_POINT,
93 /* the address of a label */
94 NIC_ADDR_LABEL,
95 /* %<*%> */
96 NIC_STAR,
97 /* %<&%> */
98 NIC_ADDR,
99 /* %<++%> */
100 NIC_PREINCREMENT,
101 /* %<--%> */
102 NIC_PREDECREMENT,
103 /* %<new%> */
104 NIC_NEW,
105 /* %<delete%> */
106 NIC_DEL,
107 /* calls to overloaded operators */
108 NIC_OVERLOADED,
109 /* an assignment */
110 NIC_ASSIGNMENT,
111 /* a comma operator */
112 NIC_COMMA,
113 /* a call to a constructor */
114 NIC_CONSTRUCTOR,
115 /* a transaction expression */
116 NIC_TRANSACTION
117 };
118
119 /* The various kinds of errors about name-lookup failing. */
120 enum name_lookup_error {
121 /* NULL */
122 NLE_NULL,
123 /* is not a type */
124 NLE_TYPE,
125 /* is not a class or namespace */
126 NLE_CXX98,
127 /* is not a class, namespace, or enumeration */
128 NLE_NOT_CXX98
129 };
130
131 /* The various kinds of required token */
132 enum required_token {
133 RT_NONE,
134 RT_SEMICOLON, /* ';' */
135 RT_OPEN_PAREN, /* '(' */
136 RT_CLOSE_BRACE, /* '}' */
137 RT_OPEN_BRACE, /* '{' */
138 RT_CLOSE_SQUARE, /* ']' */
139 RT_OPEN_SQUARE, /* '[' */
140 RT_COMMA, /* ',' */
141 RT_SCOPE, /* '::' */
142 RT_LESS, /* '<' */
143 RT_GREATER, /* '>' */
144 RT_EQ, /* '=' */
145 RT_ELLIPSIS, /* '...' */
146 RT_MULT, /* '*' */
147 RT_COMPL, /* '~' */
148 RT_COLON, /* ':' */
149 RT_COLON_SCOPE, /* ':' or '::' */
150 RT_CLOSE_PAREN, /* ')' */
151 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
152 RT_PRAGMA_EOL, /* end of line */
153 RT_NAME, /* identifier */
154
155 /* The type is CPP_KEYWORD */
156 RT_NEW, /* new */
157 RT_DELETE, /* delete */
158 RT_RETURN, /* return */
159 RT_WHILE, /* while */
160 RT_EXTERN, /* extern */
161 RT_STATIC_ASSERT, /* static_assert */
162 RT_DECLTYPE, /* decltype */
163 RT_OPERATOR, /* operator */
164 RT_CLASS, /* class */
165 RT_TEMPLATE, /* template */
166 RT_NAMESPACE, /* namespace */
167 RT_USING, /* using */
168 RT_ASM, /* asm */
169 RT_TRY, /* try */
170 RT_CATCH, /* catch */
171 RT_THROW, /* throw */
172 RT_LABEL, /* __label__ */
173 RT_AT_TRY, /* @try */
174 RT_AT_SYNCHRONIZED, /* @synchronized */
175 RT_AT_THROW, /* @throw */
176
177 RT_SELECT, /* selection-statement */
178 RT_ITERATION, /* iteration-statement */
179 RT_JUMP, /* jump-statement */
180 RT_CLASS_KEY, /* class-key */
181 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
182 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
183 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
184 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 };
186
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
189
190 class type_id_in_expr_sentinel
191 {
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
201 };
202
203 /* Prototypes. */
204
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (cp_lexer *, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
247
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
250
251 static void cp_parser_initial_pragma
252 (cp_token *);
253
254 static bool cp_parser_omp_declare_reduction_exprs
255 (tree, cp_parser *);
256 static void cp_finalize_oacc_routine
257 (cp_parser *, tree, bool);
258
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
262
263 /* Variables. */
264
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream;
267
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand;
271
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
277
278 static void
279 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
280 cp_token *start_token, unsigned num,
281 cp_token *curr_token)
282 {
283 unsigned i, nprinted;
284 cp_token *token;
285 bool do_print;
286
287 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
288
289 if (buffer == NULL)
290 return;
291
292 if (num == 0)
293 num = buffer->length ();
294
295 if (start_token == NULL)
296 start_token = buffer->address ();
297
298 if (start_token > buffer->address ())
299 {
300 cp_lexer_print_token (file, &(*buffer)[0]);
301 fprintf (file, " ... ");
302 }
303
304 do_print = false;
305 nprinted = 0;
306 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
307 {
308 if (token == start_token)
309 do_print = true;
310
311 if (!do_print)
312 continue;
313
314 nprinted++;
315 if (token == curr_token)
316 fprintf (file, "[[");
317
318 cp_lexer_print_token (file, token);
319
320 if (token == curr_token)
321 fprintf (file, "]]");
322
323 switch (token->type)
324 {
325 case CPP_SEMICOLON:
326 case CPP_OPEN_BRACE:
327 case CPP_CLOSE_BRACE:
328 case CPP_EOF:
329 fputc ('\n', file);
330 break;
331
332 default:
333 fputc (' ', file);
334 }
335 }
336
337 if (i == num && i < buffer->length ())
338 {
339 fprintf (file, " ... ");
340 cp_lexer_print_token (file, &buffer->last ());
341 }
342
343 fprintf (file, "\n");
344 }
345
346
347 /* Dump all tokens in BUFFER to stderr. */
348
349 void
350 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
351 {
352 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
353 }
354
355 DEBUG_FUNCTION void
356 debug (vec<cp_token, va_gc> &ref)
357 {
358 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
359 }
360
361 DEBUG_FUNCTION void
362 debug (vec<cp_token, va_gc> *ptr)
363 {
364 if (ptr)
365 debug (*ptr);
366 else
367 fprintf (stderr, "<nil>\n");
368 }
369
370
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
373
374 static void
375 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
376 {
377 if (t)
378 {
379 fprintf (file, "%s: ", desc);
380 print_node_brief (file, "", t, 0);
381 }
382 }
383
384
385 /* Dump parser context C to FILE. */
386
387 static void
388 cp_debug_print_context (FILE *file, cp_parser_context *c)
389 {
390 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
392 print_node_brief (file, "", c->object_type, 0);
393 fprintf (file, "}\n");
394 }
395
396
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
398
399 static void
400 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
401 {
402 unsigned i;
403 cp_parser_context *c;
404
405 fprintf (file, "Parsing context stack:\n");
406 for (i = 0, c = first; c; c = c->next, i++)
407 {
408 fprintf (file, "\t#%u: ", i);
409 cp_debug_print_context (file, c);
410 }
411 }
412
413
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
415
416 static void
417 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
418 {
419 if (flag)
420 fprintf (file, "%s: true\n", desc);
421 }
422
423
424 /* Print an unparsed function entry UF to FILE. */
425
426 static void
427 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
428 {
429 unsigned i;
430 cp_default_arg_entry *default_arg_fn;
431 tree fn;
432
433 fprintf (file, "\tFunctions with default args:\n");
434 for (i = 0;
435 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
436 i++)
437 {
438 fprintf (file, "\t\tClass type: ");
439 print_node_brief (file, "", default_arg_fn->class_type, 0);
440 fprintf (file, "\t\tDeclaration: ");
441 print_node_brief (file, "", default_arg_fn->decl, 0);
442 fprintf (file, "\n");
443 }
444
445 fprintf (file, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
448 {
449 print_node_brief (file, "", fn, 0);
450 fprintf (file, " ");
451 }
452 fprintf (file, "\n");
453
454 fprintf (file, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
457 {
458 print_node_brief (file, "", fn, 0);
459 fprintf (file, " ");
460 }
461 fprintf (file, "\n");
462 }
463
464
465 /* Print the stack of unparsed member functions S to FILE. */
466
467 static void
468 cp_debug_print_unparsed_queues (FILE *file,
469 vec<cp_unparsed_functions_entry, va_gc> *s)
470 {
471 unsigned i;
472 cp_unparsed_functions_entry *uf;
473
474 fprintf (file, "Unparsed functions\n");
475 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
476 {
477 fprintf (file, "#%u:\n", i);
478 cp_debug_print_unparsed_function (file, uf);
479 }
480 }
481
482
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
485
486 static void
487 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
488 {
489 cp_token *next_token, *first_token, *start_token;
490
491 if (file == NULL)
492 file = stderr;
493
494 next_token = parser->lexer->next_token;
495 first_token = parser->lexer->buffer->address ();
496 start_token = (next_token > first_token + window_size / 2)
497 ? next_token - window_size / 2
498 : first_token;
499 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
500 next_token);
501 }
502
503
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
506
507 void
508 cp_debug_parser (FILE *file, cp_parser *parser)
509 {
510 const size_t window_size = 20;
511 cp_token *token;
512 expanded_location eloc;
513
514 if (file == NULL)
515 file = stderr;
516
517 fprintf (file, "Parser state\n\n");
518 fprintf (file, "Number of tokens: %u\n",
519 vec_safe_length (parser->lexer->buffer));
520 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
521 cp_debug_print_tree_if_set (file, "Object scope",
522 parser->object_scope);
523 cp_debug_print_tree_if_set (file, "Qualifying scope",
524 parser->qualifying_scope);
525 cp_debug_print_context_stack (file, parser->context);
526 cp_debug_print_flag (file, "Allow GNU extensions",
527 parser->allow_gnu_extensions_p);
528 cp_debug_print_flag (file, "'>' token is greater-than",
529 parser->greater_than_is_operator_p);
530 cp_debug_print_flag (file, "Default args allowed in current "
531 "parameter list", parser->default_arg_ok_p);
532 cp_debug_print_flag (file, "Parsing integral constant-expression",
533 parser->integral_constant_expression_p);
534 cp_debug_print_flag (file, "Allow non-constant expression in current "
535 "constant-expression",
536 parser->allow_non_integral_constant_expression_p);
537 cp_debug_print_flag (file, "Seen non-constant expression",
538 parser->non_integral_constant_expression_p);
539 cp_debug_print_flag (file, "Local names forbidden in current context",
540 (parser->local_variables_forbidden_p
541 & LOCAL_VARS_FORBIDDEN));
542 cp_debug_print_flag (file, "'this' forbidden in current context",
543 (parser->local_variables_forbidden_p
544 & THIS_FORBIDDEN));
545 cp_debug_print_flag (file, "In unbraced linkage specification",
546 parser->in_unbraced_linkage_specification_p);
547 cp_debug_print_flag (file, "Parsing a declarator",
548 parser->in_declarator_p);
549 cp_debug_print_flag (file, "In template argument list",
550 parser->in_template_argument_list_p);
551 cp_debug_print_flag (file, "Parsing an iteration statement",
552 parser->in_statement & IN_ITERATION_STMT);
553 cp_debug_print_flag (file, "Parsing a switch statement",
554 parser->in_statement & IN_SWITCH_STMT);
555 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
556 parser->in_statement & IN_OMP_BLOCK);
557 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
558 parser->in_statement & IN_OMP_FOR);
559 cp_debug_print_flag (file, "Parsing an if statement",
560 parser->in_statement & IN_IF_STMT);
561 cp_debug_print_flag (file, "Parsing a type-id in an expression "
562 "context", parser->in_type_id_in_expr_p);
563 cp_debug_print_flag (file, "String expressions should be translated "
564 "to execution character set",
565 parser->translate_strings_p);
566 cp_debug_print_flag (file, "Parsing function body outside of a "
567 "local class", parser->in_function_body);
568 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
569 parser->colon_corrects_to_scope_p);
570 cp_debug_print_flag (file, "Colon doesn't start a class definition",
571 parser->colon_doesnt_start_class_def_p);
572 if (parser->type_definition_forbidden_message)
573 fprintf (file, "Error message for forbidden type definitions: %s\n",
574 parser->type_definition_forbidden_message);
575 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
576 fprintf (file, "Number of class definitions in progress: %u\n",
577 parser->num_classes_being_defined);
578 fprintf (file, "Number of template parameter lists for the current "
579 "declaration: %u\n", parser->num_template_parameter_lists);
580 cp_debug_parser_tokens (file, parser, window_size);
581 token = parser->lexer->next_token;
582 fprintf (file, "Next token to parse:\n");
583 fprintf (file, "\tToken: ");
584 cp_lexer_print_token (file, token);
585 eloc = expand_location (token->location);
586 fprintf (file, "\n\tFile: %s\n", eloc.file);
587 fprintf (file, "\tLine: %d\n", eloc.line);
588 fprintf (file, "\tColumn: %d\n", eloc.column);
589 }
590
591 DEBUG_FUNCTION void
592 debug (cp_parser &ref)
593 {
594 cp_debug_parser (stderr, &ref);
595 }
596
597 DEBUG_FUNCTION void
598 debug (cp_parser *ptr)
599 {
600 if (ptr)
601 debug (*ptr);
602 else
603 fprintf (stderr, "<nil>\n");
604 }
605
606 /* Allocate memory for a new lexer object and return it. */
607
608 static cp_lexer *
609 cp_lexer_alloc (void)
610 {
611 cp_lexer *lexer;
612
613 c_common_no_more_pch ();
614
615 /* Allocate the memory. */
616 lexer = ggc_cleared_alloc<cp_lexer> ();
617
618 /* Initially we are not debugging. */
619 lexer->debugging_p = false;
620
621 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
622
623 /* Create the buffer. */
624 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
625
626 return lexer;
627 }
628
629
630 /* Create a new main C++ lexer, the lexer that gets tokens from the
631 preprocessor. */
632
633 static cp_lexer *
634 cp_lexer_new_main (void)
635 {
636 cp_lexer *lexer;
637 cp_token token;
638
639 /* It's possible that parsing the first pragma will load a PCH file,
640 which is a GC collection point. So we have to do that before
641 allocating any memory. */
642 cp_parser_initial_pragma (&token);
643
644 lexer = cp_lexer_alloc ();
645
646 /* Put the first token in the buffer. */
647 lexer->buffer->quick_push (token);
648
649 /* Get the remaining tokens from the preprocessor. */
650 while (token.type != CPP_EOF)
651 {
652 cp_lexer_get_preprocessor_token (lexer, &token);
653 vec_safe_push (lexer->buffer, token);
654 }
655
656 lexer->last_token = lexer->buffer->address ()
657 + lexer->buffer->length ()
658 - 1;
659 lexer->next_token = lexer->buffer->length ()
660 ? lexer->buffer->address ()
661 : &eof_token;
662
663 /* Subsequent preprocessor diagnostics should use compiler
664 diagnostic functions to get the compiler source location. */
665 done_lexing = true;
666
667 gcc_assert (!lexer->next_token->purged_p);
668 return lexer;
669 }
670
671 /* Create a new lexer whose token stream is primed with the tokens in
672 CACHE. When these tokens are exhausted, no new tokens will be read. */
673
674 static cp_lexer *
675 cp_lexer_new_from_tokens (cp_token_cache *cache)
676 {
677 cp_token *first = cache->first;
678 cp_token *last = cache->last;
679 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
680
681 /* We do not own the buffer. */
682 lexer->buffer = NULL;
683 lexer->next_token = first == last ? &eof_token : first;
684 lexer->last_token = last;
685
686 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
687
688 /* Initially we are not debugging. */
689 lexer->debugging_p = false;
690
691 gcc_assert (!lexer->next_token->purged_p);
692 return lexer;
693 }
694
695 /* Frees all resources associated with LEXER. */
696
697 static void
698 cp_lexer_destroy (cp_lexer *lexer)
699 {
700 vec_free (lexer->buffer);
701 lexer->saved_tokens.release ();
702 ggc_free (lexer);
703 }
704
705 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
706 be used. The point of this flag is to help the compiler to fold away calls
707 to cp_lexer_debugging_p within this source file at compile time, when the
708 lexer is not being debugged. */
709
710 #define LEXER_DEBUGGING_ENABLED_P false
711
712 /* Returns nonzero if debugging information should be output. */
713
714 static inline bool
715 cp_lexer_debugging_p (cp_lexer *lexer)
716 {
717 if (!LEXER_DEBUGGING_ENABLED_P)
718 return false;
719
720 return lexer->debugging_p;
721 }
722
723
724 static inline cp_token_position
725 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
726 {
727 gcc_assert (!previous_p || lexer->next_token != &eof_token);
728
729 return lexer->next_token - previous_p;
730 }
731
732 static inline cp_token *
733 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
734 {
735 return pos;
736 }
737
738 static inline void
739 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
740 {
741 lexer->next_token = cp_lexer_token_at (lexer, pos);
742 }
743
744 static inline cp_token_position
745 cp_lexer_previous_token_position (cp_lexer *lexer)
746 {
747 if (lexer->next_token == &eof_token)
748 return lexer->last_token - 1;
749 else
750 return cp_lexer_token_position (lexer, true);
751 }
752
753 static inline cp_token *
754 cp_lexer_previous_token (cp_lexer *lexer)
755 {
756 cp_token_position tp = cp_lexer_previous_token_position (lexer);
757
758 /* Skip past purged tokens. */
759 while (tp->purged_p)
760 {
761 gcc_assert (tp != vec_safe_address (lexer->buffer));
762 tp--;
763 }
764
765 return cp_lexer_token_at (lexer, tp);
766 }
767
768 /* nonzero if we are presently saving tokens. */
769
770 static inline int
771 cp_lexer_saving_tokens (const cp_lexer* lexer)
772 {
773 return lexer->saved_tokens.length () != 0;
774 }
775
776 /* Store the next token from the preprocessor in *TOKEN. Return true
777 if we reach EOF. If LEXER is NULL, assume we are handling an
778 initial #pragma pch_preprocess, and thus want the lexer to return
779 processed strings. */
780
781 static void
782 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
783 {
784 static int is_extern_c = 0;
785
786 /* Get a new token from the preprocessor. */
787 token->type
788 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
789 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
790 token->keyword = RID_MAX;
791 token->purged_p = false;
792 token->error_reported = false;
793
794 /* On some systems, some header files are surrounded by an
795 implicit extern "C" block. Set a flag in the token if it
796 comes from such a header. */
797 is_extern_c += pending_lang_change;
798 pending_lang_change = 0;
799 token->implicit_extern_c = is_extern_c > 0;
800
801 /* Check to see if this token is a keyword. */
802 if (token->type == CPP_NAME)
803 {
804 if (IDENTIFIER_KEYWORD_P (token->u.value))
805 {
806 /* Mark this token as a keyword. */
807 token->type = CPP_KEYWORD;
808 /* Record which keyword. */
809 token->keyword = C_RID_CODE (token->u.value);
810 }
811 else
812 {
813 if (warn_cxx11_compat
814 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
815 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
816 {
817 /* Warn about the C++0x keyword (but still treat it as
818 an identifier). */
819 warning (OPT_Wc__11_compat,
820 "identifier %qE is a keyword in C++11",
821 token->u.value);
822
823 /* Clear out the C_RID_CODE so we don't warn about this
824 particular identifier-turned-keyword again. */
825 C_SET_RID_CODE (token->u.value, RID_MAX);
826 }
827
828 token->keyword = RID_MAX;
829 }
830 }
831 else if (token->type == CPP_AT_NAME)
832 {
833 /* This only happens in Objective-C++; it must be a keyword. */
834 token->type = CPP_KEYWORD;
835 switch (C_RID_CODE (token->u.value))
836 {
837 /* Replace 'class' with '@class', 'private' with '@private',
838 etc. This prevents confusion with the C++ keyword
839 'class', and makes the tokens consistent with other
840 Objective-C 'AT' keywords. For example '@class' is
841 reported as RID_AT_CLASS which is consistent with
842 '@synchronized', which is reported as
843 RID_AT_SYNCHRONIZED.
844 */
845 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
846 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
847 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
848 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
849 case RID_THROW: token->keyword = RID_AT_THROW; break;
850 case RID_TRY: token->keyword = RID_AT_TRY; break;
851 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
852 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
853 default: token->keyword = C_RID_CODE (token->u.value);
854 }
855 }
856 }
857
858 /* Update the globals input_location and the input file stack from TOKEN. */
859 static inline void
860 cp_lexer_set_source_position_from_token (cp_token *token)
861 {
862 if (token->type != CPP_EOF)
863 {
864 input_location = token->location;
865 }
866 }
867
868 /* Update the globals input_location and the input file stack from LEXER. */
869 static inline void
870 cp_lexer_set_source_position (cp_lexer *lexer)
871 {
872 cp_token *token = cp_lexer_peek_token (lexer);
873 cp_lexer_set_source_position_from_token (token);
874 }
875
876 /* Return a pointer to the next token in the token stream, but do not
877 consume it. */
878
879 static inline cp_token *
880 cp_lexer_peek_token (cp_lexer *lexer)
881 {
882 if (cp_lexer_debugging_p (lexer))
883 {
884 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
885 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
886 putc ('\n', cp_lexer_debug_stream);
887 }
888 return lexer->next_token;
889 }
890
891 /* Return true if the next token has the indicated TYPE. */
892
893 static inline bool
894 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
895 {
896 return cp_lexer_peek_token (lexer)->type == type;
897 }
898
899 /* Return true if the next token does not have the indicated TYPE. */
900
901 static inline bool
902 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
903 {
904 return !cp_lexer_next_token_is (lexer, type);
905 }
906
907 /* Return true if the next token is the indicated KEYWORD. */
908
909 static inline bool
910 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
911 {
912 return cp_lexer_peek_token (lexer)->keyword == keyword;
913 }
914
915 static inline bool
916 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
917 {
918 return cp_lexer_peek_nth_token (lexer, n)->type == type;
919 }
920
921 static inline bool
922 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
923 {
924 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
925 }
926
927 /* Return true if KEYWORD can start a decl-specifier. */
928
929 bool
930 cp_keyword_starts_decl_specifier_p (enum rid keyword)
931 {
932 switch (keyword)
933 {
934 /* auto specifier: storage-class-specifier in C++,
935 simple-type-specifier in C++0x. */
936 case RID_AUTO:
937 /* Storage classes. */
938 case RID_REGISTER:
939 case RID_STATIC:
940 case RID_EXTERN:
941 case RID_MUTABLE:
942 case RID_THREAD:
943 /* Elaborated type specifiers. */
944 case RID_ENUM:
945 case RID_CLASS:
946 case RID_STRUCT:
947 case RID_UNION:
948 case RID_TYPENAME:
949 /* Simple type specifiers. */
950 case RID_CHAR:
951 case RID_CHAR8:
952 case RID_CHAR16:
953 case RID_CHAR32:
954 case RID_WCHAR:
955 case RID_BOOL:
956 case RID_SHORT:
957 case RID_INT:
958 case RID_LONG:
959 case RID_SIGNED:
960 case RID_UNSIGNED:
961 case RID_FLOAT:
962 case RID_DOUBLE:
963 case RID_VOID:
964 /* GNU extensions. */
965 case RID_ATTRIBUTE:
966 case RID_TYPEOF:
967 /* C++0x extensions. */
968 case RID_DECLTYPE:
969 case RID_UNDERLYING_TYPE:
970 case RID_CONSTEXPR:
971 return true;
972
973 default:
974 if (keyword >= RID_FIRST_INT_N
975 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
976 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
977 return true;
978 return false;
979 }
980 }
981
982 /* Return true if the next token is a keyword for a decl-specifier. */
983
984 static bool
985 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
986 {
987 cp_token *token;
988
989 token = cp_lexer_peek_token (lexer);
990 return cp_keyword_starts_decl_specifier_p (token->keyword);
991 }
992
993 /* Returns TRUE iff the token T begins a decltype type. */
994
995 static bool
996 token_is_decltype (cp_token *t)
997 {
998 return (t->keyword == RID_DECLTYPE
999 || t->type == CPP_DECLTYPE);
1000 }
1001
1002 /* Returns TRUE iff the next token begins a decltype type. */
1003
1004 static bool
1005 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1006 {
1007 cp_token *t = cp_lexer_peek_token (lexer);
1008 return token_is_decltype (t);
1009 }
1010
1011 /* Called when processing a token with tree_check_value; perform or defer the
1012 associated checks and return the value. */
1013
1014 static tree
1015 saved_checks_value (struct tree_check *check_value)
1016 {
1017 /* Perform any access checks that were deferred. */
1018 vec<deferred_access_check, va_gc> *checks;
1019 deferred_access_check *chk;
1020 checks = check_value->checks;
1021 if (checks)
1022 {
1023 int i;
1024 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1025 perform_or_defer_access_check (chk->binfo,
1026 chk->decl,
1027 chk->diag_decl, tf_warning_or_error);
1028 }
1029 /* Return the stored value. */
1030 return check_value->value;
1031 }
1032
1033 /* Return a pointer to the Nth token in the token stream. If N is 1,
1034 then this is precisely equivalent to cp_lexer_peek_token (except
1035 that it is not inline). One would like to disallow that case, but
1036 there is one case (cp_parser_nth_token_starts_template_id) where
1037 the caller passes a variable for N and it might be 1. */
1038
1039 static cp_token *
1040 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1041 {
1042 cp_token *token;
1043
1044 /* N is 1-based, not zero-based. */
1045 gcc_assert (n > 0);
1046
1047 if (cp_lexer_debugging_p (lexer))
1048 fprintf (cp_lexer_debug_stream,
1049 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1050
1051 --n;
1052 token = lexer->next_token;
1053 gcc_assert (!n || token != &eof_token);
1054 while (n != 0)
1055 {
1056 ++token;
1057 if (token == lexer->last_token)
1058 {
1059 token = &eof_token;
1060 break;
1061 }
1062
1063 if (!token->purged_p)
1064 --n;
1065 }
1066
1067 if (cp_lexer_debugging_p (lexer))
1068 {
1069 cp_lexer_print_token (cp_lexer_debug_stream, token);
1070 putc ('\n', cp_lexer_debug_stream);
1071 }
1072
1073 return token;
1074 }
1075
1076 /* Return the next token, and advance the lexer's next_token pointer
1077 to point to the next non-purged token. */
1078
1079 static cp_token *
1080 cp_lexer_consume_token (cp_lexer* lexer)
1081 {
1082 cp_token *token = lexer->next_token;
1083
1084 gcc_assert (token != &eof_token);
1085 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1086
1087 do
1088 {
1089 lexer->next_token++;
1090 if (lexer->next_token == lexer->last_token)
1091 {
1092 lexer->next_token = &eof_token;
1093 break;
1094 }
1095
1096 }
1097 while (lexer->next_token->purged_p);
1098
1099 cp_lexer_set_source_position_from_token (token);
1100
1101 /* Provide debugging output. */
1102 if (cp_lexer_debugging_p (lexer))
1103 {
1104 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1105 cp_lexer_print_token (cp_lexer_debug_stream, token);
1106 putc ('\n', cp_lexer_debug_stream);
1107 }
1108
1109 return token;
1110 }
1111
1112 /* Permanently remove the next token from the token stream, and
1113 advance the next_token pointer to refer to the next non-purged
1114 token. */
1115
1116 static void
1117 cp_lexer_purge_token (cp_lexer *lexer)
1118 {
1119 cp_token *tok = lexer->next_token;
1120
1121 gcc_assert (tok != &eof_token);
1122 tok->purged_p = true;
1123 tok->location = UNKNOWN_LOCATION;
1124 tok->u.value = NULL_TREE;
1125 tok->keyword = RID_MAX;
1126
1127 do
1128 {
1129 tok++;
1130 if (tok == lexer->last_token)
1131 {
1132 tok = &eof_token;
1133 break;
1134 }
1135 }
1136 while (tok->purged_p);
1137 lexer->next_token = tok;
1138 }
1139
1140 /* Permanently remove all tokens after TOK, up to, but not
1141 including, the token that will be returned next by
1142 cp_lexer_peek_token. */
1143
1144 static void
1145 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1146 {
1147 cp_token *peek = lexer->next_token;
1148
1149 if (peek == &eof_token)
1150 peek = lexer->last_token;
1151
1152 gcc_assert (tok < peek);
1153
1154 for ( tok += 1; tok != peek; tok += 1)
1155 {
1156 tok->purged_p = true;
1157 tok->location = UNKNOWN_LOCATION;
1158 tok->u.value = NULL_TREE;
1159 tok->keyword = RID_MAX;
1160 }
1161 }
1162
1163 /* Begin saving tokens. All tokens consumed after this point will be
1164 preserved. */
1165
1166 static void
1167 cp_lexer_save_tokens (cp_lexer* lexer)
1168 {
1169 /* Provide debugging output. */
1170 if (cp_lexer_debugging_p (lexer))
1171 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1172
1173 lexer->saved_tokens.safe_push (lexer->next_token);
1174 }
1175
1176 /* Commit to the portion of the token stream most recently saved. */
1177
1178 static void
1179 cp_lexer_commit_tokens (cp_lexer* lexer)
1180 {
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1184
1185 lexer->saved_tokens.pop ();
1186 }
1187
1188 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1189 to the token stream. Stop saving tokens. */
1190
1191 static void
1192 cp_lexer_rollback_tokens (cp_lexer* lexer)
1193 {
1194 /* Provide debugging output. */
1195 if (cp_lexer_debugging_p (lexer))
1196 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1197
1198 lexer->next_token = lexer->saved_tokens.pop ();
1199 }
1200
1201 /* RAII wrapper around the above functions, with sanity checking. Creating
1202 a variable saves tokens, which are committed when the variable is
1203 destroyed unless they are explicitly rolled back by calling the rollback
1204 member function. */
1205
1206 struct saved_token_sentinel
1207 {
1208 cp_lexer *lexer;
1209 unsigned len;
1210 bool commit;
1211 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1212 {
1213 len = lexer->saved_tokens.length ();
1214 cp_lexer_save_tokens (lexer);
1215 }
1216 void rollback ()
1217 {
1218 cp_lexer_rollback_tokens (lexer);
1219 commit = false;
1220 }
1221 ~saved_token_sentinel()
1222 {
1223 if (commit)
1224 cp_lexer_commit_tokens (lexer);
1225 gcc_assert (lexer->saved_tokens.length () == len);
1226 }
1227 };
1228
1229 /* Print a representation of the TOKEN on the STREAM. */
1230
1231 static void
1232 cp_lexer_print_token (FILE * stream, cp_token *token)
1233 {
1234 /* We don't use cpp_type2name here because the parser defines
1235 a few tokens of its own. */
1236 static const char *const token_names[] = {
1237 /* cpplib-defined token types */
1238 #define OP(e, s) #e,
1239 #define TK(e, s) #e,
1240 TTYPE_TABLE
1241 #undef OP
1242 #undef TK
1243 /* C++ parser token types - see "Manifest constants", above. */
1244 "KEYWORD",
1245 "TEMPLATE_ID",
1246 "NESTED_NAME_SPECIFIER",
1247 };
1248
1249 /* For some tokens, print the associated data. */
1250 switch (token->type)
1251 {
1252 case CPP_KEYWORD:
1253 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1254 For example, `struct' is mapped to an INTEGER_CST. */
1255 if (!identifier_p (token->u.value))
1256 break;
1257 /* fall through */
1258 case CPP_NAME:
1259 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1260 break;
1261
1262 case CPP_STRING:
1263 case CPP_STRING16:
1264 case CPP_STRING32:
1265 case CPP_WSTRING:
1266 case CPP_UTF8STRING:
1267 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1268 break;
1269
1270 case CPP_NUMBER:
1271 print_generic_expr (stream, token->u.value);
1272 break;
1273
1274 default:
1275 /* If we have a name for the token, print it out. Otherwise, we
1276 simply give the numeric code. */
1277 if (token->type < ARRAY_SIZE(token_names))
1278 fputs (token_names[token->type], stream);
1279 else
1280 fprintf (stream, "[%d]", token->type);
1281 break;
1282 }
1283 }
1284
1285 DEBUG_FUNCTION void
1286 debug (cp_token &ref)
1287 {
1288 cp_lexer_print_token (stderr, &ref);
1289 fprintf (stderr, "\n");
1290 }
1291
1292 DEBUG_FUNCTION void
1293 debug (cp_token *ptr)
1294 {
1295 if (ptr)
1296 debug (*ptr);
1297 else
1298 fprintf (stderr, "<nil>\n");
1299 }
1300
1301
1302 /* Start emitting debugging information. */
1303
1304 static void
1305 cp_lexer_start_debugging (cp_lexer* lexer)
1306 {
1307 if (!LEXER_DEBUGGING_ENABLED_P)
1308 fatal_error (input_location,
1309 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1310
1311 lexer->debugging_p = true;
1312 cp_lexer_debug_stream = stderr;
1313 }
1314
1315 /* Stop emitting debugging information. */
1316
1317 static void
1318 cp_lexer_stop_debugging (cp_lexer* lexer)
1319 {
1320 if (!LEXER_DEBUGGING_ENABLED_P)
1321 fatal_error (input_location,
1322 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323
1324 lexer->debugging_p = false;
1325 cp_lexer_debug_stream = NULL;
1326 }
1327
1328 /* Create a new cp_token_cache, representing a range of tokens. */
1329
1330 static cp_token_cache *
1331 cp_token_cache_new (cp_token *first, cp_token *last)
1332 {
1333 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1334 cache->first = first;
1335 cache->last = last;
1336 return cache;
1337 }
1338
1339 /* Diagnose if #pragma omp declare simd isn't followed immediately
1340 by function declaration or definition. */
1341
1342 static inline void
1343 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1344 {
1345 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1346 {
1347 error ("%<#pragma omp declare simd%> not immediately followed by "
1348 "function declaration or definition");
1349 parser->omp_declare_simd = NULL;
1350 }
1351 }
1352
1353 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1354 and put that into "omp declare simd" attribute. */
1355
1356 static inline void
1357 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1358 {
1359 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1360 {
1361 if (fndecl == error_mark_node)
1362 {
1363 parser->omp_declare_simd = NULL;
1364 return;
1365 }
1366 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1367 {
1368 cp_ensure_no_omp_declare_simd (parser);
1369 return;
1370 }
1371 }
1372 }
1373
1374 /* Diagnose if #pragma acc routine isn't followed immediately by function
1375 declaration or definition. */
1376
1377 static inline void
1378 cp_ensure_no_oacc_routine (cp_parser *parser)
1379 {
1380 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1381 {
1382 error_at (parser->oacc_routine->loc,
1383 "%<#pragma acc routine%> not immediately followed by "
1384 "function declaration or definition");
1385 parser->oacc_routine = NULL;
1386 }
1387 }
1388 \f
1389 /* Decl-specifiers. */
1390
1391 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1392
1393 static void
1394 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1395 {
1396 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1397 }
1398
1399 /* Declarators. */
1400
1401 /* Nothing other than the parser should be creating declarators;
1402 declarators are a semi-syntactic representation of C++ entities.
1403 Other parts of the front end that need to create entities (like
1404 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1405
1406 static cp_declarator *make_call_declarator
1407 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1408 static cp_declarator *make_array_declarator
1409 (cp_declarator *, tree);
1410 static cp_declarator *make_pointer_declarator
1411 (cp_cv_quals, cp_declarator *, tree);
1412 static cp_declarator *make_reference_declarator
1413 (cp_cv_quals, cp_declarator *, bool, tree);
1414 static cp_declarator *make_ptrmem_declarator
1415 (cp_cv_quals, tree, cp_declarator *, tree);
1416
1417 /* An erroneous declarator. */
1418 static cp_declarator *cp_error_declarator;
1419
1420 /* The obstack on which declarators and related data structures are
1421 allocated. */
1422 static struct obstack declarator_obstack;
1423
1424 /* Alloc BYTES from the declarator memory pool. */
1425
1426 static inline void *
1427 alloc_declarator (size_t bytes)
1428 {
1429 return obstack_alloc (&declarator_obstack, bytes);
1430 }
1431
1432 /* Allocate a declarator of the indicated KIND. Clear fields that are
1433 common to all declarators. */
1434
1435 static cp_declarator *
1436 make_declarator (cp_declarator_kind kind)
1437 {
1438 cp_declarator *declarator;
1439
1440 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1441 declarator->kind = kind;
1442 declarator->parenthesized = UNKNOWN_LOCATION;
1443 declarator->attributes = NULL_TREE;
1444 declarator->std_attributes = NULL_TREE;
1445 declarator->declarator = NULL;
1446 declarator->parameter_pack_p = false;
1447 declarator->id_loc = UNKNOWN_LOCATION;
1448
1449 return declarator;
1450 }
1451
1452 /* Make a declarator for a generalized identifier. If
1453 QUALIFYING_SCOPE is non-NULL, the identifier is
1454 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1455 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1456 is, if any. */
1457
1458 static cp_declarator *
1459 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1460 special_function_kind sfk, location_t id_location)
1461 {
1462 cp_declarator *declarator;
1463
1464 /* It is valid to write:
1465
1466 class C { void f(); };
1467 typedef C D;
1468 void D::f();
1469
1470 The standard is not clear about whether `typedef const C D' is
1471 legal; as of 2002-09-15 the committee is considering that
1472 question. EDG 3.0 allows that syntax. Therefore, we do as
1473 well. */
1474 if (qualifying_scope && TYPE_P (qualifying_scope))
1475 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1476
1477 gcc_assert (identifier_p (unqualified_name)
1478 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1479 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1480
1481 declarator = make_declarator (cdk_id);
1482 declarator->u.id.qualifying_scope = qualifying_scope;
1483 declarator->u.id.unqualified_name = unqualified_name;
1484 declarator->u.id.sfk = sfk;
1485 declarator->id_loc = id_location;
1486
1487 return declarator;
1488 }
1489
1490 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1491 of modifiers such as const or volatile to apply to the pointer
1492 type, represented as identifiers. ATTRIBUTES represent the attributes that
1493 appertain to the pointer or reference. */
1494
1495 cp_declarator *
1496 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1497 tree attributes)
1498 {
1499 cp_declarator *declarator;
1500
1501 declarator = make_declarator (cdk_pointer);
1502 declarator->declarator = target;
1503 declarator->u.pointer.qualifiers = cv_qualifiers;
1504 declarator->u.pointer.class_type = NULL_TREE;
1505 if (target)
1506 {
1507 declarator->id_loc = target->id_loc;
1508 declarator->parameter_pack_p = target->parameter_pack_p;
1509 target->parameter_pack_p = false;
1510 }
1511 else
1512 declarator->parameter_pack_p = false;
1513
1514 declarator->std_attributes = attributes;
1515
1516 return declarator;
1517 }
1518
1519 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1520 represent the attributes that appertain to the pointer or
1521 reference. */
1522
1523 cp_declarator *
1524 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1525 bool rvalue_ref, tree attributes)
1526 {
1527 cp_declarator *declarator;
1528
1529 declarator = make_declarator (cdk_reference);
1530 declarator->declarator = target;
1531 declarator->u.reference.qualifiers = cv_qualifiers;
1532 declarator->u.reference.rvalue_ref = rvalue_ref;
1533 if (target)
1534 {
1535 declarator->id_loc = target->id_loc;
1536 declarator->parameter_pack_p = target->parameter_pack_p;
1537 target->parameter_pack_p = false;
1538 }
1539 else
1540 declarator->parameter_pack_p = false;
1541
1542 declarator->std_attributes = attributes;
1543
1544 return declarator;
1545 }
1546
1547 /* Like make_pointer_declarator -- but for a pointer to a non-static
1548 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1549 appertain to the pointer or reference. */
1550
1551 cp_declarator *
1552 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1553 cp_declarator *pointee,
1554 tree attributes)
1555 {
1556 cp_declarator *declarator;
1557
1558 declarator = make_declarator (cdk_ptrmem);
1559 declarator->declarator = pointee;
1560 declarator->u.pointer.qualifiers = cv_qualifiers;
1561 declarator->u.pointer.class_type = class_type;
1562
1563 if (pointee)
1564 {
1565 declarator->parameter_pack_p = pointee->parameter_pack_p;
1566 pointee->parameter_pack_p = false;
1567 }
1568 else
1569 declarator->parameter_pack_p = false;
1570
1571 declarator->std_attributes = attributes;
1572
1573 return declarator;
1574 }
1575
1576 /* Make a declarator for the function given by TARGET, with the
1577 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1578 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1579 indicates what exceptions can be thrown. */
1580
1581 cp_declarator *
1582 make_call_declarator (cp_declarator *target,
1583 tree parms,
1584 cp_cv_quals cv_qualifiers,
1585 cp_virt_specifiers virt_specifiers,
1586 cp_ref_qualifier ref_qualifier,
1587 tree tx_qualifier,
1588 tree exception_specification,
1589 tree late_return_type,
1590 tree requires_clause)
1591 {
1592 cp_declarator *declarator;
1593
1594 declarator = make_declarator (cdk_function);
1595 declarator->declarator = target;
1596 declarator->u.function.parameters = parms;
1597 declarator->u.function.qualifiers = cv_qualifiers;
1598 declarator->u.function.virt_specifiers = virt_specifiers;
1599 declarator->u.function.ref_qualifier = ref_qualifier;
1600 declarator->u.function.tx_qualifier = tx_qualifier;
1601 declarator->u.function.exception_specification = exception_specification;
1602 declarator->u.function.late_return_type = late_return_type;
1603 declarator->u.function.requires_clause = requires_clause;
1604 if (target)
1605 {
1606 declarator->id_loc = target->id_loc;
1607 declarator->parameter_pack_p = target->parameter_pack_p;
1608 target->parameter_pack_p = false;
1609 }
1610 else
1611 declarator->parameter_pack_p = false;
1612
1613 return declarator;
1614 }
1615
1616 /* Make a declarator for an array of BOUNDS elements, each of which is
1617 defined by ELEMENT. */
1618
1619 cp_declarator *
1620 make_array_declarator (cp_declarator *element, tree bounds)
1621 {
1622 cp_declarator *declarator;
1623
1624 declarator = make_declarator (cdk_array);
1625 declarator->declarator = element;
1626 declarator->u.array.bounds = bounds;
1627 if (element)
1628 {
1629 declarator->id_loc = element->id_loc;
1630 declarator->parameter_pack_p = element->parameter_pack_p;
1631 element->parameter_pack_p = false;
1632 }
1633 else
1634 declarator->parameter_pack_p = false;
1635
1636 return declarator;
1637 }
1638
1639 /* Determine whether the declarator we've seen so far can be a
1640 parameter pack, when followed by an ellipsis. */
1641 static bool
1642 declarator_can_be_parameter_pack (cp_declarator *declarator)
1643 {
1644 if (declarator && declarator->parameter_pack_p)
1645 /* We already saw an ellipsis. */
1646 return false;
1647
1648 /* Search for a declarator name, or any other declarator that goes
1649 after the point where the ellipsis could appear in a parameter
1650 pack. If we find any of these, then this declarator cannot be
1651 made into a parameter pack. */
1652 bool found = false;
1653 while (declarator && !found)
1654 {
1655 switch ((int)declarator->kind)
1656 {
1657 case cdk_id:
1658 case cdk_array:
1659 case cdk_decomp:
1660 found = true;
1661 break;
1662
1663 case cdk_error:
1664 return true;
1665
1666 default:
1667 declarator = declarator->declarator;
1668 break;
1669 }
1670 }
1671
1672 return !found;
1673 }
1674
1675 cp_parameter_declarator *no_parameters;
1676
1677 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1678 DECLARATOR and DEFAULT_ARGUMENT. */
1679
1680 cp_parameter_declarator *
1681 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1682 cp_declarator *declarator,
1683 tree default_argument,
1684 location_t loc,
1685 bool template_parameter_pack_p = false)
1686 {
1687 cp_parameter_declarator *parameter;
1688
1689 parameter = ((cp_parameter_declarator *)
1690 alloc_declarator (sizeof (cp_parameter_declarator)));
1691 parameter->next = NULL;
1692 if (decl_specifiers)
1693 parameter->decl_specifiers = *decl_specifiers;
1694 else
1695 clear_decl_specs (&parameter->decl_specifiers);
1696 parameter->declarator = declarator;
1697 parameter->default_argument = default_argument;
1698 parameter->template_parameter_pack_p = template_parameter_pack_p;
1699 parameter->loc = loc;
1700
1701 return parameter;
1702 }
1703
1704 /* Returns true iff DECLARATOR is a declaration for a function. */
1705
1706 static bool
1707 function_declarator_p (const cp_declarator *declarator)
1708 {
1709 while (declarator)
1710 {
1711 if (declarator->kind == cdk_function
1712 && declarator->declarator->kind == cdk_id)
1713 return true;
1714 if (declarator->kind == cdk_id
1715 || declarator->kind == cdk_decomp
1716 || declarator->kind == cdk_error)
1717 return false;
1718 declarator = declarator->declarator;
1719 }
1720 return false;
1721 }
1722
1723 /* The parser. */
1724
1725 /* Overview
1726 --------
1727
1728 A cp_parser parses the token stream as specified by the C++
1729 grammar. Its job is purely parsing, not semantic analysis. For
1730 example, the parser breaks the token stream into declarators,
1731 expressions, statements, and other similar syntactic constructs.
1732 It does not check that the types of the expressions on either side
1733 of an assignment-statement are compatible, or that a function is
1734 not declared with a parameter of type `void'.
1735
1736 The parser invokes routines elsewhere in the compiler to perform
1737 semantic analysis and to build up the abstract syntax tree for the
1738 code processed.
1739
1740 The parser (and the template instantiation code, which is, in a
1741 way, a close relative of parsing) are the only parts of the
1742 compiler that should be calling push_scope and pop_scope, or
1743 related functions. The parser (and template instantiation code)
1744 keeps track of what scope is presently active; everything else
1745 should simply honor that. (The code that generates static
1746 initializers may also need to set the scope, in order to check
1747 access control correctly when emitting the initializers.)
1748
1749 Methodology
1750 -----------
1751
1752 The parser is of the standard recursive-descent variety. Upcoming
1753 tokens in the token stream are examined in order to determine which
1754 production to use when parsing a non-terminal. Some C++ constructs
1755 require arbitrary look ahead to disambiguate. For example, it is
1756 impossible, in the general case, to tell whether a statement is an
1757 expression or declaration without scanning the entire statement.
1758 Therefore, the parser is capable of "parsing tentatively." When the
1759 parser is not sure what construct comes next, it enters this mode.
1760 Then, while we attempt to parse the construct, the parser queues up
1761 error messages, rather than issuing them immediately, and saves the
1762 tokens it consumes. If the construct is parsed successfully, the
1763 parser "commits", i.e., it issues any queued error messages and
1764 the tokens that were being preserved are permanently discarded.
1765 If, however, the construct is not parsed successfully, the parser
1766 rolls back its state completely so that it can resume parsing using
1767 a different alternative.
1768
1769 Future Improvements
1770 -------------------
1771
1772 The performance of the parser could probably be improved substantially.
1773 We could often eliminate the need to parse tentatively by looking ahead
1774 a little bit. In some places, this approach might not entirely eliminate
1775 the need to parse tentatively, but it might still speed up the average
1776 case. */
1777
1778 /* Flags that are passed to some parsing functions. These values can
1779 be bitwise-ored together. */
1780
1781 enum
1782 {
1783 /* No flags. */
1784 CP_PARSER_FLAGS_NONE = 0x0,
1785 /* The construct is optional. If it is not present, then no error
1786 should be issued. */
1787 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1788 /* When parsing a type-specifier, treat user-defined type-names
1789 as non-type identifiers. */
1790 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1791 /* When parsing a type-specifier, do not try to parse a class-specifier
1792 or enum-specifier. */
1793 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1794 /* When parsing a decl-specifier-seq, only allow type-specifier or
1795 constexpr. */
1796 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1797 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1798 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
1799 /* When parsing a decl-specifier-seq, allow missing typename. */
1800 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20
1801 };
1802
1803 /* This type is used for parameters and variables which hold
1804 combinations of the above flags. */
1805 typedef int cp_parser_flags;
1806
1807 /* The different kinds of declarators we want to parse. */
1808
1809 enum cp_parser_declarator_kind
1810 {
1811 /* We want an abstract declarator. */
1812 CP_PARSER_DECLARATOR_ABSTRACT,
1813 /* We want a named declarator. */
1814 CP_PARSER_DECLARATOR_NAMED,
1815 /* We don't mind, but the name must be an unqualified-id. */
1816 CP_PARSER_DECLARATOR_EITHER
1817 };
1818
1819 /* The precedence values used to parse binary expressions. The minimum value
1820 of PREC must be 1, because zero is reserved to quickly discriminate
1821 binary operators from other tokens. */
1822
1823 enum cp_parser_prec
1824 {
1825 PREC_NOT_OPERATOR,
1826 PREC_LOGICAL_OR_EXPRESSION,
1827 PREC_LOGICAL_AND_EXPRESSION,
1828 PREC_INCLUSIVE_OR_EXPRESSION,
1829 PREC_EXCLUSIVE_OR_EXPRESSION,
1830 PREC_AND_EXPRESSION,
1831 PREC_EQUALITY_EXPRESSION,
1832 PREC_RELATIONAL_EXPRESSION,
1833 PREC_SHIFT_EXPRESSION,
1834 PREC_ADDITIVE_EXPRESSION,
1835 PREC_MULTIPLICATIVE_EXPRESSION,
1836 PREC_PM_EXPRESSION,
1837 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1838 };
1839
1840 /* A mapping from a token type to a corresponding tree node type, with a
1841 precedence value. */
1842
1843 struct cp_parser_binary_operations_map_node
1844 {
1845 /* The token type. */
1846 enum cpp_ttype token_type;
1847 /* The corresponding tree code. */
1848 enum tree_code tree_type;
1849 /* The precedence of this operator. */
1850 enum cp_parser_prec prec;
1851 };
1852
1853 struct cp_parser_expression_stack_entry
1854 {
1855 /* Left hand side of the binary operation we are currently
1856 parsing. */
1857 cp_expr lhs;
1858 /* Original tree code for left hand side, if it was a binary
1859 expression itself (used for -Wparentheses). */
1860 enum tree_code lhs_type;
1861 /* Tree code for the binary operation we are parsing. */
1862 enum tree_code tree_type;
1863 /* Precedence of the binary operation we are parsing. */
1864 enum cp_parser_prec prec;
1865 /* Location of the binary operation we are parsing. */
1866 location_t loc;
1867 };
1868
1869 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1870 entries because precedence levels on the stack are monotonically
1871 increasing. */
1872 typedef struct cp_parser_expression_stack_entry
1873 cp_parser_expression_stack[NUM_PREC_VALUES];
1874
1875 /* Prototypes. */
1876
1877 /* Constructors and destructors. */
1878
1879 static cp_parser_context *cp_parser_context_new
1880 (cp_parser_context *);
1881
1882 /* Class variables. */
1883
1884 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1885
1886 /* The operator-precedence table used by cp_parser_binary_expression.
1887 Transformed into an associative array (binops_by_token) by
1888 cp_parser_new. */
1889
1890 static const cp_parser_binary_operations_map_node binops[] = {
1891 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1892 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1893
1894 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1895 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1896 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1897
1898 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1899 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1900
1901 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1902 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1903
1904 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1905 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1906 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1907 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1908
1909 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1910 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1911
1912 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1913
1914 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1915
1916 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1917
1918 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1919
1920 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1921 };
1922
1923 /* The same as binops, but initialized by cp_parser_new so that
1924 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1925 for speed. */
1926 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1927
1928 /* Constructors and destructors. */
1929
1930 /* Construct a new context. The context below this one on the stack
1931 is given by NEXT. */
1932
1933 static cp_parser_context *
1934 cp_parser_context_new (cp_parser_context* next)
1935 {
1936 cp_parser_context *context;
1937
1938 /* Allocate the storage. */
1939 if (cp_parser_context_free_list != NULL)
1940 {
1941 /* Pull the first entry from the free list. */
1942 context = cp_parser_context_free_list;
1943 cp_parser_context_free_list = context->next;
1944 memset (context, 0, sizeof (*context));
1945 }
1946 else
1947 context = ggc_cleared_alloc<cp_parser_context> ();
1948
1949 /* No errors have occurred yet in this context. */
1950 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1951 /* If this is not the bottommost context, copy information that we
1952 need from the previous context. */
1953 if (next)
1954 {
1955 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1956 expression, then we are parsing one in this context, too. */
1957 context->object_type = next->object_type;
1958 /* Thread the stack. */
1959 context->next = next;
1960 }
1961
1962 return context;
1963 }
1964
1965 /* Managing the unparsed function queues. */
1966
1967 #define unparsed_funs_with_default_args \
1968 parser->unparsed_queues->last ().funs_with_default_args
1969 #define unparsed_funs_with_definitions \
1970 parser->unparsed_queues->last ().funs_with_definitions
1971 #define unparsed_nsdmis \
1972 parser->unparsed_queues->last ().nsdmis
1973 #define unparsed_classes \
1974 parser->unparsed_queues->last ().classes
1975
1976 static void
1977 push_unparsed_function_queues (cp_parser *parser)
1978 {
1979 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1980 vec_safe_push (parser->unparsed_queues, e);
1981 }
1982
1983 static void
1984 pop_unparsed_function_queues (cp_parser *parser)
1985 {
1986 release_tree_vector (unparsed_funs_with_definitions);
1987 parser->unparsed_queues->pop ();
1988 }
1989
1990 /* Prototypes. */
1991
1992 /* Constructors and destructors. */
1993
1994 static cp_parser *cp_parser_new
1995 (void);
1996
1997 /* Routines to parse various constructs.
1998
1999 Those that return `tree' will return the error_mark_node (rather
2000 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2001 Sometimes, they will return an ordinary node if error-recovery was
2002 attempted, even though a parse error occurred. So, to check
2003 whether or not a parse error occurred, you should always use
2004 cp_parser_error_occurred. If the construct is optional (indicated
2005 either by an `_opt' in the name of the function that does the
2006 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2007 the construct is not present. */
2008
2009 /* Lexical conventions [gram.lex] */
2010
2011 static cp_expr cp_parser_identifier
2012 (cp_parser *);
2013 static cp_expr cp_parser_string_literal
2014 (cp_parser *, bool, bool, bool);
2015 static cp_expr cp_parser_userdef_char_literal
2016 (cp_parser *);
2017 static tree cp_parser_userdef_string_literal
2018 (tree);
2019 static cp_expr cp_parser_userdef_numeric_literal
2020 (cp_parser *);
2021
2022 /* Basic concepts [gram.basic] */
2023
2024 static void cp_parser_translation_unit (cp_parser *);
2025
2026 /* Expressions [gram.expr] */
2027
2028 static cp_expr cp_parser_primary_expression
2029 (cp_parser *, bool, bool, bool, cp_id_kind *);
2030 static cp_expr cp_parser_id_expression
2031 (cp_parser *, bool, bool, bool *, bool, bool);
2032 static cp_expr cp_parser_unqualified_id
2033 (cp_parser *, bool, bool, bool, bool);
2034 static tree cp_parser_nested_name_specifier_opt
2035 (cp_parser *, bool, bool, bool, bool, bool = false);
2036 static tree cp_parser_nested_name_specifier
2037 (cp_parser *, bool, bool, bool, bool);
2038 static tree cp_parser_qualifying_entity
2039 (cp_parser *, bool, bool, bool, bool, bool);
2040 static cp_expr cp_parser_postfix_expression
2041 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2042 static tree cp_parser_postfix_open_square_expression
2043 (cp_parser *, tree, bool, bool);
2044 static tree cp_parser_postfix_dot_deref_expression
2045 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2046 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2047 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2048 bool = false);
2049 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2050 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2051 static void cp_parser_pseudo_destructor_name
2052 (cp_parser *, tree, tree *, tree *);
2053 static cp_expr cp_parser_unary_expression
2054 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2055 static enum tree_code cp_parser_unary_operator
2056 (cp_token *);
2057 static tree cp_parser_has_attribute_expression
2058 (cp_parser *);
2059 static tree cp_parser_new_expression
2060 (cp_parser *);
2061 static vec<tree, va_gc> *cp_parser_new_placement
2062 (cp_parser *);
2063 static tree cp_parser_new_type_id
2064 (cp_parser *, tree *);
2065 static cp_declarator *cp_parser_new_declarator_opt
2066 (cp_parser *);
2067 static cp_declarator *cp_parser_direct_new_declarator
2068 (cp_parser *);
2069 static vec<tree, va_gc> *cp_parser_new_initializer
2070 (cp_parser *);
2071 static tree cp_parser_delete_expression
2072 (cp_parser *);
2073 static cp_expr cp_parser_cast_expression
2074 (cp_parser *, bool, bool, bool, cp_id_kind *);
2075 static cp_expr cp_parser_binary_expression
2076 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2077 static tree cp_parser_question_colon_clause
2078 (cp_parser *, cp_expr);
2079 static cp_expr cp_parser_assignment_expression
2080 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2081 static enum tree_code cp_parser_assignment_operator_opt
2082 (cp_parser *);
2083 static cp_expr cp_parser_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static cp_expr cp_parser_constant_expression
2086 (cp_parser *, bool = false, bool * = NULL, bool = false);
2087 static cp_expr cp_parser_builtin_offsetof
2088 (cp_parser *);
2089 static cp_expr cp_parser_lambda_expression
2090 (cp_parser *);
2091 static void cp_parser_lambda_introducer
2092 (cp_parser *, tree);
2093 static bool cp_parser_lambda_declarator_opt
2094 (cp_parser *, tree);
2095 static void cp_parser_lambda_body
2096 (cp_parser *, tree);
2097
2098 /* Statements [gram.stmt.stmt] */
2099
2100 static void cp_parser_statement
2101 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2102 static void cp_parser_label_for_labeled_statement
2103 (cp_parser *, tree);
2104 static tree cp_parser_expression_statement
2105 (cp_parser *, tree);
2106 static tree cp_parser_compound_statement
2107 (cp_parser *, tree, int, bool);
2108 static void cp_parser_statement_seq_opt
2109 (cp_parser *, tree);
2110 static tree cp_parser_selection_statement
2111 (cp_parser *, bool *, vec<tree> *);
2112 static tree cp_parser_condition
2113 (cp_parser *);
2114 static tree cp_parser_iteration_statement
2115 (cp_parser *, bool *, bool, unsigned short);
2116 static bool cp_parser_init_statement
2117 (cp_parser *, tree *decl);
2118 static tree cp_parser_for
2119 (cp_parser *, bool, unsigned short);
2120 static tree cp_parser_c_for
2121 (cp_parser *, tree, tree, bool, unsigned short);
2122 static tree cp_parser_range_for
2123 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2124 static void do_range_for_auto_deduction
2125 (tree, tree);
2126 static tree cp_parser_perform_range_for_lookup
2127 (tree, tree *, tree *);
2128 static tree cp_parser_range_for_member_function
2129 (tree, tree);
2130 static tree cp_parser_jump_statement
2131 (cp_parser *);
2132 static void cp_parser_declaration_statement
2133 (cp_parser *);
2134
2135 static tree cp_parser_implicitly_scoped_statement
2136 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2137 static void cp_parser_already_scoped_statement
2138 (cp_parser *, bool *, const token_indent_info &);
2139
2140 /* Declarations [gram.dcl.dcl] */
2141
2142 static void cp_parser_declaration_seq_opt
2143 (cp_parser *);
2144 static void cp_parser_declaration
2145 (cp_parser *);
2146 static void cp_parser_toplevel_declaration
2147 (cp_parser *);
2148 static void cp_parser_block_declaration
2149 (cp_parser *, bool);
2150 static void cp_parser_simple_declaration
2151 (cp_parser *, bool, tree *);
2152 static void cp_parser_decl_specifier_seq
2153 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2154 static tree cp_parser_storage_class_specifier_opt
2155 (cp_parser *);
2156 static tree cp_parser_function_specifier_opt
2157 (cp_parser *, cp_decl_specifier_seq *);
2158 static tree cp_parser_type_specifier
2159 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2160 int *, bool *);
2161 static tree cp_parser_simple_type_specifier
2162 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2163 static tree cp_parser_type_name
2164 (cp_parser *, bool);
2165 static tree cp_parser_nonclass_name
2166 (cp_parser* parser);
2167 static tree cp_parser_elaborated_type_specifier
2168 (cp_parser *, bool, bool);
2169 static tree cp_parser_enum_specifier
2170 (cp_parser *);
2171 static void cp_parser_enumerator_list
2172 (cp_parser *, tree);
2173 static void cp_parser_enumerator_definition
2174 (cp_parser *, tree);
2175 static tree cp_parser_namespace_name
2176 (cp_parser *);
2177 static void cp_parser_namespace_definition
2178 (cp_parser *);
2179 static void cp_parser_namespace_body
2180 (cp_parser *);
2181 static tree cp_parser_qualified_namespace_specifier
2182 (cp_parser *);
2183 static void cp_parser_namespace_alias_definition
2184 (cp_parser *);
2185 static bool cp_parser_using_declaration
2186 (cp_parser *, bool);
2187 static void cp_parser_using_directive
2188 (cp_parser *);
2189 static tree cp_parser_alias_declaration
2190 (cp_parser *);
2191 static void cp_parser_asm_definition
2192 (cp_parser *);
2193 static void cp_parser_linkage_specification
2194 (cp_parser *);
2195 static void cp_parser_static_assert
2196 (cp_parser *, bool);
2197 static tree cp_parser_decltype
2198 (cp_parser *);
2199 static tree cp_parser_decomposition_declaration
2200 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2201
2202 /* Declarators [gram.dcl.decl] */
2203
2204 static tree cp_parser_init_declarator
2205 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2206 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2207 location_t *, tree *);
2208 static cp_declarator *cp_parser_declarator
2209 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2210 bool, bool, bool);
2211 static cp_declarator *cp_parser_direct_declarator
2212 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2213 bool);
2214 static enum tree_code cp_parser_ptr_operator
2215 (cp_parser *, tree *, cp_cv_quals *, tree *);
2216 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2217 (cp_parser *);
2218 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2219 (cp_parser *);
2220 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2221 (cp_parser *);
2222 static tree cp_parser_tx_qualifier_opt
2223 (cp_parser *);
2224 static tree cp_parser_late_return_type_opt
2225 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2226 static tree cp_parser_declarator_id
2227 (cp_parser *, bool);
2228 static tree cp_parser_type_id
2229 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2230 static tree cp_parser_template_type_arg
2231 (cp_parser *);
2232 static tree cp_parser_trailing_type_id (cp_parser *);
2233 static tree cp_parser_type_id_1
2234 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2235 static void cp_parser_type_specifier_seq
2236 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2237 static tree cp_parser_parameter_declaration_clause
2238 (cp_parser *, cp_parser_flags);
2239 static tree cp_parser_parameter_declaration_list
2240 (cp_parser *, cp_parser_flags);
2241 static cp_parameter_declarator *cp_parser_parameter_declaration
2242 (cp_parser *, cp_parser_flags, bool, bool *);
2243 static tree cp_parser_default_argument
2244 (cp_parser *, bool);
2245 static void cp_parser_function_body
2246 (cp_parser *, bool);
2247 static tree cp_parser_initializer
2248 (cp_parser *, bool *, bool *, bool = false);
2249 static cp_expr cp_parser_initializer_clause
2250 (cp_parser *, bool *);
2251 static cp_expr cp_parser_braced_list
2252 (cp_parser*, bool*);
2253 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2254 (cp_parser *, bool *);
2255
2256 static void cp_parser_ctor_initializer_opt_and_function_body
2257 (cp_parser *, bool);
2258
2259 static tree cp_parser_late_parsing_omp_declare_simd
2260 (cp_parser *, tree);
2261
2262 static tree cp_parser_late_parsing_oacc_routine
2263 (cp_parser *, tree);
2264
2265 static tree synthesize_implicit_template_parm
2266 (cp_parser *, tree);
2267 static tree finish_fully_implicit_template
2268 (cp_parser *, tree);
2269 static void abort_fully_implicit_template
2270 (cp_parser *);
2271
2272 /* Classes [gram.class] */
2273
2274 static tree cp_parser_class_name
2275 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2276 static tree cp_parser_class_specifier
2277 (cp_parser *);
2278 static tree cp_parser_class_head
2279 (cp_parser *, bool *);
2280 static enum tag_types cp_parser_class_key
2281 (cp_parser *);
2282 static void cp_parser_type_parameter_key
2283 (cp_parser* parser);
2284 static void cp_parser_member_specification_opt
2285 (cp_parser *);
2286 static void cp_parser_member_declaration
2287 (cp_parser *);
2288 static tree cp_parser_pure_specifier
2289 (cp_parser *);
2290 static tree cp_parser_constant_initializer
2291 (cp_parser *);
2292
2293 /* Derived classes [gram.class.derived] */
2294
2295 static tree cp_parser_base_clause
2296 (cp_parser *);
2297 static tree cp_parser_base_specifier
2298 (cp_parser *);
2299
2300 /* Special member functions [gram.special] */
2301
2302 static tree cp_parser_conversion_function_id
2303 (cp_parser *);
2304 static tree cp_parser_conversion_type_id
2305 (cp_parser *);
2306 static cp_declarator *cp_parser_conversion_declarator_opt
2307 (cp_parser *);
2308 static void cp_parser_ctor_initializer_opt
2309 (cp_parser *);
2310 static void cp_parser_mem_initializer_list
2311 (cp_parser *);
2312 static tree cp_parser_mem_initializer
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer_id
2315 (cp_parser *);
2316
2317 /* Overloading [gram.over] */
2318
2319 static cp_expr cp_parser_operator_function_id
2320 (cp_parser *);
2321 static cp_expr cp_parser_operator
2322 (cp_parser *, location_t);
2323
2324 /* Templates [gram.temp] */
2325
2326 static void cp_parser_template_declaration
2327 (cp_parser *, bool);
2328 static tree cp_parser_template_parameter_list
2329 (cp_parser *);
2330 static tree cp_parser_template_parameter
2331 (cp_parser *, bool *, bool *);
2332 static tree cp_parser_type_parameter
2333 (cp_parser *, bool *);
2334 static tree cp_parser_template_id
2335 (cp_parser *, bool, bool, enum tag_types, bool);
2336 static tree cp_parser_template_name
2337 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2338 static tree cp_parser_template_argument_list
2339 (cp_parser *);
2340 static tree cp_parser_template_argument
2341 (cp_parser *);
2342 static void cp_parser_explicit_instantiation
2343 (cp_parser *);
2344 static void cp_parser_explicit_specialization
2345 (cp_parser *);
2346
2347 /* Exception handling [gram.exception] */
2348
2349 static tree cp_parser_try_block
2350 (cp_parser *);
2351 static void cp_parser_function_try_block
2352 (cp_parser *);
2353 static void cp_parser_handler_seq
2354 (cp_parser *);
2355 static void cp_parser_handler
2356 (cp_parser *);
2357 static tree cp_parser_exception_declaration
2358 (cp_parser *);
2359 static tree cp_parser_throw_expression
2360 (cp_parser *);
2361 static tree cp_parser_exception_specification_opt
2362 (cp_parser *);
2363 static tree cp_parser_type_id_list
2364 (cp_parser *);
2365
2366 /* GNU Extensions */
2367
2368 static tree cp_parser_asm_specification_opt
2369 (cp_parser *);
2370 static tree cp_parser_asm_operand_list
2371 (cp_parser *);
2372 static tree cp_parser_asm_clobber_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_label_list
2375 (cp_parser *);
2376 static bool cp_next_tokens_can_be_attribute_p
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_gnu_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_std_attribute_p
2381 (cp_parser *);
2382 static bool cp_nth_tokens_can_be_std_attribute_p
2383 (cp_parser *, size_t);
2384 static bool cp_nth_tokens_can_be_gnu_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_attribute_p
2387 (cp_parser *, size_t);
2388 static tree cp_parser_attributes_opt
2389 (cp_parser *);
2390 static tree cp_parser_gnu_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attribute_list
2393 (cp_parser *, bool = false);
2394 static tree cp_parser_std_attribute
2395 (cp_parser *, tree);
2396 static tree cp_parser_std_attribute_spec
2397 (cp_parser *);
2398 static tree cp_parser_std_attribute_spec_seq
2399 (cp_parser *);
2400 static size_t cp_parser_skip_attributes_opt
2401 (cp_parser *, size_t);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2406
2407 /* Concept Extensions */
2408
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2431
2432 /* Transactional Memory Extensions */
2433
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static void cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2442
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2449 };
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2452
2453 /* Objective-C++ Productions */
2454
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2491
2492 /* Utility Routines */
2493
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static cp_expr cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool, location_t);
2555 static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2557 static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559 static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561 static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563 static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571 static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575 static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577 static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579 static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583 static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585 static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591 static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593 static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597 static void cp_parser_error
2598 (cp_parser *, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601 static bool cp_parser_simulate_error
2602 (cp_parser *);
2603 static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623 static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629 static bool cp_parser_error_occurred
2630 (cp_parser *);
2631 static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633 static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641 static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643 static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645 static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647 static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649 static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2651
2652 /* Concept-related syntactic transformations */
2653
2654 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2656
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2659 //
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2662 {
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2665 }
2666
2667 cp_unevaluated::~cp_unevaluated ()
2668 {
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2671 }
2672
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2675
2676 /* Returns nonzero if we are parsing tentatively. */
2677
2678 static inline bool
2679 cp_parser_parsing_tentatively (cp_parser* parser)
2680 {
2681 return parser->context->next != NULL;
2682 }
2683
2684 /* Returns nonzero if TOKEN is a string literal. */
2685
2686 static bool
2687 cp_parser_is_pure_string_literal (cp_token* token)
2688 {
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2694 }
2695
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2698
2699 static bool
2700 cp_parser_is_string_literal (cp_token* token)
2701 {
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2708 }
2709
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2711
2712 static bool
2713 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2714 {
2715 return token->keyword == keyword;
2716 }
2717
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2720
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token *token)
2723 {
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2728 }
2729
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2740
2741 static bool
2742 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2744 {
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2754
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2759
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2766
2767 return true;
2768 }
2769
2770 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2771 RT_CLOSE_PAREN. */
2772
2773 static const char *
2774 get_matching_symbol (required_token token_desc)
2775 {
2776 switch (token_desc)
2777 {
2778 default:
2779 gcc_unreachable ();
2780 return "";
2781 case RT_CLOSE_BRACE:
2782 return "{";
2783 case RT_CLOSE_PAREN:
2784 return "(";
2785 }
2786 }
2787
2788 /* Attempt to convert TOKEN_DESC from a required_token to an
2789 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2790
2791 static enum cpp_ttype
2792 get_required_cpp_ttype (required_token token_desc)
2793 {
2794 switch (token_desc)
2795 {
2796 case RT_SEMICOLON:
2797 return CPP_SEMICOLON;
2798 case RT_OPEN_PAREN:
2799 return CPP_OPEN_PAREN;
2800 case RT_CLOSE_BRACE:
2801 return CPP_CLOSE_BRACE;
2802 case RT_OPEN_BRACE:
2803 return CPP_OPEN_BRACE;
2804 case RT_CLOSE_SQUARE:
2805 return CPP_CLOSE_SQUARE;
2806 case RT_OPEN_SQUARE:
2807 return CPP_OPEN_SQUARE;
2808 case RT_COMMA:
2809 return CPP_COMMA;
2810 case RT_COLON:
2811 return CPP_COLON;
2812 case RT_CLOSE_PAREN:
2813 return CPP_CLOSE_PAREN;
2814
2815 default:
2816 /* Use CPP_EOF as a "no completions possible" code. */
2817 return CPP_EOF;
2818 }
2819 }
2820
2821
2822 /* Subroutine of cp_parser_error and cp_parser_required_error.
2823
2824 Issue a diagnostic of the form
2825 FILE:LINE: MESSAGE before TOKEN
2826 where TOKEN is the next token in the input stream. MESSAGE
2827 (specified by the caller) is usually of the form "expected
2828 OTHER-TOKEN".
2829
2830 This bypasses the check for tentative passing, and potentially
2831 adds material needed by cp_parser_required_error.
2832
2833 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2834 suggesting insertion of the missing token.
2835
2836 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2837 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2838 location. */
2839
2840 static void
2841 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2842 required_token missing_token_desc,
2843 location_t matching_location)
2844 {
2845 cp_token *token = cp_lexer_peek_token (parser->lexer);
2846 /* This diagnostic makes more sense if it is tagged to the line
2847 of the token we just peeked at. */
2848 cp_lexer_set_source_position_from_token (token);
2849
2850 if (token->type == CPP_PRAGMA)
2851 {
2852 error_at (token->location,
2853 "%<#pragma%> is not allowed here");
2854 cp_parser_skip_to_pragma_eol (parser, token);
2855 return;
2856 }
2857
2858 /* If this is actually a conflict marker, report it as such. */
2859 if (token->type == CPP_LSHIFT
2860 || token->type == CPP_RSHIFT
2861 || token->type == CPP_EQ_EQ)
2862 {
2863 location_t loc;
2864 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2865 {
2866 error_at (loc, "version control conflict marker in file");
2867 expanded_location token_exploc = expand_location (token->location);
2868 /* Consume tokens until the end of the source line. */
2869 while (1)
2870 {
2871 cp_lexer_consume_token (parser->lexer);
2872 cp_token *next = cp_lexer_peek_token (parser->lexer);
2873 if (next == NULL)
2874 break;
2875 expanded_location next_exploc = expand_location (next->location);
2876 if (next_exploc.file != token_exploc.file)
2877 break;
2878 if (next_exploc.line != token_exploc.line)
2879 break;
2880 }
2881 return;
2882 }
2883 }
2884
2885 gcc_rich_location richloc (input_location);
2886
2887 bool added_matching_location = false;
2888
2889 if (missing_token_desc != RT_NONE)
2890 {
2891 /* Potentially supply a fix-it hint, suggesting to add the
2892 missing token immediately after the *previous* token.
2893 This may move the primary location within richloc. */
2894 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2895 location_t prev_token_loc
2896 = cp_lexer_previous_token (parser->lexer)->location;
2897 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2898
2899 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2900 Attempt to consolidate diagnostics by printing it as a
2901 secondary range within the main diagnostic. */
2902 if (matching_location != UNKNOWN_LOCATION)
2903 added_matching_location
2904 = richloc.add_location_if_nearby (matching_location);
2905 }
2906
2907 /* Actually emit the error. */
2908 c_parse_error (gmsgid,
2909 /* Because c_parser_error does not understand
2910 CPP_KEYWORD, keywords are treated like
2911 identifiers. */
2912 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2913 token->u.value, token->flags, &richloc);
2914
2915 if (missing_token_desc != RT_NONE)
2916 {
2917 /* If we weren't able to consolidate matching_location, then
2918 print it as a secondary diagnostic. */
2919 if (matching_location != UNKNOWN_LOCATION
2920 && !added_matching_location)
2921 inform (matching_location, "to match this %qs",
2922 get_matching_symbol (missing_token_desc));
2923 }
2924 }
2925
2926 /* If not parsing tentatively, issue a diagnostic of the form
2927 FILE:LINE: MESSAGE before TOKEN
2928 where TOKEN is the next token in the input stream. MESSAGE
2929 (specified by the caller) is usually of the form "expected
2930 OTHER-TOKEN". */
2931
2932 static void
2933 cp_parser_error (cp_parser* parser, const char* gmsgid)
2934 {
2935 if (!cp_parser_simulate_error (parser))
2936 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2937 }
2938
2939 /* Issue an error about name-lookup failing. NAME is the
2940 IDENTIFIER_NODE DECL is the result of
2941 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2942 the thing that we hoped to find. */
2943
2944 static void
2945 cp_parser_name_lookup_error (cp_parser* parser,
2946 tree name,
2947 tree decl,
2948 name_lookup_error desired,
2949 location_t location)
2950 {
2951 /* If name lookup completely failed, tell the user that NAME was not
2952 declared. */
2953 if (decl == error_mark_node)
2954 {
2955 if (parser->scope && parser->scope != global_namespace)
2956 error_at (location, "%<%E::%E%> has not been declared",
2957 parser->scope, name);
2958 else if (parser->scope == global_namespace)
2959 error_at (location, "%<::%E%> has not been declared", name);
2960 else if (parser->object_scope
2961 && !CLASS_TYPE_P (parser->object_scope))
2962 error_at (location, "request for member %qE in non-class type %qT",
2963 name, parser->object_scope);
2964 else if (parser->object_scope)
2965 error_at (location, "%<%T::%E%> has not been declared",
2966 parser->object_scope, name);
2967 else
2968 error_at (location, "%qE has not been declared", name);
2969 }
2970 else if (parser->scope && parser->scope != global_namespace)
2971 {
2972 switch (desired)
2973 {
2974 case NLE_TYPE:
2975 error_at (location, "%<%E::%E%> is not a type",
2976 parser->scope, name);
2977 break;
2978 case NLE_CXX98:
2979 error_at (location, "%<%E::%E%> is not a class or namespace",
2980 parser->scope, name);
2981 break;
2982 case NLE_NOT_CXX98:
2983 error_at (location,
2984 "%<%E::%E%> is not a class, namespace, or enumeration",
2985 parser->scope, name);
2986 break;
2987 default:
2988 gcc_unreachable ();
2989
2990 }
2991 }
2992 else if (parser->scope == global_namespace)
2993 {
2994 switch (desired)
2995 {
2996 case NLE_TYPE:
2997 error_at (location, "%<::%E%> is not a type", name);
2998 break;
2999 case NLE_CXX98:
3000 error_at (location, "%<::%E%> is not a class or namespace", name);
3001 break;
3002 case NLE_NOT_CXX98:
3003 error_at (location,
3004 "%<::%E%> is not a class, namespace, or enumeration",
3005 name);
3006 break;
3007 default:
3008 gcc_unreachable ();
3009 }
3010 }
3011 else
3012 {
3013 switch (desired)
3014 {
3015 case NLE_TYPE:
3016 error_at (location, "%qE is not a type", name);
3017 break;
3018 case NLE_CXX98:
3019 error_at (location, "%qE is not a class or namespace", name);
3020 break;
3021 case NLE_NOT_CXX98:
3022 error_at (location,
3023 "%qE is not a class, namespace, or enumeration", name);
3024 break;
3025 default:
3026 gcc_unreachable ();
3027 }
3028 }
3029 }
3030
3031 /* If we are parsing tentatively, remember that an error has occurred
3032 during this tentative parse. Returns true if the error was
3033 simulated; false if a message should be issued by the caller. */
3034
3035 static bool
3036 cp_parser_simulate_error (cp_parser* parser)
3037 {
3038 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3039 {
3040 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3041 return true;
3042 }
3043 return false;
3044 }
3045
3046 /* This function is called when a type is defined. If type
3047 definitions are forbidden at this point, an error message is
3048 issued. */
3049
3050 static bool
3051 cp_parser_check_type_definition (cp_parser* parser)
3052 {
3053 /* If types are forbidden here, issue a message. */
3054 if (parser->type_definition_forbidden_message)
3055 {
3056 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3057 in the message need to be interpreted. */
3058 error (parser->type_definition_forbidden_message);
3059 return false;
3060 }
3061 return true;
3062 }
3063
3064 /* This function is called when the DECLARATOR is processed. The TYPE
3065 was a type defined in the decl-specifiers. If it is invalid to
3066 define a type in the decl-specifiers for DECLARATOR, an error is
3067 issued. TYPE_LOCATION is the location of TYPE and is used
3068 for error reporting. */
3069
3070 static void
3071 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3072 tree type, location_t type_location)
3073 {
3074 /* [dcl.fct] forbids type definitions in return types.
3075 Unfortunately, it's not easy to know whether or not we are
3076 processing a return type until after the fact. */
3077 while (declarator
3078 && (declarator->kind == cdk_pointer
3079 || declarator->kind == cdk_reference
3080 || declarator->kind == cdk_ptrmem))
3081 declarator = declarator->declarator;
3082 if (declarator
3083 && declarator->kind == cdk_function)
3084 {
3085 error_at (type_location,
3086 "new types may not be defined in a return type");
3087 inform (type_location,
3088 "(perhaps a semicolon is missing after the definition of %qT)",
3089 type);
3090 }
3091 }
3092
3093 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3094 "<" in any valid C++ program. If the next token is indeed "<",
3095 issue a message warning the user about what appears to be an
3096 invalid attempt to form a template-id. LOCATION is the location
3097 of the type-specifier (TYPE) */
3098
3099 static void
3100 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3101 tree type,
3102 enum tag_types tag_type,
3103 location_t location)
3104 {
3105 cp_token_position start = 0;
3106
3107 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3108 {
3109 if (TREE_CODE (type) == TYPE_DECL)
3110 type = TREE_TYPE (type);
3111 if (TYPE_P (type) && !template_placeholder_p (type))
3112 error_at (location, "%qT is not a template", type);
3113 else if (identifier_p (type))
3114 {
3115 if (tag_type != none_type)
3116 error_at (location, "%qE is not a class template", type);
3117 else
3118 error_at (location, "%qE is not a template", type);
3119 }
3120 else
3121 error_at (location, "invalid template-id");
3122 /* Remember the location of the invalid "<". */
3123 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3124 start = cp_lexer_token_position (parser->lexer, true);
3125 /* Consume the "<". */
3126 cp_lexer_consume_token (parser->lexer);
3127 /* Parse the template arguments. */
3128 cp_parser_enclosed_template_argument_list (parser);
3129 /* Permanently remove the invalid template arguments so that
3130 this error message is not issued again. */
3131 if (start)
3132 cp_lexer_purge_tokens_after (parser->lexer, start);
3133 }
3134 }
3135
3136 /* If parsing an integral constant-expression, issue an error message
3137 about the fact that THING appeared and return true. Otherwise,
3138 return false. In either case, set
3139 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3140
3141 static bool
3142 cp_parser_non_integral_constant_expression (cp_parser *parser,
3143 non_integral_constant thing)
3144 {
3145 parser->non_integral_constant_expression_p = true;
3146 if (parser->integral_constant_expression_p)
3147 {
3148 if (!parser->allow_non_integral_constant_expression_p)
3149 {
3150 const char *msg = NULL;
3151 switch (thing)
3152 {
3153 case NIC_FLOAT:
3154 pedwarn (input_location, OPT_Wpedantic,
3155 "ISO C++ forbids using a floating-point literal "
3156 "in a constant-expression");
3157 return true;
3158 case NIC_CAST:
3159 error ("a cast to a type other than an integral or "
3160 "enumeration type cannot appear in a "
3161 "constant-expression");
3162 return true;
3163 case NIC_TYPEID:
3164 error ("%<typeid%> operator "
3165 "cannot appear in a constant-expression");
3166 return true;
3167 case NIC_NCC:
3168 error ("non-constant compound literals "
3169 "cannot appear in a constant-expression");
3170 return true;
3171 case NIC_FUNC_CALL:
3172 error ("a function call "
3173 "cannot appear in a constant-expression");
3174 return true;
3175 case NIC_INC:
3176 error ("an increment "
3177 "cannot appear in a constant-expression");
3178 return true;
3179 case NIC_DEC:
3180 error ("an decrement "
3181 "cannot appear in a constant-expression");
3182 return true;
3183 case NIC_ARRAY_REF:
3184 error ("an array reference "
3185 "cannot appear in a constant-expression");
3186 return true;
3187 case NIC_ADDR_LABEL:
3188 error ("the address of a label "
3189 "cannot appear in a constant-expression");
3190 return true;
3191 case NIC_OVERLOADED:
3192 error ("calls to overloaded operators "
3193 "cannot appear in a constant-expression");
3194 return true;
3195 case NIC_ASSIGNMENT:
3196 error ("an assignment cannot appear in a constant-expression");
3197 return true;
3198 case NIC_COMMA:
3199 error ("a comma operator "
3200 "cannot appear in a constant-expression");
3201 return true;
3202 case NIC_CONSTRUCTOR:
3203 error ("a call to a constructor "
3204 "cannot appear in a constant-expression");
3205 return true;
3206 case NIC_TRANSACTION:
3207 error ("a transaction expression "
3208 "cannot appear in a constant-expression");
3209 return true;
3210 case NIC_THIS:
3211 msg = "this";
3212 break;
3213 case NIC_FUNC_NAME:
3214 msg = "__FUNCTION__";
3215 break;
3216 case NIC_PRETTY_FUNC:
3217 msg = "__PRETTY_FUNCTION__";
3218 break;
3219 case NIC_C99_FUNC:
3220 msg = "__func__";
3221 break;
3222 case NIC_VA_ARG:
3223 msg = "va_arg";
3224 break;
3225 case NIC_ARROW:
3226 msg = "->";
3227 break;
3228 case NIC_POINT:
3229 msg = ".";
3230 break;
3231 case NIC_STAR:
3232 msg = "*";
3233 break;
3234 case NIC_ADDR:
3235 msg = "&";
3236 break;
3237 case NIC_PREINCREMENT:
3238 msg = "++";
3239 break;
3240 case NIC_PREDECREMENT:
3241 msg = "--";
3242 break;
3243 case NIC_NEW:
3244 msg = "new";
3245 break;
3246 case NIC_DEL:
3247 msg = "delete";
3248 break;
3249 default:
3250 gcc_unreachable ();
3251 }
3252 if (msg)
3253 error ("%qs cannot appear in a constant-expression", msg);
3254 return true;
3255 }
3256 }
3257 return false;
3258 }
3259
3260 /* Emit a diagnostic for an invalid type name. This function commits
3261 to the current active tentative parse, if any. (Otherwise, the
3262 problematic construct might be encountered again later, resulting
3263 in duplicate error messages.) LOCATION is the location of ID. */
3264
3265 static void
3266 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3267 location_t location)
3268 {
3269 tree decl, ambiguous_decls;
3270 cp_parser_commit_to_tentative_parse (parser);
3271 /* Try to lookup the identifier. */
3272 decl = cp_parser_lookup_name (parser, id, none_type,
3273 /*is_template=*/false,
3274 /*is_namespace=*/false,
3275 /*check_dependency=*/true,
3276 &ambiguous_decls, location);
3277 if (ambiguous_decls)
3278 /* If the lookup was ambiguous, an error will already have
3279 been issued. */
3280 return;
3281 /* If the lookup found a template-name, it means that the user forgot
3282 to specify an argument list. Emit a useful error message. */
3283 if (DECL_TYPE_TEMPLATE_P (decl))
3284 {
3285 auto_diagnostic_group d;
3286 error_at (location,
3287 "invalid use of template-name %qE without an argument list",
3288 decl);
3289 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3290 inform (location, "class template argument deduction is only available "
3291 "with -std=c++17 or -std=gnu++17");
3292 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3293 }
3294 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3295 error_at (location, "invalid use of destructor %qD as a type", id);
3296 else if (TREE_CODE (decl) == TYPE_DECL)
3297 /* Something like 'unsigned A a;' */
3298 error_at (location, "invalid combination of multiple type-specifiers");
3299 else if (!parser->scope)
3300 {
3301 /* Issue an error message. */
3302 auto_diagnostic_group d;
3303 name_hint hint;
3304 if (TREE_CODE (id) == IDENTIFIER_NODE)
3305 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3306 if (const char *suggestion = hint.suggestion ())
3307 {
3308 gcc_rich_location richloc (location);
3309 richloc.add_fixit_replace (suggestion);
3310 error_at (&richloc,
3311 "%qE does not name a type; did you mean %qs?",
3312 id, suggestion);
3313 }
3314 else
3315 error_at (location, "%qE does not name a type", id);
3316 /* If we're in a template class, it's possible that the user was
3317 referring to a type from a base class. For example:
3318
3319 template <typename T> struct A { typedef T X; };
3320 template <typename T> struct B : public A<T> { X x; };
3321
3322 The user should have said "typename A<T>::X". */
3323 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3324 inform (location, "C++11 %<constexpr%> only available with "
3325 "-std=c++11 or -std=gnu++11");
3326 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3327 inform (location, "C++11 %<noexcept%> only available with "
3328 "-std=c++11 or -std=gnu++11");
3329 else if (cxx_dialect < cxx11
3330 && TREE_CODE (id) == IDENTIFIER_NODE
3331 && id_equal (id, "thread_local"))
3332 inform (location, "C++11 %<thread_local%> only available with "
3333 "-std=c++11 or -std=gnu++11");
3334 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3335 inform (location, "%<concept%> only available with -fconcepts");
3336 else if (processing_template_decl && current_class_type
3337 && TYPE_BINFO (current_class_type))
3338 {
3339 tree b;
3340
3341 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3342 b;
3343 b = TREE_CHAIN (b))
3344 {
3345 tree base_type = BINFO_TYPE (b);
3346 if (CLASS_TYPE_P (base_type)
3347 && dependent_type_p (base_type))
3348 {
3349 tree field;
3350 /* Go from a particular instantiation of the
3351 template (which will have an empty TYPE_FIELDs),
3352 to the main version. */
3353 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3354 for (field = TYPE_FIELDS (base_type);
3355 field;
3356 field = DECL_CHAIN (field))
3357 if (TREE_CODE (field) == TYPE_DECL
3358 && DECL_NAME (field) == id)
3359 {
3360 inform (location,
3361 "(perhaps %<typename %T::%E%> was intended)",
3362 BINFO_TYPE (b), id);
3363 break;
3364 }
3365 if (field)
3366 break;
3367 }
3368 }
3369 }
3370 }
3371 /* Here we diagnose qualified-ids where the scope is actually correct,
3372 but the identifier does not resolve to a valid type name. */
3373 else if (parser->scope != error_mark_node)
3374 {
3375 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3376 {
3377 auto_diagnostic_group d;
3378 name_hint hint;
3379 if (decl == error_mark_node)
3380 hint = suggest_alternative_in_explicit_scope (location, id,
3381 parser->scope);
3382 const char *suggestion = hint.suggestion ();
3383 gcc_rich_location richloc (location_of (id));
3384 if (suggestion)
3385 richloc.add_fixit_replace (suggestion);
3386 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3387 {
3388 if (suggestion)
3389 error_at (&richloc,
3390 "%qE in namespace %qE does not name a template"
3391 " type; did you mean %qs?",
3392 id, parser->scope, suggestion);
3393 else
3394 error_at (&richloc,
3395 "%qE in namespace %qE does not name a template type",
3396 id, parser->scope);
3397 }
3398 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3399 {
3400 if (suggestion)
3401 error_at (&richloc,
3402 "%qE in namespace %qE does not name a template"
3403 " type; did you mean %qs?",
3404 TREE_OPERAND (id, 0), parser->scope, suggestion);
3405 else
3406 error_at (&richloc,
3407 "%qE in namespace %qE does not name a template"
3408 " type",
3409 TREE_OPERAND (id, 0), parser->scope);
3410 }
3411 else
3412 {
3413 if (suggestion)
3414 error_at (&richloc,
3415 "%qE in namespace %qE does not name a type"
3416 "; did you mean %qs?",
3417 id, parser->scope, suggestion);
3418 else
3419 error_at (&richloc,
3420 "%qE in namespace %qE does not name a type",
3421 id, parser->scope);
3422 }
3423 if (DECL_P (decl))
3424 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3425 }
3426 else if (CLASS_TYPE_P (parser->scope)
3427 && constructor_name_p (id, parser->scope))
3428 {
3429 /* A<T>::A<T>() */
3430 auto_diagnostic_group d;
3431 error_at (location, "%<%T::%E%> names the constructor, not"
3432 " the type", parser->scope, id);
3433 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3434 error_at (location, "and %qT has no template constructors",
3435 parser->scope);
3436 }
3437 else if (TYPE_P (parser->scope)
3438 && dependent_scope_p (parser->scope))
3439 {
3440 gcc_rich_location richloc (location);
3441 richloc.add_fixit_insert_before ("typename ");
3442 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3443 error_at (&richloc,
3444 "need %<typename%> before %<%T::%D::%E%> because "
3445 "%<%T::%D%> is a dependent scope",
3446 TYPE_CONTEXT (parser->scope),
3447 TYPENAME_TYPE_FULLNAME (parser->scope),
3448 id,
3449 TYPE_CONTEXT (parser->scope),
3450 TYPENAME_TYPE_FULLNAME (parser->scope));
3451 else
3452 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3453 "%qT is a dependent scope",
3454 parser->scope, id, parser->scope);
3455 }
3456 else if (TYPE_P (parser->scope))
3457 {
3458 auto_diagnostic_group d;
3459 if (!COMPLETE_TYPE_P (parser->scope))
3460 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3461 parser->scope);
3462 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3463 error_at (location_of (id),
3464 "%qE in %q#T does not name a template type",
3465 id, parser->scope);
3466 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3467 error_at (location_of (id),
3468 "%qE in %q#T does not name a template type",
3469 TREE_OPERAND (id, 0), parser->scope);
3470 else
3471 error_at (location_of (id),
3472 "%qE in %q#T does not name a type",
3473 id, parser->scope);
3474 if (DECL_P (decl))
3475 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3476 }
3477 else
3478 gcc_unreachable ();
3479 }
3480 }
3481
3482 /* Check for a common situation where a type-name should be present,
3483 but is not, and issue a sensible error message. Returns true if an
3484 invalid type-name was detected.
3485
3486 The situation handled by this function are variable declarations of the
3487 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3488 Usually, `ID' should name a type, but if we got here it means that it
3489 does not. We try to emit the best possible error message depending on
3490 how exactly the id-expression looks like. */
3491
3492 static bool
3493 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3494 {
3495 tree id;
3496 cp_token *token = cp_lexer_peek_token (parser->lexer);
3497
3498 /* Avoid duplicate error about ambiguous lookup. */
3499 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3500 {
3501 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3502 if (next->type == CPP_NAME && next->error_reported)
3503 goto out;
3504 }
3505
3506 cp_parser_parse_tentatively (parser);
3507 id = cp_parser_id_expression (parser,
3508 /*template_keyword_p=*/false,
3509 /*check_dependency_p=*/true,
3510 /*template_p=*/NULL,
3511 /*declarator_p=*/false,
3512 /*optional_p=*/false);
3513 /* If the next token is a (, this is a function with no explicit return
3514 type, i.e. constructor, destructor or conversion op. */
3515 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3516 || TREE_CODE (id) == TYPE_DECL)
3517 {
3518 cp_parser_abort_tentative_parse (parser);
3519 return false;
3520 }
3521 if (!cp_parser_parse_definitely (parser))
3522 return false;
3523
3524 /* Emit a diagnostic for the invalid type. */
3525 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3526 out:
3527 /* If we aren't in the middle of a declarator (i.e. in a
3528 parameter-declaration-clause), skip to the end of the declaration;
3529 there's no point in trying to process it. */
3530 if (!parser->in_declarator_p)
3531 cp_parser_skip_to_end_of_block_or_statement (parser);
3532 return true;
3533 }
3534
3535 /* Consume tokens up to, and including, the next non-nested closing `)'.
3536 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3537 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3538 found an unnested token of that type. */
3539
3540 static int
3541 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3542 bool recovering,
3543 cpp_ttype or_ttype,
3544 bool consume_paren)
3545 {
3546 unsigned paren_depth = 0;
3547 unsigned brace_depth = 0;
3548 unsigned square_depth = 0;
3549 unsigned condop_depth = 0;
3550
3551 if (recovering && or_ttype == CPP_EOF
3552 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3553 return 0;
3554
3555 while (true)
3556 {
3557 cp_token * token = cp_lexer_peek_token (parser->lexer);
3558
3559 /* Have we found what we're looking for before the closing paren? */
3560 if (token->type == or_ttype && or_ttype != CPP_EOF
3561 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3562 return -1;
3563
3564 switch (token->type)
3565 {
3566 case CPP_PRAGMA_EOL:
3567 if (!parser->lexer->in_pragma)
3568 break;
3569 /* FALLTHRU */
3570 case CPP_EOF:
3571 /* If we've run out of tokens, then there is no closing `)'. */
3572 return 0;
3573
3574 /* This is good for lambda expression capture-lists. */
3575 case CPP_OPEN_SQUARE:
3576 ++square_depth;
3577 break;
3578 case CPP_CLOSE_SQUARE:
3579 if (!square_depth--)
3580 return 0;
3581 break;
3582
3583 case CPP_SEMICOLON:
3584 /* This matches the processing in skip_to_end_of_statement. */
3585 if (!brace_depth)
3586 return 0;
3587 break;
3588
3589 case CPP_OPEN_BRACE:
3590 ++brace_depth;
3591 break;
3592 case CPP_CLOSE_BRACE:
3593 if (!brace_depth--)
3594 return 0;
3595 break;
3596
3597 case CPP_OPEN_PAREN:
3598 if (!brace_depth)
3599 ++paren_depth;
3600 break;
3601
3602 case CPP_CLOSE_PAREN:
3603 if (!brace_depth && !paren_depth--)
3604 {
3605 if (consume_paren)
3606 cp_lexer_consume_token (parser->lexer);
3607 return 1;
3608 }
3609 break;
3610
3611 case CPP_QUERY:
3612 if (!brace_depth && !paren_depth && !square_depth)
3613 ++condop_depth;
3614 break;
3615
3616 case CPP_COLON:
3617 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3618 condop_depth--;
3619 break;
3620
3621 default:
3622 break;
3623 }
3624
3625 /* Consume the token. */
3626 cp_lexer_consume_token (parser->lexer);
3627 }
3628 }
3629
3630 /* Consume tokens up to, and including, the next non-nested closing `)'.
3631 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3632 are doing error recovery. Returns -1 if OR_COMMA is true and we
3633 found an unnested token of that type. */
3634
3635 static int
3636 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3637 bool recovering,
3638 bool or_comma,
3639 bool consume_paren)
3640 {
3641 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3642 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3643 ttype, consume_paren);
3644 }
3645
3646 /* Consume tokens until we reach the end of the current statement.
3647 Normally, that will be just before consuming a `;'. However, if a
3648 non-nested `}' comes first, then we stop before consuming that. */
3649
3650 static void
3651 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3652 {
3653 unsigned nesting_depth = 0;
3654
3655 /* Unwind generic function template scope if necessary. */
3656 if (parser->fully_implicit_function_template_p)
3657 abort_fully_implicit_template (parser);
3658
3659 while (true)
3660 {
3661 cp_token *token = cp_lexer_peek_token (parser->lexer);
3662
3663 switch (token->type)
3664 {
3665 case CPP_PRAGMA_EOL:
3666 if (!parser->lexer->in_pragma)
3667 break;
3668 /* FALLTHRU */
3669 case CPP_EOF:
3670 /* If we've run out of tokens, stop. */
3671 return;
3672
3673 case CPP_SEMICOLON:
3674 /* If the next token is a `;', we have reached the end of the
3675 statement. */
3676 if (!nesting_depth)
3677 return;
3678 break;
3679
3680 case CPP_CLOSE_BRACE:
3681 /* If this is a non-nested '}', stop before consuming it.
3682 That way, when confronted with something like:
3683
3684 { 3 + }
3685
3686 we stop before consuming the closing '}', even though we
3687 have not yet reached a `;'. */
3688 if (nesting_depth == 0)
3689 return;
3690
3691 /* If it is the closing '}' for a block that we have
3692 scanned, stop -- but only after consuming the token.
3693 That way given:
3694
3695 void f g () { ... }
3696 typedef int I;
3697
3698 we will stop after the body of the erroneously declared
3699 function, but before consuming the following `typedef'
3700 declaration. */
3701 if (--nesting_depth == 0)
3702 {
3703 cp_lexer_consume_token (parser->lexer);
3704 return;
3705 }
3706 break;
3707
3708 case CPP_OPEN_BRACE:
3709 ++nesting_depth;
3710 break;
3711
3712 default:
3713 break;
3714 }
3715
3716 /* Consume the token. */
3717 cp_lexer_consume_token (parser->lexer);
3718 }
3719 }
3720
3721 /* This function is called at the end of a statement or declaration.
3722 If the next token is a semicolon, it is consumed; otherwise, error
3723 recovery is attempted. */
3724
3725 static void
3726 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3727 {
3728 /* Look for the trailing `;'. */
3729 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3730 {
3731 /* If there is additional (erroneous) input, skip to the end of
3732 the statement. */
3733 cp_parser_skip_to_end_of_statement (parser);
3734 /* If the next token is now a `;', consume it. */
3735 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3736 cp_lexer_consume_token (parser->lexer);
3737 }
3738 }
3739
3740 /* Skip tokens until we have consumed an entire block, or until we
3741 have consumed a non-nested `;'. */
3742
3743 static void
3744 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3745 {
3746 int nesting_depth = 0;
3747
3748 /* Unwind generic function template scope if necessary. */
3749 if (parser->fully_implicit_function_template_p)
3750 abort_fully_implicit_template (parser);
3751
3752 while (nesting_depth >= 0)
3753 {
3754 cp_token *token = cp_lexer_peek_token (parser->lexer);
3755
3756 switch (token->type)
3757 {
3758 case CPP_PRAGMA_EOL:
3759 if (!parser->lexer->in_pragma)
3760 break;
3761 /* FALLTHRU */
3762 case CPP_EOF:
3763 /* If we've run out of tokens, stop. */
3764 return;
3765
3766 case CPP_SEMICOLON:
3767 /* Stop if this is an unnested ';'. */
3768 if (!nesting_depth)
3769 nesting_depth = -1;
3770 break;
3771
3772 case CPP_CLOSE_BRACE:
3773 /* Stop if this is an unnested '}', or closes the outermost
3774 nesting level. */
3775 nesting_depth--;
3776 if (nesting_depth < 0)
3777 return;
3778 if (!nesting_depth)
3779 nesting_depth = -1;
3780 break;
3781
3782 case CPP_OPEN_BRACE:
3783 /* Nest. */
3784 nesting_depth++;
3785 break;
3786
3787 default:
3788 break;
3789 }
3790
3791 /* Consume the token. */
3792 cp_lexer_consume_token (parser->lexer);
3793 }
3794 }
3795
3796 /* Skip tokens until a non-nested closing curly brace is the next
3797 token, or there are no more tokens. Return true in the first case,
3798 false otherwise. */
3799
3800 static bool
3801 cp_parser_skip_to_closing_brace (cp_parser *parser)
3802 {
3803 unsigned nesting_depth = 0;
3804
3805 while (true)
3806 {
3807 cp_token *token = cp_lexer_peek_token (parser->lexer);
3808
3809 switch (token->type)
3810 {
3811 case CPP_PRAGMA_EOL:
3812 if (!parser->lexer->in_pragma)
3813 break;
3814 /* FALLTHRU */
3815 case CPP_EOF:
3816 /* If we've run out of tokens, stop. */
3817 return false;
3818
3819 case CPP_CLOSE_BRACE:
3820 /* If the next token is a non-nested `}', then we have reached
3821 the end of the current block. */
3822 if (nesting_depth-- == 0)
3823 return true;
3824 break;
3825
3826 case CPP_OPEN_BRACE:
3827 /* If it the next token is a `{', then we are entering a new
3828 block. Consume the entire block. */
3829 ++nesting_depth;
3830 break;
3831
3832 default:
3833 break;
3834 }
3835
3836 /* Consume the token. */
3837 cp_lexer_consume_token (parser->lexer);
3838 }
3839 }
3840
3841 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3842 parameter is the PRAGMA token, allowing us to purge the entire pragma
3843 sequence. */
3844
3845 static void
3846 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3847 {
3848 cp_token *token;
3849
3850 parser->lexer->in_pragma = false;
3851
3852 do
3853 token = cp_lexer_consume_token (parser->lexer);
3854 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3855
3856 /* Ensure that the pragma is not parsed again. */
3857 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3858 }
3859
3860 /* Require pragma end of line, resyncing with it as necessary. The
3861 arguments are as for cp_parser_skip_to_pragma_eol. */
3862
3863 static void
3864 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3865 {
3866 parser->lexer->in_pragma = false;
3867 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3868 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3869 }
3870
3871 /* This is a simple wrapper around make_typename_type. When the id is
3872 an unresolved identifier node, we can provide a superior diagnostic
3873 using cp_parser_diagnose_invalid_type_name. */
3874
3875 static tree
3876 cp_parser_make_typename_type (cp_parser *parser, tree id,
3877 location_t id_location)
3878 {
3879 tree result;
3880 if (identifier_p (id))
3881 {
3882 result = make_typename_type (parser->scope, id, typename_type,
3883 /*complain=*/tf_none);
3884 if (result == error_mark_node)
3885 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3886 return result;
3887 }
3888 return make_typename_type (parser->scope, id, typename_type, tf_error);
3889 }
3890
3891 /* This is a wrapper around the
3892 make_{pointer,ptrmem,reference}_declarator functions that decides
3893 which one to call based on the CODE and CLASS_TYPE arguments. The
3894 CODE argument should be one of the values returned by
3895 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3896 appertain to the pointer or reference. */
3897
3898 static cp_declarator *
3899 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3900 cp_cv_quals cv_qualifiers,
3901 cp_declarator *target,
3902 tree attributes)
3903 {
3904 if (code == ERROR_MARK || target == cp_error_declarator)
3905 return cp_error_declarator;
3906
3907 if (code == INDIRECT_REF)
3908 if (class_type == NULL_TREE)
3909 return make_pointer_declarator (cv_qualifiers, target, attributes);
3910 else
3911 return make_ptrmem_declarator (cv_qualifiers, class_type,
3912 target, attributes);
3913 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3914 return make_reference_declarator (cv_qualifiers, target,
3915 false, attributes);
3916 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3917 return make_reference_declarator (cv_qualifiers, target,
3918 true, attributes);
3919 gcc_unreachable ();
3920 }
3921
3922 /* Create a new C++ parser. */
3923
3924 static cp_parser *
3925 cp_parser_new (void)
3926 {
3927 cp_parser *parser;
3928 cp_lexer *lexer;
3929 unsigned i;
3930
3931 /* cp_lexer_new_main is called before doing GC allocation because
3932 cp_lexer_new_main might load a PCH file. */
3933 lexer = cp_lexer_new_main ();
3934
3935 /* Initialize the binops_by_token so that we can get the tree
3936 directly from the token. */
3937 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3938 binops_by_token[binops[i].token_type] = binops[i];
3939
3940 parser = ggc_cleared_alloc<cp_parser> ();
3941 parser->lexer = lexer;
3942 parser->context = cp_parser_context_new (NULL);
3943
3944 /* For now, we always accept GNU extensions. */
3945 parser->allow_gnu_extensions_p = 1;
3946
3947 /* The `>' token is a greater-than operator, not the end of a
3948 template-id. */
3949 parser->greater_than_is_operator_p = true;
3950
3951 parser->default_arg_ok_p = true;
3952
3953 /* We are not parsing a constant-expression. */
3954 parser->integral_constant_expression_p = false;
3955 parser->allow_non_integral_constant_expression_p = false;
3956 parser->non_integral_constant_expression_p = false;
3957
3958 /* Local variable names are not forbidden. */
3959 parser->local_variables_forbidden_p = 0;
3960
3961 /* We are not processing an `extern "C"' declaration. */
3962 parser->in_unbraced_linkage_specification_p = false;
3963
3964 /* We are not processing a declarator. */
3965 parser->in_declarator_p = false;
3966
3967 /* We are not processing a template-argument-list. */
3968 parser->in_template_argument_list_p = false;
3969
3970 /* We are not in an iteration statement. */
3971 parser->in_statement = 0;
3972
3973 /* We are not in a switch statement. */
3974 parser->in_switch_statement_p = false;
3975
3976 /* We are not parsing a type-id inside an expression. */
3977 parser->in_type_id_in_expr_p = false;
3978
3979 /* String literals should be translated to the execution character set. */
3980 parser->translate_strings_p = true;
3981
3982 /* We are not parsing a function body. */
3983 parser->in_function_body = false;
3984
3985 /* We can correct until told otherwise. */
3986 parser->colon_corrects_to_scope_p = true;
3987
3988 /* The unparsed function queue is empty. */
3989 push_unparsed_function_queues (parser);
3990
3991 /* There are no classes being defined. */
3992 parser->num_classes_being_defined = 0;
3993
3994 /* No template parameters apply. */
3995 parser->num_template_parameter_lists = 0;
3996
3997 /* Special parsing data structures. */
3998 parser->omp_declare_simd = NULL;
3999 parser->oacc_routine = NULL;
4000
4001 /* Not declaring an implicit function template. */
4002 parser->auto_is_implicit_function_template_parm_p = false;
4003 parser->fully_implicit_function_template_p = false;
4004 parser->implicit_template_parms = 0;
4005 parser->implicit_template_scope = 0;
4006
4007 /* Allow constrained-type-specifiers. */
4008 parser->prevent_constrained_type_specifiers = 0;
4009
4010 /* We haven't yet seen an 'extern "C"'. */
4011 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4012
4013 return parser;
4014 }
4015
4016 /* Create a cp_lexer structure which will emit the tokens in CACHE
4017 and push it onto the parser's lexer stack. This is used for delayed
4018 parsing of in-class method bodies and default arguments, and should
4019 not be confused with tentative parsing. */
4020 static void
4021 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4022 {
4023 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4024 lexer->next = parser->lexer;
4025 parser->lexer = lexer;
4026
4027 /* Move the current source position to that of the first token in the
4028 new lexer. */
4029 cp_lexer_set_source_position_from_token (lexer->next_token);
4030 }
4031
4032 /* Pop the top lexer off the parser stack. This is never used for the
4033 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4034 static void
4035 cp_parser_pop_lexer (cp_parser *parser)
4036 {
4037 cp_lexer *lexer = parser->lexer;
4038 parser->lexer = lexer->next;
4039 cp_lexer_destroy (lexer);
4040
4041 /* Put the current source position back where it was before this
4042 lexer was pushed. */
4043 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4044 }
4045
4046 /* Lexical conventions [gram.lex] */
4047
4048 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4049 identifier. */
4050
4051 static cp_expr
4052 cp_parser_identifier (cp_parser* parser)
4053 {
4054 cp_token *token;
4055
4056 /* Look for the identifier. */
4057 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4058 /* Return the value. */
4059 if (token)
4060 return cp_expr (token->u.value, token->location);
4061 else
4062 return error_mark_node;
4063 }
4064
4065 /* Parse a sequence of adjacent string constants. Returns a
4066 TREE_STRING representing the combined, nul-terminated string
4067 constant. If TRANSLATE is true, translate the string to the
4068 execution character set. If WIDE_OK is true, a wide string is
4069 invalid here.
4070
4071 C++98 [lex.string] says that if a narrow string literal token is
4072 adjacent to a wide string literal token, the behavior is undefined.
4073 However, C99 6.4.5p4 says that this results in a wide string literal.
4074 We follow C99 here, for consistency with the C front end.
4075
4076 This code is largely lifted from lex_string() in c-lex.c.
4077
4078 FUTURE: ObjC++ will need to handle @-strings here. */
4079 static cp_expr
4080 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4081 bool lookup_udlit = true)
4082 {
4083 tree value;
4084 size_t count;
4085 struct obstack str_ob;
4086 struct obstack loc_ob;
4087 cpp_string str, istr, *strs;
4088 cp_token *tok;
4089 enum cpp_ttype type, curr_type;
4090 int have_suffix_p = 0;
4091 tree string_tree;
4092 tree suffix_id = NULL_TREE;
4093 bool curr_tok_is_userdef_p = false;
4094
4095 tok = cp_lexer_peek_token (parser->lexer);
4096 if (!cp_parser_is_string_literal (tok))
4097 {
4098 cp_parser_error (parser, "expected string-literal");
4099 return error_mark_node;
4100 }
4101
4102 location_t loc = tok->location;
4103
4104 if (cpp_userdef_string_p (tok->type))
4105 {
4106 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4107 curr_type = cpp_userdef_string_remove_type (tok->type);
4108 curr_tok_is_userdef_p = true;
4109 }
4110 else
4111 {
4112 string_tree = tok->u.value;
4113 curr_type = tok->type;
4114 }
4115 type = curr_type;
4116
4117 /* Try to avoid the overhead of creating and destroying an obstack
4118 for the common case of just one string. */
4119 if (!cp_parser_is_string_literal
4120 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4121 {
4122 cp_lexer_consume_token (parser->lexer);
4123
4124 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4125 str.len = TREE_STRING_LENGTH (string_tree);
4126 count = 1;
4127
4128 if (curr_tok_is_userdef_p)
4129 {
4130 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4131 have_suffix_p = 1;
4132 curr_type = cpp_userdef_string_remove_type (tok->type);
4133 }
4134 else
4135 curr_type = tok->type;
4136
4137 strs = &str;
4138 }
4139 else
4140 {
4141 location_t last_tok_loc = tok->location;
4142 gcc_obstack_init (&str_ob);
4143 gcc_obstack_init (&loc_ob);
4144 count = 0;
4145
4146 do
4147 {
4148 cp_lexer_consume_token (parser->lexer);
4149 count++;
4150 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4151 str.len = TREE_STRING_LENGTH (string_tree);
4152
4153 if (curr_tok_is_userdef_p)
4154 {
4155 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4156 if (have_suffix_p == 0)
4157 {
4158 suffix_id = curr_suffix_id;
4159 have_suffix_p = 1;
4160 }
4161 else if (have_suffix_p == 1
4162 && curr_suffix_id != suffix_id)
4163 {
4164 error ("inconsistent user-defined literal suffixes"
4165 " %qD and %qD in string literal",
4166 suffix_id, curr_suffix_id);
4167 have_suffix_p = -1;
4168 }
4169 curr_type = cpp_userdef_string_remove_type (tok->type);
4170 }
4171 else
4172 curr_type = tok->type;
4173
4174 if (type != curr_type)
4175 {
4176 if (type == CPP_STRING)
4177 type = curr_type;
4178 else if (curr_type != CPP_STRING)
4179 {
4180 rich_location rich_loc (line_table, tok->location);
4181 rich_loc.add_range (last_tok_loc);
4182 error_at (&rich_loc,
4183 "unsupported non-standard concatenation "
4184 "of string literals");
4185 }
4186 }
4187
4188 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4189 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4190
4191 last_tok_loc = tok->location;
4192
4193 tok = cp_lexer_peek_token (parser->lexer);
4194 if (cpp_userdef_string_p (tok->type))
4195 {
4196 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4197 curr_type = cpp_userdef_string_remove_type (tok->type);
4198 curr_tok_is_userdef_p = true;
4199 }
4200 else
4201 {
4202 string_tree = tok->u.value;
4203 curr_type = tok->type;
4204 curr_tok_is_userdef_p = false;
4205 }
4206 }
4207 while (cp_parser_is_string_literal (tok));
4208
4209 /* A string literal built by concatenation has its caret=start at
4210 the start of the initial string, and its finish at the finish of
4211 the final string literal. */
4212 loc = make_location (loc, loc, get_finish (last_tok_loc));
4213
4214 strs = (cpp_string *) obstack_finish (&str_ob);
4215 }
4216
4217 if (type != CPP_STRING && !wide_ok)
4218 {
4219 cp_parser_error (parser, "a wide string is invalid in this context");
4220 type = CPP_STRING;
4221 }
4222
4223 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4224 (parse_in, strs, count, &istr, type))
4225 {
4226 value = build_string (istr.len, (const char *)istr.text);
4227 free (CONST_CAST (unsigned char *, istr.text));
4228 if (count > 1)
4229 {
4230 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4231 gcc_assert (g_string_concat_db);
4232 g_string_concat_db->record_string_concatenation (count, locs);
4233 }
4234
4235 switch (type)
4236 {
4237 default:
4238 case CPP_STRING:
4239 TREE_TYPE (value) = char_array_type_node;
4240 break;
4241 case CPP_UTF8STRING:
4242 if (flag_char8_t)
4243 TREE_TYPE (value) = char8_array_type_node;
4244 else
4245 TREE_TYPE (value) = char_array_type_node;
4246 break;
4247 case CPP_STRING16:
4248 TREE_TYPE (value) = char16_array_type_node;
4249 break;
4250 case CPP_STRING32:
4251 TREE_TYPE (value) = char32_array_type_node;
4252 break;
4253 case CPP_WSTRING:
4254 TREE_TYPE (value) = wchar_array_type_node;
4255 break;
4256 }
4257
4258 value = fix_string_type (value);
4259
4260 if (have_suffix_p)
4261 {
4262 tree literal = build_userdef_literal (suffix_id, value,
4263 OT_NONE, NULL_TREE);
4264 if (lookup_udlit)
4265 value = cp_parser_userdef_string_literal (literal);
4266 else
4267 value = literal;
4268 }
4269 }
4270 else
4271 /* cpp_interpret_string has issued an error. */
4272 value = error_mark_node;
4273
4274 if (count > 1)
4275 {
4276 obstack_free (&str_ob, 0);
4277 obstack_free (&loc_ob, 0);
4278 }
4279
4280 return cp_expr (value, loc);
4281 }
4282
4283 /* Look up a literal operator with the name and the exact arguments. */
4284
4285 static tree
4286 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4287 {
4288 tree decl = lookup_name (name);
4289 if (!decl || !is_overloaded_fn (decl))
4290 return error_mark_node;
4291
4292 for (lkp_iterator iter (decl); iter; ++iter)
4293 {
4294 tree fn = *iter;
4295
4296 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4297 {
4298 unsigned int ix;
4299 bool found = true;
4300
4301 for (ix = 0;
4302 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4303 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4304 {
4305 tree tparm = TREE_VALUE (parmtypes);
4306 tree targ = TREE_TYPE ((*args)[ix]);
4307 bool ptr = TYPE_PTR_P (tparm);
4308 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4309 if ((ptr || arr || !same_type_p (tparm, targ))
4310 && (!ptr || !arr
4311 || !same_type_p (TREE_TYPE (tparm),
4312 TREE_TYPE (targ))))
4313 found = false;
4314 }
4315
4316 if (found
4317 && ix == vec_safe_length (args)
4318 /* May be this should be sufficient_parms_p instead,
4319 depending on how exactly should user-defined literals
4320 work in presence of default arguments on the literal
4321 operator parameters. */
4322 && parmtypes == void_list_node)
4323 return decl;
4324 }
4325 }
4326
4327 return error_mark_node;
4328 }
4329
4330 /* Parse a user-defined char constant. Returns a call to a user-defined
4331 literal operator taking the character as an argument. */
4332
4333 static cp_expr
4334 cp_parser_userdef_char_literal (cp_parser *parser)
4335 {
4336 cp_token *token = cp_lexer_consume_token (parser->lexer);
4337 tree literal = token->u.value;
4338 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4339 tree value = USERDEF_LITERAL_VALUE (literal);
4340 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4341 tree decl, result;
4342
4343 /* Build up a call to the user-defined operator */
4344 /* Lookup the name we got back from the id-expression. */
4345 vec<tree, va_gc> *args = make_tree_vector ();
4346 vec_safe_push (args, value);
4347 decl = lookup_literal_operator (name, args);
4348 if (!decl || decl == error_mark_node)
4349 {
4350 error ("unable to find character literal operator %qD with %qT argument",
4351 name, TREE_TYPE (value));
4352 release_tree_vector (args);
4353 return error_mark_node;
4354 }
4355 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4356 release_tree_vector (args);
4357 return result;
4358 }
4359
4360 /* A subroutine of cp_parser_userdef_numeric_literal to
4361 create a char... template parameter pack from a string node. */
4362
4363 static tree
4364 make_char_string_pack (tree value)
4365 {
4366 tree charvec;
4367 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4368 const char *str = TREE_STRING_POINTER (value);
4369 int i, len = TREE_STRING_LENGTH (value) - 1;
4370 tree argvec = make_tree_vec (1);
4371
4372 /* Fill in CHARVEC with all of the parameters. */
4373 charvec = make_tree_vec (len);
4374 for (i = 0; i < len; ++i)
4375 {
4376 unsigned char s[3] = { '\'', str[i], '\'' };
4377 cpp_string in = { 3, s };
4378 cpp_string out = { 0, 0 };
4379 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4380 return NULL_TREE;
4381 gcc_assert (out.len == 2);
4382 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4383 out.text[0]);
4384 }
4385
4386 /* Build the argument packs. */
4387 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4388
4389 TREE_VEC_ELT (argvec, 0) = argpack;
4390
4391 return argvec;
4392 }
4393
4394 /* A subroutine of cp_parser_userdef_numeric_literal to
4395 create a char... template parameter pack from a string node. */
4396
4397 static tree
4398 make_string_pack (tree value)
4399 {
4400 tree charvec;
4401 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4402 const unsigned char *str
4403 = (const unsigned char *) TREE_STRING_POINTER (value);
4404 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4405 int len = TREE_STRING_LENGTH (value) / sz - 1;
4406 tree argvec = make_tree_vec (2);
4407
4408 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4409 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4410
4411 /* First template parm is character type. */
4412 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4413
4414 /* Fill in CHARVEC with all of the parameters. */
4415 charvec = make_tree_vec (len);
4416 for (int i = 0; i < len; ++i)
4417 TREE_VEC_ELT (charvec, i)
4418 = double_int_to_tree (str_char_type_node,
4419 double_int::from_buffer (str + i * sz, sz));
4420
4421 /* Build the argument packs. */
4422 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4423
4424 TREE_VEC_ELT (argvec, 1) = argpack;
4425
4426 return argvec;
4427 }
4428
4429 /* Parse a user-defined numeric constant. returns a call to a user-defined
4430 literal operator. */
4431
4432 static cp_expr
4433 cp_parser_userdef_numeric_literal (cp_parser *parser)
4434 {
4435 cp_token *token = cp_lexer_consume_token (parser->lexer);
4436 tree literal = token->u.value;
4437 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4438 tree value = USERDEF_LITERAL_VALUE (literal);
4439 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4440 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4441 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4442 tree decl, result;
4443 vec<tree, va_gc> *args;
4444
4445 /* Look for a literal operator taking the exact type of numeric argument
4446 as the literal value. */
4447 args = make_tree_vector ();
4448 vec_safe_push (args, value);
4449 decl = lookup_literal_operator (name, args);
4450 if (decl && decl != error_mark_node)
4451 {
4452 result = finish_call_expr (decl, &args, false, true,
4453 tf_warning_or_error);
4454
4455 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4456 {
4457 warning_at (token->location, OPT_Woverflow,
4458 "integer literal exceeds range of %qT type",
4459 long_long_unsigned_type_node);
4460 }
4461 else
4462 {
4463 if (overflow > 0)
4464 warning_at (token->location, OPT_Woverflow,
4465 "floating literal exceeds range of %qT type",
4466 long_double_type_node);
4467 else if (overflow < 0)
4468 warning_at (token->location, OPT_Woverflow,
4469 "floating literal truncated to zero");
4470 }
4471
4472 release_tree_vector (args);
4473 return result;
4474 }
4475 release_tree_vector (args);
4476
4477 /* If the numeric argument didn't work, look for a raw literal
4478 operator taking a const char* argument consisting of the number
4479 in string format. */
4480 args = make_tree_vector ();
4481 vec_safe_push (args, num_string);
4482 decl = lookup_literal_operator (name, args);
4483 if (decl && decl != error_mark_node)
4484 {
4485 result = finish_call_expr (decl, &args, false, true,
4486 tf_warning_or_error);
4487 release_tree_vector (args);
4488 return result;
4489 }
4490 release_tree_vector (args);
4491
4492 /* If the raw literal didn't work, look for a non-type template
4493 function with parameter pack char.... Call the function with
4494 template parameter characters representing the number. */
4495 args = make_tree_vector ();
4496 decl = lookup_literal_operator (name, args);
4497 if (decl && decl != error_mark_node)
4498 {
4499 tree tmpl_args = make_char_string_pack (num_string);
4500 if (tmpl_args == NULL_TREE)
4501 {
4502 error ("failed to translate literal to execution character set %qT",
4503 num_string);
4504 return error_mark_node;
4505 }
4506 decl = lookup_template_function (decl, tmpl_args);
4507 result = finish_call_expr (decl, &args, false, true,
4508 tf_warning_or_error);
4509 release_tree_vector (args);
4510 return result;
4511 }
4512
4513 release_tree_vector (args);
4514
4515 /* In C++14 the standard library defines complex number suffixes that
4516 conflict with GNU extensions. Prefer them if <complex> is #included. */
4517 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4518 bool i14 = (cxx_dialect > cxx11
4519 && (id_equal (suffix_id, "i")
4520 || id_equal (suffix_id, "if")
4521 || id_equal (suffix_id, "il")));
4522 diagnostic_t kind = DK_ERROR;
4523 int opt = 0;
4524
4525 if (i14 && ext)
4526 {
4527 tree cxlit = lookup_qualified_name (std_node,
4528 get_identifier ("complex_literals"),
4529 0, false, false);
4530 if (cxlit == error_mark_node)
4531 {
4532 /* No <complex>, so pedwarn and use GNU semantics. */
4533 kind = DK_PEDWARN;
4534 opt = OPT_Wpedantic;
4535 }
4536 }
4537
4538 bool complained
4539 = emit_diagnostic (kind, input_location, opt,
4540 "unable to find numeric literal operator %qD", name);
4541
4542 if (!complained)
4543 /* Don't inform either. */;
4544 else if (i14)
4545 {
4546 inform (token->location, "add %<using namespace std::complex_literals%> "
4547 "(from <complex>) to enable the C++14 user-defined literal "
4548 "suffixes");
4549 if (ext)
4550 inform (token->location, "or use %<j%> instead of %<i%> for the "
4551 "GNU built-in suffix");
4552 }
4553 else if (!ext)
4554 inform (token->location, "use -fext-numeric-literals "
4555 "to enable more built-in suffixes");
4556
4557 if (kind == DK_ERROR)
4558 value = error_mark_node;
4559 else
4560 {
4561 /* Use the built-in semantics. */
4562 tree type;
4563 if (id_equal (suffix_id, "i"))
4564 {
4565 if (TREE_CODE (value) == INTEGER_CST)
4566 type = integer_type_node;
4567 else
4568 type = double_type_node;
4569 }
4570 else if (id_equal (suffix_id, "if"))
4571 type = float_type_node;
4572 else /* if (id_equal (suffix_id, "il")) */
4573 type = long_double_type_node;
4574
4575 value = build_complex (build_complex_type (type),
4576 fold_convert (type, integer_zero_node),
4577 fold_convert (type, value));
4578 }
4579
4580 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4581 /* Avoid repeated diagnostics. */
4582 token->u.value = value;
4583 return value;
4584 }
4585
4586 /* Parse a user-defined string constant. Returns a call to a user-defined
4587 literal operator taking a character pointer and the length of the string
4588 as arguments. */
4589
4590 static tree
4591 cp_parser_userdef_string_literal (tree literal)
4592 {
4593 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4594 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4595 tree value = USERDEF_LITERAL_VALUE (literal);
4596 int len = TREE_STRING_LENGTH (value)
4597 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4598 tree decl;
4599
4600 /* Build up a call to the user-defined operator. */
4601 /* Lookup the name we got back from the id-expression. */
4602 releasing_vec rargs;
4603 vec<tree, va_gc> *&args = rargs.get_ref();
4604 vec_safe_push (args, value);
4605 vec_safe_push (args, build_int_cst (size_type_node, len));
4606 decl = lookup_literal_operator (name, args);
4607
4608 if (decl && decl != error_mark_node)
4609 return finish_call_expr (decl, &args, false, true,
4610 tf_warning_or_error);
4611
4612 /* Look for a suitable template function, either (C++20) with a single
4613 parameter of class type, or (N3599) with typename parameter CharT and
4614 parameter pack CharT... */
4615 args->truncate (0);
4616 decl = lookup_literal_operator (name, args);
4617 if (decl && decl != error_mark_node)
4618 {
4619 /* Use resolve_nondeduced_context to try to choose one form of template
4620 or the other. */
4621 tree tmpl_args = make_tree_vec (1);
4622 TREE_VEC_ELT (tmpl_args, 0) = value;
4623 decl = lookup_template_function (decl, tmpl_args);
4624 tree res = resolve_nondeduced_context (decl, tf_none);
4625 if (DECL_P (res))
4626 decl = res;
4627 else
4628 {
4629 TREE_OPERAND (decl, 1) = make_string_pack (value);
4630 res = resolve_nondeduced_context (decl, tf_none);
4631 if (DECL_P (res))
4632 decl = res;
4633 }
4634 if (!DECL_P (decl) && cxx_dialect > cxx17)
4635 TREE_OPERAND (decl, 1) = tmpl_args;
4636 return finish_call_expr (decl, &args, false, true,
4637 tf_warning_or_error);
4638 }
4639
4640 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4641 name, TREE_TYPE (value), size_type_node);
4642 return error_mark_node;
4643 }
4644
4645
4646 /* Basic concepts [gram.basic] */
4647
4648 /* Parse a translation-unit.
4649
4650 translation-unit:
4651 declaration-seq [opt] */
4652
4653 static void
4654 cp_parser_translation_unit (cp_parser* parser)
4655 {
4656 gcc_checking_assert (!cp_error_declarator);
4657
4658 /* Create the declarator obstack. */
4659 gcc_obstack_init (&declarator_obstack);
4660 /* Create the error declarator. */
4661 cp_error_declarator = make_declarator (cdk_error);
4662 /* Create the empty parameter list. */
4663 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4664 UNKNOWN_LOCATION);
4665 /* Remember where the base of the declarator obstack lies. */
4666 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4667
4668 bool implicit_extern_c = false;
4669
4670 for (;;)
4671 {
4672 cp_token *token = cp_lexer_peek_token (parser->lexer);
4673
4674 /* If we're entering or exiting a region that's implicitly
4675 extern "C", modify the lang context appropriately. */
4676 if (implicit_extern_c
4677 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
4678 {
4679 implicit_extern_c = !implicit_extern_c;
4680 if (implicit_extern_c)
4681 push_lang_context (lang_name_c);
4682 else
4683 pop_lang_context ();
4684 }
4685
4686 if (token->type == CPP_EOF)
4687 break;
4688
4689 if (token->type == CPP_CLOSE_BRACE)
4690 {
4691 cp_parser_error (parser, "expected declaration");
4692 cp_lexer_consume_token (parser->lexer);
4693 /* If the next token is now a `;', consume it. */
4694 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4695 cp_lexer_consume_token (parser->lexer);
4696 }
4697 else
4698 cp_parser_toplevel_declaration (parser);
4699 }
4700
4701 /* Get rid of the token array; we don't need it any more. */
4702 cp_lexer_destroy (parser->lexer);
4703 parser->lexer = NULL;
4704
4705 /* The EOF should have reset this. */
4706 gcc_checking_assert (!implicit_extern_c);
4707
4708 /* Make sure the declarator obstack was fully cleaned up. */
4709 gcc_assert (obstack_next_free (&declarator_obstack)
4710 == declarator_obstack_base);
4711 }
4712
4713 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4714 decltype context. */
4715
4716 static inline tsubst_flags_t
4717 complain_flags (bool decltype_p)
4718 {
4719 tsubst_flags_t complain = tf_warning_or_error;
4720 if (decltype_p)
4721 complain |= tf_decltype;
4722 return complain;
4723 }
4724
4725 /* We're about to parse a collection of statements. If we're currently
4726 parsing tentatively, set up a firewall so that any nested
4727 cp_parser_commit_to_tentative_parse won't affect the current context. */
4728
4729 static cp_token_position
4730 cp_parser_start_tentative_firewall (cp_parser *parser)
4731 {
4732 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4733 return 0;
4734
4735 cp_parser_parse_tentatively (parser);
4736 cp_parser_commit_to_topmost_tentative_parse (parser);
4737 return cp_lexer_token_position (parser->lexer, false);
4738 }
4739
4740 /* We've finished parsing the collection of statements. Wrap up the
4741 firewall and replace the relevant tokens with the parsed form. */
4742
4743 static void
4744 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4745 tree expr)
4746 {
4747 if (!start)
4748 return;
4749
4750 /* Finish the firewall level. */
4751 cp_parser_parse_definitely (parser);
4752 /* And remember the result of the parse for when we try again. */
4753 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4754 token->type = CPP_PREPARSED_EXPR;
4755 token->u.value = expr;
4756 token->keyword = RID_MAX;
4757 cp_lexer_purge_tokens_after (parser->lexer, start);
4758 }
4759
4760 /* Like the above functions, but let the user modify the tokens. Used by
4761 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4762 later parses, so it makes sense to localize the effects of
4763 cp_parser_commit_to_tentative_parse. */
4764
4765 struct tentative_firewall
4766 {
4767 cp_parser *parser;
4768 bool set;
4769
4770 tentative_firewall (cp_parser *p): parser(p)
4771 {
4772 /* If we're currently parsing tentatively, start a committed level as a
4773 firewall and then an inner tentative parse. */
4774 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4775 {
4776 cp_parser_parse_tentatively (parser);
4777 cp_parser_commit_to_topmost_tentative_parse (parser);
4778 cp_parser_parse_tentatively (parser);
4779 }
4780 }
4781
4782 ~tentative_firewall()
4783 {
4784 if (set)
4785 {
4786 /* Finish the inner tentative parse and the firewall, propagating any
4787 uncommitted error state to the outer tentative parse. */
4788 bool err = cp_parser_error_occurred (parser);
4789 cp_parser_parse_definitely (parser);
4790 cp_parser_parse_definitely (parser);
4791 if (err)
4792 cp_parser_simulate_error (parser);
4793 }
4794 }
4795 };
4796
4797 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4798 This class is for tracking such a matching pair of symbols.
4799 In particular, it tracks the location of the first token,
4800 so that if the second token is missing, we can highlight the
4801 location of the first token when notifying the user about the
4802 problem. */
4803
4804 template <typename traits_t>
4805 class token_pair
4806 {
4807 public:
4808 /* token_pair's ctor. */
4809 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4810
4811 /* If the next token is the opening symbol for this pair, consume it and
4812 return true.
4813 Otherwise, issue an error and return false.
4814 In either case, record the location of the opening token. */
4815
4816 bool require_open (cp_parser *parser)
4817 {
4818 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4819 return cp_parser_require (parser, traits_t::open_token_type,
4820 traits_t::required_token_open);
4821 }
4822
4823 /* Consume the next token from PARSER, recording its location as
4824 that of the opening token within the pair. */
4825
4826 cp_token * consume_open (cp_parser *parser)
4827 {
4828 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4829 gcc_assert (tok->type == traits_t::open_token_type);
4830 m_open_loc = tok->location;
4831 return tok;
4832 }
4833
4834 /* If the next token is the closing symbol for this pair, consume it
4835 and return it.
4836 Otherwise, issue an error, highlighting the location of the
4837 corresponding opening token, and return NULL. */
4838
4839 cp_token *require_close (cp_parser *parser) const
4840 {
4841 return cp_parser_require (parser, traits_t::close_token_type,
4842 traits_t::required_token_close,
4843 m_open_loc);
4844 }
4845
4846 private:
4847 location_t m_open_loc;
4848 };
4849
4850 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4851
4852 struct matching_paren_traits
4853 {
4854 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4855 static const enum required_token required_token_open = RT_OPEN_PAREN;
4856 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4857 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4858 };
4859
4860 /* "matching_parens" is a token_pair<T> class for tracking matching
4861 pairs of parentheses. */
4862
4863 typedef token_pair<matching_paren_traits> matching_parens;
4864
4865 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4866
4867 struct matching_brace_traits
4868 {
4869 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4870 static const enum required_token required_token_open = RT_OPEN_BRACE;
4871 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4872 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4873 };
4874
4875 /* "matching_braces" is a token_pair<T> class for tracking matching
4876 pairs of braces. */
4877
4878 typedef token_pair<matching_brace_traits> matching_braces;
4879
4880
4881 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4882 enclosing parentheses. */
4883
4884 static cp_expr
4885 cp_parser_statement_expr (cp_parser *parser)
4886 {
4887 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4888
4889 /* Consume the '('. */
4890 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4891 matching_parens parens;
4892 parens.consume_open (parser);
4893 /* Start the statement-expression. */
4894 tree expr = begin_stmt_expr ();
4895 /* Parse the compound-statement. */
4896 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4897 /* Finish up. */
4898 expr = finish_stmt_expr (expr, false);
4899 /* Consume the ')'. */
4900 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4901 if (!parens.require_close (parser))
4902 cp_parser_skip_to_end_of_statement (parser);
4903
4904 cp_parser_end_tentative_firewall (parser, start, expr);
4905 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4906 return cp_expr (expr, combined_loc);
4907 }
4908
4909 /* Expressions [gram.expr] */
4910
4911 /* Parse a fold-operator.
4912
4913 fold-operator:
4914 - * / % ^ & | = < > << >>
4915 = -= *= /= %= ^= &= |= <<= >>=
4916 == != <= >= && || , .* ->*
4917
4918 This returns the tree code corresponding to the matched operator
4919 as an int. When the current token matches a compound assignment
4920 opertor, the resulting tree code is the negative value of the
4921 non-assignment operator. */
4922
4923 static int
4924 cp_parser_fold_operator (cp_token *token)
4925 {
4926 switch (token->type)
4927 {
4928 case CPP_PLUS: return PLUS_EXPR;
4929 case CPP_MINUS: return MINUS_EXPR;
4930 case CPP_MULT: return MULT_EXPR;
4931 case CPP_DIV: return TRUNC_DIV_EXPR;
4932 case CPP_MOD: return TRUNC_MOD_EXPR;
4933 case CPP_XOR: return BIT_XOR_EXPR;
4934 case CPP_AND: return BIT_AND_EXPR;
4935 case CPP_OR: return BIT_IOR_EXPR;
4936 case CPP_LSHIFT: return LSHIFT_EXPR;
4937 case CPP_RSHIFT: return RSHIFT_EXPR;
4938
4939 case CPP_EQ: return -NOP_EXPR;
4940 case CPP_PLUS_EQ: return -PLUS_EXPR;
4941 case CPP_MINUS_EQ: return -MINUS_EXPR;
4942 case CPP_MULT_EQ: return -MULT_EXPR;
4943 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4944 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4945 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4946 case CPP_AND_EQ: return -BIT_AND_EXPR;
4947 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4948 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4949 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4950
4951 case CPP_EQ_EQ: return EQ_EXPR;
4952 case CPP_NOT_EQ: return NE_EXPR;
4953 case CPP_LESS: return LT_EXPR;
4954 case CPP_GREATER: return GT_EXPR;
4955 case CPP_LESS_EQ: return LE_EXPR;
4956 case CPP_GREATER_EQ: return GE_EXPR;
4957
4958 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4959 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4960
4961 case CPP_COMMA: return COMPOUND_EXPR;
4962
4963 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4964 case CPP_DEREF_STAR: return MEMBER_REF;
4965
4966 default: return ERROR_MARK;
4967 }
4968 }
4969
4970 /* Returns true if CODE indicates a binary expression, which is not allowed in
4971 the LHS of a fold-expression. More codes will need to be added to use this
4972 function in other contexts. */
4973
4974 static bool
4975 is_binary_op (tree_code code)
4976 {
4977 switch (code)
4978 {
4979 case PLUS_EXPR:
4980 case POINTER_PLUS_EXPR:
4981 case MINUS_EXPR:
4982 case MULT_EXPR:
4983 case TRUNC_DIV_EXPR:
4984 case TRUNC_MOD_EXPR:
4985 case BIT_XOR_EXPR:
4986 case BIT_AND_EXPR:
4987 case BIT_IOR_EXPR:
4988 case LSHIFT_EXPR:
4989 case RSHIFT_EXPR:
4990
4991 case MODOP_EXPR:
4992
4993 case EQ_EXPR:
4994 case NE_EXPR:
4995 case LE_EXPR:
4996 case GE_EXPR:
4997 case LT_EXPR:
4998 case GT_EXPR:
4999
5000 case TRUTH_ANDIF_EXPR:
5001 case TRUTH_ORIF_EXPR:
5002
5003 case COMPOUND_EXPR:
5004
5005 case DOTSTAR_EXPR:
5006 case MEMBER_REF:
5007 return true;
5008
5009 default:
5010 return false;
5011 }
5012 }
5013
5014 /* If the next token is a suitable fold operator, consume it and return as
5015 the function above. */
5016
5017 static int
5018 cp_parser_fold_operator (cp_parser *parser)
5019 {
5020 cp_token* token = cp_lexer_peek_token (parser->lexer);
5021 int code = cp_parser_fold_operator (token);
5022 if (code != ERROR_MARK)
5023 cp_lexer_consume_token (parser->lexer);
5024 return code;
5025 }
5026
5027 /* Parse a fold-expression.
5028
5029 fold-expression:
5030 ( ... folding-operator cast-expression)
5031 ( cast-expression folding-operator ... )
5032 ( cast-expression folding operator ... folding-operator cast-expression)
5033
5034 Note that the '(' and ')' are matched in primary expression. */
5035
5036 static cp_expr
5037 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5038 {
5039 cp_id_kind pidk;
5040
5041 // Left fold.
5042 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5043 {
5044 cp_lexer_consume_token (parser->lexer);
5045 int op = cp_parser_fold_operator (parser);
5046 if (op == ERROR_MARK)
5047 {
5048 cp_parser_error (parser, "expected binary operator");
5049 return error_mark_node;
5050 }
5051
5052 tree expr = cp_parser_cast_expression (parser, false, false,
5053 false, &pidk);
5054 if (expr == error_mark_node)
5055 return error_mark_node;
5056 return finish_left_unary_fold_expr (expr, op);
5057 }
5058
5059 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5060 int op = cp_parser_fold_operator (parser);
5061 if (op == ERROR_MARK)
5062 {
5063 cp_parser_error (parser, "expected binary operator");
5064 return error_mark_node;
5065 }
5066
5067 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5068 {
5069 cp_parser_error (parser, "expected ...");
5070 return error_mark_node;
5071 }
5072 cp_lexer_consume_token (parser->lexer);
5073
5074 /* The operands of a fold-expression are cast-expressions, so binary or
5075 conditional expressions are not allowed. We check this here to avoid
5076 tentative parsing. */
5077 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
5078 /* OK, the expression was parenthesized. */;
5079 else if (is_binary_op (TREE_CODE (expr1)))
5080 error_at (location_of (expr1),
5081 "binary expression in operand of fold-expression");
5082 else if (TREE_CODE (expr1) == COND_EXPR
5083 || (REFERENCE_REF_P (expr1)
5084 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5085 error_at (location_of (expr1),
5086 "conditional expression in operand of fold-expression");
5087
5088 // Right fold.
5089 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5090 return finish_right_unary_fold_expr (expr1, op);
5091
5092 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5093 {
5094 cp_parser_error (parser, "mismatched operator in fold-expression");
5095 return error_mark_node;
5096 }
5097 cp_lexer_consume_token (parser->lexer);
5098
5099 // Binary left or right fold.
5100 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5101 if (expr2 == error_mark_node)
5102 return error_mark_node;
5103 return finish_binary_fold_expr (expr1, expr2, op);
5104 }
5105
5106 /* Parse a primary-expression.
5107
5108 primary-expression:
5109 literal
5110 this
5111 ( expression )
5112 id-expression
5113 lambda-expression (C++11)
5114
5115 GNU Extensions:
5116
5117 primary-expression:
5118 ( compound-statement )
5119 __builtin_va_arg ( assignment-expression , type-id )
5120 __builtin_offsetof ( type-id , offsetof-expression )
5121
5122 C++ Extensions:
5123 __has_nothrow_assign ( type-id )
5124 __has_nothrow_constructor ( type-id )
5125 __has_nothrow_copy ( type-id )
5126 __has_trivial_assign ( type-id )
5127 __has_trivial_constructor ( type-id )
5128 __has_trivial_copy ( type-id )
5129 __has_trivial_destructor ( type-id )
5130 __has_virtual_destructor ( type-id )
5131 __is_abstract ( type-id )
5132 __is_base_of ( type-id , type-id )
5133 __is_class ( type-id )
5134 __is_empty ( type-id )
5135 __is_enum ( type-id )
5136 __is_final ( type-id )
5137 __is_literal_type ( type-id )
5138 __is_pod ( type-id )
5139 __is_polymorphic ( type-id )
5140 __is_std_layout ( type-id )
5141 __is_trivial ( type-id )
5142 __is_union ( type-id )
5143
5144 Objective-C++ Extension:
5145
5146 primary-expression:
5147 objc-expression
5148
5149 literal:
5150 __null
5151
5152 ADDRESS_P is true iff this expression was immediately preceded by
5153 "&" and therefore might denote a pointer-to-member. CAST_P is true
5154 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5155 true iff this expression is a template argument.
5156
5157 Returns a representation of the expression. Upon return, *IDK
5158 indicates what kind of id-expression (if any) was present. */
5159
5160 static cp_expr
5161 cp_parser_primary_expression (cp_parser *parser,
5162 bool address_p,
5163 bool cast_p,
5164 bool template_arg_p,
5165 bool decltype_p,
5166 cp_id_kind *idk)
5167 {
5168 cp_token *token = NULL;
5169
5170 /* Assume the primary expression is not an id-expression. */
5171 *idk = CP_ID_KIND_NONE;
5172
5173 /* Peek at the next token. */
5174 token = cp_lexer_peek_token (parser->lexer);
5175 switch ((int) token->type)
5176 {
5177 /* literal:
5178 integer-literal
5179 character-literal
5180 floating-literal
5181 string-literal
5182 boolean-literal
5183 pointer-literal
5184 user-defined-literal */
5185 case CPP_CHAR:
5186 case CPP_CHAR16:
5187 case CPP_CHAR32:
5188 case CPP_WCHAR:
5189 case CPP_UTF8CHAR:
5190 case CPP_NUMBER:
5191 case CPP_PREPARSED_EXPR:
5192 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5193 return cp_parser_userdef_numeric_literal (parser);
5194 token = cp_lexer_consume_token (parser->lexer);
5195 if (TREE_CODE (token->u.value) == FIXED_CST)
5196 {
5197 error_at (token->location,
5198 "fixed-point types not supported in C++");
5199 return error_mark_node;
5200 }
5201 /* Floating-point literals are only allowed in an integral
5202 constant expression if they are cast to an integral or
5203 enumeration type. */
5204 if (TREE_CODE (token->u.value) == REAL_CST
5205 && parser->integral_constant_expression_p
5206 && pedantic)
5207 {
5208 /* CAST_P will be set even in invalid code like "int(2.7 +
5209 ...)". Therefore, we have to check that the next token
5210 is sure to end the cast. */
5211 if (cast_p)
5212 {
5213 cp_token *next_token;
5214
5215 next_token = cp_lexer_peek_token (parser->lexer);
5216 if (/* The comma at the end of an
5217 enumerator-definition. */
5218 next_token->type != CPP_COMMA
5219 /* The curly brace at the end of an enum-specifier. */
5220 && next_token->type != CPP_CLOSE_BRACE
5221 /* The end of a statement. */
5222 && next_token->type != CPP_SEMICOLON
5223 /* The end of the cast-expression. */
5224 && next_token->type != CPP_CLOSE_PAREN
5225 /* The end of an array bound. */
5226 && next_token->type != CPP_CLOSE_SQUARE
5227 /* The closing ">" in a template-argument-list. */
5228 && (next_token->type != CPP_GREATER
5229 || parser->greater_than_is_operator_p)
5230 /* C++0x only: A ">>" treated like two ">" tokens,
5231 in a template-argument-list. */
5232 && (next_token->type != CPP_RSHIFT
5233 || (cxx_dialect == cxx98)
5234 || parser->greater_than_is_operator_p))
5235 cast_p = false;
5236 }
5237
5238 /* If we are within a cast, then the constraint that the
5239 cast is to an integral or enumeration type will be
5240 checked at that point. If we are not within a cast, then
5241 this code is invalid. */
5242 if (!cast_p)
5243 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5244 }
5245 return (cp_expr (token->u.value, token->location)
5246 .maybe_add_location_wrapper ());
5247
5248 case CPP_CHAR_USERDEF:
5249 case CPP_CHAR16_USERDEF:
5250 case CPP_CHAR32_USERDEF:
5251 case CPP_WCHAR_USERDEF:
5252 case CPP_UTF8CHAR_USERDEF:
5253 return cp_parser_userdef_char_literal (parser);
5254
5255 case CPP_STRING:
5256 case CPP_STRING16:
5257 case CPP_STRING32:
5258 case CPP_WSTRING:
5259 case CPP_UTF8STRING:
5260 case CPP_STRING_USERDEF:
5261 case CPP_STRING16_USERDEF:
5262 case CPP_STRING32_USERDEF:
5263 case CPP_WSTRING_USERDEF:
5264 case CPP_UTF8STRING_USERDEF:
5265 /* ??? Should wide strings be allowed when parser->translate_strings_p
5266 is false (i.e. in attributes)? If not, we can kill the third
5267 argument to cp_parser_string_literal. */
5268 return (cp_parser_string_literal (parser,
5269 parser->translate_strings_p,
5270 true)
5271 .maybe_add_location_wrapper ());
5272
5273 case CPP_OPEN_PAREN:
5274 /* If we see `( { ' then we are looking at the beginning of
5275 a GNU statement-expression. */
5276 if (cp_parser_allow_gnu_extensions_p (parser)
5277 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5278 {
5279 /* Statement-expressions are not allowed by the standard. */
5280 pedwarn (token->location, OPT_Wpedantic,
5281 "ISO C++ forbids braced-groups within expressions");
5282
5283 /* And they're not allowed outside of a function-body; you
5284 cannot, for example, write:
5285
5286 int i = ({ int j = 3; j + 1; });
5287
5288 at class or namespace scope. */
5289 if (!parser->in_function_body
5290 || parser->in_template_argument_list_p)
5291 {
5292 error_at (token->location,
5293 "statement-expressions are not allowed outside "
5294 "functions nor in template-argument lists");
5295 cp_parser_skip_to_end_of_block_or_statement (parser);
5296 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5297 cp_lexer_consume_token (parser->lexer);
5298 return error_mark_node;
5299 }
5300 else
5301 return cp_parser_statement_expr (parser);
5302 }
5303 /* Otherwise it's a normal parenthesized expression. */
5304 {
5305 cp_expr expr;
5306 bool saved_greater_than_is_operator_p;
5307
5308 location_t open_paren_loc = token->location;
5309
5310 /* Consume the `('. */
5311 matching_parens parens;
5312 parens.consume_open (parser);
5313 /* Within a parenthesized expression, a `>' token is always
5314 the greater-than operator. */
5315 saved_greater_than_is_operator_p
5316 = parser->greater_than_is_operator_p;
5317 parser->greater_than_is_operator_p = true;
5318
5319 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5320 /* Left fold expression. */
5321 expr = NULL_TREE;
5322 else
5323 /* Parse the parenthesized expression. */
5324 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5325
5326 token = cp_lexer_peek_token (parser->lexer);
5327 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5328 {
5329 expr = cp_parser_fold_expression (parser, expr);
5330 if (expr != error_mark_node
5331 && cxx_dialect < cxx17
5332 && !in_system_header_at (input_location))
5333 pedwarn (input_location, 0, "fold-expressions only available "
5334 "with -std=c++17 or -std=gnu++17");
5335 }
5336 else
5337 /* Let the front end know that this expression was
5338 enclosed in parentheses. This matters in case, for
5339 example, the expression is of the form `A::B', since
5340 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5341 not. */
5342 expr = finish_parenthesized_expr (expr);
5343
5344 /* DR 705: Wrapping an unqualified name in parentheses
5345 suppresses arg-dependent lookup. We want to pass back
5346 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5347 (c++/37862), but none of the others. */
5348 if (*idk != CP_ID_KIND_QUALIFIED)
5349 *idk = CP_ID_KIND_NONE;
5350
5351 /* The `>' token might be the end of a template-id or
5352 template-parameter-list now. */
5353 parser->greater_than_is_operator_p
5354 = saved_greater_than_is_operator_p;
5355
5356 /* Consume the `)'. */
5357 token = cp_lexer_peek_token (parser->lexer);
5358 location_t close_paren_loc = token->location;
5359 expr.set_range (open_paren_loc, close_paren_loc);
5360 if (!parens.require_close (parser)
5361 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5362 cp_parser_skip_to_end_of_statement (parser);
5363
5364 return expr;
5365 }
5366
5367 case CPP_OPEN_SQUARE:
5368 {
5369 if (c_dialect_objc ())
5370 {
5371 /* We might have an Objective-C++ message. */
5372 cp_parser_parse_tentatively (parser);
5373 tree msg = cp_parser_objc_message_expression (parser);
5374 /* If that works out, we're done ... */
5375 if (cp_parser_parse_definitely (parser))
5376 return msg;
5377 /* ... else, fall though to see if it's a lambda. */
5378 }
5379 cp_expr lam = cp_parser_lambda_expression (parser);
5380 /* Don't warn about a failed tentative parse. */
5381 if (cp_parser_error_occurred (parser))
5382 return error_mark_node;
5383 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5384 return lam;
5385 }
5386
5387 case CPP_OBJC_STRING:
5388 if (c_dialect_objc ())
5389 /* We have an Objective-C++ string literal. */
5390 return cp_parser_objc_expression (parser);
5391 cp_parser_error (parser, "expected primary-expression");
5392 return error_mark_node;
5393
5394 case CPP_KEYWORD:
5395 switch (token->keyword)
5396 {
5397 /* These two are the boolean literals. */
5398 case RID_TRUE:
5399 cp_lexer_consume_token (parser->lexer);
5400 return cp_expr (boolean_true_node, token->location);
5401 case RID_FALSE:
5402 cp_lexer_consume_token (parser->lexer);
5403 return cp_expr (boolean_false_node, token->location);
5404
5405 /* The `__null' literal. */
5406 case RID_NULL:
5407 cp_lexer_consume_token (parser->lexer);
5408 return cp_expr (null_node, token->location);
5409
5410 /* The `nullptr' literal. */
5411 case RID_NULLPTR:
5412 cp_lexer_consume_token (parser->lexer);
5413 return cp_expr (nullptr_node, token->location);
5414
5415 /* Recognize the `this' keyword. */
5416 case RID_THIS:
5417 cp_lexer_consume_token (parser->lexer);
5418 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5419 {
5420 error_at (token->location,
5421 "%<this%> may not be used in this context");
5422 return error_mark_node;
5423 }
5424 /* Pointers cannot appear in constant-expressions. */
5425 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5426 return error_mark_node;
5427 return cp_expr (finish_this_expr (), token->location);
5428
5429 /* The `operator' keyword can be the beginning of an
5430 id-expression. */
5431 case RID_OPERATOR:
5432 goto id_expression;
5433
5434 case RID_FUNCTION_NAME:
5435 case RID_PRETTY_FUNCTION_NAME:
5436 case RID_C99_FUNCTION_NAME:
5437 {
5438 non_integral_constant name;
5439
5440 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5441 __func__ are the names of variables -- but they are
5442 treated specially. Therefore, they are handled here,
5443 rather than relying on the generic id-expression logic
5444 below. Grammatically, these names are id-expressions.
5445
5446 Consume the token. */
5447 token = cp_lexer_consume_token (parser->lexer);
5448
5449 switch (token->keyword)
5450 {
5451 case RID_FUNCTION_NAME:
5452 name = NIC_FUNC_NAME;
5453 break;
5454 case RID_PRETTY_FUNCTION_NAME:
5455 name = NIC_PRETTY_FUNC;
5456 break;
5457 case RID_C99_FUNCTION_NAME:
5458 name = NIC_C99_FUNC;
5459 break;
5460 default:
5461 gcc_unreachable ();
5462 }
5463
5464 if (cp_parser_non_integral_constant_expression (parser, name))
5465 return error_mark_node;
5466
5467 /* Look up the name. */
5468 return finish_fname (token->u.value);
5469 }
5470
5471 case RID_VA_ARG:
5472 {
5473 tree expression;
5474 tree type;
5475 location_t type_location;
5476 location_t start_loc
5477 = cp_lexer_peek_token (parser->lexer)->location;
5478 /* The `__builtin_va_arg' construct is used to handle
5479 `va_arg'. Consume the `__builtin_va_arg' token. */
5480 cp_lexer_consume_token (parser->lexer);
5481 /* Look for the opening `('. */
5482 matching_parens parens;
5483 parens.require_open (parser);
5484 /* Now, parse the assignment-expression. */
5485 expression = cp_parser_assignment_expression (parser);
5486 /* Look for the `,'. */
5487 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5488 type_location = cp_lexer_peek_token (parser->lexer)->location;
5489 /* Parse the type-id. */
5490 {
5491 type_id_in_expr_sentinel s (parser);
5492 type = cp_parser_type_id (parser);
5493 }
5494 /* Look for the closing `)'. */
5495 location_t finish_loc
5496 = cp_lexer_peek_token (parser->lexer)->location;
5497 parens.require_close (parser);
5498 /* Using `va_arg' in a constant-expression is not
5499 allowed. */
5500 if (cp_parser_non_integral_constant_expression (parser,
5501 NIC_VA_ARG))
5502 return error_mark_node;
5503 /* Construct a location of the form:
5504 __builtin_va_arg (v, int)
5505 ~~~~~~~~~~~~~~~~~~~~~^~~~
5506 with the caret at the type, ranging from the start of the
5507 "__builtin_va_arg" token to the close paren. */
5508 location_t combined_loc
5509 = make_location (type_location, start_loc, finish_loc);
5510 return build_x_va_arg (combined_loc, expression, type);
5511 }
5512
5513 case RID_OFFSETOF:
5514 return cp_parser_builtin_offsetof (parser);
5515
5516 case RID_HAS_NOTHROW_ASSIGN:
5517 case RID_HAS_NOTHROW_CONSTRUCTOR:
5518 case RID_HAS_NOTHROW_COPY:
5519 case RID_HAS_TRIVIAL_ASSIGN:
5520 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5521 case RID_HAS_TRIVIAL_COPY:
5522 case RID_HAS_TRIVIAL_DESTRUCTOR:
5523 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5524 case RID_HAS_VIRTUAL_DESTRUCTOR:
5525 case RID_IS_ABSTRACT:
5526 case RID_IS_AGGREGATE:
5527 case RID_IS_BASE_OF:
5528 case RID_IS_CLASS:
5529 case RID_IS_EMPTY:
5530 case RID_IS_ENUM:
5531 case RID_IS_FINAL:
5532 case RID_IS_LITERAL_TYPE:
5533 case RID_IS_POD:
5534 case RID_IS_POLYMORPHIC:
5535 case RID_IS_SAME_AS:
5536 case RID_IS_STD_LAYOUT:
5537 case RID_IS_TRIVIAL:
5538 case RID_IS_TRIVIALLY_ASSIGNABLE:
5539 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5540 case RID_IS_TRIVIALLY_COPYABLE:
5541 case RID_IS_UNION:
5542 case RID_IS_ASSIGNABLE:
5543 case RID_IS_CONSTRUCTIBLE:
5544 return cp_parser_trait_expr (parser, token->keyword);
5545
5546 // C++ concepts
5547 case RID_REQUIRES:
5548 return cp_parser_requires_expression (parser);
5549
5550 /* Objective-C++ expressions. */
5551 case RID_AT_ENCODE:
5552 case RID_AT_PROTOCOL:
5553 case RID_AT_SELECTOR:
5554 return cp_parser_objc_expression (parser);
5555
5556 case RID_TEMPLATE:
5557 if (parser->in_function_body
5558 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5559 == CPP_LESS))
5560 {
5561 error_at (token->location,
5562 "a template declaration cannot appear at block scope");
5563 cp_parser_skip_to_end_of_block_or_statement (parser);
5564 return error_mark_node;
5565 }
5566 /* FALLTHRU */
5567 default:
5568 cp_parser_error (parser, "expected primary-expression");
5569 return error_mark_node;
5570 }
5571
5572 /* An id-expression can start with either an identifier, a
5573 `::' as the beginning of a qualified-id, or the "operator"
5574 keyword. */
5575 case CPP_NAME:
5576 case CPP_SCOPE:
5577 case CPP_TEMPLATE_ID:
5578 case CPP_NESTED_NAME_SPECIFIER:
5579 {
5580 id_expression:
5581 cp_expr id_expression;
5582 cp_expr decl;
5583 const char *error_msg;
5584 bool template_p;
5585 bool done;
5586 cp_token *id_expr_token;
5587
5588 /* Parse the id-expression. */
5589 id_expression
5590 = cp_parser_id_expression (parser,
5591 /*template_keyword_p=*/false,
5592 /*check_dependency_p=*/true,
5593 &template_p,
5594 /*declarator_p=*/false,
5595 /*optional_p=*/false);
5596 if (id_expression == error_mark_node)
5597 return error_mark_node;
5598 id_expr_token = token;
5599 token = cp_lexer_peek_token (parser->lexer);
5600 done = (token->type != CPP_OPEN_SQUARE
5601 && token->type != CPP_OPEN_PAREN
5602 && token->type != CPP_DOT
5603 && token->type != CPP_DEREF
5604 && token->type != CPP_PLUS_PLUS
5605 && token->type != CPP_MINUS_MINUS);
5606 /* If we have a template-id, then no further lookup is
5607 required. If the template-id was for a template-class, we
5608 will sometimes have a TYPE_DECL at this point. */
5609 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5610 || TREE_CODE (id_expression) == TYPE_DECL)
5611 decl = id_expression;
5612 /* Look up the name. */
5613 else
5614 {
5615 tree ambiguous_decls;
5616
5617 /* If we already know that this lookup is ambiguous, then
5618 we've already issued an error message; there's no reason
5619 to check again. */
5620 if (id_expr_token->type == CPP_NAME
5621 && id_expr_token->error_reported)
5622 {
5623 cp_parser_simulate_error (parser);
5624 return error_mark_node;
5625 }
5626
5627 decl = cp_parser_lookup_name (parser, id_expression,
5628 none_type,
5629 template_p,
5630 /*is_namespace=*/false,
5631 /*check_dependency=*/true,
5632 &ambiguous_decls,
5633 id_expression.get_location ());
5634 /* If the lookup was ambiguous, an error will already have
5635 been issued. */
5636 if (ambiguous_decls)
5637 return error_mark_node;
5638
5639 /* In Objective-C++, we may have an Objective-C 2.0
5640 dot-syntax for classes here. */
5641 if (c_dialect_objc ()
5642 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5643 && TREE_CODE (decl) == TYPE_DECL
5644 && objc_is_class_name (decl))
5645 {
5646 tree component;
5647 cp_lexer_consume_token (parser->lexer);
5648 component = cp_parser_identifier (parser);
5649 if (component == error_mark_node)
5650 return error_mark_node;
5651
5652 tree result = objc_build_class_component_ref (id_expression,
5653 component);
5654 /* Build a location of the form:
5655 expr.component
5656 ~~~~~^~~~~~~~~
5657 with caret at the start of the component name (at
5658 input_location), ranging from the start of the id_expression
5659 to the end of the component name. */
5660 location_t combined_loc
5661 = make_location (input_location, id_expression.get_start (),
5662 get_finish (input_location));
5663 protected_set_expr_location (result, combined_loc);
5664 return result;
5665 }
5666
5667 /* In Objective-C++, an instance variable (ivar) may be preferred
5668 to whatever cp_parser_lookup_name() found.
5669 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5670 rest of c-family, we have to do a little extra work to preserve
5671 any location information in cp_expr "decl". Given that
5672 objc_lookup_ivar is implemented in "c-family" and "objc", we
5673 have a trip through the pure "tree" type, rather than cp_expr.
5674 Naively copying it back to "decl" would implicitly give the
5675 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5676 store an EXPR_LOCATION. Hence we only update "decl" (and
5677 hence its location_t) if we get back a different tree node. */
5678 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5679 id_expression);
5680 if (decl_tree != decl.get_value ())
5681 decl = cp_expr (decl_tree);
5682
5683 /* If name lookup gives us a SCOPE_REF, then the
5684 qualifying scope was dependent. */
5685 if (TREE_CODE (decl) == SCOPE_REF)
5686 {
5687 /* At this point, we do not know if DECL is a valid
5688 integral constant expression. We assume that it is
5689 in fact such an expression, so that code like:
5690
5691 template <int N> struct A {
5692 int a[B<N>::i];
5693 };
5694
5695 is accepted. At template-instantiation time, we
5696 will check that B<N>::i is actually a constant. */
5697 return decl;
5698 }
5699 /* Check to see if DECL is a local variable in a context
5700 where that is forbidden. */
5701 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
5702 && local_variable_p (decl))
5703 {
5704 error_at (id_expression.get_location (),
5705 "local variable %qD may not appear in this context",
5706 decl.get_value ());
5707 return error_mark_node;
5708 }
5709 }
5710
5711 decl = (finish_id_expression
5712 (id_expression, decl, parser->scope,
5713 idk,
5714 parser->integral_constant_expression_p,
5715 parser->allow_non_integral_constant_expression_p,
5716 &parser->non_integral_constant_expression_p,
5717 template_p, done, address_p,
5718 template_arg_p,
5719 &error_msg,
5720 id_expression.get_location ()));
5721 if (error_msg)
5722 cp_parser_error (parser, error_msg);
5723 /* Build a location for an id-expression of the form:
5724 ::ns::id
5725 ~~~~~~^~
5726 or:
5727 id
5728 ^~
5729 i.e. from the start of the first token to the end of the final
5730 token, with the caret at the start of the unqualified-id. */
5731 location_t caret_loc = get_pure_location (id_expression.get_location ());
5732 location_t start_loc = get_start (id_expr_token->location);
5733 location_t finish_loc = get_finish (id_expression.get_location ());
5734 location_t combined_loc
5735 = make_location (caret_loc, start_loc, finish_loc);
5736
5737 decl.set_location (combined_loc);
5738 return decl;
5739 }
5740
5741 /* Anything else is an error. */
5742 default:
5743 cp_parser_error (parser, "expected primary-expression");
5744 return error_mark_node;
5745 }
5746 }
5747
5748 static inline cp_expr
5749 cp_parser_primary_expression (cp_parser *parser,
5750 bool address_p,
5751 bool cast_p,
5752 bool template_arg_p,
5753 cp_id_kind *idk)
5754 {
5755 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5756 /*decltype*/false, idk);
5757 }
5758
5759 /* Parse an id-expression.
5760
5761 id-expression:
5762 unqualified-id
5763 qualified-id
5764
5765 qualified-id:
5766 :: [opt] nested-name-specifier template [opt] unqualified-id
5767 :: identifier
5768 :: operator-function-id
5769 :: template-id
5770
5771 Return a representation of the unqualified portion of the
5772 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5773 a `::' or nested-name-specifier.
5774
5775 Often, if the id-expression was a qualified-id, the caller will
5776 want to make a SCOPE_REF to represent the qualified-id. This
5777 function does not do this in order to avoid wastefully creating
5778 SCOPE_REFs when they are not required.
5779
5780 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5781 `template' keyword.
5782
5783 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5784 uninstantiated templates.
5785
5786 If *TEMPLATE_P is non-NULL, it is set to true iff the
5787 `template' keyword is used to explicitly indicate that the entity
5788 named is a template.
5789
5790 If DECLARATOR_P is true, the id-expression is appearing as part of
5791 a declarator, rather than as part of an expression. */
5792
5793 static cp_expr
5794 cp_parser_id_expression (cp_parser *parser,
5795 bool template_keyword_p,
5796 bool check_dependency_p,
5797 bool *template_p,
5798 bool declarator_p,
5799 bool optional_p)
5800 {
5801 bool global_scope_p;
5802 bool nested_name_specifier_p;
5803
5804 /* Assume the `template' keyword was not used. */
5805 if (template_p)
5806 *template_p = template_keyword_p;
5807
5808 /* Look for the optional `::' operator. */
5809 global_scope_p
5810 = (!template_keyword_p
5811 && (cp_parser_global_scope_opt (parser,
5812 /*current_scope_valid_p=*/false)
5813 != NULL_TREE));
5814
5815 /* Look for the optional nested-name-specifier. */
5816 nested_name_specifier_p
5817 = (cp_parser_nested_name_specifier_opt (parser,
5818 /*typename_keyword_p=*/false,
5819 check_dependency_p,
5820 /*type_p=*/false,
5821 declarator_p,
5822 template_keyword_p)
5823 != NULL_TREE);
5824
5825 /* If there is a nested-name-specifier, then we are looking at
5826 the first qualified-id production. */
5827 if (nested_name_specifier_p)
5828 {
5829 tree saved_scope;
5830 tree saved_object_scope;
5831 tree saved_qualifying_scope;
5832 cp_expr unqualified_id;
5833 bool is_template;
5834
5835 /* See if the next token is the `template' keyword. */
5836 if (!template_p)
5837 template_p = &is_template;
5838 *template_p = cp_parser_optional_template_keyword (parser);
5839 /* Name lookup we do during the processing of the
5840 unqualified-id might obliterate SCOPE. */
5841 saved_scope = parser->scope;
5842 saved_object_scope = parser->object_scope;
5843 saved_qualifying_scope = parser->qualifying_scope;
5844 /* Process the final unqualified-id. */
5845 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5846 check_dependency_p,
5847 declarator_p,
5848 /*optional_p=*/false);
5849 /* Restore the SAVED_SCOPE for our caller. */
5850 parser->scope = saved_scope;
5851 parser->object_scope = saved_object_scope;
5852 parser->qualifying_scope = saved_qualifying_scope;
5853
5854 return unqualified_id;
5855 }
5856 /* Otherwise, if we are in global scope, then we are looking at one
5857 of the other qualified-id productions. */
5858 else if (global_scope_p)
5859 {
5860 cp_token *token;
5861 tree id;
5862
5863 /* Peek at the next token. */
5864 token = cp_lexer_peek_token (parser->lexer);
5865
5866 /* If it's an identifier, and the next token is not a "<", then
5867 we can avoid the template-id case. This is an optimization
5868 for this common case. */
5869 if (token->type == CPP_NAME
5870 && !cp_parser_nth_token_starts_template_argument_list_p
5871 (parser, 2))
5872 return cp_parser_identifier (parser);
5873
5874 cp_parser_parse_tentatively (parser);
5875 /* Try a template-id. */
5876 id = cp_parser_template_id (parser,
5877 /*template_keyword_p=*/false,
5878 /*check_dependency_p=*/true,
5879 none_type,
5880 declarator_p);
5881 /* If that worked, we're done. */
5882 if (cp_parser_parse_definitely (parser))
5883 return id;
5884
5885 /* Peek at the next token. (Changes in the token buffer may
5886 have invalidated the pointer obtained above.) */
5887 token = cp_lexer_peek_token (parser->lexer);
5888
5889 switch (token->type)
5890 {
5891 case CPP_NAME:
5892 return cp_parser_identifier (parser);
5893
5894 case CPP_KEYWORD:
5895 if (token->keyword == RID_OPERATOR)
5896 return cp_parser_operator_function_id (parser);
5897 /* Fall through. */
5898
5899 default:
5900 cp_parser_error (parser, "expected id-expression");
5901 return error_mark_node;
5902 }
5903 }
5904 else
5905 return cp_parser_unqualified_id (parser, template_keyword_p,
5906 /*check_dependency_p=*/true,
5907 declarator_p,
5908 optional_p);
5909 }
5910
5911 /* Parse an unqualified-id.
5912
5913 unqualified-id:
5914 identifier
5915 operator-function-id
5916 conversion-function-id
5917 ~ class-name
5918 template-id
5919
5920 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5921 keyword, in a construct like `A::template ...'.
5922
5923 Returns a representation of unqualified-id. For the `identifier'
5924 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5925 production a BIT_NOT_EXPR is returned; the operand of the
5926 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5927 other productions, see the documentation accompanying the
5928 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5929 names are looked up in uninstantiated templates. If DECLARATOR_P
5930 is true, the unqualified-id is appearing as part of a declarator,
5931 rather than as part of an expression. */
5932
5933 static cp_expr
5934 cp_parser_unqualified_id (cp_parser* parser,
5935 bool template_keyword_p,
5936 bool check_dependency_p,
5937 bool declarator_p,
5938 bool optional_p)
5939 {
5940 cp_token *token;
5941
5942 /* Peek at the next token. */
5943 token = cp_lexer_peek_token (parser->lexer);
5944
5945 switch ((int) token->type)
5946 {
5947 case CPP_NAME:
5948 {
5949 tree id;
5950
5951 /* We don't know yet whether or not this will be a
5952 template-id. */
5953 cp_parser_parse_tentatively (parser);
5954 /* Try a template-id. */
5955 id = cp_parser_template_id (parser, template_keyword_p,
5956 check_dependency_p,
5957 none_type,
5958 declarator_p);
5959 /* If it worked, we're done. */
5960 if (cp_parser_parse_definitely (parser))
5961 return id;
5962 /* Otherwise, it's an ordinary identifier. */
5963 return cp_parser_identifier (parser);
5964 }
5965
5966 case CPP_TEMPLATE_ID:
5967 return cp_parser_template_id (parser, template_keyword_p,
5968 check_dependency_p,
5969 none_type,
5970 declarator_p);
5971
5972 case CPP_COMPL:
5973 {
5974 tree type_decl;
5975 tree qualifying_scope;
5976 tree object_scope;
5977 tree scope;
5978 bool done;
5979
5980 /* Consume the `~' token. */
5981 cp_lexer_consume_token (parser->lexer);
5982 /* Parse the class-name. The standard, as written, seems to
5983 say that:
5984
5985 template <typename T> struct S { ~S (); };
5986 template <typename T> S<T>::~S() {}
5987
5988 is invalid, since `~' must be followed by a class-name, but
5989 `S<T>' is dependent, and so not known to be a class.
5990 That's not right; we need to look in uninstantiated
5991 templates. A further complication arises from:
5992
5993 template <typename T> void f(T t) {
5994 t.T::~T();
5995 }
5996
5997 Here, it is not possible to look up `T' in the scope of `T'
5998 itself. We must look in both the current scope, and the
5999 scope of the containing complete expression.
6000
6001 Yet another issue is:
6002
6003 struct S {
6004 int S;
6005 ~S();
6006 };
6007
6008 S::~S() {}
6009
6010 The standard does not seem to say that the `S' in `~S'
6011 should refer to the type `S' and not the data member
6012 `S::S'. */
6013
6014 /* DR 244 says that we look up the name after the "~" in the
6015 same scope as we looked up the qualifying name. That idea
6016 isn't fully worked out; it's more complicated than that. */
6017 scope = parser->scope;
6018 object_scope = parser->object_scope;
6019 qualifying_scope = parser->qualifying_scope;
6020
6021 /* Check for invalid scopes. */
6022 if (scope == error_mark_node)
6023 {
6024 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6025 cp_lexer_consume_token (parser->lexer);
6026 return error_mark_node;
6027 }
6028 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6029 {
6030 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6031 error_at (token->location,
6032 "scope %qT before %<~%> is not a class-name",
6033 scope);
6034 cp_parser_simulate_error (parser);
6035 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6036 cp_lexer_consume_token (parser->lexer);
6037 return error_mark_node;
6038 }
6039 gcc_assert (!scope || TYPE_P (scope));
6040
6041 /* If the name is of the form "X::~X" it's OK even if X is a
6042 typedef. */
6043 token = cp_lexer_peek_token (parser->lexer);
6044 if (scope
6045 && token->type == CPP_NAME
6046 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6047 != CPP_LESS)
6048 && (token->u.value == TYPE_IDENTIFIER (scope)
6049 || (CLASS_TYPE_P (scope)
6050 && constructor_name_p (token->u.value, scope))))
6051 {
6052 cp_lexer_consume_token (parser->lexer);
6053 return build_nt (BIT_NOT_EXPR, scope);
6054 }
6055
6056 /* ~auto means the destructor of whatever the object is. */
6057 if (cp_parser_is_keyword (token, RID_AUTO))
6058 {
6059 if (cxx_dialect < cxx14)
6060 pedwarn (input_location, 0,
6061 "%<~auto%> only available with "
6062 "-std=c++14 or -std=gnu++14");
6063 cp_lexer_consume_token (parser->lexer);
6064 return build_nt (BIT_NOT_EXPR, make_auto ());
6065 }
6066
6067 /* If there was an explicit qualification (S::~T), first look
6068 in the scope given by the qualification (i.e., S).
6069
6070 Note: in the calls to cp_parser_class_name below we pass
6071 typename_type so that lookup finds the injected-class-name
6072 rather than the constructor. */
6073 done = false;
6074 type_decl = NULL_TREE;
6075 if (scope)
6076 {
6077 cp_parser_parse_tentatively (parser);
6078 type_decl = cp_parser_class_name (parser,
6079 /*typename_keyword_p=*/false,
6080 /*template_keyword_p=*/false,
6081 typename_type,
6082 /*check_dependency=*/false,
6083 /*class_head_p=*/false,
6084 declarator_p);
6085 if (cp_parser_parse_definitely (parser))
6086 done = true;
6087 }
6088 /* In "N::S::~S", look in "N" as well. */
6089 if (!done && scope && qualifying_scope)
6090 {
6091 cp_parser_parse_tentatively (parser);
6092 parser->scope = qualifying_scope;
6093 parser->object_scope = NULL_TREE;
6094 parser->qualifying_scope = NULL_TREE;
6095 type_decl
6096 = cp_parser_class_name (parser,
6097 /*typename_keyword_p=*/false,
6098 /*template_keyword_p=*/false,
6099 typename_type,
6100 /*check_dependency=*/false,
6101 /*class_head_p=*/false,
6102 declarator_p);
6103 if (cp_parser_parse_definitely (parser))
6104 done = true;
6105 }
6106 /* In "p->S::~T", look in the scope given by "*p" as well. */
6107 else if (!done && object_scope)
6108 {
6109 cp_parser_parse_tentatively (parser);
6110 parser->scope = object_scope;
6111 parser->object_scope = NULL_TREE;
6112 parser->qualifying_scope = NULL_TREE;
6113 type_decl
6114 = cp_parser_class_name (parser,
6115 /*typename_keyword_p=*/false,
6116 /*template_keyword_p=*/false,
6117 typename_type,
6118 /*check_dependency=*/false,
6119 /*class_head_p=*/false,
6120 declarator_p);
6121 if (cp_parser_parse_definitely (parser))
6122 done = true;
6123 }
6124 /* Look in the surrounding context. */
6125 if (!done)
6126 {
6127 parser->scope = NULL_TREE;
6128 parser->object_scope = NULL_TREE;
6129 parser->qualifying_scope = NULL_TREE;
6130 if (processing_template_decl)
6131 cp_parser_parse_tentatively (parser);
6132 type_decl
6133 = cp_parser_class_name (parser,
6134 /*typename_keyword_p=*/false,
6135 /*template_keyword_p=*/false,
6136 typename_type,
6137 /*check_dependency=*/false,
6138 /*class_head_p=*/false,
6139 declarator_p);
6140 if (processing_template_decl
6141 && ! cp_parser_parse_definitely (parser))
6142 {
6143 /* We couldn't find a type with this name. If we're parsing
6144 tentatively, fail and try something else. */
6145 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6146 {
6147 cp_parser_simulate_error (parser);
6148 return error_mark_node;
6149 }
6150 /* Otherwise, accept it and check for a match at instantiation
6151 time. */
6152 type_decl = cp_parser_identifier (parser);
6153 if (type_decl != error_mark_node)
6154 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
6155 return type_decl;
6156 }
6157 }
6158 /* If an error occurred, assume that the name of the
6159 destructor is the same as the name of the qualifying
6160 class. That allows us to keep parsing after running
6161 into ill-formed destructor names. */
6162 if (type_decl == error_mark_node && scope)
6163 return build_nt (BIT_NOT_EXPR, scope);
6164 else if (type_decl == error_mark_node)
6165 return error_mark_node;
6166
6167 /* Check that destructor name and scope match. */
6168 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6169 {
6170 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6171 error_at (token->location,
6172 "declaration of %<~%T%> as member of %qT",
6173 type_decl, scope);
6174 cp_parser_simulate_error (parser);
6175 return error_mark_node;
6176 }
6177
6178 /* [class.dtor]
6179
6180 A typedef-name that names a class shall not be used as the
6181 identifier in the declarator for a destructor declaration. */
6182 if (declarator_p
6183 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6184 && !DECL_SELF_REFERENCE_P (type_decl)
6185 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6186 error_at (token->location,
6187 "typedef-name %qD used as destructor declarator",
6188 type_decl);
6189
6190 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6191 }
6192
6193 case CPP_KEYWORD:
6194 if (token->keyword == RID_OPERATOR)
6195 {
6196 cp_expr id;
6197
6198 /* This could be a template-id, so we try that first. */
6199 cp_parser_parse_tentatively (parser);
6200 /* Try a template-id. */
6201 id = cp_parser_template_id (parser, template_keyword_p,
6202 /*check_dependency_p=*/true,
6203 none_type,
6204 declarator_p);
6205 /* If that worked, we're done. */
6206 if (cp_parser_parse_definitely (parser))
6207 return id;
6208 /* We still don't know whether we're looking at an
6209 operator-function-id or a conversion-function-id. */
6210 cp_parser_parse_tentatively (parser);
6211 /* Try an operator-function-id. */
6212 id = cp_parser_operator_function_id (parser);
6213 /* If that didn't work, try a conversion-function-id. */
6214 if (!cp_parser_parse_definitely (parser))
6215 id = cp_parser_conversion_function_id (parser);
6216
6217 return id;
6218 }
6219 /* Fall through. */
6220
6221 default:
6222 if (optional_p)
6223 return NULL_TREE;
6224 cp_parser_error (parser, "expected unqualified-id");
6225 return error_mark_node;
6226 }
6227 }
6228
6229 /* Parse an (optional) nested-name-specifier.
6230
6231 nested-name-specifier: [C++98]
6232 class-or-namespace-name :: nested-name-specifier [opt]
6233 class-or-namespace-name :: template nested-name-specifier [opt]
6234
6235 nested-name-specifier: [C++0x]
6236 type-name ::
6237 namespace-name ::
6238 nested-name-specifier identifier ::
6239 nested-name-specifier template [opt] simple-template-id ::
6240
6241 PARSER->SCOPE should be set appropriately before this function is
6242 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6243 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6244 in name lookups.
6245
6246 Sets PARSER->SCOPE to the class (TYPE) or namespace
6247 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6248 it unchanged if there is no nested-name-specifier. Returns the new
6249 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6250
6251 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6252 part of a declaration and/or decl-specifier. */
6253
6254 static tree
6255 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6256 bool typename_keyword_p,
6257 bool check_dependency_p,
6258 bool type_p,
6259 bool is_declaration,
6260 bool template_keyword_p /* = false */)
6261 {
6262 bool success = false;
6263 cp_token_position start = 0;
6264 cp_token *token;
6265
6266 /* Remember where the nested-name-specifier starts. */
6267 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6268 {
6269 start = cp_lexer_token_position (parser->lexer, false);
6270 push_deferring_access_checks (dk_deferred);
6271 }
6272
6273 while (true)
6274 {
6275 tree new_scope;
6276 tree old_scope;
6277 tree saved_qualifying_scope;
6278
6279 /* Spot cases that cannot be the beginning of a
6280 nested-name-specifier. */
6281 token = cp_lexer_peek_token (parser->lexer);
6282
6283 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6284 the already parsed nested-name-specifier. */
6285 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6286 {
6287 /* Grab the nested-name-specifier and continue the loop. */
6288 cp_parser_pre_parsed_nested_name_specifier (parser);
6289 /* If we originally encountered this nested-name-specifier
6290 with IS_DECLARATION set to false, we will not have
6291 resolved TYPENAME_TYPEs, so we must do so here. */
6292 if (is_declaration
6293 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6294 {
6295 new_scope = resolve_typename_type (parser->scope,
6296 /*only_current_p=*/false);
6297 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6298 parser->scope = new_scope;
6299 }
6300 success = true;
6301 continue;
6302 }
6303
6304 /* Spot cases that cannot be the beginning of a
6305 nested-name-specifier. On the second and subsequent times
6306 through the loop, we look for the `template' keyword. */
6307 if (success && token->keyword == RID_TEMPLATE)
6308 ;
6309 /* A template-id can start a nested-name-specifier. */
6310 else if (token->type == CPP_TEMPLATE_ID)
6311 ;
6312 /* DR 743: decltype can be used in a nested-name-specifier. */
6313 else if (token_is_decltype (token))
6314 ;
6315 else
6316 {
6317 /* If the next token is not an identifier, then it is
6318 definitely not a type-name or namespace-name. */
6319 if (token->type != CPP_NAME)
6320 break;
6321 /* If the following token is neither a `<' (to begin a
6322 template-id), nor a `::', then we are not looking at a
6323 nested-name-specifier. */
6324 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6325
6326 if (token->type == CPP_COLON
6327 && parser->colon_corrects_to_scope_p
6328 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6329 {
6330 gcc_rich_location richloc (token->location);
6331 richloc.add_fixit_replace ("::");
6332 error_at (&richloc,
6333 "found %<:%> in nested-name-specifier, "
6334 "expected %<::%>");
6335 token->type = CPP_SCOPE;
6336 }
6337
6338 if (token->type != CPP_SCOPE
6339 && !cp_parser_nth_token_starts_template_argument_list_p
6340 (parser, 2))
6341 break;
6342 }
6343
6344 /* The nested-name-specifier is optional, so we parse
6345 tentatively. */
6346 cp_parser_parse_tentatively (parser);
6347
6348 /* Look for the optional `template' keyword, if this isn't the
6349 first time through the loop. */
6350 if (success)
6351 template_keyword_p = cp_parser_optional_template_keyword (parser);
6352
6353 /* Save the old scope since the name lookup we are about to do
6354 might destroy it. */
6355 old_scope = parser->scope;
6356 saved_qualifying_scope = parser->qualifying_scope;
6357 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6358 look up names in "X<T>::I" in order to determine that "Y" is
6359 a template. So, if we have a typename at this point, we make
6360 an effort to look through it. */
6361 if (is_declaration
6362 && !typename_keyword_p
6363 && parser->scope
6364 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6365 parser->scope = resolve_typename_type (parser->scope,
6366 /*only_current_p=*/false);
6367 /* Parse the qualifying entity. */
6368 new_scope
6369 = cp_parser_qualifying_entity (parser,
6370 typename_keyword_p,
6371 template_keyword_p,
6372 check_dependency_p,
6373 type_p,
6374 is_declaration);
6375 /* Look for the `::' token. */
6376 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6377
6378 /* If we found what we wanted, we keep going; otherwise, we're
6379 done. */
6380 if (!cp_parser_parse_definitely (parser))
6381 {
6382 bool error_p = false;
6383
6384 /* Restore the OLD_SCOPE since it was valid before the
6385 failed attempt at finding the last
6386 class-or-namespace-name. */
6387 parser->scope = old_scope;
6388 parser->qualifying_scope = saved_qualifying_scope;
6389
6390 /* If the next token is a decltype, and the one after that is a
6391 `::', then the decltype has failed to resolve to a class or
6392 enumeration type. Give this error even when parsing
6393 tentatively since it can't possibly be valid--and we're going
6394 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6395 won't get another chance.*/
6396 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6397 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6398 == CPP_SCOPE))
6399 {
6400 token = cp_lexer_consume_token (parser->lexer);
6401 error_at (token->location, "decltype evaluates to %qT, "
6402 "which is not a class or enumeration type",
6403 token->u.tree_check_value->value);
6404 parser->scope = error_mark_node;
6405 error_p = true;
6406 /* As below. */
6407 success = true;
6408 cp_lexer_consume_token (parser->lexer);
6409 }
6410
6411 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6412 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6413 {
6414 /* If we have a non-type template-id followed by ::, it can't
6415 possibly be valid. */
6416 token = cp_lexer_peek_token (parser->lexer);
6417 tree tid = token->u.tree_check_value->value;
6418 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6419 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6420 {
6421 tree tmpl = NULL_TREE;
6422 if (is_overloaded_fn (tid))
6423 {
6424 tree fns = get_fns (tid);
6425 if (OVL_SINGLE_P (fns))
6426 tmpl = OVL_FIRST (fns);
6427 error_at (token->location, "function template-id %qD "
6428 "in nested-name-specifier", tid);
6429 }
6430 else
6431 {
6432 /* Variable template. */
6433 tmpl = TREE_OPERAND (tid, 0);
6434 gcc_assert (variable_template_p (tmpl));
6435 error_at (token->location, "variable template-id %qD "
6436 "in nested-name-specifier", tid);
6437 }
6438 if (tmpl)
6439 inform (DECL_SOURCE_LOCATION (tmpl),
6440 "%qD declared here", tmpl);
6441
6442 parser->scope = error_mark_node;
6443 error_p = true;
6444 /* As below. */
6445 success = true;
6446 cp_lexer_consume_token (parser->lexer);
6447 cp_lexer_consume_token (parser->lexer);
6448 }
6449 }
6450
6451 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6452 break;
6453 /* If the next token is an identifier, and the one after
6454 that is a `::', then any valid interpretation would have
6455 found a class-or-namespace-name. */
6456 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6457 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6458 == CPP_SCOPE)
6459 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6460 != CPP_COMPL))
6461 {
6462 token = cp_lexer_consume_token (parser->lexer);
6463 if (!error_p)
6464 {
6465 if (!token->error_reported)
6466 {
6467 tree decl;
6468 tree ambiguous_decls;
6469
6470 decl = cp_parser_lookup_name (parser, token->u.value,
6471 none_type,
6472 /*is_template=*/false,
6473 /*is_namespace=*/false,
6474 /*check_dependency=*/true,
6475 &ambiguous_decls,
6476 token->location);
6477 if (TREE_CODE (decl) == TEMPLATE_DECL)
6478 error_at (token->location,
6479 "%qD used without template arguments",
6480 decl);
6481 else if (ambiguous_decls)
6482 {
6483 // cp_parser_lookup_name has the same diagnostic,
6484 // thus make sure to emit it at most once.
6485 if (cp_parser_uncommitted_to_tentative_parse_p
6486 (parser))
6487 {
6488 error_at (token->location,
6489 "reference to %qD is ambiguous",
6490 token->u.value);
6491 print_candidates (ambiguous_decls);
6492 }
6493 decl = error_mark_node;
6494 }
6495 else
6496 {
6497 if (cxx_dialect != cxx98)
6498 cp_parser_name_lookup_error
6499 (parser, token->u.value, decl, NLE_NOT_CXX98,
6500 token->location);
6501 else
6502 cp_parser_name_lookup_error
6503 (parser, token->u.value, decl, NLE_CXX98,
6504 token->location);
6505 }
6506 }
6507 parser->scope = error_mark_node;
6508 error_p = true;
6509 /* Treat this as a successful nested-name-specifier
6510 due to:
6511
6512 [basic.lookup.qual]
6513
6514 If the name found is not a class-name (clause
6515 _class_) or namespace-name (_namespace.def_), the
6516 program is ill-formed. */
6517 success = true;
6518 }
6519 cp_lexer_consume_token (parser->lexer);
6520 }
6521 break;
6522 }
6523 /* We've found one valid nested-name-specifier. */
6524 success = true;
6525 /* Name lookup always gives us a DECL. */
6526 if (TREE_CODE (new_scope) == TYPE_DECL)
6527 new_scope = TREE_TYPE (new_scope);
6528 /* Uses of "template" must be followed by actual templates. */
6529 if (template_keyword_p
6530 && !(CLASS_TYPE_P (new_scope)
6531 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6532 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6533 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6534 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6535 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6536 == TEMPLATE_ID_EXPR)))
6537 permerror (input_location, TYPE_P (new_scope)
6538 ? G_("%qT is not a template")
6539 : G_("%qD is not a template"),
6540 new_scope);
6541 /* If it is a class scope, try to complete it; we are about to
6542 be looking up names inside the class. */
6543 if (TYPE_P (new_scope)
6544 /* Since checking types for dependency can be expensive,
6545 avoid doing it if the type is already complete. */
6546 && !COMPLETE_TYPE_P (new_scope)
6547 /* Do not try to complete dependent types. */
6548 && !dependent_type_p (new_scope))
6549 {
6550 new_scope = complete_type (new_scope);
6551 /* If it is a typedef to current class, use the current
6552 class instead, as the typedef won't have any names inside
6553 it yet. */
6554 if (!COMPLETE_TYPE_P (new_scope)
6555 && currently_open_class (new_scope))
6556 new_scope = TYPE_MAIN_VARIANT (new_scope);
6557 }
6558 /* Make sure we look in the right scope the next time through
6559 the loop. */
6560 parser->scope = new_scope;
6561 }
6562
6563 /* If parsing tentatively, replace the sequence of tokens that makes
6564 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6565 token. That way, should we re-parse the token stream, we will
6566 not have to repeat the effort required to do the parse, nor will
6567 we issue duplicate error messages. */
6568 if (success && start)
6569 {
6570 cp_token *token;
6571
6572 token = cp_lexer_token_at (parser->lexer, start);
6573 /* Reset the contents of the START token. */
6574 token->type = CPP_NESTED_NAME_SPECIFIER;
6575 /* Retrieve any deferred checks. Do not pop this access checks yet
6576 so the memory will not be reclaimed during token replacing below. */
6577 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6578 token->u.tree_check_value->value = parser->scope;
6579 token->u.tree_check_value->checks = get_deferred_access_checks ();
6580 token->u.tree_check_value->qualifying_scope =
6581 parser->qualifying_scope;
6582 token->keyword = RID_MAX;
6583
6584 /* Purge all subsequent tokens. */
6585 cp_lexer_purge_tokens_after (parser->lexer, start);
6586 }
6587
6588 if (start)
6589 pop_to_parent_deferring_access_checks ();
6590
6591 return success ? parser->scope : NULL_TREE;
6592 }
6593
6594 /* Parse a nested-name-specifier. See
6595 cp_parser_nested_name_specifier_opt for details. This function
6596 behaves identically, except that it will an issue an error if no
6597 nested-name-specifier is present. */
6598
6599 static tree
6600 cp_parser_nested_name_specifier (cp_parser *parser,
6601 bool typename_keyword_p,
6602 bool check_dependency_p,
6603 bool type_p,
6604 bool is_declaration)
6605 {
6606 tree scope;
6607
6608 /* Look for the nested-name-specifier. */
6609 scope = cp_parser_nested_name_specifier_opt (parser,
6610 typename_keyword_p,
6611 check_dependency_p,
6612 type_p,
6613 is_declaration);
6614 /* If it was not present, issue an error message. */
6615 if (!scope)
6616 {
6617 cp_parser_error (parser, "expected nested-name-specifier");
6618 parser->scope = NULL_TREE;
6619 }
6620
6621 return scope;
6622 }
6623
6624 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6625 this is either a class-name or a namespace-name (which corresponds
6626 to the class-or-namespace-name production in the grammar). For
6627 C++0x, it can also be a type-name that refers to an enumeration
6628 type or a simple-template-id.
6629
6630 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6631 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6632 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6633 TYPE_P is TRUE iff the next name should be taken as a class-name,
6634 even the same name is declared to be another entity in the same
6635 scope.
6636
6637 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6638 specified by the class-or-namespace-name. If neither is found the
6639 ERROR_MARK_NODE is returned. */
6640
6641 static tree
6642 cp_parser_qualifying_entity (cp_parser *parser,
6643 bool typename_keyword_p,
6644 bool template_keyword_p,
6645 bool check_dependency_p,
6646 bool type_p,
6647 bool is_declaration)
6648 {
6649 tree saved_scope;
6650 tree saved_qualifying_scope;
6651 tree saved_object_scope;
6652 tree scope;
6653 bool only_class_p;
6654 bool successful_parse_p;
6655
6656 /* DR 743: decltype can appear in a nested-name-specifier. */
6657 if (cp_lexer_next_token_is_decltype (parser->lexer))
6658 {
6659 scope = cp_parser_decltype (parser);
6660 if (TREE_CODE (scope) != ENUMERAL_TYPE
6661 && !MAYBE_CLASS_TYPE_P (scope))
6662 {
6663 cp_parser_simulate_error (parser);
6664 return error_mark_node;
6665 }
6666 if (TYPE_NAME (scope))
6667 scope = TYPE_NAME (scope);
6668 return scope;
6669 }
6670
6671 /* Before we try to parse the class-name, we must save away the
6672 current PARSER->SCOPE since cp_parser_class_name will destroy
6673 it. */
6674 saved_scope = parser->scope;
6675 saved_qualifying_scope = parser->qualifying_scope;
6676 saved_object_scope = parser->object_scope;
6677 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6678 there is no need to look for a namespace-name. */
6679 only_class_p = template_keyword_p
6680 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6681 if (!only_class_p)
6682 cp_parser_parse_tentatively (parser);
6683 scope = cp_parser_class_name (parser,
6684 typename_keyword_p,
6685 template_keyword_p,
6686 type_p ? class_type : none_type,
6687 check_dependency_p,
6688 /*class_head_p=*/false,
6689 is_declaration,
6690 /*enum_ok=*/cxx_dialect > cxx98);
6691 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6692 /* If that didn't work, try for a namespace-name. */
6693 if (!only_class_p && !successful_parse_p)
6694 {
6695 /* Restore the saved scope. */
6696 parser->scope = saved_scope;
6697 parser->qualifying_scope = saved_qualifying_scope;
6698 parser->object_scope = saved_object_scope;
6699 /* If we are not looking at an identifier followed by the scope
6700 resolution operator, then this is not part of a
6701 nested-name-specifier. (Note that this function is only used
6702 to parse the components of a nested-name-specifier.) */
6703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6704 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6705 return error_mark_node;
6706 scope = cp_parser_namespace_name (parser);
6707 }
6708
6709 return scope;
6710 }
6711
6712 /* Return true if we are looking at a compound-literal, false otherwise. */
6713
6714 static bool
6715 cp_parser_compound_literal_p (cp_parser *parser)
6716 {
6717 cp_lexer_save_tokens (parser->lexer);
6718
6719 /* Skip tokens until the next token is a closing parenthesis.
6720 If we find the closing `)', and the next token is a `{', then
6721 we are looking at a compound-literal. */
6722 bool compound_literal_p
6723 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6724 /*consume_paren=*/true)
6725 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6726
6727 /* Roll back the tokens we skipped. */
6728 cp_lexer_rollback_tokens (parser->lexer);
6729
6730 return compound_literal_p;
6731 }
6732
6733 /* Return true if EXPR is the integer constant zero or a complex constant
6734 of zero, without any folding, but ignoring location wrappers. */
6735
6736 bool
6737 literal_integer_zerop (const_tree expr)
6738 {
6739 return (location_wrapper_p (expr)
6740 && integer_zerop (TREE_OPERAND (expr, 0)));
6741 }
6742
6743 /* Parse a postfix-expression.
6744
6745 postfix-expression:
6746 primary-expression
6747 postfix-expression [ expression ]
6748 postfix-expression ( expression-list [opt] )
6749 simple-type-specifier ( expression-list [opt] )
6750 typename :: [opt] nested-name-specifier identifier
6751 ( expression-list [opt] )
6752 typename :: [opt] nested-name-specifier template [opt] template-id
6753 ( expression-list [opt] )
6754 postfix-expression . template [opt] id-expression
6755 postfix-expression -> template [opt] id-expression
6756 postfix-expression . pseudo-destructor-name
6757 postfix-expression -> pseudo-destructor-name
6758 postfix-expression ++
6759 postfix-expression --
6760 dynamic_cast < type-id > ( expression )
6761 static_cast < type-id > ( expression )
6762 reinterpret_cast < type-id > ( expression )
6763 const_cast < type-id > ( expression )
6764 typeid ( expression )
6765 typeid ( type-id )
6766
6767 GNU Extension:
6768
6769 postfix-expression:
6770 ( type-id ) { initializer-list , [opt] }
6771
6772 This extension is a GNU version of the C99 compound-literal
6773 construct. (The C99 grammar uses `type-name' instead of `type-id',
6774 but they are essentially the same concept.)
6775
6776 If ADDRESS_P is true, the postfix expression is the operand of the
6777 `&' operator. CAST_P is true if this expression is the target of a
6778 cast.
6779
6780 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6781 class member access expressions [expr.ref].
6782
6783 Returns a representation of the expression. */
6784
6785 static cp_expr
6786 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6787 bool member_access_only_p, bool decltype_p,
6788 cp_id_kind * pidk_return)
6789 {
6790 cp_token *token;
6791 location_t loc;
6792 enum rid keyword;
6793 cp_id_kind idk = CP_ID_KIND_NONE;
6794 cp_expr postfix_expression = NULL_TREE;
6795 bool is_member_access = false;
6796
6797 /* Peek at the next token. */
6798 token = cp_lexer_peek_token (parser->lexer);
6799 loc = token->location;
6800 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6801
6802 /* Some of the productions are determined by keywords. */
6803 keyword = token->keyword;
6804 switch (keyword)
6805 {
6806 case RID_DYNCAST:
6807 case RID_STATCAST:
6808 case RID_REINTCAST:
6809 case RID_CONSTCAST:
6810 {
6811 tree type;
6812 cp_expr expression;
6813 const char *saved_message;
6814 bool saved_in_type_id_in_expr_p;
6815
6816 /* All of these can be handled in the same way from the point
6817 of view of parsing. Begin by consuming the token
6818 identifying the cast. */
6819 cp_lexer_consume_token (parser->lexer);
6820
6821 /* New types cannot be defined in the cast. */
6822 saved_message = parser->type_definition_forbidden_message;
6823 parser->type_definition_forbidden_message
6824 = G_("types may not be defined in casts");
6825
6826 /* Look for the opening `<'. */
6827 cp_parser_require (parser, CPP_LESS, RT_LESS);
6828 /* Parse the type to which we are casting. */
6829 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6830 parser->in_type_id_in_expr_p = true;
6831 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
6832 NULL);
6833 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6834 /* Look for the closing `>'. */
6835 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6836 /* Restore the old message. */
6837 parser->type_definition_forbidden_message = saved_message;
6838
6839 bool saved_greater_than_is_operator_p
6840 = parser->greater_than_is_operator_p;
6841 parser->greater_than_is_operator_p = true;
6842
6843 /* And the expression which is being cast. */
6844 matching_parens parens;
6845 parens.require_open (parser);
6846 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6847 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6848 RT_CLOSE_PAREN);
6849 location_t end_loc = close_paren ?
6850 close_paren->location : UNKNOWN_LOCATION;
6851
6852 parser->greater_than_is_operator_p
6853 = saved_greater_than_is_operator_p;
6854
6855 /* Only type conversions to integral or enumeration types
6856 can be used in constant-expressions. */
6857 if (!cast_valid_in_integral_constant_expression_p (type)
6858 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6859 {
6860 postfix_expression = error_mark_node;
6861 break;
6862 }
6863
6864 switch (keyword)
6865 {
6866 case RID_DYNCAST:
6867 postfix_expression
6868 = build_dynamic_cast (type, expression, tf_warning_or_error);
6869 break;
6870 case RID_STATCAST:
6871 postfix_expression
6872 = build_static_cast (type, expression, tf_warning_or_error);
6873 break;
6874 case RID_REINTCAST:
6875 postfix_expression
6876 = build_reinterpret_cast (type, expression,
6877 tf_warning_or_error);
6878 break;
6879 case RID_CONSTCAST:
6880 postfix_expression
6881 = build_const_cast (type, expression, tf_warning_or_error);
6882 break;
6883 default:
6884 gcc_unreachable ();
6885 }
6886
6887 /* Construct a location e.g. :
6888 reinterpret_cast <int *> (expr)
6889 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6890 ranging from the start of the "*_cast" token to the final closing
6891 paren, with the caret at the start. */
6892 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6893 postfix_expression.set_location (cp_cast_loc);
6894 }
6895 break;
6896
6897 case RID_TYPEID:
6898 {
6899 tree type;
6900 const char *saved_message;
6901 bool saved_in_type_id_in_expr_p;
6902
6903 /* Consume the `typeid' token. */
6904 cp_lexer_consume_token (parser->lexer);
6905 /* Look for the `(' token. */
6906 matching_parens parens;
6907 parens.require_open (parser);
6908 /* Types cannot be defined in a `typeid' expression. */
6909 saved_message = parser->type_definition_forbidden_message;
6910 parser->type_definition_forbidden_message
6911 = G_("types may not be defined in a %<typeid%> expression");
6912 /* We can't be sure yet whether we're looking at a type-id or an
6913 expression. */
6914 cp_parser_parse_tentatively (parser);
6915 /* Try a type-id first. */
6916 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6917 parser->in_type_id_in_expr_p = true;
6918 type = cp_parser_type_id (parser);
6919 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6920 /* Look for the `)' token. Otherwise, we can't be sure that
6921 we're not looking at an expression: consider `typeid (int
6922 (3))', for example. */
6923 cp_token *close_paren = parens.require_close (parser);
6924 /* If all went well, simply lookup the type-id. */
6925 if (cp_parser_parse_definitely (parser))
6926 postfix_expression = get_typeid (type, tf_warning_or_error);
6927 /* Otherwise, fall back to the expression variant. */
6928 else
6929 {
6930 tree expression;
6931
6932 /* Look for an expression. */
6933 expression = cp_parser_expression (parser, & idk);
6934 /* Compute its typeid. */
6935 postfix_expression = build_typeid (expression, tf_warning_or_error);
6936 /* Look for the `)' token. */
6937 close_paren = parens.require_close (parser);
6938 }
6939 /* Restore the saved message. */
6940 parser->type_definition_forbidden_message = saved_message;
6941 /* `typeid' may not appear in an integral constant expression. */
6942 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6943 postfix_expression = error_mark_node;
6944
6945 /* Construct a location e.g. :
6946 typeid (expr)
6947 ^~~~~~~~~~~~~
6948 ranging from the start of the "typeid" token to the final closing
6949 paren, with the caret at the start. */
6950 if (close_paren)
6951 {
6952 location_t typeid_loc
6953 = make_location (start_loc, start_loc, close_paren->location);
6954 postfix_expression.set_location (typeid_loc);
6955 postfix_expression.maybe_add_location_wrapper ();
6956 }
6957 }
6958 break;
6959
6960 case RID_TYPENAME:
6961 {
6962 tree type;
6963 /* The syntax permitted here is the same permitted for an
6964 elaborated-type-specifier. */
6965 ++parser->prevent_constrained_type_specifiers;
6966 type = cp_parser_elaborated_type_specifier (parser,
6967 /*is_friend=*/false,
6968 /*is_declaration=*/false);
6969 --parser->prevent_constrained_type_specifiers;
6970 postfix_expression = cp_parser_functional_cast (parser, type);
6971 }
6972 break;
6973
6974 case RID_ADDRESSOF:
6975 case RID_BUILTIN_SHUFFLE:
6976 case RID_BUILTIN_LAUNDER:
6977 {
6978 vec<tree, va_gc> *vec;
6979 unsigned int i;
6980 tree p;
6981
6982 cp_lexer_consume_token (parser->lexer);
6983 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6984 /*cast_p=*/false, /*allow_expansion_p=*/true,
6985 /*non_constant_p=*/NULL);
6986 if (vec == NULL)
6987 {
6988 postfix_expression = error_mark_node;
6989 break;
6990 }
6991
6992 FOR_EACH_VEC_ELT (*vec, i, p)
6993 mark_exp_read (p);
6994
6995 switch (keyword)
6996 {
6997 case RID_ADDRESSOF:
6998 if (vec->length () == 1)
6999 postfix_expression
7000 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7001 else
7002 {
7003 error_at (loc, "wrong number of arguments to "
7004 "%<__builtin_addressof%>");
7005 postfix_expression = error_mark_node;
7006 }
7007 break;
7008
7009 case RID_BUILTIN_LAUNDER:
7010 if (vec->length () == 1)
7011 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7012 tf_warning_or_error);
7013 else
7014 {
7015 error_at (loc, "wrong number of arguments to "
7016 "%<__builtin_launder%>");
7017 postfix_expression = error_mark_node;
7018 }
7019 break;
7020
7021 case RID_BUILTIN_SHUFFLE:
7022 if (vec->length () == 2)
7023 postfix_expression
7024 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7025 (*vec)[1], tf_warning_or_error);
7026 else if (vec->length () == 3)
7027 postfix_expression
7028 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7029 (*vec)[2], tf_warning_or_error);
7030 else
7031 {
7032 error_at (loc, "wrong number of arguments to "
7033 "%<__builtin_shuffle%>");
7034 postfix_expression = error_mark_node;
7035 }
7036 break;
7037
7038 default:
7039 gcc_unreachable ();
7040 }
7041 break;
7042 }
7043
7044 case RID_BUILTIN_CONVERTVECTOR:
7045 {
7046 tree expression;
7047 tree type;
7048 /* Consume the `__builtin_convertvector' token. */
7049 cp_lexer_consume_token (parser->lexer);
7050 /* Look for the opening `('. */
7051 matching_parens parens;
7052 parens.require_open (parser);
7053 /* Now, parse the assignment-expression. */
7054 expression = cp_parser_assignment_expression (parser);
7055 /* Look for the `,'. */
7056 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7057 location_t type_location
7058 = cp_lexer_peek_token (parser->lexer)->location;
7059 /* Parse the type-id. */
7060 {
7061 type_id_in_expr_sentinel s (parser);
7062 type = cp_parser_type_id (parser);
7063 }
7064 /* Look for the closing `)'. */
7065 parens.require_close (parser);
7066 return cp_build_vec_convert (expression, type_location, type,
7067 tf_warning_or_error);
7068 }
7069
7070 default:
7071 {
7072 tree type;
7073
7074 /* If the next thing is a simple-type-specifier, we may be
7075 looking at a functional cast. We could also be looking at
7076 an id-expression. So, we try the functional cast, and if
7077 that doesn't work we fall back to the primary-expression. */
7078 cp_parser_parse_tentatively (parser);
7079 /* Look for the simple-type-specifier. */
7080 ++parser->prevent_constrained_type_specifiers;
7081 type = cp_parser_simple_type_specifier (parser,
7082 /*decl_specs=*/NULL,
7083 CP_PARSER_FLAGS_NONE);
7084 --parser->prevent_constrained_type_specifiers;
7085 /* Parse the cast itself. */
7086 if (!cp_parser_error_occurred (parser))
7087 postfix_expression
7088 = cp_parser_functional_cast (parser, type);
7089 /* If that worked, we're done. */
7090 if (cp_parser_parse_definitely (parser))
7091 break;
7092
7093 /* If the functional-cast didn't work out, try a
7094 compound-literal. */
7095 if (cp_parser_allow_gnu_extensions_p (parser)
7096 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7097 {
7098 cp_expr initializer = NULL_TREE;
7099
7100 cp_parser_parse_tentatively (parser);
7101
7102 matching_parens parens;
7103 parens.consume_open (parser);
7104
7105 /* Avoid calling cp_parser_type_id pointlessly, see comment
7106 in cp_parser_cast_expression about c++/29234. */
7107 if (!cp_parser_compound_literal_p (parser))
7108 cp_parser_simulate_error (parser);
7109 else
7110 {
7111 /* Parse the type. */
7112 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7113 parser->in_type_id_in_expr_p = true;
7114 type = cp_parser_type_id (parser);
7115 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7116 parens.require_close (parser);
7117 }
7118
7119 /* If things aren't going well, there's no need to
7120 keep going. */
7121 if (!cp_parser_error_occurred (parser))
7122 {
7123 bool non_constant_p;
7124 /* Parse the brace-enclosed initializer list. */
7125 initializer = cp_parser_braced_list (parser,
7126 &non_constant_p);
7127 }
7128 /* If that worked, we're definitely looking at a
7129 compound-literal expression. */
7130 if (cp_parser_parse_definitely (parser))
7131 {
7132 /* Warn the user that a compound literal is not
7133 allowed in standard C++. */
7134 pedwarn (input_location, OPT_Wpedantic,
7135 "ISO C++ forbids compound-literals");
7136 /* For simplicity, we disallow compound literals in
7137 constant-expressions. We could
7138 allow compound literals of integer type, whose
7139 initializer was a constant, in constant
7140 expressions. Permitting that usage, as a further
7141 extension, would not change the meaning of any
7142 currently accepted programs. (Of course, as
7143 compound literals are not part of ISO C++, the
7144 standard has nothing to say.) */
7145 if (cp_parser_non_integral_constant_expression (parser,
7146 NIC_NCC))
7147 {
7148 postfix_expression = error_mark_node;
7149 break;
7150 }
7151 /* Form the representation of the compound-literal. */
7152 postfix_expression
7153 = finish_compound_literal (type, initializer,
7154 tf_warning_or_error, fcl_c99);
7155 postfix_expression.set_location (initializer.get_location ());
7156 break;
7157 }
7158 }
7159
7160 /* It must be a primary-expression. */
7161 postfix_expression
7162 = cp_parser_primary_expression (parser, address_p, cast_p,
7163 /*template_arg_p=*/false,
7164 decltype_p,
7165 &idk);
7166 }
7167 break;
7168 }
7169
7170 /* Note that we don't need to worry about calling build_cplus_new on a
7171 class-valued CALL_EXPR in decltype when it isn't the end of the
7172 postfix-expression; unary_complex_lvalue will take care of that for
7173 all these cases. */
7174
7175 /* Keep looping until the postfix-expression is complete. */
7176 while (true)
7177 {
7178 if (idk == CP_ID_KIND_UNQUALIFIED
7179 && identifier_p (postfix_expression)
7180 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7181 /* It is not a Koenig lookup function call. */
7182 postfix_expression
7183 = unqualified_name_lookup_error (postfix_expression);
7184
7185 /* Peek at the next token. */
7186 token = cp_lexer_peek_token (parser->lexer);
7187
7188 switch (token->type)
7189 {
7190 case CPP_OPEN_SQUARE:
7191 if (cp_next_tokens_can_be_std_attribute_p (parser))
7192 {
7193 cp_parser_error (parser,
7194 "two consecutive %<[%> shall "
7195 "only introduce an attribute");
7196 return error_mark_node;
7197 }
7198 postfix_expression
7199 = cp_parser_postfix_open_square_expression (parser,
7200 postfix_expression,
7201 false,
7202 decltype_p);
7203 postfix_expression.set_range (start_loc,
7204 postfix_expression.get_location ());
7205
7206 idk = CP_ID_KIND_NONE;
7207 is_member_access = false;
7208 break;
7209
7210 case CPP_OPEN_PAREN:
7211 /* postfix-expression ( expression-list [opt] ) */
7212 {
7213 bool koenig_p;
7214 bool is_builtin_constant_p;
7215 bool saved_integral_constant_expression_p = false;
7216 bool saved_non_integral_constant_expression_p = false;
7217 tsubst_flags_t complain = complain_flags (decltype_p);
7218 vec<tree, va_gc> *args;
7219 location_t close_paren_loc = UNKNOWN_LOCATION;
7220
7221 is_member_access = false;
7222
7223 tree stripped_expression
7224 = tree_strip_any_location_wrapper (postfix_expression);
7225 is_builtin_constant_p
7226 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7227 if (is_builtin_constant_p)
7228 {
7229 /* The whole point of __builtin_constant_p is to allow
7230 non-constant expressions to appear as arguments. */
7231 saved_integral_constant_expression_p
7232 = parser->integral_constant_expression_p;
7233 saved_non_integral_constant_expression_p
7234 = parser->non_integral_constant_expression_p;
7235 parser->integral_constant_expression_p = false;
7236 }
7237 args = (cp_parser_parenthesized_expression_list
7238 (parser, non_attr,
7239 /*cast_p=*/false, /*allow_expansion_p=*/true,
7240 /*non_constant_p=*/NULL,
7241 /*close_paren_loc=*/&close_paren_loc,
7242 /*wrap_locations_p=*/true));
7243 if (is_builtin_constant_p)
7244 {
7245 parser->integral_constant_expression_p
7246 = saved_integral_constant_expression_p;
7247 parser->non_integral_constant_expression_p
7248 = saved_non_integral_constant_expression_p;
7249 }
7250
7251 if (args == NULL)
7252 {
7253 postfix_expression = error_mark_node;
7254 break;
7255 }
7256
7257 /* Function calls are not permitted in
7258 constant-expressions. */
7259 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7260 && cp_parser_non_integral_constant_expression (parser,
7261 NIC_FUNC_CALL))
7262 {
7263 postfix_expression = error_mark_node;
7264 release_tree_vector (args);
7265 break;
7266 }
7267
7268 koenig_p = false;
7269 if (idk == CP_ID_KIND_UNQUALIFIED
7270 || idk == CP_ID_KIND_TEMPLATE_ID)
7271 {
7272 if (identifier_p (postfix_expression)
7273 /* In C++2A, we may need to perform ADL for a template
7274 name. */
7275 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7276 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7277 {
7278 if (!args->is_empty ())
7279 {
7280 koenig_p = true;
7281 if (!any_type_dependent_arguments_p (args))
7282 postfix_expression
7283 = perform_koenig_lookup (postfix_expression, args,
7284 complain);
7285 }
7286 else
7287 postfix_expression
7288 = unqualified_fn_lookup_error (postfix_expression);
7289 }
7290 /* We do not perform argument-dependent lookup if
7291 normal lookup finds a non-function, in accordance
7292 with the expected resolution of DR 218. */
7293 else if (!args->is_empty ()
7294 && is_overloaded_fn (postfix_expression))
7295 {
7296 /* We only need to look at the first function,
7297 because all the fns share the attribute we're
7298 concerned with (all member fns or all local
7299 fns). */
7300 tree fn = get_first_fn (postfix_expression);
7301 fn = STRIP_TEMPLATE (fn);
7302
7303 /* Do not do argument dependent lookup if regular
7304 lookup finds a member function or a block-scope
7305 function declaration. [basic.lookup.argdep]/3 */
7306 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn))
7307 || DECL_FUNCTION_MEMBER_P (fn)
7308 || DECL_LOCAL_FUNCTION_P (fn)))
7309 {
7310 koenig_p = true;
7311 if (!any_type_dependent_arguments_p (args))
7312 postfix_expression
7313 = perform_koenig_lookup (postfix_expression, args,
7314 complain);
7315 }
7316 }
7317 }
7318
7319 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7320 {
7321 tree instance = TREE_OPERAND (postfix_expression, 0);
7322 tree fn = TREE_OPERAND (postfix_expression, 1);
7323
7324 if (processing_template_decl
7325 && (type_dependent_object_expression_p (instance)
7326 || (!BASELINK_P (fn)
7327 && TREE_CODE (fn) != FIELD_DECL)
7328 || type_dependent_expression_p (fn)
7329 || any_type_dependent_arguments_p (args)))
7330 {
7331 maybe_generic_this_capture (instance, fn);
7332 postfix_expression
7333 = build_min_nt_call_vec (postfix_expression, args);
7334 release_tree_vector (args);
7335 break;
7336 }
7337
7338 if (BASELINK_P (fn))
7339 {
7340 postfix_expression
7341 = (build_new_method_call
7342 (instance, fn, &args, NULL_TREE,
7343 (idk == CP_ID_KIND_QUALIFIED
7344 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7345 : LOOKUP_NORMAL),
7346 /*fn_p=*/NULL,
7347 complain));
7348 }
7349 else
7350 postfix_expression
7351 = finish_call_expr (postfix_expression, &args,
7352 /*disallow_virtual=*/false,
7353 /*koenig_p=*/false,
7354 complain);
7355 }
7356 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7357 || TREE_CODE (postfix_expression) == MEMBER_REF
7358 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7359 postfix_expression = (build_offset_ref_call_from_tree
7360 (postfix_expression, &args,
7361 complain));
7362 else if (idk == CP_ID_KIND_QUALIFIED)
7363 /* A call to a static class member, or a namespace-scope
7364 function. */
7365 postfix_expression
7366 = finish_call_expr (postfix_expression, &args,
7367 /*disallow_virtual=*/true,
7368 koenig_p,
7369 complain);
7370 else
7371 /* All other function calls. */
7372 postfix_expression
7373 = finish_call_expr (postfix_expression, &args,
7374 /*disallow_virtual=*/false,
7375 koenig_p,
7376 complain);
7377
7378 if (close_paren_loc != UNKNOWN_LOCATION)
7379 {
7380 location_t combined_loc = make_location (token->location,
7381 start_loc,
7382 close_paren_loc);
7383 postfix_expression.set_location (combined_loc);
7384 }
7385
7386 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7387 idk = CP_ID_KIND_NONE;
7388
7389 release_tree_vector (args);
7390 }
7391 break;
7392
7393 case CPP_DOT:
7394 case CPP_DEREF:
7395 /* postfix-expression . template [opt] id-expression
7396 postfix-expression . pseudo-destructor-name
7397 postfix-expression -> template [opt] id-expression
7398 postfix-expression -> pseudo-destructor-name */
7399
7400 /* Consume the `.' or `->' operator. */
7401 cp_lexer_consume_token (parser->lexer);
7402
7403 postfix_expression
7404 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7405 postfix_expression,
7406 false, &idk, loc);
7407
7408 is_member_access = true;
7409 break;
7410
7411 case CPP_PLUS_PLUS:
7412 /* postfix-expression ++ */
7413 /* Consume the `++' token. */
7414 cp_lexer_consume_token (parser->lexer);
7415 /* Generate a representation for the complete expression. */
7416 postfix_expression
7417 = finish_increment_expr (postfix_expression,
7418 POSTINCREMENT_EXPR);
7419 /* Increments may not appear in constant-expressions. */
7420 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7421 postfix_expression = error_mark_node;
7422 idk = CP_ID_KIND_NONE;
7423 is_member_access = false;
7424 break;
7425
7426 case CPP_MINUS_MINUS:
7427 /* postfix-expression -- */
7428 /* Consume the `--' token. */
7429 cp_lexer_consume_token (parser->lexer);
7430 /* Generate a representation for the complete expression. */
7431 postfix_expression
7432 = finish_increment_expr (postfix_expression,
7433 POSTDECREMENT_EXPR);
7434 /* Decrements may not appear in constant-expressions. */
7435 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7436 postfix_expression = error_mark_node;
7437 idk = CP_ID_KIND_NONE;
7438 is_member_access = false;
7439 break;
7440
7441 default:
7442 if (pidk_return != NULL)
7443 * pidk_return = idk;
7444 if (member_access_only_p)
7445 return is_member_access
7446 ? postfix_expression
7447 : cp_expr (error_mark_node);
7448 else
7449 return postfix_expression;
7450 }
7451 }
7452
7453 /* We should never get here. */
7454 gcc_unreachable ();
7455 return error_mark_node;
7456 }
7457
7458 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7459 by cp_parser_builtin_offsetof. We're looking for
7460
7461 postfix-expression [ expression ]
7462 postfix-expression [ braced-init-list ] (C++11)
7463
7464 FOR_OFFSETOF is set if we're being called in that context, which
7465 changes how we deal with integer constant expressions. */
7466
7467 static tree
7468 cp_parser_postfix_open_square_expression (cp_parser *parser,
7469 tree postfix_expression,
7470 bool for_offsetof,
7471 bool decltype_p)
7472 {
7473 tree index = NULL_TREE;
7474 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7475 bool saved_greater_than_is_operator_p;
7476
7477 /* Consume the `[' token. */
7478 cp_lexer_consume_token (parser->lexer);
7479
7480 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7481 parser->greater_than_is_operator_p = true;
7482
7483 /* Parse the index expression. */
7484 /* ??? For offsetof, there is a question of what to allow here. If
7485 offsetof is not being used in an integral constant expression context,
7486 then we *could* get the right answer by computing the value at runtime.
7487 If we are in an integral constant expression context, then we might
7488 could accept any constant expression; hard to say without analysis.
7489 Rather than open the barn door too wide right away, allow only integer
7490 constant expressions here. */
7491 if (for_offsetof)
7492 index = cp_parser_constant_expression (parser);
7493 else
7494 {
7495 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7496 {
7497 bool expr_nonconst_p;
7498 cp_lexer_set_source_position (parser->lexer);
7499 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7500 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7501 }
7502 else
7503 index = cp_parser_expression (parser);
7504 }
7505
7506 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7507
7508 /* Look for the closing `]'. */
7509 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7510
7511 /* Build the ARRAY_REF. */
7512 postfix_expression = grok_array_decl (loc, postfix_expression,
7513 index, decltype_p);
7514
7515 /* When not doing offsetof, array references are not permitted in
7516 constant-expressions. */
7517 if (!for_offsetof
7518 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7519 postfix_expression = error_mark_node;
7520
7521 return postfix_expression;
7522 }
7523
7524 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7525 dereference of incomplete type, returns true if error_mark_node should
7526 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7527 and *DEPENDENT_P. */
7528
7529 bool
7530 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
7531 bool *dependent_p)
7532 {
7533 /* In a template, be permissive by treating an object expression
7534 of incomplete type as dependent (after a pedwarn). */
7535 diagnostic_t kind = (processing_template_decl
7536 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
7537
7538 switch (TREE_CODE (*postfix_expression))
7539 {
7540 case CAST_EXPR:
7541 case REINTERPRET_CAST_EXPR:
7542 case CONST_CAST_EXPR:
7543 case STATIC_CAST_EXPR:
7544 case DYNAMIC_CAST_EXPR:
7545 case IMPLICIT_CONV_EXPR:
7546 case VIEW_CONVERT_EXPR:
7547 case NON_LVALUE_EXPR:
7548 kind = DK_ERROR;
7549 break;
7550 case OVERLOAD:
7551 /* Don't emit any diagnostic for OVERLOADs. */
7552 kind = DK_IGNORED;
7553 break;
7554 default:
7555 /* Avoid clobbering e.g. DECLs. */
7556 if (!EXPR_P (*postfix_expression))
7557 kind = DK_ERROR;
7558 break;
7559 }
7560
7561 if (kind == DK_IGNORED)
7562 return false;
7563
7564 location_t exploc = location_of (*postfix_expression);
7565 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
7566 if (!MAYBE_CLASS_TYPE_P (*scope))
7567 return true;
7568 if (kind == DK_ERROR)
7569 *scope = *postfix_expression = error_mark_node;
7570 else if (processing_template_decl)
7571 {
7572 *dependent_p = true;
7573 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
7574 }
7575 return false;
7576 }
7577
7578 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7579 by cp_parser_builtin_offsetof. We're looking for
7580
7581 postfix-expression . template [opt] id-expression
7582 postfix-expression . pseudo-destructor-name
7583 postfix-expression -> template [opt] id-expression
7584 postfix-expression -> pseudo-destructor-name
7585
7586 FOR_OFFSETOF is set if we're being called in that context. That sorta
7587 limits what of the above we'll actually accept, but nevermind.
7588 TOKEN_TYPE is the "." or "->" token, which will already have been
7589 removed from the stream. */
7590
7591 static tree
7592 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7593 enum cpp_ttype token_type,
7594 cp_expr postfix_expression,
7595 bool for_offsetof, cp_id_kind *idk,
7596 location_t location)
7597 {
7598 tree name;
7599 bool dependent_p;
7600 bool pseudo_destructor_p;
7601 tree scope = NULL_TREE;
7602 location_t start_loc = postfix_expression.get_start ();
7603
7604 /* If this is a `->' operator, dereference the pointer. */
7605 if (token_type == CPP_DEREF)
7606 postfix_expression = build_x_arrow (location, postfix_expression,
7607 tf_warning_or_error);
7608 /* Check to see whether or not the expression is type-dependent and
7609 not the current instantiation. */
7610 dependent_p = type_dependent_object_expression_p (postfix_expression);
7611 /* The identifier following the `->' or `.' is not qualified. */
7612 parser->scope = NULL_TREE;
7613 parser->qualifying_scope = NULL_TREE;
7614 parser->object_scope = NULL_TREE;
7615 *idk = CP_ID_KIND_NONE;
7616
7617 /* Enter the scope corresponding to the type of the object
7618 given by the POSTFIX_EXPRESSION. */
7619 if (!dependent_p)
7620 {
7621 scope = TREE_TYPE (postfix_expression);
7622 /* According to the standard, no expression should ever have
7623 reference type. Unfortunately, we do not currently match
7624 the standard in this respect in that our internal representation
7625 of an expression may have reference type even when the standard
7626 says it does not. Therefore, we have to manually obtain the
7627 underlying type here. */
7628 scope = non_reference (scope);
7629 /* The type of the POSTFIX_EXPRESSION must be complete. */
7630 /* Unlike the object expression in other contexts, *this is not
7631 required to be of complete type for purposes of class member
7632 access (5.2.5) outside the member function body. */
7633 if (postfix_expression != current_class_ref
7634 && scope != error_mark_node
7635 && !currently_open_class (scope))
7636 {
7637 scope = complete_type (scope);
7638 if (!COMPLETE_TYPE_P (scope)
7639 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
7640 &dependent_p))
7641 return error_mark_node;
7642 }
7643
7644 if (!dependent_p)
7645 {
7646 /* Let the name lookup machinery know that we are processing a
7647 class member access expression. */
7648 parser->context->object_type = scope;
7649 /* If something went wrong, we want to be able to discern that case,
7650 as opposed to the case where there was no SCOPE due to the type
7651 of expression being dependent. */
7652 if (!scope)
7653 scope = error_mark_node;
7654 /* If the SCOPE was erroneous, make the various semantic analysis
7655 functions exit quickly -- and without issuing additional error
7656 messages. */
7657 if (scope == error_mark_node)
7658 postfix_expression = error_mark_node;
7659 }
7660 }
7661
7662 if (dependent_p)
7663 /* Tell cp_parser_lookup_name that there was an object, even though it's
7664 type-dependent. */
7665 parser->context->object_type = unknown_type_node;
7666
7667 /* Assume this expression is not a pseudo-destructor access. */
7668 pseudo_destructor_p = false;
7669
7670 /* If the SCOPE is a scalar type, then, if this is a valid program,
7671 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7672 is type dependent, it can be pseudo-destructor-name or something else.
7673 Try to parse it as pseudo-destructor-name first. */
7674 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7675 {
7676 tree s;
7677 tree type;
7678
7679 cp_parser_parse_tentatively (parser);
7680 /* Parse the pseudo-destructor-name. */
7681 s = NULL_TREE;
7682 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7683 &s, &type);
7684 if (dependent_p
7685 && (cp_parser_error_occurred (parser)
7686 || !SCALAR_TYPE_P (type)))
7687 cp_parser_abort_tentative_parse (parser);
7688 else if (cp_parser_parse_definitely (parser))
7689 {
7690 pseudo_destructor_p = true;
7691 postfix_expression
7692 = finish_pseudo_destructor_expr (postfix_expression,
7693 s, type, location);
7694 }
7695 }
7696
7697 if (!pseudo_destructor_p)
7698 {
7699 /* If the SCOPE is not a scalar type, we are looking at an
7700 ordinary class member access expression, rather than a
7701 pseudo-destructor-name. */
7702 bool template_p;
7703 cp_token *token = cp_lexer_peek_token (parser->lexer);
7704 /* Parse the id-expression. */
7705 name = (cp_parser_id_expression
7706 (parser,
7707 cp_parser_optional_template_keyword (parser),
7708 /*check_dependency_p=*/true,
7709 &template_p,
7710 /*declarator_p=*/false,
7711 /*optional_p=*/false));
7712 /* In general, build a SCOPE_REF if the member name is qualified.
7713 However, if the name was not dependent and has already been
7714 resolved; there is no need to build the SCOPE_REF. For example;
7715
7716 struct X { void f(); };
7717 template <typename T> void f(T* t) { t->X::f(); }
7718
7719 Even though "t" is dependent, "X::f" is not and has been resolved
7720 to a BASELINK; there is no need to include scope information. */
7721
7722 /* But we do need to remember that there was an explicit scope for
7723 virtual function calls. */
7724 if (parser->scope)
7725 *idk = CP_ID_KIND_QUALIFIED;
7726
7727 /* If the name is a template-id that names a type, we will get a
7728 TYPE_DECL here. That is invalid code. */
7729 if (TREE_CODE (name) == TYPE_DECL)
7730 {
7731 error_at (token->location, "invalid use of %qD", name);
7732 postfix_expression = error_mark_node;
7733 }
7734 else
7735 {
7736 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7737 {
7738 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7739 {
7740 error_at (token->location, "%<%D::%D%> is not a class member",
7741 parser->scope, name);
7742 postfix_expression = error_mark_node;
7743 }
7744 else
7745 name = build_qualified_name (/*type=*/NULL_TREE,
7746 parser->scope,
7747 name,
7748 template_p);
7749 parser->scope = NULL_TREE;
7750 parser->qualifying_scope = NULL_TREE;
7751 parser->object_scope = NULL_TREE;
7752 }
7753 if (parser->scope && name && BASELINK_P (name))
7754 adjust_result_of_qualified_name_lookup
7755 (name, parser->scope, scope);
7756 postfix_expression
7757 = finish_class_member_access_expr (postfix_expression, name,
7758 template_p,
7759 tf_warning_or_error);
7760 /* Build a location e.g.:
7761 ptr->access_expr
7762 ~~~^~~~~~~~~~~~~
7763 where the caret is at the deref token, ranging from
7764 the start of postfix_expression to the end of the access expr. */
7765 location_t end_loc
7766 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7767 location_t combined_loc
7768 = make_location (input_location, start_loc, end_loc);
7769 protected_set_expr_location (postfix_expression, combined_loc);
7770 }
7771 }
7772
7773 /* We no longer need to look up names in the scope of the object on
7774 the left-hand side of the `.' or `->' operator. */
7775 parser->context->object_type = NULL_TREE;
7776
7777 /* Outside of offsetof, these operators may not appear in
7778 constant-expressions. */
7779 if (!for_offsetof
7780 && (cp_parser_non_integral_constant_expression
7781 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7782 postfix_expression = error_mark_node;
7783
7784 return postfix_expression;
7785 }
7786
7787 /* Parse a parenthesized expression-list.
7788
7789 expression-list:
7790 assignment-expression
7791 expression-list, assignment-expression
7792
7793 attribute-list:
7794 expression-list
7795 identifier
7796 identifier, expression-list
7797
7798 CAST_P is true if this expression is the target of a cast.
7799
7800 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7801 argument pack.
7802
7803 WRAP_LOCATIONS_P is true if expressions within this list for which
7804 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7805 their source locations.
7806
7807 Returns a vector of trees. Each element is a representation of an
7808 assignment-expression. NULL is returned if the ( and or ) are
7809 missing. An empty, but allocated, vector is returned on no
7810 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7811 if we are parsing an attribute list for an attribute that wants a
7812 plain identifier argument, normal_attr for an attribute that wants
7813 an expression, or non_attr if we aren't parsing an attribute list. If
7814 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7815 not all of the expressions in the list were constant.
7816 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7817 will be written to with the location of the closing parenthesis. If
7818 an error occurs, it may or may not be written to. */
7819
7820 static vec<tree, va_gc> *
7821 cp_parser_parenthesized_expression_list (cp_parser* parser,
7822 int is_attribute_list,
7823 bool cast_p,
7824 bool allow_expansion_p,
7825 bool *non_constant_p,
7826 location_t *close_paren_loc,
7827 bool wrap_locations_p)
7828 {
7829 vec<tree, va_gc> *expression_list;
7830 bool fold_expr_p = is_attribute_list != non_attr;
7831 tree identifier = NULL_TREE;
7832 bool saved_greater_than_is_operator_p;
7833
7834 /* Assume all the expressions will be constant. */
7835 if (non_constant_p)
7836 *non_constant_p = false;
7837
7838 matching_parens parens;
7839 if (!parens.require_open (parser))
7840 return NULL;
7841
7842 expression_list = make_tree_vector ();
7843
7844 /* Within a parenthesized expression, a `>' token is always
7845 the greater-than operator. */
7846 saved_greater_than_is_operator_p
7847 = parser->greater_than_is_operator_p;
7848 parser->greater_than_is_operator_p = true;
7849
7850 cp_expr expr (NULL_TREE);
7851
7852 /* Consume expressions until there are no more. */
7853 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7854 while (true)
7855 {
7856 /* At the beginning of attribute lists, check to see if the
7857 next token is an identifier. */
7858 if (is_attribute_list == id_attr
7859 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7860 {
7861 cp_token *token;
7862
7863 /* Consume the identifier. */
7864 token = cp_lexer_consume_token (parser->lexer);
7865 /* Save the identifier. */
7866 identifier = token->u.value;
7867 }
7868 else
7869 {
7870 bool expr_non_constant_p;
7871
7872 /* Parse the next assignment-expression. */
7873 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7874 {
7875 /* A braced-init-list. */
7876 cp_lexer_set_source_position (parser->lexer);
7877 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7878 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7879 if (non_constant_p && expr_non_constant_p)
7880 *non_constant_p = true;
7881 }
7882 else if (non_constant_p)
7883 {
7884 expr = (cp_parser_constant_expression
7885 (parser, /*allow_non_constant_p=*/true,
7886 &expr_non_constant_p));
7887 if (expr_non_constant_p)
7888 *non_constant_p = true;
7889 }
7890 else
7891 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7892 cast_p);
7893
7894 if (fold_expr_p)
7895 expr = instantiate_non_dependent_expr (expr);
7896
7897 /* If we have an ellipsis, then this is an expression
7898 expansion. */
7899 if (allow_expansion_p
7900 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7901 {
7902 /* Consume the `...'. */
7903 cp_lexer_consume_token (parser->lexer);
7904
7905 /* Build the argument pack. */
7906 expr = make_pack_expansion (expr);
7907 }
7908
7909 if (wrap_locations_p)
7910 expr.maybe_add_location_wrapper ();
7911
7912 /* Add it to the list. We add error_mark_node
7913 expressions to the list, so that we can still tell if
7914 the correct form for a parenthesized expression-list
7915 is found. That gives better errors. */
7916 vec_safe_push (expression_list, expr.get_value ());
7917
7918 if (expr == error_mark_node)
7919 goto skip_comma;
7920 }
7921
7922 /* After the first item, attribute lists look the same as
7923 expression lists. */
7924 is_attribute_list = non_attr;
7925
7926 get_comma:;
7927 /* If the next token isn't a `,', then we are done. */
7928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7929 break;
7930
7931 /* Otherwise, consume the `,' and keep going. */
7932 cp_lexer_consume_token (parser->lexer);
7933 }
7934
7935 if (close_paren_loc)
7936 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7937
7938 if (!parens.require_close (parser))
7939 {
7940 int ending;
7941
7942 skip_comma:;
7943 /* We try and resync to an unnested comma, as that will give the
7944 user better diagnostics. */
7945 ending = cp_parser_skip_to_closing_parenthesis (parser,
7946 /*recovering=*/true,
7947 /*or_comma=*/true,
7948 /*consume_paren=*/true);
7949 if (ending < 0)
7950 goto get_comma;
7951 if (!ending)
7952 {
7953 parser->greater_than_is_operator_p
7954 = saved_greater_than_is_operator_p;
7955 return NULL;
7956 }
7957 }
7958
7959 parser->greater_than_is_operator_p
7960 = saved_greater_than_is_operator_p;
7961
7962 if (identifier)
7963 vec_safe_insert (expression_list, 0, identifier);
7964
7965 return expression_list;
7966 }
7967
7968 /* Parse a pseudo-destructor-name.
7969
7970 pseudo-destructor-name:
7971 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7972 :: [opt] nested-name-specifier template template-id :: ~ type-name
7973 :: [opt] nested-name-specifier [opt] ~ type-name
7974
7975 If either of the first two productions is used, sets *SCOPE to the
7976 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7977 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7978 or ERROR_MARK_NODE if the parse fails. */
7979
7980 static void
7981 cp_parser_pseudo_destructor_name (cp_parser* parser,
7982 tree object,
7983 tree* scope,
7984 tree* type)
7985 {
7986 bool nested_name_specifier_p;
7987
7988 /* Handle ~auto. */
7989 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7990 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7991 && !type_dependent_expression_p (object))
7992 {
7993 if (cxx_dialect < cxx14)
7994 pedwarn (input_location, 0,
7995 "%<~auto%> only available with "
7996 "-std=c++14 or -std=gnu++14");
7997 cp_lexer_consume_token (parser->lexer);
7998 cp_lexer_consume_token (parser->lexer);
7999 *scope = NULL_TREE;
8000 *type = TREE_TYPE (object);
8001 return;
8002 }
8003
8004 /* Assume that things will not work out. */
8005 *type = error_mark_node;
8006
8007 /* Look for the optional `::' operator. */
8008 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8009 /* Look for the optional nested-name-specifier. */
8010 nested_name_specifier_p
8011 = (cp_parser_nested_name_specifier_opt (parser,
8012 /*typename_keyword_p=*/false,
8013 /*check_dependency_p=*/true,
8014 /*type_p=*/false,
8015 /*is_declaration=*/false)
8016 != NULL_TREE);
8017 /* Now, if we saw a nested-name-specifier, we might be doing the
8018 second production. */
8019 if (nested_name_specifier_p
8020 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8021 {
8022 /* Consume the `template' keyword. */
8023 cp_lexer_consume_token (parser->lexer);
8024 /* Parse the template-id. */
8025 cp_parser_template_id (parser,
8026 /*template_keyword_p=*/true,
8027 /*check_dependency_p=*/false,
8028 class_type,
8029 /*is_declaration=*/true);
8030 /* Look for the `::' token. */
8031 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8032 }
8033 /* If the next token is not a `~', then there might be some
8034 additional qualification. */
8035 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8036 {
8037 /* At this point, we're looking for "type-name :: ~". The type-name
8038 must not be a class-name, since this is a pseudo-destructor. So,
8039 it must be either an enum-name, or a typedef-name -- both of which
8040 are just identifiers. So, we peek ahead to check that the "::"
8041 and "~" tokens are present; if they are not, then we can avoid
8042 calling type_name. */
8043 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8044 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8045 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8046 {
8047 cp_parser_error (parser, "non-scalar type");
8048 return;
8049 }
8050
8051 /* Look for the type-name. */
8052 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8053 if (*scope == error_mark_node)
8054 return;
8055
8056 /* Look for the `::' token. */
8057 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8058 }
8059 else
8060 *scope = NULL_TREE;
8061
8062 /* Look for the `~'. */
8063 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8064
8065 /* Once we see the ~, this has to be a pseudo-destructor. */
8066 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8067 cp_parser_commit_to_topmost_tentative_parse (parser);
8068
8069 /* Look for the type-name again. We are not responsible for
8070 checking that it matches the first type-name. */
8071 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8072 }
8073
8074 /* Parse a unary-expression.
8075
8076 unary-expression:
8077 postfix-expression
8078 ++ cast-expression
8079 -- cast-expression
8080 unary-operator cast-expression
8081 sizeof unary-expression
8082 sizeof ( type-id )
8083 alignof ( type-id ) [C++0x]
8084 new-expression
8085 delete-expression
8086
8087 GNU Extensions:
8088
8089 unary-expression:
8090 __extension__ cast-expression
8091 __alignof__ unary-expression
8092 __alignof__ ( type-id )
8093 alignof unary-expression [C++0x]
8094 __real__ cast-expression
8095 __imag__ cast-expression
8096 && identifier
8097 sizeof ( type-id ) { initializer-list , [opt] }
8098 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8099 __alignof__ ( type-id ) { initializer-list , [opt] }
8100
8101 ADDRESS_P is true iff the unary-expression is appearing as the
8102 operand of the `&' operator. CAST_P is true if this expression is
8103 the target of a cast.
8104
8105 Returns a representation of the expression. */
8106
8107 static cp_expr
8108 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8109 bool address_p, bool cast_p, bool decltype_p)
8110 {
8111 cp_token *token;
8112 enum tree_code unary_operator;
8113
8114 /* Peek at the next token. */
8115 token = cp_lexer_peek_token (parser->lexer);
8116 /* Some keywords give away the kind of expression. */
8117 if (token->type == CPP_KEYWORD)
8118 {
8119 enum rid keyword = token->keyword;
8120
8121 switch (keyword)
8122 {
8123 case RID_ALIGNOF:
8124 case RID_SIZEOF:
8125 {
8126 tree operand, ret;
8127 enum tree_code op;
8128 location_t start_loc = token->location;
8129
8130 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8131 bool std_alignof = id_equal (token->u.value, "alignof");
8132
8133 /* Consume the token. */
8134 cp_lexer_consume_token (parser->lexer);
8135 /* Parse the operand. */
8136 operand = cp_parser_sizeof_operand (parser, keyword);
8137
8138 if (TYPE_P (operand))
8139 ret = cxx_sizeof_or_alignof_type (operand, op, std_alignof,
8140 true);
8141 else
8142 {
8143 /* ISO C++ defines alignof only with types, not with
8144 expressions. So pedwarn if alignof is used with a non-
8145 type expression. However, __alignof__ is ok. */
8146 if (std_alignof)
8147 pedwarn (token->location, OPT_Wpedantic,
8148 "ISO C++ does not allow %<alignof%> "
8149 "with a non-type");
8150
8151 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8152 }
8153 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8154 SIZEOF_EXPR with the original operand. */
8155 if (op == SIZEOF_EXPR && ret != error_mark_node)
8156 {
8157 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8158 {
8159 if (!processing_template_decl && TYPE_P (operand))
8160 {
8161 ret = build_min (SIZEOF_EXPR, size_type_node,
8162 build1 (NOP_EXPR, operand,
8163 error_mark_node));
8164 SIZEOF_EXPR_TYPE_P (ret) = 1;
8165 }
8166 else
8167 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8168 TREE_SIDE_EFFECTS (ret) = 0;
8169 TREE_READONLY (ret) = 1;
8170 }
8171 }
8172
8173 /* Construct a location e.g. :
8174 alignof (expr)
8175 ^~~~~~~~~~~~~~
8176 with start == caret at the start of the "alignof"/"sizeof"
8177 token, with the endpoint at the final closing paren. */
8178 location_t finish_loc
8179 = cp_lexer_previous_token (parser->lexer)->location;
8180 location_t compound_loc
8181 = make_location (start_loc, start_loc, finish_loc);
8182
8183 cp_expr ret_expr (ret);
8184 ret_expr.set_location (compound_loc);
8185 ret_expr = ret_expr.maybe_add_location_wrapper ();
8186 return ret_expr;
8187 }
8188
8189 case RID_BUILTIN_HAS_ATTRIBUTE:
8190 return cp_parser_has_attribute_expression (parser);
8191
8192 case RID_NEW:
8193 return cp_parser_new_expression (parser);
8194
8195 case RID_DELETE:
8196 return cp_parser_delete_expression (parser);
8197
8198 case RID_EXTENSION:
8199 {
8200 /* The saved value of the PEDANTIC flag. */
8201 int saved_pedantic;
8202 tree expr;
8203
8204 /* Save away the PEDANTIC flag. */
8205 cp_parser_extension_opt (parser, &saved_pedantic);
8206 /* Parse the cast-expression. */
8207 expr = cp_parser_simple_cast_expression (parser);
8208 /* Restore the PEDANTIC flag. */
8209 pedantic = saved_pedantic;
8210
8211 return expr;
8212 }
8213
8214 case RID_REALPART:
8215 case RID_IMAGPART:
8216 {
8217 tree expression;
8218
8219 /* Consume the `__real__' or `__imag__' token. */
8220 cp_lexer_consume_token (parser->lexer);
8221 /* Parse the cast-expression. */
8222 expression = cp_parser_simple_cast_expression (parser);
8223 /* Create the complete representation. */
8224 return build_x_unary_op (token->location,
8225 (keyword == RID_REALPART
8226 ? REALPART_EXPR : IMAGPART_EXPR),
8227 expression,
8228 tf_warning_or_error);
8229 }
8230 break;
8231
8232 case RID_TRANSACTION_ATOMIC:
8233 case RID_TRANSACTION_RELAXED:
8234 return cp_parser_transaction_expression (parser, keyword);
8235
8236 case RID_NOEXCEPT:
8237 {
8238 tree expr;
8239 const char *saved_message;
8240 bool saved_integral_constant_expression_p;
8241 bool saved_non_integral_constant_expression_p;
8242 bool saved_greater_than_is_operator_p;
8243
8244 location_t start_loc = token->location;
8245
8246 cp_lexer_consume_token (parser->lexer);
8247 matching_parens parens;
8248 parens.require_open (parser);
8249
8250 saved_message = parser->type_definition_forbidden_message;
8251 parser->type_definition_forbidden_message
8252 = G_("types may not be defined in %<noexcept%> expressions");
8253
8254 saved_integral_constant_expression_p
8255 = parser->integral_constant_expression_p;
8256 saved_non_integral_constant_expression_p
8257 = parser->non_integral_constant_expression_p;
8258 parser->integral_constant_expression_p = false;
8259
8260 saved_greater_than_is_operator_p
8261 = parser->greater_than_is_operator_p;
8262 parser->greater_than_is_operator_p = true;
8263
8264 ++cp_unevaluated_operand;
8265 ++c_inhibit_evaluation_warnings;
8266 ++cp_noexcept_operand;
8267 expr = cp_parser_expression (parser);
8268 --cp_noexcept_operand;
8269 --c_inhibit_evaluation_warnings;
8270 --cp_unevaluated_operand;
8271
8272 parser->greater_than_is_operator_p
8273 = saved_greater_than_is_operator_p;
8274
8275 parser->integral_constant_expression_p
8276 = saved_integral_constant_expression_p;
8277 parser->non_integral_constant_expression_p
8278 = saved_non_integral_constant_expression_p;
8279
8280 parser->type_definition_forbidden_message = saved_message;
8281
8282 location_t finish_loc
8283 = cp_lexer_peek_token (parser->lexer)->location;
8284 parens.require_close (parser);
8285
8286 /* Construct a location of the form:
8287 noexcept (expr)
8288 ^~~~~~~~~~~~~~~
8289 with start == caret, finishing at the close-paren. */
8290 location_t noexcept_loc
8291 = make_location (start_loc, start_loc, finish_loc);
8292
8293 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8294 noexcept_loc);
8295 }
8296
8297 default:
8298 break;
8299 }
8300 }
8301
8302 /* Look for the `:: new' and `:: delete', which also signal the
8303 beginning of a new-expression, or delete-expression,
8304 respectively. If the next token is `::', then it might be one of
8305 these. */
8306 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8307 {
8308 enum rid keyword;
8309
8310 /* See if the token after the `::' is one of the keywords in
8311 which we're interested. */
8312 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8313 /* If it's `new', we have a new-expression. */
8314 if (keyword == RID_NEW)
8315 return cp_parser_new_expression (parser);
8316 /* Similarly, for `delete'. */
8317 else if (keyword == RID_DELETE)
8318 return cp_parser_delete_expression (parser);
8319 }
8320
8321 /* Look for a unary operator. */
8322 unary_operator = cp_parser_unary_operator (token);
8323 /* The `++' and `--' operators can be handled similarly, even though
8324 they are not technically unary-operators in the grammar. */
8325 if (unary_operator == ERROR_MARK)
8326 {
8327 if (token->type == CPP_PLUS_PLUS)
8328 unary_operator = PREINCREMENT_EXPR;
8329 else if (token->type == CPP_MINUS_MINUS)
8330 unary_operator = PREDECREMENT_EXPR;
8331 /* Handle the GNU address-of-label extension. */
8332 else if (cp_parser_allow_gnu_extensions_p (parser)
8333 && token->type == CPP_AND_AND)
8334 {
8335 tree identifier;
8336 tree expression;
8337 location_t start_loc = token->location;
8338
8339 /* Consume the '&&' token. */
8340 cp_lexer_consume_token (parser->lexer);
8341 /* Look for the identifier. */
8342 location_t finish_loc
8343 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8344 identifier = cp_parser_identifier (parser);
8345 /* Construct a location of the form:
8346 &&label
8347 ^~~~~~~
8348 with caret==start at the "&&", finish at the end of the label. */
8349 location_t combined_loc
8350 = make_location (start_loc, start_loc, finish_loc);
8351 /* Create an expression representing the address. */
8352 expression = finish_label_address_expr (identifier, combined_loc);
8353 if (cp_parser_non_integral_constant_expression (parser,
8354 NIC_ADDR_LABEL))
8355 expression = error_mark_node;
8356 return expression;
8357 }
8358 }
8359 if (unary_operator != ERROR_MARK)
8360 {
8361 cp_expr cast_expression;
8362 cp_expr expression = error_mark_node;
8363 non_integral_constant non_constant_p = NIC_NONE;
8364 location_t loc = token->location;
8365 tsubst_flags_t complain = complain_flags (decltype_p);
8366
8367 /* Consume the operator token. */
8368 token = cp_lexer_consume_token (parser->lexer);
8369 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8370
8371 /* Parse the cast-expression. */
8372 cast_expression
8373 = cp_parser_cast_expression (parser,
8374 unary_operator == ADDR_EXPR,
8375 /*cast_p=*/false,
8376 /*decltype*/false,
8377 pidk);
8378
8379 /* Make a location:
8380 OP_TOKEN CAST_EXPRESSION
8381 ^~~~~~~~~~~~~~~~~~~~~~~~~
8382 with start==caret at the operator token, and
8383 extending to the end of the cast_expression. */
8384 loc = make_location (loc, loc, cast_expression.get_finish ());
8385
8386 /* Now, build an appropriate representation. */
8387 switch (unary_operator)
8388 {
8389 case INDIRECT_REF:
8390 non_constant_p = NIC_STAR;
8391 expression = build_x_indirect_ref (loc, cast_expression,
8392 RO_UNARY_STAR,
8393 complain);
8394 /* TODO: build_x_indirect_ref does not always honor the
8395 location, so ensure it is set. */
8396 expression.set_location (loc);
8397 break;
8398
8399 case ADDR_EXPR:
8400 non_constant_p = NIC_ADDR;
8401 /* Fall through. */
8402 case BIT_NOT_EXPR:
8403 expression = build_x_unary_op (loc, unary_operator,
8404 cast_expression,
8405 complain);
8406 /* TODO: build_x_unary_op does not always honor the location,
8407 so ensure it is set. */
8408 expression.set_location (loc);
8409 break;
8410
8411 case PREINCREMENT_EXPR:
8412 case PREDECREMENT_EXPR:
8413 non_constant_p = unary_operator == PREINCREMENT_EXPR
8414 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8415 /* Fall through. */
8416 case NEGATE_EXPR:
8417 /* Immediately fold negation of a constant, unless the constant is 0
8418 (since -0 == 0) or it would overflow. */
8419 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
8420 {
8421 tree stripped_expr
8422 = tree_strip_any_location_wrapper (cast_expression);
8423 if (CONSTANT_CLASS_P (stripped_expr)
8424 && !integer_zerop (stripped_expr)
8425 && !TREE_OVERFLOW (stripped_expr))
8426 {
8427 tree folded = fold_build1 (unary_operator,
8428 TREE_TYPE (stripped_expr),
8429 stripped_expr);
8430 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8431 {
8432 expression = maybe_wrap_with_location (folded, loc);
8433 break;
8434 }
8435 }
8436 }
8437 /* Fall through. */
8438 case UNARY_PLUS_EXPR:
8439 case TRUTH_NOT_EXPR:
8440 expression = finish_unary_op_expr (loc, unary_operator,
8441 cast_expression, complain);
8442 break;
8443
8444 default:
8445 gcc_unreachable ();
8446 }
8447
8448 if (non_constant_p != NIC_NONE
8449 && cp_parser_non_integral_constant_expression (parser,
8450 non_constant_p))
8451 expression = error_mark_node;
8452
8453 return expression;
8454 }
8455
8456 return cp_parser_postfix_expression (parser, address_p, cast_p,
8457 /*member_access_only_p=*/false,
8458 decltype_p,
8459 pidk);
8460 }
8461
8462 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8463 unary-operator, the corresponding tree code is returned. */
8464
8465 static enum tree_code
8466 cp_parser_unary_operator (cp_token* token)
8467 {
8468 switch (token->type)
8469 {
8470 case CPP_MULT:
8471 return INDIRECT_REF;
8472
8473 case CPP_AND:
8474 return ADDR_EXPR;
8475
8476 case CPP_PLUS:
8477 return UNARY_PLUS_EXPR;
8478
8479 case CPP_MINUS:
8480 return NEGATE_EXPR;
8481
8482 case CPP_NOT:
8483 return TRUTH_NOT_EXPR;
8484
8485 case CPP_COMPL:
8486 return BIT_NOT_EXPR;
8487
8488 default:
8489 return ERROR_MARK;
8490 }
8491 }
8492
8493 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8494 Returns a representation of the expression. */
8495
8496 static tree
8497 cp_parser_has_attribute_expression (cp_parser *parser)
8498 {
8499 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8500
8501 /* Consume the __builtin_has_attribute token. */
8502 cp_lexer_consume_token (parser->lexer);
8503
8504 matching_parens parens;
8505 if (!parens.require_open (parser))
8506 return error_mark_node;
8507
8508 /* Types cannot be defined in a `sizeof' expression. Save away the
8509 old message. */
8510 const char *saved_message = parser->type_definition_forbidden_message;
8511 /* And create the new one. */
8512 const int kwd = RID_BUILTIN_HAS_ATTRIBUTE;
8513 char *tmp = concat ("types may not be defined in %<",
8514 IDENTIFIER_POINTER (ridpointers[kwd]),
8515 "%> expressions", NULL);
8516 parser->type_definition_forbidden_message = tmp;
8517
8518 /* The restrictions on constant-expressions do not apply inside
8519 sizeof expressions. */
8520 bool saved_integral_constant_expression_p
8521 = parser->integral_constant_expression_p;
8522 bool saved_non_integral_constant_expression_p
8523 = parser->non_integral_constant_expression_p;
8524 parser->integral_constant_expression_p = false;
8525
8526 /* Do not actually evaluate the expression. */
8527 ++cp_unevaluated_operand;
8528 ++c_inhibit_evaluation_warnings;
8529
8530 tree oper = NULL_TREE;
8531
8532 /* We can't be sure yet whether we're looking at a type-id or an
8533 expression. */
8534 cp_parser_parse_tentatively (parser);
8535
8536 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8537 parser->in_type_id_in_expr_p = true;
8538 /* Look for the type-id. */
8539 oper = cp_parser_type_id (parser);
8540 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8541
8542 cp_parser_parse_definitely (parser);
8543
8544 /* If the type-id production did not work out, then we must be
8545 looking at the unary-expression production. */
8546 if (!oper || oper == error_mark_node)
8547 oper = cp_parser_unary_expression (parser);
8548
8549 STRIP_ANY_LOCATION_WRAPPER (oper);
8550
8551 /* Go back to evaluating expressions. */
8552 --cp_unevaluated_operand;
8553 --c_inhibit_evaluation_warnings;
8554
8555 /* Free the message we created. */
8556 free (tmp);
8557 /* And restore the old one. */
8558 parser->type_definition_forbidden_message = saved_message;
8559 parser->integral_constant_expression_p
8560 = saved_integral_constant_expression_p;
8561 parser->non_integral_constant_expression_p
8562 = saved_non_integral_constant_expression_p;
8563
8564 /* Consume the comma if it's there. */
8565 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
8566 {
8567 cp_parser_skip_to_closing_parenthesis (parser, false, false,
8568 /*consume_paren=*/true);
8569 return error_mark_node;
8570 }
8571
8572 /* Parse the attribute specification. */
8573 bool ret = false;
8574 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
8575 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
8576 {
8577 if (oper != error_mark_node)
8578 {
8579 /* Fold constant expressions used in attributes first. */
8580 cp_check_const_attributes (attr);
8581
8582 /* Finally, see if OPER has been declared with ATTR. */
8583 ret = has_attribute (atloc, oper, attr, default_conversion);
8584 }
8585
8586 parens.require_close (parser);
8587 }
8588 else
8589 {
8590 error_at (atloc, "expected identifier");
8591 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8592 }
8593
8594 /* Construct a location e.g. :
8595 __builtin_has_attribute (oper, attr)
8596 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8597 with start == caret at the start of the built-in token,
8598 and with the endpoint at the final closing paren. */
8599 location_t finish_loc
8600 = cp_lexer_previous_token (parser->lexer)->location;
8601 location_t compound_loc
8602 = make_location (start_loc, start_loc, finish_loc);
8603
8604 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
8605 ret_expr.set_location (compound_loc);
8606 ret_expr = ret_expr.maybe_add_location_wrapper ();
8607 return ret_expr;
8608 }
8609
8610 /* Parse a new-expression.
8611
8612 new-expression:
8613 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8614 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8615
8616 Returns a representation of the expression. */
8617
8618 static tree
8619 cp_parser_new_expression (cp_parser* parser)
8620 {
8621 bool global_scope_p;
8622 vec<tree, va_gc> *placement;
8623 tree type;
8624 vec<tree, va_gc> *initializer;
8625 tree nelts = NULL_TREE;
8626 tree ret;
8627
8628 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8629
8630 /* Look for the optional `::' operator. */
8631 global_scope_p
8632 = (cp_parser_global_scope_opt (parser,
8633 /*current_scope_valid_p=*/false)
8634 != NULL_TREE);
8635 /* Look for the `new' operator. */
8636 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8637 /* There's no easy way to tell a new-placement from the
8638 `( type-id )' construct. */
8639 cp_parser_parse_tentatively (parser);
8640 /* Look for a new-placement. */
8641 placement = cp_parser_new_placement (parser);
8642 /* If that didn't work out, there's no new-placement. */
8643 if (!cp_parser_parse_definitely (parser))
8644 {
8645 if (placement != NULL)
8646 release_tree_vector (placement);
8647 placement = NULL;
8648 }
8649
8650 /* If the next token is a `(', then we have a parenthesized
8651 type-id. */
8652 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8653 {
8654 cp_token *token;
8655 const char *saved_message = parser->type_definition_forbidden_message;
8656
8657 /* Consume the `('. */
8658 matching_parens parens;
8659 parens.consume_open (parser);
8660
8661 /* Parse the type-id. */
8662 parser->type_definition_forbidden_message
8663 = G_("types may not be defined in a new-expression");
8664 {
8665 type_id_in_expr_sentinel s (parser);
8666 type = cp_parser_type_id (parser);
8667 }
8668 parser->type_definition_forbidden_message = saved_message;
8669
8670 /* Look for the closing `)'. */
8671 parens.require_close (parser);
8672 token = cp_lexer_peek_token (parser->lexer);
8673 /* There should not be a direct-new-declarator in this production,
8674 but GCC used to allowed this, so we check and emit a sensible error
8675 message for this case. */
8676 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8677 {
8678 error_at (token->location,
8679 "array bound forbidden after parenthesized type-id");
8680 inform (token->location,
8681 "try removing the parentheses around the type-id");
8682 cp_parser_direct_new_declarator (parser);
8683 }
8684 }
8685 /* Otherwise, there must be a new-type-id. */
8686 else
8687 type = cp_parser_new_type_id (parser, &nelts);
8688
8689 /* If the next token is a `(' or '{', then we have a new-initializer. */
8690 cp_token *token = cp_lexer_peek_token (parser->lexer);
8691 if (token->type == CPP_OPEN_PAREN
8692 || token->type == CPP_OPEN_BRACE)
8693 initializer = cp_parser_new_initializer (parser);
8694 else
8695 initializer = NULL;
8696
8697 /* A new-expression may not appear in an integral constant
8698 expression. */
8699 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8700 ret = error_mark_node;
8701 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8702 of a new-type-id or type-id of a new-expression, the new-expression shall
8703 contain a new-initializer of the form ( assignment-expression )".
8704 Additionally, consistently with the spirit of DR 1467, we want to accept
8705 'new auto { 2 }' too. */
8706 else if ((ret = type_uses_auto (type))
8707 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8708 && (vec_safe_length (initializer) != 1
8709 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8710 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8711 {
8712 error_at (token->location,
8713 "initialization of new-expression for type %<auto%> "
8714 "requires exactly one element");
8715 ret = error_mark_node;
8716 }
8717 else
8718 {
8719 /* Construct a location e.g.:
8720 ptr = new int[100]
8721 ^~~~~~~~~~~~
8722 with caret == start at the start of the "new" token, and the end
8723 at the end of the final token we consumed. */
8724 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8725 location_t end_loc = get_finish (end_tok->location);
8726 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8727
8728 /* Create a representation of the new-expression. */
8729 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8730 tf_warning_or_error);
8731 protected_set_expr_location (ret, combined_loc);
8732 }
8733
8734 if (placement != NULL)
8735 release_tree_vector (placement);
8736 if (initializer != NULL)
8737 release_tree_vector (initializer);
8738
8739 return ret;
8740 }
8741
8742 /* Parse a new-placement.
8743
8744 new-placement:
8745 ( expression-list )
8746
8747 Returns the same representation as for an expression-list. */
8748
8749 static vec<tree, va_gc> *
8750 cp_parser_new_placement (cp_parser* parser)
8751 {
8752 vec<tree, va_gc> *expression_list;
8753
8754 /* Parse the expression-list. */
8755 expression_list = (cp_parser_parenthesized_expression_list
8756 (parser, non_attr, /*cast_p=*/false,
8757 /*allow_expansion_p=*/true,
8758 /*non_constant_p=*/NULL));
8759
8760 if (expression_list && expression_list->is_empty ())
8761 error ("expected expression-list or type-id");
8762
8763 return expression_list;
8764 }
8765
8766 /* Parse a new-type-id.
8767
8768 new-type-id:
8769 type-specifier-seq new-declarator [opt]
8770
8771 Returns the TYPE allocated. If the new-type-id indicates an array
8772 type, *NELTS is set to the number of elements in the last array
8773 bound; the TYPE will not include the last array bound. */
8774
8775 static tree
8776 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8777 {
8778 cp_decl_specifier_seq type_specifier_seq;
8779 cp_declarator *new_declarator;
8780 cp_declarator *declarator;
8781 cp_declarator *outer_declarator;
8782 const char *saved_message;
8783
8784 /* The type-specifier sequence must not contain type definitions.
8785 (It cannot contain declarations of new types either, but if they
8786 are not definitions we will catch that because they are not
8787 complete.) */
8788 saved_message = parser->type_definition_forbidden_message;
8789 parser->type_definition_forbidden_message
8790 = G_("types may not be defined in a new-type-id");
8791 /* Parse the type-specifier-seq. */
8792 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
8793 /*is_declaration=*/false,
8794 /*is_trailing_return=*/false,
8795 &type_specifier_seq);
8796 /* Restore the old message. */
8797 parser->type_definition_forbidden_message = saved_message;
8798
8799 if (type_specifier_seq.type == error_mark_node)
8800 return error_mark_node;
8801
8802 /* Parse the new-declarator. */
8803 new_declarator = cp_parser_new_declarator_opt (parser);
8804
8805 /* Determine the number of elements in the last array dimension, if
8806 any. */
8807 *nelts = NULL_TREE;
8808 /* Skip down to the last array dimension. */
8809 declarator = new_declarator;
8810 outer_declarator = NULL;
8811 while (declarator && (declarator->kind == cdk_pointer
8812 || declarator->kind == cdk_ptrmem))
8813 {
8814 outer_declarator = declarator;
8815 declarator = declarator->declarator;
8816 }
8817 while (declarator
8818 && declarator->kind == cdk_array
8819 && declarator->declarator
8820 && declarator->declarator->kind == cdk_array)
8821 {
8822 outer_declarator = declarator;
8823 declarator = declarator->declarator;
8824 }
8825
8826 if (declarator && declarator->kind == cdk_array)
8827 {
8828 *nelts = declarator->u.array.bounds;
8829 if (*nelts == error_mark_node)
8830 *nelts = integer_one_node;
8831
8832 if (outer_declarator)
8833 outer_declarator->declarator = declarator->declarator;
8834 else
8835 new_declarator = NULL;
8836 }
8837
8838 return groktypename (&type_specifier_seq, new_declarator, false);
8839 }
8840
8841 /* Parse an (optional) new-declarator.
8842
8843 new-declarator:
8844 ptr-operator new-declarator [opt]
8845 direct-new-declarator
8846
8847 Returns the declarator. */
8848
8849 static cp_declarator *
8850 cp_parser_new_declarator_opt (cp_parser* parser)
8851 {
8852 enum tree_code code;
8853 tree type, std_attributes = NULL_TREE;
8854 cp_cv_quals cv_quals;
8855
8856 /* We don't know if there's a ptr-operator next, or not. */
8857 cp_parser_parse_tentatively (parser);
8858 /* Look for a ptr-operator. */
8859 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8860 /* If that worked, look for more new-declarators. */
8861 if (cp_parser_parse_definitely (parser))
8862 {
8863 cp_declarator *declarator;
8864
8865 /* Parse another optional declarator. */
8866 declarator = cp_parser_new_declarator_opt (parser);
8867
8868 declarator = cp_parser_make_indirect_declarator
8869 (code, type, cv_quals, declarator, std_attributes);
8870
8871 return declarator;
8872 }
8873
8874 /* If the next token is a `[', there is a direct-new-declarator. */
8875 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8876 return cp_parser_direct_new_declarator (parser);
8877
8878 return NULL;
8879 }
8880
8881 /* Parse a direct-new-declarator.
8882
8883 direct-new-declarator:
8884 [ expression ]
8885 direct-new-declarator [constant-expression]
8886
8887 */
8888
8889 static cp_declarator *
8890 cp_parser_direct_new_declarator (cp_parser* parser)
8891 {
8892 cp_declarator *declarator = NULL;
8893
8894 while (true)
8895 {
8896 tree expression;
8897 cp_token *token;
8898
8899 /* Look for the opening `['. */
8900 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8901
8902 token = cp_lexer_peek_token (parser->lexer);
8903 expression = cp_parser_expression (parser);
8904 /* The standard requires that the expression have integral
8905 type. DR 74 adds enumeration types. We believe that the
8906 real intent is that these expressions be handled like the
8907 expression in a `switch' condition, which also allows
8908 classes with a single conversion to integral or
8909 enumeration type. */
8910 if (!processing_template_decl)
8911 {
8912 expression
8913 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8914 expression,
8915 /*complain=*/true);
8916 if (!expression)
8917 {
8918 error_at (token->location,
8919 "expression in new-declarator must have integral "
8920 "or enumeration type");
8921 expression = error_mark_node;
8922 }
8923 }
8924
8925 /* Look for the closing `]'. */
8926 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8927
8928 /* Add this bound to the declarator. */
8929 declarator = make_array_declarator (declarator, expression);
8930
8931 /* If the next token is not a `[', then there are no more
8932 bounds. */
8933 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8934 break;
8935 }
8936
8937 return declarator;
8938 }
8939
8940 /* Parse a new-initializer.
8941
8942 new-initializer:
8943 ( expression-list [opt] )
8944 braced-init-list
8945
8946 Returns a representation of the expression-list. */
8947
8948 static vec<tree, va_gc> *
8949 cp_parser_new_initializer (cp_parser* parser)
8950 {
8951 vec<tree, va_gc> *expression_list;
8952
8953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8954 {
8955 tree t;
8956 bool expr_non_constant_p;
8957 cp_lexer_set_source_position (parser->lexer);
8958 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8959 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8960 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8961 expression_list = make_tree_vector_single (t);
8962 }
8963 else
8964 expression_list = (cp_parser_parenthesized_expression_list
8965 (parser, non_attr, /*cast_p=*/false,
8966 /*allow_expansion_p=*/true,
8967 /*non_constant_p=*/NULL));
8968
8969 return expression_list;
8970 }
8971
8972 /* Parse a delete-expression.
8973
8974 delete-expression:
8975 :: [opt] delete cast-expression
8976 :: [opt] delete [ ] cast-expression
8977
8978 Returns a representation of the expression. */
8979
8980 static tree
8981 cp_parser_delete_expression (cp_parser* parser)
8982 {
8983 bool global_scope_p;
8984 bool array_p;
8985 tree expression;
8986
8987 /* Look for the optional `::' operator. */
8988 global_scope_p
8989 = (cp_parser_global_scope_opt (parser,
8990 /*current_scope_valid_p=*/false)
8991 != NULL_TREE);
8992 /* Look for the `delete' keyword. */
8993 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8994 /* See if the array syntax is in use. */
8995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8996 {
8997 /* Consume the `[' token. */
8998 cp_lexer_consume_token (parser->lexer);
8999 /* Look for the `]' token. */
9000 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9001 /* Remember that this is the `[]' construct. */
9002 array_p = true;
9003 }
9004 else
9005 array_p = false;
9006
9007 /* Parse the cast-expression. */
9008 expression = cp_parser_simple_cast_expression (parser);
9009
9010 /* A delete-expression may not appear in an integral constant
9011 expression. */
9012 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9013 return error_mark_node;
9014
9015 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
9016 tf_warning_or_error);
9017 }
9018
9019 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9020 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9021 0 otherwise. */
9022
9023 static int
9024 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9025 {
9026 cp_token *token = cp_lexer_peek_token (parser->lexer);
9027 switch (token->type)
9028 {
9029 case CPP_COMMA:
9030 case CPP_SEMICOLON:
9031 case CPP_QUERY:
9032 case CPP_COLON:
9033 case CPP_CLOSE_SQUARE:
9034 case CPP_CLOSE_PAREN:
9035 case CPP_CLOSE_BRACE:
9036 case CPP_OPEN_BRACE:
9037 case CPP_DOT:
9038 case CPP_DOT_STAR:
9039 case CPP_DEREF:
9040 case CPP_DEREF_STAR:
9041 case CPP_DIV:
9042 case CPP_MOD:
9043 case CPP_LSHIFT:
9044 case CPP_RSHIFT:
9045 case CPP_LESS:
9046 case CPP_GREATER:
9047 case CPP_LESS_EQ:
9048 case CPP_GREATER_EQ:
9049 case CPP_EQ_EQ:
9050 case CPP_NOT_EQ:
9051 case CPP_EQ:
9052 case CPP_MULT_EQ:
9053 case CPP_DIV_EQ:
9054 case CPP_MOD_EQ:
9055 case CPP_PLUS_EQ:
9056 case CPP_MINUS_EQ:
9057 case CPP_RSHIFT_EQ:
9058 case CPP_LSHIFT_EQ:
9059 case CPP_AND_EQ:
9060 case CPP_XOR_EQ:
9061 case CPP_OR_EQ:
9062 case CPP_XOR:
9063 case CPP_OR:
9064 case CPP_OR_OR:
9065 case CPP_EOF:
9066 case CPP_ELLIPSIS:
9067 return 0;
9068
9069 case CPP_OPEN_PAREN:
9070 /* In ((type ()) () the last () isn't a valid cast-expression,
9071 so the whole must be parsed as postfix-expression. */
9072 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9073 != CPP_CLOSE_PAREN;
9074
9075 case CPP_OPEN_SQUARE:
9076 /* '[' may start a primary-expression in obj-c++ and in C++11,
9077 as a lambda-expression, eg, '(void)[]{}'. */
9078 if (cxx_dialect >= cxx11)
9079 return -1;
9080 return c_dialect_objc ();
9081
9082 case CPP_PLUS_PLUS:
9083 case CPP_MINUS_MINUS:
9084 /* '++' and '--' may or may not start a cast-expression:
9085
9086 struct T { void operator++(int); };
9087 void f() { (T())++; }
9088
9089 vs
9090
9091 int a;
9092 (int)++a; */
9093 return -1;
9094
9095 default:
9096 return 1;
9097 }
9098 }
9099
9100 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9101 in the order: const_cast, static_cast, reinterpret_cast.
9102
9103 Don't suggest dynamic_cast.
9104
9105 Return the first legal cast kind found, or NULL otherwise. */
9106
9107 static const char *
9108 get_cast_suggestion (tree dst_type, tree orig_expr)
9109 {
9110 tree trial;
9111
9112 /* Reuse the parser logic by attempting to build the various kinds of
9113 cast, with "complain" disabled.
9114 Identify the first such cast that is valid. */
9115
9116 /* Don't attempt to run such logic within template processing. */
9117 if (processing_template_decl)
9118 return NULL;
9119
9120 /* First try const_cast. */
9121 trial = build_const_cast (dst_type, orig_expr, tf_none);
9122 if (trial != error_mark_node)
9123 return "const_cast";
9124
9125 /* If that fails, try static_cast. */
9126 trial = build_static_cast (dst_type, orig_expr, tf_none);
9127 if (trial != error_mark_node)
9128 return "static_cast";
9129
9130 /* Finally, try reinterpret_cast. */
9131 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
9132 if (trial != error_mark_node)
9133 return "reinterpret_cast";
9134
9135 /* No such cast possible. */
9136 return NULL;
9137 }
9138
9139 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9140 suggesting how to convert a C-style cast of the form:
9141
9142 (DST_TYPE)ORIG_EXPR
9143
9144 to a C++-style cast.
9145
9146 The primary range of RICHLOC is asssumed to be that of the original
9147 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9148 of the parens in the C-style cast. */
9149
9150 static void
9151 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9152 location_t close_paren_loc, tree orig_expr,
9153 tree dst_type)
9154 {
9155 /* This function is non-trivial, so bail out now if the warning isn't
9156 going to be emitted. */
9157 if (!warn_old_style_cast)
9158 return;
9159
9160 /* Try to find a legal C++ cast, trying them in order:
9161 const_cast, static_cast, reinterpret_cast. */
9162 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9163 if (!cast_suggestion)
9164 return;
9165
9166 /* Replace the open paren with "CAST_SUGGESTION<". */
9167 pretty_printer pp;
9168 pp_printf (&pp, "%s<", cast_suggestion);
9169 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9170
9171 /* Replace the close paren with "> (". */
9172 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9173
9174 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9175 rich_loc->add_fixit_insert_after (")");
9176 }
9177
9178
9179 /* Parse a cast-expression.
9180
9181 cast-expression:
9182 unary-expression
9183 ( type-id ) cast-expression
9184
9185 ADDRESS_P is true iff the unary-expression is appearing as the
9186 operand of the `&' operator. CAST_P is true if this expression is
9187 the target of a cast.
9188
9189 Returns a representation of the expression. */
9190
9191 static cp_expr
9192 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9193 bool decltype_p, cp_id_kind * pidk)
9194 {
9195 /* If it's a `(', then we might be looking at a cast. */
9196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9197 {
9198 tree type = NULL_TREE;
9199 cp_expr expr (NULL_TREE);
9200 int cast_expression = 0;
9201 const char *saved_message;
9202
9203 /* There's no way to know yet whether or not this is a cast.
9204 For example, `(int (3))' is a unary-expression, while `(int)
9205 3' is a cast. So, we resort to parsing tentatively. */
9206 cp_parser_parse_tentatively (parser);
9207 /* Types may not be defined in a cast. */
9208 saved_message = parser->type_definition_forbidden_message;
9209 parser->type_definition_forbidden_message
9210 = G_("types may not be defined in casts");
9211 /* Consume the `('. */
9212 matching_parens parens;
9213 cp_token *open_paren = parens.consume_open (parser);
9214 location_t open_paren_loc = open_paren->location;
9215 location_t close_paren_loc = UNKNOWN_LOCATION;
9216
9217 /* A very tricky bit is that `(struct S) { 3 }' is a
9218 compound-literal (which we permit in C++ as an extension).
9219 But, that construct is not a cast-expression -- it is a
9220 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9221 is legal; if the compound-literal were a cast-expression,
9222 you'd need an extra set of parentheses.) But, if we parse
9223 the type-id, and it happens to be a class-specifier, then we
9224 will commit to the parse at that point, because we cannot
9225 undo the action that is done when creating a new class. So,
9226 then we cannot back up and do a postfix-expression.
9227
9228 Another tricky case is the following (c++/29234):
9229
9230 struct S { void operator () (); };
9231
9232 void foo ()
9233 {
9234 ( S()() );
9235 }
9236
9237 As a type-id we parse the parenthesized S()() as a function
9238 returning a function, groktypename complains and we cannot
9239 back up in this case either.
9240
9241 Therefore, we scan ahead to the closing `)', and check to see
9242 if the tokens after the `)' can start a cast-expression. Otherwise
9243 we are dealing with an unary-expression, a postfix-expression
9244 or something else.
9245
9246 Yet another tricky case, in C++11, is the following (c++/54891):
9247
9248 (void)[]{};
9249
9250 The issue is that usually, besides the case of lambda-expressions,
9251 the parenthesized type-id cannot be followed by '[', and, eg, we
9252 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9253 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9254 we don't commit, we try a cast-expression, then an unary-expression.
9255
9256 Save tokens so that we can put them back. */
9257 cp_lexer_save_tokens (parser->lexer);
9258
9259 /* We may be looking at a cast-expression. */
9260 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9261 /*consume_paren=*/true))
9262 cast_expression
9263 = cp_parser_tokens_start_cast_expression (parser);
9264
9265 /* Roll back the tokens we skipped. */
9266 cp_lexer_rollback_tokens (parser->lexer);
9267 /* If we aren't looking at a cast-expression, simulate an error so
9268 that the call to cp_parser_error_occurred below returns true. */
9269 if (!cast_expression)
9270 cp_parser_simulate_error (parser);
9271 else
9272 {
9273 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9274 parser->in_type_id_in_expr_p = true;
9275 /* Look for the type-id. */
9276 type = cp_parser_type_id (parser);
9277 /* Look for the closing `)'. */
9278 cp_token *close_paren = parens.require_close (parser);
9279 if (close_paren)
9280 close_paren_loc = close_paren->location;
9281 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9282 }
9283
9284 /* Restore the saved message. */
9285 parser->type_definition_forbidden_message = saved_message;
9286
9287 /* At this point this can only be either a cast or a
9288 parenthesized ctor such as `(T ())' that looks like a cast to
9289 function returning T. */
9290 if (!cp_parser_error_occurred (parser))
9291 {
9292 /* Only commit if the cast-expression doesn't start with
9293 '++', '--', or '[' in C++11. */
9294 if (cast_expression > 0)
9295 cp_parser_commit_to_topmost_tentative_parse (parser);
9296
9297 expr = cp_parser_cast_expression (parser,
9298 /*address_p=*/false,
9299 /*cast_p=*/true,
9300 /*decltype_p=*/false,
9301 pidk);
9302
9303 if (cp_parser_parse_definitely (parser))
9304 {
9305 /* Warn about old-style casts, if so requested. */
9306 if (warn_old_style_cast
9307 && !in_system_header_at (input_location)
9308 && !VOID_TYPE_P (type)
9309 && current_lang_name != lang_name_c)
9310 {
9311 gcc_rich_location rich_loc (input_location);
9312 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9313 expr, type);
9314 warning_at (&rich_loc, OPT_Wold_style_cast,
9315 "use of old-style cast to %q#T", type);
9316 }
9317
9318 /* Only type conversions to integral or enumeration types
9319 can be used in constant-expressions. */
9320 if (!cast_valid_in_integral_constant_expression_p (type)
9321 && cp_parser_non_integral_constant_expression (parser,
9322 NIC_CAST))
9323 return error_mark_node;
9324
9325 /* Perform the cast. */
9326 /* Make a location:
9327 (TYPE) EXPR
9328 ^~~~~~~~~~~
9329 with start==caret at the open paren, extending to the
9330 end of "expr". */
9331 location_t cast_loc = make_location (open_paren_loc,
9332 open_paren_loc,
9333 expr.get_finish ());
9334 expr = build_c_cast (cast_loc, type, expr);
9335 return expr;
9336 }
9337 }
9338 else
9339 cp_parser_abort_tentative_parse (parser);
9340 }
9341
9342 /* If we get here, then it's not a cast, so it must be a
9343 unary-expression. */
9344 return cp_parser_unary_expression (parser, pidk, address_p,
9345 cast_p, decltype_p);
9346 }
9347
9348 /* Parse a binary expression of the general form:
9349
9350 pm-expression:
9351 cast-expression
9352 pm-expression .* cast-expression
9353 pm-expression ->* cast-expression
9354
9355 multiplicative-expression:
9356 pm-expression
9357 multiplicative-expression * pm-expression
9358 multiplicative-expression / pm-expression
9359 multiplicative-expression % pm-expression
9360
9361 additive-expression:
9362 multiplicative-expression
9363 additive-expression + multiplicative-expression
9364 additive-expression - multiplicative-expression
9365
9366 shift-expression:
9367 additive-expression
9368 shift-expression << additive-expression
9369 shift-expression >> additive-expression
9370
9371 relational-expression:
9372 shift-expression
9373 relational-expression < shift-expression
9374 relational-expression > shift-expression
9375 relational-expression <= shift-expression
9376 relational-expression >= shift-expression
9377
9378 GNU Extension:
9379
9380 relational-expression:
9381 relational-expression <? shift-expression
9382 relational-expression >? shift-expression
9383
9384 equality-expression:
9385 relational-expression
9386 equality-expression == relational-expression
9387 equality-expression != relational-expression
9388
9389 and-expression:
9390 equality-expression
9391 and-expression & equality-expression
9392
9393 exclusive-or-expression:
9394 and-expression
9395 exclusive-or-expression ^ and-expression
9396
9397 inclusive-or-expression:
9398 exclusive-or-expression
9399 inclusive-or-expression | exclusive-or-expression
9400
9401 logical-and-expression:
9402 inclusive-or-expression
9403 logical-and-expression && inclusive-or-expression
9404
9405 logical-or-expression:
9406 logical-and-expression
9407 logical-or-expression || logical-and-expression
9408
9409 All these are implemented with a single function like:
9410
9411 binary-expression:
9412 simple-cast-expression
9413 binary-expression <token> binary-expression
9414
9415 CAST_P is true if this expression is the target of a cast.
9416
9417 The binops_by_token map is used to get the tree codes for each <token> type.
9418 binary-expressions are associated according to a precedence table. */
9419
9420 #define TOKEN_PRECEDENCE(token) \
9421 (((token->type == CPP_GREATER \
9422 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9423 && !parser->greater_than_is_operator_p) \
9424 ? PREC_NOT_OPERATOR \
9425 : binops_by_token[token->type].prec)
9426
9427 static cp_expr
9428 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9429 bool no_toplevel_fold_p,
9430 bool decltype_p,
9431 enum cp_parser_prec prec,
9432 cp_id_kind * pidk)
9433 {
9434 cp_parser_expression_stack stack;
9435 cp_parser_expression_stack_entry *sp = &stack[0];
9436 cp_parser_expression_stack_entry current;
9437 cp_expr rhs;
9438 cp_token *token;
9439 enum tree_code rhs_type;
9440 enum cp_parser_prec new_prec, lookahead_prec;
9441 tree overload;
9442
9443 /* Parse the first expression. */
9444 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9445 ? TRUTH_NOT_EXPR : ERROR_MARK);
9446 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9447 cast_p, decltype_p, pidk);
9448 current.prec = prec;
9449
9450 if (cp_parser_error_occurred (parser))
9451 return error_mark_node;
9452
9453 for (;;)
9454 {
9455 /* Get an operator token. */
9456 token = cp_lexer_peek_token (parser->lexer);
9457
9458 if (warn_cxx11_compat
9459 && token->type == CPP_RSHIFT
9460 && !parser->greater_than_is_operator_p)
9461 {
9462 if (warning_at (token->location, OPT_Wc__11_compat,
9463 "%<>>%> operator is treated"
9464 " as two right angle brackets in C++11"))
9465 inform (token->location,
9466 "suggest parentheses around %<>>%> expression");
9467 }
9468
9469 new_prec = TOKEN_PRECEDENCE (token);
9470 if (new_prec != PREC_NOT_OPERATOR
9471 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9472 /* This is a fold-expression; handle it later. */
9473 new_prec = PREC_NOT_OPERATOR;
9474
9475 /* Popping an entry off the stack means we completed a subexpression:
9476 - either we found a token which is not an operator (`>' where it is not
9477 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9478 will happen repeatedly;
9479 - or, we found an operator which has lower priority. This is the case
9480 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9481 parsing `3 * 4'. */
9482 if (new_prec <= current.prec)
9483 {
9484 if (sp == stack)
9485 break;
9486 else
9487 goto pop;
9488 }
9489
9490 get_rhs:
9491 current.tree_type = binops_by_token[token->type].tree_type;
9492 current.loc = token->location;
9493
9494 /* We used the operator token. */
9495 cp_lexer_consume_token (parser->lexer);
9496
9497 /* For "false && x" or "true || x", x will never be executed;
9498 disable warnings while evaluating it. */
9499 if (current.tree_type == TRUTH_ANDIF_EXPR)
9500 c_inhibit_evaluation_warnings +=
9501 cp_fully_fold (current.lhs) == truthvalue_false_node;
9502 else if (current.tree_type == TRUTH_ORIF_EXPR)
9503 c_inhibit_evaluation_warnings +=
9504 cp_fully_fold (current.lhs) == truthvalue_true_node;
9505
9506 /* Extract another operand. It may be the RHS of this expression
9507 or the LHS of a new, higher priority expression. */
9508 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9509 ? TRUTH_NOT_EXPR : ERROR_MARK);
9510 rhs = cp_parser_simple_cast_expression (parser);
9511
9512 /* Get another operator token. Look up its precedence to avoid
9513 building a useless (immediately popped) stack entry for common
9514 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9515 token = cp_lexer_peek_token (parser->lexer);
9516 lookahead_prec = TOKEN_PRECEDENCE (token);
9517 if (lookahead_prec != PREC_NOT_OPERATOR
9518 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9519 lookahead_prec = PREC_NOT_OPERATOR;
9520 if (lookahead_prec > new_prec)
9521 {
9522 /* ... and prepare to parse the RHS of the new, higher priority
9523 expression. Since precedence levels on the stack are
9524 monotonically increasing, we do not have to care about
9525 stack overflows. */
9526 *sp = current;
9527 ++sp;
9528 current.lhs = rhs;
9529 current.lhs_type = rhs_type;
9530 current.prec = new_prec;
9531 new_prec = lookahead_prec;
9532 goto get_rhs;
9533
9534 pop:
9535 lookahead_prec = new_prec;
9536 /* If the stack is not empty, we have parsed into LHS the right side
9537 (`4' in the example above) of an expression we had suspended.
9538 We can use the information on the stack to recover the LHS (`3')
9539 from the stack together with the tree code (`MULT_EXPR'), and
9540 the precedence of the higher level subexpression
9541 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9542 which will be used to actually build the additive expression. */
9543 rhs = current.lhs;
9544 rhs_type = current.lhs_type;
9545 --sp;
9546 current = *sp;
9547 }
9548
9549 /* Undo the disabling of warnings done above. */
9550 if (current.tree_type == TRUTH_ANDIF_EXPR)
9551 c_inhibit_evaluation_warnings -=
9552 cp_fully_fold (current.lhs) == truthvalue_false_node;
9553 else if (current.tree_type == TRUTH_ORIF_EXPR)
9554 c_inhibit_evaluation_warnings -=
9555 cp_fully_fold (current.lhs) == truthvalue_true_node;
9556
9557 if (warn_logical_not_paren
9558 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9559 && current.lhs_type == TRUTH_NOT_EXPR
9560 /* Avoid warning for !!x == y. */
9561 && (TREE_CODE (current.lhs) != NE_EXPR
9562 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9563 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9564 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9565 /* Avoid warning for !b == y where b is boolean. */
9566 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9567 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9568 != BOOLEAN_TYPE))))
9569 /* Avoid warning for !!b == y where b is boolean. */
9570 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
9571 || TREE_TYPE (current.lhs) == NULL_TREE
9572 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9573 warn_logical_not_parentheses (current.loc, current.tree_type,
9574 current.lhs, maybe_constant_value (rhs));
9575
9576 overload = NULL;
9577
9578 location_t combined_loc = make_location (current.loc,
9579 current.lhs.get_start (),
9580 rhs.get_finish ());
9581
9582 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9583 ERROR_MARK for everything that is not a binary expression.
9584 This makes warn_about_parentheses miss some warnings that
9585 involve unary operators. For unary expressions we should
9586 pass the correct tree_code unless the unary expression was
9587 surrounded by parentheses.
9588 */
9589 if (no_toplevel_fold_p
9590 && lookahead_prec <= current.prec
9591 && sp == stack)
9592 {
9593 if (current.lhs == error_mark_node || rhs == error_mark_node)
9594 current.lhs = error_mark_node;
9595 else
9596 {
9597 current.lhs
9598 = build_min (current.tree_type,
9599 TREE_CODE_CLASS (current.tree_type)
9600 == tcc_comparison
9601 ? boolean_type_node : TREE_TYPE (current.lhs),
9602 current.lhs.get_value (), rhs.get_value ());
9603 SET_EXPR_LOCATION (current.lhs, combined_loc);
9604 }
9605 }
9606 else
9607 {
9608 op_location_t op_loc (current.loc, combined_loc);
9609 current.lhs = build_x_binary_op (op_loc, current.tree_type,
9610 current.lhs, current.lhs_type,
9611 rhs, rhs_type, &overload,
9612 complain_flags (decltype_p));
9613 /* TODO: build_x_binary_op doesn't always honor the location. */
9614 current.lhs.set_location (combined_loc);
9615 }
9616 current.lhs_type = current.tree_type;
9617
9618 /* If the binary operator required the use of an overloaded operator,
9619 then this expression cannot be an integral constant-expression.
9620 An overloaded operator can be used even if both operands are
9621 otherwise permissible in an integral constant-expression if at
9622 least one of the operands is of enumeration type. */
9623
9624 if (overload
9625 && cp_parser_non_integral_constant_expression (parser,
9626 NIC_OVERLOADED))
9627 return error_mark_node;
9628 }
9629
9630 return current.lhs;
9631 }
9632
9633 static cp_expr
9634 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9635 bool no_toplevel_fold_p,
9636 enum cp_parser_prec prec,
9637 cp_id_kind * pidk)
9638 {
9639 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9640 /*decltype*/false, prec, pidk);
9641 }
9642
9643 /* Parse the `? expression : assignment-expression' part of a
9644 conditional-expression. The LOGICAL_OR_EXPR is the
9645 logical-or-expression that started the conditional-expression.
9646 Returns a representation of the entire conditional-expression.
9647
9648 This routine is used by cp_parser_assignment_expression.
9649
9650 ? expression : assignment-expression
9651
9652 GNU Extensions:
9653
9654 ? : assignment-expression */
9655
9656 static tree
9657 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9658 {
9659 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9660 cp_expr assignment_expr;
9661 struct cp_token *token;
9662 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9663
9664 /* Consume the `?' token. */
9665 cp_lexer_consume_token (parser->lexer);
9666 token = cp_lexer_peek_token (parser->lexer);
9667 if (cp_parser_allow_gnu_extensions_p (parser)
9668 && token->type == CPP_COLON)
9669 {
9670 pedwarn (token->location, OPT_Wpedantic,
9671 "ISO C++ does not allow ?: with omitted middle operand");
9672 /* Implicit true clause. */
9673 expr = NULL_TREE;
9674 c_inhibit_evaluation_warnings +=
9675 folded_logical_or_expr == truthvalue_true_node;
9676 warn_for_omitted_condop (token->location, logical_or_expr);
9677 }
9678 else
9679 {
9680 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9681 parser->colon_corrects_to_scope_p = false;
9682 /* Parse the expression. */
9683 c_inhibit_evaluation_warnings +=
9684 folded_logical_or_expr == truthvalue_false_node;
9685 expr = cp_parser_expression (parser);
9686 c_inhibit_evaluation_warnings +=
9687 ((folded_logical_or_expr == truthvalue_true_node)
9688 - (folded_logical_or_expr == truthvalue_false_node));
9689 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9690 }
9691
9692 /* The next token should be a `:'. */
9693 cp_parser_require (parser, CPP_COLON, RT_COLON);
9694 /* Parse the assignment-expression. */
9695 assignment_expr = cp_parser_assignment_expression (parser);
9696 c_inhibit_evaluation_warnings -=
9697 folded_logical_or_expr == truthvalue_true_node;
9698
9699 /* Make a location:
9700 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9701 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9702 with the caret at the "?", ranging from the start of
9703 the logical_or_expr to the end of the assignment_expr. */
9704 loc = make_location (loc,
9705 logical_or_expr.get_start (),
9706 assignment_expr.get_finish ());
9707
9708 /* Build the conditional-expression. */
9709 return build_x_conditional_expr (loc, logical_or_expr,
9710 expr,
9711 assignment_expr,
9712 tf_warning_or_error);
9713 }
9714
9715 /* Parse an assignment-expression.
9716
9717 assignment-expression:
9718 conditional-expression
9719 logical-or-expression assignment-operator assignment_expression
9720 throw-expression
9721
9722 CAST_P is true if this expression is the target of a cast.
9723 DECLTYPE_P is true if this expression is the operand of decltype.
9724
9725 Returns a representation for the expression. */
9726
9727 static cp_expr
9728 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9729 bool cast_p, bool decltype_p)
9730 {
9731 cp_expr expr;
9732
9733 /* If the next token is the `throw' keyword, then we're looking at
9734 a throw-expression. */
9735 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9736 expr = cp_parser_throw_expression (parser);
9737 /* Otherwise, it must be that we are looking at a
9738 logical-or-expression. */
9739 else
9740 {
9741 /* Parse the binary expressions (logical-or-expression). */
9742 expr = cp_parser_binary_expression (parser, cast_p, false,
9743 decltype_p,
9744 PREC_NOT_OPERATOR, pidk);
9745 /* If the next token is a `?' then we're actually looking at a
9746 conditional-expression. */
9747 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9748 return cp_parser_question_colon_clause (parser, expr);
9749 else
9750 {
9751 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9752
9753 /* If it's an assignment-operator, we're using the second
9754 production. */
9755 enum tree_code assignment_operator
9756 = cp_parser_assignment_operator_opt (parser);
9757 if (assignment_operator != ERROR_MARK)
9758 {
9759 bool non_constant_p;
9760
9761 /* Parse the right-hand side of the assignment. */
9762 cp_expr rhs = cp_parser_initializer_clause (parser,
9763 &non_constant_p);
9764
9765 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9766 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9767
9768 /* An assignment may not appear in a
9769 constant-expression. */
9770 if (cp_parser_non_integral_constant_expression (parser,
9771 NIC_ASSIGNMENT))
9772 return error_mark_node;
9773 /* Build the assignment expression. Its default
9774 location:
9775 LHS = RHS
9776 ~~~~^~~~~
9777 is the location of the '=' token as the
9778 caret, ranging from the start of the lhs to the
9779 end of the rhs. */
9780 loc = make_location (loc,
9781 expr.get_start (),
9782 rhs.get_finish ());
9783 expr = build_x_modify_expr (loc, expr,
9784 assignment_operator,
9785 rhs,
9786 complain_flags (decltype_p));
9787 /* TODO: build_x_modify_expr doesn't honor the location,
9788 so we must set it here. */
9789 expr.set_location (loc);
9790 }
9791 }
9792 }
9793
9794 return expr;
9795 }
9796
9797 /* Parse an (optional) assignment-operator.
9798
9799 assignment-operator: one of
9800 = *= /= %= += -= >>= <<= &= ^= |=
9801
9802 GNU Extension:
9803
9804 assignment-operator: one of
9805 <?= >?=
9806
9807 If the next token is an assignment operator, the corresponding tree
9808 code is returned, and the token is consumed. For example, for
9809 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9810 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9811 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9812 operator, ERROR_MARK is returned. */
9813
9814 static enum tree_code
9815 cp_parser_assignment_operator_opt (cp_parser* parser)
9816 {
9817 enum tree_code op;
9818 cp_token *token;
9819
9820 /* Peek at the next token. */
9821 token = cp_lexer_peek_token (parser->lexer);
9822
9823 switch (token->type)
9824 {
9825 case CPP_EQ:
9826 op = NOP_EXPR;
9827 break;
9828
9829 case CPP_MULT_EQ:
9830 op = MULT_EXPR;
9831 break;
9832
9833 case CPP_DIV_EQ:
9834 op = TRUNC_DIV_EXPR;
9835 break;
9836
9837 case CPP_MOD_EQ:
9838 op = TRUNC_MOD_EXPR;
9839 break;
9840
9841 case CPP_PLUS_EQ:
9842 op = PLUS_EXPR;
9843 break;
9844
9845 case CPP_MINUS_EQ:
9846 op = MINUS_EXPR;
9847 break;
9848
9849 case CPP_RSHIFT_EQ:
9850 op = RSHIFT_EXPR;
9851 break;
9852
9853 case CPP_LSHIFT_EQ:
9854 op = LSHIFT_EXPR;
9855 break;
9856
9857 case CPP_AND_EQ:
9858 op = BIT_AND_EXPR;
9859 break;
9860
9861 case CPP_XOR_EQ:
9862 op = BIT_XOR_EXPR;
9863 break;
9864
9865 case CPP_OR_EQ:
9866 op = BIT_IOR_EXPR;
9867 break;
9868
9869 default:
9870 /* Nothing else is an assignment operator. */
9871 op = ERROR_MARK;
9872 }
9873
9874 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9875 if (op != ERROR_MARK
9876 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9877 op = ERROR_MARK;
9878
9879 /* If it was an assignment operator, consume it. */
9880 if (op != ERROR_MARK)
9881 cp_lexer_consume_token (parser->lexer);
9882
9883 return op;
9884 }
9885
9886 /* Parse an expression.
9887
9888 expression:
9889 assignment-expression
9890 expression , assignment-expression
9891
9892 CAST_P is true if this expression is the target of a cast.
9893 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9894 except possibly parenthesized or on the RHS of a comma (N3276).
9895
9896 Returns a representation of the expression. */
9897
9898 static cp_expr
9899 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9900 bool cast_p, bool decltype_p)
9901 {
9902 cp_expr expression = NULL_TREE;
9903 location_t loc = UNKNOWN_LOCATION;
9904
9905 while (true)
9906 {
9907 cp_expr assignment_expression;
9908
9909 /* Parse the next assignment-expression. */
9910 assignment_expression
9911 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9912
9913 /* We don't create a temporary for a call that is the immediate operand
9914 of decltype or on the RHS of a comma. But when we see a comma, we
9915 need to create a temporary for a call on the LHS. */
9916 if (decltype_p && !processing_template_decl
9917 && TREE_CODE (assignment_expression) == CALL_EXPR
9918 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9919 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9920 assignment_expression
9921 = build_cplus_new (TREE_TYPE (assignment_expression),
9922 assignment_expression, tf_warning_or_error);
9923
9924 /* If this is the first assignment-expression, we can just
9925 save it away. */
9926 if (!expression)
9927 expression = assignment_expression;
9928 else
9929 {
9930 /* Create a location with caret at the comma, ranging
9931 from the start of the LHS to the end of the RHS. */
9932 loc = make_location (loc,
9933 expression.get_start (),
9934 assignment_expression.get_finish ());
9935 expression = build_x_compound_expr (loc, expression,
9936 assignment_expression,
9937 complain_flags (decltype_p));
9938 expression.set_location (loc);
9939 }
9940 /* If the next token is not a comma, or we're in a fold-expression, then
9941 we are done with the expression. */
9942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9943 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9944 break;
9945 /* Consume the `,'. */
9946 loc = cp_lexer_peek_token (parser->lexer)->location;
9947 cp_lexer_consume_token (parser->lexer);
9948 /* A comma operator cannot appear in a constant-expression. */
9949 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9950 expression = error_mark_node;
9951 }
9952
9953 return expression;
9954 }
9955
9956 /* Parse a constant-expression.
9957
9958 constant-expression:
9959 conditional-expression
9960
9961 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9962 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9963 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9964 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9965 only parse a conditional-expression, otherwise parse an
9966 assignment-expression. See below for rationale. */
9967
9968 static cp_expr
9969 cp_parser_constant_expression (cp_parser* parser,
9970 bool allow_non_constant_p,
9971 bool *non_constant_p,
9972 bool strict_p)
9973 {
9974 bool saved_integral_constant_expression_p;
9975 bool saved_allow_non_integral_constant_expression_p;
9976 bool saved_non_integral_constant_expression_p;
9977 cp_expr expression;
9978
9979 /* It might seem that we could simply parse the
9980 conditional-expression, and then check to see if it were
9981 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9982 one that the compiler can figure out is constant, possibly after
9983 doing some simplifications or optimizations. The standard has a
9984 precise definition of constant-expression, and we must honor
9985 that, even though it is somewhat more restrictive.
9986
9987 For example:
9988
9989 int i[(2, 3)];
9990
9991 is not a legal declaration, because `(2, 3)' is not a
9992 constant-expression. The `,' operator is forbidden in a
9993 constant-expression. However, GCC's constant-folding machinery
9994 will fold this operation to an INTEGER_CST for `3'. */
9995
9996 /* Save the old settings. */
9997 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9998 saved_allow_non_integral_constant_expression_p
9999 = parser->allow_non_integral_constant_expression_p;
10000 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10001 /* We are now parsing a constant-expression. */
10002 parser->integral_constant_expression_p = true;
10003 parser->allow_non_integral_constant_expression_p
10004 = (allow_non_constant_p || cxx_dialect >= cxx11);
10005 parser->non_integral_constant_expression_p = false;
10006 /* Although the grammar says "conditional-expression", when not STRICT_P,
10007 we parse an "assignment-expression", which also permits
10008 "throw-expression" and the use of assignment operators. In the case
10009 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10010 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10011 actually essential that we look for an assignment-expression.
10012 For example, cp_parser_initializer_clauses uses this function to
10013 determine whether a particular assignment-expression is in fact
10014 constant. */
10015 if (strict_p)
10016 {
10017 /* Parse the binary expressions (logical-or-expression). */
10018 expression = cp_parser_binary_expression (parser, false, false, false,
10019 PREC_NOT_OPERATOR, NULL);
10020 /* If the next token is a `?' then we're actually looking at
10021 a conditional-expression; otherwise we're done. */
10022 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10023 expression = cp_parser_question_colon_clause (parser, expression);
10024 }
10025 else
10026 expression = cp_parser_assignment_expression (parser);
10027 /* Restore the old settings. */
10028 parser->integral_constant_expression_p
10029 = saved_integral_constant_expression_p;
10030 parser->allow_non_integral_constant_expression_p
10031 = saved_allow_non_integral_constant_expression_p;
10032 if (cxx_dialect >= cxx11)
10033 {
10034 /* Require an rvalue constant expression here; that's what our
10035 callers expect. Reference constant expressions are handled
10036 separately in e.g. cp_parser_template_argument. */
10037 tree decay = expression;
10038 if (TREE_TYPE (expression)
10039 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10040 decay = build_address (expression);
10041 bool is_const = potential_rvalue_constant_expression (decay);
10042 parser->non_integral_constant_expression_p = !is_const;
10043 if (!is_const && !allow_non_constant_p)
10044 require_potential_rvalue_constant_expression (decay);
10045 }
10046 if (allow_non_constant_p)
10047 *non_constant_p = parser->non_integral_constant_expression_p;
10048 parser->non_integral_constant_expression_p
10049 = saved_non_integral_constant_expression_p;
10050
10051 return expression;
10052 }
10053
10054 /* Parse __builtin_offsetof.
10055
10056 offsetof-expression:
10057 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10058
10059 offsetof-member-designator:
10060 id-expression
10061 | offsetof-member-designator "." id-expression
10062 | offsetof-member-designator "[" expression "]"
10063 | offsetof-member-designator "->" id-expression */
10064
10065 static cp_expr
10066 cp_parser_builtin_offsetof (cp_parser *parser)
10067 {
10068 int save_ice_p, save_non_ice_p;
10069 tree type;
10070 cp_expr expr;
10071 cp_id_kind dummy;
10072 cp_token *token;
10073 location_t finish_loc;
10074
10075 /* We're about to accept non-integral-constant things, but will
10076 definitely yield an integral constant expression. Save and
10077 restore these values around our local parsing. */
10078 save_ice_p = parser->integral_constant_expression_p;
10079 save_non_ice_p = parser->non_integral_constant_expression_p;
10080
10081 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10082
10083 /* Consume the "__builtin_offsetof" token. */
10084 cp_lexer_consume_token (parser->lexer);
10085 /* Consume the opening `('. */
10086 matching_parens parens;
10087 parens.require_open (parser);
10088 /* Parse the type-id. */
10089 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10090 {
10091 const char *saved_message = parser->type_definition_forbidden_message;
10092 parser->type_definition_forbidden_message
10093 = G_("types may not be defined within __builtin_offsetof");
10094 type = cp_parser_type_id (parser);
10095 parser->type_definition_forbidden_message = saved_message;
10096 }
10097 /* Look for the `,'. */
10098 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10099 token = cp_lexer_peek_token (parser->lexer);
10100
10101 /* Build the (type *)null that begins the traditional offsetof macro. */
10102 tree object_ptr
10103 = build_static_cast (build_pointer_type (type), null_pointer_node,
10104 tf_warning_or_error);
10105
10106 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10107 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10108 true, &dummy, token->location);
10109 while (true)
10110 {
10111 token = cp_lexer_peek_token (parser->lexer);
10112 switch (token->type)
10113 {
10114 case CPP_OPEN_SQUARE:
10115 /* offsetof-member-designator "[" expression "]" */
10116 expr = cp_parser_postfix_open_square_expression (parser, expr,
10117 true, false);
10118 break;
10119
10120 case CPP_DEREF:
10121 /* offsetof-member-designator "->" identifier */
10122 expr = grok_array_decl (token->location, expr,
10123 integer_zero_node, false);
10124 /* FALLTHRU */
10125
10126 case CPP_DOT:
10127 /* offsetof-member-designator "." identifier */
10128 cp_lexer_consume_token (parser->lexer);
10129 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10130 expr, true, &dummy,
10131 token->location);
10132 break;
10133
10134 case CPP_CLOSE_PAREN:
10135 /* Consume the ")" token. */
10136 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10137 cp_lexer_consume_token (parser->lexer);
10138 goto success;
10139
10140 default:
10141 /* Error. We know the following require will fail, but
10142 that gives the proper error message. */
10143 parens.require_close (parser);
10144 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10145 expr = error_mark_node;
10146 goto failure;
10147 }
10148 }
10149
10150 success:
10151 /* Make a location of the form:
10152 __builtin_offsetof (struct s, f)
10153 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10154 with caret at the type-id, ranging from the start of the
10155 "_builtin_offsetof" token to the close paren. */
10156 loc = make_location (loc, start_loc, finish_loc);
10157 /* The result will be an INTEGER_CST, so we need to explicitly
10158 preserve the location. */
10159 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10160
10161 failure:
10162 parser->integral_constant_expression_p = save_ice_p;
10163 parser->non_integral_constant_expression_p = save_non_ice_p;
10164
10165 expr = expr.maybe_add_location_wrapper ();
10166 return expr;
10167 }
10168
10169 /* Parse a trait expression.
10170
10171 Returns a representation of the expression, the underlying type
10172 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10173
10174 static cp_expr
10175 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
10176 {
10177 cp_trait_kind kind;
10178 tree type1, type2 = NULL_TREE;
10179 bool binary = false;
10180 bool variadic = false;
10181
10182 switch (keyword)
10183 {
10184 case RID_HAS_NOTHROW_ASSIGN:
10185 kind = CPTK_HAS_NOTHROW_ASSIGN;
10186 break;
10187 case RID_HAS_NOTHROW_CONSTRUCTOR:
10188 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
10189 break;
10190 case RID_HAS_NOTHROW_COPY:
10191 kind = CPTK_HAS_NOTHROW_COPY;
10192 break;
10193 case RID_HAS_TRIVIAL_ASSIGN:
10194 kind = CPTK_HAS_TRIVIAL_ASSIGN;
10195 break;
10196 case RID_HAS_TRIVIAL_CONSTRUCTOR:
10197 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
10198 break;
10199 case RID_HAS_TRIVIAL_COPY:
10200 kind = CPTK_HAS_TRIVIAL_COPY;
10201 break;
10202 case RID_HAS_TRIVIAL_DESTRUCTOR:
10203 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
10204 break;
10205 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
10206 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
10207 break;
10208 case RID_HAS_VIRTUAL_DESTRUCTOR:
10209 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
10210 break;
10211 case RID_IS_ABSTRACT:
10212 kind = CPTK_IS_ABSTRACT;
10213 break;
10214 case RID_IS_AGGREGATE:
10215 kind = CPTK_IS_AGGREGATE;
10216 break;
10217 case RID_IS_BASE_OF:
10218 kind = CPTK_IS_BASE_OF;
10219 binary = true;
10220 break;
10221 case RID_IS_CLASS:
10222 kind = CPTK_IS_CLASS;
10223 break;
10224 case RID_IS_EMPTY:
10225 kind = CPTK_IS_EMPTY;
10226 break;
10227 case RID_IS_ENUM:
10228 kind = CPTK_IS_ENUM;
10229 break;
10230 case RID_IS_FINAL:
10231 kind = CPTK_IS_FINAL;
10232 break;
10233 case RID_IS_LITERAL_TYPE:
10234 kind = CPTK_IS_LITERAL_TYPE;
10235 break;
10236 case RID_IS_POD:
10237 kind = CPTK_IS_POD;
10238 break;
10239 case RID_IS_POLYMORPHIC:
10240 kind = CPTK_IS_POLYMORPHIC;
10241 break;
10242 case RID_IS_SAME_AS:
10243 kind = CPTK_IS_SAME_AS;
10244 binary = true;
10245 break;
10246 case RID_IS_STD_LAYOUT:
10247 kind = CPTK_IS_STD_LAYOUT;
10248 break;
10249 case RID_IS_TRIVIAL:
10250 kind = CPTK_IS_TRIVIAL;
10251 break;
10252 case RID_IS_TRIVIALLY_ASSIGNABLE:
10253 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10254 binary = true;
10255 break;
10256 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10257 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10258 variadic = true;
10259 break;
10260 case RID_IS_TRIVIALLY_COPYABLE:
10261 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10262 break;
10263 case RID_IS_UNION:
10264 kind = CPTK_IS_UNION;
10265 break;
10266 case RID_UNDERLYING_TYPE:
10267 kind = CPTK_UNDERLYING_TYPE;
10268 break;
10269 case RID_BASES:
10270 kind = CPTK_BASES;
10271 break;
10272 case RID_DIRECT_BASES:
10273 kind = CPTK_DIRECT_BASES;
10274 break;
10275 case RID_IS_ASSIGNABLE:
10276 kind = CPTK_IS_ASSIGNABLE;
10277 binary = true;
10278 break;
10279 case RID_IS_CONSTRUCTIBLE:
10280 kind = CPTK_IS_CONSTRUCTIBLE;
10281 variadic = true;
10282 break;
10283 default:
10284 gcc_unreachable ();
10285 }
10286
10287 /* Get location of initial token. */
10288 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10289
10290 /* Consume the token. */
10291 cp_lexer_consume_token (parser->lexer);
10292
10293 matching_parens parens;
10294 parens.require_open (parser);
10295
10296 {
10297 type_id_in_expr_sentinel s (parser);
10298 type1 = cp_parser_type_id (parser);
10299 }
10300
10301 if (type1 == error_mark_node)
10302 return error_mark_node;
10303
10304 if (binary)
10305 {
10306 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10307
10308 {
10309 type_id_in_expr_sentinel s (parser);
10310 type2 = cp_parser_type_id (parser);
10311 }
10312
10313 if (type2 == error_mark_node)
10314 return error_mark_node;
10315 }
10316 else if (variadic)
10317 {
10318 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10319 {
10320 cp_lexer_consume_token (parser->lexer);
10321 tree elt = cp_parser_type_id (parser);
10322 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10323 {
10324 cp_lexer_consume_token (parser->lexer);
10325 elt = make_pack_expansion (elt);
10326 }
10327 if (elt == error_mark_node)
10328 return error_mark_node;
10329 type2 = tree_cons (NULL_TREE, elt, type2);
10330 }
10331 }
10332
10333 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10334 parens.require_close (parser);
10335
10336 /* Construct a location of the form:
10337 __is_trivially_copyable(_Tp)
10338 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10339 with start == caret, finishing at the close-paren. */
10340 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10341
10342 /* Complete the trait expression, which may mean either processing
10343 the trait expr now or saving it for template instantiation. */
10344 switch (kind)
10345 {
10346 case CPTK_UNDERLYING_TYPE:
10347 return cp_expr (finish_underlying_type (type1), trait_loc);
10348 case CPTK_BASES:
10349 return cp_expr (finish_bases (type1, false), trait_loc);
10350 case CPTK_DIRECT_BASES:
10351 return cp_expr (finish_bases (type1, true), trait_loc);
10352 default:
10353 return cp_expr (finish_trait_expr (kind, type1, type2), trait_loc);
10354 }
10355 }
10356
10357 /* Parse a lambda expression.
10358
10359 lambda-expression:
10360 lambda-introducer lambda-declarator [opt] compound-statement
10361
10362 Returns a representation of the expression. */
10363
10364 static cp_expr
10365 cp_parser_lambda_expression (cp_parser* parser)
10366 {
10367 tree lambda_expr = build_lambda_expr ();
10368 tree type;
10369 bool ok = true;
10370 cp_token *token = cp_lexer_peek_token (parser->lexer);
10371 cp_token_position start = 0;
10372
10373 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10374
10375 if (cxx_dialect >= cxx2a)
10376 /* C++20 allows lambdas in unevaluated context. */;
10377 else if (cp_unevaluated_operand)
10378 {
10379 if (!token->error_reported)
10380 {
10381 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10382 "lambda-expression in unevaluated context"
10383 " only available with -std=c++2a or -std=gnu++2a");
10384 token->error_reported = true;
10385 }
10386 ok = false;
10387 }
10388 else if (parser->in_template_argument_list_p)
10389 {
10390 if (!token->error_reported)
10391 {
10392 error_at (token->location, "lambda-expression in template-argument"
10393 " only available with -std=c++2a or -std=gnu++2a");
10394 token->error_reported = true;
10395 }
10396 ok = false;
10397 }
10398
10399 /* We may be in the middle of deferred access check. Disable
10400 it now. */
10401 push_deferring_access_checks (dk_no_deferred);
10402
10403 cp_parser_lambda_introducer (parser, lambda_expr);
10404 if (cp_parser_error_occurred (parser))
10405 return error_mark_node;
10406
10407 type = begin_lambda_type (lambda_expr);
10408 if (type == error_mark_node)
10409 return error_mark_node;
10410
10411 record_lambda_scope (lambda_expr);
10412
10413 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10414 determine_visibility (TYPE_NAME (type));
10415
10416 /* Now that we've started the type, add the capture fields for any
10417 explicit captures. */
10418 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10419
10420 {
10421 /* Inside the class, surrounding template-parameter-lists do not apply. */
10422 unsigned int saved_num_template_parameter_lists
10423 = parser->num_template_parameter_lists;
10424 unsigned char in_statement = parser->in_statement;
10425 bool in_switch_statement_p = parser->in_switch_statement_p;
10426 bool fully_implicit_function_template_p
10427 = parser->fully_implicit_function_template_p;
10428 tree implicit_template_parms = parser->implicit_template_parms;
10429 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10430 bool auto_is_implicit_function_template_parm_p
10431 = parser->auto_is_implicit_function_template_parm_p;
10432
10433 parser->num_template_parameter_lists = 0;
10434 parser->in_statement = 0;
10435 parser->in_switch_statement_p = false;
10436 parser->fully_implicit_function_template_p = false;
10437 parser->implicit_template_parms = 0;
10438 parser->implicit_template_scope = 0;
10439 parser->auto_is_implicit_function_template_parm_p = false;
10440
10441 /* By virtue of defining a local class, a lambda expression has access to
10442 the private variables of enclosing classes. */
10443
10444 if (cp_parser_start_tentative_firewall (parser))
10445 start = token;
10446
10447 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10448
10449 if (ok && cp_parser_error_occurred (parser))
10450 ok = false;
10451
10452 if (ok)
10453 {
10454 cp_parser_lambda_body (parser, lambda_expr);
10455 }
10456 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10457 {
10458 if (cp_parser_skip_to_closing_brace (parser))
10459 cp_lexer_consume_token (parser->lexer);
10460 }
10461
10462 /* The capture list was built up in reverse order; fix that now. */
10463 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10464 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10465
10466 if (ok)
10467 maybe_add_lambda_conv_op (type);
10468
10469 type = finish_struct (type, /*attributes=*/NULL_TREE);
10470
10471 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10472 parser->in_statement = in_statement;
10473 parser->in_switch_statement_p = in_switch_statement_p;
10474 parser->fully_implicit_function_template_p
10475 = fully_implicit_function_template_p;
10476 parser->implicit_template_parms = implicit_template_parms;
10477 parser->implicit_template_scope = implicit_template_scope;
10478 parser->auto_is_implicit_function_template_parm_p
10479 = auto_is_implicit_function_template_parm_p;
10480 }
10481
10482 /* This field is only used during parsing of the lambda. */
10483 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10484
10485 /* This lambda shouldn't have any proxies left at this point. */
10486 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10487 /* And now that we're done, push proxies for an enclosing lambda. */
10488 insert_pending_capture_proxies ();
10489
10490 /* Update the lambda expression to a range. */
10491 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
10492 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
10493 token->location,
10494 end_tok->location);
10495
10496 if (ok)
10497 lambda_expr = build_lambda_object (lambda_expr);
10498 else
10499 lambda_expr = error_mark_node;
10500
10501 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10502
10503 pop_deferring_access_checks ();
10504
10505 return lambda_expr;
10506 }
10507
10508 /* Parse the beginning of a lambda expression.
10509
10510 lambda-introducer:
10511 [ lambda-capture [opt] ]
10512
10513 LAMBDA_EXPR is the current representation of the lambda expression. */
10514
10515 static void
10516 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10517 {
10518 /* Need commas after the first capture. */
10519 bool first = true;
10520
10521 /* Eat the leading `['. */
10522 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10523
10524 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10525 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10526 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10527 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10528 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10529 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10530
10531 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10532 {
10533 cp_lexer_consume_token (parser->lexer);
10534 first = false;
10535
10536 if (!(at_function_scope_p () || parsing_nsdmi ()))
10537 error ("non-local lambda expression cannot have a capture-default");
10538 }
10539
10540 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10541 {
10542 cp_token* capture_token;
10543 tree capture_id;
10544 tree capture_init_expr;
10545 cp_id_kind idk = CP_ID_KIND_NONE;
10546 bool explicit_init_p = false;
10547
10548 enum capture_kind_type
10549 {
10550 BY_COPY,
10551 BY_REFERENCE
10552 };
10553 enum capture_kind_type capture_kind = BY_COPY;
10554
10555 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10556 {
10557 error ("expected end of capture-list");
10558 return;
10559 }
10560
10561 if (first)
10562 first = false;
10563 else
10564 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10565
10566 /* Possibly capture `this'. */
10567 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10568 {
10569 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10570 if (cxx_dialect < cxx2a
10571 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10572 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10573 "with by-copy capture default");
10574 cp_lexer_consume_token (parser->lexer);
10575 add_capture (lambda_expr,
10576 /*id=*/this_identifier,
10577 /*initializer=*/finish_this_expr (),
10578 /*by_reference_p=*/true,
10579 explicit_init_p);
10580 continue;
10581 }
10582
10583 /* Possibly capture `*this'. */
10584 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10585 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10586 {
10587 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10588 if (cxx_dialect < cxx17)
10589 pedwarn (loc, 0, "%<*this%> capture only available with "
10590 "-std=c++17 or -std=gnu++17");
10591 cp_lexer_consume_token (parser->lexer);
10592 cp_lexer_consume_token (parser->lexer);
10593 add_capture (lambda_expr,
10594 /*id=*/this_identifier,
10595 /*initializer=*/finish_this_expr (),
10596 /*by_reference_p=*/false,
10597 explicit_init_p);
10598 continue;
10599 }
10600
10601 bool init_pack_expansion = false;
10602 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10603 {
10604 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10605 if (cxx_dialect < cxx2a)
10606 pedwarn (loc, 0, "pack init-capture only available with "
10607 "-std=c++2a or -std=gnu++2a");
10608 cp_lexer_consume_token (parser->lexer);
10609 init_pack_expansion = true;
10610 }
10611
10612 /* Remember whether we want to capture as a reference or not. */
10613 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10614 {
10615 capture_kind = BY_REFERENCE;
10616 cp_lexer_consume_token (parser->lexer);
10617 }
10618
10619 /* Get the identifier. */
10620 capture_token = cp_lexer_peek_token (parser->lexer);
10621 capture_id = cp_parser_identifier (parser);
10622
10623 if (capture_id == error_mark_node)
10624 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10625 delimiters, but I modified this to stop on unnested ']' as well. It
10626 was already changed to stop on unnested '}', so the
10627 "closing_parenthesis" name is no more misleading with my change. */
10628 {
10629 cp_parser_skip_to_closing_parenthesis (parser,
10630 /*recovering=*/true,
10631 /*or_comma=*/true,
10632 /*consume_paren=*/true);
10633 break;
10634 }
10635
10636 /* Find the initializer for this capture. */
10637 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10638 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10639 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10640 {
10641 bool direct, non_constant;
10642 /* An explicit initializer exists. */
10643 if (cxx_dialect < cxx14)
10644 pedwarn (input_location, 0,
10645 "lambda capture initializers "
10646 "only available with -std=c++14 or -std=gnu++14");
10647 capture_init_expr = cp_parser_initializer (parser, &direct,
10648 &non_constant, true);
10649 explicit_init_p = true;
10650 if (capture_init_expr == NULL_TREE)
10651 {
10652 error ("empty initializer for lambda init-capture");
10653 capture_init_expr = error_mark_node;
10654 }
10655 if (init_pack_expansion)
10656 capture_init_expr = make_pack_expansion (capture_init_expr);
10657 }
10658 else
10659 {
10660 const char* error_msg;
10661
10662 /* Turn the identifier into an id-expression. */
10663 capture_init_expr
10664 = cp_parser_lookup_name_simple (parser, capture_id,
10665 capture_token->location);
10666
10667 if (capture_init_expr == error_mark_node)
10668 {
10669 unqualified_name_lookup_error (capture_id);
10670 continue;
10671 }
10672 else if (!VAR_P (capture_init_expr)
10673 && TREE_CODE (capture_init_expr) != PARM_DECL)
10674 {
10675 error_at (capture_token->location,
10676 "capture of non-variable %qE",
10677 capture_init_expr);
10678 if (DECL_P (capture_init_expr))
10679 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10680 "%q#D declared here", capture_init_expr);
10681 continue;
10682 }
10683 if (VAR_P (capture_init_expr)
10684 && decl_storage_duration (capture_init_expr) != dk_auto)
10685 {
10686 if (pedwarn (capture_token->location, 0, "capture of variable "
10687 "%qD with non-automatic storage duration",
10688 capture_init_expr))
10689 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10690 "%q#D declared here", capture_init_expr);
10691 continue;
10692 }
10693
10694 capture_init_expr
10695 = finish_id_expression
10696 (capture_id,
10697 capture_init_expr,
10698 parser->scope,
10699 &idk,
10700 /*integral_constant_expression_p=*/false,
10701 /*allow_non_integral_constant_expression_p=*/false,
10702 /*non_integral_constant_expression_p=*/NULL,
10703 /*template_p=*/false,
10704 /*done=*/true,
10705 /*address_p=*/false,
10706 /*template_arg_p=*/false,
10707 &error_msg,
10708 capture_token->location);
10709
10710 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10711 {
10712 cp_lexer_consume_token (parser->lexer);
10713 capture_init_expr = make_pack_expansion (capture_init_expr);
10714 }
10715 }
10716
10717 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10718 && !explicit_init_p)
10719 {
10720 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10721 && capture_kind == BY_COPY)
10722 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10723 "of %qD redundant with by-copy capture default",
10724 capture_id);
10725 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10726 && capture_kind == BY_REFERENCE)
10727 pedwarn (capture_token->location, 0, "explicit by-reference "
10728 "capture of %qD redundant with by-reference capture "
10729 "default", capture_id);
10730 }
10731
10732 add_capture (lambda_expr,
10733 capture_id,
10734 capture_init_expr,
10735 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10736 explicit_init_p);
10737
10738 /* If there is any qualification still in effect, clear it
10739 now; we will be starting fresh with the next capture. */
10740 parser->scope = NULL_TREE;
10741 parser->qualifying_scope = NULL_TREE;
10742 parser->object_scope = NULL_TREE;
10743 }
10744
10745 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10746 }
10747
10748 /* Parse the (optional) middle of a lambda expression.
10749
10750 lambda-declarator:
10751 < template-parameter-list [opt] >
10752 ( parameter-declaration-clause [opt] )
10753 attribute-specifier [opt]
10754 decl-specifier-seq [opt]
10755 exception-specification [opt]
10756 lambda-return-type-clause [opt]
10757
10758 LAMBDA_EXPR is the current representation of the lambda expression. */
10759
10760 static bool
10761 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10762 {
10763 /* 5.1.1.4 of the standard says:
10764 If a lambda-expression does not include a lambda-declarator, it is as if
10765 the lambda-declarator were ().
10766 This means an empty parameter list, no attributes, and no exception
10767 specification. */
10768 tree param_list = void_list_node;
10769 tree attributes = NULL_TREE;
10770 tree exception_spec = NULL_TREE;
10771 tree template_param_list = NULL_TREE;
10772 tree tx_qual = NULL_TREE;
10773 tree return_type = NULL_TREE;
10774 cp_decl_specifier_seq lambda_specs;
10775 clear_decl_specs (&lambda_specs);
10776
10777 /* The template-parameter-list is optional, but must begin with
10778 an opening angle if present. */
10779 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10780 {
10781 if (cxx_dialect < cxx14)
10782 pedwarn (parser->lexer->next_token->location, 0,
10783 "lambda templates are only available with "
10784 "-std=c++14 or -std=gnu++14");
10785 else if (cxx_dialect < cxx2a)
10786 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10787 "lambda templates are only available with "
10788 "-std=c++2a or -std=gnu++2a");
10789
10790 cp_lexer_consume_token (parser->lexer);
10791
10792 template_param_list = cp_parser_template_parameter_list (parser);
10793
10794 cp_parser_skip_to_end_of_template_parameter_list (parser);
10795
10796 /* We just processed one more parameter list. */
10797 ++parser->num_template_parameter_lists;
10798 }
10799
10800 /* The parameter-declaration-clause is optional (unless
10801 template-parameter-list was given), but must begin with an
10802 opening parenthesis if present. */
10803 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10804 {
10805 matching_parens parens;
10806 parens.consume_open (parser);
10807
10808 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10809
10810 /* Parse parameters. */
10811 param_list
10812 = cp_parser_parameter_declaration_clause
10813 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
10814
10815 /* Default arguments shall not be specified in the
10816 parameter-declaration-clause of a lambda-declarator. */
10817 if (cxx_dialect < cxx14)
10818 for (tree t = param_list; t; t = TREE_CHAIN (t))
10819 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10820 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10821 "default argument specified for lambda parameter");
10822
10823 parens.require_close (parser);
10824
10825 /* In the decl-specifier-seq of the lambda-declarator, each
10826 decl-specifier shall either be mutable or constexpr. */
10827 int declares_class_or_enum;
10828 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10829 cp_parser_decl_specifier_seq (parser,
10830 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10831 &lambda_specs, &declares_class_or_enum);
10832 if (lambda_specs.storage_class == sc_mutable)
10833 {
10834 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10835 if (lambda_specs.conflicting_specifiers_p)
10836 error_at (lambda_specs.locations[ds_storage_class],
10837 "duplicate %<mutable%>");
10838 }
10839
10840 tx_qual = cp_parser_tx_qualifier_opt (parser);
10841
10842 /* Parse optional exception specification. */
10843 exception_spec = cp_parser_exception_specification_opt (parser);
10844
10845 attributes = cp_parser_std_attribute_spec_seq (parser);
10846
10847 /* Parse optional trailing return type. */
10848 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10849 {
10850 cp_lexer_consume_token (parser->lexer);
10851 return_type = cp_parser_trailing_type_id (parser);
10852 }
10853
10854 /* The function parameters must be in scope all the way until after the
10855 trailing-return-type in case of decltype. */
10856 pop_bindings_and_leave_scope ();
10857 }
10858 else if (template_param_list != NULL_TREE) // generate diagnostic
10859 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10860
10861 /* Create the function call operator.
10862
10863 Messing with declarators like this is no uglier than building up the
10864 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10865 other code. */
10866 {
10867 cp_decl_specifier_seq return_type_specs;
10868 cp_declarator* declarator;
10869 tree fco;
10870 int quals;
10871 void *p;
10872
10873 clear_decl_specs (&return_type_specs);
10874 return_type_specs.type = make_auto ();
10875
10876 if (lambda_specs.locations[ds_constexpr])
10877 {
10878 if (cxx_dialect >= cxx17)
10879 return_type_specs.locations[ds_constexpr]
10880 = lambda_specs.locations[ds_constexpr];
10881 else
10882 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10883 "lambda only available with -std=c++17 or -std=gnu++17");
10884 }
10885
10886 p = obstack_alloc (&declarator_obstack, 0);
10887
10888 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
10889 LAMBDA_EXPR_LOCATION (lambda_expr));
10890
10891 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10892 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10893 declarator = make_call_declarator (declarator, param_list, quals,
10894 VIRT_SPEC_UNSPECIFIED,
10895 REF_QUAL_NONE,
10896 tx_qual,
10897 exception_spec,
10898 return_type,
10899 /*requires_clause*/NULL_TREE);
10900 declarator->std_attributes = attributes;
10901
10902 fco = grokmethod (&return_type_specs,
10903 declarator,
10904 NULL_TREE);
10905 if (fco != error_mark_node)
10906 {
10907 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10908 DECL_ARTIFICIAL (fco) = 1;
10909 /* Give the object parameter a different name. */
10910 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
10911 DECL_LAMBDA_FUNCTION (fco) = 1;
10912 }
10913 if (template_param_list)
10914 {
10915 fco = finish_member_template_decl (fco);
10916 finish_template_decl (template_param_list);
10917 --parser->num_template_parameter_lists;
10918 }
10919 else if (parser->fully_implicit_function_template_p)
10920 fco = finish_fully_implicit_template (parser, fco);
10921
10922 finish_member_declaration (fco);
10923
10924 obstack_free (&declarator_obstack, p);
10925
10926 return (fco != error_mark_node);
10927 }
10928 }
10929
10930 /* Parse the body of a lambda expression, which is simply
10931
10932 compound-statement
10933
10934 but which requires special handling.
10935 LAMBDA_EXPR is the current representation of the lambda expression. */
10936
10937 static void
10938 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10939 {
10940 bool nested = (current_function_decl != NULL_TREE);
10941 unsigned char local_variables_forbidden_p
10942 = parser->local_variables_forbidden_p;
10943 bool in_function_body = parser->in_function_body;
10944
10945 /* The body of a lambda-expression is not a subexpression of the enclosing
10946 expression. */
10947 cp_evaluated ev;
10948
10949 if (nested)
10950 push_function_context ();
10951 else
10952 /* Still increment function_depth so that we don't GC in the
10953 middle of an expression. */
10954 ++function_depth;
10955
10956 vec<tree> omp_privatization_save;
10957 save_omp_privatization_clauses (omp_privatization_save);
10958 /* Clear this in case we're in the middle of a default argument. */
10959 parser->local_variables_forbidden_p = 0;
10960 parser->in_function_body = true;
10961
10962 {
10963 local_specialization_stack s (lss_copy);
10964 tree fco = lambda_function (lambda_expr);
10965 tree body = start_lambda_function (fco, lambda_expr);
10966 matching_braces braces;
10967
10968 if (braces.require_open (parser))
10969 {
10970 tree compound_stmt = begin_compound_stmt (0);
10971
10972 /* Originally C++11 required us to peek for 'return expr'; and
10973 process it specially here to deduce the return type. N3638
10974 removed the need for that. */
10975
10976 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10977 cp_parser_label_declaration (parser);
10978 cp_parser_statement_seq_opt (parser, NULL_TREE);
10979 braces.require_close (parser);
10980
10981 finish_compound_stmt (compound_stmt);
10982 }
10983
10984 finish_lambda_function (body);
10985 }
10986
10987 restore_omp_privatization_clauses (omp_privatization_save);
10988 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10989 parser->in_function_body = in_function_body;
10990 if (nested)
10991 pop_function_context();
10992 else
10993 --function_depth;
10994 }
10995
10996 /* Statements [gram.stmt.stmt] */
10997
10998 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
10999
11000 static void
11001 add_debug_begin_stmt (location_t loc)
11002 {
11003 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11004 return;
11005 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11006 /* A concept is never expanded normally. */
11007 return;
11008
11009 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11010 SET_EXPR_LOCATION (stmt, loc);
11011 add_stmt (stmt);
11012 }
11013
11014 /* Parse a statement.
11015
11016 statement:
11017 labeled-statement
11018 expression-statement
11019 compound-statement
11020 selection-statement
11021 iteration-statement
11022 jump-statement
11023 declaration-statement
11024 try-block
11025
11026 C++11:
11027
11028 statement:
11029 labeled-statement
11030 attribute-specifier-seq (opt) expression-statement
11031 attribute-specifier-seq (opt) compound-statement
11032 attribute-specifier-seq (opt) selection-statement
11033 attribute-specifier-seq (opt) iteration-statement
11034 attribute-specifier-seq (opt) jump-statement
11035 declaration-statement
11036 attribute-specifier-seq (opt) try-block
11037
11038 init-statement:
11039 expression-statement
11040 simple-declaration
11041
11042 TM Extension:
11043
11044 statement:
11045 atomic-statement
11046
11047 IN_COMPOUND is true when the statement is nested inside a
11048 cp_parser_compound_statement; this matters for certain pragmas.
11049
11050 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11051 is a (possibly labeled) if statement which is not enclosed in braces
11052 and has an else clause. This is used to implement -Wparentheses.
11053
11054 CHAIN is a vector of if-else-if conditions. */
11055
11056 static void
11057 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
11058 bool in_compound, bool *if_p, vec<tree> *chain,
11059 location_t *loc_after_labels)
11060 {
11061 tree statement, std_attrs = NULL_TREE;
11062 cp_token *token;
11063 location_t statement_location, attrs_loc;
11064
11065 restart:
11066 if (if_p != NULL)
11067 *if_p = false;
11068 /* There is no statement yet. */
11069 statement = NULL_TREE;
11070
11071 saved_token_sentinel saved_tokens (parser->lexer);
11072 attrs_loc = cp_lexer_peek_token (parser->lexer)->location;
11073 if (c_dialect_objc ())
11074 /* In obj-c++, seeing '[[' might be the either the beginning of
11075 c++11 attributes, or a nested objc-message-expression. So
11076 let's parse the c++11 attributes tentatively. */
11077 cp_parser_parse_tentatively (parser);
11078 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11079 if (std_attrs)
11080 {
11081 location_t end_loc
11082 = cp_lexer_previous_token (parser->lexer)->location;
11083 attrs_loc = make_location (attrs_loc, attrs_loc, end_loc);
11084 }
11085 if (c_dialect_objc ())
11086 {
11087 if (!cp_parser_parse_definitely (parser))
11088 std_attrs = NULL_TREE;
11089 }
11090
11091 /* Peek at the next token. */
11092 token = cp_lexer_peek_token (parser->lexer);
11093 /* Remember the location of the first token in the statement. */
11094 cp_token *statement_token = token;
11095 statement_location = token->location;
11096 add_debug_begin_stmt (statement_location);
11097 /* If this is a keyword, then that will often determine what kind of
11098 statement we have. */
11099 if (token->type == CPP_KEYWORD)
11100 {
11101 enum rid keyword = token->keyword;
11102
11103 switch (keyword)
11104 {
11105 case RID_CASE:
11106 case RID_DEFAULT:
11107 /* Looks like a labeled-statement with a case label.
11108 Parse the label, and then use tail recursion to parse
11109 the statement. */
11110 cp_parser_label_for_labeled_statement (parser, std_attrs);
11111 in_compound = false;
11112 goto restart;
11113
11114 case RID_IF:
11115 case RID_SWITCH:
11116 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11117 statement = cp_parser_selection_statement (parser, if_p, chain);
11118 break;
11119
11120 case RID_WHILE:
11121 case RID_DO:
11122 case RID_FOR:
11123 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11124 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
11125 break;
11126
11127 case RID_BREAK:
11128 case RID_CONTINUE:
11129 case RID_RETURN:
11130 case RID_GOTO:
11131 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11132 statement = cp_parser_jump_statement (parser);
11133 break;
11134
11135 /* Objective-C++ exception-handling constructs. */
11136 case RID_AT_TRY:
11137 case RID_AT_CATCH:
11138 case RID_AT_FINALLY:
11139 case RID_AT_SYNCHRONIZED:
11140 case RID_AT_THROW:
11141 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11142 statement = cp_parser_objc_statement (parser);
11143 break;
11144
11145 case RID_TRY:
11146 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11147 statement = cp_parser_try_block (parser);
11148 break;
11149
11150 case RID_NAMESPACE:
11151 /* This must be a namespace alias definition. */
11152 if (std_attrs != NULL_TREE)
11153 {
11154 /* Attributes should be parsed as part of the the
11155 declaration, so let's un-parse them. */
11156 saved_tokens.rollback();
11157 std_attrs = NULL_TREE;
11158 }
11159 cp_parser_declaration_statement (parser);
11160 return;
11161
11162 case RID_TRANSACTION_ATOMIC:
11163 case RID_TRANSACTION_RELAXED:
11164 case RID_SYNCHRONIZED:
11165 case RID_ATOMIC_NOEXCEPT:
11166 case RID_ATOMIC_CANCEL:
11167 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11168 statement = cp_parser_transaction (parser, token);
11169 break;
11170 case RID_TRANSACTION_CANCEL:
11171 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11172 statement = cp_parser_transaction_cancel (parser);
11173 break;
11174
11175 default:
11176 /* It might be a keyword like `int' that can start a
11177 declaration-statement. */
11178 break;
11179 }
11180 }
11181 else if (token->type == CPP_NAME)
11182 {
11183 /* If the next token is a `:', then we are looking at a
11184 labeled-statement. */
11185 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11186 if (token->type == CPP_COLON)
11187 {
11188 /* Looks like a labeled-statement with an ordinary label.
11189 Parse the label, and then use tail recursion to parse
11190 the statement. */
11191
11192 cp_parser_label_for_labeled_statement (parser, std_attrs);
11193 in_compound = false;
11194 goto restart;
11195 }
11196 }
11197 /* Anything that starts with a `{' must be a compound-statement. */
11198 else if (token->type == CPP_OPEN_BRACE)
11199 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11200 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11201 a statement all its own. */
11202 else if (token->type == CPP_PRAGMA)
11203 {
11204 /* Only certain OpenMP pragmas are attached to statements, and thus
11205 are considered statements themselves. All others are not. In
11206 the context of a compound, accept the pragma as a "statement" and
11207 return so that we can check for a close brace. Otherwise we
11208 require a real statement and must go back and read one. */
11209 if (in_compound)
11210 cp_parser_pragma (parser, pragma_compound, if_p);
11211 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
11212 goto restart;
11213 return;
11214 }
11215 else if (token->type == CPP_EOF)
11216 {
11217 cp_parser_error (parser, "expected statement");
11218 return;
11219 }
11220
11221 /* Everything else must be a declaration-statement or an
11222 expression-statement. Try for the declaration-statement
11223 first, unless we are looking at a `;', in which case we know that
11224 we have an expression-statement. */
11225 if (!statement)
11226 {
11227 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11228 {
11229 if (std_attrs != NULL_TREE)
11230 /* Attributes should be parsed as part of the declaration,
11231 so let's un-parse them. */
11232 saved_tokens.rollback();
11233
11234 cp_parser_parse_tentatively (parser);
11235 /* Try to parse the declaration-statement. */
11236 cp_parser_declaration_statement (parser);
11237 /* If that worked, we're done. */
11238 if (cp_parser_parse_definitely (parser))
11239 return;
11240 /* It didn't work, restore the post-attribute position. */
11241 if (std_attrs)
11242 cp_lexer_set_token_position (parser->lexer, statement_token);
11243 }
11244 /* All preceding labels have been parsed at this point. */
11245 if (loc_after_labels != NULL)
11246 *loc_after_labels = statement_location;
11247
11248 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
11249
11250 /* Look for an expression-statement instead. */
11251 statement = cp_parser_expression_statement (parser, in_statement_expr);
11252
11253 /* Handle [[fallthrough]];. */
11254 if (attribute_fallthrough_p (std_attrs))
11255 {
11256 /* The next token after the fallthrough attribute is ';'. */
11257 if (statement == NULL_TREE)
11258 {
11259 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11260 statement = build_call_expr_internal_loc (statement_location,
11261 IFN_FALLTHROUGH,
11262 void_type_node, 0);
11263 finish_expr_stmt (statement);
11264 }
11265 else
11266 warning_at (statement_location, OPT_Wattributes,
11267 "%<fallthrough%> attribute not followed by %<;%>");
11268 std_attrs = NULL_TREE;
11269 }
11270 }
11271
11272 /* Set the line number for the statement. */
11273 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11274 SET_EXPR_LOCATION (statement, statement_location);
11275
11276 /* Allow "[[fallthrough]];", but warn otherwise. */
11277 if (std_attrs != NULL_TREE)
11278 warning_at (attrs_loc,
11279 OPT_Wattributes,
11280 "attributes at the beginning of statement are ignored");
11281 }
11282
11283 /* Append ATTR to attribute list ATTRS. */
11284
11285 static tree
11286 attr_chainon (tree attrs, tree attr)
11287 {
11288 if (attrs == error_mark_node)
11289 return error_mark_node;
11290 if (attr == error_mark_node)
11291 return error_mark_node;
11292 return chainon (attrs, attr);
11293 }
11294
11295 /* Parse the label for a labeled-statement, i.e.
11296
11297 identifier :
11298 case constant-expression :
11299 default :
11300
11301 GNU Extension:
11302 case constant-expression ... constant-expression : statement
11303
11304 When a label is parsed without errors, the label is added to the
11305 parse tree by the finish_* functions, so this function doesn't
11306 have to return the label. */
11307
11308 static void
11309 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11310 {
11311 cp_token *token;
11312 tree label = NULL_TREE;
11313 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11314
11315 /* The next token should be an identifier. */
11316 token = cp_lexer_peek_token (parser->lexer);
11317 if (token->type != CPP_NAME
11318 && token->type != CPP_KEYWORD)
11319 {
11320 cp_parser_error (parser, "expected labeled-statement");
11321 return;
11322 }
11323
11324 /* Remember whether this case or a user-defined label is allowed to fall
11325 through to. */
11326 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11327
11328 parser->colon_corrects_to_scope_p = false;
11329 switch (token->keyword)
11330 {
11331 case RID_CASE:
11332 {
11333 tree expr, expr_hi;
11334 cp_token *ellipsis;
11335
11336 /* Consume the `case' token. */
11337 cp_lexer_consume_token (parser->lexer);
11338 /* Parse the constant-expression. */
11339 expr = cp_parser_constant_expression (parser);
11340 if (check_for_bare_parameter_packs (expr))
11341 expr = error_mark_node;
11342
11343 ellipsis = cp_lexer_peek_token (parser->lexer);
11344 if (ellipsis->type == CPP_ELLIPSIS)
11345 {
11346 /* Consume the `...' token. */
11347 cp_lexer_consume_token (parser->lexer);
11348 expr_hi = cp_parser_constant_expression (parser);
11349 if (check_for_bare_parameter_packs (expr_hi))
11350 expr_hi = error_mark_node;
11351
11352 /* We don't need to emit warnings here, as the common code
11353 will do this for us. */
11354 }
11355 else
11356 expr_hi = NULL_TREE;
11357
11358 if (parser->in_switch_statement_p)
11359 {
11360 tree l = finish_case_label (token->location, expr, expr_hi);
11361 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11362 {
11363 label = CASE_LABEL (l);
11364 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11365 }
11366 }
11367 else
11368 error_at (token->location,
11369 "case label %qE not within a switch statement",
11370 expr);
11371 }
11372 break;
11373
11374 case RID_DEFAULT:
11375 /* Consume the `default' token. */
11376 cp_lexer_consume_token (parser->lexer);
11377
11378 if (parser->in_switch_statement_p)
11379 {
11380 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11381 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11382 {
11383 label = CASE_LABEL (l);
11384 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11385 }
11386 }
11387 else
11388 error_at (token->location, "case label not within a switch statement");
11389 break;
11390
11391 default:
11392 /* Anything else must be an ordinary label. */
11393 label = finish_label_stmt (cp_parser_identifier (parser));
11394 if (label && TREE_CODE (label) == LABEL_DECL)
11395 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11396 break;
11397 }
11398
11399 /* Require the `:' token. */
11400 cp_parser_require (parser, CPP_COLON, RT_COLON);
11401
11402 /* An ordinary label may optionally be followed by attributes.
11403 However, this is only permitted if the attributes are then
11404 followed by a semicolon. This is because, for backward
11405 compatibility, when parsing
11406 lab: __attribute__ ((unused)) int i;
11407 we want the attribute to attach to "i", not "lab". */
11408 if (label != NULL_TREE
11409 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11410 {
11411 tree attrs;
11412 cp_parser_parse_tentatively (parser);
11413 attrs = cp_parser_gnu_attributes_opt (parser);
11414 if (attrs == NULL_TREE
11415 /* And fallthrough always binds to the expression-statement. */
11416 || attribute_fallthrough_p (attrs)
11417 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11418 cp_parser_abort_tentative_parse (parser);
11419 else if (!cp_parser_parse_definitely (parser))
11420 ;
11421 else
11422 attributes = attr_chainon (attributes, attrs);
11423 }
11424
11425 if (attributes != NULL_TREE)
11426 cplus_decl_attributes (&label, attributes, 0);
11427
11428 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11429 }
11430
11431 /* Parse an expression-statement.
11432
11433 expression-statement:
11434 expression [opt] ;
11435
11436 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11437 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11438 indicates whether this expression-statement is part of an
11439 expression statement. */
11440
11441 static tree
11442 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11443 {
11444 tree statement = NULL_TREE;
11445 cp_token *token = cp_lexer_peek_token (parser->lexer);
11446 location_t loc = token->location;
11447
11448 /* There might be attribute fallthrough. */
11449 tree attr = cp_parser_gnu_attributes_opt (parser);
11450
11451 /* If the next token is a ';', then there is no expression
11452 statement. */
11453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11454 {
11455 statement = cp_parser_expression (parser);
11456 if (statement == error_mark_node
11457 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11458 {
11459 cp_parser_skip_to_end_of_block_or_statement (parser);
11460 return error_mark_node;
11461 }
11462 }
11463
11464 /* Handle [[fallthrough]];. */
11465 if (attribute_fallthrough_p (attr))
11466 {
11467 /* The next token after the fallthrough attribute is ';'. */
11468 if (statement == NULL_TREE)
11469 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11470 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11471 void_type_node, 0);
11472 else
11473 warning_at (loc, OPT_Wattributes,
11474 "%<fallthrough%> attribute not followed by %<;%>");
11475 attr = NULL_TREE;
11476 }
11477
11478 /* Allow "[[fallthrough]];", but warn otherwise. */
11479 if (attr != NULL_TREE)
11480 warning_at (loc, OPT_Wattributes,
11481 "attributes at the beginning of statement are ignored");
11482
11483 /* Give a helpful message for "A<T>::type t;" and the like. */
11484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11485 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11486 {
11487 if (TREE_CODE (statement) == SCOPE_REF)
11488 error_at (token->location, "need %<typename%> before %qE because "
11489 "%qT is a dependent scope",
11490 statement, TREE_OPERAND (statement, 0));
11491 else if (is_overloaded_fn (statement)
11492 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11493 {
11494 /* A::A a; */
11495 tree fn = get_first_fn (statement);
11496 error_at (token->location,
11497 "%<%T::%D%> names the constructor, not the type",
11498 DECL_CONTEXT (fn), DECL_NAME (fn));
11499 }
11500 }
11501
11502 /* Consume the final `;'. */
11503 cp_parser_consume_semicolon_at_end_of_statement (parser);
11504
11505 if (in_statement_expr
11506 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11507 /* This is the final expression statement of a statement
11508 expression. */
11509 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11510 else if (statement)
11511 statement = finish_expr_stmt (statement);
11512
11513 return statement;
11514 }
11515
11516 /* Parse a compound-statement.
11517
11518 compound-statement:
11519 { statement-seq [opt] }
11520
11521 GNU extension:
11522
11523 compound-statement:
11524 { label-declaration-seq [opt] statement-seq [opt] }
11525
11526 label-declaration-seq:
11527 label-declaration
11528 label-declaration-seq label-declaration
11529
11530 Returns a tree representing the statement. */
11531
11532 static tree
11533 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11534 int bcs_flags, bool function_body)
11535 {
11536 tree compound_stmt;
11537 matching_braces braces;
11538
11539 /* Consume the `{'. */
11540 if (!braces.require_open (parser))
11541 return error_mark_node;
11542 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11543 && !function_body && cxx_dialect < cxx14)
11544 pedwarn (input_location, OPT_Wpedantic,
11545 "compound-statement in %<constexpr%> function");
11546 /* Begin the compound-statement. */
11547 compound_stmt = begin_compound_stmt (bcs_flags);
11548 /* If the next keyword is `__label__' we have a label declaration. */
11549 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11550 cp_parser_label_declaration (parser);
11551 /* Parse an (optional) statement-seq. */
11552 cp_parser_statement_seq_opt (parser, in_statement_expr);
11553 /* Finish the compound-statement. */
11554 finish_compound_stmt (compound_stmt);
11555 /* Consume the `}'. */
11556 braces.require_close (parser);
11557
11558 return compound_stmt;
11559 }
11560
11561 /* Parse an (optional) statement-seq.
11562
11563 statement-seq:
11564 statement
11565 statement-seq [opt] statement */
11566
11567 static void
11568 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11569 {
11570 /* Scan statements until there aren't any more. */
11571 while (true)
11572 {
11573 cp_token *token = cp_lexer_peek_token (parser->lexer);
11574
11575 /* If we are looking at a `}', then we have run out of
11576 statements; the same is true if we have reached the end
11577 of file, or have stumbled upon a stray '@end'. */
11578 if (token->type == CPP_CLOSE_BRACE
11579 || token->type == CPP_EOF
11580 || token->type == CPP_PRAGMA_EOL
11581 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11582 break;
11583
11584 /* If we are in a compound statement and find 'else' then
11585 something went wrong. */
11586 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11587 {
11588 if (parser->in_statement & IN_IF_STMT)
11589 break;
11590 else
11591 {
11592 token = cp_lexer_consume_token (parser->lexer);
11593 error_at (token->location, "%<else%> without a previous %<if%>");
11594 }
11595 }
11596
11597 /* Parse the statement. */
11598 cp_parser_statement (parser, in_statement_expr, true, NULL);
11599 }
11600 }
11601
11602 /* Return true if this is the C++20 version of range-based-for with
11603 init-statement. */
11604
11605 static bool
11606 cp_parser_range_based_for_with_init_p (cp_parser *parser)
11607 {
11608 bool r = false;
11609
11610 /* Save tokens so that we can put them back. */
11611 cp_lexer_save_tokens (parser->lexer);
11612
11613 /* There has to be an unnested ; followed by an unnested :. */
11614 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
11615 /*recovering=*/false,
11616 CPP_SEMICOLON,
11617 /*consume_paren=*/false) != -1)
11618 goto out;
11619
11620 /* We found the semicolon, eat it now. */
11621 cp_lexer_consume_token (parser->lexer);
11622
11623 /* Now look for ':' that is not nested in () or {}. */
11624 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
11625 /*recovering=*/false,
11626 CPP_COLON,
11627 /*consume_paren=*/false) == -1);
11628
11629 out:
11630 /* Roll back the tokens we skipped. */
11631 cp_lexer_rollback_tokens (parser->lexer);
11632
11633 return r;
11634 }
11635
11636 /* Return true if we're looking at (init; cond), false otherwise. */
11637
11638 static bool
11639 cp_parser_init_statement_p (cp_parser *parser)
11640 {
11641 /* Save tokens so that we can put them back. */
11642 cp_lexer_save_tokens (parser->lexer);
11643
11644 /* Look for ';' that is not nested in () or {}. */
11645 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11646 /*recovering=*/false,
11647 CPP_SEMICOLON,
11648 /*consume_paren=*/false);
11649
11650 /* Roll back the tokens we skipped. */
11651 cp_lexer_rollback_tokens (parser->lexer);
11652
11653 return ret == -1;
11654 }
11655
11656 /* Parse a selection-statement.
11657
11658 selection-statement:
11659 if ( init-statement [opt] condition ) statement
11660 if ( init-statement [opt] condition ) statement else statement
11661 switch ( init-statement [opt] condition ) statement
11662
11663 Returns the new IF_STMT or SWITCH_STMT.
11664
11665 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11666 is a (possibly labeled) if statement which is not enclosed in
11667 braces and has an else clause. This is used to implement
11668 -Wparentheses.
11669
11670 CHAIN is a vector of if-else-if conditions. This is used to implement
11671 -Wduplicated-cond. */
11672
11673 static tree
11674 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11675 vec<tree> *chain)
11676 {
11677 cp_token *token;
11678 enum rid keyword;
11679 token_indent_info guard_tinfo;
11680
11681 if (if_p != NULL)
11682 *if_p = false;
11683
11684 /* Peek at the next token. */
11685 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11686 guard_tinfo = get_token_indent_info (token);
11687
11688 /* See what kind of keyword it is. */
11689 keyword = token->keyword;
11690 switch (keyword)
11691 {
11692 case RID_IF:
11693 case RID_SWITCH:
11694 {
11695 tree statement;
11696 tree condition;
11697
11698 bool cx = false;
11699 if (keyword == RID_IF
11700 && cp_lexer_next_token_is_keyword (parser->lexer,
11701 RID_CONSTEXPR))
11702 {
11703 cx = true;
11704 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11705 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11706 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11707 "with -std=c++17 or -std=gnu++17");
11708 }
11709
11710 /* Look for the `('. */
11711 matching_parens parens;
11712 if (!parens.require_open (parser))
11713 {
11714 cp_parser_skip_to_end_of_statement (parser);
11715 return error_mark_node;
11716 }
11717
11718 /* Begin the selection-statement. */
11719 if (keyword == RID_IF)
11720 {
11721 statement = begin_if_stmt ();
11722 IF_STMT_CONSTEXPR_P (statement) = cx;
11723 }
11724 else
11725 statement = begin_switch_stmt ();
11726
11727 /* Parse the optional init-statement. */
11728 if (cp_parser_init_statement_p (parser))
11729 {
11730 tree decl;
11731 if (cxx_dialect < cxx17)
11732 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11733 "init-statement in selection statements only available "
11734 "with -std=c++17 or -std=gnu++17");
11735 cp_parser_init_statement (parser, &decl);
11736 }
11737
11738 /* Parse the condition. */
11739 condition = cp_parser_condition (parser);
11740 /* Look for the `)'. */
11741 if (!parens.require_close (parser))
11742 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11743 /*consume_paren=*/true);
11744
11745 if (keyword == RID_IF)
11746 {
11747 bool nested_if;
11748 unsigned char in_statement;
11749
11750 /* Add the condition. */
11751 condition = finish_if_stmt_cond (condition, statement);
11752
11753 if (warn_duplicated_cond)
11754 warn_duplicated_cond_add_or_warn (token->location, condition,
11755 &chain);
11756
11757 /* Parse the then-clause. */
11758 in_statement = parser->in_statement;
11759 parser->in_statement |= IN_IF_STMT;
11760
11761 /* Outside a template, the non-selected branch of a constexpr
11762 if is a 'discarded statement', i.e. unevaluated. */
11763 bool was_discarded = in_discarded_stmt;
11764 bool discard_then = (cx && !processing_template_decl
11765 && integer_zerop (condition));
11766 if (discard_then)
11767 {
11768 in_discarded_stmt = true;
11769 ++c_inhibit_evaluation_warnings;
11770 }
11771
11772 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11773 guard_tinfo);
11774
11775 parser->in_statement = in_statement;
11776
11777 finish_then_clause (statement);
11778
11779 if (discard_then)
11780 {
11781 THEN_CLAUSE (statement) = NULL_TREE;
11782 in_discarded_stmt = was_discarded;
11783 --c_inhibit_evaluation_warnings;
11784 }
11785
11786 /* If the next token is `else', parse the else-clause. */
11787 if (cp_lexer_next_token_is_keyword (parser->lexer,
11788 RID_ELSE))
11789 {
11790 bool discard_else = (cx && !processing_template_decl
11791 && integer_nonzerop (condition));
11792 if (discard_else)
11793 {
11794 in_discarded_stmt = true;
11795 ++c_inhibit_evaluation_warnings;
11796 }
11797
11798 guard_tinfo
11799 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11800 /* Consume the `else' keyword. */
11801 cp_lexer_consume_token (parser->lexer);
11802 if (warn_duplicated_cond)
11803 {
11804 if (cp_lexer_next_token_is_keyword (parser->lexer,
11805 RID_IF)
11806 && chain == NULL)
11807 {
11808 /* We've got "if (COND) else if (COND2)". Start
11809 the condition chain and add COND as the first
11810 element. */
11811 chain = new vec<tree> ();
11812 if (!CONSTANT_CLASS_P (condition)
11813 && !TREE_SIDE_EFFECTS (condition))
11814 {
11815 /* Wrap it in a NOP_EXPR so that we can set the
11816 location of the condition. */
11817 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11818 condition);
11819 SET_EXPR_LOCATION (e, token->location);
11820 chain->safe_push (e);
11821 }
11822 }
11823 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11824 RID_IF))
11825 {
11826 /* This is if-else without subsequent if. Zap the
11827 condition chain; we would have already warned at
11828 this point. */
11829 delete chain;
11830 chain = NULL;
11831 }
11832 }
11833 begin_else_clause (statement);
11834 /* Parse the else-clause. */
11835 cp_parser_implicitly_scoped_statement (parser, NULL,
11836 guard_tinfo, chain);
11837
11838 finish_else_clause (statement);
11839
11840 /* If we are currently parsing a then-clause, then
11841 IF_P will not be NULL. We set it to true to
11842 indicate that this if statement has an else clause.
11843 This may trigger the Wparentheses warning below
11844 when we get back up to the parent if statement. */
11845 if (if_p != NULL)
11846 *if_p = true;
11847
11848 if (discard_else)
11849 {
11850 ELSE_CLAUSE (statement) = NULL_TREE;
11851 in_discarded_stmt = was_discarded;
11852 --c_inhibit_evaluation_warnings;
11853 }
11854 }
11855 else
11856 {
11857 /* This if statement does not have an else clause. If
11858 NESTED_IF is true, then the then-clause has an if
11859 statement which does have an else clause. We warn
11860 about the potential ambiguity. */
11861 if (nested_if)
11862 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11863 "suggest explicit braces to avoid ambiguous"
11864 " %<else%>");
11865 if (warn_duplicated_cond)
11866 {
11867 /* We don't need the condition chain anymore. */
11868 delete chain;
11869 chain = NULL;
11870 }
11871 }
11872
11873 /* Now we're all done with the if-statement. */
11874 finish_if_stmt (statement);
11875 }
11876 else
11877 {
11878 bool in_switch_statement_p;
11879 unsigned char in_statement;
11880
11881 /* Add the condition. */
11882 finish_switch_cond (condition, statement);
11883
11884 /* Parse the body of the switch-statement. */
11885 in_switch_statement_p = parser->in_switch_statement_p;
11886 in_statement = parser->in_statement;
11887 parser->in_switch_statement_p = true;
11888 parser->in_statement |= IN_SWITCH_STMT;
11889 cp_parser_implicitly_scoped_statement (parser, if_p,
11890 guard_tinfo);
11891 parser->in_switch_statement_p = in_switch_statement_p;
11892 parser->in_statement = in_statement;
11893
11894 /* Now we're all done with the switch-statement. */
11895 finish_switch_stmt (statement);
11896 }
11897
11898 return statement;
11899 }
11900 break;
11901
11902 default:
11903 cp_parser_error (parser, "expected selection-statement");
11904 return error_mark_node;
11905 }
11906 }
11907
11908 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
11909 If we have seen at least one decl-specifier, and the next token
11910 is not a parenthesis, then we must be looking at a declaration.
11911 (After "int (" we might be looking at a functional cast.) */
11912
11913 static void
11914 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
11915 bool any_specifiers_p)
11916 {
11917 if (any_specifiers_p
11918 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11919 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11920 && !cp_parser_error_occurred (parser))
11921 cp_parser_commit_to_tentative_parse (parser);
11922 }
11923
11924 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
11925 The declarator shall not specify a function or an array. Returns
11926 TRUE if the declarator is valid, FALSE otherwise. */
11927
11928 static bool
11929 cp_parser_check_condition_declarator (cp_parser* parser,
11930 cp_declarator *declarator,
11931 location_t loc)
11932 {
11933 if (declarator == cp_error_declarator
11934 || function_declarator_p (declarator)
11935 || declarator->kind == cdk_array)
11936 {
11937 if (declarator == cp_error_declarator)
11938 /* Already complained. */;
11939 else if (declarator->kind == cdk_array)
11940 error_at (loc, "condition declares an array");
11941 else
11942 error_at (loc, "condition declares a function");
11943 if (parser->fully_implicit_function_template_p)
11944 abort_fully_implicit_template (parser);
11945 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
11946 /*or_comma=*/false,
11947 /*consume_paren=*/false);
11948 return false;
11949 }
11950 else
11951 return true;
11952 }
11953
11954 /* Parse a condition.
11955
11956 condition:
11957 expression
11958 type-specifier-seq declarator = initializer-clause
11959 type-specifier-seq declarator braced-init-list
11960
11961 GNU Extension:
11962
11963 condition:
11964 type-specifier-seq declarator asm-specification [opt]
11965 attributes [opt] = assignment-expression
11966
11967 Returns the expression that should be tested. */
11968
11969 static tree
11970 cp_parser_condition (cp_parser* parser)
11971 {
11972 cp_decl_specifier_seq type_specifiers;
11973 const char *saved_message;
11974 int declares_class_or_enum;
11975
11976 /* Try the declaration first. */
11977 cp_parser_parse_tentatively (parser);
11978 /* New types are not allowed in the type-specifier-seq for a
11979 condition. */
11980 saved_message = parser->type_definition_forbidden_message;
11981 parser->type_definition_forbidden_message
11982 = G_("types may not be defined in conditions");
11983 /* Parse the type-specifier-seq. */
11984 cp_parser_decl_specifier_seq (parser,
11985 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11986 &type_specifiers,
11987 &declares_class_or_enum);
11988 /* Restore the saved message. */
11989 parser->type_definition_forbidden_message = saved_message;
11990
11991 cp_parser_maybe_commit_to_declaration (parser,
11992 type_specifiers.any_specifiers_p);
11993
11994 /* If all is well, we might be looking at a declaration. */
11995 if (!cp_parser_error_occurred (parser))
11996 {
11997 tree decl;
11998 tree asm_specification;
11999 tree attributes;
12000 cp_declarator *declarator;
12001 tree initializer = NULL_TREE;
12002 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12003
12004 /* Parse the declarator. */
12005 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12006 CP_PARSER_FLAGS_NONE,
12007 /*ctor_dtor_or_conv_p=*/NULL,
12008 /*parenthesized_p=*/NULL,
12009 /*member_p=*/false,
12010 /*friend_p=*/false,
12011 /*static_p=*/false);
12012 /* Parse the attributes. */
12013 attributes = cp_parser_attributes_opt (parser);
12014 /* Parse the asm-specification. */
12015 asm_specification = cp_parser_asm_specification_opt (parser);
12016 /* If the next token is not an `=' or '{', then we might still be
12017 looking at an expression. For example:
12018
12019 if (A(a).x)
12020
12021 looks like a decl-specifier-seq and a declarator -- but then
12022 there is no `=', so this is an expression. */
12023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12024 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12025 cp_parser_simulate_error (parser);
12026
12027 /* If we did see an `=' or '{', then we are looking at a declaration
12028 for sure. */
12029 if (cp_parser_parse_definitely (parser))
12030 {
12031 tree pushed_scope;
12032 bool non_constant_p = false;
12033 int flags = LOOKUP_ONLYCONVERTING;
12034
12035 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
12036 return error_mark_node;
12037
12038 /* Create the declaration. */
12039 decl = start_decl (declarator, &type_specifiers,
12040 /*initialized_p=*/true,
12041 attributes, /*prefix_attributes=*/NULL_TREE,
12042 &pushed_scope);
12043
12044 /* Parse the initializer. */
12045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12046 {
12047 initializer = cp_parser_braced_list (parser, &non_constant_p);
12048 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
12049 flags = 0;
12050 }
12051 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12052 {
12053 /* Consume the `='. */
12054 cp_lexer_consume_token (parser->lexer);
12055 initializer = cp_parser_initializer_clause (parser,
12056 &non_constant_p);
12057 }
12058 else
12059 {
12060 cp_parser_error (parser, "expected initializer");
12061 initializer = error_mark_node;
12062 }
12063 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
12064 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12065
12066 /* Process the initializer. */
12067 cp_finish_decl (decl,
12068 initializer, !non_constant_p,
12069 asm_specification,
12070 flags);
12071
12072 if (pushed_scope)
12073 pop_scope (pushed_scope);
12074
12075 return convert_from_reference (decl);
12076 }
12077 }
12078 /* If we didn't even get past the declarator successfully, we are
12079 definitely not looking at a declaration. */
12080 else
12081 cp_parser_abort_tentative_parse (parser);
12082
12083 /* Otherwise, we are looking at an expression. */
12084 return cp_parser_expression (parser);
12085 }
12086
12087 /* Parses a for-statement or range-for-statement until the closing ')',
12088 not included. */
12089
12090 static tree
12091 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
12092 {
12093 tree init, scope, decl;
12094 bool is_range_for;
12095
12096 /* Begin the for-statement. */
12097 scope = begin_for_scope (&init);
12098
12099 /* Parse the initialization. */
12100 is_range_for = cp_parser_init_statement (parser, &decl);
12101
12102 if (is_range_for)
12103 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
12104 false);
12105 else
12106 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
12107 }
12108
12109 static tree
12110 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
12111 unsigned short unroll)
12112 {
12113 /* Normal for loop */
12114 tree condition = NULL_TREE;
12115 tree expression = NULL_TREE;
12116 tree stmt;
12117
12118 stmt = begin_for_stmt (scope, init);
12119 /* The init-statement has already been parsed in
12120 cp_parser_init_statement, so no work is needed here. */
12121 finish_init_stmt (stmt);
12122
12123 /* If there's a condition, process it. */
12124 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12125 condition = cp_parser_condition (parser);
12126 else if (ivdep)
12127 {
12128 cp_parser_error (parser, "missing loop condition in loop with "
12129 "%<GCC ivdep%> pragma");
12130 condition = error_mark_node;
12131 }
12132 else if (unroll)
12133 {
12134 cp_parser_error (parser, "missing loop condition in loop with "
12135 "%<GCC unroll%> pragma");
12136 condition = error_mark_node;
12137 }
12138 finish_for_cond (condition, stmt, ivdep, unroll);
12139 /* Look for the `;'. */
12140 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12141
12142 /* If there's an expression, process it. */
12143 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
12144 expression = cp_parser_expression (parser);
12145 finish_for_expr (expression, stmt);
12146
12147 return stmt;
12148 }
12149
12150 /* Tries to parse a range-based for-statement:
12151
12152 range-based-for:
12153 decl-specifier-seq declarator : expression
12154
12155 The decl-specifier-seq declarator and the `:' are already parsed by
12156 cp_parser_init_statement. If processing_template_decl it returns a
12157 newly created RANGE_FOR_STMT; if not, it is converted to a
12158 regular FOR_STMT. */
12159
12160 static tree
12161 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
12162 bool ivdep, unsigned short unroll, bool is_omp)
12163 {
12164 tree stmt, range_expr;
12165 auto_vec <cxx_binding *, 16> bindings;
12166 auto_vec <tree, 16> names;
12167 tree decomp_first_name = NULL_TREE;
12168 unsigned int decomp_cnt = 0;
12169
12170 /* Get the range declaration momentarily out of the way so that
12171 the range expression doesn't clash with it. */
12172 if (range_decl != error_mark_node)
12173 {
12174 if (DECL_HAS_VALUE_EXPR_P (range_decl))
12175 {
12176 tree v = DECL_VALUE_EXPR (range_decl);
12177 /* For decomposition declaration get all of the corresponding
12178 declarations out of the way. */
12179 if (TREE_CODE (v) == ARRAY_REF
12180 && VAR_P (TREE_OPERAND (v, 0))
12181 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
12182 {
12183 tree d = range_decl;
12184 range_decl = TREE_OPERAND (v, 0);
12185 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
12186 decomp_first_name = d;
12187 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
12188 {
12189 tree name = DECL_NAME (d);
12190 names.safe_push (name);
12191 bindings.safe_push (IDENTIFIER_BINDING (name));
12192 IDENTIFIER_BINDING (name)
12193 = IDENTIFIER_BINDING (name)->previous;
12194 }
12195 }
12196 }
12197 if (names.is_empty ())
12198 {
12199 tree name = DECL_NAME (range_decl);
12200 names.safe_push (name);
12201 bindings.safe_push (IDENTIFIER_BINDING (name));
12202 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
12203 }
12204 }
12205
12206 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12207 {
12208 bool expr_non_constant_p;
12209 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12210 }
12211 else
12212 range_expr = cp_parser_expression (parser);
12213
12214 /* Put the range declaration(s) back into scope. */
12215 for (unsigned int i = 0; i < names.length (); i++)
12216 {
12217 cxx_binding *binding = bindings[i];
12218 binding->previous = IDENTIFIER_BINDING (names[i]);
12219 IDENTIFIER_BINDING (names[i]) = binding;
12220 }
12221
12222 /* finish_omp_for has its own code for the following, so just
12223 return the range_expr instead. */
12224 if (is_omp)
12225 return range_expr;
12226
12227 /* If in template, STMT is converted to a normal for-statement
12228 at instantiation. If not, it is done just ahead. */
12229 if (processing_template_decl)
12230 {
12231 if (check_for_bare_parameter_packs (range_expr))
12232 range_expr = error_mark_node;
12233 stmt = begin_range_for_stmt (scope, init);
12234 if (ivdep)
12235 RANGE_FOR_IVDEP (stmt) = 1;
12236 if (unroll)
12237 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
12238 finish_range_for_decl (stmt, range_decl, range_expr);
12239 if (!type_dependent_expression_p (range_expr)
12240 /* do_auto_deduction doesn't mess with template init-lists. */
12241 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
12242 do_range_for_auto_deduction (range_decl, range_expr);
12243 }
12244 else
12245 {
12246 stmt = begin_for_stmt (scope, init);
12247 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
12248 decomp_first_name, decomp_cnt, ivdep,
12249 unroll);
12250 }
12251 return stmt;
12252 }
12253
12254 /* Subroutine of cp_convert_range_for: given the initializer expression,
12255 builds up the range temporary. */
12256
12257 static tree
12258 build_range_temp (tree range_expr)
12259 {
12260 tree range_type, range_temp;
12261
12262 /* Find out the type deduced by the declaration
12263 `auto &&__range = range_expr'. */
12264 range_type = cp_build_reference_type (make_auto (), true);
12265 range_type = do_auto_deduction (range_type, range_expr,
12266 type_uses_auto (range_type));
12267
12268 /* Create the __range variable. */
12269 range_temp = build_decl (input_location, VAR_DECL, for_range__identifier,
12270 range_type);
12271 TREE_USED (range_temp) = 1;
12272 DECL_ARTIFICIAL (range_temp) = 1;
12273
12274 return range_temp;
12275 }
12276
12277 /* Used by cp_parser_range_for in template context: we aren't going to
12278 do a full conversion yet, but we still need to resolve auto in the
12279 type of the for-range-declaration if present. This is basically
12280 a shortcut version of cp_convert_range_for. */
12281
12282 static void
12283 do_range_for_auto_deduction (tree decl, tree range_expr)
12284 {
12285 tree auto_node = type_uses_auto (TREE_TYPE (decl));
12286 if (auto_node)
12287 {
12288 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
12289 range_temp = convert_from_reference (build_range_temp (range_expr));
12290 iter_type = (cp_parser_perform_range_for_lookup
12291 (range_temp, &begin_dummy, &end_dummy));
12292 if (iter_type)
12293 {
12294 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
12295 iter_type);
12296 iter_decl = build_x_indirect_ref (input_location, iter_decl,
12297 RO_UNARY_STAR,
12298 tf_warning_or_error);
12299 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
12300 iter_decl, auto_node);
12301 }
12302 }
12303 }
12304
12305 /* Converts a range-based for-statement into a normal
12306 for-statement, as per the definition.
12307
12308 for (RANGE_DECL : RANGE_EXPR)
12309 BLOCK
12310
12311 should be equivalent to:
12312
12313 {
12314 auto &&__range = RANGE_EXPR;
12315 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12316 __begin != __end;
12317 ++__begin)
12318 {
12319 RANGE_DECL = *__begin;
12320 BLOCK
12321 }
12322 }
12323
12324 If RANGE_EXPR is an array:
12325 BEGIN_EXPR = __range
12326 END_EXPR = __range + ARRAY_SIZE(__range)
12327 Else if RANGE_EXPR has a member 'begin' or 'end':
12328 BEGIN_EXPR = __range.begin()
12329 END_EXPR = __range.end()
12330 Else:
12331 BEGIN_EXPR = begin(__range)
12332 END_EXPR = end(__range);
12333
12334 If __range has a member 'begin' but not 'end', or vice versa, we must
12335 still use the second alternative (it will surely fail, however).
12336 When calling begin()/end() in the third alternative we must use
12337 argument dependent lookup, but always considering 'std' as an associated
12338 namespace. */
12339
12340 tree
12341 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
12342 tree decomp_first_name, unsigned int decomp_cnt,
12343 bool ivdep, unsigned short unroll)
12344 {
12345 tree begin, end;
12346 tree iter_type, begin_expr, end_expr;
12347 tree condition, expression;
12348
12349 range_expr = mark_lvalue_use (range_expr);
12350
12351 if (range_decl == error_mark_node || range_expr == error_mark_node)
12352 /* If an error happened previously do nothing or else a lot of
12353 unhelpful errors would be issued. */
12354 begin_expr = end_expr = iter_type = error_mark_node;
12355 else
12356 {
12357 tree range_temp;
12358
12359 if (VAR_P (range_expr)
12360 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
12361 /* Can't bind a reference to an array of runtime bound. */
12362 range_temp = range_expr;
12363 else
12364 {
12365 range_temp = build_range_temp (range_expr);
12366 pushdecl (range_temp);
12367 cp_finish_decl (range_temp, range_expr,
12368 /*is_constant_init*/false, NULL_TREE,
12369 LOOKUP_ONLYCONVERTING);
12370 range_temp = convert_from_reference (range_temp);
12371 }
12372 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12373 &begin_expr, &end_expr);
12374 }
12375
12376 /* The new for initialization statement. */
12377 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
12378 iter_type);
12379 TREE_USED (begin) = 1;
12380 DECL_ARTIFICIAL (begin) = 1;
12381 pushdecl (begin);
12382 cp_finish_decl (begin, begin_expr,
12383 /*is_constant_init*/false, NULL_TREE,
12384 LOOKUP_ONLYCONVERTING);
12385
12386 if (cxx_dialect >= cxx17)
12387 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12388 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
12389 TREE_USED (end) = 1;
12390 DECL_ARTIFICIAL (end) = 1;
12391 pushdecl (end);
12392 cp_finish_decl (end, end_expr,
12393 /*is_constant_init*/false, NULL_TREE,
12394 LOOKUP_ONLYCONVERTING);
12395
12396 finish_init_stmt (statement);
12397
12398 /* The new for condition. */
12399 condition = build_x_binary_op (input_location, NE_EXPR,
12400 begin, ERROR_MARK,
12401 end, ERROR_MARK,
12402 NULL, tf_warning_or_error);
12403 finish_for_cond (condition, statement, ivdep, unroll);
12404
12405 /* The new increment expression. */
12406 expression = finish_unary_op_expr (input_location,
12407 PREINCREMENT_EXPR, begin,
12408 tf_warning_or_error);
12409 finish_for_expr (expression, statement);
12410
12411 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12412 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
12413
12414 /* The declaration is initialized with *__begin inside the loop body. */
12415 cp_finish_decl (range_decl,
12416 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
12417 tf_warning_or_error),
12418 /*is_constant_init*/false, NULL_TREE,
12419 LOOKUP_ONLYCONVERTING);
12420 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12421 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12422
12423 return statement;
12424 }
12425
12426 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12427 We need to solve both at the same time because the method used
12428 depends on the existence of members begin or end.
12429 Returns the type deduced for the iterator expression. */
12430
12431 static tree
12432 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12433 {
12434 if (error_operand_p (range))
12435 {
12436 *begin = *end = error_mark_node;
12437 return error_mark_node;
12438 }
12439
12440 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12441 {
12442 error ("range-based %<for%> expression of type %qT "
12443 "has incomplete type", TREE_TYPE (range));
12444 *begin = *end = error_mark_node;
12445 return error_mark_node;
12446 }
12447 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12448 {
12449 /* If RANGE is an array, we will use pointer arithmetic. */
12450 *begin = decay_conversion (range, tf_warning_or_error);
12451 *end = build_binary_op (input_location, PLUS_EXPR,
12452 range,
12453 array_type_nelts_top (TREE_TYPE (range)),
12454 false);
12455 return TREE_TYPE (*begin);
12456 }
12457 else
12458 {
12459 /* If it is not an array, we must do a bit of magic. */
12460 tree id_begin, id_end;
12461 tree member_begin, member_end;
12462
12463 *begin = *end = error_mark_node;
12464
12465 id_begin = get_identifier ("begin");
12466 id_end = get_identifier ("end");
12467 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12468 /*protect=*/2, /*want_type=*/false,
12469 tf_warning_or_error);
12470 member_end = lookup_member (TREE_TYPE (range), id_end,
12471 /*protect=*/2, /*want_type=*/false,
12472 tf_warning_or_error);
12473
12474 if (member_begin != NULL_TREE && member_end != NULL_TREE)
12475 {
12476 /* Use the member functions. */
12477 *begin = cp_parser_range_for_member_function (range, id_begin);
12478 *end = cp_parser_range_for_member_function (range, id_end);
12479 }
12480 else
12481 {
12482 /* Use global functions with ADL. */
12483 vec<tree, va_gc> *vec;
12484 vec = make_tree_vector ();
12485
12486 vec_safe_push (vec, range);
12487
12488 member_begin = perform_koenig_lookup (id_begin, vec,
12489 tf_warning_or_error);
12490 *begin = finish_call_expr (member_begin, &vec, false, true,
12491 tf_warning_or_error);
12492 member_end = perform_koenig_lookup (id_end, vec,
12493 tf_warning_or_error);
12494 *end = finish_call_expr (member_end, &vec, false, true,
12495 tf_warning_or_error);
12496
12497 release_tree_vector (vec);
12498 }
12499
12500 /* Last common checks. */
12501 if (*begin == error_mark_node || *end == error_mark_node)
12502 {
12503 /* If one of the expressions is an error do no more checks. */
12504 *begin = *end = error_mark_node;
12505 return error_mark_node;
12506 }
12507 else if (type_dependent_expression_p (*begin)
12508 || type_dependent_expression_p (*end))
12509 /* Can happen, when, eg, in a template context, Koenig lookup
12510 can't resolve begin/end (c++/58503). */
12511 return NULL_TREE;
12512 else
12513 {
12514 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12515 /* The unqualified type of the __begin and __end temporaries should
12516 be the same, as required by the multiple auto declaration. */
12517 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12518 {
12519 if (cxx_dialect >= cxx17
12520 && (build_x_binary_op (input_location, NE_EXPR,
12521 *begin, ERROR_MARK,
12522 *end, ERROR_MARK,
12523 NULL, tf_none)
12524 != error_mark_node))
12525 /* P0184R0 allows __begin and __end to have different types,
12526 but make sure they are comparable so we can give a better
12527 diagnostic. */;
12528 else
12529 error ("inconsistent begin/end types in range-based %<for%> "
12530 "statement: %qT and %qT",
12531 TREE_TYPE (*begin), TREE_TYPE (*end));
12532 }
12533 return iter_type;
12534 }
12535 }
12536 }
12537
12538 /* Helper function for cp_parser_perform_range_for_lookup.
12539 Builds a tree for RANGE.IDENTIFIER(). */
12540
12541 static tree
12542 cp_parser_range_for_member_function (tree range, tree identifier)
12543 {
12544 tree member, res;
12545 vec<tree, va_gc> *vec;
12546
12547 member = finish_class_member_access_expr (range, identifier,
12548 false, tf_warning_or_error);
12549 if (member == error_mark_node)
12550 return error_mark_node;
12551
12552 vec = make_tree_vector ();
12553 res = finish_call_expr (member, &vec,
12554 /*disallow_virtual=*/false,
12555 /*koenig_p=*/false,
12556 tf_warning_or_error);
12557 release_tree_vector (vec);
12558 return res;
12559 }
12560
12561 /* Parse an iteration-statement.
12562
12563 iteration-statement:
12564 while ( condition ) statement
12565 do statement while ( expression ) ;
12566 for ( init-statement condition [opt] ; expression [opt] )
12567 statement
12568
12569 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12570
12571 static tree
12572 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
12573 unsigned short unroll)
12574 {
12575 cp_token *token;
12576 enum rid keyword;
12577 tree statement;
12578 unsigned char in_statement;
12579 token_indent_info guard_tinfo;
12580
12581 /* Peek at the next token. */
12582 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12583 if (!token)
12584 return error_mark_node;
12585
12586 guard_tinfo = get_token_indent_info (token);
12587
12588 /* Remember whether or not we are already within an iteration
12589 statement. */
12590 in_statement = parser->in_statement;
12591
12592 /* See what kind of keyword it is. */
12593 keyword = token->keyword;
12594 switch (keyword)
12595 {
12596 case RID_WHILE:
12597 {
12598 tree condition;
12599
12600 /* Begin the while-statement. */
12601 statement = begin_while_stmt ();
12602 /* Look for the `('. */
12603 matching_parens parens;
12604 parens.require_open (parser);
12605 /* Parse the condition. */
12606 condition = cp_parser_condition (parser);
12607 finish_while_stmt_cond (condition, statement, ivdep, unroll);
12608 /* Look for the `)'. */
12609 parens.require_close (parser);
12610 /* Parse the dependent statement. */
12611 parser->in_statement = IN_ITERATION_STMT;
12612 bool prev = note_iteration_stmt_body_start ();
12613 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12614 note_iteration_stmt_body_end (prev);
12615 parser->in_statement = in_statement;
12616 /* We're done with the while-statement. */
12617 finish_while_stmt (statement);
12618 }
12619 break;
12620
12621 case RID_DO:
12622 {
12623 tree expression;
12624
12625 /* Begin the do-statement. */
12626 statement = begin_do_stmt ();
12627 /* Parse the body of the do-statement. */
12628 parser->in_statement = IN_ITERATION_STMT;
12629 bool prev = note_iteration_stmt_body_start ();
12630 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12631 note_iteration_stmt_body_end (prev);
12632 parser->in_statement = in_statement;
12633 finish_do_body (statement);
12634 /* Look for the `while' keyword. */
12635 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12636 /* Look for the `('. */
12637 matching_parens parens;
12638 parens.require_open (parser);
12639 /* Parse the expression. */
12640 expression = cp_parser_expression (parser);
12641 /* We're done with the do-statement. */
12642 finish_do_stmt (expression, statement, ivdep, unroll);
12643 /* Look for the `)'. */
12644 parens.require_close (parser);
12645 /* Look for the `;'. */
12646 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12647 }
12648 break;
12649
12650 case RID_FOR:
12651 {
12652 /* Look for the `('. */
12653 matching_parens parens;
12654 parens.require_open (parser);
12655
12656 statement = cp_parser_for (parser, ivdep, unroll);
12657
12658 /* Look for the `)'. */
12659 parens.require_close (parser);
12660
12661 /* Parse the body of the for-statement. */
12662 parser->in_statement = IN_ITERATION_STMT;
12663 bool prev = note_iteration_stmt_body_start ();
12664 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12665 note_iteration_stmt_body_end (prev);
12666 parser->in_statement = in_statement;
12667
12668 /* We're done with the for-statement. */
12669 finish_for_stmt (statement);
12670 }
12671 break;
12672
12673 default:
12674 cp_parser_error (parser, "expected iteration-statement");
12675 statement = error_mark_node;
12676 break;
12677 }
12678
12679 return statement;
12680 }
12681
12682 /* Parse a init-statement or the declarator of a range-based-for.
12683 Returns true if a range-based-for declaration is seen.
12684
12685 init-statement:
12686 expression-statement
12687 simple-declaration */
12688
12689 static bool
12690 cp_parser_init_statement (cp_parser *parser, tree *decl)
12691 {
12692 /* If the next token is a `;', then we have an empty
12693 expression-statement. Grammatically, this is also a
12694 simple-declaration, but an invalid one, because it does not
12695 declare anything. Therefore, if we did not handle this case
12696 specially, we would issue an error message about an invalid
12697 declaration. */
12698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12699 {
12700 bool is_range_for = false;
12701 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12702
12703 /* Try to parse the init-statement. */
12704 if (cp_parser_range_based_for_with_init_p (parser))
12705 {
12706 tree dummy;
12707 cp_parser_parse_tentatively (parser);
12708 /* Parse the declaration. */
12709 cp_parser_simple_declaration (parser,
12710 /*function_definition_allowed_p=*/false,
12711 &dummy);
12712 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12713 if (!cp_parser_parse_definitely (parser))
12714 /* That didn't work, try to parse it as an expression-statement. */
12715 cp_parser_expression_statement (parser, NULL_TREE);
12716
12717 if (cxx_dialect < cxx2a)
12718 {
12719 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12720 "range-based %<for%> loops with initializer only "
12721 "available with -std=c++2a or -std=gnu++2a");
12722 *decl = error_mark_node;
12723 }
12724 }
12725
12726 /* A colon is used in range-based for. */
12727 parser->colon_corrects_to_scope_p = false;
12728
12729 /* We're going to speculatively look for a declaration, falling back
12730 to an expression, if necessary. */
12731 cp_parser_parse_tentatively (parser);
12732 /* Parse the declaration. */
12733 cp_parser_simple_declaration (parser,
12734 /*function_definition_allowed_p=*/false,
12735 decl);
12736 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12737 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12738 {
12739 /* It is a range-for, consume the ':'. */
12740 cp_lexer_consume_token (parser->lexer);
12741 is_range_for = true;
12742 if (cxx_dialect < cxx11)
12743 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12744 "range-based %<for%> loops only available with "
12745 "-std=c++11 or -std=gnu++11");
12746 }
12747 else
12748 /* The ';' is not consumed yet because we told
12749 cp_parser_simple_declaration not to. */
12750 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12751
12752 if (cp_parser_parse_definitely (parser))
12753 return is_range_for;
12754 /* If the tentative parse failed, then we shall need to look for an
12755 expression-statement. */
12756 }
12757 /* If we are here, it is an expression-statement. */
12758 cp_parser_expression_statement (parser, NULL_TREE);
12759 return false;
12760 }
12761
12762 /* Parse a jump-statement.
12763
12764 jump-statement:
12765 break ;
12766 continue ;
12767 return expression [opt] ;
12768 return braced-init-list ;
12769 goto identifier ;
12770
12771 GNU extension:
12772
12773 jump-statement:
12774 goto * expression ;
12775
12776 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12777
12778 static tree
12779 cp_parser_jump_statement (cp_parser* parser)
12780 {
12781 tree statement = error_mark_node;
12782 cp_token *token;
12783 enum rid keyword;
12784 unsigned char in_statement;
12785
12786 /* Peek at the next token. */
12787 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12788 if (!token)
12789 return error_mark_node;
12790
12791 /* See what kind of keyword it is. */
12792 keyword = token->keyword;
12793 switch (keyword)
12794 {
12795 case RID_BREAK:
12796 in_statement = parser->in_statement & ~IN_IF_STMT;
12797 switch (in_statement)
12798 {
12799 case 0:
12800 error_at (token->location, "break statement not within loop or switch");
12801 break;
12802 default:
12803 gcc_assert ((in_statement & IN_SWITCH_STMT)
12804 || in_statement == IN_ITERATION_STMT);
12805 statement = finish_break_stmt ();
12806 if (in_statement == IN_ITERATION_STMT)
12807 break_maybe_infinite_loop ();
12808 break;
12809 case IN_OMP_BLOCK:
12810 error_at (token->location, "invalid exit from OpenMP structured block");
12811 break;
12812 case IN_OMP_FOR:
12813 error_at (token->location, "break statement used with OpenMP for loop");
12814 break;
12815 }
12816 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12817 break;
12818
12819 case RID_CONTINUE:
12820 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12821 {
12822 case 0:
12823 error_at (token->location, "continue statement not within a loop");
12824 break;
12825 /* Fall through. */
12826 case IN_ITERATION_STMT:
12827 case IN_OMP_FOR:
12828 statement = finish_continue_stmt ();
12829 break;
12830 case IN_OMP_BLOCK:
12831 error_at (token->location, "invalid exit from OpenMP structured block");
12832 break;
12833 default:
12834 gcc_unreachable ();
12835 }
12836 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12837 break;
12838
12839 case RID_RETURN:
12840 {
12841 tree expr;
12842 bool expr_non_constant_p;
12843
12844 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12845 {
12846 cp_lexer_set_source_position (parser->lexer);
12847 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12848 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12849 }
12850 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12851 expr = cp_parser_expression (parser);
12852 else
12853 /* If the next token is a `;', then there is no
12854 expression. */
12855 expr = NULL_TREE;
12856 /* Build the return-statement. */
12857 if (current_function_auto_return_pattern && in_discarded_stmt)
12858 /* Don't deduce from a discarded return statement. */;
12859 else
12860 statement = finish_return_stmt (expr);
12861 /* Look for the final `;'. */
12862 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12863 }
12864 break;
12865
12866 case RID_GOTO:
12867 if (parser->in_function_body
12868 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12869 {
12870 error ("%<goto%> in %<constexpr%> function");
12871 cp_function_chain->invalid_constexpr = true;
12872 }
12873
12874 /* Create the goto-statement. */
12875 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12876 {
12877 /* Issue a warning about this use of a GNU extension. */
12878 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12879 /* Consume the '*' token. */
12880 cp_lexer_consume_token (parser->lexer);
12881 /* Parse the dependent expression. */
12882 finish_goto_stmt (cp_parser_expression (parser));
12883 }
12884 else
12885 finish_goto_stmt (cp_parser_identifier (parser));
12886 /* Look for the final `;'. */
12887 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12888 break;
12889
12890 default:
12891 cp_parser_error (parser, "expected jump-statement");
12892 break;
12893 }
12894
12895 return statement;
12896 }
12897
12898 /* Parse a declaration-statement.
12899
12900 declaration-statement:
12901 block-declaration */
12902
12903 static void
12904 cp_parser_declaration_statement (cp_parser* parser)
12905 {
12906 void *p;
12907
12908 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12909 p = obstack_alloc (&declarator_obstack, 0);
12910
12911 /* Parse the block-declaration. */
12912 cp_parser_block_declaration (parser, /*statement_p=*/true);
12913
12914 /* Free any declarators allocated. */
12915 obstack_free (&declarator_obstack, p);
12916 }
12917
12918 /* Some dependent statements (like `if (cond) statement'), are
12919 implicitly in their own scope. In other words, if the statement is
12920 a single statement (as opposed to a compound-statement), it is
12921 none-the-less treated as if it were enclosed in braces. Any
12922 declarations appearing in the dependent statement are out of scope
12923 after control passes that point. This function parses a statement,
12924 but ensures that is in its own scope, even if it is not a
12925 compound-statement.
12926
12927 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12928 is a (possibly labeled) if statement which is not enclosed in
12929 braces and has an else clause. This is used to implement
12930 -Wparentheses.
12931
12932 CHAIN is a vector of if-else-if conditions. This is used to implement
12933 -Wduplicated-cond.
12934
12935 Returns the new statement. */
12936
12937 static tree
12938 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12939 const token_indent_info &guard_tinfo,
12940 vec<tree> *chain)
12941 {
12942 tree statement;
12943 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12944 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12945 token_indent_info body_tinfo
12946 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12947
12948 if (if_p != NULL)
12949 *if_p = false;
12950
12951 /* Mark if () ; with a special NOP_EXPR. */
12952 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12953 {
12954 cp_lexer_consume_token (parser->lexer);
12955 statement = add_stmt (build_empty_stmt (body_loc));
12956
12957 if (guard_tinfo.keyword == RID_IF
12958 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12959 warning_at (body_loc, OPT_Wempty_body,
12960 "suggest braces around empty body in an %<if%> statement");
12961 else if (guard_tinfo.keyword == RID_ELSE)
12962 warning_at (body_loc, OPT_Wempty_body,
12963 "suggest braces around empty body in an %<else%> statement");
12964 }
12965 /* if a compound is opened, we simply parse the statement directly. */
12966 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12967 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12968 /* If the token is not a `{', then we must take special action. */
12969 else
12970 {
12971 /* Create a compound-statement. */
12972 statement = begin_compound_stmt (0);
12973 /* Parse the dependent-statement. */
12974 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12975 &body_loc_after_labels);
12976 /* Finish the dummy compound-statement. */
12977 finish_compound_stmt (statement);
12978 }
12979
12980 token_indent_info next_tinfo
12981 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12982 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12983
12984 if (body_loc_after_labels != UNKNOWN_LOCATION
12985 && next_tinfo.type != CPP_SEMICOLON)
12986 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12987 guard_tinfo.location, guard_tinfo.keyword);
12988
12989 /* Return the statement. */
12990 return statement;
12991 }
12992
12993 /* For some dependent statements (like `while (cond) statement'), we
12994 have already created a scope. Therefore, even if the dependent
12995 statement is a compound-statement, we do not want to create another
12996 scope. */
12997
12998 static void
12999 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
13000 const token_indent_info &guard_tinfo)
13001 {
13002 /* If the token is a `{', then we must take special action. */
13003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13004 {
13005 token_indent_info body_tinfo
13006 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13007 location_t loc_after_labels = UNKNOWN_LOCATION;
13008
13009 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
13010 &loc_after_labels);
13011 token_indent_info next_tinfo
13012 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13013 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
13014
13015 if (loc_after_labels != UNKNOWN_LOCATION
13016 && next_tinfo.type != CPP_SEMICOLON)
13017 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
13018 guard_tinfo.location,
13019 guard_tinfo.keyword);
13020 }
13021 else
13022 {
13023 /* Avoid calling cp_parser_compound_statement, so that we
13024 don't create a new scope. Do everything else by hand. */
13025 matching_braces braces;
13026 braces.require_open (parser);
13027 /* If the next keyword is `__label__' we have a label declaration. */
13028 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
13029 cp_parser_label_declaration (parser);
13030 /* Parse an (optional) statement-seq. */
13031 cp_parser_statement_seq_opt (parser, NULL_TREE);
13032 braces.require_close (parser);
13033 }
13034 }
13035
13036 /* Declarations [gram.dcl.dcl] */
13037
13038 /* Parse an optional declaration-sequence.
13039
13040 declaration-seq:
13041 declaration
13042 declaration-seq declaration */
13043
13044 static void
13045 cp_parser_declaration_seq_opt (cp_parser* parser)
13046 {
13047 while (true)
13048 {
13049 cp_token *token = cp_lexer_peek_token (parser->lexer);
13050
13051 if (token->type == CPP_CLOSE_BRACE
13052 || token->type == CPP_EOF)
13053 break;
13054 else
13055 cp_parser_toplevel_declaration (parser);
13056 }
13057 }
13058
13059 /* Parse a declaration.
13060
13061 declaration:
13062 block-declaration
13063 function-definition
13064 template-declaration
13065 explicit-instantiation
13066 explicit-specialization
13067 linkage-specification
13068 namespace-definition
13069
13070 C++17:
13071 deduction-guide
13072
13073 GNU extension:
13074
13075 declaration:
13076 __extension__ declaration */
13077
13078 static void
13079 cp_parser_declaration (cp_parser* parser)
13080 {
13081 cp_token token1;
13082 cp_token token2;
13083 int saved_pedantic;
13084 void *p;
13085 tree attributes = NULL_TREE;
13086
13087 /* Check for the `__extension__' keyword. */
13088 if (cp_parser_extension_opt (parser, &saved_pedantic))
13089 {
13090 /* Parse the qualified declaration. */
13091 cp_parser_declaration (parser);
13092 /* Restore the PEDANTIC flag. */
13093 pedantic = saved_pedantic;
13094
13095 return;
13096 }
13097
13098 /* Try to figure out what kind of declaration is present. */
13099 token1 = *cp_lexer_peek_token (parser->lexer);
13100
13101 if (token1.type != CPP_EOF)
13102 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
13103 else
13104 {
13105 token2.type = CPP_EOF;
13106 token2.keyword = RID_MAX;
13107 }
13108
13109 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13110 p = obstack_alloc (&declarator_obstack, 0);
13111
13112 /* If the next token is `extern' and the following token is a string
13113 literal, then we have a linkage specification. */
13114 if (token1.keyword == RID_EXTERN
13115 && cp_parser_is_pure_string_literal (&token2))
13116 cp_parser_linkage_specification (parser);
13117 /* If the next token is `template', then we have either a template
13118 declaration, an explicit instantiation, or an explicit
13119 specialization. */
13120 else if (token1.keyword == RID_TEMPLATE)
13121 {
13122 /* `template <>' indicates a template specialization. */
13123 if (token2.type == CPP_LESS
13124 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
13125 cp_parser_explicit_specialization (parser);
13126 /* `template <' indicates a template declaration. */
13127 else if (token2.type == CPP_LESS)
13128 cp_parser_template_declaration (parser, /*member_p=*/false);
13129 /* Anything else must be an explicit instantiation. */
13130 else
13131 cp_parser_explicit_instantiation (parser);
13132 }
13133 /* If the next token is `export', then we have a template
13134 declaration. */
13135 else if (token1.keyword == RID_EXPORT)
13136 cp_parser_template_declaration (parser, /*member_p=*/false);
13137 /* If the next token is `extern', 'static' or 'inline' and the one
13138 after that is `template', we have a GNU extended explicit
13139 instantiation directive. */
13140 else if (cp_parser_allow_gnu_extensions_p (parser)
13141 && (token1.keyword == RID_EXTERN
13142 || token1.keyword == RID_STATIC
13143 || token1.keyword == RID_INLINE)
13144 && token2.keyword == RID_TEMPLATE)
13145 cp_parser_explicit_instantiation (parser);
13146 /* If the next token is `namespace', check for a named or unnamed
13147 namespace definition. */
13148 else if (token1.keyword == RID_NAMESPACE
13149 && (/* A named namespace definition. */
13150 (token2.type == CPP_NAME
13151 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
13152 != CPP_EQ))
13153 || (token2.type == CPP_OPEN_SQUARE
13154 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
13155 == CPP_OPEN_SQUARE)
13156 /* An unnamed namespace definition. */
13157 || token2.type == CPP_OPEN_BRACE
13158 || token2.keyword == RID_ATTRIBUTE))
13159 cp_parser_namespace_definition (parser);
13160 /* An inline (associated) namespace definition. */
13161 else if (token1.keyword == RID_INLINE
13162 && token2.keyword == RID_NAMESPACE)
13163 cp_parser_namespace_definition (parser);
13164 /* Objective-C++ declaration/definition. */
13165 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
13166 cp_parser_objc_declaration (parser, NULL_TREE);
13167 else if (c_dialect_objc ()
13168 && token1.keyword == RID_ATTRIBUTE
13169 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
13170 cp_parser_objc_declaration (parser, attributes);
13171 /* At this point we may have a template declared by a concept
13172 introduction. */
13173 else if (flag_concepts
13174 && cp_parser_template_declaration_after_export (parser,
13175 /*member_p=*/false))
13176 /* We did. */;
13177 else
13178 /* Try to parse a block-declaration, or a function-definition. */
13179 cp_parser_block_declaration (parser, /*statement_p=*/false);
13180
13181 /* Free any declarators allocated. */
13182 obstack_free (&declarator_obstack, p);
13183 }
13184
13185 /* Parse a namespace-scope declaration. */
13186
13187 static void
13188 cp_parser_toplevel_declaration (cp_parser* parser)
13189 {
13190 cp_token *token = cp_lexer_peek_token (parser->lexer);
13191
13192 if (token->type == CPP_PRAGMA)
13193 /* A top-level declaration can consist solely of a #pragma. A
13194 nested declaration cannot, so this is done here and not in
13195 cp_parser_declaration. (A #pragma at block scope is
13196 handled in cp_parser_statement.) */
13197 cp_parser_pragma (parser, pragma_external, NULL);
13198 else if (token->type == CPP_SEMICOLON)
13199 {
13200 /* A declaration consisting of a single semicolon is
13201 invalid. Allow it unless we're being pedantic. */
13202 cp_lexer_consume_token (parser->lexer);
13203 if (!in_system_header_at (input_location))
13204 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
13205 }
13206 else
13207 /* Parse the declaration itself. */
13208 cp_parser_declaration (parser);
13209 }
13210
13211 /* Parse a block-declaration.
13212
13213 block-declaration:
13214 simple-declaration
13215 asm-definition
13216 namespace-alias-definition
13217 using-declaration
13218 using-directive
13219
13220 GNU Extension:
13221
13222 block-declaration:
13223 __extension__ block-declaration
13224
13225 C++0x Extension:
13226
13227 block-declaration:
13228 static_assert-declaration
13229
13230 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13231 part of a declaration-statement. */
13232
13233 static void
13234 cp_parser_block_declaration (cp_parser *parser,
13235 bool statement_p)
13236 {
13237 cp_token *token1;
13238 int saved_pedantic;
13239
13240 /* Check for the `__extension__' keyword. */
13241 if (cp_parser_extension_opt (parser, &saved_pedantic))
13242 {
13243 /* Parse the qualified declaration. */
13244 cp_parser_block_declaration (parser, statement_p);
13245 /* Restore the PEDANTIC flag. */
13246 pedantic = saved_pedantic;
13247
13248 return;
13249 }
13250
13251 /* Peek at the next token to figure out which kind of declaration is
13252 present. */
13253 token1 = cp_lexer_peek_token (parser->lexer);
13254
13255 /* If the next keyword is `asm', we have an asm-definition. */
13256 if (token1->keyword == RID_ASM)
13257 {
13258 if (statement_p)
13259 cp_parser_commit_to_tentative_parse (parser);
13260 cp_parser_asm_definition (parser);
13261 }
13262 /* If the next keyword is `namespace', we have a
13263 namespace-alias-definition. */
13264 else if (token1->keyword == RID_NAMESPACE)
13265 cp_parser_namespace_alias_definition (parser);
13266 /* If the next keyword is `using', we have a
13267 using-declaration, a using-directive, or an alias-declaration. */
13268 else if (token1->keyword == RID_USING)
13269 {
13270 cp_token *token2;
13271
13272 if (statement_p)
13273 cp_parser_commit_to_tentative_parse (parser);
13274 /* If the token after `using' is `namespace', then we have a
13275 using-directive. */
13276 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13277 if (token2->keyword == RID_NAMESPACE)
13278 cp_parser_using_directive (parser);
13279 /* If the second token after 'using' is '=', then we have an
13280 alias-declaration. */
13281 else if (cxx_dialect >= cxx11
13282 && token2->type == CPP_NAME
13283 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
13284 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
13285 cp_parser_alias_declaration (parser);
13286 /* Otherwise, it's a using-declaration. */
13287 else
13288 cp_parser_using_declaration (parser,
13289 /*access_declaration_p=*/false);
13290 }
13291 /* If the next keyword is `__label__' we have a misplaced label
13292 declaration. */
13293 else if (token1->keyword == RID_LABEL)
13294 {
13295 cp_lexer_consume_token (parser->lexer);
13296 error_at (token1->location, "%<__label__%> not at the beginning of a block");
13297 cp_parser_skip_to_end_of_statement (parser);
13298 /* If the next token is now a `;', consume it. */
13299 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13300 cp_lexer_consume_token (parser->lexer);
13301 }
13302 /* If the next token is `static_assert' we have a static assertion. */
13303 else if (token1->keyword == RID_STATIC_ASSERT)
13304 cp_parser_static_assert (parser, /*member_p=*/false);
13305 /* Anything else must be a simple-declaration. */
13306 else
13307 cp_parser_simple_declaration (parser, !statement_p,
13308 /*maybe_range_for_decl*/NULL);
13309 }
13310
13311 /* Parse a simple-declaration.
13312
13313 simple-declaration:
13314 decl-specifier-seq [opt] init-declarator-list [opt] ;
13315 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13316 brace-or-equal-initializer ;
13317
13318 init-declarator-list:
13319 init-declarator
13320 init-declarator-list , init-declarator
13321
13322 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13323 function-definition as a simple-declaration.
13324
13325 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13326 parsed declaration if it is an uninitialized single declarator not followed
13327 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13328 if present, will not be consumed. */
13329
13330 static void
13331 cp_parser_simple_declaration (cp_parser* parser,
13332 bool function_definition_allowed_p,
13333 tree *maybe_range_for_decl)
13334 {
13335 cp_decl_specifier_seq decl_specifiers;
13336 int declares_class_or_enum;
13337 bool saw_declarator;
13338 location_t comma_loc = UNKNOWN_LOCATION;
13339 location_t init_loc = UNKNOWN_LOCATION;
13340
13341 if (maybe_range_for_decl)
13342 *maybe_range_for_decl = NULL_TREE;
13343
13344 /* Defer access checks until we know what is being declared; the
13345 checks for names appearing in the decl-specifier-seq should be
13346 done as if we were in the scope of the thing being declared. */
13347 push_deferring_access_checks (dk_deferred);
13348
13349 /* Parse the decl-specifier-seq. We have to keep track of whether
13350 or not the decl-specifier-seq declares a named class or
13351 enumeration type, since that is the only case in which the
13352 init-declarator-list is allowed to be empty.
13353
13354 [dcl.dcl]
13355
13356 In a simple-declaration, the optional init-declarator-list can be
13357 omitted only when declaring a class or enumeration, that is when
13358 the decl-specifier-seq contains either a class-specifier, an
13359 elaborated-type-specifier, or an enum-specifier. */
13360 cp_parser_decl_specifier_seq (parser,
13361 CP_PARSER_FLAGS_OPTIONAL,
13362 &decl_specifiers,
13363 &declares_class_or_enum);
13364 /* We no longer need to defer access checks. */
13365 stop_deferring_access_checks ();
13366
13367 /* In a block scope, a valid declaration must always have a
13368 decl-specifier-seq. By not trying to parse declarators, we can
13369 resolve the declaration/expression ambiguity more quickly. */
13370 if (!function_definition_allowed_p
13371 && !decl_specifiers.any_specifiers_p)
13372 {
13373 cp_parser_error (parser, "expected declaration");
13374 goto done;
13375 }
13376
13377 /* If the next two tokens are both identifiers, the code is
13378 erroneous. The usual cause of this situation is code like:
13379
13380 T t;
13381
13382 where "T" should name a type -- but does not. */
13383 if (!decl_specifiers.any_type_specifiers_p
13384 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13385 {
13386 /* If parsing tentatively, we should commit; we really are
13387 looking at a declaration. */
13388 cp_parser_commit_to_tentative_parse (parser);
13389 /* Give up. */
13390 goto done;
13391 }
13392
13393 cp_parser_maybe_commit_to_declaration (parser,
13394 decl_specifiers.any_specifiers_p);
13395
13396 /* Look for C++17 decomposition declaration. */
13397 for (size_t n = 1; ; n++)
13398 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13399 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13400 continue;
13401 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13402 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13403 && decl_specifiers.any_specifiers_p)
13404 {
13405 tree decl
13406 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13407 maybe_range_for_decl,
13408 &init_loc);
13409
13410 /* The next token should be either a `,' or a `;'. */
13411 cp_token *token = cp_lexer_peek_token (parser->lexer);
13412 /* If it's a `;', we are done. */
13413 if (token->type == CPP_SEMICOLON)
13414 goto finish;
13415 else if (maybe_range_for_decl)
13416 {
13417 if (*maybe_range_for_decl == NULL_TREE)
13418 *maybe_range_for_decl = error_mark_node;
13419 goto finish;
13420 }
13421 /* Anything else is an error. */
13422 else
13423 {
13424 /* If we have already issued an error message we don't need
13425 to issue another one. */
13426 if ((decl != error_mark_node
13427 && DECL_INITIAL (decl) != error_mark_node)
13428 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13429 cp_parser_error (parser, "expected %<,%> or %<;%>");
13430 /* Skip tokens until we reach the end of the statement. */
13431 cp_parser_skip_to_end_of_statement (parser);
13432 /* If the next token is now a `;', consume it. */
13433 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13434 cp_lexer_consume_token (parser->lexer);
13435 goto done;
13436 }
13437 }
13438 else
13439 break;
13440
13441 tree last_type;
13442 bool auto_specifier_p;
13443 /* NULL_TREE if both variable and function declaration are allowed,
13444 error_mark_node if function declaration are not allowed and
13445 a FUNCTION_DECL that should be diagnosed if it is followed by
13446 variable declarations. */
13447 tree auto_function_declaration;
13448
13449 last_type = NULL_TREE;
13450 auto_specifier_p
13451 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13452 auto_function_declaration = NULL_TREE;
13453
13454 /* Keep going until we hit the `;' at the end of the simple
13455 declaration. */
13456 saw_declarator = false;
13457 while (cp_lexer_next_token_is_not (parser->lexer,
13458 CPP_SEMICOLON))
13459 {
13460 cp_token *token;
13461 bool function_definition_p;
13462 tree decl;
13463 tree auto_result = NULL_TREE;
13464
13465 if (saw_declarator)
13466 {
13467 /* If we are processing next declarator, comma is expected */
13468 token = cp_lexer_peek_token (parser->lexer);
13469 gcc_assert (token->type == CPP_COMMA);
13470 cp_lexer_consume_token (parser->lexer);
13471 if (maybe_range_for_decl)
13472 {
13473 *maybe_range_for_decl = error_mark_node;
13474 if (comma_loc == UNKNOWN_LOCATION)
13475 comma_loc = token->location;
13476 }
13477 }
13478 else
13479 saw_declarator = true;
13480
13481 /* Parse the init-declarator. */
13482 decl = cp_parser_init_declarator (parser,
13483 CP_PARSER_FLAGS_NONE,
13484 &decl_specifiers,
13485 /*checks=*/NULL,
13486 function_definition_allowed_p,
13487 /*member_p=*/false,
13488 declares_class_or_enum,
13489 &function_definition_p,
13490 maybe_range_for_decl,
13491 &init_loc,
13492 &auto_result);
13493 /* If an error occurred while parsing tentatively, exit quickly.
13494 (That usually happens when in the body of a function; each
13495 statement is treated as a declaration-statement until proven
13496 otherwise.) */
13497 if (cp_parser_error_occurred (parser))
13498 goto done;
13499
13500 if (auto_specifier_p && cxx_dialect >= cxx14)
13501 {
13502 /* If the init-declarator-list contains more than one
13503 init-declarator, they shall all form declarations of
13504 variables. */
13505 if (auto_function_declaration == NULL_TREE)
13506 auto_function_declaration
13507 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13508 else if (TREE_CODE (decl) == FUNCTION_DECL
13509 || auto_function_declaration != error_mark_node)
13510 {
13511 error_at (decl_specifiers.locations[ds_type_spec],
13512 "non-variable %qD in declaration with more than one "
13513 "declarator with placeholder type",
13514 TREE_CODE (decl) == FUNCTION_DECL
13515 ? decl : auto_function_declaration);
13516 auto_function_declaration = error_mark_node;
13517 }
13518 }
13519
13520 if (auto_result
13521 && (!processing_template_decl || !type_uses_auto (auto_result)))
13522 {
13523 if (last_type
13524 && last_type != error_mark_node
13525 && !same_type_p (auto_result, last_type))
13526 {
13527 /* If the list of declarators contains more than one declarator,
13528 the type of each declared variable is determined as described
13529 above. If the type deduced for the template parameter U is not
13530 the same in each deduction, the program is ill-formed. */
13531 error_at (decl_specifiers.locations[ds_type_spec],
13532 "inconsistent deduction for %qT: %qT and then %qT",
13533 decl_specifiers.type, last_type, auto_result);
13534 last_type = error_mark_node;
13535 }
13536 else
13537 last_type = auto_result;
13538 }
13539
13540 /* Handle function definitions specially. */
13541 if (function_definition_p)
13542 {
13543 /* If the next token is a `,', then we are probably
13544 processing something like:
13545
13546 void f() {}, *p;
13547
13548 which is erroneous. */
13549 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13550 {
13551 cp_token *token = cp_lexer_peek_token (parser->lexer);
13552 error_at (token->location,
13553 "mixing"
13554 " declarations and function-definitions is forbidden");
13555 }
13556 /* Otherwise, we're done with the list of declarators. */
13557 else
13558 {
13559 pop_deferring_access_checks ();
13560 return;
13561 }
13562 }
13563 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13564 *maybe_range_for_decl = decl;
13565 /* The next token should be either a `,' or a `;'. */
13566 token = cp_lexer_peek_token (parser->lexer);
13567 /* If it's a `,', there are more declarators to come. */
13568 if (token->type == CPP_COMMA)
13569 /* will be consumed next time around */;
13570 /* If it's a `;', we are done. */
13571 else if (token->type == CPP_SEMICOLON)
13572 break;
13573 else if (maybe_range_for_decl)
13574 {
13575 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13576 permerror (decl_specifiers.locations[ds_type_spec],
13577 "types may not be defined in a for-range-declaration");
13578 break;
13579 }
13580 /* Anything else is an error. */
13581 else
13582 {
13583 /* If we have already issued an error message we don't need
13584 to issue another one. */
13585 if ((decl != error_mark_node
13586 && DECL_INITIAL (decl) != error_mark_node)
13587 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13588 cp_parser_error (parser, "expected %<,%> or %<;%>");
13589 /* Skip tokens until we reach the end of the statement. */
13590 cp_parser_skip_to_end_of_statement (parser);
13591 /* If the next token is now a `;', consume it. */
13592 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13593 cp_lexer_consume_token (parser->lexer);
13594 goto done;
13595 }
13596 /* After the first time around, a function-definition is not
13597 allowed -- even if it was OK at first. For example:
13598
13599 int i, f() {}
13600
13601 is not valid. */
13602 function_definition_allowed_p = false;
13603 }
13604
13605 /* Issue an error message if no declarators are present, and the
13606 decl-specifier-seq does not itself declare a class or
13607 enumeration: [dcl.dcl]/3. */
13608 if (!saw_declarator)
13609 {
13610 if (cp_parser_declares_only_class_p (parser))
13611 {
13612 if (!declares_class_or_enum
13613 && decl_specifiers.type
13614 && OVERLOAD_TYPE_P (decl_specifiers.type))
13615 /* Ensure an error is issued anyway when finish_decltype_type,
13616 called via cp_parser_decl_specifier_seq, returns a class or
13617 an enumeration (c++/51786). */
13618 decl_specifiers.type = NULL_TREE;
13619 shadow_tag (&decl_specifiers);
13620 }
13621 /* Perform any deferred access checks. */
13622 perform_deferred_access_checks (tf_warning_or_error);
13623 }
13624
13625 /* Consume the `;'. */
13626 finish:
13627 if (!maybe_range_for_decl)
13628 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13629 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13630 {
13631 if (init_loc != UNKNOWN_LOCATION)
13632 error_at (init_loc, "initializer in range-based %<for%> loop");
13633 if (comma_loc != UNKNOWN_LOCATION)
13634 error_at (comma_loc,
13635 "multiple declarations in range-based %<for%> loop");
13636 }
13637
13638 done:
13639 pop_deferring_access_checks ();
13640 }
13641
13642 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13643 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13644 initializer ; */
13645
13646 static tree
13647 cp_parser_decomposition_declaration (cp_parser *parser,
13648 cp_decl_specifier_seq *decl_specifiers,
13649 tree *maybe_range_for_decl,
13650 location_t *init_loc)
13651 {
13652 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13653 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13654 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13655
13656 /* Parse the identifier-list. */
13657 auto_vec<cp_expr, 10> v;
13658 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13659 while (true)
13660 {
13661 cp_expr e = cp_parser_identifier (parser);
13662 if (e.get_value () == error_mark_node)
13663 break;
13664 v.safe_push (e);
13665 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13666 break;
13667 cp_lexer_consume_token (parser->lexer);
13668 }
13669
13670 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13671 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13672 {
13673 end_loc = UNKNOWN_LOCATION;
13674 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13675 false);
13676 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13677 cp_lexer_consume_token (parser->lexer);
13678 else
13679 {
13680 cp_parser_skip_to_end_of_statement (parser);
13681 return error_mark_node;
13682 }
13683 }
13684
13685 if (cxx_dialect < cxx17)
13686 pedwarn (loc, 0, "structured bindings only available with "
13687 "-std=c++17 or -std=gnu++17");
13688
13689 tree pushed_scope;
13690 cp_declarator *declarator = make_declarator (cdk_decomp);
13691 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13692 declarator->id_loc = loc;
13693 if (ref_qual != REF_QUAL_NONE)
13694 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13695 ref_qual == REF_QUAL_RVALUE,
13696 NULL_TREE);
13697 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13698 NULL_TREE, decl_specifiers->attributes,
13699 &pushed_scope);
13700 tree orig_decl = decl;
13701
13702 unsigned int i;
13703 cp_expr e;
13704 cp_decl_specifier_seq decl_specs;
13705 clear_decl_specs (&decl_specs);
13706 decl_specs.type = make_auto ();
13707 tree prev = decl;
13708 FOR_EACH_VEC_ELT (v, i, e)
13709 {
13710 if (i == 0)
13711 declarator = make_id_declarator (NULL_TREE, e.get_value (),
13712 sfk_none, e.get_location ());
13713 else
13714 {
13715 declarator->u.id.unqualified_name = e.get_value ();
13716 declarator->id_loc = e.get_location ();
13717 }
13718 tree elt_pushed_scope;
13719 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13720 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13721 if (decl2 == error_mark_node)
13722 decl = error_mark_node;
13723 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13724 {
13725 /* Ensure we've diagnosed redeclaration if we aren't creating
13726 a new VAR_DECL. */
13727 gcc_assert (errorcount);
13728 decl = error_mark_node;
13729 }
13730 else
13731 prev = decl2;
13732 if (elt_pushed_scope)
13733 pop_scope (elt_pushed_scope);
13734 }
13735
13736 if (v.is_empty ())
13737 {
13738 error_at (loc, "empty structured binding declaration");
13739 decl = error_mark_node;
13740 }
13741
13742 if (maybe_range_for_decl == NULL
13743 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13744 {
13745 bool non_constant_p = false, is_direct_init = false;
13746 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13747 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13748 &non_constant_p);
13749 if (initializer == NULL_TREE
13750 || (TREE_CODE (initializer) == TREE_LIST
13751 && TREE_CHAIN (initializer))
13752 || (is_direct_init
13753 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
13754 && CONSTRUCTOR_NELTS (initializer) != 1))
13755 {
13756 error_at (loc, "invalid initializer for structured binding "
13757 "declaration");
13758 initializer = error_mark_node;
13759 }
13760
13761 if (decl != error_mark_node)
13762 {
13763 cp_maybe_mangle_decomp (decl, prev, v.length ());
13764 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13765 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13766 cp_finish_decomp (decl, prev, v.length ());
13767 }
13768 }
13769 else if (decl != error_mark_node)
13770 {
13771 *maybe_range_for_decl = prev;
13772 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13773 the underlying DECL. */
13774 cp_finish_decomp (decl, prev, v.length ());
13775 }
13776
13777 if (pushed_scope)
13778 pop_scope (pushed_scope);
13779
13780 if (decl == error_mark_node && DECL_P (orig_decl))
13781 {
13782 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13783 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13784 }
13785
13786 return decl;
13787 }
13788
13789 /* Parse a decl-specifier-seq.
13790
13791 decl-specifier-seq:
13792 decl-specifier-seq [opt] decl-specifier
13793 decl-specifier attribute-specifier-seq [opt] (C++11)
13794
13795 decl-specifier:
13796 storage-class-specifier
13797 type-specifier
13798 function-specifier
13799 friend
13800 typedef
13801
13802 GNU Extension:
13803
13804 decl-specifier:
13805 attributes
13806
13807 Concepts Extension:
13808
13809 decl-specifier:
13810 concept
13811
13812 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13813
13814 The parser flags FLAGS is used to control type-specifier parsing.
13815
13816 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13817 flags:
13818
13819 1: one of the decl-specifiers is an elaborated-type-specifier
13820 (i.e., a type declaration)
13821 2: one of the decl-specifiers is an enum-specifier or a
13822 class-specifier (i.e., a type definition)
13823
13824 */
13825
13826 static void
13827 cp_parser_decl_specifier_seq (cp_parser* parser,
13828 cp_parser_flags flags,
13829 cp_decl_specifier_seq *decl_specs,
13830 int* declares_class_or_enum)
13831 {
13832 bool constructor_possible_p = !parser->in_declarator_p;
13833 bool found_decl_spec = false;
13834 cp_token *start_token = NULL;
13835 cp_decl_spec ds;
13836
13837 /* Clear DECL_SPECS. */
13838 clear_decl_specs (decl_specs);
13839
13840 /* Assume no class or enumeration type is declared. */
13841 *declares_class_or_enum = 0;
13842
13843 /* Keep reading specifiers until there are no more to read. */
13844 while (true)
13845 {
13846 bool constructor_p;
13847 cp_token *token;
13848 ds = ds_last;
13849
13850 /* Peek at the next token. */
13851 token = cp_lexer_peek_token (parser->lexer);
13852
13853 /* Save the first token of the decl spec list for error
13854 reporting. */
13855 if (!start_token)
13856 start_token = token;
13857 /* Handle attributes. */
13858 if (cp_next_tokens_can_be_attribute_p (parser))
13859 {
13860 /* Parse the attributes. */
13861 tree attrs = cp_parser_attributes_opt (parser);
13862
13863 /* In a sequence of declaration specifiers, c++11 attributes
13864 appertain to the type that precede them. In that case
13865 [dcl.spec]/1 says:
13866
13867 The attribute-specifier-seq affects the type only for
13868 the declaration it appears in, not other declarations
13869 involving the same type.
13870
13871 But for now let's force the user to position the
13872 attribute either at the beginning of the declaration or
13873 after the declarator-id, which would clearly mean that it
13874 applies to the declarator. */
13875 if (cxx11_attribute_p (attrs))
13876 {
13877 if (!found_decl_spec)
13878 /* The c++11 attribute is at the beginning of the
13879 declaration. It appertains to the entity being
13880 declared. */;
13881 else
13882 {
13883 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13884 {
13885 /* This is an attribute following a
13886 class-specifier. */
13887 if (decl_specs->type_definition_p)
13888 warn_misplaced_attr_for_class_type (token->location,
13889 decl_specs->type);
13890 attrs = NULL_TREE;
13891 }
13892 else
13893 {
13894 decl_specs->std_attributes
13895 = attr_chainon (decl_specs->std_attributes, attrs);
13896 if (decl_specs->locations[ds_std_attribute] == 0)
13897 decl_specs->locations[ds_std_attribute] = token->location;
13898 }
13899 continue;
13900 }
13901 }
13902
13903 decl_specs->attributes
13904 = attr_chainon (decl_specs->attributes, attrs);
13905 if (decl_specs->locations[ds_attribute] == 0)
13906 decl_specs->locations[ds_attribute] = token->location;
13907 continue;
13908 }
13909 /* Assume we will find a decl-specifier keyword. */
13910 found_decl_spec = true;
13911 /* If the next token is an appropriate keyword, we can simply
13912 add it to the list. */
13913 switch (token->keyword)
13914 {
13915 /* decl-specifier:
13916 friend
13917 constexpr */
13918 case RID_FRIEND:
13919 if (!at_class_scope_p ())
13920 {
13921 gcc_rich_location richloc (token->location);
13922 richloc.add_fixit_remove ();
13923 error_at (&richloc, "%<friend%> used outside of class");
13924 cp_lexer_purge_token (parser->lexer);
13925 }
13926 else
13927 {
13928 ds = ds_friend;
13929 /* Consume the token. */
13930 cp_lexer_consume_token (parser->lexer);
13931 }
13932 break;
13933
13934 case RID_CONSTEXPR:
13935 ds = ds_constexpr;
13936 cp_lexer_consume_token (parser->lexer);
13937 break;
13938
13939 case RID_CONCEPT:
13940 ds = ds_concept;
13941 cp_lexer_consume_token (parser->lexer);
13942 break;
13943
13944 /* function-specifier:
13945 inline
13946 virtual
13947 explicit */
13948 case RID_INLINE:
13949 case RID_VIRTUAL:
13950 case RID_EXPLICIT:
13951 cp_parser_function_specifier_opt (parser, decl_specs);
13952 break;
13953
13954 /* decl-specifier:
13955 typedef */
13956 case RID_TYPEDEF:
13957 ds = ds_typedef;
13958 /* Consume the token. */
13959 cp_lexer_consume_token (parser->lexer);
13960 /* A constructor declarator cannot appear in a typedef. */
13961 constructor_possible_p = false;
13962 /* The "typedef" keyword can only occur in a declaration; we
13963 may as well commit at this point. */
13964 cp_parser_commit_to_tentative_parse (parser);
13965
13966 if (decl_specs->storage_class != sc_none)
13967 decl_specs->conflicting_specifiers_p = true;
13968 break;
13969
13970 /* storage-class-specifier:
13971 auto
13972 register
13973 static
13974 extern
13975 mutable
13976
13977 GNU Extension:
13978 thread */
13979 case RID_AUTO:
13980 if (cxx_dialect == cxx98)
13981 {
13982 /* Consume the token. */
13983 cp_lexer_consume_token (parser->lexer);
13984
13985 /* Complain about `auto' as a storage specifier, if
13986 we're complaining about C++0x compatibility. */
13987 gcc_rich_location richloc (token->location);
13988 richloc.add_fixit_remove ();
13989 warning_at (&richloc, OPT_Wc__11_compat,
13990 "%<auto%> changes meaning in C++11; "
13991 "please remove it");
13992
13993 /* Set the storage class anyway. */
13994 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13995 token);
13996 }
13997 else
13998 /* C++0x auto type-specifier. */
13999 found_decl_spec = false;
14000 break;
14001
14002 case RID_REGISTER:
14003 case RID_STATIC:
14004 case RID_EXTERN:
14005 case RID_MUTABLE:
14006 /* Consume the token. */
14007 cp_lexer_consume_token (parser->lexer);
14008 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
14009 token);
14010 break;
14011 case RID_THREAD:
14012 /* Consume the token. */
14013 ds = ds_thread;
14014 cp_lexer_consume_token (parser->lexer);
14015 break;
14016
14017 default:
14018 /* We did not yet find a decl-specifier yet. */
14019 found_decl_spec = false;
14020 break;
14021 }
14022
14023 if (found_decl_spec
14024 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
14025 && token->keyword != RID_CONSTEXPR)
14026 error ("decl-specifier invalid in condition");
14027
14028 if (found_decl_spec
14029 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
14030 && token->keyword != RID_MUTABLE
14031 && token->keyword != RID_CONSTEXPR)
14032 error_at (token->location, "%qD invalid in lambda",
14033 ridpointers[token->keyword]);
14034
14035 if (ds != ds_last)
14036 set_and_check_decl_spec_loc (decl_specs, ds, token);
14037
14038 /* Constructors are a special case. The `S' in `S()' is not a
14039 decl-specifier; it is the beginning of the declarator. */
14040 constructor_p
14041 = (!found_decl_spec
14042 && constructor_possible_p
14043 && (cp_parser_constructor_declarator_p
14044 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
14045
14046 /* If we don't have a DECL_SPEC yet, then we must be looking at
14047 a type-specifier. */
14048 if (!found_decl_spec && !constructor_p)
14049 {
14050 int decl_spec_declares_class_or_enum;
14051 bool is_cv_qualifier;
14052 tree type_spec;
14053
14054 type_spec
14055 = cp_parser_type_specifier (parser, flags,
14056 decl_specs,
14057 /*is_declaration=*/true,
14058 &decl_spec_declares_class_or_enum,
14059 &is_cv_qualifier);
14060 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
14061
14062 /* If this type-specifier referenced a user-defined type
14063 (a typedef, class-name, etc.), then we can't allow any
14064 more such type-specifiers henceforth.
14065
14066 [dcl.spec]
14067
14068 The longest sequence of decl-specifiers that could
14069 possibly be a type name is taken as the
14070 decl-specifier-seq of a declaration. The sequence shall
14071 be self-consistent as described below.
14072
14073 [dcl.type]
14074
14075 As a general rule, at most one type-specifier is allowed
14076 in the complete decl-specifier-seq of a declaration. The
14077 only exceptions are the following:
14078
14079 -- const or volatile can be combined with any other
14080 type-specifier.
14081
14082 -- signed or unsigned can be combined with char, long,
14083 short, or int.
14084
14085 -- ..
14086
14087 Example:
14088
14089 typedef char* Pc;
14090 void g (const int Pc);
14091
14092 Here, Pc is *not* part of the decl-specifier seq; it's
14093 the declarator. Therefore, once we see a type-specifier
14094 (other than a cv-qualifier), we forbid any additional
14095 user-defined types. We *do* still allow things like `int
14096 int' to be considered a decl-specifier-seq, and issue the
14097 error message later. */
14098 if (type_spec && !is_cv_qualifier)
14099 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14100 /* A constructor declarator cannot follow a type-specifier. */
14101 if (type_spec)
14102 {
14103 constructor_possible_p = false;
14104 found_decl_spec = true;
14105 if (!is_cv_qualifier)
14106 decl_specs->any_type_specifiers_p = true;
14107
14108 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
14109 error_at (token->location, "type-specifier invalid in lambda");
14110 }
14111 }
14112
14113 /* If we still do not have a DECL_SPEC, then there are no more
14114 decl-specifiers. */
14115 if (!found_decl_spec)
14116 break;
14117
14118 decl_specs->any_specifiers_p = true;
14119 /* After we see one decl-specifier, further decl-specifiers are
14120 always optional. */
14121 flags |= CP_PARSER_FLAGS_OPTIONAL;
14122 }
14123
14124 /* Don't allow a friend specifier with a class definition. */
14125 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
14126 && (*declares_class_or_enum & 2))
14127 error_at (decl_specs->locations[ds_friend],
14128 "class definition may not be declared a friend");
14129 }
14130
14131 /* Parse an (optional) storage-class-specifier.
14132
14133 storage-class-specifier:
14134 auto
14135 register
14136 static
14137 extern
14138 mutable
14139
14140 GNU Extension:
14141
14142 storage-class-specifier:
14143 thread
14144
14145 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14146
14147 static tree
14148 cp_parser_storage_class_specifier_opt (cp_parser* parser)
14149 {
14150 switch (cp_lexer_peek_token (parser->lexer)->keyword)
14151 {
14152 case RID_AUTO:
14153 if (cxx_dialect != cxx98)
14154 return NULL_TREE;
14155 /* Fall through for C++98. */
14156 gcc_fallthrough ();
14157
14158 case RID_REGISTER:
14159 case RID_STATIC:
14160 case RID_EXTERN:
14161 case RID_MUTABLE:
14162 case RID_THREAD:
14163 /* Consume the token. */
14164 return cp_lexer_consume_token (parser->lexer)->u.value;
14165
14166 default:
14167 return NULL_TREE;
14168 }
14169 }
14170
14171 /* Parse an (optional) function-specifier.
14172
14173 function-specifier:
14174 inline
14175 virtual
14176 explicit
14177
14178 C++2A Extension:
14179 explicit(constant-expression)
14180
14181 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14182 Updates DECL_SPECS, if it is non-NULL. */
14183
14184 static tree
14185 cp_parser_function_specifier_opt (cp_parser* parser,
14186 cp_decl_specifier_seq *decl_specs)
14187 {
14188 cp_token *token = cp_lexer_peek_token (parser->lexer);
14189 switch (token->keyword)
14190 {
14191 case RID_INLINE:
14192 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
14193 break;
14194
14195 case RID_VIRTUAL:
14196 /* 14.5.2.3 [temp.mem]
14197
14198 A member function template shall not be virtual. */
14199 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14200 && current_class_type)
14201 error_at (token->location, "templates may not be %<virtual%>");
14202 else
14203 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
14204 break;
14205
14206 case RID_EXPLICIT:
14207 {
14208 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
14209 /* If we see '(', it's C++20 explicit(bool). */
14210 tree expr;
14211 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14212 {
14213 matching_parens parens;
14214 parens.consume_open (parser);
14215
14216 /* New types are not allowed in an explicit-specifier. */
14217 const char *saved_message
14218 = parser->type_definition_forbidden_message;
14219 parser->type_definition_forbidden_message
14220 = G_("types may not be defined in explicit-specifier");
14221
14222 if (cxx_dialect < cxx2a)
14223 pedwarn (token->location, 0,
14224 "%<explicit(bool)%> only available with -std=c++2a "
14225 "or -std=gnu++2a");
14226
14227 /* Parse the constant-expression. */
14228 expr = cp_parser_constant_expression (parser);
14229
14230 /* Restore the saved message. */
14231 parser->type_definition_forbidden_message = saved_message;
14232 parens.require_close (parser);
14233 }
14234 else
14235 /* The explicit-specifier explicit without a constant-expression is
14236 equivalent to the explicit-specifier explicit(true). */
14237 expr = boolean_true_node;
14238
14239 /* [dcl.fct.spec]
14240 "the constant-expression, if supplied, shall be a contextually
14241 converted constant expression of type bool." */
14242 expr = build_explicit_specifier (expr, tf_warning_or_error);
14243 /* We could evaluate it -- mark the decl as appropriate. */
14244 if (expr == boolean_true_node)
14245 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
14246 else if (expr == boolean_false_node)
14247 /* Don't mark the decl as explicit. */;
14248 else if (decl_specs)
14249 /* The expression was value-dependent. Remember it so that we can
14250 substitute it later. */
14251 decl_specs->explicit_specifier = expr;
14252 return id;
14253 }
14254
14255 default:
14256 return NULL_TREE;
14257 }
14258
14259 /* Consume the token. */
14260 return cp_lexer_consume_token (parser->lexer)->u.value;
14261 }
14262
14263 /* Parse a linkage-specification.
14264
14265 linkage-specification:
14266 extern string-literal { declaration-seq [opt] }
14267 extern string-literal declaration */
14268
14269 static void
14270 cp_parser_linkage_specification (cp_parser* parser)
14271 {
14272 tree linkage;
14273
14274 /* Look for the `extern' keyword. */
14275 cp_token *extern_token
14276 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
14277
14278 /* Look for the string-literal. */
14279 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
14280 linkage = cp_parser_string_literal (parser, false, false);
14281
14282 /* Transform the literal into an identifier. If the literal is a
14283 wide-character string, or contains embedded NULs, then we can't
14284 handle it as the user wants. */
14285 if (strlen (TREE_STRING_POINTER (linkage))
14286 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
14287 {
14288 cp_parser_error (parser, "invalid linkage-specification");
14289 /* Assume C++ linkage. */
14290 linkage = lang_name_cplusplus;
14291 }
14292 else
14293 linkage = get_identifier (TREE_STRING_POINTER (linkage));
14294
14295 /* We're now using the new linkage. */
14296 push_lang_context (linkage);
14297
14298 /* Preserve the location of the the innermost linkage specification,
14299 tracking the locations of nested specifications via a local. */
14300 location_t saved_location
14301 = parser->innermost_linkage_specification_location;
14302 /* Construct a location ranging from the start of the "extern" to
14303 the end of the string-literal, with the caret at the start, e.g.:
14304 extern "C" {
14305 ^~~~~~~~~~
14306 */
14307 parser->innermost_linkage_specification_location
14308 = make_location (extern_token->location,
14309 extern_token->location,
14310 get_finish (string_token->location));
14311
14312 /* If the next token is a `{', then we're using the first
14313 production. */
14314 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14315 {
14316 cp_ensure_no_omp_declare_simd (parser);
14317 cp_ensure_no_oacc_routine (parser);
14318
14319 /* Consume the `{' token. */
14320 matching_braces braces;
14321 braces.consume_open (parser);
14322 /* Parse the declarations. */
14323 cp_parser_declaration_seq_opt (parser);
14324 /* Look for the closing `}'. */
14325 braces.require_close (parser);
14326 }
14327 /* Otherwise, there's just one declaration. */
14328 else
14329 {
14330 bool saved_in_unbraced_linkage_specification_p;
14331
14332 saved_in_unbraced_linkage_specification_p
14333 = parser->in_unbraced_linkage_specification_p;
14334 parser->in_unbraced_linkage_specification_p = true;
14335 cp_parser_declaration (parser);
14336 parser->in_unbraced_linkage_specification_p
14337 = saved_in_unbraced_linkage_specification_p;
14338 }
14339
14340 /* We're done with the linkage-specification. */
14341 pop_lang_context ();
14342
14343 /* Restore location of parent linkage specification, if any. */
14344 parser->innermost_linkage_specification_location = saved_location;
14345 }
14346
14347 /* Parse a static_assert-declaration.
14348
14349 static_assert-declaration:
14350 static_assert ( constant-expression , string-literal ) ;
14351 static_assert ( constant-expression ) ; (C++17)
14352
14353 If MEMBER_P, this static_assert is a class member. */
14354
14355 static void
14356 cp_parser_static_assert(cp_parser *parser, bool member_p)
14357 {
14358 cp_expr condition;
14359 location_t token_loc;
14360 tree message;
14361 bool dummy;
14362
14363 /* Peek at the `static_assert' token so we can keep track of exactly
14364 where the static assertion started. */
14365 token_loc = cp_lexer_peek_token (parser->lexer)->location;
14366
14367 /* Look for the `static_assert' keyword. */
14368 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
14369 RT_STATIC_ASSERT))
14370 return;
14371
14372 /* We know we are in a static assertion; commit to any tentative
14373 parse. */
14374 if (cp_parser_parsing_tentatively (parser))
14375 cp_parser_commit_to_tentative_parse (parser);
14376
14377 /* Parse the `(' starting the static assertion condition. */
14378 matching_parens parens;
14379 parens.require_open (parser);
14380
14381 /* Parse the constant-expression. Allow a non-constant expression
14382 here in order to give better diagnostics in finish_static_assert. */
14383 condition =
14384 cp_parser_constant_expression (parser,
14385 /*allow_non_constant_p=*/true,
14386 /*non_constant_p=*/&dummy);
14387
14388 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14389 {
14390 if (cxx_dialect < cxx17)
14391 pedwarn (input_location, OPT_Wpedantic,
14392 "static_assert without a message "
14393 "only available with -std=c++17 or -std=gnu++17");
14394 /* Eat the ')' */
14395 cp_lexer_consume_token (parser->lexer);
14396 message = build_string (1, "");
14397 TREE_TYPE (message) = char_array_type_node;
14398 fix_string_type (message);
14399 }
14400 else
14401 {
14402 /* Parse the separating `,'. */
14403 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
14404
14405 /* Parse the string-literal message. */
14406 message = cp_parser_string_literal (parser,
14407 /*translate=*/false,
14408 /*wide_ok=*/true);
14409
14410 /* A `)' completes the static assertion. */
14411 if (!parens.require_close (parser))
14412 cp_parser_skip_to_closing_parenthesis (parser,
14413 /*recovering=*/true,
14414 /*or_comma=*/false,
14415 /*consume_paren=*/true);
14416 }
14417
14418 /* A semicolon terminates the declaration. */
14419 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14420
14421 /* Get the location for the static assertion. Use that of the
14422 condition if available, otherwise, use that of the "static_assert"
14423 token. */
14424 location_t assert_loc = condition.get_location ();
14425 if (assert_loc == UNKNOWN_LOCATION)
14426 assert_loc = token_loc;
14427
14428 /* Complete the static assertion, which may mean either processing
14429 the static assert now or saving it for template instantiation. */
14430 finish_static_assert (condition, message, assert_loc, member_p);
14431 }
14432
14433 /* Parse the expression in decltype ( expression ). */
14434
14435 static tree
14436 cp_parser_decltype_expr (cp_parser *parser,
14437 bool &id_expression_or_member_access_p)
14438 {
14439 cp_token *id_expr_start_token;
14440 tree expr;
14441
14442 /* Since we're going to preserve any side-effects from this parse, set up a
14443 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14444 in the expression. */
14445 tentative_firewall firewall (parser);
14446
14447 /* First, try parsing an id-expression. */
14448 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14449 cp_parser_parse_tentatively (parser);
14450 expr = cp_parser_id_expression (parser,
14451 /*template_keyword_p=*/false,
14452 /*check_dependency_p=*/true,
14453 /*template_p=*/NULL,
14454 /*declarator_p=*/false,
14455 /*optional_p=*/false);
14456
14457 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14458 {
14459 bool non_integral_constant_expression_p = false;
14460 tree id_expression = expr;
14461 cp_id_kind idk;
14462 const char *error_msg;
14463
14464 if (identifier_p (expr))
14465 /* Lookup the name we got back from the id-expression. */
14466 expr = cp_parser_lookup_name_simple (parser, expr,
14467 id_expr_start_token->location);
14468
14469 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
14470 /* A template without args is not a complete id-expression. */
14471 expr = error_mark_node;
14472
14473 if (expr
14474 && expr != error_mark_node
14475 && TREE_CODE (expr) != TYPE_DECL
14476 && (TREE_CODE (expr) != BIT_NOT_EXPR
14477 || !TYPE_P (TREE_OPERAND (expr, 0)))
14478 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14479 {
14480 /* Complete lookup of the id-expression. */
14481 expr = (finish_id_expression
14482 (id_expression, expr, parser->scope, &idk,
14483 /*integral_constant_expression_p=*/false,
14484 /*allow_non_integral_constant_expression_p=*/true,
14485 &non_integral_constant_expression_p,
14486 /*template_p=*/false,
14487 /*done=*/true,
14488 /*address_p=*/false,
14489 /*template_arg_p=*/false,
14490 &error_msg,
14491 id_expr_start_token->location));
14492
14493 if (expr == error_mark_node)
14494 /* We found an id-expression, but it was something that we
14495 should not have found. This is an error, not something
14496 we can recover from, so note that we found an
14497 id-expression and we'll recover as gracefully as
14498 possible. */
14499 id_expression_or_member_access_p = true;
14500 }
14501
14502 if (expr
14503 && expr != error_mark_node
14504 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14505 /* We have an id-expression. */
14506 id_expression_or_member_access_p = true;
14507 }
14508
14509 if (!id_expression_or_member_access_p)
14510 {
14511 /* Abort the id-expression parse. */
14512 cp_parser_abort_tentative_parse (parser);
14513
14514 /* Parsing tentatively, again. */
14515 cp_parser_parse_tentatively (parser);
14516
14517 /* Parse a class member access. */
14518 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14519 /*cast_p=*/false, /*decltype*/true,
14520 /*member_access_only_p=*/true, NULL);
14521
14522 if (expr
14523 && expr != error_mark_node
14524 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14525 /* We have an id-expression. */
14526 id_expression_or_member_access_p = true;
14527 }
14528
14529 if (id_expression_or_member_access_p)
14530 /* We have parsed the complete id-expression or member access. */
14531 cp_parser_parse_definitely (parser);
14532 else
14533 {
14534 /* Abort our attempt to parse an id-expression or member access
14535 expression. */
14536 cp_parser_abort_tentative_parse (parser);
14537
14538 /* Commit to the tentative_firewall so we get syntax errors. */
14539 cp_parser_commit_to_tentative_parse (parser);
14540
14541 /* Parse a full expression. */
14542 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14543 /*decltype_p=*/true);
14544 }
14545
14546 return expr;
14547 }
14548
14549 /* Parse a `decltype' type. Returns the type.
14550
14551 simple-type-specifier:
14552 decltype ( expression )
14553 C++14 proposal:
14554 decltype ( auto ) */
14555
14556 static tree
14557 cp_parser_decltype (cp_parser *parser)
14558 {
14559 bool id_expression_or_member_access_p = false;
14560 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14561
14562 if (start_token->type == CPP_DECLTYPE)
14563 {
14564 /* Already parsed. */
14565 cp_lexer_consume_token (parser->lexer);
14566 return saved_checks_value (start_token->u.tree_check_value);
14567 }
14568
14569 /* Look for the `decltype' token. */
14570 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14571 return error_mark_node;
14572
14573 /* Parse the opening `('. */
14574 matching_parens parens;
14575 if (!parens.require_open (parser))
14576 return error_mark_node;
14577
14578 push_deferring_access_checks (dk_deferred);
14579
14580 tree expr = NULL_TREE;
14581
14582 if (cxx_dialect >= cxx14
14583 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14584 /* decltype (auto) */
14585 cp_lexer_consume_token (parser->lexer);
14586 else
14587 {
14588 /* decltype (expression) */
14589
14590 /* Types cannot be defined in a `decltype' expression. Save away the
14591 old message and set the new one. */
14592 const char *saved_message = parser->type_definition_forbidden_message;
14593 parser->type_definition_forbidden_message
14594 = G_("types may not be defined in %<decltype%> expressions");
14595
14596 /* The restrictions on constant-expressions do not apply inside
14597 decltype expressions. */
14598 bool saved_integral_constant_expression_p
14599 = parser->integral_constant_expression_p;
14600 bool saved_non_integral_constant_expression_p
14601 = parser->non_integral_constant_expression_p;
14602 parser->integral_constant_expression_p = false;
14603
14604 /* Within a parenthesized expression, a `>' token is always
14605 the greater-than operator. */
14606 bool saved_greater_than_is_operator_p
14607 = parser->greater_than_is_operator_p;
14608 parser->greater_than_is_operator_p = true;
14609
14610 /* Do not actually evaluate the expression. */
14611 ++cp_unevaluated_operand;
14612
14613 /* Do not warn about problems with the expression. */
14614 ++c_inhibit_evaluation_warnings;
14615
14616 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14617 STRIP_ANY_LOCATION_WRAPPER (expr);
14618
14619 /* Go back to evaluating expressions. */
14620 --cp_unevaluated_operand;
14621 --c_inhibit_evaluation_warnings;
14622
14623 /* The `>' token might be the end of a template-id or
14624 template-parameter-list now. */
14625 parser->greater_than_is_operator_p
14626 = saved_greater_than_is_operator_p;
14627
14628 /* Restore the old message and the integral constant expression
14629 flags. */
14630 parser->type_definition_forbidden_message = saved_message;
14631 parser->integral_constant_expression_p
14632 = saved_integral_constant_expression_p;
14633 parser->non_integral_constant_expression_p
14634 = saved_non_integral_constant_expression_p;
14635 }
14636
14637 /* Parse to the closing `)'. */
14638 if (!parens.require_close (parser))
14639 {
14640 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14641 /*consume_paren=*/true);
14642 pop_deferring_access_checks ();
14643 return error_mark_node;
14644 }
14645
14646 if (!expr)
14647 {
14648 /* Build auto. */
14649 expr = make_decltype_auto ();
14650 AUTO_IS_DECLTYPE (expr) = true;
14651 }
14652 else
14653 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14654 tf_warning_or_error);
14655
14656 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14657 it again. */
14658 start_token->type = CPP_DECLTYPE;
14659 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14660 start_token->u.tree_check_value->value = expr;
14661 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14662 start_token->keyword = RID_MAX;
14663 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14664
14665 pop_to_parent_deferring_access_checks ();
14666
14667 return expr;
14668 }
14669
14670 /* Special member functions [gram.special] */
14671
14672 /* Parse a conversion-function-id.
14673
14674 conversion-function-id:
14675 operator conversion-type-id
14676
14677 Returns an IDENTIFIER_NODE representing the operator. */
14678
14679 static tree
14680 cp_parser_conversion_function_id (cp_parser* parser)
14681 {
14682 tree type;
14683 tree saved_scope;
14684 tree saved_qualifying_scope;
14685 tree saved_object_scope;
14686 tree pushed_scope = NULL_TREE;
14687
14688 /* Look for the `operator' token. */
14689 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14690 return error_mark_node;
14691 /* When we parse the conversion-type-id, the current scope will be
14692 reset. However, we need that information in able to look up the
14693 conversion function later, so we save it here. */
14694 saved_scope = parser->scope;
14695 saved_qualifying_scope = parser->qualifying_scope;
14696 saved_object_scope = parser->object_scope;
14697 /* We must enter the scope of the class so that the names of
14698 entities declared within the class are available in the
14699 conversion-type-id. For example, consider:
14700
14701 struct S {
14702 typedef int I;
14703 operator I();
14704 };
14705
14706 S::operator I() { ... }
14707
14708 In order to see that `I' is a type-name in the definition, we
14709 must be in the scope of `S'. */
14710 if (saved_scope)
14711 pushed_scope = push_scope (saved_scope);
14712 /* Parse the conversion-type-id. */
14713 type = cp_parser_conversion_type_id (parser);
14714 /* Leave the scope of the class, if any. */
14715 if (pushed_scope)
14716 pop_scope (pushed_scope);
14717 /* Restore the saved scope. */
14718 parser->scope = saved_scope;
14719 parser->qualifying_scope = saved_qualifying_scope;
14720 parser->object_scope = saved_object_scope;
14721 /* If the TYPE is invalid, indicate failure. */
14722 if (type == error_mark_node)
14723 return error_mark_node;
14724 return make_conv_op_name (type);
14725 }
14726
14727 /* Parse a conversion-type-id:
14728
14729 conversion-type-id:
14730 type-specifier-seq conversion-declarator [opt]
14731
14732 Returns the TYPE specified. */
14733
14734 static tree
14735 cp_parser_conversion_type_id (cp_parser* parser)
14736 {
14737 tree attributes;
14738 cp_decl_specifier_seq type_specifiers;
14739 cp_declarator *declarator;
14740 tree type_specified;
14741 const char *saved_message;
14742
14743 /* Parse the attributes. */
14744 attributes = cp_parser_attributes_opt (parser);
14745
14746 saved_message = parser->type_definition_forbidden_message;
14747 parser->type_definition_forbidden_message
14748 = G_("types may not be defined in a conversion-type-id");
14749
14750 /* Parse the type-specifiers. */
14751 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
14752 /*is_declaration=*/false,
14753 /*is_trailing_return=*/false,
14754 &type_specifiers);
14755
14756 parser->type_definition_forbidden_message = saved_message;
14757
14758 /* If that didn't work, stop. */
14759 if (type_specifiers.type == error_mark_node)
14760 return error_mark_node;
14761 /* Parse the conversion-declarator. */
14762 declarator = cp_parser_conversion_declarator_opt (parser);
14763
14764 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14765 /*initialized=*/0, &attributes);
14766 if (attributes)
14767 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14768
14769 /* Don't give this error when parsing tentatively. This happens to
14770 work because we always parse this definitively once. */
14771 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14772 && type_uses_auto (type_specified))
14773 {
14774 if (cxx_dialect < cxx14)
14775 {
14776 error ("invalid use of %<auto%> in conversion operator");
14777 return error_mark_node;
14778 }
14779 else if (template_parm_scope_p ())
14780 warning (0, "use of %<auto%> in member template "
14781 "conversion operator can never be deduced");
14782 }
14783
14784 return type_specified;
14785 }
14786
14787 /* Parse an (optional) conversion-declarator.
14788
14789 conversion-declarator:
14790 ptr-operator conversion-declarator [opt]
14791
14792 */
14793
14794 static cp_declarator *
14795 cp_parser_conversion_declarator_opt (cp_parser* parser)
14796 {
14797 enum tree_code code;
14798 tree class_type, std_attributes = NULL_TREE;
14799 cp_cv_quals cv_quals;
14800
14801 /* We don't know if there's a ptr-operator next, or not. */
14802 cp_parser_parse_tentatively (parser);
14803 /* Try the ptr-operator. */
14804 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14805 &std_attributes);
14806 /* If it worked, look for more conversion-declarators. */
14807 if (cp_parser_parse_definitely (parser))
14808 {
14809 cp_declarator *declarator;
14810
14811 /* Parse another optional declarator. */
14812 declarator = cp_parser_conversion_declarator_opt (parser);
14813
14814 declarator = cp_parser_make_indirect_declarator
14815 (code, class_type, cv_quals, declarator, std_attributes);
14816
14817 return declarator;
14818 }
14819
14820 return NULL;
14821 }
14822
14823 /* Parse an (optional) ctor-initializer.
14824
14825 ctor-initializer:
14826 : mem-initializer-list */
14827
14828 static void
14829 cp_parser_ctor_initializer_opt (cp_parser* parser)
14830 {
14831 /* If the next token is not a `:', then there is no
14832 ctor-initializer. */
14833 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14834 {
14835 /* Do default initialization of any bases and members. */
14836 if (DECL_CONSTRUCTOR_P (current_function_decl))
14837 finish_mem_initializers (NULL_TREE);
14838 return;
14839 }
14840
14841 /* Consume the `:' token. */
14842 cp_lexer_consume_token (parser->lexer);
14843 /* And the mem-initializer-list. */
14844 cp_parser_mem_initializer_list (parser);
14845 }
14846
14847 /* Parse a mem-initializer-list.
14848
14849 mem-initializer-list:
14850 mem-initializer ... [opt]
14851 mem-initializer ... [opt] , mem-initializer-list */
14852
14853 static void
14854 cp_parser_mem_initializer_list (cp_parser* parser)
14855 {
14856 tree mem_initializer_list = NULL_TREE;
14857 tree target_ctor = error_mark_node;
14858 cp_token *token = cp_lexer_peek_token (parser->lexer);
14859
14860 /* Let the semantic analysis code know that we are starting the
14861 mem-initializer-list. */
14862 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14863 error_at (token->location,
14864 "only constructors take member initializers");
14865
14866 /* Loop through the list. */
14867 while (true)
14868 {
14869 tree mem_initializer;
14870
14871 token = cp_lexer_peek_token (parser->lexer);
14872 /* Parse the mem-initializer. */
14873 mem_initializer = cp_parser_mem_initializer (parser);
14874 /* If the next token is a `...', we're expanding member initializers. */
14875 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14876 if (ellipsis
14877 || (mem_initializer != error_mark_node
14878 && check_for_bare_parameter_packs (TREE_PURPOSE
14879 (mem_initializer))))
14880 {
14881 /* Consume the `...'. */
14882 if (ellipsis)
14883 cp_lexer_consume_token (parser->lexer);
14884
14885 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14886 can be expanded but members cannot. */
14887 if (mem_initializer != error_mark_node
14888 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14889 {
14890 error_at (token->location,
14891 "cannot expand initializer for member %qD",
14892 TREE_PURPOSE (mem_initializer));
14893 mem_initializer = error_mark_node;
14894 }
14895
14896 /* Construct the pack expansion type. */
14897 if (mem_initializer != error_mark_node)
14898 mem_initializer = make_pack_expansion (mem_initializer);
14899 }
14900 if (target_ctor != error_mark_node
14901 && mem_initializer != error_mark_node)
14902 {
14903 error ("mem-initializer for %qD follows constructor delegation",
14904 TREE_PURPOSE (mem_initializer));
14905 mem_initializer = error_mark_node;
14906 }
14907 /* Look for a target constructor. */
14908 if (mem_initializer != error_mark_node
14909 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14910 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14911 {
14912 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14913 if (mem_initializer_list)
14914 {
14915 error ("constructor delegation follows mem-initializer for %qD",
14916 TREE_PURPOSE (mem_initializer_list));
14917 mem_initializer = error_mark_node;
14918 }
14919 target_ctor = mem_initializer;
14920 }
14921 /* Add it to the list, unless it was erroneous. */
14922 if (mem_initializer != error_mark_node)
14923 {
14924 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14925 mem_initializer_list = mem_initializer;
14926 }
14927 /* If the next token is not a `,', we're done. */
14928 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14929 break;
14930 /* Consume the `,' token. */
14931 cp_lexer_consume_token (parser->lexer);
14932 }
14933
14934 /* Perform semantic analysis. */
14935 if (DECL_CONSTRUCTOR_P (current_function_decl))
14936 finish_mem_initializers (mem_initializer_list);
14937 }
14938
14939 /* Parse a mem-initializer.
14940
14941 mem-initializer:
14942 mem-initializer-id ( expression-list [opt] )
14943 mem-initializer-id braced-init-list
14944
14945 GNU extension:
14946
14947 mem-initializer:
14948 ( expression-list [opt] )
14949
14950 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14951 class) or FIELD_DECL (for a non-static data member) to initialize;
14952 the TREE_VALUE is the expression-list. An empty initialization
14953 list is represented by void_list_node. */
14954
14955 static tree
14956 cp_parser_mem_initializer (cp_parser* parser)
14957 {
14958 tree mem_initializer_id;
14959 tree expression_list;
14960 tree member;
14961 cp_token *token = cp_lexer_peek_token (parser->lexer);
14962
14963 /* Find out what is being initialized. */
14964 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14965 {
14966 permerror (token->location,
14967 "anachronistic old-style base class initializer");
14968 mem_initializer_id = NULL_TREE;
14969 }
14970 else
14971 {
14972 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14973 if (mem_initializer_id == error_mark_node)
14974 return mem_initializer_id;
14975 }
14976 member = expand_member_init (mem_initializer_id);
14977 if (member && !DECL_P (member))
14978 in_base_initializer = 1;
14979
14980 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14981 {
14982 bool expr_non_constant_p;
14983 cp_lexer_set_source_position (parser->lexer);
14984 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14985 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14986 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14987 expression_list = build_tree_list (NULL_TREE, expression_list);
14988 }
14989 else
14990 {
14991 vec<tree, va_gc> *vec;
14992 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14993 /*cast_p=*/false,
14994 /*allow_expansion_p=*/true,
14995 /*non_constant_p=*/NULL,
14996 /*close_paren_loc=*/NULL,
14997 /*wrap_locations_p=*/true);
14998 if (vec == NULL)
14999 return error_mark_node;
15000 expression_list = build_tree_list_vec (vec);
15001 release_tree_vector (vec);
15002 }
15003
15004 if (expression_list == error_mark_node)
15005 return error_mark_node;
15006 if (!expression_list)
15007 expression_list = void_type_node;
15008
15009 in_base_initializer = 0;
15010
15011 return member ? build_tree_list (member, expression_list) : error_mark_node;
15012 }
15013
15014 /* Parse a mem-initializer-id.
15015
15016 mem-initializer-id:
15017 :: [opt] nested-name-specifier [opt] class-name
15018 decltype-specifier (C++11)
15019 identifier
15020
15021 Returns a TYPE indicating the class to be initialized for the first
15022 production (and the second in C++11). Returns an IDENTIFIER_NODE
15023 indicating the data member to be initialized for the last production. */
15024
15025 static tree
15026 cp_parser_mem_initializer_id (cp_parser* parser)
15027 {
15028 bool global_scope_p;
15029 bool nested_name_specifier_p;
15030 bool template_p = false;
15031 tree id;
15032
15033 cp_token *token = cp_lexer_peek_token (parser->lexer);
15034
15035 /* `typename' is not allowed in this context ([temp.res]). */
15036 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15037 {
15038 error_at (token->location,
15039 "keyword %<typename%> not allowed in this context (a qualified "
15040 "member initializer is implicitly a type)");
15041 cp_lexer_consume_token (parser->lexer);
15042 }
15043 /* Look for the optional `::' operator. */
15044 global_scope_p
15045 = (cp_parser_global_scope_opt (parser,
15046 /*current_scope_valid_p=*/false)
15047 != NULL_TREE);
15048 /* Look for the optional nested-name-specifier. The simplest way to
15049 implement:
15050
15051 [temp.res]
15052
15053 The keyword `typename' is not permitted in a base-specifier or
15054 mem-initializer; in these contexts a qualified name that
15055 depends on a template-parameter is implicitly assumed to be a
15056 type name.
15057
15058 is to assume that we have seen the `typename' keyword at this
15059 point. */
15060 nested_name_specifier_p
15061 = (cp_parser_nested_name_specifier_opt (parser,
15062 /*typename_keyword_p=*/true,
15063 /*check_dependency_p=*/true,
15064 /*type_p=*/true,
15065 /*is_declaration=*/true)
15066 != NULL_TREE);
15067 if (nested_name_specifier_p)
15068 template_p = cp_parser_optional_template_keyword (parser);
15069 /* If there is a `::' operator or a nested-name-specifier, then we
15070 are definitely looking for a class-name. */
15071 if (global_scope_p || nested_name_specifier_p)
15072 return cp_parser_class_name (parser,
15073 /*typename_keyword_p=*/true,
15074 /*template_keyword_p=*/template_p,
15075 typename_type,
15076 /*check_dependency_p=*/true,
15077 /*class_head_p=*/false,
15078 /*is_declaration=*/true);
15079 /* Otherwise, we could also be looking for an ordinary identifier. */
15080 cp_parser_parse_tentatively (parser);
15081 if (cp_lexer_next_token_is_decltype (parser->lexer))
15082 /* Try a decltype-specifier. */
15083 id = cp_parser_decltype (parser);
15084 else
15085 /* Otherwise, try a class-name. */
15086 id = cp_parser_class_name (parser,
15087 /*typename_keyword_p=*/true,
15088 /*template_keyword_p=*/false,
15089 none_type,
15090 /*check_dependency_p=*/true,
15091 /*class_head_p=*/false,
15092 /*is_declaration=*/true);
15093 /* If we found one, we're done. */
15094 if (cp_parser_parse_definitely (parser))
15095 return id;
15096 /* Otherwise, look for an ordinary identifier. */
15097 return cp_parser_identifier (parser);
15098 }
15099
15100 /* Overloading [gram.over] */
15101
15102 /* Parse an operator-function-id.
15103
15104 operator-function-id:
15105 operator operator
15106
15107 Returns an IDENTIFIER_NODE for the operator which is a
15108 human-readable spelling of the identifier, e.g., `operator +'. */
15109
15110 static cp_expr
15111 cp_parser_operator_function_id (cp_parser* parser)
15112 {
15113 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
15114 /* Look for the `operator' keyword. */
15115 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
15116 return error_mark_node;
15117 /* And then the name of the operator itself. */
15118 return cp_parser_operator (parser, start_loc);
15119 }
15120
15121 /* Return an identifier node for a user-defined literal operator.
15122 The suffix identifier is chained to the operator name identifier. */
15123
15124 tree
15125 cp_literal_operator_id (const char* name)
15126 {
15127 tree identifier;
15128 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
15129 + strlen (name) + 10);
15130 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
15131 identifier = get_identifier (buffer);
15132
15133 return identifier;
15134 }
15135
15136 /* Parse an operator.
15137
15138 operator:
15139 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15140 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15141 || ++ -- , ->* -> () []
15142
15143 GNU Extensions:
15144
15145 operator:
15146 <? >? <?= >?=
15147
15148 Returns an IDENTIFIER_NODE for the operator which is a
15149 human-readable spelling of the identifier, e.g., `operator +'. */
15150
15151 static cp_expr
15152 cp_parser_operator (cp_parser* parser, location_t start_loc)
15153 {
15154 tree id = NULL_TREE;
15155 cp_token *token;
15156 bool utf8 = false;
15157
15158 /* Peek at the next token. */
15159 token = cp_lexer_peek_token (parser->lexer);
15160
15161 location_t end_loc = token->location;
15162
15163 /* Figure out which operator we have. */
15164 enum tree_code op = ERROR_MARK;
15165 bool assop = false;
15166 bool consumed = false;
15167 switch (token->type)
15168 {
15169 case CPP_KEYWORD:
15170 {
15171 /* The keyword should be either `new' or `delete'. */
15172 if (token->keyword == RID_NEW)
15173 op = NEW_EXPR;
15174 else if (token->keyword == RID_DELETE)
15175 op = DELETE_EXPR;
15176 else
15177 break;
15178
15179 /* Consume the `new' or `delete' token. */
15180 end_loc = cp_lexer_consume_token (parser->lexer)->location;
15181
15182 /* Peek at the next token. */
15183 token = cp_lexer_peek_token (parser->lexer);
15184 /* If it's a `[' token then this is the array variant of the
15185 operator. */
15186 if (token->type == CPP_OPEN_SQUARE)
15187 {
15188 /* Consume the `[' token. */
15189 cp_lexer_consume_token (parser->lexer);
15190 /* Look for the `]' token. */
15191 if (cp_token *close_token
15192 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15193 end_loc = close_token->location;
15194 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
15195 }
15196 consumed = true;
15197 break;
15198 }
15199
15200 case CPP_PLUS:
15201 op = PLUS_EXPR;
15202 break;
15203
15204 case CPP_MINUS:
15205 op = MINUS_EXPR;
15206 break;
15207
15208 case CPP_MULT:
15209 op = MULT_EXPR;
15210 break;
15211
15212 case CPP_DIV:
15213 op = TRUNC_DIV_EXPR;
15214 break;
15215
15216 case CPP_MOD:
15217 op = TRUNC_MOD_EXPR;
15218 break;
15219
15220 case CPP_XOR:
15221 op = BIT_XOR_EXPR;
15222 break;
15223
15224 case CPP_AND:
15225 op = BIT_AND_EXPR;
15226 break;
15227
15228 case CPP_OR:
15229 op = BIT_IOR_EXPR;
15230 break;
15231
15232 case CPP_COMPL:
15233 op = BIT_NOT_EXPR;
15234 break;
15235
15236 case CPP_NOT:
15237 op = TRUTH_NOT_EXPR;
15238 break;
15239
15240 case CPP_EQ:
15241 assop = true;
15242 op = NOP_EXPR;
15243 break;
15244
15245 case CPP_LESS:
15246 op = LT_EXPR;
15247 break;
15248
15249 case CPP_GREATER:
15250 op = GT_EXPR;
15251 break;
15252
15253 case CPP_PLUS_EQ:
15254 assop = true;
15255 op = PLUS_EXPR;
15256 break;
15257
15258 case CPP_MINUS_EQ:
15259 assop = true;
15260 op = MINUS_EXPR;
15261 break;
15262
15263 case CPP_MULT_EQ:
15264 assop = true;
15265 op = MULT_EXPR;
15266 break;
15267
15268 case CPP_DIV_EQ:
15269 assop = true;
15270 op = TRUNC_DIV_EXPR;
15271 break;
15272
15273 case CPP_MOD_EQ:
15274 assop = true;
15275 op = TRUNC_MOD_EXPR;
15276 break;
15277
15278 case CPP_XOR_EQ:
15279 assop = true;
15280 op = BIT_XOR_EXPR;
15281 break;
15282
15283 case CPP_AND_EQ:
15284 assop = true;
15285 op = BIT_AND_EXPR;
15286 break;
15287
15288 case CPP_OR_EQ:
15289 assop = true;
15290 op = BIT_IOR_EXPR;
15291 break;
15292
15293 case CPP_LSHIFT:
15294 op = LSHIFT_EXPR;
15295 break;
15296
15297 case CPP_RSHIFT:
15298 op = RSHIFT_EXPR;
15299 break;
15300
15301 case CPP_LSHIFT_EQ:
15302 assop = true;
15303 op = LSHIFT_EXPR;
15304 break;
15305
15306 case CPP_RSHIFT_EQ:
15307 assop = true;
15308 op = RSHIFT_EXPR;
15309 break;
15310
15311 case CPP_EQ_EQ:
15312 op = EQ_EXPR;
15313 break;
15314
15315 case CPP_NOT_EQ:
15316 op = NE_EXPR;
15317 break;
15318
15319 case CPP_LESS_EQ:
15320 op = LE_EXPR;
15321 break;
15322
15323 case CPP_GREATER_EQ:
15324 op = GE_EXPR;
15325 break;
15326
15327 case CPP_AND_AND:
15328 op = TRUTH_ANDIF_EXPR;
15329 break;
15330
15331 case CPP_OR_OR:
15332 op = TRUTH_ORIF_EXPR;
15333 break;
15334
15335 case CPP_PLUS_PLUS:
15336 op = POSTINCREMENT_EXPR;
15337 break;
15338
15339 case CPP_MINUS_MINUS:
15340 op = PREDECREMENT_EXPR;
15341 break;
15342
15343 case CPP_COMMA:
15344 op = COMPOUND_EXPR;
15345 break;
15346
15347 case CPP_DEREF_STAR:
15348 op = MEMBER_REF;
15349 break;
15350
15351 case CPP_DEREF:
15352 op = COMPONENT_REF;
15353 break;
15354
15355 case CPP_OPEN_PAREN:
15356 {
15357 /* Consume the `('. */
15358 matching_parens parens;
15359 parens.consume_open (parser);
15360 /* Look for the matching `)'. */
15361 token = parens.require_close (parser);
15362 if (token)
15363 end_loc = token->location;
15364 op = CALL_EXPR;
15365 consumed = true;
15366 break;
15367 }
15368
15369 case CPP_OPEN_SQUARE:
15370 /* Consume the `['. */
15371 cp_lexer_consume_token (parser->lexer);
15372 /* Look for the matching `]'. */
15373 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
15374 if (token)
15375 end_loc = token->location;
15376 op = ARRAY_REF;
15377 consumed = true;
15378 break;
15379
15380 case CPP_UTF8STRING:
15381 case CPP_UTF8STRING_USERDEF:
15382 utf8 = true;
15383 /* FALLTHRU */
15384 case CPP_STRING:
15385 case CPP_WSTRING:
15386 case CPP_STRING16:
15387 case CPP_STRING32:
15388 case CPP_STRING_USERDEF:
15389 case CPP_WSTRING_USERDEF:
15390 case CPP_STRING16_USERDEF:
15391 case CPP_STRING32_USERDEF:
15392 {
15393 cp_expr str;
15394 tree string_tree;
15395 int sz, len;
15396
15397 if (cxx_dialect == cxx98)
15398 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
15399
15400 /* Consume the string. */
15401 str = cp_parser_string_literal (parser, /*translate=*/true,
15402 /*wide_ok=*/true, /*lookup_udlit=*/false);
15403 if (str == error_mark_node)
15404 return error_mark_node;
15405 else if (TREE_CODE (str) == USERDEF_LITERAL)
15406 {
15407 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
15408 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
15409 end_loc = str.get_location ();
15410 }
15411 else
15412 {
15413 string_tree = str;
15414 /* Look for the suffix identifier. */
15415 token = cp_lexer_peek_token (parser->lexer);
15416 if (token->type == CPP_NAME)
15417 {
15418 id = cp_parser_identifier (parser);
15419 end_loc = token->location;
15420 }
15421 else if (token->type == CPP_KEYWORD)
15422 {
15423 error ("unexpected keyword;"
15424 " remove space between quotes and suffix identifier");
15425 return error_mark_node;
15426 }
15427 else
15428 {
15429 error ("expected suffix identifier");
15430 return error_mark_node;
15431 }
15432 }
15433 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15434 (TREE_TYPE (TREE_TYPE (string_tree))));
15435 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
15436 if (len != 0)
15437 {
15438 error ("expected empty string after %<operator%> keyword");
15439 return error_mark_node;
15440 }
15441 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
15442 != char_type_node)
15443 {
15444 error ("invalid encoding prefix in literal operator");
15445 return error_mark_node;
15446 }
15447 if (id != error_mark_node)
15448 {
15449 const char *name = IDENTIFIER_POINTER (id);
15450 id = cp_literal_operator_id (name);
15451 }
15452 /* Generate a location of the form:
15453 "" _suffix_identifier
15454 ^~~~~~~~~~~~~~~~~~~~~
15455 with caret == start at the start token, finish at the end of the
15456 suffix identifier. */
15457 location_t finish_loc
15458 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15459 location_t combined_loc
15460 = make_location (start_loc, start_loc, finish_loc);
15461 return cp_expr (id, combined_loc);
15462 }
15463
15464 default:
15465 /* Anything else is an error. */
15466 break;
15467 }
15468
15469 /* If we have selected an identifier, we need to consume the
15470 operator token. */
15471 if (op != ERROR_MARK)
15472 {
15473 id = ovl_op_identifier (assop, op);
15474 if (!consumed)
15475 cp_lexer_consume_token (parser->lexer);
15476 }
15477 /* Otherwise, no valid operator name was present. */
15478 else
15479 {
15480 cp_parser_error (parser, "expected operator");
15481 id = error_mark_node;
15482 }
15483
15484 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
15485 return cp_expr (id, start_loc);
15486 }
15487
15488 /* Parse a template-declaration.
15489
15490 template-declaration:
15491 export [opt] template < template-parameter-list > declaration
15492
15493 If MEMBER_P is TRUE, this template-declaration occurs within a
15494 class-specifier.
15495
15496 The grammar rule given by the standard isn't correct. What
15497 is really meant is:
15498
15499 template-declaration:
15500 export [opt] template-parameter-list-seq
15501 decl-specifier-seq [opt] init-declarator [opt] ;
15502 export [opt] template-parameter-list-seq
15503 function-definition
15504
15505 template-parameter-list-seq:
15506 template-parameter-list-seq [opt]
15507 template < template-parameter-list >
15508
15509 Concept Extensions:
15510
15511 template-parameter-list-seq:
15512 template < template-parameter-list > requires-clause [opt]
15513
15514 requires-clause:
15515 requires logical-or-expression */
15516
15517 static void
15518 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15519 {
15520 /* Check for `export'. */
15521 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15522 {
15523 /* Consume the `export' token. */
15524 cp_lexer_consume_token (parser->lexer);
15525 /* Warn that we do not support `export'. */
15526 warning (0, "keyword %<export%> not implemented, and will be ignored");
15527 }
15528
15529 cp_parser_template_declaration_after_export (parser, member_p);
15530 }
15531
15532 /* Parse a template-parameter-list.
15533
15534 template-parameter-list:
15535 template-parameter
15536 template-parameter-list , template-parameter
15537
15538 Returns a TREE_LIST. Each node represents a template parameter.
15539 The nodes are connected via their TREE_CHAINs. */
15540
15541 static tree
15542 cp_parser_template_parameter_list (cp_parser* parser)
15543 {
15544 tree parameter_list = NULL_TREE;
15545
15546 /* Don't create wrapper nodes within a template-parameter-list,
15547 since we don't want to have different types based on the
15548 spelling location of constants and decls within them. */
15549 auto_suppress_location_wrappers sentinel;
15550
15551 begin_template_parm_list ();
15552
15553 /* The loop below parses the template parms. We first need to know
15554 the total number of template parms to be able to compute proper
15555 canonical types of each dependent type. So after the loop, when
15556 we know the total number of template parms,
15557 end_template_parm_list computes the proper canonical types and
15558 fixes up the dependent types accordingly. */
15559 while (true)
15560 {
15561 tree parameter;
15562 bool is_non_type;
15563 bool is_parameter_pack;
15564 location_t parm_loc;
15565
15566 /* Parse the template-parameter. */
15567 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15568 parameter = cp_parser_template_parameter (parser,
15569 &is_non_type,
15570 &is_parameter_pack);
15571 /* Add it to the list. */
15572 if (parameter != error_mark_node)
15573 parameter_list = process_template_parm (parameter_list,
15574 parm_loc,
15575 parameter,
15576 is_non_type,
15577 is_parameter_pack);
15578 else
15579 {
15580 tree err_parm = build_tree_list (parameter, parameter);
15581 parameter_list = chainon (parameter_list, err_parm);
15582 }
15583
15584 /* If the next token is not a `,', we're done. */
15585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15586 break;
15587 /* Otherwise, consume the `,' token. */
15588 cp_lexer_consume_token (parser->lexer);
15589 }
15590
15591 return end_template_parm_list (parameter_list);
15592 }
15593
15594 /* Parse a introduction-list.
15595
15596 introduction-list:
15597 introduced-parameter
15598 introduction-list , introduced-parameter
15599
15600 introduced-parameter:
15601 ...[opt] identifier
15602
15603 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15604 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15605 WILDCARD_DECL will also have DECL_NAME set and token location in
15606 DECL_SOURCE_LOCATION. */
15607
15608 static tree
15609 cp_parser_introduction_list (cp_parser *parser)
15610 {
15611 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15612
15613 while (true)
15614 {
15615 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15616 if (is_pack)
15617 cp_lexer_consume_token (parser->lexer);
15618
15619 tree identifier = cp_parser_identifier (parser);
15620 if (identifier == error_mark_node)
15621 break;
15622
15623 /* Build placeholder. */
15624 tree parm = build_nt (WILDCARD_DECL);
15625 DECL_SOURCE_LOCATION (parm)
15626 = cp_lexer_peek_token (parser->lexer)->location;
15627 DECL_NAME (parm) = identifier;
15628 WILDCARD_PACK_P (parm) = is_pack;
15629 vec_safe_push (introduction_vec, parm);
15630
15631 /* If the next token is not a `,', we're done. */
15632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15633 break;
15634 /* Otherwise, consume the `,' token. */
15635 cp_lexer_consume_token (parser->lexer);
15636 }
15637
15638 /* Convert the vec into a TREE_VEC. */
15639 tree introduction_list = make_tree_vec (introduction_vec->length ());
15640 unsigned int n;
15641 tree parm;
15642 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15643 TREE_VEC_ELT (introduction_list, n) = parm;
15644
15645 release_tree_vector (introduction_vec);
15646 return introduction_list;
15647 }
15648
15649 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15650 is an abstract declarator. */
15651
15652 static inline cp_declarator*
15653 get_id_declarator (cp_declarator *declarator)
15654 {
15655 cp_declarator *d = declarator;
15656 while (d && d->kind != cdk_id)
15657 d = d->declarator;
15658 return d;
15659 }
15660
15661 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15662 is an abstract declarator. */
15663
15664 static inline tree
15665 get_unqualified_id (cp_declarator *declarator)
15666 {
15667 declarator = get_id_declarator (declarator);
15668 if (declarator)
15669 return declarator->u.id.unqualified_name;
15670 else
15671 return NULL_TREE;
15672 }
15673
15674 /* Returns true if DECL represents a constrained-parameter. */
15675
15676 static inline bool
15677 is_constrained_parameter (tree decl)
15678 {
15679 return (decl
15680 && TREE_CODE (decl) == TYPE_DECL
15681 && CONSTRAINED_PARM_CONCEPT (decl)
15682 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15683 }
15684
15685 /* Returns true if PARM declares a constrained-parameter. */
15686
15687 static inline bool
15688 is_constrained_parameter (cp_parameter_declarator *parm)
15689 {
15690 return is_constrained_parameter (parm->decl_specifiers.type);
15691 }
15692
15693 /* Check that the type parameter is only a declarator-id, and that its
15694 type is not cv-qualified. */
15695
15696 bool
15697 cp_parser_check_constrained_type_parm (cp_parser *parser,
15698 cp_parameter_declarator *parm)
15699 {
15700 if (!parm->declarator)
15701 return true;
15702
15703 if (parm->declarator->kind != cdk_id)
15704 {
15705 cp_parser_error (parser, "invalid constrained type parameter");
15706 return false;
15707 }
15708
15709 /* Don't allow cv-qualified type parameters. */
15710 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15711 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15712 {
15713 cp_parser_error (parser, "cv-qualified type parameter");
15714 return false;
15715 }
15716
15717 return true;
15718 }
15719
15720 /* Finish parsing/processing a template type parameter and checking
15721 various restrictions. */
15722
15723 static inline tree
15724 cp_parser_constrained_type_template_parm (cp_parser *parser,
15725 tree id,
15726 cp_parameter_declarator* parmdecl)
15727 {
15728 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15729 return finish_template_type_parm (class_type_node, id);
15730 else
15731 return error_mark_node;
15732 }
15733
15734 static tree
15735 finish_constrained_template_template_parm (tree proto, tree id)
15736 {
15737 /* FIXME: This should probably be copied, and we may need to adjust
15738 the template parameter depths. */
15739 tree saved_parms = current_template_parms;
15740 begin_template_parm_list ();
15741 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15742 end_template_parm_list ();
15743
15744 tree parm = finish_template_template_parm (class_type_node, id);
15745 current_template_parms = saved_parms;
15746
15747 return parm;
15748 }
15749
15750 /* Finish parsing/processing a template template parameter by borrowing
15751 the template parameter list from the prototype parameter. */
15752
15753 static tree
15754 cp_parser_constrained_template_template_parm (cp_parser *parser,
15755 tree proto,
15756 tree id,
15757 cp_parameter_declarator *parmdecl)
15758 {
15759 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15760 return error_mark_node;
15761 return finish_constrained_template_template_parm (proto, id);
15762 }
15763
15764 /* Create a new non-type template parameter from the given PARM
15765 declarator. */
15766
15767 static tree
15768 constrained_non_type_template_parm (bool *is_non_type,
15769 cp_parameter_declarator *parm)
15770 {
15771 *is_non_type = true;
15772 cp_declarator *decl = parm->declarator;
15773 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15774 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15775 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15776 }
15777
15778 /* Build a constrained template parameter based on the PARMDECL
15779 declarator. The type of PARMDECL is the constrained type, which
15780 refers to the prototype template parameter that ultimately
15781 specifies the type of the declared parameter. */
15782
15783 static tree
15784 finish_constrained_parameter (cp_parser *parser,
15785 cp_parameter_declarator *parmdecl,
15786 bool *is_non_type,
15787 bool *is_parameter_pack)
15788 {
15789 tree decl = parmdecl->decl_specifiers.type;
15790 tree id = get_unqualified_id (parmdecl->declarator);
15791 tree def = parmdecl->default_argument;
15792 tree proto = DECL_INITIAL (decl);
15793
15794 /* A template parameter constrained by a variadic concept shall also
15795 be declared as a template parameter pack. */
15796 bool is_variadic = template_parameter_pack_p (proto);
15797 if (is_variadic && !*is_parameter_pack)
15798 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15799
15800 /* Build the parameter. Return an error if the declarator was invalid. */
15801 tree parm;
15802 if (TREE_CODE (proto) == TYPE_DECL)
15803 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15804 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15805 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15806 parmdecl);
15807 else
15808 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15809 if (parm == error_mark_node)
15810 return error_mark_node;
15811
15812 /* Finish the parameter decl and create a node attaching the
15813 default argument and constraint. */
15814 parm = build_tree_list (def, parm);
15815 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15816
15817 return parm;
15818 }
15819
15820 /* Returns true if the parsed type actually represents the declaration
15821 of a type template-parameter. */
15822
15823 static inline bool
15824 declares_constrained_type_template_parameter (tree type)
15825 {
15826 return (is_constrained_parameter (type)
15827 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15828 }
15829
15830
15831 /* Returns true if the parsed type actually represents the declaration of
15832 a template template-parameter. */
15833
15834 static bool
15835 declares_constrained_template_template_parameter (tree type)
15836 {
15837 return (is_constrained_parameter (type)
15838 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15839 }
15840
15841 /* Parse a default argument for a type template-parameter.
15842 Note that diagnostics are handled in cp_parser_template_parameter. */
15843
15844 static tree
15845 cp_parser_default_type_template_argument (cp_parser *parser)
15846 {
15847 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15848
15849 /* Consume the `=' token. */
15850 cp_lexer_consume_token (parser->lexer);
15851
15852 cp_token *token = cp_lexer_peek_token (parser->lexer);
15853
15854 /* Parse the default-argument. */
15855 push_deferring_access_checks (dk_no_deferred);
15856 tree default_argument = cp_parser_type_id (parser,
15857 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15858 NULL);
15859 pop_deferring_access_checks ();
15860
15861 if (flag_concepts && type_uses_auto (default_argument))
15862 {
15863 error_at (token->location,
15864 "invalid use of %<auto%> in default template argument");
15865 return error_mark_node;
15866 }
15867
15868 return default_argument;
15869 }
15870
15871 /* Parse a default argument for a template template-parameter. */
15872
15873 static tree
15874 cp_parser_default_template_template_argument (cp_parser *parser)
15875 {
15876 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15877
15878 bool is_template;
15879
15880 /* Consume the `='. */
15881 cp_lexer_consume_token (parser->lexer);
15882 /* Parse the id-expression. */
15883 push_deferring_access_checks (dk_no_deferred);
15884 /* save token before parsing the id-expression, for error
15885 reporting */
15886 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15887 tree default_argument
15888 = cp_parser_id_expression (parser,
15889 /*template_keyword_p=*/false,
15890 /*check_dependency_p=*/true,
15891 /*template_p=*/&is_template,
15892 /*declarator_p=*/false,
15893 /*optional_p=*/false);
15894 if (TREE_CODE (default_argument) == TYPE_DECL)
15895 /* If the id-expression was a template-id that refers to
15896 a template-class, we already have the declaration here,
15897 so no further lookup is needed. */
15898 ;
15899 else
15900 /* Look up the name. */
15901 default_argument
15902 = cp_parser_lookup_name (parser, default_argument,
15903 none_type,
15904 /*is_template=*/is_template,
15905 /*is_namespace=*/false,
15906 /*check_dependency=*/true,
15907 /*ambiguous_decls=*/NULL,
15908 token->location);
15909 /* See if the default argument is valid. */
15910 default_argument = check_template_template_default_arg (default_argument);
15911 pop_deferring_access_checks ();
15912 return default_argument;
15913 }
15914
15915 /* Parse a template-parameter.
15916
15917 template-parameter:
15918 type-parameter
15919 parameter-declaration
15920
15921 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15922 the parameter. The TREE_PURPOSE is the default value, if any.
15923 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15924 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15925 set to true iff this parameter is a parameter pack. */
15926
15927 static tree
15928 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15929 bool *is_parameter_pack)
15930 {
15931 cp_token *token;
15932 cp_parameter_declarator *parameter_declarator;
15933 tree parm;
15934
15935 /* Assume it is a type parameter or a template parameter. */
15936 *is_non_type = false;
15937 /* Assume it not a parameter pack. */
15938 *is_parameter_pack = false;
15939 /* Peek at the next token. */
15940 token = cp_lexer_peek_token (parser->lexer);
15941 /* If it is `template', we have a type-parameter. */
15942 if (token->keyword == RID_TEMPLATE)
15943 return cp_parser_type_parameter (parser, is_parameter_pack);
15944 /* If it is `class' or `typename' we do not know yet whether it is a
15945 type parameter or a non-type parameter. Consider:
15946
15947 template <typename T, typename T::X X> ...
15948
15949 or:
15950
15951 template <class C, class D*> ...
15952
15953 Here, the first parameter is a type parameter, and the second is
15954 a non-type parameter. We can tell by looking at the token after
15955 the identifier -- if it is a `,', `=', or `>' then we have a type
15956 parameter. */
15957 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15958 {
15959 /* Peek at the token after `class' or `typename'. */
15960 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15961 /* If it's an ellipsis, we have a template type parameter
15962 pack. */
15963 if (token->type == CPP_ELLIPSIS)
15964 return cp_parser_type_parameter (parser, is_parameter_pack);
15965 /* If it's an identifier, skip it. */
15966 if (token->type == CPP_NAME)
15967 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15968 /* Now, see if the token looks like the end of a template
15969 parameter. */
15970 if (token->type == CPP_COMMA
15971 || token->type == CPP_EQ
15972 || token->type == CPP_GREATER)
15973 return cp_parser_type_parameter (parser, is_parameter_pack);
15974 }
15975
15976 /* Otherwise, it is a non-type parameter or a constrained parameter.
15977
15978 [temp.param]
15979
15980 When parsing a default template-argument for a non-type
15981 template-parameter, the first non-nested `>' is taken as the end
15982 of the template parameter-list rather than a greater-than
15983 operator. */
15984 parameter_declarator
15985 = cp_parser_parameter_declaration (parser,
15986 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
15987 /*template_parm_p=*/true,
15988 /*parenthesized_p=*/NULL);
15989
15990 if (!parameter_declarator)
15991 return error_mark_node;
15992
15993 /* If the parameter declaration is marked as a parameter pack, set
15994 *IS_PARAMETER_PACK to notify the caller. */
15995 if (parameter_declarator->template_parameter_pack_p)
15996 *is_parameter_pack = true;
15997
15998 if (parameter_declarator->default_argument)
15999 {
16000 /* Can happen in some cases of erroneous input (c++/34892). */
16001 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16002 /* Consume the `...' for better error recovery. */
16003 cp_lexer_consume_token (parser->lexer);
16004 }
16005
16006 // The parameter may have been constrained.
16007 if (is_constrained_parameter (parameter_declarator))
16008 return finish_constrained_parameter (parser,
16009 parameter_declarator,
16010 is_non_type,
16011 is_parameter_pack);
16012
16013 // Now we're sure that the parameter is a non-type parameter.
16014 *is_non_type = true;
16015
16016 parm = grokdeclarator (parameter_declarator->declarator,
16017 &parameter_declarator->decl_specifiers,
16018 TPARM, /*initialized=*/0,
16019 /*attrlist=*/NULL);
16020 if (parm == error_mark_node)
16021 return error_mark_node;
16022
16023 return build_tree_list (parameter_declarator->default_argument, parm);
16024 }
16025
16026 /* Parse a type-parameter.
16027
16028 type-parameter:
16029 class identifier [opt]
16030 class identifier [opt] = type-id
16031 typename identifier [opt]
16032 typename identifier [opt] = type-id
16033 template < template-parameter-list > class identifier [opt]
16034 template < template-parameter-list > class identifier [opt]
16035 = id-expression
16036
16037 GNU Extension (variadic templates):
16038
16039 type-parameter:
16040 class ... identifier [opt]
16041 typename ... identifier [opt]
16042
16043 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16044 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16045 the declaration of the parameter.
16046
16047 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16048
16049 static tree
16050 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
16051 {
16052 cp_token *token;
16053 tree parameter;
16054
16055 /* Look for a keyword to tell us what kind of parameter this is. */
16056 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
16057 if (!token)
16058 return error_mark_node;
16059
16060 switch (token->keyword)
16061 {
16062 case RID_CLASS:
16063 case RID_TYPENAME:
16064 {
16065 tree identifier;
16066 tree default_argument;
16067
16068 /* If the next token is an ellipsis, we have a template
16069 argument pack. */
16070 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16071 {
16072 /* Consume the `...' token. */
16073 cp_lexer_consume_token (parser->lexer);
16074 maybe_warn_variadic_templates ();
16075
16076 *is_parameter_pack = true;
16077 }
16078
16079 /* If the next token is an identifier, then it names the
16080 parameter. */
16081 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16082 identifier = cp_parser_identifier (parser);
16083 else
16084 identifier = NULL_TREE;
16085
16086 /* Create the parameter. */
16087 parameter = finish_template_type_parm (class_type_node, identifier);
16088
16089 /* If the next token is an `=', we have a default argument. */
16090 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16091 {
16092 default_argument
16093 = cp_parser_default_type_template_argument (parser);
16094
16095 /* Template parameter packs cannot have default
16096 arguments. */
16097 if (*is_parameter_pack)
16098 {
16099 if (identifier)
16100 error_at (token->location,
16101 "template parameter pack %qD cannot have a "
16102 "default argument", identifier);
16103 else
16104 error_at (token->location,
16105 "template parameter packs cannot have "
16106 "default arguments");
16107 default_argument = NULL_TREE;
16108 }
16109 else if (check_for_bare_parameter_packs (default_argument))
16110 default_argument = error_mark_node;
16111 }
16112 else
16113 default_argument = NULL_TREE;
16114
16115 /* Create the combined representation of the parameter and the
16116 default argument. */
16117 parameter = build_tree_list (default_argument, parameter);
16118 }
16119 break;
16120
16121 case RID_TEMPLATE:
16122 {
16123 tree identifier;
16124 tree default_argument;
16125
16126 /* Look for the `<'. */
16127 cp_parser_require (parser, CPP_LESS, RT_LESS);
16128 /* Parse the template-parameter-list. */
16129 cp_parser_template_parameter_list (parser);
16130 /* Look for the `>'. */
16131 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16132
16133 // If template requirements are present, parse them.
16134 if (flag_concepts)
16135 {
16136 tree reqs = get_shorthand_constraints (current_template_parms);
16137 if (tree r = cp_parser_requires_clause_opt (parser))
16138 reqs = conjoin_constraints (reqs, normalize_expression (r));
16139 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
16140 }
16141
16142 /* Look for the `class' or 'typename' keywords. */
16143 cp_parser_type_parameter_key (parser);
16144 /* If the next token is an ellipsis, we have a template
16145 argument pack. */
16146 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16147 {
16148 /* Consume the `...' token. */
16149 cp_lexer_consume_token (parser->lexer);
16150 maybe_warn_variadic_templates ();
16151
16152 *is_parameter_pack = true;
16153 }
16154 /* If the next token is an `=', then there is a
16155 default-argument. If the next token is a `>', we are at
16156 the end of the parameter-list. If the next token is a `,',
16157 then we are at the end of this parameter. */
16158 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16159 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
16160 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16161 {
16162 identifier = cp_parser_identifier (parser);
16163 /* Treat invalid names as if the parameter were nameless. */
16164 if (identifier == error_mark_node)
16165 identifier = NULL_TREE;
16166 }
16167 else
16168 identifier = NULL_TREE;
16169
16170 /* Create the template parameter. */
16171 parameter = finish_template_template_parm (class_type_node,
16172 identifier);
16173
16174 /* If the next token is an `=', then there is a
16175 default-argument. */
16176 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16177 {
16178 default_argument
16179 = cp_parser_default_template_template_argument (parser);
16180
16181 /* Template parameter packs cannot have default
16182 arguments. */
16183 if (*is_parameter_pack)
16184 {
16185 if (identifier)
16186 error_at (token->location,
16187 "template parameter pack %qD cannot "
16188 "have a default argument",
16189 identifier);
16190 else
16191 error_at (token->location, "template parameter packs cannot "
16192 "have default arguments");
16193 default_argument = NULL_TREE;
16194 }
16195 }
16196 else
16197 default_argument = NULL_TREE;
16198
16199 /* Create the combined representation of the parameter and the
16200 default argument. */
16201 parameter = build_tree_list (default_argument, parameter);
16202 }
16203 break;
16204
16205 default:
16206 gcc_unreachable ();
16207 break;
16208 }
16209
16210 return parameter;
16211 }
16212
16213 /* Parse a template-id.
16214
16215 template-id:
16216 template-name < template-argument-list [opt] >
16217
16218 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16219 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16220 returned. Otherwise, if the template-name names a function, or set
16221 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16222 names a class, returns a TYPE_DECL for the specialization.
16223
16224 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16225 uninstantiated templates. */
16226
16227 static tree
16228 cp_parser_template_id (cp_parser *parser,
16229 bool template_keyword_p,
16230 bool check_dependency_p,
16231 enum tag_types tag_type,
16232 bool is_declaration)
16233 {
16234 tree templ;
16235 tree arguments;
16236 tree template_id;
16237 cp_token_position start_of_id = 0;
16238 cp_token *next_token = NULL, *next_token_2 = NULL;
16239 bool is_identifier;
16240
16241 /* If the next token corresponds to a template-id, there is no need
16242 to reparse it. */
16243 cp_token *token = cp_lexer_peek_token (parser->lexer);
16244 if (token->type == CPP_TEMPLATE_ID)
16245 {
16246 cp_lexer_consume_token (parser->lexer);
16247 return saved_checks_value (token->u.tree_check_value);
16248 }
16249
16250 /* Avoid performing name lookup if there is no possibility of
16251 finding a template-id. */
16252 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
16253 || (token->type == CPP_NAME
16254 && !cp_parser_nth_token_starts_template_argument_list_p
16255 (parser, 2)))
16256 {
16257 cp_parser_error (parser, "expected template-id");
16258 return error_mark_node;
16259 }
16260
16261 /* Remember where the template-id starts. */
16262 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
16263 start_of_id = cp_lexer_token_position (parser->lexer, false);
16264
16265 push_deferring_access_checks (dk_deferred);
16266
16267 /* Parse the template-name. */
16268 is_identifier = false;
16269 templ = cp_parser_template_name (parser, template_keyword_p,
16270 check_dependency_p,
16271 is_declaration,
16272 tag_type,
16273 &is_identifier);
16274
16275 /* Push any access checks inside the firewall we're about to create. */
16276 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
16277 pop_deferring_access_checks ();
16278 if (templ == error_mark_node || is_identifier)
16279 return templ;
16280
16281 /* Since we're going to preserve any side-effects from this parse, set up a
16282 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16283 in the template arguments. */
16284 tentative_firewall firewall (parser);
16285 reopen_deferring_access_checks (checks);
16286
16287 /* If we find the sequence `[:' after a template-name, it's probably
16288 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16289 parse correctly the argument list. */
16290 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
16291 == CPP_OPEN_SQUARE)
16292 && next_token->flags & DIGRAPH
16293 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
16294 == CPP_COLON)
16295 && !(next_token_2->flags & PREV_WHITE))
16296 {
16297 cp_parser_parse_tentatively (parser);
16298 /* Change `:' into `::'. */
16299 next_token_2->type = CPP_SCOPE;
16300 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16301 CPP_LESS. */
16302 cp_lexer_consume_token (parser->lexer);
16303
16304 /* Parse the arguments. */
16305 arguments = cp_parser_enclosed_template_argument_list (parser);
16306 if (!cp_parser_parse_definitely (parser))
16307 {
16308 /* If we couldn't parse an argument list, then we revert our changes
16309 and return simply an error. Maybe this is not a template-id
16310 after all. */
16311 next_token_2->type = CPP_COLON;
16312 cp_parser_error (parser, "expected %<<%>");
16313 pop_deferring_access_checks ();
16314 return error_mark_node;
16315 }
16316 /* Otherwise, emit an error about the invalid digraph, but continue
16317 parsing because we got our argument list. */
16318 if (permerror (next_token->location,
16319 "%<<::%> cannot begin a template-argument list"))
16320 {
16321 static bool hint = false;
16322 inform (next_token->location,
16323 "%<<:%> is an alternate spelling for %<[%>."
16324 " Insert whitespace between %<<%> and %<::%>");
16325 if (!hint && !flag_permissive)
16326 {
16327 inform (next_token->location, "(if you use %<-fpermissive%> "
16328 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16329 "accept your code)");
16330 hint = true;
16331 }
16332 }
16333 }
16334 else
16335 {
16336 /* Look for the `<' that starts the template-argument-list. */
16337 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
16338 {
16339 pop_deferring_access_checks ();
16340 return error_mark_node;
16341 }
16342 /* Parse the arguments. */
16343 arguments = cp_parser_enclosed_template_argument_list (parser);
16344
16345 if ((cxx_dialect > cxx17)
16346 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
16347 && !template_keyword_p
16348 && (cp_parser_error_occurred (parser)
16349 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
16350 {
16351 /* This didn't go well. */
16352 if (TREE_CODE (templ) == FUNCTION_DECL)
16353 {
16354 /* C++2A says that "function-name < a;" is now ill-formed. */
16355 if (cp_parser_error_occurred (parser))
16356 {
16357 error_at (token->location, "invalid template-argument-list");
16358 inform (token->location, "function name as the left hand "
16359 "operand of %<<%> is ill-formed in C++2a; wrap the "
16360 "function name in %<()%>");
16361 }
16362 else
16363 /* We expect "f<targs>" to be followed by "(args)". */
16364 error_at (cp_lexer_peek_token (parser->lexer)->location,
16365 "expected %<(%> after template-argument-list");
16366 if (start_of_id)
16367 /* Purge all subsequent tokens. */
16368 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16369 }
16370 else
16371 cp_parser_simulate_error (parser);
16372 pop_deferring_access_checks ();
16373 return error_mark_node;
16374 }
16375 }
16376
16377 /* Set the location to be of the form:
16378 template-name < template-argument-list [opt] >
16379 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16380 with caret == start at the start of the template-name,
16381 ranging until the closing '>'. */
16382 location_t finish_loc
16383 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
16384 location_t combined_loc
16385 = make_location (token->location, token->location, finish_loc);
16386
16387 /* Check for concepts autos where they don't belong. We could
16388 identify types in some cases of idnetifier TEMPL, looking ahead
16389 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16390 types. We reject them in functions, but if what we have is an
16391 identifier, even with none_type we can't conclude it's NOT a
16392 type, we have to wait for template substitution. */
16393 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
16394 template_id = error_mark_node;
16395 /* Build a representation of the specialization. */
16396 else if (identifier_p (templ))
16397 template_id = build_min_nt_loc (combined_loc,
16398 TEMPLATE_ID_EXPR,
16399 templ, arguments);
16400 else if (DECL_TYPE_TEMPLATE_P (templ)
16401 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
16402 {
16403 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16404 template (rather than some instantiation thereof) only if
16405 is not nested within some other construct. For example, in
16406 "template <typename T> void f(T) { A<T>::", A<T> is just an
16407 instantiation of A. */
16408 bool entering_scope
16409 = (template_parm_scope_p ()
16410 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
16411 template_id
16412 = finish_template_type (templ, arguments, entering_scope);
16413 }
16414 /* A template-like identifier may be a partial concept id. */
16415 else if (flag_concepts
16416 && (template_id = (cp_parser_maybe_partial_concept_id
16417 (parser, templ, arguments))))
16418 return template_id;
16419 else if (variable_template_p (templ))
16420 {
16421 template_id = lookup_template_variable (templ, arguments);
16422 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16423 SET_EXPR_LOCATION (template_id, combined_loc);
16424 }
16425 else
16426 {
16427 /* If it's not a class-template or a template-template, it should be
16428 a function-template. */
16429 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
16430 || TREE_CODE (templ) == OVERLOAD
16431 || TREE_CODE (templ) == FUNCTION_DECL
16432 || BASELINK_P (templ)));
16433
16434 template_id = lookup_template_function (templ, arguments);
16435 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
16436 SET_EXPR_LOCATION (template_id, combined_loc);
16437 }
16438
16439 /* If parsing tentatively, replace the sequence of tokens that makes
16440 up the template-id with a CPP_TEMPLATE_ID token. That way,
16441 should we re-parse the token stream, we will not have to repeat
16442 the effort required to do the parse, nor will we issue duplicate
16443 error messages about problems during instantiation of the
16444 template. */
16445 if (start_of_id
16446 /* Don't do this if we had a parse error in a declarator; re-parsing
16447 might succeed if a name changes meaning (60361). */
16448 && !(cp_parser_error_occurred (parser)
16449 && cp_parser_parsing_tentatively (parser)
16450 && parser->in_declarator_p))
16451 {
16452 /* Reset the contents of the START_OF_ID token. */
16453 token->type = CPP_TEMPLATE_ID;
16454 token->location = combined_loc;
16455
16456 /* Retrieve any deferred checks. Do not pop this access checks yet
16457 so the memory will not be reclaimed during token replacing below. */
16458 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16459 token->u.tree_check_value->value = template_id;
16460 token->u.tree_check_value->checks = get_deferred_access_checks ();
16461 token->keyword = RID_MAX;
16462
16463 /* Purge all subsequent tokens. */
16464 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
16465
16466 /* ??? Can we actually assume that, if template_id ==
16467 error_mark_node, we will have issued a diagnostic to the
16468 user, as opposed to simply marking the tentative parse as
16469 failed? */
16470 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
16471 error_at (token->location, "parse error in template argument list");
16472 }
16473
16474 pop_to_parent_deferring_access_checks ();
16475 return template_id;
16476 }
16477
16478 /* Parse a template-name.
16479
16480 template-name:
16481 identifier
16482
16483 The standard should actually say:
16484
16485 template-name:
16486 identifier
16487 operator-function-id
16488
16489 A defect report has been filed about this issue.
16490
16491 A conversion-function-id cannot be a template name because they cannot
16492 be part of a template-id. In fact, looking at this code:
16493
16494 a.operator K<int>()
16495
16496 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16497 It is impossible to call a templated conversion-function-id with an
16498 explicit argument list, since the only allowed template parameter is
16499 the type to which it is converting.
16500
16501 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16502 `template' keyword, in a construction like:
16503
16504 T::template f<3>()
16505
16506 In that case `f' is taken to be a template-name, even though there
16507 is no way of knowing for sure.
16508
16509 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16510 name refers to a set of overloaded functions, at least one of which
16511 is a template, or an IDENTIFIER_NODE with the name of the template,
16512 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16513 names are looked up inside uninstantiated templates. */
16514
16515 static tree
16516 cp_parser_template_name (cp_parser* parser,
16517 bool template_keyword_p,
16518 bool check_dependency_p,
16519 bool is_declaration,
16520 enum tag_types tag_type,
16521 bool *is_identifier)
16522 {
16523 tree identifier;
16524 tree decl;
16525 cp_token *token = cp_lexer_peek_token (parser->lexer);
16526
16527 /* If the next token is `operator', then we have either an
16528 operator-function-id or a conversion-function-id. */
16529 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16530 {
16531 /* We don't know whether we're looking at an
16532 operator-function-id or a conversion-function-id. */
16533 cp_parser_parse_tentatively (parser);
16534 /* Try an operator-function-id. */
16535 identifier = cp_parser_operator_function_id (parser);
16536 /* If that didn't work, try a conversion-function-id. */
16537 if (!cp_parser_parse_definitely (parser))
16538 {
16539 cp_parser_error (parser, "expected template-name");
16540 return error_mark_node;
16541 }
16542 }
16543 /* Look for the identifier. */
16544 else
16545 identifier = cp_parser_identifier (parser);
16546
16547 /* If we didn't find an identifier, we don't have a template-id. */
16548 if (identifier == error_mark_node)
16549 return error_mark_node;
16550
16551 /* If the name immediately followed the `template' keyword, then it
16552 is a template-name. However, if the next token is not `<', then
16553 we do not treat it as a template-name, since it is not being used
16554 as part of a template-id. This enables us to handle constructs
16555 like:
16556
16557 template <typename T> struct S { S(); };
16558 template <typename T> S<T>::S();
16559
16560 correctly. We would treat `S' as a template -- if it were `S<T>'
16561 -- but we do not if there is no `<'. */
16562
16563 if (processing_template_decl
16564 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16565 {
16566 /* In a declaration, in a dependent context, we pretend that the
16567 "template" keyword was present in order to improve error
16568 recovery. For example, given:
16569
16570 template <typename T> void f(T::X<int>);
16571
16572 we want to treat "X<int>" as a template-id. */
16573 if (is_declaration
16574 && !template_keyword_p
16575 && parser->scope && TYPE_P (parser->scope)
16576 && check_dependency_p
16577 && dependent_scope_p (parser->scope)
16578 /* Do not do this for dtors (or ctors), since they never
16579 need the template keyword before their name. */
16580 && !constructor_name_p (identifier, parser->scope))
16581 {
16582 cp_token_position start = 0;
16583
16584 /* Explain what went wrong. */
16585 error_at (token->location, "non-template %qD used as template",
16586 identifier);
16587 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16588 parser->scope, identifier);
16589 /* If parsing tentatively, find the location of the "<" token. */
16590 if (cp_parser_simulate_error (parser))
16591 start = cp_lexer_token_position (parser->lexer, true);
16592 /* Parse the template arguments so that we can issue error
16593 messages about them. */
16594 cp_lexer_consume_token (parser->lexer);
16595 cp_parser_enclosed_template_argument_list (parser);
16596 /* Skip tokens until we find a good place from which to
16597 continue parsing. */
16598 cp_parser_skip_to_closing_parenthesis (parser,
16599 /*recovering=*/true,
16600 /*or_comma=*/true,
16601 /*consume_paren=*/false);
16602 /* If parsing tentatively, permanently remove the
16603 template argument list. That will prevent duplicate
16604 error messages from being issued about the missing
16605 "template" keyword. */
16606 if (start)
16607 cp_lexer_purge_tokens_after (parser->lexer, start);
16608 if (is_identifier)
16609 *is_identifier = true;
16610 parser->context->object_type = NULL_TREE;
16611 return identifier;
16612 }
16613
16614 /* If the "template" keyword is present, then there is generally
16615 no point in doing name-lookup, so we just return IDENTIFIER.
16616 But, if the qualifying scope is non-dependent then we can
16617 (and must) do name-lookup normally. */
16618 if (template_keyword_p)
16619 {
16620 tree scope = (parser->scope ? parser->scope
16621 : parser->context->object_type);
16622 if (scope && TYPE_P (scope)
16623 && (!CLASS_TYPE_P (scope)
16624 || (check_dependency_p && dependent_type_p (scope))))
16625 {
16626 /* We're optimizing away the call to cp_parser_lookup_name, but
16627 we still need to do this. */
16628 parser->context->object_type = NULL_TREE;
16629 return identifier;
16630 }
16631 }
16632 }
16633
16634 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16635 const bool scoped_p = ((parser->scope ? parser->scope
16636 : parser->context->object_type) != NULL_TREE);
16637
16638 /* Look up the name. */
16639 decl = cp_parser_lookup_name (parser, identifier,
16640 tag_type,
16641 /*is_template=*/true,
16642 /*is_namespace=*/false,
16643 check_dependency_p,
16644 /*ambiguous_decls=*/NULL,
16645 token->location);
16646
16647 decl = strip_using_decl (decl);
16648
16649 /* If DECL is a template, then the name was a template-name. */
16650 if (TREE_CODE (decl) == TEMPLATE_DECL)
16651 {
16652 if (TREE_DEPRECATED (decl)
16653 && deprecated_state != DEPRECATED_SUPPRESS)
16654 warn_deprecated_use (decl, NULL_TREE);
16655 }
16656 else
16657 {
16658 /* The standard does not explicitly indicate whether a name that
16659 names a set of overloaded declarations, some of which are
16660 templates, is a template-name. However, such a name should
16661 be a template-name; otherwise, there is no way to form a
16662 template-id for the overloaded templates. */
16663 bool found = false;
16664
16665 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16666 !found && iter; ++iter)
16667 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16668 found = true;
16669
16670 if (!found
16671 && (cxx_dialect > cxx17)
16672 && !scoped_p
16673 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
16674 && tag_type == none_type)
16675 {
16676 /* [temp.names] says "A name is also considered to refer to a template
16677 if it is an unqualified-id followed by a < and name lookup finds
16678 either one or more functions or finds nothing." */
16679
16680 /* The "more functions" case. Just use the OVERLOAD as normally.
16681 We don't use is_overloaded_fn here to avoid considering
16682 BASELINKs. */
16683 if (TREE_CODE (decl) == OVERLOAD
16684 /* Name lookup found one function. */
16685 || TREE_CODE (decl) == FUNCTION_DECL)
16686 found = true;
16687 /* Name lookup found nothing. */
16688 else if (decl == error_mark_node)
16689 return identifier;
16690 }
16691
16692 if (!found)
16693 {
16694 /* The name does not name a template. */
16695 cp_parser_error (parser, "expected template-name");
16696 return error_mark_node;
16697 }
16698 }
16699
16700 return decl;
16701 }
16702
16703 /* Parse a template-argument-list.
16704
16705 template-argument-list:
16706 template-argument ... [opt]
16707 template-argument-list , template-argument ... [opt]
16708
16709 Returns a TREE_VEC containing the arguments. */
16710
16711 static tree
16712 cp_parser_template_argument_list (cp_parser* parser)
16713 {
16714 tree fixed_args[10];
16715 unsigned n_args = 0;
16716 unsigned alloced = 10;
16717 tree *arg_ary = fixed_args;
16718 tree vec;
16719 bool saved_in_template_argument_list_p;
16720 bool saved_ice_p;
16721 bool saved_non_ice_p;
16722
16723 /* Don't create location wrapper nodes within a template-argument-list. */
16724 auto_suppress_location_wrappers sentinel;
16725
16726 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16727 parser->in_template_argument_list_p = true;
16728 /* Even if the template-id appears in an integral
16729 constant-expression, the contents of the argument list do
16730 not. */
16731 saved_ice_p = parser->integral_constant_expression_p;
16732 parser->integral_constant_expression_p = false;
16733 saved_non_ice_p = parser->non_integral_constant_expression_p;
16734 parser->non_integral_constant_expression_p = false;
16735
16736 /* Parse the arguments. */
16737 do
16738 {
16739 tree argument;
16740
16741 if (n_args)
16742 /* Consume the comma. */
16743 cp_lexer_consume_token (parser->lexer);
16744
16745 /* Parse the template-argument. */
16746 argument = cp_parser_template_argument (parser);
16747
16748 /* If the next token is an ellipsis, we're expanding a template
16749 argument pack. */
16750 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16751 {
16752 if (argument == error_mark_node)
16753 {
16754 cp_token *token = cp_lexer_peek_token (parser->lexer);
16755 error_at (token->location,
16756 "expected parameter pack before %<...%>");
16757 }
16758 /* Consume the `...' token. */
16759 cp_lexer_consume_token (parser->lexer);
16760
16761 /* Make the argument into a TYPE_PACK_EXPANSION or
16762 EXPR_PACK_EXPANSION. */
16763 argument = make_pack_expansion (argument);
16764 }
16765
16766 if (n_args == alloced)
16767 {
16768 alloced *= 2;
16769
16770 if (arg_ary == fixed_args)
16771 {
16772 arg_ary = XNEWVEC (tree, alloced);
16773 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16774 }
16775 else
16776 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16777 }
16778 arg_ary[n_args++] = argument;
16779 }
16780 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16781
16782 vec = make_tree_vec (n_args);
16783
16784 while (n_args--)
16785 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16786
16787 if (arg_ary != fixed_args)
16788 free (arg_ary);
16789 parser->non_integral_constant_expression_p = saved_non_ice_p;
16790 parser->integral_constant_expression_p = saved_ice_p;
16791 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16792 if (CHECKING_P)
16793 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16794 return vec;
16795 }
16796
16797 /* Parse a template-argument.
16798
16799 template-argument:
16800 assignment-expression
16801 type-id
16802 id-expression
16803
16804 The representation is that of an assignment-expression, type-id, or
16805 id-expression -- except that the qualified id-expression is
16806 evaluated, so that the value returned is either a DECL or an
16807 OVERLOAD.
16808
16809 Although the standard says "assignment-expression", it forbids
16810 throw-expressions or assignments in the template argument.
16811 Therefore, we use "conditional-expression" instead. */
16812
16813 static tree
16814 cp_parser_template_argument (cp_parser* parser)
16815 {
16816 tree argument;
16817 bool template_p;
16818 bool address_p;
16819 bool maybe_type_id = false;
16820 cp_token *token = NULL, *argument_start_token = NULL;
16821 location_t loc = 0;
16822 cp_id_kind idk;
16823
16824 /* There's really no way to know what we're looking at, so we just
16825 try each alternative in order.
16826
16827 [temp.arg]
16828
16829 In a template-argument, an ambiguity between a type-id and an
16830 expression is resolved to a type-id, regardless of the form of
16831 the corresponding template-parameter.
16832
16833 Therefore, we try a type-id first. */
16834 cp_parser_parse_tentatively (parser);
16835 argument = cp_parser_template_type_arg (parser);
16836 /* If there was no error parsing the type-id but the next token is a
16837 '>>', our behavior depends on which dialect of C++ we're
16838 parsing. In C++98, we probably found a typo for '> >'. But there
16839 are type-id which are also valid expressions. For instance:
16840
16841 struct X { int operator >> (int); };
16842 template <int V> struct Foo {};
16843 Foo<X () >> 5> r;
16844
16845 Here 'X()' is a valid type-id of a function type, but the user just
16846 wanted to write the expression "X() >> 5". Thus, we remember that we
16847 found a valid type-id, but we still try to parse the argument as an
16848 expression to see what happens.
16849
16850 In C++0x, the '>>' will be considered two separate '>'
16851 tokens. */
16852 if (!cp_parser_error_occurred (parser)
16853 && cxx_dialect == cxx98
16854 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16855 {
16856 maybe_type_id = true;
16857 cp_parser_abort_tentative_parse (parser);
16858 }
16859 else
16860 {
16861 /* If the next token isn't a `,' or a `>', then this argument wasn't
16862 really finished. This means that the argument is not a valid
16863 type-id. */
16864 if (!cp_parser_next_token_ends_template_argument_p (parser))
16865 cp_parser_error (parser, "expected template-argument");
16866 /* If that worked, we're done. */
16867 if (cp_parser_parse_definitely (parser))
16868 return argument;
16869 }
16870 /* We're still not sure what the argument will be. */
16871 cp_parser_parse_tentatively (parser);
16872 /* Try a template. */
16873 argument_start_token = cp_lexer_peek_token (parser->lexer);
16874 argument = cp_parser_id_expression (parser,
16875 /*template_keyword_p=*/false,
16876 /*check_dependency_p=*/true,
16877 &template_p,
16878 /*declarator_p=*/false,
16879 /*optional_p=*/false);
16880 /* If the next token isn't a `,' or a `>', then this argument wasn't
16881 really finished. */
16882 if (!cp_parser_next_token_ends_template_argument_p (parser))
16883 cp_parser_error (parser, "expected template-argument");
16884 if (!cp_parser_error_occurred (parser))
16885 {
16886 /* Figure out what is being referred to. If the id-expression
16887 was for a class template specialization, then we will have a
16888 TYPE_DECL at this point. There is no need to do name lookup
16889 at this point in that case. */
16890 if (TREE_CODE (argument) != TYPE_DECL)
16891 argument = cp_parser_lookup_name (parser, argument,
16892 none_type,
16893 /*is_template=*/template_p,
16894 /*is_namespace=*/false,
16895 /*check_dependency=*/true,
16896 /*ambiguous_decls=*/NULL,
16897 argument_start_token->location);
16898 /* Handle a constrained-type-specifier for a non-type template
16899 parameter. */
16900 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16901 argument = decl;
16902 else if (TREE_CODE (argument) != TEMPLATE_DECL
16903 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16904 cp_parser_error (parser, "expected template-name");
16905 }
16906 if (cp_parser_parse_definitely (parser))
16907 {
16908 if (TREE_DEPRECATED (argument))
16909 warn_deprecated_use (argument, NULL_TREE);
16910 return argument;
16911 }
16912 /* It must be a non-type argument. In C++17 any constant-expression is
16913 allowed. */
16914 if (cxx_dialect > cxx14)
16915 goto general_expr;
16916
16917 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16918
16919 -- an integral constant-expression of integral or enumeration
16920 type; or
16921
16922 -- the name of a non-type template-parameter; or
16923
16924 -- the name of an object or function with external linkage...
16925
16926 -- the address of an object or function with external linkage...
16927
16928 -- a pointer to member... */
16929 /* Look for a non-type template parameter. */
16930 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16931 {
16932 cp_parser_parse_tentatively (parser);
16933 argument = cp_parser_primary_expression (parser,
16934 /*address_p=*/false,
16935 /*cast_p=*/false,
16936 /*template_arg_p=*/true,
16937 &idk);
16938 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16939 || !cp_parser_next_token_ends_template_argument_p (parser))
16940 cp_parser_simulate_error (parser);
16941 if (cp_parser_parse_definitely (parser))
16942 return argument;
16943 }
16944
16945 /* If the next token is "&", the argument must be the address of an
16946 object or function with external linkage. */
16947 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16948 if (address_p)
16949 {
16950 loc = cp_lexer_peek_token (parser->lexer)->location;
16951 cp_lexer_consume_token (parser->lexer);
16952 }
16953 /* See if we might have an id-expression. */
16954 token = cp_lexer_peek_token (parser->lexer);
16955 if (token->type == CPP_NAME
16956 || token->keyword == RID_OPERATOR
16957 || token->type == CPP_SCOPE
16958 || token->type == CPP_TEMPLATE_ID
16959 || token->type == CPP_NESTED_NAME_SPECIFIER)
16960 {
16961 cp_parser_parse_tentatively (parser);
16962 argument = cp_parser_primary_expression (parser,
16963 address_p,
16964 /*cast_p=*/false,
16965 /*template_arg_p=*/true,
16966 &idk);
16967 if (cp_parser_error_occurred (parser)
16968 || !cp_parser_next_token_ends_template_argument_p (parser))
16969 cp_parser_abort_tentative_parse (parser);
16970 else
16971 {
16972 tree probe;
16973
16974 if (INDIRECT_REF_P (argument))
16975 {
16976 /* Strip the dereference temporarily. */
16977 gcc_assert (REFERENCE_REF_P (argument));
16978 argument = TREE_OPERAND (argument, 0);
16979 }
16980
16981 /* If we're in a template, we represent a qualified-id referring
16982 to a static data member as a SCOPE_REF even if the scope isn't
16983 dependent so that we can check access control later. */
16984 probe = argument;
16985 if (TREE_CODE (probe) == SCOPE_REF)
16986 probe = TREE_OPERAND (probe, 1);
16987 if (VAR_P (probe))
16988 {
16989 /* A variable without external linkage might still be a
16990 valid constant-expression, so no error is issued here
16991 if the external-linkage check fails. */
16992 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16993 cp_parser_simulate_error (parser);
16994 }
16995 else if (is_overloaded_fn (argument))
16996 /* All overloaded functions are allowed; if the external
16997 linkage test does not pass, an error will be issued
16998 later. */
16999 ;
17000 else if (address_p
17001 && (TREE_CODE (argument) == OFFSET_REF
17002 || TREE_CODE (argument) == SCOPE_REF))
17003 /* A pointer-to-member. */
17004 ;
17005 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
17006 ;
17007 else
17008 cp_parser_simulate_error (parser);
17009
17010 if (cp_parser_parse_definitely (parser))
17011 {
17012 if (address_p)
17013 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
17014 tf_warning_or_error);
17015 else
17016 argument = convert_from_reference (argument);
17017 return argument;
17018 }
17019 }
17020 }
17021 /* If the argument started with "&", there are no other valid
17022 alternatives at this point. */
17023 if (address_p)
17024 {
17025 cp_parser_error (parser, "invalid non-type template argument");
17026 return error_mark_node;
17027 }
17028
17029 general_expr:
17030 /* If the argument wasn't successfully parsed as a type-id followed
17031 by '>>', the argument can only be a constant expression now.
17032 Otherwise, we try parsing the constant-expression tentatively,
17033 because the argument could really be a type-id. */
17034 if (maybe_type_id)
17035 cp_parser_parse_tentatively (parser);
17036
17037 if (cxx_dialect <= cxx14)
17038 argument = cp_parser_constant_expression (parser);
17039 else
17040 {
17041 /* In C++20, we can encounter a braced-init-list. */
17042 if (cxx_dialect >= cxx2a
17043 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17044 {
17045 bool expr_non_constant_p;
17046 return cp_parser_braced_list (parser, &expr_non_constant_p);
17047 }
17048
17049 /* With C++17 generalized non-type template arguments we need to handle
17050 lvalue constant expressions, too. */
17051 argument = cp_parser_assignment_expression (parser);
17052 require_potential_constant_expression (argument);
17053 }
17054
17055 if (!maybe_type_id)
17056 return argument;
17057 if (!cp_parser_next_token_ends_template_argument_p (parser))
17058 cp_parser_error (parser, "expected template-argument");
17059 if (cp_parser_parse_definitely (parser))
17060 return argument;
17061 /* We did our best to parse the argument as a non type-id, but that
17062 was the only alternative that matched (albeit with a '>' after
17063 it). We can assume it's just a typo from the user, and a
17064 diagnostic will then be issued. */
17065 return cp_parser_template_type_arg (parser);
17066 }
17067
17068 /* Parse an explicit-instantiation.
17069
17070 explicit-instantiation:
17071 template declaration
17072
17073 Although the standard says `declaration', what it really means is:
17074
17075 explicit-instantiation:
17076 template decl-specifier-seq [opt] declarator [opt] ;
17077
17078 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17079 supposed to be allowed. A defect report has been filed about this
17080 issue.
17081
17082 GNU Extension:
17083
17084 explicit-instantiation:
17085 storage-class-specifier template
17086 decl-specifier-seq [opt] declarator [opt] ;
17087 function-specifier template
17088 decl-specifier-seq [opt] declarator [opt] ; */
17089
17090 static void
17091 cp_parser_explicit_instantiation (cp_parser* parser)
17092 {
17093 int declares_class_or_enum;
17094 cp_decl_specifier_seq decl_specifiers;
17095 tree extension_specifier = NULL_TREE;
17096
17097 timevar_push (TV_TEMPLATE_INST);
17098
17099 /* Look for an (optional) storage-class-specifier or
17100 function-specifier. */
17101 if (cp_parser_allow_gnu_extensions_p (parser))
17102 {
17103 extension_specifier
17104 = cp_parser_storage_class_specifier_opt (parser);
17105 if (!extension_specifier)
17106 extension_specifier
17107 = cp_parser_function_specifier_opt (parser,
17108 /*decl_specs=*/NULL);
17109 }
17110
17111 /* Look for the `template' keyword. */
17112 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17113 /* Let the front end know that we are processing an explicit
17114 instantiation. */
17115 begin_explicit_instantiation ();
17116 /* [temp.explicit] says that we are supposed to ignore access
17117 control while processing explicit instantiation directives. */
17118 push_deferring_access_checks (dk_no_check);
17119 /* Parse a decl-specifier-seq. */
17120 cp_parser_decl_specifier_seq (parser,
17121 CP_PARSER_FLAGS_OPTIONAL,
17122 &decl_specifiers,
17123 &declares_class_or_enum);
17124 /* If there was exactly one decl-specifier, and it declared a class,
17125 and there's no declarator, then we have an explicit type
17126 instantiation. */
17127 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
17128 {
17129 tree type;
17130
17131 type = check_tag_decl (&decl_specifiers,
17132 /*explicit_type_instantiation_p=*/true);
17133 /* Turn access control back on for names used during
17134 template instantiation. */
17135 pop_deferring_access_checks ();
17136 if (type)
17137 do_type_instantiation (type, extension_specifier,
17138 /*complain=*/tf_error);
17139 }
17140 else
17141 {
17142 cp_declarator *declarator;
17143 tree decl;
17144
17145 /* Parse the declarator. */
17146 declarator
17147 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17148 CP_PARSER_FLAGS_NONE,
17149 /*ctor_dtor_or_conv_p=*/NULL,
17150 /*parenthesized_p=*/NULL,
17151 /*member_p=*/false,
17152 /*friend_p=*/false,
17153 /*static_p=*/false);
17154 if (declares_class_or_enum & 2)
17155 cp_parser_check_for_definition_in_return_type (declarator,
17156 decl_specifiers.type,
17157 decl_specifiers.locations[ds_type_spec]);
17158 if (declarator != cp_error_declarator)
17159 {
17160 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
17161 permerror (decl_specifiers.locations[ds_inline],
17162 "explicit instantiation shall not use"
17163 " %<inline%> specifier");
17164 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
17165 permerror (decl_specifiers.locations[ds_constexpr],
17166 "explicit instantiation shall not use"
17167 " %<constexpr%> specifier");
17168
17169 decl = grokdeclarator (declarator, &decl_specifiers,
17170 NORMAL, 0, &decl_specifiers.attributes);
17171 /* Turn access control back on for names used during
17172 template instantiation. */
17173 pop_deferring_access_checks ();
17174 /* Do the explicit instantiation. */
17175 do_decl_instantiation (decl, extension_specifier);
17176 }
17177 else
17178 {
17179 pop_deferring_access_checks ();
17180 /* Skip the body of the explicit instantiation. */
17181 cp_parser_skip_to_end_of_statement (parser);
17182 }
17183 }
17184 /* We're done with the instantiation. */
17185 end_explicit_instantiation ();
17186
17187 cp_parser_consume_semicolon_at_end_of_statement (parser);
17188
17189 timevar_pop (TV_TEMPLATE_INST);
17190 }
17191
17192 /* Parse an explicit-specialization.
17193
17194 explicit-specialization:
17195 template < > declaration
17196
17197 Although the standard says `declaration', what it really means is:
17198
17199 explicit-specialization:
17200 template <> decl-specifier [opt] init-declarator [opt] ;
17201 template <> function-definition
17202 template <> explicit-specialization
17203 template <> template-declaration */
17204
17205 static void
17206 cp_parser_explicit_specialization (cp_parser* parser)
17207 {
17208 bool need_lang_pop;
17209 cp_token *token = cp_lexer_peek_token (parser->lexer);
17210
17211 /* Look for the `template' keyword. */
17212 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
17213 /* Look for the `<'. */
17214 cp_parser_require (parser, CPP_LESS, RT_LESS);
17215 /* Look for the `>'. */
17216 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
17217 /* We have processed another parameter list. */
17218 ++parser->num_template_parameter_lists;
17219 /* [temp]
17220
17221 A template ... explicit specialization ... shall not have C
17222 linkage. */
17223 if (current_lang_name == lang_name_c)
17224 {
17225 error_at (token->location, "template specialization with C linkage");
17226 maybe_show_extern_c_location ();
17227 /* Give it C++ linkage to avoid confusing other parts of the
17228 front end. */
17229 push_lang_context (lang_name_cplusplus);
17230 need_lang_pop = true;
17231 }
17232 else
17233 need_lang_pop = false;
17234 /* Let the front end know that we are beginning a specialization. */
17235 if (!begin_specialization ())
17236 {
17237 end_specialization ();
17238 return;
17239 }
17240
17241 /* If the next keyword is `template', we need to figure out whether
17242 or not we're looking a template-declaration. */
17243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17244 {
17245 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17246 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
17247 cp_parser_template_declaration_after_export (parser,
17248 /*member_p=*/false);
17249 else
17250 cp_parser_explicit_specialization (parser);
17251 }
17252 else
17253 /* Parse the dependent declaration. */
17254 cp_parser_single_declaration (parser,
17255 /*checks=*/NULL,
17256 /*member_p=*/false,
17257 /*explicit_specialization_p=*/true,
17258 /*friend_p=*/NULL);
17259 /* We're done with the specialization. */
17260 end_specialization ();
17261 /* For the erroneous case of a template with C linkage, we pushed an
17262 implicit C++ linkage scope; exit that scope now. */
17263 if (need_lang_pop)
17264 pop_lang_context ();
17265 /* We're done with this parameter list. */
17266 --parser->num_template_parameter_lists;
17267 }
17268
17269 /* Parse a type-specifier.
17270
17271 type-specifier:
17272 simple-type-specifier
17273 class-specifier
17274 enum-specifier
17275 elaborated-type-specifier
17276 cv-qualifier
17277
17278 GNU Extension:
17279
17280 type-specifier:
17281 __complex__
17282
17283 Returns a representation of the type-specifier. For a
17284 class-specifier, enum-specifier, or elaborated-type-specifier, a
17285 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17286
17287 The parser flags FLAGS is used to control type-specifier parsing.
17288
17289 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17290 in a decl-specifier-seq.
17291
17292 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17293 class-specifier, enum-specifier, or elaborated-type-specifier, then
17294 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17295 if a type is declared; 2 if it is defined. Otherwise, it is set to
17296 zero.
17297
17298 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17299 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17300 is set to FALSE. */
17301
17302 static tree
17303 cp_parser_type_specifier (cp_parser* parser,
17304 cp_parser_flags flags,
17305 cp_decl_specifier_seq *decl_specs,
17306 bool is_declaration,
17307 int* declares_class_or_enum,
17308 bool* is_cv_qualifier)
17309 {
17310 tree type_spec = NULL_TREE;
17311 cp_token *token;
17312 enum rid keyword;
17313 cp_decl_spec ds = ds_last;
17314
17315 /* Assume this type-specifier does not declare a new type. */
17316 if (declares_class_or_enum)
17317 *declares_class_or_enum = 0;
17318 /* And that it does not specify a cv-qualifier. */
17319 if (is_cv_qualifier)
17320 *is_cv_qualifier = false;
17321 /* Peek at the next token. */
17322 token = cp_lexer_peek_token (parser->lexer);
17323
17324 /* If we're looking at a keyword, we can use that to guide the
17325 production we choose. */
17326 keyword = token->keyword;
17327 switch (keyword)
17328 {
17329 case RID_ENUM:
17330 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17331 goto elaborated_type_specifier;
17332
17333 /* Look for the enum-specifier. */
17334 type_spec = cp_parser_enum_specifier (parser);
17335 /* If that worked, we're done. */
17336 if (type_spec)
17337 {
17338 if (declares_class_or_enum)
17339 *declares_class_or_enum = 2;
17340 if (decl_specs)
17341 cp_parser_set_decl_spec_type (decl_specs,
17342 type_spec,
17343 token,
17344 /*type_definition_p=*/true);
17345 return type_spec;
17346 }
17347 else
17348 goto elaborated_type_specifier;
17349
17350 /* Any of these indicate either a class-specifier, or an
17351 elaborated-type-specifier. */
17352 case RID_CLASS:
17353 case RID_STRUCT:
17354 case RID_UNION:
17355 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
17356 goto elaborated_type_specifier;
17357
17358 /* Parse tentatively so that we can back up if we don't find a
17359 class-specifier. */
17360 cp_parser_parse_tentatively (parser);
17361 /* Look for the class-specifier. */
17362 type_spec = cp_parser_class_specifier (parser);
17363 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
17364 /* If that worked, we're done. */
17365 if (cp_parser_parse_definitely (parser))
17366 {
17367 if (declares_class_or_enum)
17368 *declares_class_or_enum = 2;
17369 if (decl_specs)
17370 cp_parser_set_decl_spec_type (decl_specs,
17371 type_spec,
17372 token,
17373 /*type_definition_p=*/true);
17374 return type_spec;
17375 }
17376
17377 /* Fall through. */
17378 elaborated_type_specifier:
17379 /* We're declaring (not defining) a class or enum. */
17380 if (declares_class_or_enum)
17381 *declares_class_or_enum = 1;
17382
17383 /* Fall through. */
17384 case RID_TYPENAME:
17385 /* Look for an elaborated-type-specifier. */
17386 type_spec
17387 = (cp_parser_elaborated_type_specifier
17388 (parser,
17389 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
17390 is_declaration));
17391 if (decl_specs)
17392 cp_parser_set_decl_spec_type (decl_specs,
17393 type_spec,
17394 token,
17395 /*type_definition_p=*/false);
17396 return type_spec;
17397
17398 case RID_CONST:
17399 ds = ds_const;
17400 if (is_cv_qualifier)
17401 *is_cv_qualifier = true;
17402 break;
17403
17404 case RID_VOLATILE:
17405 ds = ds_volatile;
17406 if (is_cv_qualifier)
17407 *is_cv_qualifier = true;
17408 break;
17409
17410 case RID_RESTRICT:
17411 ds = ds_restrict;
17412 if (is_cv_qualifier)
17413 *is_cv_qualifier = true;
17414 break;
17415
17416 case RID_COMPLEX:
17417 /* The `__complex__' keyword is a GNU extension. */
17418 ds = ds_complex;
17419 break;
17420
17421 default:
17422 break;
17423 }
17424
17425 /* Handle simple keywords. */
17426 if (ds != ds_last)
17427 {
17428 if (decl_specs)
17429 {
17430 set_and_check_decl_spec_loc (decl_specs, ds, token);
17431 decl_specs->any_specifiers_p = true;
17432 }
17433 return cp_lexer_consume_token (parser->lexer)->u.value;
17434 }
17435
17436 /* If we do not already have a type-specifier, assume we are looking
17437 at a simple-type-specifier. */
17438 type_spec = cp_parser_simple_type_specifier (parser,
17439 decl_specs,
17440 flags);
17441
17442 /* If we didn't find a type-specifier, and a type-specifier was not
17443 optional in this context, issue an error message. */
17444 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17445 {
17446 cp_parser_error (parser, "expected type specifier");
17447 return error_mark_node;
17448 }
17449
17450 return type_spec;
17451 }
17452
17453 /* Parse a simple-type-specifier.
17454
17455 simple-type-specifier:
17456 :: [opt] nested-name-specifier [opt] type-name
17457 :: [opt] nested-name-specifier template template-id
17458 char
17459 wchar_t
17460 bool
17461 short
17462 int
17463 long
17464 signed
17465 unsigned
17466 float
17467 double
17468 void
17469
17470 C++11 Extension:
17471
17472 simple-type-specifier:
17473 auto
17474 decltype ( expression )
17475 char16_t
17476 char32_t
17477 __underlying_type ( type-id )
17478
17479 C++17 extension:
17480
17481 nested-name-specifier(opt) template-name
17482
17483 GNU Extension:
17484
17485 simple-type-specifier:
17486 __int128
17487 __typeof__ unary-expression
17488 __typeof__ ( type-id )
17489 __typeof__ ( type-id ) { initializer-list , [opt] }
17490
17491 Concepts Extension:
17492
17493 simple-type-specifier:
17494 constrained-type-specifier
17495
17496 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17497 appropriately updated. */
17498
17499 static tree
17500 cp_parser_simple_type_specifier (cp_parser* parser,
17501 cp_decl_specifier_seq *decl_specs,
17502 cp_parser_flags flags)
17503 {
17504 tree type = NULL_TREE;
17505 cp_token *token;
17506 int idx;
17507
17508 /* Peek at the next token. */
17509 token = cp_lexer_peek_token (parser->lexer);
17510
17511 /* If we're looking at a keyword, things are easy. */
17512 switch (token->keyword)
17513 {
17514 case RID_CHAR:
17515 if (decl_specs)
17516 decl_specs->explicit_char_p = true;
17517 type = char_type_node;
17518 break;
17519 case RID_CHAR8:
17520 type = char8_type_node;
17521 break;
17522 case RID_CHAR16:
17523 type = char16_type_node;
17524 break;
17525 case RID_CHAR32:
17526 type = char32_type_node;
17527 break;
17528 case RID_WCHAR:
17529 type = wchar_type_node;
17530 break;
17531 case RID_BOOL:
17532 type = boolean_type_node;
17533 break;
17534 case RID_SHORT:
17535 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
17536 type = short_integer_type_node;
17537 break;
17538 case RID_INT:
17539 if (decl_specs)
17540 decl_specs->explicit_int_p = true;
17541 type = integer_type_node;
17542 break;
17543 case RID_INT_N_0:
17544 case RID_INT_N_1:
17545 case RID_INT_N_2:
17546 case RID_INT_N_3:
17547 idx = token->keyword - RID_INT_N_0;
17548 if (! int_n_enabled_p [idx])
17549 break;
17550 if (decl_specs)
17551 {
17552 decl_specs->explicit_intN_p = true;
17553 decl_specs->int_n_idx = idx;
17554 }
17555 type = int_n_trees [idx].signed_type;
17556 break;
17557 case RID_LONG:
17558 if (decl_specs)
17559 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
17560 type = long_integer_type_node;
17561 break;
17562 case RID_SIGNED:
17563 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17564 type = integer_type_node;
17565 break;
17566 case RID_UNSIGNED:
17567 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17568 type = unsigned_type_node;
17569 break;
17570 case RID_FLOAT:
17571 type = float_type_node;
17572 break;
17573 case RID_DOUBLE:
17574 type = double_type_node;
17575 break;
17576 case RID_VOID:
17577 type = void_type_node;
17578 break;
17579
17580 case RID_AUTO:
17581 maybe_warn_cpp0x (CPP0X_AUTO);
17582 if (parser->auto_is_implicit_function_template_parm_p)
17583 {
17584 /* The 'auto' might be the placeholder return type for a function decl
17585 with trailing return type. */
17586 bool have_trailing_return_fn_decl = false;
17587
17588 cp_parser_parse_tentatively (parser);
17589 cp_lexer_consume_token (parser->lexer);
17590 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17591 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17592 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17593 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17594 {
17595 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17596 {
17597 cp_lexer_consume_token (parser->lexer);
17598 cp_parser_skip_to_closing_parenthesis (parser,
17599 /*recovering*/false,
17600 /*or_comma*/false,
17601 /*consume_paren*/true);
17602 continue;
17603 }
17604
17605 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17606 {
17607 have_trailing_return_fn_decl = true;
17608 break;
17609 }
17610
17611 cp_lexer_consume_token (parser->lexer);
17612 }
17613 cp_parser_abort_tentative_parse (parser);
17614
17615 if (have_trailing_return_fn_decl)
17616 {
17617 type = make_auto ();
17618 break;
17619 }
17620
17621 if (cxx_dialect >= cxx14)
17622 {
17623 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17624 type = TREE_TYPE (type);
17625 }
17626 else
17627 type = error_mark_node;
17628
17629 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17630 {
17631 if (cxx_dialect < cxx14)
17632 error_at (token->location,
17633 "use of %<auto%> in lambda parameter declaration "
17634 "only available with "
17635 "-std=c++14 or -std=gnu++14");
17636 }
17637 else if (cxx_dialect < cxx14)
17638 error_at (token->location,
17639 "use of %<auto%> in parameter declaration "
17640 "only available with "
17641 "-std=c++14 or -std=gnu++14");
17642 else if (!flag_concepts)
17643 pedwarn (token->location, 0,
17644 "use of %<auto%> in parameter declaration "
17645 "only available with -fconcepts");
17646 }
17647 else
17648 type = make_auto ();
17649 break;
17650
17651 case RID_DECLTYPE:
17652 /* Since DR 743, decltype can either be a simple-type-specifier by
17653 itself or begin a nested-name-specifier. Parsing it will replace
17654 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17655 handling below decide what to do. */
17656 cp_parser_decltype (parser);
17657 cp_lexer_set_token_position (parser->lexer, token);
17658 break;
17659
17660 case RID_TYPEOF:
17661 /* Consume the `typeof' token. */
17662 cp_lexer_consume_token (parser->lexer);
17663 /* Parse the operand to `typeof'. */
17664 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17665 /* If it is not already a TYPE, take its type. */
17666 if (!TYPE_P (type))
17667 type = finish_typeof (type);
17668
17669 if (decl_specs)
17670 cp_parser_set_decl_spec_type (decl_specs, type,
17671 token,
17672 /*type_definition_p=*/false);
17673
17674 return type;
17675
17676 case RID_UNDERLYING_TYPE:
17677 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17678 if (decl_specs)
17679 cp_parser_set_decl_spec_type (decl_specs, type,
17680 token,
17681 /*type_definition_p=*/false);
17682
17683 return type;
17684
17685 case RID_BASES:
17686 case RID_DIRECT_BASES:
17687 type = cp_parser_trait_expr (parser, token->keyword);
17688 if (decl_specs)
17689 cp_parser_set_decl_spec_type (decl_specs, type,
17690 token,
17691 /*type_definition_p=*/false);
17692 return type;
17693 default:
17694 break;
17695 }
17696
17697 /* If token is an already-parsed decltype not followed by ::,
17698 it's a simple-type-specifier. */
17699 if (token->type == CPP_DECLTYPE
17700 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17701 {
17702 type = saved_checks_value (token->u.tree_check_value);
17703 if (decl_specs)
17704 {
17705 cp_parser_set_decl_spec_type (decl_specs, type,
17706 token,
17707 /*type_definition_p=*/false);
17708 /* Remember that we are handling a decltype in order to
17709 implement the resolution of DR 1510 when the argument
17710 isn't instantiation dependent. */
17711 decl_specs->decltype_p = true;
17712 }
17713 cp_lexer_consume_token (parser->lexer);
17714 return type;
17715 }
17716
17717 /* If the type-specifier was for a built-in type, we're done. */
17718 if (type)
17719 {
17720 /* Record the type. */
17721 if (decl_specs
17722 && (token->keyword != RID_SIGNED
17723 && token->keyword != RID_UNSIGNED
17724 && token->keyword != RID_SHORT
17725 && token->keyword != RID_LONG))
17726 cp_parser_set_decl_spec_type (decl_specs,
17727 type,
17728 token,
17729 /*type_definition_p=*/false);
17730 if (decl_specs)
17731 decl_specs->any_specifiers_p = true;
17732
17733 /* Consume the token. */
17734 cp_lexer_consume_token (parser->lexer);
17735
17736 if (type == error_mark_node)
17737 return error_mark_node;
17738
17739 /* There is no valid C++ program where a non-template type is
17740 followed by a "<". That usually indicates that the user thought
17741 that the type was a template. */
17742 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17743 token->location);
17744
17745 return TYPE_NAME (type);
17746 }
17747
17748 /* The type-specifier must be a user-defined type. */
17749 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17750 {
17751 bool qualified_p;
17752 bool global_p;
17753 const bool typename_p = (cxx_dialect >= cxx2a
17754 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
17755
17756 /* Don't gobble tokens or issue error messages if this is an
17757 optional type-specifier. */
17758 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17759 cp_parser_parse_tentatively (parser);
17760
17761 token = cp_lexer_peek_token (parser->lexer);
17762
17763 /* Look for the optional `::' operator. */
17764 global_p
17765 = (cp_parser_global_scope_opt (parser,
17766 /*current_scope_valid_p=*/false)
17767 != NULL_TREE);
17768 /* Look for the nested-name specifier. */
17769 qualified_p
17770 = (cp_parser_nested_name_specifier_opt (parser,
17771 /*typename_keyword_p=*/false,
17772 /*check_dependency_p=*/true,
17773 /*type_p=*/false,
17774 /*is_declaration=*/false)
17775 != NULL_TREE);
17776 /* If we have seen a nested-name-specifier, and the next token
17777 is `template', then we are using the template-id production. */
17778 if (parser->scope
17779 && cp_parser_optional_template_keyword (parser))
17780 {
17781 /* Look for the template-id. */
17782 type = cp_parser_template_id (parser,
17783 /*template_keyword_p=*/true,
17784 /*check_dependency_p=*/true,
17785 none_type,
17786 /*is_declaration=*/false);
17787 /* If the template-id did not name a type, we are out of
17788 luck. */
17789 if (TREE_CODE (type) != TYPE_DECL)
17790 {
17791 /* ...unless we pretend we have seen 'typename'. */
17792 if (typename_p)
17793 type = cp_parser_make_typename_type (parser, type,
17794 token->location);
17795 else
17796 {
17797 cp_parser_error (parser, "expected template-id for type");
17798 type = NULL_TREE;
17799 }
17800 }
17801 }
17802 /* Otherwise, look for a type-name. */
17803 else
17804 type = cp_parser_type_name (parser, (qualified_p && typename_p));
17805
17806 /* Keep track of all name-lookups performed in class scopes. */
17807 if (type
17808 && !global_p
17809 && !qualified_p
17810 && TREE_CODE (type) == TYPE_DECL
17811 && identifier_p (DECL_NAME (type)))
17812 maybe_note_name_used_in_class (DECL_NAME (type), type);
17813 /* If it didn't work out, we don't have a TYPE. */
17814 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17815 && !cp_parser_parse_definitely (parser))
17816 type = NULL_TREE;
17817 if (!type && cxx_dialect >= cxx17)
17818 {
17819 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17820 cp_parser_parse_tentatively (parser);
17821
17822 cp_parser_global_scope_opt (parser,
17823 /*current_scope_valid_p=*/false);
17824 cp_parser_nested_name_specifier_opt (parser,
17825 /*typename_keyword_p=*/false,
17826 /*check_dependency_p=*/true,
17827 /*type_p=*/false,
17828 /*is_declaration=*/false);
17829 tree name = cp_parser_identifier (parser);
17830 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17831 && parser->scope != error_mark_node)
17832 {
17833 tree tmpl = cp_parser_lookup_name (parser, name,
17834 none_type,
17835 /*is_template=*/false,
17836 /*is_namespace=*/false,
17837 /*check_dependency=*/true,
17838 /*ambiguous_decls=*/NULL,
17839 token->location);
17840 if (tmpl && tmpl != error_mark_node
17841 && (DECL_CLASS_TEMPLATE_P (tmpl)
17842 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17843 type = make_template_placeholder (tmpl);
17844 else
17845 {
17846 type = error_mark_node;
17847 if (!cp_parser_simulate_error (parser))
17848 cp_parser_name_lookup_error (parser, name, tmpl,
17849 NLE_TYPE, token->location);
17850 }
17851 }
17852 else
17853 type = error_mark_node;
17854
17855 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17856 && !cp_parser_parse_definitely (parser))
17857 type = NULL_TREE;
17858 }
17859 if (type && decl_specs)
17860 cp_parser_set_decl_spec_type (decl_specs, type,
17861 token,
17862 /*type_definition_p=*/false);
17863 }
17864
17865 /* If we didn't get a type-name, issue an error message. */
17866 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17867 {
17868 cp_parser_error (parser, "expected type-name");
17869 return error_mark_node;
17870 }
17871
17872 if (type && type != error_mark_node)
17873 {
17874 /* See if TYPE is an Objective-C type, and if so, parse and
17875 accept any protocol references following it. Do this before
17876 the cp_parser_check_for_invalid_template_id() call, because
17877 Objective-C types can be followed by '<...>' which would
17878 enclose protocol names rather than template arguments, and so
17879 everything is fine. */
17880 if (c_dialect_objc () && !parser->scope
17881 && (objc_is_id (type) || objc_is_class_name (type)))
17882 {
17883 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17884 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17885
17886 /* Clobber the "unqualified" type previously entered into
17887 DECL_SPECS with the new, improved protocol-qualified version. */
17888 if (decl_specs)
17889 decl_specs->type = qual_type;
17890
17891 return qual_type;
17892 }
17893
17894 /* There is no valid C++ program where a non-template type is
17895 followed by a "<". That usually indicates that the user
17896 thought that the type was a template. */
17897 cp_parser_check_for_invalid_template_id (parser, type,
17898 none_type,
17899 token->location);
17900 }
17901
17902 return type;
17903 }
17904
17905 /* Parse a type-name.
17906
17907 type-name:
17908 class-name
17909 enum-name
17910 typedef-name
17911 simple-template-id [in c++0x]
17912
17913 enum-name:
17914 identifier
17915
17916 typedef-name:
17917 identifier
17918
17919 Concepts:
17920
17921 type-name:
17922 concept-name
17923 partial-concept-id
17924
17925 concept-name:
17926 identifier
17927
17928 Returns a TYPE_DECL for the type. */
17929
17930 static tree
17931 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17932 {
17933 tree type_decl;
17934
17935 /* We can't know yet whether it is a class-name or not. */
17936 cp_parser_parse_tentatively (parser);
17937 /* Try a class-name. */
17938 type_decl = cp_parser_class_name (parser,
17939 typename_keyword_p,
17940 /*template_keyword_p=*/false,
17941 none_type,
17942 /*check_dependency_p=*/true,
17943 /*class_head_p=*/false,
17944 /*is_declaration=*/false);
17945 /* If it's not a class-name, keep looking. */
17946 if (!cp_parser_parse_definitely (parser))
17947 {
17948 if (cxx_dialect < cxx11)
17949 /* It must be a typedef-name or an enum-name. */
17950 return cp_parser_nonclass_name (parser);
17951
17952 cp_parser_parse_tentatively (parser);
17953 /* It is either a simple-template-id representing an
17954 instantiation of an alias template... */
17955 type_decl = cp_parser_template_id (parser,
17956 /*template_keyword_p=*/false,
17957 /*check_dependency_p=*/true,
17958 none_type,
17959 /*is_declaration=*/false);
17960 /* Note that this must be an instantiation of an alias template
17961 because [temp.names]/6 says:
17962
17963 A template-id that names an alias template specialization
17964 is a type-name.
17965
17966 Whereas [temp.names]/7 says:
17967
17968 A simple-template-id that names a class template
17969 specialization is a class-name.
17970
17971 With concepts, this could also be a partial-concept-id that
17972 declares a non-type template parameter. */
17973 if (type_decl != NULL_TREE
17974 && TREE_CODE (type_decl) == TYPE_DECL
17975 && TYPE_DECL_ALIAS_P (type_decl))
17976 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17977 else if (is_constrained_parameter (type_decl))
17978 /* Don't do anything. */ ;
17979 else
17980 cp_parser_simulate_error (parser);
17981
17982 if (!cp_parser_parse_definitely (parser))
17983 /* ... Or a typedef-name or an enum-name. */
17984 return cp_parser_nonclass_name (parser);
17985 }
17986
17987 return type_decl;
17988 }
17989
17990 /* Check if DECL and ARGS can form a constrained-type-specifier.
17991 If ARGS is non-null, we try to form a concept check of the
17992 form DECL<?, ARGS> where ? is a wildcard that matches any
17993 kind of template argument. If ARGS is NULL, then we try to
17994 form a concept check of the form DECL<?>. */
17995
17996 static tree
17997 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17998 tree decl, tree args)
17999 {
18000 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
18001
18002 /* If we a constrained-type-specifier cannot be deduced. */
18003 if (parser->prevent_constrained_type_specifiers)
18004 return NULL_TREE;
18005
18006 /* A constrained type specifier can only be found in an
18007 overload set or as a reference to a template declaration.
18008
18009 FIXME: This might be masking a bug. It's possible that
18010 that the deduction below is causing template specializations
18011 to be formed with the wildcard as an argument. */
18012 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
18013 return NULL_TREE;
18014
18015 /* Try to build a call expression that evaluates the
18016 concept. This can fail if the overload set refers
18017 only to non-templates. */
18018 tree placeholder = build_nt (WILDCARD_DECL);
18019 tree check = build_concept_check (decl, placeholder, args);
18020 if (check == error_mark_node)
18021 return NULL_TREE;
18022
18023 /* Deduce the checked constraint and the prototype parameter.
18024
18025 FIXME: In certain cases, failure to deduce should be a
18026 diagnosable error. */
18027 tree conc;
18028 tree proto;
18029 if (!deduce_constrained_parameter (check, conc, proto))
18030 return NULL_TREE;
18031
18032 /* In template parameter scope, this results in a constrained
18033 parameter. Return a descriptor of that parm. */
18034 if (processing_template_parmlist)
18035 return build_constrained_parameter (conc, proto, args);
18036
18037 /* In a parameter-declaration-clause, constrained-type
18038 specifiers result in invented template parameters. */
18039 if (parser->auto_is_implicit_function_template_parm_p)
18040 {
18041 tree x = build_constrained_parameter (conc, proto, args);
18042 return synthesize_implicit_template_parm (parser, x);
18043 }
18044 else
18045 {
18046 /* Otherwise, we're in a context where the constrained
18047 type name is deduced and the constraint applies
18048 after deduction. */
18049 return make_constrained_auto (conc, args);
18050 }
18051
18052 return NULL_TREE;
18053 }
18054
18055 /* If DECL refers to a concept, return a TYPE_DECL representing
18056 the result of using the constrained type specifier in the
18057 current context. DECL refers to a concept if
18058
18059 - it is an overload set containing a function concept taking a single
18060 type argument, or
18061
18062 - it is a variable concept taking a single type argument. */
18063
18064 static tree
18065 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
18066 {
18067 if (flag_concepts
18068 && (TREE_CODE (decl) == OVERLOAD
18069 || BASELINK_P (decl)
18070 || variable_concept_p (decl)))
18071 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
18072 else
18073 return NULL_TREE;
18074 }
18075
18076 /* Check if DECL and ARGS form a partial-concept-id. If so,
18077 assign ID to the resulting constrained placeholder.
18078
18079 Returns true if the partial-concept-id designates a placeholder
18080 and false otherwise. Note that *id is set to NULL_TREE in
18081 this case. */
18082
18083 static tree
18084 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
18085 {
18086 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
18087 }
18088
18089 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18090 or a concept-name.
18091
18092 enum-name:
18093 identifier
18094
18095 typedef-name:
18096 identifier
18097
18098 concept-name:
18099 identifier
18100
18101 Returns a TYPE_DECL for the type. */
18102
18103 static tree
18104 cp_parser_nonclass_name (cp_parser* parser)
18105 {
18106 tree type_decl;
18107 tree identifier;
18108
18109 cp_token *token = cp_lexer_peek_token (parser->lexer);
18110 identifier = cp_parser_identifier (parser);
18111 if (identifier == error_mark_node)
18112 return error_mark_node;
18113
18114 /* Look up the type-name. */
18115 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
18116
18117 type_decl = strip_using_decl (type_decl);
18118
18119 /* If we found an overload set, then it may refer to a concept-name. */
18120 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
18121 type_decl = decl;
18122
18123 if (TREE_CODE (type_decl) != TYPE_DECL
18124 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
18125 {
18126 /* See if this is an Objective-C type. */
18127 tree protos = cp_parser_objc_protocol_refs_opt (parser);
18128 tree type = objc_get_protocol_qualified_type (identifier, protos);
18129 if (type)
18130 type_decl = TYPE_NAME (type);
18131 }
18132
18133 /* Issue an error if we did not find a type-name. */
18134 if (TREE_CODE (type_decl) != TYPE_DECL
18135 /* In Objective-C, we have the complication that class names are
18136 normally type names and start declarations (eg, the
18137 "NSObject" in "NSObject *object;"), but can be used in an
18138 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18139 is an expression. So, a classname followed by a dot is not a
18140 valid type-name. */
18141 || (objc_is_class_name (TREE_TYPE (type_decl))
18142 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
18143 {
18144 if (!cp_parser_simulate_error (parser))
18145 cp_parser_name_lookup_error (parser, identifier, type_decl,
18146 NLE_TYPE, token->location);
18147 return error_mark_node;
18148 }
18149 /* Remember that the name was used in the definition of the
18150 current class so that we can check later to see if the
18151 meaning would have been different after the class was
18152 entirely defined. */
18153 else if (type_decl != error_mark_node
18154 && !parser->scope)
18155 maybe_note_name_used_in_class (identifier, type_decl);
18156
18157 return type_decl;
18158 }
18159
18160 /* Parse an elaborated-type-specifier. Note that the grammar given
18161 here incorporates the resolution to DR68.
18162
18163 elaborated-type-specifier:
18164 class-key :: [opt] nested-name-specifier [opt] identifier
18165 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18166 enum-key :: [opt] nested-name-specifier [opt] identifier
18167 typename :: [opt] nested-name-specifier identifier
18168 typename :: [opt] nested-name-specifier template [opt]
18169 template-id
18170
18171 GNU extension:
18172
18173 elaborated-type-specifier:
18174 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18175 class-key attributes :: [opt] nested-name-specifier [opt]
18176 template [opt] template-id
18177 enum attributes :: [opt] nested-name-specifier [opt] identifier
18178
18179 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18180 declared `friend'. If IS_DECLARATION is TRUE, then this
18181 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18182 something is being declared.
18183
18184 Returns the TYPE specified. */
18185
18186 static tree
18187 cp_parser_elaborated_type_specifier (cp_parser* parser,
18188 bool is_friend,
18189 bool is_declaration)
18190 {
18191 enum tag_types tag_type;
18192 tree identifier;
18193 tree type = NULL_TREE;
18194 tree attributes = NULL_TREE;
18195 tree globalscope;
18196 cp_token *token = NULL;
18197
18198 /* See if we're looking at the `enum' keyword. */
18199 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
18200 {
18201 /* Consume the `enum' token. */
18202 cp_lexer_consume_token (parser->lexer);
18203 /* Remember that it's an enumeration type. */
18204 tag_type = enum_type;
18205 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18206 enums) is used here. */
18207 cp_token *token = cp_lexer_peek_token (parser->lexer);
18208 if (cp_parser_is_keyword (token, RID_CLASS)
18209 || cp_parser_is_keyword (token, RID_STRUCT))
18210 {
18211 gcc_rich_location richloc (token->location);
18212 richloc.add_range (input_location);
18213 richloc.add_fixit_remove ();
18214 pedwarn (&richloc, 0, "elaborated-type-specifier for "
18215 "a scoped enum must not use the %qD keyword",
18216 token->u.value);
18217 /* Consume the `struct' or `class' and parse it anyway. */
18218 cp_lexer_consume_token (parser->lexer);
18219 }
18220 /* Parse the attributes. */
18221 attributes = cp_parser_attributes_opt (parser);
18222 }
18223 /* Or, it might be `typename'. */
18224 else if (cp_lexer_next_token_is_keyword (parser->lexer,
18225 RID_TYPENAME))
18226 {
18227 /* Consume the `typename' token. */
18228 cp_lexer_consume_token (parser->lexer);
18229 /* Remember that it's a `typename' type. */
18230 tag_type = typename_type;
18231 }
18232 /* Otherwise it must be a class-key. */
18233 else
18234 {
18235 tag_type = cp_parser_class_key (parser);
18236 if (tag_type == none_type)
18237 return error_mark_node;
18238 /* Parse the attributes. */
18239 attributes = cp_parser_attributes_opt (parser);
18240 }
18241
18242 /* Look for the `::' operator. */
18243 globalscope = cp_parser_global_scope_opt (parser,
18244 /*current_scope_valid_p=*/false);
18245 /* Look for the nested-name-specifier. */
18246 tree nested_name_specifier;
18247 if (tag_type == typename_type && !globalscope)
18248 {
18249 nested_name_specifier
18250 = cp_parser_nested_name_specifier (parser,
18251 /*typename_keyword_p=*/true,
18252 /*check_dependency_p=*/true,
18253 /*type_p=*/true,
18254 is_declaration);
18255 if (!nested_name_specifier)
18256 return error_mark_node;
18257 }
18258 else
18259 /* Even though `typename' is not present, the proposed resolution
18260 to Core Issue 180 says that in `class A<T>::B', `B' should be
18261 considered a type-name, even if `A<T>' is dependent. */
18262 nested_name_specifier
18263 = cp_parser_nested_name_specifier_opt (parser,
18264 /*typename_keyword_p=*/true,
18265 /*check_dependency_p=*/true,
18266 /*type_p=*/true,
18267 is_declaration);
18268 /* For everything but enumeration types, consider a template-id.
18269 For an enumeration type, consider only a plain identifier. */
18270 if (tag_type != enum_type)
18271 {
18272 bool template_p = false;
18273 tree decl;
18274
18275 /* Allow the `template' keyword. */
18276 template_p = cp_parser_optional_template_keyword (parser);
18277 /* If we didn't see `template', we don't know if there's a
18278 template-id or not. */
18279 if (!template_p)
18280 cp_parser_parse_tentatively (parser);
18281 /* The `template' keyword must follow a nested-name-specifier. */
18282 else if (!nested_name_specifier)
18283 {
18284 cp_parser_error (parser, "%<template%> must follow a nested-"
18285 "name-specifier");
18286 return error_mark_node;
18287 }
18288
18289 /* Parse the template-id. */
18290 token = cp_lexer_peek_token (parser->lexer);
18291 decl = cp_parser_template_id (parser, template_p,
18292 /*check_dependency_p=*/true,
18293 tag_type,
18294 is_declaration);
18295 /* If we didn't find a template-id, look for an ordinary
18296 identifier. */
18297 if (!template_p && !cp_parser_parse_definitely (parser))
18298 ;
18299 /* We can get here when cp_parser_template_id, called by
18300 cp_parser_class_name with tag_type == none_type, succeeds
18301 and caches a BASELINK. Then, when called again here,
18302 instead of failing and returning an error_mark_node
18303 returns it (see template/typename17.C in C++11).
18304 ??? Could we diagnose this earlier? */
18305 else if (tag_type == typename_type && BASELINK_P (decl))
18306 {
18307 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
18308 type = error_mark_node;
18309 }
18310 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18311 in effect, then we must assume that, upon instantiation, the
18312 template will correspond to a class. */
18313 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
18314 && tag_type == typename_type)
18315 type = make_typename_type (parser->scope, decl,
18316 typename_type,
18317 /*complain=*/tf_error);
18318 /* If the `typename' keyword is in effect and DECL is not a type
18319 decl, then type is non existent. */
18320 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
18321 ;
18322 else if (TREE_CODE (decl) == TYPE_DECL)
18323 {
18324 type = check_elaborated_type_specifier (tag_type, decl,
18325 /*allow_template_p=*/true);
18326
18327 /* If the next token is a semicolon, this must be a specialization,
18328 instantiation, or friend declaration. Check the scope while we
18329 still know whether or not we had a nested-name-specifier. */
18330 if (type != error_mark_node
18331 && !nested_name_specifier && !is_friend
18332 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18333 check_unqualified_spec_or_inst (type, token->location);
18334 }
18335 else if (decl == error_mark_node)
18336 type = error_mark_node;
18337 }
18338
18339 if (!type)
18340 {
18341 token = cp_lexer_peek_token (parser->lexer);
18342 identifier = cp_parser_identifier (parser);
18343
18344 if (identifier == error_mark_node)
18345 {
18346 parser->scope = NULL_TREE;
18347 return error_mark_node;
18348 }
18349
18350 /* For a `typename', we needn't call xref_tag. */
18351 if (tag_type == typename_type
18352 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
18353 return cp_parser_make_typename_type (parser, identifier,
18354 token->location);
18355
18356 /* Template parameter lists apply only if we are not within a
18357 function parameter list. */
18358 bool template_parm_lists_apply
18359 = parser->num_template_parameter_lists;
18360 if (template_parm_lists_apply)
18361 for (cp_binding_level *s = current_binding_level;
18362 s && s->kind != sk_template_parms;
18363 s = s->level_chain)
18364 if (s->kind == sk_function_parms)
18365 template_parm_lists_apply = false;
18366
18367 /* Look up a qualified name in the usual way. */
18368 if (parser->scope)
18369 {
18370 tree decl;
18371 tree ambiguous_decls;
18372
18373 decl = cp_parser_lookup_name (parser, identifier,
18374 tag_type,
18375 /*is_template=*/false,
18376 /*is_namespace=*/false,
18377 /*check_dependency=*/true,
18378 &ambiguous_decls,
18379 token->location);
18380
18381 /* If the lookup was ambiguous, an error will already have been
18382 issued. */
18383 if (ambiguous_decls)
18384 return error_mark_node;
18385
18386 /* If we are parsing friend declaration, DECL may be a
18387 TEMPLATE_DECL tree node here. However, we need to check
18388 whether this TEMPLATE_DECL results in valid code. Consider
18389 the following example:
18390
18391 namespace N {
18392 template <class T> class C {};
18393 }
18394 class X {
18395 template <class T> friend class N::C; // #1, valid code
18396 };
18397 template <class T> class Y {
18398 friend class N::C; // #2, invalid code
18399 };
18400
18401 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18402 name lookup of `N::C'. We see that friend declaration must
18403 be template for the code to be valid. Note that
18404 processing_template_decl does not work here since it is
18405 always 1 for the above two cases. */
18406
18407 decl = (cp_parser_maybe_treat_template_as_class
18408 (decl, /*tag_name_p=*/is_friend
18409 && template_parm_lists_apply));
18410
18411 if (TREE_CODE (decl) != TYPE_DECL)
18412 {
18413 cp_parser_diagnose_invalid_type_name (parser,
18414 identifier,
18415 token->location);
18416 return error_mark_node;
18417 }
18418
18419 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
18420 {
18421 bool allow_template = (template_parm_lists_apply
18422 || DECL_SELF_REFERENCE_P (decl));
18423 type = check_elaborated_type_specifier (tag_type, decl,
18424 allow_template);
18425
18426 if (type == error_mark_node)
18427 return error_mark_node;
18428 }
18429
18430 /* Forward declarations of nested types, such as
18431
18432 class C1::C2;
18433 class C1::C2::C3;
18434
18435 are invalid unless all components preceding the final '::'
18436 are complete. If all enclosing types are complete, these
18437 declarations become merely pointless.
18438
18439 Invalid forward declarations of nested types are errors
18440 caught elsewhere in parsing. Those that are pointless arrive
18441 here. */
18442
18443 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18444 && !is_friend && !processing_explicit_instantiation)
18445 warning (0, "declaration %qD does not declare anything", decl);
18446
18447 type = TREE_TYPE (decl);
18448 }
18449 else
18450 {
18451 /* An elaborated-type-specifier sometimes introduces a new type and
18452 sometimes names an existing type. Normally, the rule is that it
18453 introduces a new type only if there is not an existing type of
18454 the same name already in scope. For example, given:
18455
18456 struct S {};
18457 void f() { struct S s; }
18458
18459 the `struct S' in the body of `f' is the same `struct S' as in
18460 the global scope; the existing definition is used. However, if
18461 there were no global declaration, this would introduce a new
18462 local class named `S'.
18463
18464 An exception to this rule applies to the following code:
18465
18466 namespace N { struct S; }
18467
18468 Here, the elaborated-type-specifier names a new type
18469 unconditionally; even if there is already an `S' in the
18470 containing scope this declaration names a new type.
18471 This exception only applies if the elaborated-type-specifier
18472 forms the complete declaration:
18473
18474 [class.name]
18475
18476 A declaration consisting solely of `class-key identifier ;' is
18477 either a redeclaration of the name in the current scope or a
18478 forward declaration of the identifier as a class name. It
18479 introduces the name into the current scope.
18480
18481 We are in this situation precisely when the next token is a `;'.
18482
18483 An exception to the exception is that a `friend' declaration does
18484 *not* name a new type; i.e., given:
18485
18486 struct S { friend struct T; };
18487
18488 `T' is not a new type in the scope of `S'.
18489
18490 Also, `new struct S' or `sizeof (struct S)' never results in the
18491 definition of a new type; a new type can only be declared in a
18492 declaration context. */
18493
18494 tag_scope ts;
18495 bool template_p;
18496
18497 if (is_friend)
18498 /* Friends have special name lookup rules. */
18499 ts = ts_within_enclosing_non_class;
18500 else if (is_declaration
18501 && cp_lexer_next_token_is (parser->lexer,
18502 CPP_SEMICOLON))
18503 /* This is a `class-key identifier ;' */
18504 ts = ts_current;
18505 else
18506 ts = ts_global;
18507
18508 template_p =
18509 (template_parm_lists_apply
18510 && (cp_parser_next_token_starts_class_definition_p (parser)
18511 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
18512 /* An unqualified name was used to reference this type, so
18513 there were no qualifying templates. */
18514 if (template_parm_lists_apply
18515 && !cp_parser_check_template_parameters (parser,
18516 /*num_templates=*/0,
18517 /*template_id*/false,
18518 token->location,
18519 /*declarator=*/NULL))
18520 return error_mark_node;
18521 type = xref_tag (tag_type, identifier, ts, template_p);
18522 }
18523 }
18524
18525 if (type == error_mark_node)
18526 return error_mark_node;
18527
18528 /* Allow attributes on forward declarations of classes. */
18529 if (attributes)
18530 {
18531 if (TREE_CODE (type) == TYPENAME_TYPE)
18532 warning (OPT_Wattributes,
18533 "attributes ignored on uninstantiated type");
18534 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
18535 && ! processing_explicit_instantiation)
18536 warning (OPT_Wattributes,
18537 "attributes ignored on template instantiation");
18538 else if (is_declaration && cp_parser_declares_only_class_p (parser))
18539 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
18540 else
18541 warning (OPT_Wattributes,
18542 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18543 }
18544
18545 if (tag_type != enum_type)
18546 {
18547 /* Indicate whether this class was declared as a `class' or as a
18548 `struct'. */
18549 if (CLASS_TYPE_P (type))
18550 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
18551 cp_parser_check_class_key (tag_type, type);
18552 }
18553
18554 /* A "<" cannot follow an elaborated type specifier. If that
18555 happens, the user was probably trying to form a template-id. */
18556 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
18557 token->location);
18558
18559 return type;
18560 }
18561
18562 /* Parse an enum-specifier.
18563
18564 enum-specifier:
18565 enum-head { enumerator-list [opt] }
18566 enum-head { enumerator-list , } [C++0x]
18567
18568 enum-head:
18569 enum-key identifier [opt] enum-base [opt]
18570 enum-key nested-name-specifier identifier enum-base [opt]
18571
18572 enum-key:
18573 enum
18574 enum class [C++0x]
18575 enum struct [C++0x]
18576
18577 enum-base: [C++0x]
18578 : type-specifier-seq
18579
18580 opaque-enum-specifier:
18581 enum-key identifier enum-base [opt] ;
18582
18583 GNU Extensions:
18584 enum-key attributes[opt] identifier [opt] enum-base [opt]
18585 { enumerator-list [opt] }attributes[opt]
18586 enum-key attributes[opt] identifier [opt] enum-base [opt]
18587 { enumerator-list, }attributes[opt] [C++0x]
18588
18589 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18590 if the token stream isn't an enum-specifier after all. */
18591
18592 static tree
18593 cp_parser_enum_specifier (cp_parser* parser)
18594 {
18595 tree identifier;
18596 tree type = NULL_TREE;
18597 tree prev_scope;
18598 tree nested_name_specifier = NULL_TREE;
18599 tree attributes;
18600 bool scoped_enum_p = false;
18601 bool has_underlying_type = false;
18602 bool nested_being_defined = false;
18603 bool new_value_list = false;
18604 bool is_new_type = false;
18605 bool is_unnamed = false;
18606 tree underlying_type = NULL_TREE;
18607 cp_token *type_start_token = NULL;
18608 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18609
18610 parser->colon_corrects_to_scope_p = false;
18611
18612 /* Parse tentatively so that we can back up if we don't find a
18613 enum-specifier. */
18614 cp_parser_parse_tentatively (parser);
18615
18616 /* Caller guarantees that the current token is 'enum', an identifier
18617 possibly follows, and the token after that is an opening brace.
18618 If we don't have an identifier, fabricate an anonymous name for
18619 the enumeration being defined. */
18620 cp_lexer_consume_token (parser->lexer);
18621
18622 /* Parse the "class" or "struct", which indicates a scoped
18623 enumeration type in C++0x. */
18624 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18625 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18626 {
18627 if (cxx_dialect < cxx11)
18628 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18629
18630 /* Consume the `struct' or `class' token. */
18631 cp_lexer_consume_token (parser->lexer);
18632
18633 scoped_enum_p = true;
18634 }
18635
18636 attributes = cp_parser_attributes_opt (parser);
18637
18638 /* Clear the qualification. */
18639 parser->scope = NULL_TREE;
18640 parser->qualifying_scope = NULL_TREE;
18641 parser->object_scope = NULL_TREE;
18642
18643 /* Figure out in what scope the declaration is being placed. */
18644 prev_scope = current_scope ();
18645
18646 type_start_token = cp_lexer_peek_token (parser->lexer);
18647
18648 push_deferring_access_checks (dk_no_check);
18649 nested_name_specifier
18650 = cp_parser_nested_name_specifier_opt (parser,
18651 /*typename_keyword_p=*/true,
18652 /*check_dependency_p=*/false,
18653 /*type_p=*/false,
18654 /*is_declaration=*/false);
18655
18656 if (nested_name_specifier)
18657 {
18658 tree name;
18659
18660 identifier = cp_parser_identifier (parser);
18661 name = cp_parser_lookup_name (parser, identifier,
18662 enum_type,
18663 /*is_template=*/false,
18664 /*is_namespace=*/false,
18665 /*check_dependency=*/true,
18666 /*ambiguous_decls=*/NULL,
18667 input_location);
18668 if (name && name != error_mark_node)
18669 {
18670 type = TREE_TYPE (name);
18671 if (TREE_CODE (type) == TYPENAME_TYPE)
18672 {
18673 /* Are template enums allowed in ISO? */
18674 if (template_parm_scope_p ())
18675 pedwarn (type_start_token->location, OPT_Wpedantic,
18676 "%qD is an enumeration template", name);
18677 /* ignore a typename reference, for it will be solved by name
18678 in start_enum. */
18679 type = NULL_TREE;
18680 }
18681 }
18682 else if (nested_name_specifier == error_mark_node)
18683 /* We already issued an error. */;
18684 else
18685 {
18686 error_at (type_start_token->location,
18687 "%qD does not name an enumeration in %qT",
18688 identifier, nested_name_specifier);
18689 nested_name_specifier = error_mark_node;
18690 }
18691 }
18692 else
18693 {
18694 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18695 identifier = cp_parser_identifier (parser);
18696 else
18697 {
18698 identifier = make_anon_name ();
18699 is_unnamed = true;
18700 if (scoped_enum_p)
18701 error_at (type_start_token->location,
18702 "unnamed scoped enum is not allowed");
18703 }
18704 }
18705 pop_deferring_access_checks ();
18706
18707 /* Check for the `:' that denotes a specified underlying type in C++0x.
18708 Note that a ':' could also indicate a bitfield width, however. */
18709 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18710 {
18711 cp_decl_specifier_seq type_specifiers;
18712
18713 /* Consume the `:'. */
18714 cp_lexer_consume_token (parser->lexer);
18715
18716 /* Parse the type-specifier-seq. */
18717 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
18718 /*is_declaration=*/false,
18719 /*is_trailing_return=*/false,
18720 &type_specifiers);
18721
18722 /* At this point this is surely not elaborated type specifier. */
18723 if (!cp_parser_parse_definitely (parser))
18724 return NULL_TREE;
18725
18726 if (cxx_dialect < cxx11)
18727 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18728
18729 has_underlying_type = true;
18730
18731 /* If that didn't work, stop. */
18732 if (type_specifiers.type != error_mark_node)
18733 {
18734 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18735 /*initialized=*/0, NULL);
18736 if (underlying_type == error_mark_node
18737 || check_for_bare_parameter_packs (underlying_type))
18738 underlying_type = NULL_TREE;
18739 }
18740 }
18741
18742 /* Look for the `{' but don't consume it yet. */
18743 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18744 {
18745 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18746 {
18747 cp_parser_error (parser, "expected %<{%>");
18748 if (has_underlying_type)
18749 {
18750 type = NULL_TREE;
18751 goto out;
18752 }
18753 }
18754 /* An opaque-enum-specifier must have a ';' here. */
18755 if ((scoped_enum_p || underlying_type)
18756 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18757 {
18758 cp_parser_error (parser, "expected %<;%> or %<{%>");
18759 if (has_underlying_type)
18760 {
18761 type = NULL_TREE;
18762 goto out;
18763 }
18764 }
18765 }
18766
18767 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18768 return NULL_TREE;
18769
18770 if (nested_name_specifier)
18771 {
18772 if (CLASS_TYPE_P (nested_name_specifier))
18773 {
18774 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18775 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18776 push_scope (nested_name_specifier);
18777 }
18778 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18779 {
18780 push_nested_namespace (nested_name_specifier);
18781 }
18782 }
18783
18784 /* Issue an error message if type-definitions are forbidden here. */
18785 if (!cp_parser_check_type_definition (parser))
18786 type = error_mark_node;
18787 else
18788 /* Create the new type. We do this before consuming the opening
18789 brace so the enum will be recorded as being on the line of its
18790 tag (or the 'enum' keyword, if there is no tag). */
18791 type = start_enum (identifier, type, underlying_type,
18792 attributes, scoped_enum_p, &is_new_type);
18793
18794 /* If the next token is not '{' it is an opaque-enum-specifier or an
18795 elaborated-type-specifier. */
18796 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18797 {
18798 timevar_push (TV_PARSE_ENUM);
18799 if (nested_name_specifier
18800 && nested_name_specifier != error_mark_node)
18801 {
18802 /* The following catches invalid code such as:
18803 enum class S<int>::E { A, B, C }; */
18804 if (!processing_specialization
18805 && CLASS_TYPE_P (nested_name_specifier)
18806 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18807 error_at (type_start_token->location, "cannot add an enumerator "
18808 "list to a template instantiation");
18809
18810 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18811 {
18812 error_at (type_start_token->location,
18813 "%<%T::%E%> has not been declared",
18814 TYPE_CONTEXT (nested_name_specifier),
18815 nested_name_specifier);
18816 type = error_mark_node;
18817 }
18818 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18819 && !CLASS_TYPE_P (nested_name_specifier))
18820 {
18821 error_at (type_start_token->location, "nested name specifier "
18822 "%qT for enum declaration does not name a class "
18823 "or namespace", nested_name_specifier);
18824 type = error_mark_node;
18825 }
18826 /* If that scope does not contain the scope in which the
18827 class was originally declared, the program is invalid. */
18828 else if (prev_scope && !is_ancestor (prev_scope,
18829 nested_name_specifier))
18830 {
18831 if (at_namespace_scope_p ())
18832 error_at (type_start_token->location,
18833 "declaration of %qD in namespace %qD which does not "
18834 "enclose %qD",
18835 type, prev_scope, nested_name_specifier);
18836 else
18837 error_at (type_start_token->location,
18838 "declaration of %qD in %qD which does not "
18839 "enclose %qD",
18840 type, prev_scope, nested_name_specifier);
18841 type = error_mark_node;
18842 }
18843 /* If that scope is the scope where the declaration is being placed
18844 the program is invalid. */
18845 else if (CLASS_TYPE_P (nested_name_specifier)
18846 && CLASS_TYPE_P (prev_scope)
18847 && same_type_p (nested_name_specifier, prev_scope))
18848 {
18849 permerror (type_start_token->location,
18850 "extra qualification not allowed");
18851 nested_name_specifier = NULL_TREE;
18852 }
18853 }
18854
18855 if (scoped_enum_p)
18856 begin_scope (sk_scoped_enum, type);
18857
18858 /* Consume the opening brace. */
18859 matching_braces braces;
18860 braces.consume_open (parser);
18861
18862 if (type == error_mark_node)
18863 ; /* Nothing to add */
18864 else if (OPAQUE_ENUM_P (type)
18865 || (cxx_dialect > cxx98 && processing_specialization))
18866 {
18867 new_value_list = true;
18868 SET_OPAQUE_ENUM_P (type, false);
18869 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18870 }
18871 else
18872 {
18873 error_at (type_start_token->location,
18874 "multiple definition of %q#T", type);
18875 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18876 "previous definition here");
18877 type = error_mark_node;
18878 }
18879
18880 if (type == error_mark_node)
18881 cp_parser_skip_to_end_of_block_or_statement (parser);
18882 /* If the next token is not '}', then there are some enumerators. */
18883 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18884 {
18885 if (is_unnamed && !scoped_enum_p)
18886 pedwarn (type_start_token->location, OPT_Wpedantic,
18887 "ISO C++ forbids empty unnamed enum");
18888 }
18889 else
18890 cp_parser_enumerator_list (parser, type);
18891
18892 /* Consume the final '}'. */
18893 braces.require_close (parser);
18894
18895 if (scoped_enum_p)
18896 finish_scope ();
18897 timevar_pop (TV_PARSE_ENUM);
18898 }
18899 else
18900 {
18901 /* If a ';' follows, then it is an opaque-enum-specifier
18902 and additional restrictions apply. */
18903 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18904 {
18905 if (is_unnamed)
18906 error_at (type_start_token->location,
18907 "opaque-enum-specifier without name");
18908 else if (nested_name_specifier)
18909 error_at (type_start_token->location,
18910 "opaque-enum-specifier must use a simple identifier");
18911 }
18912 }
18913
18914 /* Look for trailing attributes to apply to this enumeration, and
18915 apply them if appropriate. */
18916 if (cp_parser_allow_gnu_extensions_p (parser))
18917 {
18918 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18919 cplus_decl_attributes (&type,
18920 trailing_attr,
18921 (int) ATTR_FLAG_TYPE_IN_PLACE);
18922 }
18923
18924 /* Finish up the enumeration. */
18925 if (type != error_mark_node)
18926 {
18927 if (new_value_list)
18928 finish_enum_value_list (type);
18929 if (is_new_type)
18930 finish_enum (type);
18931 }
18932
18933 if (nested_name_specifier)
18934 {
18935 if (CLASS_TYPE_P (nested_name_specifier))
18936 {
18937 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18938 pop_scope (nested_name_specifier);
18939 }
18940 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18941 {
18942 pop_nested_namespace (nested_name_specifier);
18943 }
18944 }
18945 out:
18946 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18947 return type;
18948 }
18949
18950 /* Parse an enumerator-list. The enumerators all have the indicated
18951 TYPE.
18952
18953 enumerator-list:
18954 enumerator-definition
18955 enumerator-list , enumerator-definition */
18956
18957 static void
18958 cp_parser_enumerator_list (cp_parser* parser, tree type)
18959 {
18960 while (true)
18961 {
18962 /* Parse an enumerator-definition. */
18963 cp_parser_enumerator_definition (parser, type);
18964
18965 /* If the next token is not a ',', we've reached the end of
18966 the list. */
18967 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18968 break;
18969 /* Otherwise, consume the `,' and keep going. */
18970 cp_lexer_consume_token (parser->lexer);
18971 /* If the next token is a `}', there is a trailing comma. */
18972 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18973 {
18974 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18975 pedwarn (input_location, OPT_Wpedantic,
18976 "comma at end of enumerator list");
18977 break;
18978 }
18979 }
18980 }
18981
18982 /* Parse an enumerator-definition. The enumerator has the indicated
18983 TYPE.
18984
18985 enumerator-definition:
18986 enumerator
18987 enumerator = constant-expression
18988
18989 enumerator:
18990 identifier
18991
18992 GNU Extensions:
18993
18994 enumerator-definition:
18995 enumerator attributes [opt]
18996 enumerator attributes [opt] = constant-expression */
18997
18998 static void
18999 cp_parser_enumerator_definition (cp_parser* parser, tree type)
19000 {
19001 tree identifier;
19002 tree value;
19003 location_t loc;
19004
19005 /* Save the input location because we are interested in the location
19006 of the identifier and not the location of the explicit value. */
19007 loc = cp_lexer_peek_token (parser->lexer)->location;
19008
19009 /* Look for the identifier. */
19010 identifier = cp_parser_identifier (parser);
19011 if (identifier == error_mark_node)
19012 return;
19013
19014 /* Parse any specified attributes. */
19015 tree attrs = cp_parser_attributes_opt (parser);
19016
19017 /* If the next token is an '=', then there is an explicit value. */
19018 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19019 {
19020 /* Consume the `=' token. */
19021 cp_lexer_consume_token (parser->lexer);
19022 /* Parse the value. */
19023 value = cp_parser_constant_expression (parser);
19024 }
19025 else
19026 value = NULL_TREE;
19027
19028 /* If we are processing a template, make sure the initializer of the
19029 enumerator doesn't contain any bare template parameter pack. */
19030 if (check_for_bare_parameter_packs (value))
19031 value = error_mark_node;
19032
19033 /* Create the enumerator. */
19034 build_enumerator (identifier, value, type, attrs, loc);
19035 }
19036
19037 /* Parse a namespace-name.
19038
19039 namespace-name:
19040 original-namespace-name
19041 namespace-alias
19042
19043 Returns the NAMESPACE_DECL for the namespace. */
19044
19045 static tree
19046 cp_parser_namespace_name (cp_parser* parser)
19047 {
19048 tree identifier;
19049 tree namespace_decl;
19050
19051 cp_token *token = cp_lexer_peek_token (parser->lexer);
19052
19053 /* Get the name of the namespace. */
19054 identifier = cp_parser_identifier (parser);
19055 if (identifier == error_mark_node)
19056 return error_mark_node;
19057
19058 /* Look up the identifier in the currently active scope. Look only
19059 for namespaces, due to:
19060
19061 [basic.lookup.udir]
19062
19063 When looking up a namespace-name in a using-directive or alias
19064 definition, only namespace names are considered.
19065
19066 And:
19067
19068 [basic.lookup.qual]
19069
19070 During the lookup of a name preceding the :: scope resolution
19071 operator, object, function, and enumerator names are ignored.
19072
19073 (Note that cp_parser_qualifying_entity only calls this
19074 function if the token after the name is the scope resolution
19075 operator.) */
19076 namespace_decl = cp_parser_lookup_name (parser, identifier,
19077 none_type,
19078 /*is_template=*/false,
19079 /*is_namespace=*/true,
19080 /*check_dependency=*/true,
19081 /*ambiguous_decls=*/NULL,
19082 token->location);
19083 /* If it's not a namespace, issue an error. */
19084 if (namespace_decl == error_mark_node
19085 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
19086 {
19087 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19088 {
19089 auto_diagnostic_group d;
19090 name_hint hint;
19091 if (namespace_decl == error_mark_node
19092 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
19093 hint = suggest_alternative_in_explicit_scope (token->location,
19094 identifier,
19095 parser->scope);
19096 if (const char *suggestion = hint.suggestion ())
19097 {
19098 gcc_rich_location richloc (token->location);
19099 richloc.add_fixit_replace (suggestion);
19100 error_at (&richloc,
19101 "%qD is not a namespace-name; did you mean %qs?",
19102 identifier, suggestion);
19103 }
19104 else
19105 error_at (token->location, "%qD is not a namespace-name",
19106 identifier);
19107 }
19108 else
19109 cp_parser_error (parser, "expected namespace-name");
19110 namespace_decl = error_mark_node;
19111 }
19112
19113 return namespace_decl;
19114 }
19115
19116 /* Parse a namespace-definition.
19117
19118 namespace-definition:
19119 named-namespace-definition
19120 unnamed-namespace-definition
19121
19122 named-namespace-definition:
19123 original-namespace-definition
19124 extension-namespace-definition
19125
19126 original-namespace-definition:
19127 namespace identifier { namespace-body }
19128
19129 extension-namespace-definition:
19130 namespace original-namespace-name { namespace-body }
19131
19132 unnamed-namespace-definition:
19133 namespace { namespace-body } */
19134
19135 static void
19136 cp_parser_namespace_definition (cp_parser* parser)
19137 {
19138 tree identifier;
19139 int nested_definition_count = 0;
19140
19141 cp_ensure_no_omp_declare_simd (parser);
19142 cp_ensure_no_oacc_routine (parser);
19143
19144 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
19145 const bool topmost_inline_p = is_inline;
19146
19147 if (is_inline)
19148 {
19149 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
19150 cp_lexer_consume_token (parser->lexer);
19151 }
19152
19153 /* Look for the `namespace' keyword. */
19154 cp_token* token
19155 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19156
19157 /* Parse any specified attributes before the identifier. */
19158 tree attribs = cp_parser_attributes_opt (parser);
19159
19160 for (;;)
19161 {
19162 identifier = NULL_TREE;
19163
19164 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
19165 RID_INLINE);
19166 if (nested_inline_p && nested_definition_count != 0)
19167 {
19168 if (cxx_dialect < cxx2a)
19169 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
19170 OPT_Wpedantic, "nested inline namespace definitions only "
19171 "available with -std=c++2a or -std=gnu++2a");
19172 cp_lexer_consume_token (parser->lexer);
19173 }
19174
19175 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19176 {
19177 identifier = cp_parser_identifier (parser);
19178
19179 if (cp_next_tokens_can_be_std_attribute_p (parser))
19180 pedwarn (input_location, OPT_Wpedantic,
19181 "standard attributes on namespaces must precede "
19182 "the namespace name");
19183
19184 /* Parse any attributes specified after the identifier. */
19185 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
19186 }
19187
19188 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19189 {
19190 /* Don't forget that the innermost namespace might have been
19191 marked as inline. Use |= because we cannot overwrite
19192 IS_INLINE in case the outermost namespace is inline, but
19193 there are no nested inlines. */
19194 is_inline |= nested_inline_p;
19195 break;
19196 }
19197
19198 if (!nested_definition_count && cxx_dialect < cxx17)
19199 pedwarn (input_location, OPT_Wpedantic,
19200 "nested namespace definitions only available with "
19201 "-std=c++17 or -std=gnu++17");
19202
19203 /* Nested namespace names can create new namespaces (unlike
19204 other qualified-ids). */
19205 if (int count = (identifier
19206 ? push_namespace (identifier, nested_inline_p)
19207 : 0))
19208 nested_definition_count += count;
19209 else
19210 cp_parser_error (parser, "nested namespace name required");
19211 cp_lexer_consume_token (parser->lexer);
19212 }
19213
19214 if (nested_definition_count && !identifier)
19215 cp_parser_error (parser, "namespace name required");
19216
19217 if (nested_definition_count && attribs)
19218 error_at (token->location,
19219 "a nested namespace definition cannot have attributes");
19220 if (nested_definition_count && topmost_inline_p)
19221 error_at (token->location,
19222 "a nested namespace definition cannot be inline");
19223
19224 /* Start the namespace. */
19225 nested_definition_count += push_namespace (identifier, is_inline);
19226
19227 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
19228
19229 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
19230
19231 /* Look for the `{' to validate starting the namespace. */
19232 matching_braces braces;
19233 if (braces.require_open (parser))
19234 {
19235 /* Parse the body of the namespace. */
19236 cp_parser_namespace_body (parser);
19237
19238 /* Look for the final `}'. */
19239 braces.require_close (parser);
19240 }
19241
19242 if (has_visibility)
19243 pop_visibility (1);
19244
19245 /* Pop the nested namespace definitions. */
19246 while (nested_definition_count--)
19247 pop_namespace ();
19248 }
19249
19250 /* Parse a namespace-body.
19251
19252 namespace-body:
19253 declaration-seq [opt] */
19254
19255 static void
19256 cp_parser_namespace_body (cp_parser* parser)
19257 {
19258 cp_parser_declaration_seq_opt (parser);
19259 }
19260
19261 /* Parse a namespace-alias-definition.
19262
19263 namespace-alias-definition:
19264 namespace identifier = qualified-namespace-specifier ; */
19265
19266 static void
19267 cp_parser_namespace_alias_definition (cp_parser* parser)
19268 {
19269 tree identifier;
19270 tree namespace_specifier;
19271
19272 cp_token *token = cp_lexer_peek_token (parser->lexer);
19273
19274 /* Look for the `namespace' keyword. */
19275 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19276 /* Look for the identifier. */
19277 identifier = cp_parser_identifier (parser);
19278 if (identifier == error_mark_node)
19279 return;
19280 /* Look for the `=' token. */
19281 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
19282 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19283 {
19284 error_at (token->location, "%<namespace%> definition is not allowed here");
19285 /* Skip the definition. */
19286 cp_lexer_consume_token (parser->lexer);
19287 if (cp_parser_skip_to_closing_brace (parser))
19288 cp_lexer_consume_token (parser->lexer);
19289 return;
19290 }
19291 cp_parser_require (parser, CPP_EQ, RT_EQ);
19292 /* Look for the qualified-namespace-specifier. */
19293 namespace_specifier
19294 = cp_parser_qualified_namespace_specifier (parser);
19295 /* Look for the `;' token. */
19296 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19297
19298 /* Register the alias in the symbol table. */
19299 do_namespace_alias (identifier, namespace_specifier);
19300 }
19301
19302 /* Parse a qualified-namespace-specifier.
19303
19304 qualified-namespace-specifier:
19305 :: [opt] nested-name-specifier [opt] namespace-name
19306
19307 Returns a NAMESPACE_DECL corresponding to the specified
19308 namespace. */
19309
19310 static tree
19311 cp_parser_qualified_namespace_specifier (cp_parser* parser)
19312 {
19313 /* Look for the optional `::'. */
19314 cp_parser_global_scope_opt (parser,
19315 /*current_scope_valid_p=*/false);
19316
19317 /* Look for the optional nested-name-specifier. */
19318 cp_parser_nested_name_specifier_opt (parser,
19319 /*typename_keyword_p=*/false,
19320 /*check_dependency_p=*/true,
19321 /*type_p=*/false,
19322 /*is_declaration=*/true);
19323
19324 return cp_parser_namespace_name (parser);
19325 }
19326
19327 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19328 access declaration.
19329
19330 using-declaration:
19331 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19332 using :: unqualified-id ;
19333
19334 access-declaration:
19335 qualified-id ;
19336
19337 */
19338
19339 static bool
19340 cp_parser_using_declaration (cp_parser* parser,
19341 bool access_declaration_p)
19342 {
19343 cp_token *token;
19344 bool typename_p = false;
19345 bool global_scope_p;
19346 tree decl;
19347 tree identifier;
19348 tree qscope;
19349 int oldcount = errorcount;
19350 cp_token *diag_token = NULL;
19351
19352 if (access_declaration_p)
19353 {
19354 diag_token = cp_lexer_peek_token (parser->lexer);
19355 cp_parser_parse_tentatively (parser);
19356 }
19357 else
19358 {
19359 /* Look for the `using' keyword. */
19360 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19361
19362 again:
19363 /* Peek at the next token. */
19364 token = cp_lexer_peek_token (parser->lexer);
19365 /* See if it's `typename'. */
19366 if (token->keyword == RID_TYPENAME)
19367 {
19368 /* Remember that we've seen it. */
19369 typename_p = true;
19370 /* Consume the `typename' token. */
19371 cp_lexer_consume_token (parser->lexer);
19372 }
19373 }
19374
19375 /* Look for the optional global scope qualification. */
19376 global_scope_p
19377 = (cp_parser_global_scope_opt (parser,
19378 /*current_scope_valid_p=*/false)
19379 != NULL_TREE);
19380
19381 /* If we saw `typename', or didn't see `::', then there must be a
19382 nested-name-specifier present. */
19383 if (typename_p || !global_scope_p)
19384 {
19385 qscope = cp_parser_nested_name_specifier (parser, typename_p,
19386 /*check_dependency_p=*/true,
19387 /*type_p=*/false,
19388 /*is_declaration=*/true);
19389 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
19390 {
19391 cp_parser_skip_to_end_of_block_or_statement (parser);
19392 return false;
19393 }
19394 }
19395 /* Otherwise, we could be in either of the two productions. In that
19396 case, treat the nested-name-specifier as optional. */
19397 else
19398 qscope = cp_parser_nested_name_specifier_opt (parser,
19399 /*typename_keyword_p=*/false,
19400 /*check_dependency_p=*/true,
19401 /*type_p=*/false,
19402 /*is_declaration=*/true);
19403 if (!qscope)
19404 qscope = global_namespace;
19405 else if (UNSCOPED_ENUM_P (qscope))
19406 qscope = CP_TYPE_CONTEXT (qscope);
19407
19408 if (access_declaration_p && cp_parser_error_occurred (parser))
19409 /* Something has already gone wrong; there's no need to parse
19410 further. Since an error has occurred, the return value of
19411 cp_parser_parse_definitely will be false, as required. */
19412 return cp_parser_parse_definitely (parser);
19413
19414 token = cp_lexer_peek_token (parser->lexer);
19415 /* Parse the unqualified-id. */
19416 identifier = cp_parser_unqualified_id (parser,
19417 /*template_keyword_p=*/false,
19418 /*check_dependency_p=*/true,
19419 /*declarator_p=*/true,
19420 /*optional_p=*/false);
19421
19422 if (access_declaration_p)
19423 {
19424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19425 cp_parser_simulate_error (parser);
19426 if (!cp_parser_parse_definitely (parser))
19427 return false;
19428 }
19429 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19430 {
19431 cp_token *ell = cp_lexer_consume_token (parser->lexer);
19432 if (cxx_dialect < cxx17
19433 && !in_system_header_at (ell->location))
19434 pedwarn (ell->location, 0,
19435 "pack expansion in using-declaration only available "
19436 "with -std=c++17 or -std=gnu++17");
19437 qscope = make_pack_expansion (qscope);
19438 }
19439
19440 /* The function we call to handle a using-declaration is different
19441 depending on what scope we are in. */
19442 if (qscope == error_mark_node || identifier == error_mark_node)
19443 ;
19444 else if (!identifier_p (identifier)
19445 && TREE_CODE (identifier) != BIT_NOT_EXPR)
19446 /* [namespace.udecl]
19447
19448 A using declaration shall not name a template-id. */
19449 error_at (token->location,
19450 "a template-id may not appear in a using-declaration");
19451 else
19452 {
19453 if (at_class_scope_p ())
19454 {
19455 /* Create the USING_DECL. */
19456 decl = do_class_using_decl (qscope, identifier);
19457
19458 if (decl && typename_p)
19459 USING_DECL_TYPENAME_P (decl) = 1;
19460
19461 if (check_for_bare_parameter_packs (decl))
19462 {
19463 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19464 return false;
19465 }
19466 else
19467 /* Add it to the list of members in this class. */
19468 finish_member_declaration (decl);
19469 }
19470 else
19471 {
19472 decl = cp_parser_lookup_name_simple (parser,
19473 identifier,
19474 token->location);
19475 if (decl == error_mark_node)
19476 cp_parser_name_lookup_error (parser, identifier,
19477 decl, NLE_NULL,
19478 token->location);
19479 else if (check_for_bare_parameter_packs (decl))
19480 {
19481 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19482 return false;
19483 }
19484 else if (!at_namespace_scope_p ())
19485 finish_local_using_decl (decl, qscope, identifier);
19486 else
19487 finish_namespace_using_decl (decl, qscope, identifier);
19488 }
19489 }
19490
19491 if (!access_declaration_p
19492 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19493 {
19494 cp_token *comma = cp_lexer_consume_token (parser->lexer);
19495 if (cxx_dialect < cxx17)
19496 pedwarn (comma->location, 0,
19497 "comma-separated list in using-declaration only available "
19498 "with -std=c++17 or -std=gnu++17");
19499 goto again;
19500 }
19501
19502 /* Look for the final `;'. */
19503 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19504
19505 if (access_declaration_p && errorcount == oldcount)
19506 warning_at (diag_token->location, OPT_Wdeprecated,
19507 "access declarations are deprecated "
19508 "in favour of using-declarations; "
19509 "suggestion: add the %<using%> keyword");
19510
19511 return true;
19512 }
19513
19514 /* Parse an alias-declaration.
19515
19516 alias-declaration:
19517 using identifier attribute-specifier-seq [opt] = type-id */
19518
19519 static tree
19520 cp_parser_alias_declaration (cp_parser* parser)
19521 {
19522 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
19523 location_t id_location, type_location;
19524 cp_declarator *declarator;
19525 cp_decl_specifier_seq decl_specs;
19526 bool member_p;
19527 const char *saved_message = NULL;
19528
19529 /* Look for the `using' keyword. */
19530 cp_token *using_token
19531 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
19532 if (using_token == NULL)
19533 return error_mark_node;
19534
19535 id_location = cp_lexer_peek_token (parser->lexer)->location;
19536 id = cp_parser_identifier (parser);
19537 if (id == error_mark_node)
19538 return error_mark_node;
19539
19540 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
19541 attributes = cp_parser_attributes_opt (parser);
19542 if (attributes == error_mark_node)
19543 return error_mark_node;
19544
19545 cp_parser_require (parser, CPP_EQ, RT_EQ);
19546
19547 if (cp_parser_error_occurred (parser))
19548 return error_mark_node;
19549
19550 cp_parser_commit_to_tentative_parse (parser);
19551
19552 /* Now we are going to parse the type-id of the declaration. */
19553
19554 /*
19555 [dcl.type]/3 says:
19556
19557 "A type-specifier-seq shall not define a class or enumeration
19558 unless it appears in the type-id of an alias-declaration (7.1.3) that
19559 is not the declaration of a template-declaration."
19560
19561 In other words, if we currently are in an alias template, the
19562 type-id should not define a type.
19563
19564 So let's set parser->type_definition_forbidden_message in that
19565 case; cp_parser_check_type_definition (called by
19566 cp_parser_class_specifier) will then emit an error if a type is
19567 defined in the type-id. */
19568 if (parser->num_template_parameter_lists)
19569 {
19570 saved_message = parser->type_definition_forbidden_message;
19571 parser->type_definition_forbidden_message =
19572 G_("types may not be defined in alias template declarations");
19573 }
19574
19575 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
19576 &type_location);
19577
19578 /* Restore the error message if need be. */
19579 if (parser->num_template_parameter_lists)
19580 parser->type_definition_forbidden_message = saved_message;
19581
19582 if (type == error_mark_node
19583 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
19584 {
19585 cp_parser_skip_to_end_of_block_or_statement (parser);
19586 return error_mark_node;
19587 }
19588
19589 /* A typedef-name can also be introduced by an alias-declaration. The
19590 identifier following the using keyword becomes a typedef-name. It has
19591 the same semantics as if it were introduced by the typedef
19592 specifier. In particular, it does not define a new type and it shall
19593 not appear in the type-id. */
19594
19595 clear_decl_specs (&decl_specs);
19596 decl_specs.type = type;
19597 if (attributes != NULL_TREE)
19598 {
19599 decl_specs.attributes = attributes;
19600 set_and_check_decl_spec_loc (&decl_specs,
19601 ds_attribute,
19602 attrs_token);
19603 }
19604 set_and_check_decl_spec_loc (&decl_specs,
19605 ds_typedef,
19606 using_token);
19607 set_and_check_decl_spec_loc (&decl_specs,
19608 ds_alias,
19609 using_token);
19610 decl_specs.locations[ds_type_spec] = type_location;
19611
19612 if (parser->num_template_parameter_lists
19613 && !cp_parser_check_template_parameters (parser,
19614 /*num_templates=*/0,
19615 /*template_id*/false,
19616 id_location,
19617 /*declarator=*/NULL))
19618 return error_mark_node;
19619
19620 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
19621
19622 member_p = at_class_scope_p ();
19623 if (member_p)
19624 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
19625 NULL_TREE, attributes);
19626 else
19627 decl = start_decl (declarator, &decl_specs, 0,
19628 attributes, NULL_TREE, &pushed_scope);
19629 if (decl == error_mark_node)
19630 return decl;
19631
19632 // Attach constraints to the alias declaration.
19633 if (flag_concepts && current_template_parms)
19634 {
19635 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19636 tree constr = build_constraints (reqs, NULL_TREE);
19637 set_constraints (decl, constr);
19638 }
19639
19640 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19641
19642 if (pushed_scope)
19643 pop_scope (pushed_scope);
19644
19645 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19646 added into the symbol table; otherwise, return the TYPE_DECL. */
19647 if (DECL_LANG_SPECIFIC (decl)
19648 && DECL_TEMPLATE_INFO (decl)
19649 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19650 {
19651 decl = DECL_TI_TEMPLATE (decl);
19652 if (member_p)
19653 check_member_template (decl);
19654 }
19655
19656 return decl;
19657 }
19658
19659 /* Parse a using-directive.
19660
19661 using-directive:
19662 using namespace :: [opt] nested-name-specifier [opt]
19663 namespace-name ; */
19664
19665 static void
19666 cp_parser_using_directive (cp_parser* parser)
19667 {
19668 tree namespace_decl;
19669 tree attribs;
19670
19671 /* Look for the `using' keyword. */
19672 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19673 /* And the `namespace' keyword. */
19674 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19675 /* Look for the optional `::' operator. */
19676 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19677 /* And the optional nested-name-specifier. */
19678 cp_parser_nested_name_specifier_opt (parser,
19679 /*typename_keyword_p=*/false,
19680 /*check_dependency_p=*/true,
19681 /*type_p=*/false,
19682 /*is_declaration=*/true);
19683 /* Get the namespace being used. */
19684 namespace_decl = cp_parser_namespace_name (parser);
19685 /* And any specified attributes. */
19686 attribs = cp_parser_attributes_opt (parser);
19687
19688 /* Update the symbol table. */
19689 if (namespace_bindings_p ())
19690 finish_namespace_using_directive (namespace_decl, attribs);
19691 else
19692 finish_local_using_directive (namespace_decl, attribs);
19693
19694 /* Look for the final `;'. */
19695 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19696 }
19697
19698 /* Parse an asm-definition.
19699
19700 asm-qualifier:
19701 volatile
19702 inline
19703 goto
19704
19705 asm-qualifier-list:
19706 asm-qualifier
19707 asm-qualifier-list asm-qualifier
19708
19709 asm-definition:
19710 asm ( string-literal ) ;
19711
19712 GNU Extension:
19713
19714 asm-definition:
19715 asm asm-qualifier-list [opt] ( string-literal ) ;
19716 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19717 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19718 : asm-operand-list [opt] ) ;
19719 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19720 : asm-operand-list [opt]
19721 : asm-clobber-list [opt] ) ;
19722 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19723 : asm-clobber-list [opt]
19724 : asm-goto-list ) ;
19725
19726 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19727 contains goto, and is the only allowed form in that case. No duplicates are
19728 allowed in an asm-qualifier-list. */
19729
19730 static void
19731 cp_parser_asm_definition (cp_parser* parser)
19732 {
19733 tree string;
19734 tree outputs = NULL_TREE;
19735 tree inputs = NULL_TREE;
19736 tree clobbers = NULL_TREE;
19737 tree labels = NULL_TREE;
19738 tree asm_stmt;
19739 bool extended_p = false;
19740 bool invalid_inputs_p = false;
19741 bool invalid_outputs_p = false;
19742 required_token missing = RT_NONE;
19743
19744 /* Look for the `asm' keyword. */
19745 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19746
19747 if (parser->in_function_body
19748 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19749 {
19750 error ("%<asm%> in %<constexpr%> function");
19751 cp_function_chain->invalid_constexpr = true;
19752 }
19753
19754 /* Handle the asm-qualifier-list. */
19755 location_t volatile_loc = UNKNOWN_LOCATION;
19756 location_t inline_loc = UNKNOWN_LOCATION;
19757 location_t goto_loc = UNKNOWN_LOCATION;
19758
19759 if (cp_parser_allow_gnu_extensions_p (parser) && parser->in_function_body)
19760 for (;;)
19761 {
19762 cp_token *token = cp_lexer_peek_token (parser->lexer);
19763 location_t loc = token->location;
19764 switch (cp_lexer_peek_token (parser->lexer)->keyword)
19765 {
19766 case RID_VOLATILE:
19767 if (volatile_loc)
19768 {
19769 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19770 inform (volatile_loc, "first seen here");
19771 }
19772 else
19773 volatile_loc = loc;
19774 cp_lexer_consume_token (parser->lexer);
19775 continue;
19776
19777 case RID_INLINE:
19778 if (inline_loc)
19779 {
19780 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19781 inform (inline_loc, "first seen here");
19782 }
19783 else
19784 inline_loc = loc;
19785 cp_lexer_consume_token (parser->lexer);
19786 continue;
19787
19788 case RID_GOTO:
19789 if (goto_loc)
19790 {
19791 error_at (loc, "duplicate asm qualifier %qT", token->u.value);
19792 inform (goto_loc, "first seen here");
19793 }
19794 else
19795 goto_loc = loc;
19796 cp_lexer_consume_token (parser->lexer);
19797 continue;
19798
19799 case RID_CONST:
19800 case RID_RESTRICT:
19801 error_at (loc, "%qT is not an asm qualifier", token->u.value);
19802 cp_lexer_consume_token (parser->lexer);
19803 continue;
19804
19805 default:
19806 break;
19807 }
19808 break;
19809 }
19810
19811 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
19812 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
19813 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
19814
19815 /* Look for the opening `('. */
19816 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19817 return;
19818 /* Look for the string. */
19819 string = cp_parser_string_literal (parser, false, false);
19820 if (string == error_mark_node)
19821 {
19822 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19823 /*consume_paren=*/true);
19824 return;
19825 }
19826
19827 /* If we're allowing GNU extensions, check for the extended assembly
19828 syntax. Unfortunately, the `:' tokens need not be separated by
19829 a space in C, and so, for compatibility, we tolerate that here
19830 too. Doing that means that we have to treat the `::' operator as
19831 two `:' tokens. */
19832 if (cp_parser_allow_gnu_extensions_p (parser)
19833 && parser->in_function_body
19834 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19835 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19836 {
19837 bool inputs_p = false;
19838 bool clobbers_p = false;
19839 bool labels_p = false;
19840
19841 /* The extended syntax was used. */
19842 extended_p = true;
19843
19844 /* Look for outputs. */
19845 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19846 {
19847 /* Consume the `:'. */
19848 cp_lexer_consume_token (parser->lexer);
19849 /* Parse the output-operands. */
19850 if (cp_lexer_next_token_is_not (parser->lexer,
19851 CPP_COLON)
19852 && cp_lexer_next_token_is_not (parser->lexer,
19853 CPP_SCOPE)
19854 && cp_lexer_next_token_is_not (parser->lexer,
19855 CPP_CLOSE_PAREN)
19856 && !goto_p)
19857 {
19858 outputs = cp_parser_asm_operand_list (parser);
19859 if (outputs == error_mark_node)
19860 invalid_outputs_p = true;
19861 }
19862 }
19863 /* If the next token is `::', there are no outputs, and the
19864 next token is the beginning of the inputs. */
19865 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19866 /* The inputs are coming next. */
19867 inputs_p = true;
19868
19869 /* Look for inputs. */
19870 if (inputs_p
19871 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19872 {
19873 /* Consume the `:' or `::'. */
19874 cp_lexer_consume_token (parser->lexer);
19875 /* Parse the output-operands. */
19876 if (cp_lexer_next_token_is_not (parser->lexer,
19877 CPP_COLON)
19878 && cp_lexer_next_token_is_not (parser->lexer,
19879 CPP_SCOPE)
19880 && cp_lexer_next_token_is_not (parser->lexer,
19881 CPP_CLOSE_PAREN))
19882 {
19883 inputs = cp_parser_asm_operand_list (parser);
19884 if (inputs == error_mark_node)
19885 invalid_inputs_p = true;
19886 }
19887 }
19888 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19889 /* The clobbers are coming next. */
19890 clobbers_p = true;
19891
19892 /* Look for clobbers. */
19893 if (clobbers_p
19894 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19895 {
19896 clobbers_p = true;
19897 /* Consume the `:' or `::'. */
19898 cp_lexer_consume_token (parser->lexer);
19899 /* Parse the clobbers. */
19900 if (cp_lexer_next_token_is_not (parser->lexer,
19901 CPP_COLON)
19902 && cp_lexer_next_token_is_not (parser->lexer,
19903 CPP_CLOSE_PAREN))
19904 clobbers = cp_parser_asm_clobber_list (parser);
19905 }
19906 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19907 /* The labels are coming next. */
19908 labels_p = true;
19909
19910 /* Look for labels. */
19911 if (labels_p
19912 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19913 {
19914 labels_p = true;
19915 /* Consume the `:' or `::'. */
19916 cp_lexer_consume_token (parser->lexer);
19917 /* Parse the labels. */
19918 labels = cp_parser_asm_label_list (parser);
19919 }
19920
19921 if (goto_p && !labels_p)
19922 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19923 }
19924 else if (goto_p)
19925 missing = RT_COLON_SCOPE;
19926
19927 /* Look for the closing `)'. */
19928 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19929 missing ? missing : RT_CLOSE_PAREN))
19930 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19931 /*consume_paren=*/true);
19932 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19933
19934 if (!invalid_inputs_p && !invalid_outputs_p)
19935 {
19936 /* Create the ASM_EXPR. */
19937 if (parser->in_function_body)
19938 {
19939 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19940 inputs, clobbers, labels, inline_p);
19941 /* If the extended syntax was not used, mark the ASM_EXPR. */
19942 if (!extended_p)
19943 {
19944 tree temp = asm_stmt;
19945 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19946 temp = TREE_OPERAND (temp, 0);
19947
19948 ASM_INPUT_P (temp) = 1;
19949 }
19950 }
19951 else
19952 symtab->finalize_toplevel_asm (string);
19953 }
19954 }
19955
19956 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19957 type that comes from the decl-specifier-seq. */
19958
19959 static tree
19960 strip_declarator_types (tree type, cp_declarator *declarator)
19961 {
19962 for (cp_declarator *d = declarator; d;)
19963 switch (d->kind)
19964 {
19965 case cdk_id:
19966 case cdk_decomp:
19967 case cdk_error:
19968 d = NULL;
19969 break;
19970
19971 default:
19972 if (TYPE_PTRMEMFUNC_P (type))
19973 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19974 type = TREE_TYPE (type);
19975 d = d->declarator;
19976 break;
19977 }
19978
19979 return type;
19980 }
19981
19982 /* Declarators [gram.dcl.decl] */
19983
19984 /* Parse an init-declarator.
19985
19986 init-declarator:
19987 declarator initializer [opt]
19988
19989 GNU Extension:
19990
19991 init-declarator:
19992 declarator asm-specification [opt] attributes [opt] initializer [opt]
19993
19994 function-definition:
19995 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19996 function-body
19997 decl-specifier-seq [opt] declarator function-try-block
19998
19999 GNU Extension:
20000
20001 function-definition:
20002 __extension__ function-definition
20003
20004 TM Extension:
20005
20006 function-definition:
20007 decl-specifier-seq [opt] declarator function-transaction-block
20008
20009 The parser flags FLAGS is used to control type-specifier parsing.
20010
20011 The DECL_SPECIFIERS apply to this declarator. Returns a
20012 representation of the entity declared. If MEMBER_P is TRUE, then
20013 this declarator appears in a class scope. The new DECL created by
20014 this declarator is returned.
20015
20016 The CHECKS are access checks that should be performed once we know
20017 what entity is being declared (and, therefore, what classes have
20018 befriended it).
20019
20020 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20021 for a function-definition here as well. If the declarator is a
20022 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20023 be TRUE upon return. By that point, the function-definition will
20024 have been completely parsed.
20025
20026 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20027 is FALSE.
20028
20029 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20030 parsed declaration if it is an uninitialized single declarator not followed
20031 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20032 if present, will not be consumed. If returned, this declarator will be
20033 created with SD_INITIALIZED but will not call cp_finish_decl.
20034
20035 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20036 and there is an initializer, the pointed location_t is set to the
20037 location of the '=' or `(', or '{' in C++11 token introducing the
20038 initializer. */
20039
20040 static tree
20041 cp_parser_init_declarator (cp_parser* parser,
20042 cp_parser_flags flags,
20043 cp_decl_specifier_seq *decl_specifiers,
20044 vec<deferred_access_check, va_gc> *checks,
20045 bool function_definition_allowed_p,
20046 bool member_p,
20047 int declares_class_or_enum,
20048 bool* function_definition_p,
20049 tree* maybe_range_for_decl,
20050 location_t* init_loc,
20051 tree* auto_result)
20052 {
20053 cp_token *token = NULL, *asm_spec_start_token = NULL,
20054 *attributes_start_token = NULL;
20055 cp_declarator *declarator;
20056 tree prefix_attributes;
20057 tree attributes = NULL;
20058 tree asm_specification;
20059 tree initializer;
20060 tree decl = NULL_TREE;
20061 tree scope;
20062 int is_initialized;
20063 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20064 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20065 "(...)". */
20066 enum cpp_ttype initialization_kind;
20067 bool is_direct_init = false;
20068 bool is_non_constant_init;
20069 int ctor_dtor_or_conv_p;
20070 bool friend_p = cp_parser_friend_p (decl_specifiers);
20071 tree pushed_scope = NULL_TREE;
20072 bool range_for_decl_p = false;
20073 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20074 location_t tmp_init_loc = UNKNOWN_LOCATION;
20075
20076 /* Gather the attributes that were provided with the
20077 decl-specifiers. */
20078 prefix_attributes = decl_specifiers->attributes;
20079
20080 /* Assume that this is not the declarator for a function
20081 definition. */
20082 if (function_definition_p)
20083 *function_definition_p = false;
20084
20085 /* Default arguments are only permitted for function parameters. */
20086 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
20087 parser->default_arg_ok_p = false;
20088
20089 /* Defer access checks while parsing the declarator; we cannot know
20090 what names are accessible until we know what is being
20091 declared. */
20092 resume_deferring_access_checks ();
20093
20094 token = cp_lexer_peek_token (parser->lexer);
20095
20096 /* Parse the declarator. */
20097 declarator
20098 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20099 flags, &ctor_dtor_or_conv_p,
20100 /*parenthesized_p=*/NULL,
20101 member_p, friend_p, /*static_p=*/false);
20102 /* Gather up the deferred checks. */
20103 stop_deferring_access_checks ();
20104
20105 parser->default_arg_ok_p = saved_default_arg_ok_p;
20106
20107 /* If the DECLARATOR was erroneous, there's no need to go
20108 further. */
20109 if (declarator == cp_error_declarator)
20110 return error_mark_node;
20111
20112 /* Check that the number of template-parameter-lists is OK. */
20113 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
20114 token->location))
20115 return error_mark_node;
20116
20117 if (declares_class_or_enum & 2)
20118 cp_parser_check_for_definition_in_return_type (declarator,
20119 decl_specifiers->type,
20120 decl_specifiers->locations[ds_type_spec]);
20121
20122 /* Figure out what scope the entity declared by the DECLARATOR is
20123 located in. `grokdeclarator' sometimes changes the scope, so
20124 we compute it now. */
20125 scope = get_scope_of_declarator (declarator);
20126
20127 /* Perform any lookups in the declared type which were thought to be
20128 dependent, but are not in the scope of the declarator. */
20129 decl_specifiers->type
20130 = maybe_update_decl_type (decl_specifiers->type, scope);
20131
20132 /* If we're allowing GNU extensions, look for an
20133 asm-specification. */
20134 if (cp_parser_allow_gnu_extensions_p (parser))
20135 {
20136 /* Look for an asm-specification. */
20137 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
20138 asm_specification = cp_parser_asm_specification_opt (parser);
20139 }
20140 else
20141 asm_specification = NULL_TREE;
20142
20143 /* Look for attributes. */
20144 attributes_start_token = cp_lexer_peek_token (parser->lexer);
20145 attributes = cp_parser_attributes_opt (parser);
20146
20147 /* Peek at the next token. */
20148 token = cp_lexer_peek_token (parser->lexer);
20149
20150 bool bogus_implicit_tmpl = false;
20151
20152 if (function_declarator_p (declarator))
20153 {
20154 /* Handle C++17 deduction guides. */
20155 if (!decl_specifiers->type
20156 && ctor_dtor_or_conv_p <= 0
20157 && cxx_dialect >= cxx17)
20158 {
20159 cp_declarator *id = get_id_declarator (declarator);
20160 tree name = id->u.id.unqualified_name;
20161 parser->scope = id->u.id.qualifying_scope;
20162 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
20163 if (tmpl
20164 && (DECL_CLASS_TEMPLATE_P (tmpl)
20165 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
20166 {
20167 id->u.id.unqualified_name = dguide_name (tmpl);
20168 id->u.id.sfk = sfk_deduction_guide;
20169 ctor_dtor_or_conv_p = 1;
20170 }
20171 }
20172
20173 /* Check to see if the token indicates the start of a
20174 function-definition. */
20175 if (cp_parser_token_starts_function_definition_p (token))
20176 {
20177 if (!function_definition_allowed_p)
20178 {
20179 /* If a function-definition should not appear here, issue an
20180 error message. */
20181 cp_parser_error (parser,
20182 "a function-definition is not allowed here");
20183 return error_mark_node;
20184 }
20185
20186 location_t func_brace_location
20187 = cp_lexer_peek_token (parser->lexer)->location;
20188
20189 /* Neither attributes nor an asm-specification are allowed
20190 on a function-definition. */
20191 if (asm_specification)
20192 error_at (asm_spec_start_token->location,
20193 "an asm-specification is not allowed "
20194 "on a function-definition");
20195 if (attributes)
20196 error_at (attributes_start_token->location,
20197 "attributes are not allowed "
20198 "on a function-definition");
20199 /* This is a function-definition. */
20200 *function_definition_p = true;
20201
20202 /* Parse the function definition. */
20203 if (member_p)
20204 decl = cp_parser_save_member_function_body (parser,
20205 decl_specifiers,
20206 declarator,
20207 prefix_attributes);
20208 else
20209 decl =
20210 (cp_parser_function_definition_from_specifiers_and_declarator
20211 (parser, decl_specifiers, prefix_attributes, declarator));
20212
20213 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
20214 {
20215 /* This is where the prologue starts... */
20216 DECL_STRUCT_FUNCTION (decl)->function_start_locus
20217 = func_brace_location;
20218 }
20219
20220 return decl;
20221 }
20222 }
20223 else if (parser->fully_implicit_function_template_p)
20224 {
20225 /* A non-template declaration involving a function parameter list
20226 containing an implicit template parameter will be made into a
20227 template. If the resulting declaration is not going to be an
20228 actual function then finish the template scope here to prevent it.
20229 An error message will be issued once we have a decl to talk about.
20230
20231 FIXME probably we should do type deduction rather than create an
20232 implicit template, but the standard currently doesn't allow it. */
20233 bogus_implicit_tmpl = true;
20234 finish_fully_implicit_template (parser, NULL_TREE);
20235 }
20236
20237 /* [dcl.dcl]
20238
20239 Only in function declarations for constructors, destructors, type
20240 conversions, and deduction guides can the decl-specifier-seq be omitted.
20241
20242 We explicitly postpone this check past the point where we handle
20243 function-definitions because we tolerate function-definitions
20244 that are missing their return types in some modes. */
20245 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
20246 {
20247 cp_parser_error (parser,
20248 "expected constructor, destructor, or type conversion");
20249 return error_mark_node;
20250 }
20251
20252 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20253 if (token->type == CPP_EQ
20254 || token->type == CPP_OPEN_PAREN
20255 || token->type == CPP_OPEN_BRACE)
20256 {
20257 is_initialized = SD_INITIALIZED;
20258 initialization_kind = token->type;
20259 if (maybe_range_for_decl)
20260 *maybe_range_for_decl = error_mark_node;
20261 tmp_init_loc = token->location;
20262 if (init_loc && *init_loc == UNKNOWN_LOCATION)
20263 *init_loc = tmp_init_loc;
20264
20265 if (token->type == CPP_EQ
20266 && function_declarator_p (declarator))
20267 {
20268 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
20269 if (t2->keyword == RID_DEFAULT)
20270 is_initialized = SD_DEFAULTED;
20271 else if (t2->keyword == RID_DELETE)
20272 is_initialized = SD_DELETED;
20273 }
20274 }
20275 else
20276 {
20277 /* If the init-declarator isn't initialized and isn't followed by a
20278 `,' or `;', it's not a valid init-declarator. */
20279 if (token->type != CPP_COMMA
20280 && token->type != CPP_SEMICOLON)
20281 {
20282 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
20283 range_for_decl_p = true;
20284 else
20285 {
20286 if (!maybe_range_for_decl)
20287 cp_parser_error (parser, "expected initializer");
20288 return error_mark_node;
20289 }
20290 }
20291 is_initialized = SD_UNINITIALIZED;
20292 initialization_kind = CPP_EOF;
20293 }
20294
20295 /* Because start_decl has side-effects, we should only call it if we
20296 know we're going ahead. By this point, we know that we cannot
20297 possibly be looking at any other construct. */
20298 cp_parser_commit_to_tentative_parse (parser);
20299
20300 /* Enter the newly declared entry in the symbol table. If we're
20301 processing a declaration in a class-specifier, we wait until
20302 after processing the initializer. */
20303 if (!member_p)
20304 {
20305 if (parser->in_unbraced_linkage_specification_p)
20306 decl_specifiers->storage_class = sc_extern;
20307 decl = start_decl (declarator, decl_specifiers,
20308 range_for_decl_p? SD_INITIALIZED : is_initialized,
20309 attributes, prefix_attributes, &pushed_scope);
20310 cp_finalize_omp_declare_simd (parser, decl);
20311 cp_finalize_oacc_routine (parser, decl, false);
20312 /* Adjust location of decl if declarator->id_loc is more appropriate:
20313 set, and decl wasn't merged with another decl, in which case its
20314 location would be different from input_location, and more accurate. */
20315 if (DECL_P (decl)
20316 && declarator->id_loc != UNKNOWN_LOCATION
20317 && DECL_SOURCE_LOCATION (decl) == input_location)
20318 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
20319 }
20320 else if (scope)
20321 /* Enter the SCOPE. That way unqualified names appearing in the
20322 initializer will be looked up in SCOPE. */
20323 pushed_scope = push_scope (scope);
20324
20325 /* Perform deferred access control checks, now that we know in which
20326 SCOPE the declared entity resides. */
20327 if (!member_p && decl)
20328 {
20329 tree saved_current_function_decl = NULL_TREE;
20330
20331 /* If the entity being declared is a function, pretend that we
20332 are in its scope. If it is a `friend', it may have access to
20333 things that would not otherwise be accessible. */
20334 if (TREE_CODE (decl) == FUNCTION_DECL)
20335 {
20336 saved_current_function_decl = current_function_decl;
20337 current_function_decl = decl;
20338 }
20339
20340 /* Perform access checks for template parameters. */
20341 cp_parser_perform_template_parameter_access_checks (checks);
20342
20343 /* Perform the access control checks for the declarator and the
20344 decl-specifiers. */
20345 perform_deferred_access_checks (tf_warning_or_error);
20346
20347 /* Restore the saved value. */
20348 if (TREE_CODE (decl) == FUNCTION_DECL)
20349 current_function_decl = saved_current_function_decl;
20350 }
20351
20352 /* Parse the initializer. */
20353 initializer = NULL_TREE;
20354 is_direct_init = false;
20355 is_non_constant_init = true;
20356 if (is_initialized)
20357 {
20358 if (function_declarator_p (declarator))
20359 {
20360 if (initialization_kind == CPP_EQ)
20361 initializer = cp_parser_pure_specifier (parser);
20362 else
20363 {
20364 /* If the declaration was erroneous, we don't really
20365 know what the user intended, so just silently
20366 consume the initializer. */
20367 if (decl != error_mark_node)
20368 error_at (tmp_init_loc, "initializer provided for function");
20369 cp_parser_skip_to_closing_parenthesis (parser,
20370 /*recovering=*/true,
20371 /*or_comma=*/false,
20372 /*consume_paren=*/true);
20373 }
20374 }
20375 else
20376 {
20377 /* We want to record the extra mangling scope for in-class
20378 initializers of class members and initializers of static data
20379 member templates. The former involves deferring
20380 parsing of the initializer until end of class as with default
20381 arguments. So right here we only handle the latter. */
20382 if (!member_p && processing_template_decl && decl != error_mark_node)
20383 start_lambda_scope (decl);
20384 initializer = cp_parser_initializer (parser,
20385 &is_direct_init,
20386 &is_non_constant_init);
20387 if (!member_p && processing_template_decl && decl != error_mark_node)
20388 finish_lambda_scope ();
20389 if (initializer == error_mark_node)
20390 cp_parser_skip_to_end_of_statement (parser);
20391 }
20392 }
20393
20394 /* The old parser allows attributes to appear after a parenthesized
20395 initializer. Mark Mitchell proposed removing this functionality
20396 on the GCC mailing lists on 2002-08-13. This parser accepts the
20397 attributes -- but ignores them. Made a permerror in GCC 8. */
20398 if (cp_parser_allow_gnu_extensions_p (parser)
20399 && initialization_kind == CPP_OPEN_PAREN
20400 && cp_parser_attributes_opt (parser)
20401 && permerror (input_location,
20402 "attributes after parenthesized initializer ignored"))
20403 {
20404 static bool hint;
20405 if (flag_permissive && !hint)
20406 {
20407 hint = true;
20408 inform (input_location,
20409 "this flexibility is deprecated and will be removed");
20410 }
20411 }
20412
20413 /* And now complain about a non-function implicit template. */
20414 if (bogus_implicit_tmpl && decl != error_mark_node)
20415 error_at (DECL_SOURCE_LOCATION (decl),
20416 "non-function %qD declared as implicit template", decl);
20417
20418 /* For an in-class declaration, use `grokfield' to create the
20419 declaration. */
20420 if (member_p)
20421 {
20422 if (pushed_scope)
20423 {
20424 pop_scope (pushed_scope);
20425 pushed_scope = NULL_TREE;
20426 }
20427 decl = grokfield (declarator, decl_specifiers,
20428 initializer, !is_non_constant_init,
20429 /*asmspec=*/NULL_TREE,
20430 attr_chainon (attributes, prefix_attributes));
20431 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
20432 cp_parser_save_default_args (parser, decl);
20433 cp_finalize_omp_declare_simd (parser, decl);
20434 cp_finalize_oacc_routine (parser, decl, false);
20435 }
20436
20437 /* Finish processing the declaration. But, skip member
20438 declarations. */
20439 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
20440 {
20441 cp_finish_decl (decl,
20442 initializer, !is_non_constant_init,
20443 asm_specification,
20444 /* If the initializer is in parentheses, then this is
20445 a direct-initialization, which means that an
20446 `explicit' constructor is OK. Otherwise, an
20447 `explicit' constructor cannot be used. */
20448 ((is_direct_init || !is_initialized)
20449 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
20450 }
20451 else if ((cxx_dialect != cxx98) && friend_p
20452 && decl && TREE_CODE (decl) == FUNCTION_DECL)
20453 /* Core issue #226 (C++0x only): A default template-argument
20454 shall not be specified in a friend class template
20455 declaration. */
20456 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
20457 /*is_partial=*/false, /*is_friend_decl=*/1);
20458
20459 if (!friend_p && pushed_scope)
20460 pop_scope (pushed_scope);
20461
20462 if (function_declarator_p (declarator)
20463 && parser->fully_implicit_function_template_p)
20464 {
20465 if (member_p)
20466 decl = finish_fully_implicit_template (parser, decl);
20467 else
20468 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
20469 }
20470
20471 if (auto_result && is_initialized && decl_specifiers->type
20472 && type_uses_auto (decl_specifiers->type))
20473 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
20474
20475 return decl;
20476 }
20477
20478 /* Parse a declarator.
20479
20480 declarator:
20481 direct-declarator
20482 ptr-operator declarator
20483
20484 abstract-declarator:
20485 ptr-operator abstract-declarator [opt]
20486 direct-abstract-declarator
20487
20488 GNU Extensions:
20489
20490 declarator:
20491 attributes [opt] direct-declarator
20492 attributes [opt] ptr-operator declarator
20493
20494 abstract-declarator:
20495 attributes [opt] ptr-operator abstract-declarator [opt]
20496 attributes [opt] direct-abstract-declarator
20497
20498 The parser flags FLAGS is used to control type-specifier parsing.
20499
20500 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20501 detect constructors, destructors, deduction guides, or conversion operators.
20502 It is set to -1 if the declarator is a name, and +1 if it is a
20503 function. Otherwise it is set to zero. Usually you just want to
20504 test for >0, but internally the negative value is used.
20505
20506 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20507 a decl-specifier-seq unless it declares a constructor, destructor,
20508 or conversion. It might seem that we could check this condition in
20509 semantic analysis, rather than parsing, but that makes it difficult
20510 to handle something like `f()'. We want to notice that there are
20511 no decl-specifiers, and therefore realize that this is an
20512 expression, not a declaration.)
20513
20514 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20515 the declarator is a direct-declarator of the form "(...)".
20516
20517 MEMBER_P is true iff this declarator is a member-declarator.
20518
20519 FRIEND_P is true iff this declarator is a friend.
20520
20521 STATIC_P is true iff the keyword static was seen. */
20522
20523 static cp_declarator *
20524 cp_parser_declarator (cp_parser* parser,
20525 cp_parser_declarator_kind dcl_kind,
20526 cp_parser_flags flags,
20527 int* ctor_dtor_or_conv_p,
20528 bool* parenthesized_p,
20529 bool member_p, bool friend_p, bool static_p)
20530 {
20531 cp_declarator *declarator;
20532 enum tree_code code;
20533 cp_cv_quals cv_quals;
20534 tree class_type;
20535 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
20536
20537 /* Assume this is not a constructor, destructor, or type-conversion
20538 operator. */
20539 if (ctor_dtor_or_conv_p)
20540 *ctor_dtor_or_conv_p = 0;
20541
20542 if (cp_parser_allow_gnu_extensions_p (parser))
20543 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
20544
20545 /* Check for the ptr-operator production. */
20546 cp_parser_parse_tentatively (parser);
20547 /* Parse the ptr-operator. */
20548 code = cp_parser_ptr_operator (parser,
20549 &class_type,
20550 &cv_quals,
20551 &std_attributes);
20552
20553 /* If that worked, then we have a ptr-operator. */
20554 if (cp_parser_parse_definitely (parser))
20555 {
20556 /* If a ptr-operator was found, then this declarator was not
20557 parenthesized. */
20558 if (parenthesized_p)
20559 *parenthesized_p = true;
20560 /* The dependent declarator is optional if we are parsing an
20561 abstract-declarator. */
20562 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20563 cp_parser_parse_tentatively (parser);
20564
20565 /* Parse the dependent declarator. */
20566 declarator = cp_parser_declarator (parser, dcl_kind,
20567 CP_PARSER_FLAGS_NONE,
20568 /*ctor_dtor_or_conv_p=*/NULL,
20569 /*parenthesized_p=*/NULL,
20570 /*member_p=*/false,
20571 friend_p, /*static_p=*/false);
20572
20573 /* If we are parsing an abstract-declarator, we must handle the
20574 case where the dependent declarator is absent. */
20575 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
20576 && !cp_parser_parse_definitely (parser))
20577 declarator = NULL;
20578
20579 declarator = cp_parser_make_indirect_declarator
20580 (code, class_type, cv_quals, declarator, std_attributes);
20581 }
20582 /* Everything else is a direct-declarator. */
20583 else
20584 {
20585 if (parenthesized_p)
20586 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
20587 CPP_OPEN_PAREN);
20588 declarator = cp_parser_direct_declarator (parser, dcl_kind,
20589 flags, ctor_dtor_or_conv_p,
20590 member_p, friend_p, static_p);
20591 }
20592
20593 if (gnu_attributes && declarator && declarator != cp_error_declarator)
20594 declarator->attributes = gnu_attributes;
20595 return declarator;
20596 }
20597
20598 /* Parse a direct-declarator or direct-abstract-declarator.
20599
20600 direct-declarator:
20601 declarator-id
20602 direct-declarator ( parameter-declaration-clause )
20603 cv-qualifier-seq [opt]
20604 ref-qualifier [opt]
20605 exception-specification [opt]
20606 direct-declarator [ constant-expression [opt] ]
20607 ( declarator )
20608
20609 direct-abstract-declarator:
20610 direct-abstract-declarator [opt]
20611 ( parameter-declaration-clause )
20612 cv-qualifier-seq [opt]
20613 ref-qualifier [opt]
20614 exception-specification [opt]
20615 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20616 ( abstract-declarator )
20617
20618 Returns a representation of the declarator. DCL_KIND is
20619 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20620 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20621 we are parsing a direct-declarator. It is
20622 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20623 of ambiguity we prefer an abstract declarator, as per
20624 [dcl.ambig.res].
20625 The parser flags FLAGS is used to control type-specifier parsing.
20626 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20627 as for cp_parser_declarator. */
20628
20629 static cp_declarator *
20630 cp_parser_direct_declarator (cp_parser* parser,
20631 cp_parser_declarator_kind dcl_kind,
20632 cp_parser_flags flags,
20633 int* ctor_dtor_or_conv_p,
20634 bool member_p, bool friend_p, bool static_p)
20635 {
20636 cp_token *token;
20637 cp_declarator *declarator = NULL;
20638 tree scope = NULL_TREE;
20639 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20640 bool saved_in_declarator_p = parser->in_declarator_p;
20641 bool first = true;
20642 tree pushed_scope = NULL_TREE;
20643 cp_token *open_paren = NULL, *close_paren = NULL;
20644
20645 while (true)
20646 {
20647 /* Peek at the next token. */
20648 token = cp_lexer_peek_token (parser->lexer);
20649 if (token->type == CPP_OPEN_PAREN)
20650 {
20651 /* This is either a parameter-declaration-clause, or a
20652 parenthesized declarator. When we know we are parsing a
20653 named declarator, it must be a parenthesized declarator
20654 if FIRST is true. For instance, `(int)' is a
20655 parameter-declaration-clause, with an omitted
20656 direct-abstract-declarator. But `((*))', is a
20657 parenthesized abstract declarator. Finally, when T is a
20658 template parameter `(T)' is a
20659 parameter-declaration-clause, and not a parenthesized
20660 named declarator.
20661
20662 We first try and parse a parameter-declaration-clause,
20663 and then try a nested declarator (if FIRST is true).
20664
20665 It is not an error for it not to be a
20666 parameter-declaration-clause, even when FIRST is
20667 false. Consider,
20668
20669 int i (int);
20670 int i (3);
20671
20672 The first is the declaration of a function while the
20673 second is the definition of a variable, including its
20674 initializer.
20675
20676 Having seen only the parenthesis, we cannot know which of
20677 these two alternatives should be selected. Even more
20678 complex are examples like:
20679
20680 int i (int (a));
20681 int i (int (3));
20682
20683 The former is a function-declaration; the latter is a
20684 variable initialization.
20685
20686 Thus again, we try a parameter-declaration-clause, and if
20687 that fails, we back out and return. */
20688
20689 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20690 {
20691 tree params;
20692 bool is_declarator = false;
20693
20694 open_paren = NULL;
20695
20696 /* In a member-declarator, the only valid interpretation
20697 of a parenthesis is the start of a
20698 parameter-declaration-clause. (It is invalid to
20699 initialize a static data member with a parenthesized
20700 initializer; only the "=" form of initialization is
20701 permitted.) */
20702 if (!member_p)
20703 cp_parser_parse_tentatively (parser);
20704
20705 /* Consume the `('. */
20706 matching_parens parens;
20707 parens.consume_open (parser);
20708 if (first)
20709 {
20710 /* If this is going to be an abstract declarator, we're
20711 in a declarator and we can't have default args. */
20712 parser->default_arg_ok_p = false;
20713 parser->in_declarator_p = true;
20714 }
20715
20716 begin_scope (sk_function_parms, NULL_TREE);
20717
20718 /* Parse the parameter-declaration-clause. */
20719 params
20720 = cp_parser_parameter_declaration_clause (parser, flags);
20721
20722 /* Consume the `)'. */
20723 parens.require_close (parser);
20724
20725 /* If all went well, parse the cv-qualifier-seq,
20726 ref-qualifier and the exception-specification. */
20727 if (member_p || cp_parser_parse_definitely (parser))
20728 {
20729 cp_cv_quals cv_quals;
20730 cp_virt_specifiers virt_specifiers;
20731 cp_ref_qualifier ref_qual;
20732 tree exception_specification;
20733 tree late_return;
20734 tree attrs;
20735 bool memfn = (member_p || (pushed_scope
20736 && CLASS_TYPE_P (pushed_scope)));
20737 unsigned char local_variables_forbidden_p
20738 = parser->local_variables_forbidden_p;
20739 /* 'this' is not allowed in static member functions. */
20740 if (static_p || friend_p)
20741 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
20742
20743 is_declarator = true;
20744
20745 if (ctor_dtor_or_conv_p)
20746 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20747 first = false;
20748
20749 /* Parse the cv-qualifier-seq. */
20750 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20751 /* Parse the ref-qualifier. */
20752 ref_qual = cp_parser_ref_qualifier_opt (parser);
20753 /* Parse the tx-qualifier. */
20754 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20755 /* And the exception-specification. */
20756 exception_specification
20757 = cp_parser_exception_specification_opt (parser);
20758
20759 attrs = cp_parser_std_attribute_spec_seq (parser);
20760
20761 /* In here, we handle cases where attribute is used after
20762 the function declaration. For example:
20763 void func (int x) __attribute__((vector(..))); */
20764 tree gnu_attrs = NULL_TREE;
20765 tree requires_clause = NULL_TREE;
20766 late_return = (cp_parser_late_return_type_opt
20767 (parser, declarator, requires_clause,
20768 memfn ? cv_quals : -1));
20769
20770 /* Parse the virt-specifier-seq. */
20771 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20772
20773 /* Create the function-declarator. */
20774 declarator = make_call_declarator (declarator,
20775 params,
20776 cv_quals,
20777 virt_specifiers,
20778 ref_qual,
20779 tx_qual,
20780 exception_specification,
20781 late_return,
20782 requires_clause);
20783 declarator->std_attributes = attrs;
20784 declarator->attributes = gnu_attrs;
20785 /* Any subsequent parameter lists are to do with
20786 return type, so are not those of the declared
20787 function. */
20788 parser->default_arg_ok_p = false;
20789
20790 /* Restore the state of local_variables_forbidden_p. */
20791 parser->local_variables_forbidden_p
20792 = local_variables_forbidden_p;
20793 }
20794
20795 /* Remove the function parms from scope. */
20796 pop_bindings_and_leave_scope ();
20797
20798 if (is_declarator)
20799 /* Repeat the main loop. */
20800 continue;
20801 }
20802
20803 /* If this is the first, we can try a parenthesized
20804 declarator. */
20805 if (first)
20806 {
20807 bool saved_in_type_id_in_expr_p;
20808
20809 parser->default_arg_ok_p = saved_default_arg_ok_p;
20810 parser->in_declarator_p = saved_in_declarator_p;
20811
20812 open_paren = token;
20813 /* Consume the `('. */
20814 matching_parens parens;
20815 parens.consume_open (parser);
20816 /* Parse the nested declarator. */
20817 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20818 parser->in_type_id_in_expr_p = true;
20819 declarator
20820 = cp_parser_declarator (parser, dcl_kind, flags,
20821 ctor_dtor_or_conv_p,
20822 /*parenthesized_p=*/NULL,
20823 member_p, friend_p,
20824 /*static_p=*/false);
20825 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20826 first = false;
20827 /* Expect a `)'. */
20828 close_paren = cp_lexer_peek_token (parser->lexer);
20829 if (!parens.require_close (parser))
20830 declarator = cp_error_declarator;
20831 if (declarator == cp_error_declarator)
20832 break;
20833
20834 goto handle_declarator;
20835 }
20836 /* Otherwise, we must be done. */
20837 else
20838 break;
20839 }
20840 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20841 && token->type == CPP_OPEN_SQUARE
20842 && !cp_next_tokens_can_be_attribute_p (parser))
20843 {
20844 /* Parse an array-declarator. */
20845 tree bounds, attrs;
20846
20847 if (ctor_dtor_or_conv_p)
20848 *ctor_dtor_or_conv_p = 0;
20849
20850 open_paren = NULL;
20851 first = false;
20852 parser->default_arg_ok_p = false;
20853 parser->in_declarator_p = true;
20854 /* Consume the `['. */
20855 cp_lexer_consume_token (parser->lexer);
20856 /* Peek at the next token. */
20857 token = cp_lexer_peek_token (parser->lexer);
20858 /* If the next token is `]', then there is no
20859 constant-expression. */
20860 if (token->type != CPP_CLOSE_SQUARE)
20861 {
20862 bool non_constant_p;
20863 bounds
20864 = cp_parser_constant_expression (parser,
20865 /*allow_non_constant=*/true,
20866 &non_constant_p);
20867 if (!non_constant_p)
20868 /* OK */;
20869 else if (error_operand_p (bounds))
20870 /* Already gave an error. */;
20871 else if (!parser->in_function_body
20872 || current_binding_level->kind == sk_function_parms)
20873 {
20874 /* Normally, the array bound must be an integral constant
20875 expression. However, as an extension, we allow VLAs
20876 in function scopes as long as they aren't part of a
20877 parameter declaration. */
20878 cp_parser_error (parser,
20879 "array bound is not an integer constant");
20880 bounds = error_mark_node;
20881 }
20882 else if (processing_template_decl
20883 && !type_dependent_expression_p (bounds))
20884 {
20885 /* Remember this wasn't a constant-expression. */
20886 bounds = build_nop (TREE_TYPE (bounds), bounds);
20887 TREE_SIDE_EFFECTS (bounds) = 1;
20888 }
20889 }
20890 else
20891 bounds = NULL_TREE;
20892 /* Look for the closing `]'. */
20893 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20894 {
20895 declarator = cp_error_declarator;
20896 break;
20897 }
20898
20899 attrs = cp_parser_std_attribute_spec_seq (parser);
20900 declarator = make_array_declarator (declarator, bounds);
20901 declarator->std_attributes = attrs;
20902 }
20903 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20904 {
20905 {
20906 tree qualifying_scope;
20907 tree unqualified_name;
20908 tree attrs;
20909 special_function_kind sfk;
20910 bool abstract_ok;
20911 bool pack_expansion_p = false;
20912 cp_token *declarator_id_start_token;
20913
20914 /* Parse a declarator-id */
20915 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20916 if (abstract_ok)
20917 {
20918 cp_parser_parse_tentatively (parser);
20919
20920 /* If we see an ellipsis, we should be looking at a
20921 parameter pack. */
20922 if (token->type == CPP_ELLIPSIS)
20923 {
20924 /* Consume the `...' */
20925 cp_lexer_consume_token (parser->lexer);
20926
20927 pack_expansion_p = true;
20928 }
20929 }
20930
20931 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20932 unqualified_name
20933 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20934 qualifying_scope = parser->scope;
20935 if (abstract_ok)
20936 {
20937 bool okay = false;
20938
20939 if (!unqualified_name && pack_expansion_p)
20940 {
20941 /* Check whether an error occurred. */
20942 okay = !cp_parser_error_occurred (parser);
20943
20944 /* We already consumed the ellipsis to mark a
20945 parameter pack, but we have no way to report it,
20946 so abort the tentative parse. We will be exiting
20947 immediately anyway. */
20948 cp_parser_abort_tentative_parse (parser);
20949 }
20950 else
20951 okay = cp_parser_parse_definitely (parser);
20952
20953 if (!okay)
20954 unqualified_name = error_mark_node;
20955 else if (unqualified_name
20956 && (qualifying_scope
20957 || (!identifier_p (unqualified_name))))
20958 {
20959 cp_parser_error (parser, "expected unqualified-id");
20960 unqualified_name = error_mark_node;
20961 }
20962 }
20963
20964 if (!unqualified_name)
20965 return NULL;
20966 if (unqualified_name == error_mark_node)
20967 {
20968 declarator = cp_error_declarator;
20969 pack_expansion_p = false;
20970 declarator->parameter_pack_p = false;
20971 break;
20972 }
20973
20974 attrs = cp_parser_std_attribute_spec_seq (parser);
20975
20976 if (qualifying_scope && at_namespace_scope_p ()
20977 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20978 {
20979 /* In the declaration of a member of a template class
20980 outside of the class itself, the SCOPE will sometimes
20981 be a TYPENAME_TYPE. For example, given:
20982
20983 template <typename T>
20984 int S<T>::R::i = 3;
20985
20986 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20987 this context, we must resolve S<T>::R to an ordinary
20988 type, rather than a typename type.
20989
20990 The reason we normally avoid resolving TYPENAME_TYPEs
20991 is that a specialization of `S' might render
20992 `S<T>::R' not a type. However, if `S' is
20993 specialized, then this `i' will not be used, so there
20994 is no harm in resolving the types here. */
20995 tree type;
20996
20997 /* Resolve the TYPENAME_TYPE. */
20998 type = resolve_typename_type (qualifying_scope,
20999 /*only_current_p=*/false);
21000 /* If that failed, the declarator is invalid. */
21001 if (TREE_CODE (type) == TYPENAME_TYPE)
21002 {
21003 if (typedef_variant_p (type))
21004 error_at (declarator_id_start_token->location,
21005 "cannot define member of dependent typedef "
21006 "%qT", type);
21007 else
21008 error_at (declarator_id_start_token->location,
21009 "%<%T::%E%> is not a type",
21010 TYPE_CONTEXT (qualifying_scope),
21011 TYPE_IDENTIFIER (qualifying_scope));
21012 }
21013 qualifying_scope = type;
21014 }
21015
21016 sfk = sfk_none;
21017
21018 if (unqualified_name)
21019 {
21020 tree class_type;
21021
21022 if (qualifying_scope
21023 && CLASS_TYPE_P (qualifying_scope))
21024 class_type = qualifying_scope;
21025 else
21026 class_type = current_class_type;
21027
21028 if (TREE_CODE (unqualified_name) == TYPE_DECL)
21029 {
21030 tree name_type = TREE_TYPE (unqualified_name);
21031
21032 if (!class_type || !same_type_p (name_type, class_type))
21033 {
21034 /* We do not attempt to print the declarator
21035 here because we do not have enough
21036 information about its original syntactic
21037 form. */
21038 cp_parser_error (parser, "invalid declarator");
21039 declarator = cp_error_declarator;
21040 break;
21041 }
21042 else if (qualifying_scope
21043 && CLASSTYPE_USE_TEMPLATE (name_type))
21044 {
21045 error_at (declarator_id_start_token->location,
21046 "invalid use of constructor as a template");
21047 inform (declarator_id_start_token->location,
21048 "use %<%T::%D%> instead of %<%T::%D%> to "
21049 "name the constructor in a qualified name",
21050 class_type,
21051 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
21052 class_type, name_type);
21053 declarator = cp_error_declarator;
21054 break;
21055 }
21056 unqualified_name = constructor_name (class_type);
21057 }
21058
21059 if (class_type)
21060 {
21061 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
21062 sfk = sfk_destructor;
21063 else if (identifier_p (unqualified_name)
21064 && IDENTIFIER_CONV_OP_P (unqualified_name))
21065 sfk = sfk_conversion;
21066 else if (/* There's no way to declare a constructor
21067 for an unnamed type, even if the type
21068 got a name for linkage purposes. */
21069 !TYPE_WAS_UNNAMED (class_type)
21070 /* Handle correctly (c++/19200):
21071
21072 struct S {
21073 struct T{};
21074 friend void S(T);
21075 };
21076
21077 and also:
21078
21079 namespace N {
21080 void S();
21081 }
21082
21083 struct S {
21084 friend void N::S();
21085 }; */
21086 && (!friend_p || class_type == qualifying_scope)
21087 && constructor_name_p (unqualified_name,
21088 class_type))
21089 sfk = sfk_constructor;
21090 else if (is_overloaded_fn (unqualified_name)
21091 && DECL_CONSTRUCTOR_P (get_first_fn
21092 (unqualified_name)))
21093 sfk = sfk_constructor;
21094
21095 if (ctor_dtor_or_conv_p && sfk != sfk_none)
21096 *ctor_dtor_or_conv_p = -1;
21097 }
21098 }
21099 declarator = make_id_declarator (qualifying_scope,
21100 unqualified_name,
21101 sfk, token->location);
21102 declarator->std_attributes = attrs;
21103 declarator->parameter_pack_p = pack_expansion_p;
21104
21105 if (pack_expansion_p)
21106 maybe_warn_variadic_templates ();
21107
21108 /* We're looking for this case in [temp.res]:
21109 A qualified-id is assumed to name a type if [...]
21110 - it is a decl-specifier of the decl-specifier-seq of a
21111 parameter-declaration in a declarator of a function or
21112 function template declaration, ... */
21113 if (cxx_dialect >= cxx2a
21114 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
21115 && declarator->kind == cdk_id
21116 && !at_class_scope_p ()
21117 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21118 {
21119 /* ...whose declarator-id is qualified. If it isn't, never
21120 assume the parameters to refer to types. */
21121 if (qualifying_scope == NULL_TREE)
21122 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21123 else
21124 {
21125 /* Now we have something like
21126 template <typename T> int C::x(S::p);
21127 which can be a function template declaration or a
21128 variable template definition. If name lookup for
21129 the declarator-id C::x finds one or more function
21130 templates, assume S::p to name a type. Otherwise,
21131 don't. */
21132 tree decl
21133 = cp_parser_lookup_name_simple (parser, unqualified_name,
21134 token->location);
21135 if (!is_overloaded_fn (decl))
21136 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
21137 }
21138 }
21139 }
21140
21141 handle_declarator:;
21142 scope = get_scope_of_declarator (declarator);
21143 if (scope)
21144 {
21145 /* Any names that appear after the declarator-id for a
21146 member are looked up in the containing scope. */
21147 if (at_function_scope_p ())
21148 {
21149 /* But declarations with qualified-ids can't appear in a
21150 function. */
21151 cp_parser_error (parser, "qualified-id in declaration");
21152 declarator = cp_error_declarator;
21153 break;
21154 }
21155 pushed_scope = push_scope (scope);
21156 }
21157 parser->in_declarator_p = true;
21158 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
21159 || (declarator && declarator->kind == cdk_id))
21160 /* Default args are only allowed on function
21161 declarations. */
21162 parser->default_arg_ok_p = saved_default_arg_ok_p;
21163 else
21164 parser->default_arg_ok_p = false;
21165
21166 first = false;
21167 }
21168 /* We're done. */
21169 else
21170 break;
21171 }
21172
21173 /* For an abstract declarator, we might wind up with nothing at this
21174 point. That's an error; the declarator is not optional. */
21175 if (!declarator)
21176 cp_parser_error (parser, "expected declarator");
21177 else if (open_paren)
21178 {
21179 /* Record overly parenthesized declarator so we can give a
21180 diagnostic about confusing decl/expr disambiguation. */
21181 if (declarator->kind == cdk_array)
21182 {
21183 /* If the open and close parens are on different lines, this
21184 is probably a formatting thing, so ignore. */
21185 expanded_location open = expand_location (open_paren->location);
21186 expanded_location close = expand_location (close_paren->location);
21187 if (open.line != close.line || open.file != close.file)
21188 open_paren = NULL;
21189 }
21190 if (open_paren)
21191 declarator->parenthesized = open_paren->location;
21192 }
21193
21194 /* If we entered a scope, we must exit it now. */
21195 if (pushed_scope)
21196 pop_scope (pushed_scope);
21197
21198 parser->default_arg_ok_p = saved_default_arg_ok_p;
21199 parser->in_declarator_p = saved_in_declarator_p;
21200
21201 return declarator;
21202 }
21203
21204 /* Parse a ptr-operator.
21205
21206 ptr-operator:
21207 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21208 * cv-qualifier-seq [opt]
21209 &
21210 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21211 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21212
21213 GNU Extension:
21214
21215 ptr-operator:
21216 & cv-qualifier-seq [opt]
21217
21218 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21219 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21220 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21221 filled in with the TYPE containing the member. *CV_QUALS is
21222 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21223 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21224 Note that the tree codes returned by this function have nothing
21225 to do with the types of trees that will be eventually be created
21226 to represent the pointer or reference type being parsed. They are
21227 just constants with suggestive names. */
21228 static enum tree_code
21229 cp_parser_ptr_operator (cp_parser* parser,
21230 tree* type,
21231 cp_cv_quals *cv_quals,
21232 tree *attributes)
21233 {
21234 enum tree_code code = ERROR_MARK;
21235 cp_token *token;
21236 tree attrs = NULL_TREE;
21237
21238 /* Assume that it's not a pointer-to-member. */
21239 *type = NULL_TREE;
21240 /* And that there are no cv-qualifiers. */
21241 *cv_quals = TYPE_UNQUALIFIED;
21242
21243 /* Peek at the next token. */
21244 token = cp_lexer_peek_token (parser->lexer);
21245
21246 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21247 if (token->type == CPP_MULT)
21248 code = INDIRECT_REF;
21249 else if (token->type == CPP_AND)
21250 code = ADDR_EXPR;
21251 else if ((cxx_dialect != cxx98) &&
21252 token->type == CPP_AND_AND) /* C++0x only */
21253 code = NON_LVALUE_EXPR;
21254
21255 if (code != ERROR_MARK)
21256 {
21257 /* Consume the `*', `&' or `&&'. */
21258 cp_lexer_consume_token (parser->lexer);
21259
21260 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21261 `&', if we are allowing GNU extensions. (The only qualifier
21262 that can legally appear after `&' is `restrict', but that is
21263 enforced during semantic analysis. */
21264 if (code == INDIRECT_REF
21265 || cp_parser_allow_gnu_extensions_p (parser))
21266 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21267
21268 attrs = cp_parser_std_attribute_spec_seq (parser);
21269 if (attributes != NULL)
21270 *attributes = attrs;
21271 }
21272 else
21273 {
21274 /* Try the pointer-to-member case. */
21275 cp_parser_parse_tentatively (parser);
21276 /* Look for the optional `::' operator. */
21277 cp_parser_global_scope_opt (parser,
21278 /*current_scope_valid_p=*/false);
21279 /* Look for the nested-name specifier. */
21280 token = cp_lexer_peek_token (parser->lexer);
21281 cp_parser_nested_name_specifier (parser,
21282 /*typename_keyword_p=*/false,
21283 /*check_dependency_p=*/true,
21284 /*type_p=*/false,
21285 /*is_declaration=*/false);
21286 /* If we found it, and the next token is a `*', then we are
21287 indeed looking at a pointer-to-member operator. */
21288 if (!cp_parser_error_occurred (parser)
21289 && cp_parser_require (parser, CPP_MULT, RT_MULT))
21290 {
21291 /* Indicate that the `*' operator was used. */
21292 code = INDIRECT_REF;
21293
21294 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
21295 error_at (token->location, "%qD is a namespace", parser->scope);
21296 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
21297 error_at (token->location, "cannot form pointer to member of "
21298 "non-class %q#T", parser->scope);
21299 else
21300 {
21301 /* The type of which the member is a member is given by the
21302 current SCOPE. */
21303 *type = parser->scope;
21304 /* The next name will not be qualified. */
21305 parser->scope = NULL_TREE;
21306 parser->qualifying_scope = NULL_TREE;
21307 parser->object_scope = NULL_TREE;
21308 /* Look for optional c++11 attributes. */
21309 attrs = cp_parser_std_attribute_spec_seq (parser);
21310 if (attributes != NULL)
21311 *attributes = attrs;
21312 /* Look for the optional cv-qualifier-seq. */
21313 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
21314 }
21315 }
21316 /* If that didn't work we don't have a ptr-operator. */
21317 if (!cp_parser_parse_definitely (parser))
21318 cp_parser_error (parser, "expected ptr-operator");
21319 }
21320
21321 return code;
21322 }
21323
21324 /* Parse an (optional) cv-qualifier-seq.
21325
21326 cv-qualifier-seq:
21327 cv-qualifier cv-qualifier-seq [opt]
21328
21329 cv-qualifier:
21330 const
21331 volatile
21332
21333 GNU Extension:
21334
21335 cv-qualifier:
21336 __restrict__
21337
21338 Returns a bitmask representing the cv-qualifiers. */
21339
21340 static cp_cv_quals
21341 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
21342 {
21343 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
21344
21345 while (true)
21346 {
21347 cp_token *token;
21348 cp_cv_quals cv_qualifier;
21349
21350 /* Peek at the next token. */
21351 token = cp_lexer_peek_token (parser->lexer);
21352 /* See if it's a cv-qualifier. */
21353 switch (token->keyword)
21354 {
21355 case RID_CONST:
21356 cv_qualifier = TYPE_QUAL_CONST;
21357 break;
21358
21359 case RID_VOLATILE:
21360 cv_qualifier = TYPE_QUAL_VOLATILE;
21361 break;
21362
21363 case RID_RESTRICT:
21364 cv_qualifier = TYPE_QUAL_RESTRICT;
21365 break;
21366
21367 default:
21368 cv_qualifier = TYPE_UNQUALIFIED;
21369 break;
21370 }
21371
21372 if (!cv_qualifier)
21373 break;
21374
21375 if (cv_quals & cv_qualifier)
21376 {
21377 gcc_rich_location richloc (token->location);
21378 richloc.add_fixit_remove ();
21379 error_at (&richloc, "duplicate cv-qualifier");
21380 cp_lexer_purge_token (parser->lexer);
21381 }
21382 else
21383 {
21384 cp_lexer_consume_token (parser->lexer);
21385 cv_quals |= cv_qualifier;
21386 }
21387 }
21388
21389 return cv_quals;
21390 }
21391
21392 /* Parse an (optional) ref-qualifier
21393
21394 ref-qualifier:
21395 &
21396 &&
21397
21398 Returns cp_ref_qualifier representing ref-qualifier. */
21399
21400 static cp_ref_qualifier
21401 cp_parser_ref_qualifier_opt (cp_parser* parser)
21402 {
21403 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
21404
21405 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21406 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
21407 return ref_qual;
21408
21409 while (true)
21410 {
21411 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
21412 cp_token *token = cp_lexer_peek_token (parser->lexer);
21413
21414 switch (token->type)
21415 {
21416 case CPP_AND:
21417 curr_ref_qual = REF_QUAL_LVALUE;
21418 break;
21419
21420 case CPP_AND_AND:
21421 curr_ref_qual = REF_QUAL_RVALUE;
21422 break;
21423
21424 default:
21425 curr_ref_qual = REF_QUAL_NONE;
21426 break;
21427 }
21428
21429 if (!curr_ref_qual)
21430 break;
21431 else if (ref_qual)
21432 {
21433 error_at (token->location, "multiple ref-qualifiers");
21434 cp_lexer_purge_token (parser->lexer);
21435 }
21436 else
21437 {
21438 ref_qual = curr_ref_qual;
21439 cp_lexer_consume_token (parser->lexer);
21440 }
21441 }
21442
21443 return ref_qual;
21444 }
21445
21446 /* Parse an optional tx-qualifier.
21447
21448 tx-qualifier:
21449 transaction_safe
21450 transaction_safe_dynamic */
21451
21452 static tree
21453 cp_parser_tx_qualifier_opt (cp_parser *parser)
21454 {
21455 cp_token *token = cp_lexer_peek_token (parser->lexer);
21456 if (token->type == CPP_NAME)
21457 {
21458 tree name = token->u.value;
21459 const char *p = IDENTIFIER_POINTER (name);
21460 const int len = strlen ("transaction_safe");
21461 if (!strncmp (p, "transaction_safe", len))
21462 {
21463 p += len;
21464 if (*p == '\0'
21465 || !strcmp (p, "_dynamic"))
21466 {
21467 cp_lexer_consume_token (parser->lexer);
21468 if (!flag_tm)
21469 {
21470 error ("%qE requires %<-fgnu-tm%>", name);
21471 return NULL_TREE;
21472 }
21473 else
21474 return name;
21475 }
21476 }
21477 }
21478 return NULL_TREE;
21479 }
21480
21481 /* Parse an (optional) virt-specifier-seq.
21482
21483 virt-specifier-seq:
21484 virt-specifier virt-specifier-seq [opt]
21485
21486 virt-specifier:
21487 override
21488 final
21489
21490 Returns a bitmask representing the virt-specifiers. */
21491
21492 static cp_virt_specifiers
21493 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
21494 {
21495 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21496
21497 while (true)
21498 {
21499 cp_token *token;
21500 cp_virt_specifiers virt_specifier;
21501
21502 /* Peek at the next token. */
21503 token = cp_lexer_peek_token (parser->lexer);
21504 /* See if it's a virt-specifier-qualifier. */
21505 if (token->type != CPP_NAME)
21506 break;
21507 if (id_equal (token->u.value, "override"))
21508 {
21509 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21510 virt_specifier = VIRT_SPEC_OVERRIDE;
21511 }
21512 else if (id_equal (token->u.value, "final"))
21513 {
21514 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
21515 virt_specifier = VIRT_SPEC_FINAL;
21516 }
21517 else if (id_equal (token->u.value, "__final"))
21518 {
21519 virt_specifier = VIRT_SPEC_FINAL;
21520 }
21521 else
21522 break;
21523
21524 if (virt_specifiers & virt_specifier)
21525 {
21526 gcc_rich_location richloc (token->location);
21527 richloc.add_fixit_remove ();
21528 error_at (&richloc, "duplicate virt-specifier");
21529 cp_lexer_purge_token (parser->lexer);
21530 }
21531 else
21532 {
21533 cp_lexer_consume_token (parser->lexer);
21534 virt_specifiers |= virt_specifier;
21535 }
21536 }
21537 return virt_specifiers;
21538 }
21539
21540 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21541 is in scope even though it isn't real. */
21542
21543 void
21544 inject_this_parameter (tree ctype, cp_cv_quals quals)
21545 {
21546 tree this_parm;
21547
21548 if (current_class_ptr)
21549 {
21550 /* We don't clear this between NSDMIs. Is it already what we want? */
21551 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
21552 if (DECL_P (current_class_ptr)
21553 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
21554 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
21555 && cp_type_quals (type) == quals)
21556 return;
21557 }
21558
21559 this_parm = build_this_parm (NULL_TREE, ctype, quals);
21560 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21561 current_class_ptr = NULL_TREE;
21562 current_class_ref
21563 = cp_build_fold_indirect_ref (this_parm);
21564 current_class_ptr = this_parm;
21565 }
21566
21567 /* Return true iff our current scope is a non-static data member
21568 initializer. */
21569
21570 bool
21571 parsing_nsdmi (void)
21572 {
21573 /* We recognize NSDMI context by the context-less 'this' pointer set up
21574 by the function above. */
21575 if (current_class_ptr
21576 && TREE_CODE (current_class_ptr) == PARM_DECL
21577 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
21578 return true;
21579 return false;
21580 }
21581
21582 /* Parse a late-specified return type, if any. This is not a separate
21583 non-terminal, but part of a function declarator, which looks like
21584
21585 -> trailing-type-specifier-seq abstract-declarator(opt)
21586
21587 Returns the type indicated by the type-id.
21588
21589 In addition to this, parse any queued up #pragma omp declare simd
21590 clauses, and #pragma acc routine clauses.
21591
21592 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21593 function. */
21594
21595 static tree
21596 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
21597 tree& requires_clause, cp_cv_quals quals)
21598 {
21599 cp_token *token;
21600 tree type = NULL_TREE;
21601 bool declare_simd_p = (parser->omp_declare_simd
21602 && declarator
21603 && declarator->kind == cdk_id);
21604
21605 bool oacc_routine_p = (parser->oacc_routine
21606 && declarator
21607 && declarator->kind == cdk_id);
21608
21609 /* Peek at the next token. */
21610 token = cp_lexer_peek_token (parser->lexer);
21611 /* A late-specified return type is indicated by an initial '->'. */
21612 if (token->type != CPP_DEREF
21613 && token->keyword != RID_REQUIRES
21614 && !(token->type == CPP_NAME
21615 && token->u.value == ridpointers[RID_REQUIRES])
21616 && !(declare_simd_p || oacc_routine_p))
21617 return NULL_TREE;
21618
21619 tree save_ccp = current_class_ptr;
21620 tree save_ccr = current_class_ref;
21621 if (quals >= 0)
21622 {
21623 /* DR 1207: 'this' is in scope in the trailing return type. */
21624 inject_this_parameter (current_class_type, quals);
21625 }
21626
21627 if (token->type == CPP_DEREF)
21628 {
21629 /* Consume the ->. */
21630 cp_lexer_consume_token (parser->lexer);
21631
21632 type = cp_parser_trailing_type_id (parser);
21633 }
21634
21635 /* Function declarations may be followed by a trailing
21636 requires-clause. */
21637 requires_clause = cp_parser_requires_clause_opt (parser);
21638
21639 if (declare_simd_p)
21640 declarator->attributes
21641 = cp_parser_late_parsing_omp_declare_simd (parser,
21642 declarator->attributes);
21643 if (oacc_routine_p)
21644 declarator->attributes
21645 = cp_parser_late_parsing_oacc_routine (parser,
21646 declarator->attributes);
21647
21648 if (quals >= 0)
21649 {
21650 current_class_ptr = save_ccp;
21651 current_class_ref = save_ccr;
21652 }
21653
21654 return type;
21655 }
21656
21657 /* Parse a declarator-id.
21658
21659 declarator-id:
21660 id-expression
21661 :: [opt] nested-name-specifier [opt] type-name
21662
21663 In the `id-expression' case, the value returned is as for
21664 cp_parser_id_expression if the id-expression was an unqualified-id.
21665 If the id-expression was a qualified-id, then a SCOPE_REF is
21666 returned. The first operand is the scope (either a NAMESPACE_DECL
21667 or TREE_TYPE), but the second is still just a representation of an
21668 unqualified-id. */
21669
21670 static tree
21671 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
21672 {
21673 tree id;
21674 /* The expression must be an id-expression. Assume that qualified
21675 names are the names of types so that:
21676
21677 template <class T>
21678 int S<T>::R::i = 3;
21679
21680 will work; we must treat `S<T>::R' as the name of a type.
21681 Similarly, assume that qualified names are templates, where
21682 required, so that:
21683
21684 template <class T>
21685 int S<T>::R<T>::i = 3;
21686
21687 will work, too. */
21688 id = cp_parser_id_expression (parser,
21689 /*template_keyword_p=*/false,
21690 /*check_dependency_p=*/false,
21691 /*template_p=*/NULL,
21692 /*declarator_p=*/true,
21693 optional_p);
21694 if (id && BASELINK_P (id))
21695 id = BASELINK_FUNCTIONS (id);
21696 return id;
21697 }
21698
21699 /* Parse a type-id.
21700
21701 type-id:
21702 type-specifier-seq abstract-declarator [opt]
21703
21704 The parser flags FLAGS is used to control type-specifier parsing.
21705
21706 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
21707
21708 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21709 i.e. we've just seen "->".
21710
21711 Returns the TYPE specified. */
21712
21713 static tree
21714 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
21715 bool is_template_arg, bool is_trailing_return,
21716 location_t *type_location)
21717 {
21718 cp_decl_specifier_seq type_specifier_seq;
21719 cp_declarator *abstract_declarator;
21720
21721 /* Parse the type-specifier-seq. */
21722 cp_parser_type_specifier_seq (parser, flags,
21723 /*is_declaration=*/false,
21724 is_trailing_return,
21725 &type_specifier_seq);
21726 if (type_location)
21727 *type_location = type_specifier_seq.locations[ds_type_spec];
21728
21729 if (is_template_arg && type_specifier_seq.type
21730 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
21731 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
21732 /* A bare template name as a template argument is a template template
21733 argument, not a placeholder, so fail parsing it as a type argument. */
21734 {
21735 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21736 cp_parser_simulate_error (parser);
21737 return error_mark_node;
21738 }
21739 if (type_specifier_seq.type == error_mark_node)
21740 return error_mark_node;
21741
21742 /* There might or might not be an abstract declarator. */
21743 cp_parser_parse_tentatively (parser);
21744 /* Look for the declarator. */
21745 abstract_declarator
21746 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
21747 CP_PARSER_FLAGS_NONE, NULL,
21748 /*parenthesized_p=*/NULL,
21749 /*member_p=*/false,
21750 /*friend_p=*/false,
21751 /*static_p=*/false);
21752 /* Check to see if there really was a declarator. */
21753 if (!cp_parser_parse_definitely (parser))
21754 abstract_declarator = NULL;
21755
21756 if (type_specifier_seq.type
21757 /* The concepts TS allows 'auto' as a type-id. */
21758 && (!flag_concepts || parser->in_type_id_in_expr_p)
21759 /* None of the valid uses of 'auto' in C++14 involve the type-id
21760 nonterminal, but it is valid in a trailing-return-type. */
21761 && !(cxx_dialect >= cxx14 && is_trailing_return))
21762 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21763 {
21764 /* A type-id with type 'auto' is only ok if the abstract declarator
21765 is a function declarator with a late-specified return type.
21766
21767 A type-id with 'auto' is also valid in a trailing-return-type
21768 in a compound-requirement. */
21769 if (abstract_declarator
21770 && abstract_declarator->kind == cdk_function
21771 && abstract_declarator->u.function.late_return_type)
21772 /* OK */;
21773 else if (parser->in_result_type_constraint_p)
21774 /* OK */;
21775 else
21776 {
21777 location_t loc = type_specifier_seq.locations[ds_type_spec];
21778 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21779 {
21780 error_at (loc, "missing template arguments after %qT",
21781 auto_node);
21782 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21783 tmpl);
21784 }
21785 else
21786 error_at (loc, "invalid use of %qT", auto_node);
21787 return error_mark_node;
21788 }
21789 }
21790
21791 return groktypename (&type_specifier_seq, abstract_declarator,
21792 is_template_arg);
21793 }
21794
21795 /* Wrapper for cp_parser_type_id_1. */
21796
21797 static tree
21798 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
21799 location_t *type_location)
21800 {
21801 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
21802 }
21803
21804 /* Wrapper for cp_parser_type_id_1. */
21805
21806 static tree
21807 cp_parser_template_type_arg (cp_parser *parser)
21808 {
21809 tree r;
21810 const char *saved_message = parser->type_definition_forbidden_message;
21811 parser->type_definition_forbidden_message
21812 = G_("types may not be defined in template arguments");
21813 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
21814 parser->type_definition_forbidden_message = saved_message;
21815 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21816 {
21817 error ("invalid use of %<auto%> in template argument");
21818 r = error_mark_node;
21819 }
21820 return r;
21821 }
21822
21823 /* Wrapper for cp_parser_type_id_1. */
21824
21825 static tree
21826 cp_parser_trailing_type_id (cp_parser *parser)
21827 {
21828 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21829 false, true, NULL);
21830 }
21831
21832 /* Parse a type-specifier-seq.
21833
21834 type-specifier-seq:
21835 type-specifier type-specifier-seq [opt]
21836
21837 GNU extension:
21838
21839 type-specifier-seq:
21840 attributes type-specifier-seq [opt]
21841
21842 The parser flags FLAGS is used to control type-specifier parsing.
21843
21844 If IS_DECLARATION is true, we are at the start of a "condition" or
21845 exception-declaration, so we might be followed by a declarator-id.
21846
21847 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21848 i.e. we've just seen "->".
21849
21850 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21851
21852 static void
21853 cp_parser_type_specifier_seq (cp_parser* parser,
21854 cp_parser_flags flags,
21855 bool is_declaration,
21856 bool is_trailing_return,
21857 cp_decl_specifier_seq *type_specifier_seq)
21858 {
21859 bool seen_type_specifier = false;
21860 cp_token *start_token = NULL;
21861
21862 /* Clear the TYPE_SPECIFIER_SEQ. */
21863 clear_decl_specs (type_specifier_seq);
21864
21865 flags |= CP_PARSER_FLAGS_OPTIONAL;
21866 /* In the context of a trailing return type, enum E { } is an
21867 elaborated-type-specifier followed by a function-body, not an
21868 enum-specifier. */
21869 if (is_trailing_return)
21870 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21871
21872 /* Parse the type-specifiers and attributes. */
21873 while (true)
21874 {
21875 tree type_specifier;
21876 bool is_cv_qualifier;
21877
21878 /* Check for attributes first. */
21879 if (cp_next_tokens_can_be_attribute_p (parser))
21880 {
21881 type_specifier_seq->attributes
21882 = attr_chainon (type_specifier_seq->attributes,
21883 cp_parser_attributes_opt (parser));
21884 continue;
21885 }
21886
21887 /* record the token of the beginning of the type specifier seq,
21888 for error reporting purposes*/
21889 if (!start_token)
21890 start_token = cp_lexer_peek_token (parser->lexer);
21891
21892 /* Look for the type-specifier. */
21893 type_specifier = cp_parser_type_specifier (parser,
21894 flags,
21895 type_specifier_seq,
21896 /*is_declaration=*/false,
21897 NULL,
21898 &is_cv_qualifier);
21899 if (!type_specifier)
21900 {
21901 /* If the first type-specifier could not be found, this is not a
21902 type-specifier-seq at all. */
21903 if (!seen_type_specifier)
21904 {
21905 /* Set in_declarator_p to avoid skipping to the semicolon. */
21906 int in_decl = parser->in_declarator_p;
21907 parser->in_declarator_p = true;
21908
21909 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21910 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21911 cp_parser_error (parser, "expected type-specifier");
21912
21913 parser->in_declarator_p = in_decl;
21914
21915 type_specifier_seq->type = error_mark_node;
21916 return;
21917 }
21918 /* If subsequent type-specifiers could not be found, the
21919 type-specifier-seq is complete. */
21920 break;
21921 }
21922
21923 seen_type_specifier = true;
21924 /* The standard says that a condition can be:
21925
21926 type-specifier-seq declarator = assignment-expression
21927
21928 However, given:
21929
21930 struct S {};
21931 if (int S = ...)
21932
21933 we should treat the "S" as a declarator, not as a
21934 type-specifier. The standard doesn't say that explicitly for
21935 type-specifier-seq, but it does say that for
21936 decl-specifier-seq in an ordinary declaration. Perhaps it
21937 would be clearer just to allow a decl-specifier-seq here, and
21938 then add a semantic restriction that if any decl-specifiers
21939 that are not type-specifiers appear, the program is invalid. */
21940 if (is_declaration && !is_cv_qualifier)
21941 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21942 }
21943 }
21944
21945 /* Return whether the function currently being declared has an associated
21946 template parameter list. */
21947
21948 static bool
21949 function_being_declared_is_template_p (cp_parser* parser)
21950 {
21951 if (!current_template_parms || processing_template_parmlist)
21952 return false;
21953
21954 if (parser->implicit_template_scope)
21955 return true;
21956
21957 if (at_class_scope_p ()
21958 && TYPE_BEING_DEFINED (current_class_type))
21959 return parser->num_template_parameter_lists != 0;
21960
21961 return ((int) parser->num_template_parameter_lists > template_class_depth
21962 (current_class_type));
21963 }
21964
21965 /* Parse a parameter-declaration-clause.
21966
21967 parameter-declaration-clause:
21968 parameter-declaration-list [opt] ... [opt]
21969 parameter-declaration-list , ...
21970
21971 The parser flags FLAGS is used to control type-specifier parsing.
21972
21973 Returns a representation for the parameter declarations. A return
21974 value of NULL indicates a parameter-declaration-clause consisting
21975 only of an ellipsis. */
21976
21977 static tree
21978 cp_parser_parameter_declaration_clause (cp_parser* parser,
21979 cp_parser_flags flags)
21980 {
21981 tree parameters;
21982 cp_token *token;
21983 bool ellipsis_p;
21984
21985 temp_override<bool> cleanup
21986 (parser->auto_is_implicit_function_template_parm_p);
21987
21988 if (!processing_specialization
21989 && !processing_template_parmlist
21990 && !processing_explicit_instantiation
21991 /* default_arg_ok_p tracks whether this is a parameter-clause for an
21992 actual function or a random abstract declarator. */
21993 && parser->default_arg_ok_p)
21994 if (!current_function_decl
21995 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21996 parser->auto_is_implicit_function_template_parm_p = true;
21997
21998 /* Peek at the next token. */
21999 token = cp_lexer_peek_token (parser->lexer);
22000 /* Check for trivial parameter-declaration-clauses. */
22001 if (token->type == CPP_ELLIPSIS)
22002 {
22003 /* Consume the `...' token. */
22004 cp_lexer_consume_token (parser->lexer);
22005 return NULL_TREE;
22006 }
22007 else if (token->type == CPP_CLOSE_PAREN)
22008 /* There are no parameters. */
22009 return void_list_node;
22010 /* Check for `(void)', too, which is a special case. */
22011 else if (token->keyword == RID_VOID
22012 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22013 == CPP_CLOSE_PAREN))
22014 {
22015 /* Consume the `void' token. */
22016 cp_lexer_consume_token (parser->lexer);
22017 /* There are no parameters. */
22018 return void_list_node;
22019 }
22020
22021 /* Parse the parameter-declaration-list. */
22022 parameters = cp_parser_parameter_declaration_list (parser, flags);
22023 /* If a parse error occurred while parsing the
22024 parameter-declaration-list, then the entire
22025 parameter-declaration-clause is erroneous. */
22026 if (parameters == error_mark_node)
22027 return NULL_TREE;
22028
22029 /* Peek at the next token. */
22030 token = cp_lexer_peek_token (parser->lexer);
22031 /* If it's a `,', the clause should terminate with an ellipsis. */
22032 if (token->type == CPP_COMMA)
22033 {
22034 /* Consume the `,'. */
22035 cp_lexer_consume_token (parser->lexer);
22036 /* Expect an ellipsis. */
22037 ellipsis_p
22038 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
22039 }
22040 /* It might also be `...' if the optional trailing `,' was
22041 omitted. */
22042 else if (token->type == CPP_ELLIPSIS)
22043 {
22044 /* Consume the `...' token. */
22045 cp_lexer_consume_token (parser->lexer);
22046 /* And remember that we saw it. */
22047 ellipsis_p = true;
22048 }
22049 else
22050 ellipsis_p = false;
22051
22052 /* Finish the parameter list. */
22053 if (!ellipsis_p)
22054 parameters = chainon (parameters, void_list_node);
22055
22056 return parameters;
22057 }
22058
22059 /* Parse a parameter-declaration-list.
22060
22061 parameter-declaration-list:
22062 parameter-declaration
22063 parameter-declaration-list , parameter-declaration
22064
22065 The parser flags FLAGS is used to control type-specifier parsing.
22066
22067 Returns a representation of the parameter-declaration-list, as for
22068 cp_parser_parameter_declaration_clause. However, the
22069 `void_list_node' is never appended to the list. */
22070
22071 static tree
22072 cp_parser_parameter_declaration_list (cp_parser* parser, cp_parser_flags flags)
22073 {
22074 tree parameters = NULL_TREE;
22075 tree *tail = &parameters;
22076 bool saved_in_unbraced_linkage_specification_p;
22077 int index = 0;
22078
22079 /* The special considerations that apply to a function within an
22080 unbraced linkage specifications do not apply to the parameters
22081 to the function. */
22082 saved_in_unbraced_linkage_specification_p
22083 = parser->in_unbraced_linkage_specification_p;
22084 parser->in_unbraced_linkage_specification_p = false;
22085
22086 /* Look for more parameters. */
22087 while (true)
22088 {
22089 cp_parameter_declarator *parameter;
22090 tree decl = error_mark_node;
22091 bool parenthesized_p = false;
22092
22093 /* Parse the parameter. */
22094 parameter
22095 = cp_parser_parameter_declaration (parser, flags,
22096 /*template_parm_p=*/false,
22097 &parenthesized_p);
22098
22099 /* We don't know yet if the enclosing context is deprecated, so wait
22100 and warn in grokparms if appropriate. */
22101 deprecated_state = DEPRECATED_SUPPRESS;
22102
22103 if (parameter)
22104 {
22105 decl = grokdeclarator (parameter->declarator,
22106 &parameter->decl_specifiers,
22107 PARM,
22108 parameter->default_argument != NULL_TREE,
22109 &parameter->decl_specifiers.attributes);
22110 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
22111 DECL_SOURCE_LOCATION (decl) = parameter->loc;
22112 }
22113
22114 deprecated_state = DEPRECATED_NORMAL;
22115
22116 /* If a parse error occurred parsing the parameter declaration,
22117 then the entire parameter-declaration-list is erroneous. */
22118 if (decl == error_mark_node)
22119 {
22120 parameters = error_mark_node;
22121 break;
22122 }
22123
22124 if (parameter->decl_specifiers.attributes)
22125 cplus_decl_attributes (&decl,
22126 parameter->decl_specifiers.attributes,
22127 0);
22128 if (DECL_NAME (decl))
22129 decl = pushdecl (decl);
22130
22131 if (decl != error_mark_node)
22132 {
22133 retrofit_lang_decl (decl);
22134 DECL_PARM_INDEX (decl) = ++index;
22135 DECL_PARM_LEVEL (decl) = function_parm_depth ();
22136 }
22137
22138 /* Add the new parameter to the list. */
22139 *tail = build_tree_list (parameter->default_argument, decl);
22140 tail = &TREE_CHAIN (*tail);
22141
22142 /* Peek at the next token. */
22143 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
22144 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
22145 /* These are for Objective-C++ */
22146 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22147 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22148 /* The parameter-declaration-list is complete. */
22149 break;
22150 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22151 {
22152 cp_token *token;
22153
22154 /* Peek at the next token. */
22155 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22156 /* If it's an ellipsis, then the list is complete. */
22157 if (token->type == CPP_ELLIPSIS)
22158 break;
22159 /* Otherwise, there must be more parameters. Consume the
22160 `,'. */
22161 cp_lexer_consume_token (parser->lexer);
22162 /* When parsing something like:
22163
22164 int i(float f, double d)
22165
22166 we can tell after seeing the declaration for "f" that we
22167 are not looking at an initialization of a variable "i",
22168 but rather at the declaration of a function "i".
22169
22170 Due to the fact that the parsing of template arguments
22171 (as specified to a template-id) requires backtracking we
22172 cannot use this technique when inside a template argument
22173 list. */
22174 if (!parser->in_template_argument_list_p
22175 && !parser->in_type_id_in_expr_p
22176 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22177 /* However, a parameter-declaration of the form
22178 "float(f)" (which is a valid declaration of a
22179 parameter "f") can also be interpreted as an
22180 expression (the conversion of "f" to "float"). */
22181 && !parenthesized_p)
22182 cp_parser_commit_to_tentative_parse (parser);
22183 }
22184 else
22185 {
22186 cp_parser_error (parser, "expected %<,%> or %<...%>");
22187 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
22188 cp_parser_skip_to_closing_parenthesis (parser,
22189 /*recovering=*/true,
22190 /*or_comma=*/false,
22191 /*consume_paren=*/false);
22192 break;
22193 }
22194 }
22195
22196 parser->in_unbraced_linkage_specification_p
22197 = saved_in_unbraced_linkage_specification_p;
22198
22199 /* Reset implicit_template_scope if we are about to leave the function
22200 parameter list that introduced it. Note that for out-of-line member
22201 definitions, there will be one or more class scopes before we get to
22202 the template parameter scope. */
22203
22204 if (cp_binding_level *its = parser->implicit_template_scope)
22205 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
22206 {
22207 while (maybe_its->kind == sk_class)
22208 maybe_its = maybe_its->level_chain;
22209 if (maybe_its == its)
22210 {
22211 parser->implicit_template_parms = 0;
22212 parser->implicit_template_scope = 0;
22213 }
22214 }
22215
22216 return parameters;
22217 }
22218
22219 /* Parse a parameter declaration.
22220
22221 parameter-declaration:
22222 decl-specifier-seq ... [opt] declarator
22223 decl-specifier-seq declarator = assignment-expression
22224 decl-specifier-seq ... [opt] abstract-declarator [opt]
22225 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22226
22227 The parser flags FLAGS is used to control type-specifier parsing.
22228
22229 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22230 declares a template parameter. (In that case, a non-nested `>'
22231 token encountered during the parsing of the assignment-expression
22232 is not interpreted as a greater-than operator.)
22233
22234 Returns a representation of the parameter, or NULL if an error
22235 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22236 true iff the declarator is of the form "(p)". */
22237
22238 static cp_parameter_declarator *
22239 cp_parser_parameter_declaration (cp_parser *parser,
22240 cp_parser_flags flags,
22241 bool template_parm_p,
22242 bool *parenthesized_p)
22243 {
22244 int declares_class_or_enum;
22245 cp_decl_specifier_seq decl_specifiers;
22246 cp_declarator *declarator;
22247 tree default_argument;
22248 cp_token *token = NULL, *declarator_token_start = NULL;
22249 const char *saved_message;
22250 bool template_parameter_pack_p = false;
22251
22252 /* In a template parameter, `>' is not an operator.
22253
22254 [temp.param]
22255
22256 When parsing a default template-argument for a non-type
22257 template-parameter, the first non-nested `>' is taken as the end
22258 of the template parameter-list rather than a greater-than
22259 operator. */
22260
22261 /* Type definitions may not appear in parameter types. */
22262 saved_message = parser->type_definition_forbidden_message;
22263 parser->type_definition_forbidden_message
22264 = G_("types may not be defined in parameter types");
22265
22266 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
22267 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22268 (current_template_parms)) : 0);
22269
22270 /* Parse the declaration-specifiers. */
22271 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22272 cp_parser_decl_specifier_seq (parser,
22273 flags,
22274 &decl_specifiers,
22275 &declares_class_or_enum);
22276
22277 /* Complain about missing 'typename' or other invalid type names. */
22278 if (!decl_specifiers.any_type_specifiers_p
22279 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22280 decl_specifiers.type = error_mark_node;
22281
22282 /* If an error occurred, there's no reason to attempt to parse the
22283 rest of the declaration. */
22284 if (cp_parser_error_occurred (parser))
22285 {
22286 parser->type_definition_forbidden_message = saved_message;
22287 return NULL;
22288 }
22289
22290 /* Peek at the next token. */
22291 token = cp_lexer_peek_token (parser->lexer);
22292
22293 /* If the next token is a `)', `,', `=', `>', or `...', then there
22294 is no declarator. However, when variadic templates are enabled,
22295 there may be a declarator following `...'. */
22296 if (token->type == CPP_CLOSE_PAREN
22297 || token->type == CPP_COMMA
22298 || token->type == CPP_EQ
22299 || token->type == CPP_GREATER)
22300 {
22301 declarator = NULL;
22302 if (parenthesized_p)
22303 *parenthesized_p = false;
22304 }
22305 /* Otherwise, there should be a declarator. */
22306 else
22307 {
22308 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22309 parser->default_arg_ok_p = false;
22310
22311 /* After seeing a decl-specifier-seq, if the next token is not a
22312 "(", there is no possibility that the code is a valid
22313 expression. Therefore, if parsing tentatively, we commit at
22314 this point. */
22315 if (!parser->in_template_argument_list_p
22316 /* In an expression context, having seen:
22317
22318 (int((char ...
22319
22320 we cannot be sure whether we are looking at a
22321 function-type (taking a "char" as a parameter) or a cast
22322 of some object of type "char" to "int". */
22323 && !parser->in_type_id_in_expr_p
22324 && cp_parser_uncommitted_to_tentative_parse_p (parser)
22325 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
22326 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
22327 cp_parser_commit_to_tentative_parse (parser);
22328 /* Parse the declarator. */
22329 declarator_token_start = token;
22330 declarator = cp_parser_declarator (parser,
22331 CP_PARSER_DECLARATOR_EITHER,
22332 CP_PARSER_FLAGS_NONE,
22333 /*ctor_dtor_or_conv_p=*/NULL,
22334 parenthesized_p,
22335 /*member_p=*/false,
22336 /*friend_p=*/false,
22337 /*static_p=*/false);
22338 parser->default_arg_ok_p = saved_default_arg_ok_p;
22339 /* After the declarator, allow more attributes. */
22340 decl_specifiers.attributes
22341 = attr_chainon (decl_specifiers.attributes,
22342 cp_parser_attributes_opt (parser));
22343
22344 /* If the declarator is a template parameter pack, remember that and
22345 clear the flag in the declarator itself so we don't get errors
22346 from grokdeclarator. */
22347 if (template_parm_p && declarator && declarator->parameter_pack_p)
22348 {
22349 declarator->parameter_pack_p = false;
22350 template_parameter_pack_p = true;
22351 }
22352 }
22353
22354 /* If the next token is an ellipsis, and we have not seen a declarator
22355 name, and if either the type of the declarator contains parameter
22356 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22357 for, eg, abbreviated integral type names), then we actually have a
22358 parameter pack expansion expression. Otherwise, leave the ellipsis
22359 for a C-style variadic function. */
22360 token = cp_lexer_peek_token (parser->lexer);
22361
22362 /* If a function parameter pack was specified and an implicit template
22363 parameter was introduced during cp_parser_parameter_declaration,
22364 change any implicit parameters introduced into packs. */
22365 if (parser->implicit_template_parms
22366 && ((token->type == CPP_ELLIPSIS
22367 && declarator_can_be_parameter_pack (declarator))
22368 || (declarator && declarator->parameter_pack_p)))
22369 {
22370 int latest_template_parm_idx = TREE_VEC_LENGTH
22371 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
22372
22373 if (latest_template_parm_idx != template_parm_idx)
22374 decl_specifiers.type = convert_generic_types_to_packs
22375 (decl_specifiers.type,
22376 template_parm_idx, latest_template_parm_idx);
22377 }
22378
22379 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22380 {
22381 tree type = decl_specifiers.type;
22382
22383 if (type && DECL_P (type))
22384 type = TREE_TYPE (type);
22385
22386 if (((type
22387 && TREE_CODE (type) != TYPE_PACK_EXPANSION
22388 && (template_parm_p || uses_parameter_packs (type)))
22389 || (!type && template_parm_p))
22390 && declarator_can_be_parameter_pack (declarator))
22391 {
22392 /* Consume the `...'. */
22393 cp_lexer_consume_token (parser->lexer);
22394 maybe_warn_variadic_templates ();
22395
22396 /* Build a pack expansion type */
22397 if (template_parm_p)
22398 template_parameter_pack_p = true;
22399 else if (declarator)
22400 declarator->parameter_pack_p = true;
22401 else
22402 decl_specifiers.type = make_pack_expansion (type);
22403 }
22404 }
22405
22406 /* The restriction on defining new types applies only to the type
22407 of the parameter, not to the default argument. */
22408 parser->type_definition_forbidden_message = saved_message;
22409
22410 /* If the next token is `=', then process a default argument. */
22411 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22412 {
22413 tree type = decl_specifiers.type;
22414 token = cp_lexer_peek_token (parser->lexer);
22415 /* If we are defining a class, then the tokens that make up the
22416 default argument must be saved and processed later. */
22417 if (!template_parm_p && at_class_scope_p ()
22418 && TYPE_BEING_DEFINED (current_class_type)
22419 && !LAMBDA_TYPE_P (current_class_type))
22420 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
22421
22422 // A constrained-type-specifier may declare a type template-parameter.
22423 else if (declares_constrained_type_template_parameter (type))
22424 default_argument
22425 = cp_parser_default_type_template_argument (parser);
22426
22427 // A constrained-type-specifier may declare a template-template-parameter.
22428 else if (declares_constrained_template_template_parameter (type))
22429 default_argument
22430 = cp_parser_default_template_template_argument (parser);
22431
22432 /* Outside of a class definition, we can just parse the
22433 assignment-expression. */
22434 else
22435 default_argument
22436 = cp_parser_default_argument (parser, template_parm_p);
22437
22438 if (!parser->default_arg_ok_p)
22439 {
22440 permerror (token->location,
22441 "default arguments are only "
22442 "permitted for function parameters");
22443 }
22444 else if ((declarator && declarator->parameter_pack_p)
22445 || template_parameter_pack_p
22446 || (decl_specifiers.type
22447 && PACK_EXPANSION_P (decl_specifiers.type)))
22448 {
22449 /* Find the name of the parameter pack. */
22450 cp_declarator *id_declarator = declarator;
22451 while (id_declarator && id_declarator->kind != cdk_id)
22452 id_declarator = id_declarator->declarator;
22453
22454 if (id_declarator && id_declarator->kind == cdk_id)
22455 error_at (declarator_token_start->location,
22456 template_parm_p
22457 ? G_("template parameter pack %qD "
22458 "cannot have a default argument")
22459 : G_("parameter pack %qD cannot have "
22460 "a default argument"),
22461 id_declarator->u.id.unqualified_name);
22462 else
22463 error_at (declarator_token_start->location,
22464 template_parm_p
22465 ? G_("template parameter pack cannot have "
22466 "a default argument")
22467 : G_("parameter pack cannot have a "
22468 "default argument"));
22469
22470 default_argument = NULL_TREE;
22471 }
22472 }
22473 else
22474 default_argument = NULL_TREE;
22475
22476 if (default_argument)
22477 STRIP_ANY_LOCATION_WRAPPER (default_argument);
22478
22479 /* Generate a location for the parameter, ranging from the start of the
22480 initial token to the end of the final token (using input_location for
22481 the latter, set up by cp_lexer_set_source_position_from_token when
22482 consuming tokens).
22483
22484 If we have a identifier, then use it for the caret location, e.g.
22485
22486 extern int callee (int one, int (*two)(int, int), float three);
22487 ~~~~~~^~~~~~~~~~~~~~
22488
22489 otherwise, reuse the start location for the caret location e.g.:
22490
22491 extern int callee (int one, int (*)(int, int), float three);
22492 ^~~~~~~~~~~~~~~~~
22493
22494 */
22495 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
22496 ? declarator->id_loc
22497 : decl_spec_token_start->location);
22498 location_t param_loc = make_location (caret_loc,
22499 decl_spec_token_start->location,
22500 input_location);
22501
22502 return make_parameter_declarator (&decl_specifiers,
22503 declarator,
22504 default_argument,
22505 param_loc,
22506 template_parameter_pack_p);
22507 }
22508
22509 /* Parse a default argument and return it.
22510
22511 TEMPLATE_PARM_P is true if this is a default argument for a
22512 non-type template parameter. */
22513 static tree
22514 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
22515 {
22516 tree default_argument = NULL_TREE;
22517 bool saved_greater_than_is_operator_p;
22518 unsigned char saved_local_variables_forbidden_p;
22519 bool non_constant_p, is_direct_init;
22520
22521 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22522 set correctly. */
22523 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
22524 parser->greater_than_is_operator_p = !template_parm_p;
22525 /* Local variable names (and the `this' keyword) may not
22526 appear in a default argument. */
22527 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
22528 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
22529 /* Parse the assignment-expression. */
22530 if (template_parm_p)
22531 push_deferring_access_checks (dk_no_deferred);
22532 tree saved_class_ptr = NULL_TREE;
22533 tree saved_class_ref = NULL_TREE;
22534 /* The "this" pointer is not valid in a default argument. */
22535 if (cfun)
22536 {
22537 saved_class_ptr = current_class_ptr;
22538 cp_function_chain->x_current_class_ptr = NULL_TREE;
22539 saved_class_ref = current_class_ref;
22540 cp_function_chain->x_current_class_ref = NULL_TREE;
22541 }
22542 default_argument
22543 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
22544 /* Restore the "this" pointer. */
22545 if (cfun)
22546 {
22547 cp_function_chain->x_current_class_ptr = saved_class_ptr;
22548 cp_function_chain->x_current_class_ref = saved_class_ref;
22549 }
22550 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
22551 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22552 if (template_parm_p)
22553 pop_deferring_access_checks ();
22554 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
22555 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
22556
22557 return default_argument;
22558 }
22559
22560 /* Parse a function-body.
22561
22562 function-body:
22563 compound_statement */
22564
22565 static void
22566 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
22567 {
22568 cp_parser_compound_statement (parser, NULL, (in_function_try_block
22569 ? BCS_TRY_BLOCK : BCS_NORMAL),
22570 true);
22571 }
22572
22573 /* Parse a ctor-initializer-opt followed by a function-body. Return
22574 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22575 is true we are parsing a function-try-block. */
22576
22577 static void
22578 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
22579 bool in_function_try_block)
22580 {
22581 tree body, list;
22582 const bool check_body_p =
22583 DECL_CONSTRUCTOR_P (current_function_decl)
22584 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
22585 tree last = NULL;
22586
22587 /* Begin the function body. */
22588 body = begin_function_body ();
22589 /* Parse the optional ctor-initializer. */
22590 cp_parser_ctor_initializer_opt (parser);
22591
22592 /* If we're parsing a constexpr constructor definition, we need
22593 to check that the constructor body is indeed empty. However,
22594 before we get to cp_parser_function_body lot of junk has been
22595 generated, so we can't just check that we have an empty block.
22596 Rather we take a snapshot of the outermost block, and check whether
22597 cp_parser_function_body changed its state. */
22598 if (check_body_p)
22599 {
22600 list = cur_stmt_list;
22601 if (STATEMENT_LIST_TAIL (list))
22602 last = STATEMENT_LIST_TAIL (list)->stmt;
22603 }
22604 /* Parse the function-body. */
22605 cp_parser_function_body (parser, in_function_try_block);
22606 if (check_body_p)
22607 check_constexpr_ctor_body (last, list, /*complain=*/true);
22608 /* Finish the function body. */
22609 finish_function_body (body);
22610 }
22611
22612 /* Parse an initializer.
22613
22614 initializer:
22615 = initializer-clause
22616 ( expression-list )
22617
22618 Returns an expression representing the initializer. If no
22619 initializer is present, NULL_TREE is returned.
22620
22621 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22622 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22623 set to TRUE if there is no initializer present. If there is an
22624 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22625 is set to true; otherwise it is set to false. */
22626
22627 static tree
22628 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
22629 bool* non_constant_p, bool subexpression_p)
22630 {
22631 cp_token *token;
22632 tree init;
22633
22634 /* Peek at the next token. */
22635 token = cp_lexer_peek_token (parser->lexer);
22636
22637 /* Let our caller know whether or not this initializer was
22638 parenthesized. */
22639 *is_direct_init = (token->type != CPP_EQ);
22640 /* Assume that the initializer is constant. */
22641 *non_constant_p = false;
22642
22643 if (token->type == CPP_EQ)
22644 {
22645 /* Consume the `='. */
22646 cp_lexer_consume_token (parser->lexer);
22647 /* Parse the initializer-clause. */
22648 init = cp_parser_initializer_clause (parser, non_constant_p);
22649 }
22650 else if (token->type == CPP_OPEN_PAREN)
22651 {
22652 vec<tree, va_gc> *vec;
22653 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
22654 /*cast_p=*/false,
22655 /*allow_expansion_p=*/true,
22656 non_constant_p);
22657 if (vec == NULL)
22658 return error_mark_node;
22659 init = build_tree_list_vec (vec);
22660 release_tree_vector (vec);
22661 }
22662 else if (token->type == CPP_OPEN_BRACE)
22663 {
22664 cp_lexer_set_source_position (parser->lexer);
22665 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
22666 init = cp_parser_braced_list (parser, non_constant_p);
22667 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
22668 }
22669 else
22670 {
22671 /* Anything else is an error. */
22672 cp_parser_error (parser, "expected initializer");
22673 init = error_mark_node;
22674 }
22675
22676 if (!subexpression_p && check_for_bare_parameter_packs (init))
22677 init = error_mark_node;
22678
22679 return init;
22680 }
22681
22682 /* Parse an initializer-clause.
22683
22684 initializer-clause:
22685 assignment-expression
22686 braced-init-list
22687
22688 Returns an expression representing the initializer.
22689
22690 If the `assignment-expression' production is used the value
22691 returned is simply a representation for the expression.
22692
22693 Otherwise, calls cp_parser_braced_list. */
22694
22695 static cp_expr
22696 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
22697 {
22698 cp_expr initializer;
22699
22700 /* Assume the expression is constant. */
22701 *non_constant_p = false;
22702
22703 /* If it is not a `{', then we are looking at an
22704 assignment-expression. */
22705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
22706 {
22707 initializer
22708 = cp_parser_constant_expression (parser,
22709 /*allow_non_constant_p=*/true,
22710 non_constant_p);
22711 }
22712 else
22713 initializer = cp_parser_braced_list (parser, non_constant_p);
22714
22715 return initializer;
22716 }
22717
22718 /* Parse a brace-enclosed initializer list.
22719
22720 braced-init-list:
22721 { initializer-list , [opt] }
22722 { designated-initializer-list , [opt] }
22723 { }
22724
22725 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
22726 the elements of the initializer-list (or NULL, if the last
22727 production is used). The TREE_TYPE for the CONSTRUCTOR will be
22728 NULL_TREE. There is no way to detect whether or not the optional
22729 trailing `,' was provided. NON_CONSTANT_P is as for
22730 cp_parser_initializer. */
22731
22732 static cp_expr
22733 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
22734 {
22735 tree initializer;
22736 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
22737
22738 /* Consume the `{' token. */
22739 matching_braces braces;
22740 braces.require_open (parser);
22741 /* Create a CONSTRUCTOR to represent the braced-initializer. */
22742 initializer = make_node (CONSTRUCTOR);
22743 /* If it's not a `}', then there is a non-trivial initializer. */
22744 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22745 {
22746 /* Parse the initializer list. */
22747 CONSTRUCTOR_ELTS (initializer)
22748 = cp_parser_initializer_list (parser, non_constant_p);
22749 /* A trailing `,' token is allowed. */
22750 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22751 cp_lexer_consume_token (parser->lexer);
22752 }
22753 else
22754 *non_constant_p = false;
22755 /* Now, there should be a trailing `}'. */
22756 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22757 braces.require_close (parser);
22758 TREE_TYPE (initializer) = init_list_type_node;
22759
22760 cp_expr result (initializer);
22761 /* Build a location of the form:
22762 { ... }
22763 ^~~~~~~
22764 with caret==start at the open brace, finish at the close brace. */
22765 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22766 result.set_location (combined_loc);
22767 return result;
22768 }
22769
22770 /* Consume tokens up to, and including, the next non-nested closing `]'.
22771 Returns true iff we found a closing `]'. */
22772
22773 static bool
22774 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22775 {
22776 unsigned square_depth = 0;
22777
22778 while (true)
22779 {
22780 cp_token * token = cp_lexer_peek_token (parser->lexer);
22781
22782 switch (token->type)
22783 {
22784 case CPP_PRAGMA_EOL:
22785 if (!parser->lexer->in_pragma)
22786 break;
22787 /* FALLTHRU */
22788 case CPP_EOF:
22789 /* If we've run out of tokens, then there is no closing `]'. */
22790 return false;
22791
22792 case CPP_OPEN_SQUARE:
22793 ++square_depth;
22794 break;
22795
22796 case CPP_CLOSE_SQUARE:
22797 if (!square_depth--)
22798 {
22799 cp_lexer_consume_token (parser->lexer);
22800 return true;
22801 }
22802 break;
22803
22804 default:
22805 break;
22806 }
22807
22808 /* Consume the token. */
22809 cp_lexer_consume_token (parser->lexer);
22810 }
22811 }
22812
22813 /* Return true if we are looking at an array-designator, false otherwise. */
22814
22815 static bool
22816 cp_parser_array_designator_p (cp_parser *parser)
22817 {
22818 /* Consume the `['. */
22819 cp_lexer_consume_token (parser->lexer);
22820
22821 cp_lexer_save_tokens (parser->lexer);
22822
22823 /* Skip tokens until the next token is a closing square bracket.
22824 If we find the closing `]', and the next token is a `=', then
22825 we are looking at an array designator. */
22826 bool array_designator_p
22827 = (cp_parser_skip_to_closing_square_bracket (parser)
22828 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22829
22830 /* Roll back the tokens we skipped. */
22831 cp_lexer_rollback_tokens (parser->lexer);
22832
22833 return array_designator_p;
22834 }
22835
22836 /* Parse an initializer-list.
22837
22838 initializer-list:
22839 initializer-clause ... [opt]
22840 initializer-list , initializer-clause ... [opt]
22841
22842 C++2A Extension:
22843
22844 designated-initializer-list:
22845 designated-initializer-clause
22846 designated-initializer-list , designated-initializer-clause
22847
22848 designated-initializer-clause:
22849 designator brace-or-equal-initializer
22850
22851 designator:
22852 . identifier
22853
22854 GNU Extension:
22855
22856 initializer-list:
22857 designation initializer-clause ...[opt]
22858 initializer-list , designation initializer-clause ...[opt]
22859
22860 designation:
22861 . identifier =
22862 identifier :
22863 [ constant-expression ] =
22864
22865 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22866 for the initializer. If the INDEX of the elt is non-NULL, it is the
22867 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22868 as for cp_parser_initializer. */
22869
22870 static vec<constructor_elt, va_gc> *
22871 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22872 {
22873 vec<constructor_elt, va_gc> *v = NULL;
22874 bool first_p = true;
22875 tree first_designator = NULL_TREE;
22876
22877 /* Assume all of the expressions are constant. */
22878 *non_constant_p = false;
22879
22880 /* Parse the rest of the list. */
22881 while (true)
22882 {
22883 cp_token *token;
22884 tree designator;
22885 tree initializer;
22886 bool clause_non_constant_p;
22887 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22888
22889 /* Handle the C++2A syntax, '. id ='. */
22890 if ((cxx_dialect >= cxx2a
22891 || cp_parser_allow_gnu_extensions_p (parser))
22892 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22893 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22894 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
22895 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
22896 == CPP_OPEN_BRACE)))
22897 {
22898 if (cxx_dialect < cxx2a)
22899 pedwarn (loc, OPT_Wpedantic,
22900 "C++ designated initializers only available with "
22901 "-std=c++2a or -std=gnu++2a");
22902 /* Consume the `.'. */
22903 cp_lexer_consume_token (parser->lexer);
22904 /* Consume the identifier. */
22905 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22906 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22907 /* Consume the `='. */
22908 cp_lexer_consume_token (parser->lexer);
22909 }
22910 /* Also, if the next token is an identifier and the following one is a
22911 colon, we are looking at the GNU designated-initializer
22912 syntax. */
22913 else if (cp_parser_allow_gnu_extensions_p (parser)
22914 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22915 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22916 == CPP_COLON))
22917 {
22918 /* Warn the user that they are using an extension. */
22919 pedwarn (loc, OPT_Wpedantic,
22920 "ISO C++ does not allow GNU designated initializers");
22921 /* Consume the identifier. */
22922 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22923 /* Consume the `:'. */
22924 cp_lexer_consume_token (parser->lexer);
22925 }
22926 /* Also handle C99 array designators, '[ const ] ='. */
22927 else if (cp_parser_allow_gnu_extensions_p (parser)
22928 && !c_dialect_objc ()
22929 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22930 {
22931 /* In C++11, [ could start a lambda-introducer. */
22932 bool non_const = false;
22933
22934 cp_parser_parse_tentatively (parser);
22935
22936 if (!cp_parser_array_designator_p (parser))
22937 {
22938 cp_parser_simulate_error (parser);
22939 designator = NULL_TREE;
22940 }
22941 else
22942 {
22943 designator = cp_parser_constant_expression (parser, true,
22944 &non_const);
22945 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22946 cp_parser_require (parser, CPP_EQ, RT_EQ);
22947 }
22948
22949 if (!cp_parser_parse_definitely (parser))
22950 designator = NULL_TREE;
22951 else if (non_const
22952 && (!require_potential_rvalue_constant_expression
22953 (designator)))
22954 designator = NULL_TREE;
22955 if (designator)
22956 /* Warn the user that they are using an extension. */
22957 pedwarn (loc, OPT_Wpedantic,
22958 "ISO C++ does not allow C99 designated initializers");
22959 }
22960 else
22961 designator = NULL_TREE;
22962
22963 if (first_p)
22964 {
22965 first_designator = designator;
22966 first_p = false;
22967 }
22968 else if (cxx_dialect >= cxx2a
22969 && first_designator != error_mark_node
22970 && (!first_designator != !designator))
22971 {
22972 error_at (loc, "either all initializer clauses should be designated "
22973 "or none of them should be");
22974 first_designator = error_mark_node;
22975 }
22976 else if (cxx_dialect < cxx2a && !first_designator)
22977 first_designator = designator;
22978
22979 /* Parse the initializer. */
22980 initializer = cp_parser_initializer_clause (parser,
22981 &clause_non_constant_p);
22982 /* If any clause is non-constant, so is the entire initializer. */
22983 if (clause_non_constant_p)
22984 *non_constant_p = true;
22985
22986 /* If we have an ellipsis, this is an initializer pack
22987 expansion. */
22988 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22989 {
22990 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22991
22992 /* Consume the `...'. */
22993 cp_lexer_consume_token (parser->lexer);
22994
22995 if (designator && cxx_dialect >= cxx2a)
22996 error_at (loc,
22997 "%<...%> not allowed in designated initializer list");
22998
22999 /* Turn the initializer into an initializer expansion. */
23000 initializer = make_pack_expansion (initializer);
23001 }
23002
23003 /* Add it to the vector. */
23004 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
23005
23006 /* If the next token is not a comma, we have reached the end of
23007 the list. */
23008 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23009 break;
23010
23011 /* Peek at the next token. */
23012 token = cp_lexer_peek_nth_token (parser->lexer, 2);
23013 /* If the next token is a `}', then we're still done. An
23014 initializer-clause can have a trailing `,' after the
23015 initializer-list and before the closing `}'. */
23016 if (token->type == CPP_CLOSE_BRACE)
23017 break;
23018
23019 /* Consume the `,' token. */
23020 cp_lexer_consume_token (parser->lexer);
23021 }
23022
23023 /* The same identifier shall not appear in multiple designators
23024 of a designated-initializer-list. */
23025 if (first_designator)
23026 {
23027 unsigned int i;
23028 tree designator, val;
23029 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23030 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23031 {
23032 if (IDENTIFIER_MARKED (designator))
23033 {
23034 error_at (cp_expr_loc_or_loc (val, input_location),
23035 "%<.%s%> designator used multiple times in "
23036 "the same initializer list",
23037 IDENTIFIER_POINTER (designator));
23038 (*v)[i].index = error_mark_node;
23039 }
23040 else
23041 IDENTIFIER_MARKED (designator) = 1;
23042 }
23043 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
23044 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
23045 IDENTIFIER_MARKED (designator) = 0;
23046 }
23047
23048 return v;
23049 }
23050
23051 /* Classes [gram.class] */
23052
23053 /* Parse a class-name.
23054
23055 class-name:
23056 identifier
23057 template-id
23058
23059 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23060 to indicate that names looked up in dependent types should be
23061 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23062 keyword has been used to indicate that the name that appears next
23063 is a template. TAG_TYPE indicates the explicit tag given before
23064 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23065 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23066 is the class being defined in a class-head. If ENUM_OK is TRUE,
23067 enum-names are also accepted.
23068
23069 Returns the TYPE_DECL representing the class. */
23070
23071 static tree
23072 cp_parser_class_name (cp_parser *parser,
23073 bool typename_keyword_p,
23074 bool template_keyword_p,
23075 enum tag_types tag_type,
23076 bool check_dependency_p,
23077 bool class_head_p,
23078 bool is_declaration,
23079 bool enum_ok)
23080 {
23081 tree decl;
23082 tree scope;
23083 bool typename_p;
23084 cp_token *token;
23085 tree identifier = NULL_TREE;
23086
23087 /* All class-names start with an identifier. */
23088 token = cp_lexer_peek_token (parser->lexer);
23089 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
23090 {
23091 cp_parser_error (parser, "expected class-name");
23092 return error_mark_node;
23093 }
23094
23095 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23096 to a template-id, so we save it here. */
23097 scope = parser->scope;
23098 if (scope == error_mark_node)
23099 return error_mark_node;
23100
23101 /* Any name names a type if we're following the `typename' keyword
23102 in a qualified name where the enclosing scope is type-dependent. */
23103 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
23104 && dependent_type_p (scope));
23105 /* Handle the common case (an identifier, but not a template-id)
23106 efficiently. */
23107 if (token->type == CPP_NAME
23108 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
23109 {
23110 cp_token *identifier_token;
23111 bool ambiguous_p;
23112
23113 /* Look for the identifier. */
23114 identifier_token = cp_lexer_peek_token (parser->lexer);
23115 ambiguous_p = identifier_token->error_reported;
23116 identifier = cp_parser_identifier (parser);
23117 /* If the next token isn't an identifier, we are certainly not
23118 looking at a class-name. */
23119 if (identifier == error_mark_node)
23120 decl = error_mark_node;
23121 /* If we know this is a type-name, there's no need to look it
23122 up. */
23123 else if (typename_p)
23124 decl = identifier;
23125 else
23126 {
23127 tree ambiguous_decls;
23128 /* If we already know that this lookup is ambiguous, then
23129 we've already issued an error message; there's no reason
23130 to check again. */
23131 if (ambiguous_p)
23132 {
23133 cp_parser_simulate_error (parser);
23134 return error_mark_node;
23135 }
23136 /* If the next token is a `::', then the name must be a type
23137 name.
23138
23139 [basic.lookup.qual]
23140
23141 During the lookup for a name preceding the :: scope
23142 resolution operator, object, function, and enumerator
23143 names are ignored. */
23144 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23145 tag_type = scope_type;
23146 /* Look up the name. */
23147 decl = cp_parser_lookup_name (parser, identifier,
23148 tag_type,
23149 /*is_template=*/false,
23150 /*is_namespace=*/false,
23151 check_dependency_p,
23152 &ambiguous_decls,
23153 identifier_token->location);
23154 if (ambiguous_decls)
23155 {
23156 if (cp_parser_parsing_tentatively (parser))
23157 cp_parser_simulate_error (parser);
23158 return error_mark_node;
23159 }
23160 }
23161 }
23162 else
23163 {
23164 /* Try a template-id. */
23165 decl = cp_parser_template_id (parser, template_keyword_p,
23166 check_dependency_p,
23167 tag_type,
23168 is_declaration);
23169 if (decl == error_mark_node)
23170 return error_mark_node;
23171 }
23172
23173 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
23174
23175 /* If this is a typename, create a TYPENAME_TYPE. */
23176 if (typename_p
23177 && decl != error_mark_node
23178 && !is_overloaded_fn (decl))
23179 {
23180 decl = make_typename_type (scope, decl, typename_type,
23181 /*complain=*/tf_error);
23182 if (decl != error_mark_node)
23183 decl = TYPE_NAME (decl);
23184 }
23185
23186 decl = strip_using_decl (decl);
23187
23188 /* Check to see that it is really the name of a class. */
23189 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
23190 && identifier_p (TREE_OPERAND (decl, 0))
23191 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
23192 /* Situations like this:
23193
23194 template <typename T> struct A {
23195 typename T::template X<int>::I i;
23196 };
23197
23198 are problematic. Is `T::template X<int>' a class-name? The
23199 standard does not seem to be definitive, but there is no other
23200 valid interpretation of the following `::'. Therefore, those
23201 names are considered class-names. */
23202 {
23203 decl = make_typename_type (scope, decl, tag_type, tf_error);
23204 if (decl != error_mark_node)
23205 decl = TYPE_NAME (decl);
23206 }
23207 else if (TREE_CODE (decl) != TYPE_DECL
23208 || TREE_TYPE (decl) == error_mark_node
23209 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
23210 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
23211 /* In Objective-C 2.0, a classname followed by '.' starts a
23212 dot-syntax expression, and it's not a type-name. */
23213 || (c_dialect_objc ()
23214 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
23215 && objc_is_class_name (decl)))
23216 decl = error_mark_node;
23217
23218 if (decl == error_mark_node)
23219 cp_parser_error (parser, "expected class-name");
23220 else if (identifier && !parser->scope)
23221 maybe_note_name_used_in_class (identifier, decl);
23222
23223 return decl;
23224 }
23225
23226 /* Parse a class-specifier.
23227
23228 class-specifier:
23229 class-head { member-specification [opt] }
23230
23231 Returns the TREE_TYPE representing the class. */
23232
23233 static tree
23234 cp_parser_class_specifier_1 (cp_parser* parser)
23235 {
23236 tree type;
23237 tree attributes = NULL_TREE;
23238 bool nested_name_specifier_p;
23239 unsigned saved_num_template_parameter_lists;
23240 bool saved_in_function_body;
23241 unsigned char in_statement;
23242 bool in_switch_statement_p;
23243 bool saved_in_unbraced_linkage_specification_p;
23244 tree old_scope = NULL_TREE;
23245 tree scope = NULL_TREE;
23246 cp_token *closing_brace;
23247
23248 push_deferring_access_checks (dk_no_deferred);
23249
23250 /* Parse the class-head. */
23251 type = cp_parser_class_head (parser,
23252 &nested_name_specifier_p);
23253 /* If the class-head was a semantic disaster, skip the entire body
23254 of the class. */
23255 if (!type)
23256 {
23257 cp_parser_skip_to_end_of_block_or_statement (parser);
23258 pop_deferring_access_checks ();
23259 return error_mark_node;
23260 }
23261
23262 /* Look for the `{'. */
23263 matching_braces braces;
23264 if (!braces.require_open (parser))
23265 {
23266 pop_deferring_access_checks ();
23267 return error_mark_node;
23268 }
23269
23270 cp_ensure_no_omp_declare_simd (parser);
23271 cp_ensure_no_oacc_routine (parser);
23272
23273 /* Issue an error message if type-definitions are forbidden here. */
23274 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
23275 /* Remember that we are defining one more class. */
23276 ++parser->num_classes_being_defined;
23277 /* Inside the class, surrounding template-parameter-lists do not
23278 apply. */
23279 saved_num_template_parameter_lists
23280 = parser->num_template_parameter_lists;
23281 parser->num_template_parameter_lists = 0;
23282 /* We are not in a function body. */
23283 saved_in_function_body = parser->in_function_body;
23284 parser->in_function_body = false;
23285 /* Or in a loop. */
23286 in_statement = parser->in_statement;
23287 parser->in_statement = 0;
23288 /* Or in a switch. */
23289 in_switch_statement_p = parser->in_switch_statement_p;
23290 parser->in_switch_statement_p = false;
23291 /* We are not immediately inside an extern "lang" block. */
23292 saved_in_unbraced_linkage_specification_p
23293 = parser->in_unbraced_linkage_specification_p;
23294 parser->in_unbraced_linkage_specification_p = false;
23295
23296 // Associate constraints with the type.
23297 if (flag_concepts)
23298 type = associate_classtype_constraints (type);
23299
23300 /* Start the class. */
23301 if (nested_name_specifier_p)
23302 {
23303 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
23304 old_scope = push_inner_scope (scope);
23305 }
23306 type = begin_class_definition (type);
23307
23308 if (type == error_mark_node)
23309 /* If the type is erroneous, skip the entire body of the class. */
23310 cp_parser_skip_to_closing_brace (parser);
23311 else
23312 /* Parse the member-specification. */
23313 cp_parser_member_specification_opt (parser);
23314
23315 /* Look for the trailing `}'. */
23316 closing_brace = braces.require_close (parser);
23317 /* Look for trailing attributes to apply to this class. */
23318 if (cp_parser_allow_gnu_extensions_p (parser))
23319 attributes = cp_parser_gnu_attributes_opt (parser);
23320 if (type != error_mark_node)
23321 type = finish_struct (type, attributes);
23322 if (nested_name_specifier_p)
23323 pop_inner_scope (old_scope, scope);
23324
23325 /* We've finished a type definition. Check for the common syntax
23326 error of forgetting a semicolon after the definition. We need to
23327 be careful, as we can't just check for not-a-semicolon and be done
23328 with it; the user might have typed:
23329
23330 class X { } c = ...;
23331 class X { } *p = ...;
23332
23333 and so forth. Instead, enumerate all the possible tokens that
23334 might follow this production; if we don't see one of them, then
23335 complain and silently insert the semicolon. */
23336 {
23337 cp_token *token = cp_lexer_peek_token (parser->lexer);
23338 bool want_semicolon = true;
23339
23340 if (cp_next_tokens_can_be_std_attribute_p (parser))
23341 /* Don't try to parse c++11 attributes here. As per the
23342 grammar, that should be a task for
23343 cp_parser_decl_specifier_seq. */
23344 want_semicolon = false;
23345
23346 switch (token->type)
23347 {
23348 case CPP_NAME:
23349 case CPP_SEMICOLON:
23350 case CPP_MULT:
23351 case CPP_AND:
23352 case CPP_OPEN_PAREN:
23353 case CPP_CLOSE_PAREN:
23354 case CPP_COMMA:
23355 want_semicolon = false;
23356 break;
23357
23358 /* While it's legal for type qualifiers and storage class
23359 specifiers to follow type definitions in the grammar, only
23360 compiler testsuites contain code like that. Assume that if
23361 we see such code, then what we're really seeing is a case
23362 like:
23363
23364 class X { }
23365 const <type> var = ...;
23366
23367 or
23368
23369 class Y { }
23370 static <type> func (...) ...
23371
23372 i.e. the qualifier or specifier applies to the next
23373 declaration. To do so, however, we need to look ahead one
23374 more token to see if *that* token is a type specifier.
23375
23376 This code could be improved to handle:
23377
23378 class Z { }
23379 static const <type> var = ...; */
23380 case CPP_KEYWORD:
23381 if (keyword_is_decl_specifier (token->keyword))
23382 {
23383 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
23384
23385 /* Handling user-defined types here would be nice, but very
23386 tricky. */
23387 want_semicolon
23388 = (lookahead->type == CPP_KEYWORD
23389 && keyword_begins_type_specifier (lookahead->keyword));
23390 }
23391 break;
23392 default:
23393 break;
23394 }
23395
23396 /* If we don't have a type, then something is very wrong and we
23397 shouldn't try to do anything clever. Likewise for not seeing the
23398 closing brace. */
23399 if (closing_brace && TYPE_P (type) && want_semicolon)
23400 {
23401 /* Locate the closing brace. */
23402 cp_token_position prev
23403 = cp_lexer_previous_token_position (parser->lexer);
23404 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
23405 location_t loc = prev_token->location;
23406
23407 /* We want to suggest insertion of a ';' immediately *after* the
23408 closing brace, so, if we can, offset the location by 1 column. */
23409 location_t next_loc = loc;
23410 if (!linemap_location_from_macro_expansion_p (line_table, loc))
23411 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
23412
23413 rich_location richloc (line_table, next_loc);
23414
23415 /* If we successfully offset the location, suggest the fix-it. */
23416 if (next_loc != loc)
23417 richloc.add_fixit_insert_before (next_loc, ";");
23418
23419 if (CLASSTYPE_DECLARED_CLASS (type))
23420 error_at (&richloc,
23421 "expected %<;%> after class definition");
23422 else if (TREE_CODE (type) == RECORD_TYPE)
23423 error_at (&richloc,
23424 "expected %<;%> after struct definition");
23425 else if (TREE_CODE (type) == UNION_TYPE)
23426 error_at (&richloc,
23427 "expected %<;%> after union definition");
23428 else
23429 gcc_unreachable ();
23430
23431 /* Unget one token and smash it to look as though we encountered
23432 a semicolon in the input stream. */
23433 cp_lexer_set_token_position (parser->lexer, prev);
23434 token = cp_lexer_peek_token (parser->lexer);
23435 token->type = CPP_SEMICOLON;
23436 token->keyword = RID_MAX;
23437 }
23438 }
23439
23440 /* If this class is not itself within the scope of another class,
23441 then we need to parse the bodies of all of the queued function
23442 definitions. Note that the queued functions defined in a class
23443 are not always processed immediately following the
23444 class-specifier for that class. Consider:
23445
23446 struct A {
23447 struct B { void f() { sizeof (A); } };
23448 };
23449
23450 If `f' were processed before the processing of `A' were
23451 completed, there would be no way to compute the size of `A'.
23452 Note that the nesting we are interested in here is lexical --
23453 not the semantic nesting given by TYPE_CONTEXT. In particular,
23454 for:
23455
23456 struct A { struct B; };
23457 struct A::B { void f() { } };
23458
23459 there is no need to delay the parsing of `A::B::f'. */
23460 if (--parser->num_classes_being_defined == 0)
23461 {
23462 tree decl;
23463 tree class_type = NULL_TREE;
23464 tree pushed_scope = NULL_TREE;
23465 unsigned ix;
23466 cp_default_arg_entry *e;
23467 tree save_ccp, save_ccr;
23468
23469 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
23470 {
23471 /* Skip default arguments, NSDMIs, etc, in order to improve
23472 error recovery (c++/71169, c++/71832). */
23473 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23474 vec_safe_truncate (unparsed_nsdmis, 0);
23475 vec_safe_truncate (unparsed_classes, 0);
23476 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23477 }
23478
23479 /* In a first pass, parse default arguments to the functions.
23480 Then, in a second pass, parse the bodies of the functions.
23481 This two-phased approach handles cases like:
23482
23483 struct S {
23484 void f() { g(); }
23485 void g(int i = 3);
23486 };
23487
23488 */
23489 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
23490 {
23491 decl = e->decl;
23492 /* If there are default arguments that have not yet been processed,
23493 take care of them now. */
23494 if (class_type != e->class_type)
23495 {
23496 if (pushed_scope)
23497 pop_scope (pushed_scope);
23498 class_type = e->class_type;
23499 pushed_scope = push_scope (class_type);
23500 }
23501 /* Make sure that any template parameters are in scope. */
23502 maybe_begin_member_template_processing (decl);
23503 /* Parse the default argument expressions. */
23504 cp_parser_late_parsing_default_args (parser, decl);
23505 /* Remove any template parameters from the symbol table. */
23506 maybe_end_member_template_processing ();
23507 }
23508 vec_safe_truncate (unparsed_funs_with_default_args, 0);
23509 /* Now parse any NSDMIs. */
23510 save_ccp = current_class_ptr;
23511 save_ccr = current_class_ref;
23512 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
23513 {
23514 if (class_type != DECL_CONTEXT (decl))
23515 {
23516 if (pushed_scope)
23517 pop_scope (pushed_scope);
23518 class_type = DECL_CONTEXT (decl);
23519 pushed_scope = push_scope (class_type);
23520 }
23521 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
23522 cp_parser_late_parsing_nsdmi (parser, decl);
23523 }
23524 vec_safe_truncate (unparsed_nsdmis, 0);
23525 current_class_ptr = save_ccp;
23526 current_class_ref = save_ccr;
23527 if (pushed_scope)
23528 pop_scope (pushed_scope);
23529
23530 /* Now do some post-NSDMI bookkeeping. */
23531 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
23532 after_nsdmi_defaulted_late_checks (class_type);
23533 vec_safe_truncate (unparsed_classes, 0);
23534 after_nsdmi_defaulted_late_checks (type);
23535
23536 /* Now parse the body of the functions. */
23537 if (flag_openmp)
23538 {
23539 /* OpenMP UDRs need to be parsed before all other functions. */
23540 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23541 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
23542 cp_parser_late_parsing_for_member (parser, decl);
23543 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23544 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
23545 cp_parser_late_parsing_for_member (parser, decl);
23546 }
23547 else
23548 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
23549 cp_parser_late_parsing_for_member (parser, decl);
23550 vec_safe_truncate (unparsed_funs_with_definitions, 0);
23551 }
23552 else
23553 vec_safe_push (unparsed_classes, type);
23554
23555 /* Put back any saved access checks. */
23556 pop_deferring_access_checks ();
23557
23558 /* Restore saved state. */
23559 parser->in_switch_statement_p = in_switch_statement_p;
23560 parser->in_statement = in_statement;
23561 parser->in_function_body = saved_in_function_body;
23562 parser->num_template_parameter_lists
23563 = saved_num_template_parameter_lists;
23564 parser->in_unbraced_linkage_specification_p
23565 = saved_in_unbraced_linkage_specification_p;
23566
23567 return type;
23568 }
23569
23570 static tree
23571 cp_parser_class_specifier (cp_parser* parser)
23572 {
23573 tree ret;
23574 timevar_push (TV_PARSE_STRUCT);
23575 ret = cp_parser_class_specifier_1 (parser);
23576 timevar_pop (TV_PARSE_STRUCT);
23577 return ret;
23578 }
23579
23580 /* Parse a class-head.
23581
23582 class-head:
23583 class-key identifier [opt] base-clause [opt]
23584 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23585 class-key nested-name-specifier [opt] template-id
23586 base-clause [opt]
23587
23588 class-virt-specifier:
23589 final
23590
23591 GNU Extensions:
23592 class-key attributes identifier [opt] base-clause [opt]
23593 class-key attributes nested-name-specifier identifier base-clause [opt]
23594 class-key attributes nested-name-specifier [opt] template-id
23595 base-clause [opt]
23596
23597 Upon return BASES is initialized to the list of base classes (or
23598 NULL, if there are none) in the same form returned by
23599 cp_parser_base_clause.
23600
23601 Returns the TYPE of the indicated class. Sets
23602 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23603 involving a nested-name-specifier was used, and FALSE otherwise.
23604
23605 Returns error_mark_node if this is not a class-head.
23606
23607 Returns NULL_TREE if the class-head is syntactically valid, but
23608 semantically invalid in a way that means we should skip the entire
23609 body of the class. */
23610
23611 static tree
23612 cp_parser_class_head (cp_parser* parser,
23613 bool* nested_name_specifier_p)
23614 {
23615 tree nested_name_specifier;
23616 enum tag_types class_key;
23617 tree id = NULL_TREE;
23618 tree type = NULL_TREE;
23619 tree attributes;
23620 tree bases;
23621 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23622 bool template_id_p = false;
23623 bool qualified_p = false;
23624 bool invalid_nested_name_p = false;
23625 bool invalid_explicit_specialization_p = false;
23626 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23627 tree pushed_scope = NULL_TREE;
23628 unsigned num_templates;
23629 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
23630 /* Assume no nested-name-specifier will be present. */
23631 *nested_name_specifier_p = false;
23632 /* Assume no template parameter lists will be used in defining the
23633 type. */
23634 num_templates = 0;
23635 parser->colon_corrects_to_scope_p = false;
23636
23637 /* Look for the class-key. */
23638 class_key = cp_parser_class_key (parser);
23639 if (class_key == none_type)
23640 return error_mark_node;
23641
23642 location_t class_head_start_location = input_location;
23643
23644 /* Parse the attributes. */
23645 attributes = cp_parser_attributes_opt (parser);
23646
23647 /* If the next token is `::', that is invalid -- but sometimes
23648 people do try to write:
23649
23650 struct ::S {};
23651
23652 Handle this gracefully by accepting the extra qualifier, and then
23653 issuing an error about it later if this really is a
23654 class-head. If it turns out just to be an elaborated type
23655 specifier, remain silent. */
23656 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
23657 qualified_p = true;
23658
23659 push_deferring_access_checks (dk_no_check);
23660
23661 /* Determine the name of the class. Begin by looking for an
23662 optional nested-name-specifier. */
23663 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
23664 nested_name_specifier
23665 = cp_parser_nested_name_specifier_opt (parser,
23666 /*typename_keyword_p=*/false,
23667 /*check_dependency_p=*/false,
23668 /*type_p=*/true,
23669 /*is_declaration=*/false);
23670 /* If there was a nested-name-specifier, then there *must* be an
23671 identifier. */
23672
23673 cp_token *bad_template_keyword = NULL;
23674
23675 if (nested_name_specifier)
23676 {
23677 type_start_token = cp_lexer_peek_token (parser->lexer);
23678 /* Although the grammar says `identifier', it really means
23679 `class-name' or `template-name'. You are only allowed to
23680 define a class that has already been declared with this
23681 syntax.
23682
23683 The proposed resolution for Core Issue 180 says that wherever
23684 you see `class T::X' you should treat `X' as a type-name.
23685
23686 It is OK to define an inaccessible class; for example:
23687
23688 class A { class B; };
23689 class A::B {};
23690
23691 We do not know if we will see a class-name, or a
23692 template-name. We look for a class-name first, in case the
23693 class-name is a template-id; if we looked for the
23694 template-name first we would stop after the template-name. */
23695 cp_parser_parse_tentatively (parser);
23696 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23697 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
23698 type = cp_parser_class_name (parser,
23699 /*typename_keyword_p=*/false,
23700 /*template_keyword_p=*/false,
23701 class_type,
23702 /*check_dependency_p=*/false,
23703 /*class_head_p=*/true,
23704 /*is_declaration=*/false);
23705 /* If that didn't work, ignore the nested-name-specifier. */
23706 if (!cp_parser_parse_definitely (parser))
23707 {
23708 invalid_nested_name_p = true;
23709 type_start_token = cp_lexer_peek_token (parser->lexer);
23710 id = cp_parser_identifier (parser);
23711 if (id == error_mark_node)
23712 id = NULL_TREE;
23713 }
23714 /* If we could not find a corresponding TYPE, treat this
23715 declaration like an unqualified declaration. */
23716 if (type == error_mark_node)
23717 nested_name_specifier = NULL_TREE;
23718 /* Otherwise, count the number of templates used in TYPE and its
23719 containing scopes. */
23720 else
23721 num_templates = num_template_headers_for_class (TREE_TYPE (type));
23722 }
23723 /* Otherwise, the identifier is optional. */
23724 else
23725 {
23726 /* We don't know whether what comes next is a template-id,
23727 an identifier, or nothing at all. */
23728 cp_parser_parse_tentatively (parser);
23729 /* Check for a template-id. */
23730 type_start_token = cp_lexer_peek_token (parser->lexer);
23731 id = cp_parser_template_id (parser,
23732 /*template_keyword_p=*/false,
23733 /*check_dependency_p=*/true,
23734 class_key,
23735 /*is_declaration=*/true);
23736 /* If that didn't work, it could still be an identifier. */
23737 if (!cp_parser_parse_definitely (parser))
23738 {
23739 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23740 {
23741 type_start_token = cp_lexer_peek_token (parser->lexer);
23742 id = cp_parser_identifier (parser);
23743 }
23744 else
23745 id = NULL_TREE;
23746 }
23747 else
23748 {
23749 template_id_p = true;
23750 ++num_templates;
23751 }
23752 }
23753
23754 pop_deferring_access_checks ();
23755
23756 if (id)
23757 {
23758 cp_parser_check_for_invalid_template_id (parser, id,
23759 class_key,
23760 type_start_token->location);
23761 }
23762 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23763
23764 /* If it's not a `:' or a `{' then we can't really be looking at a
23765 class-head, since a class-head only appears as part of a
23766 class-specifier. We have to detect this situation before calling
23767 xref_tag, since that has irreversible side-effects. */
23768 if (!cp_parser_next_token_starts_class_definition_p (parser))
23769 {
23770 cp_parser_error (parser, "expected %<{%> or %<:%>");
23771 type = error_mark_node;
23772 goto out;
23773 }
23774
23775 /* At this point, we're going ahead with the class-specifier, even
23776 if some other problem occurs. */
23777 cp_parser_commit_to_tentative_parse (parser);
23778 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
23779 {
23780 cp_parser_error (parser,
23781 "cannot specify %<override%> for a class");
23782 type = error_mark_node;
23783 goto out;
23784 }
23785 /* Issue the error about the overly-qualified name now. */
23786 if (qualified_p)
23787 {
23788 cp_parser_error (parser,
23789 "global qualification of class name is invalid");
23790 type = error_mark_node;
23791 goto out;
23792 }
23793 else if (invalid_nested_name_p)
23794 {
23795 cp_parser_error (parser,
23796 "qualified name does not name a class");
23797 type = error_mark_node;
23798 goto out;
23799 }
23800 else if (nested_name_specifier)
23801 {
23802 tree scope;
23803
23804 if (bad_template_keyword)
23805 /* [temp.names]: in a qualified-id formed by a class-head-name, the
23806 keyword template shall not appear at the top level. */
23807 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
23808 "keyword %<template%> not allowed in class-head-name");
23809
23810 /* Reject typedef-names in class heads. */
23811 if (!DECL_IMPLICIT_TYPEDEF_P (type))
23812 {
23813 error_at (type_start_token->location,
23814 "invalid class name in declaration of %qD",
23815 type);
23816 type = NULL_TREE;
23817 goto done;
23818 }
23819
23820 /* Figure out in what scope the declaration is being placed. */
23821 scope = current_scope ();
23822 /* If that scope does not contain the scope in which the
23823 class was originally declared, the program is invalid. */
23824 if (scope && !is_ancestor (scope, nested_name_specifier))
23825 {
23826 if (at_namespace_scope_p ())
23827 error_at (type_start_token->location,
23828 "declaration of %qD in namespace %qD which does not "
23829 "enclose %qD",
23830 type, scope, nested_name_specifier);
23831 else
23832 error_at (type_start_token->location,
23833 "declaration of %qD in %qD which does not enclose %qD",
23834 type, scope, nested_name_specifier);
23835 type = NULL_TREE;
23836 goto done;
23837 }
23838 /* [dcl.meaning]
23839
23840 A declarator-id shall not be qualified except for the
23841 definition of a ... nested class outside of its class
23842 ... [or] the definition or explicit instantiation of a
23843 class member of a namespace outside of its namespace. */
23844 if (scope == nested_name_specifier)
23845 {
23846 permerror (nested_name_specifier_token_start->location,
23847 "extra qualification not allowed");
23848 nested_name_specifier = NULL_TREE;
23849 num_templates = 0;
23850 }
23851 }
23852 /* An explicit-specialization must be preceded by "template <>". If
23853 it is not, try to recover gracefully. */
23854 if (at_namespace_scope_p ()
23855 && parser->num_template_parameter_lists == 0
23856 && !processing_template_parmlist
23857 && template_id_p)
23858 {
23859 /* Build a location of this form:
23860 struct typename <ARGS>
23861 ^~~~~~~~~~~~~~~~~~~~~~
23862 with caret==start at the start token, and
23863 finishing at the end of the type. */
23864 location_t reported_loc
23865 = make_location (class_head_start_location,
23866 class_head_start_location,
23867 get_finish (type_start_token->location));
23868 rich_location richloc (line_table, reported_loc);
23869 richloc.add_fixit_insert_before (class_head_start_location,
23870 "template <> ");
23871 error_at (&richloc,
23872 "an explicit specialization must be preceded by"
23873 " %<template <>%>");
23874 invalid_explicit_specialization_p = true;
23875 /* Take the same action that would have been taken by
23876 cp_parser_explicit_specialization. */
23877 ++parser->num_template_parameter_lists;
23878 begin_specialization ();
23879 }
23880 /* There must be no "return" statements between this point and the
23881 end of this function; set "type "to the correct return value and
23882 use "goto done;" to return. */
23883 /* Make sure that the right number of template parameters were
23884 present. */
23885 if (!cp_parser_check_template_parameters (parser, num_templates,
23886 template_id_p,
23887 type_start_token->location,
23888 /*declarator=*/NULL))
23889 {
23890 /* If something went wrong, there is no point in even trying to
23891 process the class-definition. */
23892 type = NULL_TREE;
23893 goto done;
23894 }
23895
23896 /* Look up the type. */
23897 if (template_id_p)
23898 {
23899 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23900 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23901 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23902 {
23903 error_at (type_start_token->location,
23904 "function template %qD redeclared as a class template", id);
23905 type = error_mark_node;
23906 }
23907 else
23908 {
23909 type = TREE_TYPE (id);
23910 type = maybe_process_partial_specialization (type);
23911
23912 /* Check the scope while we still know whether or not we had a
23913 nested-name-specifier. */
23914 if (type != error_mark_node)
23915 check_unqualified_spec_or_inst (type, type_start_token->location);
23916 }
23917 if (nested_name_specifier)
23918 pushed_scope = push_scope (nested_name_specifier);
23919 }
23920 else if (nested_name_specifier)
23921 {
23922 tree class_type;
23923
23924 /* Given:
23925
23926 template <typename T> struct S { struct T };
23927 template <typename T> struct S<T>::T { };
23928
23929 we will get a TYPENAME_TYPE when processing the definition of
23930 `S::T'. We need to resolve it to the actual type before we
23931 try to define it. */
23932 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23933 {
23934 class_type = resolve_typename_type (TREE_TYPE (type),
23935 /*only_current_p=*/false);
23936 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23937 type = TYPE_NAME (class_type);
23938 else
23939 {
23940 cp_parser_error (parser, "could not resolve typename type");
23941 type = error_mark_node;
23942 }
23943 }
23944
23945 if (maybe_process_partial_specialization (TREE_TYPE (type))
23946 == error_mark_node)
23947 {
23948 type = NULL_TREE;
23949 goto done;
23950 }
23951
23952 class_type = current_class_type;
23953 /* Enter the scope indicated by the nested-name-specifier. */
23954 pushed_scope = push_scope (nested_name_specifier);
23955 /* Get the canonical version of this type. */
23956 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23957 /* Call push_template_decl if it seems like we should be defining a
23958 template either from the template headers or the type we're
23959 defining, so that we diagnose both extra and missing headers. */
23960 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23961 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23962 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23963 {
23964 type = push_template_decl (type);
23965 if (type == error_mark_node)
23966 {
23967 type = NULL_TREE;
23968 goto done;
23969 }
23970 }
23971
23972 type = TREE_TYPE (type);
23973 *nested_name_specifier_p = true;
23974 }
23975 else /* The name is not a nested name. */
23976 {
23977 /* If the class was unnamed, create a dummy name. */
23978 if (!id)
23979 id = make_anon_name ();
23980 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23981 ? ts_within_enclosing_non_class
23982 : ts_current);
23983 type = xref_tag (class_key, id, tag_scope,
23984 parser->num_template_parameter_lists);
23985 }
23986
23987 /* Indicate whether this class was declared as a `class' or as a
23988 `struct'. */
23989 if (TREE_CODE (type) == RECORD_TYPE)
23990 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23991 cp_parser_check_class_key (class_key, type);
23992
23993 /* If this type was already complete, and we see another definition,
23994 that's an error. */
23995 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23996 {
23997 error_at (type_start_token->location, "redefinition of %q#T",
23998 type);
23999 inform (location_of (type), "previous definition of %q#T",
24000 type);
24001 type = NULL_TREE;
24002 goto done;
24003 }
24004 else if (type == error_mark_node)
24005 type = NULL_TREE;
24006
24007 if (type)
24008 {
24009 /* Apply attributes now, before any use of the class as a template
24010 argument in its base list. */
24011 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
24012 fixup_attribute_variants (type);
24013 }
24014
24015 /* We will have entered the scope containing the class; the names of
24016 base classes should be looked up in that context. For example:
24017
24018 struct A { struct B {}; struct C; };
24019 struct A::C : B {};
24020
24021 is valid. */
24022
24023 /* Get the list of base-classes, if there is one. */
24024 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
24025 {
24026 /* PR59482: enter the class scope so that base-specifiers are looked
24027 up correctly. */
24028 if (type)
24029 pushclass (type);
24030 bases = cp_parser_base_clause (parser);
24031 /* PR59482: get out of the previously pushed class scope so that the
24032 subsequent pops pop the right thing. */
24033 if (type)
24034 popclass ();
24035 }
24036 else
24037 bases = NULL_TREE;
24038
24039 /* If we're really defining a class, process the base classes.
24040 If they're invalid, fail. */
24041 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24042 xref_basetypes (type, bases);
24043
24044 done:
24045 /* Leave the scope given by the nested-name-specifier. We will
24046 enter the class scope itself while processing the members. */
24047 if (pushed_scope)
24048 pop_scope (pushed_scope);
24049
24050 if (invalid_explicit_specialization_p)
24051 {
24052 end_specialization ();
24053 --parser->num_template_parameter_lists;
24054 }
24055
24056 if (type)
24057 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
24058 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
24059 CLASSTYPE_FINAL (type) = 1;
24060 out:
24061 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24062 return type;
24063 }
24064
24065 /* Parse a class-key.
24066
24067 class-key:
24068 class
24069 struct
24070 union
24071
24072 Returns the kind of class-key specified, or none_type to indicate
24073 error. */
24074
24075 static enum tag_types
24076 cp_parser_class_key (cp_parser* parser)
24077 {
24078 cp_token *token;
24079 enum tag_types tag_type;
24080
24081 /* Look for the class-key. */
24082 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
24083 if (!token)
24084 return none_type;
24085
24086 /* Check to see if the TOKEN is a class-key. */
24087 tag_type = cp_parser_token_is_class_key (token);
24088 if (!tag_type)
24089 cp_parser_error (parser, "expected class-key");
24090 return tag_type;
24091 }
24092
24093 /* Parse a type-parameter-key.
24094
24095 type-parameter-key:
24096 class
24097 typename
24098 */
24099
24100 static void
24101 cp_parser_type_parameter_key (cp_parser* parser)
24102 {
24103 /* Look for the type-parameter-key. */
24104 enum tag_types tag_type = none_type;
24105 cp_token *token = cp_lexer_peek_token (parser->lexer);
24106 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
24107 {
24108 cp_lexer_consume_token (parser->lexer);
24109 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
24110 /* typename is not allowed in a template template parameter
24111 by the standard until C++17. */
24112 pedwarn (token->location, OPT_Wpedantic,
24113 "ISO C++ forbids typename key in template template parameter;"
24114 " use -std=c++17 or -std=gnu++17");
24115 }
24116 else
24117 cp_parser_error (parser, "expected %<class%> or %<typename%>");
24118
24119 return;
24120 }
24121
24122 /* Parse an (optional) member-specification.
24123
24124 member-specification:
24125 member-declaration member-specification [opt]
24126 access-specifier : member-specification [opt] */
24127
24128 static void
24129 cp_parser_member_specification_opt (cp_parser* parser)
24130 {
24131 while (true)
24132 {
24133 cp_token *token;
24134 enum rid keyword;
24135
24136 /* Peek at the next token. */
24137 token = cp_lexer_peek_token (parser->lexer);
24138 /* If it's a `}', or EOF then we've seen all the members. */
24139 if (token->type == CPP_CLOSE_BRACE
24140 || token->type == CPP_EOF
24141 || token->type == CPP_PRAGMA_EOL)
24142 break;
24143
24144 /* See if this token is a keyword. */
24145 keyword = token->keyword;
24146 switch (keyword)
24147 {
24148 case RID_PUBLIC:
24149 case RID_PROTECTED:
24150 case RID_PRIVATE:
24151 /* Consume the access-specifier. */
24152 cp_lexer_consume_token (parser->lexer);
24153 /* Remember which access-specifier is active. */
24154 current_access_specifier = token->u.value;
24155 /* Look for the `:'. */
24156 cp_parser_require (parser, CPP_COLON, RT_COLON);
24157 break;
24158
24159 default:
24160 /* Accept #pragmas at class scope. */
24161 if (token->type == CPP_PRAGMA)
24162 {
24163 cp_parser_pragma (parser, pragma_member, NULL);
24164 break;
24165 }
24166
24167 /* Otherwise, the next construction must be a
24168 member-declaration. */
24169 cp_parser_member_declaration (parser);
24170 }
24171 }
24172 }
24173
24174 /* Parse a member-declaration.
24175
24176 member-declaration:
24177 decl-specifier-seq [opt] member-declarator-list [opt] ;
24178 function-definition ; [opt]
24179 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24180 using-declaration
24181 template-declaration
24182 alias-declaration
24183
24184 member-declarator-list:
24185 member-declarator
24186 member-declarator-list , member-declarator
24187
24188 member-declarator:
24189 declarator pure-specifier [opt]
24190 declarator constant-initializer [opt]
24191 identifier [opt] : constant-expression
24192
24193 GNU Extensions:
24194
24195 member-declaration:
24196 __extension__ member-declaration
24197
24198 member-declarator:
24199 declarator attributes [opt] pure-specifier [opt]
24200 declarator attributes [opt] constant-initializer [opt]
24201 identifier [opt] attributes [opt] : constant-expression
24202
24203 C++0x Extensions:
24204
24205 member-declaration:
24206 static_assert-declaration */
24207
24208 static void
24209 cp_parser_member_declaration (cp_parser* parser)
24210 {
24211 cp_decl_specifier_seq decl_specifiers;
24212 tree prefix_attributes;
24213 tree decl;
24214 int declares_class_or_enum;
24215 bool friend_p;
24216 cp_token *token = NULL;
24217 cp_token *decl_spec_token_start = NULL;
24218 cp_token *initializer_token_start = NULL;
24219 int saved_pedantic;
24220 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
24221
24222 /* Check for the `__extension__' keyword. */
24223 if (cp_parser_extension_opt (parser, &saved_pedantic))
24224 {
24225 /* Recurse. */
24226 cp_parser_member_declaration (parser);
24227 /* Restore the old value of the PEDANTIC flag. */
24228 pedantic = saved_pedantic;
24229
24230 return;
24231 }
24232
24233 /* Check for a template-declaration. */
24234 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24235 {
24236 /* An explicit specialization here is an error condition, and we
24237 expect the specialization handler to detect and report this. */
24238 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
24239 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
24240 cp_parser_explicit_specialization (parser);
24241 else
24242 cp_parser_template_declaration (parser, /*member_p=*/true);
24243
24244 return;
24245 }
24246 /* Check for a template introduction. */
24247 else if (cp_parser_template_declaration_after_export (parser, true))
24248 return;
24249
24250 /* Check for a using-declaration. */
24251 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24252 {
24253 if (cxx_dialect < cxx11)
24254 {
24255 /* Parse the using-declaration. */
24256 cp_parser_using_declaration (parser,
24257 /*access_declaration_p=*/false);
24258 return;
24259 }
24260 else
24261 {
24262 tree decl;
24263 bool alias_decl_expected;
24264 cp_parser_parse_tentatively (parser);
24265 decl = cp_parser_alias_declaration (parser);
24266 /* Note that if we actually see the '=' token after the
24267 identifier, cp_parser_alias_declaration commits the
24268 tentative parse. In that case, we really expect an
24269 alias-declaration. Otherwise, we expect a using
24270 declaration. */
24271 alias_decl_expected =
24272 !cp_parser_uncommitted_to_tentative_parse_p (parser);
24273 cp_parser_parse_definitely (parser);
24274
24275 if (alias_decl_expected)
24276 finish_member_declaration (decl);
24277 else
24278 cp_parser_using_declaration (parser,
24279 /*access_declaration_p=*/false);
24280 return;
24281 }
24282 }
24283
24284 /* Check for @defs. */
24285 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
24286 {
24287 tree ivar, member;
24288 tree ivar_chains = cp_parser_objc_defs_expression (parser);
24289 ivar = ivar_chains;
24290 while (ivar)
24291 {
24292 member = ivar;
24293 ivar = TREE_CHAIN (member);
24294 TREE_CHAIN (member) = NULL_TREE;
24295 finish_member_declaration (member);
24296 }
24297 return;
24298 }
24299
24300 /* If the next token is `static_assert' we have a static assertion. */
24301 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
24302 {
24303 cp_parser_static_assert (parser, /*member_p=*/true);
24304 return;
24305 }
24306
24307 parser->colon_corrects_to_scope_p = false;
24308
24309 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
24310 goto out;
24311
24312 /* Parse the decl-specifier-seq. */
24313 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24314 cp_parser_decl_specifier_seq (parser,
24315 (CP_PARSER_FLAGS_OPTIONAL
24316 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
24317 &decl_specifiers,
24318 &declares_class_or_enum);
24319 /* Check for an invalid type-name. */
24320 if (!decl_specifiers.any_type_specifiers_p
24321 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24322 goto out;
24323 /* If there is no declarator, then the decl-specifier-seq should
24324 specify a type. */
24325 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24326 {
24327 /* If there was no decl-specifier-seq, and the next token is a
24328 `;', then we have something like:
24329
24330 struct S { ; };
24331
24332 [class.mem]
24333
24334 Each member-declaration shall declare at least one member
24335 name of the class. */
24336 if (!decl_specifiers.any_specifiers_p)
24337 {
24338 cp_token *token = cp_lexer_peek_token (parser->lexer);
24339 if (!in_system_header_at (token->location))
24340 {
24341 gcc_rich_location richloc (token->location);
24342 richloc.add_fixit_remove ();
24343 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
24344 }
24345 }
24346 else
24347 {
24348 tree type;
24349
24350 /* See if this declaration is a friend. */
24351 friend_p = cp_parser_friend_p (&decl_specifiers);
24352 /* If there were decl-specifiers, check to see if there was
24353 a class-declaration. */
24354 type = check_tag_decl (&decl_specifiers,
24355 /*explicit_type_instantiation_p=*/false);
24356 /* Nested classes have already been added to the class, but
24357 a `friend' needs to be explicitly registered. */
24358 if (friend_p)
24359 {
24360 /* If the `friend' keyword was present, the friend must
24361 be introduced with a class-key. */
24362 if (!declares_class_or_enum && cxx_dialect < cxx11)
24363 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
24364 "in C++03 a class-key must be used "
24365 "when declaring a friend");
24366 /* In this case:
24367
24368 template <typename T> struct A {
24369 friend struct A<T>::B;
24370 };
24371
24372 A<T>::B will be represented by a TYPENAME_TYPE, and
24373 therefore not recognized by check_tag_decl. */
24374 if (!type)
24375 {
24376 type = decl_specifiers.type;
24377 if (type && TREE_CODE (type) == TYPE_DECL)
24378 type = TREE_TYPE (type);
24379 }
24380 if (!type || !TYPE_P (type))
24381 error_at (decl_spec_token_start->location,
24382 "friend declaration does not name a class or "
24383 "function");
24384 else
24385 make_friend_class (current_class_type, type,
24386 /*complain=*/true);
24387 }
24388 /* If there is no TYPE, an error message will already have
24389 been issued. */
24390 else if (!type || type == error_mark_node)
24391 ;
24392 /* An anonymous aggregate has to be handled specially; such
24393 a declaration really declares a data member (with a
24394 particular type), as opposed to a nested class. */
24395 else if (ANON_AGGR_TYPE_P (type))
24396 {
24397 /* C++11 9.5/6. */
24398 if (decl_specifiers.storage_class != sc_none)
24399 error_at (decl_spec_token_start->location,
24400 "a storage class on an anonymous aggregate "
24401 "in class scope is not allowed");
24402
24403 /* Remove constructors and such from TYPE, now that we
24404 know it is an anonymous aggregate. */
24405 fixup_anonymous_aggr (type);
24406 /* And make the corresponding data member. */
24407 decl = build_decl (decl_spec_token_start->location,
24408 FIELD_DECL, NULL_TREE, type);
24409 /* Add it to the class. */
24410 finish_member_declaration (decl);
24411 }
24412 else
24413 cp_parser_check_access_in_redeclaration
24414 (TYPE_NAME (type),
24415 decl_spec_token_start->location);
24416 }
24417 }
24418 else
24419 {
24420 bool assume_semicolon = false;
24421
24422 /* Clear attributes from the decl_specifiers but keep them
24423 around as prefix attributes that apply them to the entity
24424 being declared. */
24425 prefix_attributes = decl_specifiers.attributes;
24426 decl_specifiers.attributes = NULL_TREE;
24427
24428 /* See if these declarations will be friends. */
24429 friend_p = cp_parser_friend_p (&decl_specifiers);
24430
24431 /* Keep going until we hit the `;' at the end of the
24432 declaration. */
24433 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24434 {
24435 tree attributes = NULL_TREE;
24436 tree first_attribute;
24437 tree initializer;
24438 bool named_bitfld = false;
24439
24440 /* Peek at the next token. */
24441 token = cp_lexer_peek_token (parser->lexer);
24442
24443 /* The following code wants to know early if it is a bit-field
24444 or some other declaration. Attributes can appear before
24445 the `:' token. Skip over them without consuming any tokens
24446 to peek if they are followed by `:'. */
24447 if (cp_next_tokens_can_be_attribute_p (parser)
24448 || (token->type == CPP_NAME
24449 && cp_nth_tokens_can_be_attribute_p (parser, 2)
24450 && (named_bitfld = true)))
24451 {
24452 size_t n
24453 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
24454 token = cp_lexer_peek_nth_token (parser->lexer, n);
24455 }
24456
24457 /* Check for a bitfield declaration. */
24458 if (token->type == CPP_COLON
24459 || (token->type == CPP_NAME
24460 && token == cp_lexer_peek_token (parser->lexer)
24461 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
24462 && (named_bitfld = true)))
24463 {
24464 tree identifier;
24465 tree width;
24466 tree late_attributes = NULL_TREE;
24467 location_t id_location
24468 = cp_lexer_peek_token (parser->lexer)->location;
24469
24470 if (named_bitfld)
24471 identifier = cp_parser_identifier (parser);
24472 else
24473 identifier = NULL_TREE;
24474
24475 /* Look for attributes that apply to the bitfield. */
24476 attributes = cp_parser_attributes_opt (parser);
24477
24478 /* Consume the `:' token. */
24479 cp_lexer_consume_token (parser->lexer);
24480
24481 /* Get the width of the bitfield. */
24482 width = cp_parser_constant_expression (parser, false, NULL,
24483 cxx_dialect >= cxx11);
24484
24485 /* In C++2A and as extension for C++11 and above we allow
24486 default member initializers for bit-fields. */
24487 initializer = NULL_TREE;
24488 if (cxx_dialect >= cxx11
24489 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
24490 || cp_lexer_next_token_is (parser->lexer,
24491 CPP_OPEN_BRACE)))
24492 {
24493 location_t loc
24494 = cp_lexer_peek_token (parser->lexer)->location;
24495 if (cxx_dialect < cxx2a
24496 && !in_system_header_at (loc)
24497 && identifier != NULL_TREE)
24498 pedwarn (loc, 0,
24499 "default member initializers for bit-fields "
24500 "only available with -std=c++2a or "
24501 "-std=gnu++2a");
24502
24503 initializer = cp_parser_save_nsdmi (parser);
24504 if (identifier == NULL_TREE)
24505 {
24506 error_at (loc, "default member initializer for "
24507 "unnamed bit-field");
24508 initializer = NULL_TREE;
24509 }
24510 }
24511 else
24512 {
24513 /* Look for attributes that apply to the bitfield after
24514 the `:' token and width. This is where GCC used to
24515 parse attributes in the past, pedwarn if there is
24516 a std attribute. */
24517 if (cp_next_tokens_can_be_std_attribute_p (parser))
24518 pedwarn (input_location, OPT_Wpedantic,
24519 "ISO C++ allows bit-field attributes only "
24520 "before the %<:%> token");
24521
24522 late_attributes = cp_parser_attributes_opt (parser);
24523 }
24524
24525 attributes = attr_chainon (attributes, late_attributes);
24526
24527 /* Remember which attributes are prefix attributes and
24528 which are not. */
24529 first_attribute = attributes;
24530 /* Combine the attributes. */
24531 attributes = attr_chainon (prefix_attributes, attributes);
24532
24533 /* Create the bitfield declaration. */
24534 decl = grokbitfield (identifier
24535 ? make_id_declarator (NULL_TREE,
24536 identifier,
24537 sfk_none,
24538 id_location)
24539 : NULL,
24540 &decl_specifiers,
24541 width, initializer,
24542 attributes);
24543 }
24544 else
24545 {
24546 cp_declarator *declarator;
24547 tree asm_specification;
24548 int ctor_dtor_or_conv_p;
24549 bool static_p = (decl_specifiers.storage_class == sc_static);
24550
24551 /* Parse the declarator. */
24552 declarator
24553 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24554 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24555 &ctor_dtor_or_conv_p,
24556 /*parenthesized_p=*/NULL,
24557 /*member_p=*/true,
24558 friend_p, static_p);
24559
24560 /* If something went wrong parsing the declarator, make sure
24561 that we at least consume some tokens. */
24562 if (declarator == cp_error_declarator)
24563 {
24564 /* Skip to the end of the statement. */
24565 cp_parser_skip_to_end_of_statement (parser);
24566 /* If the next token is not a semicolon, that is
24567 probably because we just skipped over the body of
24568 a function. So, we consume a semicolon if
24569 present, but do not issue an error message if it
24570 is not present. */
24571 if (cp_lexer_next_token_is (parser->lexer,
24572 CPP_SEMICOLON))
24573 cp_lexer_consume_token (parser->lexer);
24574 goto out;
24575 }
24576
24577 if (declares_class_or_enum & 2)
24578 cp_parser_check_for_definition_in_return_type
24579 (declarator, decl_specifiers.type,
24580 decl_specifiers.locations[ds_type_spec]);
24581
24582 /* Look for an asm-specification. */
24583 asm_specification = cp_parser_asm_specification_opt (parser);
24584 /* Look for attributes that apply to the declaration. */
24585 attributes = cp_parser_attributes_opt (parser);
24586 /* Remember which attributes are prefix attributes and
24587 which are not. */
24588 first_attribute = attributes;
24589 /* Combine the attributes. */
24590 attributes = attr_chainon (prefix_attributes, attributes);
24591
24592 /* If it's an `=', then we have a constant-initializer or a
24593 pure-specifier. It is not correct to parse the
24594 initializer before registering the member declaration
24595 since the member declaration should be in scope while
24596 its initializer is processed. However, the rest of the
24597 front end does not yet provide an interface that allows
24598 us to handle this correctly. */
24599 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24600 {
24601 /* In [class.mem]:
24602
24603 A pure-specifier shall be used only in the declaration of
24604 a virtual function.
24605
24606 A member-declarator can contain a constant-initializer
24607 only if it declares a static member of integral or
24608 enumeration type.
24609
24610 Therefore, if the DECLARATOR is for a function, we look
24611 for a pure-specifier; otherwise, we look for a
24612 constant-initializer. When we call `grokfield', it will
24613 perform more stringent semantics checks. */
24614 initializer_token_start = cp_lexer_peek_token (parser->lexer);
24615 if (function_declarator_p (declarator)
24616 || (decl_specifiers.type
24617 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
24618 && declarator->kind == cdk_id
24619 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
24620 == FUNCTION_TYPE)))
24621 initializer = cp_parser_pure_specifier (parser);
24622 else if (decl_specifiers.storage_class != sc_static)
24623 initializer = cp_parser_save_nsdmi (parser);
24624 else if (cxx_dialect >= cxx11)
24625 {
24626 bool nonconst;
24627 /* Don't require a constant rvalue in C++11, since we
24628 might want a reference constant. We'll enforce
24629 constancy later. */
24630 cp_lexer_consume_token (parser->lexer);
24631 /* Parse the initializer. */
24632 initializer = cp_parser_initializer_clause (parser,
24633 &nonconst);
24634 }
24635 else
24636 /* Parse the initializer. */
24637 initializer = cp_parser_constant_initializer (parser);
24638 }
24639 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
24640 && !function_declarator_p (declarator))
24641 {
24642 bool x;
24643 if (decl_specifiers.storage_class != sc_static)
24644 initializer = cp_parser_save_nsdmi (parser);
24645 else
24646 initializer = cp_parser_initializer (parser, &x, &x);
24647 }
24648 /* Otherwise, there is no initializer. */
24649 else
24650 initializer = NULL_TREE;
24651
24652 /* See if we are probably looking at a function
24653 definition. We are certainly not looking at a
24654 member-declarator. Calling `grokfield' has
24655 side-effects, so we must not do it unless we are sure
24656 that we are looking at a member-declarator. */
24657 if (cp_parser_token_starts_function_definition_p
24658 (cp_lexer_peek_token (parser->lexer)))
24659 {
24660 /* The grammar does not allow a pure-specifier to be
24661 used when a member function is defined. (It is
24662 possible that this fact is an oversight in the
24663 standard, since a pure function may be defined
24664 outside of the class-specifier. */
24665 if (initializer && initializer_token_start)
24666 error_at (initializer_token_start->location,
24667 "pure-specifier on function-definition");
24668 decl = cp_parser_save_member_function_body (parser,
24669 &decl_specifiers,
24670 declarator,
24671 attributes);
24672 if (parser->fully_implicit_function_template_p)
24673 decl = finish_fully_implicit_template (parser, decl);
24674 /* If the member was not a friend, declare it here. */
24675 if (!friend_p)
24676 finish_member_declaration (decl);
24677 /* Peek at the next token. */
24678 token = cp_lexer_peek_token (parser->lexer);
24679 /* If the next token is a semicolon, consume it. */
24680 if (token->type == CPP_SEMICOLON)
24681 {
24682 location_t semicolon_loc
24683 = cp_lexer_consume_token (parser->lexer)->location;
24684 gcc_rich_location richloc (semicolon_loc);
24685 richloc.add_fixit_remove ();
24686 warning_at (&richloc, OPT_Wextra_semi,
24687 "extra %<;%> after in-class "
24688 "function definition");
24689 }
24690 goto out;
24691 }
24692 else
24693 if (declarator->kind == cdk_function)
24694 declarator->id_loc = token->location;
24695 /* Create the declaration. */
24696 decl = grokfield (declarator, &decl_specifiers,
24697 initializer, /*init_const_expr_p=*/true,
24698 asm_specification, attributes);
24699 if (parser->fully_implicit_function_template_p)
24700 {
24701 if (friend_p)
24702 finish_fully_implicit_template (parser, 0);
24703 else
24704 decl = finish_fully_implicit_template (parser, decl);
24705 }
24706 }
24707
24708 cp_finalize_omp_declare_simd (parser, decl);
24709 cp_finalize_oacc_routine (parser, decl, false);
24710
24711 /* Reset PREFIX_ATTRIBUTES. */
24712 if (attributes != error_mark_node)
24713 {
24714 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24715 attributes = TREE_CHAIN (attributes);
24716 if (attributes)
24717 TREE_CHAIN (attributes) = NULL_TREE;
24718 }
24719
24720 /* If there is any qualification still in effect, clear it
24721 now; we will be starting fresh with the next declarator. */
24722 parser->scope = NULL_TREE;
24723 parser->qualifying_scope = NULL_TREE;
24724 parser->object_scope = NULL_TREE;
24725 /* If it's a `,', then there are more declarators. */
24726 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24727 {
24728 cp_lexer_consume_token (parser->lexer);
24729 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24730 {
24731 cp_token *token = cp_lexer_previous_token (parser->lexer);
24732 gcc_rich_location richloc (token->location);
24733 richloc.add_fixit_remove ();
24734 error_at (&richloc, "stray %<,%> at end of "
24735 "member declaration");
24736 }
24737 }
24738 /* If the next token isn't a `;', then we have a parse error. */
24739 else if (cp_lexer_next_token_is_not (parser->lexer,
24740 CPP_SEMICOLON))
24741 {
24742 /* The next token might be a ways away from where the
24743 actual semicolon is missing. Find the previous token
24744 and use that for our error position. */
24745 cp_token *token = cp_lexer_previous_token (parser->lexer);
24746 gcc_rich_location richloc (token->location);
24747 richloc.add_fixit_insert_after (";");
24748 error_at (&richloc, "expected %<;%> at end of "
24749 "member declaration");
24750
24751 /* Assume that the user meant to provide a semicolon. If
24752 we were to cp_parser_skip_to_end_of_statement, we might
24753 skip to a semicolon inside a member function definition
24754 and issue nonsensical error messages. */
24755 assume_semicolon = true;
24756 }
24757
24758 if (decl)
24759 {
24760 /* Add DECL to the list of members. */
24761 if (!friend_p
24762 /* Explicitly include, eg, NSDMIs, for better error
24763 recovery (c++/58650). */
24764 || !DECL_DECLARES_FUNCTION_P (decl))
24765 finish_member_declaration (decl);
24766
24767 if (TREE_CODE (decl) == FUNCTION_DECL)
24768 cp_parser_save_default_args (parser, decl);
24769 else if (TREE_CODE (decl) == FIELD_DECL
24770 && DECL_INITIAL (decl))
24771 /* Add DECL to the queue of NSDMI to be parsed later. */
24772 vec_safe_push (unparsed_nsdmis, decl);
24773 }
24774
24775 if (assume_semicolon)
24776 goto out;
24777 }
24778 }
24779
24780 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24781 out:
24782 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
24783 }
24784
24785 /* Parse a pure-specifier.
24786
24787 pure-specifier:
24788 = 0
24789
24790 Returns INTEGER_ZERO_NODE if a pure specifier is found.
24791 Otherwise, ERROR_MARK_NODE is returned. */
24792
24793 static tree
24794 cp_parser_pure_specifier (cp_parser* parser)
24795 {
24796 cp_token *token;
24797
24798 /* Look for the `=' token. */
24799 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24800 return error_mark_node;
24801 /* Look for the `0' token. */
24802 token = cp_lexer_peek_token (parser->lexer);
24803
24804 if (token->type == CPP_EOF
24805 || token->type == CPP_PRAGMA_EOL)
24806 return error_mark_node;
24807
24808 cp_lexer_consume_token (parser->lexer);
24809
24810 /* Accept = default or = delete in c++0x mode. */
24811 if (token->keyword == RID_DEFAULT
24812 || token->keyword == RID_DELETE)
24813 {
24814 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
24815 return token->u.value;
24816 }
24817
24818 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
24819 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
24820 {
24821 cp_parser_error (parser,
24822 "invalid pure specifier (only %<= 0%> is allowed)");
24823 cp_parser_skip_to_end_of_statement (parser);
24824 return error_mark_node;
24825 }
24826 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24827 {
24828 error_at (token->location, "templates may not be %<virtual%>");
24829 return error_mark_node;
24830 }
24831
24832 return integer_zero_node;
24833 }
24834
24835 /* Parse a constant-initializer.
24836
24837 constant-initializer:
24838 = constant-expression
24839
24840 Returns a representation of the constant-expression. */
24841
24842 static tree
24843 cp_parser_constant_initializer (cp_parser* parser)
24844 {
24845 /* Look for the `=' token. */
24846 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24847 return error_mark_node;
24848
24849 /* It is invalid to write:
24850
24851 struct S { static const int i = { 7 }; };
24852
24853 */
24854 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24855 {
24856 cp_parser_error (parser,
24857 "a brace-enclosed initializer is not allowed here");
24858 /* Consume the opening brace. */
24859 matching_braces braces;
24860 braces.consume_open (parser);
24861 /* Skip the initializer. */
24862 cp_parser_skip_to_closing_brace (parser);
24863 /* Look for the trailing `}'. */
24864 braces.require_close (parser);
24865
24866 return error_mark_node;
24867 }
24868
24869 return cp_parser_constant_expression (parser);
24870 }
24871
24872 /* Derived classes [gram.class.derived] */
24873
24874 /* Parse a base-clause.
24875
24876 base-clause:
24877 : base-specifier-list
24878
24879 base-specifier-list:
24880 base-specifier ... [opt]
24881 base-specifier-list , base-specifier ... [opt]
24882
24883 Returns a TREE_LIST representing the base-classes, in the order in
24884 which they were declared. The representation of each node is as
24885 described by cp_parser_base_specifier.
24886
24887 In the case that no bases are specified, this function will return
24888 NULL_TREE, not ERROR_MARK_NODE. */
24889
24890 static tree
24891 cp_parser_base_clause (cp_parser* parser)
24892 {
24893 tree bases = NULL_TREE;
24894
24895 /* Look for the `:' that begins the list. */
24896 cp_parser_require (parser, CPP_COLON, RT_COLON);
24897
24898 /* Scan the base-specifier-list. */
24899 while (true)
24900 {
24901 cp_token *token;
24902 tree base;
24903 bool pack_expansion_p = false;
24904
24905 /* Look for the base-specifier. */
24906 base = cp_parser_base_specifier (parser);
24907 /* Look for the (optional) ellipsis. */
24908 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24909 {
24910 /* Consume the `...'. */
24911 cp_lexer_consume_token (parser->lexer);
24912
24913 pack_expansion_p = true;
24914 }
24915
24916 /* Add BASE to the front of the list. */
24917 if (base && base != error_mark_node)
24918 {
24919 if (pack_expansion_p)
24920 /* Make this a pack expansion type. */
24921 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24922
24923 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24924 {
24925 TREE_CHAIN (base) = bases;
24926 bases = base;
24927 }
24928 }
24929 /* Peek at the next token. */
24930 token = cp_lexer_peek_token (parser->lexer);
24931 /* If it's not a comma, then the list is complete. */
24932 if (token->type != CPP_COMMA)
24933 break;
24934 /* Consume the `,'. */
24935 cp_lexer_consume_token (parser->lexer);
24936 }
24937
24938 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24939 base class had a qualified name. However, the next name that
24940 appears is certainly not qualified. */
24941 parser->scope = NULL_TREE;
24942 parser->qualifying_scope = NULL_TREE;
24943 parser->object_scope = NULL_TREE;
24944
24945 return nreverse (bases);
24946 }
24947
24948 /* Parse a base-specifier.
24949
24950 base-specifier:
24951 :: [opt] nested-name-specifier [opt] class-name
24952 virtual access-specifier [opt] :: [opt] nested-name-specifier
24953 [opt] class-name
24954 access-specifier virtual [opt] :: [opt] nested-name-specifier
24955 [opt] class-name
24956
24957 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24958 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24959 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24960 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24961
24962 static tree
24963 cp_parser_base_specifier (cp_parser* parser)
24964 {
24965 cp_token *token;
24966 bool done = false;
24967 bool virtual_p = false;
24968 bool duplicate_virtual_error_issued_p = false;
24969 bool duplicate_access_error_issued_p = false;
24970 bool class_scope_p, template_p;
24971 tree access = access_default_node;
24972 tree type;
24973
24974 /* Process the optional `virtual' and `access-specifier'. */
24975 while (!done)
24976 {
24977 /* Peek at the next token. */
24978 token = cp_lexer_peek_token (parser->lexer);
24979 /* Process `virtual'. */
24980 switch (token->keyword)
24981 {
24982 case RID_VIRTUAL:
24983 /* If `virtual' appears more than once, issue an error. */
24984 if (virtual_p && !duplicate_virtual_error_issued_p)
24985 {
24986 cp_parser_error (parser,
24987 "%<virtual%> specified more than once in base-specifier");
24988 duplicate_virtual_error_issued_p = true;
24989 }
24990
24991 virtual_p = true;
24992
24993 /* Consume the `virtual' token. */
24994 cp_lexer_consume_token (parser->lexer);
24995
24996 break;
24997
24998 case RID_PUBLIC:
24999 case RID_PROTECTED:
25000 case RID_PRIVATE:
25001 /* If more than one access specifier appears, issue an
25002 error. */
25003 if (access != access_default_node
25004 && !duplicate_access_error_issued_p)
25005 {
25006 cp_parser_error (parser,
25007 "more than one access specifier in base-specifier");
25008 duplicate_access_error_issued_p = true;
25009 }
25010
25011 access = ridpointers[(int) token->keyword];
25012
25013 /* Consume the access-specifier. */
25014 cp_lexer_consume_token (parser->lexer);
25015
25016 break;
25017
25018 default:
25019 done = true;
25020 break;
25021 }
25022 }
25023 /* It is not uncommon to see programs mechanically, erroneously, use
25024 the 'typename' keyword to denote (dependent) qualified types
25025 as base classes. */
25026 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25027 {
25028 token = cp_lexer_peek_token (parser->lexer);
25029 if (!processing_template_decl)
25030 error_at (token->location,
25031 "keyword %<typename%> not allowed outside of templates");
25032 else
25033 error_at (token->location,
25034 "keyword %<typename%> not allowed in this context "
25035 "(the base class is implicitly a type)");
25036 cp_lexer_consume_token (parser->lexer);
25037 }
25038
25039 /* Look for the optional `::' operator. */
25040 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
25041 /* Look for the nested-name-specifier. The simplest way to
25042 implement:
25043
25044 [temp.res]
25045
25046 The keyword `typename' is not permitted in a base-specifier or
25047 mem-initializer; in these contexts a qualified name that
25048 depends on a template-parameter is implicitly assumed to be a
25049 type name.
25050
25051 is to pretend that we have seen the `typename' keyword at this
25052 point. */
25053 cp_parser_nested_name_specifier_opt (parser,
25054 /*typename_keyword_p=*/true,
25055 /*check_dependency_p=*/true,
25056 /*type_p=*/true,
25057 /*is_declaration=*/true);
25058 /* If the base class is given by a qualified name, assume that names
25059 we see are type names or templates, as appropriate. */
25060 class_scope_p = (parser->scope && TYPE_P (parser->scope));
25061 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
25062
25063 if (!parser->scope
25064 && cp_lexer_next_token_is_decltype (parser->lexer))
25065 /* DR 950 allows decltype as a base-specifier. */
25066 type = cp_parser_decltype (parser);
25067 else
25068 {
25069 /* Otherwise, look for the class-name. */
25070 type = cp_parser_class_name (parser,
25071 class_scope_p,
25072 template_p,
25073 typename_type,
25074 /*check_dependency_p=*/true,
25075 /*class_head_p=*/false,
25076 /*is_declaration=*/true);
25077 type = TREE_TYPE (type);
25078 }
25079
25080 if (type == error_mark_node)
25081 return error_mark_node;
25082
25083 return finish_base_specifier (type, access, virtual_p);
25084 }
25085
25086 /* Exception handling [gram.exception] */
25087
25088 /* Parse an (optional) noexcept-specification.
25089
25090 noexcept-specification:
25091 noexcept ( constant-expression ) [opt]
25092
25093 If no noexcept-specification is present, returns NULL_TREE.
25094 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25095 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25096 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25097 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25098 in which case a boolean condition is returned instead. */
25099
25100 static tree
25101 cp_parser_noexcept_specification_opt (cp_parser* parser,
25102 bool require_constexpr,
25103 bool* consumed_expr,
25104 bool return_cond)
25105 {
25106 cp_token *token;
25107 const char *saved_message;
25108
25109 /* Peek at the next token. */
25110 token = cp_lexer_peek_token (parser->lexer);
25111
25112 /* Is it a noexcept-specification? */
25113 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
25114 {
25115 tree expr;
25116 cp_lexer_consume_token (parser->lexer);
25117
25118 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
25119 {
25120 matching_parens parens;
25121 parens.consume_open (parser);
25122
25123 tree save_ccp = current_class_ptr;
25124 tree save_ccr = current_class_ref;
25125
25126 if (current_class_type)
25127 inject_this_parameter (current_class_type, TYPE_UNQUALIFIED);
25128
25129 if (require_constexpr)
25130 {
25131 /* Types may not be defined in an exception-specification. */
25132 saved_message = parser->type_definition_forbidden_message;
25133 parser->type_definition_forbidden_message
25134 = G_("types may not be defined in an exception-specification");
25135
25136 expr = cp_parser_constant_expression (parser);
25137
25138 /* Restore the saved message. */
25139 parser->type_definition_forbidden_message = saved_message;
25140 }
25141 else
25142 {
25143 expr = cp_parser_expression (parser);
25144 *consumed_expr = true;
25145 }
25146
25147 parens.require_close (parser);
25148
25149 current_class_ptr = save_ccp;
25150 current_class_ref = save_ccr;
25151 }
25152 else
25153 {
25154 expr = boolean_true_node;
25155 if (!require_constexpr)
25156 *consumed_expr = false;
25157 }
25158
25159 /* We cannot build a noexcept-spec right away because this will check
25160 that expr is a constexpr. */
25161 if (!return_cond)
25162 return build_noexcept_spec (expr, tf_warning_or_error);
25163 else
25164 return expr;
25165 }
25166 else
25167 return NULL_TREE;
25168 }
25169
25170 /* Parse an (optional) exception-specification.
25171
25172 exception-specification:
25173 throw ( type-id-list [opt] )
25174
25175 Returns a TREE_LIST representing the exception-specification. The
25176 TREE_VALUE of each node is a type. */
25177
25178 static tree
25179 cp_parser_exception_specification_opt (cp_parser* parser)
25180 {
25181 cp_token *token;
25182 tree type_id_list;
25183 const char *saved_message;
25184
25185 /* Peek at the next token. */
25186 token = cp_lexer_peek_token (parser->lexer);
25187
25188 /* Is it a noexcept-specification? */
25189 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
25190 false);
25191 if (type_id_list != NULL_TREE)
25192 return type_id_list;
25193
25194 /* If it's not `throw', then there's no exception-specification. */
25195 if (!cp_parser_is_keyword (token, RID_THROW))
25196 return NULL_TREE;
25197
25198 location_t loc = token->location;
25199
25200 /* Consume the `throw'. */
25201 cp_lexer_consume_token (parser->lexer);
25202
25203 /* Look for the `('. */
25204 matching_parens parens;
25205 parens.require_open (parser);
25206
25207 /* Peek at the next token. */
25208 token = cp_lexer_peek_token (parser->lexer);
25209 /* If it's not a `)', then there is a type-id-list. */
25210 if (token->type != CPP_CLOSE_PAREN)
25211 {
25212 /* Types may not be defined in an exception-specification. */
25213 saved_message = parser->type_definition_forbidden_message;
25214 parser->type_definition_forbidden_message
25215 = G_("types may not be defined in an exception-specification");
25216 /* Parse the type-id-list. */
25217 type_id_list = cp_parser_type_id_list (parser);
25218 /* Restore the saved message. */
25219 parser->type_definition_forbidden_message = saved_message;
25220
25221 if (cxx_dialect >= cxx17)
25222 {
25223 error_at (loc, "ISO C++17 does not allow dynamic exception "
25224 "specifications");
25225 type_id_list = NULL_TREE;
25226 }
25227 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
25228 warning_at (loc, OPT_Wdeprecated,
25229 "dynamic exception specifications are deprecated in "
25230 "C++11");
25231 }
25232 /* In C++17, throw() is equivalent to noexcept (true). throw()
25233 is deprecated in C++11 and above as well, but is still widely used,
25234 so don't warn about it yet. */
25235 else if (cxx_dialect >= cxx17)
25236 type_id_list = noexcept_true_spec;
25237 else
25238 type_id_list = empty_except_spec;
25239
25240 /* Look for the `)'. */
25241 parens.require_close (parser);
25242
25243 return type_id_list;
25244 }
25245
25246 /* Parse an (optional) type-id-list.
25247
25248 type-id-list:
25249 type-id ... [opt]
25250 type-id-list , type-id ... [opt]
25251
25252 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25253 in the order that the types were presented. */
25254
25255 static tree
25256 cp_parser_type_id_list (cp_parser* parser)
25257 {
25258 tree types = NULL_TREE;
25259
25260 while (true)
25261 {
25262 cp_token *token;
25263 tree type;
25264
25265 token = cp_lexer_peek_token (parser->lexer);
25266
25267 /* Get the next type-id. */
25268 type = cp_parser_type_id (parser);
25269 /* Check for invalid 'auto'. */
25270 if (flag_concepts && type_uses_auto (type))
25271 {
25272 error_at (token->location,
25273 "invalid use of %<auto%> in exception-specification");
25274 type = error_mark_node;
25275 }
25276 /* Parse the optional ellipsis. */
25277 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25278 {
25279 /* Consume the `...'. */
25280 cp_lexer_consume_token (parser->lexer);
25281
25282 /* Turn the type into a pack expansion expression. */
25283 type = make_pack_expansion (type);
25284 }
25285 /* Add it to the list. */
25286 types = add_exception_specifier (types, type, /*complain=*/1);
25287 /* Peek at the next token. */
25288 token = cp_lexer_peek_token (parser->lexer);
25289 /* If it is not a `,', we are done. */
25290 if (token->type != CPP_COMMA)
25291 break;
25292 /* Consume the `,'. */
25293 cp_lexer_consume_token (parser->lexer);
25294 }
25295
25296 return nreverse (types);
25297 }
25298
25299 /* Parse a try-block.
25300
25301 try-block:
25302 try compound-statement handler-seq */
25303
25304 static tree
25305 cp_parser_try_block (cp_parser* parser)
25306 {
25307 tree try_block;
25308
25309 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
25310 if (parser->in_function_body
25311 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
25312 error ("%<try%> in %<constexpr%> function");
25313
25314 try_block = begin_try_block ();
25315 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
25316 finish_try_block (try_block);
25317 cp_parser_handler_seq (parser);
25318 finish_handler_sequence (try_block);
25319
25320 return try_block;
25321 }
25322
25323 /* Parse a function-try-block.
25324
25325 function-try-block:
25326 try ctor-initializer [opt] function-body handler-seq */
25327
25328 static void
25329 cp_parser_function_try_block (cp_parser* parser)
25330 {
25331 tree compound_stmt;
25332 tree try_block;
25333
25334 /* Look for the `try' keyword. */
25335 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
25336 return;
25337 /* Let the rest of the front end know where we are. */
25338 try_block = begin_function_try_block (&compound_stmt);
25339 /* Parse the function-body. */
25340 cp_parser_ctor_initializer_opt_and_function_body
25341 (parser, /*in_function_try_block=*/true);
25342 /* We're done with the `try' part. */
25343 finish_function_try_block (try_block);
25344 /* Parse the handlers. */
25345 cp_parser_handler_seq (parser);
25346 /* We're done with the handlers. */
25347 finish_function_handler_sequence (try_block, compound_stmt);
25348 }
25349
25350 /* Parse a handler-seq.
25351
25352 handler-seq:
25353 handler handler-seq [opt] */
25354
25355 static void
25356 cp_parser_handler_seq (cp_parser* parser)
25357 {
25358 while (true)
25359 {
25360 cp_token *token;
25361
25362 /* Parse the handler. */
25363 cp_parser_handler (parser);
25364 /* Peek at the next token. */
25365 token = cp_lexer_peek_token (parser->lexer);
25366 /* If it's not `catch' then there are no more handlers. */
25367 if (!cp_parser_is_keyword (token, RID_CATCH))
25368 break;
25369 }
25370 }
25371
25372 /* Parse a handler.
25373
25374 handler:
25375 catch ( exception-declaration ) compound-statement */
25376
25377 static void
25378 cp_parser_handler (cp_parser* parser)
25379 {
25380 tree handler;
25381 tree declaration;
25382
25383 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
25384 handler = begin_handler ();
25385 matching_parens parens;
25386 parens.require_open (parser);
25387 declaration = cp_parser_exception_declaration (parser);
25388 finish_handler_parms (declaration, handler);
25389 parens.require_close (parser);
25390 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
25391 finish_handler (handler);
25392 }
25393
25394 /* Parse an exception-declaration.
25395
25396 exception-declaration:
25397 type-specifier-seq declarator
25398 type-specifier-seq abstract-declarator
25399 type-specifier-seq
25400 ...
25401
25402 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25403 ellipsis variant is used. */
25404
25405 static tree
25406 cp_parser_exception_declaration (cp_parser* parser)
25407 {
25408 cp_decl_specifier_seq type_specifiers;
25409 cp_declarator *declarator;
25410 const char *saved_message;
25411
25412 /* If it's an ellipsis, it's easy to handle. */
25413 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25414 {
25415 /* Consume the `...' token. */
25416 cp_lexer_consume_token (parser->lexer);
25417 return NULL_TREE;
25418 }
25419
25420 /* Types may not be defined in exception-declarations. */
25421 saved_message = parser->type_definition_forbidden_message;
25422 parser->type_definition_forbidden_message
25423 = G_("types may not be defined in exception-declarations");
25424
25425 /* Parse the type-specifier-seq. */
25426 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
25427 /*is_declaration=*/true,
25428 /*is_trailing_return=*/false,
25429 &type_specifiers);
25430 /* If it's a `)', then there is no declarator. */
25431 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
25432 declarator = NULL;
25433 else
25434 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
25435 CP_PARSER_FLAGS_NONE,
25436 /*ctor_dtor_or_conv_p=*/NULL,
25437 /*parenthesized_p=*/NULL,
25438 /*member_p=*/false,
25439 /*friend_p=*/false,
25440 /*static_p=*/false);
25441
25442 /* Restore the saved message. */
25443 parser->type_definition_forbidden_message = saved_message;
25444
25445 if (!type_specifiers.any_specifiers_p)
25446 return error_mark_node;
25447
25448 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
25449 }
25450
25451 /* Parse a throw-expression.
25452
25453 throw-expression:
25454 throw assignment-expression [opt]
25455
25456 Returns a THROW_EXPR representing the throw-expression. */
25457
25458 static tree
25459 cp_parser_throw_expression (cp_parser* parser)
25460 {
25461 tree expression;
25462 cp_token* token;
25463
25464 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
25465 token = cp_lexer_peek_token (parser->lexer);
25466 /* Figure out whether or not there is an assignment-expression
25467 following the "throw" keyword. */
25468 if (token->type == CPP_COMMA
25469 || token->type == CPP_SEMICOLON
25470 || token->type == CPP_CLOSE_PAREN
25471 || token->type == CPP_CLOSE_SQUARE
25472 || token->type == CPP_CLOSE_BRACE
25473 || token->type == CPP_COLON)
25474 expression = NULL_TREE;
25475 else
25476 expression = cp_parser_assignment_expression (parser);
25477
25478 return build_throw (expression);
25479 }
25480
25481 /* GNU Extensions */
25482
25483 /* Parse an (optional) asm-specification.
25484
25485 asm-specification:
25486 asm ( string-literal )
25487
25488 If the asm-specification is present, returns a STRING_CST
25489 corresponding to the string-literal. Otherwise, returns
25490 NULL_TREE. */
25491
25492 static tree
25493 cp_parser_asm_specification_opt (cp_parser* parser)
25494 {
25495 cp_token *token;
25496 tree asm_specification;
25497
25498 /* Peek at the next token. */
25499 token = cp_lexer_peek_token (parser->lexer);
25500 /* If the next token isn't the `asm' keyword, then there's no
25501 asm-specification. */
25502 if (!cp_parser_is_keyword (token, RID_ASM))
25503 return NULL_TREE;
25504
25505 /* Consume the `asm' token. */
25506 cp_lexer_consume_token (parser->lexer);
25507 /* Look for the `('. */
25508 matching_parens parens;
25509 parens.require_open (parser);
25510
25511 /* Look for the string-literal. */
25512 asm_specification = cp_parser_string_literal (parser, false, false);
25513
25514 /* Look for the `)'. */
25515 parens.require_close (parser);
25516
25517 return asm_specification;
25518 }
25519
25520 /* Parse an asm-operand-list.
25521
25522 asm-operand-list:
25523 asm-operand
25524 asm-operand-list , asm-operand
25525
25526 asm-operand:
25527 string-literal ( expression )
25528 [ string-literal ] string-literal ( expression )
25529
25530 Returns a TREE_LIST representing the operands. The TREE_VALUE of
25531 each node is the expression. The TREE_PURPOSE is itself a
25532 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
25533 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
25534 is a STRING_CST for the string literal before the parenthesis. Returns
25535 ERROR_MARK_NODE if any of the operands are invalid. */
25536
25537 static tree
25538 cp_parser_asm_operand_list (cp_parser* parser)
25539 {
25540 tree asm_operands = NULL_TREE;
25541 bool invalid_operands = false;
25542
25543 while (true)
25544 {
25545 tree string_literal;
25546 tree expression;
25547 tree name;
25548
25549 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25550 {
25551 /* Consume the `[' token. */
25552 cp_lexer_consume_token (parser->lexer);
25553 /* Read the operand name. */
25554 name = cp_parser_identifier (parser);
25555 if (name != error_mark_node)
25556 name = build_string (IDENTIFIER_LENGTH (name),
25557 IDENTIFIER_POINTER (name));
25558 /* Look for the closing `]'. */
25559 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25560 }
25561 else
25562 name = NULL_TREE;
25563 /* Look for the string-literal. */
25564 string_literal = cp_parser_string_literal (parser, false, false);
25565
25566 /* Look for the `('. */
25567 matching_parens parens;
25568 parens.require_open (parser);
25569 /* Parse the expression. */
25570 expression = cp_parser_expression (parser);
25571 /* Look for the `)'. */
25572 parens.require_close (parser);
25573
25574 if (name == error_mark_node
25575 || string_literal == error_mark_node
25576 || expression == error_mark_node)
25577 invalid_operands = true;
25578
25579 /* Add this operand to the list. */
25580 asm_operands = tree_cons (build_tree_list (name, string_literal),
25581 expression,
25582 asm_operands);
25583 /* If the next token is not a `,', there are no more
25584 operands. */
25585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25586 break;
25587 /* Consume the `,'. */
25588 cp_lexer_consume_token (parser->lexer);
25589 }
25590
25591 return invalid_operands ? error_mark_node : nreverse (asm_operands);
25592 }
25593
25594 /* Parse an asm-clobber-list.
25595
25596 asm-clobber-list:
25597 string-literal
25598 asm-clobber-list , string-literal
25599
25600 Returns a TREE_LIST, indicating the clobbers in the order that they
25601 appeared. The TREE_VALUE of each node is a STRING_CST. */
25602
25603 static tree
25604 cp_parser_asm_clobber_list (cp_parser* parser)
25605 {
25606 tree clobbers = NULL_TREE;
25607
25608 while (true)
25609 {
25610 tree string_literal;
25611
25612 /* Look for the string literal. */
25613 string_literal = cp_parser_string_literal (parser, false, false);
25614 /* Add it to the list. */
25615 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
25616 /* If the next token is not a `,', then the list is
25617 complete. */
25618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25619 break;
25620 /* Consume the `,' token. */
25621 cp_lexer_consume_token (parser->lexer);
25622 }
25623
25624 return clobbers;
25625 }
25626
25627 /* Parse an asm-label-list.
25628
25629 asm-label-list:
25630 identifier
25631 asm-label-list , identifier
25632
25633 Returns a TREE_LIST, indicating the labels in the order that they
25634 appeared. The TREE_VALUE of each node is a label. */
25635
25636 static tree
25637 cp_parser_asm_label_list (cp_parser* parser)
25638 {
25639 tree labels = NULL_TREE;
25640
25641 while (true)
25642 {
25643 tree identifier, label, name;
25644
25645 /* Look for the identifier. */
25646 identifier = cp_parser_identifier (parser);
25647 if (!error_operand_p (identifier))
25648 {
25649 label = lookup_label (identifier);
25650 if (TREE_CODE (label) == LABEL_DECL)
25651 {
25652 TREE_USED (label) = 1;
25653 check_goto (label);
25654 name = build_string (IDENTIFIER_LENGTH (identifier),
25655 IDENTIFIER_POINTER (identifier));
25656 labels = tree_cons (name, label, labels);
25657 }
25658 }
25659 /* If the next token is not a `,', then the list is
25660 complete. */
25661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25662 break;
25663 /* Consume the `,' token. */
25664 cp_lexer_consume_token (parser->lexer);
25665 }
25666
25667 return nreverse (labels);
25668 }
25669
25670 /* Return TRUE iff the next tokens in the stream are possibly the
25671 beginning of a GNU extension attribute. */
25672
25673 static bool
25674 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
25675 {
25676 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
25677 }
25678
25679 /* Return TRUE iff the next tokens in the stream are possibly the
25680 beginning of a standard C++-11 attribute specifier. */
25681
25682 static bool
25683 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
25684 {
25685 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
25686 }
25687
25688 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25689 beginning of a standard C++-11 attribute specifier. */
25690
25691 static bool
25692 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
25693 {
25694 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25695
25696 return (cxx_dialect >= cxx11
25697 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
25698 || (token->type == CPP_OPEN_SQUARE
25699 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
25700 && token->type == CPP_OPEN_SQUARE)));
25701 }
25702
25703 /* Return TRUE iff the next Nth tokens in the stream are possibly the
25704 beginning of a GNU extension attribute. */
25705
25706 static bool
25707 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
25708 {
25709 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
25710
25711 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
25712 }
25713
25714 /* Return true iff the next tokens can be the beginning of either a
25715 GNU attribute list, or a standard C++11 attribute sequence. */
25716
25717 static bool
25718 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
25719 {
25720 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
25721 || cp_next_tokens_can_be_std_attribute_p (parser));
25722 }
25723
25724 /* Return true iff the next Nth tokens can be the beginning of either
25725 a GNU attribute list, or a standard C++11 attribute sequence. */
25726
25727 static bool
25728 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
25729 {
25730 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
25731 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
25732 }
25733
25734 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
25735 of GNU attributes, or return NULL. */
25736
25737 static tree
25738 cp_parser_attributes_opt (cp_parser *parser)
25739 {
25740 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
25741 return cp_parser_gnu_attributes_opt (parser);
25742 return cp_parser_std_attribute_spec_seq (parser);
25743 }
25744
25745 /* Parse an (optional) series of attributes.
25746
25747 attributes:
25748 attributes attribute
25749
25750 attribute:
25751 __attribute__ (( attribute-list [opt] ))
25752
25753 The return value is as for cp_parser_gnu_attribute_list. */
25754
25755 static tree
25756 cp_parser_gnu_attributes_opt (cp_parser* parser)
25757 {
25758 tree attributes = NULL_TREE;
25759
25760 temp_override<bool> cleanup
25761 (parser->auto_is_implicit_function_template_parm_p, false);
25762
25763 while (true)
25764 {
25765 cp_token *token;
25766 tree attribute_list;
25767 bool ok = true;
25768
25769 /* Peek at the next token. */
25770 token = cp_lexer_peek_token (parser->lexer);
25771 /* If it's not `__attribute__', then we're done. */
25772 if (token->keyword != RID_ATTRIBUTE)
25773 break;
25774
25775 /* Consume the `__attribute__' keyword. */
25776 cp_lexer_consume_token (parser->lexer);
25777 /* Look for the two `(' tokens. */
25778 matching_parens outer_parens;
25779 if (!outer_parens.require_open (parser))
25780 ok = false;
25781 matching_parens inner_parens;
25782 if (!inner_parens.require_open (parser))
25783 ok = false;
25784
25785 /* Peek at the next token. */
25786 token = cp_lexer_peek_token (parser->lexer);
25787 if (token->type != CPP_CLOSE_PAREN)
25788 /* Parse the attribute-list. */
25789 attribute_list = cp_parser_gnu_attribute_list (parser);
25790 else
25791 /* If the next token is a `)', then there is no attribute
25792 list. */
25793 attribute_list = NULL;
25794
25795 /* Look for the two `)' tokens. */
25796 if (!inner_parens.require_close (parser))
25797 ok = false;
25798 if (!outer_parens.require_close (parser))
25799 ok = false;
25800 if (!ok)
25801 cp_parser_skip_to_end_of_statement (parser);
25802
25803 /* Add these new attributes to the list. */
25804 attributes = attr_chainon (attributes, attribute_list);
25805 }
25806
25807 return attributes;
25808 }
25809
25810 /* Parse a GNU attribute-list.
25811
25812 attribute-list:
25813 attribute
25814 attribute-list , attribute
25815
25816 attribute:
25817 identifier
25818 identifier ( identifier )
25819 identifier ( identifier , expression-list )
25820 identifier ( expression-list )
25821
25822 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25823 to an attribute. The TREE_PURPOSE of each node is the identifier
25824 indicating which attribute is in use. The TREE_VALUE represents
25825 the arguments, if any. */
25826
25827 static tree
25828 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
25829 {
25830 tree attribute_list = NULL_TREE;
25831 bool save_translate_strings_p = parser->translate_strings_p;
25832
25833 /* Don't create wrapper nodes within attributes: the
25834 handlers don't know how to handle them. */
25835 auto_suppress_location_wrappers sentinel;
25836
25837 parser->translate_strings_p = false;
25838 while (true)
25839 {
25840 cp_token *token;
25841 tree identifier;
25842 tree attribute;
25843
25844 /* Look for the identifier. We also allow keywords here; for
25845 example `__attribute__ ((const))' is legal. */
25846 token = cp_lexer_peek_token (parser->lexer);
25847 if (token->type == CPP_NAME
25848 || token->type == CPP_KEYWORD)
25849 {
25850 tree arguments = NULL_TREE;
25851
25852 /* Consume the token, but save it since we need it for the
25853 SIMD enabled function parsing. */
25854 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25855
25856 /* Save away the identifier that indicates which attribute
25857 this is. */
25858 identifier = (token->type == CPP_KEYWORD)
25859 /* For keywords, use the canonical spelling, not the
25860 parsed identifier. */
25861 ? ridpointers[(int) token->keyword]
25862 : id_token->u.value;
25863
25864 identifier = canonicalize_attr_name (identifier);
25865 attribute = build_tree_list (identifier, NULL_TREE);
25866
25867 /* Peek at the next token. */
25868 token = cp_lexer_peek_token (parser->lexer);
25869 /* If it's an `(', then parse the attribute arguments. */
25870 if (token->type == CPP_OPEN_PAREN)
25871 {
25872 vec<tree, va_gc> *vec;
25873 int attr_flag = (attribute_takes_identifier_p (identifier)
25874 ? id_attr : normal_attr);
25875 vec = cp_parser_parenthesized_expression_list
25876 (parser, attr_flag, /*cast_p=*/false,
25877 /*allow_expansion_p=*/false,
25878 /*non_constant_p=*/NULL);
25879 if (vec == NULL)
25880 arguments = error_mark_node;
25881 else
25882 {
25883 arguments = build_tree_list_vec (vec);
25884 release_tree_vector (vec);
25885 }
25886 /* Save the arguments away. */
25887 TREE_VALUE (attribute) = arguments;
25888 }
25889
25890 if (arguments != error_mark_node)
25891 {
25892 /* Add this attribute to the list. */
25893 TREE_CHAIN (attribute) = attribute_list;
25894 attribute_list = attribute;
25895 }
25896
25897 token = cp_lexer_peek_token (parser->lexer);
25898 }
25899 /* Unless EXACTLY_ONE is set look for more attributes.
25900 If the next token isn't a `,', we're done. */
25901 if (exactly_one || token->type != CPP_COMMA)
25902 break;
25903
25904 /* Consume the comma and keep going. */
25905 cp_lexer_consume_token (parser->lexer);
25906 }
25907 parser->translate_strings_p = save_translate_strings_p;
25908
25909 /* We built up the list in reverse order. */
25910 return nreverse (attribute_list);
25911 }
25912
25913 /* Parse a standard C++11 attribute.
25914
25915 The returned representation is a TREE_LIST which TREE_PURPOSE is
25916 the scoped name of the attribute, and the TREE_VALUE is its
25917 arguments list.
25918
25919 Note that the scoped name of the attribute is itself a TREE_LIST
25920 which TREE_PURPOSE is the namespace of the attribute, and
25921 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25922 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25923 and which TREE_PURPOSE is directly the attribute name.
25924
25925 Clients of the attribute code should use get_attribute_namespace
25926 and get_attribute_name to get the actual namespace and name of
25927 attributes, regardless of their being GNU or C++11 attributes.
25928
25929 attribute:
25930 attribute-token attribute-argument-clause [opt]
25931
25932 attribute-token:
25933 identifier
25934 attribute-scoped-token
25935
25936 attribute-scoped-token:
25937 attribute-namespace :: identifier
25938
25939 attribute-namespace:
25940 identifier
25941
25942 attribute-argument-clause:
25943 ( balanced-token-seq )
25944
25945 balanced-token-seq:
25946 balanced-token [opt]
25947 balanced-token-seq balanced-token
25948
25949 balanced-token:
25950 ( balanced-token-seq )
25951 [ balanced-token-seq ]
25952 { balanced-token-seq }. */
25953
25954 static tree
25955 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25956 {
25957 tree attribute, attr_id = NULL_TREE, arguments;
25958 cp_token *token;
25959
25960 temp_override<bool> cleanup
25961 (parser->auto_is_implicit_function_template_parm_p, false);
25962
25963 /* First, parse name of the attribute, a.k.a attribute-token. */
25964
25965 token = cp_lexer_peek_token (parser->lexer);
25966 if (token->type == CPP_NAME)
25967 attr_id = token->u.value;
25968 else if (token->type == CPP_KEYWORD)
25969 attr_id = ridpointers[(int) token->keyword];
25970 else if (token->flags & NAMED_OP)
25971 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25972
25973 if (attr_id == NULL_TREE)
25974 return NULL_TREE;
25975
25976 cp_lexer_consume_token (parser->lexer);
25977
25978 token = cp_lexer_peek_token (parser->lexer);
25979 if (token->type == CPP_SCOPE)
25980 {
25981 /* We are seeing a scoped attribute token. */
25982
25983 cp_lexer_consume_token (parser->lexer);
25984 if (attr_ns)
25985 error_at (token->location, "attribute using prefix used together "
25986 "with scoped attribute token");
25987 attr_ns = attr_id;
25988
25989 token = cp_lexer_consume_token (parser->lexer);
25990 if (token->type == CPP_NAME)
25991 attr_id = token->u.value;
25992 else if (token->type == CPP_KEYWORD)
25993 attr_id = ridpointers[(int) token->keyword];
25994 else if (token->flags & NAMED_OP)
25995 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25996 else
25997 {
25998 error_at (token->location,
25999 "expected an identifier for the attribute name");
26000 return error_mark_node;
26001 }
26002
26003 attr_ns = canonicalize_attr_name (attr_ns);
26004 attr_id = canonicalize_attr_name (attr_id);
26005 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26006 NULL_TREE);
26007 token = cp_lexer_peek_token (parser->lexer);
26008 }
26009 else if (attr_ns)
26010 {
26011 attr_ns = canonicalize_attr_name (attr_ns);
26012 attr_id = canonicalize_attr_name (attr_id);
26013 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
26014 NULL_TREE);
26015 }
26016 else
26017 {
26018 attr_id = canonicalize_attr_name (attr_id);
26019 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
26020 NULL_TREE);
26021 /* C++11 noreturn attribute is equivalent to GNU's. */
26022 if (is_attribute_p ("noreturn", attr_id))
26023 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26024 /* C++14 deprecated attribute is equivalent to GNU's. */
26025 else if (is_attribute_p ("deprecated", attr_id))
26026 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26027 /* C++17 fallthrough attribute is equivalent to GNU's. */
26028 else if (is_attribute_p ("fallthrough", attr_id))
26029 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
26030 /* Transactional Memory TS optimize_for_synchronized attribute is
26031 equivalent to GNU transaction_callable. */
26032 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
26033 TREE_PURPOSE (attribute)
26034 = get_identifier ("transaction_callable");
26035 /* Transactional Memory attributes are GNU attributes. */
26036 else if (tm_attr_to_mask (attr_id))
26037 TREE_PURPOSE (attribute) = attr_id;
26038 }
26039
26040 /* Now parse the optional argument clause of the attribute. */
26041
26042 if (token->type != CPP_OPEN_PAREN)
26043 return attribute;
26044
26045 {
26046 vec<tree, va_gc> *vec;
26047 int attr_flag = normal_attr;
26048
26049 if (attr_ns == gnu_identifier
26050 && attribute_takes_identifier_p (attr_id))
26051 /* A GNU attribute that takes an identifier in parameter. */
26052 attr_flag = id_attr;
26053
26054 vec = cp_parser_parenthesized_expression_list
26055 (parser, attr_flag, /*cast_p=*/false,
26056 /*allow_expansion_p=*/true,
26057 /*non_constant_p=*/NULL);
26058 if (vec == NULL)
26059 arguments = error_mark_node;
26060 else
26061 {
26062 arguments = build_tree_list_vec (vec);
26063 release_tree_vector (vec);
26064 }
26065
26066 if (arguments == error_mark_node)
26067 attribute = error_mark_node;
26068 else
26069 TREE_VALUE (attribute) = arguments;
26070 }
26071
26072 return attribute;
26073 }
26074
26075 /* Check that the attribute ATTRIBUTE appears at most once in the
26076 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
26077 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
26078 isn't implemented yet in GCC. */
26079
26080 static void
26081 cp_parser_check_std_attribute (tree attributes, tree attribute)
26082 {
26083 if (attributes)
26084 {
26085 tree name = get_attribute_name (attribute);
26086 if (is_attribute_p ("noreturn", name)
26087 && lookup_attribute ("noreturn", attributes))
26088 error ("attribute %<noreturn%> can appear at most once "
26089 "in an attribute-list");
26090 else if (is_attribute_p ("deprecated", name)
26091 && lookup_attribute ("deprecated", attributes))
26092 error ("attribute %<deprecated%> can appear at most once "
26093 "in an attribute-list");
26094 }
26095 }
26096
26097 /* Parse a list of standard C++-11 attributes.
26098
26099 attribute-list:
26100 attribute [opt]
26101 attribute-list , attribute[opt]
26102 attribute ...
26103 attribute-list , attribute ...
26104 */
26105
26106 static tree
26107 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
26108 {
26109 tree attributes = NULL_TREE, attribute = NULL_TREE;
26110 cp_token *token = NULL;
26111
26112 while (true)
26113 {
26114 attribute = cp_parser_std_attribute (parser, attr_ns);
26115 if (attribute == error_mark_node)
26116 break;
26117 if (attribute != NULL_TREE)
26118 {
26119 cp_parser_check_std_attribute (attributes, attribute);
26120 TREE_CHAIN (attribute) = attributes;
26121 attributes = attribute;
26122 }
26123 token = cp_lexer_peek_token (parser->lexer);
26124 if (token->type == CPP_ELLIPSIS)
26125 {
26126 cp_lexer_consume_token (parser->lexer);
26127 if (attribute == NULL_TREE)
26128 error_at (token->location,
26129 "expected attribute before %<...%>");
26130 else
26131 {
26132 tree pack = make_pack_expansion (TREE_VALUE (attribute));
26133 if (pack == error_mark_node)
26134 return error_mark_node;
26135 TREE_VALUE (attribute) = pack;
26136 }
26137 token = cp_lexer_peek_token (parser->lexer);
26138 }
26139 if (token->type != CPP_COMMA)
26140 break;
26141 cp_lexer_consume_token (parser->lexer);
26142 }
26143 attributes = nreverse (attributes);
26144 return attributes;
26145 }
26146
26147 /* Parse a standard C++-11 attribute specifier.
26148
26149 attribute-specifier:
26150 [ [ attribute-using-prefix [opt] attribute-list ] ]
26151 alignment-specifier
26152
26153 attribute-using-prefix:
26154 using attribute-namespace :
26155
26156 alignment-specifier:
26157 alignas ( type-id ... [opt] )
26158 alignas ( alignment-expression ... [opt] ). */
26159
26160 static tree
26161 cp_parser_std_attribute_spec (cp_parser *parser)
26162 {
26163 tree attributes = NULL_TREE;
26164 cp_token *token = cp_lexer_peek_token (parser->lexer);
26165
26166 if (token->type == CPP_OPEN_SQUARE
26167 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
26168 {
26169 tree attr_ns = NULL_TREE;
26170
26171 cp_lexer_consume_token (parser->lexer);
26172 cp_lexer_consume_token (parser->lexer);
26173
26174 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26175 {
26176 token = cp_lexer_peek_nth_token (parser->lexer, 2);
26177 if (token->type == CPP_NAME)
26178 attr_ns = token->u.value;
26179 else if (token->type == CPP_KEYWORD)
26180 attr_ns = ridpointers[(int) token->keyword];
26181 else if (token->flags & NAMED_OP)
26182 attr_ns = get_identifier (cpp_type2name (token->type,
26183 token->flags));
26184 if (attr_ns
26185 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
26186 {
26187 if (cxx_dialect < cxx17
26188 && !in_system_header_at (input_location))
26189 pedwarn (input_location, 0,
26190 "attribute using prefix only available "
26191 "with -std=c++17 or -std=gnu++17");
26192
26193 cp_lexer_consume_token (parser->lexer);
26194 cp_lexer_consume_token (parser->lexer);
26195 cp_lexer_consume_token (parser->lexer);
26196 }
26197 else
26198 attr_ns = NULL_TREE;
26199 }
26200
26201 attributes = cp_parser_std_attribute_list (parser, attr_ns);
26202
26203 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
26204 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
26205 cp_parser_skip_to_end_of_statement (parser);
26206 else
26207 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26208 when we are sure that we have actually parsed them. */
26209 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26210 }
26211 else
26212 {
26213 tree alignas_expr;
26214
26215 /* Look for an alignment-specifier. */
26216
26217 token = cp_lexer_peek_token (parser->lexer);
26218
26219 if (token->type != CPP_KEYWORD
26220 || token->keyword != RID_ALIGNAS)
26221 return NULL_TREE;
26222
26223 cp_lexer_consume_token (parser->lexer);
26224 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
26225
26226 matching_parens parens;
26227 if (!parens.require_open (parser))
26228 return error_mark_node;
26229
26230 cp_parser_parse_tentatively (parser);
26231 alignas_expr = cp_parser_type_id (parser);
26232
26233 if (!cp_parser_parse_definitely (parser))
26234 {
26235 alignas_expr = cp_parser_assignment_expression (parser);
26236 if (alignas_expr == error_mark_node)
26237 cp_parser_skip_to_end_of_statement (parser);
26238 if (alignas_expr == NULL_TREE
26239 || alignas_expr == error_mark_node)
26240 return alignas_expr;
26241 }
26242
26243 alignas_expr = cxx_alignas_expr (alignas_expr);
26244 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
26245
26246 /* Handle alignas (pack...). */
26247 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26248 {
26249 cp_lexer_consume_token (parser->lexer);
26250 alignas_expr = make_pack_expansion (alignas_expr);
26251 }
26252
26253 /* Something went wrong, so don't build the attribute. */
26254 if (alignas_expr == error_mark_node)
26255 return error_mark_node;
26256
26257 if (!parens.require_close (parser))
26258 return error_mark_node;
26259
26260 /* Build the C++-11 representation of an 'aligned'
26261 attribute. */
26262 attributes
26263 = build_tree_list (build_tree_list (gnu_identifier,
26264 aligned_identifier), alignas_expr);
26265 }
26266
26267 return attributes;
26268 }
26269
26270 /* Parse a standard C++-11 attribute-specifier-seq.
26271
26272 attribute-specifier-seq:
26273 attribute-specifier-seq [opt] attribute-specifier
26274 */
26275
26276 static tree
26277 cp_parser_std_attribute_spec_seq (cp_parser *parser)
26278 {
26279 tree attr_specs = NULL_TREE;
26280 tree attr_last = NULL_TREE;
26281
26282 /* Don't create wrapper nodes within attributes: the
26283 handlers don't know how to handle them. */
26284 auto_suppress_location_wrappers sentinel;
26285
26286 while (true)
26287 {
26288 tree attr_spec = cp_parser_std_attribute_spec (parser);
26289 if (attr_spec == NULL_TREE)
26290 break;
26291 if (attr_spec == error_mark_node)
26292 return error_mark_node;
26293
26294 if (attr_last)
26295 TREE_CHAIN (attr_last) = attr_spec;
26296 else
26297 attr_specs = attr_last = attr_spec;
26298 attr_last = tree_last (attr_last);
26299 }
26300
26301 return attr_specs;
26302 }
26303
26304 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26305 return index of the first token after balanced-token, or N on failure. */
26306
26307 static size_t
26308 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
26309 {
26310 size_t orig_n = n;
26311 int nparens = 0, nbraces = 0, nsquares = 0;
26312 do
26313 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
26314 {
26315 case CPP_PRAGMA_EOL:
26316 if (!parser->lexer->in_pragma)
26317 break;
26318 /* FALLTHRU */
26319 case CPP_EOF:
26320 /* Ran out of tokens. */
26321 return orig_n;
26322 case CPP_OPEN_PAREN:
26323 ++nparens;
26324 break;
26325 case CPP_OPEN_BRACE:
26326 ++nbraces;
26327 break;
26328 case CPP_OPEN_SQUARE:
26329 ++nsquares;
26330 break;
26331 case CPP_CLOSE_PAREN:
26332 --nparens;
26333 break;
26334 case CPP_CLOSE_BRACE:
26335 --nbraces;
26336 break;
26337 case CPP_CLOSE_SQUARE:
26338 --nsquares;
26339 break;
26340 default:
26341 break;
26342 }
26343 while (nparens || nbraces || nsquares);
26344 return n;
26345 }
26346
26347 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26348 return index of the first token after the GNU attribute tokens, or N on
26349 failure. */
26350
26351 static size_t
26352 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
26353 {
26354 while (true)
26355 {
26356 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
26357 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
26358 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
26359 break;
26360
26361 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
26362 if (n2 == n + 2)
26363 break;
26364 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
26365 break;
26366 n = n2 + 1;
26367 }
26368 return n;
26369 }
26370
26371 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26372 next token), return index of the first token after the standard C++11
26373 attribute tokens, or N on failure. */
26374
26375 static size_t
26376 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
26377 {
26378 while (true)
26379 {
26380 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
26381 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
26382 {
26383 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26384 if (n2 == n + 1)
26385 break;
26386 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
26387 break;
26388 n = n2 + 1;
26389 }
26390 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
26391 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
26392 {
26393 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
26394 if (n2 == n + 1)
26395 break;
26396 n = n2;
26397 }
26398 else
26399 break;
26400 }
26401 return n;
26402 }
26403
26404 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26405 as the next token), return index of the first token after the attribute
26406 tokens, or N on failure. */
26407
26408 static size_t
26409 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
26410 {
26411 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
26412 return cp_parser_skip_gnu_attributes_opt (parser, n);
26413 return cp_parser_skip_std_attribute_spec_seq (parser, n);
26414 }
26415
26416 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
26417 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
26418 current value of the PEDANTIC flag, regardless of whether or not
26419 the `__extension__' keyword is present. The caller is responsible
26420 for restoring the value of the PEDANTIC flag. */
26421
26422 static bool
26423 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
26424 {
26425 /* Save the old value of the PEDANTIC flag. */
26426 *saved_pedantic = pedantic;
26427
26428 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
26429 {
26430 /* Consume the `__extension__' token. */
26431 cp_lexer_consume_token (parser->lexer);
26432 /* We're not being pedantic while the `__extension__' keyword is
26433 in effect. */
26434 pedantic = 0;
26435
26436 return true;
26437 }
26438
26439 return false;
26440 }
26441
26442 /* Parse a label declaration.
26443
26444 label-declaration:
26445 __label__ label-declarator-seq ;
26446
26447 label-declarator-seq:
26448 identifier , label-declarator-seq
26449 identifier */
26450
26451 static void
26452 cp_parser_label_declaration (cp_parser* parser)
26453 {
26454 /* Look for the `__label__' keyword. */
26455 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
26456
26457 while (true)
26458 {
26459 tree identifier;
26460
26461 /* Look for an identifier. */
26462 identifier = cp_parser_identifier (parser);
26463 /* If we failed, stop. */
26464 if (identifier == error_mark_node)
26465 break;
26466 /* Declare it as a label. */
26467 finish_label_decl (identifier);
26468 /* If the next token is a `;', stop. */
26469 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26470 break;
26471 /* Look for the `,' separating the label declarations. */
26472 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
26473 }
26474
26475 /* Look for the final `;'. */
26476 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26477 }
26478
26479 // -------------------------------------------------------------------------- //
26480 // Requires Clause
26481
26482 // Parse a requires clause.
26483 //
26484 // requires-clause:
26485 // 'requires' logical-or-expression
26486 //
26487 // The required logical-or-expression must be a constant expression. Note
26488 // that we don't check that the expression is constepxr here. We defer until
26489 // we analyze constraints and then, we only check atomic constraints.
26490 static tree
26491 cp_parser_requires_clause (cp_parser *parser)
26492 {
26493 // Parse the requires clause so that it is not automatically folded.
26494 ++processing_template_decl;
26495 tree expr = cp_parser_binary_expression (parser, false, false,
26496 PREC_NOT_OPERATOR, NULL);
26497 if (check_for_bare_parameter_packs (expr))
26498 expr = error_mark_node;
26499 --processing_template_decl;
26500 return expr;
26501 }
26502
26503 // Optionally parse a requires clause:
26504 static tree
26505 cp_parser_requires_clause_opt (cp_parser *parser)
26506 {
26507 cp_token *tok = cp_lexer_peek_token (parser->lexer);
26508 if (tok->keyword != RID_REQUIRES)
26509 {
26510 if (!flag_concepts && tok->type == CPP_NAME
26511 && tok->u.value == ridpointers[RID_REQUIRES])
26512 {
26513 error_at (cp_lexer_peek_token (parser->lexer)->location,
26514 "%<requires%> only available with -fconcepts");
26515 /* Parse and discard the requires-clause. */
26516 cp_lexer_consume_token (parser->lexer);
26517 cp_parser_requires_clause (parser);
26518 }
26519 return NULL_TREE;
26520 }
26521 cp_lexer_consume_token (parser->lexer);
26522 return cp_parser_requires_clause (parser);
26523 }
26524
26525
26526 /*---------------------------------------------------------------------------
26527 Requires expressions
26528 ---------------------------------------------------------------------------*/
26529
26530 /* Parse a requires expression
26531
26532 requirement-expression:
26533 'requires' requirement-parameter-list [opt] requirement-body */
26534 static tree
26535 cp_parser_requires_expression (cp_parser *parser)
26536 {
26537 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
26538 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
26539
26540 /* A requires-expression shall appear only within a concept
26541 definition or a requires-clause.
26542
26543 TODO: Implement this diagnostic correctly. */
26544 if (!processing_template_decl)
26545 {
26546 error_at (loc, "a requires expression cannot appear outside a template");
26547 cp_parser_skip_to_end_of_statement (parser);
26548 return error_mark_node;
26549 }
26550
26551 tree parms, reqs;
26552 {
26553 /* Local parameters are delared as variables within the scope
26554 of the expression. They are not visible past the end of
26555 the expression. Expressions within the requires-expression
26556 are unevaluated. */
26557 struct scope_sentinel
26558 {
26559 scope_sentinel ()
26560 {
26561 ++cp_unevaluated_operand;
26562 begin_scope (sk_block, NULL_TREE);
26563 }
26564
26565 ~scope_sentinel ()
26566 {
26567 pop_bindings_and_leave_scope ();
26568 --cp_unevaluated_operand;
26569 }
26570 } s;
26571
26572 /* Parse the optional parameter list. */
26573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26574 {
26575 parms = cp_parser_requirement_parameter_list (parser);
26576 if (parms == error_mark_node)
26577 return error_mark_node;
26578 }
26579 else
26580 parms = NULL_TREE;
26581
26582 /* Parse the requirement body. */
26583 reqs = cp_parser_requirement_body (parser);
26584 if (reqs == error_mark_node)
26585 return error_mark_node;
26586 }
26587
26588 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
26589 the parm chain. */
26590 grokparms (parms, &parms);
26591 return finish_requires_expr (parms, reqs);
26592 }
26593
26594 /* Parse a parameterized requirement.
26595
26596 requirement-parameter-list:
26597 '(' parameter-declaration-clause ')' */
26598 static tree
26599 cp_parser_requirement_parameter_list (cp_parser *parser)
26600 {
26601 matching_parens parens;
26602 if (!parens.require_open (parser))
26603 return error_mark_node;
26604
26605 tree parms
26606 = cp_parser_parameter_declaration_clause (parser, CP_PARSER_FLAGS_NONE);
26607
26608 if (!parens.require_close (parser))
26609 return error_mark_node;
26610
26611 return parms;
26612 }
26613
26614 /* Parse the body of a requirement.
26615
26616 requirement-body:
26617 '{' requirement-list '}' */
26618 static tree
26619 cp_parser_requirement_body (cp_parser *parser)
26620 {
26621 matching_braces braces;
26622 if (!braces.require_open (parser))
26623 return error_mark_node;
26624
26625 tree reqs = cp_parser_requirement_list (parser);
26626
26627 if (!braces.require_close (parser))
26628 return error_mark_node;
26629
26630 return reqs;
26631 }
26632
26633 /* Parse a list of requirements.
26634
26635 requirement-list:
26636 requirement
26637 requirement-list ';' requirement[opt] */
26638 static tree
26639 cp_parser_requirement_list (cp_parser *parser)
26640 {
26641 tree result = NULL_TREE;
26642 while (true)
26643 {
26644 tree req = cp_parser_requirement (parser);
26645 if (req == error_mark_node)
26646 return error_mark_node;
26647
26648 result = tree_cons (NULL_TREE, req, result);
26649
26650 /* If we see a semi-colon, consume it. */
26651 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26652 cp_lexer_consume_token (parser->lexer);
26653
26654 /* Stop processing at the end of the list. */
26655 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26656 break;
26657 }
26658
26659 /* Reverse the order of requirements so they are analyzed in
26660 declaration order. */
26661 return nreverse (result);
26662 }
26663
26664 /* Parse a syntactic requirement or type requirement.
26665
26666 requirement:
26667 simple-requirement
26668 compound-requirement
26669 type-requirement
26670 nested-requirement */
26671 static tree
26672 cp_parser_requirement (cp_parser *parser)
26673 {
26674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26675 return cp_parser_compound_requirement (parser);
26676 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
26677 return cp_parser_type_requirement (parser);
26678 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
26679 return cp_parser_nested_requirement (parser);
26680 else
26681 return cp_parser_simple_requirement (parser);
26682 }
26683
26684 /* Parse a simple requirement.
26685
26686 simple-requirement:
26687 expression ';' */
26688 static tree
26689 cp_parser_simple_requirement (cp_parser *parser)
26690 {
26691 tree expr = cp_parser_expression (parser, NULL, false, false);
26692 if (!expr || expr == error_mark_node)
26693 return error_mark_node;
26694
26695 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26696 return error_mark_node;
26697
26698 return finish_simple_requirement (expr);
26699 }
26700
26701 /* Parse a type requirement
26702
26703 type-requirement
26704 nested-name-specifier [opt] required-type-name ';'
26705
26706 required-type-name:
26707 type-name
26708 'template' [opt] simple-template-id */
26709 static tree
26710 cp_parser_type_requirement (cp_parser *parser)
26711 {
26712 cp_lexer_consume_token (parser->lexer);
26713
26714 // Save the scope before parsing name specifiers.
26715 tree saved_scope = parser->scope;
26716 tree saved_object_scope = parser->object_scope;
26717 tree saved_qualifying_scope = parser->qualifying_scope;
26718 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
26719 cp_parser_nested_name_specifier_opt (parser,
26720 /*typename_keyword_p=*/true,
26721 /*check_dependency_p=*/false,
26722 /*type_p=*/true,
26723 /*is_declaration=*/false);
26724
26725 tree type;
26726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26727 {
26728 cp_lexer_consume_token (parser->lexer);
26729 type = cp_parser_template_id (parser,
26730 /*template_keyword_p=*/true,
26731 /*check_dependency=*/false,
26732 /*tag_type=*/none_type,
26733 /*is_declaration=*/false);
26734 type = make_typename_type (parser->scope, type, typename_type,
26735 /*complain=*/tf_error);
26736 }
26737 else
26738 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
26739
26740 if (TREE_CODE (type) == TYPE_DECL)
26741 type = TREE_TYPE (type);
26742
26743 parser->scope = saved_scope;
26744 parser->object_scope = saved_object_scope;
26745 parser->qualifying_scope = saved_qualifying_scope;
26746
26747 if (type == error_mark_node)
26748 cp_parser_skip_to_end_of_statement (parser);
26749
26750 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26751 return error_mark_node;
26752 if (type == error_mark_node)
26753 return error_mark_node;
26754
26755 return finish_type_requirement (type);
26756 }
26757
26758 /* Parse a compound requirement
26759
26760 compound-requirement:
26761 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
26762 static tree
26763 cp_parser_compound_requirement (cp_parser *parser)
26764 {
26765 /* Parse an expression enclosed in '{ }'s. */
26766 matching_braces braces;
26767 if (!braces.require_open (parser))
26768 return error_mark_node;
26769
26770 tree expr = cp_parser_expression (parser, NULL, false, false);
26771 if (!expr || expr == error_mark_node)
26772 return error_mark_node;
26773
26774 if (!braces.require_close (parser))
26775 return error_mark_node;
26776
26777 /* Parse the optional noexcept. */
26778 bool noexcept_p = false;
26779 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
26780 {
26781 cp_lexer_consume_token (parser->lexer);
26782 noexcept_p = true;
26783 }
26784
26785 /* Parse the optional trailing return type. */
26786 tree type = NULL_TREE;
26787 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
26788 {
26789 cp_lexer_consume_token (parser->lexer);
26790 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
26791 parser->in_result_type_constraint_p = true;
26792 type = cp_parser_trailing_type_id (parser);
26793 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
26794 if (type == error_mark_node)
26795 return error_mark_node;
26796 }
26797
26798 return finish_compound_requirement (expr, type, noexcept_p);
26799 }
26800
26801 /* Parse a nested requirement. This is the same as a requires clause.
26802
26803 nested-requirement:
26804 requires-clause */
26805 static tree
26806 cp_parser_nested_requirement (cp_parser *parser)
26807 {
26808 cp_lexer_consume_token (parser->lexer);
26809 tree req = cp_parser_requires_clause (parser);
26810 if (req == error_mark_node)
26811 return error_mark_node;
26812 return finish_nested_requirement (req);
26813 }
26814
26815 /* Support Functions */
26816
26817 /* Return the appropriate prefer_type argument for lookup_name_real based on
26818 tag_type and template_mem_access. */
26819
26820 static inline int
26821 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
26822 {
26823 /* DR 141: When looking in the current enclosing context for a template-name
26824 after -> or ., only consider class templates. */
26825 if (template_mem_access)
26826 return 2;
26827 switch (tag_type)
26828 {
26829 case none_type: return 0; // No preference.
26830 case scope_type: return 1; // Type or namespace.
26831 default: return 2; // Type only.
26832 }
26833 }
26834
26835 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
26836 NAME should have one of the representations used for an
26837 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
26838 is returned. If PARSER->SCOPE is a dependent type, then a
26839 SCOPE_REF is returned.
26840
26841 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
26842 returned; the name was already resolved when the TEMPLATE_ID_EXPR
26843 was formed. Abstractly, such entities should not be passed to this
26844 function, because they do not need to be looked up, but it is
26845 simpler to check for this special case here, rather than at the
26846 call-sites.
26847
26848 In cases not explicitly covered above, this function returns a
26849 DECL, OVERLOAD, or baselink representing the result of the lookup.
26850 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
26851 is returned.
26852
26853 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
26854 (e.g., "struct") that was used. In that case bindings that do not
26855 refer to types are ignored.
26856
26857 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
26858 ignored.
26859
26860 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
26861 are ignored.
26862
26863 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
26864 types.
26865
26866 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
26867 TREE_LIST of candidates if name-lookup results in an ambiguity, and
26868 NULL_TREE otherwise. */
26869
26870 static cp_expr
26871 cp_parser_lookup_name (cp_parser *parser, tree name,
26872 enum tag_types tag_type,
26873 bool is_template,
26874 bool is_namespace,
26875 bool check_dependency,
26876 tree *ambiguous_decls,
26877 location_t name_location)
26878 {
26879 tree decl;
26880 tree object_type = parser->context->object_type;
26881
26882 /* Assume that the lookup will be unambiguous. */
26883 if (ambiguous_decls)
26884 *ambiguous_decls = NULL_TREE;
26885
26886 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
26887 no longer valid. Note that if we are parsing tentatively, and
26888 the parse fails, OBJECT_TYPE will be automatically restored. */
26889 parser->context->object_type = NULL_TREE;
26890
26891 if (name == error_mark_node)
26892 return error_mark_node;
26893
26894 /* A template-id has already been resolved; there is no lookup to
26895 do. */
26896 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
26897 return name;
26898 if (BASELINK_P (name))
26899 {
26900 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
26901 == TEMPLATE_ID_EXPR);
26902 return name;
26903 }
26904
26905 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26906 it should already have been checked to make sure that the name
26907 used matches the type being destroyed. */
26908 if (TREE_CODE (name) == BIT_NOT_EXPR)
26909 {
26910 tree type;
26911
26912 /* Figure out to which type this destructor applies. */
26913 if (parser->scope)
26914 type = parser->scope;
26915 else if (object_type)
26916 type = object_type;
26917 else
26918 type = current_class_type;
26919 /* If that's not a class type, there is no destructor. */
26920 if (!type || !CLASS_TYPE_P (type))
26921 return error_mark_node;
26922
26923 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26924 lazily_declare_fn (sfk_destructor, type);
26925
26926 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26927 return dtor;
26928
26929 return error_mark_node;
26930 }
26931
26932 /* By this point, the NAME should be an ordinary identifier. If
26933 the id-expression was a qualified name, the qualifying scope is
26934 stored in PARSER->SCOPE at this point. */
26935 gcc_assert (identifier_p (name));
26936
26937 /* Perform the lookup. */
26938 if (parser->scope)
26939 {
26940 bool dependent_p;
26941
26942 if (parser->scope == error_mark_node)
26943 return error_mark_node;
26944
26945 /* If the SCOPE is dependent, the lookup must be deferred until
26946 the template is instantiated -- unless we are explicitly
26947 looking up names in uninstantiated templates. Even then, we
26948 cannot look up the name if the scope is not a class type; it
26949 might, for example, be a template type parameter. */
26950 dependent_p = (TYPE_P (parser->scope)
26951 && dependent_scope_p (parser->scope));
26952 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26953 && dependent_p)
26954 /* Defer lookup. */
26955 decl = error_mark_node;
26956 else
26957 {
26958 tree pushed_scope = NULL_TREE;
26959
26960 /* If PARSER->SCOPE is a dependent type, then it must be a
26961 class type, and we must not be checking dependencies;
26962 otherwise, we would have processed this lookup above. So
26963 that PARSER->SCOPE is not considered a dependent base by
26964 lookup_member, we must enter the scope here. */
26965 if (dependent_p)
26966 pushed_scope = push_scope (parser->scope);
26967
26968 /* If the PARSER->SCOPE is a template specialization, it
26969 may be instantiated during name lookup. In that case,
26970 errors may be issued. Even if we rollback the current
26971 tentative parse, those errors are valid. */
26972 decl = lookup_qualified_name (parser->scope, name,
26973 prefer_type_arg (tag_type),
26974 /*complain=*/true);
26975
26976 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26977 lookup result and the nested-name-specifier nominates a class C:
26978 * if the name specified after the nested-name-specifier, when
26979 looked up in C, is the injected-class-name of C (Clause 9), or
26980 * if the name specified after the nested-name-specifier is the
26981 same as the identifier or the simple-template-id's template-
26982 name in the last component of the nested-name-specifier,
26983 the name is instead considered to name the constructor of
26984 class C. [ Note: for example, the constructor is not an
26985 acceptable lookup result in an elaborated-type-specifier so
26986 the constructor would not be used in place of the
26987 injected-class-name. --end note ] Such a constructor name
26988 shall be used only in the declarator-id of a declaration that
26989 names a constructor or in a using-declaration. */
26990 if (tag_type == none_type
26991 && DECL_SELF_REFERENCE_P (decl)
26992 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26993 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26994 prefer_type_arg (tag_type),
26995 /*complain=*/true);
26996
26997 /* If we have a single function from a using decl, pull it out. */
26998 if (TREE_CODE (decl) == OVERLOAD
26999 && !really_overloaded_fn (decl))
27000 decl = OVL_FUNCTION (decl);
27001
27002 if (pushed_scope)
27003 pop_scope (pushed_scope);
27004 }
27005
27006 /* If the scope is a dependent type and either we deferred lookup or
27007 we did lookup but didn't find the name, rememeber the name. */
27008 if (decl == error_mark_node && TYPE_P (parser->scope)
27009 && dependent_type_p (parser->scope))
27010 {
27011 if (tag_type)
27012 {
27013 tree type;
27014
27015 /* The resolution to Core Issue 180 says that `struct
27016 A::B' should be considered a type-name, even if `A'
27017 is dependent. */
27018 type = make_typename_type (parser->scope, name, tag_type,
27019 /*complain=*/tf_error);
27020 if (type != error_mark_node)
27021 decl = TYPE_NAME (type);
27022 }
27023 else if (is_template
27024 && (cp_parser_next_token_ends_template_argument_p (parser)
27025 || cp_lexer_next_token_is (parser->lexer,
27026 CPP_CLOSE_PAREN)))
27027 decl = make_unbound_class_template (parser->scope,
27028 name, NULL_TREE,
27029 /*complain=*/tf_error);
27030 else
27031 decl = build_qualified_name (/*type=*/NULL_TREE,
27032 parser->scope, name,
27033 is_template);
27034 }
27035 parser->qualifying_scope = parser->scope;
27036 parser->object_scope = NULL_TREE;
27037 }
27038 else if (object_type)
27039 {
27040 /* Look up the name in the scope of the OBJECT_TYPE, unless the
27041 OBJECT_TYPE is not a class. */
27042 if (CLASS_TYPE_P (object_type))
27043 /* If the OBJECT_TYPE is a template specialization, it may
27044 be instantiated during name lookup. In that case, errors
27045 may be issued. Even if we rollback the current tentative
27046 parse, those errors are valid. */
27047 decl = lookup_member (object_type,
27048 name,
27049 /*protect=*/0,
27050 prefer_type_arg (tag_type),
27051 tf_warning_or_error);
27052 else
27053 decl = NULL_TREE;
27054
27055 if (!decl)
27056 /* Look it up in the enclosing context. DR 141: When looking for a
27057 template-name after -> or ., only consider class templates. */
27058 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
27059 /*nonclass=*/0,
27060 /*block_p=*/true, is_namespace, 0);
27061 if (object_type == unknown_type_node)
27062 /* The object is type-dependent, so we can't look anything up; we used
27063 this to get the DR 141 behavior. */
27064 object_type = NULL_TREE;
27065 parser->object_scope = object_type;
27066 parser->qualifying_scope = NULL_TREE;
27067 }
27068 else
27069 {
27070 decl = lookup_name_real (name, prefer_type_arg (tag_type),
27071 /*nonclass=*/0,
27072 /*block_p=*/true, is_namespace, 0);
27073 parser->qualifying_scope = NULL_TREE;
27074 parser->object_scope = NULL_TREE;
27075 }
27076
27077 /* If the lookup failed, let our caller know. */
27078 if (!decl || decl == error_mark_node)
27079 return error_mark_node;
27080
27081 /* Pull out the template from an injected-class-name (or multiple). */
27082 if (is_template)
27083 decl = maybe_get_template_decl_from_type_decl (decl);
27084
27085 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
27086 if (TREE_CODE (decl) == TREE_LIST)
27087 {
27088 if (ambiguous_decls)
27089 *ambiguous_decls = decl;
27090 /* The error message we have to print is too complicated for
27091 cp_parser_error, so we incorporate its actions directly. */
27092 if (!cp_parser_simulate_error (parser))
27093 {
27094 error_at (name_location, "reference to %qD is ambiguous",
27095 name);
27096 print_candidates (decl);
27097 }
27098 return error_mark_node;
27099 }
27100
27101 gcc_assert (DECL_P (decl)
27102 || TREE_CODE (decl) == OVERLOAD
27103 || TREE_CODE (decl) == SCOPE_REF
27104 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
27105 || BASELINK_P (decl));
27106
27107 /* If we have resolved the name of a member declaration, check to
27108 see if the declaration is accessible. When the name resolves to
27109 set of overloaded functions, accessibility is checked when
27110 overload resolution is done.
27111
27112 During an explicit instantiation, access is not checked at all,
27113 as per [temp.explicit]. */
27114 if (DECL_P (decl))
27115 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
27116
27117 maybe_record_typedef_use (decl);
27118
27119 return cp_expr (decl, name_location);
27120 }
27121
27122 /* Like cp_parser_lookup_name, but for use in the typical case where
27123 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
27124 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
27125
27126 static tree
27127 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
27128 {
27129 return cp_parser_lookup_name (parser, name,
27130 none_type,
27131 /*is_template=*/false,
27132 /*is_namespace=*/false,
27133 /*check_dependency=*/true,
27134 /*ambiguous_decls=*/NULL,
27135 location);
27136 }
27137
27138 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
27139 the current context, return the TYPE_DECL. If TAG_NAME_P is
27140 true, the DECL indicates the class being defined in a class-head,
27141 or declared in an elaborated-type-specifier.
27142
27143 Otherwise, return DECL. */
27144
27145 static tree
27146 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
27147 {
27148 /* If the TEMPLATE_DECL is being declared as part of a class-head,
27149 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
27150
27151 struct A {
27152 template <typename T> struct B;
27153 };
27154
27155 template <typename T> struct A::B {};
27156
27157 Similarly, in an elaborated-type-specifier:
27158
27159 namespace N { struct X{}; }
27160
27161 struct A {
27162 template <typename T> friend struct N::X;
27163 };
27164
27165 However, if the DECL refers to a class type, and we are in
27166 the scope of the class, then the name lookup automatically
27167 finds the TYPE_DECL created by build_self_reference rather
27168 than a TEMPLATE_DECL. For example, in:
27169
27170 template <class T> struct S {
27171 S s;
27172 };
27173
27174 there is no need to handle such case. */
27175
27176 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
27177 return DECL_TEMPLATE_RESULT (decl);
27178
27179 return decl;
27180 }
27181
27182 /* If too many, or too few, template-parameter lists apply to the
27183 declarator, issue an error message. Returns TRUE if all went well,
27184 and FALSE otherwise. */
27185
27186 static bool
27187 cp_parser_check_declarator_template_parameters (cp_parser* parser,
27188 cp_declarator *declarator,
27189 location_t declarator_location)
27190 {
27191 switch (declarator->kind)
27192 {
27193 case cdk_id:
27194 {
27195 unsigned num_templates = 0;
27196 tree scope = declarator->u.id.qualifying_scope;
27197 bool template_id_p = false;
27198
27199 if (scope)
27200 num_templates = num_template_headers_for_class (scope);
27201 else if (TREE_CODE (declarator->u.id.unqualified_name)
27202 == TEMPLATE_ID_EXPR)
27203 {
27204 /* If the DECLARATOR has the form `X<y>' then it uses one
27205 additional level of template parameters. */
27206 ++num_templates;
27207 template_id_p = true;
27208 }
27209
27210 return cp_parser_check_template_parameters
27211 (parser, num_templates, template_id_p, declarator_location,
27212 declarator);
27213 }
27214
27215 case cdk_function:
27216 case cdk_array:
27217 case cdk_pointer:
27218 case cdk_reference:
27219 case cdk_ptrmem:
27220 return (cp_parser_check_declarator_template_parameters
27221 (parser, declarator->declarator, declarator_location));
27222
27223 case cdk_decomp:
27224 case cdk_error:
27225 return true;
27226
27227 default:
27228 gcc_unreachable ();
27229 }
27230 return false;
27231 }
27232
27233 /* NUM_TEMPLATES were used in the current declaration. If that is
27234 invalid, return FALSE and issue an error messages. Otherwise,
27235 return TRUE. If DECLARATOR is non-NULL, then we are checking a
27236 declarator and we can print more accurate diagnostics. */
27237
27238 static bool
27239 cp_parser_check_template_parameters (cp_parser* parser,
27240 unsigned num_templates,
27241 bool template_id_p,
27242 location_t location,
27243 cp_declarator *declarator)
27244 {
27245 /* If there are the same number of template classes and parameter
27246 lists, that's OK. */
27247 if (parser->num_template_parameter_lists == num_templates)
27248 return true;
27249 /* If there are more, but only one more, and the name ends in an identifier,
27250 then we are declaring a primary template. That's OK too. */
27251 if (!template_id_p
27252 && parser->num_template_parameter_lists == num_templates + 1)
27253 return true;
27254 /* If there are more template classes than parameter lists, we have
27255 something like:
27256
27257 template <class T> void S<T>::R<T>::f (); */
27258 if (parser->num_template_parameter_lists < num_templates)
27259 {
27260 if (declarator && !current_function_decl)
27261 error_at (location, "specializing member %<%T::%E%> "
27262 "requires %<template<>%> syntax",
27263 declarator->u.id.qualifying_scope,
27264 declarator->u.id.unqualified_name);
27265 else if (declarator)
27266 error_at (location, "invalid declaration of %<%T::%E%>",
27267 declarator->u.id.qualifying_scope,
27268 declarator->u.id.unqualified_name);
27269 else
27270 error_at (location, "too few template-parameter-lists");
27271 return false;
27272 }
27273 /* Otherwise, there are too many template parameter lists. We have
27274 something like:
27275
27276 template <class T> template <class U> void S::f(); */
27277 error_at (location, "too many template-parameter-lists");
27278 return false;
27279 }
27280
27281 /* Parse an optional `::' token indicating that the following name is
27282 from the global namespace. If so, PARSER->SCOPE is set to the
27283 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
27284 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
27285 Returns the new value of PARSER->SCOPE, if the `::' token is
27286 present, and NULL_TREE otherwise. */
27287
27288 static tree
27289 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
27290 {
27291 cp_token *token;
27292
27293 /* Peek at the next token. */
27294 token = cp_lexer_peek_token (parser->lexer);
27295 /* If we're looking at a `::' token then we're starting from the
27296 global namespace, not our current location. */
27297 if (token->type == CPP_SCOPE)
27298 {
27299 /* Consume the `::' token. */
27300 cp_lexer_consume_token (parser->lexer);
27301 /* Set the SCOPE so that we know where to start the lookup. */
27302 parser->scope = global_namespace;
27303 parser->qualifying_scope = global_namespace;
27304 parser->object_scope = NULL_TREE;
27305
27306 return parser->scope;
27307 }
27308 else if (!current_scope_valid_p)
27309 {
27310 parser->scope = NULL_TREE;
27311 parser->qualifying_scope = NULL_TREE;
27312 parser->object_scope = NULL_TREE;
27313 }
27314
27315 return NULL_TREE;
27316 }
27317
27318 /* Returns TRUE if the upcoming token sequence is the start of a
27319 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
27320 declarator is preceded by the `friend' specifier. */
27321
27322 static bool
27323 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
27324 {
27325 bool constructor_p;
27326 bool outside_class_specifier_p;
27327 tree nested_name_specifier;
27328 cp_token *next_token;
27329
27330 /* The common case is that this is not a constructor declarator, so
27331 try to avoid doing lots of work if at all possible. It's not
27332 valid declare a constructor at function scope. */
27333 if (parser->in_function_body)
27334 return false;
27335 /* And only certain tokens can begin a constructor declarator. */
27336 next_token = cp_lexer_peek_token (parser->lexer);
27337 if (next_token->type != CPP_NAME
27338 && next_token->type != CPP_SCOPE
27339 && next_token->type != CPP_NESTED_NAME_SPECIFIER
27340 && next_token->type != CPP_TEMPLATE_ID)
27341 return false;
27342
27343 /* Parse tentatively; we are going to roll back all of the tokens
27344 consumed here. */
27345 cp_parser_parse_tentatively (parser);
27346 /* Assume that we are looking at a constructor declarator. */
27347 constructor_p = true;
27348
27349 /* Look for the optional `::' operator. */
27350 cp_parser_global_scope_opt (parser,
27351 /*current_scope_valid_p=*/false);
27352 /* Look for the nested-name-specifier. */
27353 nested_name_specifier
27354 = (cp_parser_nested_name_specifier_opt (parser,
27355 /*typename_keyword_p=*/false,
27356 /*check_dependency_p=*/false,
27357 /*type_p=*/false,
27358 /*is_declaration=*/false));
27359
27360 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
27361 if (nested_name_specifier
27362 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
27363 {
27364 tree s = resolve_typename_type (nested_name_specifier,
27365 /*only_current_p=*/false);
27366 if (TREE_CODE (s) != TYPENAME_TYPE)
27367 nested_name_specifier = s;
27368 }
27369
27370 outside_class_specifier_p = (!at_class_scope_p ()
27371 || !TYPE_BEING_DEFINED (current_class_type)
27372 || friend_p);
27373
27374 /* Outside of a class-specifier, there must be a
27375 nested-name-specifier. Except in C++17 mode, where we
27376 might be declaring a guiding declaration. */
27377 if (!nested_name_specifier && outside_class_specifier_p
27378 && cxx_dialect < cxx17)
27379 constructor_p = false;
27380 else if (nested_name_specifier == error_mark_node)
27381 constructor_p = false;
27382
27383 /* If we have a class scope, this is easy; DR 147 says that S::S always
27384 names the constructor, and no other qualified name could. */
27385 if (constructor_p && nested_name_specifier
27386 && CLASS_TYPE_P (nested_name_specifier))
27387 {
27388 tree id = cp_parser_unqualified_id (parser,
27389 /*template_keyword_p=*/false,
27390 /*check_dependency_p=*/false,
27391 /*declarator_p=*/true,
27392 /*optional_p=*/false);
27393 if (is_overloaded_fn (id))
27394 id = DECL_NAME (get_first_fn (id));
27395 if (!constructor_name_p (id, nested_name_specifier))
27396 constructor_p = false;
27397 }
27398 /* If we still think that this might be a constructor-declarator,
27399 look for a class-name. */
27400 else if (constructor_p)
27401 {
27402 /* If we have:
27403
27404 template <typename T> struct S {
27405 S();
27406 };
27407
27408 we must recognize that the nested `S' names a class. */
27409 if (cxx_dialect >= cxx17)
27410 cp_parser_parse_tentatively (parser);
27411
27412 tree type_decl;
27413 type_decl = cp_parser_class_name (parser,
27414 /*typename_keyword_p=*/false,
27415 /*template_keyword_p=*/false,
27416 none_type,
27417 /*check_dependency_p=*/false,
27418 /*class_head_p=*/false,
27419 /*is_declaration=*/false);
27420
27421 if (cxx_dialect >= cxx17
27422 && !cp_parser_parse_definitely (parser))
27423 {
27424 type_decl = NULL_TREE;
27425 tree tmpl = cp_parser_template_name (parser,
27426 /*template_keyword*/false,
27427 /*check_dependency_p*/false,
27428 /*is_declaration*/false,
27429 none_type,
27430 /*is_identifier*/NULL);
27431 if (DECL_CLASS_TEMPLATE_P (tmpl)
27432 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27433 /* It's a deduction guide, return true. */;
27434 else
27435 cp_parser_simulate_error (parser);
27436 }
27437
27438 /* If there was no class-name, then this is not a constructor.
27439 Otherwise, if we are in a class-specifier and we aren't
27440 handling a friend declaration, check that its type matches
27441 current_class_type (c++/38313). Note: error_mark_node
27442 is left alone for error recovery purposes. */
27443 constructor_p = (!cp_parser_error_occurred (parser)
27444 && (outside_class_specifier_p
27445 || type_decl == NULL_TREE
27446 || type_decl == error_mark_node
27447 || same_type_p (current_class_type,
27448 TREE_TYPE (type_decl))));
27449
27450 /* If we're still considering a constructor, we have to see a `(',
27451 to begin the parameter-declaration-clause, followed by either a
27452 `)', an `...', or a decl-specifier. We need to check for a
27453 type-specifier to avoid being fooled into thinking that:
27454
27455 S (f) (int);
27456
27457 is a constructor. (It is actually a function named `f' that
27458 takes one parameter (of type `int') and returns a value of type
27459 `S'. */
27460 if (constructor_p
27461 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27462 constructor_p = false;
27463
27464 if (constructor_p
27465 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
27466 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
27467 /* A parameter declaration begins with a decl-specifier,
27468 which is either the "attribute" keyword, a storage class
27469 specifier, or (usually) a type-specifier. */
27470 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
27471 {
27472 tree type;
27473 tree pushed_scope = NULL_TREE;
27474 unsigned saved_num_template_parameter_lists;
27475
27476 /* Names appearing in the type-specifier should be looked up
27477 in the scope of the class. */
27478 if (current_class_type)
27479 type = NULL_TREE;
27480 else if (type_decl)
27481 {
27482 type = TREE_TYPE (type_decl);
27483 if (TREE_CODE (type) == TYPENAME_TYPE)
27484 {
27485 type = resolve_typename_type (type,
27486 /*only_current_p=*/false);
27487 if (TREE_CODE (type) == TYPENAME_TYPE)
27488 {
27489 cp_parser_abort_tentative_parse (parser);
27490 return false;
27491 }
27492 }
27493 pushed_scope = push_scope (type);
27494 }
27495
27496 /* Inside the constructor parameter list, surrounding
27497 template-parameter-lists do not apply. */
27498 saved_num_template_parameter_lists
27499 = parser->num_template_parameter_lists;
27500 parser->num_template_parameter_lists = 0;
27501
27502 /* Look for the type-specifier. */
27503 cp_parser_type_specifier (parser,
27504 CP_PARSER_FLAGS_NONE,
27505 /*decl_specs=*/NULL,
27506 /*is_declarator=*/true,
27507 /*declares_class_or_enum=*/NULL,
27508 /*is_cv_qualifier=*/NULL);
27509
27510 parser->num_template_parameter_lists
27511 = saved_num_template_parameter_lists;
27512
27513 /* Leave the scope of the class. */
27514 if (pushed_scope)
27515 pop_scope (pushed_scope);
27516
27517 constructor_p = !cp_parser_error_occurred (parser);
27518 }
27519 }
27520
27521 /* We did not really want to consume any tokens. */
27522 cp_parser_abort_tentative_parse (parser);
27523
27524 return constructor_p;
27525 }
27526
27527 /* Parse the definition of the function given by the DECL_SPECIFIERS,
27528 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
27529 they must be performed once we are in the scope of the function.
27530
27531 Returns the function defined. */
27532
27533 static tree
27534 cp_parser_function_definition_from_specifiers_and_declarator
27535 (cp_parser* parser,
27536 cp_decl_specifier_seq *decl_specifiers,
27537 tree attributes,
27538 const cp_declarator *declarator)
27539 {
27540 tree fn;
27541 bool success_p;
27542
27543 /* Begin the function-definition. */
27544 success_p = start_function (decl_specifiers, declarator, attributes);
27545
27546 /* The things we're about to see are not directly qualified by any
27547 template headers we've seen thus far. */
27548 reset_specialization ();
27549
27550 /* If there were names looked up in the decl-specifier-seq that we
27551 did not check, check them now. We must wait until we are in the
27552 scope of the function to perform the checks, since the function
27553 might be a friend. */
27554 perform_deferred_access_checks (tf_warning_or_error);
27555
27556 if (success_p)
27557 {
27558 cp_finalize_omp_declare_simd (parser, current_function_decl);
27559 parser->omp_declare_simd = NULL;
27560 cp_finalize_oacc_routine (parser, current_function_decl, true);
27561 parser->oacc_routine = NULL;
27562 }
27563
27564 if (!success_p)
27565 {
27566 /* Skip the entire function. */
27567 cp_parser_skip_to_end_of_block_or_statement (parser);
27568 fn = error_mark_node;
27569 }
27570 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
27571 {
27572 /* Seen already, skip it. An error message has already been output. */
27573 cp_parser_skip_to_end_of_block_or_statement (parser);
27574 fn = current_function_decl;
27575 current_function_decl = NULL_TREE;
27576 /* If this is a function from a class, pop the nested class. */
27577 if (current_class_name)
27578 pop_nested_class ();
27579 }
27580 else
27581 {
27582 timevar_id_t tv;
27583 if (DECL_DECLARED_INLINE_P (current_function_decl))
27584 tv = TV_PARSE_INLINE;
27585 else
27586 tv = TV_PARSE_FUNC;
27587 timevar_push (tv);
27588 fn = cp_parser_function_definition_after_declarator (parser,
27589 /*inline_p=*/false);
27590 timevar_pop (tv);
27591 }
27592
27593 return fn;
27594 }
27595
27596 /* Parse the part of a function-definition that follows the
27597 declarator. INLINE_P is TRUE iff this function is an inline
27598 function defined within a class-specifier.
27599
27600 Returns the function defined. */
27601
27602 static tree
27603 cp_parser_function_definition_after_declarator (cp_parser* parser,
27604 bool inline_p)
27605 {
27606 tree fn;
27607 bool saved_in_unbraced_linkage_specification_p;
27608 bool saved_in_function_body;
27609 unsigned saved_num_template_parameter_lists;
27610 cp_token *token;
27611 bool fully_implicit_function_template_p
27612 = parser->fully_implicit_function_template_p;
27613 parser->fully_implicit_function_template_p = false;
27614 tree implicit_template_parms
27615 = parser->implicit_template_parms;
27616 parser->implicit_template_parms = 0;
27617 cp_binding_level* implicit_template_scope
27618 = parser->implicit_template_scope;
27619 parser->implicit_template_scope = 0;
27620
27621 saved_in_function_body = parser->in_function_body;
27622 parser->in_function_body = true;
27623 /* If the next token is `return', then the code may be trying to
27624 make use of the "named return value" extension that G++ used to
27625 support. */
27626 token = cp_lexer_peek_token (parser->lexer);
27627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
27628 {
27629 /* Consume the `return' keyword. */
27630 cp_lexer_consume_token (parser->lexer);
27631 /* Look for the identifier that indicates what value is to be
27632 returned. */
27633 cp_parser_identifier (parser);
27634 /* Issue an error message. */
27635 error_at (token->location,
27636 "named return values are no longer supported");
27637 /* Skip tokens until we reach the start of the function body. */
27638 while (true)
27639 {
27640 cp_token *token = cp_lexer_peek_token (parser->lexer);
27641 if (token->type == CPP_OPEN_BRACE
27642 || token->type == CPP_EOF
27643 || token->type == CPP_PRAGMA_EOL)
27644 break;
27645 cp_lexer_consume_token (parser->lexer);
27646 }
27647 }
27648 /* The `extern' in `extern "C" void f () { ... }' does not apply to
27649 anything declared inside `f'. */
27650 saved_in_unbraced_linkage_specification_p
27651 = parser->in_unbraced_linkage_specification_p;
27652 parser->in_unbraced_linkage_specification_p = false;
27653 /* Inside the function, surrounding template-parameter-lists do not
27654 apply. */
27655 saved_num_template_parameter_lists
27656 = parser->num_template_parameter_lists;
27657 parser->num_template_parameter_lists = 0;
27658
27659 /* If the next token is `try', `__transaction_atomic', or
27660 `__transaction_relaxed`, then we are looking at either function-try-block
27661 or function-transaction-block. Note that all of these include the
27662 function-body. */
27663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
27664 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
27665 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27666 RID_TRANSACTION_RELAXED))
27667 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
27668 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27669 cp_parser_function_try_block (parser);
27670 else
27671 cp_parser_ctor_initializer_opt_and_function_body
27672 (parser, /*in_function_try_block=*/false);
27673
27674 /* Finish the function. */
27675 fn = finish_function (inline_p);
27676 /* Generate code for it, if necessary. */
27677 expand_or_defer_fn (fn);
27678 /* Restore the saved values. */
27679 parser->in_unbraced_linkage_specification_p
27680 = saved_in_unbraced_linkage_specification_p;
27681 parser->num_template_parameter_lists
27682 = saved_num_template_parameter_lists;
27683 parser->in_function_body = saved_in_function_body;
27684
27685 parser->fully_implicit_function_template_p
27686 = fully_implicit_function_template_p;
27687 parser->implicit_template_parms
27688 = implicit_template_parms;
27689 parser->implicit_template_scope
27690 = implicit_template_scope;
27691
27692 if (parser->fully_implicit_function_template_p)
27693 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
27694
27695 return fn;
27696 }
27697
27698 /* Parse a template-declaration body (following argument list). */
27699
27700 static void
27701 cp_parser_template_declaration_after_parameters (cp_parser* parser,
27702 tree parameter_list,
27703 bool member_p)
27704 {
27705 tree decl = NULL_TREE;
27706 bool friend_p = false;
27707
27708 /* We just processed one more parameter list. */
27709 ++parser->num_template_parameter_lists;
27710
27711 /* Get the deferred access checks from the parameter list. These
27712 will be checked once we know what is being declared, as for a
27713 member template the checks must be performed in the scope of the
27714 class containing the member. */
27715 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
27716
27717 /* Tentatively parse for a new template parameter list, which can either be
27718 the template keyword or a template introduction. */
27719 if (cp_parser_template_declaration_after_export (parser, member_p))
27720 /* OK */;
27721 else if (cxx_dialect >= cxx11
27722 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27723 decl = cp_parser_alias_declaration (parser);
27724 else
27725 {
27726 /* There are no access checks when parsing a template, as we do not
27727 know if a specialization will be a friend. */
27728 push_deferring_access_checks (dk_no_check);
27729 cp_token *token = cp_lexer_peek_token (parser->lexer);
27730 decl = cp_parser_single_declaration (parser,
27731 checks,
27732 member_p,
27733 /*explicit_specialization_p=*/false,
27734 &friend_p);
27735 pop_deferring_access_checks ();
27736
27737 /* If this is a member template declaration, let the front
27738 end know. */
27739 if (member_p && !friend_p && decl)
27740 {
27741 if (TREE_CODE (decl) == TYPE_DECL)
27742 cp_parser_check_access_in_redeclaration (decl, token->location);
27743
27744 decl = finish_member_template_decl (decl);
27745 }
27746 else if (friend_p && decl
27747 && DECL_DECLARES_TYPE_P (decl))
27748 make_friend_class (current_class_type, TREE_TYPE (decl),
27749 /*complain=*/true);
27750 }
27751 /* We are done with the current parameter list. */
27752 --parser->num_template_parameter_lists;
27753
27754 pop_deferring_access_checks ();
27755
27756 /* Finish up. */
27757 finish_template_decl (parameter_list);
27758
27759 /* Check the template arguments for a literal operator template. */
27760 if (decl
27761 && DECL_DECLARES_FUNCTION_P (decl)
27762 && UDLIT_OPER_P (DECL_NAME (decl)))
27763 {
27764 bool ok = true;
27765 if (parameter_list == NULL_TREE)
27766 ok = false;
27767 else
27768 {
27769 int num_parms = TREE_VEC_LENGTH (parameter_list);
27770 if (num_parms == 1)
27771 {
27772 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
27773 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27774 if (CLASS_TYPE_P (TREE_TYPE (parm)))
27775 /* OK, C++20 string literal operator template. We don't need
27776 to warn in lower dialects here because we will have already
27777 warned about the template parameter. */;
27778 else if (TREE_TYPE (parm) != char_type_node
27779 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27780 ok = false;
27781 }
27782 else if (num_parms == 2 && cxx_dialect >= cxx14)
27783 {
27784 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
27785 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
27786 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
27787 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
27788 if (parm == error_mark_node
27789 || TREE_TYPE (parm) != TREE_TYPE (type)
27790 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
27791 ok = false;
27792 else
27793 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
27794 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
27795 "ISO C++ did not adopt string literal operator templa"
27796 "tes taking an argument pack of characters");
27797 }
27798 else
27799 ok = false;
27800 }
27801 if (!ok)
27802 {
27803 if (cxx_dialect > cxx17)
27804 error ("literal operator template %qD has invalid parameter list;"
27805 " Expected non-type template parameter pack <char...> "
27806 " or single non-type parameter of class type",
27807 decl);
27808 else
27809 error ("literal operator template %qD has invalid parameter list."
27810 " Expected non-type template parameter pack <char...>",
27811 decl);
27812 }
27813 }
27814
27815 /* Register member declarations. */
27816 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
27817 finish_member_declaration (decl);
27818 /* If DECL is a function template, we must return to parse it later.
27819 (Even though there is no definition, there might be default
27820 arguments that need handling.) */
27821 if (member_p && decl
27822 && DECL_DECLARES_FUNCTION_P (decl))
27823 vec_safe_push (unparsed_funs_with_definitions, decl);
27824 }
27825
27826 /* Parse a template introduction header for a template-declaration. Returns
27827 false if tentative parse fails. */
27828
27829 static bool
27830 cp_parser_template_introduction (cp_parser* parser, bool member_p)
27831 {
27832 cp_parser_parse_tentatively (parser);
27833
27834 tree saved_scope = parser->scope;
27835 tree saved_object_scope = parser->object_scope;
27836 tree saved_qualifying_scope = parser->qualifying_scope;
27837
27838 /* Look for the optional `::' operator. */
27839 cp_parser_global_scope_opt (parser,
27840 /*current_scope_valid_p=*/false);
27841 /* Look for the nested-name-specifier. */
27842 cp_parser_nested_name_specifier_opt (parser,
27843 /*typename_keyword_p=*/false,
27844 /*check_dependency_p=*/true,
27845 /*type_p=*/false,
27846 /*is_declaration=*/false);
27847
27848 cp_token *token = cp_lexer_peek_token (parser->lexer);
27849 tree concept_name = cp_parser_identifier (parser);
27850
27851 /* Look up the concept for which we will be matching
27852 template parameters. */
27853 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
27854 token->location);
27855 parser->scope = saved_scope;
27856 parser->object_scope = saved_object_scope;
27857 parser->qualifying_scope = saved_qualifying_scope;
27858
27859 if (concept_name == error_mark_node)
27860 cp_parser_simulate_error (parser);
27861
27862 /* Look for opening brace for introduction. */
27863 matching_braces braces;
27864 braces.require_open (parser);
27865
27866 if (!cp_parser_parse_definitely (parser))
27867 return false;
27868
27869 push_deferring_access_checks (dk_deferred);
27870
27871 /* Build vector of placeholder parameters and grab
27872 matching identifiers. */
27873 tree introduction_list = cp_parser_introduction_list (parser);
27874
27875 /* Look for closing brace for introduction. */
27876 if (!braces.require_close (parser))
27877 return true;
27878
27879 /* The introduction-list shall not be empty. */
27880 int nargs = TREE_VEC_LENGTH (introduction_list);
27881 if (nargs == 0)
27882 {
27883 /* In cp_parser_introduction_list we have already issued an error. */
27884 return true;
27885 }
27886
27887 if (tmpl_decl == error_mark_node)
27888 {
27889 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
27890 token->location);
27891 return true;
27892 }
27893
27894 /* Build and associate the constraint. */
27895 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
27896 if (parms && parms != error_mark_node)
27897 {
27898 cp_parser_template_declaration_after_parameters (parser, parms,
27899 member_p);
27900 return true;
27901 }
27902
27903 error_at (token->location, "no matching concept for template-introduction");
27904 return true;
27905 }
27906
27907 /* Parse a normal template-declaration following the template keyword. */
27908
27909 static void
27910 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
27911 {
27912 tree parameter_list;
27913 bool need_lang_pop;
27914 location_t location = input_location;
27915
27916 /* Look for the `<' token. */
27917 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
27918 return;
27919 if (at_class_scope_p () && current_function_decl)
27920 {
27921 /* 14.5.2.2 [temp.mem]
27922
27923 A local class shall not have member templates. */
27924 error_at (location,
27925 "invalid declaration of member template in local class");
27926 cp_parser_skip_to_end_of_block_or_statement (parser);
27927 return;
27928 }
27929 /* [temp]
27930
27931 A template ... shall not have C linkage. */
27932 if (current_lang_name == lang_name_c)
27933 {
27934 error_at (location, "template with C linkage");
27935 maybe_show_extern_c_location ();
27936 /* Give it C++ linkage to avoid confusing other parts of the
27937 front end. */
27938 push_lang_context (lang_name_cplusplus);
27939 need_lang_pop = true;
27940 }
27941 else
27942 need_lang_pop = false;
27943
27944 /* We cannot perform access checks on the template parameter
27945 declarations until we know what is being declared, just as we
27946 cannot check the decl-specifier list. */
27947 push_deferring_access_checks (dk_deferred);
27948
27949 /* If the next token is `>', then we have an invalid
27950 specialization. Rather than complain about an invalid template
27951 parameter, issue an error message here. */
27952 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27953 {
27954 cp_parser_error (parser, "invalid explicit specialization");
27955 begin_specialization ();
27956 parameter_list = NULL_TREE;
27957 }
27958 else
27959 {
27960 /* Parse the template parameters. */
27961 parameter_list = cp_parser_template_parameter_list (parser);
27962 }
27963
27964 /* Look for the `>'. */
27965 cp_parser_skip_to_end_of_template_parameter_list (parser);
27966
27967 /* Manage template requirements */
27968 if (flag_concepts)
27969 {
27970 tree reqs = get_shorthand_constraints (current_template_parms);
27971 if (tree r = cp_parser_requires_clause_opt (parser))
27972 reqs = conjoin_constraints (reqs, normalize_expression (r));
27973 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27974 }
27975
27976 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27977 member_p);
27978
27979 /* For the erroneous case of a template with C linkage, we pushed an
27980 implicit C++ linkage scope; exit that scope now. */
27981 if (need_lang_pop)
27982 pop_lang_context ();
27983 }
27984
27985 /* Parse a template-declaration, assuming that the `export' (and
27986 `extern') keywords, if present, has already been scanned. MEMBER_P
27987 is as for cp_parser_template_declaration. */
27988
27989 static bool
27990 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27991 {
27992 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27993 {
27994 cp_lexer_consume_token (parser->lexer);
27995 cp_parser_explicit_template_declaration (parser, member_p);
27996 return true;
27997 }
27998 else if (flag_concepts)
27999 return cp_parser_template_introduction (parser, member_p);
28000
28001 return false;
28002 }
28003
28004 /* Perform the deferred access checks from a template-parameter-list.
28005 CHECKS is a TREE_LIST of access checks, as returned by
28006 get_deferred_access_checks. */
28007
28008 static void
28009 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
28010 {
28011 ++processing_template_parmlist;
28012 perform_access_checks (checks, tf_warning_or_error);
28013 --processing_template_parmlist;
28014 }
28015
28016 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
28017 `function-definition' sequence that follows a template header.
28018 If MEMBER_P is true, this declaration appears in a class scope.
28019
28020 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
28021 *FRIEND_P is set to TRUE iff the declaration is a friend. */
28022
28023 static tree
28024 cp_parser_single_declaration (cp_parser* parser,
28025 vec<deferred_access_check, va_gc> *checks,
28026 bool member_p,
28027 bool explicit_specialization_p,
28028 bool* friend_p)
28029 {
28030 int declares_class_or_enum;
28031 tree decl = NULL_TREE;
28032 cp_decl_specifier_seq decl_specifiers;
28033 bool function_definition_p = false;
28034 cp_token *decl_spec_token_start;
28035
28036 /* This function is only used when processing a template
28037 declaration. */
28038 gcc_assert (innermost_scope_kind () == sk_template_parms
28039 || innermost_scope_kind () == sk_template_spec);
28040
28041 /* Defer access checks until we know what is being declared. */
28042 push_deferring_access_checks (dk_deferred);
28043
28044 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
28045 alternative. */
28046 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
28047 cp_parser_decl_specifier_seq (parser,
28048 (CP_PARSER_FLAGS_OPTIONAL
28049 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
28050 &decl_specifiers,
28051 &declares_class_or_enum);
28052 if (friend_p)
28053 *friend_p = cp_parser_friend_p (&decl_specifiers);
28054
28055 /* There are no template typedefs. */
28056 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
28057 {
28058 error_at (decl_spec_token_start->location,
28059 "template declaration of %<typedef%>");
28060 decl = error_mark_node;
28061 }
28062
28063 /* Gather up the access checks that occurred the
28064 decl-specifier-seq. */
28065 stop_deferring_access_checks ();
28066
28067 /* Check for the declaration of a template class. */
28068 if (declares_class_or_enum)
28069 {
28070 if (cp_parser_declares_only_class_p (parser)
28071 || (declares_class_or_enum & 2))
28072 {
28073 // If this is a declaration, but not a definition, associate
28074 // any constraints with the type declaration. Constraints
28075 // are associated with definitions in cp_parser_class_specifier.
28076 if (declares_class_or_enum == 1)
28077 associate_classtype_constraints (decl_specifiers.type);
28078
28079 decl = shadow_tag (&decl_specifiers);
28080
28081 /* In this case:
28082
28083 struct C {
28084 friend template <typename T> struct A<T>::B;
28085 };
28086
28087 A<T>::B will be represented by a TYPENAME_TYPE, and
28088 therefore not recognized by shadow_tag. */
28089 if (friend_p && *friend_p
28090 && !decl
28091 && decl_specifiers.type
28092 && TYPE_P (decl_specifiers.type))
28093 decl = decl_specifiers.type;
28094
28095 if (decl && decl != error_mark_node)
28096 decl = TYPE_NAME (decl);
28097 else
28098 decl = error_mark_node;
28099
28100 /* Perform access checks for template parameters. */
28101 cp_parser_perform_template_parameter_access_checks (checks);
28102
28103 /* Give a helpful diagnostic for
28104 template <class T> struct A { } a;
28105 if we aren't already recovering from an error. */
28106 if (!cp_parser_declares_only_class_p (parser)
28107 && !seen_error ())
28108 {
28109 error_at (cp_lexer_peek_token (parser->lexer)->location,
28110 "a class template declaration must not declare "
28111 "anything else");
28112 cp_parser_skip_to_end_of_block_or_statement (parser);
28113 goto out;
28114 }
28115 }
28116 }
28117
28118 /* Complain about missing 'typename' or other invalid type names. */
28119 if (!decl_specifiers.any_type_specifiers_p
28120 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
28121 {
28122 /* cp_parser_parse_and_diagnose_invalid_type_name calls
28123 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
28124 the rest of this declaration. */
28125 decl = error_mark_node;
28126 goto out;
28127 }
28128
28129 /* If it's not a template class, try for a template function. If
28130 the next token is a `;', then this declaration does not declare
28131 anything. But, if there were errors in the decl-specifiers, then
28132 the error might well have come from an attempted class-specifier.
28133 In that case, there's no need to warn about a missing declarator. */
28134 if (!decl
28135 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
28136 || decl_specifiers.type != error_mark_node))
28137 {
28138 decl = cp_parser_init_declarator (parser,
28139 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
28140 &decl_specifiers,
28141 checks,
28142 /*function_definition_allowed_p=*/true,
28143 member_p,
28144 declares_class_or_enum,
28145 &function_definition_p,
28146 NULL, NULL, NULL);
28147
28148 /* 7.1.1-1 [dcl.stc]
28149
28150 A storage-class-specifier shall not be specified in an explicit
28151 specialization... */
28152 if (decl
28153 && explicit_specialization_p
28154 && decl_specifiers.storage_class != sc_none)
28155 {
28156 error_at (decl_spec_token_start->location,
28157 "explicit template specialization cannot have a storage class");
28158 decl = error_mark_node;
28159 }
28160
28161 if (decl && VAR_P (decl))
28162 check_template_variable (decl);
28163 }
28164
28165 /* Look for a trailing `;' after the declaration. */
28166 if (!function_definition_p
28167 && (decl == error_mark_node
28168 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
28169 cp_parser_skip_to_end_of_block_or_statement (parser);
28170
28171 out:
28172 pop_deferring_access_checks ();
28173
28174 /* Clear any current qualification; whatever comes next is the start
28175 of something new. */
28176 parser->scope = NULL_TREE;
28177 parser->qualifying_scope = NULL_TREE;
28178 parser->object_scope = NULL_TREE;
28179
28180 return decl;
28181 }
28182
28183 /* Parse a cast-expression that is not the operand of a unary "&". */
28184
28185 static cp_expr
28186 cp_parser_simple_cast_expression (cp_parser *parser)
28187 {
28188 return cp_parser_cast_expression (parser, /*address_p=*/false,
28189 /*cast_p=*/false, /*decltype*/false, NULL);
28190 }
28191
28192 /* Parse a functional cast to TYPE. Returns an expression
28193 representing the cast. */
28194
28195 static cp_expr
28196 cp_parser_functional_cast (cp_parser* parser, tree type)
28197 {
28198 vec<tree, va_gc> *vec;
28199 tree expression_list;
28200 cp_expr cast;
28201 bool nonconst_p;
28202
28203 location_t start_loc = input_location;
28204
28205 if (!type)
28206 type = error_mark_node;
28207
28208 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28209 {
28210 cp_lexer_set_source_position (parser->lexer);
28211 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28212 expression_list = cp_parser_braced_list (parser, &nonconst_p);
28213 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
28214 if (TREE_CODE (type) == TYPE_DECL)
28215 type = TREE_TYPE (type);
28216
28217 cast = finish_compound_literal (type, expression_list,
28218 tf_warning_or_error, fcl_functional);
28219 /* Create a location of the form:
28220 type_name{i, f}
28221 ^~~~~~~~~~~~~~~
28222 with caret == start at the start of the type name,
28223 finishing at the closing brace. */
28224 location_t finish_loc
28225 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28226 location_t combined_loc = make_location (start_loc, start_loc,
28227 finish_loc);
28228 cast.set_location (combined_loc);
28229 return cast;
28230 }
28231
28232
28233 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
28234 /*cast_p=*/true,
28235 /*allow_expansion_p=*/true,
28236 /*non_constant_p=*/NULL);
28237 if (vec == NULL)
28238 expression_list = error_mark_node;
28239 else
28240 {
28241 expression_list = build_tree_list_vec (vec);
28242 release_tree_vector (vec);
28243 }
28244
28245 cast = build_functional_cast (type, expression_list,
28246 tf_warning_or_error);
28247 /* [expr.const]/1: In an integral constant expression "only type
28248 conversions to integral or enumeration type can be used". */
28249 if (TREE_CODE (type) == TYPE_DECL)
28250 type = TREE_TYPE (type);
28251 if (cast != error_mark_node
28252 && !cast_valid_in_integral_constant_expression_p (type)
28253 && cp_parser_non_integral_constant_expression (parser,
28254 NIC_CONSTRUCTOR))
28255 return error_mark_node;
28256
28257 /* Create a location of the form:
28258 float(i)
28259 ^~~~~~~~
28260 with caret == start at the start of the type name,
28261 finishing at the closing paren. */
28262 location_t finish_loc
28263 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
28264 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
28265 cast.set_location (combined_loc);
28266 return cast;
28267 }
28268
28269 /* Save the tokens that make up the body of a member function defined
28270 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
28271 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
28272 specifiers applied to the declaration. Returns the FUNCTION_DECL
28273 for the member function. */
28274
28275 static tree
28276 cp_parser_save_member_function_body (cp_parser* parser,
28277 cp_decl_specifier_seq *decl_specifiers,
28278 cp_declarator *declarator,
28279 tree attributes)
28280 {
28281 cp_token *first;
28282 cp_token *last;
28283 tree fn;
28284 bool function_try_block = false;
28285
28286 /* Create the FUNCTION_DECL. */
28287 fn = grokmethod (decl_specifiers, declarator, attributes);
28288 cp_finalize_omp_declare_simd (parser, fn);
28289 cp_finalize_oacc_routine (parser, fn, true);
28290 /* If something went badly wrong, bail out now. */
28291 if (fn == error_mark_node)
28292 {
28293 /* If there's a function-body, skip it. */
28294 if (cp_parser_token_starts_function_definition_p
28295 (cp_lexer_peek_token (parser->lexer)))
28296 cp_parser_skip_to_end_of_block_or_statement (parser);
28297 return error_mark_node;
28298 }
28299
28300 /* Remember it, if there default args to post process. */
28301 cp_parser_save_default_args (parser, fn);
28302
28303 /* Save away the tokens that make up the body of the
28304 function. */
28305 first = parser->lexer->next_token;
28306
28307 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
28308 cp_lexer_consume_token (parser->lexer);
28309 else if (cp_lexer_next_token_is_keyword (parser->lexer,
28310 RID_TRANSACTION_ATOMIC))
28311 {
28312 cp_lexer_consume_token (parser->lexer);
28313 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
28314 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
28315 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
28316 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
28317 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28318 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
28319 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
28320 {
28321 cp_lexer_consume_token (parser->lexer);
28322 cp_lexer_consume_token (parser->lexer);
28323 cp_lexer_consume_token (parser->lexer);
28324 cp_lexer_consume_token (parser->lexer);
28325 cp_lexer_consume_token (parser->lexer);
28326 }
28327 else
28328 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
28329 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
28330 {
28331 cp_lexer_consume_token (parser->lexer);
28332 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28333 break;
28334 }
28335 }
28336
28337 /* Handle function try blocks. */
28338 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
28339 {
28340 cp_lexer_consume_token (parser->lexer);
28341 function_try_block = true;
28342 }
28343 /* We can have braced-init-list mem-initializers before the fn body. */
28344 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
28345 {
28346 cp_lexer_consume_token (parser->lexer);
28347 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
28348 {
28349 /* cache_group will stop after an un-nested { } pair, too. */
28350 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
28351 break;
28352
28353 /* variadic mem-inits have ... after the ')'. */
28354 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28355 cp_lexer_consume_token (parser->lexer);
28356 }
28357 }
28358 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28359 /* Handle function try blocks. */
28360 if (function_try_block)
28361 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
28362 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28363 last = parser->lexer->next_token;
28364
28365 /* Save away the inline definition; we will process it when the
28366 class is complete. */
28367 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
28368 DECL_PENDING_INLINE_P (fn) = 1;
28369
28370 /* We need to know that this was defined in the class, so that
28371 friend templates are handled correctly. */
28372 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
28373
28374 /* Add FN to the queue of functions to be parsed later. */
28375 vec_safe_push (unparsed_funs_with_definitions, fn);
28376
28377 return fn;
28378 }
28379
28380 /* Save the tokens that make up the in-class initializer for a non-static
28381 data member. Returns a DEFAULT_ARG. */
28382
28383 static tree
28384 cp_parser_save_nsdmi (cp_parser* parser)
28385 {
28386 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
28387 }
28388
28389 /* Parse a template-argument-list, as well as the trailing ">" (but
28390 not the opening "<"). See cp_parser_template_argument_list for the
28391 return value. */
28392
28393 static tree
28394 cp_parser_enclosed_template_argument_list (cp_parser* parser)
28395 {
28396 tree arguments;
28397 tree saved_scope;
28398 tree saved_qualifying_scope;
28399 tree saved_object_scope;
28400 bool saved_greater_than_is_operator_p;
28401
28402 /* [temp.names]
28403
28404 When parsing a template-id, the first non-nested `>' is taken as
28405 the end of the template-argument-list rather than a greater-than
28406 operator. */
28407 saved_greater_than_is_operator_p
28408 = parser->greater_than_is_operator_p;
28409 parser->greater_than_is_operator_p = false;
28410 /* Parsing the argument list may modify SCOPE, so we save it
28411 here. */
28412 saved_scope = parser->scope;
28413 saved_qualifying_scope = parser->qualifying_scope;
28414 saved_object_scope = parser->object_scope;
28415 /* We need to evaluate the template arguments, even though this
28416 template-id may be nested within a "sizeof". */
28417 cp_evaluated ev;
28418 /* Parse the template-argument-list itself. */
28419 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
28420 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28421 arguments = NULL_TREE;
28422 else
28423 arguments = cp_parser_template_argument_list (parser);
28424 /* Look for the `>' that ends the template-argument-list. If we find
28425 a '>>' instead, it's probably just a typo. */
28426 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
28427 {
28428 if (cxx_dialect != cxx98)
28429 {
28430 /* In C++0x, a `>>' in a template argument list or cast
28431 expression is considered to be two separate `>'
28432 tokens. So, change the current token to a `>', but don't
28433 consume it: it will be consumed later when the outer
28434 template argument list (or cast expression) is parsed.
28435 Note that this replacement of `>' for `>>' is necessary
28436 even if we are parsing tentatively: in the tentative
28437 case, after calling
28438 cp_parser_enclosed_template_argument_list we will always
28439 throw away all of the template arguments and the first
28440 closing `>', either because the template argument list
28441 was erroneous or because we are replacing those tokens
28442 with a CPP_TEMPLATE_ID token. The second `>' (which will
28443 not have been thrown away) is needed either to close an
28444 outer template argument list or to complete a new-style
28445 cast. */
28446 cp_token *token = cp_lexer_peek_token (parser->lexer);
28447 token->type = CPP_GREATER;
28448 }
28449 else if (!saved_greater_than_is_operator_p)
28450 {
28451 /* If we're in a nested template argument list, the '>>' has
28452 to be a typo for '> >'. We emit the error message, but we
28453 continue parsing and we push a '>' as next token, so that
28454 the argument list will be parsed correctly. Note that the
28455 global source location is still on the token before the
28456 '>>', so we need to say explicitly where we want it. */
28457 cp_token *token = cp_lexer_peek_token (parser->lexer);
28458 gcc_rich_location richloc (token->location);
28459 richloc.add_fixit_replace ("> >");
28460 error_at (&richloc, "%<>>%> should be %<> >%> "
28461 "within a nested template argument list");
28462
28463 token->type = CPP_GREATER;
28464 }
28465 else
28466 {
28467 /* If this is not a nested template argument list, the '>>'
28468 is a typo for '>'. Emit an error message and continue.
28469 Same deal about the token location, but here we can get it
28470 right by consuming the '>>' before issuing the diagnostic. */
28471 cp_token *token = cp_lexer_consume_token (parser->lexer);
28472 error_at (token->location,
28473 "spurious %<>>%>, use %<>%> to terminate "
28474 "a template argument list");
28475 }
28476 }
28477 else
28478 cp_parser_skip_to_end_of_template_parameter_list (parser);
28479 /* The `>' token might be a greater-than operator again now. */
28480 parser->greater_than_is_operator_p
28481 = saved_greater_than_is_operator_p;
28482 /* Restore the SAVED_SCOPE. */
28483 parser->scope = saved_scope;
28484 parser->qualifying_scope = saved_qualifying_scope;
28485 parser->object_scope = saved_object_scope;
28486
28487 return arguments;
28488 }
28489
28490 /* MEMBER_FUNCTION is a member function, or a friend. If default
28491 arguments, or the body of the function have not yet been parsed,
28492 parse them now. */
28493
28494 static void
28495 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
28496 {
28497 timevar_push (TV_PARSE_INMETH);
28498 /* If this member is a template, get the underlying
28499 FUNCTION_DECL. */
28500 if (DECL_FUNCTION_TEMPLATE_P (member_function))
28501 member_function = DECL_TEMPLATE_RESULT (member_function);
28502
28503 /* There should not be any class definitions in progress at this
28504 point; the bodies of members are only parsed outside of all class
28505 definitions. */
28506 gcc_assert (parser->num_classes_being_defined == 0);
28507 /* While we're parsing the member functions we might encounter more
28508 classes. We want to handle them right away, but we don't want
28509 them getting mixed up with functions that are currently in the
28510 queue. */
28511 push_unparsed_function_queues (parser);
28512
28513 /* Make sure that any template parameters are in scope. */
28514 maybe_begin_member_template_processing (member_function);
28515
28516 /* If the body of the function has not yet been parsed, parse it
28517 now. */
28518 if (DECL_PENDING_INLINE_P (member_function))
28519 {
28520 tree function_scope;
28521 cp_token_cache *tokens;
28522
28523 /* The function is no longer pending; we are processing it. */
28524 tokens = DECL_PENDING_INLINE_INFO (member_function);
28525 DECL_PENDING_INLINE_INFO (member_function) = NULL;
28526 DECL_PENDING_INLINE_P (member_function) = 0;
28527
28528 /* If this is a local class, enter the scope of the containing
28529 function. */
28530 function_scope = current_function_decl;
28531 if (function_scope)
28532 push_function_context ();
28533
28534 /* Push the body of the function onto the lexer stack. */
28535 cp_parser_push_lexer_for_tokens (parser, tokens);
28536
28537 /* Let the front end know that we going to be defining this
28538 function. */
28539 start_preparsed_function (member_function, NULL_TREE,
28540 SF_PRE_PARSED | SF_INCLASS_INLINE);
28541
28542 /* Don't do access checking if it is a templated function. */
28543 if (processing_template_decl)
28544 push_deferring_access_checks (dk_no_check);
28545
28546 /* #pragma omp declare reduction needs special parsing. */
28547 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
28548 {
28549 parser->lexer->in_pragma = true;
28550 cp_parser_omp_declare_reduction_exprs (member_function, parser);
28551 finish_function (/*inline_p=*/true);
28552 cp_check_omp_declare_reduction (member_function);
28553 }
28554 else
28555 /* Now, parse the body of the function. */
28556 cp_parser_function_definition_after_declarator (parser,
28557 /*inline_p=*/true);
28558
28559 if (processing_template_decl)
28560 pop_deferring_access_checks ();
28561
28562 /* Leave the scope of the containing function. */
28563 if (function_scope)
28564 pop_function_context ();
28565 cp_parser_pop_lexer (parser);
28566 }
28567
28568 /* Remove any template parameters from the symbol table. */
28569 maybe_end_member_template_processing ();
28570
28571 /* Restore the queue. */
28572 pop_unparsed_function_queues (parser);
28573 timevar_pop (TV_PARSE_INMETH);
28574 }
28575
28576 /* If DECL contains any default args, remember it on the unparsed
28577 functions queue. */
28578
28579 static void
28580 cp_parser_save_default_args (cp_parser* parser, tree decl)
28581 {
28582 tree probe;
28583
28584 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
28585 probe;
28586 probe = TREE_CHAIN (probe))
28587 if (TREE_PURPOSE (probe))
28588 {
28589 cp_default_arg_entry entry = {current_class_type, decl};
28590 vec_safe_push (unparsed_funs_with_default_args, entry);
28591 break;
28592 }
28593 }
28594
28595 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
28596 which is either a FIELD_DECL or PARM_DECL. Parse it and return
28597 the result. For a PARM_DECL, PARMTYPE is the corresponding type
28598 from the parameter-type-list. */
28599
28600 static tree
28601 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
28602 tree default_arg, tree parmtype)
28603 {
28604 cp_token_cache *tokens;
28605 tree parsed_arg;
28606 bool dummy;
28607
28608 if (default_arg == error_mark_node)
28609 return error_mark_node;
28610
28611 /* Push the saved tokens for the default argument onto the parser's
28612 lexer stack. */
28613 tokens = DEFARG_TOKENS (default_arg);
28614 cp_parser_push_lexer_for_tokens (parser, tokens);
28615
28616 start_lambda_scope (decl);
28617
28618 /* Parse the default argument. */
28619 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
28620 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
28621 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28622
28623 finish_lambda_scope ();
28624
28625 if (parsed_arg == error_mark_node)
28626 cp_parser_skip_to_end_of_statement (parser);
28627
28628 if (!processing_template_decl)
28629 {
28630 /* In a non-template class, check conversions now. In a template,
28631 we'll wait and instantiate these as needed. */
28632 if (TREE_CODE (decl) == PARM_DECL)
28633 parsed_arg = check_default_argument (parmtype, parsed_arg,
28634 tf_warning_or_error);
28635 else if (maybe_reject_flexarray_init (decl, parsed_arg))
28636 parsed_arg = error_mark_node;
28637 else
28638 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
28639 }
28640
28641 /* If the token stream has not been completely used up, then
28642 there was extra junk after the end of the default
28643 argument. */
28644 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
28645 {
28646 if (TREE_CODE (decl) == PARM_DECL)
28647 cp_parser_error (parser, "expected %<,%>");
28648 else
28649 cp_parser_error (parser, "expected %<;%>");
28650 }
28651
28652 /* Revert to the main lexer. */
28653 cp_parser_pop_lexer (parser);
28654
28655 return parsed_arg;
28656 }
28657
28658 /* FIELD is a non-static data member with an initializer which we saved for
28659 later; parse it now. */
28660
28661 static void
28662 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
28663 {
28664 tree def;
28665
28666 maybe_begin_member_template_processing (field);
28667
28668 push_unparsed_function_queues (parser);
28669 def = cp_parser_late_parse_one_default_arg (parser, field,
28670 DECL_INITIAL (field),
28671 NULL_TREE);
28672 pop_unparsed_function_queues (parser);
28673
28674 maybe_end_member_template_processing ();
28675
28676 DECL_INITIAL (field) = def;
28677 }
28678
28679 /* FN is a FUNCTION_DECL which may contains a parameter with an
28680 unparsed DEFAULT_ARG. Parse the default args now. This function
28681 assumes that the current scope is the scope in which the default
28682 argument should be processed. */
28683
28684 static void
28685 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
28686 {
28687 unsigned char saved_local_variables_forbidden_p;
28688 tree parm, parmdecl;
28689
28690 /* While we're parsing the default args, we might (due to the
28691 statement expression extension) encounter more classes. We want
28692 to handle them right away, but we don't want them getting mixed
28693 up with default args that are currently in the queue. */
28694 push_unparsed_function_queues (parser);
28695
28696 /* Local variable names (and the `this' keyword) may not appear
28697 in a default argument. */
28698 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
28699 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
28700
28701 push_defarg_context (fn);
28702
28703 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
28704 parmdecl = DECL_ARGUMENTS (fn);
28705 parm && parm != void_list_node;
28706 parm = TREE_CHAIN (parm),
28707 parmdecl = DECL_CHAIN (parmdecl))
28708 {
28709 tree default_arg = TREE_PURPOSE (parm);
28710 tree parsed_arg;
28711 vec<tree, va_gc> *insts;
28712 tree copy;
28713 unsigned ix;
28714
28715 if (!default_arg)
28716 continue;
28717
28718 if (TREE_CODE (default_arg) != DEFAULT_ARG)
28719 /* This can happen for a friend declaration for a function
28720 already declared with default arguments. */
28721 continue;
28722
28723 parsed_arg
28724 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
28725 default_arg,
28726 TREE_VALUE (parm));
28727 TREE_PURPOSE (parm) = parsed_arg;
28728
28729 /* Update any instantiations we've already created. */
28730 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
28731 vec_safe_iterate (insts, ix, &copy); ix++)
28732 TREE_PURPOSE (copy) = parsed_arg;
28733 }
28734
28735 pop_defarg_context ();
28736
28737 /* Make sure no default arg is missing. */
28738 check_default_args (fn);
28739
28740 /* Restore the state of local_variables_forbidden_p. */
28741 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
28742
28743 /* Restore the queue. */
28744 pop_unparsed_function_queues (parser);
28745 }
28746
28747 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
28748
28749 sizeof ... ( identifier )
28750
28751 where the 'sizeof' token has already been consumed. */
28752
28753 static tree
28754 cp_parser_sizeof_pack (cp_parser *parser)
28755 {
28756 /* Consume the `...'. */
28757 cp_lexer_consume_token (parser->lexer);
28758 maybe_warn_variadic_templates ();
28759
28760 matching_parens parens;
28761 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
28762 if (paren)
28763 parens.consume_open (parser);
28764 else
28765 permerror (cp_lexer_peek_token (parser->lexer)->location,
28766 "%<sizeof...%> argument must be surrounded by parentheses");
28767
28768 cp_token *token = cp_lexer_peek_token (parser->lexer);
28769 tree name = cp_parser_identifier (parser);
28770 if (name == error_mark_node)
28771 return error_mark_node;
28772 /* The name is not qualified. */
28773 parser->scope = NULL_TREE;
28774 parser->qualifying_scope = NULL_TREE;
28775 parser->object_scope = NULL_TREE;
28776 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
28777 if (expr == error_mark_node)
28778 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
28779 token->location);
28780 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
28781 expr = TREE_TYPE (expr);
28782 else if (TREE_CODE (expr) == CONST_DECL)
28783 expr = DECL_INITIAL (expr);
28784 expr = make_pack_expansion (expr);
28785 PACK_EXPANSION_SIZEOF_P (expr) = true;
28786
28787 if (paren)
28788 parens.require_close (parser);
28789
28790 return expr;
28791 }
28792
28793 /* Parse the operand of `sizeof' (or a similar operator). Returns
28794 either a TYPE or an expression, depending on the form of the
28795 input. The KEYWORD indicates which kind of expression we have
28796 encountered. */
28797
28798 static tree
28799 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
28800 {
28801 tree expr = NULL_TREE;
28802 const char *saved_message;
28803 char *tmp;
28804 bool saved_integral_constant_expression_p;
28805 bool saved_non_integral_constant_expression_p;
28806
28807 /* If it's a `...', then we are computing the length of a parameter
28808 pack. */
28809 if (keyword == RID_SIZEOF
28810 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28811 return cp_parser_sizeof_pack (parser);
28812
28813 /* Types cannot be defined in a `sizeof' expression. Save away the
28814 old message. */
28815 saved_message = parser->type_definition_forbidden_message;
28816 /* And create the new one. */
28817 tmp = concat ("types may not be defined in %<",
28818 IDENTIFIER_POINTER (ridpointers[keyword]),
28819 "%> expressions", NULL);
28820 parser->type_definition_forbidden_message = tmp;
28821
28822 /* The restrictions on constant-expressions do not apply inside
28823 sizeof expressions. */
28824 saved_integral_constant_expression_p
28825 = parser->integral_constant_expression_p;
28826 saved_non_integral_constant_expression_p
28827 = parser->non_integral_constant_expression_p;
28828 parser->integral_constant_expression_p = false;
28829
28830 /* Do not actually evaluate the expression. */
28831 ++cp_unevaluated_operand;
28832 ++c_inhibit_evaluation_warnings;
28833 /* If it's a `(', then we might be looking at the type-id
28834 construction. */
28835 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28836 {
28837 tree type = NULL_TREE;
28838
28839 /* We can't be sure yet whether we're looking at a type-id or an
28840 expression. */
28841 cp_parser_parse_tentatively (parser);
28842
28843 matching_parens parens;
28844 parens.consume_open (parser);
28845
28846 /* Note: as a GNU Extension, compound literals are considered
28847 postfix-expressions as they are in C99, so they are valid
28848 arguments to sizeof. See comment in cp_parser_cast_expression
28849 for details. */
28850 if (cp_parser_compound_literal_p (parser))
28851 cp_parser_simulate_error (parser);
28852 else
28853 {
28854 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
28855 parser->in_type_id_in_expr_p = true;
28856 /* Look for the type-id. */
28857 type = cp_parser_type_id (parser);
28858 /* Look for the closing `)'. */
28859 parens.require_close (parser);
28860 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
28861 }
28862
28863 /* If all went well, then we're done. */
28864 if (cp_parser_parse_definitely (parser))
28865 expr = type;
28866 }
28867
28868 /* If the type-id production did not work out, then we must be
28869 looking at the unary-expression production. */
28870 if (!expr)
28871 expr = cp_parser_unary_expression (parser);
28872
28873 /* Go back to evaluating expressions. */
28874 --cp_unevaluated_operand;
28875 --c_inhibit_evaluation_warnings;
28876
28877 /* Free the message we created. */
28878 free (tmp);
28879 /* And restore the old one. */
28880 parser->type_definition_forbidden_message = saved_message;
28881 parser->integral_constant_expression_p
28882 = saved_integral_constant_expression_p;
28883 parser->non_integral_constant_expression_p
28884 = saved_non_integral_constant_expression_p;
28885
28886 return expr;
28887 }
28888
28889 /* If the current declaration has no declarator, return true. */
28890
28891 static bool
28892 cp_parser_declares_only_class_p (cp_parser *parser)
28893 {
28894 /* If the next token is a `;' or a `,' then there is no
28895 declarator. */
28896 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28897 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
28898 }
28899
28900 /* Update the DECL_SPECS to reflect the storage class indicated by
28901 KEYWORD. */
28902
28903 static void
28904 cp_parser_set_storage_class (cp_parser *parser,
28905 cp_decl_specifier_seq *decl_specs,
28906 enum rid keyword,
28907 cp_token *token)
28908 {
28909 cp_storage_class storage_class;
28910
28911 if (parser->in_unbraced_linkage_specification_p)
28912 {
28913 error_at (token->location, "invalid use of %qD in linkage specification",
28914 ridpointers[keyword]);
28915 return;
28916 }
28917 else if (decl_specs->storage_class != sc_none)
28918 {
28919 decl_specs->conflicting_specifiers_p = true;
28920 return;
28921 }
28922
28923 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28924 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28925 && decl_specs->gnu_thread_keyword_p)
28926 {
28927 pedwarn (decl_specs->locations[ds_thread], 0,
28928 "%<__thread%> before %qD", ridpointers[keyword]);
28929 }
28930
28931 switch (keyword)
28932 {
28933 case RID_AUTO:
28934 storage_class = sc_auto;
28935 break;
28936 case RID_REGISTER:
28937 storage_class = sc_register;
28938 break;
28939 case RID_STATIC:
28940 storage_class = sc_static;
28941 break;
28942 case RID_EXTERN:
28943 storage_class = sc_extern;
28944 break;
28945 case RID_MUTABLE:
28946 storage_class = sc_mutable;
28947 break;
28948 default:
28949 gcc_unreachable ();
28950 }
28951 decl_specs->storage_class = storage_class;
28952 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28953
28954 /* A storage class specifier cannot be applied alongside a typedef
28955 specifier. If there is a typedef specifier present then set
28956 conflicting_specifiers_p which will trigger an error later
28957 on in grokdeclarator. */
28958 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28959 decl_specs->conflicting_specifiers_p = true;
28960 }
28961
28962 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28963 is true, the type is a class or enum definition. */
28964
28965 static void
28966 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28967 tree type_spec,
28968 cp_token *token,
28969 bool type_definition_p)
28970 {
28971 decl_specs->any_specifiers_p = true;
28972
28973 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
28974 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
28975 this is what happened. In system headers, we ignore these
28976 declarations so that G++ can work with system headers that are not
28977 C++-safe. */
28978 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28979 && !type_definition_p
28980 && (type_spec == boolean_type_node
28981 || type_spec == char8_type_node
28982 || type_spec == char16_type_node
28983 || type_spec == char32_type_node
28984 || type_spec == wchar_type_node)
28985 && (decl_specs->type
28986 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28987 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28988 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28989 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28990 {
28991 decl_specs->redefined_builtin_type = type_spec;
28992 set_and_check_decl_spec_loc (decl_specs,
28993 ds_redefined_builtin_type_spec,
28994 token);
28995 if (!decl_specs->type)
28996 {
28997 decl_specs->type = type_spec;
28998 decl_specs->type_definition_p = false;
28999 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
29000 }
29001 }
29002 else if (decl_specs->type)
29003 decl_specs->multiple_types_p = true;
29004 else
29005 {
29006 decl_specs->type = type_spec;
29007 decl_specs->type_definition_p = type_definition_p;
29008 decl_specs->redefined_builtin_type = NULL_TREE;
29009 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
29010 }
29011 }
29012
29013 /* True iff TOKEN is the GNU keyword __thread. */
29014
29015 static bool
29016 token_is__thread (cp_token *token)
29017 {
29018 gcc_assert (token->keyword == RID_THREAD);
29019 return id_equal (token->u.value, "__thread");
29020 }
29021
29022 /* Set the location for a declarator specifier and check if it is
29023 duplicated.
29024
29025 DECL_SPECS is the sequence of declarator specifiers onto which to
29026 set the location.
29027
29028 DS is the single declarator specifier to set which location is to
29029 be set onto the existing sequence of declarators.
29030
29031 LOCATION is the location for the declarator specifier to
29032 consider. */
29033
29034 static void
29035 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
29036 cp_decl_spec ds, cp_token *token)
29037 {
29038 gcc_assert (ds < ds_last);
29039
29040 if (decl_specs == NULL)
29041 return;
29042
29043 location_t location = token->location;
29044
29045 if (decl_specs->locations[ds] == 0)
29046 {
29047 decl_specs->locations[ds] = location;
29048 if (ds == ds_thread)
29049 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
29050 }
29051 else
29052 {
29053 if (ds == ds_long)
29054 {
29055 if (decl_specs->locations[ds_long_long] != 0)
29056 error_at (location,
29057 "%<long long long%> is too long for GCC");
29058 else
29059 {
29060 decl_specs->locations[ds_long_long] = location;
29061 pedwarn_cxx98 (location,
29062 OPT_Wlong_long,
29063 "ISO C++ 1998 does not support %<long long%>");
29064 }
29065 }
29066 else if (ds == ds_thread)
29067 {
29068 bool gnu = token_is__thread (token);
29069 gcc_rich_location richloc (location);
29070 if (gnu != decl_specs->gnu_thread_keyword_p)
29071 {
29072 richloc.add_range (decl_specs->locations[ds_thread]);
29073 error_at (&richloc,
29074 "both %<__thread%> and %<thread_local%> specified");
29075 }
29076 else
29077 {
29078 richloc.add_fixit_remove ();
29079 error_at (&richloc, "duplicate %qD", token->u.value);
29080 }
29081 }
29082 else
29083 {
29084 static const char *const decl_spec_names[] = {
29085 "signed",
29086 "unsigned",
29087 "short",
29088 "long",
29089 "const",
29090 "volatile",
29091 "restrict",
29092 "inline",
29093 "virtual",
29094 "explicit",
29095 "friend",
29096 "typedef",
29097 "using",
29098 "constexpr",
29099 "__complex"
29100 };
29101 gcc_rich_location richloc (location);
29102 richloc.add_fixit_remove ();
29103 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
29104 }
29105 }
29106 }
29107
29108 /* Return true iff the declarator specifier DS is present in the
29109 sequence of declarator specifiers DECL_SPECS. */
29110
29111 bool
29112 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
29113 cp_decl_spec ds)
29114 {
29115 gcc_assert (ds < ds_last);
29116
29117 if (decl_specs == NULL)
29118 return false;
29119
29120 return decl_specs->locations[ds] != 0;
29121 }
29122
29123 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
29124 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
29125
29126 static bool
29127 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
29128 {
29129 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
29130 }
29131
29132 /* Issue an error message indicating that TOKEN_DESC was expected.
29133 If KEYWORD is true, it indicated this function is called by
29134 cp_parser_require_keword and the required token can only be
29135 a indicated keyword.
29136
29137 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29138 within any error as the location of an "opening" token matching
29139 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29140 RT_CLOSE_PAREN). */
29141
29142 static void
29143 cp_parser_required_error (cp_parser *parser,
29144 required_token token_desc,
29145 bool keyword,
29146 location_t matching_location)
29147 {
29148 if (cp_parser_simulate_error (parser))
29149 return;
29150
29151 const char *gmsgid = NULL;
29152 switch (token_desc)
29153 {
29154 case RT_NEW:
29155 gmsgid = G_("expected %<new%>");
29156 break;
29157 case RT_DELETE:
29158 gmsgid = G_("expected %<delete%>");
29159 break;
29160 case RT_RETURN:
29161 gmsgid = G_("expected %<return%>");
29162 break;
29163 case RT_WHILE:
29164 gmsgid = G_("expected %<while%>");
29165 break;
29166 case RT_EXTERN:
29167 gmsgid = G_("expected %<extern%>");
29168 break;
29169 case RT_STATIC_ASSERT:
29170 gmsgid = G_("expected %<static_assert%>");
29171 break;
29172 case RT_DECLTYPE:
29173 gmsgid = G_("expected %<decltype%>");
29174 break;
29175 case RT_OPERATOR:
29176 gmsgid = G_("expected %<operator%>");
29177 break;
29178 case RT_CLASS:
29179 gmsgid = G_("expected %<class%>");
29180 break;
29181 case RT_TEMPLATE:
29182 gmsgid = G_("expected %<template%>");
29183 break;
29184 case RT_NAMESPACE:
29185 gmsgid = G_("expected %<namespace%>");
29186 break;
29187 case RT_USING:
29188 gmsgid = G_("expected %<using%>");
29189 break;
29190 case RT_ASM:
29191 gmsgid = G_("expected %<asm%>");
29192 break;
29193 case RT_TRY:
29194 gmsgid = G_("expected %<try%>");
29195 break;
29196 case RT_CATCH:
29197 gmsgid = G_("expected %<catch%>");
29198 break;
29199 case RT_THROW:
29200 gmsgid = G_("expected %<throw%>");
29201 break;
29202 case RT_LABEL:
29203 gmsgid = G_("expected %<__label__%>");
29204 break;
29205 case RT_AT_TRY:
29206 gmsgid = G_("expected %<@try%>");
29207 break;
29208 case RT_AT_SYNCHRONIZED:
29209 gmsgid = G_("expected %<@synchronized%>");
29210 break;
29211 case RT_AT_THROW:
29212 gmsgid = G_("expected %<@throw%>");
29213 break;
29214 case RT_TRANSACTION_ATOMIC:
29215 gmsgid = G_("expected %<__transaction_atomic%>");
29216 break;
29217 case RT_TRANSACTION_RELAXED:
29218 gmsgid = G_("expected %<__transaction_relaxed%>");
29219 break;
29220 default:
29221 break;
29222 }
29223
29224 if (!gmsgid && !keyword)
29225 {
29226 switch (token_desc)
29227 {
29228 case RT_SEMICOLON:
29229 gmsgid = G_("expected %<;%>");
29230 break;
29231 case RT_OPEN_PAREN:
29232 gmsgid = G_("expected %<(%>");
29233 break;
29234 case RT_CLOSE_BRACE:
29235 gmsgid = G_("expected %<}%>");
29236 break;
29237 case RT_OPEN_BRACE:
29238 gmsgid = G_("expected %<{%>");
29239 break;
29240 case RT_CLOSE_SQUARE:
29241 gmsgid = G_("expected %<]%>");
29242 break;
29243 case RT_OPEN_SQUARE:
29244 gmsgid = G_("expected %<[%>");
29245 break;
29246 case RT_COMMA:
29247 gmsgid = G_("expected %<,%>");
29248 break;
29249 case RT_SCOPE:
29250 gmsgid = G_("expected %<::%>");
29251 break;
29252 case RT_LESS:
29253 gmsgid = G_("expected %<<%>");
29254 break;
29255 case RT_GREATER:
29256 gmsgid = G_("expected %<>%>");
29257 break;
29258 case RT_EQ:
29259 gmsgid = G_("expected %<=%>");
29260 break;
29261 case RT_ELLIPSIS:
29262 gmsgid = G_("expected %<...%>");
29263 break;
29264 case RT_MULT:
29265 gmsgid = G_("expected %<*%>");
29266 break;
29267 case RT_COMPL:
29268 gmsgid = G_("expected %<~%>");
29269 break;
29270 case RT_COLON:
29271 gmsgid = G_("expected %<:%>");
29272 break;
29273 case RT_COLON_SCOPE:
29274 gmsgid = G_("expected %<:%> or %<::%>");
29275 break;
29276 case RT_CLOSE_PAREN:
29277 gmsgid = G_("expected %<)%>");
29278 break;
29279 case RT_COMMA_CLOSE_PAREN:
29280 gmsgid = G_("expected %<,%> or %<)%>");
29281 break;
29282 case RT_PRAGMA_EOL:
29283 gmsgid = G_("expected end of line");
29284 break;
29285 case RT_NAME:
29286 gmsgid = G_("expected identifier");
29287 break;
29288 case RT_SELECT:
29289 gmsgid = G_("expected selection-statement");
29290 break;
29291 case RT_ITERATION:
29292 gmsgid = G_("expected iteration-statement");
29293 break;
29294 case RT_JUMP:
29295 gmsgid = G_("expected jump-statement");
29296 break;
29297 case RT_CLASS_KEY:
29298 gmsgid = G_("expected class-key");
29299 break;
29300 case RT_CLASS_TYPENAME_TEMPLATE:
29301 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
29302 break;
29303 default:
29304 gcc_unreachable ();
29305 }
29306 }
29307
29308 if (gmsgid)
29309 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
29310 }
29311
29312
29313 /* If the next token is of the indicated TYPE, consume it. Otherwise,
29314 issue an error message indicating that TOKEN_DESC was expected.
29315
29316 Returns the token consumed, if the token had the appropriate type.
29317 Otherwise, returns NULL.
29318
29319 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
29320 within any error as the location of an "opening" token matching
29321 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
29322 RT_CLOSE_PAREN). */
29323
29324 static cp_token *
29325 cp_parser_require (cp_parser* parser,
29326 enum cpp_ttype type,
29327 required_token token_desc,
29328 location_t matching_location)
29329 {
29330 if (cp_lexer_next_token_is (parser->lexer, type))
29331 return cp_lexer_consume_token (parser->lexer);
29332 else
29333 {
29334 /* Output the MESSAGE -- unless we're parsing tentatively. */
29335 if (!cp_parser_simulate_error (parser))
29336 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
29337 matching_location);
29338 return NULL;
29339 }
29340 }
29341
29342 /* An error message is produced if the next token is not '>'.
29343 All further tokens are skipped until the desired token is
29344 found or '{', '}', ';' or an unbalanced ')' or ']'. */
29345
29346 static void
29347 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
29348 {
29349 /* Current level of '< ... >'. */
29350 unsigned level = 0;
29351 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
29352 unsigned nesting_depth = 0;
29353
29354 /* Are we ready, yet? If not, issue error message. */
29355 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
29356 return;
29357
29358 /* Skip tokens until the desired token is found. */
29359 while (true)
29360 {
29361 /* Peek at the next token. */
29362 switch (cp_lexer_peek_token (parser->lexer)->type)
29363 {
29364 case CPP_LESS:
29365 if (!nesting_depth)
29366 ++level;
29367 break;
29368
29369 case CPP_RSHIFT:
29370 if (cxx_dialect == cxx98)
29371 /* C++0x views the `>>' operator as two `>' tokens, but
29372 C++98 does not. */
29373 break;
29374 else if (!nesting_depth && level-- == 0)
29375 {
29376 /* We've hit a `>>' where the first `>' closes the
29377 template argument list, and the second `>' is
29378 spurious. Just consume the `>>' and stop; we've
29379 already produced at least one error. */
29380 cp_lexer_consume_token (parser->lexer);
29381 return;
29382 }
29383 /* Fall through for C++0x, so we handle the second `>' in
29384 the `>>'. */
29385 gcc_fallthrough ();
29386
29387 case CPP_GREATER:
29388 if (!nesting_depth && level-- == 0)
29389 {
29390 /* We've reached the token we want, consume it and stop. */
29391 cp_lexer_consume_token (parser->lexer);
29392 return;
29393 }
29394 break;
29395
29396 case CPP_OPEN_PAREN:
29397 case CPP_OPEN_SQUARE:
29398 ++nesting_depth;
29399 break;
29400
29401 case CPP_CLOSE_PAREN:
29402 case CPP_CLOSE_SQUARE:
29403 if (nesting_depth-- == 0)
29404 return;
29405 break;
29406
29407 case CPP_EOF:
29408 case CPP_PRAGMA_EOL:
29409 case CPP_SEMICOLON:
29410 case CPP_OPEN_BRACE:
29411 case CPP_CLOSE_BRACE:
29412 /* The '>' was probably forgotten, don't look further. */
29413 return;
29414
29415 default:
29416 break;
29417 }
29418
29419 /* Consume this token. */
29420 cp_lexer_consume_token (parser->lexer);
29421 }
29422 }
29423
29424 /* If the next token is the indicated keyword, consume it. Otherwise,
29425 issue an error message indicating that TOKEN_DESC was expected.
29426
29427 Returns the token consumed, if the token had the appropriate type.
29428 Otherwise, returns NULL. */
29429
29430 static cp_token *
29431 cp_parser_require_keyword (cp_parser* parser,
29432 enum rid keyword,
29433 required_token token_desc)
29434 {
29435 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
29436
29437 if (token && token->keyword != keyword)
29438 {
29439 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
29440 UNKNOWN_LOCATION);
29441 return NULL;
29442 }
29443
29444 return token;
29445 }
29446
29447 /* Returns TRUE iff TOKEN is a token that can begin the body of a
29448 function-definition. */
29449
29450 static bool
29451 cp_parser_token_starts_function_definition_p (cp_token* token)
29452 {
29453 return (/* An ordinary function-body begins with an `{'. */
29454 token->type == CPP_OPEN_BRACE
29455 /* A ctor-initializer begins with a `:'. */
29456 || token->type == CPP_COLON
29457 /* A function-try-block begins with `try'. */
29458 || token->keyword == RID_TRY
29459 /* A function-transaction-block begins with `__transaction_atomic'
29460 or `__transaction_relaxed'. */
29461 || token->keyword == RID_TRANSACTION_ATOMIC
29462 || token->keyword == RID_TRANSACTION_RELAXED
29463 /* The named return value extension begins with `return'. */
29464 || token->keyword == RID_RETURN);
29465 }
29466
29467 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
29468 definition. */
29469
29470 static bool
29471 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
29472 {
29473 cp_token *token;
29474
29475 token = cp_lexer_peek_token (parser->lexer);
29476 return (token->type == CPP_OPEN_BRACE
29477 || (token->type == CPP_COLON
29478 && !parser->colon_doesnt_start_class_def_p));
29479 }
29480
29481 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
29482 C++0x) ending a template-argument. */
29483
29484 static bool
29485 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
29486 {
29487 cp_token *token;
29488
29489 token = cp_lexer_peek_token (parser->lexer);
29490 return (token->type == CPP_COMMA
29491 || token->type == CPP_GREATER
29492 || token->type == CPP_ELLIPSIS
29493 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
29494 }
29495
29496 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
29497 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
29498
29499 static bool
29500 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
29501 size_t n)
29502 {
29503 cp_token *token;
29504
29505 token = cp_lexer_peek_nth_token (parser->lexer, n);
29506 if (token->type == CPP_LESS)
29507 return true;
29508 /* Check for the sequence `<::' in the original code. It would be lexed as
29509 `[:', where `[' is a digraph, and there is no whitespace before
29510 `:'. */
29511 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
29512 {
29513 cp_token *token2;
29514 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
29515 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
29516 return true;
29517 }
29518 return false;
29519 }
29520
29521 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
29522 or none_type otherwise. */
29523
29524 static enum tag_types
29525 cp_parser_token_is_class_key (cp_token* token)
29526 {
29527 switch (token->keyword)
29528 {
29529 case RID_CLASS:
29530 return class_type;
29531 case RID_STRUCT:
29532 return record_type;
29533 case RID_UNION:
29534 return union_type;
29535
29536 default:
29537 return none_type;
29538 }
29539 }
29540
29541 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
29542 or none_type otherwise or if the token is null. */
29543
29544 static enum tag_types
29545 cp_parser_token_is_type_parameter_key (cp_token* token)
29546 {
29547 if (!token)
29548 return none_type;
29549
29550 switch (token->keyword)
29551 {
29552 case RID_CLASS:
29553 return class_type;
29554 case RID_TYPENAME:
29555 return typename_type;
29556
29557 default:
29558 return none_type;
29559 }
29560 }
29561
29562 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
29563
29564 static void
29565 cp_parser_check_class_key (enum tag_types class_key, tree type)
29566 {
29567 if (type == error_mark_node)
29568 return;
29569 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
29570 {
29571 if (permerror (input_location, "%qs tag used in naming %q#T",
29572 class_key == union_type ? "union"
29573 : class_key == record_type ? "struct" : "class",
29574 type))
29575 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
29576 "%q#T was previously declared here", type);
29577 }
29578 }
29579
29580 /* Issue an error message if DECL is redeclared with different
29581 access than its original declaration [class.access.spec/3].
29582 This applies to nested classes, nested class templates and
29583 enumerations [class.mem/1]. */
29584
29585 static void
29586 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
29587 {
29588 if (!decl
29589 || (!CLASS_TYPE_P (TREE_TYPE (decl))
29590 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
29591 return;
29592
29593 if ((TREE_PRIVATE (decl)
29594 != (current_access_specifier == access_private_node))
29595 || (TREE_PROTECTED (decl)
29596 != (current_access_specifier == access_protected_node)))
29597 error_at (location, "%qD redeclared with different access", decl);
29598 }
29599
29600 /* Look for the `template' keyword, as a syntactic disambiguator.
29601 Return TRUE iff it is present, in which case it will be
29602 consumed. */
29603
29604 static bool
29605 cp_parser_optional_template_keyword (cp_parser *parser)
29606 {
29607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
29608 {
29609 /* In C++98 the `template' keyword can only be used within templates;
29610 outside templates the parser can always figure out what is a
29611 template and what is not. In C++11, per the resolution of DR 468,
29612 `template' is allowed in cases where it is not strictly necessary. */
29613 if (!processing_template_decl
29614 && pedantic && cxx_dialect == cxx98)
29615 {
29616 cp_token *token = cp_lexer_peek_token (parser->lexer);
29617 pedwarn (token->location, OPT_Wpedantic,
29618 "in C++98 %<template%> (as a disambiguator) is only "
29619 "allowed within templates");
29620 /* If this part of the token stream is rescanned, the same
29621 error message would be generated. So, we purge the token
29622 from the stream. */
29623 cp_lexer_purge_token (parser->lexer);
29624 return false;
29625 }
29626 else
29627 {
29628 /* Consume the `template' keyword. */
29629 cp_lexer_consume_token (parser->lexer);
29630 return true;
29631 }
29632 }
29633 return false;
29634 }
29635
29636 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
29637 set PARSER->SCOPE, and perform other related actions. */
29638
29639 static void
29640 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
29641 {
29642 struct tree_check *check_value;
29643
29644 /* Get the stored value. */
29645 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
29646 /* Set the scope from the stored value. */
29647 parser->scope = saved_checks_value (check_value);
29648 parser->qualifying_scope = check_value->qualifying_scope;
29649 parser->object_scope = NULL_TREE;
29650 }
29651
29652 /* Consume tokens up through a non-nested END token. Returns TRUE if we
29653 encounter the end of a block before what we were looking for. */
29654
29655 static bool
29656 cp_parser_cache_group (cp_parser *parser,
29657 enum cpp_ttype end,
29658 unsigned depth)
29659 {
29660 while (true)
29661 {
29662 cp_token *token = cp_lexer_peek_token (parser->lexer);
29663
29664 /* Abort a parenthesized expression if we encounter a semicolon. */
29665 if ((end == CPP_CLOSE_PAREN || depth == 0)
29666 && token->type == CPP_SEMICOLON)
29667 return true;
29668 /* If we've reached the end of the file, stop. */
29669 if (token->type == CPP_EOF
29670 || (end != CPP_PRAGMA_EOL
29671 && token->type == CPP_PRAGMA_EOL))
29672 return true;
29673 if (token->type == CPP_CLOSE_BRACE && depth == 0)
29674 /* We've hit the end of an enclosing block, so there's been some
29675 kind of syntax error. */
29676 return true;
29677
29678 /* Consume the token. */
29679 cp_lexer_consume_token (parser->lexer);
29680 /* See if it starts a new group. */
29681 if (token->type == CPP_OPEN_BRACE)
29682 {
29683 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
29684 /* In theory this should probably check end == '}', but
29685 cp_parser_save_member_function_body needs it to exit
29686 after either '}' or ')' when called with ')'. */
29687 if (depth == 0)
29688 return false;
29689 }
29690 else if (token->type == CPP_OPEN_PAREN)
29691 {
29692 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
29693 if (depth == 0 && end == CPP_CLOSE_PAREN)
29694 return false;
29695 }
29696 else if (token->type == CPP_PRAGMA)
29697 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
29698 else if (token->type == end)
29699 return false;
29700 }
29701 }
29702
29703 /* Like above, for caching a default argument or NSDMI. Both of these are
29704 terminated by a non-nested comma, but it can be unclear whether or not a
29705 comma is nested in a template argument list unless we do more parsing.
29706 In order to handle this ambiguity, when we encounter a ',' after a '<'
29707 we try to parse what follows as a parameter-declaration-list (in the
29708 case of a default argument) or a member-declarator (in the case of an
29709 NSDMI). If that succeeds, then we stop caching. */
29710
29711 static tree
29712 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
29713 {
29714 unsigned depth = 0;
29715 int maybe_template_id = 0;
29716 cp_token *first_token;
29717 cp_token *token;
29718 tree default_argument;
29719
29720 /* Add tokens until we have processed the entire default
29721 argument. We add the range [first_token, token). */
29722 first_token = cp_lexer_peek_token (parser->lexer);
29723 if (first_token->type == CPP_OPEN_BRACE)
29724 {
29725 /* For list-initialization, this is straightforward. */
29726 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
29727 token = cp_lexer_peek_token (parser->lexer);
29728 }
29729 else while (true)
29730 {
29731 bool done = false;
29732
29733 /* Peek at the next token. */
29734 token = cp_lexer_peek_token (parser->lexer);
29735 /* What we do depends on what token we have. */
29736 switch (token->type)
29737 {
29738 /* In valid code, a default argument must be
29739 immediately followed by a `,' `)', or `...'. */
29740 case CPP_COMMA:
29741 if (depth == 0 && maybe_template_id)
29742 {
29743 /* If we've seen a '<', we might be in a
29744 template-argument-list. Until Core issue 325 is
29745 resolved, we don't know how this situation ought
29746 to be handled, so try to DTRT. We check whether
29747 what comes after the comma is a valid parameter
29748 declaration list. If it is, then the comma ends
29749 the default argument; otherwise the default
29750 argument continues. */
29751 bool error = false;
29752 cp_token *peek;
29753
29754 /* Set ITALP so cp_parser_parameter_declaration_list
29755 doesn't decide to commit to this parse. */
29756 bool saved_italp = parser->in_template_argument_list_p;
29757 parser->in_template_argument_list_p = true;
29758
29759 cp_parser_parse_tentatively (parser);
29760
29761 if (nsdmi)
29762 {
29763 /* Parse declarators until we reach a non-comma or
29764 somthing that cannot be an initializer.
29765 Just checking whether we're looking at a single
29766 declarator is insufficient. Consider:
29767 int var = tuple<T,U>::x;
29768 The template parameter 'U' looks exactly like a
29769 declarator. */
29770 do
29771 {
29772 int ctor_dtor_or_conv_p;
29773 cp_lexer_consume_token (parser->lexer);
29774 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29775 CP_PARSER_FLAGS_NONE,
29776 &ctor_dtor_or_conv_p,
29777 /*parenthesized_p=*/NULL,
29778 /*member_p=*/true,
29779 /*friend_p=*/false,
29780 /*static_p=*/false);
29781 peek = cp_lexer_peek_token (parser->lexer);
29782 if (cp_parser_error_occurred (parser))
29783 break;
29784 }
29785 while (peek->type == CPP_COMMA);
29786 /* If we met an '=' or ';' then the original comma
29787 was the end of the NSDMI. Otherwise assume
29788 we're still in the NSDMI. */
29789 error = (peek->type != CPP_EQ
29790 && peek->type != CPP_SEMICOLON);
29791 }
29792 else
29793 {
29794 cp_lexer_consume_token (parser->lexer);
29795 begin_scope (sk_function_parms, NULL_TREE);
29796 tree t = cp_parser_parameter_declaration_list
29797 (parser, CP_PARSER_FLAGS_NONE);
29798 if (t == error_mark_node)
29799 error = true;
29800 pop_bindings_and_leave_scope ();
29801 }
29802 if (!cp_parser_error_occurred (parser) && !error)
29803 done = true;
29804 cp_parser_abort_tentative_parse (parser);
29805
29806 parser->in_template_argument_list_p = saved_italp;
29807 break;
29808 }
29809 /* FALLTHRU */
29810 case CPP_CLOSE_PAREN:
29811 case CPP_ELLIPSIS:
29812 /* If we run into a non-nested `;', `}', or `]',
29813 then the code is invalid -- but the default
29814 argument is certainly over. */
29815 case CPP_SEMICOLON:
29816 case CPP_CLOSE_BRACE:
29817 case CPP_CLOSE_SQUARE:
29818 if (depth == 0
29819 /* Handle correctly int n = sizeof ... ( p ); */
29820 && token->type != CPP_ELLIPSIS)
29821 done = true;
29822 /* Update DEPTH, if necessary. */
29823 else if (token->type == CPP_CLOSE_PAREN
29824 || token->type == CPP_CLOSE_BRACE
29825 || token->type == CPP_CLOSE_SQUARE)
29826 --depth;
29827 break;
29828
29829 case CPP_OPEN_PAREN:
29830 case CPP_OPEN_SQUARE:
29831 case CPP_OPEN_BRACE:
29832 ++depth;
29833 break;
29834
29835 case CPP_LESS:
29836 if (depth == 0)
29837 /* This might be the comparison operator, or it might
29838 start a template argument list. */
29839 ++maybe_template_id;
29840 break;
29841
29842 case CPP_RSHIFT:
29843 if (cxx_dialect == cxx98)
29844 break;
29845 /* Fall through for C++0x, which treats the `>>'
29846 operator like two `>' tokens in certain
29847 cases. */
29848 gcc_fallthrough ();
29849
29850 case CPP_GREATER:
29851 if (depth == 0)
29852 {
29853 /* This might be an operator, or it might close a
29854 template argument list. But if a previous '<'
29855 started a template argument list, this will have
29856 closed it, so we can't be in one anymore. */
29857 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
29858 if (maybe_template_id < 0)
29859 maybe_template_id = 0;
29860 }
29861 break;
29862
29863 /* If we run out of tokens, issue an error message. */
29864 case CPP_EOF:
29865 case CPP_PRAGMA_EOL:
29866 error_at (token->location, "file ends in default argument");
29867 return error_mark_node;
29868
29869 case CPP_NAME:
29870 case CPP_SCOPE:
29871 /* In these cases, we should look for template-ids.
29872 For example, if the default argument is
29873 `X<int, double>()', we need to do name lookup to
29874 figure out whether or not `X' is a template; if
29875 so, the `,' does not end the default argument.
29876
29877 That is not yet done. */
29878 break;
29879
29880 default:
29881 break;
29882 }
29883
29884 /* If we've reached the end, stop. */
29885 if (done)
29886 break;
29887
29888 /* Add the token to the token block. */
29889 token = cp_lexer_consume_token (parser->lexer);
29890 }
29891
29892 /* Create a DEFAULT_ARG to represent the unparsed default
29893 argument. */
29894 default_argument = make_node (DEFAULT_ARG);
29895 DEFARG_TOKENS (default_argument)
29896 = cp_token_cache_new (first_token, token);
29897 DEFARG_INSTANTIATIONS (default_argument) = NULL;
29898
29899 return default_argument;
29900 }
29901
29902 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
29903
29904 location_t
29905 defarg_location (tree default_argument)
29906 {
29907 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
29908 location_t start = tokens->first->location;
29909 location_t end = tokens->last->location;
29910 return make_location (start, start, end);
29911 }
29912
29913 /* Begin parsing tentatively. We always save tokens while parsing
29914 tentatively so that if the tentative parsing fails we can restore the
29915 tokens. */
29916
29917 static void
29918 cp_parser_parse_tentatively (cp_parser* parser)
29919 {
29920 /* Enter a new parsing context. */
29921 parser->context = cp_parser_context_new (parser->context);
29922 /* Begin saving tokens. */
29923 cp_lexer_save_tokens (parser->lexer);
29924 /* In order to avoid repetitive access control error messages,
29925 access checks are queued up until we are no longer parsing
29926 tentatively. */
29927 push_deferring_access_checks (dk_deferred);
29928 }
29929
29930 /* Commit to the currently active tentative parse. */
29931
29932 static void
29933 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29934 {
29935 cp_parser_context *context;
29936 cp_lexer *lexer;
29937
29938 /* Mark all of the levels as committed. */
29939 lexer = parser->lexer;
29940 for (context = parser->context; context->next; context = context->next)
29941 {
29942 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29943 break;
29944 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29945 while (!cp_lexer_saving_tokens (lexer))
29946 lexer = lexer->next;
29947 cp_lexer_commit_tokens (lexer);
29948 }
29949 }
29950
29951 /* Commit to the topmost currently active tentative parse.
29952
29953 Note that this function shouldn't be called when there are
29954 irreversible side-effects while in a tentative state. For
29955 example, we shouldn't create a permanent entry in the symbol
29956 table, or issue an error message that might not apply if the
29957 tentative parse is aborted. */
29958
29959 static void
29960 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29961 {
29962 cp_parser_context *context = parser->context;
29963 cp_lexer *lexer = parser->lexer;
29964
29965 if (context)
29966 {
29967 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29968 return;
29969 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29970
29971 while (!cp_lexer_saving_tokens (lexer))
29972 lexer = lexer->next;
29973 cp_lexer_commit_tokens (lexer);
29974 }
29975 }
29976
29977 /* Abort the currently active tentative parse. All consumed tokens
29978 will be rolled back, and no diagnostics will be issued. */
29979
29980 static void
29981 cp_parser_abort_tentative_parse (cp_parser* parser)
29982 {
29983 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29984 || errorcount > 0);
29985 cp_parser_simulate_error (parser);
29986 /* Now, pretend that we want to see if the construct was
29987 successfully parsed. */
29988 cp_parser_parse_definitely (parser);
29989 }
29990
29991 /* Stop parsing tentatively. If a parse error has occurred, restore the
29992 token stream. Otherwise, commit to the tokens we have consumed.
29993 Returns true if no error occurred; false otherwise. */
29994
29995 static bool
29996 cp_parser_parse_definitely (cp_parser* parser)
29997 {
29998 bool error_occurred;
29999 cp_parser_context *context;
30000
30001 /* Remember whether or not an error occurred, since we are about to
30002 destroy that information. */
30003 error_occurred = cp_parser_error_occurred (parser);
30004 /* Remove the topmost context from the stack. */
30005 context = parser->context;
30006 parser->context = context->next;
30007 /* If no parse errors occurred, commit to the tentative parse. */
30008 if (!error_occurred)
30009 {
30010 /* Commit to the tokens read tentatively, unless that was
30011 already done. */
30012 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
30013 cp_lexer_commit_tokens (parser->lexer);
30014
30015 pop_to_parent_deferring_access_checks ();
30016 }
30017 /* Otherwise, if errors occurred, roll back our state so that things
30018 are just as they were before we began the tentative parse. */
30019 else
30020 {
30021 cp_lexer_rollback_tokens (parser->lexer);
30022 pop_deferring_access_checks ();
30023 }
30024 /* Add the context to the front of the free list. */
30025 context->next = cp_parser_context_free_list;
30026 cp_parser_context_free_list = context;
30027
30028 return !error_occurred;
30029 }
30030
30031 /* Returns true if we are parsing tentatively and are not committed to
30032 this tentative parse. */
30033
30034 static bool
30035 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
30036 {
30037 return (cp_parser_parsing_tentatively (parser)
30038 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
30039 }
30040
30041 /* Returns nonzero iff an error has occurred during the most recent
30042 tentative parse. */
30043
30044 static bool
30045 cp_parser_error_occurred (cp_parser* parser)
30046 {
30047 return (cp_parser_parsing_tentatively (parser)
30048 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
30049 }
30050
30051 /* Returns nonzero if GNU extensions are allowed. */
30052
30053 static bool
30054 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
30055 {
30056 return parser->allow_gnu_extensions_p;
30057 }
30058 \f
30059 /* Objective-C++ Productions */
30060
30061
30062 /* Parse an Objective-C expression, which feeds into a primary-expression
30063 above.
30064
30065 objc-expression:
30066 objc-message-expression
30067 objc-string-literal
30068 objc-encode-expression
30069 objc-protocol-expression
30070 objc-selector-expression
30071
30072 Returns a tree representation of the expression. */
30073
30074 static cp_expr
30075 cp_parser_objc_expression (cp_parser* parser)
30076 {
30077 /* Try to figure out what kind of declaration is present. */
30078 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30079
30080 switch (kwd->type)
30081 {
30082 case CPP_OPEN_SQUARE:
30083 return cp_parser_objc_message_expression (parser);
30084
30085 case CPP_OBJC_STRING:
30086 kwd = cp_lexer_consume_token (parser->lexer);
30087 return objc_build_string_object (kwd->u.value);
30088
30089 case CPP_KEYWORD:
30090 switch (kwd->keyword)
30091 {
30092 case RID_AT_ENCODE:
30093 return cp_parser_objc_encode_expression (parser);
30094
30095 case RID_AT_PROTOCOL:
30096 return cp_parser_objc_protocol_expression (parser);
30097
30098 case RID_AT_SELECTOR:
30099 return cp_parser_objc_selector_expression (parser);
30100
30101 default:
30102 break;
30103 }
30104 /* FALLTHRU */
30105 default:
30106 error_at (kwd->location,
30107 "misplaced %<@%D%> Objective-C++ construct",
30108 kwd->u.value);
30109 cp_parser_skip_to_end_of_block_or_statement (parser);
30110 }
30111
30112 return error_mark_node;
30113 }
30114
30115 /* Parse an Objective-C message expression.
30116
30117 objc-message-expression:
30118 [ objc-message-receiver objc-message-args ]
30119
30120 Returns a representation of an Objective-C message. */
30121
30122 static tree
30123 cp_parser_objc_message_expression (cp_parser* parser)
30124 {
30125 tree receiver, messageargs;
30126
30127 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30128 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
30129 receiver = cp_parser_objc_message_receiver (parser);
30130 messageargs = cp_parser_objc_message_args (parser);
30131 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
30132 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
30133
30134 tree result = objc_build_message_expr (receiver, messageargs);
30135
30136 /* Construct a location e.g.
30137 [self func1:5]
30138 ^~~~~~~~~~~~~~
30139 ranging from the '[' to the ']', with the caret at the start. */
30140 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
30141 protected_set_expr_location (result, combined_loc);
30142
30143 return result;
30144 }
30145
30146 /* Parse an objc-message-receiver.
30147
30148 objc-message-receiver:
30149 expression
30150 simple-type-specifier
30151
30152 Returns a representation of the type or expression. */
30153
30154 static tree
30155 cp_parser_objc_message_receiver (cp_parser* parser)
30156 {
30157 tree rcv;
30158
30159 /* An Objective-C message receiver may be either (1) a type
30160 or (2) an expression. */
30161 cp_parser_parse_tentatively (parser);
30162 rcv = cp_parser_expression (parser);
30163
30164 /* If that worked out, fine. */
30165 if (cp_parser_parse_definitely (parser))
30166 return rcv;
30167
30168 cp_parser_parse_tentatively (parser);
30169 rcv = cp_parser_simple_type_specifier (parser,
30170 /*decl_specs=*/NULL,
30171 CP_PARSER_FLAGS_NONE);
30172
30173 if (cp_parser_parse_definitely (parser))
30174 return objc_get_class_reference (rcv);
30175
30176 cp_parser_error (parser, "objective-c++ message receiver expected");
30177 return error_mark_node;
30178 }
30179
30180 /* Parse the arguments and selectors comprising an Objective-C message.
30181
30182 objc-message-args:
30183 objc-selector
30184 objc-selector-args
30185 objc-selector-args , objc-comma-args
30186
30187 objc-selector-args:
30188 objc-selector [opt] : assignment-expression
30189 objc-selector-args objc-selector [opt] : assignment-expression
30190
30191 objc-comma-args:
30192 assignment-expression
30193 objc-comma-args , assignment-expression
30194
30195 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
30196 selector arguments and TREE_VALUE containing a list of comma
30197 arguments. */
30198
30199 static tree
30200 cp_parser_objc_message_args (cp_parser* parser)
30201 {
30202 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
30203 bool maybe_unary_selector_p = true;
30204 cp_token *token = cp_lexer_peek_token (parser->lexer);
30205
30206 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30207 {
30208 tree selector = NULL_TREE, arg;
30209
30210 if (token->type != CPP_COLON)
30211 selector = cp_parser_objc_selector (parser);
30212
30213 /* Detect if we have a unary selector. */
30214 if (maybe_unary_selector_p
30215 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30216 return build_tree_list (selector, NULL_TREE);
30217
30218 maybe_unary_selector_p = false;
30219 cp_parser_require (parser, CPP_COLON, RT_COLON);
30220 arg = cp_parser_assignment_expression (parser);
30221
30222 sel_args
30223 = chainon (sel_args,
30224 build_tree_list (selector, arg));
30225
30226 token = cp_lexer_peek_token (parser->lexer);
30227 }
30228
30229 /* Handle non-selector arguments, if any. */
30230 while (token->type == CPP_COMMA)
30231 {
30232 tree arg;
30233
30234 cp_lexer_consume_token (parser->lexer);
30235 arg = cp_parser_assignment_expression (parser);
30236
30237 addl_args
30238 = chainon (addl_args,
30239 build_tree_list (NULL_TREE, arg));
30240
30241 token = cp_lexer_peek_token (parser->lexer);
30242 }
30243
30244 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
30245 {
30246 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
30247 return build_tree_list (error_mark_node, error_mark_node);
30248 }
30249
30250 return build_tree_list (sel_args, addl_args);
30251 }
30252
30253 /* Parse an Objective-C encode expression.
30254
30255 objc-encode-expression:
30256 @encode objc-typename
30257
30258 Returns an encoded representation of the type argument. */
30259
30260 static cp_expr
30261 cp_parser_objc_encode_expression (cp_parser* parser)
30262 {
30263 tree type;
30264 cp_token *token;
30265 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30266
30267 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
30268 matching_parens parens;
30269 parens.require_open (parser);
30270 token = cp_lexer_peek_token (parser->lexer);
30271 type = complete_type (cp_parser_type_id (parser));
30272 parens.require_close (parser);
30273
30274 if (!type)
30275 {
30276 error_at (token->location,
30277 "%<@encode%> must specify a type as an argument");
30278 return error_mark_node;
30279 }
30280
30281 /* This happens if we find @encode(T) (where T is a template
30282 typename or something dependent on a template typename) when
30283 parsing a template. In that case, we can't compile it
30284 immediately, but we rather create an AT_ENCODE_EXPR which will
30285 need to be instantiated when the template is used.
30286 */
30287 if (dependent_type_p (type))
30288 {
30289 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
30290 TREE_READONLY (value) = 1;
30291 return value;
30292 }
30293
30294
30295 /* Build a location of the form:
30296 @encode(int)
30297 ^~~~~~~~~~~~
30298 with caret==start at the @ token, finishing at the close paren. */
30299 location_t combined_loc
30300 = make_location (start_loc, start_loc,
30301 cp_lexer_previous_token (parser->lexer)->location);
30302
30303 return cp_expr (objc_build_encode_expr (type), combined_loc);
30304 }
30305
30306 /* Parse an Objective-C @defs expression. */
30307
30308 static tree
30309 cp_parser_objc_defs_expression (cp_parser *parser)
30310 {
30311 tree name;
30312
30313 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
30314 matching_parens parens;
30315 parens.require_open (parser);
30316 name = cp_parser_identifier (parser);
30317 parens.require_close (parser);
30318
30319 return objc_get_class_ivars (name);
30320 }
30321
30322 /* Parse an Objective-C protocol expression.
30323
30324 objc-protocol-expression:
30325 @protocol ( identifier )
30326
30327 Returns a representation of the protocol expression. */
30328
30329 static tree
30330 cp_parser_objc_protocol_expression (cp_parser* parser)
30331 {
30332 tree proto;
30333 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
30334
30335 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30336 matching_parens parens;
30337 parens.require_open (parser);
30338 proto = cp_parser_identifier (parser);
30339 parens.require_close (parser);
30340
30341 /* Build a location of the form:
30342 @protocol(prot)
30343 ^~~~~~~~~~~~~~~
30344 with caret==start at the @ token, finishing at the close paren. */
30345 location_t combined_loc
30346 = make_location (start_loc, start_loc,
30347 cp_lexer_previous_token (parser->lexer)->location);
30348 tree result = objc_build_protocol_expr (proto);
30349 protected_set_expr_location (result, combined_loc);
30350 return result;
30351 }
30352
30353 /* Parse an Objective-C selector expression.
30354
30355 objc-selector-expression:
30356 @selector ( objc-method-signature )
30357
30358 objc-method-signature:
30359 objc-selector
30360 objc-selector-seq
30361
30362 objc-selector-seq:
30363 objc-selector :
30364 objc-selector-seq objc-selector :
30365
30366 Returns a representation of the method selector. */
30367
30368 static tree
30369 cp_parser_objc_selector_expression (cp_parser* parser)
30370 {
30371 tree sel_seq = NULL_TREE;
30372 bool maybe_unary_selector_p = true;
30373 cp_token *token;
30374 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30375
30376 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
30377 matching_parens parens;
30378 parens.require_open (parser);
30379 token = cp_lexer_peek_token (parser->lexer);
30380
30381 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
30382 || token->type == CPP_SCOPE)
30383 {
30384 tree selector = NULL_TREE;
30385
30386 if (token->type != CPP_COLON
30387 || token->type == CPP_SCOPE)
30388 selector = cp_parser_objc_selector (parser);
30389
30390 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
30391 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
30392 {
30393 /* Detect if we have a unary selector. */
30394 if (maybe_unary_selector_p)
30395 {
30396 sel_seq = selector;
30397 goto finish_selector;
30398 }
30399 else
30400 {
30401 cp_parser_error (parser, "expected %<:%>");
30402 }
30403 }
30404 maybe_unary_selector_p = false;
30405 token = cp_lexer_consume_token (parser->lexer);
30406
30407 if (token->type == CPP_SCOPE)
30408 {
30409 sel_seq
30410 = chainon (sel_seq,
30411 build_tree_list (selector, NULL_TREE));
30412 sel_seq
30413 = chainon (sel_seq,
30414 build_tree_list (NULL_TREE, NULL_TREE));
30415 }
30416 else
30417 sel_seq
30418 = chainon (sel_seq,
30419 build_tree_list (selector, NULL_TREE));
30420
30421 token = cp_lexer_peek_token (parser->lexer);
30422 }
30423
30424 finish_selector:
30425 parens.require_close (parser);
30426
30427
30428 /* Build a location of the form:
30429 @selector(func)
30430 ^~~~~~~~~~~~~~~
30431 with caret==start at the @ token, finishing at the close paren. */
30432 location_t combined_loc
30433 = make_location (loc, loc,
30434 cp_lexer_previous_token (parser->lexer)->location);
30435 tree result = objc_build_selector_expr (combined_loc, sel_seq);
30436 /* TODO: objc_build_selector_expr doesn't always honor the location. */
30437 protected_set_expr_location (result, combined_loc);
30438 return result;
30439 }
30440
30441 /* Parse a list of identifiers.
30442
30443 objc-identifier-list:
30444 identifier
30445 objc-identifier-list , identifier
30446
30447 Returns a TREE_LIST of identifier nodes. */
30448
30449 static tree
30450 cp_parser_objc_identifier_list (cp_parser* parser)
30451 {
30452 tree identifier;
30453 tree list;
30454 cp_token *sep;
30455
30456 identifier = cp_parser_identifier (parser);
30457 if (identifier == error_mark_node)
30458 return error_mark_node;
30459
30460 list = build_tree_list (NULL_TREE, identifier);
30461 sep = cp_lexer_peek_token (parser->lexer);
30462
30463 while (sep->type == CPP_COMMA)
30464 {
30465 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30466 identifier = cp_parser_identifier (parser);
30467 if (identifier == error_mark_node)
30468 return list;
30469
30470 list = chainon (list, build_tree_list (NULL_TREE,
30471 identifier));
30472 sep = cp_lexer_peek_token (parser->lexer);
30473 }
30474
30475 return list;
30476 }
30477
30478 /* Parse an Objective-C alias declaration.
30479
30480 objc-alias-declaration:
30481 @compatibility_alias identifier identifier ;
30482
30483 This function registers the alias mapping with the Objective-C front end.
30484 It returns nothing. */
30485
30486 static void
30487 cp_parser_objc_alias_declaration (cp_parser* parser)
30488 {
30489 tree alias, orig;
30490
30491 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
30492 alias = cp_parser_identifier (parser);
30493 orig = cp_parser_identifier (parser);
30494 objc_declare_alias (alias, orig);
30495 cp_parser_consume_semicolon_at_end_of_statement (parser);
30496 }
30497
30498 /* Parse an Objective-C class forward-declaration.
30499
30500 objc-class-declaration:
30501 @class objc-identifier-list ;
30502
30503 The function registers the forward declarations with the Objective-C
30504 front end. It returns nothing. */
30505
30506 static void
30507 cp_parser_objc_class_declaration (cp_parser* parser)
30508 {
30509 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
30510 while (true)
30511 {
30512 tree id;
30513
30514 id = cp_parser_identifier (parser);
30515 if (id == error_mark_node)
30516 break;
30517
30518 objc_declare_class (id);
30519
30520 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30521 cp_lexer_consume_token (parser->lexer);
30522 else
30523 break;
30524 }
30525 cp_parser_consume_semicolon_at_end_of_statement (parser);
30526 }
30527
30528 /* Parse a list of Objective-C protocol references.
30529
30530 objc-protocol-refs-opt:
30531 objc-protocol-refs [opt]
30532
30533 objc-protocol-refs:
30534 < objc-identifier-list >
30535
30536 Returns a TREE_LIST of identifiers, if any. */
30537
30538 static tree
30539 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
30540 {
30541 tree protorefs = NULL_TREE;
30542
30543 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
30544 {
30545 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
30546 protorefs = cp_parser_objc_identifier_list (parser);
30547 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
30548 }
30549
30550 return protorefs;
30551 }
30552
30553 /* Parse a Objective-C visibility specification. */
30554
30555 static void
30556 cp_parser_objc_visibility_spec (cp_parser* parser)
30557 {
30558 cp_token *vis = cp_lexer_peek_token (parser->lexer);
30559
30560 switch (vis->keyword)
30561 {
30562 case RID_AT_PRIVATE:
30563 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
30564 break;
30565 case RID_AT_PROTECTED:
30566 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
30567 break;
30568 case RID_AT_PUBLIC:
30569 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
30570 break;
30571 case RID_AT_PACKAGE:
30572 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
30573 break;
30574 default:
30575 return;
30576 }
30577
30578 /* Eat '@private'/'@protected'/'@public'. */
30579 cp_lexer_consume_token (parser->lexer);
30580 }
30581
30582 /* Parse an Objective-C method type. Return 'true' if it is a class
30583 (+) method, and 'false' if it is an instance (-) method. */
30584
30585 static inline bool
30586 cp_parser_objc_method_type (cp_parser* parser)
30587 {
30588 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
30589 return true;
30590 else
30591 return false;
30592 }
30593
30594 /* Parse an Objective-C protocol qualifier. */
30595
30596 static tree
30597 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
30598 {
30599 tree quals = NULL_TREE, node;
30600 cp_token *token = cp_lexer_peek_token (parser->lexer);
30601
30602 node = token->u.value;
30603
30604 while (node && identifier_p (node)
30605 && (node == ridpointers [(int) RID_IN]
30606 || node == ridpointers [(int) RID_OUT]
30607 || node == ridpointers [(int) RID_INOUT]
30608 || node == ridpointers [(int) RID_BYCOPY]
30609 || node == ridpointers [(int) RID_BYREF]
30610 || node == ridpointers [(int) RID_ONEWAY]))
30611 {
30612 quals = tree_cons (NULL_TREE, node, quals);
30613 cp_lexer_consume_token (parser->lexer);
30614 token = cp_lexer_peek_token (parser->lexer);
30615 node = token->u.value;
30616 }
30617
30618 return quals;
30619 }
30620
30621 /* Parse an Objective-C typename. */
30622
30623 static tree
30624 cp_parser_objc_typename (cp_parser* parser)
30625 {
30626 tree type_name = NULL_TREE;
30627
30628 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30629 {
30630 tree proto_quals, cp_type = NULL_TREE;
30631
30632 matching_parens parens;
30633 parens.consume_open (parser); /* Eat '('. */
30634 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
30635
30636 /* An ObjC type name may consist of just protocol qualifiers, in which
30637 case the type shall default to 'id'. */
30638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
30639 {
30640 cp_type = cp_parser_type_id (parser);
30641
30642 /* If the type could not be parsed, an error has already
30643 been produced. For error recovery, behave as if it had
30644 not been specified, which will use the default type
30645 'id'. */
30646 if (cp_type == error_mark_node)
30647 {
30648 cp_type = NULL_TREE;
30649 /* We need to skip to the closing parenthesis as
30650 cp_parser_type_id() does not seem to do it for
30651 us. */
30652 cp_parser_skip_to_closing_parenthesis (parser,
30653 /*recovering=*/true,
30654 /*or_comma=*/false,
30655 /*consume_paren=*/false);
30656 }
30657 }
30658
30659 parens.require_close (parser);
30660 type_name = build_tree_list (proto_quals, cp_type);
30661 }
30662
30663 return type_name;
30664 }
30665
30666 /* Check to see if TYPE refers to an Objective-C selector name. */
30667
30668 static bool
30669 cp_parser_objc_selector_p (enum cpp_ttype type)
30670 {
30671 return (type == CPP_NAME || type == CPP_KEYWORD
30672 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
30673 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
30674 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
30675 || type == CPP_XOR || type == CPP_XOR_EQ);
30676 }
30677
30678 /* Parse an Objective-C selector. */
30679
30680 static tree
30681 cp_parser_objc_selector (cp_parser* parser)
30682 {
30683 cp_token *token = cp_lexer_consume_token (parser->lexer);
30684
30685 if (!cp_parser_objc_selector_p (token->type))
30686 {
30687 error_at (token->location, "invalid Objective-C++ selector name");
30688 return error_mark_node;
30689 }
30690
30691 /* C++ operator names are allowed to appear in ObjC selectors. */
30692 switch (token->type)
30693 {
30694 case CPP_AND_AND: return get_identifier ("and");
30695 case CPP_AND_EQ: return get_identifier ("and_eq");
30696 case CPP_AND: return get_identifier ("bitand");
30697 case CPP_OR: return get_identifier ("bitor");
30698 case CPP_COMPL: return get_identifier ("compl");
30699 case CPP_NOT: return get_identifier ("not");
30700 case CPP_NOT_EQ: return get_identifier ("not_eq");
30701 case CPP_OR_OR: return get_identifier ("or");
30702 case CPP_OR_EQ: return get_identifier ("or_eq");
30703 case CPP_XOR: return get_identifier ("xor");
30704 case CPP_XOR_EQ: return get_identifier ("xor_eq");
30705 default: return token->u.value;
30706 }
30707 }
30708
30709 /* Parse an Objective-C params list. */
30710
30711 static tree
30712 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
30713 {
30714 tree params = NULL_TREE;
30715 bool maybe_unary_selector_p = true;
30716 cp_token *token = cp_lexer_peek_token (parser->lexer);
30717
30718 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
30719 {
30720 tree selector = NULL_TREE, type_name, identifier;
30721 tree parm_attr = NULL_TREE;
30722
30723 if (token->keyword == RID_ATTRIBUTE)
30724 break;
30725
30726 if (token->type != CPP_COLON)
30727 selector = cp_parser_objc_selector (parser);
30728
30729 /* Detect if we have a unary selector. */
30730 if (maybe_unary_selector_p
30731 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30732 {
30733 params = selector; /* Might be followed by attributes. */
30734 break;
30735 }
30736
30737 maybe_unary_selector_p = false;
30738 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30739 {
30740 /* Something went quite wrong. There should be a colon
30741 here, but there is not. Stop parsing parameters. */
30742 break;
30743 }
30744 type_name = cp_parser_objc_typename (parser);
30745 /* New ObjC allows attributes on parameters too. */
30746 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
30747 parm_attr = cp_parser_attributes_opt (parser);
30748 identifier = cp_parser_identifier (parser);
30749
30750 params
30751 = chainon (params,
30752 objc_build_keyword_decl (selector,
30753 type_name,
30754 identifier,
30755 parm_attr));
30756
30757 token = cp_lexer_peek_token (parser->lexer);
30758 }
30759
30760 if (params == NULL_TREE)
30761 {
30762 cp_parser_error (parser, "objective-c++ method declaration is expected");
30763 return error_mark_node;
30764 }
30765
30766 /* We allow tail attributes for the method. */
30767 if (token->keyword == RID_ATTRIBUTE)
30768 {
30769 *attributes = cp_parser_attributes_opt (parser);
30770 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30771 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30772 return params;
30773 cp_parser_error (parser,
30774 "method attributes must be specified at the end");
30775 return error_mark_node;
30776 }
30777
30778 if (params == NULL_TREE)
30779 {
30780 cp_parser_error (parser, "objective-c++ method declaration is expected");
30781 return error_mark_node;
30782 }
30783 return params;
30784 }
30785
30786 /* Parse the non-keyword Objective-C params. */
30787
30788 static tree
30789 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
30790 tree* attributes)
30791 {
30792 tree params = make_node (TREE_LIST);
30793 cp_token *token = cp_lexer_peek_token (parser->lexer);
30794 *ellipsisp = false; /* Initially, assume no ellipsis. */
30795
30796 while (token->type == CPP_COMMA)
30797 {
30798 cp_parameter_declarator *parmdecl;
30799 tree parm;
30800
30801 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30802 token = cp_lexer_peek_token (parser->lexer);
30803
30804 if (token->type == CPP_ELLIPSIS)
30805 {
30806 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
30807 *ellipsisp = true;
30808 token = cp_lexer_peek_token (parser->lexer);
30809 break;
30810 }
30811
30812 /* TODO: parse attributes for tail parameters. */
30813 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
30814 false, NULL);
30815 parm = grokdeclarator (parmdecl->declarator,
30816 &parmdecl->decl_specifiers,
30817 PARM, /*initialized=*/0,
30818 /*attrlist=*/NULL);
30819
30820 chainon (params, build_tree_list (NULL_TREE, parm));
30821 token = cp_lexer_peek_token (parser->lexer);
30822 }
30823
30824 /* We allow tail attributes for the method. */
30825 if (token->keyword == RID_ATTRIBUTE)
30826 {
30827 if (*attributes == NULL_TREE)
30828 {
30829 *attributes = cp_parser_attributes_opt (parser);
30830 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
30831 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30832 return params;
30833 }
30834 else
30835 /* We have an error, but parse the attributes, so that we can
30836 carry on. */
30837 *attributes = cp_parser_attributes_opt (parser);
30838
30839 cp_parser_error (parser,
30840 "method attributes must be specified at the end");
30841 return error_mark_node;
30842 }
30843
30844 return params;
30845 }
30846
30847 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
30848
30849 static void
30850 cp_parser_objc_interstitial_code (cp_parser* parser)
30851 {
30852 cp_token *token = cp_lexer_peek_token (parser->lexer);
30853
30854 /* If the next token is `extern' and the following token is a string
30855 literal, then we have a linkage specification. */
30856 if (token->keyword == RID_EXTERN
30857 && cp_parser_is_pure_string_literal
30858 (cp_lexer_peek_nth_token (parser->lexer, 2)))
30859 cp_parser_linkage_specification (parser);
30860 /* Handle #pragma, if any. */
30861 else if (token->type == CPP_PRAGMA)
30862 cp_parser_pragma (parser, pragma_objc_icode, NULL);
30863 /* Allow stray semicolons. */
30864 else if (token->type == CPP_SEMICOLON)
30865 cp_lexer_consume_token (parser->lexer);
30866 /* Mark methods as optional or required, when building protocols. */
30867 else if (token->keyword == RID_AT_OPTIONAL)
30868 {
30869 cp_lexer_consume_token (parser->lexer);
30870 objc_set_method_opt (true);
30871 }
30872 else if (token->keyword == RID_AT_REQUIRED)
30873 {
30874 cp_lexer_consume_token (parser->lexer);
30875 objc_set_method_opt (false);
30876 }
30877 else if (token->keyword == RID_NAMESPACE)
30878 cp_parser_namespace_definition (parser);
30879 /* Other stray characters must generate errors. */
30880 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
30881 {
30882 cp_lexer_consume_token (parser->lexer);
30883 error ("stray %qs between Objective-C++ methods",
30884 token->type == CPP_OPEN_BRACE ? "{" : "}");
30885 }
30886 /* Finally, try to parse a block-declaration, or a function-definition. */
30887 else
30888 cp_parser_block_declaration (parser, /*statement_p=*/false);
30889 }
30890
30891 /* Parse a method signature. */
30892
30893 static tree
30894 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
30895 {
30896 tree rettype, kwdparms, optparms;
30897 bool ellipsis = false;
30898 bool is_class_method;
30899
30900 is_class_method = cp_parser_objc_method_type (parser);
30901 rettype = cp_parser_objc_typename (parser);
30902 *attributes = NULL_TREE;
30903 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
30904 if (kwdparms == error_mark_node)
30905 return error_mark_node;
30906 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
30907 if (optparms == error_mark_node)
30908 return error_mark_node;
30909
30910 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
30911 }
30912
30913 static bool
30914 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
30915 {
30916 tree tattr;
30917 cp_lexer_save_tokens (parser->lexer);
30918 tattr = cp_parser_attributes_opt (parser);
30919 gcc_assert (tattr) ;
30920
30921 /* If the attributes are followed by a method introducer, this is not allowed.
30922 Dump the attributes and flag the situation. */
30923 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30924 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30925 return true;
30926
30927 /* Otherwise, the attributes introduce some interstitial code, possibly so
30928 rewind to allow that check. */
30929 cp_lexer_rollback_tokens (parser->lexer);
30930 return false;
30931 }
30932
30933 /* Parse an Objective-C method prototype list. */
30934
30935 static void
30936 cp_parser_objc_method_prototype_list (cp_parser* parser)
30937 {
30938 cp_token *token = cp_lexer_peek_token (parser->lexer);
30939
30940 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30941 {
30942 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30943 {
30944 tree attributes, sig;
30945 bool is_class_method;
30946 if (token->type == CPP_PLUS)
30947 is_class_method = true;
30948 else
30949 is_class_method = false;
30950 sig = cp_parser_objc_method_signature (parser, &attributes);
30951 if (sig == error_mark_node)
30952 {
30953 cp_parser_skip_to_end_of_block_or_statement (parser);
30954 token = cp_lexer_peek_token (parser->lexer);
30955 continue;
30956 }
30957 objc_add_method_declaration (is_class_method, sig, attributes);
30958 cp_parser_consume_semicolon_at_end_of_statement (parser);
30959 }
30960 else if (token->keyword == RID_AT_PROPERTY)
30961 cp_parser_objc_at_property_declaration (parser);
30962 else if (token->keyword == RID_ATTRIBUTE
30963 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30964 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30965 OPT_Wattributes,
30966 "prefix attributes are ignored for methods");
30967 else
30968 /* Allow for interspersed non-ObjC++ code. */
30969 cp_parser_objc_interstitial_code (parser);
30970
30971 token = cp_lexer_peek_token (parser->lexer);
30972 }
30973
30974 if (token->type != CPP_EOF)
30975 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30976 else
30977 cp_parser_error (parser, "expected %<@end%>");
30978
30979 objc_finish_interface ();
30980 }
30981
30982 /* Parse an Objective-C method definition list. */
30983
30984 static void
30985 cp_parser_objc_method_definition_list (cp_parser* parser)
30986 {
30987 cp_token *token = cp_lexer_peek_token (parser->lexer);
30988
30989 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30990 {
30991 tree meth;
30992
30993 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30994 {
30995 cp_token *ptk;
30996 tree sig, attribute;
30997 bool is_class_method;
30998 if (token->type == CPP_PLUS)
30999 is_class_method = true;
31000 else
31001 is_class_method = false;
31002 push_deferring_access_checks (dk_deferred);
31003 sig = cp_parser_objc_method_signature (parser, &attribute);
31004 if (sig == error_mark_node)
31005 {
31006 cp_parser_skip_to_end_of_block_or_statement (parser);
31007 token = cp_lexer_peek_token (parser->lexer);
31008 continue;
31009 }
31010 objc_start_method_definition (is_class_method, sig, attribute,
31011 NULL_TREE);
31012
31013 /* For historical reasons, we accept an optional semicolon. */
31014 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31015 cp_lexer_consume_token (parser->lexer);
31016
31017 ptk = cp_lexer_peek_token (parser->lexer);
31018 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
31019 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
31020 {
31021 perform_deferred_access_checks (tf_warning_or_error);
31022 stop_deferring_access_checks ();
31023 meth = cp_parser_function_definition_after_declarator (parser,
31024 false);
31025 pop_deferring_access_checks ();
31026 objc_finish_method_definition (meth);
31027 }
31028 }
31029 /* The following case will be removed once @synthesize is
31030 completely implemented. */
31031 else if (token->keyword == RID_AT_PROPERTY)
31032 cp_parser_objc_at_property_declaration (parser);
31033 else if (token->keyword == RID_AT_SYNTHESIZE)
31034 cp_parser_objc_at_synthesize_declaration (parser);
31035 else if (token->keyword == RID_AT_DYNAMIC)
31036 cp_parser_objc_at_dynamic_declaration (parser);
31037 else if (token->keyword == RID_ATTRIBUTE
31038 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
31039 warning_at (token->location, OPT_Wattributes,
31040 "prefix attributes are ignored for methods");
31041 else
31042 /* Allow for interspersed non-ObjC++ code. */
31043 cp_parser_objc_interstitial_code (parser);
31044
31045 token = cp_lexer_peek_token (parser->lexer);
31046 }
31047
31048 if (token->type != CPP_EOF)
31049 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31050 else
31051 cp_parser_error (parser, "expected %<@end%>");
31052
31053 objc_finish_implementation ();
31054 }
31055
31056 /* Parse Objective-C ivars. */
31057
31058 static void
31059 cp_parser_objc_class_ivars (cp_parser* parser)
31060 {
31061 cp_token *token = cp_lexer_peek_token (parser->lexer);
31062
31063 if (token->type != CPP_OPEN_BRACE)
31064 return; /* No ivars specified. */
31065
31066 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
31067 token = cp_lexer_peek_token (parser->lexer);
31068
31069 while (token->type != CPP_CLOSE_BRACE
31070 && token->keyword != RID_AT_END && token->type != CPP_EOF)
31071 {
31072 cp_decl_specifier_seq declspecs;
31073 int decl_class_or_enum_p;
31074 tree prefix_attributes;
31075
31076 cp_parser_objc_visibility_spec (parser);
31077
31078 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
31079 break;
31080
31081 cp_parser_decl_specifier_seq (parser,
31082 CP_PARSER_FLAGS_OPTIONAL,
31083 &declspecs,
31084 &decl_class_or_enum_p);
31085
31086 /* auto, register, static, extern, mutable. */
31087 if (declspecs.storage_class != sc_none)
31088 {
31089 cp_parser_error (parser, "invalid type for instance variable");
31090 declspecs.storage_class = sc_none;
31091 }
31092
31093 /* thread_local. */
31094 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31095 {
31096 cp_parser_error (parser, "invalid type for instance variable");
31097 declspecs.locations[ds_thread] = 0;
31098 }
31099
31100 /* typedef. */
31101 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31102 {
31103 cp_parser_error (parser, "invalid type for instance variable");
31104 declspecs.locations[ds_typedef] = 0;
31105 }
31106
31107 prefix_attributes = declspecs.attributes;
31108 declspecs.attributes = NULL_TREE;
31109
31110 /* Keep going until we hit the `;' at the end of the
31111 declaration. */
31112 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31113 {
31114 tree width = NULL_TREE, attributes, first_attribute, decl;
31115 cp_declarator *declarator = NULL;
31116 int ctor_dtor_or_conv_p;
31117
31118 /* Check for a (possibly unnamed) bitfield declaration. */
31119 token = cp_lexer_peek_token (parser->lexer);
31120 if (token->type == CPP_COLON)
31121 goto eat_colon;
31122
31123 if (token->type == CPP_NAME
31124 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
31125 == CPP_COLON))
31126 {
31127 /* Get the name of the bitfield. */
31128 declarator = make_id_declarator (NULL_TREE,
31129 cp_parser_identifier (parser),
31130 sfk_none, token->location);
31131
31132 eat_colon:
31133 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31134 /* Get the width of the bitfield. */
31135 width
31136 = cp_parser_constant_expression (parser);
31137 }
31138 else
31139 {
31140 /* Parse the declarator. */
31141 declarator
31142 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31143 CP_PARSER_FLAGS_NONE,
31144 &ctor_dtor_or_conv_p,
31145 /*parenthesized_p=*/NULL,
31146 /*member_p=*/false,
31147 /*friend_p=*/false,
31148 /*static_p=*/false);
31149 }
31150
31151 /* Look for attributes that apply to the ivar. */
31152 attributes = cp_parser_attributes_opt (parser);
31153 /* Remember which attributes are prefix attributes and
31154 which are not. */
31155 first_attribute = attributes;
31156 /* Combine the attributes. */
31157 attributes = attr_chainon (prefix_attributes, attributes);
31158
31159 if (width)
31160 /* Create the bitfield declaration. */
31161 decl = grokbitfield (declarator, &declspecs,
31162 width, NULL_TREE, attributes);
31163 else
31164 decl = grokfield (declarator, &declspecs,
31165 NULL_TREE, /*init_const_expr_p=*/false,
31166 NULL_TREE, attributes);
31167
31168 /* Add the instance variable. */
31169 if (decl != error_mark_node && decl != NULL_TREE)
31170 objc_add_instance_variable (decl);
31171
31172 /* Reset PREFIX_ATTRIBUTES. */
31173 if (attributes != error_mark_node)
31174 {
31175 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31176 attributes = TREE_CHAIN (attributes);
31177 if (attributes)
31178 TREE_CHAIN (attributes) = NULL_TREE;
31179 }
31180
31181 token = cp_lexer_peek_token (parser->lexer);
31182
31183 if (token->type == CPP_COMMA)
31184 {
31185 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31186 continue;
31187 }
31188 break;
31189 }
31190
31191 cp_parser_consume_semicolon_at_end_of_statement (parser);
31192 token = cp_lexer_peek_token (parser->lexer);
31193 }
31194
31195 if (token->keyword == RID_AT_END)
31196 cp_parser_error (parser, "expected %<}%>");
31197
31198 /* Do not consume the RID_AT_END, so it will be read again as terminating
31199 the @interface of @implementation. */
31200 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
31201 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
31202
31203 /* For historical reasons, we accept an optional semicolon. */
31204 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31205 cp_lexer_consume_token (parser->lexer);
31206 }
31207
31208 /* Parse an Objective-C protocol declaration. */
31209
31210 static void
31211 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
31212 {
31213 tree proto, protorefs;
31214 cp_token *tok;
31215
31216 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
31217 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31218 {
31219 tok = cp_lexer_peek_token (parser->lexer);
31220 error_at (tok->location, "identifier expected after %<@protocol%>");
31221 cp_parser_consume_semicolon_at_end_of_statement (parser);
31222 return;
31223 }
31224
31225 /* See if we have a forward declaration or a definition. */
31226 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
31227
31228 /* Try a forward declaration first. */
31229 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
31230 {
31231 while (true)
31232 {
31233 tree id;
31234
31235 id = cp_parser_identifier (parser);
31236 if (id == error_mark_node)
31237 break;
31238
31239 objc_declare_protocol (id, attributes);
31240
31241 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31242 cp_lexer_consume_token (parser->lexer);
31243 else
31244 break;
31245 }
31246 cp_parser_consume_semicolon_at_end_of_statement (parser);
31247 }
31248
31249 /* Ok, we got a full-fledged definition (or at least should). */
31250 else
31251 {
31252 proto = cp_parser_identifier (parser);
31253 protorefs = cp_parser_objc_protocol_refs_opt (parser);
31254 objc_start_protocol (proto, protorefs, attributes);
31255 cp_parser_objc_method_prototype_list (parser);
31256 }
31257 }
31258
31259 /* Parse an Objective-C superclass or category. */
31260
31261 static void
31262 cp_parser_objc_superclass_or_category (cp_parser *parser,
31263 bool iface_p,
31264 tree *super,
31265 tree *categ, bool *is_class_extension)
31266 {
31267 cp_token *next = cp_lexer_peek_token (parser->lexer);
31268
31269 *super = *categ = NULL_TREE;
31270 *is_class_extension = false;
31271 if (next->type == CPP_COLON)
31272 {
31273 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
31274 *super = cp_parser_identifier (parser);
31275 }
31276 else if (next->type == CPP_OPEN_PAREN)
31277 {
31278 matching_parens parens;
31279 parens.consume_open (parser); /* Eat '('. */
31280
31281 /* If there is no category name, and this is an @interface, we
31282 have a class extension. */
31283 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31284 {
31285 *categ = NULL_TREE;
31286 *is_class_extension = true;
31287 }
31288 else
31289 *categ = cp_parser_identifier (parser);
31290
31291 parens.require_close (parser);
31292 }
31293 }
31294
31295 /* Parse an Objective-C class interface. */
31296
31297 static void
31298 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
31299 {
31300 tree name, super, categ, protos;
31301 bool is_class_extension;
31302
31303 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
31304 name = cp_parser_identifier (parser);
31305 if (name == error_mark_node)
31306 {
31307 /* It's hard to recover because even if valid @interface stuff
31308 is to follow, we can't compile it (or validate it) if we
31309 don't even know which class it refers to. Let's assume this
31310 was a stray '@interface' token in the stream and skip it.
31311 */
31312 return;
31313 }
31314 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
31315 &is_class_extension);
31316 protos = cp_parser_objc_protocol_refs_opt (parser);
31317
31318 /* We have either a class or a category on our hands. */
31319 if (categ || is_class_extension)
31320 objc_start_category_interface (name, categ, protos, attributes);
31321 else
31322 {
31323 objc_start_class_interface (name, super, protos, attributes);
31324 /* Handle instance variable declarations, if any. */
31325 cp_parser_objc_class_ivars (parser);
31326 objc_continue_interface ();
31327 }
31328
31329 cp_parser_objc_method_prototype_list (parser);
31330 }
31331
31332 /* Parse an Objective-C class implementation. */
31333
31334 static void
31335 cp_parser_objc_class_implementation (cp_parser* parser)
31336 {
31337 tree name, super, categ;
31338 bool is_class_extension;
31339
31340 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
31341 name = cp_parser_identifier (parser);
31342 if (name == error_mark_node)
31343 {
31344 /* It's hard to recover because even if valid @implementation
31345 stuff is to follow, we can't compile it (or validate it) if
31346 we don't even know which class it refers to. Let's assume
31347 this was a stray '@implementation' token in the stream and
31348 skip it.
31349 */
31350 return;
31351 }
31352 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
31353 &is_class_extension);
31354
31355 /* We have either a class or a category on our hands. */
31356 if (categ)
31357 objc_start_category_implementation (name, categ);
31358 else
31359 {
31360 objc_start_class_implementation (name, super);
31361 /* Handle instance variable declarations, if any. */
31362 cp_parser_objc_class_ivars (parser);
31363 objc_continue_implementation ();
31364 }
31365
31366 cp_parser_objc_method_definition_list (parser);
31367 }
31368
31369 /* Consume the @end token and finish off the implementation. */
31370
31371 static void
31372 cp_parser_objc_end_implementation (cp_parser* parser)
31373 {
31374 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
31375 objc_finish_implementation ();
31376 }
31377
31378 /* Parse an Objective-C declaration. */
31379
31380 static void
31381 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
31382 {
31383 /* Try to figure out what kind of declaration is present. */
31384 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31385
31386 if (attributes)
31387 switch (kwd->keyword)
31388 {
31389 case RID_AT_ALIAS:
31390 case RID_AT_CLASS:
31391 case RID_AT_END:
31392 error_at (kwd->location, "attributes may not be specified before"
31393 " the %<@%D%> Objective-C++ keyword",
31394 kwd->u.value);
31395 attributes = NULL;
31396 break;
31397 case RID_AT_IMPLEMENTATION:
31398 warning_at (kwd->location, OPT_Wattributes,
31399 "prefix attributes are ignored before %<@%D%>",
31400 kwd->u.value);
31401 attributes = NULL;
31402 default:
31403 break;
31404 }
31405
31406 switch (kwd->keyword)
31407 {
31408 case RID_AT_ALIAS:
31409 cp_parser_objc_alias_declaration (parser);
31410 break;
31411 case RID_AT_CLASS:
31412 cp_parser_objc_class_declaration (parser);
31413 break;
31414 case RID_AT_PROTOCOL:
31415 cp_parser_objc_protocol_declaration (parser, attributes);
31416 break;
31417 case RID_AT_INTERFACE:
31418 cp_parser_objc_class_interface (parser, attributes);
31419 break;
31420 case RID_AT_IMPLEMENTATION:
31421 cp_parser_objc_class_implementation (parser);
31422 break;
31423 case RID_AT_END:
31424 cp_parser_objc_end_implementation (parser);
31425 break;
31426 default:
31427 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31428 kwd->u.value);
31429 cp_parser_skip_to_end_of_block_or_statement (parser);
31430 }
31431 }
31432
31433 /* Parse an Objective-C try-catch-finally statement.
31434
31435 objc-try-catch-finally-stmt:
31436 @try compound-statement objc-catch-clause-seq [opt]
31437 objc-finally-clause [opt]
31438
31439 objc-catch-clause-seq:
31440 objc-catch-clause objc-catch-clause-seq [opt]
31441
31442 objc-catch-clause:
31443 @catch ( objc-exception-declaration ) compound-statement
31444
31445 objc-finally-clause:
31446 @finally compound-statement
31447
31448 objc-exception-declaration:
31449 parameter-declaration
31450 '...'
31451
31452 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
31453
31454 Returns NULL_TREE.
31455
31456 PS: This function is identical to c_parser_objc_try_catch_finally_statement
31457 for C. Keep them in sync. */
31458
31459 static tree
31460 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
31461 {
31462 location_t location;
31463 tree stmt;
31464
31465 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
31466 location = cp_lexer_peek_token (parser->lexer)->location;
31467 objc_maybe_warn_exceptions (location);
31468 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
31469 node, lest it get absorbed into the surrounding block. */
31470 stmt = push_stmt_list ();
31471 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31472 objc_begin_try_stmt (location, pop_stmt_list (stmt));
31473
31474 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
31475 {
31476 cp_parameter_declarator *parm;
31477 tree parameter_declaration = error_mark_node;
31478 bool seen_open_paren = false;
31479 matching_parens parens;
31480
31481 cp_lexer_consume_token (parser->lexer);
31482 if (parens.require_open (parser))
31483 seen_open_paren = true;
31484 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
31485 {
31486 /* We have "@catch (...)" (where the '...' are literally
31487 what is in the code). Skip the '...'.
31488 parameter_declaration is set to NULL_TREE, and
31489 objc_being_catch_clauses() knows that that means
31490 '...'. */
31491 cp_lexer_consume_token (parser->lexer);
31492 parameter_declaration = NULL_TREE;
31493 }
31494 else
31495 {
31496 /* We have "@catch (NSException *exception)" or something
31497 like that. Parse the parameter declaration. */
31498 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
31499 false, NULL);
31500 if (parm == NULL)
31501 parameter_declaration = error_mark_node;
31502 else
31503 parameter_declaration = grokdeclarator (parm->declarator,
31504 &parm->decl_specifiers,
31505 PARM, /*initialized=*/0,
31506 /*attrlist=*/NULL);
31507 }
31508 if (seen_open_paren)
31509 parens.require_close (parser);
31510 else
31511 {
31512 /* If there was no open parenthesis, we are recovering from
31513 an error, and we are trying to figure out what mistake
31514 the user has made. */
31515
31516 /* If there is an immediate closing parenthesis, the user
31517 probably forgot the opening one (ie, they typed "@catch
31518 NSException *e)". Parse the closing parenthesis and keep
31519 going. */
31520 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
31521 cp_lexer_consume_token (parser->lexer);
31522
31523 /* If these is no immediate closing parenthesis, the user
31524 probably doesn't know that parenthesis are required at
31525 all (ie, they typed "@catch NSException *e"). So, just
31526 forget about the closing parenthesis and keep going. */
31527 }
31528 objc_begin_catch_clause (parameter_declaration);
31529 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31530 objc_finish_catch_clause ();
31531 }
31532 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
31533 {
31534 cp_lexer_consume_token (parser->lexer);
31535 location = cp_lexer_peek_token (parser->lexer)->location;
31536 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
31537 node, lest it get absorbed into the surrounding block. */
31538 stmt = push_stmt_list ();
31539 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31540 objc_build_finally_clause (location, pop_stmt_list (stmt));
31541 }
31542
31543 return objc_finish_try_stmt ();
31544 }
31545
31546 /* Parse an Objective-C synchronized statement.
31547
31548 objc-synchronized-stmt:
31549 @synchronized ( expression ) compound-statement
31550
31551 Returns NULL_TREE. */
31552
31553 static tree
31554 cp_parser_objc_synchronized_statement (cp_parser *parser)
31555 {
31556 location_t location;
31557 tree lock, stmt;
31558
31559 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
31560
31561 location = cp_lexer_peek_token (parser->lexer)->location;
31562 objc_maybe_warn_exceptions (location);
31563 matching_parens parens;
31564 parens.require_open (parser);
31565 lock = cp_parser_expression (parser);
31566 parens.require_close (parser);
31567
31568 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
31569 node, lest it get absorbed into the surrounding block. */
31570 stmt = push_stmt_list ();
31571 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
31572
31573 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
31574 }
31575
31576 /* Parse an Objective-C throw statement.
31577
31578 objc-throw-stmt:
31579 @throw assignment-expression [opt] ;
31580
31581 Returns a constructed '@throw' statement. */
31582
31583 static tree
31584 cp_parser_objc_throw_statement (cp_parser *parser)
31585 {
31586 tree expr = NULL_TREE;
31587 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31588
31589 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
31590
31591 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31592 expr = cp_parser_expression (parser);
31593
31594 cp_parser_consume_semicolon_at_end_of_statement (parser);
31595
31596 return objc_build_throw_stmt (loc, expr);
31597 }
31598
31599 /* Parse an Objective-C statement. */
31600
31601 static tree
31602 cp_parser_objc_statement (cp_parser * parser)
31603 {
31604 /* Try to figure out what kind of declaration is present. */
31605 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
31606
31607 switch (kwd->keyword)
31608 {
31609 case RID_AT_TRY:
31610 return cp_parser_objc_try_catch_finally_statement (parser);
31611 case RID_AT_SYNCHRONIZED:
31612 return cp_parser_objc_synchronized_statement (parser);
31613 case RID_AT_THROW:
31614 return cp_parser_objc_throw_statement (parser);
31615 default:
31616 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
31617 kwd->u.value);
31618 cp_parser_skip_to_end_of_block_or_statement (parser);
31619 }
31620
31621 return error_mark_node;
31622 }
31623
31624 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
31625 look ahead to see if an objc keyword follows the attributes. This
31626 is to detect the use of prefix attributes on ObjC @interface and
31627 @protocol. */
31628
31629 static bool
31630 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
31631 {
31632 cp_lexer_save_tokens (parser->lexer);
31633 *attrib = cp_parser_attributes_opt (parser);
31634 gcc_assert (*attrib);
31635 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
31636 {
31637 cp_lexer_commit_tokens (parser->lexer);
31638 return true;
31639 }
31640 cp_lexer_rollback_tokens (parser->lexer);
31641 return false;
31642 }
31643
31644 /* This routine is a minimal replacement for
31645 c_parser_struct_declaration () used when parsing the list of
31646 types/names or ObjC++ properties. For example, when parsing the
31647 code
31648
31649 @property (readonly) int a, b, c;
31650
31651 this function is responsible for parsing "int a, int b, int c" and
31652 returning the declarations as CHAIN of DECLs.
31653
31654 TODO: Share this code with cp_parser_objc_class_ivars. It's very
31655 similar parsing. */
31656 static tree
31657 cp_parser_objc_struct_declaration (cp_parser *parser)
31658 {
31659 tree decls = NULL_TREE;
31660 cp_decl_specifier_seq declspecs;
31661 int decl_class_or_enum_p;
31662 tree prefix_attributes;
31663
31664 cp_parser_decl_specifier_seq (parser,
31665 CP_PARSER_FLAGS_NONE,
31666 &declspecs,
31667 &decl_class_or_enum_p);
31668
31669 if (declspecs.type == error_mark_node)
31670 return error_mark_node;
31671
31672 /* auto, register, static, extern, mutable. */
31673 if (declspecs.storage_class != sc_none)
31674 {
31675 cp_parser_error (parser, "invalid type for property");
31676 declspecs.storage_class = sc_none;
31677 }
31678
31679 /* thread_local. */
31680 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
31681 {
31682 cp_parser_error (parser, "invalid type for property");
31683 declspecs.locations[ds_thread] = 0;
31684 }
31685
31686 /* typedef. */
31687 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
31688 {
31689 cp_parser_error (parser, "invalid type for property");
31690 declspecs.locations[ds_typedef] = 0;
31691 }
31692
31693 prefix_attributes = declspecs.attributes;
31694 declspecs.attributes = NULL_TREE;
31695
31696 /* Keep going until we hit the `;' at the end of the declaration. */
31697 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
31698 {
31699 tree attributes, first_attribute, decl;
31700 cp_declarator *declarator;
31701 cp_token *token;
31702
31703 /* Parse the declarator. */
31704 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
31705 CP_PARSER_FLAGS_NONE,
31706 NULL, NULL, false, false, false);
31707
31708 /* Look for attributes that apply to the ivar. */
31709 attributes = cp_parser_attributes_opt (parser);
31710 /* Remember which attributes are prefix attributes and
31711 which are not. */
31712 first_attribute = attributes;
31713 /* Combine the attributes. */
31714 attributes = attr_chainon (prefix_attributes, attributes);
31715
31716 decl = grokfield (declarator, &declspecs,
31717 NULL_TREE, /*init_const_expr_p=*/false,
31718 NULL_TREE, attributes);
31719
31720 if (decl == error_mark_node || decl == NULL_TREE)
31721 return error_mark_node;
31722
31723 /* Reset PREFIX_ATTRIBUTES. */
31724 if (attributes != error_mark_node)
31725 {
31726 while (attributes && TREE_CHAIN (attributes) != first_attribute)
31727 attributes = TREE_CHAIN (attributes);
31728 if (attributes)
31729 TREE_CHAIN (attributes) = NULL_TREE;
31730 }
31731
31732 DECL_CHAIN (decl) = decls;
31733 decls = decl;
31734
31735 token = cp_lexer_peek_token (parser->lexer);
31736 if (token->type == CPP_COMMA)
31737 {
31738 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
31739 continue;
31740 }
31741 else
31742 break;
31743 }
31744 return decls;
31745 }
31746
31747 /* Parse an Objective-C @property declaration. The syntax is:
31748
31749 objc-property-declaration:
31750 '@property' objc-property-attributes[opt] struct-declaration ;
31751
31752 objc-property-attributes:
31753 '(' objc-property-attribute-list ')'
31754
31755 objc-property-attribute-list:
31756 objc-property-attribute
31757 objc-property-attribute-list, objc-property-attribute
31758
31759 objc-property-attribute
31760 'getter' = identifier
31761 'setter' = identifier
31762 'readonly'
31763 'readwrite'
31764 'assign'
31765 'retain'
31766 'copy'
31767 'nonatomic'
31768
31769 For example:
31770 @property NSString *name;
31771 @property (readonly) id object;
31772 @property (retain, nonatomic, getter=getTheName) id name;
31773 @property int a, b, c;
31774
31775 PS: This function is identical to
31776 c_parser_objc_at_property_declaration for C. Keep them in sync. */
31777 static void
31778 cp_parser_objc_at_property_declaration (cp_parser *parser)
31779 {
31780 /* The following variables hold the attributes of the properties as
31781 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
31782 seen. When we see an attribute, we set them to 'true' (if they
31783 are boolean properties) or to the identifier (if they have an
31784 argument, ie, for getter and setter). Note that here we only
31785 parse the list of attributes, check the syntax and accumulate the
31786 attributes that we find. objc_add_property_declaration() will
31787 then process the information. */
31788 bool property_assign = false;
31789 bool property_copy = false;
31790 tree property_getter_ident = NULL_TREE;
31791 bool property_nonatomic = false;
31792 bool property_readonly = false;
31793 bool property_readwrite = false;
31794 bool property_retain = false;
31795 tree property_setter_ident = NULL_TREE;
31796
31797 /* 'properties' is the list of properties that we read. Usually a
31798 single one, but maybe more (eg, in "@property int a, b, c;" there
31799 are three). */
31800 tree properties;
31801 location_t loc;
31802
31803 loc = cp_lexer_peek_token (parser->lexer)->location;
31804
31805 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
31806
31807 /* Parse the optional attribute list... */
31808 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31809 {
31810 /* Eat the '('. */
31811 matching_parens parens;
31812 parens.consume_open (parser);
31813
31814 while (true)
31815 {
31816 bool syntax_error = false;
31817 cp_token *token = cp_lexer_peek_token (parser->lexer);
31818 enum rid keyword;
31819
31820 if (token->type != CPP_NAME)
31821 {
31822 cp_parser_error (parser, "expected identifier");
31823 break;
31824 }
31825 keyword = C_RID_CODE (token->u.value);
31826 cp_lexer_consume_token (parser->lexer);
31827 switch (keyword)
31828 {
31829 case RID_ASSIGN: property_assign = true; break;
31830 case RID_COPY: property_copy = true; break;
31831 case RID_NONATOMIC: property_nonatomic = true; break;
31832 case RID_READONLY: property_readonly = true; break;
31833 case RID_READWRITE: property_readwrite = true; break;
31834 case RID_RETAIN: property_retain = true; break;
31835
31836 case RID_GETTER:
31837 case RID_SETTER:
31838 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
31839 {
31840 if (keyword == RID_GETTER)
31841 cp_parser_error (parser,
31842 "missing %<=%> (after %<getter%> attribute)");
31843 else
31844 cp_parser_error (parser,
31845 "missing %<=%> (after %<setter%> attribute)");
31846 syntax_error = true;
31847 break;
31848 }
31849 cp_lexer_consume_token (parser->lexer); /* eat the = */
31850 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
31851 {
31852 cp_parser_error (parser, "expected identifier");
31853 syntax_error = true;
31854 break;
31855 }
31856 if (keyword == RID_SETTER)
31857 {
31858 if (property_setter_ident != NULL_TREE)
31859 {
31860 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
31861 cp_lexer_consume_token (parser->lexer);
31862 }
31863 else
31864 property_setter_ident = cp_parser_objc_selector (parser);
31865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
31866 cp_parser_error (parser, "setter name must terminate with %<:%>");
31867 else
31868 cp_lexer_consume_token (parser->lexer);
31869 }
31870 else
31871 {
31872 if (property_getter_ident != NULL_TREE)
31873 {
31874 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
31875 cp_lexer_consume_token (parser->lexer);
31876 }
31877 else
31878 property_getter_ident = cp_parser_objc_selector (parser);
31879 }
31880 break;
31881 default:
31882 cp_parser_error (parser, "unknown property attribute");
31883 syntax_error = true;
31884 break;
31885 }
31886
31887 if (syntax_error)
31888 break;
31889
31890 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31891 cp_lexer_consume_token (parser->lexer);
31892 else
31893 break;
31894 }
31895
31896 /* FIXME: "@property (setter, assign);" will generate a spurious
31897 "error: expected ‘)’ before ‘,’ token". This is because
31898 cp_parser_require, unlike the C counterpart, will produce an
31899 error even if we are in error recovery. */
31900 if (!parens.require_close (parser))
31901 {
31902 cp_parser_skip_to_closing_parenthesis (parser,
31903 /*recovering=*/true,
31904 /*or_comma=*/false,
31905 /*consume_paren=*/true);
31906 }
31907 }
31908
31909 /* ... and the property declaration(s). */
31910 properties = cp_parser_objc_struct_declaration (parser);
31911
31912 if (properties == error_mark_node)
31913 {
31914 cp_parser_skip_to_end_of_statement (parser);
31915 /* If the next token is now a `;', consume it. */
31916 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
31917 cp_lexer_consume_token (parser->lexer);
31918 return;
31919 }
31920
31921 if (properties == NULL_TREE)
31922 cp_parser_error (parser, "expected identifier");
31923 else
31924 {
31925 /* Comma-separated properties are chained together in
31926 reverse order; add them one by one. */
31927 properties = nreverse (properties);
31928
31929 for (; properties; properties = TREE_CHAIN (properties))
31930 objc_add_property_declaration (loc, copy_node (properties),
31931 property_readonly, property_readwrite,
31932 property_assign, property_retain,
31933 property_copy, property_nonatomic,
31934 property_getter_ident, property_setter_ident);
31935 }
31936
31937 cp_parser_consume_semicolon_at_end_of_statement (parser);
31938 }
31939
31940 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31941
31942 objc-synthesize-declaration:
31943 @synthesize objc-synthesize-identifier-list ;
31944
31945 objc-synthesize-identifier-list:
31946 objc-synthesize-identifier
31947 objc-synthesize-identifier-list, objc-synthesize-identifier
31948
31949 objc-synthesize-identifier
31950 identifier
31951 identifier = identifier
31952
31953 For example:
31954 @synthesize MyProperty;
31955 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31956
31957 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31958 for C. Keep them in sync.
31959 */
31960 static void
31961 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31962 {
31963 tree list = NULL_TREE;
31964 location_t loc;
31965 loc = cp_lexer_peek_token (parser->lexer)->location;
31966
31967 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31968 while (true)
31969 {
31970 tree property, ivar;
31971 property = cp_parser_identifier (parser);
31972 if (property == error_mark_node)
31973 {
31974 cp_parser_consume_semicolon_at_end_of_statement (parser);
31975 return;
31976 }
31977 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31978 {
31979 cp_lexer_consume_token (parser->lexer);
31980 ivar = cp_parser_identifier (parser);
31981 if (ivar == error_mark_node)
31982 {
31983 cp_parser_consume_semicolon_at_end_of_statement (parser);
31984 return;
31985 }
31986 }
31987 else
31988 ivar = NULL_TREE;
31989 list = chainon (list, build_tree_list (ivar, property));
31990 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31991 cp_lexer_consume_token (parser->lexer);
31992 else
31993 break;
31994 }
31995 cp_parser_consume_semicolon_at_end_of_statement (parser);
31996 objc_add_synthesize_declaration (loc, list);
31997 }
31998
31999 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
32000
32001 objc-dynamic-declaration:
32002 @dynamic identifier-list ;
32003
32004 For example:
32005 @dynamic MyProperty;
32006 @dynamic MyProperty, AnotherProperty;
32007
32008 PS: This function is identical to c_parser_objc_at_dynamic_declaration
32009 for C. Keep them in sync.
32010 */
32011 static void
32012 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
32013 {
32014 tree list = NULL_TREE;
32015 location_t loc;
32016 loc = cp_lexer_peek_token (parser->lexer)->location;
32017
32018 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
32019 while (true)
32020 {
32021 tree property;
32022 property = cp_parser_identifier (parser);
32023 if (property == error_mark_node)
32024 {
32025 cp_parser_consume_semicolon_at_end_of_statement (parser);
32026 return;
32027 }
32028 list = chainon (list, build_tree_list (NULL, property));
32029 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32030 cp_lexer_consume_token (parser->lexer);
32031 else
32032 break;
32033 }
32034 cp_parser_consume_semicolon_at_end_of_statement (parser);
32035 objc_add_dynamic_declaration (loc, list);
32036 }
32037
32038 \f
32039 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
32040
32041 /* Returns name of the next clause.
32042 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
32043 the token is not consumed. Otherwise appropriate pragma_omp_clause is
32044 returned and the token is consumed. */
32045
32046 static pragma_omp_clause
32047 cp_parser_omp_clause_name (cp_parser *parser)
32048 {
32049 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
32050
32051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32052 result = PRAGMA_OACC_CLAUSE_AUTO;
32053 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
32054 result = PRAGMA_OMP_CLAUSE_IF;
32055 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
32056 result = PRAGMA_OMP_CLAUSE_DEFAULT;
32057 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
32058 result = PRAGMA_OACC_CLAUSE_DELETE;
32059 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
32060 result = PRAGMA_OMP_CLAUSE_PRIVATE;
32061 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
32062 result = PRAGMA_OMP_CLAUSE_FOR;
32063 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32064 {
32065 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32066 const char *p = IDENTIFIER_POINTER (id);
32067
32068 switch (p[0])
32069 {
32070 case 'a':
32071 if (!strcmp ("aligned", p))
32072 result = PRAGMA_OMP_CLAUSE_ALIGNED;
32073 else if (!strcmp ("async", p))
32074 result = PRAGMA_OACC_CLAUSE_ASYNC;
32075 break;
32076 case 'c':
32077 if (!strcmp ("collapse", p))
32078 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
32079 else if (!strcmp ("copy", p))
32080 result = PRAGMA_OACC_CLAUSE_COPY;
32081 else if (!strcmp ("copyin", p))
32082 result = PRAGMA_OMP_CLAUSE_COPYIN;
32083 else if (!strcmp ("copyout", p))
32084 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32085 else if (!strcmp ("copyprivate", p))
32086 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
32087 else if (!strcmp ("create", p))
32088 result = PRAGMA_OACC_CLAUSE_CREATE;
32089 break;
32090 case 'd':
32091 if (!strcmp ("defaultmap", p))
32092 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
32093 else if (!strcmp ("depend", p))
32094 result = PRAGMA_OMP_CLAUSE_DEPEND;
32095 else if (!strcmp ("device", p))
32096 result = PRAGMA_OMP_CLAUSE_DEVICE;
32097 else if (!strcmp ("deviceptr", p))
32098 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
32099 else if (!strcmp ("device_resident", p))
32100 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
32101 else if (!strcmp ("dist_schedule", p))
32102 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
32103 break;
32104 case 'f':
32105 if (!strcmp ("final", p))
32106 result = PRAGMA_OMP_CLAUSE_FINAL;
32107 else if (!strcmp ("finalize", p))
32108 result = PRAGMA_OACC_CLAUSE_FINALIZE;
32109 else if (!strcmp ("firstprivate", p))
32110 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
32111 else if (!strcmp ("from", p))
32112 result = PRAGMA_OMP_CLAUSE_FROM;
32113 break;
32114 case 'g':
32115 if (!strcmp ("gang", p))
32116 result = PRAGMA_OACC_CLAUSE_GANG;
32117 else if (!strcmp ("grainsize", p))
32118 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
32119 break;
32120 case 'h':
32121 if (!strcmp ("hint", p))
32122 result = PRAGMA_OMP_CLAUSE_HINT;
32123 else if (!strcmp ("host", p))
32124 result = PRAGMA_OACC_CLAUSE_HOST;
32125 break;
32126 case 'i':
32127 if (!strcmp ("if_present", p))
32128 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
32129 else if (!strcmp ("in_reduction", p))
32130 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
32131 else if (!strcmp ("inbranch", p))
32132 result = PRAGMA_OMP_CLAUSE_INBRANCH;
32133 else if (!strcmp ("independent", p))
32134 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
32135 else if (!strcmp ("is_device_ptr", p))
32136 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
32137 break;
32138 case 'l':
32139 if (!strcmp ("lastprivate", p))
32140 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
32141 else if (!strcmp ("linear", p))
32142 result = PRAGMA_OMP_CLAUSE_LINEAR;
32143 else if (!strcmp ("link", p))
32144 result = PRAGMA_OMP_CLAUSE_LINK;
32145 break;
32146 case 'm':
32147 if (!strcmp ("map", p))
32148 result = PRAGMA_OMP_CLAUSE_MAP;
32149 else if (!strcmp ("mergeable", p))
32150 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
32151 break;
32152 case 'n':
32153 if (!strcmp ("nogroup", p))
32154 result = PRAGMA_OMP_CLAUSE_NOGROUP;
32155 else if (!strcmp ("nontemporal", p))
32156 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
32157 else if (!strcmp ("notinbranch", p))
32158 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
32159 else if (!strcmp ("nowait", p))
32160 result = PRAGMA_OMP_CLAUSE_NOWAIT;
32161 else if (!strcmp ("num_gangs", p))
32162 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
32163 else if (!strcmp ("num_tasks", p))
32164 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
32165 else if (!strcmp ("num_teams", p))
32166 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
32167 else if (!strcmp ("num_threads", p))
32168 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
32169 else if (!strcmp ("num_workers", p))
32170 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
32171 break;
32172 case 'o':
32173 if (!strcmp ("ordered", p))
32174 result = PRAGMA_OMP_CLAUSE_ORDERED;
32175 break;
32176 case 'p':
32177 if (!strcmp ("parallel", p))
32178 result = PRAGMA_OMP_CLAUSE_PARALLEL;
32179 else if (!strcmp ("present", p))
32180 result = PRAGMA_OACC_CLAUSE_PRESENT;
32181 else if (!strcmp ("present_or_copy", p)
32182 || !strcmp ("pcopy", p))
32183 result = PRAGMA_OACC_CLAUSE_COPY;
32184 else if (!strcmp ("present_or_copyin", p)
32185 || !strcmp ("pcopyin", p))
32186 result = PRAGMA_OACC_CLAUSE_COPYIN;
32187 else if (!strcmp ("present_or_copyout", p)
32188 || !strcmp ("pcopyout", p))
32189 result = PRAGMA_OACC_CLAUSE_COPYOUT;
32190 else if (!strcmp ("present_or_create", p)
32191 || !strcmp ("pcreate", p))
32192 result = PRAGMA_OACC_CLAUSE_CREATE;
32193 else if (!strcmp ("priority", p))
32194 result = PRAGMA_OMP_CLAUSE_PRIORITY;
32195 else if (!strcmp ("proc_bind", p))
32196 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
32197 break;
32198 case 'r':
32199 if (!strcmp ("reduction", p))
32200 result = PRAGMA_OMP_CLAUSE_REDUCTION;
32201 break;
32202 case 's':
32203 if (!strcmp ("safelen", p))
32204 result = PRAGMA_OMP_CLAUSE_SAFELEN;
32205 else if (!strcmp ("schedule", p))
32206 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
32207 else if (!strcmp ("sections", p))
32208 result = PRAGMA_OMP_CLAUSE_SECTIONS;
32209 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
32210 result = PRAGMA_OACC_CLAUSE_HOST;
32211 else if (!strcmp ("seq", p))
32212 result = PRAGMA_OACC_CLAUSE_SEQ;
32213 else if (!strcmp ("shared", p))
32214 result = PRAGMA_OMP_CLAUSE_SHARED;
32215 else if (!strcmp ("simd", p))
32216 result = PRAGMA_OMP_CLAUSE_SIMD;
32217 else if (!strcmp ("simdlen", p))
32218 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
32219 break;
32220 case 't':
32221 if (!strcmp ("task_reduction", p))
32222 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
32223 else if (!strcmp ("taskgroup", p))
32224 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
32225 else if (!strcmp ("thread_limit", p))
32226 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
32227 else if (!strcmp ("threads", p))
32228 result = PRAGMA_OMP_CLAUSE_THREADS;
32229 else if (!strcmp ("tile", p))
32230 result = PRAGMA_OACC_CLAUSE_TILE;
32231 else if (!strcmp ("to", p))
32232 result = PRAGMA_OMP_CLAUSE_TO;
32233 break;
32234 case 'u':
32235 if (!strcmp ("uniform", p))
32236 result = PRAGMA_OMP_CLAUSE_UNIFORM;
32237 else if (!strcmp ("untied", p))
32238 result = PRAGMA_OMP_CLAUSE_UNTIED;
32239 else if (!strcmp ("use_device", p))
32240 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
32241 else if (!strcmp ("use_device_ptr", p))
32242 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
32243 break;
32244 case 'v':
32245 if (!strcmp ("vector", p))
32246 result = PRAGMA_OACC_CLAUSE_VECTOR;
32247 else if (!strcmp ("vector_length", p))
32248 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
32249 break;
32250 case 'w':
32251 if (!strcmp ("wait", p))
32252 result = PRAGMA_OACC_CLAUSE_WAIT;
32253 else if (!strcmp ("worker", p))
32254 result = PRAGMA_OACC_CLAUSE_WORKER;
32255 break;
32256 }
32257 }
32258
32259 if (result != PRAGMA_OMP_CLAUSE_NONE)
32260 cp_lexer_consume_token (parser->lexer);
32261
32262 return result;
32263 }
32264
32265 /* Validate that a clause of the given type does not already exist. */
32266
32267 static void
32268 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
32269 const char *name, location_t location)
32270 {
32271 tree c;
32272
32273 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
32274 if (OMP_CLAUSE_CODE (c) == code)
32275 {
32276 error_at (location, "too many %qs clauses", name);
32277 break;
32278 }
32279 }
32280
32281 /* OpenMP 2.5:
32282 variable-list:
32283 identifier
32284 variable-list , identifier
32285
32286 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
32287 colon). An opening parenthesis will have been consumed by the caller.
32288
32289 If KIND is nonzero, create the appropriate node and install the decl
32290 in OMP_CLAUSE_DECL and add the node to the head of the list.
32291
32292 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
32293 return the list created.
32294
32295 COLON can be NULL if only closing parenthesis should end the list,
32296 or pointer to bool which will receive false if the list is terminated
32297 by closing parenthesis or true if the list is terminated by colon. */
32298
32299 static tree
32300 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
32301 tree list, bool *colon)
32302 {
32303 cp_token *token;
32304 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32305 if (colon)
32306 {
32307 parser->colon_corrects_to_scope_p = false;
32308 *colon = false;
32309 }
32310 while (1)
32311 {
32312 tree name, decl;
32313
32314 if (kind == OMP_CLAUSE_DEPEND)
32315 cp_parser_parse_tentatively (parser);
32316 token = cp_lexer_peek_token (parser->lexer);
32317 if (kind != 0
32318 && current_class_ptr
32319 && cp_parser_is_keyword (token, RID_THIS))
32320 {
32321 decl = finish_this_expr ();
32322 if (TREE_CODE (decl) == NON_LVALUE_EXPR
32323 || CONVERT_EXPR_P (decl))
32324 decl = TREE_OPERAND (decl, 0);
32325 cp_lexer_consume_token (parser->lexer);
32326 }
32327 else
32328 {
32329 name = cp_parser_id_expression (parser, /*template_p=*/false,
32330 /*check_dependency_p=*/true,
32331 /*template_p=*/NULL,
32332 /*declarator_p=*/false,
32333 /*optional_p=*/false);
32334 if (name == error_mark_node)
32335 {
32336 if (kind == OMP_CLAUSE_DEPEND
32337 && cp_parser_simulate_error (parser))
32338 goto depend_lvalue;
32339 goto skip_comma;
32340 }
32341
32342 if (identifier_p (name))
32343 decl = cp_parser_lookup_name_simple (parser, name, token->location);
32344 else
32345 decl = name;
32346 if (decl == error_mark_node)
32347 {
32348 if (kind == OMP_CLAUSE_DEPEND
32349 && cp_parser_simulate_error (parser))
32350 goto depend_lvalue;
32351 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
32352 token->location);
32353 }
32354 }
32355 if (decl == error_mark_node)
32356 ;
32357 else if (kind != 0)
32358 {
32359 switch (kind)
32360 {
32361 case OMP_CLAUSE__CACHE_:
32362 /* The OpenACC cache directive explicitly only allows "array
32363 elements or subarrays". */
32364 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
32365 {
32366 error_at (token->location, "expected %<[%>");
32367 decl = error_mark_node;
32368 break;
32369 }
32370 /* FALLTHROUGH. */
32371 case OMP_CLAUSE_MAP:
32372 case OMP_CLAUSE_FROM:
32373 case OMP_CLAUSE_TO:
32374 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
32375 {
32376 location_t loc
32377 = cp_lexer_peek_token (parser->lexer)->location;
32378 cp_id_kind idk = CP_ID_KIND_NONE;
32379 cp_lexer_consume_token (parser->lexer);
32380 decl = convert_from_reference (decl);
32381 decl
32382 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
32383 decl, false,
32384 &idk, loc);
32385 }
32386 /* FALLTHROUGH. */
32387 case OMP_CLAUSE_DEPEND:
32388 case OMP_CLAUSE_REDUCTION:
32389 case OMP_CLAUSE_IN_REDUCTION:
32390 case OMP_CLAUSE_TASK_REDUCTION:
32391 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
32392 {
32393 tree low_bound = NULL_TREE, length = NULL_TREE;
32394
32395 parser->colon_corrects_to_scope_p = false;
32396 cp_lexer_consume_token (parser->lexer);
32397 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32398 low_bound = cp_parser_expression (parser);
32399 if (!colon)
32400 parser->colon_corrects_to_scope_p
32401 = saved_colon_corrects_to_scope_p;
32402 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
32403 length = integer_one_node;
32404 else
32405 {
32406 /* Look for `:'. */
32407 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32408 {
32409 if (kind == OMP_CLAUSE_DEPEND
32410 && cp_parser_simulate_error (parser))
32411 goto depend_lvalue;
32412 goto skip_comma;
32413 }
32414 if (kind == OMP_CLAUSE_DEPEND)
32415 cp_parser_commit_to_tentative_parse (parser);
32416 if (!cp_lexer_next_token_is (parser->lexer,
32417 CPP_CLOSE_SQUARE))
32418 length = cp_parser_expression (parser);
32419 }
32420 /* Look for the closing `]'. */
32421 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
32422 RT_CLOSE_SQUARE))
32423 {
32424 if (kind == OMP_CLAUSE_DEPEND
32425 && cp_parser_simulate_error (parser))
32426 goto depend_lvalue;
32427 goto skip_comma;
32428 }
32429
32430 decl = tree_cons (low_bound, length, decl);
32431 }
32432 break;
32433 default:
32434 break;
32435 }
32436
32437 if (kind == OMP_CLAUSE_DEPEND)
32438 {
32439 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
32440 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
32441 && cp_parser_simulate_error (parser))
32442 {
32443 depend_lvalue:
32444 cp_parser_abort_tentative_parse (parser);
32445 decl = cp_parser_assignment_expression (parser, NULL,
32446 false, false);
32447 }
32448 else
32449 cp_parser_parse_definitely (parser);
32450 }
32451
32452 tree u = build_omp_clause (token->location, kind);
32453 OMP_CLAUSE_DECL (u) = decl;
32454 OMP_CLAUSE_CHAIN (u) = list;
32455 list = u;
32456 }
32457 else
32458 list = tree_cons (decl, NULL_TREE, list);
32459
32460 get_comma:
32461 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32462 break;
32463 cp_lexer_consume_token (parser->lexer);
32464 }
32465
32466 if (colon)
32467 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32468
32469 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32470 {
32471 *colon = true;
32472 cp_parser_require (parser, CPP_COLON, RT_COLON);
32473 return list;
32474 }
32475
32476 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32477 {
32478 int ending;
32479
32480 /* Try to resync to an unnested comma. Copied from
32481 cp_parser_parenthesized_expression_list. */
32482 skip_comma:
32483 if (colon)
32484 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32485 ending = cp_parser_skip_to_closing_parenthesis (parser,
32486 /*recovering=*/true,
32487 /*or_comma=*/true,
32488 /*consume_paren=*/true);
32489 if (ending < 0)
32490 goto get_comma;
32491 }
32492
32493 return list;
32494 }
32495
32496 /* Similarly, but expect leading and trailing parenthesis. This is a very
32497 common case for omp clauses. */
32498
32499 static tree
32500 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
32501 {
32502 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32503 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
32504 return list;
32505 }
32506
32507 /* OpenACC 2.0:
32508 copy ( variable-list )
32509 copyin ( variable-list )
32510 copyout ( variable-list )
32511 create ( variable-list )
32512 delete ( variable-list )
32513 present ( variable-list ) */
32514
32515 static tree
32516 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
32517 tree list)
32518 {
32519 enum gomp_map_kind kind;
32520 switch (c_kind)
32521 {
32522 case PRAGMA_OACC_CLAUSE_COPY:
32523 kind = GOMP_MAP_TOFROM;
32524 break;
32525 case PRAGMA_OACC_CLAUSE_COPYIN:
32526 kind = GOMP_MAP_TO;
32527 break;
32528 case PRAGMA_OACC_CLAUSE_COPYOUT:
32529 kind = GOMP_MAP_FROM;
32530 break;
32531 case PRAGMA_OACC_CLAUSE_CREATE:
32532 kind = GOMP_MAP_ALLOC;
32533 break;
32534 case PRAGMA_OACC_CLAUSE_DELETE:
32535 kind = GOMP_MAP_RELEASE;
32536 break;
32537 case PRAGMA_OACC_CLAUSE_DEVICE:
32538 kind = GOMP_MAP_FORCE_TO;
32539 break;
32540 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32541 kind = GOMP_MAP_DEVICE_RESIDENT;
32542 break;
32543 case PRAGMA_OACC_CLAUSE_HOST:
32544 kind = GOMP_MAP_FORCE_FROM;
32545 break;
32546 case PRAGMA_OACC_CLAUSE_LINK:
32547 kind = GOMP_MAP_LINK;
32548 break;
32549 case PRAGMA_OACC_CLAUSE_PRESENT:
32550 kind = GOMP_MAP_FORCE_PRESENT;
32551 break;
32552 default:
32553 gcc_unreachable ();
32554 }
32555 tree nl, c;
32556 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
32557
32558 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
32559 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32560
32561 return nl;
32562 }
32563
32564 /* OpenACC 2.0:
32565 deviceptr ( variable-list ) */
32566
32567 static tree
32568 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
32569 {
32570 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32571 tree vars, t;
32572
32573 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
32574 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
32575 variable-list must only allow for pointer variables. */
32576 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
32577 for (t = vars; t; t = TREE_CHAIN (t))
32578 {
32579 tree v = TREE_PURPOSE (t);
32580 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
32581 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
32582 OMP_CLAUSE_DECL (u) = v;
32583 OMP_CLAUSE_CHAIN (u) = list;
32584 list = u;
32585 }
32586
32587 return list;
32588 }
32589
32590 /* OpenACC 2.5:
32591 auto
32592 finalize
32593 independent
32594 nohost
32595 seq */
32596
32597 static tree
32598 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
32599 tree list)
32600 {
32601 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
32602
32603 tree c = build_omp_clause (loc, code);
32604 OMP_CLAUSE_CHAIN (c) = list;
32605
32606 return c;
32607 }
32608
32609 /* OpenACC:
32610 num_gangs ( expression )
32611 num_workers ( expression )
32612 vector_length ( expression ) */
32613
32614 static tree
32615 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
32616 const char *str, tree list)
32617 {
32618 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32619
32620 matching_parens parens;
32621 if (!parens.require_open (parser))
32622 return list;
32623
32624 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
32625
32626 if (t == error_mark_node
32627 || !parens.require_close (parser))
32628 {
32629 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32630 /*or_comma=*/false,
32631 /*consume_paren=*/true);
32632 return list;
32633 }
32634
32635 check_no_duplicate_clause (list, code, str, loc);
32636
32637 tree c = build_omp_clause (loc, code);
32638 OMP_CLAUSE_OPERAND (c, 0) = t;
32639 OMP_CLAUSE_CHAIN (c) = list;
32640 return c;
32641 }
32642
32643 /* OpenACC:
32644
32645 gang [( gang-arg-list )]
32646 worker [( [num:] int-expr )]
32647 vector [( [length:] int-expr )]
32648
32649 where gang-arg is one of:
32650
32651 [num:] int-expr
32652 static: size-expr
32653
32654 and size-expr may be:
32655
32656 *
32657 int-expr
32658 */
32659
32660 static tree
32661 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
32662 omp_clause_code kind,
32663 const char *str, tree list)
32664 {
32665 const char *id = "num";
32666 cp_lexer *lexer = parser->lexer;
32667 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
32668
32669 if (kind == OMP_CLAUSE_VECTOR)
32670 id = "length";
32671
32672 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
32673 {
32674 matching_parens parens;
32675 parens.consume_open (parser);
32676
32677 do
32678 {
32679 cp_token *next = cp_lexer_peek_token (lexer);
32680 int idx = 0;
32681
32682 /* Gang static argument. */
32683 if (kind == OMP_CLAUSE_GANG
32684 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
32685 {
32686 cp_lexer_consume_token (lexer);
32687
32688 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32689 goto cleanup_error;
32690
32691 idx = 1;
32692 if (ops[idx] != NULL)
32693 {
32694 cp_parser_error (parser, "too many %<static%> arguments");
32695 goto cleanup_error;
32696 }
32697
32698 /* Check for the '*' argument. */
32699 if (cp_lexer_next_token_is (lexer, CPP_MULT)
32700 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32701 || cp_lexer_nth_token_is (parser->lexer, 2,
32702 CPP_CLOSE_PAREN)))
32703 {
32704 cp_lexer_consume_token (lexer);
32705 ops[idx] = integer_minus_one_node;
32706
32707 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
32708 {
32709 cp_lexer_consume_token (lexer);
32710 continue;
32711 }
32712 else break;
32713 }
32714 }
32715 /* Worker num: argument and vector length: arguments. */
32716 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
32717 && id_equal (next->u.value, id)
32718 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
32719 {
32720 cp_lexer_consume_token (lexer); /* id */
32721 cp_lexer_consume_token (lexer); /* ':' */
32722 }
32723
32724 /* Now collect the actual argument. */
32725 if (ops[idx] != NULL_TREE)
32726 {
32727 cp_parser_error (parser, "unexpected argument");
32728 goto cleanup_error;
32729 }
32730
32731 tree expr = cp_parser_assignment_expression (parser, NULL, false,
32732 false);
32733 if (expr == error_mark_node)
32734 goto cleanup_error;
32735
32736 mark_exp_read (expr);
32737 ops[idx] = expr;
32738
32739 if (kind == OMP_CLAUSE_GANG
32740 && cp_lexer_next_token_is (lexer, CPP_COMMA))
32741 {
32742 cp_lexer_consume_token (lexer);
32743 continue;
32744 }
32745 break;
32746 }
32747 while (1);
32748
32749 if (!parens.require_close (parser))
32750 goto cleanup_error;
32751 }
32752
32753 check_no_duplicate_clause (list, kind, str, loc);
32754
32755 c = build_omp_clause (loc, kind);
32756
32757 if (ops[1])
32758 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
32759
32760 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
32761 OMP_CLAUSE_CHAIN (c) = list;
32762
32763 return c;
32764
32765 cleanup_error:
32766 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32767 return list;
32768 }
32769
32770 /* OpenACC 2.0:
32771 tile ( size-expr-list ) */
32772
32773 static tree
32774 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
32775 {
32776 tree c, expr = error_mark_node;
32777 tree tile = NULL_TREE;
32778
32779 /* Collapse and tile are mutually exclusive. (The spec doesn't say
32780 so, but the spec authors never considered such a case and have
32781 differing opinions on what it might mean, including 'not
32782 allowed'.) */
32783 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
32784 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
32785 clause_loc);
32786
32787 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32788 return list;
32789
32790 do
32791 {
32792 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
32793 return list;
32794
32795 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
32796 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
32797 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
32798 {
32799 cp_lexer_consume_token (parser->lexer);
32800 expr = integer_zero_node;
32801 }
32802 else
32803 expr = cp_parser_constant_expression (parser);
32804
32805 tile = tree_cons (NULL_TREE, expr, tile);
32806 }
32807 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
32808
32809 /* Consume the trailing ')'. */
32810 cp_lexer_consume_token (parser->lexer);
32811
32812 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
32813 tile = nreverse (tile);
32814 OMP_CLAUSE_TILE_LIST (c) = tile;
32815 OMP_CLAUSE_CHAIN (c) = list;
32816 return c;
32817 }
32818
32819 /* OpenACC 2.0
32820 Parse wait clause or directive parameters. */
32821
32822 static tree
32823 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
32824 {
32825 vec<tree, va_gc> *args;
32826 tree t, args_tree;
32827
32828 args = cp_parser_parenthesized_expression_list (parser, non_attr,
32829 /*cast_p=*/false,
32830 /*allow_expansion_p=*/true,
32831 /*non_constant_p=*/NULL);
32832
32833 if (args == NULL || args->length () == 0)
32834 {
32835 if (args != NULL)
32836 {
32837 cp_parser_error (parser, "expected integer expression list");
32838 release_tree_vector (args);
32839 }
32840 return list;
32841 }
32842
32843 args_tree = build_tree_list_vec (args);
32844
32845 release_tree_vector (args);
32846
32847 for (t = args_tree; t; t = TREE_CHAIN (t))
32848 {
32849 tree targ = TREE_VALUE (t);
32850
32851 if (targ != error_mark_node)
32852 {
32853 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
32854 error ("%<wait%> expression must be integral");
32855 else
32856 {
32857 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
32858
32859 targ = mark_rvalue_use (targ);
32860 OMP_CLAUSE_DECL (c) = targ;
32861 OMP_CLAUSE_CHAIN (c) = list;
32862 list = c;
32863 }
32864 }
32865 }
32866
32867 return list;
32868 }
32869
32870 /* OpenACC:
32871 wait [( int-expr-list )] */
32872
32873 static tree
32874 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
32875 {
32876 location_t location = cp_lexer_peek_token (parser->lexer)->location;
32877
32878 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32879 list = cp_parser_oacc_wait_list (parser, location, list);
32880 else
32881 {
32882 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
32883
32884 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32885 OMP_CLAUSE_CHAIN (c) = list;
32886 list = c;
32887 }
32888
32889 return list;
32890 }
32891
32892 /* OpenMP 3.0:
32893 collapse ( constant-expression ) */
32894
32895 static tree
32896 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
32897 {
32898 tree c, num;
32899 location_t loc;
32900 HOST_WIDE_INT n;
32901
32902 loc = cp_lexer_peek_token (parser->lexer)->location;
32903 matching_parens parens;
32904 if (!parens.require_open (parser))
32905 return list;
32906
32907 num = cp_parser_constant_expression (parser);
32908
32909 if (!parens.require_close (parser))
32910 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32911 /*or_comma=*/false,
32912 /*consume_paren=*/true);
32913
32914 if (num == error_mark_node)
32915 return list;
32916 num = fold_non_dependent_expr (num);
32917 if (!tree_fits_shwi_p (num)
32918 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32919 || (n = tree_to_shwi (num)) <= 0
32920 || (int) n != n)
32921 {
32922 error_at (loc, "collapse argument needs positive constant integer expression");
32923 return list;
32924 }
32925
32926 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
32927 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
32928 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
32929 OMP_CLAUSE_CHAIN (c) = list;
32930 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
32931
32932 return c;
32933 }
32934
32935 /* OpenMP 2.5:
32936 default ( none | shared )
32937
32938 OpenACC:
32939 default ( none | present ) */
32940
32941 static tree
32942 cp_parser_omp_clause_default (cp_parser *parser, tree list,
32943 location_t location, bool is_oacc)
32944 {
32945 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
32946 tree c;
32947
32948 matching_parens parens;
32949 if (!parens.require_open (parser))
32950 return list;
32951 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32952 {
32953 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32954 const char *p = IDENTIFIER_POINTER (id);
32955
32956 switch (p[0])
32957 {
32958 case 'n':
32959 if (strcmp ("none", p) != 0)
32960 goto invalid_kind;
32961 kind = OMP_CLAUSE_DEFAULT_NONE;
32962 break;
32963
32964 case 'p':
32965 if (strcmp ("present", p) != 0 || !is_oacc)
32966 goto invalid_kind;
32967 kind = OMP_CLAUSE_DEFAULT_PRESENT;
32968 break;
32969
32970 case 's':
32971 if (strcmp ("shared", p) != 0 || is_oacc)
32972 goto invalid_kind;
32973 kind = OMP_CLAUSE_DEFAULT_SHARED;
32974 break;
32975
32976 default:
32977 goto invalid_kind;
32978 }
32979
32980 cp_lexer_consume_token (parser->lexer);
32981 }
32982 else
32983 {
32984 invalid_kind:
32985 if (is_oacc)
32986 cp_parser_error (parser, "expected %<none%> or %<present%>");
32987 else
32988 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32989 }
32990
32991 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32992 || !parens.require_close (parser))
32993 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32994 /*or_comma=*/false,
32995 /*consume_paren=*/true);
32996
32997 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32998 return list;
32999
33000 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
33001 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
33002 OMP_CLAUSE_CHAIN (c) = list;
33003 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
33004
33005 return c;
33006 }
33007
33008 /* OpenMP 3.1:
33009 final ( expression ) */
33010
33011 static tree
33012 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
33013 {
33014 tree t, c;
33015
33016 matching_parens parens;
33017 if (!parens.require_open (parser))
33018 return list;
33019
33020 t = cp_parser_assignment_expression (parser);
33021
33022 if (t == error_mark_node
33023 || !parens.require_close (parser))
33024 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33025 /*or_comma=*/false,
33026 /*consume_paren=*/true);
33027
33028 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
33029
33030 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
33031 OMP_CLAUSE_FINAL_EXPR (c) = t;
33032 OMP_CLAUSE_CHAIN (c) = list;
33033
33034 return c;
33035 }
33036
33037 /* OpenMP 2.5:
33038 if ( expression )
33039
33040 OpenMP 4.5:
33041 if ( directive-name-modifier : expression )
33042
33043 directive-name-modifier:
33044 parallel | task | taskloop | target data | target | target update
33045 | target enter data | target exit data
33046
33047 OpenMP 5.0:
33048 directive-name-modifier:
33049 ... | simd | cancel */
33050
33051 static tree
33052 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
33053 bool is_omp)
33054 {
33055 tree t, c;
33056 enum tree_code if_modifier = ERROR_MARK;
33057
33058 matching_parens parens;
33059 if (!parens.require_open (parser))
33060 return list;
33061
33062 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33063 {
33064 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33065 const char *p = IDENTIFIER_POINTER (id);
33066 int n = 2;
33067
33068 if (strcmp ("cancel", p) == 0)
33069 if_modifier = VOID_CST;
33070 else if (strcmp ("parallel", p) == 0)
33071 if_modifier = OMP_PARALLEL;
33072 else if (strcmp ("simd", p) == 0)
33073 if_modifier = OMP_SIMD;
33074 else if (strcmp ("task", p) == 0)
33075 if_modifier = OMP_TASK;
33076 else if (strcmp ("taskloop", p) == 0)
33077 if_modifier = OMP_TASKLOOP;
33078 else if (strcmp ("target", p) == 0)
33079 {
33080 if_modifier = OMP_TARGET;
33081 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
33082 {
33083 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
33084 p = IDENTIFIER_POINTER (id);
33085 if (strcmp ("data", p) == 0)
33086 if_modifier = OMP_TARGET_DATA;
33087 else if (strcmp ("update", p) == 0)
33088 if_modifier = OMP_TARGET_UPDATE;
33089 else if (strcmp ("enter", p) == 0)
33090 if_modifier = OMP_TARGET_ENTER_DATA;
33091 else if (strcmp ("exit", p) == 0)
33092 if_modifier = OMP_TARGET_EXIT_DATA;
33093 if (if_modifier != OMP_TARGET)
33094 n = 3;
33095 else
33096 {
33097 location_t loc
33098 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
33099 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
33100 "or %<exit%>");
33101 if_modifier = ERROR_MARK;
33102 }
33103 if (if_modifier == OMP_TARGET_ENTER_DATA
33104 || if_modifier == OMP_TARGET_EXIT_DATA)
33105 {
33106 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
33107 {
33108 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
33109 p = IDENTIFIER_POINTER (id);
33110 if (strcmp ("data", p) == 0)
33111 n = 4;
33112 }
33113 if (n != 4)
33114 {
33115 location_t loc
33116 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
33117 error_at (loc, "expected %<data%>");
33118 if_modifier = ERROR_MARK;
33119 }
33120 }
33121 }
33122 }
33123 if (if_modifier != ERROR_MARK)
33124 {
33125 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
33126 {
33127 while (n-- > 0)
33128 cp_lexer_consume_token (parser->lexer);
33129 }
33130 else
33131 {
33132 if (n > 2)
33133 {
33134 location_t loc
33135 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
33136 error_at (loc, "expected %<:%>");
33137 }
33138 if_modifier = ERROR_MARK;
33139 }
33140 }
33141 }
33142
33143 t = cp_parser_assignment_expression (parser);
33144
33145 if (t == error_mark_node
33146 || !parens.require_close (parser))
33147 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33148 /*or_comma=*/false,
33149 /*consume_paren=*/true);
33150
33151 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33152 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
33153 {
33154 if (if_modifier != ERROR_MARK
33155 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33156 {
33157 const char *p = NULL;
33158 switch (if_modifier)
33159 {
33160 case VOID_CST: p = "cancel"; break;
33161 case OMP_PARALLEL: p = "parallel"; break;
33162 case OMP_SIMD: p = "simd"; break;
33163 case OMP_TASK: p = "task"; break;
33164 case OMP_TASKLOOP: p = "taskloop"; break;
33165 case OMP_TARGET_DATA: p = "target data"; break;
33166 case OMP_TARGET: p = "target"; break;
33167 case OMP_TARGET_UPDATE: p = "target update"; break;
33168 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
33169 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
33170 default: gcc_unreachable ();
33171 }
33172 error_at (location, "too many %<if%> clauses with %qs modifier",
33173 p);
33174 return list;
33175 }
33176 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
33177 {
33178 if (!is_omp)
33179 error_at (location, "too many %<if%> clauses");
33180 else
33181 error_at (location, "too many %<if%> clauses without modifier");
33182 return list;
33183 }
33184 else if (if_modifier == ERROR_MARK
33185 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
33186 {
33187 error_at (location, "if any %<if%> clause has modifier, then all "
33188 "%<if%> clauses have to use modifier");
33189 return list;
33190 }
33191 }
33192
33193 c = build_omp_clause (location, OMP_CLAUSE_IF);
33194 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
33195 OMP_CLAUSE_IF_EXPR (c) = t;
33196 OMP_CLAUSE_CHAIN (c) = list;
33197
33198 return c;
33199 }
33200
33201 /* OpenMP 3.1:
33202 mergeable */
33203
33204 static tree
33205 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
33206 tree list, location_t location)
33207 {
33208 tree c;
33209
33210 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
33211 location);
33212
33213 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
33214 OMP_CLAUSE_CHAIN (c) = list;
33215 return c;
33216 }
33217
33218 /* OpenMP 2.5:
33219 nowait */
33220
33221 static tree
33222 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
33223 tree list, location_t location)
33224 {
33225 tree c;
33226
33227 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
33228
33229 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
33230 OMP_CLAUSE_CHAIN (c) = list;
33231 return c;
33232 }
33233
33234 /* OpenMP 2.5:
33235 num_threads ( expression ) */
33236
33237 static tree
33238 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
33239 location_t location)
33240 {
33241 tree t, c;
33242
33243 matching_parens parens;
33244 if (!parens.require_open (parser))
33245 return list;
33246
33247 t = cp_parser_assignment_expression (parser);
33248
33249 if (t == error_mark_node
33250 || !parens.require_close (parser))
33251 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33252 /*or_comma=*/false,
33253 /*consume_paren=*/true);
33254
33255 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
33256 "num_threads", location);
33257
33258 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
33259 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
33260 OMP_CLAUSE_CHAIN (c) = list;
33261
33262 return c;
33263 }
33264
33265 /* OpenMP 4.5:
33266 num_tasks ( expression ) */
33267
33268 static tree
33269 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
33270 location_t location)
33271 {
33272 tree t, c;
33273
33274 matching_parens parens;
33275 if (!parens.require_open (parser))
33276 return list;
33277
33278 t = cp_parser_assignment_expression (parser);
33279
33280 if (t == error_mark_node
33281 || !parens.require_close (parser))
33282 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33283 /*or_comma=*/false,
33284 /*consume_paren=*/true);
33285
33286 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
33287 "num_tasks", location);
33288
33289 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
33290 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
33291 OMP_CLAUSE_CHAIN (c) = list;
33292
33293 return c;
33294 }
33295
33296 /* OpenMP 4.5:
33297 grainsize ( expression ) */
33298
33299 static tree
33300 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
33301 location_t location)
33302 {
33303 tree t, c;
33304
33305 matching_parens parens;
33306 if (!parens.require_open (parser))
33307 return list;
33308
33309 t = cp_parser_assignment_expression (parser);
33310
33311 if (t == error_mark_node
33312 || !parens.require_close (parser))
33313 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33314 /*or_comma=*/false,
33315 /*consume_paren=*/true);
33316
33317 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
33318 "grainsize", location);
33319
33320 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
33321 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
33322 OMP_CLAUSE_CHAIN (c) = list;
33323
33324 return c;
33325 }
33326
33327 /* OpenMP 4.5:
33328 priority ( expression ) */
33329
33330 static tree
33331 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
33332 location_t location)
33333 {
33334 tree t, c;
33335
33336 matching_parens parens;
33337 if (!parens.require_open (parser))
33338 return list;
33339
33340 t = cp_parser_assignment_expression (parser);
33341
33342 if (t == error_mark_node
33343 || !parens.require_close (parser))
33344 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33345 /*or_comma=*/false,
33346 /*consume_paren=*/true);
33347
33348 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
33349 "priority", location);
33350
33351 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
33352 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
33353 OMP_CLAUSE_CHAIN (c) = list;
33354
33355 return c;
33356 }
33357
33358 /* OpenMP 4.5:
33359 hint ( expression ) */
33360
33361 static tree
33362 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
33363 {
33364 tree t, c;
33365
33366 matching_parens parens;
33367 if (!parens.require_open (parser))
33368 return list;
33369
33370 t = cp_parser_assignment_expression (parser);
33371
33372 if (t == error_mark_node
33373 || !parens.require_close (parser))
33374 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33375 /*or_comma=*/false,
33376 /*consume_paren=*/true);
33377
33378 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
33379
33380 c = build_omp_clause (location, OMP_CLAUSE_HINT);
33381 OMP_CLAUSE_HINT_EXPR (c) = t;
33382 OMP_CLAUSE_CHAIN (c) = list;
33383
33384 return c;
33385 }
33386
33387 /* OpenMP 4.5:
33388 defaultmap ( tofrom : scalar )
33389
33390 OpenMP 5.0:
33391 defaultmap ( implicit-behavior [ : variable-category ] ) */
33392
33393 static tree
33394 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
33395 location_t location)
33396 {
33397 tree c, id;
33398 const char *p;
33399 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33400 enum omp_clause_defaultmap_kind category
33401 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
33402
33403 matching_parens parens;
33404 if (!parens.require_open (parser))
33405 return list;
33406
33407 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
33408 p = "default";
33409 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33410 {
33411 invalid_behavior:
33412 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
33413 "%<tofrom%>, %<firstprivate%>, %<none%> "
33414 "or %<default%>");
33415 goto out_err;
33416 }
33417 else
33418 {
33419 id = cp_lexer_peek_token (parser->lexer)->u.value;
33420 p = IDENTIFIER_POINTER (id);
33421 }
33422
33423 switch (p[0])
33424 {
33425 case 'a':
33426 if (strcmp ("alloc", p) == 0)
33427 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
33428 else
33429 goto invalid_behavior;
33430 break;
33431
33432 case 'd':
33433 if (strcmp ("default", p) == 0)
33434 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
33435 else
33436 goto invalid_behavior;
33437 break;
33438
33439 case 'f':
33440 if (strcmp ("firstprivate", p) == 0)
33441 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
33442 else if (strcmp ("from", p) == 0)
33443 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
33444 else
33445 goto invalid_behavior;
33446 break;
33447
33448 case 'n':
33449 if (strcmp ("none", p) == 0)
33450 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
33451 else
33452 goto invalid_behavior;
33453 break;
33454
33455 case 't':
33456 if (strcmp ("tofrom", p) == 0)
33457 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
33458 else if (strcmp ("to", p) == 0)
33459 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
33460 else
33461 goto invalid_behavior;
33462 break;
33463
33464 default:
33465 goto invalid_behavior;
33466 }
33467 cp_lexer_consume_token (parser->lexer);
33468
33469 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
33470 {
33471 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33472 goto out_err;
33473
33474 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33475 {
33476 invalid_category:
33477 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
33478 "%<pointer%>");
33479 goto out_err;
33480 }
33481 id = cp_lexer_peek_token (parser->lexer)->u.value;
33482 p = IDENTIFIER_POINTER (id);
33483
33484 switch (p[0])
33485 {
33486 case 'a':
33487 if (strcmp ("aggregate", p) == 0)
33488 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
33489 else
33490 goto invalid_category;
33491 break;
33492
33493 case 'p':
33494 if (strcmp ("pointer", p) == 0)
33495 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
33496 else
33497 goto invalid_category;
33498 break;
33499
33500 case 's':
33501 if (strcmp ("scalar", p) == 0)
33502 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
33503 else
33504 goto invalid_category;
33505 break;
33506
33507 default:
33508 goto invalid_category;
33509 }
33510
33511 cp_lexer_consume_token (parser->lexer);
33512 }
33513 if (!parens.require_close (parser))
33514 goto out_err;
33515
33516 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
33517 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
33518 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
33519 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
33520 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
33521 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
33522 {
33523 enum omp_clause_defaultmap_kind cat = category;
33524 location_t loc = OMP_CLAUSE_LOCATION (c);
33525 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
33526 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
33527 p = NULL;
33528 switch (cat)
33529 {
33530 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
33531 p = NULL;
33532 break;
33533 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
33534 p = "aggregate";
33535 break;
33536 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
33537 p = "pointer";
33538 break;
33539 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
33540 p = "scalar";
33541 break;
33542 default:
33543 gcc_unreachable ();
33544 }
33545 if (p)
33546 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
33547 p);
33548 else
33549 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
33550 "category");
33551 break;
33552 }
33553
33554 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
33555 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
33556 OMP_CLAUSE_CHAIN (c) = list;
33557 return c;
33558
33559 out_err:
33560 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33561 /*or_comma=*/false,
33562 /*consume_paren=*/true);
33563 return list;
33564 }
33565
33566 /* OpenMP 2.5:
33567 ordered
33568
33569 OpenMP 4.5:
33570 ordered ( constant-expression ) */
33571
33572 static tree
33573 cp_parser_omp_clause_ordered (cp_parser *parser,
33574 tree list, location_t location)
33575 {
33576 tree c, num = NULL_TREE;
33577 HOST_WIDE_INT n;
33578
33579 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
33580 "ordered", location);
33581
33582 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33583 {
33584 matching_parens parens;
33585 parens.consume_open (parser);
33586
33587 num = cp_parser_constant_expression (parser);
33588
33589 if (!parens.require_close (parser))
33590 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33591 /*or_comma=*/false,
33592 /*consume_paren=*/true);
33593
33594 if (num == error_mark_node)
33595 return list;
33596 num = fold_non_dependent_expr (num);
33597 if (!tree_fits_shwi_p (num)
33598 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
33599 || (n = tree_to_shwi (num)) <= 0
33600 || (int) n != n)
33601 {
33602 error_at (location,
33603 "ordered argument needs positive constant integer "
33604 "expression");
33605 return list;
33606 }
33607 }
33608
33609 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
33610 OMP_CLAUSE_ORDERED_EXPR (c) = num;
33611 OMP_CLAUSE_CHAIN (c) = list;
33612 return c;
33613 }
33614
33615 /* OpenMP 2.5:
33616 reduction ( reduction-operator : variable-list )
33617
33618 reduction-operator:
33619 One of: + * - & ^ | && ||
33620
33621 OpenMP 3.1:
33622
33623 reduction-operator:
33624 One of: + * - & ^ | && || min max
33625
33626 OpenMP 4.0:
33627
33628 reduction-operator:
33629 One of: + * - & ^ | && ||
33630 id-expression
33631
33632 OpenMP 5.0:
33633 reduction ( reduction-modifier, reduction-operator : variable-list )
33634 in_reduction ( reduction-operator : variable-list )
33635 task_reduction ( reduction-operator : variable-list ) */
33636
33637 static tree
33638 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
33639 bool is_omp, tree list)
33640 {
33641 enum tree_code code = ERROR_MARK;
33642 tree nlist, c, id = NULL_TREE;
33643 bool task = false;
33644 bool inscan = false;
33645
33646 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33647 return list;
33648
33649 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
33650 {
33651 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
33652 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33653 {
33654 cp_lexer_consume_token (parser->lexer);
33655 cp_lexer_consume_token (parser->lexer);
33656 }
33657 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33658 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
33659 {
33660 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33661 const char *p = IDENTIFIER_POINTER (id);
33662 if (strcmp (p, "task") == 0)
33663 task = true;
33664 else if (strcmp (p, "inscan") == 0)
33665 {
33666 inscan = true;
33667 sorry ("%<inscan%> modifier on %<reduction%> clause "
33668 "not supported yet");
33669 }
33670 if (task || inscan)
33671 {
33672 cp_lexer_consume_token (parser->lexer);
33673 cp_lexer_consume_token (parser->lexer);
33674 }
33675 }
33676 }
33677
33678 switch (cp_lexer_peek_token (parser->lexer)->type)
33679 {
33680 case CPP_PLUS: code = PLUS_EXPR; break;
33681 case CPP_MULT: code = MULT_EXPR; break;
33682 case CPP_MINUS: code = MINUS_EXPR; break;
33683 case CPP_AND: code = BIT_AND_EXPR; break;
33684 case CPP_XOR: code = BIT_XOR_EXPR; break;
33685 case CPP_OR: code = BIT_IOR_EXPR; break;
33686 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
33687 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
33688 default: break;
33689 }
33690
33691 if (code != ERROR_MARK)
33692 cp_lexer_consume_token (parser->lexer);
33693 else
33694 {
33695 bool saved_colon_corrects_to_scope_p;
33696 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
33697 parser->colon_corrects_to_scope_p = false;
33698 id = cp_parser_id_expression (parser, /*template_p=*/false,
33699 /*check_dependency_p=*/true,
33700 /*template_p=*/NULL,
33701 /*declarator_p=*/false,
33702 /*optional_p=*/false);
33703 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
33704 if (identifier_p (id))
33705 {
33706 const char *p = IDENTIFIER_POINTER (id);
33707
33708 if (strcmp (p, "min") == 0)
33709 code = MIN_EXPR;
33710 else if (strcmp (p, "max") == 0)
33711 code = MAX_EXPR;
33712 else if (id == ovl_op_identifier (false, PLUS_EXPR))
33713 code = PLUS_EXPR;
33714 else if (id == ovl_op_identifier (false, MULT_EXPR))
33715 code = MULT_EXPR;
33716 else if (id == ovl_op_identifier (false, MINUS_EXPR))
33717 code = MINUS_EXPR;
33718 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
33719 code = BIT_AND_EXPR;
33720 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
33721 code = BIT_IOR_EXPR;
33722 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
33723 code = BIT_XOR_EXPR;
33724 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
33725 code = TRUTH_ANDIF_EXPR;
33726 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
33727 code = TRUTH_ORIF_EXPR;
33728 id = omp_reduction_id (code, id, NULL_TREE);
33729 tree scope = parser->scope;
33730 if (scope)
33731 id = build_qualified_name (NULL_TREE, scope, id, false);
33732 parser->scope = NULL_TREE;
33733 parser->qualifying_scope = NULL_TREE;
33734 parser->object_scope = NULL_TREE;
33735 }
33736 else
33737 {
33738 error ("invalid reduction-identifier");
33739 resync_fail:
33740 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33741 /*or_comma=*/false,
33742 /*consume_paren=*/true);
33743 return list;
33744 }
33745 }
33746
33747 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33748 goto resync_fail;
33749
33750 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
33751 NULL);
33752 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33753 {
33754 OMP_CLAUSE_REDUCTION_CODE (c) = code;
33755 if (task)
33756 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
33757 else if (inscan)
33758 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
33759 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
33760 }
33761
33762 return nlist;
33763 }
33764
33765 /* OpenMP 2.5:
33766 schedule ( schedule-kind )
33767 schedule ( schedule-kind , expression )
33768
33769 schedule-kind:
33770 static | dynamic | guided | runtime | auto
33771
33772 OpenMP 4.5:
33773 schedule ( schedule-modifier : schedule-kind )
33774 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
33775
33776 schedule-modifier:
33777 simd
33778 monotonic
33779 nonmonotonic */
33780
33781 static tree
33782 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
33783 {
33784 tree c, t;
33785 int modifiers = 0, nmodifiers = 0;
33786
33787 matching_parens parens;
33788 if (!parens.require_open (parser))
33789 return list;
33790
33791 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
33792
33793 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33794 {
33795 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33796 const char *p = IDENTIFIER_POINTER (id);
33797 if (strcmp ("simd", p) == 0)
33798 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
33799 else if (strcmp ("monotonic", p) == 0)
33800 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
33801 else if (strcmp ("nonmonotonic", p) == 0)
33802 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
33803 else
33804 break;
33805 cp_lexer_consume_token (parser->lexer);
33806 if (nmodifiers++ == 0
33807 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33808 cp_lexer_consume_token (parser->lexer);
33809 else
33810 {
33811 cp_parser_require (parser, CPP_COLON, RT_COLON);
33812 break;
33813 }
33814 }
33815
33816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33817 {
33818 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33819 const char *p = IDENTIFIER_POINTER (id);
33820
33821 switch (p[0])
33822 {
33823 case 'd':
33824 if (strcmp ("dynamic", p) != 0)
33825 goto invalid_kind;
33826 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
33827 break;
33828
33829 case 'g':
33830 if (strcmp ("guided", p) != 0)
33831 goto invalid_kind;
33832 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
33833 break;
33834
33835 case 'r':
33836 if (strcmp ("runtime", p) != 0)
33837 goto invalid_kind;
33838 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
33839 break;
33840
33841 default:
33842 goto invalid_kind;
33843 }
33844 }
33845 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33846 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
33847 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
33848 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
33849 else
33850 goto invalid_kind;
33851 cp_lexer_consume_token (parser->lexer);
33852
33853 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
33854 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33855 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
33856 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
33857 {
33858 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
33859 "specified");
33860 modifiers = 0;
33861 }
33862
33863 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33864 {
33865 cp_token *token;
33866 cp_lexer_consume_token (parser->lexer);
33867
33868 token = cp_lexer_peek_token (parser->lexer);
33869 t = cp_parser_assignment_expression (parser);
33870
33871 if (t == error_mark_node)
33872 goto resync_fail;
33873 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
33874 error_at (token->location, "schedule %<runtime%> does not take "
33875 "a %<chunk_size%> parameter");
33876 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
33877 error_at (token->location, "schedule %<auto%> does not take "
33878 "a %<chunk_size%> parameter");
33879 else
33880 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
33881
33882 if (!parens.require_close (parser))
33883 goto resync_fail;
33884 }
33885 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33886 goto resync_fail;
33887
33888 OMP_CLAUSE_SCHEDULE_KIND (c)
33889 = (enum omp_clause_schedule_kind)
33890 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
33891
33892 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
33893 OMP_CLAUSE_CHAIN (c) = list;
33894 return c;
33895
33896 invalid_kind:
33897 cp_parser_error (parser, "invalid schedule kind");
33898 resync_fail:
33899 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33900 /*or_comma=*/false,
33901 /*consume_paren=*/true);
33902 return list;
33903 }
33904
33905 /* OpenMP 3.0:
33906 untied */
33907
33908 static tree
33909 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
33910 tree list, location_t location)
33911 {
33912 tree c;
33913
33914 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
33915
33916 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
33917 OMP_CLAUSE_CHAIN (c) = list;
33918 return c;
33919 }
33920
33921 /* OpenMP 4.0:
33922 inbranch
33923 notinbranch */
33924
33925 static tree
33926 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
33927 tree list, location_t location)
33928 {
33929 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33930 tree c = build_omp_clause (location, code);
33931 OMP_CLAUSE_CHAIN (c) = list;
33932 return c;
33933 }
33934
33935 /* OpenMP 4.0:
33936 parallel
33937 for
33938 sections
33939 taskgroup */
33940
33941 static tree
33942 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
33943 enum omp_clause_code code,
33944 tree list, location_t location)
33945 {
33946 tree c = build_omp_clause (location, code);
33947 OMP_CLAUSE_CHAIN (c) = list;
33948 return c;
33949 }
33950
33951 /* OpenMP 4.5:
33952 nogroup */
33953
33954 static tree
33955 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
33956 tree list, location_t location)
33957 {
33958 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
33959 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
33960 OMP_CLAUSE_CHAIN (c) = list;
33961 return c;
33962 }
33963
33964 /* OpenMP 4.5:
33965 simd
33966 threads */
33967
33968 static tree
33969 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
33970 enum omp_clause_code code,
33971 tree list, location_t location)
33972 {
33973 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
33974 tree c = build_omp_clause (location, code);
33975 OMP_CLAUSE_CHAIN (c) = list;
33976 return c;
33977 }
33978
33979 /* OpenMP 4.0:
33980 num_teams ( expression ) */
33981
33982 static tree
33983 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
33984 location_t location)
33985 {
33986 tree t, c;
33987
33988 matching_parens parens;
33989 if (!parens.require_open (parser))
33990 return list;
33991
33992 t = cp_parser_assignment_expression (parser);
33993
33994 if (t == error_mark_node
33995 || !parens.require_close (parser))
33996 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33997 /*or_comma=*/false,
33998 /*consume_paren=*/true);
33999
34000 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
34001 "num_teams", location);
34002
34003 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
34004 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
34005 OMP_CLAUSE_CHAIN (c) = list;
34006
34007 return c;
34008 }
34009
34010 /* OpenMP 4.0:
34011 thread_limit ( expression ) */
34012
34013 static tree
34014 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
34015 location_t location)
34016 {
34017 tree t, c;
34018
34019 matching_parens parens;
34020 if (!parens.require_open (parser))
34021 return list;
34022
34023 t = cp_parser_assignment_expression (parser);
34024
34025 if (t == error_mark_node
34026 || !parens.require_close (parser))
34027 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34028 /*or_comma=*/false,
34029 /*consume_paren=*/true);
34030
34031 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
34032 "thread_limit", location);
34033
34034 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
34035 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
34036 OMP_CLAUSE_CHAIN (c) = list;
34037
34038 return c;
34039 }
34040
34041 /* OpenMP 4.0:
34042 aligned ( variable-list )
34043 aligned ( variable-list : constant-expression ) */
34044
34045 static tree
34046 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
34047 {
34048 tree nlist, c, alignment = NULL_TREE;
34049 bool colon;
34050
34051 matching_parens parens;
34052 if (!parens.require_open (parser))
34053 return list;
34054
34055 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
34056 &colon);
34057
34058 if (colon)
34059 {
34060 alignment = cp_parser_constant_expression (parser);
34061
34062 if (!parens.require_close (parser))
34063 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34064 /*or_comma=*/false,
34065 /*consume_paren=*/true);
34066
34067 if (alignment == error_mark_node)
34068 alignment = NULL_TREE;
34069 }
34070
34071 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34072 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
34073
34074 return nlist;
34075 }
34076
34077 /* OpenMP 2.5:
34078 lastprivate ( variable-list )
34079
34080 OpenMP 5.0:
34081 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
34082
34083 static tree
34084 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
34085 {
34086 bool conditional = false;
34087
34088 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34089 return list;
34090
34091 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34092 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
34093 {
34094 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34095 const char *p = IDENTIFIER_POINTER (id);
34096
34097 if (strcmp ("conditional", p) == 0)
34098 {
34099 conditional = true;
34100 cp_lexer_consume_token (parser->lexer);
34101 cp_lexer_consume_token (parser->lexer);
34102 }
34103 }
34104
34105 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
34106 list, NULL);
34107
34108 if (conditional)
34109 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34110 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
34111 return nlist;
34112 }
34113
34114 /* OpenMP 4.0:
34115 linear ( variable-list )
34116 linear ( variable-list : expression )
34117
34118 OpenMP 4.5:
34119 linear ( modifier ( variable-list ) )
34120 linear ( modifier ( variable-list ) : expression ) */
34121
34122 static tree
34123 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
34124 bool declare_simd)
34125 {
34126 tree nlist, c, step = integer_one_node;
34127 bool colon;
34128 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
34129
34130 matching_parens parens;
34131 if (!parens.require_open (parser))
34132 return list;
34133
34134 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34135 {
34136 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34137 const char *p = IDENTIFIER_POINTER (id);
34138
34139 if (strcmp ("ref", p) == 0)
34140 kind = OMP_CLAUSE_LINEAR_REF;
34141 else if (strcmp ("val", p) == 0)
34142 kind = OMP_CLAUSE_LINEAR_VAL;
34143 else if (strcmp ("uval", p) == 0)
34144 kind = OMP_CLAUSE_LINEAR_UVAL;
34145 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
34146 cp_lexer_consume_token (parser->lexer);
34147 else
34148 kind = OMP_CLAUSE_LINEAR_DEFAULT;
34149 }
34150
34151 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
34152 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
34153 &colon);
34154 else
34155 {
34156 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
34157 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
34158 if (colon)
34159 cp_parser_require (parser, CPP_COLON, RT_COLON);
34160 else if (!parens.require_close (parser))
34161 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34162 /*or_comma=*/false,
34163 /*consume_paren=*/true);
34164 }
34165
34166 if (colon)
34167 {
34168 step = NULL_TREE;
34169 if (declare_simd
34170 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34171 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
34172 {
34173 cp_token *token = cp_lexer_peek_token (parser->lexer);
34174 cp_parser_parse_tentatively (parser);
34175 step = cp_parser_id_expression (parser, /*template_p=*/false,
34176 /*check_dependency_p=*/true,
34177 /*template_p=*/NULL,
34178 /*declarator_p=*/false,
34179 /*optional_p=*/false);
34180 if (step != error_mark_node)
34181 step = cp_parser_lookup_name_simple (parser, step, token->location);
34182 if (step == error_mark_node)
34183 {
34184 step = NULL_TREE;
34185 cp_parser_abort_tentative_parse (parser);
34186 }
34187 else if (!cp_parser_parse_definitely (parser))
34188 step = NULL_TREE;
34189 }
34190 if (!step)
34191 step = cp_parser_assignment_expression (parser);
34192
34193 if (!parens.require_close (parser))
34194 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34195 /*or_comma=*/false,
34196 /*consume_paren=*/true);
34197
34198 if (step == error_mark_node)
34199 return list;
34200 }
34201
34202 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34203 {
34204 OMP_CLAUSE_LINEAR_STEP (c) = step;
34205 OMP_CLAUSE_LINEAR_KIND (c) = kind;
34206 }
34207
34208 return nlist;
34209 }
34210
34211 /* OpenMP 4.0:
34212 safelen ( constant-expression ) */
34213
34214 static tree
34215 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
34216 location_t location)
34217 {
34218 tree t, c;
34219
34220 matching_parens parens;
34221 if (!parens.require_open (parser))
34222 return list;
34223
34224 t = cp_parser_constant_expression (parser);
34225
34226 if (t == error_mark_node
34227 || !parens.require_close (parser))
34228 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34229 /*or_comma=*/false,
34230 /*consume_paren=*/true);
34231
34232 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
34233
34234 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
34235 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
34236 OMP_CLAUSE_CHAIN (c) = list;
34237
34238 return c;
34239 }
34240
34241 /* OpenMP 4.0:
34242 simdlen ( constant-expression ) */
34243
34244 static tree
34245 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
34246 location_t location)
34247 {
34248 tree t, c;
34249
34250 matching_parens parens;
34251 if (!parens.require_open (parser))
34252 return list;
34253
34254 t = cp_parser_constant_expression (parser);
34255
34256 if (t == error_mark_node
34257 || !parens.require_close (parser))
34258 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34259 /*or_comma=*/false,
34260 /*consume_paren=*/true);
34261
34262 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
34263
34264 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
34265 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
34266 OMP_CLAUSE_CHAIN (c) = list;
34267
34268 return c;
34269 }
34270
34271 /* OpenMP 4.5:
34272 vec:
34273 identifier [+/- integer]
34274 vec , identifier [+/- integer]
34275 */
34276
34277 static tree
34278 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
34279 tree list)
34280 {
34281 tree vec = NULL;
34282
34283 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34284 {
34285 cp_parser_error (parser, "expected identifier");
34286 return list;
34287 }
34288
34289 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34290 {
34291 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
34292 tree t, identifier = cp_parser_identifier (parser);
34293 tree addend = NULL;
34294
34295 if (identifier == error_mark_node)
34296 t = error_mark_node;
34297 else
34298 {
34299 t = cp_parser_lookup_name_simple
34300 (parser, identifier,
34301 cp_lexer_peek_token (parser->lexer)->location);
34302 if (t == error_mark_node)
34303 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
34304 id_loc);
34305 }
34306
34307 bool neg = false;
34308 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
34309 neg = true;
34310 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
34311 {
34312 addend = integer_zero_node;
34313 goto add_to_vector;
34314 }
34315 cp_lexer_consume_token (parser->lexer);
34316
34317 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
34318 {
34319 cp_parser_error (parser, "expected integer");
34320 return list;
34321 }
34322
34323 addend = cp_lexer_peek_token (parser->lexer)->u.value;
34324 if (TREE_CODE (addend) != INTEGER_CST)
34325 {
34326 cp_parser_error (parser, "expected integer");
34327 return list;
34328 }
34329 cp_lexer_consume_token (parser->lexer);
34330
34331 add_to_vector:
34332 if (t != error_mark_node)
34333 {
34334 vec = tree_cons (addend, t, vec);
34335 if (neg)
34336 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
34337 }
34338
34339 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
34340 break;
34341
34342 cp_lexer_consume_token (parser->lexer);
34343 }
34344
34345 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
34346 {
34347 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
34348 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
34349 OMP_CLAUSE_DECL (u) = nreverse (vec);
34350 OMP_CLAUSE_CHAIN (u) = list;
34351 return u;
34352 }
34353 return list;
34354 }
34355
34356 /* OpenMP 5.0:
34357 iterators ( iterators-definition )
34358
34359 iterators-definition:
34360 iterator-specifier
34361 iterator-specifier , iterators-definition
34362
34363 iterator-specifier:
34364 identifier = range-specification
34365 iterator-type identifier = range-specification
34366
34367 range-specification:
34368 begin : end
34369 begin : end : step */
34370
34371 static tree
34372 cp_parser_omp_iterators (cp_parser *parser)
34373 {
34374 tree ret = NULL_TREE, *last = &ret;
34375 cp_lexer_consume_token (parser->lexer);
34376
34377 matching_parens parens;
34378 if (!parens.require_open (parser))
34379 return error_mark_node;
34380
34381 bool saved_colon_corrects_to_scope_p
34382 = parser->colon_corrects_to_scope_p;
34383 bool saved_colon_doesnt_start_class_def_p
34384 = parser->colon_doesnt_start_class_def_p;
34385
34386 do
34387 {
34388 tree iter_type;
34389 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34390 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
34391 iter_type = integer_type_node;
34392 else
34393 {
34394 const char *saved_message
34395 = parser->type_definition_forbidden_message;
34396 parser->type_definition_forbidden_message
34397 = G_("types may not be defined in iterator type");
34398
34399 iter_type = cp_parser_type_id (parser);
34400
34401 parser->type_definition_forbidden_message = saved_message;
34402 }
34403
34404 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34406 {
34407 cp_parser_error (parser, "expected identifier");
34408 break;
34409 }
34410
34411 tree id = cp_parser_identifier (parser);
34412 if (id == error_mark_node)
34413 break;
34414
34415 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34416 break;
34417
34418 parser->colon_corrects_to_scope_p = false;
34419 parser->colon_doesnt_start_class_def_p = true;
34420 tree begin = cp_parser_assignment_expression (parser);
34421
34422 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34423 break;
34424
34425 tree end = cp_parser_assignment_expression (parser);
34426
34427 tree step = integer_one_node;
34428 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
34429 {
34430 cp_lexer_consume_token (parser->lexer);
34431 step = cp_parser_assignment_expression (parser);
34432 }
34433
34434 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
34435 DECL_ARTIFICIAL (iter_var) = 1;
34436 DECL_CONTEXT (iter_var) = current_function_decl;
34437 pushdecl (iter_var);
34438
34439 *last = make_tree_vec (6);
34440 TREE_VEC_ELT (*last, 0) = iter_var;
34441 TREE_VEC_ELT (*last, 1) = begin;
34442 TREE_VEC_ELT (*last, 2) = end;
34443 TREE_VEC_ELT (*last, 3) = step;
34444 last = &TREE_CHAIN (*last);
34445
34446 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34447 {
34448 cp_lexer_consume_token (parser->lexer);
34449 continue;
34450 }
34451 break;
34452 }
34453 while (1);
34454
34455 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
34456 parser->colon_doesnt_start_class_def_p
34457 = saved_colon_doesnt_start_class_def_p;
34458
34459 if (!parens.require_close (parser))
34460 cp_parser_skip_to_closing_parenthesis (parser,
34461 /*recovering=*/true,
34462 /*or_comma=*/false,
34463 /*consume_paren=*/true);
34464
34465 return ret ? ret : error_mark_node;
34466 }
34467
34468 /* OpenMP 4.0:
34469 depend ( depend-kind : variable-list )
34470
34471 depend-kind:
34472 in | out | inout
34473
34474 OpenMP 4.5:
34475 depend ( source )
34476
34477 depend ( sink : vec )
34478
34479 OpenMP 5.0:
34480 depend ( depend-modifier , depend-kind: variable-list )
34481
34482 depend-kind:
34483 in | out | inout | mutexinoutset | depobj
34484
34485 depend-modifier:
34486 iterator ( iterators-definition ) */
34487
34488 static tree
34489 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
34490 {
34491 tree nlist, c, iterators = NULL_TREE;
34492 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
34493
34494 matching_parens parens;
34495 if (!parens.require_open (parser))
34496 return list;
34497
34498 do
34499 {
34500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
34501 goto invalid_kind;
34502
34503 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34504 const char *p = IDENTIFIER_POINTER (id);
34505
34506 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
34507 {
34508 begin_scope (sk_omp, NULL);
34509 iterators = cp_parser_omp_iterators (parser);
34510 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
34511 continue;
34512 }
34513 if (strcmp ("in", p) == 0)
34514 kind = OMP_CLAUSE_DEPEND_IN;
34515 else if (strcmp ("inout", p) == 0)
34516 kind = OMP_CLAUSE_DEPEND_INOUT;
34517 else if (strcmp ("mutexinoutset", p) == 0)
34518 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
34519 else if (strcmp ("out", p) == 0)
34520 kind = OMP_CLAUSE_DEPEND_OUT;
34521 else if (strcmp ("depobj", p) == 0)
34522 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
34523 else if (strcmp ("sink", p) == 0)
34524 kind = OMP_CLAUSE_DEPEND_SINK;
34525 else if (strcmp ("source", p) == 0)
34526 kind = OMP_CLAUSE_DEPEND_SOURCE;
34527 else
34528 goto invalid_kind;
34529 break;
34530 }
34531 while (1);
34532
34533 cp_lexer_consume_token (parser->lexer);
34534
34535 if (iterators
34536 && (kind == OMP_CLAUSE_DEPEND_SOURCE || kind == OMP_CLAUSE_DEPEND_SINK))
34537 {
34538 poplevel (0, 1, 0);
34539 error_at (loc, "%<iterator%> modifier incompatible with %qs",
34540 kind == OMP_CLAUSE_DEPEND_SOURCE ? "source" : "sink");
34541 iterators = NULL_TREE;
34542 }
34543
34544 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
34545 {
34546 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
34547 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34548 OMP_CLAUSE_DECL (c) = NULL_TREE;
34549 OMP_CLAUSE_CHAIN (c) = list;
34550 if (!parens.require_close (parser))
34551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34552 /*or_comma=*/false,
34553 /*consume_paren=*/true);
34554 return c;
34555 }
34556
34557 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
34558 goto resync_fail;
34559
34560 if (kind == OMP_CLAUSE_DEPEND_SINK)
34561 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
34562 else
34563 {
34564 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
34565 list, NULL);
34566
34567 if (iterators)
34568 {
34569 tree block = poplevel (1, 1, 0);
34570 if (iterators == error_mark_node)
34571 iterators = NULL_TREE;
34572 else
34573 TREE_VEC_ELT (iterators, 5) = block;
34574 }
34575
34576 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34577 {
34578 OMP_CLAUSE_DEPEND_KIND (c) = kind;
34579 if (iterators)
34580 OMP_CLAUSE_DECL (c)
34581 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
34582 }
34583 }
34584 return nlist;
34585
34586 invalid_kind:
34587 cp_parser_error (parser, "invalid depend kind");
34588 resync_fail:
34589 if (iterators)
34590 poplevel (0, 1, 0);
34591 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34592 /*or_comma=*/false,
34593 /*consume_paren=*/true);
34594 return list;
34595 }
34596
34597 /* OpenMP 4.0:
34598 map ( map-kind : variable-list )
34599 map ( variable-list )
34600
34601 map-kind:
34602 alloc | to | from | tofrom
34603
34604 OpenMP 4.5:
34605 map-kind:
34606 alloc | to | from | tofrom | release | delete
34607
34608 map ( always [,] map-kind: variable-list ) */
34609
34610 static tree
34611 cp_parser_omp_clause_map (cp_parser *parser, tree list)
34612 {
34613 tree nlist, c;
34614 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
34615 bool always = false;
34616
34617 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34618 return list;
34619
34620 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34621 {
34622 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34623 const char *p = IDENTIFIER_POINTER (id);
34624
34625 if (strcmp ("always", p) == 0)
34626 {
34627 int nth = 2;
34628 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
34629 nth++;
34630 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
34631 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
34632 == RID_DELETE))
34633 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
34634 == CPP_COLON))
34635 {
34636 always = true;
34637 cp_lexer_consume_token (parser->lexer);
34638 if (nth == 3)
34639 cp_lexer_consume_token (parser->lexer);
34640 }
34641 }
34642 }
34643
34644 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
34645 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34646 {
34647 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34648 const char *p = IDENTIFIER_POINTER (id);
34649
34650 if (strcmp ("alloc", p) == 0)
34651 kind = GOMP_MAP_ALLOC;
34652 else if (strcmp ("to", p) == 0)
34653 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
34654 else if (strcmp ("from", p) == 0)
34655 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
34656 else if (strcmp ("tofrom", p) == 0)
34657 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
34658 else if (strcmp ("release", p) == 0)
34659 kind = GOMP_MAP_RELEASE;
34660 else
34661 {
34662 cp_parser_error (parser, "invalid map kind");
34663 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34664 /*or_comma=*/false,
34665 /*consume_paren=*/true);
34666 return list;
34667 }
34668 cp_lexer_consume_token (parser->lexer);
34669 cp_lexer_consume_token (parser->lexer);
34670 }
34671 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
34672 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
34673 {
34674 kind = GOMP_MAP_DELETE;
34675 cp_lexer_consume_token (parser->lexer);
34676 cp_lexer_consume_token (parser->lexer);
34677 }
34678
34679 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
34680 NULL);
34681
34682 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
34683 OMP_CLAUSE_SET_MAP_KIND (c, kind);
34684
34685 return nlist;
34686 }
34687
34688 /* OpenMP 4.0:
34689 device ( expression ) */
34690
34691 static tree
34692 cp_parser_omp_clause_device (cp_parser *parser, tree list,
34693 location_t location)
34694 {
34695 tree t, c;
34696
34697 matching_parens parens;
34698 if (!parens.require_open (parser))
34699 return list;
34700
34701 t = cp_parser_assignment_expression (parser);
34702
34703 if (t == error_mark_node
34704 || !parens.require_close (parser))
34705 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34706 /*or_comma=*/false,
34707 /*consume_paren=*/true);
34708
34709 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
34710 "device", location);
34711
34712 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
34713 OMP_CLAUSE_DEVICE_ID (c) = t;
34714 OMP_CLAUSE_CHAIN (c) = list;
34715
34716 return c;
34717 }
34718
34719 /* OpenMP 4.0:
34720 dist_schedule ( static )
34721 dist_schedule ( static , expression ) */
34722
34723 static tree
34724 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
34725 location_t location)
34726 {
34727 tree c, t;
34728
34729 matching_parens parens;
34730 if (!parens.require_open (parser))
34731 return list;
34732
34733 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
34734
34735 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
34736 goto invalid_kind;
34737 cp_lexer_consume_token (parser->lexer);
34738
34739 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34740 {
34741 cp_lexer_consume_token (parser->lexer);
34742
34743 t = cp_parser_assignment_expression (parser);
34744
34745 if (t == error_mark_node)
34746 goto resync_fail;
34747 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
34748
34749 if (!parens.require_close (parser))
34750 goto resync_fail;
34751 }
34752 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34753 goto resync_fail;
34754
34755 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
34756 location);
34757 OMP_CLAUSE_CHAIN (c) = list;
34758 return c;
34759
34760 invalid_kind:
34761 cp_parser_error (parser, "invalid dist_schedule kind");
34762 resync_fail:
34763 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34764 /*or_comma=*/false,
34765 /*consume_paren=*/true);
34766 return list;
34767 }
34768
34769 /* OpenMP 4.0:
34770 proc_bind ( proc-bind-kind )
34771
34772 proc-bind-kind:
34773 master | close | spread */
34774
34775 static tree
34776 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
34777 location_t location)
34778 {
34779 tree c;
34780 enum omp_clause_proc_bind_kind kind;
34781
34782 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34783 return list;
34784
34785 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34786 {
34787 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34788 const char *p = IDENTIFIER_POINTER (id);
34789
34790 if (strcmp ("master", p) == 0)
34791 kind = OMP_CLAUSE_PROC_BIND_MASTER;
34792 else if (strcmp ("close", p) == 0)
34793 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
34794 else if (strcmp ("spread", p) == 0)
34795 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
34796 else
34797 goto invalid_kind;
34798 }
34799 else
34800 goto invalid_kind;
34801
34802 cp_lexer_consume_token (parser->lexer);
34803 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
34804 goto resync_fail;
34805
34806 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
34807 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
34808 location);
34809 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
34810 OMP_CLAUSE_CHAIN (c) = list;
34811 return c;
34812
34813 invalid_kind:
34814 cp_parser_error (parser, "invalid depend kind");
34815 resync_fail:
34816 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34817 /*or_comma=*/false,
34818 /*consume_paren=*/true);
34819 return list;
34820 }
34821
34822 /* OpenACC:
34823 async [( int-expr )] */
34824
34825 static tree
34826 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
34827 {
34828 tree c, t;
34829 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34830
34831 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
34832
34833 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
34834 {
34835 matching_parens parens;
34836 parens.consume_open (parser);
34837
34838 t = cp_parser_expression (parser);
34839 if (t == error_mark_node
34840 || !parens.require_close (parser))
34841 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34842 /*or_comma=*/false,
34843 /*consume_paren=*/true);
34844 }
34845
34846 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
34847
34848 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
34849 OMP_CLAUSE_ASYNC_EXPR (c) = t;
34850 OMP_CLAUSE_CHAIN (c) = list;
34851 list = c;
34852
34853 return list;
34854 }
34855
34856 /* Parse all OpenACC clauses. The set clauses allowed by the directive
34857 is a bitmask in MASK. Return the list of clauses found. */
34858
34859 static tree
34860 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
34861 const char *where, cp_token *pragma_tok,
34862 bool finish_p = true)
34863 {
34864 tree clauses = NULL;
34865 bool first = true;
34866
34867 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
34868 {
34869 location_t here;
34870 pragma_omp_clause c_kind;
34871 omp_clause_code code;
34872 const char *c_name;
34873 tree prev = clauses;
34874
34875 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34876 cp_lexer_consume_token (parser->lexer);
34877
34878 here = cp_lexer_peek_token (parser->lexer)->location;
34879 c_kind = cp_parser_omp_clause_name (parser);
34880
34881 switch (c_kind)
34882 {
34883 case PRAGMA_OACC_CLAUSE_ASYNC:
34884 clauses = cp_parser_oacc_clause_async (parser, clauses);
34885 c_name = "async";
34886 break;
34887 case PRAGMA_OACC_CLAUSE_AUTO:
34888 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
34889 clauses);
34890 c_name = "auto";
34891 break;
34892 case PRAGMA_OACC_CLAUSE_COLLAPSE:
34893 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
34894 c_name = "collapse";
34895 break;
34896 case PRAGMA_OACC_CLAUSE_COPY:
34897 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34898 c_name = "copy";
34899 break;
34900 case PRAGMA_OACC_CLAUSE_COPYIN:
34901 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34902 c_name = "copyin";
34903 break;
34904 case PRAGMA_OACC_CLAUSE_COPYOUT:
34905 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34906 c_name = "copyout";
34907 break;
34908 case PRAGMA_OACC_CLAUSE_CREATE:
34909 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34910 c_name = "create";
34911 break;
34912 case PRAGMA_OACC_CLAUSE_DELETE:
34913 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34914 c_name = "delete";
34915 break;
34916 case PRAGMA_OMP_CLAUSE_DEFAULT:
34917 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
34918 c_name = "default";
34919 break;
34920 case PRAGMA_OACC_CLAUSE_DEVICE:
34921 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34922 c_name = "device";
34923 break;
34924 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
34925 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
34926 c_name = "deviceptr";
34927 break;
34928 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
34929 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34930 c_name = "device_resident";
34931 break;
34932 case PRAGMA_OACC_CLAUSE_FINALIZE:
34933 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
34934 clauses);
34935 c_name = "finalize";
34936 break;
34937 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
34938 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
34939 clauses);
34940 c_name = "firstprivate";
34941 break;
34942 case PRAGMA_OACC_CLAUSE_GANG:
34943 c_name = "gang";
34944 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
34945 c_name, clauses);
34946 break;
34947 case PRAGMA_OACC_CLAUSE_HOST:
34948 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34949 c_name = "host";
34950 break;
34951 case PRAGMA_OACC_CLAUSE_IF:
34952 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
34953 c_name = "if";
34954 break;
34955 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
34956 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
34957 clauses);
34958 c_name = "if_present";
34959 break;
34960 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
34961 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
34962 clauses);
34963 c_name = "independent";
34964 break;
34965 case PRAGMA_OACC_CLAUSE_LINK:
34966 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34967 c_name = "link";
34968 break;
34969 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
34970 code = OMP_CLAUSE_NUM_GANGS;
34971 c_name = "num_gangs";
34972 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34973 clauses);
34974 break;
34975 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
34976 c_name = "num_workers";
34977 code = OMP_CLAUSE_NUM_WORKERS;
34978 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
34979 clauses);
34980 break;
34981 case PRAGMA_OACC_CLAUSE_PRESENT:
34982 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
34983 c_name = "present";
34984 break;
34985 case PRAGMA_OACC_CLAUSE_PRIVATE:
34986 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
34987 clauses);
34988 c_name = "private";
34989 break;
34990 case PRAGMA_OACC_CLAUSE_REDUCTION:
34991 clauses
34992 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
34993 false, clauses);
34994 c_name = "reduction";
34995 break;
34996 case PRAGMA_OACC_CLAUSE_SEQ:
34997 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
34998 clauses);
34999 c_name = "seq";
35000 break;
35001 case PRAGMA_OACC_CLAUSE_TILE:
35002 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
35003 c_name = "tile";
35004 break;
35005 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
35006 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35007 clauses);
35008 c_name = "use_device";
35009 break;
35010 case PRAGMA_OACC_CLAUSE_VECTOR:
35011 c_name = "vector";
35012 clauses = cp_parser_oacc_shape_clause (parser, here,
35013 OMP_CLAUSE_VECTOR,
35014 c_name, clauses);
35015 break;
35016 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
35017 c_name = "vector_length";
35018 code = OMP_CLAUSE_VECTOR_LENGTH;
35019 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
35020 clauses);
35021 break;
35022 case PRAGMA_OACC_CLAUSE_WAIT:
35023 clauses = cp_parser_oacc_clause_wait (parser, clauses);
35024 c_name = "wait";
35025 break;
35026 case PRAGMA_OACC_CLAUSE_WORKER:
35027 c_name = "worker";
35028 clauses = cp_parser_oacc_shape_clause (parser, here,
35029 OMP_CLAUSE_WORKER,
35030 c_name, clauses);
35031 break;
35032 default:
35033 cp_parser_error (parser, "expected %<#pragma acc%> clause");
35034 goto saw_error;
35035 }
35036
35037 first = false;
35038
35039 if (((mask >> c_kind) & 1) == 0)
35040 {
35041 /* Remove the invalid clause(s) from the list to avoid
35042 confusing the rest of the compiler. */
35043 clauses = prev;
35044 error_at (here, "%qs is not valid for %qs", c_name, where);
35045 }
35046 }
35047
35048 saw_error:
35049 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35050
35051 if (finish_p)
35052 return finish_omp_clauses (clauses, C_ORT_ACC);
35053
35054 return clauses;
35055 }
35056
35057 /* Parse all OpenMP clauses. The set clauses allowed by the directive
35058 is a bitmask in MASK. Return the list of clauses found; the result
35059 of clause default goes in *pdefault. */
35060
35061 static tree
35062 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
35063 const char *where, cp_token *pragma_tok,
35064 bool finish_p = true)
35065 {
35066 tree clauses = NULL;
35067 bool first = true;
35068 cp_token *token = NULL;
35069
35070 /* Don't create location wrapper nodes within OpenMP clauses. */
35071 auto_suppress_location_wrappers sentinel;
35072
35073 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35074 {
35075 pragma_omp_clause c_kind;
35076 const char *c_name;
35077 tree prev = clauses;
35078
35079 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35080 cp_lexer_consume_token (parser->lexer);
35081
35082 token = cp_lexer_peek_token (parser->lexer);
35083 c_kind = cp_parser_omp_clause_name (parser);
35084
35085 switch (c_kind)
35086 {
35087 case PRAGMA_OMP_CLAUSE_COLLAPSE:
35088 clauses = cp_parser_omp_clause_collapse (parser, clauses,
35089 token->location);
35090 c_name = "collapse";
35091 break;
35092 case PRAGMA_OMP_CLAUSE_COPYIN:
35093 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
35094 c_name = "copyin";
35095 break;
35096 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
35097 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
35098 clauses);
35099 c_name = "copyprivate";
35100 break;
35101 case PRAGMA_OMP_CLAUSE_DEFAULT:
35102 clauses = cp_parser_omp_clause_default (parser, clauses,
35103 token->location, false);
35104 c_name = "default";
35105 break;
35106 case PRAGMA_OMP_CLAUSE_FINAL:
35107 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
35108 c_name = "final";
35109 break;
35110 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
35111 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
35112 clauses);
35113 c_name = "firstprivate";
35114 break;
35115 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
35116 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
35117 token->location);
35118 c_name = "grainsize";
35119 break;
35120 case PRAGMA_OMP_CLAUSE_HINT:
35121 clauses = cp_parser_omp_clause_hint (parser, clauses,
35122 token->location);
35123 c_name = "hint";
35124 break;
35125 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
35126 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
35127 token->location);
35128 c_name = "defaultmap";
35129 break;
35130 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
35131 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
35132 clauses);
35133 c_name = "use_device_ptr";
35134 break;
35135 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
35136 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
35137 clauses);
35138 c_name = "is_device_ptr";
35139 break;
35140 case PRAGMA_OMP_CLAUSE_IF:
35141 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
35142 true);
35143 c_name = "if";
35144 break;
35145 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
35146 clauses
35147 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
35148 true, clauses);
35149 c_name = "in_reduction";
35150 break;
35151 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
35152 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
35153 c_name = "lastprivate";
35154 break;
35155 case PRAGMA_OMP_CLAUSE_MERGEABLE:
35156 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
35157 token->location);
35158 c_name = "mergeable";
35159 break;
35160 case PRAGMA_OMP_CLAUSE_NOWAIT:
35161 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
35162 c_name = "nowait";
35163 break;
35164 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
35165 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
35166 token->location);
35167 c_name = "num_tasks";
35168 break;
35169 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
35170 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
35171 token->location);
35172 c_name = "num_threads";
35173 break;
35174 case PRAGMA_OMP_CLAUSE_ORDERED:
35175 clauses = cp_parser_omp_clause_ordered (parser, clauses,
35176 token->location);
35177 c_name = "ordered";
35178 break;
35179 case PRAGMA_OMP_CLAUSE_PRIORITY:
35180 clauses = cp_parser_omp_clause_priority (parser, clauses,
35181 token->location);
35182 c_name = "priority";
35183 break;
35184 case PRAGMA_OMP_CLAUSE_PRIVATE:
35185 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
35186 clauses);
35187 c_name = "private";
35188 break;
35189 case PRAGMA_OMP_CLAUSE_REDUCTION:
35190 clauses
35191 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
35192 true, clauses);
35193 c_name = "reduction";
35194 break;
35195 case PRAGMA_OMP_CLAUSE_SCHEDULE:
35196 clauses = cp_parser_omp_clause_schedule (parser, clauses,
35197 token->location);
35198 c_name = "schedule";
35199 break;
35200 case PRAGMA_OMP_CLAUSE_SHARED:
35201 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
35202 clauses);
35203 c_name = "shared";
35204 break;
35205 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
35206 clauses
35207 = cp_parser_omp_clause_reduction (parser,
35208 OMP_CLAUSE_TASK_REDUCTION,
35209 true, clauses);
35210 c_name = "task_reduction";
35211 break;
35212 case PRAGMA_OMP_CLAUSE_UNTIED:
35213 clauses = cp_parser_omp_clause_untied (parser, clauses,
35214 token->location);
35215 c_name = "untied";
35216 break;
35217 case PRAGMA_OMP_CLAUSE_INBRANCH:
35218 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
35219 clauses, token->location);
35220 c_name = "inbranch";
35221 break;
35222 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
35223 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
35224 clauses);
35225 c_name = "nontemporal";
35226 break;
35227 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
35228 clauses = cp_parser_omp_clause_branch (parser,
35229 OMP_CLAUSE_NOTINBRANCH,
35230 clauses, token->location);
35231 c_name = "notinbranch";
35232 break;
35233 case PRAGMA_OMP_CLAUSE_PARALLEL:
35234 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
35235 clauses, token->location);
35236 c_name = "parallel";
35237 if (!first)
35238 {
35239 clause_not_first:
35240 error_at (token->location, "%qs must be the first clause of %qs",
35241 c_name, where);
35242 clauses = prev;
35243 }
35244 break;
35245 case PRAGMA_OMP_CLAUSE_FOR:
35246 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
35247 clauses, token->location);
35248 c_name = "for";
35249 if (!first)
35250 goto clause_not_first;
35251 break;
35252 case PRAGMA_OMP_CLAUSE_SECTIONS:
35253 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
35254 clauses, token->location);
35255 c_name = "sections";
35256 if (!first)
35257 goto clause_not_first;
35258 break;
35259 case PRAGMA_OMP_CLAUSE_TASKGROUP:
35260 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
35261 clauses, token->location);
35262 c_name = "taskgroup";
35263 if (!first)
35264 goto clause_not_first;
35265 break;
35266 case PRAGMA_OMP_CLAUSE_LINK:
35267 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
35268 c_name = "to";
35269 break;
35270 case PRAGMA_OMP_CLAUSE_TO:
35271 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
35272 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35273 clauses);
35274 else
35275 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
35276 c_name = "to";
35277 break;
35278 case PRAGMA_OMP_CLAUSE_FROM:
35279 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
35280 c_name = "from";
35281 break;
35282 case PRAGMA_OMP_CLAUSE_UNIFORM:
35283 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
35284 clauses);
35285 c_name = "uniform";
35286 break;
35287 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
35288 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
35289 token->location);
35290 c_name = "num_teams";
35291 break;
35292 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
35293 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
35294 token->location);
35295 c_name = "thread_limit";
35296 break;
35297 case PRAGMA_OMP_CLAUSE_ALIGNED:
35298 clauses = cp_parser_omp_clause_aligned (parser, clauses);
35299 c_name = "aligned";
35300 break;
35301 case PRAGMA_OMP_CLAUSE_LINEAR:
35302 {
35303 bool declare_simd = false;
35304 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
35305 declare_simd = true;
35306 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
35307 }
35308 c_name = "linear";
35309 break;
35310 case PRAGMA_OMP_CLAUSE_DEPEND:
35311 clauses = cp_parser_omp_clause_depend (parser, clauses,
35312 token->location);
35313 c_name = "depend";
35314 break;
35315 case PRAGMA_OMP_CLAUSE_MAP:
35316 clauses = cp_parser_omp_clause_map (parser, clauses);
35317 c_name = "map";
35318 break;
35319 case PRAGMA_OMP_CLAUSE_DEVICE:
35320 clauses = cp_parser_omp_clause_device (parser, clauses,
35321 token->location);
35322 c_name = "device";
35323 break;
35324 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
35325 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
35326 token->location);
35327 c_name = "dist_schedule";
35328 break;
35329 case PRAGMA_OMP_CLAUSE_PROC_BIND:
35330 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
35331 token->location);
35332 c_name = "proc_bind";
35333 break;
35334 case PRAGMA_OMP_CLAUSE_SAFELEN:
35335 clauses = cp_parser_omp_clause_safelen (parser, clauses,
35336 token->location);
35337 c_name = "safelen";
35338 break;
35339 case PRAGMA_OMP_CLAUSE_SIMDLEN:
35340 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
35341 token->location);
35342 c_name = "simdlen";
35343 break;
35344 case PRAGMA_OMP_CLAUSE_NOGROUP:
35345 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
35346 token->location);
35347 c_name = "nogroup";
35348 break;
35349 case PRAGMA_OMP_CLAUSE_THREADS:
35350 clauses
35351 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
35352 clauses, token->location);
35353 c_name = "threads";
35354 break;
35355 case PRAGMA_OMP_CLAUSE_SIMD:
35356 clauses
35357 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
35358 clauses, token->location);
35359 c_name = "simd";
35360 break;
35361 default:
35362 cp_parser_error (parser, "expected %<#pragma omp%> clause");
35363 goto saw_error;
35364 }
35365
35366 first = false;
35367
35368 if (((mask >> c_kind) & 1) == 0)
35369 {
35370 /* Remove the invalid clause(s) from the list to avoid
35371 confusing the rest of the compiler. */
35372 clauses = prev;
35373 error_at (token->location, "%qs is not valid for %qs", c_name, where);
35374 }
35375 }
35376 saw_error:
35377 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35378 if (finish_p)
35379 {
35380 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
35381 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
35382 else
35383 return finish_omp_clauses (clauses, C_ORT_OMP);
35384 }
35385 return clauses;
35386 }
35387
35388 /* OpenMP 2.5:
35389 structured-block:
35390 statement
35391
35392 In practice, we're also interested in adding the statement to an
35393 outer node. So it is convenient if we work around the fact that
35394 cp_parser_statement calls add_stmt. */
35395
35396 static unsigned
35397 cp_parser_begin_omp_structured_block (cp_parser *parser)
35398 {
35399 unsigned save = parser->in_statement;
35400
35401 /* Only move the values to IN_OMP_BLOCK if they weren't false.
35402 This preserves the "not within loop or switch" style error messages
35403 for nonsense cases like
35404 void foo() {
35405 #pragma omp single
35406 break;
35407 }
35408 */
35409 if (parser->in_statement)
35410 parser->in_statement = IN_OMP_BLOCK;
35411
35412 return save;
35413 }
35414
35415 static void
35416 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
35417 {
35418 parser->in_statement = save;
35419 }
35420
35421 static tree
35422 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
35423 {
35424 tree stmt = begin_omp_structured_block ();
35425 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35426
35427 cp_parser_statement (parser, NULL_TREE, false, if_p);
35428
35429 cp_parser_end_omp_structured_block (parser, save);
35430 return finish_omp_structured_block (stmt);
35431 }
35432
35433 /* OpenMP 2.5:
35434 # pragma omp atomic new-line
35435 expression-stmt
35436
35437 expression-stmt:
35438 x binop= expr | x++ | ++x | x-- | --x
35439 binop:
35440 +, *, -, /, &, ^, |, <<, >>
35441
35442 where x is an lvalue expression with scalar type.
35443
35444 OpenMP 3.1:
35445 # pragma omp atomic new-line
35446 update-stmt
35447
35448 # pragma omp atomic read new-line
35449 read-stmt
35450
35451 # pragma omp atomic write new-line
35452 write-stmt
35453
35454 # pragma omp atomic update new-line
35455 update-stmt
35456
35457 # pragma omp atomic capture new-line
35458 capture-stmt
35459
35460 # pragma omp atomic capture new-line
35461 capture-block
35462
35463 read-stmt:
35464 v = x
35465 write-stmt:
35466 x = expr
35467 update-stmt:
35468 expression-stmt | x = x binop expr
35469 capture-stmt:
35470 v = expression-stmt
35471 capture-block:
35472 { v = x; update-stmt; } | { update-stmt; v = x; }
35473
35474 OpenMP 4.0:
35475 update-stmt:
35476 expression-stmt | x = x binop expr | x = expr binop x
35477 capture-stmt:
35478 v = update-stmt
35479 capture-block:
35480 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
35481
35482 where x and v are lvalue expressions with scalar type. */
35483
35484 static void
35485 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
35486 {
35487 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
35488 tree rhs1 = NULL_TREE, orig_lhs;
35489 location_t loc = pragma_tok->location;
35490 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
35491 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
35492 bool structured_block = false;
35493 bool first = true;
35494 tree clauses = NULL_TREE;
35495
35496 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35497 {
35498 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35499 cp_lexer_consume_token (parser->lexer);
35500
35501 first = false;
35502
35503 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35504 {
35505 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35506 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
35507 const char *p = IDENTIFIER_POINTER (id);
35508 enum tree_code new_code = ERROR_MARK;
35509 enum omp_memory_order new_memory_order
35510 = OMP_MEMORY_ORDER_UNSPECIFIED;
35511
35512 if (!strcmp (p, "read"))
35513 new_code = OMP_ATOMIC_READ;
35514 else if (!strcmp (p, "write"))
35515 new_code = NOP_EXPR;
35516 else if (!strcmp (p, "update"))
35517 new_code = OMP_ATOMIC;
35518 else if (!strcmp (p, "capture"))
35519 new_code = OMP_ATOMIC_CAPTURE_NEW;
35520 else if (!strcmp (p, "seq_cst"))
35521 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35522 else if (!strcmp (p, "acq_rel"))
35523 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35524 else if (!strcmp (p, "release"))
35525 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
35526 else if (!strcmp (p, "acquire"))
35527 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35528 else if (!strcmp (p, "relaxed"))
35529 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
35530 else if (!strcmp (p, "hint"))
35531 {
35532 cp_lexer_consume_token (parser->lexer);
35533 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
35534 continue;
35535 }
35536 else
35537 {
35538 p = NULL;
35539 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
35540 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
35541 "%<release%>, %<relaxed%> or %<hint%> clause");
35542 }
35543 if (p)
35544 {
35545 if (new_code != ERROR_MARK)
35546 {
35547 if (code != ERROR_MARK)
35548 error_at (cloc, "too many atomic clauses");
35549 else
35550 code = new_code;
35551 }
35552 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35553 {
35554 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
35555 error_at (cloc, "too many memory order clauses");
35556 else
35557 memory_order = new_memory_order;
35558 }
35559 cp_lexer_consume_token (parser->lexer);
35560 continue;
35561 }
35562 }
35563 break;
35564 }
35565 cp_parser_require_pragma_eol (parser, pragma_tok);
35566
35567 if (code == ERROR_MARK)
35568 code = OMP_ATOMIC;
35569 if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
35570 {
35571 omp_requires_mask
35572 = (enum omp_requires) (omp_requires_mask
35573 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
35574 switch ((enum omp_memory_order)
35575 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
35576 {
35577 case OMP_MEMORY_ORDER_UNSPECIFIED:
35578 case OMP_MEMORY_ORDER_RELAXED:
35579 memory_order = OMP_MEMORY_ORDER_RELAXED;
35580 break;
35581 case OMP_MEMORY_ORDER_SEQ_CST:
35582 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35583 break;
35584 case OMP_MEMORY_ORDER_ACQ_REL:
35585 switch (code)
35586 {
35587 case OMP_ATOMIC_READ:
35588 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
35589 break;
35590 case NOP_EXPR: /* atomic write */
35591 case OMP_ATOMIC:
35592 memory_order = OMP_MEMORY_ORDER_RELEASE;
35593 break;
35594 default:
35595 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
35596 break;
35597 }
35598 break;
35599 default:
35600 gcc_unreachable ();
35601 }
35602 }
35603 else
35604 switch (code)
35605 {
35606 case OMP_ATOMIC_READ:
35607 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35608 || memory_order == OMP_MEMORY_ORDER_RELEASE)
35609 {
35610 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
35611 "%<acq_rel%> or %<release%> clauses");
35612 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35613 }
35614 break;
35615 case NOP_EXPR: /* atomic write */
35616 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35617 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35618 {
35619 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
35620 "%<acq_rel%> or %<acquire%> clauses");
35621 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35622 }
35623 break;
35624 case OMP_ATOMIC:
35625 if (memory_order == OMP_MEMORY_ORDER_ACQ_REL
35626 || memory_order == OMP_MEMORY_ORDER_ACQUIRE)
35627 {
35628 error_at (loc, "%<#pragma omp atomic update%> incompatible with "
35629 "%<acq_rel%> or %<acquire%> clauses");
35630 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
35631 }
35632 break;
35633 default:
35634 break;
35635 }
35636
35637 switch (code)
35638 {
35639 case OMP_ATOMIC_READ:
35640 case NOP_EXPR: /* atomic write */
35641 v = cp_parser_unary_expression (parser);
35642 if (v == error_mark_node)
35643 goto saw_error;
35644 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35645 goto saw_error;
35646 if (code == NOP_EXPR)
35647 lhs = cp_parser_expression (parser);
35648 else
35649 lhs = cp_parser_unary_expression (parser);
35650 if (lhs == error_mark_node)
35651 goto saw_error;
35652 if (code == NOP_EXPR)
35653 {
35654 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
35655 opcode. */
35656 code = OMP_ATOMIC;
35657 rhs = lhs;
35658 lhs = v;
35659 v = NULL_TREE;
35660 }
35661 goto done;
35662 case OMP_ATOMIC_CAPTURE_NEW:
35663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35664 {
35665 cp_lexer_consume_token (parser->lexer);
35666 structured_block = true;
35667 }
35668 else
35669 {
35670 v = cp_parser_unary_expression (parser);
35671 if (v == error_mark_node)
35672 goto saw_error;
35673 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35674 goto saw_error;
35675 }
35676 default:
35677 break;
35678 }
35679
35680 restart:
35681 lhs = cp_parser_unary_expression (parser);
35682 orig_lhs = lhs;
35683 switch (TREE_CODE (lhs))
35684 {
35685 case ERROR_MARK:
35686 goto saw_error;
35687
35688 case POSTINCREMENT_EXPR:
35689 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35690 code = OMP_ATOMIC_CAPTURE_OLD;
35691 /* FALLTHROUGH */
35692 case PREINCREMENT_EXPR:
35693 lhs = TREE_OPERAND (lhs, 0);
35694 opcode = PLUS_EXPR;
35695 rhs = integer_one_node;
35696 break;
35697
35698 case POSTDECREMENT_EXPR:
35699 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
35700 code = OMP_ATOMIC_CAPTURE_OLD;
35701 /* FALLTHROUGH */
35702 case PREDECREMENT_EXPR:
35703 lhs = TREE_OPERAND (lhs, 0);
35704 opcode = MINUS_EXPR;
35705 rhs = integer_one_node;
35706 break;
35707
35708 case COMPOUND_EXPR:
35709 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
35710 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
35711 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
35712 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
35713 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
35714 (TREE_OPERAND (lhs, 1), 0), 0)))
35715 == BOOLEAN_TYPE)
35716 /* Undo effects of boolean_increment for post {in,de}crement. */
35717 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
35718 /* FALLTHRU */
35719 case MODIFY_EXPR:
35720 if (TREE_CODE (lhs) == MODIFY_EXPR
35721 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
35722 {
35723 /* Undo effects of boolean_increment. */
35724 if (integer_onep (TREE_OPERAND (lhs, 1)))
35725 {
35726 /* This is pre or post increment. */
35727 rhs = TREE_OPERAND (lhs, 1);
35728 lhs = TREE_OPERAND (lhs, 0);
35729 opcode = NOP_EXPR;
35730 if (code == OMP_ATOMIC_CAPTURE_NEW
35731 && !structured_block
35732 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
35733 code = OMP_ATOMIC_CAPTURE_OLD;
35734 break;
35735 }
35736 }
35737 /* FALLTHRU */
35738 default:
35739 switch (cp_lexer_peek_token (parser->lexer)->type)
35740 {
35741 case CPP_MULT_EQ:
35742 opcode = MULT_EXPR;
35743 break;
35744 case CPP_DIV_EQ:
35745 opcode = TRUNC_DIV_EXPR;
35746 break;
35747 case CPP_PLUS_EQ:
35748 opcode = PLUS_EXPR;
35749 break;
35750 case CPP_MINUS_EQ:
35751 opcode = MINUS_EXPR;
35752 break;
35753 case CPP_LSHIFT_EQ:
35754 opcode = LSHIFT_EXPR;
35755 break;
35756 case CPP_RSHIFT_EQ:
35757 opcode = RSHIFT_EXPR;
35758 break;
35759 case CPP_AND_EQ:
35760 opcode = BIT_AND_EXPR;
35761 break;
35762 case CPP_OR_EQ:
35763 opcode = BIT_IOR_EXPR;
35764 break;
35765 case CPP_XOR_EQ:
35766 opcode = BIT_XOR_EXPR;
35767 break;
35768 case CPP_EQ:
35769 enum cp_parser_prec oprec;
35770 cp_token *token;
35771 cp_lexer_consume_token (parser->lexer);
35772 cp_parser_parse_tentatively (parser);
35773 rhs1 = cp_parser_simple_cast_expression (parser);
35774 if (rhs1 == error_mark_node)
35775 {
35776 cp_parser_abort_tentative_parse (parser);
35777 cp_parser_simple_cast_expression (parser);
35778 goto saw_error;
35779 }
35780 token = cp_lexer_peek_token (parser->lexer);
35781 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
35782 {
35783 cp_parser_abort_tentative_parse (parser);
35784 cp_parser_parse_tentatively (parser);
35785 rhs = cp_parser_binary_expression (parser, false, true,
35786 PREC_NOT_OPERATOR, NULL);
35787 if (rhs == error_mark_node)
35788 {
35789 cp_parser_abort_tentative_parse (parser);
35790 cp_parser_binary_expression (parser, false, true,
35791 PREC_NOT_OPERATOR, NULL);
35792 goto saw_error;
35793 }
35794 switch (TREE_CODE (rhs))
35795 {
35796 case MULT_EXPR:
35797 case TRUNC_DIV_EXPR:
35798 case RDIV_EXPR:
35799 case PLUS_EXPR:
35800 case MINUS_EXPR:
35801 case LSHIFT_EXPR:
35802 case RSHIFT_EXPR:
35803 case BIT_AND_EXPR:
35804 case BIT_IOR_EXPR:
35805 case BIT_XOR_EXPR:
35806 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
35807 {
35808 if (cp_parser_parse_definitely (parser))
35809 {
35810 opcode = TREE_CODE (rhs);
35811 rhs1 = TREE_OPERAND (rhs, 0);
35812 rhs = TREE_OPERAND (rhs, 1);
35813 goto stmt_done;
35814 }
35815 else
35816 goto saw_error;
35817 }
35818 break;
35819 default:
35820 break;
35821 }
35822 cp_parser_abort_tentative_parse (parser);
35823 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
35824 {
35825 rhs = cp_parser_expression (parser);
35826 if (rhs == error_mark_node)
35827 goto saw_error;
35828 opcode = NOP_EXPR;
35829 rhs1 = NULL_TREE;
35830 goto stmt_done;
35831 }
35832 cp_parser_error (parser,
35833 "invalid form of %<#pragma omp atomic%>");
35834 goto saw_error;
35835 }
35836 if (!cp_parser_parse_definitely (parser))
35837 goto saw_error;
35838 switch (token->type)
35839 {
35840 case CPP_SEMICOLON:
35841 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35842 {
35843 code = OMP_ATOMIC_CAPTURE_OLD;
35844 v = lhs;
35845 lhs = NULL_TREE;
35846 lhs1 = rhs1;
35847 rhs1 = NULL_TREE;
35848 cp_lexer_consume_token (parser->lexer);
35849 goto restart;
35850 }
35851 else if (structured_block)
35852 {
35853 opcode = NOP_EXPR;
35854 rhs = rhs1;
35855 rhs1 = NULL_TREE;
35856 goto stmt_done;
35857 }
35858 cp_parser_error (parser,
35859 "invalid form of %<#pragma omp atomic%>");
35860 goto saw_error;
35861 case CPP_MULT:
35862 opcode = MULT_EXPR;
35863 break;
35864 case CPP_DIV:
35865 opcode = TRUNC_DIV_EXPR;
35866 break;
35867 case CPP_PLUS:
35868 opcode = PLUS_EXPR;
35869 break;
35870 case CPP_MINUS:
35871 opcode = MINUS_EXPR;
35872 break;
35873 case CPP_LSHIFT:
35874 opcode = LSHIFT_EXPR;
35875 break;
35876 case CPP_RSHIFT:
35877 opcode = RSHIFT_EXPR;
35878 break;
35879 case CPP_AND:
35880 opcode = BIT_AND_EXPR;
35881 break;
35882 case CPP_OR:
35883 opcode = BIT_IOR_EXPR;
35884 break;
35885 case CPP_XOR:
35886 opcode = BIT_XOR_EXPR;
35887 break;
35888 default:
35889 cp_parser_error (parser,
35890 "invalid operator for %<#pragma omp atomic%>");
35891 goto saw_error;
35892 }
35893 oprec = TOKEN_PRECEDENCE (token);
35894 gcc_assert (oprec != PREC_NOT_OPERATOR);
35895 if (commutative_tree_code (opcode))
35896 oprec = (enum cp_parser_prec) (oprec - 1);
35897 cp_lexer_consume_token (parser->lexer);
35898 rhs = cp_parser_binary_expression (parser, false, false,
35899 oprec, NULL);
35900 if (rhs == error_mark_node)
35901 goto saw_error;
35902 goto stmt_done;
35903 /* FALLTHROUGH */
35904 default:
35905 cp_parser_error (parser,
35906 "invalid operator for %<#pragma omp atomic%>");
35907 goto saw_error;
35908 }
35909 cp_lexer_consume_token (parser->lexer);
35910
35911 rhs = cp_parser_expression (parser);
35912 if (rhs == error_mark_node)
35913 goto saw_error;
35914 break;
35915 }
35916 stmt_done:
35917 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
35918 {
35919 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
35920 goto saw_error;
35921 v = cp_parser_unary_expression (parser);
35922 if (v == error_mark_node)
35923 goto saw_error;
35924 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
35925 goto saw_error;
35926 lhs1 = cp_parser_unary_expression (parser);
35927 if (lhs1 == error_mark_node)
35928 goto saw_error;
35929 }
35930 if (structured_block)
35931 {
35932 cp_parser_consume_semicolon_at_end_of_statement (parser);
35933 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35934 }
35935 done:
35936 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35937 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
35938 rhs1, clauses, memory_order);
35939 if (!structured_block)
35940 cp_parser_consume_semicolon_at_end_of_statement (parser);
35941 return;
35942
35943 saw_error:
35944 cp_parser_skip_to_end_of_block_or_statement (parser);
35945 if (structured_block)
35946 {
35947 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35948 cp_lexer_consume_token (parser->lexer);
35949 else if (code == OMP_ATOMIC_CAPTURE_NEW)
35950 {
35951 cp_parser_skip_to_end_of_block_or_statement (parser);
35952 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35953 cp_lexer_consume_token (parser->lexer);
35954 }
35955 }
35956 }
35957
35958
35959 /* OpenMP 2.5:
35960 # pragma omp barrier new-line */
35961
35962 static void
35963 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
35964 {
35965 cp_parser_require_pragma_eol (parser, pragma_tok);
35966 finish_omp_barrier ();
35967 }
35968
35969 /* OpenMP 2.5:
35970 # pragma omp critical [(name)] new-line
35971 structured-block
35972
35973 OpenMP 4.5:
35974 # pragma omp critical [(name) [hint(expression)]] new-line
35975 structured-block */
35976
35977 #define OMP_CRITICAL_CLAUSE_MASK \
35978 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
35979
35980 static tree
35981 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35982 {
35983 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
35984
35985 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35986 {
35987 matching_parens parens;
35988 parens.consume_open (parser);
35989
35990 name = cp_parser_identifier (parser);
35991
35992 if (name == error_mark_node
35993 || !parens.require_close (parser))
35994 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35995 /*or_comma=*/false,
35996 /*consume_paren=*/true);
35997 if (name == error_mark_node)
35998 name = NULL;
35999
36000 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
36001 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
36002 cp_lexer_consume_token (parser->lexer);
36003
36004 clauses = cp_parser_omp_all_clauses (parser,
36005 OMP_CRITICAL_CLAUSE_MASK,
36006 "#pragma omp critical", pragma_tok);
36007 }
36008 else
36009 cp_parser_require_pragma_eol (parser, pragma_tok);
36010
36011 stmt = cp_parser_omp_structured_block (parser, if_p);
36012 return c_finish_omp_critical (input_location, stmt, name, clauses);
36013 }
36014
36015 /* OpenMP 5.0:
36016 # pragma omp depobj ( depobj ) depobj-clause new-line
36017
36018 depobj-clause:
36019 depend (dependence-type : locator)
36020 destroy
36021 update (dependence-type)
36022
36023 dependence-type:
36024 in
36025 out
36026 inout
36027 mutexinout */
36028
36029 static void
36030 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
36031 {
36032 location_t loc = pragma_tok->location;
36033 matching_parens parens;
36034 if (!parens.require_open (parser))
36035 {
36036 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36037 return;
36038 }
36039
36040 tree depobj = cp_parser_assignment_expression (parser);
36041
36042 if (!parens.require_close (parser))
36043 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36044 /*or_comma=*/false,
36045 /*consume_paren=*/true);
36046
36047 tree clause = NULL_TREE;
36048 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
36049 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
36050 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36051 {
36052 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36053 const char *p = IDENTIFIER_POINTER (id);
36054
36055 cp_lexer_consume_token (parser->lexer);
36056 if (!strcmp ("depend", p))
36057 {
36058 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
36059 if (clause)
36060 clause = finish_omp_clauses (clause, C_ORT_OMP);
36061 if (!clause)
36062 clause = error_mark_node;
36063 }
36064 else if (!strcmp ("destroy", p))
36065 kind = OMP_CLAUSE_DEPEND_LAST;
36066 else if (!strcmp ("update", p))
36067 {
36068 matching_parens c_parens;
36069 if (c_parens.require_open (parser))
36070 {
36071 location_t c2_loc
36072 = cp_lexer_peek_token (parser->lexer)->location;
36073 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36074 {
36075 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
36076 const char *p2 = IDENTIFIER_POINTER (id2);
36077
36078 cp_lexer_consume_token (parser->lexer);
36079 if (!strcmp ("in", p2))
36080 kind = OMP_CLAUSE_DEPEND_IN;
36081 else if (!strcmp ("out", p2))
36082 kind = OMP_CLAUSE_DEPEND_OUT;
36083 else if (!strcmp ("inout", p2))
36084 kind = OMP_CLAUSE_DEPEND_INOUT;
36085 else if (!strcmp ("mutexinoutset", p2))
36086 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
36087 }
36088 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
36089 {
36090 clause = error_mark_node;
36091 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%> or "
36092 "%<mutexinoutset%>");
36093 }
36094 if (!c_parens.require_close (parser))
36095 cp_parser_skip_to_closing_parenthesis (parser,
36096 /*recovering=*/true,
36097 /*or_comma=*/false,
36098 /*consume_paren=*/true);
36099 }
36100 else
36101 clause = error_mark_node;
36102 }
36103 }
36104 if (!clause && kind == OMP_CLAUSE_DEPEND_SOURCE)
36105 {
36106 clause = error_mark_node;
36107 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
36108 }
36109 cp_parser_require_pragma_eol (parser, pragma_tok);
36110
36111 finish_omp_depobj (loc, depobj, kind, clause);
36112 }
36113
36114
36115 /* OpenMP 2.5:
36116 # pragma omp flush flush-vars[opt] new-line
36117
36118 flush-vars:
36119 ( variable-list )
36120
36121 OpenMP 5.0:
36122 # pragma omp flush memory-order-clause new-line */
36123
36124 static void
36125 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
36126 {
36127 enum memmodel mo = MEMMODEL_LAST;
36128 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36129 {
36130 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36131 const char *p = IDENTIFIER_POINTER (id);
36132 if (!strcmp (p, "acq_rel"))
36133 mo = MEMMODEL_ACQ_REL;
36134 else if (!strcmp (p, "release"))
36135 mo = MEMMODEL_RELEASE;
36136 else if (!strcmp (p, "acquire"))
36137 mo = MEMMODEL_ACQUIRE;
36138 else
36139 error_at (cp_lexer_peek_token (parser->lexer)->location,
36140 "expected %<acq_rel%>, %<release%> or %<acquire%>");
36141 cp_lexer_consume_token (parser->lexer);
36142 }
36143 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36144 {
36145 if (mo != MEMMODEL_LAST)
36146 error_at (cp_lexer_peek_token (parser->lexer)->location,
36147 "%<flush%> list specified together with memory order "
36148 "clause");
36149 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
36150 }
36151 cp_parser_require_pragma_eol (parser, pragma_tok);
36152
36153 finish_omp_flush (mo);
36154 }
36155
36156 /* Helper function, to parse omp for increment expression. */
36157
36158 static tree
36159 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
36160 {
36161 tree cond = cp_parser_binary_expression (parser, false, true,
36162 PREC_NOT_OPERATOR, NULL);
36163 if (cond == error_mark_node
36164 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36165 {
36166 cp_parser_skip_to_end_of_statement (parser);
36167 return error_mark_node;
36168 }
36169
36170 switch (TREE_CODE (cond))
36171 {
36172 case GT_EXPR:
36173 case GE_EXPR:
36174 case LT_EXPR:
36175 case LE_EXPR:
36176 break;
36177 case NE_EXPR:
36178 if (code != OACC_LOOP)
36179 break;
36180 gcc_fallthrough ();
36181 default:
36182 return error_mark_node;
36183 }
36184
36185 /* If decl is an iterator, preserve LHS and RHS of the relational
36186 expr until finish_omp_for. */
36187 if (decl
36188 && (type_dependent_expression_p (decl)
36189 || CLASS_TYPE_P (TREE_TYPE (decl))))
36190 return cond;
36191
36192 return build_x_binary_op (cp_expr_loc_or_loc (cond, input_location),
36193 TREE_CODE (cond),
36194 TREE_OPERAND (cond, 0), ERROR_MARK,
36195 TREE_OPERAND (cond, 1), ERROR_MARK,
36196 /*overload=*/NULL, tf_warning_or_error);
36197 }
36198
36199 /* Helper function, to parse omp for increment expression. */
36200
36201 static tree
36202 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
36203 {
36204 cp_token *token = cp_lexer_peek_token (parser->lexer);
36205 enum tree_code op;
36206 tree lhs, rhs;
36207 cp_id_kind idk;
36208 bool decl_first;
36209
36210 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36211 {
36212 op = (token->type == CPP_PLUS_PLUS
36213 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
36214 cp_lexer_consume_token (parser->lexer);
36215 lhs = cp_parser_simple_cast_expression (parser);
36216 if (lhs != decl
36217 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36218 return error_mark_node;
36219 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36220 }
36221
36222 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
36223 if (lhs != decl
36224 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
36225 return error_mark_node;
36226
36227 token = cp_lexer_peek_token (parser->lexer);
36228 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
36229 {
36230 op = (token->type == CPP_PLUS_PLUS
36231 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
36232 cp_lexer_consume_token (parser->lexer);
36233 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
36234 }
36235
36236 op = cp_parser_assignment_operator_opt (parser);
36237 if (op == ERROR_MARK)
36238 return error_mark_node;
36239
36240 if (op != NOP_EXPR)
36241 {
36242 rhs = cp_parser_assignment_expression (parser);
36243 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
36244 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36245 }
36246
36247 lhs = cp_parser_binary_expression (parser, false, false,
36248 PREC_ADDITIVE_EXPRESSION, NULL);
36249 token = cp_lexer_peek_token (parser->lexer);
36250 decl_first = (lhs == decl
36251 || (processing_template_decl && cp_tree_equal (lhs, decl)));
36252 if (decl_first)
36253 lhs = NULL_TREE;
36254 if (token->type != CPP_PLUS
36255 && token->type != CPP_MINUS)
36256 return error_mark_node;
36257
36258 do
36259 {
36260 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
36261 cp_lexer_consume_token (parser->lexer);
36262 rhs = cp_parser_binary_expression (parser, false, false,
36263 PREC_ADDITIVE_EXPRESSION, NULL);
36264 token = cp_lexer_peek_token (parser->lexer);
36265 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
36266 {
36267 if (lhs == NULL_TREE)
36268 {
36269 if (op == PLUS_EXPR)
36270 lhs = rhs;
36271 else
36272 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
36273 tf_warning_or_error);
36274 }
36275 else
36276 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
36277 ERROR_MARK, NULL, tf_warning_or_error);
36278 }
36279 }
36280 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
36281
36282 if (!decl_first)
36283 {
36284 if ((rhs != decl
36285 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
36286 || op == MINUS_EXPR)
36287 return error_mark_node;
36288 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
36289 }
36290 else
36291 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
36292
36293 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
36294 }
36295
36296 /* Parse the initialization statement of an OpenMP for loop.
36297
36298 Return true if the resulting construct should have an
36299 OMP_CLAUSE_PRIVATE added to it. */
36300
36301 static tree
36302 cp_parser_omp_for_loop_init (cp_parser *parser,
36303 tree &this_pre_body,
36304 vec<tree, va_gc> *&for_block,
36305 tree &init,
36306 tree &orig_init,
36307 tree &decl,
36308 tree &real_decl)
36309 {
36310 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
36311 return NULL_TREE;
36312
36313 tree add_private_clause = NULL_TREE;
36314
36315 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
36316
36317 init-expr:
36318 var = lb
36319 integer-type var = lb
36320 random-access-iterator-type var = lb
36321 pointer-type var = lb
36322 */
36323 cp_decl_specifier_seq type_specifiers;
36324
36325 /* First, try to parse as an initialized declaration. See
36326 cp_parser_condition, from whence the bulk of this is copied. */
36327
36328 cp_parser_parse_tentatively (parser);
36329 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
36330 /*is_declaration=*/true,
36331 /*is_trailing_return=*/false,
36332 &type_specifiers);
36333 if (cp_parser_parse_definitely (parser))
36334 {
36335 /* If parsing a type specifier seq succeeded, then this
36336 MUST be a initialized declaration. */
36337 tree asm_specification, attributes;
36338 cp_declarator *declarator;
36339
36340 declarator = cp_parser_declarator (parser,
36341 CP_PARSER_DECLARATOR_NAMED,
36342 CP_PARSER_FLAGS_NONE,
36343 /*ctor_dtor_or_conv_p=*/NULL,
36344 /*parenthesized_p=*/NULL,
36345 /*member_p=*/false,
36346 /*friend_p=*/false,
36347 /*static_p=*/false);
36348 attributes = cp_parser_attributes_opt (parser);
36349 asm_specification = cp_parser_asm_specification_opt (parser);
36350
36351 if (declarator == cp_error_declarator)
36352 cp_parser_skip_to_end_of_statement (parser);
36353
36354 else
36355 {
36356 tree pushed_scope, auto_node;
36357
36358 decl = start_decl (declarator, &type_specifiers,
36359 SD_INITIALIZED, attributes,
36360 /*prefix_attributes=*/NULL_TREE,
36361 &pushed_scope);
36362
36363 auto_node = type_uses_auto (TREE_TYPE (decl));
36364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36365 {
36366 if (cp_lexer_next_token_is (parser->lexer,
36367 CPP_OPEN_PAREN))
36368 error ("parenthesized initialization is not allowed in "
36369 "OpenMP %<for%> loop");
36370 else
36371 /* Trigger an error. */
36372 cp_parser_require (parser, CPP_EQ, RT_EQ);
36373
36374 init = error_mark_node;
36375 cp_parser_skip_to_end_of_statement (parser);
36376 }
36377 else if (CLASS_TYPE_P (TREE_TYPE (decl))
36378 || type_dependent_expression_p (decl)
36379 || auto_node)
36380 {
36381 bool is_direct_init, is_non_constant_init;
36382
36383 init = cp_parser_initializer (parser,
36384 &is_direct_init,
36385 &is_non_constant_init);
36386
36387 if (auto_node)
36388 {
36389 TREE_TYPE (decl)
36390 = do_auto_deduction (TREE_TYPE (decl), init,
36391 auto_node);
36392
36393 if (!CLASS_TYPE_P (TREE_TYPE (decl))
36394 && !type_dependent_expression_p (decl))
36395 goto non_class;
36396 }
36397
36398 cp_finish_decl (decl, init, !is_non_constant_init,
36399 asm_specification,
36400 LOOKUP_ONLYCONVERTING);
36401 orig_init = init;
36402 if (CLASS_TYPE_P (TREE_TYPE (decl)))
36403 {
36404 vec_safe_push (for_block, this_pre_body);
36405 init = NULL_TREE;
36406 }
36407 else
36408 {
36409 init = pop_stmt_list (this_pre_body);
36410 if (init && TREE_CODE (init) == STATEMENT_LIST)
36411 {
36412 tree_stmt_iterator i = tsi_start (init);
36413 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
36414 while (!tsi_end_p (i))
36415 {
36416 tree t = tsi_stmt (i);
36417 if (TREE_CODE (t) == DECL_EXPR
36418 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
36419 {
36420 tsi_delink (&i);
36421 vec_safe_push (for_block, t);
36422 continue;
36423 }
36424 break;
36425 }
36426 if (tsi_one_before_end_p (i))
36427 {
36428 tree t = tsi_stmt (i);
36429 tsi_delink (&i);
36430 free_stmt_list (init);
36431 init = t;
36432 }
36433 }
36434 }
36435 this_pre_body = NULL_TREE;
36436 }
36437 else
36438 {
36439 /* Consume '='. */
36440 cp_lexer_consume_token (parser->lexer);
36441 init = cp_parser_assignment_expression (parser);
36442
36443 non_class:
36444 if (TYPE_REF_P (TREE_TYPE (decl)))
36445 init = error_mark_node;
36446 else
36447 cp_finish_decl (decl, NULL_TREE,
36448 /*init_const_expr_p=*/false,
36449 asm_specification,
36450 LOOKUP_ONLYCONVERTING);
36451 }
36452
36453 if (pushed_scope)
36454 pop_scope (pushed_scope);
36455 }
36456 }
36457 else
36458 {
36459 cp_id_kind idk;
36460 /* If parsing a type specifier sequence failed, then
36461 this MUST be a simple expression. */
36462 cp_parser_parse_tentatively (parser);
36463 decl = cp_parser_primary_expression (parser, false, false,
36464 false, &idk);
36465 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
36466 if (!cp_parser_error_occurred (parser)
36467 && decl
36468 && (TREE_CODE (decl) == COMPONENT_REF
36469 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
36470 {
36471 cp_parser_abort_tentative_parse (parser);
36472 cp_parser_parse_tentatively (parser);
36473 cp_token *token = cp_lexer_peek_token (parser->lexer);
36474 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
36475 /*check_dependency_p=*/true,
36476 /*template_p=*/NULL,
36477 /*declarator_p=*/false,
36478 /*optional_p=*/false);
36479 if (name != error_mark_node
36480 && last_tok == cp_lexer_peek_token (parser->lexer))
36481 {
36482 decl = cp_parser_lookup_name_simple (parser, name,
36483 token->location);
36484 if (TREE_CODE (decl) == FIELD_DECL)
36485 add_private_clause = omp_privatize_field (decl, false);
36486 }
36487 cp_parser_abort_tentative_parse (parser);
36488 cp_parser_parse_tentatively (parser);
36489 decl = cp_parser_primary_expression (parser, false, false,
36490 false, &idk);
36491 }
36492 if (!cp_parser_error_occurred (parser)
36493 && decl
36494 && DECL_P (decl)
36495 && CLASS_TYPE_P (TREE_TYPE (decl)))
36496 {
36497 tree rhs;
36498
36499 cp_parser_parse_definitely (parser);
36500 cp_parser_require (parser, CPP_EQ, RT_EQ);
36501 rhs = cp_parser_assignment_expression (parser);
36502 orig_init = rhs;
36503 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
36504 decl, NOP_EXPR,
36505 rhs,
36506 tf_warning_or_error));
36507 if (!add_private_clause)
36508 add_private_clause = decl;
36509 }
36510 else
36511 {
36512 decl = NULL;
36513 cp_parser_abort_tentative_parse (parser);
36514 init = cp_parser_expression (parser);
36515 if (init)
36516 {
36517 if (TREE_CODE (init) == MODIFY_EXPR
36518 || TREE_CODE (init) == MODOP_EXPR)
36519 real_decl = TREE_OPERAND (init, 0);
36520 }
36521 }
36522 }
36523 return add_private_clause;
36524 }
36525
36526 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
36527
36528 void
36529 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
36530 tree &decl, tree &orig_decl, tree &init,
36531 tree &orig_init, tree &cond, tree &incr)
36532 {
36533 tree begin, end, range_temp_decl = NULL_TREE;
36534 tree iter_type, begin_expr, end_expr;
36535
36536 if (processing_template_decl)
36537 {
36538 if (check_for_bare_parameter_packs (init))
36539 init = error_mark_node;
36540 if (!type_dependent_expression_p (init)
36541 /* do_auto_deduction doesn't mess with template init-lists. */
36542 && !BRACE_ENCLOSED_INITIALIZER_P (init))
36543 {
36544 tree d = decl;
36545 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
36546 {
36547 tree v = DECL_VALUE_EXPR (decl);
36548 if (TREE_CODE (v) == ARRAY_REF
36549 && VAR_P (TREE_OPERAND (v, 0))
36550 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36551 d = TREE_OPERAND (v, 0);
36552 }
36553 do_range_for_auto_deduction (d, init);
36554 }
36555 cond = global_namespace;
36556 incr = NULL_TREE;
36557 orig_init = init;
36558 if (this_pre_body)
36559 this_pre_body = pop_stmt_list (this_pre_body);
36560 return;
36561 }
36562
36563 init = mark_lvalue_use (init);
36564
36565 if (decl == error_mark_node || init == error_mark_node)
36566 /* If an error happened previously do nothing or else a lot of
36567 unhelpful errors would be issued. */
36568 begin_expr = end_expr = iter_type = error_mark_node;
36569 else
36570 {
36571 tree range_temp;
36572
36573 if (VAR_P (init)
36574 && array_of_runtime_bound_p (TREE_TYPE (init)))
36575 /* Can't bind a reference to an array of runtime bound. */
36576 range_temp = init;
36577 else
36578 {
36579 range_temp = build_range_temp (init);
36580 DECL_NAME (range_temp) = NULL_TREE;
36581 pushdecl (range_temp);
36582 cp_finish_decl (range_temp, init,
36583 /*is_constant_init*/false, NULL_TREE,
36584 LOOKUP_ONLYCONVERTING);
36585 range_temp_decl = range_temp;
36586 range_temp = convert_from_reference (range_temp);
36587 }
36588 iter_type = cp_parser_perform_range_for_lookup (range_temp,
36589 &begin_expr, &end_expr);
36590 }
36591
36592 tree end_iter_type = iter_type;
36593 if (cxx_dialect >= cxx17)
36594 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
36595 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
36596 TREE_USED (end) = 1;
36597 DECL_ARTIFICIAL (end) = 1;
36598 pushdecl (end);
36599 cp_finish_decl (end, end_expr,
36600 /*is_constant_init*/false, NULL_TREE,
36601 LOOKUP_ONLYCONVERTING);
36602
36603 /* The new for initialization statement. */
36604 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
36605 TREE_USED (begin) = 1;
36606 DECL_ARTIFICIAL (begin) = 1;
36607 pushdecl (begin);
36608 orig_init = init;
36609 if (CLASS_TYPE_P (iter_type))
36610 init = NULL_TREE;
36611 else
36612 {
36613 init = begin_expr;
36614 begin_expr = NULL_TREE;
36615 }
36616 cp_finish_decl (begin, begin_expr,
36617 /*is_constant_init*/false, NULL_TREE,
36618 LOOKUP_ONLYCONVERTING);
36619
36620 /* The new for condition. */
36621 if (CLASS_TYPE_P (iter_type))
36622 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
36623 else
36624 cond = build_x_binary_op (input_location, NE_EXPR,
36625 begin, ERROR_MARK,
36626 end, ERROR_MARK,
36627 NULL, tf_warning_or_error);
36628
36629 /* The new increment expression. */
36630 if (CLASS_TYPE_P (iter_type))
36631 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
36632 else
36633 incr = finish_unary_op_expr (input_location,
36634 PREINCREMENT_EXPR, begin,
36635 tf_warning_or_error);
36636
36637 orig_decl = decl;
36638 decl = begin;
36639 if (for_block)
36640 {
36641 vec_safe_push (for_block, this_pre_body);
36642 this_pre_body = NULL_TREE;
36643 }
36644
36645 tree decomp_first_name = NULL_TREE;
36646 unsigned decomp_cnt = 0;
36647 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
36648 {
36649 tree v = DECL_VALUE_EXPR (orig_decl);
36650 if (TREE_CODE (v) == ARRAY_REF
36651 && VAR_P (TREE_OPERAND (v, 0))
36652 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
36653 {
36654 tree d = orig_decl;
36655 orig_decl = TREE_OPERAND (v, 0);
36656 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
36657 decomp_first_name = d;
36658 }
36659 }
36660
36661 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
36662 if (auto_node)
36663 {
36664 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36665 tf_none);
36666 if (!error_operand_p (t))
36667 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
36668 t, auto_node);
36669 }
36670
36671 tree v = make_tree_vec (decomp_cnt + 3);
36672 TREE_VEC_ELT (v, 0) = range_temp_decl;
36673 TREE_VEC_ELT (v, 1) = end;
36674 TREE_VEC_ELT (v, 2) = orig_decl;
36675 for (unsigned i = 0; i < decomp_cnt; i++)
36676 {
36677 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
36678 decomp_first_name = DECL_CHAIN (decomp_first_name);
36679 }
36680 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
36681 }
36682
36683 /* Helper for cp_parser_omp_for_loop, finalize part of range for
36684 inside of the collapsed body. */
36685
36686 void
36687 cp_finish_omp_range_for (tree orig, tree begin)
36688 {
36689 gcc_assert (TREE_CODE (orig) == TREE_LIST
36690 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
36691 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
36692 tree decomp_first_name = NULL_TREE;
36693 unsigned int decomp_cnt = 0;
36694
36695 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36696 {
36697 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
36698 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
36699 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
36700 }
36701
36702 /* The declaration is initialized with *__begin inside the loop body. */
36703 cp_finish_decl (decl,
36704 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
36705 tf_warning_or_error),
36706 /*is_constant_init*/false, NULL_TREE,
36707 LOOKUP_ONLYCONVERTING);
36708 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
36709 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
36710 }
36711
36712 /* Parse the restricted form of the for statement allowed by OpenMP. */
36713
36714 static tree
36715 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
36716 tree *cclauses, bool *if_p)
36717 {
36718 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
36719 tree orig_decl;
36720 tree real_decl, initv, condv, incrv, declv, orig_declv;
36721 tree this_pre_body, cl, ordered_cl = NULL_TREE;
36722 location_t loc_first;
36723 bool collapse_err = false;
36724 int i, collapse = 1, ordered = 0, count, nbraces = 0;
36725 vec<tree, va_gc> *for_block = make_tree_vector ();
36726 auto_vec<tree, 4> orig_inits;
36727 bool tiling = false;
36728
36729 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
36730 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
36731 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
36732 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
36733 {
36734 tiling = true;
36735 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
36736 }
36737 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
36738 && OMP_CLAUSE_ORDERED_EXPR (cl))
36739 {
36740 ordered_cl = cl;
36741 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
36742 }
36743
36744 if (ordered && ordered < collapse)
36745 {
36746 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36747 "%<ordered%> clause parameter is less than %<collapse%>");
36748 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
36749 = build_int_cst (NULL_TREE, collapse);
36750 ordered = collapse;
36751 }
36752 if (ordered)
36753 {
36754 for (tree *pc = &clauses; *pc; )
36755 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
36756 {
36757 error_at (OMP_CLAUSE_LOCATION (*pc),
36758 "%<linear%> clause may not be specified together "
36759 "with %<ordered%> clause with a parameter");
36760 *pc = OMP_CLAUSE_CHAIN (*pc);
36761 }
36762 else
36763 pc = &OMP_CLAUSE_CHAIN (*pc);
36764 }
36765
36766 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
36767 count = ordered ? ordered : collapse;
36768
36769 declv = make_tree_vec (count);
36770 initv = make_tree_vec (count);
36771 condv = make_tree_vec (count);
36772 incrv = make_tree_vec (count);
36773 orig_declv = NULL_TREE;
36774
36775 loc_first = cp_lexer_peek_token (parser->lexer)->location;
36776
36777 for (i = 0; i < count; i++)
36778 {
36779 int bracecount = 0;
36780 tree add_private_clause = NULL_TREE;
36781 location_t loc;
36782
36783 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36784 {
36785 if (!collapse_err)
36786 cp_parser_error (parser, "for statement expected");
36787 return NULL;
36788 }
36789 loc = cp_lexer_consume_token (parser->lexer)->location;
36790
36791 /* Don't create location wrapper nodes within an OpenMP "for"
36792 statement. */
36793 auto_suppress_location_wrappers sentinel;
36794
36795 matching_parens parens;
36796 if (!parens.require_open (parser))
36797 return NULL;
36798
36799 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
36800 this_pre_body = push_stmt_list ();
36801
36802 if (code != OACC_LOOP && cxx_dialect >= cxx11)
36803 {
36804 /* Save tokens so that we can put them back. */
36805 cp_lexer_save_tokens (parser->lexer);
36806
36807 /* Look for ':' that is not nested in () or {}. */
36808 bool is_range_for
36809 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
36810 /*recovering=*/false,
36811 CPP_COLON,
36812 /*consume_paren=*/
36813 false) == -1);
36814
36815 /* Roll back the tokens we skipped. */
36816 cp_lexer_rollback_tokens (parser->lexer);
36817
36818 if (is_range_for)
36819 {
36820 bool saved_colon_corrects_to_scope_p
36821 = parser->colon_corrects_to_scope_p;
36822
36823 /* A colon is used in range-based for. */
36824 parser->colon_corrects_to_scope_p = false;
36825
36826 /* Parse the declaration. */
36827 cp_parser_simple_declaration (parser,
36828 /*function_definition_allowed_p=*/
36829 false, &decl);
36830 parser->colon_corrects_to_scope_p
36831 = saved_colon_corrects_to_scope_p;
36832
36833 cp_parser_require (parser, CPP_COLON, RT_COLON);
36834
36835 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
36836 false, 0, true);
36837
36838 cp_convert_omp_range_for (this_pre_body, for_block, decl,
36839 orig_decl, init, orig_init,
36840 cond, incr);
36841 if (this_pre_body)
36842 {
36843 if (pre_body)
36844 {
36845 tree t = pre_body;
36846 pre_body = push_stmt_list ();
36847 add_stmt (t);
36848 add_stmt (this_pre_body);
36849 pre_body = pop_stmt_list (pre_body);
36850 }
36851 else
36852 pre_body = this_pre_body;
36853 }
36854
36855 if (ordered_cl)
36856 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
36857 "%<ordered%> clause with parameter on "
36858 "range-based %<for%> loop");
36859
36860 goto parse_close_paren;
36861 }
36862 }
36863
36864 add_private_clause
36865 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
36866 init, orig_init, decl, real_decl);
36867
36868 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36869 if (this_pre_body)
36870 {
36871 this_pre_body = pop_stmt_list (this_pre_body);
36872 if (pre_body)
36873 {
36874 tree t = pre_body;
36875 pre_body = push_stmt_list ();
36876 add_stmt (t);
36877 add_stmt (this_pre_body);
36878 pre_body = pop_stmt_list (pre_body);
36879 }
36880 else
36881 pre_body = this_pre_body;
36882 }
36883
36884 if (decl)
36885 real_decl = decl;
36886 if (cclauses != NULL
36887 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
36888 && real_decl != NULL_TREE)
36889 {
36890 tree *c;
36891 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
36892 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
36893 && OMP_CLAUSE_DECL (*c) == real_decl)
36894 {
36895 error_at (loc, "iteration variable %qD"
36896 " should not be firstprivate", real_decl);
36897 *c = OMP_CLAUSE_CHAIN (*c);
36898 }
36899 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
36900 && OMP_CLAUSE_DECL (*c) == real_decl)
36901 {
36902 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
36903 tree l = *c;
36904 *c = OMP_CLAUSE_CHAIN (*c);
36905 if (code == OMP_SIMD)
36906 {
36907 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
36908 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
36909 }
36910 else
36911 {
36912 OMP_CLAUSE_CHAIN (l) = clauses;
36913 clauses = l;
36914 }
36915 add_private_clause = NULL_TREE;
36916 }
36917 else
36918 {
36919 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
36920 && OMP_CLAUSE_DECL (*c) == real_decl)
36921 add_private_clause = NULL_TREE;
36922 c = &OMP_CLAUSE_CHAIN (*c);
36923 }
36924 }
36925
36926 if (add_private_clause)
36927 {
36928 tree c;
36929 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
36930 {
36931 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
36932 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
36933 && OMP_CLAUSE_DECL (c) == decl)
36934 break;
36935 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
36936 && OMP_CLAUSE_DECL (c) == decl)
36937 error_at (loc, "iteration variable %qD "
36938 "should not be firstprivate",
36939 decl);
36940 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
36941 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
36942 && OMP_CLAUSE_DECL (c) == decl)
36943 error_at (loc, "iteration variable %qD should not be reduction",
36944 decl);
36945 }
36946 if (c == NULL)
36947 {
36948 if (code != OMP_SIMD)
36949 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
36950 else if (collapse == 1)
36951 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
36952 else
36953 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
36954 OMP_CLAUSE_DECL (c) = add_private_clause;
36955 c = finish_omp_clauses (c, C_ORT_OMP);
36956 if (c)
36957 {
36958 OMP_CLAUSE_CHAIN (c) = clauses;
36959 clauses = c;
36960 /* For linear, signal that we need to fill up
36961 the so far unknown linear step. */
36962 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
36963 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
36964 }
36965 }
36966 }
36967
36968 cond = NULL;
36969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36970 cond = cp_parser_omp_for_cond (parser, decl, code);
36971 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
36972
36973 incr = NULL;
36974 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
36975 {
36976 /* If decl is an iterator, preserve the operator on decl
36977 until finish_omp_for. */
36978 if (real_decl
36979 && ((processing_template_decl
36980 && (TREE_TYPE (real_decl) == NULL_TREE
36981 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
36982 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
36983 incr = cp_parser_omp_for_incr (parser, real_decl);
36984 else
36985 incr = cp_parser_expression (parser);
36986 if (!EXPR_HAS_LOCATION (incr))
36987 protected_set_expr_location (incr, input_location);
36988 }
36989
36990 parse_close_paren:
36991 if (!parens.require_close (parser))
36992 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36993 /*or_comma=*/false,
36994 /*consume_paren=*/true);
36995
36996 TREE_VEC_ELT (declv, i) = decl;
36997 TREE_VEC_ELT (initv, i) = init;
36998 TREE_VEC_ELT (condv, i) = cond;
36999 TREE_VEC_ELT (incrv, i) = incr;
37000 if (orig_init)
37001 {
37002 orig_inits.safe_grow_cleared (i + 1);
37003 orig_inits[i] = orig_init;
37004 }
37005 if (orig_decl)
37006 {
37007 if (!orig_declv)
37008 orig_declv = copy_node (declv);
37009 TREE_VEC_ELT (orig_declv, i) = orig_decl;
37010 }
37011 else if (orig_declv)
37012 TREE_VEC_ELT (orig_declv, i) = decl;
37013
37014 if (i == count - 1)
37015 break;
37016
37017 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
37018 in between the collapsed for loops to be still considered perfectly
37019 nested. Hopefully the final version clarifies this.
37020 For now handle (multiple) {'s and empty statements. */
37021 cp_parser_parse_tentatively (parser);
37022 for (;;)
37023 {
37024 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37025 break;
37026 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
37027 {
37028 cp_lexer_consume_token (parser->lexer);
37029 bracecount++;
37030 }
37031 else if (bracecount
37032 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37033 cp_lexer_consume_token (parser->lexer);
37034 else
37035 {
37036 loc = cp_lexer_peek_token (parser->lexer)->location;
37037 error_at (loc, "not enough for loops to collapse");
37038 collapse_err = true;
37039 cp_parser_abort_tentative_parse (parser);
37040 declv = NULL_TREE;
37041 break;
37042 }
37043 }
37044
37045 if (declv)
37046 {
37047 cp_parser_parse_definitely (parser);
37048 nbraces += bracecount;
37049 }
37050 }
37051
37052 if (nbraces)
37053 if_p = NULL;
37054
37055 /* Note that we saved the original contents of this flag when we entered
37056 the structured block, and so we don't need to re-save it here. */
37057 parser->in_statement = IN_OMP_FOR;
37058
37059 /* Note that the grammar doesn't call for a structured block here,
37060 though the loop as a whole is a structured block. */
37061 if (orig_declv)
37062 {
37063 body = begin_omp_structured_block ();
37064 for (i = 0; i < count; i++)
37065 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
37066 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
37067 TREE_VEC_ELT (declv, i));
37068 }
37069 else
37070 body = push_stmt_list ();
37071 cp_parser_statement (parser, NULL_TREE, false, if_p);
37072 if (orig_declv)
37073 body = finish_omp_structured_block (body);
37074 else
37075 body = pop_stmt_list (body);
37076
37077 if (declv == NULL_TREE)
37078 ret = NULL_TREE;
37079 else
37080 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
37081 incrv, body, pre_body, &orig_inits, clauses);
37082
37083 while (nbraces)
37084 {
37085 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
37086 {
37087 cp_lexer_consume_token (parser->lexer);
37088 nbraces--;
37089 }
37090 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
37091 cp_lexer_consume_token (parser->lexer);
37092 else
37093 {
37094 if (!collapse_err)
37095 {
37096 error_at (cp_lexer_peek_token (parser->lexer)->location,
37097 "collapsed loops not perfectly nested");
37098 }
37099 collapse_err = true;
37100 cp_parser_statement_seq_opt (parser, NULL);
37101 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
37102 break;
37103 }
37104 }
37105
37106 while (!for_block->is_empty ())
37107 {
37108 tree t = for_block->pop ();
37109 if (TREE_CODE (t) == STATEMENT_LIST)
37110 add_stmt (pop_stmt_list (t));
37111 else
37112 add_stmt (t);
37113 }
37114 release_tree_vector (for_block);
37115
37116 return ret;
37117 }
37118
37119 /* Helper function for OpenMP parsing, split clauses and call
37120 finish_omp_clauses on each of the set of clauses afterwards. */
37121
37122 static void
37123 cp_omp_split_clauses (location_t loc, enum tree_code code,
37124 omp_clause_mask mask, tree clauses, tree *cclauses)
37125 {
37126 int i;
37127 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
37128 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
37129 if (cclauses[i])
37130 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
37131 }
37132
37133 /* OpenMP 4.0:
37134 #pragma omp simd simd-clause[optseq] new-line
37135 for-loop */
37136
37137 #define OMP_SIMD_CLAUSE_MASK \
37138 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
37139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37145 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37146 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37147 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL))
37148
37149 static tree
37150 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
37151 char *p_name, omp_clause_mask mask, tree *cclauses,
37152 bool *if_p)
37153 {
37154 tree clauses, sb, ret;
37155 unsigned int save;
37156 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37157
37158 strcat (p_name, " simd");
37159 mask |= OMP_SIMD_CLAUSE_MASK;
37160
37161 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37162 cclauses == NULL);
37163 if (cclauses)
37164 {
37165 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
37166 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
37167 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
37168 OMP_CLAUSE_ORDERED);
37169 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
37170 {
37171 error_at (OMP_CLAUSE_LOCATION (c),
37172 "%<ordered%> clause with parameter may not be specified "
37173 "on %qs construct", p_name);
37174 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
37175 }
37176 }
37177
37178 keep_next_level (true);
37179 sb = begin_omp_structured_block ();
37180 save = cp_parser_begin_omp_structured_block (parser);
37181
37182 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
37183
37184 cp_parser_end_omp_structured_block (parser, save);
37185 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37186
37187 return ret;
37188 }
37189
37190 /* OpenMP 2.5:
37191 #pragma omp for for-clause[optseq] new-line
37192 for-loop
37193
37194 OpenMP 4.0:
37195 #pragma omp for simd for-simd-clause[optseq] new-line
37196 for-loop */
37197
37198 #define OMP_FOR_CLAUSE_MASK \
37199 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
37205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
37206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
37207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37208
37209 static tree
37210 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
37211 char *p_name, omp_clause_mask mask, tree *cclauses,
37212 bool *if_p)
37213 {
37214 tree clauses, sb, ret;
37215 unsigned int save;
37216 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37217
37218 strcat (p_name, " for");
37219 mask |= OMP_FOR_CLAUSE_MASK;
37220 /* parallel for{, simd} disallows nowait clause, but for
37221 target {teams distribute ,}parallel for{, simd} it should be accepted. */
37222 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
37223 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37224 /* Composite distribute parallel for{, simd} disallows ordered clause. */
37225 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37226 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
37227
37228 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37229 {
37230 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37231 const char *p = IDENTIFIER_POINTER (id);
37232
37233 if (strcmp (p, "simd") == 0)
37234 {
37235 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37236 if (cclauses == NULL)
37237 cclauses = cclauses_buf;
37238
37239 cp_lexer_consume_token (parser->lexer);
37240 if (!flag_openmp) /* flag_openmp_simd */
37241 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37242 cclauses, if_p);
37243 sb = begin_omp_structured_block ();
37244 save = cp_parser_begin_omp_structured_block (parser);
37245 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37246 cclauses, if_p);
37247 cp_parser_end_omp_structured_block (parser, save);
37248 tree body = finish_omp_structured_block (sb);
37249 if (ret == NULL)
37250 return ret;
37251 ret = make_node (OMP_FOR);
37252 TREE_TYPE (ret) = void_type_node;
37253 OMP_FOR_BODY (ret) = body;
37254 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37255 SET_EXPR_LOCATION (ret, loc);
37256 add_stmt (ret);
37257 return ret;
37258 }
37259 }
37260 if (!flag_openmp) /* flag_openmp_simd */
37261 {
37262 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37263 return NULL_TREE;
37264 }
37265
37266 /* Composite distribute parallel for disallows linear clause. */
37267 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37268 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
37269
37270 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37271 cclauses == NULL);
37272 if (cclauses)
37273 {
37274 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
37275 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
37276 }
37277
37278 keep_next_level (true);
37279 sb = begin_omp_structured_block ();
37280 save = cp_parser_begin_omp_structured_block (parser);
37281
37282 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
37283
37284 cp_parser_end_omp_structured_block (parser, save);
37285 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37286
37287 return ret;
37288 }
37289
37290 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
37291 omp_clause_mask, tree *, bool *);
37292
37293 /* OpenMP 2.5:
37294 # pragma omp master new-line
37295 structured-block */
37296
37297 static tree
37298 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
37299 char *p_name, omp_clause_mask mask, tree *cclauses,
37300 bool *if_p)
37301 {
37302 tree clauses, sb, ret;
37303 unsigned int save;
37304 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37305
37306 strcat (p_name, " master");
37307
37308 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37309 {
37310 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37311 const char *p = IDENTIFIER_POINTER (id);
37312
37313 if (strcmp (p, "taskloop") == 0)
37314 {
37315 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37316 if (cclauses == NULL)
37317 cclauses = cclauses_buf;
37318
37319 cp_lexer_consume_token (parser->lexer);
37320 if (!flag_openmp) /* flag_openmp_simd */
37321 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37322 cclauses, if_p);
37323 sb = begin_omp_structured_block ();
37324 save = cp_parser_begin_omp_structured_block (parser);
37325 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
37326 cclauses, if_p);
37327 cp_parser_end_omp_structured_block (parser, save);
37328 tree body = finish_omp_structured_block (sb);
37329 if (ret == NULL)
37330 return ret;
37331 return c_finish_omp_master (loc, body);
37332 }
37333 }
37334 if (!flag_openmp) /* flag_openmp_simd */
37335 {
37336 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37337 return NULL_TREE;
37338 }
37339
37340 if (cclauses)
37341 {
37342 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37343 false);
37344 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
37345 }
37346 else
37347 cp_parser_require_pragma_eol (parser, pragma_tok);
37348
37349 return c_finish_omp_master (loc,
37350 cp_parser_omp_structured_block (parser, if_p));
37351 }
37352
37353 /* OpenMP 2.5:
37354 # pragma omp ordered new-line
37355 structured-block
37356
37357 OpenMP 4.5:
37358 # pragma omp ordered ordered-clauses new-line
37359 structured-block */
37360
37361 #define OMP_ORDERED_CLAUSE_MASK \
37362 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
37363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
37364
37365 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
37366 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37367
37368 static bool
37369 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
37370 enum pragma_context context, bool *if_p)
37371 {
37372 location_t loc = pragma_tok->location;
37373
37374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37375 {
37376 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37377 const char *p = IDENTIFIER_POINTER (id);
37378
37379 if (strcmp (p, "depend") == 0)
37380 {
37381 if (!flag_openmp) /* flag_openmp_simd */
37382 {
37383 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37384 return false;
37385 }
37386 if (context == pragma_stmt)
37387 {
37388 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
37389 "%<depend%> clause may only be used in compound "
37390 "statements");
37391 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37392 return false;
37393 }
37394 tree clauses
37395 = cp_parser_omp_all_clauses (parser,
37396 OMP_ORDERED_DEPEND_CLAUSE_MASK,
37397 "#pragma omp ordered", pragma_tok);
37398 c_finish_omp_ordered (loc, clauses, NULL_TREE);
37399 return false;
37400 }
37401 }
37402
37403 tree clauses
37404 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
37405 "#pragma omp ordered", pragma_tok);
37406
37407 if (!flag_openmp /* flag_openmp_simd */
37408 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
37409 return false;
37410
37411 c_finish_omp_ordered (loc, clauses,
37412 cp_parser_omp_structured_block (parser, if_p));
37413 return true;
37414 }
37415
37416 /* OpenMP 2.5:
37417
37418 section-scope:
37419 { section-sequence }
37420
37421 section-sequence:
37422 section-directive[opt] structured-block
37423 section-sequence section-directive structured-block */
37424
37425 static tree
37426 cp_parser_omp_sections_scope (cp_parser *parser)
37427 {
37428 tree stmt, substmt;
37429 bool error_suppress = false;
37430 cp_token *tok;
37431
37432 matching_braces braces;
37433 if (!braces.require_open (parser))
37434 return NULL_TREE;
37435
37436 stmt = push_stmt_list ();
37437
37438 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
37439 != PRAGMA_OMP_SECTION)
37440 {
37441 substmt = cp_parser_omp_structured_block (parser, NULL);
37442 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37443 add_stmt (substmt);
37444 }
37445
37446 while (1)
37447 {
37448 tok = cp_lexer_peek_token (parser->lexer);
37449 if (tok->type == CPP_CLOSE_BRACE)
37450 break;
37451 if (tok->type == CPP_EOF)
37452 break;
37453
37454 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
37455 {
37456 cp_lexer_consume_token (parser->lexer);
37457 cp_parser_require_pragma_eol (parser, tok);
37458 error_suppress = false;
37459 }
37460 else if (!error_suppress)
37461 {
37462 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
37463 error_suppress = true;
37464 }
37465
37466 substmt = cp_parser_omp_structured_block (parser, NULL);
37467 substmt = build1 (OMP_SECTION, void_type_node, substmt);
37468 add_stmt (substmt);
37469 }
37470 braces.require_close (parser);
37471
37472 substmt = pop_stmt_list (stmt);
37473
37474 stmt = make_node (OMP_SECTIONS);
37475 TREE_TYPE (stmt) = void_type_node;
37476 OMP_SECTIONS_BODY (stmt) = substmt;
37477
37478 add_stmt (stmt);
37479 return stmt;
37480 }
37481
37482 /* OpenMP 2.5:
37483 # pragma omp sections sections-clause[optseq] newline
37484 sections-scope */
37485
37486 #define OMP_SECTIONS_CLAUSE_MASK \
37487 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37492
37493 static tree
37494 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
37495 char *p_name, omp_clause_mask mask, tree *cclauses)
37496 {
37497 tree clauses, ret;
37498 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37499
37500 strcat (p_name, " sections");
37501 mask |= OMP_SECTIONS_CLAUSE_MASK;
37502 if (cclauses)
37503 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
37504
37505 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37506 cclauses == NULL);
37507 if (cclauses)
37508 {
37509 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
37510 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
37511 }
37512
37513 ret = cp_parser_omp_sections_scope (parser);
37514 if (ret)
37515 OMP_SECTIONS_CLAUSES (ret) = clauses;
37516
37517 return ret;
37518 }
37519
37520 /* OpenMP 2.5:
37521 # pragma omp parallel parallel-clause[optseq] new-line
37522 structured-block
37523 # pragma omp parallel for parallel-for-clause[optseq] new-line
37524 structured-block
37525 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
37526 structured-block
37527
37528 OpenMP 4.0:
37529 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
37530 structured-block */
37531
37532 #define OMP_PARALLEL_CLAUSE_MASK \
37533 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
37539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
37541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
37542
37543 static tree
37544 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
37545 char *p_name, omp_clause_mask mask, tree *cclauses,
37546 bool *if_p)
37547 {
37548 tree stmt, clauses, block;
37549 unsigned int save;
37550 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37551
37552 strcat (p_name, " parallel");
37553 mask |= OMP_PARALLEL_CLAUSE_MASK;
37554 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
37555 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
37556 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
37557 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
37558
37559 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
37560 {
37561 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37562 if (cclauses == NULL)
37563 cclauses = cclauses_buf;
37564
37565 cp_lexer_consume_token (parser->lexer);
37566 if (!flag_openmp) /* flag_openmp_simd */
37567 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37568 if_p);
37569 block = begin_omp_parallel ();
37570 save = cp_parser_begin_omp_structured_block (parser);
37571 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
37572 if_p);
37573 cp_parser_end_omp_structured_block (parser, save);
37574 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37575 block);
37576 if (ret == NULL_TREE)
37577 return ret;
37578 OMP_PARALLEL_COMBINED (stmt) = 1;
37579 return stmt;
37580 }
37581 /* When combined with distribute, parallel has to be followed by for.
37582 #pragma omp target parallel is allowed though. */
37583 else if (cclauses
37584 && (mask & (OMP_CLAUSE_MASK_1
37585 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
37586 {
37587 error_at (loc, "expected %<for%> after %qs", p_name);
37588 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37589 return NULL_TREE;
37590 }
37591 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37592 {
37593 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37594 const char *p = IDENTIFIER_POINTER (id);
37595 if (strcmp (p, "master") == 0)
37596 {
37597 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37598 cclauses = cclauses_buf;
37599
37600 cp_lexer_consume_token (parser->lexer);
37601 block = begin_omp_parallel ();
37602 save = cp_parser_begin_omp_structured_block (parser);
37603 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
37604 cclauses, if_p);
37605 cp_parser_end_omp_structured_block (parser, save);
37606 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37607 block);
37608 OMP_PARALLEL_COMBINED (stmt) = 1;
37609 if (ret == NULL_TREE)
37610 return ret;
37611 return stmt;
37612 }
37613 else if (!flag_openmp) /* flag_openmp_simd */
37614 {
37615 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37616 return NULL_TREE;
37617 }
37618 else if (strcmp (p, "sections") == 0)
37619 {
37620 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37621 cclauses = cclauses_buf;
37622
37623 cp_lexer_consume_token (parser->lexer);
37624 block = begin_omp_parallel ();
37625 save = cp_parser_begin_omp_structured_block (parser);
37626 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
37627 cp_parser_end_omp_structured_block (parser, save);
37628 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
37629 block);
37630 OMP_PARALLEL_COMBINED (stmt) = 1;
37631 return stmt;
37632 }
37633 }
37634 else if (!flag_openmp) /* flag_openmp_simd */
37635 {
37636 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37637 return NULL_TREE;
37638 }
37639
37640 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37641 cclauses == NULL);
37642 if (cclauses)
37643 {
37644 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
37645 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
37646 }
37647
37648 block = begin_omp_parallel ();
37649 save = cp_parser_begin_omp_structured_block (parser);
37650 cp_parser_statement (parser, NULL_TREE, false, if_p);
37651 cp_parser_end_omp_structured_block (parser, save);
37652 stmt = finish_omp_parallel (clauses, block);
37653 return stmt;
37654 }
37655
37656 /* OpenMP 2.5:
37657 # pragma omp single single-clause[optseq] new-line
37658 structured-block */
37659
37660 #define OMP_SINGLE_CLAUSE_MASK \
37661 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
37664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
37665
37666 static tree
37667 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37668 {
37669 tree stmt = make_node (OMP_SINGLE);
37670 TREE_TYPE (stmt) = void_type_node;
37671 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37672
37673 OMP_SINGLE_CLAUSES (stmt)
37674 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
37675 "#pragma omp single", pragma_tok);
37676 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
37677
37678 return add_stmt (stmt);
37679 }
37680
37681 /* OpenMP 3.0:
37682 # pragma omp task task-clause[optseq] new-line
37683 structured-block */
37684
37685 #define OMP_TASK_CLAUSE_MASK \
37686 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
37695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
37696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
37697
37698 static tree
37699 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37700 {
37701 tree clauses, block;
37702 unsigned int save;
37703
37704 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
37705 "#pragma omp task", pragma_tok);
37706 block = begin_omp_task ();
37707 save = cp_parser_begin_omp_structured_block (parser);
37708 cp_parser_statement (parser, NULL_TREE, false, if_p);
37709 cp_parser_end_omp_structured_block (parser, save);
37710 return finish_omp_task (clauses, block);
37711 }
37712
37713 /* OpenMP 3.0:
37714 # pragma omp taskwait new-line
37715
37716 OpenMP 5.0:
37717 # pragma omp taskwait taskwait-clause[opt] new-line */
37718
37719 #define OMP_TASKWAIT_CLAUSE_MASK \
37720 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
37721
37722 static void
37723 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
37724 {
37725 tree clauses
37726 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
37727 "#pragma omp taskwait", pragma_tok);
37728
37729 if (clauses)
37730 {
37731 tree stmt = make_node (OMP_TASK);
37732 TREE_TYPE (stmt) = void_node;
37733 OMP_TASK_CLAUSES (stmt) = clauses;
37734 OMP_TASK_BODY (stmt) = NULL_TREE;
37735 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37736 add_stmt (stmt);
37737 }
37738 else
37739 finish_omp_taskwait ();
37740 }
37741
37742 /* OpenMP 3.1:
37743 # pragma omp taskyield new-line */
37744
37745 static void
37746 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
37747 {
37748 cp_parser_require_pragma_eol (parser, pragma_tok);
37749 finish_omp_taskyield ();
37750 }
37751
37752 /* OpenMP 4.0:
37753 # pragma omp taskgroup new-line
37754 structured-block
37755
37756 OpenMP 5.0:
37757 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
37758
37759 #define OMP_TASKGROUP_CLAUSE_MASK \
37760 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
37761
37762 static tree
37763 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37764 {
37765 tree clauses
37766 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
37767 "#pragma omp taskgroup", pragma_tok);
37768 return c_finish_omp_taskgroup (input_location,
37769 cp_parser_omp_structured_block (parser,
37770 if_p),
37771 clauses);
37772 }
37773
37774
37775 /* OpenMP 2.5:
37776 # pragma omp threadprivate (variable-list) */
37777
37778 static void
37779 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
37780 {
37781 tree vars;
37782
37783 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37784 cp_parser_require_pragma_eol (parser, pragma_tok);
37785
37786 finish_omp_threadprivate (vars);
37787 }
37788
37789 /* OpenMP 4.0:
37790 # pragma omp cancel cancel-clause[optseq] new-line */
37791
37792 #define OMP_CANCEL_CLAUSE_MASK \
37793 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37794 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37795 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37796 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
37797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
37798
37799 static void
37800 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
37801 {
37802 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
37803 "#pragma omp cancel", pragma_tok);
37804 finish_omp_cancel (clauses);
37805 }
37806
37807 /* OpenMP 4.0:
37808 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
37809
37810 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
37811 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
37812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
37813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
37814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
37815
37816 static void
37817 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
37818 enum pragma_context context)
37819 {
37820 tree clauses;
37821 bool point_seen = false;
37822
37823 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37824 {
37825 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37826 const char *p = IDENTIFIER_POINTER (id);
37827
37828 if (strcmp (p, "point") == 0)
37829 {
37830 cp_lexer_consume_token (parser->lexer);
37831 point_seen = true;
37832 }
37833 }
37834 if (!point_seen)
37835 {
37836 cp_parser_error (parser, "expected %<point%>");
37837 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37838 return;
37839 }
37840
37841 if (context != pragma_compound)
37842 {
37843 if (context == pragma_stmt)
37844 error_at (pragma_tok->location,
37845 "%<#pragma %s%> may only be used in compound statements",
37846 "omp cancellation point");
37847 else
37848 cp_parser_error (parser, "expected declaration specifiers");
37849 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37850 return;
37851 }
37852
37853 clauses = cp_parser_omp_all_clauses (parser,
37854 OMP_CANCELLATION_POINT_CLAUSE_MASK,
37855 "#pragma omp cancellation point",
37856 pragma_tok);
37857 finish_omp_cancellation_point (clauses);
37858 }
37859
37860 /* OpenMP 4.0:
37861 #pragma omp distribute distribute-clause[optseq] new-line
37862 for-loop */
37863
37864 #define OMP_DISTRIBUTE_CLAUSE_MASK \
37865 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
37869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
37870
37871 static tree
37872 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
37873 char *p_name, omp_clause_mask mask, tree *cclauses,
37874 bool *if_p)
37875 {
37876 tree clauses, sb, ret;
37877 unsigned int save;
37878 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37879
37880 strcat (p_name, " distribute");
37881 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
37882
37883 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37884 {
37885 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37886 const char *p = IDENTIFIER_POINTER (id);
37887 bool simd = false;
37888 bool parallel = false;
37889
37890 if (strcmp (p, "simd") == 0)
37891 simd = true;
37892 else
37893 parallel = strcmp (p, "parallel") == 0;
37894 if (parallel || simd)
37895 {
37896 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37897 if (cclauses == NULL)
37898 cclauses = cclauses_buf;
37899 cp_lexer_consume_token (parser->lexer);
37900 if (!flag_openmp) /* flag_openmp_simd */
37901 {
37902 if (simd)
37903 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37904 cclauses, if_p);
37905 else
37906 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37907 cclauses, if_p);
37908 }
37909 sb = begin_omp_structured_block ();
37910 save = cp_parser_begin_omp_structured_block (parser);
37911 if (simd)
37912 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37913 cclauses, if_p);
37914 else
37915 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
37916 cclauses, if_p);
37917 cp_parser_end_omp_structured_block (parser, save);
37918 tree body = finish_omp_structured_block (sb);
37919 if (ret == NULL)
37920 return ret;
37921 ret = make_node (OMP_DISTRIBUTE);
37922 TREE_TYPE (ret) = void_type_node;
37923 OMP_FOR_BODY (ret) = body;
37924 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37925 SET_EXPR_LOCATION (ret, loc);
37926 add_stmt (ret);
37927 return ret;
37928 }
37929 }
37930 if (!flag_openmp) /* flag_openmp_simd */
37931 {
37932 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37933 return NULL_TREE;
37934 }
37935
37936 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37937 cclauses == NULL);
37938 if (cclauses)
37939 {
37940 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
37941 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
37942 }
37943
37944 keep_next_level (true);
37945 sb = begin_omp_structured_block ();
37946 save = cp_parser_begin_omp_structured_block (parser);
37947
37948 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
37949
37950 cp_parser_end_omp_structured_block (parser, save);
37951 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
37952
37953 return ret;
37954 }
37955
37956 /* OpenMP 4.0:
37957 # pragma omp teams teams-clause[optseq] new-line
37958 structured-block */
37959
37960 #define OMP_TEAMS_CLAUSE_MASK \
37961 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
37965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
37966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
37967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
37968
37969 static tree
37970 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
37971 char *p_name, omp_clause_mask mask, tree *cclauses,
37972 bool *if_p)
37973 {
37974 tree clauses, sb, ret;
37975 unsigned int save;
37976 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37977
37978 strcat (p_name, " teams");
37979 mask |= OMP_TEAMS_CLAUSE_MASK;
37980
37981 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37982 {
37983 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37984 const char *p = IDENTIFIER_POINTER (id);
37985 if (strcmp (p, "distribute") == 0)
37986 {
37987 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37988 if (cclauses == NULL)
37989 cclauses = cclauses_buf;
37990
37991 cp_lexer_consume_token (parser->lexer);
37992 if (!flag_openmp) /* flag_openmp_simd */
37993 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37994 cclauses, if_p);
37995 keep_next_level (true);
37996 sb = begin_omp_structured_block ();
37997 save = cp_parser_begin_omp_structured_block (parser);
37998 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
37999 cclauses, if_p);
38000 cp_parser_end_omp_structured_block (parser, save);
38001 tree body = finish_omp_structured_block (sb);
38002 if (ret == NULL)
38003 return ret;
38004 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38005 ret = make_node (OMP_TEAMS);
38006 TREE_TYPE (ret) = void_type_node;
38007 OMP_TEAMS_CLAUSES (ret) = clauses;
38008 OMP_TEAMS_BODY (ret) = body;
38009 OMP_TEAMS_COMBINED (ret) = 1;
38010 SET_EXPR_LOCATION (ret, loc);
38011 return add_stmt (ret);
38012 }
38013 }
38014 if (!flag_openmp) /* flag_openmp_simd */
38015 {
38016 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38017 return NULL_TREE;
38018 }
38019
38020 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
38021 cclauses == NULL);
38022 if (cclauses)
38023 {
38024 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
38025 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38026 }
38027
38028 tree stmt = make_node (OMP_TEAMS);
38029 TREE_TYPE (stmt) = void_type_node;
38030 OMP_TEAMS_CLAUSES (stmt) = clauses;
38031 keep_next_level (true);
38032 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38033 SET_EXPR_LOCATION (stmt, loc);
38034
38035 return add_stmt (stmt);
38036 }
38037
38038 /* OpenMP 4.0:
38039 # pragma omp target data target-data-clause[optseq] new-line
38040 structured-block */
38041
38042 #define OMP_TARGET_DATA_CLAUSE_MASK \
38043 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
38047
38048 static tree
38049 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38050 {
38051 tree clauses
38052 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
38053 "#pragma omp target data", pragma_tok);
38054 int map_seen = 0;
38055 for (tree *pc = &clauses; *pc;)
38056 {
38057 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38058 switch (OMP_CLAUSE_MAP_KIND (*pc))
38059 {
38060 case GOMP_MAP_TO:
38061 case GOMP_MAP_ALWAYS_TO:
38062 case GOMP_MAP_FROM:
38063 case GOMP_MAP_ALWAYS_FROM:
38064 case GOMP_MAP_TOFROM:
38065 case GOMP_MAP_ALWAYS_TOFROM:
38066 case GOMP_MAP_ALLOC:
38067 map_seen = 3;
38068 break;
38069 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38070 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38071 case GOMP_MAP_ALWAYS_POINTER:
38072 break;
38073 default:
38074 map_seen |= 1;
38075 error_at (OMP_CLAUSE_LOCATION (*pc),
38076 "%<#pragma omp target data%> with map-type other "
38077 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38078 "on %<map%> clause");
38079 *pc = OMP_CLAUSE_CHAIN (*pc);
38080 continue;
38081 }
38082 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR)
38083 map_seen = 3;
38084 pc = &OMP_CLAUSE_CHAIN (*pc);
38085 }
38086
38087 if (map_seen != 3)
38088 {
38089 if (map_seen == 0)
38090 error_at (pragma_tok->location,
38091 "%<#pragma omp target data%> must contain at least "
38092 "one %<map%> or %<use_device_ptr%> clause");
38093 return NULL_TREE;
38094 }
38095
38096 tree stmt = make_node (OMP_TARGET_DATA);
38097 TREE_TYPE (stmt) = void_type_node;
38098 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
38099
38100 keep_next_level (true);
38101 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38102
38103 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38104 return add_stmt (stmt);
38105 }
38106
38107 /* OpenMP 4.5:
38108 # pragma omp target enter data target-enter-data-clause[optseq] new-line
38109 structured-block */
38110
38111 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
38112 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38117
38118 static tree
38119 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
38120 enum pragma_context context)
38121 {
38122 bool data_seen = false;
38123 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38124 {
38125 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38126 const char *p = IDENTIFIER_POINTER (id);
38127
38128 if (strcmp (p, "data") == 0)
38129 {
38130 cp_lexer_consume_token (parser->lexer);
38131 data_seen = true;
38132 }
38133 }
38134 if (!data_seen)
38135 {
38136 cp_parser_error (parser, "expected %<data%>");
38137 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38138 return NULL_TREE;
38139 }
38140
38141 if (context == pragma_stmt)
38142 {
38143 error_at (pragma_tok->location,
38144 "%<#pragma %s%> may only be used in compound statements",
38145 "omp target enter data");
38146 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38147 return NULL_TREE;
38148 }
38149
38150 tree clauses
38151 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
38152 "#pragma omp target enter data", pragma_tok);
38153 int map_seen = 0;
38154 for (tree *pc = &clauses; *pc;)
38155 {
38156 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38157 switch (OMP_CLAUSE_MAP_KIND (*pc))
38158 {
38159 case GOMP_MAP_TO:
38160 case GOMP_MAP_ALWAYS_TO:
38161 case GOMP_MAP_ALLOC:
38162 map_seen = 3;
38163 break;
38164 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38165 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38166 case GOMP_MAP_ALWAYS_POINTER:
38167 break;
38168 default:
38169 map_seen |= 1;
38170 error_at (OMP_CLAUSE_LOCATION (*pc),
38171 "%<#pragma omp target enter data%> with map-type other "
38172 "than %<to%> or %<alloc%> on %<map%> clause");
38173 *pc = OMP_CLAUSE_CHAIN (*pc);
38174 continue;
38175 }
38176 pc = &OMP_CLAUSE_CHAIN (*pc);
38177 }
38178
38179 if (map_seen != 3)
38180 {
38181 if (map_seen == 0)
38182 error_at (pragma_tok->location,
38183 "%<#pragma omp target enter data%> must contain at least "
38184 "one %<map%> clause");
38185 return NULL_TREE;
38186 }
38187
38188 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
38189 TREE_TYPE (stmt) = void_type_node;
38190 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
38191 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38192 return add_stmt (stmt);
38193 }
38194
38195 /* OpenMP 4.5:
38196 # pragma omp target exit data target-enter-data-clause[optseq] new-line
38197 structured-block */
38198
38199 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
38200 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38205
38206 static tree
38207 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
38208 enum pragma_context context)
38209 {
38210 bool data_seen = false;
38211 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38212 {
38213 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38214 const char *p = IDENTIFIER_POINTER (id);
38215
38216 if (strcmp (p, "data") == 0)
38217 {
38218 cp_lexer_consume_token (parser->lexer);
38219 data_seen = true;
38220 }
38221 }
38222 if (!data_seen)
38223 {
38224 cp_parser_error (parser, "expected %<data%>");
38225 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38226 return NULL_TREE;
38227 }
38228
38229 if (context == pragma_stmt)
38230 {
38231 error_at (pragma_tok->location,
38232 "%<#pragma %s%> may only be used in compound statements",
38233 "omp target exit data");
38234 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38235 return NULL_TREE;
38236 }
38237
38238 tree clauses
38239 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
38240 "#pragma omp target exit data", pragma_tok);
38241 int map_seen = 0;
38242 for (tree *pc = &clauses; *pc;)
38243 {
38244 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38245 switch (OMP_CLAUSE_MAP_KIND (*pc))
38246 {
38247 case GOMP_MAP_FROM:
38248 case GOMP_MAP_ALWAYS_FROM:
38249 case GOMP_MAP_RELEASE:
38250 case GOMP_MAP_DELETE:
38251 map_seen = 3;
38252 break;
38253 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38254 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38255 case GOMP_MAP_ALWAYS_POINTER:
38256 break;
38257 default:
38258 map_seen |= 1;
38259 error_at (OMP_CLAUSE_LOCATION (*pc),
38260 "%<#pragma omp target exit data%> with map-type other "
38261 "than %<from%>, %<release%> or %<delete%> on %<map%>"
38262 " clause");
38263 *pc = OMP_CLAUSE_CHAIN (*pc);
38264 continue;
38265 }
38266 pc = &OMP_CLAUSE_CHAIN (*pc);
38267 }
38268
38269 if (map_seen != 3)
38270 {
38271 if (map_seen == 0)
38272 error_at (pragma_tok->location,
38273 "%<#pragma omp target exit data%> must contain at least "
38274 "one %<map%> clause");
38275 return NULL_TREE;
38276 }
38277
38278 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
38279 TREE_TYPE (stmt) = void_type_node;
38280 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
38281 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38282 return add_stmt (stmt);
38283 }
38284
38285 /* OpenMP 4.0:
38286 # pragma omp target update target-update-clause[optseq] new-line */
38287
38288 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
38289 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
38290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
38291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38295
38296 static bool
38297 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
38298 enum pragma_context context)
38299 {
38300 if (context == pragma_stmt)
38301 {
38302 error_at (pragma_tok->location,
38303 "%<#pragma %s%> may only be used in compound statements",
38304 "omp target update");
38305 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38306 return false;
38307 }
38308
38309 tree clauses
38310 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
38311 "#pragma omp target update", pragma_tok);
38312 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
38313 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
38314 {
38315 error_at (pragma_tok->location,
38316 "%<#pragma omp target update%> must contain at least one "
38317 "%<from%> or %<to%> clauses");
38318 return false;
38319 }
38320
38321 tree stmt = make_node (OMP_TARGET_UPDATE);
38322 TREE_TYPE (stmt) = void_type_node;
38323 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
38324 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38325 add_stmt (stmt);
38326 return false;
38327 }
38328
38329 /* OpenMP 4.0:
38330 # pragma omp target target-clause[optseq] new-line
38331 structured-block */
38332
38333 #define OMP_TARGET_CLAUSE_MASK \
38334 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
38335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
38336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
38338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
38342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
38343
38344 static bool
38345 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
38346 enum pragma_context context, bool *if_p)
38347 {
38348 tree *pc = NULL, stmt;
38349
38350 if (flag_openmp)
38351 omp_requires_mask
38352 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
38353
38354 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38355 {
38356 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38357 const char *p = IDENTIFIER_POINTER (id);
38358 enum tree_code ccode = ERROR_MARK;
38359
38360 if (strcmp (p, "teams") == 0)
38361 ccode = OMP_TEAMS;
38362 else if (strcmp (p, "parallel") == 0)
38363 ccode = OMP_PARALLEL;
38364 else if (strcmp (p, "simd") == 0)
38365 ccode = OMP_SIMD;
38366 if (ccode != ERROR_MARK)
38367 {
38368 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
38369 char p_name[sizeof ("#pragma omp target teams distribute "
38370 "parallel for simd")];
38371
38372 cp_lexer_consume_token (parser->lexer);
38373 strcpy (p_name, "#pragma omp target");
38374 if (!flag_openmp) /* flag_openmp_simd */
38375 {
38376 tree stmt;
38377 switch (ccode)
38378 {
38379 case OMP_TEAMS:
38380 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
38381 OMP_TARGET_CLAUSE_MASK,
38382 cclauses, if_p);
38383 break;
38384 case OMP_PARALLEL:
38385 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38386 OMP_TARGET_CLAUSE_MASK,
38387 cclauses, if_p);
38388 break;
38389 case OMP_SIMD:
38390 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
38391 OMP_TARGET_CLAUSE_MASK,
38392 cclauses, if_p);
38393 break;
38394 default:
38395 gcc_unreachable ();
38396 }
38397 return stmt != NULL_TREE;
38398 }
38399 keep_next_level (true);
38400 tree sb = begin_omp_structured_block (), ret;
38401 unsigned save = cp_parser_begin_omp_structured_block (parser);
38402 switch (ccode)
38403 {
38404 case OMP_TEAMS:
38405 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
38406 OMP_TARGET_CLAUSE_MASK, cclauses,
38407 if_p);
38408 break;
38409 case OMP_PARALLEL:
38410 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
38411 OMP_TARGET_CLAUSE_MASK, cclauses,
38412 if_p);
38413 break;
38414 case OMP_SIMD:
38415 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
38416 OMP_TARGET_CLAUSE_MASK, cclauses,
38417 if_p);
38418 break;
38419 default:
38420 gcc_unreachable ();
38421 }
38422 cp_parser_end_omp_structured_block (parser, save);
38423 tree body = finish_omp_structured_block (sb);
38424 if (ret == NULL_TREE)
38425 return false;
38426 if (ccode == OMP_TEAMS && !processing_template_decl)
38427 {
38428 /* For combined target teams, ensure the num_teams and
38429 thread_limit clause expressions are evaluated on the host,
38430 before entering the target construct. */
38431 tree c;
38432 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
38433 c; c = OMP_CLAUSE_CHAIN (c))
38434 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
38435 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
38436 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
38437 {
38438 tree expr = OMP_CLAUSE_OPERAND (c, 0);
38439 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
38440 if (expr == error_mark_node)
38441 continue;
38442 tree tmp = TARGET_EXPR_SLOT (expr);
38443 add_stmt (expr);
38444 OMP_CLAUSE_OPERAND (c, 0) = expr;
38445 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
38446 OMP_CLAUSE_FIRSTPRIVATE);
38447 OMP_CLAUSE_DECL (tc) = tmp;
38448 OMP_CLAUSE_CHAIN (tc)
38449 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38450 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
38451 }
38452 }
38453 tree stmt = make_node (OMP_TARGET);
38454 TREE_TYPE (stmt) = void_type_node;
38455 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
38456 OMP_TARGET_BODY (stmt) = body;
38457 OMP_TARGET_COMBINED (stmt) = 1;
38458 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38459 add_stmt (stmt);
38460 pc = &OMP_TARGET_CLAUSES (stmt);
38461 goto check_clauses;
38462 }
38463 else if (!flag_openmp) /* flag_openmp_simd */
38464 {
38465 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38466 return false;
38467 }
38468 else if (strcmp (p, "data") == 0)
38469 {
38470 cp_lexer_consume_token (parser->lexer);
38471 cp_parser_omp_target_data (parser, pragma_tok, if_p);
38472 return true;
38473 }
38474 else if (strcmp (p, "enter") == 0)
38475 {
38476 cp_lexer_consume_token (parser->lexer);
38477 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
38478 return false;
38479 }
38480 else if (strcmp (p, "exit") == 0)
38481 {
38482 cp_lexer_consume_token (parser->lexer);
38483 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
38484 return false;
38485 }
38486 else if (strcmp (p, "update") == 0)
38487 {
38488 cp_lexer_consume_token (parser->lexer);
38489 return cp_parser_omp_target_update (parser, pragma_tok, context);
38490 }
38491 }
38492 if (!flag_openmp) /* flag_openmp_simd */
38493 {
38494 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38495 return false;
38496 }
38497
38498 stmt = make_node (OMP_TARGET);
38499 TREE_TYPE (stmt) = void_type_node;
38500
38501 OMP_TARGET_CLAUSES (stmt)
38502 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
38503 "#pragma omp target", pragma_tok);
38504 pc = &OMP_TARGET_CLAUSES (stmt);
38505 keep_next_level (true);
38506 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
38507
38508 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38509 add_stmt (stmt);
38510
38511 check_clauses:
38512 while (*pc)
38513 {
38514 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
38515 switch (OMP_CLAUSE_MAP_KIND (*pc))
38516 {
38517 case GOMP_MAP_TO:
38518 case GOMP_MAP_ALWAYS_TO:
38519 case GOMP_MAP_FROM:
38520 case GOMP_MAP_ALWAYS_FROM:
38521 case GOMP_MAP_TOFROM:
38522 case GOMP_MAP_ALWAYS_TOFROM:
38523 case GOMP_MAP_ALLOC:
38524 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38525 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
38526 case GOMP_MAP_ALWAYS_POINTER:
38527 break;
38528 default:
38529 error_at (OMP_CLAUSE_LOCATION (*pc),
38530 "%<#pragma omp target%> with map-type other "
38531 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
38532 "on %<map%> clause");
38533 *pc = OMP_CLAUSE_CHAIN (*pc);
38534 continue;
38535 }
38536 pc = &OMP_CLAUSE_CHAIN (*pc);
38537 }
38538 return true;
38539 }
38540
38541 /* OpenACC 2.0:
38542 # pragma acc cache (variable-list) new-line
38543 */
38544
38545 static tree
38546 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
38547 {
38548 tree stmt, clauses;
38549
38550 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
38551 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38552
38553 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
38554
38555 stmt = make_node (OACC_CACHE);
38556 TREE_TYPE (stmt) = void_type_node;
38557 OACC_CACHE_CLAUSES (stmt) = clauses;
38558 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38559 add_stmt (stmt);
38560
38561 return stmt;
38562 }
38563
38564 /* OpenACC 2.0:
38565 # pragma acc data oacc-data-clause[optseq] new-line
38566 structured-block */
38567
38568 #define OACC_DATA_CLAUSE_MASK \
38569 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38576
38577 static tree
38578 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38579 {
38580 tree stmt, clauses, block;
38581 unsigned int save;
38582
38583 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
38584 "#pragma acc data", pragma_tok);
38585
38586 block = begin_omp_parallel ();
38587 save = cp_parser_begin_omp_structured_block (parser);
38588 cp_parser_statement (parser, NULL_TREE, false, if_p);
38589 cp_parser_end_omp_structured_block (parser, save);
38590 stmt = finish_oacc_data (clauses, block);
38591 return stmt;
38592 }
38593
38594 /* OpenACC 2.0:
38595 # pragma acc host_data <clauses> new-line
38596 structured-block */
38597
38598 #define OACC_HOST_DATA_CLAUSE_MASK \
38599 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
38600
38601 static tree
38602 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38603 {
38604 tree stmt, clauses, block;
38605 unsigned int save;
38606
38607 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
38608 "#pragma acc host_data", pragma_tok);
38609
38610 block = begin_omp_parallel ();
38611 save = cp_parser_begin_omp_structured_block (parser);
38612 cp_parser_statement (parser, NULL_TREE, false, if_p);
38613 cp_parser_end_omp_structured_block (parser, save);
38614 stmt = finish_oacc_host_data (clauses, block);
38615 return stmt;
38616 }
38617
38618 /* OpenACC 2.0:
38619 # pragma acc declare oacc-data-clause[optseq] new-line
38620 */
38621
38622 #define OACC_DECLARE_CLAUSE_MASK \
38623 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
38629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
38630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
38631
38632 static tree
38633 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
38634 {
38635 tree clauses, stmt;
38636 bool error = false;
38637
38638 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
38639 "#pragma acc declare", pragma_tok, true);
38640
38641
38642 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38643 {
38644 error_at (pragma_tok->location,
38645 "no valid clauses specified in %<#pragma acc declare%>");
38646 return NULL_TREE;
38647 }
38648
38649 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
38650 {
38651 location_t loc = OMP_CLAUSE_LOCATION (t);
38652 tree decl = OMP_CLAUSE_DECL (t);
38653 if (!DECL_P (decl))
38654 {
38655 error_at (loc, "array section in %<#pragma acc declare%>");
38656 error = true;
38657 continue;
38658 }
38659 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
38660 switch (OMP_CLAUSE_MAP_KIND (t))
38661 {
38662 case GOMP_MAP_FIRSTPRIVATE_POINTER:
38663 case GOMP_MAP_ALLOC:
38664 case GOMP_MAP_TO:
38665 case GOMP_MAP_FORCE_DEVICEPTR:
38666 case GOMP_MAP_DEVICE_RESIDENT:
38667 break;
38668
38669 case GOMP_MAP_LINK:
38670 if (!global_bindings_p ()
38671 && (TREE_STATIC (decl)
38672 || !DECL_EXTERNAL (decl)))
38673 {
38674 error_at (loc,
38675 "%qD must be a global variable in "
38676 "%<#pragma acc declare link%>",
38677 decl);
38678 error = true;
38679 continue;
38680 }
38681 break;
38682
38683 default:
38684 if (global_bindings_p ())
38685 {
38686 error_at (loc, "invalid OpenACC clause at file scope");
38687 error = true;
38688 continue;
38689 }
38690 if (DECL_EXTERNAL (decl))
38691 {
38692 error_at (loc,
38693 "invalid use of %<extern%> variable %qD "
38694 "in %<#pragma acc declare%>", decl);
38695 error = true;
38696 continue;
38697 }
38698 else if (TREE_PUBLIC (decl))
38699 {
38700 error_at (loc,
38701 "invalid use of %<global%> variable %qD "
38702 "in %<#pragma acc declare%>", decl);
38703 error = true;
38704 continue;
38705 }
38706 break;
38707 }
38708
38709 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
38710 || lookup_attribute ("omp declare target link",
38711 DECL_ATTRIBUTES (decl)))
38712 {
38713 error_at (loc, "variable %qD used more than once with "
38714 "%<#pragma acc declare%>", decl);
38715 error = true;
38716 continue;
38717 }
38718
38719 if (!error)
38720 {
38721 tree id;
38722
38723 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
38724 id = get_identifier ("omp declare target link");
38725 else
38726 id = get_identifier ("omp declare target");
38727
38728 DECL_ATTRIBUTES (decl)
38729 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
38730 if (global_bindings_p ())
38731 {
38732 symtab_node *node = symtab_node::get (decl);
38733 if (node != NULL)
38734 {
38735 node->offloadable = 1;
38736 if (ENABLE_OFFLOADING)
38737 {
38738 g->have_offload = true;
38739 if (is_a <varpool_node *> (node))
38740 vec_safe_push (offload_vars, decl);
38741 }
38742 }
38743 }
38744 }
38745 }
38746
38747 if (error || global_bindings_p ())
38748 return NULL_TREE;
38749
38750 stmt = make_node (OACC_DECLARE);
38751 TREE_TYPE (stmt) = void_type_node;
38752 OACC_DECLARE_CLAUSES (stmt) = clauses;
38753 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38754
38755 add_stmt (stmt);
38756
38757 return NULL_TREE;
38758 }
38759
38760 /* OpenACC 2.0:
38761 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
38762
38763 or
38764
38765 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
38766
38767 LOC is the location of the #pragma token.
38768 */
38769
38770 #define OACC_ENTER_DATA_CLAUSE_MASK \
38771 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38776
38777 #define OACC_EXIT_DATA_CLAUSE_MASK \
38778 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
38782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
38783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38784
38785 static tree
38786 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
38787 bool enter)
38788 {
38789 location_t loc = pragma_tok->location;
38790 tree stmt, clauses;
38791 const char *p = "";
38792
38793 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38794 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38795
38796 if (strcmp (p, "data") != 0)
38797 {
38798 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
38799 enter ? "enter" : "exit");
38800 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38801 return NULL_TREE;
38802 }
38803
38804 cp_lexer_consume_token (parser->lexer);
38805
38806 if (enter)
38807 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
38808 "#pragma acc enter data", pragma_tok);
38809 else
38810 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
38811 "#pragma acc exit data", pragma_tok);
38812
38813 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38814 {
38815 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
38816 enter ? "enter" : "exit");
38817 return NULL_TREE;
38818 }
38819
38820 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
38821 TREE_TYPE (stmt) = void_type_node;
38822 OMP_STANDALONE_CLAUSES (stmt) = clauses;
38823 SET_EXPR_LOCATION (stmt, loc);
38824 add_stmt (stmt);
38825 return stmt;
38826 }
38827
38828 /* OpenACC 2.0:
38829 # pragma acc loop oacc-loop-clause[optseq] new-line
38830 structured-block */
38831
38832 #define OACC_LOOP_CLAUSE_MASK \
38833 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
38834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
38837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
38838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
38839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
38840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
38841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
38842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
38843
38844 static tree
38845 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
38846 omp_clause_mask mask, tree *cclauses, bool *if_p)
38847 {
38848 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
38849
38850 strcat (p_name, " loop");
38851 mask |= OACC_LOOP_CLAUSE_MASK;
38852
38853 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
38854 cclauses == NULL);
38855 if (cclauses)
38856 {
38857 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
38858 if (*cclauses)
38859 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
38860 if (clauses)
38861 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
38862 }
38863
38864 tree block = begin_omp_structured_block ();
38865 int save = cp_parser_begin_omp_structured_block (parser);
38866 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
38867 cp_parser_end_omp_structured_block (parser, save);
38868 add_stmt (finish_omp_structured_block (block));
38869
38870 return stmt;
38871 }
38872
38873 /* OpenACC 2.0:
38874 # pragma acc kernels oacc-kernels-clause[optseq] new-line
38875 structured-block
38876
38877 or
38878
38879 # pragma acc parallel oacc-parallel-clause[optseq] new-line
38880 structured-block
38881 */
38882
38883 #define OACC_KERNELS_CLAUSE_MASK \
38884 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38890 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38891 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38892 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38896 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38897
38898 #define OACC_PARALLEL_CLAUSE_MASK \
38899 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38900 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
38901 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
38902 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
38903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
38904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
38905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
38906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
38907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
38909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
38910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
38911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
38912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
38913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
38914 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
38915
38916 static tree
38917 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
38918 char *p_name, bool *if_p)
38919 {
38920 omp_clause_mask mask;
38921 enum tree_code code;
38922 switch (cp_parser_pragma_kind (pragma_tok))
38923 {
38924 case PRAGMA_OACC_KERNELS:
38925 strcat (p_name, " kernels");
38926 mask = OACC_KERNELS_CLAUSE_MASK;
38927 code = OACC_KERNELS;
38928 break;
38929 case PRAGMA_OACC_PARALLEL:
38930 strcat (p_name, " parallel");
38931 mask = OACC_PARALLEL_CLAUSE_MASK;
38932 code = OACC_PARALLEL;
38933 break;
38934 default:
38935 gcc_unreachable ();
38936 }
38937
38938 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38939 {
38940 const char *p
38941 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
38942 if (strcmp (p, "loop") == 0)
38943 {
38944 cp_lexer_consume_token (parser->lexer);
38945 tree block = begin_omp_parallel ();
38946 tree clauses;
38947 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
38948 &clauses, if_p);
38949 protected_set_expr_location (stmt, pragma_tok->location);
38950 return finish_omp_construct (code, block, clauses);
38951 }
38952 }
38953
38954 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
38955
38956 tree block = begin_omp_parallel ();
38957 unsigned int save = cp_parser_begin_omp_structured_block (parser);
38958 cp_parser_statement (parser, NULL_TREE, false, if_p);
38959 cp_parser_end_omp_structured_block (parser, save);
38960 return finish_omp_construct (code, block, clauses);
38961 }
38962
38963 /* OpenACC 2.0:
38964 # pragma acc update oacc-update-clause[optseq] new-line
38965 */
38966
38967 #define OACC_UPDATE_CLAUSE_MASK \
38968 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
38969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
38970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
38971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
38972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
38973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
38974
38975 static tree
38976 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
38977 {
38978 tree stmt, clauses;
38979
38980 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
38981 "#pragma acc update", pragma_tok);
38982
38983 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
38984 {
38985 error_at (pragma_tok->location,
38986 "%<#pragma acc update%> must contain at least one "
38987 "%<device%> or %<host%> or %<self%> clause");
38988 return NULL_TREE;
38989 }
38990
38991 stmt = make_node (OACC_UPDATE);
38992 TREE_TYPE (stmt) = void_type_node;
38993 OACC_UPDATE_CLAUSES (stmt) = clauses;
38994 SET_EXPR_LOCATION (stmt, pragma_tok->location);
38995 add_stmt (stmt);
38996 return stmt;
38997 }
38998
38999 /* OpenACC 2.0:
39000 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
39001
39002 LOC is the location of the #pragma token.
39003 */
39004
39005 #define OACC_WAIT_CLAUSE_MASK \
39006 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
39007
39008 static tree
39009 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
39010 {
39011 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
39012 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39013
39014 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
39015 list = cp_parser_oacc_wait_list (parser, loc, list);
39016
39017 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
39018 "#pragma acc wait", pragma_tok);
39019
39020 stmt = c_finish_oacc_wait (loc, list, clauses);
39021 stmt = finish_expr_stmt (stmt);
39022
39023 return stmt;
39024 }
39025
39026 /* OpenMP 4.0:
39027 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
39028
39029 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
39030 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39031 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39032 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
39034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
39035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
39036
39037 static void
39038 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
39039 enum pragma_context context)
39040 {
39041 bool first_p = parser->omp_declare_simd == NULL;
39042 cp_omp_declare_simd_data data;
39043 if (first_p)
39044 {
39045 data.error_seen = false;
39046 data.fndecl_seen = false;
39047 data.tokens = vNULL;
39048 data.clauses = NULL_TREE;
39049 /* It is safe to take the address of a local variable; it will only be
39050 used while this scope is live. */
39051 parser->omp_declare_simd = &data;
39052 }
39053
39054 /* Store away all pragma tokens. */
39055 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39056 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39057 cp_lexer_consume_token (parser->lexer);
39058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39059 parser->omp_declare_simd->error_seen = true;
39060 cp_parser_require_pragma_eol (parser, pragma_tok);
39061 struct cp_token_cache *cp
39062 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
39063 parser->omp_declare_simd->tokens.safe_push (cp);
39064
39065 if (first_p)
39066 {
39067 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
39068 cp_parser_pragma (parser, context, NULL);
39069 switch (context)
39070 {
39071 case pragma_external:
39072 cp_parser_declaration (parser);
39073 break;
39074 case pragma_member:
39075 cp_parser_member_declaration (parser);
39076 break;
39077 case pragma_objc_icode:
39078 cp_parser_block_declaration (parser, /*statement_p=*/false);
39079 break;
39080 default:
39081 cp_parser_declaration_statement (parser);
39082 break;
39083 }
39084 if (parser->omp_declare_simd
39085 && !parser->omp_declare_simd->error_seen
39086 && !parser->omp_declare_simd->fndecl_seen)
39087 error_at (pragma_tok->location,
39088 "%<#pragma omp declare simd%> not immediately followed by "
39089 "function declaration or definition");
39090 data.tokens.release ();
39091 parser->omp_declare_simd = NULL;
39092 }
39093 }
39094
39095 /* Finalize #pragma omp declare simd clauses after direct declarator has
39096 been parsed, and put that into "omp declare simd" attribute. */
39097
39098 static tree
39099 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
39100 {
39101 struct cp_token_cache *ce;
39102 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
39103 int i;
39104
39105 if (!data->error_seen && data->fndecl_seen)
39106 {
39107 error ("%<#pragma omp declare simd%> not immediately followed by "
39108 "a single function declaration or definition");
39109 data->error_seen = true;
39110 }
39111 if (data->error_seen)
39112 return attrs;
39113
39114 FOR_EACH_VEC_ELT (data->tokens, i, ce)
39115 {
39116 tree c, cl;
39117
39118 cp_parser_push_lexer_for_tokens (parser, ce);
39119 parser->lexer->in_pragma = true;
39120 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
39121 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
39122 cp_lexer_consume_token (parser->lexer);
39123 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
39124 "#pragma omp declare simd", pragma_tok);
39125 cp_parser_pop_lexer (parser);
39126 if (cl)
39127 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
39128 c = build_tree_list (get_identifier ("omp declare simd"), cl);
39129 TREE_CHAIN (c) = attrs;
39130 if (processing_template_decl)
39131 ATTR_IS_DEPENDENT (c) = 1;
39132 attrs = c;
39133 }
39134
39135 data->fndecl_seen = true;
39136 return attrs;
39137 }
39138
39139
39140 /* OpenMP 4.0:
39141 # pragma omp declare target new-line
39142 declarations and definitions
39143 # pragma omp end declare target new-line
39144
39145 OpenMP 4.5:
39146 # pragma omp declare target ( extended-list ) new-line
39147
39148 # pragma omp declare target declare-target-clauses[seq] new-line */
39149
39150 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
39151 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
39153
39154 static void
39155 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
39156 {
39157 tree clauses = NULL_TREE;
39158 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39159 clauses
39160 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
39161 "#pragma omp declare target", pragma_tok);
39162 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39163 {
39164 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
39165 clauses);
39166 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
39167 cp_parser_require_pragma_eol (parser, pragma_tok);
39168 }
39169 else
39170 {
39171 cp_parser_require_pragma_eol (parser, pragma_tok);
39172 scope_chain->omp_declare_target_attribute++;
39173 return;
39174 }
39175 if (scope_chain->omp_declare_target_attribute)
39176 error_at (pragma_tok->location,
39177 "%<#pragma omp declare target%> with clauses in between "
39178 "%<#pragma omp declare target%> without clauses and "
39179 "%<#pragma omp end declare target%>");
39180 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
39181 {
39182 tree t = OMP_CLAUSE_DECL (c), id;
39183 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
39184 tree at2 = lookup_attribute ("omp declare target link",
39185 DECL_ATTRIBUTES (t));
39186 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
39187 {
39188 id = get_identifier ("omp declare target link");
39189 std::swap (at1, at2);
39190 }
39191 else
39192 id = get_identifier ("omp declare target");
39193 if (at2)
39194 {
39195 error_at (OMP_CLAUSE_LOCATION (c),
39196 "%qD specified both in declare target %<link%> and %<to%>"
39197 " clauses", t);
39198 continue;
39199 }
39200 if (!at1)
39201 {
39202 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
39203 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
39204 continue;
39205
39206 symtab_node *node = symtab_node::get (t);
39207 if (node != NULL)
39208 {
39209 node->offloadable = 1;
39210 if (ENABLE_OFFLOADING)
39211 {
39212 g->have_offload = true;
39213 if (is_a <varpool_node *> (node))
39214 vec_safe_push (offload_vars, t);
39215 }
39216 }
39217 }
39218 }
39219 }
39220
39221 static void
39222 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
39223 {
39224 const char *p = "";
39225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39226 {
39227 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39228 p = IDENTIFIER_POINTER (id);
39229 }
39230 if (strcmp (p, "declare") == 0)
39231 {
39232 cp_lexer_consume_token (parser->lexer);
39233 p = "";
39234 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39235 {
39236 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39237 p = IDENTIFIER_POINTER (id);
39238 }
39239 if (strcmp (p, "target") == 0)
39240 cp_lexer_consume_token (parser->lexer);
39241 else
39242 {
39243 cp_parser_error (parser, "expected %<target%>");
39244 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39245 return;
39246 }
39247 }
39248 else
39249 {
39250 cp_parser_error (parser, "expected %<declare%>");
39251 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39252 return;
39253 }
39254 cp_parser_require_pragma_eol (parser, pragma_tok);
39255 if (!scope_chain->omp_declare_target_attribute)
39256 error_at (pragma_tok->location,
39257 "%<#pragma omp end declare target%> without corresponding "
39258 "%<#pragma omp declare target%>");
39259 else
39260 scope_chain->omp_declare_target_attribute--;
39261 }
39262
39263 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
39264 expression and optional initializer clause of
39265 #pragma omp declare reduction. We store the expression(s) as
39266 either 3, 6 or 7 special statements inside of the artificial function's
39267 body. The first two statements are DECL_EXPRs for the artificial
39268 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
39269 expression that uses those variables.
39270 If there was any INITIALIZER clause, this is followed by further statements,
39271 the fourth and fifth statements are DECL_EXPRs for the artificial
39272 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
39273 constructor variant (first token after open paren is not omp_priv),
39274 then the sixth statement is a statement with the function call expression
39275 that uses the OMP_PRIV and optionally OMP_ORIG variable.
39276 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
39277 to initialize the OMP_PRIV artificial variable and there is seventh
39278 statement, a DECL_EXPR of the OMP_PRIV statement again. */
39279
39280 static bool
39281 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
39282 {
39283 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
39284 gcc_assert (TYPE_REF_P (type));
39285 type = TREE_TYPE (type);
39286 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
39287 DECL_ARTIFICIAL (omp_out) = 1;
39288 pushdecl (omp_out);
39289 add_decl_expr (omp_out);
39290 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
39291 DECL_ARTIFICIAL (omp_in) = 1;
39292 pushdecl (omp_in);
39293 add_decl_expr (omp_in);
39294 tree combiner;
39295 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
39296
39297 keep_next_level (true);
39298 tree block = begin_omp_structured_block ();
39299 combiner = cp_parser_expression (parser);
39300 finish_expr_stmt (combiner);
39301 block = finish_omp_structured_block (block);
39302 add_stmt (block);
39303
39304 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
39305 return false;
39306
39307 const char *p = "";
39308 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39309 {
39310 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39311 p = IDENTIFIER_POINTER (id);
39312 }
39313
39314 if (strcmp (p, "initializer") == 0)
39315 {
39316 cp_lexer_consume_token (parser->lexer);
39317 matching_parens parens;
39318 if (!parens.require_open (parser))
39319 return false;
39320
39321 p = "";
39322 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39323 {
39324 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39325 p = IDENTIFIER_POINTER (id);
39326 }
39327
39328 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
39329 DECL_ARTIFICIAL (omp_priv) = 1;
39330 pushdecl (omp_priv);
39331 add_decl_expr (omp_priv);
39332 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
39333 DECL_ARTIFICIAL (omp_orig) = 1;
39334 pushdecl (omp_orig);
39335 add_decl_expr (omp_orig);
39336
39337 keep_next_level (true);
39338 block = begin_omp_structured_block ();
39339
39340 bool ctor = false;
39341 if (strcmp (p, "omp_priv") == 0)
39342 {
39343 bool is_direct_init, is_non_constant_init;
39344 ctor = true;
39345 cp_lexer_consume_token (parser->lexer);
39346 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
39347 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
39348 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39349 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
39350 == CPP_CLOSE_PAREN
39351 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
39352 == CPP_CLOSE_PAREN))
39353 {
39354 finish_omp_structured_block (block);
39355 error ("invalid initializer clause");
39356 return false;
39357 }
39358 initializer = cp_parser_initializer (parser, &is_direct_init,
39359 &is_non_constant_init);
39360 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
39361 NULL_TREE, LOOKUP_ONLYCONVERTING);
39362 }
39363 else
39364 {
39365 cp_parser_parse_tentatively (parser);
39366 /* Don't create location wrapper nodes here. */
39367 auto_suppress_location_wrappers sentinel;
39368 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
39369 /*check_dependency_p=*/true,
39370 /*template_p=*/NULL,
39371 /*declarator_p=*/false,
39372 /*optional_p=*/false);
39373 vec<tree, va_gc> *args;
39374 if (fn_name == error_mark_node
39375 || cp_parser_error_occurred (parser)
39376 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
39377 || ((args = cp_parser_parenthesized_expression_list
39378 (parser, non_attr, /*cast_p=*/false,
39379 /*allow_expansion_p=*/true,
39380 /*non_constant_p=*/NULL)),
39381 cp_parser_error_occurred (parser)))
39382 {
39383 finish_omp_structured_block (block);
39384 cp_parser_abort_tentative_parse (parser);
39385 cp_parser_error (parser, "expected id-expression (arguments)");
39386 return false;
39387 }
39388 unsigned int i;
39389 tree arg;
39390 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
39391 if (arg == omp_priv
39392 || (TREE_CODE (arg) == ADDR_EXPR
39393 && TREE_OPERAND (arg, 0) == omp_priv))
39394 break;
39395 cp_parser_abort_tentative_parse (parser);
39396 if (arg == NULL_TREE)
39397 error ("one of the initializer call arguments should be %<omp_priv%>"
39398 " or %<&omp_priv%>");
39399 initializer = cp_parser_postfix_expression (parser, false, false, false,
39400 false, NULL);
39401 finish_expr_stmt (initializer);
39402 }
39403
39404 block = finish_omp_structured_block (block);
39405 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
39406 add_stmt (block);
39407
39408 if (ctor)
39409 add_decl_expr (omp_orig);
39410
39411 if (!parens.require_close (parser))
39412 return false;
39413 }
39414
39415 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
39416 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
39417 UNKNOWN_LOCATION);
39418
39419 return true;
39420 }
39421
39422 /* OpenMP 4.0
39423 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39424 initializer-clause[opt] new-line
39425
39426 initializer-clause:
39427 initializer (omp_priv initializer)
39428 initializer (function-name (argument-list)) */
39429
39430 static void
39431 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
39432 enum pragma_context)
39433 {
39434 auto_vec<tree> types;
39435 enum tree_code reduc_code = ERROR_MARK;
39436 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
39437 unsigned int i;
39438 cp_token *first_token;
39439 cp_token_cache *cp;
39440 int errs;
39441 void *p;
39442
39443 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
39444 p = obstack_alloc (&declarator_obstack, 0);
39445
39446 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39447 goto fail;
39448
39449 switch (cp_lexer_peek_token (parser->lexer)->type)
39450 {
39451 case CPP_PLUS:
39452 reduc_code = PLUS_EXPR;
39453 break;
39454 case CPP_MULT:
39455 reduc_code = MULT_EXPR;
39456 break;
39457 case CPP_MINUS:
39458 reduc_code = MINUS_EXPR;
39459 break;
39460 case CPP_AND:
39461 reduc_code = BIT_AND_EXPR;
39462 break;
39463 case CPP_XOR:
39464 reduc_code = BIT_XOR_EXPR;
39465 break;
39466 case CPP_OR:
39467 reduc_code = BIT_IOR_EXPR;
39468 break;
39469 case CPP_AND_AND:
39470 reduc_code = TRUTH_ANDIF_EXPR;
39471 break;
39472 case CPP_OR_OR:
39473 reduc_code = TRUTH_ORIF_EXPR;
39474 break;
39475 case CPP_NAME:
39476 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
39477 break;
39478 default:
39479 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
39480 "%<|%>, %<&&%>, %<||%> or identifier");
39481 goto fail;
39482 }
39483
39484 if (reduc_code != ERROR_MARK)
39485 cp_lexer_consume_token (parser->lexer);
39486
39487 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
39488 if (reduc_id == error_mark_node)
39489 goto fail;
39490
39491 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39492 goto fail;
39493
39494 /* Types may not be defined in declare reduction type list. */
39495 const char *saved_message;
39496 saved_message = parser->type_definition_forbidden_message;
39497 parser->type_definition_forbidden_message
39498 = G_("types may not be defined in declare reduction type list");
39499 bool saved_colon_corrects_to_scope_p;
39500 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39501 parser->colon_corrects_to_scope_p = false;
39502 bool saved_colon_doesnt_start_class_def_p;
39503 saved_colon_doesnt_start_class_def_p
39504 = parser->colon_doesnt_start_class_def_p;
39505 parser->colon_doesnt_start_class_def_p = true;
39506
39507 while (true)
39508 {
39509 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39510 type = cp_parser_type_id (parser);
39511 if (type == error_mark_node)
39512 ;
39513 else if (ARITHMETIC_TYPE_P (type)
39514 && (orig_reduc_id == NULL_TREE
39515 || (TREE_CODE (type) != COMPLEX_TYPE
39516 && (id_equal (orig_reduc_id, "min")
39517 || id_equal (orig_reduc_id, "max")))))
39518 error_at (loc, "predeclared arithmetic type %qT in "
39519 "%<#pragma omp declare reduction%>", type);
39520 else if (TREE_CODE (type) == FUNCTION_TYPE
39521 || TREE_CODE (type) == METHOD_TYPE
39522 || TREE_CODE (type) == ARRAY_TYPE)
39523 error_at (loc, "function or array type %qT in "
39524 "%<#pragma omp declare reduction%>", type);
39525 else if (TYPE_REF_P (type))
39526 error_at (loc, "reference type %qT in "
39527 "%<#pragma omp declare reduction%>", type);
39528 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
39529 error_at (loc, "const, volatile or __restrict qualified type %qT in "
39530 "%<#pragma omp declare reduction%>", type);
39531 else
39532 types.safe_push (type);
39533
39534 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39535 cp_lexer_consume_token (parser->lexer);
39536 else
39537 break;
39538 }
39539
39540 /* Restore the saved message. */
39541 parser->type_definition_forbidden_message = saved_message;
39542 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39543 parser->colon_doesnt_start_class_def_p
39544 = saved_colon_doesnt_start_class_def_p;
39545
39546 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
39547 || types.is_empty ())
39548 {
39549 fail:
39550 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39551 goto done;
39552 }
39553
39554 first_token = cp_lexer_peek_token (parser->lexer);
39555 cp = NULL;
39556 errs = errorcount;
39557 FOR_EACH_VEC_ELT (types, i, type)
39558 {
39559 tree fntype
39560 = build_function_type_list (void_type_node,
39561 cp_build_reference_type (type, false),
39562 NULL_TREE);
39563 tree this_reduc_id = reduc_id;
39564 if (!dependent_type_p (type))
39565 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
39566 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
39567 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
39568 DECL_ARTIFICIAL (fndecl) = 1;
39569 DECL_EXTERNAL (fndecl) = 1;
39570 DECL_DECLARED_INLINE_P (fndecl) = 1;
39571 DECL_IGNORED_P (fndecl) = 1;
39572 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
39573 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
39574 DECL_ATTRIBUTES (fndecl)
39575 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
39576 DECL_ATTRIBUTES (fndecl));
39577 if (processing_template_decl)
39578 fndecl = push_template_decl (fndecl);
39579 bool block_scope = false;
39580 tree block = NULL_TREE;
39581 if (current_function_decl)
39582 {
39583 block_scope = true;
39584 DECL_CONTEXT (fndecl) = global_namespace;
39585 if (!processing_template_decl)
39586 pushdecl (fndecl);
39587 }
39588 else if (current_class_type)
39589 {
39590 if (cp == NULL)
39591 {
39592 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39593 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
39594 cp_lexer_consume_token (parser->lexer);
39595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39596 goto fail;
39597 cp = cp_token_cache_new (first_token,
39598 cp_lexer_peek_nth_token (parser->lexer,
39599 2));
39600 }
39601 DECL_STATIC_FUNCTION_P (fndecl) = 1;
39602 finish_member_declaration (fndecl);
39603 DECL_PENDING_INLINE_INFO (fndecl) = cp;
39604 DECL_PENDING_INLINE_P (fndecl) = 1;
39605 vec_safe_push (unparsed_funs_with_definitions, fndecl);
39606 continue;
39607 }
39608 else
39609 {
39610 DECL_CONTEXT (fndecl) = current_namespace;
39611 pushdecl (fndecl);
39612 }
39613 if (!block_scope)
39614 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
39615 else
39616 block = begin_omp_structured_block ();
39617 if (cp)
39618 {
39619 cp_parser_push_lexer_for_tokens (parser, cp);
39620 parser->lexer->in_pragma = true;
39621 }
39622 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
39623 {
39624 if (!block_scope)
39625 finish_function (/*inline_p=*/false);
39626 else
39627 DECL_CONTEXT (fndecl) = current_function_decl;
39628 if (cp)
39629 cp_parser_pop_lexer (parser);
39630 goto fail;
39631 }
39632 if (cp)
39633 cp_parser_pop_lexer (parser);
39634 if (!block_scope)
39635 finish_function (/*inline_p=*/false);
39636 else
39637 {
39638 DECL_CONTEXT (fndecl) = current_function_decl;
39639 block = finish_omp_structured_block (block);
39640 if (TREE_CODE (block) == BIND_EXPR)
39641 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
39642 else if (TREE_CODE (block) == STATEMENT_LIST)
39643 DECL_SAVED_TREE (fndecl) = block;
39644 if (processing_template_decl)
39645 add_decl_expr (fndecl);
39646 }
39647 cp_check_omp_declare_reduction (fndecl);
39648 if (cp == NULL && types.length () > 1)
39649 cp = cp_token_cache_new (first_token,
39650 cp_lexer_peek_nth_token (parser->lexer, 2));
39651 if (errs != errorcount)
39652 break;
39653 }
39654
39655 cp_parser_require_pragma_eol (parser, pragma_tok);
39656
39657 done:
39658 /* Free any declarators allocated. */
39659 obstack_free (&declarator_obstack, p);
39660 }
39661
39662 /* OpenMP 4.0
39663 #pragma omp declare simd declare-simd-clauses[optseq] new-line
39664 #pragma omp declare reduction (reduction-id : typename-list : expression) \
39665 initializer-clause[opt] new-line
39666 #pragma omp declare target new-line */
39667
39668 static bool
39669 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
39670 enum pragma_context context)
39671 {
39672 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39673 {
39674 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39675 const char *p = IDENTIFIER_POINTER (id);
39676
39677 if (strcmp (p, "simd") == 0)
39678 {
39679 cp_lexer_consume_token (parser->lexer);
39680 cp_parser_omp_declare_simd (parser, pragma_tok,
39681 context);
39682 return true;
39683 }
39684 cp_ensure_no_omp_declare_simd (parser);
39685 if (strcmp (p, "reduction") == 0)
39686 {
39687 cp_lexer_consume_token (parser->lexer);
39688 cp_parser_omp_declare_reduction (parser, pragma_tok,
39689 context);
39690 return false;
39691 }
39692 if (!flag_openmp) /* flag_openmp_simd */
39693 {
39694 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39695 return false;
39696 }
39697 if (strcmp (p, "target") == 0)
39698 {
39699 cp_lexer_consume_token (parser->lexer);
39700 cp_parser_omp_declare_target (parser, pragma_tok);
39701 return false;
39702 }
39703 }
39704 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
39705 "or %<target%>");
39706 cp_parser_require_pragma_eol (parser, pragma_tok);
39707 return false;
39708 }
39709
39710 /* OpenMP 5.0
39711 #pragma omp requires clauses[optseq] new-line */
39712
39713 static bool
39714 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
39715 {
39716 bool first = true;
39717 enum omp_requires new_req = (enum omp_requires) 0;
39718
39719 location_t loc = pragma_tok->location;
39720 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
39721 {
39722 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39723 cp_lexer_consume_token (parser->lexer);
39724
39725 first = false;
39726
39727 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39728 {
39729 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39730 const char *p = IDENTIFIER_POINTER (id);
39731 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
39732 enum omp_requires this_req = (enum omp_requires) 0;
39733
39734 if (!strcmp (p, "unified_address"))
39735 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
39736 else if (!strcmp (p, "unified_shared_memory"))
39737 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
39738 else if (!strcmp (p, "dynamic_allocators"))
39739 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
39740 else if (!strcmp (p, "reverse_offload"))
39741 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
39742 else if (!strcmp (p, "atomic_default_mem_order"))
39743 {
39744 cp_lexer_consume_token (parser->lexer);
39745
39746 matching_parens parens;
39747 if (parens.require_open (parser))
39748 {
39749 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39750 {
39751 id = cp_lexer_peek_token (parser->lexer)->u.value;
39752 p = IDENTIFIER_POINTER (id);
39753
39754 if (!strcmp (p, "seq_cst"))
39755 this_req
39756 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
39757 else if (!strcmp (p, "relaxed"))
39758 this_req
39759 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
39760 else if (!strcmp (p, "acq_rel"))
39761 this_req
39762 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
39763 }
39764 if (this_req == 0)
39765 {
39766 error_at (cp_lexer_peek_token (parser->lexer)->location,
39767 "expected %<seq_cst%>, %<relaxed%> or "
39768 "%<acq_rel%>");
39769 if (cp_lexer_nth_token_is (parser->lexer, 2,
39770 CPP_CLOSE_PAREN))
39771 cp_lexer_consume_token (parser->lexer);
39772 }
39773 else
39774 cp_lexer_consume_token (parser->lexer);
39775
39776 if (!parens.require_close (parser))
39777 cp_parser_skip_to_closing_parenthesis (parser,
39778 /*recovering=*/true,
39779 /*or_comma=*/false,
39780 /*consume_paren=*/
39781 true);
39782
39783 if (this_req == 0)
39784 {
39785 cp_parser_require_pragma_eol (parser, pragma_tok);
39786 return false;
39787 }
39788 }
39789 p = NULL;
39790 }
39791 else
39792 {
39793 error_at (cloc, "expected %<unified_address%>, "
39794 "%<unified_shared_memory%>, "
39795 "%<dynamic_allocators%>, "
39796 "%<reverse_offload%> "
39797 "or %<atomic_default_mem_order%> clause");
39798 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39799 return false;
39800 }
39801 if (p)
39802 sorry_at (cloc, "%qs clause on %<requires%> directive not "
39803 "supported yet", p);
39804 if (p)
39805 cp_lexer_consume_token (parser->lexer);
39806 if (this_req)
39807 {
39808 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39809 {
39810 if ((this_req & new_req) != 0)
39811 error_at (cloc, "too many %qs clauses", p);
39812 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
39813 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
39814 error_at (cloc, "%qs clause used lexically after first "
39815 "target construct or offloading API", p);
39816 }
39817 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39818 {
39819 error_at (cloc, "too many %qs clauses",
39820 "atomic_default_mem_order");
39821 this_req = (enum omp_requires) 0;
39822 }
39823 else if ((omp_requires_mask
39824 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
39825 {
39826 error_at (cloc, "more than one %<atomic_default_mem_order%>"
39827 " clause in a single compilation unit");
39828 this_req
39829 = (enum omp_requires)
39830 (omp_requires_mask
39831 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
39832 }
39833 else if ((omp_requires_mask
39834 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
39835 error_at (cloc, "%<atomic_default_mem_order%> clause used "
39836 "lexically after first %<atomic%> construct "
39837 "without memory order clause");
39838 new_req = (enum omp_requires) (new_req | this_req);
39839 omp_requires_mask
39840 = (enum omp_requires) (omp_requires_mask | this_req);
39841 continue;
39842 }
39843 }
39844 break;
39845 }
39846 cp_parser_require_pragma_eol (parser, pragma_tok);
39847
39848 if (new_req == 0)
39849 error_at (loc, "%<pragma omp requires%> requires at least one clause");
39850 return false;
39851 }
39852
39853
39854 /* OpenMP 4.5:
39855 #pragma omp taskloop taskloop-clause[optseq] new-line
39856 for-loop
39857
39858 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
39859 for-loop */
39860
39861 #define OMP_TASKLOOP_CLAUSE_MASK \
39862 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
39868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
39869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
39875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39878
39879 static tree
39880 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
39881 char *p_name, omp_clause_mask mask, tree *cclauses,
39882 bool *if_p)
39883 {
39884 tree clauses, sb, ret;
39885 unsigned int save;
39886 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39887
39888 strcat (p_name, " taskloop");
39889 mask |= OMP_TASKLOOP_CLAUSE_MASK;
39890 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
39891 clause. */
39892 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
39893 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
39894
39895 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39896 {
39897 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39898 const char *p = IDENTIFIER_POINTER (id);
39899
39900 if (strcmp (p, "simd") == 0)
39901 {
39902 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
39903 if (cclauses == NULL)
39904 cclauses = cclauses_buf;
39905
39906 cp_lexer_consume_token (parser->lexer);
39907 if (!flag_openmp) /* flag_openmp_simd */
39908 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39909 cclauses, if_p);
39910 sb = begin_omp_structured_block ();
39911 save = cp_parser_begin_omp_structured_block (parser);
39912 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
39913 cclauses, if_p);
39914 cp_parser_end_omp_structured_block (parser, save);
39915 tree body = finish_omp_structured_block (sb);
39916 if (ret == NULL)
39917 return ret;
39918 ret = make_node (OMP_TASKLOOP);
39919 TREE_TYPE (ret) = void_type_node;
39920 OMP_FOR_BODY (ret) = body;
39921 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39922 SET_EXPR_LOCATION (ret, loc);
39923 add_stmt (ret);
39924 return ret;
39925 }
39926 }
39927 if (!flag_openmp) /* flag_openmp_simd */
39928 {
39929 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
39930 return NULL_TREE;
39931 }
39932
39933 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
39934 cclauses == NULL);
39935 if (cclauses)
39936 {
39937 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
39938 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
39939 }
39940
39941 keep_next_level (true);
39942 sb = begin_omp_structured_block ();
39943 save = cp_parser_begin_omp_structured_block (parser);
39944
39945 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
39946 if_p);
39947
39948 cp_parser_end_omp_structured_block (parser, save);
39949 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
39950
39951 return ret;
39952 }
39953
39954
39955 /* OpenACC 2.0:
39956 # pragma acc routine oacc-routine-clause[optseq] new-line
39957 function-definition
39958
39959 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
39960 */
39961
39962 #define OACC_ROUTINE_CLAUSE_MASK \
39963 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
39964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
39965 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
39966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
39967
39968
39969 /* Parse the OpenACC routine pragma. This has an optional '( name )'
39970 component, which must resolve to a declared namespace-scope
39971 function. The clauses are either processed directly (for a named
39972 function), or defered until the immediatley following declaration
39973 is parsed. */
39974
39975 static void
39976 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
39977 enum pragma_context context)
39978 {
39979 gcc_checking_assert (context == pragma_external);
39980 /* The checking for "another pragma following this one" in the "no optional
39981 '( name )'" case makes sure that we dont re-enter. */
39982 gcc_checking_assert (parser->oacc_routine == NULL);
39983
39984 cp_oacc_routine_data data;
39985 data.error_seen = false;
39986 data.fndecl_seen = false;
39987 data.tokens = vNULL;
39988 data.clauses = NULL_TREE;
39989 data.loc = pragma_tok->location;
39990 /* It is safe to take the address of a local variable; it will only be
39991 used while this scope is live. */
39992 parser->oacc_routine = &data;
39993
39994 /* Look for optional '( name )'. */
39995 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
39996 {
39997 matching_parens parens;
39998 parens.consume_open (parser); /* '(' */
39999
40000 /* We parse the name as an id-expression. If it resolves to
40001 anything other than a non-overloaded function at namespace
40002 scope, it's an error. */
40003 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
40004 tree name = cp_parser_id_expression (parser,
40005 /*template_keyword_p=*/false,
40006 /*check_dependency_p=*/false,
40007 /*template_p=*/NULL,
40008 /*declarator_p=*/false,
40009 /*optional_p=*/false);
40010 tree decl = (identifier_p (name)
40011 ? cp_parser_lookup_name_simple (parser, name, name_loc)
40012 : name);
40013 if (name != error_mark_node && decl == error_mark_node)
40014 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
40015
40016 if (decl == error_mark_node
40017 || !parens.require_close (parser))
40018 {
40019 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40020 parser->oacc_routine = NULL;
40021 return;
40022 }
40023
40024 data.clauses
40025 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40026 "#pragma acc routine",
40027 cp_lexer_peek_token (parser->lexer));
40028
40029 if (decl && is_overloaded_fn (decl)
40030 && (TREE_CODE (decl) != FUNCTION_DECL
40031 || DECL_FUNCTION_TEMPLATE_P (decl)))
40032 {
40033 error_at (name_loc,
40034 "%<#pragma acc routine%> names a set of overloads");
40035 parser->oacc_routine = NULL;
40036 return;
40037 }
40038
40039 /* Perhaps we should use the same rule as declarations in different
40040 namespaces? */
40041 if (!DECL_NAMESPACE_SCOPE_P (decl))
40042 {
40043 error_at (name_loc,
40044 "%qD does not refer to a namespace scope function", decl);
40045 parser->oacc_routine = NULL;
40046 return;
40047 }
40048
40049 if (TREE_CODE (decl) != FUNCTION_DECL)
40050 {
40051 error_at (name_loc, "%qD does not refer to a function", decl);
40052 parser->oacc_routine = NULL;
40053 return;
40054 }
40055
40056 cp_finalize_oacc_routine (parser, decl, false);
40057 parser->oacc_routine = NULL;
40058 }
40059 else /* No optional '( name )'. */
40060 {
40061 /* Store away all pragma tokens. */
40062 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
40063 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
40064 cp_lexer_consume_token (parser->lexer);
40065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40066 parser->oacc_routine->error_seen = true;
40067 cp_parser_require_pragma_eol (parser, pragma_tok);
40068 struct cp_token_cache *cp
40069 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
40070 parser->oacc_routine->tokens.safe_push (cp);
40071
40072 /* Emit a helpful diagnostic if there's another pragma following this
40073 one. */
40074 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
40075 {
40076 cp_ensure_no_oacc_routine (parser);
40077 data.tokens.release ();
40078 /* ..., and then just keep going. */
40079 return;
40080 }
40081
40082 /* We only have to consider the pragma_external case here. */
40083 cp_parser_declaration (parser);
40084 if (parser->oacc_routine
40085 && !parser->oacc_routine->fndecl_seen)
40086 cp_ensure_no_oacc_routine (parser);
40087 else
40088 parser->oacc_routine = NULL;
40089 data.tokens.release ();
40090 }
40091 }
40092
40093 /* Finalize #pragma acc routine clauses after direct declarator has
40094 been parsed. */
40095
40096 static tree
40097 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
40098 {
40099 struct cp_token_cache *ce;
40100 cp_oacc_routine_data *data = parser->oacc_routine;
40101
40102 if (!data->error_seen && data->fndecl_seen)
40103 {
40104 error_at (data->loc,
40105 "%<#pragma acc routine%> not immediately followed by "
40106 "a single function declaration or definition");
40107 data->error_seen = true;
40108 }
40109 if (data->error_seen)
40110 return attrs;
40111
40112 gcc_checking_assert (data->tokens.length () == 1);
40113 ce = data->tokens[0];
40114
40115 cp_parser_push_lexer_for_tokens (parser, ce);
40116 parser->lexer->in_pragma = true;
40117 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
40118
40119 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
40120 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
40121 parser->oacc_routine->clauses
40122 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
40123 "#pragma acc routine", pragma_tok);
40124 cp_parser_pop_lexer (parser);
40125 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
40126 fndecl_seen. */
40127
40128 return attrs;
40129 }
40130
40131 /* Apply any saved OpenACC routine clauses to a just-parsed
40132 declaration. */
40133
40134 static void
40135 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
40136 {
40137 if (__builtin_expect (parser->oacc_routine != NULL, 0))
40138 {
40139 /* Keep going if we're in error reporting mode. */
40140 if (parser->oacc_routine->error_seen
40141 || fndecl == error_mark_node)
40142 return;
40143
40144 if (parser->oacc_routine->fndecl_seen)
40145 {
40146 error_at (parser->oacc_routine->loc,
40147 "%<#pragma acc routine%> not immediately followed by"
40148 " a single function declaration or definition");
40149 parser->oacc_routine = NULL;
40150 return;
40151 }
40152 if (TREE_CODE (fndecl) != FUNCTION_DECL)
40153 {
40154 cp_ensure_no_oacc_routine (parser);
40155 return;
40156 }
40157
40158 if (oacc_get_fn_attrib (fndecl))
40159 {
40160 error_at (parser->oacc_routine->loc,
40161 "%<#pragma acc routine%> already applied to %qD", fndecl);
40162 parser->oacc_routine = NULL;
40163 return;
40164 }
40165
40166 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
40167 {
40168 error_at (parser->oacc_routine->loc,
40169 TREE_USED (fndecl)
40170 ? G_("%<#pragma acc routine%> must be applied before use")
40171 : G_("%<#pragma acc routine%> must be applied before "
40172 "definition"));
40173 parser->oacc_routine = NULL;
40174 return;
40175 }
40176
40177 /* Process the routine's dimension clauses. */
40178 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
40179 oacc_replace_fn_attrib (fndecl, dims);
40180
40181 /* Add an "omp declare target" attribute. */
40182 DECL_ATTRIBUTES (fndecl)
40183 = tree_cons (get_identifier ("omp declare target"),
40184 NULL_TREE, DECL_ATTRIBUTES (fndecl));
40185
40186 /* Don't unset parser->oacc_routine here: we may still need it to
40187 diagnose wrong usage. But, remember that we've used this "#pragma acc
40188 routine". */
40189 parser->oacc_routine->fndecl_seen = true;
40190 }
40191 }
40192
40193 /* Main entry point to OpenMP statement pragmas. */
40194
40195 static void
40196 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
40197 {
40198 tree stmt;
40199 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
40200 omp_clause_mask mask (0);
40201
40202 switch (cp_parser_pragma_kind (pragma_tok))
40203 {
40204 case PRAGMA_OACC_ATOMIC:
40205 cp_parser_omp_atomic (parser, pragma_tok);
40206 return;
40207 case PRAGMA_OACC_CACHE:
40208 stmt = cp_parser_oacc_cache (parser, pragma_tok);
40209 break;
40210 case PRAGMA_OACC_DATA:
40211 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
40212 break;
40213 case PRAGMA_OACC_ENTER_DATA:
40214 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
40215 break;
40216 case PRAGMA_OACC_EXIT_DATA:
40217 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
40218 break;
40219 case PRAGMA_OACC_HOST_DATA:
40220 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
40221 break;
40222 case PRAGMA_OACC_KERNELS:
40223 case PRAGMA_OACC_PARALLEL:
40224 strcpy (p_name, "#pragma acc");
40225 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
40226 if_p);
40227 break;
40228 case PRAGMA_OACC_LOOP:
40229 strcpy (p_name, "#pragma acc");
40230 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
40231 if_p);
40232 break;
40233 case PRAGMA_OACC_UPDATE:
40234 stmt = cp_parser_oacc_update (parser, pragma_tok);
40235 break;
40236 case PRAGMA_OACC_WAIT:
40237 stmt = cp_parser_oacc_wait (parser, pragma_tok);
40238 break;
40239 case PRAGMA_OMP_ATOMIC:
40240 cp_parser_omp_atomic (parser, pragma_tok);
40241 return;
40242 case PRAGMA_OMP_CRITICAL:
40243 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
40244 break;
40245 case PRAGMA_OMP_DISTRIBUTE:
40246 strcpy (p_name, "#pragma omp");
40247 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
40248 if_p);
40249 break;
40250 case PRAGMA_OMP_FOR:
40251 strcpy (p_name, "#pragma omp");
40252 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
40253 if_p);
40254 break;
40255 case PRAGMA_OMP_MASTER:
40256 strcpy (p_name, "#pragma omp");
40257 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
40258 if_p);
40259 break;
40260 case PRAGMA_OMP_PARALLEL:
40261 strcpy (p_name, "#pragma omp");
40262 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
40263 if_p);
40264 break;
40265 case PRAGMA_OMP_SECTIONS:
40266 strcpy (p_name, "#pragma omp");
40267 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
40268 break;
40269 case PRAGMA_OMP_SIMD:
40270 strcpy (p_name, "#pragma omp");
40271 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
40272 if_p);
40273 break;
40274 case PRAGMA_OMP_SINGLE:
40275 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
40276 break;
40277 case PRAGMA_OMP_TASK:
40278 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
40279 break;
40280 case PRAGMA_OMP_TASKGROUP:
40281 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
40282 break;
40283 case PRAGMA_OMP_TASKLOOP:
40284 strcpy (p_name, "#pragma omp");
40285 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
40286 if_p);
40287 break;
40288 case PRAGMA_OMP_TEAMS:
40289 strcpy (p_name, "#pragma omp");
40290 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
40291 if_p);
40292 break;
40293 default:
40294 gcc_unreachable ();
40295 }
40296
40297 protected_set_expr_location (stmt, pragma_tok->location);
40298 }
40299 \f
40300 /* Transactional Memory parsing routines. */
40301
40302 /* Parse a transaction attribute.
40303
40304 txn-attribute:
40305 attribute
40306 [ [ identifier ] ]
40307
40308 We use this instead of cp_parser_attributes_opt for transactions to avoid
40309 the pedwarn in C++98 mode. */
40310
40311 static tree
40312 cp_parser_txn_attribute_opt (cp_parser *parser)
40313 {
40314 cp_token *token;
40315 tree attr_name, attr = NULL;
40316
40317 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
40318 return cp_parser_attributes_opt (parser);
40319
40320 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
40321 return NULL_TREE;
40322 cp_lexer_consume_token (parser->lexer);
40323 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
40324 goto error1;
40325
40326 token = cp_lexer_peek_token (parser->lexer);
40327 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
40328 {
40329 token = cp_lexer_consume_token (parser->lexer);
40330
40331 attr_name = (token->type == CPP_KEYWORD
40332 /* For keywords, use the canonical spelling,
40333 not the parsed identifier. */
40334 ? ridpointers[(int) token->keyword]
40335 : token->u.value);
40336 attr = build_tree_list (attr_name, NULL_TREE);
40337 }
40338 else
40339 cp_parser_error (parser, "expected identifier");
40340
40341 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40342 error1:
40343 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
40344 return attr;
40345 }
40346
40347 /* Parse a __transaction_atomic or __transaction_relaxed statement.
40348
40349 transaction-statement:
40350 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
40351 compound-statement
40352 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
40353 */
40354
40355 static tree
40356 cp_parser_transaction (cp_parser *parser, cp_token *token)
40357 {
40358 unsigned char old_in = parser->in_transaction;
40359 unsigned char this_in = 1, new_in;
40360 enum rid keyword = token->keyword;
40361 tree stmt, attrs, noex;
40362
40363 cp_lexer_consume_token (parser->lexer);
40364
40365 if (keyword == RID_TRANSACTION_RELAXED
40366 || keyword == RID_SYNCHRONIZED)
40367 this_in |= TM_STMT_ATTR_RELAXED;
40368 else
40369 {
40370 attrs = cp_parser_txn_attribute_opt (parser);
40371 if (attrs)
40372 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40373 }
40374
40375 /* Parse a noexcept specification. */
40376 if (keyword == RID_ATOMIC_NOEXCEPT)
40377 noex = boolean_true_node;
40378 else if (keyword == RID_ATOMIC_CANCEL)
40379 {
40380 /* cancel-and-throw is unimplemented. */
40381 sorry ("atomic_cancel");
40382 noex = NULL_TREE;
40383 }
40384 else
40385 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
40386
40387 /* Keep track if we're in the lexical scope of an outer transaction. */
40388 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
40389
40390 stmt = begin_transaction_stmt (token->location, NULL, this_in);
40391
40392 parser->in_transaction = new_in;
40393 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
40394 parser->in_transaction = old_in;
40395
40396 finish_transaction_stmt (stmt, NULL, this_in, noex);
40397
40398 return stmt;
40399 }
40400
40401 /* Parse a __transaction_atomic or __transaction_relaxed expression.
40402
40403 transaction-expression:
40404 __transaction_atomic txn-noexcept-spec[opt] ( expression )
40405 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
40406 */
40407
40408 static tree
40409 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
40410 {
40411 unsigned char old_in = parser->in_transaction;
40412 unsigned char this_in = 1;
40413 cp_token *token;
40414 tree expr, noex;
40415 bool noex_expr;
40416 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40417
40418 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40419 || keyword == RID_TRANSACTION_RELAXED);
40420
40421 if (!flag_tm)
40422 error_at (loc,
40423 keyword == RID_TRANSACTION_RELAXED
40424 ? G_("%<__transaction_relaxed%> without transactional memory "
40425 "support enabled")
40426 : G_("%<__transaction_atomic%> without transactional memory "
40427 "support enabled"));
40428
40429 token = cp_parser_require_keyword (parser, keyword,
40430 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40431 : RT_TRANSACTION_RELAXED));
40432 gcc_assert (token != NULL);
40433
40434 if (keyword == RID_TRANSACTION_RELAXED)
40435 this_in |= TM_STMT_ATTR_RELAXED;
40436
40437 /* Set this early. This might mean that we allow transaction_cancel in
40438 an expression that we find out later actually has to be a constexpr.
40439 However, we expect that cxx_constant_value will be able to deal with
40440 this; also, if the noexcept has no constexpr, then what we parse next
40441 really is a transaction's body. */
40442 parser->in_transaction = this_in;
40443
40444 /* Parse a noexcept specification. */
40445 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
40446 true);
40447
40448 if (!noex || !noex_expr
40449 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40450 {
40451 matching_parens parens;
40452 parens.require_open (parser);
40453
40454 expr = cp_parser_expression (parser);
40455 expr = finish_parenthesized_expr (expr);
40456
40457 parens.require_close (parser);
40458 }
40459 else
40460 {
40461 /* The only expression that is available got parsed for the noexcept
40462 already. noexcept is true then. */
40463 expr = noex;
40464 noex = boolean_true_node;
40465 }
40466
40467 expr = build_transaction_expr (token->location, expr, this_in, noex);
40468 parser->in_transaction = old_in;
40469
40470 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
40471 return error_mark_node;
40472
40473 return (flag_tm ? expr : error_mark_node);
40474 }
40475
40476 /* Parse a function-transaction-block.
40477
40478 function-transaction-block:
40479 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
40480 function-body
40481 __transaction_atomic txn-attribute[opt] function-try-block
40482 __transaction_relaxed ctor-initializer[opt] function-body
40483 __transaction_relaxed function-try-block
40484 */
40485
40486 static void
40487 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
40488 {
40489 unsigned char old_in = parser->in_transaction;
40490 unsigned char new_in = 1;
40491 tree compound_stmt, stmt, attrs;
40492 cp_token *token;
40493
40494 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
40495 || keyword == RID_TRANSACTION_RELAXED);
40496 token = cp_parser_require_keyword (parser, keyword,
40497 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
40498 : RT_TRANSACTION_RELAXED));
40499 gcc_assert (token != NULL);
40500
40501 if (keyword == RID_TRANSACTION_RELAXED)
40502 new_in |= TM_STMT_ATTR_RELAXED;
40503 else
40504 {
40505 attrs = cp_parser_txn_attribute_opt (parser);
40506 if (attrs)
40507 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
40508 }
40509
40510 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
40511
40512 parser->in_transaction = new_in;
40513
40514 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
40515 cp_parser_function_try_block (parser);
40516 else
40517 cp_parser_ctor_initializer_opt_and_function_body
40518 (parser, /*in_function_try_block=*/false);
40519
40520 parser->in_transaction = old_in;
40521
40522 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
40523 }
40524
40525 /* Parse a __transaction_cancel statement.
40526
40527 cancel-statement:
40528 __transaction_cancel txn-attribute[opt] ;
40529 __transaction_cancel txn-attribute[opt] throw-expression ;
40530
40531 ??? Cancel and throw is not yet implemented. */
40532
40533 static tree
40534 cp_parser_transaction_cancel (cp_parser *parser)
40535 {
40536 cp_token *token;
40537 bool is_outer = false;
40538 tree stmt, attrs;
40539
40540 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
40541 RT_TRANSACTION_CANCEL);
40542 gcc_assert (token != NULL);
40543
40544 attrs = cp_parser_txn_attribute_opt (parser);
40545 if (attrs)
40546 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
40547
40548 /* ??? Parse cancel-and-throw here. */
40549
40550 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
40551
40552 if (!flag_tm)
40553 {
40554 error_at (token->location, "%<__transaction_cancel%> without "
40555 "transactional memory support enabled");
40556 return error_mark_node;
40557 }
40558 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
40559 {
40560 error_at (token->location, "%<__transaction_cancel%> within a "
40561 "%<__transaction_relaxed%>");
40562 return error_mark_node;
40563 }
40564 else if (is_outer)
40565 {
40566 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
40567 && !is_tm_may_cancel_outer (current_function_decl))
40568 {
40569 error_at (token->location, "outer %<__transaction_cancel%> not "
40570 "within outer %<__transaction_atomic%>");
40571 error_at (token->location,
40572 " or a %<transaction_may_cancel_outer%> function");
40573 return error_mark_node;
40574 }
40575 }
40576 else if (parser->in_transaction == 0)
40577 {
40578 error_at (token->location, "%<__transaction_cancel%> not within "
40579 "%<__transaction_atomic%>");
40580 return error_mark_node;
40581 }
40582
40583 stmt = build_tm_abort_call (token->location, is_outer);
40584 add_stmt (stmt);
40585
40586 return stmt;
40587 }
40588 \f
40589 /* The parser. */
40590
40591 static GTY (()) cp_parser *the_parser;
40592
40593 \f
40594 /* Special handling for the first token or line in the file. The first
40595 thing in the file might be #pragma GCC pch_preprocess, which loads a
40596 PCH file, which is a GC collection point. So we need to handle this
40597 first pragma without benefit of an existing lexer structure.
40598
40599 Always returns one token to the caller in *FIRST_TOKEN. This is
40600 either the true first token of the file, or the first token after
40601 the initial pragma. */
40602
40603 static void
40604 cp_parser_initial_pragma (cp_token *first_token)
40605 {
40606 tree name = NULL;
40607
40608 cp_lexer_get_preprocessor_token (NULL, first_token);
40609 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
40610 return;
40611
40612 cp_lexer_get_preprocessor_token (NULL, first_token);
40613 if (first_token->type == CPP_STRING)
40614 {
40615 name = first_token->u.value;
40616
40617 cp_lexer_get_preprocessor_token (NULL, first_token);
40618 if (first_token->type != CPP_PRAGMA_EOL)
40619 error_at (first_token->location,
40620 "junk at end of %<#pragma GCC pch_preprocess%>");
40621 }
40622 else
40623 error_at (first_token->location, "expected string literal");
40624
40625 /* Skip to the end of the pragma. */
40626 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
40627 cp_lexer_get_preprocessor_token (NULL, first_token);
40628
40629 /* Now actually load the PCH file. */
40630 if (name)
40631 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
40632
40633 /* Read one more token to return to our caller. We have to do this
40634 after reading the PCH file in, since its pointers have to be
40635 live. */
40636 cp_lexer_get_preprocessor_token (NULL, first_token);
40637 }
40638
40639 /* Parse a pragma GCC ivdep. */
40640
40641 static bool
40642 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
40643 {
40644 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40645 return true;
40646 }
40647
40648 /* Parse a pragma GCC unroll. */
40649
40650 static unsigned short
40651 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
40652 {
40653 location_t location = cp_lexer_peek_token (parser->lexer)->location;
40654 tree expr = cp_parser_constant_expression (parser);
40655 unsigned short unroll;
40656 expr = maybe_constant_value (expr);
40657 HOST_WIDE_INT lunroll = 0;
40658 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
40659 || TREE_CODE (expr) != INTEGER_CST
40660 || (lunroll = tree_to_shwi (expr)) < 0
40661 || lunroll >= USHRT_MAX)
40662 {
40663 error_at (location, "%<#pragma GCC unroll%> requires an"
40664 " assignment-expression that evaluates to a non-negative"
40665 " integral constant less than %u", USHRT_MAX);
40666 unroll = 0;
40667 }
40668 else
40669 {
40670 unroll = (unsigned short)lunroll;
40671 if (unroll == 0)
40672 unroll = 1;
40673 }
40674 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40675 return unroll;
40676 }
40677
40678 /* Normal parsing of a pragma token. Here we can (and must) use the
40679 regular lexer. */
40680
40681 static bool
40682 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
40683 {
40684 cp_token *pragma_tok;
40685 unsigned int id;
40686 tree stmt;
40687 bool ret;
40688
40689 pragma_tok = cp_lexer_consume_token (parser->lexer);
40690 gcc_assert (pragma_tok->type == CPP_PRAGMA);
40691 parser->lexer->in_pragma = true;
40692
40693 id = cp_parser_pragma_kind (pragma_tok);
40694 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
40695 cp_ensure_no_omp_declare_simd (parser);
40696 switch (id)
40697 {
40698 case PRAGMA_GCC_PCH_PREPROCESS:
40699 error_at (pragma_tok->location,
40700 "%<#pragma GCC pch_preprocess%> must be first");
40701 break;
40702
40703 case PRAGMA_OMP_BARRIER:
40704 switch (context)
40705 {
40706 case pragma_compound:
40707 cp_parser_omp_barrier (parser, pragma_tok);
40708 return false;
40709 case pragma_stmt:
40710 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40711 "used in compound statements", "omp barrier");
40712 break;
40713 default:
40714 goto bad_stmt;
40715 }
40716 break;
40717
40718 case PRAGMA_OMP_DEPOBJ:
40719 switch (context)
40720 {
40721 case pragma_compound:
40722 cp_parser_omp_depobj (parser, pragma_tok);
40723 return false;
40724 case pragma_stmt:
40725 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40726 "used in compound statements", "omp depobj");
40727 break;
40728 default:
40729 goto bad_stmt;
40730 }
40731 break;
40732
40733 case PRAGMA_OMP_FLUSH:
40734 switch (context)
40735 {
40736 case pragma_compound:
40737 cp_parser_omp_flush (parser, pragma_tok);
40738 return false;
40739 case pragma_stmt:
40740 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
40741 "used in compound statements", "omp flush");
40742 break;
40743 default:
40744 goto bad_stmt;
40745 }
40746 break;
40747
40748 case PRAGMA_OMP_TASKWAIT:
40749 switch (context)
40750 {
40751 case pragma_compound:
40752 cp_parser_omp_taskwait (parser, pragma_tok);
40753 return false;
40754 case pragma_stmt:
40755 error_at (pragma_tok->location,
40756 "%<#pragma %s%> may only be used in compound statements",
40757 "omp taskwait");
40758 break;
40759 default:
40760 goto bad_stmt;
40761 }
40762 break;
40763
40764 case PRAGMA_OMP_TASKYIELD:
40765 switch (context)
40766 {
40767 case pragma_compound:
40768 cp_parser_omp_taskyield (parser, pragma_tok);
40769 return false;
40770 case pragma_stmt:
40771 error_at (pragma_tok->location,
40772 "%<#pragma %s%> may only be used in compound statements",
40773 "omp taskyield");
40774 break;
40775 default:
40776 goto bad_stmt;
40777 }
40778 break;
40779
40780 case PRAGMA_OMP_CANCEL:
40781 switch (context)
40782 {
40783 case pragma_compound:
40784 cp_parser_omp_cancel (parser, pragma_tok);
40785 return false;
40786 case pragma_stmt:
40787 error_at (pragma_tok->location,
40788 "%<#pragma %s%> may only be used in compound statements",
40789 "omp cancel");
40790 break;
40791 default:
40792 goto bad_stmt;
40793 }
40794 break;
40795
40796 case PRAGMA_OMP_CANCELLATION_POINT:
40797 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
40798 return false;
40799
40800 case PRAGMA_OMP_THREADPRIVATE:
40801 cp_parser_omp_threadprivate (parser, pragma_tok);
40802 return false;
40803
40804 case PRAGMA_OMP_DECLARE:
40805 return cp_parser_omp_declare (parser, pragma_tok, context);
40806
40807 case PRAGMA_OACC_DECLARE:
40808 cp_parser_oacc_declare (parser, pragma_tok);
40809 return false;
40810
40811 case PRAGMA_OACC_ENTER_DATA:
40812 if (context == pragma_stmt)
40813 {
40814 error_at (pragma_tok->location,
40815 "%<#pragma %s%> may only be used in compound statements",
40816 "acc enter data");
40817 break;
40818 }
40819 else if (context != pragma_compound)
40820 goto bad_stmt;
40821 cp_parser_omp_construct (parser, pragma_tok, if_p);
40822 return true;
40823
40824 case PRAGMA_OACC_EXIT_DATA:
40825 if (context == pragma_stmt)
40826 {
40827 error_at (pragma_tok->location,
40828 "%<#pragma %s%> may only be used in compound statements",
40829 "acc exit data");
40830 break;
40831 }
40832 else if (context != pragma_compound)
40833 goto bad_stmt;
40834 cp_parser_omp_construct (parser, pragma_tok, if_p);
40835 return true;
40836
40837 case PRAGMA_OACC_ROUTINE:
40838 if (context != pragma_external)
40839 {
40840 error_at (pragma_tok->location,
40841 "%<#pragma acc routine%> must be at file scope");
40842 break;
40843 }
40844 cp_parser_oacc_routine (parser, pragma_tok, context);
40845 return false;
40846
40847 case PRAGMA_OACC_UPDATE:
40848 if (context == pragma_stmt)
40849 {
40850 error_at (pragma_tok->location,
40851 "%<#pragma %s%> may only be used in compound statements",
40852 "acc update");
40853 break;
40854 }
40855 else if (context != pragma_compound)
40856 goto bad_stmt;
40857 cp_parser_omp_construct (parser, pragma_tok, if_p);
40858 return true;
40859
40860 case PRAGMA_OACC_WAIT:
40861 if (context == pragma_stmt)
40862 {
40863 error_at (pragma_tok->location,
40864 "%<#pragma %s%> may only be used in compound statements",
40865 "acc wait");
40866 break;
40867 }
40868 else if (context != pragma_compound)
40869 goto bad_stmt;
40870 cp_parser_omp_construct (parser, pragma_tok, if_p);
40871 return true;
40872
40873 case PRAGMA_OACC_ATOMIC:
40874 case PRAGMA_OACC_CACHE:
40875 case PRAGMA_OACC_DATA:
40876 case PRAGMA_OACC_HOST_DATA:
40877 case PRAGMA_OACC_KERNELS:
40878 case PRAGMA_OACC_PARALLEL:
40879 case PRAGMA_OACC_LOOP:
40880 case PRAGMA_OMP_ATOMIC:
40881 case PRAGMA_OMP_CRITICAL:
40882 case PRAGMA_OMP_DISTRIBUTE:
40883 case PRAGMA_OMP_FOR:
40884 case PRAGMA_OMP_MASTER:
40885 case PRAGMA_OMP_PARALLEL:
40886 case PRAGMA_OMP_SECTIONS:
40887 case PRAGMA_OMP_SIMD:
40888 case PRAGMA_OMP_SINGLE:
40889 case PRAGMA_OMP_TASK:
40890 case PRAGMA_OMP_TASKGROUP:
40891 case PRAGMA_OMP_TASKLOOP:
40892 case PRAGMA_OMP_TEAMS:
40893 if (context != pragma_stmt && context != pragma_compound)
40894 goto bad_stmt;
40895 stmt = push_omp_privatization_clauses (false);
40896 cp_parser_omp_construct (parser, pragma_tok, if_p);
40897 pop_omp_privatization_clauses (stmt);
40898 return true;
40899
40900 case PRAGMA_OMP_REQUIRES:
40901 return cp_parser_omp_requires (parser, pragma_tok);
40902
40903 case PRAGMA_OMP_ORDERED:
40904 if (context != pragma_stmt && context != pragma_compound)
40905 goto bad_stmt;
40906 stmt = push_omp_privatization_clauses (false);
40907 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
40908 pop_omp_privatization_clauses (stmt);
40909 return ret;
40910
40911 case PRAGMA_OMP_TARGET:
40912 if (context != pragma_stmt && context != pragma_compound)
40913 goto bad_stmt;
40914 stmt = push_omp_privatization_clauses (false);
40915 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
40916 pop_omp_privatization_clauses (stmt);
40917 return ret;
40918
40919 case PRAGMA_OMP_END_DECLARE_TARGET:
40920 cp_parser_omp_end_declare_target (parser, pragma_tok);
40921 return false;
40922
40923 case PRAGMA_OMP_SECTION:
40924 error_at (pragma_tok->location,
40925 "%<#pragma omp section%> may only be used in "
40926 "%<#pragma omp sections%> construct");
40927 break;
40928
40929 case PRAGMA_IVDEP:
40930 {
40931 if (context == pragma_external)
40932 {
40933 error_at (pragma_tok->location,
40934 "%<#pragma GCC ivdep%> must be inside a function");
40935 break;
40936 }
40937 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
40938 unsigned short unroll;
40939 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40940 if (tok->type == CPP_PRAGMA
40941 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
40942 {
40943 tok = cp_lexer_consume_token (parser->lexer);
40944 unroll = cp_parser_pragma_unroll (parser, tok);
40945 tok = cp_lexer_peek_token (the_parser->lexer);
40946 }
40947 else
40948 unroll = 0;
40949 if (tok->type != CPP_KEYWORD
40950 || (tok->keyword != RID_FOR
40951 && tok->keyword != RID_WHILE
40952 && tok->keyword != RID_DO))
40953 {
40954 cp_parser_error (parser, "for, while or do statement expected");
40955 return false;
40956 }
40957 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40958 return true;
40959 }
40960
40961 case PRAGMA_UNROLL:
40962 {
40963 if (context == pragma_external)
40964 {
40965 error_at (pragma_tok->location,
40966 "%<#pragma GCC unroll%> must be inside a function");
40967 break;
40968 }
40969 const unsigned short unroll
40970 = cp_parser_pragma_unroll (parser, pragma_tok);
40971 bool ivdep;
40972 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
40973 if (tok->type == CPP_PRAGMA
40974 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
40975 {
40976 tok = cp_lexer_consume_token (parser->lexer);
40977 ivdep = cp_parser_pragma_ivdep (parser, tok);
40978 tok = cp_lexer_peek_token (the_parser->lexer);
40979 }
40980 else
40981 ivdep = false;
40982 if (tok->type != CPP_KEYWORD
40983 || (tok->keyword != RID_FOR
40984 && tok->keyword != RID_WHILE
40985 && tok->keyword != RID_DO))
40986 {
40987 cp_parser_error (parser, "for, while or do statement expected");
40988 return false;
40989 }
40990 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
40991 return true;
40992 }
40993
40994 default:
40995 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
40996 c_invoke_pragma_handler (id);
40997 break;
40998
40999 bad_stmt:
41000 cp_parser_error (parser, "expected declaration specifiers");
41001 break;
41002 }
41003
41004 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41005 return false;
41006 }
41007
41008 /* The interface the pragma parsers have to the lexer. */
41009
41010 enum cpp_ttype
41011 pragma_lex (tree *value, location_t *loc)
41012 {
41013 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
41014 enum cpp_ttype ret = tok->type;
41015
41016 *value = tok->u.value;
41017 if (loc)
41018 *loc = tok->location;
41019
41020 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
41021 ret = CPP_EOF;
41022 else if (ret == CPP_STRING)
41023 *value = cp_parser_string_literal (the_parser, false, false);
41024 else
41025 {
41026 if (ret == CPP_KEYWORD)
41027 ret = CPP_NAME;
41028 cp_lexer_consume_token (the_parser->lexer);
41029 }
41030
41031 return ret;
41032 }
41033
41034 \f
41035 /* External interface. */
41036
41037 /* Parse one entire translation unit. */
41038
41039 void
41040 c_parse_file (void)
41041 {
41042 static bool already_called = false;
41043
41044 if (already_called)
41045 fatal_error (input_location,
41046 "inter-module optimizations not implemented for C++");
41047 already_called = true;
41048
41049 the_parser = cp_parser_new ();
41050 push_deferring_access_checks (flag_access_control
41051 ? dk_no_deferred : dk_no_check);
41052 cp_parser_translation_unit (the_parser);
41053 the_parser = NULL;
41054
41055 finish_translation_unit ();
41056 }
41057
41058 /* Create an identifier for a generic parameter type (a synthesized
41059 template parameter implied by `auto' or a concept identifier). */
41060
41061 static GTY(()) int generic_parm_count;
41062 static tree
41063 make_generic_type_name ()
41064 {
41065 char buf[32];
41066 sprintf (buf, "auto:%d", ++generic_parm_count);
41067 return get_identifier (buf);
41068 }
41069
41070 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
41071 (creating a new template parameter list if necessary). Returns the newly
41072 created template type parm. */
41073
41074 static tree
41075 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
41076 {
41077 gcc_assert (current_binding_level->kind == sk_function_parms);
41078
41079 /* Before committing to modifying any scope, if we're in an
41080 implicit template scope, and we're trying to synthesize a
41081 constrained parameter, try to find a previous parameter with
41082 the same name. This is the same-type rule for abbreviated
41083 function templates.
41084
41085 NOTE: We can generate implicit parameters when tentatively
41086 parsing a nested name specifier, only to reject that parse
41087 later. However, matching the same template-id as part of a
41088 direct-declarator should generate an identical template
41089 parameter, so this rule will merge them. */
41090 if (parser->implicit_template_scope && constr)
41091 {
41092 tree t = parser->implicit_template_parms;
41093 while (t)
41094 {
41095 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
41096 {
41097 tree d = TREE_VALUE (t);
41098 if (TREE_CODE (d) == PARM_DECL)
41099 /* Return the TEMPLATE_PARM_INDEX. */
41100 d = DECL_INITIAL (d);
41101 return d;
41102 }
41103 t = TREE_CHAIN (t);
41104 }
41105 }
41106
41107 /* We are either continuing a function template that already contains implicit
41108 template parameters, creating a new fully-implicit function template, or
41109 extending an existing explicit function template with implicit template
41110 parameters. */
41111
41112 cp_binding_level *const entry_scope = current_binding_level;
41113
41114 bool become_template = false;
41115 cp_binding_level *parent_scope = 0;
41116
41117 if (parser->implicit_template_scope)
41118 {
41119 gcc_assert (parser->implicit_template_parms);
41120
41121 current_binding_level = parser->implicit_template_scope;
41122 }
41123 else
41124 {
41125 /* Roll back to the existing template parameter scope (in the case of
41126 extending an explicit function template) or introduce a new template
41127 parameter scope ahead of the function parameter scope (or class scope
41128 in the case of out-of-line member definitions). The function scope is
41129 added back after template parameter synthesis below. */
41130
41131 cp_binding_level *scope = entry_scope;
41132
41133 while (scope->kind == sk_function_parms)
41134 {
41135 parent_scope = scope;
41136 scope = scope->level_chain;
41137 }
41138 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
41139 {
41140 /* If not defining a class, then any class scope is a scope level in
41141 an out-of-line member definition. In this case simply wind back
41142 beyond the first such scope to inject the template parameter list.
41143 Otherwise wind back to the class being defined. The latter can
41144 occur in class member friend declarations such as:
41145
41146 class A {
41147 void foo (auto);
41148 };
41149 class B {
41150 friend void A::foo (auto);
41151 };
41152
41153 The template parameter list synthesized for the friend declaration
41154 must be injected in the scope of 'B'. This can also occur in
41155 erroneous cases such as:
41156
41157 struct A {
41158 struct B {
41159 void foo (auto);
41160 };
41161 void B::foo (auto) {}
41162 };
41163
41164 Here the attempted definition of 'B::foo' within 'A' is ill-formed
41165 but, nevertheless, the template parameter list synthesized for the
41166 declarator should be injected into the scope of 'A' as if the
41167 ill-formed template was specified explicitly. */
41168
41169 while (scope->kind == sk_class && !scope->defining_class_p)
41170 {
41171 parent_scope = scope;
41172 scope = scope->level_chain;
41173 }
41174 }
41175
41176 current_binding_level = scope;
41177
41178 if (scope->kind != sk_template_parms
41179 || !function_being_declared_is_template_p (parser))
41180 {
41181 /* Introduce a new template parameter list for implicit template
41182 parameters. */
41183
41184 become_template = true;
41185
41186 parser->implicit_template_scope
41187 = begin_scope (sk_template_parms, NULL);
41188
41189 ++processing_template_decl;
41190
41191 parser->fully_implicit_function_template_p = true;
41192 ++parser->num_template_parameter_lists;
41193 }
41194 else
41195 {
41196 /* Synthesize implicit template parameters at the end of the explicit
41197 template parameter list. */
41198
41199 gcc_assert (current_template_parms);
41200
41201 parser->implicit_template_scope = scope;
41202
41203 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41204 parser->implicit_template_parms
41205 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
41206 }
41207 }
41208
41209 /* Synthesize a new template parameter and track the current template
41210 parameter chain with implicit_template_parms. */
41211
41212 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
41213 tree synth_id = make_generic_type_name ();
41214 tree synth_tmpl_parm;
41215 bool non_type = false;
41216
41217 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
41218 synth_tmpl_parm
41219 = finish_template_type_parm (class_type_node, synth_id);
41220 else if (TREE_CODE (proto) == TEMPLATE_DECL)
41221 synth_tmpl_parm
41222 = finish_constrained_template_template_parm (proto, synth_id);
41223 else
41224 {
41225 synth_tmpl_parm = copy_decl (proto);
41226 DECL_NAME (synth_tmpl_parm) = synth_id;
41227 non_type = true;
41228 }
41229
41230 // Attach the constraint to the parm before processing.
41231 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
41232 TREE_TYPE (node) = constr;
41233 tree new_parm
41234 = process_template_parm (parser->implicit_template_parms,
41235 input_location,
41236 node,
41237 /*non_type=*/non_type,
41238 /*param_pack=*/false);
41239
41240 // Chain the new parameter to the list of implicit parameters.
41241 if (parser->implicit_template_parms)
41242 parser->implicit_template_parms
41243 = TREE_CHAIN (parser->implicit_template_parms);
41244 else
41245 parser->implicit_template_parms = new_parm;
41246
41247 tree new_decl = get_local_decls ();
41248 if (non_type)
41249 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
41250 new_decl = DECL_INITIAL (new_decl);
41251
41252 /* If creating a fully implicit function template, start the new implicit
41253 template parameter list with this synthesized type, otherwise grow the
41254 current template parameter list. */
41255
41256 if (become_template)
41257 {
41258 parent_scope->level_chain = current_binding_level;
41259
41260 tree new_parms = make_tree_vec (1);
41261 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
41262 current_template_parms = tree_cons (size_int (processing_template_decl),
41263 new_parms, current_template_parms);
41264 }
41265 else
41266 {
41267 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
41268 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
41269 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
41270 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
41271 }
41272
41273 // If the new parameter was constrained, we need to add that to the
41274 // constraints in the template parameter list.
41275 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
41276 {
41277 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
41278 reqs = conjoin_constraints (reqs, req);
41279 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
41280 }
41281
41282 current_binding_level = entry_scope;
41283
41284 return new_decl;
41285 }
41286
41287 /* Finish the declaration of a fully implicit function template. Such a
41288 template has no explicit template parameter list so has not been through the
41289 normal template head and tail processing. synthesize_implicit_template_parm
41290 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
41291 provided if the declaration is a class member such that its template
41292 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
41293 form is returned. Otherwise NULL_TREE is returned. */
41294
41295 static tree
41296 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
41297 {
41298 gcc_assert (parser->fully_implicit_function_template_p);
41299
41300 if (member_decl_opt && member_decl_opt != error_mark_node
41301 && DECL_VIRTUAL_P (member_decl_opt))
41302 {
41303 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
41304 "implicit templates may not be %<virtual%>");
41305 DECL_VIRTUAL_P (member_decl_opt) = false;
41306 }
41307
41308 if (member_decl_opt)
41309 member_decl_opt = finish_member_template_decl (member_decl_opt);
41310 end_template_decl ();
41311
41312 parser->fully_implicit_function_template_p = false;
41313 parser->implicit_template_parms = 0;
41314 parser->implicit_template_scope = 0;
41315 --parser->num_template_parameter_lists;
41316
41317 return member_decl_opt;
41318 }
41319
41320 /* Like finish_fully_implicit_template, but to be used in error
41321 recovery, rearranging scopes so that we restore the state we had
41322 before synthesize_implicit_template_parm inserted the implement
41323 template parms scope. */
41324
41325 static void
41326 abort_fully_implicit_template (cp_parser *parser)
41327 {
41328 cp_binding_level *return_to_scope = current_binding_level;
41329
41330 if (parser->implicit_template_scope
41331 && return_to_scope != parser->implicit_template_scope)
41332 {
41333 cp_binding_level *child = return_to_scope;
41334 for (cp_binding_level *scope = child->level_chain;
41335 scope != parser->implicit_template_scope;
41336 scope = child->level_chain)
41337 child = scope;
41338 child->level_chain = parser->implicit_template_scope->level_chain;
41339 parser->implicit_template_scope->level_chain = return_to_scope;
41340 current_binding_level = parser->implicit_template_scope;
41341 }
41342 else
41343 return_to_scope = return_to_scope->level_chain;
41344
41345 finish_fully_implicit_template (parser, NULL);
41346
41347 gcc_assert (current_binding_level == return_to_scope);
41348 }
41349
41350 /* Helper function for diagnostics that have complained about things
41351 being used with 'extern "C"' linkage.
41352
41353 Attempt to issue a note showing where the 'extern "C"' linkage began. */
41354
41355 void
41356 maybe_show_extern_c_location (void)
41357 {
41358 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
41359 inform (the_parser->innermost_linkage_specification_location,
41360 "%<extern \"C\"%> linkage started here");
41361 }
41362
41363 #include "gt-cp-parser.h"